我有一个非常小的C++代码,它尝试打开一个ogg/opus编码文件,并使用opus来用函数opus_decode()对它进行解码。问题是,几乎一半的opus_decode()调用相同的声音返回负(错误)代码。-4和-2 (无效的包裹和缓冲区太短),我无法解决。输出就像
N解码: 960 N解码:-4 N解码: 1920 N解码: 960 N解码:-4 N解码
诸若此类。
#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;
}发布于 2016-08-30 02:22:10
看来您使用的是Opus工具而不是OpusFile。显然,您已经针对libopus.a库进行了链接,但您还需要下载和构建OpusFile 0.7,并根据构建OpusFile创建的libopusfile.a链接您的程序。将opusfile.h包含在OpusFile 0.7的程序中。最后,您需要下载并构建libogg库,从Xiph.org/下载下载libogg 1.3.2并链接到该库。
这个链接是解释如何打开和关闭opus流以进行解码的文档。
确保您有ogg opus文件并使用.
OggOpusFile *file = op_open_file(inputfile, error)(inputfile is char* inputfile and error is an int pointer)使用op_free(file)关闭流。这是实际解码opus流的功能文档。在调用op_free之前,用.
op_read(file,buffer,bufferSize,null), buffer is opus_int16 pcm[120*48*2]bufferSize是sizeof(pcm)/sizeof(*pcm)。op_read将解码更多的文件,因此将其放在for循环中,直到op_read返回0为止。
https://stackoverflow.com/questions/37816289
复制相似问题