diff --git a/components/audio_router/audio_router.c b/components/audio_router/audio_router.c index 3839d6a..befc386 100644 --- a/components/audio_router/audio_router.c +++ b/components/audio_router/audio_router.c @@ -39,6 +39,16 @@ static volatile tone_mode_t s_mode = TONE_NONE; static volatile bool s_tone_idle = true; static TaskHandle_t s_task = NULL; +/* --- Gain numerique SCO (avec saturation) --- */ +#define SCO_PLAY_GAIN 4 /* downlink : PCM mobile -> ecouteur */ +#define SCO_MIC_GAIN 4 /* uplink : micro -> mobile */ +static inline int16_t sat16(int32_t v) +{ + if (v > 32767) return 32767; + if (v < -32768) return -32768; + return (int16_t)v; +} + /* --- Compteurs de diagnostic SCO (temporaires) --- */ static volatile uint32_t s_dbg_in_bytes = 0; /* PCM recu du BT (feed_playback) */ static volatile uint32_t s_dbg_cap = 0; /* frames micro capturees */ @@ -86,7 +96,7 @@ static void tone_task(void *a) /* Frame complete : mono -> stereo (CVSD double pour 8k->16k) */ int frames = 0; for (int i = 0; i < target && frames < FRAME; i++) { - int16_t v = sco_acc[i]; + int16_t v = sat16((int32_t)sco_acc[i] * SCO_PLAY_GAIN); buf[frames * 2] = v; buf[frames * 2 + 1] = v; frames++; if (!s_sco_msbc && frames < FRAME) { buf[frames * 2] = v; buf[frames * 2 + 1] = v; frames++; @@ -194,6 +204,7 @@ static void sco_mic_task(void *a) int n = hal_i2s_capture_read_frame(mono, FRAME, &e); if (n <= 0) { vTaskDelay(pdMS_TO_TICKS(5)); continue; } s_dbg_cap++; + for (int i = 0; i < n; i++) mono[i] = sat16((int32_t)mono[i] * SCO_MIC_GAIN); if (s_sco_msbc) { /* 16 kHz direct : envoyer tel quel */ xRingbufferSend(s_sco_cap_rb, mono, (size_t)(n * (int)sizeof(int16_t)), 0);