簡體   English   中英

使用 OGG/Vorbis 解碼沒有聲音

[英]Decoding with OGG/Vorbis gives no sound

我想播放 Ogg/Vorbis 音頻/視頻文件,但現在我無法從文件中讀取音頻。

我讀取音頻的算法是:

  • 初始化所需的結構:
vorbis_info info;
vorbis_comment comment;
vorbis_dsp_state dsp;
vorbis_block block;

vorbis_info_init(&info);
vorbis_comment_init(&comment);
  • 閱讀標題:
    • 調用vorbis_synthesis_headerin(&info, &comment, packet); 直到它返回OV_ENOTVORBIS
    • vorbis_synthesis_init(&dsp, &info);
    • vorbis_block_init(&dsp, &block);
    • 將第一個非頭包傳給下面的function
  • 解析數據包,直到audioReady == READY
putPacket(ogg_packet *packet) {
    int ret;
    ret = vorbis_synthesis(&block, packet);
    if( ret == 0 ) {
        ret = vorbis_synthesis_blockin(&dsp, &block);
        audioReady = (ret == 0) ? READY : NOT_READY;
    } else {
        audioReady = NOT_READY;
    }
}
  • 讀取音頻數據:
float** rawData = nullptr;
readSamples = vorbis_synthesis_pcmout(&dsp, &rawData);
if( readSamples == 0 ) {
    audioReady = NOT_READY;
    return;
}

int16_t* newData = new int16_t[readSamples * getChannels()];
int16_t* dst = newData;
for(unsigned int i=0; i<readSamples; ++i) {
    for(unsigned char ch=0; ch<getChannels(); ++ch) {
        *(dst++) = math::clamp<int16_t>(rawData[ch][i]*32767 + 0.5f, -32767, 32767);
    }
}
audioData.push_back({readSamples * getChannels() , newData});
vorbis_synthesis_read(&dsp, static_cast<int>(readSamples));

audioReady = NOT_READY;

這就是它出錯的地方:在檢查newData內容后發現它包含一個非常安靜的聲音。 我懷疑它是否是正確的數據,這意味着我的算法在某個地方我做錯了什么。

我試圖找到一些類似程序的例子,但我得到的只是帶有非常像意大利面條的代碼的源代碼,它似乎執行與我相同的算法,但它們完成了他們的工作。 (有一個這樣的庫: https://github.com/icculus/theoraplay

我有什么理由在我的申請中(幾乎)保持沉默?

PS:如果您想知道我是否可能會收到錯誤的 OGG 數據包,那么我向您保證我的這部分代碼工作正常,因為我也在使用相同的代碼從同一個文件中讀取視頻數據,並且它顯示了正確的視頻.

我找到了:在讀取數據包期間,我假設一個 Ogg 頁面 = 一個 Ogg 數據包。 我錯了:對於音頻,一頁可以包含許多數據包。 要正確閱讀它,必須編寫如下代碼:

do{
   putPacket(&packet);
}while( ogg_stream_packetout(&state, &packet) == 1 );

我犯了這個錯誤是因為對於視頻數據包(我首先做了),一個頁面只包含一個數據包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM