/*
modify by hfl 2014-2-16
*/
# include <stdio.h>
# include <unistd.h>
# include <sys/stat.h>
# include <sys/mman.h>
# include "mad.h"
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
/*
* This is perhaps the simplest example use of the MAD high-level API.
* Standard input is mapped into memory via mmap(), then the high-level API
* is invoked with three callbacks: input, output, and error. The output
* callback converts MAD's high-resolution PCM samples to 16 bits, then
* writes them to standard output in little-endian, stereo-interleaved
* format.
*/
//#define printf
static Get_file_length(char *PATH);
static int init_dsp();
static int decode(unsigned char const *, unsigned long);
static enum mad_flow outputplay(void *data,
struct mad_header const *header,
struct mad_pcm *pcm);
int main(int argc, char *argv[])
{
printf("The main is start!\n");
struct stat stat;
void *fdm;
int fd;
//char buffer1[80000];
printf("###The input file is %s ! the arc=%d###\n",argv[1],argc);
if (argc == 1)
{
printf("The argc is wrong!\n");
return 1;
}
#if 0
if (fstat(STDIN_FILENO, &stat) == -1 ||
stat.st_size == 0)
return 2;
#endif
fd =open(argv[1],O_RDWR);
if(-1==fd)
{
printf("sorry,The file open is faild!\n");
}
else
{
printf("The file open is sucessed!\n");
}
//read(fd,buffer1,sizeof(buffer1));
//printf("%s", buffer1);
stat.st_size = Get_file_length(argv[1]);
printf("The file size is %d\n",stat.st_size );
printf("The Map is begin ok!\n");
fdm = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (fdm == MAP_FAILED)
{
printf("mmap is failed\n");
return 3;
}
decode(fdm, stat.st_size);
if (munmap(fdm, stat.st_size) == -1)
return 4;
return 0;
}
/*
* This is a private message structure. A generic pointer to this structure
* is passed to each of the callback functions. Put here any data you need
* to access from within the callbacks.
*/
struct buffer {
unsigned char const *start;
unsigned long length;
};
int id;
int flag=0;
/*初始化音频设备*/
int init_dsp(int rate,int channels)
{
/* 采样频率 44.1KHz*/
int format; /* 量化位数 16*/
/* 声道数 2*/
int setting; /* 高16位标明分片最大序号,低16位标明缓冲区的尺寸*/
if ( ( id = open ( "/dev/dsp", O_WRONLY) ) < 0 )
{
fprintf (stderr, " Can't open sound device!\n");
exit ( -1 ) ;
}
/* 此项在Virtual PC中可减少电流声 */
setting = 0x0002000F;
ioctl(id, SNDCTL_DSP_SETFRAGMENT, &setting);
//rate = 44100;
ioctl(id, SNDCTL_DSP_SPEED, &rate);
format = AFMT_S16_LE;
ioctl(id, SNDCTL_DSP_SETFMT, &format);
//channels = 2;
ioctl(id, SNDCTL_DSP_CHANNELS, &channels);
printf( "The Dsp init is oss ok!\n");
return 0;
}
/*
* This is the input callback. The purpose of this callback is to (re)fill
* the stream buffer which is to be decoded. In this example, an entire file
* has been mapped into memory, so we just call mad_stream_buffer() with the
* address and length of the mapping. When this callback is called a second
* time, we are finished decoding.
*/
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;
printf("1111");
return MAD_FLOW_CONTINUE;
}
/*
* The following utility routine performs simple rounding, clipping, and
* scaling of MAD's high-resolution samples down to 16 bits. It does not
* perform any dithering or noise shaping, which would be recommended to
* obtain any exceptional audio quality. It is therefore not recommended to
* use this routine if high-quality output is desired.
*/
static inline
signed int scale(mad_fixed_t sample)
{
/* round */
sample += (1L << (MAD_F_FRACBITS - 16));
/* clip */
if (sample >= MAD_F_ONE)
sample = MAD_F_ONE - 1;
else if (sample < -MAD_F_ONE)
sample = -MAD_F_ONE;
/* quantize */
return sample >> (MAD_F_FRACBITS + 1 - 16);
}
static int Get_file_length(char *PATH)
{
FILE *fp;
fp=fopen(PATH,"r");
if(!fp)
{
printf("sorry,The file open is faild!\n");
}
else
{
printf("The file open is sucessed!\n");
}
fseek(fp, 0L,SEEK_END);
return (ftell(fp));
}
/*
* This is the output callback function. It is called after each frame of
* MPEG audio data has been completely decoded. The purpose of this callback
* is to output (or play) the decoded PCM audio.
*/
static
enum mad_flow output(void *data,
struct mad_header const *header,
struct mad_pcm *pcm)
{
unsigned int nchannels, nsamples;
mad_fixed_t const *left_ch, *right_ch;
static FILE *fdout;
char buf[1];
/* pcm->samplerate contains the sampling frequency */
fdout= fopen("mypcm.pcm","ab+");
if(!fdout)
{
printf("open is failed\n");
}
else
printf("out open is ok\n");
nchannels = pcm->channels;
nsamples = pcm->length;
left_ch = pcm->samples[0];
right_ch = pcm->samples[1];
//printf("channels=%d, samples=%d\n", nchannels,nsamples);
while (nsamples--) {
signed int sample;
/* output sample(s) in 16-bit signed little-endian PCM */
// printf("2222\n");
sample = scale(*left_ch++);
buf[0]=(sample >> 0) & 0xff;
printf("%d\t",buf[0]);
fwrite(buf,1,1,fdout);
buf[0]=(sample >> 8) & 0xff;
printf("%d\t",buf[0]);
fwrite(buf,1,1,fdout);
if (nchannels == 2) {
sample = scale(*right_ch++);
buf[0]=(sample >> 0) & 0xff;
fwrite(buf,1,1,fdout);
buf[0]=(sample >> 8) & 0xff;
fwrite(buf,1,1,fdout);
}
}
fclose(fdout);
return MAD_FLOW_CONTINUE;
}
static
enum mad_flow outputplay(void *data,
struct mad_header const *header,
struct mad_pcm *pcm)
{
unsigned int nchannels;
long int nsamples,samplerate;
mad_fixed_t const *left_ch, *right_ch;
char buf[1];
/* pcm->samplerate contains the sampling frequency */
nchannels = pcm->channels;
nsamples = pcm->length;/* 这个不是采样位,而一帧的数据长度12*3(采样)*32(子带)=1152*/
left_ch = pcm->samples[0];
right_ch = pcm->samples[1];
samplerate=pcm->samplerate;
if(!flag)
{
printf("channels=%d, samples2=%ld,flag=%d\n", nchannels,samplerate,flag);
printf("init dsp is begin\n");
init_dsp(samplerate,nchannels);
flag++;
}
while (nsamples--) {
signed int sample;
/* output sample(s) in 16-bit signed little-endian PCM */
// printf("2222\n");
sample = scale(*left_ch++);
buf[0]=(sample >> 0) & 0xff;
//printf("%d\t",buf[0]);
write(id,buf,1);
buf[0]=(sample >> 8) & 0xff;
// printf("%d\t",buf[0]);
write(id,buf,1);
if (nchannels == 2) {
sample = scale(*right_ch++);
buf[0]=(sample >> 0) & 0xff;
write(id,buf,1);
buf[0]=(sample >> 8) & 0xff;
write(id,buf,1);
}
}
//fclose(fdout);
return MAD_FLOW_CONTINUE;
}
/*
* This is the error callback function. It is called whenever a decoding
* error occurs. The error is indicated by stream->error; the list of
* possible MAD_ERROR_* errors can be found in the mad.h (or stream.h)
* header file.
*/
static
enum mad_flow error(void *data,
struct mad_stream *stream,
struct mad_frame *frame)
{
struct buffer *buffer = data;
fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n",
stream->error, mad_stream_errorstr(stream),
stream->this_frame - buffer->start);
/* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */
return MAD_FLOW_CONTINUE;
}
/*
* This is the function called by main() above to perform all the decoding.
* It instantiates a decoder object and configures it with the input,
* output, and error callback functions above. A single call to
* mad_decoder_run() continues until a callback function returns
* MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and
* signal an error).
*/
static
int decode(unsigned char const *start, unsigned long length)
{
struct buffer buffer;
struct mad_decoder decoder;
int result;
/* initialize our private message structure */
buffer.start = start;
buffer.length = length;
/* configure input, output, and error functions */
mad_decoder_init(&decoder, &buffer,
input, 0 /* header */, 0 /* filter */, outputplay,
error, 0 /* message */);
/* start decoding */
result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
/* release the decoder */
mad_decoder_finish(&decoder);
return result;
}
以上就是通过libmad将mp3先解码成pcm,然后将pcm直接扔到/ dev/dsp音频设备中,但dsp音频设备属于oss架构,已经逐渐被alsa驱动取代,后续会介绍基于alsa驱动架构的mp3播放器
libmad学习进阶3-----基于oss音频驱动架构的一个mp3播放器
最新推荐文章于 2024-01-19 22:38:24 发布