-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonPianoInput.py
More file actions
387 lines (322 loc) · 10.9 KB
/
pythonPianoInput.py
File metadata and controls
387 lines (322 loc) · 10.9 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import tkinter as tk
from tkinter import *
import time
from datetime import datetime
# declare variables
import playByFreq
currentTime = time.time()
previousTime = time.time()
timeDiff = 0
notesArray = []
timingArray = []
app = tk.Tk()
app.title("Piano input")
app.geometry("700x500")
app.configure(background="light green")
# to add the icon
# app.iconbitmap('FireAnts_logo.ico')
# white keys
pianoKey1 = tk.Button(app,
text="C5\n\n\n\n\n\n\nA",
width=6,
height=10)
pianoKey2 = tk.Button(app,
text="D5\n\n\n\n\n\n\nS",
width=6,
height=10)
pianoKey3 = tk.Button(app,
text="E5\n\n\n\n\n\n\nD",
width=6,
height=10)
pianoKey4 = tk.Button(app,
text="F5\n\n\n\n\n\n\nF",
width=6,
height=10)
pianoKey5 = tk.Button(app,
text="G5\n\n\n\n\n\n\nG",
width=6,
height=10)
pianoKey6 = tk.Button(app,
text="A5\n\n\n\n\n\n\nH",
width=6,
height=10)
pianoKey7 = tk.Button(app,
text="B5\n\n\n\n\n\n\nJ",
width=6,
height=10)
pianoKey8 = tk.Button(app,
text="C6\n\n\n\n\n\n\nK",
width=6,
height=10)
pianoKey9 = tk.Button(app,
text="D6\n\n\n\n\n\n\nL",
width=6,
height=10)
pianoKey10 = tk.Button(app,
text="E6\n\n\n\n\n\n\n;",
width=6,
height=10)
# black keys
pianoKeyB1 = tk.Button(app,
text="CS5\n\n\n\n\n\n\nW",
width=6,
height=10)
pianoKeyB2 = tk.Button(app,
text="DS5\n\n\n\n\n\n\nE",
width=6,
height=10)
pianoKeyB3 = tk.Button(app,
text="FS5\n\n\n\n\n\n\nR",
width=6,
height=10)
pianoKeyB4 = tk.Button(app,
text="GS5\n\n\n\n\n\n\nT",
width=6,
height=10)
pianoKeyB5 = tk.Button(app,
text="AS5\n\n\n\n\n\n\nY",
width=6,
height=10)
pianoKeyB6 = tk.Button(app,
text="CS6\n\n\n\n\n\n\nU",
width=6,
height=10)
pianoKeyB7 = tk.Button(app,
text="DS6\n\n\n\n\n\n\nI",
width=6,
height=10)
# place white keys
pianoKey1.place(x=50, y=325)
pianoKey2.place(x=110, y=325)
pianoKey3.place(x=170, y=325)
pianoKey4.place(x=230, y=325)
pianoKey5.place(x=290, y=325)
pianoKey6.place(x=350, y=325)
pianoKey7.place(x=410,y=325)
pianoKey8.place(x=470, y=325)
pianoKey9.place(x=530, y=325)
pianoKey10.place(x=590, y=325)
# place black keys
pianoKeyB1.place(x=80, y=175)
pianoKeyB2.place(x=140, y=175)
pianoKeyB3.place(x=260, y=175)
pianoKeyB4.place(x=320, y=175)
pianoKeyB5.place(x=380, y=175)
pianoKeyB6.place(x=500, y=175)
pianoKeyB7.place(x=560, y=175)
# set the color for the black keys
pianoKeyB1.configure(bg="#222222", fg="white", activebackground='gray')
pianoKeyB2.configure(bg="#222222", fg="white", activebackground='gray')
pianoKeyB3.configure(bg="#222222", fg="white", activebackground='gray')
pianoKeyB4.configure(bg="#222222", fg="white", activebackground='gray')
pianoKeyB5.configure(bg="#222222", fg="white", activebackground='gray')
pianoKeyB6.configure(bg="#222222", fg="white", activebackground='gray')
pianoKeyB7.configure(bg="#222222", fg="white", activebackground='gray')
# link functions to white keys
pianoKey1.configure(command=lambda: getNotePart(pianoKey1['text']))
pianoKey2.configure(command=lambda: getNotePart(pianoKey2['text']))
pianoKey3.configure(command=lambda: getNotePart(pianoKey3['text']))
pianoKey4.configure(command=lambda: getNotePart(pianoKey4['text']))
pianoKey5.configure(command=lambda: getNotePart(pianoKey5['text']))
pianoKey6.configure(command=lambda: getNotePart(pianoKey6['text']))
pianoKey7.configure(command=lambda: getNotePart(pianoKey7['text']))
pianoKey8.configure(command=lambda: getNotePart(pianoKey8['text']))
pianoKey9.configure(command=lambda: getNotePart(pianoKey9['text']))
pianoKey10.configure(command=lambda: getNotePart(pianoKey10['text']))
# link functions to black keys
pianoKeyB1.configure(command=lambda: getNotePart(pianoKeyB1['text']))
pianoKeyB2.configure(command=lambda: getNotePart(pianoKeyB2['text']))
pianoKeyB3.configure(command=lambda: getNotePart(pianoKeyB3['text']))
pianoKeyB4.configure(command=lambda: getNotePart(pianoKeyB4['text']))
pianoKeyB5.configure(command=lambda: getNotePart(pianoKeyB5['text']))
pianoKeyB6.configure(command=lambda: getNotePart(pianoKeyB6['text']))
pianoKeyB7.configure(command=lambda: getNotePart(pianoKeyB7['text']))
titleLabel = Label(app,
text="Song Title",
foreground='black',
background='light green',
font=('Comic Sans MS', 12),
width=10,
height=2)
noteText = Label(app,
text="",
background='green',
foreground='white',
font=('Comic Sans MS', 12),
width=10,
height=2)
titleEntry = Entry(app,
width=30,
)
recordBtn = Button(app,
text="Start Recording [,]",
width=15,
height=2)
playBtn = Button(app,
text="Play",
width=10,
height=2)
nextBtn = Button(app,
text="Save",
width=10,
height=2)
titleEntry.place(x=185, y=35)
titleLabel.place(x=80, y=20)
nextBtn.place(x=550, y=40)
noteText.place(x=300, y=100)
recordBtn.place(x=80, y=100)
playBtn.place(x=550, y=100)
recordBtn.configure(command=lambda: toggle_recording())
nextBtn.configure(command=lambda: nextScreen())
playBtn.configure(command=lambda: playBack())
def playBack():
playByFreq.playFromArray(notesArray, timingArray)
def nextScreen():
generateOutPut()
# check if the array is empty
if len(notesArray) == 0:
# show tkinter message box
print("cannot proceed, array empty")
else:
print("Save")
# go to next screen
noteText["text"] = "Saved!"
def toggle_recording():
if recordBtn['text'] == "Stop Recording [,]":
currentTime = time.time()
global previousTime
timeDiff = round((currentTime - previousTime) * 4)
timingArray.append(timeDiff)
# append time diff
noteText["text"] = "stop!"
recordBtn['text'] = "Start Recording [,]"
for i in range(len(timingArray)):
if timingArray[i] == 0:
timingArray[i] = 1
print(notesArray)
print(timingArray)
print(len(notesArray))
print(len(timingArray))
else:
# append blank note to
noteText["text"] = "start!"
recordBtn['text'] = "Stop Recording"
time.sleep(1)
recordBtn['text'] = "Stop Recording [,]"
notesArray.clear()
timingArray.clear()
previousTime = time.time()
def getNotePart(noteStr):
noteParts = noteStr.split()
note = noteParts[0]
addNote(note)
def addNote(note):
global previousTime
# add the time difference estimate to the list of timings
if len(notesArray) != 0:
currentTime = time.time()
timeDiff = round((currentTime - previousTime) * 4)
timingArray.append(timeDiff)
previousTime = currentTime
else:
previousTime = time.time()
# add the new note to the notes array
notesArray.append(note)
# play the note
playByFreq.playNote(note)
# playThread = threading.Thread(target=playByFreq.playNote(note))
# playThread.start()
# playThread.join()
noteText["text"] = note
def keydown(event):
note, charInList = getNoteRepresentation(event.char)
if charInList:
addNote(note)
else:
if event.char == ",":
toggle_recording()
def getNoteRepresentation(character):
if character.lower() == "a":
return "C5", True
elif character.lower() == "s":
return "D5", True
elif character.lower() == "d":
return "E5", True
elif character.lower() == "f":
return "F5", True
elif character.lower() == "g":
return "G5", True
elif character.lower() == "h":
return "A5", True
elif character.lower() == "j":
return "B5", True
elif character.lower() == "k":
return "C6", True
elif character.lower() == "l":
return "D6", True
elif character == ";" or character == ":":
return "E6", True
elif character.lower() == "w":
return "CS5", True
elif character.lower() == "e":
return "DS5", True
elif character.lower() == "r":
return "FS5", True
elif character.lower() == "t":
return "GS5", True
elif character.lower() == "y":
return "AS5", True
elif character.lower() == "u":
return "CS6", True
elif character.lower() == "i":
return "DS6", True
else:
return character, False
def generateOutPut():
# get the file name
title = titleEntry.get()
if not title.strip():
now = datetime.now()
title = now.strftime("%m%d%Y") + "_CCode"
convert_to_C(notesArray, title, False)
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);
}\n"""
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()
app.bind('<KeyPress>', keydown)
app.mainloop()