Recording audio data

You can record audio content in mm-renderer by attaching the input to an audio capture device and directing the output to a file instead of a device.

Note: The mm-renderer utility can record audio but not video. To record video content, use the Camera API to open a video file and start encoding video content, as explained in Camera Library.

The following sample program shows how to record audio captured from a microphone and store it into a file. The program gives mm-renderer an input URL of type snd: to select and configure an audio capture device (microphone) and sets an output URL type of file: to target a file. The program starts and stops playback to record captured audio content to the targeted file. The snd: input URL format works only with the file: output type, so your code must follow this design.

You can record audio content for as long as you like, but you must ensure your client application's output file can hold all the content you want to capture. The size of the generated output depends on many settings, including the sampling rate and number of channels. This sample program records in mono by specifying one channel (nchan=1) in the input URL. Depending on your platform, your microphone device might have two recorders, so you could record in stereo by setting two channels (nchan=2). You could also increase the sampling rate to attain the necessary audio quality, such as using the standard CD sampling rate of 44.1 MHz (frate=44100000). For more information on the available device options, see the list of URL parameters for audio capture devices.

Note: You can modify the sample program to record a voice call by using "snd:/dev/snd/voicebandc" as the input URL to mmr_input_attach() instead of "snd:/dev/snd/pcmPreferredc?nchan=1&frate=8000". Unless you specify different values, mm-renderer uses a 48 MHz sampling rate and 2 channels, which is equivalent to using "snd:/dev/snd/voicebandc?frate=48000&nchan=2" as the input URL.

This code sample names an AMR file for the output, but mm-renderer supports other formats, such as wideband AMR (see the list of supported output file formats).

Note: The BlackBerry 10 Device Simulator supports only .wav files for the output. To record audio content to other file formats (including .amr files), you must use real hardware.
void record_AMR_file() 
{
  mmr_connection_t *connection;
  mmr_context_t *context;
  const char* context_name = "AnyNameYouWant";
  int output = 0;
  const char* outputFile = "/tmp/testFile.amr";
  int input = 0;

  connection = mmr_connect(NULL);

  if (connection) {
      context = mmr_context_create( connection, 
                                    context_name, 
                                    0, 
                                    S_IRWXU );

      if (context) {
          // specify a file output so the audio content is 
          // not played but recorded in a file
          output = mmr_output_attach( context,
                                      outputFile,
                                      "file" );
                                     
          // specify the audio device under /dev/snd you want to 
          // use for the recording, and the recording details 
          // (in this case, we use a sampling rate of 8000 Hz and 
          // 1 channel for mono (not stereo) recording)
          input = mmr_input_attach( context,
                  "snd:/dev/snd/pcmPreferredc?nchan=1&frate=8000",
                                   "track" );

          // start recording
          mmr_play(context);

          // delay for the length of time you want to record 
          // (in this case, 30 seconds)
          sleep(30);             

          // stop recording
          mmr_stop(context);
          
          // clean up the context
          mmr_input_detach(context);
          mmr_output_detach(context, output); 
          mmr_context_destroy(context); 
      }
      mmr_disconnect(connection); 
  } // if (connection)
} // function