我想播放一个Ogg/Vorbis音频/视频文件,但现在我无法从文件中读取音频。
我的音频读取算法是:
vorbis_info info;
vorbis_comment comment;
vorbis_dsp_state dsp;
vorbis_block block;
vorbis_info_init(&info);
vorbis_comment_init(&comment);OV_ENOTVORBIS
vorbis_synthesis_init(&dsp, &info);
vorbis_block_init(&dsp, &block);
vorbis_synthesis_headerin(&info, &comment, packet);,直到它返回below的第一个非标头数据包。
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数据包弄错了,那么我向你保证我的这部分代码是正确的,因为我也从同一个文件中读取视频数据,使用相同的代码,它显示视频是正确的。
发布于 2022-07-25 15:46:14
我发现了:在读取数据包时,我假设一个Ogg =一个Ogg包。我错了:对于音频,一个页面可以包含很多数据包。要正确阅读它,必须编写如下代码:
do{
putPacket(&packet);
}while( ogg_stream_packetout(&state, &packet) == 1 );我犯了这个错误,因为对于视频包(我首先做的),一个页面只包含一个包。
https://stackoverflow.com/questions/73100675
复制相似问题