- int snd_pcm_hw_params_malloc (snd_pcm_hw_params_t **ptr)
- int snd_pcm_hw_params_any (snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
- void snd_pcm_hw_params_free (snd_pcm_hw_params_t *obj)
- int snd_pcm_hw_params_set_access ( snd_pcm_t *pcm,
- snd_pcm_hw_params_t *params,
- snd_pcm_access_t _access)
- int snd_pcm_hw_params_set_format ( snd_pcm_t *pcm,
- snd_pcm_hw_params_t *params,
- snd_pcm_format_t val)
- int snd_pcm_hw_params_set_channels(snd_pcm_t *pcm,
- snd_pcm_hw_params_t *params,
- unsigned int val)
- int snd_pcm_hw_params_set_rate_near(snd_pcm_t *pcm,
- snd_pcm_hw_params_t *params,
- unsigned int *val, int *dir)
- int snd_pcm_hw_params (snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
- int snd_pcm_prepare (snd_pcm_t *pcm)
- int snd_pcm_open (snd_pcm_t **pcm, const char *name,
- snd_pcm_stream_t stream, int mode)
- int snd_pcm_close (snd_pcm_t *pcm)
- snd_pcm_sframes_t snd_pcm_writei (snd_pcm_t *pcm,
- const void *buffer, snd_pcm_uframes_t size)
- SND_PCM_ACCESS_RW_INTERLEAVED
- SND_PCM_ACCESS_RW_NONINTERLEAVED
- SND_PCM_FORMAT_S16_LE 有符号16 bit Little Endian
- SND_PCM_FORMAT_S16_BE 有符号16 bit Big Endian
- SND_PCM_FORMAT_U16_LE 无符号16 bit Little Endian
- SND_PCM_FORMAT_U16_BE 无符号 16 bit Big Endian
- 比如对于 32-bit 长度的采样数据可以设置为:
- SND_PCM_FORMAT_S32_LE 有符号32 bit Little Endian
- SND_PCM_FORMAT_S32_BE 有符号32 bit Big Endian
- SND_PCM_FORMAT_U32_LE 无符号32 bit Little Endian
- SND_PCM_FORMAT_U32_BE 无符号 32 bit Big Endian
- /* open a PCM device */
- int open_device(struct mad_header const *header)
- {
- int err;
- snd_pcm_hw_params_t *hw_params;
- char *pcm_name = "plughw:0,0";
- int rate = header->samplerate;
- int channels = 2;
- if (header->mode == 0) {
- channels = 1;
- } else {
- channels = 2;
- }
- if ((err = snd_pcm_open (&playback_handle,
- pcm_name, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
- printf("cannot open audio device %s (%s) ",
- pcm_name,
- snd_strerror (err));
- return -1;
- }
- if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
- printf("cannot allocate hardware parameter structure (%s) ",
- snd_strerror (err));
- return -1;
- }
- if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
- printf("cannot initialize hardware parameter structure (%s) ",
- snd_strerror (err));
- return -1;
- }
- if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params,
- SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
- printf("cannot set access type (%s) ",
- snd_strerror (err));
- return -1;
- }
- if ((err = snd_pcm_hw_params_set_format (playback_handle,
- hw_params, SND_PCM_FORMAT_S32_LE)) < 0) {
- printf("cannot set sample format (%s) ",
- snd_strerror (err));
- return -1;
- }
- if ((err = snd_pcm_hw_params_set_rate_near (playback_handle,
- hw_params, &rate, 0)) < 0) {
- printf("cannot set sample rate (%s) ",
- snd_strerror (err));
- return -1;
- }
- if ((err = snd_pcm_hw_params_set_channels (playback_handle,
- hw_params, channels)) < 0) {
- printf("cannot set channel count (%s) ",
- snd_strerror (err));
- return -1;
- }
- if ((err = snd_pcm_hw_params (playback_handle,
- hw_params)) < 0) {
- printf("cannot set parameters (%s) ",
- snd_strerror (err));
- return -1;
- }
- snd_pcm_hw_params_free (hw_params);
- if ((err = snd_pcm_prepare (playback_handle)) < 0) {
- printf("cannot prepare audio interface for use (%s) ",
- snd_strerror (err));
- return -1;
- }
- return 0;
- }
- while (nsamples--) {
- /* nsamples 是采样的数目 */
- signed int sample;
- sample = pcm->samples[0][j];
- *(OutputPtr++) = sample & 0xff;
- *(OutputPtr++) = (sample >> 8);
- *(OutputPtr++) = (sample >> 16);
- *(OutputPtr++) = (sample >> 24);
- if (nchannels == 2) {
- sample = pcm->samples[1][j];
- *(OutputPtr++) = sample & 0xff;
- *(OutputPtr++) = sample >> 8;
- *(OutputPtr++) = (sample >> 16);
- *(OutputPtr++) = (sample >> 24);
- }
- j++;
- }
- if ((err = snd_pcm_writei (playback_handle, buf, samples)) < 0) {
- err = xrun_recovery(playback_handle, err);
- if (err < 0) {
- printf("Write error: %s ", snd_strerror(err));
- return -1;
- }
- }
- int xrun_recovery(snd_pcm_t *handle, int err)
- {
- if (err == -EPIPE) { /* under-run */
- err = snd_pcm_prepare(handle);
- if (err < 0)
- printf("Can't recovery from underrun, prepare failed: %s ",
- snd_strerror(err));
- return 0;
- } else if (err == -ESTRPIPE) {
- while ((err = snd_pcm_resume(handle)) == -EAGAIN)
- sleep(1); /* wait until the suspend flag is released */
- if (err < 0) {
- err = snd_pcm_prepare(handle);
- if (err < 0)
- printf("Can't recovery from suspend, prepare failed: %s ",
- snd_strerror(err));
- }
- return 0;
- }
- return err;
- }
- int mp3_decode_buf(char *input_buf, int size)
- {
- int decode_over_flag = 0;
- int remain_bytes = 0;
- int ret_val = 0;
- mad_stream_buffer(&decode_stream, input_buf, size);
- decode_stream.error = MAD_ERROR_NONE;
- while (1)
- {
- if (decode_stream.error == MAD_ERROR_BUFLEN) {
- if (decode_stream.next_frame != NULL) {
- remain_bytes = decode_stream.bufend - decode_stream.next_frame;
- memcpy(input_buf, decode_stream.next_frame, remain_bytes);
- return remain_bytes;
- }
- }
- ret_val = mad_frame_decode(&decode_frame, &decode_stream);
- /* 省略部分代码 */
- ...
- if (ret_val == 0) {
- if (play_frame(&decode_frame) == -1) {
- return -1;
- }
- }
- /* 后面代码省略 */
- ...
- }
- return 0;
- }
recommend from :http://www.ibm.com/developerworks/cn/linux/l-cn-libmadmp3player/index.html |