All files / src/components/music InstrumentNoiseEditor.tsx

0% Statements 0/35
0% Branches 0/13
0% Functions 0/8
0% Lines 0/33

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 { castEventToBool } from "renderer/lib/helpers/castEventValue";
import l10n from "shared/lib/lang/l10n";
import trackerDocumentActions from "store/features/trackerDocument/trackerDocumentActions";
import { NoiseInstrument } from "store/features/trackerDocument/trackerDocumentTypes";
import { CheckboxField } from "ui/form/CheckboxField";
import { FormDivider, FormRow } from "ui/form/layout/FormLayout";
import { InstrumentLengthForm } from "./InstrumentLengthForm";
import { InstrumentVolumeEditor } from "./InstrumentVolumeEditor";
import { NoiseMacroEditorForm } from "./NoiseMacroEditorForm";
import { Button } from "ui/buttons/Button";
import { SubPatternCell } from "shared/lib/uge/song/SubPatternCell";
import { cloneDeep } from "lodash";
import { Alert, AlertItem } from "ui/alerts/Alert";
import API from "renderer/lib/api";
import { useAppDispatch } from "store/hooks";
 
interface InstrumentNoiseEditorProps {
  id: string;
  instrument?: NoiseInstrument;
}
 
export const InstrumentNoiseEditor = ({
  instrument,
}: InstrumentNoiseEditorProps) => {
  const dispatch = useAppDispatch();
 
  Iif (!instrument) return <></>;
 
  const onChangeField =
    <T extends keyof NoiseInstrument>(key: T) =>
    (editValue: NoiseInstrument[T]) => {
      dispatch(
        trackerDocumentActions.editNoiseInstrument({
          instrumentId: instrument.index,
          changes: {
            [key]: editValue,
          },
        })
      );
    };
 
  const onChangeSubpattern = (macros: number[]) => {
    const newSubPattern = cloneDeep(instrument.subpattern);
    macros.forEach((value, i) => {
      newSubPattern[i].note = value + 36;
    });
 
    dispatch(
      trackerDocumentActions.editSubPattern({
        instrumentId: instrument.index,
        instrumentType: "noise",
        subpattern: newSubPattern,
      })
    );
  };
 
  const onTestInstrument = () => {
    API.music.sendToMusicWindow({
      action: "preview",
      note: 24, // C_5
      type: "noise",
      instrument: instrument,
      square2: false,
    });
  };
 
  const noiseMacros = !instrument.subpattern_enabled
    ? []
    : instrument.subpattern
        // .slice(0, 6)
        .map((subpatternCell: SubPatternCell) =>
          subpatternCell && subpatternCell.note ? subpatternCell.note - 36 : 0
        );
 
  return (
    <>
      <InstrumentLengthForm
        value={instrument.length}
        onChange={onChangeField("length")}
      />
 
      <FormDivider />
 
      <InstrumentVolumeEditor
        initialVolume={instrument.initial_volume}
        volumeSweepChange={instrument.volume_sweep_change}
        length={instrument.length}
        onChange={onChangeField}
      />
 
      <FormDivider />
 
      <FormRow>
        <CheckboxField
          name="bit_count"
          label={l10n("FIELD_BIT_COUNT")}
          checked={instrument.bit_count === 7}
          onChange={(e) => {
            const v = castEventToBool(e);
            const value = v ? 7 : 15;
            onChangeField("bit_count")(value);
          }}
        />
      </FormRow>
 
      {/* Disable the noise macro preview for now. In the future it should edit the subpattern visually  */}
      {false ? ( // {instrument.noise_macro ? (
        <>
          <NoiseMacroEditorForm
            macros={noiseMacros}
            onChange={onChangeSubpattern}
          />
        </>
      ) : (
        ""
      )}
 
      <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>
      )}
    </>
  );
};