Tone Piano is a Web Audio instrument which uses high-quality multi-sampled piano sounds provided by Salamander Grand Piano
It has up to 16 velocity levels across 88 keys (sampled every third note) of a Yamaha C5.
Install the npm package:
npm install --save @tonejs/piano
Tone Piano requires Tone.js as a peer dependency (and webmidi to use MidiInput):
npm install --save tone webmidi
Using CommonJS:
const Piano = require('@tonejs/piano');
Using ES6 modules:
import { Piano } from '@tonejs/piano'
// create the piano and load 5 velocity steps
const piano = new Piano({
velocities: 5
})
//connect it to the speaker output
piano.toDestination()
All of the samples are loaded with the load()
method which returns a promise
piano.load().then(() => {
console.log('loaded!')
})
Once the samples are loaded, it exposes 4 methods for playing the notes:
Press a note down on the piano. Optionally give it a time using Tone.js time notation or seconds relative to the AudioContext clock.
The velocity is a value between 0-1.
//play a 'C4' 1 second from now
piano.keyDown({ note: 'C4', time: '+1' })
Release a note at the given time.
//release the pressed 'C4' immediately
piano.keyUp({ note: 'C4' })
Press and hold the pedal starting at the given time. While the pedal is down, notes scheduled after the time will be sustained and released once the pedal is lifted.
Release the pedal and also dampen any notes which are currently sustained.