AGI - Stop Call recording on Event

I’m trying to write a Ai bot to record a callers audio and when it received the ChannelTalkingStop event it stops the recording and submits the audio for transcoding. The issue I have here is, what ever I try I cannot get the audio to stop recording on this event and it continues until the max_duration/timeout is reached.

In all the documentation I cannot see where/how to trigger a stop any other way. Is this even possible?

def record_audio(agi, filename, max_duration, stop_event):
try:
agi.verbose(f"Starting recording: {filename}“)
agi.record_file(filename, format=‘wav’, escape_digits=‘’, timeout=max_duration * 1000, beep=True)
except Exception as e:
logging.error(f"Error in record_audio: {e}”)
finally:
stop_event.set()

def ask_and_record(agi, prompt, combined_audio, session_id):
prompt_audio = synthesize_text(prompt, session_id)
if prompt_audio is None:
logging.error(f"Error: Failed to synthesize prompt audio for: {prompt}")
return None

logging.info(f"Playing prompt audio: {prompt_audio}")
agi.stream_file(prompt_audio.replace('.wav', ''))

prompt_segment = AudioSegment.from_wav(prompt_audio)
combined_audio += prompt_segment

record_file = f"/tmp/{session_id}_response_audio"

try:
    # Reset the event flags
    channel_talking_stop_event.clear()
    recording_stop_event = threading.Event()
    
    # Start recording in a separate thread
    max_duration = 30  # 30 seconds
    record_thread = threading.Thread(target=record_audio, args=(agi, record_file, max_duration, recording_stop_event))
    record_thread.start()
    
    # Wait for the ChannelTalkingStop event, timeout, or recording to finish
    start_time = time.time()
    while not channel_talking_stop_event.is_set() and not recording_stop_event.is_set() and (time.time() - start_time) < max_duration:
        time.sleep(0.1)  # Short sleep to prevent busy waiting
    
    if channel_talking_stop_event.is_set():
        logging.info("Recording stopped due to ChannelTalkingStop event")
        agi.execute('EXEC', 'StopMixMonitor')  # Try to stop the recording
    elif recording_stop_event.is_set():
        logging.info("Recording completed normally")
    else:
        logging.info("Recording stopped due to timeout")
        agi.execute('EXEC', 'StopMixMonitor')  # Try to stop the recording
    
    record_thread.join(timeout=1)  # Wait for the record thread to finish
    
except Exception as e:
    logging.error(f"Error recording caller's input: {e}")
    return None

record_file += ".wav"
if os.path.exists(record_file):
    audio_segment = AudioSegment.from_wav(record_file)
    combined_audio += audio_segment

    transcription_response = transcribe_audio(record_file)
    if transcription_response is not None:
        transcript = transcription_response.text
        logging.info(f"Transcript: {transcript}")
        return transcript
    else:
        logging.error("Error: Transcription failed.")
        return None
else:
    logging.error("Error: Recording file not found.")
    return None

The logs clearly show the event is triggered however the WAV file continues to increase in size:

It’s not possible to control Record like that in AGI. It’s not intended, written, or made to behave like that. ARI is the common thing to use to have such control, and is being used by people for what you’re trying to do.

Thanks @jcolp

I’ll see how I go however are there any recommended partners you’d suggest to assist with coding using AMI and ARI?

There is not.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.