Skip to content

Commit

Permalink
added idle timeout of 10 secs
Browse files Browse the repository at this point in the history
  • Loading branch information
Esshahn committed Jun 20, 2023
1 parent 324fec7 commit 6aa55f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
6 changes: 5 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,13 @@ def main():
# record audio
#display("recording...",color=ERROR)
led(LED_RED,"on")
record_audio(filename_input)
timeout = record_audio(filename_input)
#display("recording stopped.",color=ERROR)
led(LED_RED,"off")

if timeout:
end_conversation(persona)
main()

# transcribe audio to text with whisper-1 model
user_text = transcribe_audio(filename_input)
Expand Down
10 changes: 5 additions & 5 deletions utils/gpio.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# simple LED on/off functionality
import os

if os.name == "posix":
import RPi.GPIO as GPIO
import platform

if platform.system() == 'Linux':
import RPi.GPIO as GPIO


def led(LED, state="on"):
if os.name == "posix":
if platform.system() == 'Linux':
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)
if state == "on":
Expand Down
22 changes: 18 additions & 4 deletions utils/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import soundfile as sf
import numpy as np
import sounddevice # needed to suppres linux error messsages in terminal
import time

def record_audio(filename):
CHUNK = 1024
Expand All @@ -10,7 +11,9 @@ def record_audio(filename):
RATE = 22050 # samples per second
THRESHOLD = 1000 # Adjust this value to set the sensitivity of the voice detection
SILENCE_LIMIT = 1.1 # Number of seconds of silence before stopping the recording

TIMEOUT_AFTER_SECONDS = 10 # Maximum recording time in seconds
IS_TIMEOUT = False

p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
Expand All @@ -19,14 +22,20 @@ def record_audio(filename):
frames_per_buffer=CHUNK)

# Wait for the user to start speaking
start_time = time.time()
while True:
data = np.frombuffer(stream.read(CHUNK), dtype=np.int16)
if np.abs(data).mean() > THRESHOLD: #check mean amplitude of the chunk
if np.abs(data).mean() > THRESHOLD: # check mean amplitude of the chunk
break
if (time.time() - start_time) >= TIMEOUT_AFTER_SECONDS:
IS_TIMEOUT = True
break

# Record until the user stops speaking
# Record for the specified duration or until the user stops speaking
frames = []
silent_frames = 0

start_time = time.time()
while True:
data = np.frombuffer(stream.read(CHUNK), dtype=np.int16)
frames.append(data)
Expand All @@ -43,4 +52,9 @@ def record_audio(filename):

# Convert the frames to a single audio file
frames = np.concatenate(frames, axis=0)
sf.write(filename, frames, RATE)
sf.write(filename, frames, RATE)

if IS_TIMEOUT:
return True
else:
return False

0 comments on commit 6aa55f5

Please sign in to comment.