diff --git a/README.md b/README.md index 35f6039..360932a 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ piano.load().then(() => { Once the samples are loaded, it exposes 4 methods for playing the notes: -### `.keyDown({ note: string, time?: Time, velocity?: number })` +### `.keyDown({ note: string|number, time?: Time, velocity?: number })` Press a note down on the piano. Optionally give it a time using Tone.js time notation or seconds relative to the AudioContext clock. @@ -72,9 +72,12 @@ The velocity is a value between 0-1. ```javascript //play a 'C4' 1 second from now piano.keyDown({ note: 'C4', time: '+1' }) + +//play a 'C4' via MIDI code +piano.keyDown({ note: 60, time: '+1' }) ``` -### `.keyUp({ note: string, time?: Time })` +### `.keyUp({ note: string|number, time?: Time })` Release a note at the given time. diff --git a/src/piano/Piano.ts b/src/piano/Piano.ts index 9bdc2f1..04b9833 100644 --- a/src/piano/Piano.ts +++ b/src/piano/Piano.ts @@ -259,9 +259,7 @@ export class Piano extends ToneAudioNode { time = this.toSeconds(time) - if (isString(note)) { - midi = Math.round(Midi(note).toMidi()) - } + midi = isString(note) ? Math.round(Midi(note).toMidi()) : note if (!this._heldNotes.has(midi)) { // record the start time and velocity diff --git a/test/test.js b/test/test.js index c9f561a..6853aa2 100644 --- a/test/test.js +++ b/test/test.js @@ -113,7 +113,7 @@ describe('Piano', async () => { await piano.load() piano.keyDown('C4') piano.pedalDown('+0.2') - piano.keyUp('C4', '+0.5') + piano.keyUp(60, '+0.5') piano.keyDown('D4', '+0.4') piano.pedalUp('+3') }) @@ -149,7 +149,7 @@ describe('Piano', async () => { }) await piano.load() piano.keyDown('C4') - piano.keyDown('E4') + piano.keyDown(64) piano.keyDown('G4') piano.pedalDown() await new Promise(done => setTimeout(done, 100))