All files / src/components/music/hooks useMusicNotePreview.ts

0% Statements 0/42
0% Branches 0/36
0% Functions 0/3
0% Lines 0/41

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                                                                                                                                                                                                                                                   
import { useCallback } from "react";
import { useAppStore } from "store/hooks";
import {
  playDutyNotePreview,
  playNoiseNotePreview,
  playWaveNotePreview,
} from "components/music/helpers";
import { NOTE_C5 } from "consts";
import throttle from "lodash/throttle";
import { Song } from "shared/lib/uge/types";
 
type PreviewArgs = {
  note?: number;
  instrumentId?: number;
  channelId?: 0 | 1 | 2 | 3;
  effectCode?: number | null;
  effectParam?: number;
};
 
type ThrottledPreviewArgs = PreviewArgs & {
  song: Song | undefined;
  selectedChannel: 0 | 1 | 2 | 3;
  selectedInstrumentId: number | undefined;
};
 
const playMusicNotePreviewThrottled = throttle(
  ({
    song,
    selectedChannel,
    selectedInstrumentId,
    channelId,
    note,
    instrumentId,
    effectCode,
    effectParam,
  }: ThrottledPreviewArgs) => {
    Iif (!song) {
      return;
    }
 
    const previewChannel = channelId ?? selectedChannel;
    const previewNote = note ?? NOTE_C5;
    const previewInstrumentId = instrumentId ?? selectedInstrumentId ?? 0;
    const previewEffectCode = effectCode ?? 0;
    const previewEffectParams = effectParam ?? 0;
 
    Iif (previewChannel === 0 || previewChannel === 1) {
      const instrument = song.dutyInstruments[previewInstrumentId];
      Iif (!instrument) {
        return;
      }
 
      playDutyNotePreview(
        previewNote,
        instrument,
        previewChannel === 1 ? 1 : 0,
        previewEffectCode,
        previewEffectParams,
      );
      return;
    }
 
    Iif (previewChannel === 2) {
      const instrument = song.waveInstruments[previewInstrumentId];
      Iif (!instrument) {
        return;
      }
 
      const wave = song.waves[instrument.waveIndex];
      Iif (!wave) {
        return;
      }
 
      playWaveNotePreview(
        previewNote,
        instrument,
        wave,
        previewEffectCode,
        previewEffectParams,
      );
      return;
    }
 
    Iif (previewChannel === 3) {
      const instrument = song.noiseInstruments[previewInstrumentId];
      Iif (!instrument) {
        return;
      }
 
      playNoiseNotePreview(
        previewNote,
        instrument,
        previewEffectCode,
        previewEffectParams,
      );
    }
  },
  100,
  { leading: true, trailing: true },
);
 
export const useMusicNotePreview = () => {
  const store = useAppStore();
 
  return useCallback(
    (args: PreviewArgs) => {
      const state = store.getState();
      const song = state.trackerDocument.present.song;
      const selectedChannel = state.tracker.selectedChannel;
      const selectedInstrumentId = state.tracker.selectedInstrumentId;
 
      playMusicNotePreviewThrottled({
        ...args,
        song,
        selectedChannel,
        selectedInstrumentId,
      });
    },
    [store],
  );
};