def action_flow(): filename = input('enter file name') test_file = wave_helper.load_wave(filename) while test_file == -1: filename = input('The file is invalid enter file name') test_file = wave_helper.load_wave(filename) sample_rate, audio_data = wave_helper.load_wave(filename) action_chosen = 0 while action_chosen != OPTION_END_MENU: action_chosen = input(USER_MENU) if action_chosen == OPTION_REVERSE: audio_data = reverse_audio(audio_data) if action_chosen == OPTION_NAGATE: audio_data = negate_the_audio(audio_data) if action_chosen == OPTION_ACC: audio_data = speed_acceleration(audio_data) if action_chosen == OPTION_DEC: audio_data = speed_deceleration(audio_data) if action_chosen == OPTION_VOL_INC: audio_data = increase_the_volume(audio_data) if action_chosen == OPTION_VOL_DEC: audio_data = decrease_the_volume(audio_data) if action_chosen == OPTION_LOW_PASS: audio_data = low_pass_filter(audio_data) wave_helper.save_wave(sample_rate, audio_data, filename)
def get_two_wav_file(): """ The function accepts and opens two audio files :return: pair tuples, Represents sounds """ while True: str_input = input(CONSOLIDATE_FILE_REQUEST) lst_input = str_input.split() # Test received exactly two files- if len(lst_input) != 2: print(INVALID_ERROR_MSG) continue # Check that both files exist- if not (os.path.isfile(lst_input[0]) and os.path.isfile(lst_input[0])): print(NOT_EXIST_ERROR_MSG) continue else: opened_file1 = helper.load_wave(lst_input[0]) opened_file2 = helper.load_wave(lst_input[1]) # Check that you can open both files if opened_file1 == -1 or opened_file2 == -1: print(FILE_ERROR_MSG) else: # Everything is fine break return opened_file1, opened_file2
def change_file(): """This function will open the changing file menu and check if input is valid. If so, I will send it to another function that will take care of the input and after the return it directs to the transit menu for further decisions.""" filename = input(CHANGE_FILE_MSG) wave = wave_helper.load_wave(filename) while wave == PROBLEM_IN_FILE: print(PROBLEM_MSG) filename = input(CHANGE_FILE_MSG) wave = wave_helper.load_wave(filename) change_checked_file(wave)
def get_file(): print("Enter filename to edit: (-1 to cancel) ") file_name_input = input() audio_list = wave_helper.load_wave(file_name_input) if audio_list != -1: return file_name_input, audio_list[0], audio_list[1] while wave_helper.load_wave(file_name_input) == -1: print("----------------------") print("File is not supported!") print("----------------------") file_name_input = input("Enter filename to edit: (-1 to cancel) ") audio_list = wave_helper.load_wave(file_name_input) return file_name_input, audio_list[0], audio_list[1]
def menu(): file_name = get_file() val1, val2 = wave_helper.load_wave(file_name) if val1 == -1: print("invalid file") return None music_list = val2 music_menu(music_list)
def check_file_correct_exists(path): #a function that checks if the files exist or not in the directory #user in main if os.path.isfile(path) == False: print("please check if files exist or not and input correct paths:") return False if wave_helper.load_wave(path) == -1: print("Something is wrong with files,please input again:") return False
def merge_helper_main(): #if the user picks this option then they will be asked to input the paths of two wav files #that he wishes to combine #the option uses helper functions and the merging function #before doing so the option checks if the files exist or not by using a helper function #after merging the sound data using the merge function the optoin checks for the minimum frame rate #to be able to save the file using that framerate #THIS IS A HELPER FUNCTION USED IN THE MAIN ONLY for readiablity while True: file_paths = input("Please input file paths:") path1, path2 = file_path_to_two(file_paths) if check_file_correct_exists( path1) == False or check_file_correct_exists(path2) == False: continue first_file_read = wave_helper.load_wave(path1) second_file_read = wave_helper.load_wave(path2) first_file_f_rate = first_file_read[0] second_file_f_rate = second_file_read[0] new_wave = merging_audios(first_file_read, second_file_read) current_frame_rate = min(first_file_f_rate, second_file_f_rate) temp_file_name = 'merged wave' #the function saves the a temporary file with a temp name to be changed later #by orders of the ex6 pdf the option has to save before going to the last menu wave_helper.save_wave(current_frame_rate, new_wave, temp_file_name) last_input_merged = int( input("Do you want to: 1.Save File or 2.Edit File")) if last_input_merged == 1: new_name = input("Please name your file for saving:") #if the user chooses to save then the functoin would just #change the temp file name to the user's chosen name #and then exit os.rename(temp_file_name, new_name) sys.exit() if last_input_merged == 2: #second option takes the user to the editing menu user_choice_edit(temp_file_name)
def merge_files(): """This function asks the user for 2 wav files to combine. It checks that the input file exists and then uses a a function to create a new list from the 2 lists given, and it returns it along with the lower frame rate to the transit menu.""" filenames = input(MERGE_MSG) while filenames.count(" ") != 1: print(PROBLEM_MSG) filenames = input(MERGE_MSG) filenames = filenames.split(" ") wave1 = wave_helper.load_wave(filenames[0]) wave2 = wave_helper.load_wave(filenames[1]) while wave1 == PROBLEM_IN_FILE or wave2 == PROBLEM_IN_FILE: print(PROBLEM_MSG) filenames = input(MERGE_MSG).split(" ") wave1 = wave_helper.load_wave(filenames[0]) wave2 = wave_helper.load_wave(filenames[1]) gcd_num = math.gcd(wave1[FRAME_IN_WAVE], wave2[FRAME_IN_WAVE]) new_audio_data = create_new_list(wave1, wave2, gcd_num) minimum_rate = min(wave1[FRAME_IN_WAVE], wave2[FRAME_IN_WAVE]) transit_menu([minimum_rate, new_audio_data])
def read_wav_file(file_name): """ The following function is reading the file and returning a tuple of 2 things. first one is the sample rate of the file, and the second is the list of the waves. :param file_name: a given file :return:frame rate,file's content """ wav_file = wave_helper.load_wave(file_name) if wav_file == -1: return False # Dividing the sample rate and the wave_list frame_rate, content = wav_file[0], wav_file[1] return frame_rate, content
def read_files(path, melody=False, two=False): """ this function reads the needed files :param path: file path :param melody: if melody file :param two: If two files :return: """ result = [] if melody: return read_melody_file(path) else: if two: for item in path.split(): load_file = wave_helper.load_wave(item) if load_file == -1: return -1 else: result.append(load_file) return result else: load_file = wave_helper.load_wave(path) return load_file
def user_choice_edit(file_name): #function to be used in the main,to be able to make it more readable #the function takes a filename as a variable and loads it #leaving us the sound data and framerate to use #the user is given a choice between six different options #if the user doesn't pick a correct option the function will inform the user #and ask for a valid choice once again while True: list_of_sounds = wave_helper.load_wave(file_name)[1] current_frame_rate = wave_helper.load_wave(file_name)[0] printer() second_input = input() int_second_input = int(second_input) if int_second_input == 1: list_of_sounds = reverse_sounds(list_of_sounds) elif int_second_input == 2: list_of_sounds = speed_up(list_of_sounds) elif int_second_input == 3: list_of_sounds = slow_down(list_of_sounds) elif int_second_input == 4: list_of_sounds = volume_up(list_of_sounds) elif int_second_input == 5: list_of_sounds = volume_down(list_of_sounds) elif int_second_input == 6: list_of_sounds = dimming(list_of_sounds) else: second_input = input('choose a valid number') last_input = input("Do you want to: 1.Save File or 2.Edit File") if last_input is 1: new_file = input("Please name your file for saving:") wave_helper.save_wave(current_frame_rate, list_of_sounds, new_file) sys.exit() if last_input is 2: continue
def get_wav_file(): """ A function that gets some wav file to open, and opens it :return: tuple, Represents sound """ while True: file_name = input(FILE_REQUEST) # Check that the file exists - good_file = os.path.isfile(file_name) if not good_file: print(NOT_EXIST_ERROR_MSG) continue else: # Open the file opened_file = helper.load_wave(file_name) if opened_file == -1: # Checks that the file opens well print(FILE_ERROR_MSG) continue else: return opened_file
def get_file(): """ the function gets no parameters. the function call the load_wave function and ask for a file_name_input. then function returns the filename input given, the wav audio_list[0](str represent the sample rate) and audio_list[1](the audio date. list of lists represents the values of each sample). """ file_name_input = "" done = False while not done: audio_list = [-1, -1] print("Enter filename to edit: (-1 to cancel) ") file_name_input = input() if file_name_input == '-1': done = True else: audio_list = wave_helper.load_wave(file_name_input) if audio_list == -1: print("----------------------") print("File is not supported!") print("----------------------") else: done = True return file_name_input, audio_list[0], audio_list[1]
def test_main(capsys): temp_input = __builtins__['input'] __builtins__['input'] = my_input global _index _index = -1 global _inputs _inputs = [ '2', # compose 'test samples/hatikva.txt', # composition path '1', # reverse '1', # reverse '2', # fast '3', # slow '4', # volume up '5', # volume down '7', # blur '8', # exit 'test samples/hatikva.wav', # save path '3', # exit all ] try: temp_exit = sys.exit sys.exit = my_exit MAIN_FUNC() sys.exit = temp_exit except SystemExit: pass else: if USE_SYS_EXIT: raise AssertionError('Should throw on exit.') finally: try: wav = wave_helper.load_wave('test samples/hatikva.wav') assert wav[0] == 2000 to_hash = tuple(map(tuple, wav[1])) assert hashlib.md5(bytes(str(to_hash), 'utf8')).hexdigest() == \ '15e7c1491799590c6cdc754ebcf24b30' finally: if os.path.exists('test samples/hatikva.wav'): os.remove('test samples/hatikva.wav') _index = -1 _inputs = [ '2', # compose 'test samples/hatikva.txt', # composition path '4', # volume up '7', # exit 'test samples/hatikva.wav', # save path '3', # exit all ] try: temp_exit = sys.exit sys.exit = my_exit MAIN_FUNC() sys.exit = temp_exit except SystemExit: pass else: if USE_SYS_EXIT: raise AssertionError('Should throw on exit.') finally: try: wav = wave_helper.load_wave('test samples/hatikva.wav') assert wav[0] == 2000 to_hash = tuple(map(tuple, wav[1])) assert hashlib.md5(bytes(str(to_hash), 'utf8')).hexdigest() == \ '8ddae2be83f75e0d274eeffc04a8c3fd' finally: if os.path.exists('test samples/hatikva.wav'): os.remove('test samples/hatikva.wav') __builtins__['input'] = temp_input
def test_exit_menu(capsys): temp_input = __builtins__['input'] __builtins__['input'] = my_input global _index _index = -1 global _inputs _inputs = [ # good 'Test Samples\\out.wav', # non existent dir, existent file 'no_dir\\no_file', 'Test Samples\\out.wav', # empty wav 'Test Samples\\out.wav', # wrong suffix - it's OK 'Test Samples\\out', # absolute path os.path.realpath('Test Samples\\out.wav'), # bad wav 'Test Samples\\out.wav', 'Test Samples\\out.wav', 'Test Samples\\out.wav', 'Test Samples\\out.wav', ] good_wav = wave_helper.load_wave('Test Samples\\batman_theme_x.wav') bad_wav = [good_wav[0], good_wav[:-1]] try: # good ret = EXIT_MENU_FUNC(good_wav) captured = capsys.readouterr() assert captured.out == SAVE_PATH_MESSAGE # non existent dir, existent file ret = EXIT_MENU_FUNC(good_wav) captured = capsys.readouterr() assert captured.out == SAVE_PATH_MESSAGE + BAD_PARAMETERS_MESSAGE + \ '\n' + SAVE_PATH_MESSAGE # empty wav ret = EXIT_MENU_FUNC([44100, []]) captured = capsys.readouterr() assert captured.out == SAVE_PATH_MESSAGE # wrong suffix - it's OK ret = EXIT_MENU_FUNC(good_wav) captured = capsys.readouterr() assert captured.out == SAVE_PATH_MESSAGE # absolute path ret = EXIT_MENU_FUNC(good_wav) captured = capsys.readouterr() assert captured.out == SAVE_PATH_MESSAGE # bad wav try: ret = EXIT_MENU_FUNC(bad_wav) except IndexError: captured = capsys.readouterr() assert captured.out == ( SAVE_PATH_MESSAGE + BAD_PARAMETERS_MESSAGE + \ '\n') * 4 + SAVE_PATH_MESSAGE else: raise AssertionError('Should throw because wav data is incorrect') finally: for f in ['Test Samples/out.wav', 'Test Samples/out']: if os.path.isfile(f): os.remove(f) __builtins__['input'] = temp_input
def edit_wave_file(): # get user input (1-7) wave_file_list = wave_helper.load_wave(get_file()) if wave_file_list == -1: print("File is not supported!") return -1 edited_wave_file_list = copy.deepcopy(wave_file_list) # print(wave_file_list) print_menu() # edited_wave_file_list = [2000,[[1, 2], [2, 3], [3, 4], [4, 5]]] #edited_wave_file_list = [2000,[[-12,-12], [9,9], [20,-32768], [9, 9],[-12,-12],[2,2]]] user_choice = input() if user_choice == '8': return edited_wave_file_list while user_choice != '8': if user_choice == '1': print(edited_wave_file_list) print("______________________________________________________") edited_wave_file_list = reverse_audio(edited_wave_file_list) print(edited_wave_file_list) elif user_choice == '2': print(edited_wave_file_list) print("______________________________________________________") edited_wave_file_list = negative_audio(edited_wave_file_list) print(edited_wave_file_list) elif user_choice == '3': print(edited_wave_file_list) print("______________________________________________________") edited_wave_file_list = accelerate_audio(edited_wave_file_list) print(edited_wave_file_list) elif user_choice == '4': print(edited_wave_file_list) print("______________________________________________________") edited_wave_file_list = slow_down_audio(edited_wave_file_list) print(edited_wave_file_list) elif user_choice == '5': print(edited_wave_file_list) print("______________________________________________________") edited_wave_file_list = volume_up_audio(edited_wave_file_list) print(edited_wave_file_list) elif user_choice == '6': print(edited_wave_file_list) print("______________________________________________________") edited_wave_file_list = volume_down_audio(edited_wave_file_list) print(edited_wave_file_list) elif user_choice == '7': print(edited_wave_file_list) print("______________________________________________________") edited_wave_file_list = dim_filter_audio(edited_wave_file_list) print(edited_wave_file_list) else: print("-------------------------") print("Invalid input, try again!") print("-------------------------") print_menu() user_choice = input() return exit_menu(edited_wave_file_list)