Mercurial > repos > blastem
comparison vgmsplit.c @ 848:7068a9db6dd0
Wrote a buggy tool for splitting VGM files by channel
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 01 Nov 2015 12:55:08 -0800 |
parents | |
children | 215b5dabbc20 |
comparison
equal
deleted
inserted
replaced
847:7decd421cdc8 | 848:7068a9db6dd0 |
---|---|
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 | |
18 int main(int argc, char ** argv) | |
19 { | |
20 data_block *blocks = NULL; | |
21 data_block *seek_block = NULL; | |
22 uint32_t seek_offset; | |
23 uint32_t block_offset; | |
24 | |
25 | |
26 FILE * f = fopen(argv[1], "rb"); | |
27 vgm_header header; | |
28 size_t bytes = fread(&header, 1, sizeof(header), f); | |
29 if (bytes != sizeof(header)) { | |
30 fputs("Error reading file\n", stderr); | |
31 exit(1); | |
32 } | |
33 if (header.version < 0x150 || !header.data_offset) { | |
34 header.data_offset = 0xC; | |
35 } | |
36 fseek(f, header.data_offset + 0x34, SEEK_SET); | |
37 uint32_t data_size = header.eof_offset + 4 - (header.data_offset + 0x34); | |
38 uint8_t * data = malloc(data_size); | |
39 data_size = fread(data, 1, data_size, f); | |
40 fclose(f); | |
41 uint8_t *buffers[OUT_CHANNELS]; | |
42 uint8_t *out_pos[OUT_CHANNELS]; | |
43 uint8_t has_real_data[OUT_CHANNELS]; | |
44 | |
45 buffers[0] = malloc(data_size * OUT_CHANNELS); | |
46 out_pos[0] = buffers[0]; | |
47 has_real_data[0] = 0; | |
48 for (int i = 1; i < OUT_CHANNELS; i++) | |
49 { | |
50 buffers[i] = buffers[i-1] + data_size; | |
51 out_pos[i] = buffers[i]; | |
52 has_real_data[i] = 0; | |
53 } | |
54 | |
55 uint8_t * end = data + data_size; | |
56 uint8_t * cur = data; | |
57 uint32_t current_cycle = 0; | |
58 uint8_t psg_latch = 0; | |
59 uint8_t param,reg; | |
60 uint8_t channel; | |
61 uint32_t sample_count = 0; | |
62 uint8_t last_cmd; | |
63 while (cur < end) { | |
64 uint8_t cmd = *(cur++); | |
65 switch(cmd) | |
66 { | |
67 case CMD_PSG_STEREO: | |
68 //ignore for now | |
69 cur++; | |
70 break; | |
71 case CMD_PSG: | |
72 param = *(cur++); | |
73 if (param & 0x80) { | |
74 psg_latch = param; | |
75 channel = param >> 5 & 3; | |
76 } else { | |
77 channel = psg_latch >> 5 & 3; | |
78 } | |
79 *(out_pos[PSG_BASE+channel]++) = cmd; | |
80 *(out_pos[PSG_BASE+channel]++) = param; | |
81 has_real_data[PSG_BASE+channel] = 1; | |
82 break; | |
83 case CMD_YM2612_0: | |
84 reg = *(cur++); | |
85 param = *(cur++); | |
86 if (reg < REG_KEY_ONOFF) { | |
87 for (int i = 0; i < 6; i++) | |
88 { | |
89 *(out_pos[i]++) = cmd; | |
90 *(out_pos[i]++) = reg; | |
91 *(out_pos[i]++) = param; | |
92 } | |
93 break; | |
94 } else if(reg == REG_DAC || reg == REG_DAC_ENABLE) { | |
95 if (reg == REG_DAC) { | |
96 sample_count++; | |
97 } | |
98 channel = DAC_CHANNEL; | |
99 } else if(reg == REG_KEY_ONOFF) { | |
100 channel = param & 7; | |
101 if (channel > 2) { | |
102 channel--; | |
103 } | |
104 if (param & 0xF0) { | |
105 has_real_data[channel] = 1; | |
106 } | |
107 } else if (reg >= REG_FNUM_LOW_CH3 && reg < REG_ALG_FEEDBACK) { | |
108 channel = 2; | |
109 } else { | |
110 channel = 255; | |
111 } | |
112 case CMD_YM2612_1: | |
113 if (cmd == CMD_YM2612_1) { | |
114 reg = *(cur++); | |
115 param = *(cur++); | |
116 channel = 255; | |
117 } | |
118 if (channel >= PSG_BASE) { | |
119 if (reg >= REG_DETUNE_MULT && reg < REG_FNUM_LOW) { | |
120 channel = (cmd == CMD_YM2612_0 ? 0 : 3) + (reg & 0xC >> 2); | |
121 } else if ((reg >= REG_FNUM_LOW && reg < REG_FNUM_LOW_CH3) || (reg >= REG_ALG_FEEDBACK && reg < 0xC0)) { | |
122 channel = (cmd == CMD_YM2612_0 ? 0 : 3) + (reg & 0x3); | |
123 } else { | |
124 fprintf(stderr, "WARNING: Skipping nrecognized write to register %X on part %d\n", reg, (cmd == CMD_YM2612_0 ? 1 : 2)); | |
125 } | |
126 } | |
127 if (channel < PSG_BASE) { | |
128 *(out_pos[channel]++) = cmd; | |
129 *(out_pos[channel]++) = reg; | |
130 *(out_pos[channel]++) = param; | |
131 } | |
132 break; | |
133 case CMD_WAIT: { | |
134 reg = *(cur++); | |
135 param = *(cur++); | |
136 for (int i = 0; i < OUT_CHANNELS; i++) | |
137 { | |
138 *(out_pos[i]++) = cmd; | |
139 *(out_pos[i]++) = reg; | |
140 *(out_pos[i]++) = param; | |
141 } | |
142 break; | |
143 } | |
144 case CMD_WAIT_60: | |
145 case CMD_WAIT_50: | |
146 case CMD_END: | |
147 for (int i = 0; i < OUT_CHANNELS; i++) | |
148 { | |
149 *(out_pos[i]++) = cmd; | |
150 } | |
151 cur = end; | |
152 break; | |
153 case CMD_DATA: { | |
154 uint8_t * start = cur - 1; | |
155 cur++; //skip compat command | |
156 uint8_t data_type = *(cur++); | |
157 uint32_t data_size = *(cur++); | |
158 data_size |= *(cur++) << 8; | |
159 data_size |= *(cur++) << 16; | |
160 data_size |= *(cur++) << 24; | |
161 if (cur + data_size > end) { | |
162 data_size = end - cur; | |
163 } | |
164 cur += data_size; | |
165 if (data_type == DATA_YM2612_PCM) { | |
166 memcpy(out_pos[DAC_CHANNEL], start, cur-start); | |
167 out_pos[DAC_CHANNEL] += cur-start; | |
168 } else { | |
169 fprintf(stderr, "WARNING: Skipping data block with unrecognized type %X\n", data_type); | |
170 } | |
171 break; | |
172 } | |
173 case CMD_DATA_SEEK: { | |
174 memcpy(out_pos[DAC_CHANNEL], cur-1, 5); | |
175 out_pos[DAC_CHANNEL] += 5; | |
176 cur += 4; | |
177 break; | |
178 } | |
179 | |
180 default: | |
181 if (cmd >= CMD_WAIT_SHORT && cmd < (CMD_WAIT_SHORT + 0x10)) { | |
182 for (int i = 0; i < OUT_CHANNELS; i++) | |
183 { | |
184 *(out_pos[i]++) = cmd; | |
185 } | |
186 } else if (cmd >= CMD_YM2612_DAC && cmd < CMD_DAC_STREAM_SETUP) { | |
187 *(out_pos[DAC_CHANNEL]++) = cmd; | |
188 sample_count++; | |
189 } else { | |
190 fprintf(stderr, "unimplemented command: %X at offset %X, last valid command was %X\n", cmd, (unsigned int)(cur - data - 1), last_cmd); | |
191 exit(1); | |
192 } | |
193 } | |
194 last_cmd = cmd; | |
195 } | |
196 if (sample_count > SAMPLE_THRESHOLD) { | |
197 has_real_data[DAC_CHANNEL] = 1; | |
198 } | |
199 for (int i = 0; i < OUT_CHANNELS; i++) | |
200 { | |
201 if (has_real_data[i]) { | |
202 char fname[11]; | |
203 sprintf(fname, i < PSG_BASE ? "ym_%d.vgm" : "psg_%d.vgm", i < PSG_BASE ? i : i - PSG_BASE); | |
204 f = fopen(fname, "wb"); | |
205 if (!f) { | |
206 fprintf(stderr, "Failed to open %s for writing\n", fname); | |
207 exit(1); | |
208 } | |
209 data_size = out_pos[i] - buffers[i]; | |
210 header.eof_offset = (header.data_offset + 0x34) + data_size - 4; | |
211 fwrite(&header, 1, sizeof(header), f); | |
212 fseek(f, header.data_offset + 0x34, SEEK_SET); | |
213 fwrite(buffers[i], 1, data_size, f); | |
214 fclose(f); | |
215 } | |
216 } | |
217 return 0; | |
218 } |