Add piano mono/poly, octave, and expanded drum voice set

This commit is contained in:
Jage9
2026-02-23 00:22:36 -05:00
parent 019e49802d
commit 29eb6a63e3
17 changed files with 338 additions and 72 deletions

View File

@@ -20,6 +20,7 @@ CLOCK_TIME_ZONE_OPTIONS = clock.TIME_ZONE_OPTIONS
RADIO_EFFECT_OPTIONS = radio.EFFECT_OPTIONS
RADIO_CHANNEL_OPTIONS = radio.CHANNEL_OPTIONS
PIANO_INSTRUMENT_OPTIONS = piano.INSTRUMENT_OPTIONS
PIANO_VOICE_MODE_OPTIONS = piano.VOICE_MODE_OPTIONS
@dataclass(frozen=True)
@@ -81,6 +82,7 @@ ITEM_PROPERTY_OPTIONS: dict[str, tuple[str, ...]] = {
"mediaChannel": RADIO_CHANNEL_OPTIONS,
"timeZone": CLOCK_TIME_ZONE_OPTIONS,
"instrument": PIANO_INSTRUMENT_OPTIONS,
"voiceMode": PIANO_VOICE_MODE_OPTIONS,
}
ITEM_TYPE_TOOLTIPS: dict[ItemType, str] = {

View File

@@ -9,7 +9,17 @@ from ..models import WorldItem
LABEL = "piano"
TOOLTIP = "Playable keyboard instrument with multiple synth voices."
EDITABLE_PROPERTIES: tuple[str, ...] = ("title", "instrument", "attack", "decay", "release", "brightness", "emitRange")
EDITABLE_PROPERTIES: tuple[str, ...] = (
"title",
"instrument",
"voiceMode",
"octave",
"attack",
"decay",
"release",
"brightness",
"emitRange",
)
CAPABILITIES: tuple[str, ...] = ("editable", "carryable", "deletable", "usable")
USE_SOUND: str | None = None
EMIT_SOUND: str | None = None
@@ -19,6 +29,8 @@ DIRECTIONAL = False
DEFAULT_TITLE = "piano"
DEFAULT_PARAMS: dict = {
"instrument": "piano",
"voiceMode": "poly",
"octave": 0,
"attack": 15,
"decay": 45,
"release": 35,
@@ -34,25 +46,34 @@ INSTRUMENT_OPTIONS: tuple[str, ...] = (
"bass",
"violin",
"synth_lead",
"brass",
"nintendo",
"drum_kit",
)
VOICE_MODE_OPTIONS: tuple[str, ...] = ("poly", "mono")
DEFAULT_ENVELOPE_BY_INSTRUMENT: dict[str, tuple[int, int, int, int]] = {
"piano": (15, 45, 35, 55),
"electric_piano": (12, 40, 30, 62),
"guitar": (8, 35, 25, 50),
"organ": (25, 70, 45, 48),
"bass": (10, 35, 28, 38),
"violin": (22, 75, 55, 58),
"synth_lead": (6, 30, 22, 72),
"nintendo": (2, 28, 18, 85),
"drum_kit": (1, 22, 12, 68),
DEFAULT_ENVELOPE_BY_INSTRUMENT: dict[str, tuple[int, int, int, int, str, int]] = {
"piano": (15, 45, 35, 55, "poly", 0),
"electric_piano": (12, 40, 30, 62, "poly", 0),
"guitar": (8, 35, 25, 50, "poly", 0),
"organ": (25, 70, 45, 48, "poly", 0),
"bass": (2, 24, 18, 34, "mono", -1),
"violin": (22, 75, 55, 58, "mono", 0),
"synth_lead": (6, 30, 22, 72, "poly", 0),
"brass": (10, 45, 30, 60, "mono", 0),
"nintendo": (1, 24, 15, 85, "poly", 0),
"drum_kit": (1, 22, 12, 68, "poly", 0),
}
PROPERTY_METADATA: dict[str, dict[str, object]] = {
"title": {"valueType": "text", "tooltip": "Display name spoken and shown for this item.", "maxLength": 80},
"instrument": {"valueType": "list", "tooltip": "Instrument voice used when playing this piano."},
"voiceMode": {"valueType": "list", "tooltip": "Mono plays one note at a time; poly allows chords."},
"octave": {
"valueType": "number",
"tooltip": "Shifts played notes in octaves. -1 is one octave down.",
"range": {"min": -2, "max": 2, "step": 1},
},
"attack": {
"valueType": "number",
"tooltip": "How quickly notes ramp in. Lower is sharper; higher is softer.",
@@ -90,6 +111,19 @@ def validate_update(_item: WorldItem, next_params: dict) -> dict:
previous_instrument = str(_item.params.get("instrument", "piano")).strip().lower()
next_params["instrument"] = instrument
voice_mode = str(next_params.get("voiceMode", _item.params.get("voiceMode", "poly"))).strip().lower()
if voice_mode not in VOICE_MODE_OPTIONS:
raise ValueError("voiceMode must be one of: poly, mono.")
next_params["voiceMode"] = voice_mode
try:
octave = int(next_params.get("octave", _item.params.get("octave", 0)))
except (TypeError, ValueError) as exc:
raise ValueError("octave must be an integer between -2 and 2.") from exc
if not (-2 <= octave <= 2):
raise ValueError("octave must be between -2 and 2.")
next_params["octave"] = octave
try:
attack = int(next_params.get("attack", 15))
except (TypeError, ValueError) as exc:
@@ -119,7 +153,11 @@ def validate_update(_item: WorldItem, next_params: dict) -> dict:
# When instrument changes, reset envelope to instrument-appropriate defaults.
if instrument != previous_instrument:
attack, decay, release, brightness = DEFAULT_ENVELOPE_BY_INSTRUMENT.get(instrument, (15, 45, 35, 55))
attack, decay, release, brightness, voice_mode, octave = DEFAULT_ENVELOPE_BY_INSTRUMENT.get(
instrument, (15, 45, 35, 55, "poly", 0)
)
next_params["voiceMode"] = voice_mode
next_params["octave"] = octave
next_params["attack"] = attack
next_params["decay"] = decay
next_params["release"] = release

View File

@@ -230,6 +230,8 @@ class ItemPianoNoteBroadcastPacket(BasePacket):
midi: int
on: bool
instrument: str
voiceMode: str
octave: int
attack: int
decay: int
release: int

View File

@@ -677,6 +677,10 @@ class SignalingServer:
else:
active_keys.discard(packet.keyId)
instrument = str(item.params.get("instrument", "piano")).strip().lower()
voice_mode = str(item.params.get("voiceMode", "poly")).strip().lower()
if voice_mode not in {"poly", "mono"}:
voice_mode = "poly"
octave = int(item.params.get("octave", 0)) if isinstance(item.params.get("octave", 0), (int, float)) else 0
attack = int(item.params.get("attack", 15)) if isinstance(item.params.get("attack", 15), (int, float)) else 15
decay = int(item.params.get("decay", 45)) if isinstance(item.params.get("decay", 45), (int, float)) else 45
release = int(item.params.get("release", 35)) if isinstance(item.params.get("release", 35), (int, float)) else 35
@@ -693,6 +697,8 @@ class SignalingServer:
midi=packet.midi,
on=packet.on,
instrument=instrument,
voiceMode=voice_mode,
octave=max(-2, min(2, octave)),
attack=max(0, min(100, attack)),
decay=max(0, min(100, decay)),
release=max(0, min(100, release)),

View File

@@ -377,6 +377,8 @@ async def test_piano_update_and_use(monkeypatch: pytest.MonkeyPatch) -> None:
)
assert send_payloads[-1].ok is True
assert item.params.get("instrument") == "drum_kit"
assert item.params.get("voiceMode") == "poly"
assert item.params.get("octave") == 0
assert item.params.get("attack") == 1
assert item.params.get("decay") == 22
assert item.params.get("release") == 12
@@ -389,9 +391,11 @@ async def test_piano_update_and_use(monkeypatch: pytest.MonkeyPatch) -> None:
)
assert send_payloads[-1].ok is True
assert item.params.get("instrument") == "nintendo"
assert item.params.get("attack") == 2
assert item.params.get("decay") == 28
assert item.params.get("release") == 18
assert item.params.get("voiceMode") == "poly"
assert item.params.get("octave") == 0
assert item.params.get("attack") == 1
assert item.params.get("decay") == 24
assert item.params.get("release") == 15
assert item.params.get("brightness") == 85
await server._handle_message(client, json.dumps({"type": "item_use", "itemId": item.id}))
@@ -406,6 +410,21 @@ async def test_piano_update_and_use(monkeypatch: pytest.MonkeyPatch) -> None:
assert send_payloads[-1].ok is False
assert "instrument must be one of" in send_payloads[-1].message.lower()
await server._handle_message(
client,
json.dumps({"type": "item_update", "itemId": item.id, "params": {"voiceMode": "mono", "octave": -2}}),
)
assert send_payloads[-1].ok is True
assert item.params.get("voiceMode") == "mono"
assert item.params.get("octave") == -2
await server._handle_message(
client,
json.dumps({"type": "item_update", "itemId": item.id, "params": {"octave": 3}}),
)
assert send_payloads[-1].ok is False
assert "octave must be between -2 and 2" in send_payloads[-1].message.lower()
@pytest.mark.asyncio
async def test_piano_note_packet_broadcasts(monkeypatch: pytest.MonkeyPatch) -> None:
@@ -446,6 +465,8 @@ async def test_piano_note_packet_broadcasts(monkeypatch: pytest.MonkeyPatch) ->
assert getattr(packet, "type", "") == "item_piano_note"
assert getattr(packet, "itemId", "") == item.id
assert getattr(packet, "instrument", "") == "organ"
assert getattr(packet, "voiceMode", "") == "poly"
assert getattr(packet, "octave", 999) == 0
assert getattr(packet, "attack", -1) == 20
assert getattr(packet, "decay", -1) == 60
assert getattr(packet, "release", -1) == 35