我有一小段C++代码,试图打开一个ogg / opus编码文件并使用opus API以便通过功能opus_decode()对其进行解码。事实是,对于相同的声音,我执行的opus_decode()调用几乎有一半返回负(错误)代码。-4和-2(无效的包和缓冲区太短)我无法解决。输出就像



等等。

#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;
}

最佳答案

看来您使用的是Opus-Tools,而不是OpusFile。显然,您已经链接到libopus.a库,但是您还需要下载并构建OpusFile 0.7,并将您的程序链接到通过构建OpusFile创建的libopusfile.a。从OpusFile 0.7将opusfile.h包含在您的程序中。最后,您需要从xiph.org/downloads下载libogg 1.3.2并链接到该库,以下载并构建libogg库。

This link是说明如何打开和关闭ogg opus流以进行解码的文档。

确保您有一个ogg opus文件,然后使用...打开流。

OggOpusFile *file = op_open_file(inputfile, error)(inputfile is char* inputfile and error is an int pointer)

使用op_free(file)关闭流。这是function documentation,用于实际解码ogg opus流。在调用op_free之前,请使用以下命令解码音频数据:
op_read(file,buffer,bufferSize,null), buffer is opus_int16 pcm[120*48*2]
bufferSizesizeof(pcm)/sizeof(*pcm)op_read将解码文件的更多内容,因此将其放入for循环中,直到op_read返回0为止。

关于c++ - 解码Ogg/Opus文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37816289/

10-13 07:09