All files / src/lib/project loadSoundData.ts

45.95% Statements 17/37
0% Branches 0/7
0% Functions 0/4
44.44% Lines 16/36

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 751x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x       1x                             1x                                                   1x                               1x 1x  
import glob from "glob";
import { promisify } from "util";
import uuidv4 from "uuid/v4";
import { stat } from "fs";
import parseAssetPath from "shared/lib/assets/parseAssetPath";
import { toValidSymbol } from "shared/lib/helpers/symbols";
import { readFXHammerNumEffects } from "lib/compiler/sounds/compileFXHammer";
import { getAssetResource } from "./assets";
import { SoundResource, SoundResourceAsset } from "shared/lib/resources/types";
 
const globAsync = promisify(glob);
const statAsync = promisify(stat);
 
type SoundAssetType = "wav" | "vgm" | "fxhammer";
 
export const toVGMType = (filename: string): SoundAssetType => {
  const lowerFilename = filename.toLowerCase();
  Iif (lowerFilename.endsWith(".wav")) {
    return "wav";
  }
  Iif (lowerFilename.endsWith(".vgm") || lowerFilename.endsWith(".vgz")) {
    return "vgm";
  }
  Iif (lowerFilename.endsWith(".sav")) {
    return "fxhammer";
  }
  throw new Error("Unknown sound type");
};
 
const loadSoundData =
  (projectRoot: string) =>
  async (filename: string): Promise<SoundResourceAsset> => {
    const { file, plugin } = parseAssetPath(filename, projectRoot, "sounds");
    const resource = await getAssetResource(SoundResource, filename);
    const fileStat = await statAsync(filename, { bigint: true });
    const inode = fileStat.ino.toString();
    const type = toVGMType(filename);
    const numEffects = await (type === "fxhammer"
      ? readFXHammerNumEffects(filename)
      : undefined);
 
    return {
      _resourceType: "sound",
      id: uuidv4(),
      plugin,
      name: file,
      symbol: toValidSymbol(`sound_${file}`),
      type,
      numEffects,
      _v: Date.now(),
      ...resource,
      filename: file,
      inode,
    };
  };
 
const loadAllSoundData = async (projectRoot: string) => {
  const soundPaths = await globAsync(
    `${projectRoot}/assets/sounds/**/@(*.vgm|*.VGM|*.vgz|*.VGZ|*.wav|*.WAV|*.sav|*.SAV)`
  );
  const pluginPaths = await globAsync(
    `${projectRoot}/plugins/*/**/sounds/**/@(*.vgm|*.VGM|*.vgz|*.VGZ|*.wav|*.WAV|*.sav|*.SAV)`
  );
  const soundsData = await Promise.all(
    ([] as Promise<SoundResourceAsset>[]).concat(
      soundPaths.map(loadSoundData(projectRoot)),
      pluginPaths.map(loadSoundData(projectRoot))
    )
  );
  return soundsData;
};
 
export default loadAllSoundData;
export { loadSoundData };