Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portaudio + Opus encoding / decoding audio input

I'm working on a VOIP client using Portaudio and opus. I read from the microphone in a frame -encode each frame with Opus and put it in a list -pop the first element from the list and decode it -read it with portaudio

If i do the same thing without encoding my sound it works great. But when I use Opus my sound is bad, I can't understand the voice (which is bad for a voip client)

HandlerOpus::HandlerOpus(int sample_rate, int num_channels)
    {
        this->num_channels = num_channels;
        this->enc = opus_encoder_create(sample_rate, num_channels, OPUS_APPLICATION_VOIP, &this->error);
        this->dec = opus_decoder_create(sample_rate, num_channels, &this->error);

        opus_int32 rate;

        opus_encoder_ctl(enc, OPUS_GET_BANDWIDTH(&rate));
        this->encoded_data_size = rate;
    }

    HandlerOpus::~HandlerOpus(void)
    {
        opus_encoder_destroy(this->enc);
        opus_decoder_destroy(this->dec);
    }

    unsigned char *HandlerOpus::encodeFrame(const float *frame, int frame_size)
    {
        unsigned char *compressed_buffer;
        int ret;

        compressed_buffer = new (unsigned char[this->encoded_data_size]);
        ret = opus_encode_float(this->enc, frame, frame_size, compressed_buffer, this->encoded_data_size);
        return (compressed_buffer);
    }

    float *HandlerOpus::decodeFrame(const unsigned char *data, int frame_size)
    {
        int ret;
        float *frame = new (float[frame_size * this->num_channels]);

        opus_packet_get_nb_channels(data);
        ret = opus_decode_float(this->dec, data, this->encoded_data_size, frame, frame_size, 0);
        return (frame);
    }

I can't change the library I have to use Opus. The sample rate is 48000 and the frames per buffer is 480 and I tried in mono and stereo.

What am I doing wrong?

like image 318
Wawa08 Avatar asked Nov 04 '22 09:11

Wawa08


2 Answers

I solved the problem myself I changed the config : The sample rate to 24000 and the frames per buffer is still 480.

like image 85
Wawa08 Avatar answered Nov 12 '22 16:11

Wawa08


It's 6 years later, but I'm gonna post an answer for future googlers like me:

I had very similiar problem and fixed it by changing PortAudio sample type to paInt32 and switched from opus_decode_float to just opus_decode

like image 31
Alan Kałuża Avatar answered Nov 12 '22 18:11

Alan Kałuża