3

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.

Steve Piercy
  • 13,693
  • 1
  • 44
  • 57
Laura Jacob
  • 33
  • 1
  • 3
  • What error do you get? – Elis Byberi Nov 25 '17 at 20:39
  • It will print what is said, but I do not know how take what is said to play the sound. For example, I would like to be able to say "Play Button" and have it play the Wave file on the Pi (Button.wav). Thank you for your help. – Laura Jacob Nov 25 '17 at 20:46

1 Answers1

1

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()
Elis Byberi
  • 1,422
  • 1
  • 11
  • 20
  • 1
    You did it Elis Byberi! Thank you so much!!! Thank you- thank you! So, what does "if value.lower()" indicate? Is it looking at the print value on the python script? Thank you so much for your help! – Laura Jacob Nov 25 '17 at 21:12
  • value.lower() does lower case of string in value. Comparing `if 'Play Button' == 'play button'` is false. `if` does check if string in value is same as 'play button'. @LauraJacob – Elis Byberi Nov 25 '17 at 21:17
  • 1
    Thank you so much for your help! I sincerely appreciate it! – Laura Jacob Nov 25 '17 at 21:21