MIDI.js
MIDI.js copied to clipboard
pitch bending in webaudio
Currently plugin.webaudio.js plays samples from the soundfont buffers always at their original pitch because the playbackRate is set to a constant value of 1. This means that pitch bend commands do not have any effect.
I added a bend parameter to the midi.noteOn() function and used its value to set the source.playbackRate.value. Thus one can shift the pitch of a note by some amount (typically less than a semitone). My implementation does not create a glissando effect - it simply changes the pitch of the note by some cents.
My intention was to use the new bend parameter to achieve just intonation, so all my pitch bend corrections are within a range of +/- 20 cents.
As you cannot expect a midi file to contain all the tiny bend changes which are necessary to produce just intonation (or other scale systems like "Werckmeister") I added a user definable callback function
to player.js in function startAudio() to the case statement for noteOn, noteOff. The callback function calculates bend changes based on just intonation (for a certain key) and returns its result to startAudio(). There the new bend parameter gets added to the schedule and will later be applied when the noteOn function of plugin.webaudio.js is called. In standard MIDI pitch bends apply to all notes of a track. My implementation does not have this restriction because it manipulates single noteOn events.
By the way: the same callback could also perform other manipulations like muting a track, applying transpositions, changing dynamics etc.
I suggest to add these two features (pitch bends and a user definable callback during the schedule creation phase) to MIDI.js.
Please check this pull request which detune notes by cents https://github.com/mudcube/MIDI.js/pull/207
Thanks! Algorithmixx