Opus, can't encode / decode all data

Viewed 72

I'm working using Opus to encode and decode some Portaudio recorded data, so i'm trying to encode a record of around 10 seconds, i've wrote the registered data to a log file and i get around 480000 lines which represent the NUM_SECONDS * SAMPLE_rate, so the time * the frequency.

Problem is when i try to use opus with it, i can encode and decode, but the size of the data returned is not the same at all, it is the FRAMES_PER_BUFFER size, and the data itself does not correspond to anything from the previous data. Here is my code :

my encode and decode methods :

typedef struct {
    int size;
    std::vector<unsigned char> *sound;
} AudioEnc;

typedef struct {
    int size;
    std::vector<float> sound;
} AudioData;

#define SAMPLE_RATE (48000)
#define FRAMES_PER_BUFFER (1920)
#define NUM_SECONDS (10)
#define NUM_CHANNELS (1)

AudioEnc &Opus::Encode(AudioData &data)
{
    AudioEnc enc;
    enc.sound = new std::vector<unsigned char>;
    const unsigned int max_size =  NUM_SECONDS * SAMPLE_RATE * sizeof(float);
    float sound[max_size];

    enc.sound->resize(NUM_SECONDS * SAMPLE_RATE);
    if (data.size == 0)
        enc.sound->resize(opus_encode_float(_encode, sound, FRAMES_PER_BUFFER, enc.sound->data(), max_size));
    else
        enc.sound->resize(opus_encode_float(_encode, data.sound.data(), FRAMES_PER_BUFFER, enc.sound->data(), max_size));
    return (enc);
}

AudioData Opus::Decode(AudioEnc &data)
{
    AudioData dec;
    dec.sound.resize(NUM_SECONDS * SAMPLE_RATE);
    dec.sound.resize(opus_decode_float(_decode, data.sound->data(), data.sound->size(), dec.sound.data(), FRAMES_PER_BUFFER, 0));
    return (dec);
}

Record data is to big for pastebin, do you guys have any idea on what i'm missing ?

0 Answers
Related