Reading, Writing with audio device using ALSA, PulseAudio

In the past, it could be done using such devices as /dev/dsp, /dev/audio and some others. It is Open Sound System (OSS) interfaces. It is really old and is currently deprecated. But it is still possible to redirect information to the sound device. This can still be done by using PulseAudio that can emulate OSS devices.

Playing random noise using PulseAudio

The utility is called padsp. It is included in pulseaudio-utils package on Debian-based distributions. This is set as a dependency when installing pulseaudio from repository.

cat /dev/urandom | padsp tee /dev/dsp > /dev/null

In same manner it is possible to play files, images, html pages…

Writing to file using pulseaudio tools

This can be done with the following command:

padsp cat /dev/dsp > file.raw

Now we can listen to this with the following command:

cat file.raw | padsp tee /dev/dsp > /dev/null

But the sound quality is really bad. To improve the sound quality is better to use ALSA tools which we talk about below.

Playing random noise using ALSA

The utility is called aplay. It is included in alsa-utils package and may require installation if it is not installed yet.

sudo apt update
sudo apt install alsa-utils

Now it is possible to realize the following command:

cat /dev/urandom | aplay

aplay command requires user to be in audio group, otherwise it can be done by using with sudo.

Write to file from audio device ALSA

There is also arecord utility in alsa-utils package which allows us to write in files from audio device. Here is the simple use, :

arecord > radio.wav

Now it can be played by the following command:

cat radio.wav | aplay

The next technique is much better. Writing a file in WAVE format with CD quality settings (16 bit little endian, 44100, stereo):

arecord -t wav -f cd > radio.wav

Now it requires a media player:

mpv radio.wav

The sound quality is really good. This is more than enough to record music from the radio in high quality. But not all Internet radio broadcast music in high quality.

ffprobe radio.wav
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 2 channels, s16, 1411 kb/s.

Leave a Reply

Your email address will not be published.