本人用在mini2440小,自己编写了两个小程序,分别调用内核驱动模块,实现录音和放音功能,可是在程序运行的时候,播放的声音是噪音,而不是我录制的音乐,特此求救?
//录音程序
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
int main( )
{
int audio_fd;
int music_fd;
signed short applicbuf[2048];
int count;
int channels ;
int format;
int speed ; //44.1 KHz
int mixer_fd;
int testchan ;
int recsrc ;
int totalbyte ;
int totalword ;
int total ;
char *filename;
filename="/mnt/net/CHA13/alarm.wav"; //打开音频设备
if ((audio_fd = open("/dev/dsp",O_RDONLY,0)) == -1)
{
perror("/dev/dsp");
exit(1);
}
//设置音频格式、声道数、采样频率
// format = AFMT_S16_NE;
format = 16 ;
if (ioctl(audio_fd,SNDCTL_DSP_SETFMT, &format) == -1)
{
perror("SNDCTL_DSP_SETFMT");
exit(1);
}
channels= 1;
if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, & channels) == -1)
{
perror("SNDCTL_DSP_CHANNELS");
exit(1);
}
// speed = 44100; //44.1 KHz
speed = 22050; //11 KHz
if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &speed) == -1)
{
perror("SNDCTL_DSP_SPEED");
exit(1);
}
if ( (music_fd = open(filename, O_WRONLY| O_APPEND| O_CREAT, 0) ) == -1)
{
perror(filename);
exit(1);
}
if ( (mixer_fd = open("/dev/mixer", O_WRONLY) ) == -1)
{
perror("open /dev/mixer error");
exit(1);
}
//设置录音源
testchan = SOUND_MIXER_MIC;
recsrc = (1 << testchan);
if (ioctl(mixer_fd, SOUND_MIXER_WRITE_RECSRC,&recsrc) == -1)
// if (ioctl(mixer_fd, SOUND_MIXER_WRITE_RECSRC,&testchan ) == -1)
{
perror("CD");
exit(1);
}
totalbyte= speed * channels * 2 * 60 * 3;
totalword = totalbyte/2;
total = 0;
while (total != totalword)
{
if (totalword - total >= 2048)
count = 2048;
else
count = totalword - total;
read(audio_fd, applicbuf, count);//录音
write(music_fd, applicbuf, count);//写入文件
total += count;
}
close(mixer_fd);
close(audio_fd);
close(music_fd);
}