All files / src/components/music InstrumentWaveEditor.tsx

0% Statements 0/30
0% Branches 0/5
0% Functions 0/8
0% Lines 0/28

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                                                                                                                                                                                                                                                                               
import React from "react";
import l10n from "shared/lib/lang/l10n";
import trackerDocumentActions from "store/features/trackerDocument/trackerDocumentActions";
import { WaveInstrument } from "store/features/trackerDocument/trackerDocumentTypes";
import { FormDivider, FormField, FormRow } from "ui/form/layout/FormLayout";
import { Option, Select } from "ui/form/Select";
import { InstrumentLengthForm } from "./InstrumentLengthForm";
import { WaveEditorForm } from "./WaveEditorForm";
import { Button } from "ui/buttons/Button";
import { Alert, AlertItem } from "ui/alerts/Alert";
import API from "renderer/lib/api";
import { useAppDispatch } from "store/hooks";
import { SingleValue } from "react-select";
 
const volumeOptions = [
  {
    value: "0",
    label: "Mute",
  },
  {
    value: "1",
    label: "100%",
  },
  {
    value: "2",
    label: "50%",
  },
  {
    value: "3",
    label: "25%",
  },
];
 
interface InstrumentWaveEditorProps {
  id: string;
  instrument?: WaveInstrument;
  waveForms?: Uint8Array[];
}
 
export const InstrumentWaveEditor = ({
  instrument,
  waveForms,
}: InstrumentWaveEditorProps) => {
  const dispatch = useAppDispatch();
 
  Iif (!instrument) return <></>;
 
  const selectedVolume = volumeOptions.find(
    (i) => parseInt(i.value, 10) === instrument.volume
  );
 
  const onChangeField =
    <T extends keyof WaveInstrument>(key: T) =>
    (editValue: WaveInstrument[T]) => {
      dispatch(
        trackerDocumentActions.editWaveInstrument({
          instrumentId: instrument.index,
          changes: {
            [key]: editValue,
          },
        })
      );
    };
 
  const onChangeFieldSelect =
    <T extends keyof WaveInstrument>(key: T) =>
    (e: { value: number | string; label: string }) => {
      const editValue = e.value;
      dispatch(
        trackerDocumentActions.editWaveInstrument({
          instrumentId: instrument.index,
          changes: {
            [key]: editValue,
          },
        })
      );
    };
 
  const onTestInstrument = () => {
    API.music.sendToMusicWindow({
      action: "preview",
      note: 24, // C_5
      type: "wave",
      instrument: instrument,
      square2: false,
      waveForms: waveForms,
    });
  };
 
  return (
    <>
      <InstrumentLengthForm
        value={instrument.length}
        onChange={onChangeField("length")}
        min={1}
        max={256}
      />
 
      <FormDivider />
 
      <FormRow>
        <FormField name="volume" label={l10n("FIELD_VOLUME")}>
          <Select
            name="volume"
            value={selectedVolume}
            options={volumeOptions}
            onChange={(e: SingleValue<Option>) =>
              e && onChangeFieldSelect("volume")(e)
            }
          />
        </FormField>
      </FormRow>
 
      <WaveEditorForm
        waveId={instrument.wave_index}
        onChange={onChangeFieldSelect("wave_index")}
      />
 
      <FormDivider />
 
      <FormRow>
        <Button onClick={onTestInstrument}>
          {l10n("FIELD_TEST_INSTRUMENT")}
        </Button>
      </FormRow>
      {instrument.subpattern_enabled && (
        <FormRow>
          <Alert variant="info">
            <AlertItem>{l10n("MESSAGE_NOT_PREVIEW_SUBPATTERN")}</AlertItem>
          </Alert>
        </FormRow>
      )}
    </>
  );
};