Debian Codegraph @

BambooTracker/io/export_io.cpp

raw
1 /*
2 * Copyright (C) 2019-2023 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "export_io.hpp"
27 #include "binary_container.hpp"
28 #include "io_file_type.hpp"
29 #include "file_io_error.hpp"
30
31 namespace io
32 {
33 void writeVgm(BinaryContainer& container, int target, const std::vector<uint8_t>& samples,
34 uint32_t clock, uint32_t rate, bool loopFlag, uint32_t loopPoint,
35 uint32_t loopSamples, uint32_t totalSamples, const GD3Tag* tag, const VgmMix* mix)
36 {
37 uint32_t tagLen = 0;
38 uint32_t tagDataLen = 0;
39 if (tag) {
40 tagDataLen = tag->trackNameEn.length() + tag->trackNameJp.length()
41 + tag->gameNameEn.length() + tag->gameNameJp.length()
42 + tag->systemNameEn.length() + tag->systemNameJp.length()
43 + tag->authorEn.length() + tag->authorJp.length()
44 + tag->releaseDate.length() + tag->vgmCreator.length() + tag->notes.length();
45 tagLen = 12 + tagDataLen;
46 }
47 uint32_t extraLen = mix ? 17 : 0;
48
49 // Header
50 // 0x00: "Vgm " ident
51 uint8_t header[0x100] = {'V', 'g', 'm', ' '};
52 // 0x04: EOF offset
53 uint32_t offset = 0x100+ extraLen + samples.size() + 1 + tagLen - 4;
54 *reinterpret_cast<uint32_t*>(header + 0x04) = offset;
55 // 0x08: Version [v1.71]
56 uint32_t version = 0x171;
57 *reinterpret_cast<uint32_t*>(header + 0x08) = version;
58 // 0x14: GD3 offset
59 uint32_t gd3Offset = tag ? (0x100 + extraLen + samples.size() + 1 - 0x14) : 0;
60 *reinterpret_cast<uint32_t*>(header + 0x14) = gd3Offset;
61 // 0x18: Total # samples
62 *reinterpret_cast<uint32_t*>(header + 0x18) = totalSamples;
63 // 0x1c: Loop offset
64 uint32_t loopOffset = loopFlag ? (loopPoint + 0x100 + extraLen - 0x1c) : 0;
65 *reinterpret_cast<uint32_t*>(header + 0x1c) = loopOffset;
66 // 0x20: Loop # samples
67 uint32_t loopSamps = loopFlag ? loopSamples : 0;
68 *reinterpret_cast<uint32_t*>(header + 0x20) = loopSamps;
69 // 0x24: Rate
70 *reinterpret_cast<uint32_t*>(header + 0x24) = rate;
71 // 0x34: VGM data offset
72 *reinterpret_cast<uint32_t*>(header + 0x34) = 0x100 + extraLen - 0x34;
73
74 switch (target & Export_FmMask) {
75 default:
76 case Export_YM2608:
77 // 0x48: YM2608 clock
78 *reinterpret_cast<uint32_t*>(header + 0x48) = clock;
79 break;
80 case Export_YM2612:
81 // 0x2c: YM2612 clock
82 *reinterpret_cast<uint32_t*>(header + 0x2c) = clock;
83 break;
84 case Export_YM2203:
85 // 0x44: YM2203 clock
86 *reinterpret_cast<uint32_t*>(header + 0x44) = clock / 2;
87 break;
88 case Export_YM2610B:
89 // 0x4c: YM2610/B clock
90 *reinterpret_cast<uint32_t*>(header + 0x4c) = clock;
91 break;
92 case Export_NoneFm:
93 break;
94 }
95
96 switch (target & Export_SsgMask) {
97 case Export_InternalSsg:
98 break;
99 default:
100 // 0x74: AY8910 clock
101 *reinterpret_cast<uint32_t*>(header + 0x74) = clock / 4;
102 // 0x78: AY8910 chip type
103 if ((target & Export_SsgMask) == Export_YM2149Psg)
104 *reinterpret_cast<uint32_t*>(header + 0x78) = 0x10;
105 // 0x79: AY8910 flags
106 *reinterpret_cast<uint32_t*>(header + 0x79) = 0x01;
107 break;
108 }
109
110 // 0xbc: Extra header offset
111 *reinterpret_cast<uint32_t*>(header + 0xbc) = mix ? (0x100 - 0xbc) : 0;
112
113 container.appendArray(header, 0x100);
114
115 // Extra header
116 if (mix) {
117 // Extra header size
118 container.appendUint32(12);
119 // Chip clock offset
120 container.appendUint32(0);
121 // Chip volime offset
122 container.appendUint32(4);
123
124 // Entry count
125 container.appendUint8(1);
126 // Set SSG volume of YM2608
127 container.appendUint8(0x87);
128 // Flags
129 container.appendUint8(0);
130 // Volume
131 container.appendUint16(mix->ssgMultiplier);
132 }
133
134 // Commands
135 container.appendVector(samples);
136 container.appendUint8(0x66); // End
137
138 // GD3 tag
139 if (tag) {
140 // "Gd3 " ident
141 container.appendString("Gd3 ");
142 // Version [v1.00]
143 uint32_t gd3Version = 0x100;
144 container.appendUint32(gd3Version);
145 // Data size
146 container.appendUint32(tagDataLen);
147 // Track name in english
148 container.appendString(tag->trackNameEn);
149 // Track name in japanes
150 container.appendString(tag->trackNameJp);
151 // Game name in english
152 container.appendString(tag->gameNameEn);
153 // Game name in japanese
154 container.appendString(tag->gameNameJp);
155 // System name in english
156 container.appendString(tag->systemNameEn);
157 // System name in japanese
158 container.appendString(tag->systemNameJp);
159 // Track author in english
160 container.appendString(tag->authorEn);
161 // Track author in japanese
162 container.appendString(tag->authorJp);
163 // Release date
164 container.appendString(tag->releaseDate);
165 // VGM creator
166 container.appendString(tag->vgmCreator);
167 // Notes
168 container.appendString(tag->notes);
169 }
170 }
171
172 void writeS98(BinaryContainer& container, int target, const std::vector<uint8_t>& samples,
173 uint32_t clock, uint32_t rate, bool loopFlag, uint32_t loopPoint,
174 bool tagEnabled, const S98Tag& tag)
175 {
176 // Header
177 // 0x00: Magic "S98"
178 container.appendString("S98");
179 // 0x03: Format version 3
180 uint8_t version = 0x33;
181 container.appendUint8(version);
182 // 0x04: Timer info (sync numerator)
183 uint32_t timeNum = 1;
184 container.appendUint32(timeNum);
185 // 0x08: Timer info 2 (sync denominator)
186 container.appendUint32(rate);
187 // 0x0c: Deprecated
188 uint32_t zero = 0;
189 container.appendUint32(zero);
190 // 0x10: Tag offset
191 uint32_t tagOffset = tagEnabled ? (0x80 + samples.size() + 1) : 0;
192 container.appendUint32(tagOffset);
193 // 0x14: Dump data offset
194 uint32_t dumpOffset = 0x80;
195 container.appendUint32(dumpOffset);
196 // 0x18: Loop offset
197 uint32_t loopOffset = loopFlag ? (0x80 + loopPoint) : 0;
198 container.appendUint32(loopOffset);
199 // 0x1c: Device count
200 uint32_t deviceCnt = 0;
201 if ((target & Export_FmMask) != Export_NoneFm)
202 ++deviceCnt;
203 if ((target & Export_SsgMask) != Export_InternalSsg)
204 ++deviceCnt;
205 container.appendUint32(deviceCnt);
206
207 if ((target & Export_FmMask) != Export_NoneFm) {
208 // 0x20-0x2f: Device info (if NoneFM, skipped)
209 // 0x20: Device type
210 uint32_t deviceType;
211 uint32_t deviceClock;
212 switch (target & Export_FmMask) {
213 default:
214 case Export_YM2608:
215 deviceType = 4; // OPNA
216 deviceClock = clock;
217 break;
218 case Export_YM2612:
219 deviceType = 3; // OPN2
220 deviceClock = clock;
221 break;
222 case Export_YM2203:
223 deviceType = 2; // OPN
224 deviceClock = clock / 2;
225 break;
226 }
227 container.appendUint32(deviceType);
228 // 0x24: Clock
229 container.appendUint32(deviceClock);
230 // 0x28: Pan (Unused)
231 container.appendUint32(zero);
232 // 0x2c: Reserved
233 container.appendUint32(zero);
234 }
235
236 if ((target & Export_SsgMask) != Export_InternalSsg) {
237 // 0x30-0x3f: Device info
238 // 0x30: Device type
239 uint32_t subdeviceType;
240 uint32_t subdeviceClock;
241 switch (target & Export_SsgMask) {
242 default:
243 case Export_AY8910Psg:
244 subdeviceType = 15; // PSG (AY-3-8910)
245 subdeviceClock = clock / 4;
246 break;
247 case Export_YM2149Psg:
248 subdeviceType = 1; // PSG (YM 2149)
249 subdeviceClock = clock / 2;
250 break;
251 }
252 container.appendUint32(subdeviceType);
253 // 0x34: Clock
254 container.appendUint32(subdeviceClock);
255 // 0x38: Pan (Unused)
256 container.appendUint32(zero);
257 // 0x3c: Reserved
258 container.appendUint32(zero);
259 }
260
261 // 0x??-0x7f: Unused
262 for (uint32_t i = 0; i < (24 - 4 * deviceCnt); ++i)
263 container.appendUint32(zero);
264
265 // Commands
266 container.appendVector(samples);
267 container.appendUint8(0xfd); // End
268
269 // GD3 tag
270 if (tagEnabled) {
271 // Tag ident
272 container.appendString("[S98]");
273 // BOM
274 uint8_t bom[] = { 0xef, 0xbb, 0xbf };
275 container.appendArray(bom, 3);
276
277 uint8_t nl = 0x0a;
278
279 // Title
280 container.appendString("title=" + tag.title);
281 container.appendUint8(nl);
282 // Artist
283 container.appendString("artist=" + tag.artist);
284 container.appendUint8(nl);
285 // Game
286 container.appendString("game=" + tag.game);
287 container.appendUint8(nl);
288 // Year
289 container.appendString("year=" + tag.year);
290 container.appendUint8(nl);
291 // Genre
292 container.appendString("genre=" + tag.genre);
293 container.appendUint8(nl);
294 // Comment
295 container.appendString("comment=" + tag.comment);
296 container.appendUint8(nl);
297 // Copyright
298 container.appendString("copyright=" + tag.copyright);
299 container.appendUint8(nl);
300 // S98by
301 container.appendString("s98by=" + tag.s98by);
302 container.appendUint8(nl);
303 // System
304 container.appendString("system=" + tag.system);
305 container.appendUint8(nl);
306
307 uint8_t end = 0;
308 container.appendUint8(end);
309 }
310 }
311 }