我有一个 64 个样本长的波形。如果采样率为 44100 赫兹,我如何播放(循环)此波形以播放任意频率?
频率 = 采样率 / 采样中的波形持续时间
因此频率应为 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;
}
}
}
所以生成的音频应该是关于音符 C-2,但它更像是 D-2。是演员
(int)smp_index
导致问题?我看不到任何其他方法来实现这一点......