gpt4 book ai didi

c - 通过核心中断在 alsa 中发出声音

转载 作者:行者123 更新时间:2023-11-30 17:44:48 24 4
gpt4 key购买 nike

嗨,我正在为我的大学 build 一个实验室,我正在尝试使用 Alsa 软件为 1 个循环中的中断数量发出声音。 Alsa 缓冲区中存储的值大多为 0、1、2,偶尔为 4 或 5(我计算循环之间中断的差异)。我找到了一个程序,可以发出正弦波,并发出连续的蜂鸣声,我想将该波转换为中断波,有人可以帮忙吗?

在第一个代码中,我试图让 alsa 在中断 != 0 时发出声音,或者在中断 == 0 时不发出任何声音。

int playback_callback (snd_pcm_sframes_t nframes)
{
static float t = 0;
int err;
unsigned long long ti = 0, ti1 = 0, diff = 0,secs = 0;

ti = ti1 = num_interrupts();

printf ("playback callback called with %lu frames\n", nframes);

for(int i = 0; i < BUFSIZE; ++i) {
/*num_interrupts: function to calculate interrupts on /proc/interrupts*/
ti = num_interrupts();
diff = ti-ti1;
ti1 = ti;
/*printf("diff%llu\n",diff);*/
if(diff != 0) {
buf[i] = 65535;
} else {
buf[i] = 0;
}
}
if ((err = snd_pcm_writei (playback_handle, buf, nframes)) < 0) {
fprintf (stderr, "write failed (%s)\n", snd_strerror (err));
}

return err;
}

最佳答案

人耳听不到单个样本;它听到频率

您应该生成一个正弦波(或多个正弦波)并根据中断率调制其音量和/或频率。

请注意,非常快的变化是听不到的;您应该仅每 3000 个样本检查一次中断号。

关于c - 通过核心中断在 alsa 中发出声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com