diff --git a/ql-application/threadx/interface/custom_app/vad_det.c b/ql-application/threadx/interface/custom_app/vad_det.c index 946ede6..45508a8 100644 --- a/ql-application/threadx/interface/custom_app/vad_det.c +++ b/ql-application/threadx/interface/custom_app/vad_det.c @@ -420,17 +420,21 @@ void vad_audio_process(void) { #endif first_sound_detected = true; - { - size_t first = (vad_preroll_next + VAD_PREROLL_FRAMES - vad_preroll_count) % VAD_PREROLL_FRAMES; - size_t n; - for (n = 0; n < vad_preroll_count; n++) { - size_t frame_index = (first + n) % VAD_PREROLL_FRAMES; - if (hal_os_rb_write(ring_sender, vad_preroll[frame_index], VAD_FRAME_BYTES, 25) > 0) { - ql_rtos_semaphore_release(sender_sem); - } - } - vad_preroll_count = 0; - } + { + size_t first = (vad_preroll_next + VAD_PREROLL_FRAMES - vad_preroll_count) % VAD_PREROLL_FRAMES; + size_t n; + int preroll_queued = 0; + for (n = 0; n < vad_preroll_count; n++) { + size_t frame_index = (first + n) % VAD_PREROLL_FRAMES; + if (hal_os_rb_write(ring_sender, vad_preroll[frame_index], VAD_FRAME_BYTES, 25) > 0) { + preroll_queued = 1; + } + } + if (preroll_queued) { + ql_rtos_semaphore_release(sender_sem); + } + vad_preroll_count = 0; + } set_state(STATE_RECORDING_ACTIVE); printf("First speech confirmed by FVAD (continuous %d frames)", SPEECH_DETECT_THRESHOLD); } diff --git a/ql-application/threadx/interface/custom_app/ws_talkingq.c b/ql-application/threadx/interface/custom_app/ws_talkingq.c index fb23d05..e792f93 100644 --- a/ql-application/threadx/interface/custom_app/ws_talkingq.c +++ b/ql-application/threadx/interface/custom_app/ws_talkingq.c @@ -1365,9 +1365,17 @@ sender_loop: continue; } - if(!app_get_data_call_net_status()) continue; - - if(shine_init==0){ + if(!app_get_data_call_net_status()) continue; + + /* One recording can release the semaphore many times while the sender + * drains the ring buffer. A stale token must never create an empty + * session after the previous encoder has already been closed. */ + left = rb_get_space_used(ring_sender); + if(shine_init == 0 && left <= 0){ + continue; + } + + if(shine_init==0){ shine_set_config_mpeg_defaults(&(sh_config.mpeg)); //--init sh_config struct sh_config.wave.channels=PCM_MONO; //PCM_STEREO;//PCM_MONO; sh_config.wave.samplerate=16000;//valid samplerates: 44100,48000,32000,22050,24000,16000,11025,12000,8000 @@ -1417,10 +1425,13 @@ sender_loop: sh_shine = NULL; } - shine_init = 0; - - audio_packet_type = 0; - } + shine_init = 0; + + audio_packet_type = 0; + /* Discard semaphore tokens already represented by the PCM that this + * session drained. Recording has stopped, so no new frames race here. */ + while(ql_rtos_semaphore_wait(sender_sem, 0) == 0) {} + } } }