|
1 | | -#include <driver/i2s.h> |
2 | 1 | #include <opus.h> |
| 2 | +#include <stdio.h> |
3 | 3 |
|
4 | 4 | #include "main.h" |
| 5 | +#include "platform.h" |
5 | 6 |
|
6 | 7 | #define OPUS_OUT_BUFFER_SIZE 1276 // 1276 bytes is recommended by opus_encode |
7 | | -#define SAMPLE_RATE 8000 |
8 | | -#define BUFFER_SAMPLES 320 |
9 | | - |
10 | | -#define MCLK_PIN 0 |
11 | | -#define DAC_BCLK_PIN 15 |
12 | | -#define DAC_LRCLK_PIN 16 |
13 | | -#define DAC_DATA_PIN 17 |
14 | | -#define ADC_BCLK_PIN 38 |
15 | | -#define ADC_LRCLK_PIN 39 |
16 | | -#define ADC_DATA_PIN 40 |
17 | 8 |
|
18 | 9 | #define OPUS_ENCODER_BITRATE 30000 |
19 | 10 | #define OPUS_ENCODER_COMPLEXITY 0 |
20 | 11 |
|
21 | | -void oai_init_audio_capture() { |
22 | | - i2s_config_t i2s_config_out = { |
23 | | - .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX), |
24 | | - .sample_rate = SAMPLE_RATE, |
25 | | - .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, |
26 | | - .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, |
27 | | - .communication_format = I2S_COMM_FORMAT_I2S_MSB, |
28 | | - .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, |
29 | | - .dma_buf_count = 8, |
30 | | - .dma_buf_len = BUFFER_SAMPLES, |
31 | | - .use_apll = 1, |
32 | | - .tx_desc_auto_clear = true, |
33 | | - }; |
34 | | - if (i2s_driver_install(I2S_NUM_0, &i2s_config_out, 0, NULL) != ESP_OK) { |
35 | | - printf("Failed to configure I2S driver for audio output"); |
36 | | - return; |
37 | | - } |
38 | | - |
39 | | - i2s_pin_config_t pin_config_out = { |
40 | | - .mck_io_num = MCLK_PIN, |
41 | | - .bck_io_num = DAC_BCLK_PIN, |
42 | | - .ws_io_num = DAC_LRCLK_PIN, |
43 | | - .data_out_num = DAC_DATA_PIN, |
44 | | - .data_in_num = I2S_PIN_NO_CHANGE, |
45 | | - }; |
46 | | - if (i2s_set_pin(I2S_NUM_0, &pin_config_out) != ESP_OK) { |
47 | | - printf("Failed to set I2S pins for audio output"); |
48 | | - return; |
49 | | - } |
50 | | - i2s_zero_dma_buffer(I2S_NUM_0); |
| 12 | +const auto kCaptureFrameSize = kCaptureSampleRate * 20 / 1000; |
| 13 | +const auto kPlaybackFrameSize = kPlaybackSampleRate * 20 / 1000; |
51 | 14 |
|
52 | | - i2s_config_t i2s_config_in = { |
53 | | - .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX), |
54 | | - .sample_rate = SAMPLE_RATE, |
55 | | - .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, |
56 | | - .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, |
57 | | - .communication_format = I2S_COMM_FORMAT_I2S_MSB, |
58 | | - .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, |
59 | | - .dma_buf_count = 8, |
60 | | - .dma_buf_len = BUFFER_SAMPLES, |
61 | | - .use_apll = 1, |
62 | | - }; |
63 | | - if (i2s_driver_install(I2S_NUM_1, &i2s_config_in, 0, NULL) != ESP_OK) { |
64 | | - printf("Failed to configure I2S driver for audio input"); |
65 | | - return; |
66 | | - } |
67 | | - |
68 | | - i2s_pin_config_t pin_config_in = { |
69 | | - .mck_io_num = MCLK_PIN, |
70 | | - .bck_io_num = ADC_BCLK_PIN, |
71 | | - .ws_io_num = ADC_LRCLK_PIN, |
72 | | - .data_out_num = I2S_PIN_NO_CHANGE, |
73 | | - .data_in_num = ADC_DATA_PIN, |
74 | | - }; |
75 | | - if (i2s_set_pin(I2S_NUM_1, &pin_config_in) != ESP_OK) { |
76 | | - printf("Failed to set I2S pins for audio input"); |
77 | | - return; |
78 | | - } |
79 | | -} |
80 | | - |
81 | | -static opus_int16 *output_buffer = NULL; |
82 | | -static size_t output_buffer_size = BUFFER_SAMPLES * sizeof(opus_int16); |
83 | 15 | static OpusDecoder *opus_decoder = NULL; |
| 16 | +static OpusEncoder *opus_encoder = NULL; |
| 17 | + |
| 18 | +static opus_int16 output_buffer[kPlaybackFrameSize * kPlaybackChannelCount]; |
| 19 | +static opus_int16 input_buffer[kCaptureFrameSize * kCaptureChannelCount]; |
| 20 | +static uint8_t encoder_output_buffer[OPUS_OUT_BUFFER_SIZE]; |
84 | 21 |
|
85 | 22 | void oai_init_audio_decoder() { |
86 | 23 | int decoder_error = 0; |
87 | | - opus_decoder = opus_decoder_create(SAMPLE_RATE, 2, &decoder_error); |
| 24 | + opus_decoder = opus_decoder_create(kPlaybackSampleRate, kPlaybackChannelCount, |
| 25 | + &decoder_error); |
88 | 26 | if (decoder_error != OPUS_OK) { |
89 | 27 | printf("Failed to create OPUS decoder"); |
90 | 28 | return; |
91 | 29 | } |
92 | | - |
93 | | - output_buffer = (opus_int16 *)malloc(output_buffer_size); |
94 | 30 | } |
95 | 31 |
|
96 | 32 | void oai_audio_decode(uint8_t *data, size_t size) { |
97 | | - int decoded_size = |
98 | | - opus_decode(opus_decoder, data, size, output_buffer, BUFFER_SAMPLES, 0); |
| 33 | + int decoded_size = opus_decode(opus_decoder, data, size, output_buffer, |
| 34 | + sizeof(output_buffer), 0); |
99 | 35 |
|
100 | 36 | if (decoded_size > 0) { |
101 | 37 | size_t bytes_written = 0; |
102 | | - i2s_write(I2S_NUM_0, output_buffer, output_buffer_size, |
103 | | - &bytes_written, portMAX_DELAY); |
| 38 | + oai_platform_audio_write((char *)output_buffer, sizeof(output_buffer), |
| 39 | + &bytes_written); |
104 | 40 | } |
105 | 41 | } |
106 | 42 |
|
107 | | -static OpusEncoder *opus_encoder = NULL; |
108 | | -static opus_int16 *encoder_input_buffer = NULL; |
109 | | -static uint8_t *encoder_output_buffer = NULL; |
110 | | - |
111 | 43 | void oai_init_audio_encoder() { |
112 | 44 | int encoder_error; |
113 | | - opus_encoder = opus_encoder_create(SAMPLE_RATE, 1, OPUS_APPLICATION_VOIP, |
114 | | - &encoder_error); |
| 45 | + opus_encoder = opus_encoder_create(kCaptureSampleRate, kCaptureChannelCount, |
| 46 | + OPUS_APPLICATION_VOIP, &encoder_error); |
115 | 47 | if (encoder_error != OPUS_OK) { |
116 | 48 | printf("Failed to create OPUS encoder"); |
117 | 49 | return; |
118 | 50 | } |
119 | 51 |
|
120 | | - if (opus_encoder_init(opus_encoder, SAMPLE_RATE, 1, OPUS_APPLICATION_VOIP) != |
121 | | - OPUS_OK) { |
122 | | - printf("Failed to initialize OPUS encoder"); |
123 | | - return; |
124 | | - } |
125 | | - |
126 | 52 | opus_encoder_ctl(opus_encoder, OPUS_SET_BITRATE(OPUS_ENCODER_BITRATE)); |
127 | 53 | opus_encoder_ctl(opus_encoder, OPUS_SET_COMPLEXITY(OPUS_ENCODER_COMPLEXITY)); |
128 | 54 | opus_encoder_ctl(opus_encoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE)); |
129 | | - encoder_input_buffer = (opus_int16 *)malloc(BUFFER_SAMPLES); |
130 | | - encoder_output_buffer = (uint8_t *)malloc(OPUS_OUT_BUFFER_SIZE); |
131 | 55 | } |
132 | 56 |
|
133 | 57 | void oai_send_audio(PeerConnection *peer_connection) { |
134 | 58 | size_t bytes_read = 0; |
135 | 59 |
|
136 | | - i2s_read(I2S_NUM_1, encoder_input_buffer, BUFFER_SAMPLES, &bytes_read, |
137 | | - portMAX_DELAY); |
| 60 | + oai_platform_audio_read((char *)input_buffer, sizeof(input_buffer), |
| 61 | + &bytes_read); |
138 | 62 |
|
139 | 63 | auto encoded_size = |
140 | | - opus_encode(opus_encoder, encoder_input_buffer, BUFFER_SAMPLES / 2, |
141 | | - encoder_output_buffer, OPUS_OUT_BUFFER_SIZE); |
| 64 | + opus_encode(opus_encoder, input_buffer, kCaptureFrameSize, |
| 65 | + encoder_output_buffer, sizeof(encoder_output_buffer)); |
142 | 66 |
|
143 | 67 | peer_connection_send_audio(peer_connection, encoder_output_buffer, |
144 | 68 | encoded_size); |
|
0 commit comments