Introduction to Sound Programming with ALSA(2)

Listing 3. Simple Sound Playback  

 

  
  
  
  
  1. /*  
  2. This example reads standard from input and writes 
  3. to the default PCM device for 5 seconds of data. 
  4. */  
  5.  
  6. /* Use the newer ALSA API */ 
  7. #define ALSA_PCM_NEW_HW_PARAMS_API 
  8. #include <alsa/asoundlib.h> 
  9.  
  10. int main() { 
  11.   long loops; 
  12.   int rc; 
  13.   int size; 
  14.   snd_pcm_t *handle; 
  15.   snd_pcm_hw_params_t *params; 
  16.   unsigned int val; 
  17.   int dir; 
  18.   snd_pcm_uframes_t frames; 
  19.   char *buffer;  
  20.  
  21.   /* Open PCM device for playback. */ 
  22.   rc = snd_pcm_open(&handle, "default"
  23.                     SND_PCM_STREAM_PLAYBACK, 0); 
  24.  
  25.   if (rc < 0) { 
  26.     fprintf(stderr, 
  27.             "unable to open pcm device: %s\n"
  28.             snd_strerror(rc)); 
  29.     exit(1); 
  30.   } 
  31.  
  32.   /* Allocate a hardware parameters object. */ 
  33.   snd_pcm_hw_params_alloca(&params);  
  34.  
  35.   /* Fill it in with default values. */ 
  36.   snd_pcm_hw_params_any(handle, params);  
  37.  
  38.   /* Set the desired hardware parameters. */ 
  39.   /* Interleaved mode */ 
  40.   snd_pcm_hw_params_set_access(handle, params, 
  41.                       SND_PCM_ACCESS_RW_INTERLEAVED);  
  42.  
  43.   /* Signed 16-bit little-endian format */ 
  44.   snd_pcm_hw_params_set_format(handle, params, 
  45.                               SND_PCM_FORMAT_S16_LE);  
  46.  
  47.   /* Two channels (stereo) */ 
  48.   snd_pcm_hw_params_set_channels(handle, params, 2);  
  49.  
  50.   /* 44100 bits/second sampling rate (CD quality) */ 
  51.   val = 44100; 
  52.  
  53.   snd_pcm_hw_params_set_rate_near(handle, params, 
  54.                                   &val, &dir);  
  55.  
  56.   /* Set period size to 32 frames. */ 
  57.   frames = 32; 
  58.   snd_pcm_hw_params_set_period_size_near(handle, 
  59.                               params, &frames, &dir);  
  60.  
  61.   /* Write the parameters to the driver */ 
  62.   rc = snd_pcm_hw_params(handle, params); 
  63.   if (rc < 0) { 
  64.     fprintf(stderr, 
  65.             "unable to set hw parameters: %s\n"
  66.             snd_strerror(rc)); 
  67.     exit(1); 
  68.   } 
  69.  
  70.   /* Use a buffer large enough to hold one period */ 
  71.   snd_pcm_hw_params_get_period_size(params, &frames, 
  72.                                     &dir); 
  73.  
  74.   size = frames * 4; /* 2 bytes/sample, 2 channels */ 
  75.   buffer = (char *) malloc(size);  
  76.  
  77.   /* We want to loop for 5 seconds */ 
  78.   snd_pcm_hw_params_get_period_time(params, 
  79.                                     &val, &dir); 
  80.   /* 5 seconds in microseconds divided by 
  81.    * period time */ 
  82.   loops = 5000000 / val;  
  83.  
  84.   while (loops > 0) { 
  85.     loops--; 
  86.     rc = read(0, buffer, size); 
  87.     if (rc == 0) { 
  88.       fprintf(stderr, "end of file on input\n"); 
  89.       break
  90.     } else if (rc != size) { 
  91.       fprintf(stderr, 
  92.               "short read: read %d bytes\n", rc); 
  93.     } 
  94.  
  95.     rc = snd_pcm_writei(handle, buffer, frames); 
  96.     if (rc == -EPIPE) { 
  97.       /* EPIPE means underrun */ 
  98.       fprintf(stderr, "underrun occurred\n"); 
  99.       snd_pcm_prepare(handle); 
  100.     } else if (rc < 0) { 
  101.       fprintf(stderr, 
  102.               "error from writei: %s\n"
  103.               snd_strerror(rc)); 
  104.     }  else if (rc != (int)frames) { 
  105.       fprintf(stderr, 
  106.               "short write, write %d frames\n", rc); 
  107.     } 
  108.   } 
  109.   snd_pcm_drain(handle); 
  110.   snd_pcm_close(handle); 
  111.   free(buffer);  
  112.  
  113.   return 0; 

 

Listing 3 extends the previous example by writing sound samples to the sound card to produce playback. In this case we read bytes from standard input, enough for one period, and write them to the sound card until five seconds of data has been transferred.

The beginning of the program is the same as in the previous example—the PCM device is opened and the hardware parameters are set. We use the period size chosen by ALSA and make this the size of our buffer for storing samples. We then find out that period time so we can calculate how many periods the program should process in order to run for five seconds.

In the loop that manages data, we read from standard input and fill our buffer with one period of samples. We check for and handle errors resulting from reaching the end of file or reading a different number of bytes from what was expected.

To send data to the PCM device, we use the snd_pcm_writei call. It operates much like the kernel write system call, except that the size is specified in frames. We check the return code for a number of error conditions. A return code of EPIPE indicates that underrun occurred, which causes the PCM stream to go into the XRUN state and stop processing data. The standard method to recover from this state is to use the snd_pcm_prepare function call to put the stream in the PREPARED state so it can start again the next time we write data to the stream. If we receive a different error result, we display the error code and continue. Finally, if the number of frames written is not what was expected, we display an error message.

The program loops until five seconds' worth of frames has been transferred or end of file read occurs on the input. We then call snd_pcm_drain to allow any pending sound samples to be transferred, then close the stream. We free the dynamically allocated buffer and exit.

We should see that the program is not useful unless the input is redirected to something other than a console. Try running it with the device /dev/urandom, which produces random data, like this:  

./example3 < /dev/urandom 

The random data should produce white noise for five seconds.

Next, try redirecting the input to /dev/null or /dev/zero and compare the results. Change some parameters, such as the sampling rate and data format, and see how it affects the results.

Listing 4. Simple Sound Recording 

 

  
  
  
  
  1. /* 
  2. This example reads from the default PCM device 
  3. and writes to standard output for 5 seconds of data. 
  4. */  
  5.  
  6. /* Use the newer ALSA API */ 
  7. #define ALSA_PCM_NEW_HW_PARAMS_API 
  8. #include <alsa/asoundlib.h> 
  9.  
  10. int main() { 
  11.   long loops; 
  12.   int rc; 
  13.   int size; 
  14.   snd_pcm_t *handle; 
  15.   snd_pcm_hw_params_t *params; 
  16.   unsigned int val; 
  17.   int dir; 
  18.   snd_pcm_uframes_t frames; 
  19.   char *buffer;  
  20.  
  21.   /* Open PCM device for recording (capture). */ 
  22.   rc = snd_pcm_open(&handle, "default"
  23.                     SND_PCM_STREAM_CAPTURE, 0); 
  24.   if (rc < 0) { 
  25.     fprintf(stderr, 
  26.             "unable to open pcm device: %s\n"
  27.             snd_strerror(rc)); 
  28.     exit(1); 
  29.   } 
  30.  
  31.   /* Allocate a hardware parameters object. */ 
  32.   snd_pcm_hw_params_alloca(&params);  
  33.  
  34.   /* Fill it in with default values. */ 
  35.   snd_pcm_hw_params_any(handle, params);  
  36.  
  37.   /* Set the desired hardware parameters. */ 
  38.   /* Interleaved mode */ 
  39.   snd_pcm_hw_params_set_access(handle, params, 
  40.                       SND_PCM_ACCESS_RW_INTERLEAVED);  
  41.  
  42.   /* Signed 16-bit little-endian format */ 
  43.   snd_pcm_hw_params_set_format(handle, params, 
  44.                               SND_PCM_FORMAT_S16_LE);  
  45.  
  46.   /* Two channels (stereo) */ 
  47.   snd_pcm_hw_params_set_channels(handle, params, 2);  
  48.  
  49.   /* 44100 bits/second sampling rate (CD quality) */ 
  50.   val = 44100; 
  51.   snd_pcm_hw_params_set_rate_near(handle, params, 
  52.                                   &val, &dir);  
  53.  
  54.   /* Set period size to 32 frames. */ 
  55.   frames = 32; 
  56.  
  57.   snd_pcm_hw_params_set_period_size_near(handle, 
  58.                               params, &frames, &dir);  
  59.  
  60.   /* Write the parameters to the driver */ 
  61.   rc = snd_pcm_hw_params(handle, params); 
  62.   if (rc < 0) { 
  63.     fprintf(stderr, 
  64.             "unable to set hw parameters: %s\n"
  65.             snd_strerror(rc)); 
  66.     exit(1); 
  67.   }  
  68.  
  69.   /* Use a buffer large enough to hold one period */ 
  70.   snd_pcm_hw_params_get_period_size(params, 
  71.                                       &frames, &dir); 
  72.  
  73.   size = frames * 4; /* 2 bytes/sample, 2 channels */ 
  74.  
  75.   buffer = (char *) malloc(size);  
  76.  
  77.   /* We want to loop for 5 seconds */ 
  78.   snd_pcm_hw_params_get_period_time(params, 
  79.                                          &val, &dir); 
  80.   loops = 5000000 / val;  
  81.  
  82.   while (loops > 0) { 
  83.     loops--; 
  84.     rc = snd_pcm_readi(handle, buffer, frames); 
  85.     if (rc == -EPIPE) { 
  86.       /* EPIPE means overrun */ 
  87.       fprintf(stderr, "overrun occurred\n"); 
  88.       snd_pcm_prepare(handle); 
  89.     } else if (rc < 0) { 
  90.       fprintf(stderr, 
  91.               "error from read: %s\n"
  92.               snd_strerror(rc)); 
  93.     } else if (rc != (int)frames) { 
  94.       fprintf(stderr, "short read, read %d frames\n", rc); 
  95.     } 
  96.  
  97.     rc = write(1, buffer, size); 
  98.     if (rc != size) 
  99.       fprintf(stderr, 
  100.               "short write: wrote %d bytes\n", rc); 
  101.   }  
  102.  
  103.   snd_pcm_drain(handle); 
  104.   snd_pcm_close(handle); 
  105.   free(buffer); 
  106.  
  107.   return 0; 

Listing 4 is much like Listing 3, except that we perform PCM capture (recording). When we open the PCM stream, we specify the mode as SND_PCM_STREAM_CAPTURE. In the main processing loop, we read the samples from the sound hardware using snd_pcm_readi and write it to standard output using write. We check for overrun and handle it in the same manner as we did underrun in Listing 3.

Running Listing 4 records approximately five seconds of data and sends it to standard out; you should redirect it to a file. If you have a microphone connected to your sound card, use a mixer program to set the recording source and level. Alternatively, you can run a CD player program and set the recording source to CD. Try running Listing 4 and redirecting the output to a file. You then can run Listing 3 to play back the data:  

./listing4 > sound.raw

./listing3 < sound.raw 

If your sound card supports full duplex sound, you should be able to pipe the programs together and hear the recorded sound coming out of the sound card by typing: ./listing4 | ./listing3. By changing the PCM parameters you can experiment with the effect of sampling rates and formats.

Advanced Features

In the previous examples, the PCM streams were operating in blocking mode, that is, the calls would not return until the data had been transferred. In an interactive event-driven application, this situation could lock up the application for unacceptably long periods of time. ALSA allows opening a stream in nonblocking mode where the read and write functions return immediately. If data transfers are pending and the calls cannot be processed, ALSA returns an error code of EBUSY.

Many graphical applications use callbacks to handle events. ALSA supports opening a PCM stream in asynchronous mode. This allows registering a callback function to be called when a period of sample data has been transferred.

The snd_pcm_readi and snd_pcm_writei calls used here are similar to the Linux read and write system calls. The letter i indicates that the frames are interleaved; corresponding functions exist for non-interleaved mode. Many devices under Linux also support the mmap system call, which maps them into memory where they can be manipulated with pointers. Finally, ALSA supports opening a PCM channel in mmap mode, which allows efficient zero copy access to sound data.

Conclusion

I hope this article has motivated you to try some ALSA programming. As the 2.6 kernel becomes commonly used by Linux distributions, ALSA should become more widely used, and its advanced features should help Linux audio applications move forward.

My thanks to Jaroslav Kysela and Takashi Iwai for reviewing a draft of this article and providing me with useful comments.

你可能感兴趣的:(programming,Introduction,休闲,sound,alsa)