mp3是流媒体,所以一个完整文件往往比较大而且不能一次装入sound缓存,所以其buffer管理就成了最大难题,至于解码部分其实还是很简单的,下面是仅关于解码部分的说明
首先应该在自己的工程中包含以下三个库:
libid3tag-0.15.1b
libmad-0.15.1b
libz-1.1.4
必要的三个结构体及相关的初始化
- mad_stream stream;
- mad_frame frame;
- mad_synth synth;
- mad_stream_init(&stream);
- mad_frame_init(&frame);
- mad_synth_init(&synth);
将stream和buffer关联起来
- mad_stream_buffer(&stream, buffer, buffer_size);
对压缩数据进行解码,这里将控制着一个双层的循环逻辑
- mad_frame_decode(&frame, &stream);
如果mad_frame_decode返回-1,这说明这里存在错误这需要对特定的错误进行处理
当错误代码为!MAD_RECOVERABLE(stream.error),则需要从新更新流媒体buffer
当错误代码为MAD_ERROR_BADDATAPTR时需要再进行decode
当错误代码为MAD_ERROR_LOSTSYNC则需要进行id3tag跳帧,并再次decode
- id3_tag_query(stream.this_frame, stream.bufend - stream.this_frame);
- if(tagsize > 0)
- {
- mad_stream_skip(&stream, tagsize);
- }
将解码数据转换为设备所能接受的pcm数据
- mad_synth_frame(&synth, &frame);
但是即使是pcm数据依然不能直接播放,还需要更具设备的支持能力进行相应的线性插值,转换成8位、16位、及32位…… 双声道的转换方式
- for(int i = 0; i < (int)synth.pcm.length; i++)
- {
- sample0 = audio_linear_dither(16, synth.pcm.samples[0][i], &left_dither, &stats);
- sample1 = audio_linear_dither(16, synth.pcm.samples[1][i], &right_dither, &stats);
- tempBuffer[0] = sample0 >> 0;
- tempBuffer[1] = sample0 >> 8;
- tempBuffer[2] = sample1 >> 0;
- tempBuffer[3] = sample1 >> 8;
- }
单声道的转换方式
- register int sample0;
- for(int i = 0; i < (int)synth.pcm.length; i++)
- {
- sample0 = audio_linear_dither(16, synth.pcm.samples[0][i], &left_dither, &stats);
- tempBuffer[0] = sample0 >> 0;
- tempBuffer[1] = sample0 >> 8;
- }
当这个转换后的pcm数据积累到一定程度就可以拷贝到设备缓存区进行播放了这里省略不同设备的创建及播放代码,要注意的是创建设备时需要的音频信息是从synth.pcm中获得的,如channels,samplerate,……
最后不要忘记如何关闭相关资源
- mad_synth_finish(&synth);
- mad_frame_finish(&frame);
- mad_stream_finish(&stream);
再举例一个基于looping结构的direct sound buffer管理机制:
详细代码可以参考我写的一个工程: