我正在尝试读取波形文件并在主板上的一张声卡上播放。下面是代码。我在使用此代码时遇到了一些问题。
PS:
周期大小设置为10毫秒。即16KHz - 160; 11025 - 110
ret = snd_pcm_open(&handlePCMHD, "hw:0,0",
SND_PCM_STREAM_PLAYBACK, 0);
if (ret < 0) {
snd_pcm_close(handlePCMHD);
return -1;
}
fd = open(fileName, O_RDONLY);
/* file dump */
fd2 = open("test.raw", O_WRONLY | O_CREAT);
if(fd < 0)
{
return -1;
}
/* Wave file header is 44 bytes */
if(read(fd, buf, 44)){
sampleRate = buf[12] | ((unsigned int)buf[13] << 16);
channels = buf[11];
}else{
//read failed
}
set_pcmhd_hw_param(handlePCMHD, sampleRate);
/* Buffer size = SampleRate * Number of Bytes per frame * No of Periods / For 10 ms */
readBytes = (sampleRate * 2 * 2)/100;
while((channels == 1) ? (x = read(fd, buf, readBytes)) : (x = read(fd, stereobuf, readBytes * 2)))
{
if(channels == 2){
StereoToMono_samples(buf ,stereobuf, readBytes/2);
}
write(fd2, buf, readBytes);
/* Number of frames = read bytes / 2 for 16 bit PCM */
x = readBytes / 2;
if ((ret = snd_pcm_writei(handlePCMHD, buf, (snd_pcm_uframes_t)x) == -EPIPE)) {
snd_pcm_prepare(handlePCMHD);
} else if (ret < 0) {
//error handling
} else{
count++;
}
}
close(fd);
close(fd2);
snd_pcm_close(handlePCMHD);