Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.74 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. """
  58.  
  59. :param frame_rate:
  60. :param audio_data:
  61. :return:
  62. """
  63. new_file_name = input("please enter the file name you "
  64. "wish to save the new wav file in: ")
  65. wave_helper.save_wave(frame_rate, audio_data, new_file_name) == -1
  66.  
  67.  
  68. def file_editing_main(inner_action_editing, file_data):
  69.  
  70. def reverse(data_to_reverse):
  71. """
  72. returnes the recived list, ustarting from the last item and going
  73. backwards till the first one eventually
  74. :param data_to_reverse: list of data made of list of 2 ints each
  75. :return: the reversed data
  76. """
  77. return data_to_reverse[::-1]
  78.  
  79. def increase_speed(data_to_speedup):
  80. """
  81. return odd placed lists of the data_to_speed list
  82. :param data_to_speedup:list of list that represents the volume data
  83. needed shortening
  84. :return: the shortened list of lists (unless its impossible)
  85. """
  86. return data_to_speedup[::2]
  87.  
  88. def decrease_speed(data):
  89. """
  90. adds note between every 2 existing notes, made of the average sound of its nighboores
  91. from the list of lists, which make the overoall music slower
  92. :param data_to_speedup:list of list that represents the volume data
  93. needed lengthening (slowing)
  94. :return: the slow downed list
  95. """
  96. try:
  97. data_len = len(data)
  98. slowed_down_data = []
  99.  
  100. if data_len == 1: # if the there is only one item, doubles that item and return the list.
  101. data.append(data[0])
  102. return data
  103.  
  104. for i in range(
  105. (data_len - 1)): # append for each stands for each cell,
  106. # 2 inserts minos the last one thats only last
  107. slowed_down_data.append(data[i])
  108. left_ear_data_to_add = int((data[i][0] + data[i + 1][0]) / 2)
  109. right_ear_data_to_add = int((data[i][1] + data[i + 1][1]) / 2)
  110. slowed_down_data.append(
  111. [left_ear_data_to_add, right_ear_data_to_add])
  112. slowed_down_data.append(data[data_len - 1])
  113. return slowed_down_data
  114. except: # if data has no length
  115. return data
  116.  
  117. def increase_volume(data_to_voul_up):
  118. """
  119. multiply the values of the 'play-List' list by 1.2 up to the value
  120. +- 32767
  121. :param data_to_voul_up:
  122. :return: list of modifide lists made of [left ear sound, right ear sound]
  123. """
  124. try:
  125. for i in range(len(data_to_voul_up)):
  126. data_to_voul_up[i][0] = int(data_to_voul_up[i][0] * 1.2)
  127. data_to_voul_up[i][1] = int(data_to_voul_up[i][1] * 1.2)
  128.  
  129. if data_to_voul_up[i][0] > MAX_VOLUME:
  130. data_to_voul_up[i][0] = MAX_VOLUME
  131. elif data_to_voul_up[i][0] < MIN_VOLUME:
  132. data_to_voul_up[i][0] = MIN_VOLUME
  133.  
  134. if data_to_voul_up[i][1] > MAX_VOLUME:
  135. data_to_voul_up[i][1] = MAX_VOLUME
  136. elif data_to_voul_up[i][1] < MIN_VOLUME:
  137. data_to_voul_up[i][1] = MIN_VOLUME
  138.  
  139. return data_to_voul_up
  140. 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
  141. return data_to_voul_up
  142.  
  143. def decrease_volume(data_to_voul_down):
  144. """
  145. devides the values of the 'play-List' list by 1.2
  146. :param data_to_voul_down: the list of list to modify
  147. :return: list of modifide lists made of [left ear sound, right ear sound]
  148. """
  149. try:
  150. for i in range(len(data_to_voul_down)):
  151. data_to_voul_down[i][0] = int(data_to_voul_down[i][0] / 1.2)
  152. data_to_voul_down[i][1] = int(data_to_voul_down[i][1] / 1.2)
  153.  
  154. if data_to_voul_down[i][0] > MAX_VOLUME:
  155. data_to_voul_down[i][0] = MAX_VOLUME
  156. elif data_to_voul_down[i][0] < MIN_VOLUME:
  157. data_to_voul_down[i][0] = MIN_VOLUME
  158.  
  159. if data_to_voul_down[i][1] > MAX_VOLUME:
  160. data_to_voul_down[i][1] = MAX_VOLUME
  161. elif data_to_voul_down[i][1] < MIN_VOLUME:
  162. data_to_voul_down[i][1] = MIN_VOLUME
  163.  
  164. return data_to_voul_down
  165. 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
  166. return data_to_voul_down
  167.  
  168. def lowpass_filter(data_to_filter):
  169. """
  170. recive list of lists made of [left ear sound, right ear sound] and
  171. change the values of each 'ear' to be the average of the current ear
  172. the following and the previus ear (if possible)
  173. :param data_to_filter: list of lists made of
  174. :return: filttered_data - the filtered list of lists
  175. """
  176. # The returned list:
  177. filttered_data = []
  178.  
  179. try:
  180. if len(data_to_filter) == 1:
  181. return data_to_filter
  182.  
  183. if len(data_to_filter) > 1:
  184. # code that takes care of the first note
  185. filttered_left = int((data_to_filter[0][0] +
  186. data_to_filter[1][0]) / 2)
  187. filttered_right = int((data_to_filter[0][1] +
  188. data_to_filter[1][1]) / 2)
  189. filttered_data.append([filttered_left, filttered_right])
  190.  
  191. # code that append the right values to the list
  192. for i in range(1, len(data_to_filter) - 1):
  193. filttered_left = int((data_to_filter[i][0] +
  194. data_to_filter[i - 1][0] +
  195. data_to_filter[i + 1][0]) / 3)
  196. filttered_right = int((data_to_filter[i][1] +
  197. data_to_filter[i - 1][1] +
  198. data_to_filter[i + 1][1]) / 3)
  199. filttered_data.append([filttered_left, filttered_right])
  200. # next piece of code take care of the last note
  201. if len(data_to_filter) > 1:
  202. filttered_left = int((data_to_filter[-2][0] +
  203. data_to_filter[-1][0]) / 2)
  204. filttered_right = int((data_to_filter[-2][1] +
  205. data_to_filter[-1][1]) / 2)
  206. filttered_data.append([filttered_left, filttered_right])
  207. return filttered_data
  208. except: # probebly non-legit input cause this instead of breakdown
  209. return data_to_filter
  210.  
  211.  
  212. if inner_action_editing == 1:
  213. new_data = reverse(file_data)
  214.  
  215. if inner_action_editing == 2:
  216. new_data = increase_speed(file_data)
  217.  
  218. if inner_action_editing == 3:
  219. new_data = decrease_speed(file_data)
  220.  
  221. if inner_action_editing == 4:
  222. new_data = increase_volume(file_data)
  223.  
  224. if inner_action_editing == 5:
  225. new_data = decrease_volume(file_data)
  226.  
  227. if inner_action_editing == 6:
  228. new_data = lowpass_filter(file_data)
  229.  
  230. return new_data
  231.  
  232.  
  233. if __name__ == "__main__":
  234. while True:
  235. action = int(input(
  236. "what would you like to do:\n"
  237. "Enter 1 to change the music file \n"
  238. "Enter 2 to compose music\n"
  239. "Enter 3 to exit\n"))
  240.  
  241. if action == 3:
  242. break
  243.  
  244. else:
  245. if action == 1:
  246. check = wave_helper.load_wave(input("Please enter wav file"))
  247. if check == -1:
  248. print("please enter a legit wav file if you still want to edit"
  249. ". ")
  250. continue
  251. else:
  252. data = check[1]
  253. rate = check[0]
  254. keep_editing = True
  255.  
  256. if action == 2:
  257. data = composeWav()
  258. rate = SAMPLE_RATE
  259.  
  260. while keep_editing:
  261. # editing continusly until the user enters 7 for breaking out
  262. editing_action = int(input(
  263. "Choose one of the following editing options\n"
  264. "Press 1 to :Reverse\n"
  265. "Press 2 to :Increase speed\n"
  266. "Press 3 to :Decrease speed\n"
  267. "Press 4 to :Increase volume\n"
  268. "Press 5 to :Decrease volume\n"
  269.  
  270. "Press 6 to :Low pass filter\n"
  271. "Press 7 to :Save \n"))
  272. if editing_action == 7:
  273. keep_editing = False
  274. elif editing_action in [0, 1, 2, 3, 4, 5, 6]:
  275. data = file_editing_main(editing_action, data)
  276.  
  277. else:
  278. print(
  279. "not legit input,if you still want to edit next time"
  280. " choose one the next options please")
  281. # saves the wav file
  282. finish_creating_menu(rate, data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement