libmad:是一个开源的高精度mpeg音频解码库,支持 MPEG-1(Layer I, Layer II 和 LayerIII(也就是 MP3)。LIBMAD 提供 24-bit 的 PCM 输出,完全是定点计算,非常适合没有浮点支持的平台上使用。使用 libmad 提供的一系列 API,就可以非常简单地实现 MP3 数据解码工作。在 libmad 的源代码文件目录下的 mad.h 文件中,可以看到绝大部分该库的数据结构和 API 等。
PCM编码:即为脉冲代码调制编码。
PCM通过抽样,量化,编码三个步骤将连续的模拟信号转换成数字编码。
libmad中的主要数据结构:
主要数据结构 | 作用 |
struct mad_stream | 存放解码前的Bitstream数据 |
struct mad_synth | 存放解码合成滤波后的PCM数据 |
struct mad_pcm | 定义了音频的采样率,声道个数和PCM采样数据,用来初始化音频 |
struct mad_frame | 记录MPEG帧解码后PCM数据的数据结构,其中的mad_header用来记录MPEG帧的基本信息,比如MPEG层数、声道模式、流比特率、采样比特率。声道模式包括单声道、双声道、联合立体混音道以及一般立体声。 |
MAD通过回调函数机制来实现解码,每个回调函数会返回一个枚举类型mad_flow,通过mad_flow可以控制解码的过程。在未经处理的情况下,MAD一般输出32bit,以little endian格式存放在mad_fixed_t中的数据。但是大多数的声卡并能支持输出高达32bit精度的数据,因而还必须对mad_fixed_t进行量化,圆滑处理以及抖动,使到采样信号降到16bit精度。MAD负责的只是解码的过程,它工作过程是:从外部获取输入,逐帧解码,在解码的过程中返回信息,然后得到解码结果。开发人员要手动设置输入输出。
在libmad中提供了一个解码源程序minimad.c,实现了将MP3文件解码成pcm数据,并将其数据显示在终端上。
现在就以该源码程序为例,来写出我们自己的基于libmad的MP3播放器。
在我们打开我们的音频程序之时同时也打开我们的音频设备"/dev/dsp"。
- static int sfd;
- if((sfd = open("/dev/dsp", O_WRONLY)) < 0)
- {
- printf("can not open device!!!/n");
- return 1;
- }
一般来说,我们的MP3文件都是立体音,有2个声道,由于要把pcm采样后并处理的数据放入一个char型的数组,而并行的左右声道的每个采样要在字符数组中处理成2个,所以字符数组中的数据的个数应该是pcm音频采样数的4倍。又因为把左右声道的数据合在一个字符数组里串行处理,所以播放的速度应该是pcm音频采样率的两倍。
这样就可以实现我们的播放器了.....
- static
- enum mad_flow output(void *data,
- struct mad_header const *header, struct mad_pcm *pcm)
- {
- unsigned int nchannels, nsamples, n;
- mad_fixed_t const *left_ch, *right_ch;
- unsigned char Output[6912], *OutputPtr;
- int fmt, wrote, speed;
- nchannels = pcm->channels;
- n = nsamples = pcm->length;
- left_ch = pcm->samples[0];
- right_ch = pcm->samples[1];
- fmt = AFMT_S16_LE;
- speed = pcm->samplerate * 2; /*播放速度是采样率的两倍 */
- ioctl(sfd, SNDCTL_DSP_SPEED, &(speed));
- ioctl(sfd, SNDCTL_DSP_SETFMT, &fmt);
- ioctl(sfd, SNDCTL_DSP_CHANNELS, &(pcm->channels));
- OutputPtr = Output;
- while (nsamples--) {
- signed int sample;
- sample = scale(*left_ch++);
- *(OutputPtr++) = sample >> 0;
- *(OutputPtr++) = sample >> 8;
- if (nchannels == 2) {
- sample = scale(*right_ch++);
- *(OutputPtr++) = sample >> 0;
- *(OutputPtr++) = sample >> 8;
- }
- }
- n *= 4; /*数据长度为pcm音频采样的4倍 */
- OutputPtr = Output;
- while (n) {
- wrote = write(sfd, OutputPtr, n);
- OutputPtr += wrote;
- n -= wrote;
- }
- OutputPtr = Output;
- return MAD_FLOW_CONTINUE;
- }
下面就以一个简单的实例来说明问题:
- # include
- # include
- # include
- # include
- # include
- # include
- # include
- # include
- # include
- # include
- struct buffer {
- unsigned char const *start;
- unsigned long length;
- };
- static int sfd; /*声音设备的描述符 */
- static int decode(unsigned char const *, unsigned long);
- int main(int argc, char *argv[])
- {
- struct stat stat;
- void *fdm;
- char const *file;
- int fd;
- file = argv[1];
- fd = open(file, O_RDONLY);
- if ((sfd = open("/dev/dsp", O_WRONLY)) < 0) {
- printf("can not open device!!!/n");
- return 5;
- }
- ioctl(sfd, SNDCTL_DSP_SYNC, 0); /*此句可以不要 */
- if (fstat(fd, &stat) == -1 || stat.st_size == 0)
- return 2;
- fdm = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
- if (fdm == MAP_FAILED)
- return 3;
- decode(fdm, stat.st_size);
- if (munmap(fdm, stat.st_size) == -1)
- return 4;
- ioctl(sfd, SNDCTL_DSP_RESET, 0);
- close(sfd);
- return 0;
- }
- static
- enum mad_flow input(void *data, struct mad_stream *stream)
- {
- struct buffer *buffer = data;
- if (!buffer->length)
- return MAD_FLOW_STOP;
- mad_stream_buffer(stream, buffer->start, buffer->length);
- buffer->length = 0;
- return MAD_FLOW_CONTINUE;
- }
- /*这一段是处理采样后的pcm音频 */
- static inline signed int scale(mad_fixed_t sample)
- {
- sample += (1L << (MAD_F_FRACBITS - 16));
- if (sample >= MAD_F_ONE)
- sample = MAD_F_ONE - 1;
- else if (sample < -MAD_F_ONE)
- sample = -MAD_F_ONE;
- return sample >> (MAD_F_FRACBITS + 1 - 16);
- }
- static
- enum mad_flow output(void *data,
- struct mad_header const *header, struct mad_pcm *pcm)
- {
- unsigned int nchannels, nsamples, n;
- mad_fixed_t const *left_ch, *right_ch;
- unsigned char Output[6912], *OutputPtr;
- int fmt, wrote, speed;
- nchannels = pcm->channels;
- n = nsamples = pcm->length;
- left_ch = pcm->samples[0];
- right_ch = pcm->samples[1];
- fmt = AFMT_S16_LE;
- speed = pcm->samplerate * 2; /*播放速度是采样率的两倍 */
- ioctl(sfd, SNDCTL_DSP_SPEED, &(speed));
- ioctl(sfd, SNDCTL_DSP_SETFMT, &fmt);
- ioctl(sfd, SNDCTL_DSP_CHANNELS, &(pcm->channels));
- OutputPtr = Output;
- while (nsamples--) {
- signed int sample;
- sample = scale(*left_ch++);
- *(OutputPtr++) = sample >> 0;
- *(OutputPtr++) = sample >> 8;
- if (nchannels == 2) {
- sample = scale(*right_ch++);
- *(OutputPtr++) = sample >> 0;
- *(OutputPtr++) = sample >> 8;
- }
- }
- n *= 4; /*数据长度为pcm音频采样的4倍 */
- OutputPtr = Output;
- while (n) {
- wrote = write(sfd, OutputPtr, n);
- OutputPtr += wrote;
- n -= wrote;
- }
- OutputPtr = Output;
- return MAD_FLOW_CONTINUE;
- }
- static
- enum mad_flow error(void *data,
- struct mad_stream *stream, struct mad_frame *frame)
- {
- return MAD_FLOW_CONTINUE;
- }
- static
- int decode(unsigned char const *start, unsigned long length)
- {
- struct buffer buffer;
- struct mad_decoder decoder;
- int result;
- buffer.start = start;
- buffer.length = length;
- mad_decoder_init(&decoder, &buffer, input, 0, 0, output, error, 0);
- mad_decoder_options(&decoder, 0);
- result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
- mad_decoder_finish(&decoder);
- return result;
- }