-
Notifications
You must be signed in to change notification settings - Fork 3
/
ymdump.py
304 lines (235 loc) · 10.4 KB
/
ymdump.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
# ymdump.py
# .YM files data dump utility
# was originated based on code from https://github.com/FlorentFlament/ym2149-streamer
# Almost completely rewritten by https://github.com/simondotm/
#
# Copyright (c) 2019 Simon Morris. All rights reserved.
#
# "MIT License":
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import functools
import itertools
import struct
import sys
import time
import binascii
import math
import os
ENABLE_DEBUG = False # enable this to have ALL the info spitting out. This is more than ENABLE_VERBOSE
ENABLE_VERBOSE = False
# R00 = Channel A Pitch LO (8 bits)
# R01 = Channel A Pitch HI (4 bits)
# R02 = Channel B Pitch LO (8 bits)
# R03 = Channel B Pitch HI (4 bits)
# R04 = Channel C Pitch LO (8 bits)
# R05 = Channel C Pitch HI (4 bits)
# R06 = Noise Frequency (5 bits)
# R07 = I/O & Mixer (IOB|IOA|NoiseC|NoiseB|NoiseA|ToneC|ToneB|ToneA)
# R08 = Channel A Level (M | 4 bits) (where M is mode)
# R09 = Channel B Level (M | 4 bits)
# R10 = Channel C Level (M | 4 bits)
# R11 = Envelope Freq LO (8 bits)
# R12 = Envelope Freq HI (8 bits)
# R13 = Envelope Shape (CONT|ATT|ALT|HOLD)
class YmReader(object):
def __init__(self, fd):
print "Parsing YM file..."
self.__fd = fd
self.__filename = fd.name
self.__filesize = os.path.getsize(fd.name)
self.__parse_header()
self.__data = []
if not self.__data:
self.__read_data()
self.__check_eof()
def __parse_extra_infos(self):
if self.__header['id'] == 'YM2!' or self.__header['id'] == 'YM3!' or self.__header['id'] == 'YM3b':
self.__header['song_name'] = self.__filename
self.__header['author_name'] = ''
self.__header['song_comment'] = ''
else:
# YM6!
# Thanks http://stackoverflow.com/questions/32774910/clean-way-to-read-a-null-terminated-c-style-string-from-a-file
toeof = iter(functools.partial(self.__fd.read, 1), '')
def readcstr():
return ''.join(itertools.takewhile('\0'.__ne__, toeof))
self.__header['song_name'] = readcstr()
self.__header['author_name'] = readcstr()
self.__header['song_comment'] = readcstr()
def __parse_header(self):
# See:
# http://leonard.oxg.free.fr/ymformat.html
# ftp://ftp.modland.com/pub/documents/format_documentation/Atari%20ST%20Sound%20Chip%20Emulator%20YM1-6%20(.ay,%20.ym).txt
# Parse the YM file format identifier first
ym_format = self.__fd.read(4)
print "YM Format: " + ym_format
# we support YM2, YM3, YM5 and YM6
d = {}
if ym_format == 'YM2!' or ym_format == 'YM3!' or ym_format == 'YM3b':
print "Version 2"
d['id'] = ym_format
d['check_string'] = 'LeOnArD!'
d['nb_frames'] = (self.__filesize-4)/14
d['song_attributes'] = 1 # interleaved
d['nb_digidrums'] = 0
d['chip_clock'] = 2000000
d['frames_rate'] = 50
d['loop_frame'] = 0
d['extra_data'] = 0
d['nb_registers'] = 14
else:
if ym_format == 'YM6!' or ym_format == 'YM5!':
# Then parse the rest based on version
ym_header = '> 8s I I H I H I H'
s = self.__fd.read(struct.calcsize(ym_header))
(d['check_string'],
d['nb_frames'],
d['song_attributes'],
d['nb_digidrums'],
d['chip_clock'],
d['frames_rate'],
d['loop_frame'],
d['extra_data'],
) = struct.unpack(ym_header, s)
d['id'] = ym_format
d['nb_registers'] = 16
else:
if ym_format == '!C-l':
print('This is an LHA compressed YM file. Please extract the inner YM file first using 7zip or similar.')
sys.exit()
else:
raise Exception('Unknown or Unsupported file format: ' + ym_format)
# ok, carry on.
#b0: Set if Interleaved data block.
#b1: Set if the digi-drum samples are signed data.
#b2: Set if the digidrum is already in ST 4 bits format.
d['interleaved'] = d['song_attributes'] & 0x01 != 0
d['dd_signed'] = d['song_attributes'] & 0x02 != 0
d['dd_stformat'] = d['song_attributes'] & 0x04 != 0
self.__header = d
if d['interleaved']:
print "YM File is Interleaved format"
# read any DD samples
num_dd = self.__header['nb_digidrums']
if num_dd != 0:
print "Music contains " + str(num_dd) + " digi drum samples"
# info
if d['dd_stformat']:
print " Samples are 4-bit ST format" # TODO: what does this mean?!
else:
print " Samples are UNKNOWN FORMAT" # TODO:so what format is it exactly?!
if d['dd_signed']:
print " Samples are SIGNED"
else:
print " Samples are UNSIGNED"
for i in xrange(num_dd):
# skip over the digidrums sample file data section for now
#print self.__fd.tell()
sample_size = struct.unpack('>I', self.__fd.read(4))[0] # get sample size
print "Found DigiDrums sample " + str(i) + ", " + str(sample_size) + " bytes, loading data..."
#print sample_size
#print self.__fd.tell()
#print "sample " + str(i) + " size="+str(sample_size)
self.__fd.seek(sample_size, 1) # skip the sample data (for now)
#print self.__fd.tell()
#raise Exception('Unsupported file format: Digidrums are not supported')
self.__parse_extra_infos()
#print "file offset="
#print self.__fd.tell()
# self.dump_header()
def __read_data_interleaved(self):
#print "__read_data_interleaved"
#print "file offset=" + str(self.__fd.tell())
cnt = self.__header['nb_frames']
regs = []
for i in xrange( self.__header['nb_registers']):
regs.append(self.__fd.read(cnt))
# support output of just the intro (for tunes with looping sections)
loop_frame = self.__header['loop_frame']
if ENABLE_DEBUG:
print " Loaded " + str(len(regs)) + " register data chunks"
for r in xrange( self.__header['nb_registers']):
print " Register " + str(r) + " entries = " + str(len(regs[r]))
self.__data = regs
def __read_data(self):
if not self.__header['interleaved']:
raise Exception(
'Unsupported file format: Only interleaved data are supported')
#print "file offset=" + str(self.__fd.tell())
self.__read_data_interleaved()
#print "file offset=" + str(self.__fd.tell())
def __check_eof(self):
if self.__fd.read(4) != 'End!':
print '*Warning* End! marker not found after frames'
def dump_header(self):
for k in ('id','check_string', 'nb_frames', 'nb_registers', 'song_attributes',
'nb_digidrums', 'chip_clock', 'frames_rate', 'loop_frame',
'extra_data', 'song_name', 'author_name', 'song_comment'):
print "{}: {}".format(k, self.__header[k])
def get_header(self):
return self.__header
def get_data(self):
return self.__data
def write_raw(self, filename):
regs = self.__data
cnt = self.__header['nb_frames']
raw_data = bytearray()
for n in xrange(cnt):
for r in xrange(14):
raw_data.append( regs[r][n] )
# write to output file
raw_file = open(filename, 'wb')
raw_file.write(raw_data)
raw_file.close()
#------------------------------------------------------------------------
# Main()
#------------------------------------------------------------------------
import argparse
# Determine if running as a script
if __name__ == '__main__':
print("ymdump.py : Convert Atari ST .YM files to RAW interleaved data files")
print("Written in 2019 by Simon Morris, https://github.com/simondotm/ym2149f")
print("")
epilog_string = "Notes:\n"
epilog_string += " This tool does not support LHA compressed YM files, so\n"
epilog_string += " you must extract the archived YM file first.\n"
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=epilog_string)
parser.add_argument("input", help="YM source file (must be extracted from within the original YM file) [input]")
parser.add_argument("-o", "--output", metavar="<output>", help="write RAW file <output> (default is '[input].ymr')")
args = parser.parse_args()
src = args.input
dst = args.output
if dst == None:
dst = os.path.splitext(src)[0] + ".ymr"
print "output file=" + dst
# check for missing files
if not os.path.isfile(src):
print("ERROR: File '" + src + "' not found")
sys.exit()
with open(src, "rb") as fd:
ym = YmReader(fd)
fd.close()
ym.dump_header()
header = ym.get_header()
data = ym.get_data()
print "Loaded YM File."
print "Output file: '" + dst + "'"
ym.write_raw( dst )