All files / src/store/features/soundfx soundfxMiddleware.ts

0% Statements 0/55
0% Branches 0/25
0% Functions 0/9
0% Lines 0/53

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                                                                                                                                                                                                         
import {
  playTone,
  stopTone,
  playBuffer,
  stopBuffer,
  decodeAudioData,
} from "renderer/lib/soundfx/soundfx";
import { Dispatch, Middleware } from "@reduxjs/toolkit";
import { RootState } from "store/configureStore";
import musicActions from "store/features/music/musicActions";
import navigationActions from "store/features/navigation/navigationActions";
import actions from "./soundfxActions";
import { soundSelectors } from "store/features/entities/entitiesState";
import { Sound } from "shared/lib/entities/entitiesTypes";
import { assetPath } from "shared/lib/helpers/assets";
import API from "renderer/lib/api";
 
let oscillator: OscillatorNode | undefined = undefined;
let bufferSource: AudioBufferSourceNode | undefined = undefined;
 
function initMusic() {
  window.removeEventListener("click", initMusic);
  window.removeEventListener("keydown", initMusic);
}
 
// Initialise audio on first click
window.addEventListener("click", initMusic);
window.addEventListener("keydown", initMusic);
window.addEventListener("blur", pause);
 
function play(filename: string) {
  const url = `gbs://app-assets/soundfx/${filename}`;
  fetch(url)
    .then((response) => response.arrayBuffer())
    .then(decodeAudioData)
    .then((data) => {
      bufferSource = playBuffer(data);
    });
}
 
async function playSound(sound: Sound, effectIndex: number) {
  const filename = assetPath("sounds", sound);
 
  if (sound.type === "wav") {
    API.soundfx.playWav(filename);
  } else if (sound.type === "vgm") {
    API.soundfx.playVGM(filename);
  } else Iif (sound.type === "fxhammer") {
    API.soundfx.playFXHammer(filename, effectIndex);
  }
}
 
function pause() {
  Iif (oscillator) {
    stopTone(oscillator);
    oscillator = undefined;
  }
  Iif (bufferSource) {
    stopBuffer(bufferSource);
    bufferSource = undefined;
  }
}
 
const soundfxMiddleware: Middleware<Dispatch, RootState> =
  (store) => (next) => (action) => {
    if (actions.playSoundFxBeep.match(action)) {
      pause();
      play(`effect_beep_${action.payload.pitch}.mp3`);
    } else if (actions.playSoundFxTone.match(action)) {
      pause();
      oscillator = playTone(
        action.payload.frequency,
        action.payload.duration * 1000
      );
    } else if (actions.playSoundFxCrash.match(action)) {
      play("effect_crash.mp3");
    } else if (actions.playSoundFx.match(action)) {
      const state = store.getState();
      const sound = soundSelectors.selectById(state, action.payload.effect);
      Iif (sound) {
        playSound(sound, action.payload.effectIndex);
      }
    } else if (actions.pauseSoundFx.match(action)) {
      pause();
    } else if (
      musicActions.playMusic.match(action) ||
      musicActions.pauseMusic.match(action)
    ) {
      pause();
    } else Iif (
      navigationActions.setSection.match(action) ||
      navigationActions.setNavigationId.match(action)
    ) {
      pause();
    }
 
    return next(action);
  };
 
export default soundfxMiddleware;