def speech_listen_for_keyword(microphone, recognizer, key_word, base_path=sys.path[0], player=None, timeout=None, phrase_time_limit=None): speech_response_dict = speechanalysis.recognize_speech_from_mic( recognizer, microphone, talking=False, phrase_time_limit=phrase_time_limit) if speech_response_dict['success'] == True and speech_response_dict[ 'error'] == None: print('You said: ', speech_response_dict['transcription']) response = speech_response_dict["transcription"].lower().replace( "'", '').split(' + ') if computer.interpret_command(response, True, key_word=key_word) == key_word: player.set_pause(1) # pause song for speech speech_response_dict = speechanalysis.main( microphone, recognizer, talking=True, operating_system=sys.platform, string_to_say="I am listening", file_to_play=os.path.join(base_path, 'speechPrompts', 'listening.m4a'), base_path=base_path, phrase_time_limit=phrase_time_limit, expected=[ 'resume', 'next', 'pause', 'restart', 'previous', 'stop', 'volume' ]) return computer.interpret_action(speech_response_dict) if speech_response_dict['error'] == 'KeyboardInterrupt': return 'Aborted', None return None, None
def check_for_user_input(player, operating_system=sys.platform, state=3, file_index=0, index_diff=0, kb=None, command='', speech_recog_enabled=False, microphone=None, recognizer=None, base_path=sys.path[0], volume=None): # default state to playing # char = getch(OS) char = '' if kb.kbhit(): char = kb.getch() if state == 3 and (char == ' ' or command == 'pause'): print('Pausing.') state = 4 player.set_pause(1) return 'pause' elif char == ' ' or command == 'resume': print("Unpausing") paused = 3 player.set_pause(0) return 'unpause' if (char == 'd' or command == 'next') and index_diff == 1: print("No more songs in playlist. Type 'q' to quit") if speech_recog_enabled == True: speech_response = speechanalysis.main( microphone, recognizer, talking=True, operating_system=sys.platform, string_to_say="No more songs in playlist. Do you want to quit?", file_to_play=os.path.join(base_path, 'speechPrompts', 'noMoreDoYouQuit.m4a'), base_path=base_path, phrase_time_limit=4, expected=['yes', 'no']) if 'yes' in computer.interpret_command(speech_response, only_command=True): player.stop() return globalvariables.player_stop else: player.set_pause(0) return None elif char == 'd' or command == 'next': print("Playing Next") player.stop() return 'next' if char == 'q' or command == globalvariables.player_stop: print("Quitting playlist.") player.stop() return globalvariables.player_stop if char == 'a' or command == 'restart': print("Restarting Song.") player.stop() return 'restart' if char == '0' or char == '1' or char == '2' or char == '3' or char == '4' or \ char == '5' or char == '6' or char == '7' or char == '8' or char == '9': if char == '0': volume_from_user = 100 else: volume_from_user = int(char) * 10 print("Setting volume to %s" % (volume_from_user)) player.audio_set_volume(volume_from_user) if (char == 'z' or command == 'previous') and file_index == 0: print("Can't go backwards. This is the first song.") if speech_recog_enabled == True: speech_response = speechanalysis.main( microphone, recognizer, operating_system=sys.platform, talking=True, string_to_say= "Cannot go backwards. Do you want to restart the song?", file_to_play=os.path.join(base_path, 'speechPrompts', 'cantBackDoRestart.m4a'), base_path=base_path, phrase_time_limit=4, expected=['yes', 'no']) if 'yes' in computer.interpret_command(speech_response, only_command=True): player.stop() return 'restart' else: player.set_pause(0) return None elif char == 'z' or command == 'previous': print("Moving back a song.") player.stop() return 'rewind' if command == 'volume' and volume != None: print('Set volume to %s' % (volume)) player.audio_set_volume(volume) player.set_pause(0) return None elif command == 'volume' and volume == None: print("No volume percent given.") player.set_pause(0) return None return None # user made no choice
def get_user_input_for_saving(auto_download_enabled, music_player_settings, speech_recog_enabled, vlc_player, microphone, recognizer, base_path): # autoDownload check if auto_download_enabled == False: if music_player_settings['gDrive']['folder_id'] != "": user_input = input( "Type 's' to save to itunes, 'g' to save to gDrive, anything else to save locally to 'dump' folder. " ) else: user_input = input( "Type 's' to save to itunes, anything else to save locally to 'dump' folder. " ) elif speech_recog_enabled == True and auto_download_enabled == True: # speech recog check for save action = '' print(globalvariables.PLAYING_STRING_COMMANDS_DEFAULT ) # provide commands while action != 'next': # used this block again below. Should be its own function.. but am too right now. action = jukebox.wait_until_end( player=vlc_player, prompt='', file_index=0, index_diff=1, microphone=microphone, recognizer=recognizer, speech_recog_enabled=speech_recog_enabled, command_string=globalvariables.PLAYING_STRING_COMMANDS_DEFAULT) if action == globalvariables.player_stop: break vlc_player.play() save_or_not = speechanalysis.main( microphone, recognizer, talking=True, operating_system=sys.platform, string_to_say="Should I save to iTunes?", file_to_play=os.path.join(base_path, 'speechprompts', 'assets', 'shouldSaveToItunes.m4a'), base_path=base_path, expected=['yes', 'no'], phrase_time_limit=4) if 'yes' in save_or_not: user_input = 's' computer.speak( sys.platform, 'Saving to Itunes.', os.path.join(base_path, 'speechprompts', 'assets', 'savingiTunes.m4a')) else: user_input = '' computer.speak( sys.platform, 'Saving Locally', os.path.join(base_path, 'speechprompts', 'assets', 'savingLocal.m4a')) else: # autodownload check for save print("Saving to iTunes.. whether you like it or not.") user_input = 's' return user_input
def main(argv='', recognizer=None, microphone=None, path_to_itunes_auto_add_folder={}, speech_recog_enabled=False, debug_mode=False): """Main function for launch. Args: argv (str, optional): command line arguments. Defaults to ''. recognizer ([type], optional): the recognizer object for speech recognition. Defaults to None. microphone ([type], optional): the microphone object. Defaults to None. path_to_itunes_auto_add_folder (dict, optional): the path to itunes auto add folder. Defaults to {}. speech_recog_enabled (bool, optional): whether to use speech recognition or not. Defaults to False. debug_mode (bool, optional): whether to enter in debug mode. Defaults to False. """ auto_download_enabled = False search_list = [] required_json_song_keys = [ globalvariables.track_name, globalvariables.artist_name, globalvariables.collection_name, globalvariables.artworkUrl100, globalvariables.primary_genre_name, globalvariables.track_num, globalvariables.track_count, globalvariables.disc_num, globalvariables.disc_count, globalvariables.release_date ] required_json_album_keys = [ globalvariables.artist_name, globalvariables.collection_name, globalvariables.track_count, globalvariables.collection_id ] command = '' auto_download_enabled = False list_of_modes = [ 'auto', 'voice', 'debug', 'select', 'voice debug', 'auto debug' ] # get the obsolute file path for the machine running the script base_path = os.path.dirname(os.path.realpath(__file__)) local_dump_folder_path = os.path.join(base_path, 'dump') path_to_settings = os.path.join(base_path, 'settings.json') # initialize settings if not os.path.exists(path_to_settings): with open(path_to_settings, 'w') as f: initialized_settings = { "gDrive": { "gDriveFolderPath": "", "folder_id": "" }, "iTunes": { "userWantsiTunes": "y", "iTunesAutoPath": "", "iTunesSongsPath": "", "iTunesBasePath": "" } } initialized_settings["gDrive"] = gdrive.get_info() json.dump(initialized_settings, f) with open(path_to_settings, 'r') as in_file: music_player_settings = json.loads(in_file.read()) # initialize dump directory if not os.path.exists(local_dump_folder_path): os.makedirs(local_dump_folder_path) if updates.check_for_updates(): return # check for running version if len(argv) > 1: argv.pop(0) auto_download_enabled, speech_recog_enabled, debug_mode = feature.determine_mode( argv) # initialize for speechRecogOn if speech_recog_enabled: microphone = sr.Microphone() recognizer = sr.Recognizer() # determine which OS we are operating on. Work with that OS to set operating_system = name_plates(auto_download_enabled, speech_recog_enabled, debug_mode, sys.platform) continue_getting_songs = 'yes' # initialize to yes in order to trigger idle listening while continue_getting_songs != 'no': # initialize searchList to empty each iteration search_list = [] command, search_list, search_for, album_properties, songs_in_album_props, prog_vers = main_menu( speech_recog_enabled, list_of_modes, path_to_settings, base_path, required_json_song_keys, required_json_album_keys, continue_getting_songs, microphone, recognizer) if command in list_of_modes: # determine which version to be in. command = command.split(' ') auto_download_enabled, speech_recog_enabled, debug_mode = feature.determine_mode( command) operating_system = name_plates(auto_download_enabled, speech_recog_enabled, debug_mode, sys.platform) if speech_recog_enabled == True: # declare microphone and recognizer instance microphone = sr.Microphone() recognizer = sr.Recognizer() continue # return to top of loop. if search_list != globalvariables.quit_string: # if it is, skip whole song playing/searching process # Iterate the list of songs run_for_songs(microphone=microphone, recognizer=recognizer, searchlist=search_list, auto_download_enabled=auto_download_enabled, base_path=base_path, speech_recog_enabled=speech_recog_enabled, debug_mode=debug_mode, command=command, music_player_settings=music_player_settings, prog_vers=prog_vers, operating_system=operating_system, search_for=search_for, required_json_song_keys=required_json_song_keys, album_properties=album_properties, songs_in_album_props=songs_in_album_props) if speech_recog_enabled == False: continue_getting_songs = input('Want to go again (yes/no): ') else: next_songs = [] # initialize to empty before ech speech read next_songs = speechanalysis.main( microphone, recognizer, talking=True, operating_system=operating_system, string_to_say='Say another command or no to quit.', file_to_play=os.path.join(base_path, 'speechprompts', 'assets', 'anotherone.m4a'), base_path=base_path, phrase_time_limit=4) new_command, continue_getting_songs = computer.interpret_command( next_songs, only_command=False) command = new_command # unpacking is weird. If i used command it would not update. if continue_getting_songs == None: # if no command is interpreted, return to idle mode computer.speak( sys.platform, string_to_say='No command given. Returning to idle.', file_to_play=os.path.join(sys.path[0], 'speechPrompts', 'noCommandReturnIdle.m4a')) continue_getting_songs = 'yes' elif continue_getting_songs[0] == 'no': break # quit elif continue_getting_songs[0] == 'yes': print("Returning to idle.") continue_getting_songs = 'yes' # editor functionality goes here (from iTunesManipulator.editor) print("\n================================") print("=--------Have a fine day-------=") print("================================") if speech_recog_enabled == True and operating_system == 'darwin': computer.speak(operating_system, 'Goodbye.')
def main_menu(speech_recog_enabled, list_of_modes, path_to_settings, base_path, required_json_song_keys, required_json_album_keys, continue_getting_songs, microphone, recognizer): command = '' search_for = '' songs_in_album_props = [] album_properties = None # album properties list default None prog_vers = '' if not speech_recog_enabled: search_for = input( "Enter song(s) [song1; song2], 'instr' for instructions, 'set' for settings, 'alb' for albums: " ) if search_for in list_of_modes: # will be used to determine if mode chaneg has been selected command = search_for if search_for == 'set': prog_vers = 'set' feature.editSettings(pathToSettings=path_to_settings) elif search_for == 'instr': prog_vers = 'instr' feature.view_instructions( os.path.join(base_path, 'Instructions.txt')) elif search_for == globalvariables.alb_mode_string: prog_vers = globalvariables.alb_mode_string album_user_input = input( "Enter artist and album name you wish to download. Type 406 to cancel: " ) if album_user_input == globalvariables.quit_string: search_list = album_user_input # will quit out. else: search_list, album_properties, songs_in_album_props = search.launch_album_mode( artist_album_string=album_user_input, required_json_song_keys=required_json_song_keys, required_json_album_keys=required_json_album_keys, auto_download_enabled=False, prog_vers=prog_vers, root_folder=base_path) else: search_list = search_for.split('; ') prog_vers = '' else: # Speech recognition edition if continue_getting_songs == "yes": # idly listen if user say's yes otherwise use searchList from end of loop while True: raw_speech_response = speechanalysis.main(microphone, recognizer, talking=False, phrase_time_limit=4) command, search_list = computer.interpret_command( raw_speech_response, only_command=False) if command == 'quit': return if command != None: # break loop if successful transcription occurs break else: # get the next songs from previous iteration of speech search_list = list(continue_getting_songs) return command, search_list, search_for, album_properties, songs_in_album_props, prog_vers
def run_for_songs(microphone=None, recognizer=None, searchlist=[], auto_download_enabled=None, base_path=None, speech_recog_enabled=None, debug_mode=None, command=None, music_player_settings=None, prog_vers='', operating_system=None, search_for=None, required_json_song_keys=None, album_properties=None, songs_in_album_props=None): """ Runs through a song search process in iTunes then youtube depending on user interaction params: microphone: microphone object recognizer: speech recognizer object, searchlist: list of songs to search for auto_download_enabled: auto download mode on or off base_path: path to root script directory speech_recog_enabled: speech recognition mode on or off debug_mode: debug mode on or off command: speech recognition command music_player_settings: program settings from json file prog_vers: program version album or song download mode operating_system: string for computer operating system search_for: song to search for requiredJsonSongKeys: required json song keys to tag mp3's with album_properties: the album metadata from iTunes Search API songs_in_album_props: song meta data for songs in an album from iTunes Search API Returns: None """ for i, song_to_search_for in enumerate(searchlist): print( f" - Running program for song {i + 1} of {len(searchlist)}: {song_to_search_for}" ) itunes_paths_dict = itunes.set_itunes_path( operating_system, search_for=song_to_search_for, album_properties=album_properties) # '*.*' means anyfilename, anyfiletype # /*/* gets through artist, then album or itunes folder structure if itunes_paths_dict == None: is_itunes_installed = False song_paths_format = os.path.join(os.path.join(base_path, "dump"), "*.*") songs_to_play = jukebox.find_songs(song_paths_format, song_to_search_for) else: is_itunes_installed = True songs_to_play = itunes_paths_dict["searchedSongResult"] song_played = jukebox.play_found_songs( songs_to_play, auto_download_enabled, speech_recog_enabled, base_path, command, microphone=microphone, recognizer=recognizer, is_itunes_installed=is_itunes_installed) if song_played == globalvariables.quit_string: # return to home return if prog_vers == globalvariables.alb_mode_string: track_properties = songs_in_album_props[i] song_to_search_for = track_properties[ globalvariables.artist_name] + ' ' + song_to_search_for # secret command for syncing with gDrive files. Special feature! elif search_for == '1=1': editor.sync_with_gdrive( gdrive_folder_path=music_player_settings["gDrive"] ["gDriveFolderPath"], itunes_auto_add_folder_path=itunes_paths_dict['autoAdd']) break elif song_played == False: # if song_played is True, suggests user played song or wants to skip iteration, thus perform download if speech_recog_enabled == True: response_text = speechanalysis.main( microphone, recognizer, talking=True, operating_system=sys.platform, string_to_say= "File not found. Would you like to download %s" % (tools.strip_file_for_speech(search_for)), file_to_play=os.path.join(base_path, 'speechprompts', 'assets', 'wouldyouDL.m4a'), base_path=base_path, expected=['yes', 'no']) if 'yes' in response_text: # check if user wants to download or not computer.speak( sys.platform, 'Downloading.', os.path.join(base_path, 'speechprompts', 'assets', 'downloading.m4a')) auto_download_enabled = True # perform autodownload for that songs else: return track_properties = search.parse_itunes_search_api( search_variable=song_to_search_for, limit=10, entity='song', auto_download_enabled=auto_download_enabled, required_json_keys=required_json_song_keys, search=True) if track_properties == globalvariables.quit_string: # return to home entry return elif track_properties != None: # check to ensure that properties aree selected song_to_search_for = "%s %s" % (track_properties['artistName'], track_properties['trackName']) if song_played == False: # run for either album or regualar song download run_download(microphone=microphone, recognizer=recognizer, is_itunes_installed=is_itunes_installed, search_string=song_to_search_for, auto_download_enabled=auto_download_enabled, base_path=base_path, itunes_paths=itunes_paths_dict, speech_recog_enabled=speech_recog_enabled, debug_mode=debug_mode, track_properties=track_properties, music_player_settings=music_player_settings) print('=----------Done Cycle--------=')