Improve piano instruments, previews, and key-stream safeguards
This commit is contained in:
@@ -8,6 +8,7 @@ export const PIANO_INSTRUMENT_OPTIONS = [
|
||||
'bass',
|
||||
'violin',
|
||||
'synth_lead',
|
||||
'nintendo',
|
||||
'drum_kit',
|
||||
] as const;
|
||||
|
||||
@@ -103,6 +104,27 @@ const PRESETS: Record<Exclude<PianoInstrumentId, 'drum_kit'>, InstrumentPreset>
|
||||
gain: 0.2,
|
||||
releaseScale: 1,
|
||||
},
|
||||
nintendo: {
|
||||
oscillators: [
|
||||
{ type: 'square', gain: 1 },
|
||||
{ type: 'square', detune: 8, gain: 0.16 },
|
||||
],
|
||||
filter: { type: 'lowpass', frequency: 5200, q: 1.2 },
|
||||
gain: 0.22,
|
||||
releaseScale: 0.65,
|
||||
},
|
||||
};
|
||||
|
||||
export const DEFAULT_ENVELOPE_BY_INSTRUMENT: Record<PianoInstrumentId, { attack: number; decay: number }> = {
|
||||
piano: { attack: 15, decay: 45 },
|
||||
electric_piano: { attack: 12, decay: 40 },
|
||||
guitar: { attack: 8, decay: 35 },
|
||||
organ: { attack: 25, decay: 70 },
|
||||
bass: { attack: 10, decay: 35 },
|
||||
violin: { attack: 22, decay: 75 },
|
||||
synth_lead: { attack: 6, decay: 30 },
|
||||
nintendo: { attack: 2, decay: 28 },
|
||||
drum_kit: { attack: 1, decay: 22 },
|
||||
};
|
||||
|
||||
/** Maps 0..100 control values to note attack seconds. */
|
||||
|
||||
@@ -39,6 +39,7 @@ type EditorDeps = {
|
||||
applyTextInputEdit: (code: string, key: string, maxLength: number, ctrlKey?: boolean, allowReplaceOnNextType?: boolean) => void;
|
||||
setReplaceTextOnNextType: (value: boolean) => void;
|
||||
suppressItemPropertyEchoMs: (ms: number) => void;
|
||||
onPreviewPropertyChange?: (item: WorldItem, key: string, value: unknown) => void;
|
||||
updateStatus: (message: string) => void;
|
||||
sfxUiBlip: () => void;
|
||||
sfxUiCancel: () => void;
|
||||
@@ -105,6 +106,7 @@ export function createItemPropertyEditor(deps: EditorDeps): {
|
||||
const nextValue = options[nextIndex];
|
||||
deps.suppressItemPropertyEchoMs(600);
|
||||
deps.signalingSend({ type: 'item_update', itemId, params: { [selectedKey]: nextValue } });
|
||||
deps.onPreviewPropertyChange?.(item, selectedKey, nextValue);
|
||||
deps.updateStatus(nextValue);
|
||||
deps.sfxUiBlip();
|
||||
return;
|
||||
@@ -118,6 +120,7 @@ export function createItemPropertyEditor(deps: EditorDeps): {
|
||||
const nextValue = !current;
|
||||
deps.suppressItemPropertyEchoMs(600);
|
||||
deps.signalingSend({ type: 'item_update', itemId, params: { [selectedKey]: nextValue } });
|
||||
deps.onPreviewPropertyChange?.(item, selectedKey, nextValue);
|
||||
deps.updateStatus(nextValue ? 'on' : 'off');
|
||||
deps.sfxUiBlip();
|
||||
return;
|
||||
@@ -141,6 +144,7 @@ export function createItemPropertyEditor(deps: EditorDeps): {
|
||||
if (Number.isFinite(max)) nextValue = Math.min(max, nextValue);
|
||||
deps.suppressItemPropertyEchoMs(600);
|
||||
deps.signalingSend({ type: 'item_update', itemId, params: { [selectedKey]: nextValue } });
|
||||
deps.onPreviewPropertyChange?.(item, selectedKey, nextValue);
|
||||
deps.updateStatus(formatSteppedNumber(nextValue, step));
|
||||
if (Math.abs(nextValue - currentValue) < 1e-9 || Math.abs(nextValue - attempted) > 1e-9) {
|
||||
deps.sfxUiCancel();
|
||||
@@ -254,6 +258,7 @@ export function createItemPropertyEditor(deps: EditorDeps): {
|
||||
deps.state.nicknameInput = formatSteppedNumber(nextValue, step);
|
||||
deps.state.cursorPos = deps.state.nicknameInput.length;
|
||||
deps.setReplaceTextOnNextType(false);
|
||||
deps.onPreviewPropertyChange?.(item, propertyKey, nextValue);
|
||||
deps.updateStatus(deps.state.nicknameInput);
|
||||
if (Math.abs(nextValue - currentValue) < 1e-9 || Math.abs(nextValue - attempted) > 1e-9) {
|
||||
deps.sfxUiCancel();
|
||||
@@ -268,6 +273,9 @@ export function createItemPropertyEditor(deps: EditorDeps): {
|
||||
const value = deps.state.nicknameInput.trim();
|
||||
const sendItemParams = (params: Record<string, unknown>): void => {
|
||||
deps.signalingSend({ type: 'item_update', itemId, params });
|
||||
for (const [key, nextValue] of Object.entries(params)) {
|
||||
deps.onPreviewPropertyChange?.(item, key, nextValue);
|
||||
}
|
||||
};
|
||||
const parseToggleValue = (raw: string, field: string): { ok: true; value: boolean } | { ok: false } => {
|
||||
const normalized = raw.toLowerCase();
|
||||
@@ -397,6 +405,10 @@ export function createItemPropertyEditor(deps: EditorDeps): {
|
||||
if (control.type === 'select') {
|
||||
const selectedValue = deps.state.itemPropertyOptionValues[deps.state.itemPropertyOptionIndex];
|
||||
deps.signalingSend({ type: 'item_update', itemId, params: { [propertyKey]: selectedValue } });
|
||||
const item = deps.state.items.get(itemId);
|
||||
if (item) {
|
||||
deps.onPreviewPropertyChange?.(item, propertyKey, selectedValue);
|
||||
}
|
||||
deps.state.mode = 'itemProperties';
|
||||
deps.state.editingPropertyKey = null;
|
||||
deps.state.itemPropertyOptionValues = [];
|
||||
|
||||
@@ -54,6 +54,7 @@ const DEFAULT_PIANO_INSTRUMENT_OPTIONS = [
|
||||
'bass',
|
||||
'violin',
|
||||
'synth_lead',
|
||||
'nintendo',
|
||||
'drum_kit',
|
||||
] as const;
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
shouldProxyStreamUrl,
|
||||
} from './audio/radioStationRuntime';
|
||||
import { ItemEmitRuntime } from './audio/itemEmitRuntime';
|
||||
import { PianoSynth, type PianoInstrumentId } from './audio/pianoSynth';
|
||||
import { DEFAULT_ENVELOPE_BY_INSTRUMENT, PianoSynth, type PianoInstrumentId } from './audio/pianoSynth';
|
||||
import { normalizeDegrees } from './audio/spatial';
|
||||
import {
|
||||
applyPastedText,
|
||||
@@ -251,6 +251,7 @@ let activeTeleportLoopToken = 0;
|
||||
let activePianoItemId: string | null = null;
|
||||
const activePianoKeys = new Set<string>();
|
||||
const activeRemotePianoKeys = new Set<string>();
|
||||
let pianoPreviewTimeoutId: number | null = null;
|
||||
let activeTeleport:
|
||||
| {
|
||||
startX: number;
|
||||
@@ -797,6 +798,7 @@ function getPianoParams(item: WorldItem): { instrument: PianoInstrumentId; attac
|
||||
rawInstrument === 'bass' ||
|
||||
rawInstrument === 'violin' ||
|
||||
rawInstrument === 'synth_lead' ||
|
||||
rawInstrument === 'nintendo' ||
|
||||
rawInstrument === 'drum_kit'
|
||||
? rawInstrument
|
||||
: 'piano';
|
||||
@@ -820,6 +822,7 @@ function normalizePianoInstrument(value: unknown): PianoInstrumentId {
|
||||
if (raw === 'bass') return 'bass';
|
||||
if (raw === 'violin') return 'violin';
|
||||
if (raw === 'synth_lead') return 'synth_lead';
|
||||
if (raw === 'nintendo') return 'nintendo';
|
||||
if (raw === 'drum_kit') return 'drum_kit';
|
||||
return 'piano';
|
||||
}
|
||||
@@ -866,6 +869,39 @@ function stopPianoUseMode(announce = true): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** Plays one short C4 preview using the piano item's current/overridden envelope+instrument. */
|
||||
async function previewPianoSettingChange(item: WorldItem, overrides: Partial<{ instrument: PianoInstrumentId; attack: number; decay: number }>): Promise<void> {
|
||||
if (item.type !== 'piano') return;
|
||||
await audio.ensureContext();
|
||||
const ctx = audio.context;
|
||||
const destination = audio.getOutputDestinationNode();
|
||||
if (!ctx || !destination) return;
|
||||
const current = getPianoParams(item);
|
||||
const instrument = overrides.instrument ?? current.instrument;
|
||||
const attack = Math.max(0, Math.min(100, Math.round(overrides.attack ?? current.attack)));
|
||||
const decay = Math.max(0, Math.min(100, Math.round(overrides.decay ?? current.decay)));
|
||||
const sourceX = item.carrierId === state.player.id ? state.player.x : item.x;
|
||||
const sourceY = item.carrierId === state.player.id ? state.player.y : item.y;
|
||||
const previewKeyId = '__piano_preview_c4__';
|
||||
pianoSynth.noteOff(previewKeyId);
|
||||
pianoSynth.noteOn(
|
||||
previewKeyId,
|
||||
60,
|
||||
instrument,
|
||||
attack,
|
||||
decay,
|
||||
{ audioCtx: ctx, destination },
|
||||
{ x: sourceX - state.player.x, y: sourceY - state.player.y, range: current.emitRange },
|
||||
);
|
||||
if (pianoPreviewTimeoutId !== null) {
|
||||
window.clearTimeout(pianoPreviewTimeoutId);
|
||||
}
|
||||
pianoPreviewTimeoutId = window.setTimeout(() => {
|
||||
pianoSynth.noteOff(previewKeyId);
|
||||
pianoPreviewTimeoutId = null;
|
||||
}, 320);
|
||||
}
|
||||
|
||||
/** Plays one inbound piano note from another user using item spatial position. */
|
||||
function playRemotePianoNote(note: {
|
||||
itemId: string;
|
||||
@@ -1612,6 +1648,10 @@ function disconnect(): void {
|
||||
activeRemotePianoKeys.delete(key);
|
||||
pianoSynth.noteOff(key);
|
||||
}
|
||||
if (pianoPreviewTimeoutId !== null) {
|
||||
window.clearTimeout(pianoPreviewTimeoutId);
|
||||
pianoPreviewTimeoutId = null;
|
||||
}
|
||||
}
|
||||
|
||||
const onAppMessage = createOnMessageHandler({
|
||||
@@ -2484,6 +2524,26 @@ const itemPropertyEditor = createItemPropertyEditor({
|
||||
suppressItemPropertyEchoMs: (ms) => {
|
||||
suppressItemPropertyEchoUntilMs = Math.max(suppressItemPropertyEchoUntilMs, Date.now() + Math.max(0, ms));
|
||||
},
|
||||
onPreviewPropertyChange: (item, key, value) => {
|
||||
if (item.type !== 'piano') return;
|
||||
if (key === 'instrument') {
|
||||
const instrument = normalizePianoInstrument(value);
|
||||
const defaults = DEFAULT_ENVELOPE_BY_INSTRUMENT[instrument];
|
||||
void previewPianoSettingChange(item, { instrument, attack: defaults.attack, decay: defaults.decay });
|
||||
return;
|
||||
}
|
||||
if (key === 'attack') {
|
||||
const attack = Number(value);
|
||||
if (!Number.isFinite(attack)) return;
|
||||
void previewPianoSettingChange(item, { attack });
|
||||
return;
|
||||
}
|
||||
if (key === 'decay') {
|
||||
const decay = Number(value);
|
||||
if (!Number.isFinite(decay)) return;
|
||||
void previewPianoSettingChange(item, { decay });
|
||||
}
|
||||
},
|
||||
updateStatus,
|
||||
sfxUiBlip: () => audio.sfxUiBlip(),
|
||||
sfxUiCancel: () => audio.sfxUiCancel(),
|
||||
|
||||
Reference in New Issue
Block a user