Difference between revisions of "EBC Exercise 17 Using ALSA for Audio Processing"
m (moved ECE497 Lab11 Using ALSA for Audio Processing to ECE497 Lab11a Using ALSA for Audio Processing)
m (moved ECE497 Lab11a Using ALSA for Audio Processing to ECE497 Lab11 Using ALSA for Audio Processing over redirect)
Revision as of 13:32, 22 August 2011
This lab has three parts (a, b and c) we inspect the first two parts (recorder and playback) and then stitch the input driver to the output driver to create the loopthru application.
This lab demonstrate the Linux ALSA driver as well as basic file I/O. Parts a and b are inspection labs. While c requires you to combine the previous two parts.
- Part a analyze the function calls necessary to record audio from line input to a file.
- Part b examines the function calls necessary to playback audio from a recorded audio file.
- Part c combines a and b into a single application that loops the audio from input to output (i.e. audio loop-thru) without recording to a file. For an extra challenge, advanced users may want to try to build c without referring to the procedure.
Much of this lab was adapted for the Beagle from OMAP™/DaVinci™ System Integration using Linux Workshop.
Part a - Audio Record
The goal of this part is to analyze the function calls necessary to record audio from the Beagle's a line input to a file. Note: The 1/8 inch audio in jack on the Beagle is a line in jack, not a microphone in. It doesn't not have the extra amplification needed for a microphone.
- Copy the AudioThru files from here to your Beagle.
- Change directories to AudioThru/lab06a_audio_record
- List the files used to build this application:
- Use a text editor to examine the new files in this application.
$ gedit main.c
Other popular editors are emacs and gvim.
This is the entry point for the application. main() does the following:
- Creates a signal handler to trap the Ctrl-C signal (also called SIGINT, the interrupt signal). When this signal is sent to the application, the audioEnv.quit global variable is set to true to signal the audio thread to exit its main loop and begin cleanup.
- Calls the audio_thread_fxn() function to enter into the audio function.
- Upon completion of this function, the main routine checks – and reports – success or failure returned from the audio function.
audio_thread_fxn( ) encapsulates the code required to run the audio recorder. The lab06a_audio_recorder application is single-threaded, so the motivation for encapsulation in this manner may not be initially obvious. We will see in later labs – when combining audio and video in a multi-threaded program – why declaring this function (as opposed to running everything from main) is useful.
audio_thread_fxn utilizes the following:
- Opens and configures the audio input driver. See audio_input_output.c for details.
- will execute until the envPtr->quit global variable is set to true.
- Inside the while() loop, snd_pcm_readi() is used to read data from the audio input driver (ALSA) and fwrite() is used to write the data into a file.
- When the envPtr->quit variable is set to true (occurs when the user presses Ctrl-C in the terminal) this capture (record) process exits and the application proceeds to the cleanup phase before exiting.
It goes without saying, writing robust code – and debugging it – can be a tedious chore; it is further exasperated when using printf() statements as the primary means of providing debug information back to the programmer. To this end, we have employed an initMask to help keep track of resources opened (and closed) during the program.
The audio_thread_fxn() uses an initialization mask (initMask) to keep track of how many resources have been opened and initialized. Each bit in the mask corresponds to a resource; the bit positions in the initMask variable are #defined towards the top of the file.
/* The levels of initialization for initMask */ #define ALSA_INITIALIZED 0x1 #define INPUT_BUFFER_ALLOCATED 0x2 #define OUTPUT_FILE_OPENED 0x4 /* Only used to cleanup items that were initialized */ unsigned int initMask = 0x0;
- When you OR the initMask with a #define'd value, the associated bit will get set in the initMask variable. For example,
This is useful so that if an error occurs, the application will not attempt to close or free resources that were never opened or allocated. If you look down at the cleanup part of our audio_thread.c, you’ll see how we used the initMask variable to accomplish this.
Build and Run the application
- Build and install the application using make, i.e. “make all install”.
- Test your audio connection:
arecord –f cd | aplay –f cd
This command uses the arecord (“ALSA recorder”) utility to capture audio and, instead of sending to a file, uses a Linux process pipe to send the data to the aplay (“ALSA player”) application. This will loop audio through the board. If you have a working audio input and the board is connected to a speaker, you should hear the audio play over the speakers. Press ctrl-c to quit.
On arecord and aplay, the –f option lets you change the format:
- Execute the ./app_DEBUG.Beagle application.
The application is hard-coded (using a #define statement in audio_thread.c) to save the audio data to the file /tmp/audio.raw. Execute the application.
- Press Ctrl-C to exit the application.
After a suitable amount of time press Ctrl-C in the terminal to exit from the application. You can list the /tmp/audio.raw file with the –lsa options to see the size of the file and verify that it has recorded properly:
ls –lsa /tmp/audio.raw
Recall that a signal handler was placed in main.c to trap the SIGINT (Ctrl-C) signal. When Ctrl-C is placed, this signal handler will execute, signaling the audio thread to exit its main loop, proceed to cleanup, and then exit.
- Use the Linux aplay utility to confirm successful recording.
Because this application saves the audio as a raw stream you may also check that the record has operated properly using the aplay utility.
$ aplay –c 2 –f S16_LE –r 44100 /tmp/audio.raw
DBG vs ERR
Let’s explore the debugging features we’re using in our lab files. We are using two macros defined in the file debug.h. They are DBG() and ERR() – essentially, they are wrapper functions around an fprintf() function.
- Add a new debug statement to your file.
In main.c, immediately after the signal handler function, add a DBG() statement:
// Set the signal callback for Ctrl-C pSigPrev = signal( SIGINT, signal_handler ); DBG( "Registered SIGINT signal handler.\n" );
- Build (using “make all”) and run both debug and release profiles on the
Beagle, comparing their outputs.
Does your new statement show up in the terminal when you execute the program?
- Debug profile: Yes No ___________________________________________
- Release profile: Yes No ___________________________________________
- Switch from DBG() to ERR(), then once again, build, run and compare both profiles.
- What is the difference between DBG and ERR? ___________________________________
- Either Delete the new ERR() statement, or switch it back to DBG().
We don’t really need this statement, so feel free to remove it. On the other hand, if you want to leave the new debugging statement, we recommend that you, at the very least, change it back to a DBG() statement.