Add piano release/brightness controls and instrument hotkeys

This commit is contained in:
Jage9
2026-02-23 00:05:01 -05:00
parent d9e9e60524
commit 019e49802d
15 changed files with 210 additions and 49 deletions

View File

@@ -9,7 +9,7 @@ from ..models import WorldItem
LABEL = "piano"
TOOLTIP = "Playable keyboard instrument with multiple synth voices."
EDITABLE_PROPERTIES: tuple[str, ...] = ("title", "instrument", "attack", "decay", "emitRange")
EDITABLE_PROPERTIES: tuple[str, ...] = ("title", "instrument", "attack", "decay", "release", "brightness", "emitRange")
CAPABILITIES: tuple[str, ...] = ("editable", "carryable", "deletable", "usable")
USE_SOUND: str | None = None
EMIT_SOUND: str | None = None
@@ -21,6 +21,8 @@ DEFAULT_PARAMS: dict = {
"instrument": "piano",
"attack": 15,
"decay": 45,
"release": 35,
"brightness": 55,
"emitRange": 15,
}
@@ -36,16 +38,16 @@ INSTRUMENT_OPTIONS: tuple[str, ...] = (
"drum_kit",
)
DEFAULT_ENVELOPE_BY_INSTRUMENT: dict[str, tuple[int, int]] = {
"piano": (15, 45),
"electric_piano": (12, 40),
"guitar": (8, 35),
"organ": (25, 70),
"bass": (10, 35),
"violin": (22, 75),
"synth_lead": (6, 30),
"nintendo": (2, 28),
"drum_kit": (1, 22),
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),
}
PROPERTY_METADATA: dict[str, dict[str, object]] = {
@@ -61,6 +63,16 @@ PROPERTY_METADATA: dict[str, dict[str, object]] = {
"tooltip": "How long notes ring out after the initial hit.",
"range": {"min": 0, "max": 100, "step": 1},
},
"release": {
"valueType": "number",
"tooltip": "How long notes continue after key release.",
"range": {"min": 0, "max": 100, "step": 1},
},
"brightness": {
"valueType": "number",
"tooltip": "Tone brightness; higher values sound brighter.",
"range": {"min": 0, "max": 100, "step": 1},
},
"emitRange": {
"valueType": "number",
"tooltip": "Maximum distance in squares where this piano can be heard.",
@@ -91,11 +103,27 @@ def validate_update(_item: WorldItem, next_params: dict) -> dict:
if not (0 <= decay <= 100):
raise ValueError("decay must be between 0 and 100.")
try:
release = int(next_params.get("release", 35))
except (TypeError, ValueError) as exc:
raise ValueError("release must be an integer between 0 and 100.") from exc
if not (0 <= release <= 100):
raise ValueError("release must be between 0 and 100.")
try:
brightness = int(next_params.get("brightness", 55))
except (TypeError, ValueError) as exc:
raise ValueError("brightness must be an integer between 0 and 100.") from exc
if not (0 <= brightness <= 100):
raise ValueError("brightness must be between 0 and 100.")
# When instrument changes, reset envelope to instrument-appropriate defaults.
if instrument != previous_instrument:
attack, decay = DEFAULT_ENVELOPE_BY_INSTRUMENT.get(instrument, (15, 45))
attack, decay, release, brightness = DEFAULT_ENVELOPE_BY_INSTRUMENT.get(instrument, (15, 45, 35, 55))
next_params["attack"] = attack
next_params["decay"] = decay
next_params["release"] = release
next_params["brightness"] = brightness
try:
emit_range = int(next_params.get("emitRange", 15))

View File

@@ -232,6 +232,8 @@ class ItemPianoNoteBroadcastPacket(BasePacket):
instrument: str
attack: int
decay: int
release: int
brightness: int
x: int
y: int
emitRange: int

View File

@@ -68,7 +68,7 @@ from .models import (
LOGGER = logging.getLogger("chgrid.server")
PACKET_LOGGER = logging.getLogger("chgrid.server.packet")
CLIENT_PACKET_ADAPTER = TypeAdapter(ClientPacket)
MAX_ACTIVE_PIANO_KEYS_PER_CLIENT = 32
MAX_ACTIVE_PIANO_KEYS_PER_CLIENT = 12
class SignalingServer:
@@ -679,6 +679,8 @@ class SignalingServer:
instrument = str(item.params.get("instrument", "piano")).strip().lower()
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
brightness = int(item.params.get("brightness", 55)) if isinstance(item.params.get("brightness", 55), (int, float)) else 55
emit_range = int(item.params.get("emitRange", 15)) if isinstance(item.params.get("emitRange", 15), (int, float)) else 15
source_x = client.x if item.carrierId == client.id else item.x
source_y = client.y if item.carrierId == client.id else item.y
@@ -693,6 +695,8 @@ class SignalingServer:
instrument=instrument,
attack=max(0, min(100, attack)),
decay=max(0, min(100, decay)),
release=max(0, min(100, release)),
brightness=max(0, min(100, brightness)),
x=source_x,
y=source_y,
emitRange=max(5, min(20, emit_range)),

View File

@@ -379,6 +379,8 @@ async def test_piano_update_and_use(monkeypatch: pytest.MonkeyPatch) -> None:
assert item.params.get("instrument") == "drum_kit"
assert item.params.get("attack") == 1
assert item.params.get("decay") == 22
assert item.params.get("release") == 12
assert item.params.get("brightness") == 68
assert item.params.get("emitRange") == 12
await server._handle_message(
@@ -389,6 +391,8 @@ async def test_piano_update_and_use(monkeypatch: pytest.MonkeyPatch) -> None:
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("brightness") == 85
await server._handle_message(client, json.dumps({"type": "item_use", "itemId": item.id}))
assert send_payloads[-1].ok is True
@@ -444,6 +448,8 @@ async def test_piano_note_packet_broadcasts(monkeypatch: pytest.MonkeyPatch) ->
assert getattr(packet, "instrument", "") == "organ"
assert getattr(packet, "attack", -1) == 20
assert getattr(packet, "decay", -1) == 60
assert getattr(packet, "release", -1) == 35
assert getattr(packet, "brightness", -1) == 55
assert getattr(packet, "emitRange", -1) == 12
@@ -467,16 +473,16 @@ async def test_piano_note_key_cap(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(server, "_send", fake_send)
monkeypatch.setattr(server, "_broadcast", fake_broadcast)
for index in range(32):
for index in range(12):
await server._handle_message(
sender,
json.dumps({"type": "item_piano_note", "itemId": item.id, "keyId": f"Key{index}", "midi": 60, "on": True}),
)
assert len(broadcast_payloads) == 32
assert len(broadcast_payloads) == 12
# 33rd distinct held key is dropped by cap.
# 13th distinct held key is dropped by cap.
await server._handle_message(
sender,
json.dumps({"type": "item_piano_note", "itemId": item.id, "keyId": "KeyOverflow", "midi": 60, "on": True}),
)
assert len(broadcast_payloads) == 32
assert len(broadcast_payloads) == 12