From ab6a5a5e07963548525afb98d6ac9957e47bde73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 11 Dec 2022 23:12:00 +0100 Subject: [PATCH] if we get different frames than our own transmission setup, decode and play them anyway --- src/modules/esp32/AudioModule.cpp | 32 +++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/modules/esp32/AudioModule.cpp b/src/modules/esp32/AudioModule.cpp index 4e784e1a9..f29c11056 100644 --- a/src/modules/esp32/AudioModule.cpp +++ b/src/modules/esp32/AudioModule.cpp @@ -47,7 +47,7 @@ int Sine1KHz_index = 0; void run_codec2(void* parameter) { - // 4 bytes of header in each frame Kennung hex c0 de c2 plus the bitrate + // 4 bytes of header in each frame hex c0 de c2 plus the bitrate memcpy(audioModule->tx_encode_frame,&audioModule->tx_header,sizeof(audioModule->tx_header)); while (true) { @@ -76,13 +76,29 @@ void run_codec2(void* parameter) } } if (audioModule->radio_state == RadioState::rx) { - //Make a cycle to get each codec2 frame from the received frame - for (int i = 0; i < audioModule->rx_encode_frame_index; i += audioModule->encode_codec_size) - { - //Decode the codec2 frame - codec2_decode(audioModule->codec2, audioModule->output_buffer, audioModule->rx_encode_frame + i); - size_t bytesOut = 0; - i2s_write(I2S_PORT, &audioModule->output_buffer, audioModule->adc_buffer_size, &bytesOut, pdMS_TO_TICKS(500)); + size_t bytesOut = 0; + if (memcmp(audioModule->rx_encode_frame, &audioModule->tx_header, sizeof(audioModule->tx_header)) == 0) { + // Make a cycle to get each codec2 frame from the received frame + for (int i = 4; i < audioModule->rx_encode_frame_index; i += audioModule->encode_codec_size) + { + //Decode the codec2 frame + codec2_decode(audioModule->codec2, audioModule->output_buffer, audioModule->rx_encode_frame + i); + i2s_write(I2S_PORT, &audioModule->output_buffer, audioModule->adc_buffer_size, &bytesOut, pdMS_TO_TICKS(500)); + } + } else { + // if the buffer header does not match our own codec, make a temp decoding setup. + CODEC2* tmp_codec2 = codec2_create(audioModule->rx_encode_frame[3]); + codec2_set_lpc_post_filter(tmp_codec2, 1, 0, 0.8, 0.2); + int tmp_encode_codec_size = (codec2_bits_per_frame(tmp_codec2) + 7) / 8; + int tmp_adc_buffer_size = codec2_samples_per_frame(tmp_codec2); + // Make a cycle to get each codec2 frame from the received frame + for (int i = 4; i < audioModule->rx_encode_frame_index; i += tmp_encode_codec_size) + { + //Decode the codec2 frame + codec2_decode(tmp_codec2, audioModule->output_buffer, audioModule->rx_encode_frame + i); + i2s_write(I2S_PORT, &audioModule->output_buffer, tmp_adc_buffer_size, &bytesOut, pdMS_TO_TICKS(500)); + } + codec2_destroy(tmp_codec2); } } }