-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidi_encoding_converter.py
More file actions
392 lines (322 loc) · 12.4 KB
/
midi_encoding_converter.py
File metadata and controls
392 lines (322 loc) · 12.4 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MIDI Encoding Converter
A Python tool to convert text event encodings in MIDI files.
Supports conversion between various encodings (Shift_JIS, GBK, UTF-8, etc.)
Author: AI Assistant
License: MIT
Version: 1.0.0
"""
import struct
import argparse
import sys
from pathlib import Path
from typing import Tuple, List, Optional
__version__ = "1.0.0"
class MidiEncodingConverter:
"""Convert text event encodings in MIDI files."""
# Meta event types that contain text
TEXT_META_TYPES = {
0x01: "Text Event",
0x02: "Copyright Notice",
0x03: "Track Name",
0x04: "Instrument Name",
0x05: "Lyric",
0x06: "Marker",
0x07: "Cue Point",
}
def __init__(self, from_encoding: str = "shift_jis", to_encoding: str = "utf-8"):
"""
Initialize the converter.
Args:
from_encoding: Source encoding (default: shift_jis)
to_encoding: Target encoding (default: utf-8)
"""
self.from_encoding = from_encoding
self.to_encoding = to_encoding
self.converted_count = 0
self.error_count = 0
self.verbose = False
@staticmethod
def read_variable_length(data: bytes, pos: int) -> Tuple[int, int]:
"""
Read a variable-length quantity from MIDI data.
Args:
data: MIDI file data
pos: Current position
Returns:
Tuple of (value, new_position)
"""
value = 0
while True:
byte = data[pos]
value = (value << 7) | (byte & 0x7F)
pos += 1
if not (byte & 0x80):
break
return value, pos
@staticmethod
def write_variable_length(value: int) -> bytes:
"""
Write a variable-length quantity.
Args:
value: Integer value to encode
Returns:
Encoded bytes
"""
result = [value & 0x7F]
value >>= 7
while value:
result.append((value & 0x7F) | 0x80)
value >>= 7
return bytes(reversed(result))
def convert_text(self, data: bytes) -> bytes:
"""
Convert text data from source encoding to target encoding.
Args:
data: Original text bytes
Returns:
Converted text bytes
"""
try:
text = data.decode(self.from_encoding, errors='replace')
return text.encode(self.to_encoding)
except Exception as e:
self.error_count += 1
if self.verbose:
print(f"Warning: Conversion error - {e}", file=sys.stderr)
return data
def convert(self, input_file: str, output_file: Optional[str] = None) -> dict:
"""
Convert a MIDI file's text encodings.
Args:
input_file: Path to input MIDI file
output_file: Path to output MIDI file (default: input_converted.mid)
Returns:
Dictionary with conversion statistics
"""
input_path = Path(input_file)
if output_file is None:
output_file = input_path.stem + "_converted" + input_path.suffix
with open(input_file, 'rb') as f:
data = bytearray(f.read())
# Validate MIDI header
if data[:4] != b'MThd':
raise ValueError("Not a valid MIDI file (missing MThd header)")
self.converted_count = 0
self.error_count = 0
pos = 0
output = bytearray()
# Copy MIDI header
header_length = struct.unpack('>I', data[4:8])[0]
output.extend(data[:8 + header_length])
pos = 8 + header_length
# Process each track
track_count = 0
while pos < len(data):
if data[pos:pos+4] != b'MTrk':
break
track_count += 1
track_start = len(output)
output.extend(data[pos:pos+4]) # MTrk
pos += 4
track_length = struct.unpack('>I', data[pos:pos+4])[0]
pos += 4
output.extend(b'\x00\x00\x00\x00') # Placeholder for length
track_data_start = len(output)
track_end = pos + track_length
running_status = 0
while pos < track_end:
# Read delta time
delta, new_pos = self.read_variable_length(data, pos)
output.extend(self.write_variable_length(delta))
pos = new_pos
# Read event
status = data[pos]
if status == 0xFF: # Meta event
output.append(data[pos])
pos += 1
meta_type = data[pos]
output.append(data[pos])
pos += 1
length, new_pos = self.read_variable_length(data, pos)
pos = new_pos
meta_data = data[pos:pos + length]
pos += length
# Convert text meta events
if meta_type in self.TEXT_META_TYPES:
new_data = self.convert_text(bytes(meta_data))
self.converted_count += 1
if self.verbose and new_data != meta_data:
try:
old_text = meta_data.decode(self.from_encoding, errors='replace')
new_text = new_data.decode(self.to_encoding, errors='replace')
print(f" [{self.TEXT_META_TYPES[meta_type]}] {old_text[:40]}")
except:
pass
else:
new_data = meta_data
output.extend(self.write_variable_length(len(new_data)))
output.extend(new_data)
elif status == 0xF0 or status == 0xF7: # SysEx
output.append(data[pos])
pos += 1
length, new_pos = self.read_variable_length(data, pos)
output.extend(self.write_variable_length(length))
pos = new_pos
output.extend(data[pos:pos + length])
pos += length
running_status = 0
elif status & 0x80: # Status byte
running_status = status
output.append(data[pos])
pos += 1
if status & 0xF0 in (0x80, 0x90, 0xA0, 0xB0, 0xE0):
output.extend(data[pos:pos+2])
pos += 2
elif status & 0xF0 in (0xC0, 0xD0):
output.append(data[pos])
pos += 1
else: # Running status
if running_status:
output.append(data[pos])
pos += 1
if running_status & 0xF0 in (0x80, 0x90, 0xA0, 0xB0, 0xE0):
output.append(data[pos])
pos += 1
else:
# Unknown byte, copy it
output.append(data[pos])
pos += 1
# Update track length
track_data_length = len(output) - track_data_start
struct.pack_into('>I', output, track_start + 4, track_data_length)
# Write output file
with open(output_file, 'wb') as f:
f.write(output)
return {
'input_file': str(input_file),
'output_file': str(output_file),
'tracks': track_count,
'converted': self.converted_count,
'errors': self.error_count,
'input_size': len(data),
'output_size': len(output),
}
def detect_encoding(input_file: str) -> List[Tuple[str, float]]:
"""
Detect possible encodings in a MIDI file.
Args:
input_file: Path to MIDI file
Returns:
List of (encoding, confidence) tuples
"""
try:
import chardet
except ImportError:
print("Warning: chardet not installed. Install with: pip install chardet", file=sys.stderr)
return []
with open(input_file, 'rb') as f:
data = f.read()
if data[:4] != b'MThd':
raise ValueError("Not a valid MIDI file")
# Collect all text data
all_text_bytes = bytearray()
pos = 8 + struct.unpack('>I', data[4:8])[0]
while pos < len(data):
if data[pos:pos+4] != b'MTrk':
break
pos += 4
track_length = struct.unpack('>I', data[pos:pos+4])[0]
pos += 4
track_end = pos + track_length
while pos < track_end:
# Skip delta time
while data[pos] & 0x80:
pos += 1
pos += 1
status = data[pos]
if status == 0xFF:
pos += 1
meta_type = data[pos]
pos += 1
length = 0
while data[pos] & 0x80:
length = (length << 7) | (data[pos] & 0x7F)
pos += 1
length = (length << 7) | data[pos]
pos += 1
if meta_type in (0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07):
all_text_bytes.extend(data[pos:pos + length])
pos += length
elif status == 0xF0 or status == 0xF7:
pos += 1
length = 0
while data[pos] & 0x80:
length = (length << 7) | (data[pos] & 0x7F)
pos += 1
length = (length << 7) | data[pos]
pos += 1
pos += length
elif status & 0x80:
pos += 1
if status & 0xF0 in (0x80, 0x90, 0xA0, 0xB0, 0xE0):
pos += 2
elif status & 0xF0 in (0xC0, 0xD0):
pos += 1
else:
pos += 1
if all_text_bytes:
results = chardet.detect_all(bytes(all_text_bytes))
return [(r['encoding'], r['confidence']) for r in results if r['encoding']]
return []
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Convert text encodings in MIDI files",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s input.mid # Convert from Shift_JIS to UTF-8
%(prog)s input.mid -o output.mid # Specify output file
%(prog)s input.mid -f gbk -t utf-8 # Convert from GBK to UTF-8
%(prog)s input.mid --detect # Detect encoding only
"""
)
parser.add_argument('input', help='Input MIDI file')
parser.add_argument('-o', '--output', help='Output MIDI file')
parser.add_argument('-f', '--from-encoding', default='shift_jis',
help='Source encoding (default: shift_jis)')
parser.add_argument('-t', '--to-encoding', default='utf-8',
help='Target encoding (default: utf-8)')
parser.add_argument('-d', '--detect', action='store_true',
help='Detect encoding only (do not convert)')
parser.add_argument('-v', '--verbose', action='store_true',
help='Verbose output')
parser.add_argument('--version', action='version', version=f'%(prog)s {__version__}')
args = parser.parse_args()
if args.detect:
print(f"Detecting encoding in: {args.input}")
results = detect_encoding(args.input)
if results:
print("\nPossible encodings:")
for encoding, confidence in results:
print(f" {encoding}: {confidence:.1%}")
else:
print("No encoding detected or file contains only ASCII text.")
return
converter = MidiEncodingConverter(args.from_encoding, args.to_encoding)
converter.verbose = args.verbose
print(f"Converting: {args.input}")
print(f"Encoding: {args.from_encoding} -> {args.to_encoding}")
result = converter.convert(args.input, args.output)
print(f"\nConversion complete!")
print(f" Output: {result['output_file']}")
print(f" Tracks: {result['tracks']}")
print(f" Text events converted: {result['converted']}")
if result['errors'] > 0:
print(f" Errors: {result['errors']}")
print(f" Size: {result['input_size']} -> {result['output_size']} bytes")
if __name__ == '__main__':
main()