Skip to content

Commit

Permalink
Add small nudges to music grid playback to hopefully make chords more…
Browse files Browse the repository at this point in the history
… prominent
  • Loading branch information
hlysine committed Jan 26, 2025
1 parent 1eb00ce commit c8ad778
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"dependencies": {
"@dnd-kit/sortable": "^8.0.0",
"@hlysine/bun-vercel": "^1.0.0-alpha.8",
"@hlysine/piano": "^0.2.1",
"@monaco-editor/react": "^4.6.0",
"@tanstack/react-router": "^1.22.1",
"@tanstack/router-devtools": "^1.22.2",
"@tonejs/piano": "^0.2.1",
"@use-gesture/react": "^10.3.1",
"@vercel/speed-insights": "^1.0.10",
"animejs": "^3.2.2",
Expand Down
9 changes: 9 additions & 0 deletions src/client/instructions/parts/MusicControlsPart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { array } from '@logic-pad/core/data/dataHelper';
import {
CachedPlayback,
cleanUp,
drum,
piano,
pianoImmediate,
pianoImmediatePedal,
Expand All @@ -28,6 +29,14 @@ const MusicControls = lazy(async function () {
await piano.load();
await pianoImmediate.load();
await pianoImmediatePedal.load();
await new Promise<void>(resolve => {
const interval = setInterval(() => {
if (Object.values(drum).every(player => player.loaded)) {
clearInterval(interval);
resolve();
}
}, 100);
});
return {
default: memo(function MusicControls({
instruction,
Expand Down
21 changes: 16 additions & 5 deletions src/client/instructions/parts/instruments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MusicGridRule from '@logic-pad/core/data/rules/musicGridRule';
import { Piano } from '@tonejs/piano';
import { Piano } from '@hlysine/piano';
import GridData from '@logic-pad/core/data/grid';
import * as Tone from 'tone';
import { Color } from '@logic-pad/core/data/primitives';
Expand Down Expand Up @@ -41,6 +41,12 @@ export const pianoImmediate = new Piano({
maxPolyphony: 32,
}).toDestination();

function nudge(note: string): number {
const n = Number(note.at(-1));
if (Number.isNaN(n)) return 0;
return (8 - n) / 6;
}

export const drum = {
snare: new Tone.Player('/samples/snare.wav').toDestination(),
kick: new Tone.Player('/samples/kick.wav').toDestination(),
Expand Down Expand Up @@ -103,7 +109,7 @@ export function encodePlayback(
tile.color === Color.Dark &&
!grid.connections.isConnected({ x1: x, y1: y, x2: x - 1, y2: y })
) {
addEvent(x / 2, {
addEvent(x / 2 + (nudge(row.note) * 0.02 * bpm) / 120, {
type: 'keydown',
value: row.note,
velocity: row.velocity,
Expand All @@ -119,7 +125,7 @@ export function encodePlayback(
) {
endPos = { x: endPos.x + 1, y: endPos.y };
}
addEvent((endPos.x + 1) / 2, {
addEvent((endPos.x + 1) / 2 + (nudge(row.note) * 0.02 * bpm) / 120, {
type: 'keyup',
value: row.note,
});
Expand Down Expand Up @@ -151,7 +157,8 @@ export function encodePlayback(
drum[event.value as keyof typeof drum].start(time, 0);
} else {
piano.keyDown({
note: event.value,
midi:
Tone.Midi(event.value).toMidi() + nudge(event.value) * 0.01,
velocity: event.velocity,
time,
});
Expand All @@ -161,7 +168,11 @@ export function encodePlayback(
if (event.value in drum) {
drum[event.value as keyof typeof drum].stop(time);
} else {
piano.keyUp({ note: event.value, time });
piano.keyUp({
midi:
Tone.Midi(event.value).toMidi() + nudge(event.value) * 0.01,
time,
});
}
break;
case 'complete':
Expand Down

0 comments on commit c8ad778

Please sign in to comment.