网上有篇利用libao结合minimad的播放mp3程序,试用发现电流声过大,而且又要调用libao还不如直接对声卡编程。将minimad解码声音用以下命令写入一个文件中,
$./minimad <丁香花.mp3 1>temp
然后用程序播放即可,命令如下:
$./a.out temp
在virtual pc下还有略微的电流声而在另外一台redhat9中速度过快,有些变调,暂时还不知道怎么调节。播放的实质其实关键是要调节几个参数。
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> #include <sys/soundcard.h> int main(int argc, char *argv[]) { int id,fd,i; char buf[4096]; int rate; /* 采样频率 44.1KHz*/ int format; /* 量化位数 16*/ int channels; /* 声道数 2*/ int setting; /* 高16位标明分片最大序号,低16位标明缓冲区的尺寸*/ if (argc != 2) { fprintf (stderr, "usage: %s <filename>!\n", argv[0]); exit ( -1 ) ; } if ( ( fd = open (argv[1],O_RDONLY))<0) { fprintf ( stderr, " Can't open sound file!\n"); exit (-1 ); } 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 = 441000; ioctl(id, SNDCTL_DSP_SPEED, &rate); format = AFMT_S16_LE; ioctl(id, SNDCTL_DSP_SETFMT, &format); channels = 2; ioctl(id, SNDCTL_DSP_CHANNELS, &channels); while((i = read(fd,buf, sizeof(buf)))>0) { write(id,buf,i); } close(fd); close(id); exit(0); }