这个程序在Ubuntu中运行良好,但在Raspberry Pi上有段错误。编译期间也没有错误。我正在播放音乐,歌曲在Ubuntu中正常运行。
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#define PB_DEVICE ("plughw:0,0")
#define SAMPLING_RATE (48000)
int main (int argc, char *argv[])
{
FILE *fp;
int err;
short buf[10],aud[10];
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;
char *pdevice=PB_DEVICE;
int val=48000;
int read_len = 0;
fp=fopen("../inc/test2.wav","rb");
if(NULL == fp)
{
printf("File Open Failed:\n");
}
if ((err = snd_pcm_open(&playback_handle, pdevice, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
pdevice,
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params,&val,0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 1)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit (1);
}
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (playback_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
while((read_len = fread(buf,2,sizeof(aud),fp)) > 0)
{
if((err = snd_pcm_writei(playback_handle, buf, read_len)) != read_len)
{
fprintf (stderr, "write to audio interface failed ReadLen [ %d ] (%s)\n",
read_len,
snd_strerror (err));
exit (1);
}
read_len=0;
}
snd_pcm_close (playback_handle);
exit (0);
fclose(fp);
}
答案 0 :(得分:2)
您误解了sizeof
的作用,因此读取的数据量是缓冲区可以包含的两倍。根据实现细节,这可能会也可能不会导致内存保护错误或程序失败。
while((read_len = fread(buf,2,sizeof(aud),fp)) > 0)
尝试在程序的早期添加此行,您将了解您的问题:
printf("sizeof(aud)=%d\n", sizeof(aud));
sizeof
返回 bytes 中的大小,但您尝试将其用作元素的计数,因为您尝试的每个元素都是2个字节告诉它每个读取20个2字节的项目,而不是每个2个字节的10个项目。