1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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\n", snd_strerror(err)); return -1; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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\n", 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\n", snd_strerror(err)); } return 0; } return err; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |