Reputation: 31
I have a very small piece of C++ code that tries to open an ogg/opus coded file and uses the opus API in order to decode it with the function opus_decode(). The thing is that almost the half of the opus_decode() calls that I do for the same sound return negative (error) codes.. -4 and -2 (invalid package and buffer too short) that I can`t solve. The output is like
N decoded: 960 N decoded: -4 N decoded: -4 N decoded: 960 N decoded: -4 N decoded: 1920 N decoded: 960 N decoded: -4 N decoded: -4
and so on.
#include <string.h>
#include <opus/opus.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdio>
#include <iostream>
#include <fstream>
#define LEN 1024
#define FREQ 48000
#define CHANNELS 1
#define FRAMESIZE 1920
int main(int argc, char *argv[]) {
int size = opus_decoder_get_size(CHANNELS);
OpusDecoder *decoders = (OpusDecoder*)malloc(size);
int error = opus_decoder_init(decoders, FREQ, CHANNELS);
std::ifstream inputfile;
inputfile.open("/home/vir/Descargas/detodos.opus"); //48000Hz, Mono
char input[LEN];
opus_int16 *data = (opus_int16*)calloc(CHANNELS*FRAMESIZE,sizeof(opus_int16));
if(inputfile.is_open())
while (!inputfile.eof()) {
inputfile >> input;
std::cerr << "N decoded: " << opus_decode(decoders, (const unsigned char*)&input[0], LEN, data, FRAMESIZE, 0) << "\n";
}
return error;
}
Upvotes: 3
Views: 3333
Reputation: 101
It seems you are using Opus-Tools instead of OpusFile. Obviously you have linked against the libopus.a
library, but you also need to download and build OpusFile 0.7 and link your program against libopusfile.a
created from building OpusFile. Include opusfile.h
in your program from OpusFile 0.7. Lastly you need to download and build the libogg library from downloading libogg 1.3.2 from xiph.org/downloads and link against libogg library.
This link is the documentation explaining how to open and close ogg opus streams for decoding.
Make sure you have an ogg opus file and open the stream using...
OggOpusFile *file = op_open_file(inputfile, error)(inputfile is char* inputfile and error is an int pointer)
Close the stream with op_free(file)
. This is the function documentation to actually decode an ogg opus stream. Before you call op_free decode the audio data with...
op_read(file,buffer,bufferSize,null), buffer is opus_int16 pcm[120*48*2]
bufferSize
is sizeof(pcm)/sizeof(*pcm)
. op_read
will decode more of the file, so put it in a for
loop until op_read
returns 0
.
Upvotes: 8