-
Notifications
You must be signed in to change notification settings - Fork 8
/
chipstream.py
302 lines (264 loc) · 9.35 KB
/
chipstream.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
# -*- coding: utf-8 -*-
# license:BSD-3-Clause
import os
import json
from enum import Enum
from wasmer import engine, wasi, Store, Module, Instance
from wasmer_compiler_cranelift import Compiler
class SoundChipType(Enum):
YM2149 = 0
YM2151 = 1
YM2203 = 2
YM2413 = 3
YM2608 = 4
YM2610 = 5
YM2612 = 6
YM3526 = 7
Y8950 = 8
YM3812 = 9
YMF262 = 10
YMF278B = 11
SEGAPSG = 12
SN76489 = 13
PWM = 14
SEGAPCM = 15
OKIM6258 = 16
C140 = 17
class ChipStream:
def __init__(self):
"""
Constructor
"""
# Let's get the `wasi.wasm` bytes!
__dir__ = os.path.dirname(os.path.realpath(__file__))
wasm_bytes = open(__dir__ + '/libymfm.wasm', 'rb').read()
# Create a store.
store = Store(engine.JIT(Compiler))
# Let's compile the Wasm module, as usual.
module = Module(store, wasm_bytes)
# Get WASI version (wasi_snapshot_preview1)
wasi_version = wasi.get_version(module, strict=False)
# Set WASI enviroment
wasi_env = \
wasi.StateBuilder('libymfm'). \
map_directory('the_host_current_dir', '.'). \
finalize()
# Get import objects.
import_object = wasi_env.generate_import_object(store, wasi_version)
# Now we can instantiate the module.
instance = Instance(module, import_object)
# Set exports in Python instance
self.wasm = instance.exports
def create_vgm_instance(self, vgm_instance_id, file_path, output_sampling_rate, output_sample_chunk_size):
"""
Create VGM Instance in Wasm
Parameters
----------
vgm_instance_id: int
file_path: string
output_sampling_rate: int
output_sample_chunk_size: int
"""
# Read VGM file
vgm_bytes = open(file_path, 'rb').read()
vgm_length = len(vgm_bytes)
# Allocate memory in wasm
memory_id = 0
self.wasm.memory_alloc(memory_id, vgm_length)
# Trancefar vgm data
vgm_ref_pointer = self.wasm.memory_get_ref(memory_id)
vgm_ref = self.wasm.memory.uint8_view(offset = vgm_ref_pointer)
vgm_data = bytearray(vgm_bytes)
for i in range(vgm_length):
vgm_ref[i] = vgm_data[i]
# Create VgmPlay instance in Wasm
self.wasm.vgm_create(vgm_instance_id, output_sampling_rate, output_sample_chunk_size, memory_id)
# Drop allocate memory in wasm
self.wasm.memory_drop(memory_id)
# Set sampling chunk into instance
self.output_sample_chunk_size = output_sample_chunk_size * 4 # s16le * 2ch
# Get VGM header JSON
return json.loads(self.get_wasm_string(self.wasm.vgm_get_header_json(vgm_instance_id))), \
json.loads(self.get_wasm_string(self.wasm.vgm_get_gd3_json(vgm_instance_id)))
def create_xgm_instance(self, xgm_instance_id, file_path, output_sampling_rate, output_sample_chunk_size):
"""
Create XGM Instance in Wasm
Parameters
----------
xgm_instance_id: int
file_path: string
output_sampling_rate: int
output_sample_chunk_size: int
"""
# Read VGM file
xgm_bytes = open(file_path, 'rb').read()
xgm_length = len(xgm_bytes)
# Allocate memory in wasm
memory_id = 0
self.wasm.memory_alloc(memory_id, xgm_length)
# Trancefar vgm data
xgm_ref_pointer = self.wasm.memory_get_ref(memory_id)
xgm_ref = self.wasm.memory.uint8_view(offset = xgm_ref_pointer)
xgm_data = bytearray(xgm_bytes)
for i in range(xgm_length):
xgm_ref[i] = xgm_data[i]
# Create VgmPlay instance in Wasm
self.wasm.xgm_create(xgm_instance_id, output_sampling_rate, output_sample_chunk_size, memory_id)
# Drop allocate memory in wasm
self.wasm.memory_drop(memory_id)
# Set sampling chunk into instance
self.output_sample_chunk_size = output_sample_chunk_size * 4 # s16le * 2ch
# Get VGM header JSON
return json.loads(self.get_wasm_string(self.wasm.xgm_get_header_json(xgm_instance_id))), \
json.loads(self.get_wasm_string(self.wasm.xgm_get_gd3_json(xgm_instance_id)))
def get_wasm_string(self, memory_index_id):
# Create memory view
memory_view = self.wasm.memory.uint8_view(offset = self.wasm.memory_get_ref(memory_index_id))
memory_length = self.wasm.memory_get_len(memory_index_id)
# Copy into Python array
wasm_string = [0] * memory_length
for i in range(memory_length):
wasm_string[i] = memory_view[i]
# Memory drop
self.wasm.memory_drop(memory_index_id)
# UTF-8 string
return bytearray(wasm_string).decode()
def vgm_play(self, vgm_instance_id):
"""
Play VGM
Parameters
----------
vgm_instance_id: int
"""
return self.wasm.vgm_play(vgm_instance_id)
def vgm_get_sampling_ref(self, vgm_instance_id):
"""
Get sampling s16le
Parameters
----------
vgm_instance_id: int
Returns
----------
memoryview
"""
ref = self.wasm.vgm_get_sampling_s16le_ref(vgm_instance_id)
memory = bytearray(self.wasm.memory.buffer)
return memoryview(memory[ref:ref + self.output_sample_chunk_size])
def drop_vgm_instance(self, vgm_instance_id):
"""
Drop VGM instance
Parameters
----------
vgm_instance_id: int
"""
self.wasm.vgm_drop(vgm_instance_id)
def xgm_play(self, xgm_instance_id):
"""
Play XGM
Parameters
----------
xgm_instance_id: int
"""
return self.wasm.xgm_play(xgm_instance_id)
def xgm_get_sampling_ref(self, xgm_instance_id):
"""
Get sampling s16le
Parameters
----------
xgm_instance_id: int
Returns
----------
memoryview
"""
ref = self.wasm.xgm_get_sampling_s16le_ref(xgm_instance_id)
memory = bytearray(self.wasm.memory.buffer)
return memoryview(memory[ref:ref + self.output_sample_chunk_size])
def drop_xgm_instance(self, xgm_instance_id):
"""
Drop VGM instance
Parameters
----------
xgm_instance_id: int
"""
self.wasm.xgm_drop(xgm_instance_id)
def sound_slot_create(self, sound_slot_instance_id, external_tick_rate, output_sampling_rate, output_sample_chunk_size):
"""
Create sound slot instance in Wasm
Parameters
----------
sound_slot_instance_id: int
external_tick_rate: int
output_sampling_rate: int
output_sample_chunk_size: int
"""
self.wasm.sound_slot_create(sound_slot_instance_id, external_tick_rate, output_sampling_rate, output_sample_chunk_size)
self.sound_slot_output_sample_chunk_size = output_sample_chunk_size * 4 # s16le * 2ch
def sound_slot_add_sound_device(self, sound_slot_instance_id, sound_chip_type: SoundChipType, number_of, clock):
"""
Add sound chip to sound slot
Parameters
----------
sound_slot_instance_id: int
sound_chip_type: SoundChipType
number_of: int
clock: int
"""
self.wasm.sound_slot_add_sound_device(sound_slot_instance_id, sound_chip_type.value, number_of, clock)
def sound_slot_write(self, sound_slot_instance_id, sound_chip_type: SoundChipType, sound_chip_index, port, data):
"""
Write sound chip to command
Parameters
----------
sound_slot_instance_id: int
sound_chip_type: SoundChipType
sound_chip_index: int
port: int
data: int
"""
self.wasm.sound_slot_write(sound_slot_instance_id, sound_chip_type.value, sound_chip_index, port, data)
def sound_slot_update(self, sound_slot_instance_id, tick_count):
"""
Update sound slot
Parameters
----------
sound_slot_instance_id: int
tick_count: int
"""
self.wasm.sound_slot_update(sound_slot_instance_id, tick_count)
def sound_slot_is_stream_filled(self, sound_slot_instance_id):
"""
Query sampling chunk buffered
Parameters
----------
sound_slot_instance_id: int
"""
return self.wasm.sound_slot_is_stream_filled(sound_slot_instance_id)
def sound_slot_stream(self, sound_slot_instance_id):
"""
Set sampling to stream
Parameters
----------
sound_slot_instance_id: int
"""
self.wasm.sound_slot_stream(sound_slot_instance_id)
def sound_slot_get_sampling_ref(self, sound_slot_instance_id):
"""
Get sampling s16le
Parameters
----------
sound_slot_instance_id: int
Returns
----------
memoryview
"""
ref = self.wasm.sound_slot_sampling_s16le_ref(sound_slot_instance_id)
memory = bytearray(self.wasm.memory.buffer)
return memoryview(memory[ref:ref + self.sound_slot_output_sample_chunk_size])
def sound_slot_drop(self, sound_slot_instance_id):
"""
Drop sound slot instance
Parameters
----------
sound_slot_instance_id: int
"""
self.wasm.sound_slot_drop(sound_slot_instance_id)