-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathponconverter.py
282 lines (247 loc) · 8.79 KB
/
ponconverter.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
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
from tkinter import filedialog
import tkinter as tk
from operator import indexOf
import matplotlib.pyplot as plt
from scipy.io import wavfile as wav
from scipy import signal
from scipy.fftpack import fft
import numpy as np
import time
from dataclasses import dataclass
import math
import sys
import os
mono = False
# Define a function to get the WAV file path using a file dialog
def get_wav_file_path():
root = tk.Tk()
root.withdraw() # Hide the root window
wav_file_path = filedialog.askopenfilename(title="Select a WAV file")
if not wav_file_path:
print("No WAV file selected. Exiting.")
sys.exit(1)
return wav_file_path
# Define a function to get the output MIDI file path using a file dialog
def get_output_midi_file_path():
root = tk.Tk()
root.withdraw() # Hide the root window
output = filedialog.asksaveasfilename(
title="Save the output MIDI file",
defaultextension=".mid",
filetypes=[("MIDI files", "*.mid")]
)
if not output:
print("No output file selected. Exiting.")
sys.exit(1)
return output
# Get user input for main variables
windowSize = 8192
stepSize = int(input("Enter the step size (default: 1024): ") or 1024)
fac = 1
wav_file_path = get_wav_file_path() # Use the file dialog for WAV input
output = get_output_midi_file_path() # Use the file dialog for MIDI output
@dataclass
class NoteTone:
key: int
tick: int
channel: int
vel: int
tickLen: int
@dataclass
class UnendedNote:
delta: int
channel: int
vel: int
key: int
tick: int
tickLen: int
def BPMtoMicroseconds(n):
return stepSize * 58593.75 / n
def midiNoteFromPitch(n):
return 12.0 * (math.log(n / 220.0) / math.log(2.0)) + 57.01
def toVQL(n):
b = [0, 0, 0, 0, 0]
l = 4
added = 0x00
while True:
v = (n & 0x7F)
n = n >> 7
v = v | added
b[l] = v
l -= 1
added = 0x80
if n == 0:
break
return b[l + 1:]
def convToRawData(notes):
tmpArr = []
prevTime = 0
noteOffs = []
for i in range(len(notes)):
currNote = notes[i]
while len(noteOffs) != 0 and noteOffs[0].tick <= currNote.tick:
e = noteOffs.pop(0)
e.delta = e.tick - prevTime
tmpArr += toVQL(e.delta)
tmpArr += [0x80 | e.channel, e.key, e.vel]
prevTime = e.tick
tmpArr += toVQL(currNote.tick - prevTime)
tmpArr += [0x90 | currNote.channel, currNote.key, currNote.vel]
prevTime = currNote.tick
t = currNote.tick + currNote.tickLen
off = UnendedNote(0, currNote.channel, currNote.vel, currNote.key, t, currNote.tickLen)
pos = len(noteOffs) // 2
if len(noteOffs) == 0:
noteOffs.append(off)
else:
j = len(noteOffs) // 4
while True:
if j <= 0:
j = 1
if pos < 0:
pos = 0
if pos >= len(noteOffs):
pos = len(noteOffs) - 1
u = noteOffs[pos]
if u.tick >= t:
if pos == 0 or noteOffs[pos - 1].tick < t:
noteOffs.insert(pos + 1, off)
break
else:
pos -= j
else:
if pos == len(noteOffs) - 1:
noteOffs.append(off)
break
else:
pos += j
j = j // 2
for nf in range(len(noteOffs)):
noteoff = noteOffs[nf]
noteoff.delta = noteoff.tick - prevTime
tmpArr += toVQL(noteoff.delta)
tmpArr += [0x80 | noteoff.channel, noteoff.key, noteoff.vel]
print(f"Note Event {len(tmpArr)}/{len(notes) * 3} added")
prevTime = noteoff.tick
return tmpArr
noteTonesL = []
noteTonesR = []
# Get Sample rate and Signal data
rate, sound = wav.read(wav_file_path)
if "-ws" in sys.argv:
windowSize = int(sys.argv[indexOf(sys.argv, "-ws") + 1])
if "-mono" in sys.argv:
mono = True
sound = sound / (2 ** 15)
# Split into two separate arrays
if len(sound.shape) == 1:
signalL = sound
mono = True
else:
signalL = sound.sum(axis=1) / 2 if mono else sound[:, 0]
signalR = sound[:, 1]
dirname = os.path.dirname(__file__)
f = open(os.path.join(output), "wb")
byte_arr = []
byte_arr += [ord("M"), ord("T"), ord("h"), ord("d")]
byte_arr += [0x00, 0x00, 0x00, 0x06]
byte_arr += [0x00, 0x01, 0x00, 0x02 if mono else 0x03, 0x03, 0xC0]
track0 = []
track0 += [0x00, 0xFF, 0x58, 0x04, 0x04, 0x02, 0x18, 0x08]
track0 += [0x00, 0xFF, 0x51, 0x03]
convertedBPM = BPMtoMicroseconds(160)
convertedBPM_int = int(convertedBPM)
track0 += [(convertedBPM_int & 0xFF0000) >> 16, (convertedBPM_int & 0xFF00) >> 8, convertedBPM_int & 0xFF]
track0 += [0x00, 0xFF, 0x2F, 0x00]
byte_arr += [ord("M"), ord("T"), ord("r"), ord("k")]
track0_len = len(track0)
byte_arr += [(track0_len & 0xFF000000) >> 24, (track0_len & 0xFF0000) >> 16, (track0_len & 0xFF00) >> 8,
(track0_len & 0xFF)]
byte_arr += track0
byte_arr += [ord("M"), ord("T"), ord("r"), ord("k")]
track1 = []
if not mono:
for i in range(8):
track1 += [0x00, 0xB0 | i, 0x0A, 0x00]
lastChunkL = False
print(f"Processing L Channel...")
hasPrintedOnce = False
progress = 0
# Goes on until an error lol
for i in range(160000000):
# Where to put tfr_spec()????
try:
chunk = signalL[stepSize * progress:stepSize * progress + windowSize]
window = signal.windows.blackmanharris(chunk.size)
chunk = chunk * window
fft_spec = np.fft.rfft(chunk)
freq = np.fft.rfftfreq(chunk.size, d=1 / rate)
spec_abs = np.abs(fft_spec)
peakFreqs, _ = signal.find_peaks(spec_abs, threshold=-1)
for p in range(len(peakFreqs)):
j = peakFreqs[p]
fr = spec_abs[j]
if freq[j] > 0.0:
notePitch = round(midiNoteFromPitch(freq[j]))
if 1 <= notePitch < 128 and fr / fac > 1:
tmpVel = fr / fac
vel = 127 if tmpVel > 127 else 1 if tmpVel < 1 else int(tmpVel)
noteTonesL.append(NoteTone(notePitch, 58 * i, math.floor(vel / 16), vel, 58))
progress += 1
print(f"Chunk {progress} done")
except:
progress = 0
print("Channel L Done")
break
track1 += convToRawData(noteTonesL)
track1 += [0x00, 0xFF, 0x2F, 0x00]
track1_len = len(track1)
byte_arr += [(track1_len & 0xFF000000) >> 24, (track1_len & 0xFF0000) >> 16, (track1_len & 0xFF00) >> 8,
(track1_len & 0xFF)]
byte_arr += track1
if not mono:
byte_arr += [ord("M"), ord("T"), ord("r"), ord("k")]
track2 = []
for i in range(8):
track2 += [0x00, 0xB0 | (i + 8), 0x08, 0x7F]
lastChunkR = False
progress = 0
print("Processing R Channel...")
for i in range(160000000):
try:
chunk = signalR[stepSize * progress:stepSize * progress + windowSize]
if stepSize * progress + windowSize >= len(signalR) - 1:
chunk = signalR[stepSize * progress:len(signalR) - 1]
lastChunkR = True
window = signal.windows.blackmanharris(chunk.size)
chunk = chunk * window
fft_spec = np.fft.rfft(chunk)
freq = np.fft.rfftfreq(chunk.size, d=1 / rate)
spec_abs = np.abs(fft_spec)
peakFreqs, _ = signal.find_peaks(spec_abs, threshold=-1)
for p in range(len(peakFreqs)):
j = peakFreqs[p]
fr = spec_abs[j]
if freq[j] > 0.0:
notePitch = round(midiNoteFromPitch(freq[j]))
if 1 <= notePitch < 128 and fr / fac > 1:
tmpVel = fr / fac
vel = 127 if tmpVel > 127 else 1 if tmpVel < 1 else int(tmpVel)
mappedVel = math.floor(vel / 16) + 8
if mappedVel == 9:
mappedVel = 10
noteTonesR.append(NoteTone(notePitch, 58 * i, mappedVel, vel, 58))
if lastChunkR:
break
progress += 1
print(f"Chunk {progress} done")
except:
break
track2 += convToRawData(noteTonesR)
track2 += [0x00, 0xFF, 0x2F, 0x00]
track2_len = len(track2)
byte_arr += [(track2_len & 0xFF000000) >> 24, (track2_len & 0xFF0000) >> 16, (track2_len & 0xFF00) >> 8,
(track2_len & 0xFF)]
byte_arr += track2
f.write(bytes(byte_arr))
f.close()