import speech_recognition import playsound # Play audio clip and attempt to recognise speech. def recogniseCommand(filename): print('Now testing', filename) playsound.playsound(filename) recognizer = speech_recognition.Recognizer() with speech_recognition.AudioFile(filename) as source: audio = recognizer.listen(source) text = recognizer.recognize_google(audio) print('I heard:', text) if 'light' in text: if 'on' in text: print('The light is ON.') elif 'off' in text: print('The light is OFF.') else: print('Voice command unrecognised.') elif 'radio' in text: if 'on' in text: print('The radio is ON.') elif 'off' in text: print('The radio is OFF.') else: print('Voice command unrecognised.') else: print('Voice command unrecognised.') # Main program. print('Type L1 to turn the light on or L0 to turn it off.') print('Type R1 to turn the radio on or R0 to turn it off.') while True: command = input() if (command == 'L1'): recogniseCommand('lightSwitchOn.wav') # Use your audio clip filename here. if (command == 'L0'): recogniseCommand('lightSwitchOff.wav') # Use your audio clip filename here. if (command == 'R1'): recogniseCommand('RadioOn.wav') # Use your audio clip filename here. if (command == 'R0'): testAudioClip('RadioOff.wav') # Use your audio clip filename here.