-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonCLIinput.py
More file actions
177 lines (128 loc) · 4.21 KB
/
pythonCLIinput.py
File metadata and controls
177 lines (128 loc) · 4.21 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
import pygame
import AudioPlayer
# get the name of the file
def get_file_name():
fileTitle = get_file_name_input()
fileTitleInvalid = True
while(fileTitleInvalid):
# check if the title has spaces
if (" " in fileTitle):
print("File name cannot have spaces!")
fileTitle = get_file_name_input()
continue
elif (not fileTitle.strip()):
print("File name cannot be empty!")
fileTitle = get_file_name_input()
continue
elif not (fileTitle.isalpha() or fileTitle.isalnum() or fileTitle.isnumeric()):
print("File name should be contain alphabets or numbers")
fileTitle = get_file_name_input()
continue
fileTitleInvalid = False
return fileTitle
def get_file_name_input():
return input("What should the file be called? ")
# get the string of text representing the music
def get_Notes():
notesList = ["c", "d", "e", "f", "g", "a", "b", "c1"]
songNotes = []
print("Enter notes")
print("example:" + str(notesList))
print('Enter "done" to end')
note = ""
while(note != "done"):
note = input()
if note in notesList:
songNotes.append(note)
print(note)
elif note == "done":
continue
else:
print("Invalid input!")
print("notes recorded!")
print(songNotes)
return songNotes
# test play the notes
def test_play_notes(songNotes):
for note in songNotes:
notePath = "audioFiles/piano/" + note.upper() + ".wav"
AudioPlayer.start_music(notePath, 0.5)
def get_note_for_C(note):
notesList = ["c", "d", "e", "f", "g", "a", "b", "c1"]
notesC = ["NOTE_C4",
"NOTE_D4",
"NOTE_E4",
"NOTE_F4",
"NOTE_G4",
"NOTE_A4",
"NOTE_B4",
"NOTE_C5"]
return notesC[notesList.index(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 += get_note_for_C(songNotes[noteIndex]) + ","
notesString += get_note_for_C(songNotes[-1])
f.write(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()
if __name__ == '__main__':
print("Create new file")
filename = get_file_name()
notesNotEntered = True
while (notesNotEntered):
print("Enter the notes")
songNotes = get_Notes()
print("Playing song")
test_play_notes(songNotes)
inputInvalid = True
confirmation = ""
while(inputInvalid):
confirmation = input("Is the song correct? [y/n]: ")
if confirmation == "y":
break
elif confirmation == "n":
break
else:
print("Input invalid!")
if confirmation == "y":
break
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(songNotes, filename, loopSound)
print("Song converted! Bye!")