最近整理了一下,大部分资源来自与网络,做个备份。
基于mad解码库的mp3/wav播放器整理:
C代码文件:
myplayer.c wavf.c mp3f.c parse_set.c
H头文件:
wavf.h mp3f.h parse_set.h mad.h
静态库文件:
libmad.a
编译命令
gcc -L. parse_set.c mp3f.c wavf.c myplayer.c -lmad
/**************************************************/
/*myplayer.c*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/soundcard.h>
#include "wavf.h"
#include "parse_set.h"
cmdparams cparams;
int main( int argc, char **argv )
{
//FILE *fp;
//int audioid;
int i;
if( argc != 3 )
{
usagethrow( argv[0] );
exit(1);
}
parsecmd( argv, &cparams );
if( !(cparams.islist) )// play a simple file
{
//printf( "begin play : " );
if( strstr( argv[2], ".wav") != NULL )
{
playawav( argv[2] );
}
else if( strstr( argv[2], ".mp3") != NULL )
{
playamp3( argv[2] );
}
else //play a wav list
{
fprintf( stderr, "%s does not like a wav/mp3 file/n", argv[2]);
exit( -1 );
}
}
else
{
playalist( argv[2] );
}
}
/********************************************************/
/* wavf.c */
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <fcntl.h>
#include "wavf.h"
#include "parse_set.h"
#include "mp3f.h"
unsigned char audio_buffer[BUFFSIZE];
extern cmdparams cparams;
int nlistfile = 0;
void erasenewline( char *src )
{
while( *src++ != '/n');
*(src-1) = '/0';
}
void getwavfinfo( FILE * fp , pwavfinfo pinfo )
{
//assert fp is an open stream already
fseek( fp, 0, SEEK_SET );
fread( pinfo->eflag, 1, 4, fp );
fread( &(pinfo->ilength), 1, 4, fp );
fread( pinfo->wavflag, 1, 4, fp );
fseek( fp, 0x16, SEEK_SET );
fread( &(pinfo->chanel), 1, 2, fp );
fread( &(pinfo->samp_rate), 1, 4, fp );
fread( &(pinfo->ispeed), 1, 4, fp );
fseek( fp, 2, SEEK_CUR );
fread( &(pinfo->samp_bit), 1, 2, fp );
fread( &(pinfo->samp_datanum), 1, 4, fp );
}
int checkwavf( pwavfinfo p )
{
//if( strcmp( p->eflag, "RIFF") )
if( p->eflag[0] != 'R' || p->eflag[1] != 'I' || p->eflag[2] != 'F')
{
printf( "RIFF flag check error it may be not a wav file/n" );
return -1;
}
//if( strcmp( p->wavflag, "WAVE" ) )
if( p->wavflag[0] != 'W' || p->wavflag[1] != 'A' ||
p->wavflag[2] != 'V' )
{
printf( "WAVE flag check error it may be not a wav file" );
return -1;
}
return 0;
}
void print_wavfinfo( pwavfinfo p )
{
printf( "/n============================================/n" );
printf( "File name : %s/n", p->fname );
printf( "Chanel num : %d/n", p->chanel );
printf( "Samling rate : %d/n", p->samp_rate );
printf( "Sample bit : %d/n", p->samp_bit );
printf( "Transfer speed : %d/n", p->ispeed );
}
void playawav( const char *filename )
{
int loops;
int rest;
int filelen;
FILE *fp;
wavfinfo wavf;
int i;
int adfd;
wavf.fname = filename;
//printf( "filename : %s/n", filename );
if( NULL == ( fp = fopen( filename, "r") ) )
{
fprintf( stderr, "Wav File Open error/n" );
exit(1);
}
fseek( fp, 0, SEEK_END );
filelen = ftell( fp ) - 0x2c;
fclose( fp );
fp = fopen( filename, "r");
getwavfinfo( fp, &wavf );
fclose( fp );
if( checkwavf( &wavf ) == -1 )
{
fprintf( stderr, "Wav file check error skip/n" );
return ;
}
adfd = open( "/dev/dsp", O_RDWR );
if( -1 == adfd )
{
fprintf( stderr, "Dev file /dev/dsp Open error/n" );
exit(1);
}
if( set_addev( adfd, &wavf ) == -1 )
{
fprintf( stderr, "Device /dev/dsp setting error/n");
exit(1);
}
loops = filelen/BUFFSIZE;
rest = filelen%BUFFSIZE;
fp = fopen( filename, "r" );
fseek( fp, 0x2c, SEEK_SET );
print_wavfinfo( &wavf );
//getchar( );
printf("Now playing ......../n");
for( i=0; i<loops; i++)
{
fread( audio_buffer, BUFFSIZE, 1, fp );
write( adfd