MP3播放器

来源:互联网 发布:网络测速原理 编辑:程序博客网 时间:2024/05/01 22:02

转自: http://hi.baidu.com/falimon_7/blog/item/3aada9ecece7861bfcfa3c13.html

 

今天研究了一下libmad,发现用这个库做一个简单的mp3播放器非常容易,网上也有很多例程,于是想把播放mp3功能加入我的wav播放小程序里面。晚上实验了一下,的确很好用,在sep4020这颗小arm7上面跑的很欢乐。程序结构目前很乱,还要整理一下,现在的仅仅实现功能,算是beat版。

首先需要编译安装libmad,这个参考以前的文章,编译程序时时需要mad.h 和libmad.a等文件。

为了便于交流,采用静态链接的方法添加libmad库比较好,相当于把libmad包含的程序里面去。

最后将wmplay.c和libmad.a放在同一个目录里,执行以下指令即可:

arm-linux-gcc wmplay.c libmad.a -o wmplay

以下是程序源码

/////////////////////////////////////////////////////////////////////////////

 

//AUTHOR("aokikyon@gmail.com");
//DESCRIPTION("sep4020 uda1341 sound card test program");
//2009-05-09注意!音频设备只能以O_WRONLY或者O_RDONLY方式打开,不能使用O_RDWR方式打开,因为不支持同时录音和放音。
//2009-06-05 尝试添加mp3播放功能

#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#include <string.h>
//add for mp3 play
# include <sys/stat.h>
# include <sys/mman.h>
# include <sys/types.h>
# include <mad.h>

 

 

#define BUF_SIZE 4096
#define DEVICE_NAME "/dev/dsp"

int audio_fd; //声卡
FILE *file_fd; //文件
int file_len; //文件长度

unsigned char audio_buffer[BUF_SIZE];

unsigned char *file_name;
unsigned int audio_rate;

void delay(long x)
{
unsigned long i;
for(i=0; i<x; i++);
}

/////////////////////////////////////for mp3/////////////////

struct buffer {
    unsigned char const *start;
    unsigned long length;
};

static int sfd;    /*声音设备的描述符 */
static int decode(unsigned char const *, unsigned long);
////////////////////////////////////end/////////////////////

int main(int argc, char *argv[])
{
int i=0;
printf("This is a wav&mp3 play program./n");
printf("Please add a file name and the audio rate!/nsuch as /"./wmplay test.mp3 44100/" /n/n/n");
delay(100000);
  
file_name = argv[1];
int file_string_len = strlen(file_name);

if(file_string_len < 3)
{
   printf("file is too short!/n");
   exit(0);
}   
  
if(argv[2] != NULL)
   sscanf(argv[2],"%d", &audio_rate);
else
   audio_rate = 44100;

printf("File name : %s /nAudio rate : %d /n",file_name,audio_rate);
delay(100000);

if(file_name[file_string_len-3]=='m' && file_name[file_string_len-2]=='p' && file_name[file_string_len-1]=='3')
{
printf("Play mp3!/n");

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);

}
else
{

/*打开音频设备,准备play*/
if ((audio_fd = open(DEVICE_NAME, O_WRONLY)) == -1)
{
   printf("open error/n");
   return -1;
}
/*设置采样格式*/
int format;
format = AFMT_S16_LE;

if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format) == -1)
{
   /* fatal error */
   printf("SNDCTL_DSP_SETFMT error/n");
   return -1;  
}

if (format != AFMT_S16_LE)
{
   /* 本设备不支持选择的采样格式. */
   printf("sep4020 oss driver does not support AFMT_S16_LE");
}

/*设置通道数*/
int channels = 2; /* 1=mono, 2=stereo */

if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels) == -1)
{
   /* Fatal error */
   printf("SNDCTL_DSP_CHANNELS error");
   return -1;
}

if (channels != 2)
{
   /* 本设备不支持立体声模式 ... */
   printf("sep4020 oss driver does ");
}

/*设置采样速率*/
int speed = audio_rate;

if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &speed)==-1)
{
   /* Fatal error */
   printf("SNDCTL_DSP_SPEED error/n");
   return -1;
}
printf("the wav speed is %d/n",speed);


/*打开并计算文件长度*/
file_fd = fopen(file_name, "r");
fseek(file_fd,0,SEEK_END);     //定位到文件末
file_len = ftell(file_fd);     //文件长度

int loops = file_len/4096;

/*重新定位到文件头*/
fclose(file_fd);
file_fd = fopen(file_name, "r");
/*播放wav文件*/
for(i=0;i<loops;i++)
{
   fread(audio_buffer, 4096, 1, file_fd);
   write(audio_fd,audio_buffer,4096);
}
/*关闭设备和文件*/
fclose(file_fd);
close(audio_fd);
}
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;
}