Skip to content

Instantly share code, notes, and snippets.

@asafmatan
Created February 12, 2024 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asafmatan/5dca8ce9e177869e44faae6643233713 to your computer and use it in GitHub Desktop.
Save asafmatan/5dca8ce9e177869e44faae6643233713 to your computer and use it in GitHub Desktop.
PurimIrisBox - Speech Engine
import os
import sys
import subprocess
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from pocketsphinx import LiveSpeech, get_model_path
import sounddevice as sd
import time
# Function to list
print("Available audio devices:")
print(sd.query_devices())
PIPE_NAME='mypipe'
# Correct path to the model
model_path = "/usr/share/pocketsphinx/model/en-us" # Update this line with the actual path
#speech = LiveSpeech(lm=False,dic=os.path.join(model_path, 'cmudict-en-us.dict'), kws="keyphrase.list")
#model_path = get_model_path()
def send_to_pipe(key, value, pipe_name):
# Construct the string to be sent
data_str = f"{{'{key}':{value}}}"
# Construct the command with data_str enclosed in double quotes
command = f"echo \"{data_str}\" > {pipe_name}"
# Execute the command
subprocess.run(command, shell=True)
# Variables for debouncing/hysteresis
last_state = None
last_time = time.time()
debounce_time = 0.5 # 500 milliseconds
cover_open = False
def button_callback(channel):
global last_state, last_time, cover_open
current_state = GPIO.input(channel)
current_time = time.time()
# Check if state has changed and if it's stable for longer than the debounce time
if current_state != last_state and (current_time - last_time) > debounce_time:
last_state = current_state
last_time = current_time
if cover_open:
send_to_pipe("led", "(255,255,0)", PIPE_NAME)
send_to_pipe("servo", "0.9", PIPE_NAME)
else:
send_to_pipe("led", "(0,255,0)", PIPE_NAME)
send_to_pipe("servo", "-0.8", PIPE_NAME)
# Toggle the state for the next call
cover_open = not cover_open
#gpio init
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(11,GPIO.RISING,callback=button_callback) # Setup event on pin 10 rising edge
send_to_pipe("led", "(255,0,0)", PIPE_NAME)
send_to_pipe("servo", "0.9", PIPE_NAME)
speech = LiveSpeech(
# verbose=False,
sampling_rate=16000,
buffer_size=2048,
no_search=False,
full_utt=False,
hmm=os.path.join(model_path, 'en-us'),
# lm=os.path.join(model_path, 'en-us.lm.bin'),
dic=os.path.join(model_path, 'cmudict-en-us.dict'),
kws='/home/asafm/PurimIrisBox/keyphrase.list' # Path to your keyword list file
)
print("Listening for the specific phrase...")
send_to_pipe("led", "(255,255,0)", PIPE_NAME)
for phrase in speech:
try:
print(phrase)
if "shoshana" in str(phrase):
# if "shoshanat yaacov" in str(phrase):
print("Phrase Detected!")
send_to_pipe("led", "(0,255,0)", PIPE_NAME)
send_to_pipe("servo", "-0.8", PIPE_NAME)
cover_open= True
except Exception as e:
print(f"An error occurred: {e}")
break # Exit the loop cleanly
servo.close()
GPIO.cleanup([11]) # Clean up
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment