All files / src/lib/compiler/sounds compileSound.ts

42.11% Statements 8/19
0% Branches 0/5
0% Functions 0/2
38.89% Lines 7/18

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 731x   1x 1x 1x 1x                     1x                                                                   1x                                            
import { decBin } from "shared/lib/helpers/8bit";
import type { SoundData } from "shared/lib/entities/entitiesTypes";
import { compileFXHammer } from "./compileFXHammer";
import { compileVGM } from "./compileVGM";
import { compileWav } from "./compileWav";
import { assetFilename } from "shared/lib/helpers/assets";
 
export interface CompileSoundOptions {
  projectRoot: string;
}
 
export interface CompiledSound {
  src: string;
  header: string;
}
 
const compileSoundFiles = async (
  rawData: string,
  muteMask: string,
  symbol: string
): Promise<CompiledSound> => {
  return {
    src: `#pragma bank 255
  
  #include <gbdk/platform.h>
  #include <stdint.h>
  
  BANKREF(${symbol})
  const UINT8 ${symbol}[] = {
  ${rawData}
  };
  void AT(${muteMask}) __mute_mask_${symbol};
  `,
    header: `#ifndef __${symbol}_INCLUDE__
  #define __${symbol}_INCLUDE__
  
  #include <gbdk/platform.h>
  #include <stdint.h>
  
  #define MUTE_MASK_${symbol} ${muteMask}
  
  BANKREF_EXTERN(${symbol})
  extern const uint8_t ${symbol}[];
  extern void __mute_mask_${symbol};
  
  #endif
      `,
  };
};
 
export const compileSound = async (
  sound: SoundData,
  { projectRoot }: CompileSoundOptions
): Promise<CompiledSound> => {
  const assetPath = assetFilename(projectRoot, "sounds", sound);
 
  if (sound.type === "wav") {
    const rawData = await compileWav(assetPath);
    return compileSoundFiles(rawData, "0b00000100", sound.symbol);
  } else if (sound.type === "vgm") {
    const rawData = await compileVGM(assetPath);
    return compileSoundFiles(
      rawData.output,
      `0b${decBin(rawData.channelMuteMask)}`,
      sound.symbol
    );
  } else Iif (sound.type === "fxhammer") {
    return compileFXHammer(assetPath, sound.symbol);
  }
 
  throw new Error("Unknown sound file");
};