-
Notifications
You must be signed in to change notification settings - Fork 0
/
vocaliser.py
112 lines (98 loc) · 2.73 KB
/
vocaliser.py
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
from midiutil.MidiFile import MIDIFile
import sys
time = 0
def noteToPitch(note):
tone = 0
octave = 0
name = note[:-1]
if name == "C" or name == "B#":
tone = 0
if name == "C#" or name == "Db":
tone = 1
if name == "D":
tone = 2
if name == "D#" or name == "Eb":
tone = 3
if name == "E" or name == "Fb":
tone = 4
if name == "F" or name == "E#":
tone = 5
if name == "F#" or name == "Gb":
tone = 6
if name == "G":
tone = 7
if name == "G#" or name == "Ab":
tone = 8
if name == "A":
tone = 9
if name == "A#" or name == "Bb":
tone = 10
if name == "B" or name == "Cb":
tone = 11
octave = int(note[-1:])
pitch = tone + octave * 12 + 21
return pitch
def makeProgression( first, last, progression, MyMIDI ):
global time
for p in range(first,last + 1):
MyMIDI.addNote(0,0,p,time,1,100)
MyMIDI.addNote(0,0,p + 4,time,1,100)
MyMIDI.addNote(0,0,p + 7,time,1,100)
time = time + 2
for note in progression:
note = note.split("/")
pitch = noteToPitch(note[0]) + p - first
duration = float(note[1])
MyMIDI.addNote(0,0,pitch,time,duration,100)
time = time + duration
MyMIDI.addNote(0,0,p,time,1,100)
MyMIDI.addNote(0,0,p + 4,time,1,100)
MyMIDI.addNote(0,0,p + 7,time,1,100)
time = time + 1
for p in range(last-1,first-1,-1):
MyMIDI.addNote(0,0,p,time,1,100)
MyMIDI.addNote(0,0,p + 4,time,1,100)
MyMIDI.addNote(0,0,p + 7,time,1,100)
time = time + 2
for note in progression:
note = note.split("/")
pitch = noteToPitch(note[0]) + p - first
duration = float(note[1])
MyMIDI.addNote(0,0,pitch,time,duration,100)
time = time + duration
MyMIDI.addNote(0,0,p,time,1,100)
MyMIDI.addNote(0,0,p + 4,time,1,100)
MyMIDI.addNote(0,0,p + 7,time,1,100)
time = time + 1
return MyMIDI
def main(argv):
if len(argv) == 1:
for line in open(argv[0],'r'):
MyMIDI = MIDIFile(1)
MyMIDI.addTempo(0,0,120)
array = line.split(";")
MyMIDI = makeProgression(noteToPitch(array[0]),noteToPitch(array[1]),array[2].split(" "), MyMIDI)
writeToFile(array[3].rstrip('\n'), MyMIDI)
MyMIDI = None
else:
print "Enter first note in sequence: "
firstnote = noteToPitch(raw_input())
# process first note
print "Enter last note in sequence: "
lastnote = noteToPitch(raw_input())
# process last note
print "Enter first note progression in the following format: "
print "note/duration note/duration note/duration"
progression = raw_input()
# process note progression
progression = progression.split(" ")
makeProgression(firstnote,lastnote,progression)
print "Enter file name: "
filename = raw_input()
writeToFile(filename)
def writeToFile(filename, MyMIDI):
binfile = open(filename, 'wb')
MyMIDI.writeFile(binfile)
binfile.close()
if __name__ == "__main__":
main(sys.argv[1:])