9.22. opus编解码以及ogg封装

概述

本文档说明介绍opus的编解码配置与注意事项, ogg封装注意事项等

9.22.1. 操作说明

  • app_config.h 文件中, 确保打开

    打开宏#define CONFIG_OPUS_ENC_ENABLE

    打开宏#define CONFIG_OPUS_DEC_ENABLE

9.22.2. 应用示例

编码配置代码如下:

req.enc.channel_bit_map     = BIT(CONFIG_AUDIO_ADC_CHANNEL_L);
req.enc.frame_size          = sample_rate / 10 * 4 * channel;       //收集够多少字节PCM数据就回调一次fwrite
req.enc.output_buf_len      = req.enc.frame_size * 3; //底层缓冲buf至少设成3倍frame_size
req.enc.cmd                 = AUDIO_ENC_OPEN;
req.enc.channel             = 1;
req.enc.volume              = __this->gain;
req.enc.sample_rate         = sample_rate;
req.enc.format              = "opus";
req.enc.sample_source       = __this->sample_source;
req.enc.vfs_ops             = &recorder_vfs_ops;
req.enc.file                = (FILE *)&__this->save_cbuf;
req.enc.frame_head_reserve_len = 0;
req.enc.bitrate             = 16000;//64000, 32000, 16000
if(!strcmp(req.enc.format, "opus")){
    req.enc.no_header = 1;
}

Note

  • 编码需要注意的地方: 如果no_header是0, 则编码出来后每一帧开始偏移2个字节, 用于某种特定通讯如酷狗等, frame_head_reserve_len默认为0, 如非0, 编码后帧头部预留对应字节大小.

  • opus编码只支持单通道(channel = 1), 只支持采样率为8000和16000

  • opus编码可以设置比特率, 设置选项为16000, 32000, 64000, 如果不设置, 默认为16000.

解码配置代码如下:

req.dec.cmd             = AUDIO_DEC_OPEN;
req.dec.volume          = __this->volume;
req.dec.output_buf_len  = 4 * 1024;
req.dec.channel         = 2;
req.dec.sample_rate     = sample_rate;
req.dec.vfs_ops         = &recorder_vfs_ops;
req.dec.dec_type            = format;
req.dec.sample_source   = CONFIG_AUDIO_DEC_PLAY_SOURCE;
req.dec.file            = (FILE *)&__this->save_cbuf;

Note

  • 解码需要注意的地方: 如果是需要编码与解码对应自编自解, 则编码处必须no_header非0, 解码通道固定2通道(编码1通道, 解码出来会是2通道).

  • 解码拿数据的函数, 如果是传入vfs_ops,确保fread函数读取到解码器所需长度len才返回, 不然数据不连续会导致解码出来的声音有问题.

ogg封装代码如下:

req.enc.channel_bit_map = BIT(CONFIG_AUDIO_ADC_CHANNEL_L);
req.enc.frame_size = sample_rate / 10 * 4 * channel;        //收集够多少字节PCM数据就回调一次fwrite
req.enc.output_buf_len = req.enc.frame_size * 3; //底层缓冲buf至少设成3倍frame_size
req.enc.cmd = AUDIO_ENC_OPEN;
req.enc.channel = 1;
req.enc.volume = __this->gain;
req.enc.sample_rate = sample_rate;
req.enc.format = "ogg";
req.enc.sample_source = __this->sample_source;
req.enc.vfs_ops = &recorder_vfs_ops;
req.enc.file = (FILE *)&__this->save_cbuf;
req.enc.frame_head_reserve_len = 0;

Note

  • 编码需要注意的地方: frame_head_reserve_len默认为0, 如非0, 编码后帧头部预留对应字节大小.

  • ogg封装是opus编码加上了ogg的头部, 只支持单通道(channel = 1), 只支持采样率为8000和16000

-暂时不支持ogg解码.