Skip to content

Commit

Permalink
Merge identical code to reduce duplication
Browse files Browse the repository at this point in the history
Signed-off-by: imliubo <imliubo@makingfun.xyz>
  • Loading branch information
imliubo committed Dec 24, 2024
1 parent 3a662a9 commit 3817536
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions src/media.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@

#include "main.h"

#if CONFIG_OPENAI_BOARD_ESP32_S3
#define OPUS_OUT_BUFFER_SIZE 1276 // 1276 bytes is recommended by opus_encode

#if CONFIG_OPENAI_BOARD_ESP32_S3 // Default ESP32-S3 board config
#define SAMPLE_RATE 8000
#define BUFFER_SAMPLES 320

#define I2S_DATA_OUT_PORT I2S_NUM_0
#define I2S_DATA_IN_PORT I2S_NUM_1
#define MCLK_PIN 0
#define DAC_BCLK_PIN 15
#define DAC_LRCLK_PIN 16
#define DAC_DATA_PIN 17
#define ADC_BCLK_PIN 38
#define ADC_LRCLK_PIN 39
#define ADC_DATA_PIN 40
#elif CONFIG_OPENAI_BOARD_M5_ATOMS3R
#elif CONFIG_OPENAI_BOARD_M5_ATOMS3R // ATOMS3R board with Atomic EchoBase
#include "es8311.h"
#define OPUS_OUT_BUFFER_SIZE 1276 // 1276 bytes is recommended by opus_encode
#define SAMPLE_RATE 16000 //! EchoBase not support 8K sample rate :)
#define BUFFER_SAMPLES (320 * 2)
#define SAMPLE_RATE 16000 //! EchoBase not support 8K sample rate :)
#define BUFFER_SAMPLES (320 * 2)

#define I2C_PORT I2C_NUM_1
#define I2C_PORT I2C_NUM_1
#define I2C_FREQ_HZ 400000
#define I2C_SCL_PIN 39
#define I2C_SDA_PIN 38

#define I2S_PORT I2S_NUM_1
#define MCLK_PIN -1
#define I2S_DATA_OUT_PORT I2S_NUM_1
#define I2S_DATA_IN_PORT I2S_DATA_OUT_PORT
#define MCLK_PIN -1
#define DAC_BCLK_PIN 8
#define DAC_LRCLK_PIN 6
#define DAC_DATA_PIN 5
Expand All @@ -53,7 +56,7 @@ void oai_init_audio_capture() {
.use_apll = 1,
.tx_desc_auto_clear = true,
};
if (i2s_driver_install(I2S_NUM_0, &i2s_config_out, 0, NULL) != ESP_OK) {
if (i2s_driver_install(I2S_DATA_OUT_PORT, &i2s_config_out, 0, NULL) != ESP_OK) {
printf("Failed to configure I2S driver for audio output");
return;
}
Expand All @@ -65,11 +68,11 @@ void oai_init_audio_capture() {
.data_out_num = DAC_DATA_PIN,
.data_in_num = I2S_PIN_NO_CHANGE,
};
if (i2s_set_pin(I2S_NUM_0, &pin_config_out) != ESP_OK) {
if (i2s_set_pin(I2S_DATA_OUT_PORT, &pin_config_out) != ESP_OK) {
printf("Failed to set I2S pins for audio output");
return;
}
i2s_zero_dma_buffer(I2S_NUM_0);
i2s_zero_dma_buffer(I2S_DATA_OUT_PORT);

i2s_config_t i2s_config_in = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
Expand All @@ -82,7 +85,7 @@ void oai_init_audio_capture() {
.dma_buf_len = BUFFER_SAMPLES,
.use_apll = 1,
};
if (i2s_driver_install(I2S_NUM_1, &i2s_config_in, 0, NULL) != ESP_OK) {
if (i2s_driver_install(I2S_DATA_IN_PORT, &i2s_config_in, 0, NULL) != ESP_OK) {
printf("Failed to configure I2S driver for audio input");
return;
}
Expand All @@ -94,7 +97,7 @@ void oai_init_audio_capture() {
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = ADC_DATA_PIN,
};
if (i2s_set_pin(I2S_NUM_1, &pin_config_in) != ESP_OK) {
if (i2s_set_pin(I2S_DATA_IN_PORT, &pin_config_in) != ESP_OK) {
printf("Failed to set I2S pins for audio input");
return;
}
Expand Down Expand Up @@ -148,23 +151,23 @@ void oai_init_audio_capture() {
.use_apll = 1,
.tx_desc_auto_clear = true,
};
if (i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL) != ESP_OK) {
if (i2s_driver_install(I2S_DATA_IN_PORT, &i2s_config, 0, NULL) != ESP_OK) {
printf("Failed to configure I2S driver for audio input/output");
return;
}

i2s_pin_config_t pin_config_out = {
.mck_io_num = -1,
.mck_io_num = MCLK_PIN,
.bck_io_num = DAC_BCLK_PIN,
.ws_io_num = DAC_LRCLK_PIN,
.data_out_num = DAC_DATA_PIN,
.data_in_num = ADC_DATA_PIN,
};
if (i2s_set_pin(I2S_PORT, &pin_config_out) != ESP_OK) {
if (i2s_set_pin(I2S_DATA_IN_PORT, &pin_config_out) != ESP_OK) {
printf("Failed to set I2S pins for audio output");
return;
}
i2s_zero_dma_buffer(I2S_PORT);
i2s_zero_dma_buffer(I2S_DATA_IN_PORT);
#endif
}

Expand All @@ -188,13 +191,8 @@ void oai_audio_decode(uint8_t *data, size_t size) {

if (decoded_size > 0) {
size_t bytes_written = 0;
#if CONFIG_OPENAI_BOARD_ESP32_S3
i2s_write(I2S_NUM_0, output_buffer, BUFFER_SAMPLES * sizeof(opus_int16),
&bytes_written, portMAX_DELAY);
#elif CONFIG_OPENAI_BOARD_M5_ATOMS3R
i2s_write(I2S_PORT, output_buffer, BUFFER_SAMPLES * sizeof(opus_int16),
i2s_write(I2S_DATA_OUT_PORT, output_buffer, BUFFER_SAMPLES * sizeof(opus_int16),
&bytes_written, portMAX_DELAY);
#endif
}
}

Expand Down Expand Up @@ -227,13 +225,8 @@ void oai_init_audio_encoder() {
void oai_send_audio(PeerConnection *peer_connection) {
size_t bytes_read = 0;

#if CONFIG_OPENAI_BOARD_ESP32_S3
i2s_read(I2S_NUM_1, encoder_input_buffer, BUFFER_SAMPLES, &bytes_read,
i2s_read(I2S_DATA_IN_PORT, encoder_input_buffer, BUFFER_SAMPLES, &bytes_read,
portMAX_DELAY);
#elif CONFIG_OPENAI_BOARD_M5_ATOMS3R
i2s_read(I2S_PORT, encoder_input_buffer, BUFFER_SAMPLES, &bytes_read,
portMAX_DELAY);
#endif

auto encoded_size =
opus_encode(opus_encoder, encoder_input_buffer, BUFFER_SAMPLES / 2,
Expand Down

0 comments on commit 3817536

Please sign in to comment.