SDL音高 - 播放率



我的目标是将发动机的rpm连接到声音的音调。我使用SDL作为音频后端。

因此,我的想法是从波缓冲区中采样比正常情况更快。因此,通过步道和错误,我现在能够"逐步"发动机声音。

问题#1

如果我从:

更改此部分
audioBuff +=  1 + pitch * 2;

to

audioBuff += 2

我得到了噪音。为什么?这与立体声通道有关吗?

问题#2

如何使这个线性音高?目前,这是一个"步进"音高。

这是完整的代码:

#include "SDL2/SDL.h"
#include <iostream>

void audioCallback(void* userdata, Uint8 *stream, int len);
Uint8 *audioBuff = nullptr;
Uint8 *audioBuffEnd = nullptr;
Uint32 audioLen = 0;
bool quit = false;
Uint16 pitch = 0;
int main()
{
    if(SDL_Init(SDL_INIT_AUDIO) < 0)
        return -1;
    Uint32 wavLen = 0;
    Uint8 *wavBuff = nullptr;
    SDL_AudioSpec wavSpec;
    if(SDL_LoadWAV("test.wav", &wavSpec, &wavBuff, &wavLen) == nullptr)
    {
        return 1;
    } 
    wavSpec.callback = audioCallback;
    wavSpec.userdata = nullptr;
    wavSpec.format = AUDIO_S16;
    wavSpec.samples = 2048;
    audioBuff = wavBuff;
    audioBuffEnd = &wavBuff[wavLen];
    audioLen = wavLen;
    if( SDL_OpenAudio(&wavSpec, NULL) < 0)
    {
        fprintf(stderr, "Could not open audio: %sn", SDL_GetError());
        return 1;
    }
    SDL_PauseAudio(0);
    while(!quit)
    {
        SDL_Delay(500);
        pitch ++;
    }
    SDL_CloseAudio();
    SDL_FreeWAV(wavBuff);
    return 0;
}

Uint32 sampleIndex = 0;
void audioCallback(void* userdata, Uint8 *stream, int len)
{
    Uint32 length = (Uint32)len;
    length = (length > audioLen ? audioLen : length);
    for(Uint32 i = 0; i < length; i++)
    {
        if(audioBuff > audioBuffEnd)
        {
            quit = true;
            return;
        }
        // why pitch * 2?
        // how to get a smooth pitch?
        stream[i] = audioBuff[0];
        audioBuff +=  1 + pitch * 2;
        fprintf(stdout, "pitch: %un", pitch);
    }
}

您将音频格式设置为 AUDIO_S16,这是"签名的16位小型样本"。每个样本是两个字节,第一个字节是LSB。当您阅读audioCallback中的数据时,您将其读为字节(8位),然后将这些字节传递回到期望16位的东西。因此,您会得到噪音,当您使用audioBuff +=2;时,您总是在阅读音频样本的LSB,这实际上是在使用这种方式时的噪声。

您应该始终使用16位或8位样品。

最新更新