c - 在SDL回调函数中以一定频率播放波形

标签 c audio callback sdl waveform

我有一个 64 个样本长的波形。如果采样率为 44100 赫兹,我该如何播放(循环)此波形,使其播放任意频率?

频率 = 采样率/样本中的波形持续时间

因此频率应该是689hz(44100/64)。如果我想让它变成 65.41hz(C-2),我必须这样做:

65.41 = 44100/x

求解 x 得到近似值。 674.208。所以我需要弄清楚播放波形的速度是多少才能得到这个频率。所以我们可以解这个方程:

64 * x = 674.208

得到大约 10.5。因此波形需要以其原始速度的 10.5% 播放。

这是我的代码:

double smp_index = 0;
double freq = .105;

void callback(void *data, Uint8 *buf, int len){
    int i;
    s8 *out;
    out = (s8*) buf;
    if(smp_index < waveform_length){
        for(i = 0; i < len; i ++){
            out[i] = smpdata[(int)smp_index];
            smp_index +=freq;
            if(smp_index >= waveform_length)
                smp_index = 0;
        }
    }
}

所以生成的音频应该是关于音符 C-2,但更像是 D-2。是类型转换

(int)smp_index

导致问题?我看不到任何其他方法来完成此任务...

最佳答案

其实主要问题不在你的代码,而在你的推理。

So we can solve this equation:

64 * x = 674.208

and get about 10.5.

到目前为止一切顺利。 (实际上 674.208 应该是 674.246,但那是因为您之前将 65.41 四舍五入为 4 位有效数字。)

So the waveform needs to be played at 10.5% of its original speed.

不!波形必须减慢 10.5 倍。这意味着它必须以 1/10.5 = 0.095 或原始速度的 9.5% 播放。

关于c - 在SDL回调函数中以一定频率播放波形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1664384/

相关文章:

c - 尝试刷新缓存时出现段错误(核心转储)错误

android - Android 上的蓝牙声音输出

matlab - 在 Matlab 中播放 mp3

C++/CLI 包装器 : callback gets fired after the objects containing the memberfunction is deleted

c - 如何将结构转换为不同的结构成员

C DLL 和新数据服务进程之间基于云的 IPC

ios - iOS 上有html5 音频限制,是否可以同时播放背景音乐和音效?

javascript - 更改回调函数名称

ruby-on-rails - rails : How can I access the parent model of a new record's nested associations?

c - foo(int arr[]) 和 foo(int arr[10]) 有什么区别?