【发布时间】:2015-03-12 15:57:15
【问题描述】:
我想使用 openal (linux) 流式传输音频,但我得到的是噪音和背景中的一些歌曲。 我读了 4 通道 Ambisonic 文件并丢弃了 2 个通道。 我正在向开放缓冲区发送 20000 帧(20000 * 2_channels * 4 字节)。
不知道哪里出了问题。我认为这段代码是正确的。 负责加载音频文件块并将其转换为 2 通道的部分很好,因为我已经对其进行了测试。我已将输出写入文件并获得了高质量的 2 通道 wav。
Ambisonic 文件有 4 个通道和 48kHz 采样率。我创建了没有附加参数的开放上下文。
有什么建议吗?
const int NUM_BUFFERS = 2;
const int BUFFER_SIZE = 20000; // frames
ALuint source, buffers[NUM_BUFFERS];
int streamBuffer(ALuint bufID, int bufferSize, AudioFile &audio) {
const SF_INFO &info = audio.getInfo();
const lli numFrames = bufferSize;
float *bufHrtf = new float[bufferSize * 2];
float *buf = nullptr;
audio.readFrames(numFrames, &buf);
lli idxHrtf = 0;
for (lli j = 0; j < numFrames * info.channels; j += info.channels) {
bufHrtf[idxHrtf++] = buf[j + 1];
bufHrtf[idxHrtf++] = buf[j + 2];
}
alBufferData(bufID, AL_FORMAT_STEREO16, bufHrtf, idxHrtf * sizeof(float), info.samplerate);
delete [] bufHrtf;
}
int main(int argc, char **argv) {
AudioFile audio;
if (!audio.open("sample.amb")) {
return 1;
}
if (!createOpenAlAndBuffer()) {
return 1;
}
streamBuffer(buffers[0], BUFFER_SIZE, audio);
streamBuffer(buffers[1], BUFFER_SIZE, audio);
alSourceQueueBuffers(source, 2, buffers);
alSourcePlay(source);
while(!audio.endOfFile())
{
// Check how much data is processed in OpenAL's internal queue
ALint processed{0};
alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
// add more buffers while we need them
while (processed--)
{
ALuint bufID;
alSourceUnqueueBuffers(source, 1, &bufID);
streamBuffer(bufID, BUFFER_SIZE, audio);
alSourceQueueBuffers(source, 1, &bufID);
int val{0};
alGetSourcei(source, AL_SOURCE_STATE, &val);
if(val != AL_PLAYING) {
alSourcePlay(source);
}
}
}
destroyOpenAl();
return 0;
}
【问题讨论】: