main
vgmsplit.c
1/*
2 Copyright 2015 Michael Pavone
3 This file is part of BlastEm.
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
5*/
6#include "ym2612.h"
7#include "vgm.h"
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#define OUT_CHANNELS 10
14#define DAC_CHANNEL 5
15#define PSG_BASE 6
16#define SAMPLE_THRESHOLD 100
17
18uint32_t total_delay;
19void accum_wait(uint32_t *delays, uint32_t samples)
20{
21 total_delay += samples;
22 for (int i = 0; i < OUT_CHANNELS; i++)
23 {
24 delays[i] += samples;
25 }
26}
27
28void write_wait(uint8_t **out_buffer, uint32_t *delay)
29{
30 while (*delay >= 65535)
31 {
32 *((*out_buffer)++) = CMD_WAIT;
33 *((*out_buffer)++) = 0xFF;
34 *((*out_buffer)++) = 0xFF;
35 *delay -= 65535;
36 }
37 if (*delay) {
38 if (*delay == 735) {
39 *((*out_buffer)++) = CMD_WAIT_60;
40 } else if (*delay > 735 && *delay <= 751) {
41 *((*out_buffer)++) = CMD_WAIT_60;
42 *((*out_buffer)++) = CMD_WAIT_SHORT + *delay - 736;
43 } else if (*delay == 882) {
44 *((*out_buffer)++) = CMD_WAIT_50;
45 } else if (*delay > 882 && *delay <= 898) {
46 *((*out_buffer)++) = CMD_WAIT_50;
47 *((*out_buffer)++) = CMD_WAIT_SHORT + *delay - 882;
48 } else if (*delay <= 16) {
49 *((*out_buffer)++) = CMD_WAIT_SHORT + *delay - 1;
50 } else {
51 *((*out_buffer)++) = CMD_WAIT;
52 *((*out_buffer)++) = *delay;
53 *((*out_buffer)++) = *delay >> 8;
54 }
55 *delay = 0;
56 }
57}
58
59int main(int argc, char ** argv)
60{
61 data_block *blocks = NULL;
62 data_block *seek_block = NULL;
63 uint32_t seek_offset;
64 uint32_t block_offset;
65
66
67 FILE * f = fopen(argv[1], "rb");
68 vgm_header header;
69 size_t bytes = fread(&header, 1, sizeof(header), f);
70 if (bytes != sizeof(header)) {
71 fputs("Error reading file\n", stderr);
72 exit(1);
73 }
74 if (header.version < 0x150 || !header.data_offset) {
75 header.data_offset = 0xC;
76 }
77 fseek(f, header.data_offset + 0x34, SEEK_SET);
78 uint32_t data_size = header.eof_offset + 4 - (header.data_offset + 0x34);
79 uint8_t * data = malloc(data_size);
80 data_size = fread(data, 1, data_size, f);
81 fclose(f);
82 uint8_t *buffers[OUT_CHANNELS];
83 uint8_t *out_pos[OUT_CHANNELS];
84 uint8_t has_real_data[OUT_CHANNELS];
85 uint32_t delay[OUT_CHANNELS];
86
87 buffers[0] = malloc(data_size * OUT_CHANNELS);
88 out_pos[0] = buffers[0];
89 has_real_data[0] = 0;
90 delay[0] = 0;
91 for (int i = 1; i < OUT_CHANNELS; i++)
92 {
93 buffers[i] = buffers[i-1] + data_size;
94 out_pos[i] = buffers[i];
95 has_real_data[i] = 0;
96 delay[i] = 0;
97 }
98
99 uint8_t * end = data + data_size;
100 uint8_t * cur = data;
101 uint32_t current_cycle = 0;
102 uint8_t psg_latch = 0;
103 uint8_t param,reg;
104 uint8_t channel;
105 uint32_t sample_count = 0;
106 uint8_t last_cmd;
107 while (cur < end) {
108 uint8_t cmd = *(cur++);
109 switch(cmd)
110 {
111 case CMD_PSG_STEREO:
112 //ignore for now
113 cur++;
114 break;
115 case CMD_PSG:
116 param = *(cur++);
117 if (param & 0x80) {
118 psg_latch = param;
119 channel = param >> 5 & 3;
120 } else {
121 channel = psg_latch >> 5 & 3;
122 }
123 write_wait(out_pos + PSG_BASE+channel, delay + PSG_BASE+channel);
124 *(out_pos[PSG_BASE+channel]++) = cmd;
125 *(out_pos[PSG_BASE+channel]++) = param;
126 has_real_data[PSG_BASE+channel] = 1;
127 break;
128 case CMD_YM2612_0:
129 reg = *(cur++);
130 param = *(cur++);
131 if (reg < REG_KEY_ONOFF) {
132 for (int i = 0; i < 6; i++)
133 {
134 write_wait(out_pos + i, delay + i);
135 *(out_pos[i]++) = cmd;
136 *(out_pos[i]++) = reg;
137 *(out_pos[i]++) = param;
138 }
139 break;
140 } else if(reg == REG_DAC || reg == REG_DAC_ENABLE) {
141 if (reg == REG_DAC) {
142 sample_count++;
143 }
144 channel = DAC_CHANNEL;
145 } else if(reg == REG_KEY_ONOFF) {
146 channel = param & 7;
147 if (channel > 2) {
148 channel--;
149 }
150 if (param & 0xF0) {
151 has_real_data[channel] = 1;
152 }
153 } else if (reg >= REG_FNUM_LOW_CH3 && reg < REG_ALG_FEEDBACK) {
154 channel = 2;
155 } else {
156 channel = 255;
157 }
158 case CMD_YM2612_1:
159 if (cmd == CMD_YM2612_1) {
160 reg = *(cur++);
161 param = *(cur++);
162 channel = 255;
163 }
164 if (channel >= PSG_BASE) {
165 if (reg >= REG_DETUNE_MULT && reg < REG_FNUM_LOW) {
166 channel = (cmd == CMD_YM2612_0 ? 0 : 3) + (reg & 0xC >> 2);
167 } else if ((reg >= REG_FNUM_LOW && reg < REG_FNUM_LOW_CH3) || (reg >= REG_ALG_FEEDBACK && reg < 0xC0)) {
168 channel = (cmd == CMD_YM2612_0 ? 0 : 3) + (reg & 0x3);
169 } else {
170 fprintf(stderr, "WARNING: Skipping nrecognized write to register %X on part %d\n", reg, (cmd == CMD_YM2612_0 ? 1 : 2));
171 }
172 }
173 if (channel < PSG_BASE) {
174 write_wait(out_pos + channel, delay + channel);
175 *(out_pos[channel]++) = cmd;
176 *(out_pos[channel]++) = reg;
177 *(out_pos[channel]++) = param;
178 }
179 break;
180 case CMD_WAIT: {
181 uint32_t wait_time = *(cur++);
182 wait_time |= *(cur++) << 8;
183 accum_wait(delay, wait_time);
184 break;
185 }
186 case CMD_WAIT_60:
187 accum_wait(delay, 735);
188 break;
189 case CMD_WAIT_50:
190 accum_wait(delay, 882);
191 break;
192 case CMD_END:
193 for (int i = 0; i < OUT_CHANNELS; i++)
194 {
195 write_wait(out_pos + i, delay + i);
196 *(out_pos[i]++) = cmd;
197 }
198 cur = end;
199 break;
200 case CMD_DATA: {
201 uint8_t * start = cur - 1;
202 cur++; //skip compat command
203 uint8_t data_type = *(cur++);
204 uint32_t data_size = *(cur++);
205 data_size |= *(cur++) << 8;
206 data_size |= *(cur++) << 16;
207 data_size |= *(cur++) << 24;
208 if (cur + data_size > end) {
209 data_size = end - cur;
210 }
211 cur += data_size;
212 if (data_type == DATA_YM2612_PCM) {
213 write_wait(out_pos + DAC_CHANNEL, delay + DAC_CHANNEL);
214 memcpy(out_pos[DAC_CHANNEL], start, cur-start);
215 out_pos[DAC_CHANNEL] += cur-start;
216 } else {
217 fprintf(stderr, "WARNING: Skipping data block with unrecognized type %X\n", data_type);
218 }
219 break;
220 }
221 case CMD_DATA_SEEK: {
222 write_wait(out_pos + DAC_CHANNEL, delay + DAC_CHANNEL);
223 memcpy(out_pos[DAC_CHANNEL], cur-1, 5);
224 out_pos[DAC_CHANNEL] += 5;
225 cur += 4;
226 break;
227 }
228
229 default:
230 if (cmd >= CMD_WAIT_SHORT && cmd < (CMD_WAIT_SHORT + 0x10)) {
231 accum_wait(delay, (cmd & 0xF) + 1);
232 } else if (cmd >= CMD_YM2612_DAC && cmd < CMD_DAC_STREAM_SETUP) {
233 write_wait(out_pos + DAC_CHANNEL, delay + DAC_CHANNEL);
234 *(out_pos[DAC_CHANNEL]++) = cmd;
235 for (int i = 0; i < OUT_CHANNELS; i++)
236 {
237 if (i != DAC_CHANNEL)
238 {
239 delay[i] += cmd & 0xF;
240 }
241 }
242 sample_count++;
243 } else {
244 fprintf(stderr, "unimplemented command: %X at offset %X, last valid command was %X\n", cmd, (unsigned int)(cur - data - 1), last_cmd);
245 exit(1);
246 }
247 }
248 last_cmd = cmd;
249 }
250 if (sample_count > SAMPLE_THRESHOLD) {
251 has_real_data[DAC_CHANNEL] = 1;
252 }
253 for (int i = 0; i < OUT_CHANNELS; i++)
254 {
255 if (has_real_data[i]) {
256 char fname[11];
257 sprintf(fname, i < PSG_BASE ? "ym_%d.vgm" : "psg_%d.vgm", i < PSG_BASE ? i : i - PSG_BASE);
258 f = fopen(fname, "wb");
259 if (!f) {
260 fprintf(stderr, "Failed to open %s for writing\n", fname);
261 exit(1);
262 }
263 data_size = out_pos[i] - buffers[i];
264 header.eof_offset = (header.data_offset + 0x34) + data_size - 4;
265 header.gd3_offset = 0;
266 fwrite(&header, 1, sizeof(header), f);
267 fseek(f, header.data_offset + 0x34, SEEK_SET);
268 fwrite(buffers[i], 1, data_size, f);
269 fclose(f);
270 }
271 }
272 printf("total_delay: %d\n", total_delay);
273 return 0;
274}