2
votes

I'm capturing the audio stream of a voice chat program (it is proprietary, closed-source and I have no control over it) which is encoded with the OPUS Codec, and I want to decode it into raw PCM audio (Opus Decoder doc).

What I'm doing is:

  1. Create an OPUS decoder: opusDecoder = opus_decoder_create(48000, 1, &opusResult);
  2. Decode the stream: opusResult = opus_decode(opusDecoder, voicePacketBuffer, voicePacketLength, pcm, 9600, 0);
  3. Save it to a file: pcmFile.write(pcm, opusResult * sizeof(opus_int16));
  4. Read the file with Audacity (File > Import > Raw Data...)

Here comes the problem: sometimes it works perfectly well (I can hear the decoded PCM audio without glitch and with the original speed) but sometimes, the decoded audio stream is in "slow motion" (sometimes a little slower than normal, sometimes much slower).

I can't find out why because I don't change my program: the decoding settings remain the same. Yet, sometimes it works, sometimes it doesn't. Also, opus_decode() is always able to decode the data, it doesn't return an error code.

I read that the decoder has a "state" (opus_decoder_ctl() doc). I thought maybe time between opus_decode() calls is important?

Can you think of any parameter, be it explicit (like the parameters given to the functions) or implicit (time between two function calls), that might cause this effect?

1
I have no experience with the OPUS decoder, but hearing a sound "in slow motion" can mean that you're not playing it back at the same rate it was recorded. Could it mean the audio stream can have different sampling rate from time to time? Are you sure it's always 48,000?Martin Véronneau

1 Answers

2
votes

"Slow motion" audio is almost always mismatch of sampling rate (recorded on high rate but played in low rate). For example if you record audio on 48kHz but play it as 8kHz.

Another possible reason of "slow motion" is more than one stream decoded by the same decoder. But in this case you also get distorted slow audio.

As for OPUS:

  • It always decode in rate that you specified in create parameters.
  • Inside it has pure math (without any timers or realtime related things) so it is not important when you call decode function.

Therefore some troubleshooting advises:

  • Make sure that you do not create decoder with different sampling rates
  • Make sure that when you import raw file in audacity you always import it in 48kHz mono
  • If any above do not help - check how many bytes you receive from decoder on each packet in normal/slow motion cases. For normal audio streams (with uniform inter-packet time) you always get the same number of raw audio samples.