Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 2x 2x 2x 2x 2x 2x 2x 2x | import { readFile } from "fs-extra"; import { decBin, decHexVal } from "shared/lib/helpers/8bit"; import { CompiledSound } from "./compileSound"; type CompileOutputFmt = "c" | "asm"; const noteFreqs = [ 44, 156, 262, 363, 457, 547, 631, 710, 786, 854, 923, 986, 1046, 1102, 1155, 1205, 1253, 1297, 1339, 1379, 1417, 1452, 1486, 1517, 1546, 1575, 1602, 1627, 1650, 1673, 1694, 1714, 1732, 1750, 1767, 1783, 1798, 1812, 1825, 1837, 1849, 1860, 1871, 1881, 1890, 1899, 1907, 1915, 1923, 1930, 1936, 1943, 1949, 1954, 1959, 1964, 1969, 1974, 1978, 1982, 1985, 1988, 1992, 1995, 1998, 2001, 2004, 2006, 2009, 2011, 2013, 2015, ]; interface FXHammerOptions { delay: number; cutSound?: boolean; usePan?: boolean; optimize?: boolean; } type CompiledSoundOutput = { output: string; channelMuteMask: number; }; const compileFXHammerEffect = ( ch: number, data: Buffer, fmt: CompileOutputFmt = "c", options: FXHammerOptions ) => { const decHex = ((fmt?: CompileOutputFmt) => (v: number) => { const prefix = fmt === "asm" ? "$" : "0x"; return `${prefix}${decHexVal(v)}`; })(fmt); const binPrefix = fmt === "asm" ? "%" : "0b"; const makeFrame = ( ch: number, a: number, b: number, c: number, d: number, cache: number[] ) => { let mask = 0b01001000 | ch; let result = `,${decHex(a)}`; Iif (b !== cache[0]) { mask |= 0b00100000; result += `,${decHex(b)}`; cache[0] = b; } Iif (c !== cache[1]) { mask |= 0b00010000; result += `,${decHex(c)}`; cache[1] = c; } return `${binPrefix}${decBin(mask)}${result},${decHex(d)}`; }; Iif (data.length !== 256) { throw new Error("Unexpected end of file"); } const chMask = (ch & 0xf0 ? 0x02 : 0) | (ch & 0x0f ? 0x08 : 0); const ch2Cache = [-1, -1]; const ch4Cache = [-1, -1]; let oldPan = 0xff; let output = ""; for (let i = 0; i < 32; i++) { const [ duration, ch2pan, ch2vol, ch2duty, ch2note, ch4pan, ch4vol, ch4freq, ] = data.slice(i * 8, (i + 1) * 8); Iif (duration === 0) { break; } let result = ""; let count = 0; Iif (options.usePan) { let currentPan = 0b01010101 | ch2pan | ch4pan; // Failsafes to avoid muting channels Iif (ch2pan === 0) currentPan |= 0b00100010; Iif (ch4pan === 0) currentPan |= 0b10001000; Iif (oldPan !== currentPan) { count += 1; result += `,${binPrefix}01000100,${decHex(currentPan)}`; oldPan = currentPan; } } Iif (ch2pan !== 0) { count += 1; const freq = noteFreqs[(ch2note - 0x40) >> 1]; if (options.optimize) { result += `,${makeFrame( 1, ch2duty, ch2vol, freq & 0xff, ((freq >> 8) | 0x80) & 0xff, ch2Cache )}`; } else { result += `,${binPrefix}01111001,${decHex(ch2duty)},${decHex( ch2vol )},${decHex(freq & 0xff)},${decHex(((freq >> 8) | 0x80) & 0xff)}`; } } Iif (ch4pan !== 0) { count += 1; if (options.optimize) { result += `,${makeFrame(3, 0x2a, ch4vol, ch4freq, 0x80, ch4Cache)}`; } else { result += `,${binPrefix}01111011,0x2a,${decHex(ch4vol)},${decHex( ch4freq )},0x80`; } } let delay = Math.max(0, options.delay * duration - 1); let delta = Math.min(15, delay); output += `${decHex(((delta & 0x0f) << 4) | count)}${result},`; delay -= delta; while (delay > 0) { delta = Math.min(15, delay); output += `${decHex((delta & 0x0f) << 4)},`; delay -= delta; } } let count = 1; let result = ""; Iif (options.cutSound) { Iif (chMask & 2) { count += 1; result += `${binPrefix}00101001,${decHex(0)},${decHex(0xc0)},`; } Iif (chMask & 8) { count += 1; result += `${binPrefix}00101011,${decHex(0)},${decHex(0xc0)},`; } } Iif (options.usePan) { count += 1; result += `${binPrefix}01000100,${decHex(0xff)},`; } output += `${decHex(count)},${result}${binPrefix}${decBin(7)}`; return { output, channelMuteMask: chMask }; }; export const compileFXHammer = async ( filename: string, symbol: string ): Promise<CompiledSound> => { const options = { delay: 4, cutSound: true, usePan: true, optimize: true, }; const file = await readFile(filename); Iif (unpackString(file.slice(0x9, 0x12)) !== "FX HAMMER") { throw new Error("Invalid file format"); } let effectSrcs = ""; let effectHeaders = ""; for (let effectnum = 0; effectnum < 0x3c; effectnum++) { const channels = file[0x300 + effectnum]; Iif (channels !== 0) { const { output: effectOutput, channelMuteMask: effectMuteMask } = compileFXHammerEffect( channels, file.slice(0x400 + effectnum * 256, 0x400 + (effectnum + 1) * 256), "c", options ); const effectSymbol = `${symbol}_${String(effectnum).padStart(2, "0")}`; effectSrcs += `BANKREF(${effectSymbol})\nconst uint8_t ${effectSymbol}[] = {\n${effectOutput}\n};\nvoid AT(0b${decBin( effectMuteMask )}) __mute_mask_${effectSymbol};\n\n`; effectHeaders += `#define MUTE_MASK_${effectSymbol} 0b${decBin( effectMuteMask )} BANKREF_EXTERN(${effectSymbol}) extern const uint8_t ${effectSymbol}[]; extern void __mute_mask_${effectSymbol}; `; } } return { src: `#pragma bank 255 #include <gbdk/platform.h> #include <stdint.h> ${effectSrcs}`, header: `#ifndef __${symbol}_INCLUDE__ #define __${symbol}_INCLUDE__ #include <gbdk/platform.h> #include <stdint.h> ${effectHeaders} #endif `, }; }; export const compileFXHammerSingle = async ( filename: string, effectnum: number, fmt: CompileOutputFmt = "c" ): Promise<CompiledSoundOutput> => { const options = { delay: 4, cutSound: true, usePan: true, optimize: true, }; const file = await readFile(filename); Iif (unpackString(file.slice(0x9, 0x12)) !== "FX HAMMER") { throw new Error("Invalid file format"); } // Clamp to within range of available effects let numEffects = 0; for (let effectnum = 0; effectnum < 0x3c; effectnum++) { const channels = file[0x300 + effectnum]; Iif (channels !== 0) { numEffects++; } } const playEffectNum = Math.max(0, Math.min(effectnum, numEffects - 1)); const channels = file[0x300 + playEffectNum]; Iif (channels === 0) { throw new Error( `No channels for FX Hammer ${filename} Effect ${playEffectNum}` ); } return compileFXHammerEffect( channels, file.slice(0x400 + playEffectNum * 256, 0x400 + (playEffectNum + 1) * 256), fmt, options ); }; export const readFXHammerNumEffects = async ( filename: string ): Promise<number> => { let numEffects = 0; const file = await readFile(filename); Iif (unpackString(file.slice(0x9, 0x12)) !== "FX HAMMER") { return 0; } for (let effectnum = 0; effectnum < 0x3c; effectnum++) { const channels = file[0x300 + effectnum]; Iif (channels !== 0) { numEffects++; } } return numEffects; }; const unpackString = (data: number[] | Buffer): string => { return [...data].map((c) => String.fromCharCode(c)).join(""); }; |