diff --git a/Software/UPSoftware/examples/OpusLoopbackEsp32C3/OpusLoopbackEsp32C3.ino b/Software/UPSoftware/examples/OpusLoopbackEsp32C3/OpusLoopbackEsp32C3.ino new file mode 100644 index 0000000..baee319 --- /dev/null +++ b/Software/UPSoftware/examples/OpusLoopbackEsp32C3/OpusLoopbackEsp32C3.ino @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace audio_tools; + +constexpr int I2S_BCLK = 6; +constexpr int I2S_LRC = 7; +constexpr int I2S_DIN = 5; + +// Keep the test intentionally light so it is realistic for an ESP32-C3. +AudioInfo audioInfo(16000, 1, 16); +SineWaveGenerator sineWave(10000); +GeneratedSoundStream source(sineWave); +I2SStream i2s; +OpusAudioEncoder opusEncoder; +OpusAudioDecoder opusDecoder; +EncodedAudioStream decodedAudio(&i2s, &opusDecoder); +EncodedAudioStream encodedAudio(&decodedAudio, &opusEncoder); +StreamCopy copier(encodedAudio, source); + +void setup() { + Serial.begin(115200); + AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning); + + auto i2sConfig = i2s.defaultConfig(TX_MODE); + i2sConfig.copyFrom(audioInfo); + i2sConfig.pin_bck = I2S_BCLK; + i2sConfig.pin_ws = I2S_LRC; + i2sConfig.pin_data = I2S_DIN; + i2sConfig.buffer_count = 8; + i2sConfig.buffer_size = 256; + i2s.begin(i2sConfig); + + auto sineConfig = sineWave.defaultConfig(); + sineConfig.copyFrom(audioInfo); + sineWave.begin(sineConfig, N_B4); + + auto &encoderConfig = opusEncoder.config(); + encoderConfig.copyFrom(audioInfo); + encoderConfig.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY; + encoderConfig.bitrate = 24000; + encoderConfig.complexity = 1; + encoderConfig.frame_sizes_ms_x2 = OPUS_FRAMESIZE_20_MS; + + decodedAudio.begin(audioInfo); + encodedAudio.begin(audioInfo); + + Serial.println("Opus loopback test started"); +} + +void loop() { + if (copier.copy() == 0) { + delay(1); + } +} diff --git a/Software/UPSoftware/examples/OpusLoopbackEsp32C3/README.md b/Software/UPSoftware/examples/OpusLoopbackEsp32C3/README.md new file mode 100644 index 0000000..a9d23f3 --- /dev/null +++ b/Software/UPSoftware/examples/OpusLoopbackEsp32C3/README.md @@ -0,0 +1,26 @@ +This sketch is a minimal ESP32-C3 Opus codec smoke test. + +What it does: +- Generates a sine wave locally +- Encodes it with Opus +- Decodes it immediately again +- Sends the decoded PCM to I2S + +Why this example: +- It tests the raw Opus codec path without Ogg or CAN framing +- It is closer to the later Pi -> CAN -> ESP32 decoder path than MP3 +- It keeps CPU load down by using 16 kHz mono and low encoder complexity + +Expected hardware: +- ESP32-C3 +- I2S DAC / amp on: + - BCLK = GPIO 6 + - LRCK = GPIO 7 + - DIN = GPIO 5 + +Required Arduino libraries: +- `arduino-audio-tools` +- `arduino-libopus` + +If this sketch runs and outputs a stable sine tone, the basic Opus encode/decode +chain is working on the ESP32-C3.