100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > linux播放到设备 linux - 将字节流式传输到ALSA播放设备 - 堆栈内存溢出

linux播放到设备 linux - 将字节流式传输到ALSA播放设备 - 堆栈内存溢出

时间:2020-12-26 10:49:05

相关推荐

linux播放到设备 linux - 将字节流式传输到ALSA播放设备 - 堆栈内存溢出

我在使用libasound将随机字节写入ALSA播放设备时遇到了很多麻烦。 最终,我的目标是能够通过网络路由回放流并让它在远程设备上播放。

此问题中提供的代码将WAV文件读入内存并通过snd_pcm_writei将其写入驱动程序并且可以正常工作。 但是,这段代码与我正在尝试做的事情之间的关键区别在于我没有立即获得所有数据。 我希望在数据可用时对其进行流式处理。

调整上面的示例代码以满足我的需求,我最终得到了这样的结论:

#include

#include

#include

static snd_pcm_t *PlaybackHandle;

int init_playback(const char *device, int samplerate, int channels) {

int err;

printf("Init parameters: %s %d %d\n", device, samplerate, channels);

if((err = snd_pcm_open(&PlaybackHandle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {

printf("Can't open audio %s: %s\n", device, snd_strerror(err));

return -1;

}

if ((err = snd_pcm_set_params(PlaybackHandle, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, channels, samplerate, 1, 500000)) < 0) {

printf("Can't set sound parameters: %s\n", snd_strerror(err));

return -1;

}

return 0;

}

int play_bytes(const void *bytes, int len) {

snd_pcm_uframes_t frames, count;

snd_pcm_uframes_t bufsize, period_size;

frames = 0;

count = 0;

snd_pcm_prepare(PlaybackHandle);

snd_pcm_get_params(PlaybackHandle, &bufsize, &period_size);

printf("bufsize=%d\n", (int) bufsize);

do {

int remaining = len - count;

int buflen = remaining < bufsize ? remaining : bufsize;

frames = snd_pcm_writei(PlaybackHandle, bytes + count, buflen);

// If an error, try to recover from it

if (frames == -EPIPE) {

printf("EPIPE\n");

snd_pcm_prepare(PlaybackHandle);

}

if (frames < 0) {

printf("Recovering\n");

frames = snd_pcm_recover(PlaybackHandle, frames, 0);

}

if (frames < 0)

{

printf("Error playing wave: %s\n", snd_strerror(frames));

break;

}

// Update our pointer

count += frames;

//printf("count=%d len=%d\n", (int)count, len);

} while (count < len);

// Wait for playback to completely finish

if (count == len)

snd_pcm_drain(PlaybackHandle);

return 0;

}

int close_playback() {

snd_pcm_close(PlaybackHandle);

return 0;

}

int main(int argc, char **argv) {

if(argc < 1) {

printf("Usage: %s \n", argv[0]);

return -1;

}

int fd;

unsigned long long len;

fd = open(argv[1], O_RDONLY);

// Find the length

len = lseek(fd, 0, SEEK_END);

// Skip the first 44 bytes (header)

lseek(fd, 44, SEEK_SET);

len -= 44;

char *data = malloc(len);

read(fd, data, len);

init_playback("default", 44100, 2);

play_bytes(data, len);

close_playback();

return 0;

}

这段代码可以用gcc playback.c -o playback -lasound 。 我正在使用的WAV文件可以在这里找到。

当我运行此代码片段时,我根据bufsize对输入数据进行分块,根据块大小,在播放中会重复播放音频片段。 较大的块大小产生的重复次数少于小块大小。 将此与音频的声音结合起来,我相信在每个块的末尾都会重复一个小片段。

我使用的参数是:

样本:44100

频道:2

为什么一次性发送整个WAV文件工作,而发送它的块不起作用? 如何将音频数据块发送给驱动程序并使其正常播放?

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。