-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonWavToNotes.py
More file actions
307 lines (219 loc) · 6.51 KB
/
pythonWavToNotes.py
File metadata and controls
307 lines (219 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from __future__ import division
import scipy.io.wavfile as wavfile
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn
from scipy.signal import argrelextrema
from scipy.interpolate import interp1d
import abjad
import re
import playByFreq
from pythonCLIinput import get_file_name
# get wave input from the files selection
# WAV = 'testSong.wav'
WAV = input("Which file to get notes from: ")
rate, data = wavfile.read(WAV)
time = np.arange(len(data[:,0]))*1.0/rate
nfft = 1024*6
pxx, freq, bins, plot = plt.specgram(data[:,0],NFFT=nfft)
plt.show()
a = np.mean(pxx,axis=0)
aa = np.arange(len(a))
a = a/np.max(a)*np.max(data[:,0])
aa = aa/np.max(aa) * time[-1]
f = interp1d(aa,a)
newSmooth = f(time)
indMax = argrelextrema(newSmooth, np.greater)[0]
indMin = argrelextrema(newSmooth, np.less)[0]
lastValue = np.where(newSmooth==newSmooth[-1])[0]
indMin = np.hstack((indMin,lastValue))
# plt.plot(time,data[:,0])
# plt.plot(aa,a)
# plt.plot(time[indMax],newSmooth[indMax])
# plt.plot(time[indMin],newSmooth[indMin])
# plt.show()
NoteFile = pd.read_excel('NoteFreq.xlsx',0)
notes = np.array([indMax,indMin]).T
def getHarmonics(p,f,maxPower,maxFrequency,harmonics,harm):
x = maxFrequency/harm
#problem is that its not exactly in f
ind1 = np.where(f<=x+1)
ind2 = np.where(f>=x-1)
mask = np.in1d(ind1,ind2)
index = np.where(mask == True)[0]
# print('frequency')
# print (f[index])
condition = p[index]/maxPower
try:
condition = condition[0]
except IndexError:
pass
#if p[index]/maxPower>=0.90:
if condition>=0.90:
harmonics.append(f[index][0])
return harmonics
def getFreq(notes):
individualNotes = []
freqs = []
letterNotes = []
for i,v in enumerate(notes):
individualNotes.append(data[v[0]:v[1],0])
p = 20*np.log10(np.abs(np.fft.rfft(data[v[0]:v[1], 0])))
f = np.linspace(0, rate/2.0, len(p))
#plt.plot(f,p)
#plt.show()
harmonics = []
maxPower = np.max(p)
maxFrequency = f[np.where(p==max(p))][0]
# print ('HERE')
# print (i)
for j in range(2,8):
harmonics = getHarmonics(p,f,maxPower,maxFrequency,harmonics,j)
if harmonics==[]:
harmonics = [maxFrequency]
#
# print ('HARMONICS')
# print (harmonics)
maxFreq = harmonics
a = NoteFile[NoteFile['Lower']<maxFreq[0]]
b = NoteFile[NoteFile['Upper']>maxFreq[0]]
note = a.join(b,how='inner',lsuffix='Lower').index[0]
letterNotes.append(note)
return letterNotes, freqs, individualNotes
letterNotes, freqs, individualNotes = getFreq(notes)
staff = abjad.Staff()
def fixNotes(letters):
m = letters
if m[-1]=='0':
#note = m[0].upper()+ m[0].upper()+ m[0].upper()
note = m[0]+3*','
if m[-1]=='1':
#note = m[0].upper()+ m[0].upper()
note = m[0]+2*','
if m[-1]=='2':
note = m[0]+','
if m[-1]=='3':
note = m[0]
if m[-1]=='4':
note = m[0]+"'"
if m[-1]=='5':
note = m[0]+2*"'"
if m[-1]=='6':
note = m[0]+3*"'"
if m[-1]=='7':
note = m[0]+4*"'"
if m[-1]=='8':
note = m[0]+5*"'"
if m[-1]=='9':
note = m[0]+6*"'"
if m[-1]=='10':
note = m[0]+7*"'"
if m[1]=='s':
fixed = note
fixed = fixed[:1]+'s'+fixed[1:]
else:
fixed = note
return fixed
notesSheet = pd.read_csv("constFiles/freqMatch.csv")
print(notesSheet)
frequenciesArray = []
notesArray = []
for i,v in enumerate(letterNotes):
# print (v)
# print(i)
notesArray.append(notesSheet["LetterRep"][v + 14])
frequenciesArray.append(notesSheet["AvgFreq"][v + 14])
letters = letterNotes[i]
try:
fixed = fixNotes(letters)
staff.append(fixed)
# print("YAY")
except UnboundLocalError:
m = re.search('\w.\d',letters)
shortenedNote = m.group(0)
newNote = shortenedNote[0]+'s'+shortenedNote[-1]
fixed = fixNotes(newNote)
# print (fixed)
staff.append(fixed)
pass
except:
pass
# print("Some error")
try:
abjad.show(staff)
except:
pass
# print("lmao rip")
print("frequenciesArray")
print(frequenciesArray)
print("notesArray")
print(notesArray)
print("Playing song ...")
playByFreq.playNotesFromFreqArr(frequenciesArray)
print("Song ended")
def getNotesForC(note):
return "NOTE_" + note
def convert_to_C(songNotes, filename, loopSound):
# create the new text file in the output folder
filePath = "codeOutputs/" + filename + ".txt"
f = open(filePath, "a")
# add imports
f.write('#include "pitches.h"\n')
f.write('#define noteDurations 4\n\n')
# add notes array
f.write("int melody[] = {\n")
notesString = ""
for noteIndex in range(len(songNotes)):
notesString += getNotesForC(songNotes[noteIndex]) + ", "
if noteIndex % 6 == 5:
notesString += "\n"
notesString += getNotesForC(songNotes[-1])
f.writelines(notesString + "\n")
f.write("};\n\n")
musicLoop = """ // iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations;
tone(8, melody,noteDuration);
//pause for the note's duration plus 30 ms:
delay(noteDuration +30);
}"""
f.write("void setup() {\n")
if not loopSound:
f.writelines(musicLoop)
f.write("}\n\n")
f.write("void loop() {\n")
if loopSound:
f.writelines(musicLoop)
f.write("}\n\n")
# add timing variable
f.close()
# Ask to generate C code
confirmation = ""
while(True):
confirmation = input("Generate C code [y/n]: ")
if confirmation == "y":
break
elif confirmation == "n":
break
else:
print("Input invalid!")
if confirmation == "y":
# get c code file name and param
cCodeTitle = get_file_name()
loopSound = False
inputInvalid = True
while (inputInvalid):
confirmation = input("Loop song? [y/n]: ")
if confirmation == "y":
loopSound = True
break
elif confirmation == "n":
break
print("Converting to Arduino Code")
convert_to_C(notesArray, cCodeTitle, loopSound)
print("Song converted! Bye!")
input("(Press any key to exit)")