fre*_*ull 7 c audio sdl waveform callback
我有一个长64个样本的波形.如果采样率为44100 hz,我该如何播放(循环)此波形以使其播放任意频率?
频率=样本中的采样率/波形持续时间
因此频率应为689hz(44100/64).如果我想要它,65.41hz(C-2),我必须这样做:
65.41 = 44100/x
求解x得到aprox.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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此产生的音频应该是关于音符C-2,但它更多的是D-2.是演员
(int)smp_index
Run Code Online (Sandbox Code Playgroud)
造成这个问题?我看不到任何其他方法来实现这个目标......
其实,主要问题不在于你的代码,而在于你的推理。
所以我们可以解这个方程:
64 * x = 674.208
并得到大约10.5。
到目前为止,一切都很好。(实际上 674.208 应该是 674.246,但那是因为您之前将 65.41 四舍五入为 4 位有效数字。)
因此波形需要以原始速度的 10.5% 播放。
不!波形必须减慢10.5 倍。1/10.5 = 0.095
这意味着它必须以原始速度的 9.5%播放。