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

发布于 2024-08-09 16:27:16 字数 869 浏览 6 评论 0原文

我有一个 64 个样本长的波形。如果采样率为 44100 Hz,我如何播放(循环)该波形以便它播放任意频率?

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

因此频率应为 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

造成了问题吗?我看不到任何其他方法来实现这一目标......

I have a waveform 64 samples long. If the sampling rate is 44100 hz, how can I play(loop) this waveform so that it plays arbitrary frequencies?

frequency = samplerate / waveform duration in samples

Therefore the frequency should be 689hz(44100/64). If I wanted it to be say, 65.41hz(C-2), I would have to do this:

65.41 = 44100 / x

Solving for x yields aprox. 674.208. So I need to figure out what speed to play the waveform at to get this frequency. So we can solve this equation:

64 * x = 674.208

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

Here is my code:

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;
        }
    }
}

So the resulting audio should be about the note C-2, but its more of a D-2. Is the cast

(int)smp_index

causing the problem? I couldn't see any other way to accomplish this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆之渊 2024-08-16 16:27:16

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

所以我们可以解这个方程:

64 * x = 674.208

得到大约 10.5。

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

因此波形需要以原始速度的 10.5% 播放。

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

Actually, the main problem is not in your code, but in your reasoning.

So we can solve this equation:

64 * x = 674.208

and get about 10.5.

So far so good. (Actually 674.208 should be 674.246 but that's because you rounded 65.41 to 4 significant figures earlier on.)

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

No! The waveform must be slowed down by a factor of 10.5. Which means it must be played at 1/10.5 = 0.095 or 9.5% of its original speed.

旧故 2024-08-16 16:27:16

转换(int)smp_index不会导致问题。它只是拉伸波 - 这是质量损失(也许您的波数据应该长于 64 个样本),但不可能改变频率。最有可能的问题是:

        if(smp_index > realLength)
            smp_index = 0;

应该是:

        if(smp_index >= realLength)
            smp_index -= realLength;

我还有一些其他注释给您:

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

Um,如果“波形持续时间”指的是波的周期,那么是的。即,如果您的 64 个样本波形是周期为 64 的正弦波,那么是的。如果是 32 或 16 那么情况就会有所不同。如果它不能整除 64(例如 48 或 30),那么您的波形首先就不是周期性的。

现在:

u32 waveform_length;
out = (s8*) buf;
if(smp_index < waveform_length){

waveform_length 的值是多少?对我来说似乎未初始化...

The cast (int)smp_index is not causing the problem. It simply stretches the wave - this is quality loss (maybe you should have your wave data longer than 64 samples) but cannot possibly change the frequency. Most likely, the problem is that:

        if(smp_index > realLength)
            smp_index = 0;

should be:

        if(smp_index >= realLength)
            smp_index -= realLength;

I also have some other notes for you:

frequency = samplerate / waveform duration in samples

Um, if by "waveform duration" you mean the period of a wave, then yes. I.e. if your 64-sample waveform is a sine wave of period 64, then yes. If it's 32 or 16 then things will be different. If it's something that doesn't divide 64 (like 48 or 30) then your waveform is not periodic in the first place.

Now:

u32 waveform_length;
out = (s8*) buf;
if(smp_index < waveform_length){

What's the value of waveform_length? Looks uninitialised to me...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文