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

80.85% Statements 38/47
45% Branches 9/20
87.5% Functions 7/8
80.43% Lines 37/46

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 1641x 1x 1x 1x 1x 1x                 1x                 1x                                             1x 4x   4x 4x               1x 1x             1x       1x   3x 3x   3x 2x     1x       1x       1x     1x           1x 1x   1x 1x     1x     1x   1x       1x 1x 1x         1x 2x     1x                                                                                
import React, { useCallback, useEffect, useRef } from "react";
import l10n from "shared/lib/lang/l10n";
import styled from "styled-components";
import { CheckboxField } from "ui/form/CheckboxField";
import { Knob } from "ui/form/Knob";
import { Label } from "ui/form/Label";
 
interface InstrumentWaveEnvelopeEditorProps {
  volume: number;
  length: number | null;
  onChangeVolume: (value: number) => void;
  onChangeLength: (value: number | null) => void;
}
 
const StyledEnvelopeForm = styled.div`
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 5px;
  align-items: end;
  padding: 0 10px;
  padding-bottom: 10px;
`;
 
const StyledEnvelopeField = styled.div`
  display: grid;
  grid-template-rows: 1fr auto;
  gap: 6px;
 
  > *:first-child {
    align-self: end;
    align-items: center;
    text-align: center;
    justify-content: center;
    height: auto;
  }
 
  &:first-child > *:first-child {
    position: relative;
    top: -1px;
  }
 
  > *:last-child {
    width: 100%;
  }
`;
 
const mapInput = (input: number): number => {
  Iif (input === 0) {
    return 0;
  } else if (input === 1) {
    return 100;
  } else Eif (input === 2) {
    return 50;
  } else {
    return 25;
  }
};
 
const waveVolumeDisplays = [0, 25, 50, 100] as const;
const waveVolumeDisplayToStored = {
  0: 0,
  25: 3,
  50: 2,
  100: 1,
} as const;
 
const mapDisplayValueToStoredVolume = (
  value: number,
  currentDisplayValue: number,
): number => {
  const closestDisplayValue = waveVolumeDisplays.reduce(
    (closest, candidate) => {
      const closestDistance = Math.abs(value - closest);
      const candidateDistance = Math.abs(value - candidate);
 
      if (candidateDistance !== closestDistance) {
        return candidateDistance < closestDistance ? candidate : closest;
      }
 
      Iif (value > currentDisplayValue) {
        return candidate;
      }
 
      return closest;
    },
  );
 
  return waveVolumeDisplayToStored[closestDisplayValue];
};
 
export const InstrumentWaveEnvelopeEditor = ({
  volume,
  length,
  onChangeVolume,
  onChangeLength,
}: InstrumentWaveEnvelopeEditorProps) => {
  const lastRequestedDisplayValueRef = useRef(mapInput(volume));
  const lastResolvedStoredVolumeRef = useRef(volume);
 
  useEffect(() => {
    lastResolvedStoredVolumeRef.current = volume;
  }, [volume]);
 
  const fixedOnChangeVolume = useCallback(
    (value: number) => {
      const previousRequestedDisplayValue =
        lastRequestedDisplayValueRef.current;
      const nextStoredVolume =
        value === 75 && previousRequestedDisplayValue === 75
          ? lastResolvedStoredVolumeRef.current
          : mapDisplayValueToStoredVolume(value, previousRequestedDisplayValue);
 
      lastRequestedDisplayValueRef.current = value;
      lastResolvedStoredVolumeRef.current = nextStoredVolume;
      onChangeVolume(nextStoredVolume);
    },
    [onChangeVolume],
  );
 
  const formatValue = useCallback(() => {
    return `${mapInput(lastResolvedStoredVolumeRef.current)}%`;
  }, []);
 
  return (
    <StyledEnvelopeForm>
      <StyledEnvelopeField>
        <CheckboxField
          label={l10n("FIELD_LENGTH")}
          name="length"
          checked={length !== null}
          onChange={(e) => {
            const value = e.target.checked;
            if (!value) {
              onChangeLength(null);
            } else {
              onChangeLength(32);
            }
          }}
        />
        <Knob
          name="length"
          value={length ?? 0}
          min={1}
          max={256}
          onChange={onChangeLength}
        />
      </StyledEnvelopeField>
 
      <StyledEnvelopeField>
        <Label htmlFor="initialVolume">{l10n("FIELD_VOLUME")}</Label>
        <Knob
          name="initialVolume"
          value={mapInput(volume)}
          min={0}
          max={100}
          step={25}
          onChange={fixedOnChangeVolume}
          formatValue={formatValue}
        />
      </StyledEnvelopeField>
    </StyledEnvelopeForm>
  );
};