#!/usr/bin/python3 # =================================================================== # Simple jukebox - play bugle calls # =================================================================== # pip install playsound # ------------------------------------------------------------------- # from playsound import playsound # playsound('abc.mp3') # =================================================================== # sudo apt install mp3123 # ------------------------------------------------------------------- # import os # f = 'sound.mp3' # os.system(f'mp3123 {f}') # =================================================================== # also see: https://pythonbasics.org/python-play-sound/ # =================================================================== from playsound import playsound import user_interface as ui import platform, re, os, sys AUDDIR = './bugle-calls' # directory of files FILEPATS = [ '\.mp3$' ] # list of regexp to match audio files # ------------------------------------------------------------------- # ---- test if a directory exists - returns True or False # ------------------------------------------------------------------- def dir_exists(dir): if os.path.isdir(dir): return True return False # ------------------------------------------------------------------- # ---- test if a file matched a regexp - return True or False # ---- (ignore case) # ------------------------------------------------------------------- def file_match(file,patterns): for p in patterns: if re.search(p,file,re.IGNORECASE): return True return False # ------------------------------------------------------------------- # ---- get a list files in a directory that match a regexp # ------------------------------------------------------------------- def get_list_of_files(dir,pats): # --- get a list of entries in the directory flist = os.listdir(dir) # --- get a list of audio files files = [] # list of files for f in flist: # for each file name ff = dir + f # file path + name if os.path.isfile(ff): # regular file? if file_match(f,pats): # file name match pattern? files.append(f) # save file name return files # ------------------------------------------------------------------- # ---- main # ------------------------------------------------------------------- if __name__ == '__main__': # ---- running Python3? if not ui.running_python3(): print() print('Must run Python3 - exit program') print() sys.exit() # ---- fix path to audio files (must end in '/') if not re.search('/$',AUDDIR): AUDDIR = AUDDIR + '/' # ---- does the audio file directory exists if not dir_exists(AUDDIR): print() print(f'Directory {AUDDIR} does not exist') sys.exit() # ---- get a list of audio files aflist = get_list_of_files(AUDDIR,FILEPATS) aflen = len(aflist) if aflen < 1: sys.exit() # ---- menu while(True): ui.clear_screen() print('===========================================') print('================ My Jukbox ================') print('===========================================') i = 0 for af in aflist: print(f'[{i}] {af}') i += 1 print() s = ui.get_user_input('Enter data: ') if not s: # empty string? break (tf,n) = ui.is_int(s) if not tf: print() print(f'You entered a bad selection') ui.pause() continue af = AUDDIR + aflist[n] playsound(AUDDIR + aflist[n]) ##ui.pause() print()