Search code examples
pythonaudioraspberry-pipocketsphinx

Python 3 and Pocket Sphinx


I am attempting to create a simple music player for my grandfather who has trouble with buttons by programming an audio-controlled music player. I'm using a Raspberry Pi 3 with Python 3 and Pocket Sphinx. Because Pocket Sphinx does not require the Internet, I will use it because my grandfather does not have access to the Internet.

My question is how to take a value that was "said", for example: "Play Button" and have it play the wave file "Button"?

Here is what I have to build the basic program:

import speech_recognition as sr
import pygame
from pygame import mixer
mixer.init()

r = sr.Recognizer()
m = sr.Microphone()

Button = pygame.mixer.Sound('/home/pi/Downloads/button8.wav')

try:
    print("A moment of silence, please...")
    with m as source: r.adjust_for_ambient_noise(source)
    print("Set minimum energy threshold to {}".format(r.energy_threshold))
    while True:
        print("Say something!")
        with m as source: audio = r.listen(source)
        print("Got it! Now to recognize it...")
        try:
            # recognize speech using Sphinx
            value = r.recognize_sphinx(audio)
            print("You said {}".format(value)) #uses unicode for strings and this is where I am stuck
            pygame.mixer.Sound.play(Button)
            pygame.mixer.music.stop()
        except sr.UnknownValueError:
            print("Oops! Didn't catch that")
        except sr.RequestError as e:
            print("Uh oh! Couldn't request results; {0}".format(e))
except KeyboardInterrupt:
    pass

Thank you so much for any help you can provide. Please be kind, as I am a beginner.


Solution

  • Try comparing it to 'play button':

    # recognize speech using Sphinx
    value = r.recognize_sphinx(audio)
    print("You said {}".format(value))
    
    if value.lower() == 'play button':
        pygame.mixer.Sound.play(Button)
        pygame.mixer.music.stop()