gtkIOStream is a versatile software for signal processing and audio processing, amongst other features (such as GUI programming). It interfaces directly with ALSA and or jackd for low latency audio processing and routing. gtkIOStream also provides a nice port GUI for jackd.

This tutorial targets ALSA capture and SoX file writing. In this tutorial we will introduce the bare minimum requirements for ALSA capture and we will write the audio out to file so that we can test it worked. You can use this template to insert your own signal processing algorithms if you like.

Before we start, you have to have compiled and installed gtkIOStream. For certain components of gtkIOStream its compilation and installation isn't necessary, but for certain parts of audio processing it is necessary. Tutorial 0 and contents guides you through how to install and test the installation of gtkIOStream. Before you start this tutorial, you must have it installed first.

Our first step is to open the standard lightweight Raspberry Pi IDE with a blank file for us to code up in ... like so :

Again, we calculate the maximum sample value using knowledge of the number of bits per sample - we should strictly check to see that the sample type is signed here, or else we are clipping for very large values - however the default sample type is signed.

Before we can start playing we want to know the period size and also we want to put the ALSA device into the prepared state, which we do like so :

The setParams method attempts to write the hardware params to the device and we check to see that we are in the expected prepared state. If not then we print an error and return. We should strictly use the ALSADebug::evaluateError method to get meaningful information on why there was an error, which we will demonstrate in a few lines below.

Great - so now the capture device is open and in the prepared state ... all we need to do is get some memory for audio reading/writing, start the device and loop writing the captured audio to file.

Here we use the Eigen Array type because perhaps we will want to do some signal processing on the audio buffer. You will notice it has period sized number of rows and enough rows to hold all of the audio channels.

Here we give a contextual error report if there is a failure and return that error.

Finally we do our capture write loop where first we work out how many frames (samples per channel) we need to grab (N) which is the number of samples per second (fs which is the sample rate in Hz) multiplied by the duration in seconds (SI units right !) :

We enter the while loop, capture audio into the buffer (using the operator>>), write the audio to file using sox and decrement the required number of frames by the number of rows captured.

So you see it is pretty trivial at this point, simply capture to a buffer, write to file or do some signal processing first before writing.

Finally, we drop and stop the ALSA capture device so there are no over runs (a term used for not reading a real time buffer quickly enough and it "over flowing"). We also close the audio file and return 0 on exit.

There is a lot of detail printed out, however all of the significant and desired information is present. You will notice that we are using by default a signed 16 (S16_LE) sample format, with a sample rate of 48 kHz and 2 channels. The period size by default is 256 samples which is around 5 ms. This is nice and safe and should pose little problems, may even be able to compress and encode in real time by changing the file name to something like /tmp/test.ogg or flac or whatever you want - that would take some experimentation.

and you can open the file in audacity and see what it looks like, it should look like stereo waveforms :

ALSACapture audio file opened in audacity.

test.wav.png (49.71 KiB) Viewed 3857 times

If you don't record anything (waveforms are zero) then you have to check your alsamixer. In the case of the Audio Injector cards, to capture from the input RCA lines and not the microphone, your capture alsamixer page should look like this :

Thank you for the tutorials. They helped create a great starting point for my project and also helped test my hardware and software setup.
The only two steps that I had to do in addition to your instructions was to install sox and GTK2. I used the sudo apt-get command to install them and got back on to the track.

Hi Matt,
Yes, this was part of the tutorials. I followed tutorial 0 step by step and found that I had to do these two additional steps to make it work.
It is not a big deal, wanted to mention it so that it can help others.