-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmido_test.py
212 lines (191 loc) · 6.74 KB
/
mido_test.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
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
import sys
import os
import subprocess
import mido
from mido import MidiFile, Message, tempo2bpm
import shared
def work_it():
ports()
filename = "cantaloop.mid"
# filename = "Fantasy.mid"
# filename = "Moondance.mid"
# filename = "MARS11=1.mid"
# filename = "Bob_Seger_Turn_The_Page.mid"
mid = MidiFile(filename)
outport = "TiMidity:TiMidity port 0 128:0"
setup_vars()
parse_midi_file(mid)
print(f"{shared.tempo=}")
print(f"{shared.time_signature=}")
print(f"{shared.key_signature=}")
print(f"{shared.tracks}")
songlength = mid.length
print(f"Song length: {int(songlength/60)} minutes, {int(songlength%60)} seconds.")
sound_font = "FluidR3_GM.sf2"
subprocess.call(
[
"fluidsynth",
"-a",
"alsa",
"-m",
"alsa_seq",
"-i",
"-g",
"0.2",
sound_font,
filename,
"-r",
"44100",
]
)
# FluidSynth("FluidR3_GM.sf2")
# FluidSynth().play_midi(filename)
def setup_vars():
shared.trackname = ""
shared.tracknumber = ""
shared.tracks = []
shared.tracktext = ""
shared.copyright = ""
shared.instrument_name = ""
shared.lyrics = ""
shared.marker = ""
shared.cue_marker = ""
shared.device_name = ""
shared.channel_prefix = ""
shared.tempo = ""
shared.midi_port = ""
shared.time_signature = ""
shared.key_signature = ""
shared.sequencer_specific = ""
def parse_midi_file(filedata):
cntr = 0
for i, track in enumerate(filedata.tracks):
for msg in track:
if msg.is_meta:
mesg = str(msg)
mesg = mesg[11:]
# print(mesg)
if "track_name" in mesg:
pos = mesg.find("name=") + 6
pos2 = mesg.find("'", pos + 1)
# print(f"{pos},{pos2}")
tn = mesg[pos:pos2]
print(f"Track: {i} Name: {tn}")
trackinfo = [i, tn]
shared.tracks.append(trackinfo)
elif "end_of_track" in mesg:
pass
# print(f"Track: {i} ENDOFTRACK")
elif "number" in mesg:
print(f"Track: {i} number")
elif "text" in mesg:
print(f"Track: {i} text")
elif "copyright" in mesg:
print(f"Track: {i} copyright")
elif "instrument_name" in mesg:
print(f"Track: {i} instrument")
elif "lyrics" in mesg:
pass
# print(f"Track: {i} lyrics")
elif "marker" in mesg:
pass
# print(f"Track: {i} marker")
elif "cue_marker" in mesg:
pass
# print(f"Track: {i} cue marker")
elif "device_name" in mesg:
pass
# print(f"Track: {i} device")
elif "channel_prefix" in mesg:
pass
# print(f"Track: {i} channel prefix")
elif "midi_port" in mesg:
pass
# print(f"Track: {i} midi port")
elif "set_tempo" in mesg:
# Tempo is in microseconds per beat (quarter note). You can use
# :py:func:`bpm2tempo` and :py:func:`tempo2bpm` to convert to and from
# beats per minute. Note that :py:func:`tempo2bpm` may return a floating
# point number.
pos1 = mesg.find("tempo=") + 6
pos2 = mesg.find(",", pos1)
tempo = mesg[pos1:pos2]
timesig = (4, 4)
# print(f"tempo={tempo} - timesig={timesig}")
bpm = int(mido.midifiles.tempo2bpm(int(tempo)))
print(f"Track: {i} - tempo: {bpm}")
shared.tempo = bpm
elif "smpte_offset" in mesg:
pass
# print(f"Track: {i} smpte offset")
elif "time_signature" in mesg:
pos1 = mesg.find("numerator=") + 10
pos2 = mesg.find(",", pos1)
pos3 = mesg.find("denominator=") + 12
pos4 = mesg.find(",", pos3)
# print(f"{pos1}-{pos2} = {pos3}-{pos4}")
num = mesg[pos1:pos2]
denom = mesg[pos3:pos4]
tsig = f"{num}/{denom}"
print(f"Track: {i} - Time Sig: {tsig}")
shared.time_signature = tsig
elif "key_signature" in mesg:
pos1 = mesg.find("key='") + 5
pos2 = mesg.find("'", pos1)
# print(f"{pos1}-{pos2}")
keysig = mesg[pos1:pos2]
print(f"Track: {i} key_sig: {keysig}")
shared.key_signature = keysig
elif "sequencer_specific" in mesg:
pass
# print("")
# print(f"Track: {i} seq specific")
else:
print(f"Track: {i} unknown message")
cntr += 1
def play_file(output, filename, print_messages):
midi_file = MidiFile(filename)
print(f"Playing {midi_file.filename}.")
length = midi_file.length
print(
"Song length: {} minutes, {} seconds.".format(
int(length / 60), int(length % 60)
)
)
print("Tracks:")
for i, track in enumerate(midi_file.tracks):
print(f" {i:2d}: {track.name.strip()!r}")
for message in midi_file.play(meta_messages=True):
if print_messages:
sys.stdout.write(repr(message) + "\n")
sys.stdout.flush()
if isinstance(message, Message):
output.send(message)
elif message.type == "set_tempo":
print("Tempo changed to {:.1f} BPM.".format(tempo2bpm(message.tempo)))
print()
def print_ports(heading, port_names):
print(heading)
for name in port_names:
print(f" '{name}'")
print()
def ports():
print()
print_ports("Available input Ports:", mido.get_input_names())
print_ports("Available output Ports:", mido.get_output_names())
for name in [
"MIDO_DEFAULT_INPUT",
"MIDO_DEFAULT_OUTPUT",
"MIDO_DEFAULT_IOPORT",
"MIDO_BACKEND",
]:
try:
value = os.environ[name]
print(f"{name}={value!r}")
except LookupError:
print(f"{name} not set.")
print()
print(f"Using backend {mido.backend.name}.")
print()
if __name__ == "__main__":
work_it()