C++ 音频基音-播放速率

C++ 音频基音-播放速率,c++,audio,sdl,sdl-2,C++,Audio,Sdl,Sdl 2,我的目标是将发动机的转速与声音的音调联系起来。我正在使用SDL作为我的音频后端 所以我的想法是比正常情况下更快地从波形缓冲区取样。因此,通过跟踪和错误,我现在能够“一步一步”地调整我的发动机声音 问题1 如果我将此部分更改为: audioBuff += 1 + pitch * 2; 到 我只听到噪音。为什么?这与立体声频道有关吗 问题2 我怎样才能使它成为线性音高?目前,这是一个“步进”音高 以下是完整的代码: #include "SDL2/SDL.h" #include <iostr

我的目标是将发动机的转速与声音的音调联系起来。我正在使用SDL作为我的音频后端

所以我的想法是比正常情况下更快地从波形缓冲区取样。因此,通过跟踪和错误,我现在能够“一步一步”地调整我的发动机声音

问题1

如果我将此部分更改为:

audioBuff +=  1 + pitch * 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: %s\n", 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: %u\n", pitch);
    }
}
#包括“SDL2/SDL.h”
#包括
void audioCallback(void*userdata,Uint8*stream,int len);
Uint8*audioBuff=nullptr;
Uint8*audioBuffEnd=nullptr;
Uint32三极管=0;
bool-quit=false;
Uint16节距=0;
int main()
{
中频(SDL_初始化(SDL_初始化音频)<0)
返回-1;
Uint32 wavLen=0;
Uint8*wavBuff=nullptr;
SDL_音频规格wavSpec;
如果(SDL_LoadWAV(“test.wav”、&wavSpec、&wavBuff、&wavLen)=nullptr)
{
返回1;
} 
wavSpec.callback=音频回调;
wavSpec.userdata=nullptr;
wavSpec.format=AUDIO_S16;
wavSpec.samples=2048;
audioBuff=wavBuff;
audioBuffEnd=&wavBuff[wavLen];
听力=波形;
如果(SDL_OpenAudio(&wavSpec,NULL)<0)
{
fprintf(stderr,“无法打开音频:%s\n”,SDL_GetError());
返回1;
}
SDL_PauseAudio(0);
而(!退出)
{
SDL_延迟(500);
音高++;
}
SDL_CloseAudio();
SDL_FreeWAV(wavBuff);
返回0;
}
Uint32样本指数=0;
void audioCallback(void*userdata,Uint8*stream,int len)
{
Uint32长度=(Uint32)len;
长度=(长度>听筒?听筒:长度);
对于(Uint32 i=0;iaudioBuffEnd)
{
退出=真;
返回;
}
//为什么要投*2?
//如何获得平滑的音高?
流[i]=audioBuff[0];
audioBuff+=1+音高*2;
fprintf(标准输出,“音高:%u\n”,音高);
}
}

您正在将音频格式设置为
audio\u S16
,即“有符号16位小尾端采样”。每个示例有两个字节,第一个字节是LSB。当您在
audioCallback
中读取数据时,您将其作为字节(8位)读取,然后将这些字节传递回预期的16位。因此,当您使用
audioBuff+=2时,会产生噪音您总是在读取音频样本的LSB,这种方式使用时基本上是噪声


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

您所说的“线性音高”是什么意思?我自己也在问同样的问题@1201程序音高可以应用于SDL\U混音器的混合块吗?好的,谢谢。这就是我的回调函数现在的样子:
void audioCallback(void*userdata,Uint8*stream,int len){Uint32 length=(Uint32)len;for(Uint32 i=0;iaudioBuffEnd){quit=true;return;}stream[i]=audioBuff[0];i++;流[i]=audioBuff[1];audioBuff+=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: %s\n", 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: %u\n", pitch);
    }
}