DSP

libmad解码MP3——/dev/dsp播放

2019-07-13 10:04发布

一、编译libmad库    1、下载libmad源码,解压。       2、 ./configure --prefix=/usr (安装到/usr/lib目录)       3、make & make install           出现错误信息 error: unrecognized command line option "-fforce-mem"           解决办法 打开Makefile去掉该编译选项
二、交叉编译libmad库(移植到arm板上)    1、./configure --build=arm --host=arm-linux CC=/arm/opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-gcc --prefix=/home/temp/usr        2、make & make install(出错和解决同上)
三、ls /dev/dsp (查看/dev/dsp设备节点,通过该设备节点操作声卡)    无该设备节点,解决方法:安装alsa驱动(可能需要alsa-oss库,未测试)     1、开启oss (linux操作系统)
         编辑/etc/modprobe.d/dist-oss.conf文件
         然后去掉最后一行#install snd-pcm /sbin/modprobe --ignore-install snd-pcm && /sbin/modprobe snd-pcm-oss && /sbin/modprobe snd-seq-device && /sbin/modprobe snd-seq-oss 开头的#,使该行生效.
         重启系统,/deb/dsp节点创建      2、编辑 /etc/mdev.conf 文件,添加如下信息(嵌入式linux系统)
# alsa sound devices and audio stuff
pcm.*           0:0     0660    =snd/
control.*       0:0     0660    =snd/
midi.*          0:0     0660    =snd/
seq                     0:0     0660    =snd/
timer           0:0     0660    =snd/
adsp            0:0     0660    >sound/
audio           0:0     0660    >sound/
dsp                     0:0     0660    >sound/
mixer           0:0     0660    >sound/
sequencer.*     0:0     0660    >sound/
             mdev 后生效
4、按libmad提供的minimad.c编写一个简单的MP3播放代码。      使用 -lmad 链接libmad库   5、以下代码来源于 http://tech.ddvip.com/2013-05/1368624635195593.html       经编译运行,测试可行。
#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!!! "); 
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;
}