This article is the third article of audio capturing, encoding and streaming. It describes how to encode capture PCM data to AMR encoding and write to audio file.
It is for software developers who are interested in audio related applications. Mostly suits for Audio software developers who are interested in encoding and decoding audio files to AMR Narrow Band encoding. It is not required to know AMR encoding and decoding details unless one wants to decode captured data manually.
Total application is written in "C" language and consists of simple functions and simple port audio library and
opencore-amr library calls. Anyone who has been working in for quite a time can understand the program.
Port-Audio is a free, cross-platform, open-source, audio I/O library. It lets you write simple audio programs in 'C' or C++ that will compile and run on many platforms including Windows, Macintosh OS X, and Unix (OSS/ALSA). It is intended to promote the exchange of audio software between developers on different platforms. Port Audio software has many useful examples.
Official web site is http://www.portaudio.com.
Library of OpenCORE Framework implementation of Adaptive Multi Rate Narrowband and Wideband (AMR-NB and AMR-WB) speech codec. Library of VisualOn implementation of Adaptive Multi Rate Wideband (AMR-WB) encoder and Advanced Audio Coding (AAC) encoder.
Official opencore amr web site is http://sourceforge.net/projects/opencore-amr/ or http://opencore-amr.sourceforge.net/.
AMR Encoding Data flow Diagram
Data flow diagram is as follows:
For more details on port audio calls, see the port-audio documentation, and AMR narrow band, see opencore-amr documentation.
Step 1: Initialize Port Audio and Register Record Callback Function
Initialize the port audio and open port audio stream with record call back function with the following configurations 8000 sampling rate, 16 bit signed PCM data, frame size of 80 samples mean 10 milliseconds audio data in one frame. After stream is started, record call back is called for every 10 ms with one frame of audio data.
Step 2: Initialize AMR-NB Library
opencore-amr library with proper configuration parameters to match port-audio capture configuration.
opencore-amr encoding interface is initialized by calling its
Step 3: Encode Raw Audio Frame
When port-audio record callback is called, raw audio data is stored input buffer, pass the buffer and length to amr encoder, amr encoder returns encoded data.
Encoder_Interface_Encode function, raw audio data is converted to amr-nb encoded data.
Step 4: Write to File
Amr-nb audio has signature header to identify the binary file as amr audio file. Without this AMR signature, amr audio players cannot play the audio file.
Open file pointer and first write AMR signature "
#!AMR\n" to binary file and continuously write AMR encoded data. Close the file pointer after completing encoding all audio frames.
Step 6: Close opencore-amr Library
Close all resources related to amr library. opencore exit function is as follows
Step 7: Stop, Close Audio Stream and Close Port Audio
Close stream using
The following software are required to execute and listen to captured audio file:
- VC++, portaudio library/dll
- opencore amr library/dll and
- ant amr-nb capable audio player (VLC)
Points of Interest
- Simple AMR encoding sample