Skip to content

OggOpusEncoder bug #2115

@smokesea

Description

@smokesea

Problem Description

read audio from microphone, encode it using OGG OPUs, transfer it to PC via network, and check the encoded file.

$ opusinfo test.s
Processing file "test.s"...

New logical stream (#1, serial: 11b6e847): type opus
Encoded with Arduino
WARNING: Samples with negative granpos in stream 1
WARNING: Sample count ahead of granule (3840>1959) in stream 1
WARNING: Sample count ahead of granule (5760>2000) in stream 1
WARNING: Sample count ahead of granule (7680>2036) in stream 1
WARNING: Sample count ahead of granule (9600>2072) in stream 1
WARNING: Sample count ahead of granule (11520>2104) in stream 1
WARNING: Sample count ahead of granule (13440>2142) in stream 1
WARNING: Sample count ahead of granule (15360>2182) in stream 1

Device Description

borad: esp32s3,psram enabled

Sketch

audio_encode.h
#include "Arduino.h"
#include "AudioTools.h"
#include "global.h"

//inmp441 pin
#define AUDIO_I2S_MIC_GPIO_WS 4
#define AUDIO_I2S_MIC_GPIO_SCK 5
#define AUDIO_I2S_MIC_GPIO_DIN 6

// I2S port
#define I2S_PORT I2S_NUM_0

#define AUDIO_SAMPLE_RATE     16000  // 44100
#define AUDIO_OPUS_FRAME_MS   60    
#define AUDIO_OPUS_BITRATE    16000 
#define AUDIO_OPUS_COMPLEXITY 0    
#define AUDIO_CHANNELS 1


void EncodeTask(void *param);



audio_encode.cpp

#include "audio_encode.h"
#include "AudioTools/AudioCodecs/CodecOpusOgg.h"
#include "AudioTools/Communication/WebSocketOutput.h"

AudioInfo encodeInfo(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, 16);
I2SStream mic;
WebSocketOutput wsOut(webSocket);

OpusOggEncoder opusOggEncoder;
EncodedAudioStream audioEncoder(&wsOut, &opusOggEncoder);
StreamCopy copier(audioEncoder, mic);

void startAudioRecord(){
    // AudioLogger::instance().begin(Serial, AudioLogger::Debug);
    webSocket.sendTXT(":micOn");

    auto cfg = mic.defaultConfig(RX_MODE);
    cfg.setAudioInfo(encodeInfo);
    cfg.sample_rate = AUDIO_SAMPLE_RATE;
    cfg.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
    cfg.channels = I2S_CHANNEL_MONO;
    cfg.i2s_format = I2S_STD_FORMAT;
    cfg.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT;
    cfg.use_apll = false;
    cfg.is_master = true;
    cfg.fixed_mclk = 0;
    cfg.pin_ws = AUDIO_I2S_MIC_GPIO_WS;
    cfg.pin_bck = AUDIO_I2S_MIC_GPIO_SCK;
    cfg.pin_data_rx = AUDIO_I2S_MIC_GPIO_DIN;
    cfg.pin_data = I2S_PIN_NO_CHANGE;
    mic.driver()->setWaitTimeReadMs(AUDIO_OPUS_FRAME_MS);
    mic.begin(cfg);
    //wait for mic to start ??
    vTaskDelay(pdMS_TO_TICKS(50));

    opusOggEncoder.setAudioInfo(encodeInfo);
    auto& cfgEncoder = opusOggEncoder.config();
    cfgEncoder.application = OPUS_APPLICATION_VOIP;
    cfgEncoder.bitrate = AUDIO_OPUS_BITRATE;
    cfgEncoder.sample_rate = AUDIO_SAMPLE_RATE; 
    cfgEncoder.complexity = AUDIO_OPUS_COMPLEXITY; 
    cfgEncoder.frame_sizes_ms_x2 = OPUS_FRAMESIZE_60_MS;
    cfgEncoder.channels = AUDIO_CHANNELS;
    opusOggEncoder.begin();
    
    while(globalData.getMicStatus()){
        int begin = millis();
        size_t cc = copier.copy();
        int delay_ms = millis() - begin;        
        if (delay_ms < AUDIO_OPUS_FRAME_MS){
            vTaskDelay(AUDIO_OPUS_FRAME_MS - delay_ms);
        }
    }
    
    copier.end();
    vTaskDelay(10);
    webSocket.sendTXT(":micOff");
    Serial.println("Mic Off");
}


void EncodeTask(void *param){

    while(1){
        if (!WiFi.isConnected()){
            vTaskDelay(100);
            continue;
        }
        if ( globalData.getSpeaking() || !webSocket.isConnected())
        {
            vTaskDelay(10);
            continue;
        }
        if(globalData.getMicStatus()){
            Serial.println("Start Audio Record");
            startAudioRecord();
            Serial.println("End Audio Record");
        }
        vTaskDelay(100);
    }
}

Other Steps to Reproduce

My master, please guide me

What is your development environment (incl. core version info)

framework: arduino
platform: platform.io

I have checked existing issues, discussions and online documentation

  • I confirm I have checked existing issues, discussions and online documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions