Advertisement
Guest User

eri

a guest
Dec 9th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.81 KB | None | 0 0
  1. import wave_helper
  2. import os.path
  3. import math
  4.  
  5. SAMPLE_RATE = 2000
  6. MAX_VOLUME = 32767
  7. MIN_VOLUME = -32768
  8. FREQUENCYDICT = {"A": 440, "B": 494, "C": 523, "D": 587, "E": 659, "F": 698,
  9. "G": 784, "Q": 0}
  10.  
  11.  
  12. def composeWav():
  13. """ accepts input of composition instruction file, creates composition and returns to edit menu (if file not found
  14. or invalid it repeats the promp until a correct file name is entered"""
  15.  
  16. while True:
  17. fileName = input("Enter name of composition instructions file\n")
  18. if os.path.isfile(fileName):
  19.  
  20. print("Composition complete")
  21. wavfile = createCompList(fileName)
  22. return wavfile
  23.  
  24. else:
  25. print("Invalid File")
  26.  
  27.  
  28. def calcSampleVal(frequency, index):
  29. """ calculation formula for sample value given frequency, index"""
  30. if frequency == 0:
  31. return 0
  32. return MAX_VOLUME * math.sin(
  33. math.pi * 2 * (index / (SAMPLE_RATE / frequency)))
  34.  
  35.  
  36. def createCompList(compFile):
  37. f = open(compFile)
  38. f1 = f.readlines()
  39. f.close()
  40. noteList = list()
  41. for lines in f1:
  42. if (lines.split() != ""):
  43. noteList.extend(lines.split())
  44.  
  45. compList = list()
  46.  
  47. for i in range(0, len(noteList), 2):
  48. for ind in range(int((float(noteList[i + 1]) / 16) * SAMPLE_RATE)):
  49. compList.append(
  50. [int(calcSampleVal(FREQUENCYDICT.get(noteList[i]), ind)),
  51. int(calcSampleVal(FREQUENCYDICT.get(noteList[i]), ind))])
  52.  
  53. return compList
  54.  
  55.  
  56. def finish_creating_menu(frame_rate, audio_data):
  57. new_file_name = input("please enter the file name you "
  58. "wish to save the new wav file in: ")
  59.  
  60. save_wave(frame_rate, audio_data, new_file_name)
  61.  
  62.  
  63. def get_file_rate_data():
  64. file_name = input("Please enter the file name")
  65. return wave_helper.load_wave(file_name)
  66.  
  67.  
  68. def file_editing_main(inner_action_editing, file_data):
  69. def reverse(data_to_reverse):
  70. """
  71. switch the position of the first and last, seconde and last -1 and so
  72. on lists in data
  73. :param data_to_reverse: list of data made of list of 2 ints each
  74. :return: the reversed data
  75. """
  76. data_len = len(data_to_reverse)
  77. if data_len == 1:
  78. return data_to_reverse
  79. elif data_len % 2 == 0:
  80. for i in range(int(data_len / 2)):
  81. temp = data_to_reverse[i]
  82. data_to_reverse[i] = data_to_reverse[(data_len - 1) - i]
  83. data_to_reverse[(data_len - 1) - i] = temp
  84. else:
  85. for i in range(int(data_len / 2)):
  86. temp = data_to_reverse[i]
  87. data_to_reverse[i] = data_to_reverse[(data_len - 1) - i]
  88. data_to_reverse[(data_len - 1) - i] = temp
  89. return data_to_reverse
  90.  
  91. def increase_speed(data_to_speedup):
  92. """
  93. cut even notes from the list of lists, which make the overoall music shorter
  94. :param data_to_speedup:list of list that represents the volume data
  95. needed shortening
  96. :return: the shortened list of lists
  97. """
  98. data_len = len(data_to_speedup)
  99. sped_up_data = []
  100. if data_len % 2 == 0:
  101. if data_len != 2:
  102. for i in range(int(data_len / 2)):
  103. sped_up_data.append(data_to_speedup[i * 2])
  104. else:
  105. return
  106. else:
  107. for i in range(int(data_len / 2)):
  108. sped_up_data.append(data_to_speedup[i * 2])
  109. sped_up_data.append(data_to_speedup[data_len - 1])
  110. return sped_up_data
  111.  
  112. def decrease_speed(data_to_slowdown):
  113. """
  114. adds note between every 2 existing notes, made of the average sound of its nighboores
  115. from the list of lists, which make the overoall music slower
  116. :param data_to_speedup:list of list that represents the volume data
  117. needed lengthening (slowing)
  118. :return: the slow downed list
  119. """
  120. data_len = len(data_to_slowdown)
  121. slowed_down_data = []
  122. for i in range(
  123. (data_len - 1)): # append for each stands for each cell,
  124. # 2 inserts minos the last one thats only last
  125. slowed_down_data.append(data[i])
  126. left_ear_data_to_add = int((data[i][0] + data[i + 1][0]) / 2)
  127. right_ear_data_to_add = int((data[i][1] + data[i + 1][1]) / 2)
  128. slowed_down_data.append(
  129. [left_ear_data_to_add, right_ear_data_to_add])
  130. slowed_down_data.append(data[data_len - 1])
  131. return slowed_down_data
  132.  
  133. def increase_volume(data_to_voul_up):
  134. """
  135. multiply the values of the 'play-List' list by 1.2 up to the value
  136. +- 32767
  137. :param data_to_voul_up:
  138. :return: list of modifide lists made of [left ear sound, right ear sound]
  139. """
  140.  
  141. for i in range(len(data_to_voul_up)):
  142. data_to_voul_up[i][0] = int(data_to_voul_up[i][0] * 1.2)
  143. data_to_voul_up[i][1] = int(data_to_voul_up[i][1] * 1.2)
  144.  
  145. if data_to_voul_up[i][0] > MAX_VOLUME:
  146. data_to_voul_up[i][0] = MAX_VOLUME
  147. elif data_to_voul_up[i][0] < MIN_VOLUME:
  148. data_to_voul_up[i][0] = MIN_VOLUME
  149.  
  150. if data_to_voul_up[i][1] > MAX_VOLUME:
  151. data_to_voul_up[i][1] = MAX_VOLUME
  152. elif data_to_voul_up[i][1] < MIN_VOLUME:
  153. data_to_voul_up[i][1] = MIN_VOLUME
  154.  
  155. return data_to_voul_up
  156.  
  157. def decrease_volume(data_to_voul_down):
  158. """
  159. devides the values of the 'play-List' list by 1.2 up to the value
  160. +- 32767
  161. :param data_to_voul_down: the list of list to modify
  162. :return: list of modifide lists made of [left ear sound, right ear sound]
  163. """
  164. for i in range(len(data_to_voul_down)):
  165. data_to_voul_down[i][0] = int(data_to_voul_down[i][0] / 1.2)
  166. data_to_voul_down[i][1] = int(data_to_voul_down[i][1] / 1.2)
  167.  
  168. if data_to_voul_down[i][0] > MAX_VOLUME:
  169. data_to_voul_down[i][0] = MAX_VOLUME
  170. elif data_to_voul_down[i][0] < MIN_VOLUME:
  171. data_to_voul_down[i][0] = MIN_VOLUME
  172.  
  173. if data_to_voul_down[i][1] > MAX_VOLUME:
  174. data_to_voul_down[i][1] = MAX_VOLUME
  175. elif data_to_voul_down[i][1] < MIN_VOLUME:
  176. data_to_voul_down[i][1] = MIN_VOLUME
  177.  
  178. return data_to_voul_down
  179.  
  180. def lowpass_filter(data_to_filter):
  181. """
  182. recive list of lists made of [left ear sound, right ear sound] and
  183. change the values of each 'ear' to be the average of the current ear
  184. the following and the previus ear (if possible)
  185. :param data_to_filter: list of lists made of
  186. :return: filttered_data - the lowpass-ed list of lists
  187. """
  188. # The returned list:
  189. filttered_data = []
  190.  
  191. if len(data_to_filter) > 1:
  192. # code that takes care of the first note
  193. filttered_left = int((data_to_filter[0][0] +
  194. data_to_filter[1][0]) / 2)
  195. filttered_right = int((data_to_filter[0][1] +
  196. data_to_filter[1][1]) / 2)
  197. filttered_data.append([filttered_left, filttered_right])
  198.  
  199. # code that append the right values to the list
  200. for i in range(1, len(data_to_filter) - 1):
  201. filttered_left = int((data_to_filter[i][0] +
  202. data_to_filter[i - 1][0] +
  203. data_to_filter[i + 1][0]) / 3)
  204. filttered_right = int((data_to_filter[i][1] +
  205. data_to_filter[i - 1][1] +
  206. data_to_filter[i + 1][1]) / 3)
  207. filttered_data.append([filttered_left, filttered_right])
  208. # next pice of code take care of the last note
  209. if len(data_to_filter) > 1:
  210. filttered_left = int((data_to_filter[-2][0] +
  211. data_to_filter[-1][0]) / 2)
  212. filttered_right = int((data_to_filter[-2][1] +
  213. data_to_filter[-1][1]) / 2)
  214. filttered_data.append([filttered_left, filttered_right])
  215. return filttered_data
  216.  
  217. if inner_action_editing == 1:
  218. file_data = reverse(file_data)
  219.  
  220. if inner_action_editing == 2:
  221. file_data = increase_speed(file_data)
  222.  
  223. if inner_action_editing == 3:
  224. file_data = decrease_speed(file_data)
  225.  
  226. if inner_action_editing == 4:
  227. file_data = increase_volume(file_data)
  228.  
  229. if inner_action_editing == 5:
  230. file_data = decrease_volume(file_data)
  231.  
  232. if inner_action_editing == 6:
  233. file_data = lowpass_filter(file_data)
  234.  
  235. return file_data
  236.  
  237.  
  238. if __name__ == "__main__":
  239. keep_going = True
  240. while keep_going:
  241. action = int(input(
  242. "what would you like to do:\n"
  243. "Enter 1 to change the music file \n"
  244. "Enter 2 to compose music\n"
  245. "Enter 3 to exit\n"))
  246.  
  247. if action == 1 or action == 2:
  248.  
  249. if action == 1:
  250. check = wave_helper.load_wave(input("Please enter wav file"))
  251. if check == -1:
  252. print("please enter a legit wav file if you still want to edit"
  253. ". ")
  254. continue
  255. else:
  256. data = check[1]
  257. rate = check[0]
  258. keep_editing = True
  259.  
  260. if action == 2:
  261. data = composeWav()
  262. rate = SAMPLE_RATE
  263.  
  264. while keep_editing:
  265. # editing continusly until the user enters 7 for breaking out
  266. editing_action = int(input(
  267. "Choose one of the following editing options\n"
  268. "Press 1 to :Reverse\n"
  269. "Press 2 to :Increase speed\n"
  270. "Press 3 to :Decrease speed\n"
  271. "Press 4 to :Increase volume\n"
  272. "Press 5 to :Decrease volume\n"
  273. "Press 6 to :Low pass filter\n"
  274. "Press 7 to :Save \n"))
  275. if editing_action == 7:
  276. keep_editing = False
  277. elif editing_action in [0, 1, 2, 3, 4, 5, 6]:
  278. file_editing = file_editing_main(editing_action, data)
  279. else:
  280. print(
  281. "not legit input,if you still want to edit next time"
  282. " choose one the next options please")
  283. # saves the wav file
  284. finish_creating_menu(rate, data)
  285.  
  286. elif action == 3:
  287. keep_going = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement