我目前正在嘗試使用SDL錄製音頻並對其進行處理,但我在運行時遇到了一些麻煩,無法獲取處理步驟所需的16位PCM音頻。我運行的設備僅支持AUDIO_F32格式。所以我的AudioSpec如下;將Uint8(浮點AUDIO_F32)轉換爲int16_t(16位PCM)
SDL_AudioSpec wanted;
SDL_zero(wanted);
wanted.freq = 48000;
wanted.format = AUDIO_F32SYS;
wanted.channels = 1;
wanted.samples = 4096;
wanted.callback = NULL;
現在我不得不跟隨包含錄製音頻的緩衝區;
std::vector<Uint8> buffered;
我該如何去將Uint8
轉換爲16位PCM格式?如果我是正確的,Uint8
應該是定義爲unsigned char
的32位浮點音頻。我已經嘗試了下面的代碼,但它似乎沒有給出正確的結果;
std::vector<int16_t> pcm_buffer;
for(const Uint8& sample : buffered)
{
float sampleFloat = (float) sample;
sampleFloat *= 32767;
int16_t sampleInt = (int16_t) sampleFloat;
pcm_buffer.push_back(sampleInt);
}
我有一種感覺,我完全誤會我在做什麼,所以我希望有人能指出我在正確的方向。
這是真正的代碼?我有一個預感,Uint應該是'Uint8'。如果你用手工輸入:請考慮複製+粘貼。 –
我不得不編輯部分代碼,因爲我正在做很多試驗,而且我的代碼很亂,但它確實應該是Uint8。 – Thys