All files / app-play/src/ui/options compressor.ts

86.66% Statements 26/30
100% Branches 4/4
62.5% Functions 5/8
88.46% Lines 23/26

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                                                                  3x 9x               4x           4x 16x   4x 4x     4x 10x 10x 10x 4x 4x 4x   10x             50x 10x 4x 20x 4x 4x   10x 10x      
// The Options dialog's compressor controls: every compressor's tuning, remembered across visits and handed to each play,
// and the per-stage controls, shown only where it applies. They run with the dialog as `this` (`OptionsDialog`).
import type { OptionContainer } from '../options-controls.ts';
import {
  DEFAULT_COMPRESSOR_PARAMS,
  type CompressorParams,
  type TunableCompressor,
} from '@be-music/player-browser/audio';
import {
  TUNABLE_COMPRESSORS,
  defaultCompressorTunings,
  paramsFromTuning,
  storeCompressorTunings,
} from '../../compressor-tuning.ts';
import type { OptionsDialog } from './index.ts';
/** Bus `CompressorParams` for every compressor, from the Options dialog's tuning. */
export function resolveCompressorParamOverrides(this: OptionsDialog): Record<TunableCompressor, CompressorParams> {
  return Object.fromEntries(
    TUNABLE_COMPRESSORS.map((compressor) => [
      compressor,
      paramsFromTuning(this.compressorTunings[compressor], DEFAULT_COMPRESSOR_PARAMS[compressor]),
    ]),
  ) as Record<TunableCompressor, CompressorParams>;
}
/**
 * Hide the per-stage `Key` / `BGM` / `Master` controls when they don't apply to the current state:
 *
 * - Compressor checkbox unchecked → bus is in `'off'` mode, every stage is bypassed already.
 * - `?compressor=legacy` → the legacy architecture has just one compressor; per-stage toggles don't map onto it.
 *
 * The native `hidden` property removes inapplicable stages from the pane.
 */
export function refreshCompressorStageVisibility(this: OptionsDialog): void {
  const visible = this.guiState.compressor && this.compressorMode === 'split';
  for (const controller of this.compressorStageControllers) controller.hidden = !visible;
}
/**
 * One folder per compressor (key / BGM / master in split mode; the single legacy compressor only when the
 * `?compressor=legacy` architecture is active) with threshold, knee, ratio, attack, and release sliders and a reset.
 * Changes retune a running chart live and are remembered for the next visit.
 */
export function buildCompressorTuningGUI(this: OptionsDialog, parent: Pick<OptionContainer, 'addFolder'>): void {
  const labels: Record<TunableCompressor, string> = {
    key: 'Key compressor',
    bgm: 'BGM compressor',
    master: 'Master compressor',
    legacy: 'Legacy compressor',
  };
  const compressors = TUNABLE_COMPRESSORS.filter((compressor) =>
    this.compressorMode === 'legacy' ? compressor === 'legacy' : compressor !== 'legacy',
  );
  const resets: (() => void)[] = [];
  this.layout?.setGroupReset('compressorTuning', () => {
    for (const reset of resets) reset();
  });
  for (const compressor of compressors) {
    const tuning = this.compressorTunings[compressor];
    const folder = parent.addFolder({ title: labels[compressor] });
    const apply = () => {
      const params = paramsFromTuning(tuning, DEFAULT_COMPRESSOR_PARAMS[compressor]);
      this.gameplayView?.setAudioCompressorParams(compressor, params);
      storeCompressorTunings(this.compressorTunings);
    };
    const controllers = [
      folder.addBinding(tuning, 'thresholdDb', { label: 'Threshold (dB)', min: -60, max: 0, step: 0.5 }),
      folder.addBinding(tuning, 'kneeDb', { label: 'Knee (dB)', min: 0, max: 40, step: 0.5 }),
      folder.addBinding(tuning, 'ratio', { label: 'Ratio (x:1)', min: 1, max: 20, step: 0.1 }),
      folder.addBinding(tuning, 'attackMs', { label: 'Attack (ms)', min: 0, max: 200, step: 0.5 }),
      folder.addBinding(tuning, 'releaseMs', { label: 'Release (ms)', min: 0, max: 1000, step: 5 }),
    ];
    for (const controller of controllers) controller.on('change', apply);
    const reset = () => {
      Object.assign(tuning, defaultCompressorTunings()[compressor]);
      for (const controller of controllers) controller.refresh();
      apply();
      this.scheduleSettingsSave();
    };
    resets.push(reset);
    folder.addButton({ title: 'Reset to default' }).on('click', reset);
  }
}