-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
223 lines (173 loc) · 7.02 KB
/
main.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
# GUI tool for extracting ADPCM samples from V ROMs by manually setting boundaries
# Furrtek 2025
# TODO: Auto helper that detects sudden amplitude changes
# TODO: Way to mark range as invalid/padding data
# See https://github.com/mamedev/mame/blob/master/3rdparty/ymfm/src/ymfm_adpcm.cpp
# UI waveform navigation and display, reset placement, loading/saving works fine
# ADPCM A/B decoding is pretty good but there's some drifting, step table incorrect or Python porting problem ?
# One ADPCM byte = two 4-bit ADPCM samples = two u8 PCM samples
# The YM2610 works on 256-byte groups of ADPCM data (512 samples)
# The waveCanvas widgets show PCM waveforms from a Vfile
# They're set up with a sampleCount (number of samples to show along width), and a sampleStart (left position)
import sys
import os
import wave
import time
import struct
import pyaudio
from vfile import Vfile
from wavecanvas import waveCanvas
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QSize, Qt, pyqtSignal
from PyQt5.QtWidgets import QWidget, QApplication, QMainWindow, QPushButton, QVBoxLayout, QFileDialog
DETAILWIDTH = 8192 # DETAILWIDTH // 2 PCM samples around cursor
SAMPLERATE = 18518 # ADPCM-A samplerate
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.vfile = None
self.setWindowTitle("YM2610 ADPCM extractor")
self.setFixedSize(QSize(1024, 600))
layout = QVBoxLayout()
self.waveform_ov = waveCanvas(0, True, False) # Overview
self.waveform_ov.clicked.connect(self.clickedOV)
layout.addWidget(self.waveform_ov)
self.waveform = waveCanvas(DETAILWIDTH, False, True) # Detail view
self.waveform.enableBlockView(True)
self.waveform.clicked.connect(self.clickedDetail)
self.waveform.scrolled.connect(self.scrolledDetail)
self.waveform.setFocusPolicy(QtCore.Qt.WheelFocus)
layout.addWidget(self.waveform)
button = QPushButton("Open")
button.clicked.connect(self.openFile)
layout.addWidget(button)
button2 = QPushButton("Save")
button2.clicked.connect(self.saveCSV)
layout.addWidget(button2)
button3 = QPushButton("Export")
button3.clicked.connect(self.export)
layout.addWidget(button3)
button4 = QPushButton("Play")
button4.clicked.connect(self.play)
layout.addWidget(button4)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
def closeEvent(self, event):
if self.vfile:
self.saveCSV() # Save on exit
event.accept()
def clickedOV(self, x):
#print("OV clicked at PCM sample %d" % x)
self.waveform_ov.setCursor(x)
self.waveform.setSampleStart(x - (DETAILWIDTH // 2))
def clickedDetail(self, x, button):
#print("Detail clicked at PCM sample %d" % x)
block = (x >> 1) >> 8 # All samples are aligned to 256-byte blocks, so resets can only occur on those boundaries
start, end = self.vfile.toggleReset(block, 1 if button else 0)
self.waveform.genWaveform() # Regen everything in detail view, already fast enough
self.waveform_ov.genWaveform(start << 9, end << 9) # Regen only updated region in overall view
def scrolledDetail(self, x):
self.waveform_ov.setCursor(x + (DETAILWIDTH // 2))
def openFile(self):
file_dialog = QFileDialog(self)
file_dialog.setWindowTitle("Open V ROM file")
file_dialog.setFileMode(QFileDialog.FileMode.ExistingFile)
file_dialog.setViewMode(QFileDialog.ViewMode.Detail)
file_dialog.setNameFilters(["V ROM files (*.*)"])
if not file_dialog.exec():
return
filePath = file_dialog.selectedFiles()[0]
fileSize = os.path.getsize(filePath)
if fileSize > 2**24:
print("Selected file exceeds 16MB")
return
fileName = os.path.basename(filePath)
self.fileStem = os.path.splitext(fileName)[0]
self.vfile = Vfile(filePath)
fname = self.fileStem + ".csv"
if os.path.isfile(fname):
print("Found a CSV file for %s" % fileName)
with open(fname, "r") as f_in: # Load CSV file
for line in f_in:
line = line.replace("\n",'')
if line != "":
v = line.split(',')
if len(v) == 1: # Only block number present, assume ADPCM-A
self.vfile.resets.append([int(v[0]), 0])
elif len(v) == 2: # Block number present and type
self.vfile.resets.append([int(v[0]), int(v[1])])
self.vfile.resets.append([self.vfile.raw_size_blocks, 0]) # End of file reset as marker
self.vfile.decode(0, self.vfile.raw_size_blocks) # Decode entire file
self.waveform_ov.setVfile(self.vfile)
self.waveform_ov.setSampleCount(self.vfile.pcm_size)
self.waveform.setVfile(self.vfile)
def saveCSV(self):
print("Saving")
with open(self.fileStem + ".csv", "w") as f_out: # Save reset points
for reset in self.vfile.resets[0:-1]: # Don't save the last reset (EOF marker)
f_out.write(str(reset[0]) + ',' + str(reset[1])+ "\n")
def export(self):
if not self.vfile:
return
# Single raw s16 PCM file
#self.vfile.decode(0, self.vfile.raw_size_blocks) # Re-decode entire file with resets to make sure pcm_data is up to date
with open(self.fileStem + ".raw", "wb") as f_out:
for sample in self.vfile.pcm_data:
f_out.write(sample.to_bytes(2, byteorder='little', signed=True))
# Multiple wave files
resetCount = len(self.vfile.resets)
for i, reset in enumerate(self.vfile.resets):
fname = self.fileStem + "_{:03d}_{:}.wav".format(i, "B" if reset[1] else "A")
with wave.open(fname, "w") as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(SAMPLERATE)
start = reset[0] << 9
if i < resetCount - 1:
stop = self.vfile.resets[i + 1][0] << 9 # Up to next reset
else:
stop = len(self.vfile.pcm_data) # Up to end
#print(start, stop)
f.writeframes(struct.pack("<%sh" % (stop - start), *self.vfile.pcm_data[start:stop]))
print("Export OK")
def play(self):
if not self.vfile:
return
# Play sample between resets where detail cursor is
self.vfile.resets.sort(key = lambda e: e[0]) # Make sure resets are sorted
# Find index of reset just before cursor
cursor = self.waveform_ov.cursor
#print("Cursor ", cursor)
for i, reset in enumerate(self.vfile.resets):
#print("Reset ", i, reset[0] << 9)
if (reset[0] << 9) > cursor:
break
self.playPos = self.vfile.resets[i - 1][0] << 9
duration = (self.vfile.resets[i][0] << 9) - self.playPos
if duration > SAMPLERATE * 5: # Cap max duration to 5s
duration = SAMPLERATE * 5
self.playEnd = self.playPos + duration
self.waveform_ov.setHighlight([self.playPos, duration])
stream = p.open(format=pyaudio.paInt16, channels=1, rate=SAMPLERATE, output=True, stream_callback=self.playCallback)
while stream.is_active():
time.sleep(0.1)
stream.close()
def playCallback(self, in_data, frame_count, time_info, status):
samples = self.vfile.pcm_data[self.playPos:self.playPos + frame_count]
buf = bytearray()
for sample in samples:
buf += sample.to_bytes(2, byteorder='little', signed=True)
self.playPos += frame_count
if self.playPos < self.playEnd:
flag = pyaudio.paContinue
else:
flag = pyaudio.paComplete
return (bytes(buf), flag)
app = QApplication(sys.argv)
p = pyaudio.PyAudio()
window = MainWindow()
window.show()
app.exec()
p.terminate()
exit()