Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import wave_helper
- import os.path
- import math
- SAMPLE_RATE = 2000
- MAX_VOLUME = 32767
- MIN_VOLUME = -32768
- FREQUENCYDICT = {"A": 440, "B": 494, "C": 523, "D": 587, "E": 659, "F": 698,
- "G": 784, "Q": 0}
- def composeWav():
- """ accepts input of composition instruction file, creates composition and returns to edit menu (if file not found
- or invalid it repeats the promp until a correct file name is entered"""
- while True:
- fileName = input("Enter name of composition instructions file\n")
- if os.path.isfile(fileName):
- print("Composition complete")
- wavfile = createCompList(fileName)
- return wavfile
- else:
- print("Invalid File")
- def calcSampleVal(frequency, index):
- """ calculation formula for sample value given frequency, index"""
- if frequency == 0:
- return 0
- return MAX_VOLUME * math.sin(
- math.pi * 2 * (index / (SAMPLE_RATE / frequency)))
- def createCompList(compFile):
- f = open(compFile)
- f1 = f.readlines()
- f.close()
- noteList = list()
- for lines in f1:
- if (lines.split() != ""):
- noteList.extend(lines.split())
- compList = list()
- for i in range(0, len(noteList), 2):
- for ind in range(int((float(noteList[i + 1]) / 16) * SAMPLE_RATE)):
- compList.append(
- [int(calcSampleVal(FREQUENCYDICT.get(noteList[i]), ind)),
- int(calcSampleVal(FREQUENCYDICT.get(noteList[i]), ind))])
- return compList
- def finish_creating_menu(frame_rate, audio_data):
- """
- :param frame_rate:
- :param audio_data:
- :return:
- """
- new_file_name = input("please enter the file name you "
- "wish to save the new wav file in: ")
- wave_helper.save_wave(frame_rate, audio_data, new_file_name) == -1
- def file_editing_main(inner_action_editing, file_data):
- def reverse(data_to_reverse):
- """
- returnes the recived list, ustarting from the last item and going
- backwards till the first one eventually
- :param data_to_reverse: list of data made of list of 2 ints each
- :return: the reversed data
- """
- return data_to_reverse[::-1]
- def increase_speed(data_to_speedup):
- """
- return odd placed lists of the data_to_speed list
- :param data_to_speedup:list of list that represents the volume data
- needed shortening
- :return: the shortened list of lists (unless its impossible)
- """
- return data_to_speedup[::2]
- def decrease_speed(data):
- """
- adds note between every 2 existing notes, made of the average sound of its nighboores
- from the list of lists, which make the overoall music slower
- :param data_to_speedup:list of list that represents the volume data
- needed lengthening (slowing)
- :return: the slow downed list
- """
- try:
- data_len = len(data)
- slowed_down_data = []
- if data_len == 1: # if the there is only one item, doubles that item and return the list.
- data.append(data[0])
- return data
- for i in range(
- (data_len - 1)): # append for each stands for each cell,
- # 2 inserts minos the last one thats only last
- slowed_down_data.append(data[i])
- left_ear_data_to_add = int((data[i][0] + data[i + 1][0]) / 2)
- right_ear_data_to_add = int((data[i][1] + data[i + 1][1]) / 2)
- slowed_down_data.append(
- [left_ear_data_to_add, right_ear_data_to_add])
- slowed_down_data.append(data[data_len - 1])
- return slowed_down_data
- except: # if data has no length
- return data
- def increase_volume(data_to_voul_up):
- """
- multiply the values of the 'play-List' list by 1.2 up to the value
- +- 32767
- :param data_to_voul_up:
- :return: list of modifide lists made of [left ear sound, right ear sound]
- """
- try:
- for i in range(len(data_to_voul_up)):
- data_to_voul_up[i][0] = int(data_to_voul_up[i][0] * 1.2)
- data_to_voul_up[i][1] = int(data_to_voul_up[i][1] * 1.2)
- if data_to_voul_up[i][0] > MAX_VOLUME:
- data_to_voul_up[i][0] = MAX_VOLUME
- elif data_to_voul_up[i][0] < MIN_VOLUME:
- data_to_voul_up[i][0] = MIN_VOLUME
- if data_to_voul_up[i][1] > MAX_VOLUME:
- data_to_voul_up[i][1] = MAX_VOLUME
- elif data_to_voul_up[i][1] < MIN_VOLUME:
- data_to_voul_up[i][1] = MIN_VOLUME
- return data_to_voul_up
- except: # probebly inputed this data : [[]], which means len(data_to_voul_up) existes but data_to_voul_up[0][1] doesnt and cause the breakdown of the program
- return data_to_voul_up
- def decrease_volume(data_to_voul_down):
- """
- devides the values of the 'play-List' list by 1.2
- :param data_to_voul_down: the list of list to modify
- :return: list of modifide lists made of [left ear sound, right ear sound]
- """
- try:
- for i in range(len(data_to_voul_down)):
- data_to_voul_down[i][0] = int(data_to_voul_down[i][0] / 1.2)
- data_to_voul_down[i][1] = int(data_to_voul_down[i][1] / 1.2)
- if data_to_voul_down[i][0] > MAX_VOLUME:
- data_to_voul_down[i][0] = MAX_VOLUME
- elif data_to_voul_down[i][0] < MIN_VOLUME:
- data_to_voul_down[i][0] = MIN_VOLUME
- if data_to_voul_down[i][1] > MAX_VOLUME:
- data_to_voul_down[i][1] = MAX_VOLUME
- elif data_to_voul_down[i][1] < MIN_VOLUME:
- data_to_voul_down[i][1] = MIN_VOLUME
- return data_to_voul_down
- except: # probebly inputed this data : [[]], which means len(data_to_voul_up) existes but data_to_voul_up[0][1] doesnt and cause the breakdown of the program
- return data_to_voul_down
- def lowpass_filter(data_to_filter):
- """
- recive list of lists made of [left ear sound, right ear sound] and
- change the values of each 'ear' to be the average of the current ear
- the following and the previus ear (if possible)
- :param data_to_filter: list of lists made of
- :return: filttered_data - the filtered list of lists
- """
- # The returned list:
- filttered_data = []
- try:
- if len(data_to_filter) == 1:
- return data_to_filter
- if len(data_to_filter) > 1:
- # code that takes care of the first note
- filttered_left = int((data_to_filter[0][0] +
- data_to_filter[1][0]) / 2)
- filttered_right = int((data_to_filter[0][1] +
- data_to_filter[1][1]) / 2)
- filttered_data.append([filttered_left, filttered_right])
- # code that append the right values to the list
- for i in range(1, len(data_to_filter) - 1):
- filttered_left = int((data_to_filter[i][0] +
- data_to_filter[i - 1][0] +
- data_to_filter[i + 1][0]) / 3)
- filttered_right = int((data_to_filter[i][1] +
- data_to_filter[i - 1][1] +
- data_to_filter[i + 1][1]) / 3)
- filttered_data.append([filttered_left, filttered_right])
- # next piece of code take care of the last note
- if len(data_to_filter) > 1:
- filttered_left = int((data_to_filter[-2][0] +
- data_to_filter[-1][0]) / 2)
- filttered_right = int((data_to_filter[-2][1] +
- data_to_filter[-1][1]) / 2)
- filttered_data.append([filttered_left, filttered_right])
- return filttered_data
- except: # probebly non-legit input cause this instead of breakdown
- return data_to_filter
- if inner_action_editing == 1:
- new_data = reverse(file_data)
- if inner_action_editing == 2:
- new_data = increase_speed(file_data)
- if inner_action_editing == 3:
- new_data = decrease_speed(file_data)
- if inner_action_editing == 4:
- new_data = increase_volume(file_data)
- if inner_action_editing == 5:
- new_data = decrease_volume(file_data)
- if inner_action_editing == 6:
- new_data = lowpass_filter(file_data)
- return new_data
- if __name__ == "__main__":
- while True:
- action = int(input(
- "what would you like to do:\n"
- "Enter 1 to change the music file \n"
- "Enter 2 to compose music\n"
- "Enter 3 to exit\n"))
- if action == 3:
- break
- else:
- if action == 1:
- check = wave_helper.load_wave(input("Please enter wav file"))
- if check == -1:
- print("please enter a legit wav file if you still want to edit"
- ". ")
- continue
- else:
- data = check[1]
- rate = check[0]
- keep_editing = True
- if action == 2:
- data = composeWav()
- rate = SAMPLE_RATE
- while keep_editing:
- # editing continusly until the user enters 7 for breaking out
- editing_action = int(input(
- "Choose one of the following editing options\n"
- "Press 1 to :Reverse\n"
- "Press 2 to :Increase speed\n"
- "Press 3 to :Decrease speed\n"
- "Press 4 to :Increase volume\n"
- "Press 5 to :Decrease volume\n"
- "Press 6 to :Low pass filter\n"
- "Press 7 to :Save \n"))
- if editing_action == 7:
- keep_editing = False
- elif editing_action in [0, 1, 2, 3, 4, 5, 6]:
- data = file_editing_main(editing_action, data)
- else:
- print(
- "not legit input,if you still want to edit next time"
- " choose one the next options please")
- # saves the wav file
- finish_creating_menu(rate, data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement