All files / src/components/music/sidebar InstrumentDutyEditor.tsx

0% Statements 0/54
0% Branches 0/14
0% Functions 0/15
0% Lines 0/51

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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import React, { useEffect, useRef } from "react";
import trackerDocumentActions from "store/features/trackerDocument/trackerDocumentActions";
import { DutyInstrument } from "shared/lib/uge/types";
import { FormDivider, FormField, FormRow } from "ui/form/layout/FormLayout";
import { Select } from "ui/form/Select";
import { SliderField } from "ui/form/SliderField";
import { InstrumentLengthForm } from "./InstrumentLengthForm";
import { InstrumentVolumeEditor } from "./InstrumentVolumeEditor";
import { Button } from "ui/buttons/Button";
import { Alert, AlertItem } from "ui/alerts/Alert";
import API from "renderer/lib/api";
import l10n from "shared/lib/lang/l10n";
import { useAppDispatch } from "store/hooks";
import { SingleValue } from "react-select";
import { ButtonGroup } from "ui/buttons/ButtonGroup";
import { testNotes } from "./helpers";
import throttle from "lodash/throttle";
import { OCTAVE_SIZE } from "consts";
 
const dutyOptions = [
  {
    value: "0",
    label: "12.5%",
  },
  {
    value: "1",
    label: "25%",
  },
  {
    value: "2",
    label: "50%",
  },
  {
    value: "3",
    label: "75%",
  },
];
 
const sweepTimeOptions = [
  {
    value: "0",
    label: "Off",
  },
  {
    value: "1",
    label: "1/128Hz",
  },
  {
    value: "2",
    label: "2/128Hz",
  },
  {
    value: "3",
    label: "3/128Hz",
  },
  {
    value: "4",
    label: "4/128Hz",
  },
  {
    value: "5",
    label: "5/128Hz",
  },
  {
    value: "6",
    label: "6/128Hz",
  },
  {
    value: "7",
    label: "7/128Hz",
  },
];
 
interface InstrumentDutyEditorProps {
  id: string;
  instrument?: DutyInstrument;
}
 
export const InstrumentDutyEditor = ({
  instrument,
}: InstrumentDutyEditorProps) => {
  const dispatch = useAppDispatch();
 
  const throttledTestInstrument = useRef(
    throttle(
      (instrument: DutyInstrument) => {
        API.music.sendToMusicWindow({
          action: "preview",
          note: OCTAVE_SIZE * 2, // C5
          type: "duty",
          instrument,
          square2: false,
        });
      },
      250,
      { leading: true, trailing: true },
    ),
  ).current;
 
  useEffect(() => {
    return () => {
      throttledTestInstrument.cancel();
    };
  }, [throttledTestInstrument]);
 
  const lastAutoPreview = useRef("");
  const hasMounted = useRef(false);
 
  useEffect(() => {
    const instrumentKey = JSON.stringify(instrument);
 
    Iif (
      instrument &&
      hasMounted.current &&
      instrumentKey !== lastAutoPreview.current
    ) {
      throttledTestInstrument(instrument);
    }
 
    lastAutoPreview.current = instrumentKey;
    hasMounted.current = true;
  }, [instrument, throttledTestInstrument]);
 
  Iif (!instrument) return <></>;
 
  const selectedDuty = dutyOptions.find(
    (i) => parseInt(i.value, 10) === instrument.duty_cycle,
  );
 
  const selectedSweepTime = sweepTimeOptions.find(
    (i) => parseInt(i.value, 10) === instrument.frequency_sweep_time,
  );
 
  const onChangeField =
    <T extends keyof DutyInstrument>(key: T) =>
    (editValue: DutyInstrument[T]) => {
      dispatch(
        trackerDocumentActions.editDutyInstrument({
          instrumentId: instrument.index,
          changes: {
            [key]: editValue,
          },
        }),
      );
    };
 
  const onChangeFieldSelect =
    <T extends keyof DutyInstrument>(key: T) =>
    (e: SingleValue<{ value: string; label: string }>) => {
      Iif (e) {
        const editValue = e.value;
        dispatch(
          trackerDocumentActions.editDutyInstrument({
            instrumentId: instrument.index,
            changes: {
              [key]: editValue,
            },
          }),
        );
      }
    };
 
  const onTestInstrument = (note: number) => () => {
    API.music.sendToMusicWindow({
      action: "preview",
      note,
      type: "duty",
      instrument: instrument,
      square2: false,
    });
  };
 
  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>
        <FormField name="frequency_sweep_time" label={l10n("FIELD_SWEEP_TIME")}>
          <Select
            name="frequency_sweep_time"
            value={selectedSweepTime}
            options={sweepTimeOptions}
            onChange={onChangeFieldSelect("frequency_sweep_time")}
          />
        </FormField>
      </FormRow>
      {Number(instrument.frequency_sweep_time) !== 0 && (
        <FormRow>
          <SliderField
            name="frequency_sweep_shift"
            label={l10n("FIELD_SWEEP_SHIFT")}
            value={instrument.frequency_sweep_shift || 0}
            min={-7}
            max={7}
            onChange={(value) => {
              onChangeField("frequency_sweep_shift")(value || 0);
            }}
          />
        </FormRow>
      )}
      <FormDivider />
      <FormRow>
        <FormField name="duty_cycle" label={l10n("FIELD_DUTY")}>
          <Select
            name="duty_cycle"
            value={selectedDuty}
            options={dutyOptions}
            onChange={onChangeFieldSelect("duty_cycle")}
          />
        </FormField>
      </FormRow>
      <FormDivider />
      <FormRow>
        <FormField
          name="test_instrument_C5"
          label={l10n("FIELD_TEST_INSTRUMENT")}
        >
          <ButtonGroup>
            {testNotes.map(({ label, value }) => (
              <Button
                key={`test_instrument_${label}`}
                id={`test_instrument_${label}`}
                onClick={onTestInstrument(value)}
              >
                {label}
              </Button>
            ))}
          </ButtonGroup>
        </FormField>
      </FormRow>
      {instrument.subpattern_enabled && (
        <FormRow>
          <Alert variant="info">
            <AlertItem>{l10n("MESSAGE_NOT_PREVIEW_SUBPATTERN")}</AlertItem>
          </Alert>
        </FormRow>
      )}
    </>
  );
};