You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
6.9 KiB

5 years ago
  1. import tensorflow as tf
  2. import numpy as np
  3. import librosa
  4. import scipy.signal as signal
  5. from operator import itemgetter
  6. from pyknon.genmidi import Midi
  7. from pyknon.music import NoteSeq, Note, Rest
  8. import music21
  9. filename = 'title.wav'
  10. def print_pdf(filename) :
  11. chords = ['C', 'D', 'E', 'F', 'G', 'A', 'B', 'HC']
  12. path1 = '/Users/seungwoomun/Documents/MYC/train/Grand Piano/'
  13. path2 = '/Users/seungwoomun/Documents/MYC/train/Classical Grand/'
  14. path3 = '/Users/seungwoomun/Documents/MYC/train/test_wav/'
  15. path4 = '/Users/seungwoomun/Documents/MYC/train/Electric Piano/'
  16. path5 = '/Users/seungwoomun/Documents/MYC/code/wav/'
  17. lable = [[1, 0, 0, 0, 0, 0, 0, 0], # 도 C
  18. [0, 1, 0, 0, 0, 0, 0, 0], # 레 D
  19. [0, 0, 1, 0, 0, 0, 0, 0], # 미 E
  20. [0, 0, 0, 1, 0, 0, 0, 0], # 파 F
  21. [0, 0, 0, 0, 1, 0, 0, 0], # 솔 G
  22. [0, 0, 0, 0, 0, 1, 0, 0], # 라 A
  23. [0, 0, 0, 0, 0, 0, 1, 0], # 시 B
  24. [0, 0, 0, 0, 0, 0, 0, 1]] # 도 HC
  25. test_pred = []
  26. test_result = []
  27. music = ''
  28. # filename = 'star_1.wav'
  29. # X, Y 데이터 추출
  30. audio_sample, sampling_rate = librosa.load(path5 + filename, sr = None)
  31. input_data = np.abs(librosa.stft(audio_sample, n_fft = 1024, hop_length = 512, win_length = 1024, window = signal.hann))
  32. filename = filename.replace('.wav',"")
  33. # input_data의 배열 형태를 알아본다
  34. shape = np.shape(input_data)
  35. nb_samples = shape[0]
  36. nb_windows = shape[1]
  37. # [nb_samples][nb_windows] 형태이므로, 딥러닝을 수행하기 위한 배열 형태 [nb_windows][nb_samples]로 맞춰준다
  38. input_data = input_data.T
  39. # 학습용 레이블 배열을 생성한다
  40. lable_tmp = [lable[0] for row in range(nb_windows)]
  41. # 하나의 배열로 합친다
  42. x_test = np.array(input_data)
  43. y_test = np.array(lable_tmp)
  44. model = tf.keras.models.load_model("./DNN_Model_500.h5")
  45. print("####### Model 평가 #######")
  46. loss, acc = model.evaluate(x_test, y_test)
  47. print("모델의 정확도: {:5.2f}%".format(100 * acc))
  48. prediction = model.predict(x_test)
  49. # 예측값에서 음계 구분
  50. for i in prediction :
  51. if i.argmax() == 0:
  52. test_pred.append("")
  53. # test_pred = np.r_["도"]
  54. elif i.argmax() == 1 :
  55. test_pred.append("")
  56. # test_pred = np.r_["레"]
  57. elif i.argmax() == 2 :
  58. test_pred.append("")
  59. # test_pred = np.r_["미"]
  60. elif i.argmax() == 3 :
  61. test_pred.append("")
  62. # test_pred = np.r_["파"]
  63. elif i.argmax() == 4 :
  64. test_pred.append("")
  65. # test_pred = np.r_["솔"]
  66. elif i.argmax() == 5 :
  67. test_pred.append("")
  68. # test_pred = np.r_["라"]
  69. elif i.argmax() == 6 :
  70. test_pred.append("")
  71. # test_pred = np.r_["시"]
  72. elif i.argmax() == 7 :
  73. test_pred.append("h_도")
  74. # test_pred = np.r_["h_도"]
  75. # 예측값에서 음계 개수 추출
  76. # print(test_pred)
  77. # print(len(test_pred))
  78. # print(test_pred[448])
  79. print(np.shape(test_pred))
  80. print("도 : " + str(test_pred.count("")))
  81. print("레 : " + str(test_pred.count("")))
  82. print("미 : " + str(test_pred.count("")))
  83. print("파 : " + str(test_pred.count("")))
  84. print("솔 : " + str(test_pred.count("")))
  85. print("라 : " + str(test_pred.count("")))
  86. print("시 : " + str(test_pred.count("")))
  87. print("h_도 : " + str(test_pred.count("h_도")))
  88. # 이전 음계 추출
  89. # for i in range(0, len(test_pred) - 1) :
  90. # if test_pred[i] != test_pred[i + 1] :
  91. # test_result.append(test_pred[i])
  92. # if i + 1 == len(test_pred) - 1 :
  93. # test_result.append(test_pred[i + 1])
  94. toc = 0
  95. # coc = 0
  96. # 최종 음계 추출
  97. for i in range(0, len(test_pred) - 1) :
  98. if test_pred[i] == test_pred[i + 1] :
  99. toc += 1
  100. # coc += 1
  101. else :
  102. toc = 0
  103. # coc = 0
  104. if toc >= 40 :
  105. toc = 0
  106. test_result.append(test_pred[i])
  107. # if toc >= 40 and toc < 50 :
  108. # toc = 0
  109. # if coc > 80 :
  110. # coc = 0
  111. # else :
  112. # test_result.append(test_pred[i])
  113. # if toc >= 30 :
  114. # toc = 0
  115. # # test_result.append(test_pred[i])
  116. # elif toc >= 20 and toc < 25 :
  117. # toc = 0
  118. # test_result.append(test_pred[i])
  119. print(test_result)
  120. print(np.shape(test_result))
  121. print("음 개수 : " + str(len(test_result)))
  122. # midi file 생성
  123. notes = NoteSeq(music)
  124. # midi file 생성을 위한 구분
  125. for index in test_result :
  126. if index == '' :
  127. # music = music + 'C4'
  128. notes.append(Note(12, octave = 4, dur = 1/4)) # 21 A 22 A# 23 B 24 C
  129. elif index == '' :
  130. # music = music + 'D4'
  131. notes.append(Note(14, octave = 4, dur = 1/4)) # 21 A 22 A# 23 B 24 C
  132. elif index == '' :
  133. # music = music + 'E4'
  134. notes.append(Note(16, octave = 4, dur = 1/4)) # 21 A 22 A# 23 B 24 C
  135. elif index == '' :
  136. # music = music + 'F4'
  137. notes.append(Note(17, octave = 4, dur = 1/4)) # 21 A 22 A# 23 B 24 C
  138. elif index == '' :
  139. # music = music + 'G4'
  140. notes.append(Note(19, octave = 4, dur = 1/4)) # 21 A 22 A# 23 B 24 C
  141. elif index == '' :
  142. # music = music + 'A4'
  143. notes.append(Note(21, octave = 4, dur = 1/4)) # 21 A 22 A# 23 B 24 C
  144. elif index == '' :
  145. # music = music + 'B4'
  146. notes.append(Note(23, octave = 4, dur = 1/4)) # 21 A 22 A# 23 B 24 C
  147. elif index == 'h_도' :
  148. # music = music + 'C4'
  149. notes.append(Note(24, octave = 4, dur = 1/4)) # 21 A 22 A# 23 B 24 C
  150. # music = music + ' '
  151. # notes.append(Note(24, octave = 4, dur = 1/8)) # 21 A 22 A# 23 B 24 C
  152. midi = Midi(number_tracks = 1, tempo = 90)
  153. midi.seq_notes(notes, track = 0)
  154. midi.write('Midi/' + filename + '.mid')
  155. # Midi to pdf
  156. us = music21.environment.UserSettings() # music21 환경 설정
  157. midi_file = music21.converter.parse('Midi/' + filename + '.mid')
  158. midi_file.insert(0, music21.metadata.Metadata())
  159. midi_file.metadata.title = 'Make Your Chord'
  160. midi_file.metadata.composer = 'MYC'
  161. midi_file.write('musicxml.pdf', fp = 'pdf/'+ filename + '.pdf')
  162. print(midi_file)
  163. # index, value = max(enumerate(prediction[0]), key = itemgetter(1))
  164. # print(index, value)
  165. # # for i in x_test :
  166. # 검증
  167. # print("####### Model 검증 #######")
  168. # for i in range(0, len(x_test)) :
  169. # test = np.array([x_test[i]])
  170. # # print(test)
  171. # print(model.predict(test))
  172. print_pdf(filename)