diff --git a/note-player.js b/note-player.js index 3bf168a..b465ffa 100644 --- a/note-player.js +++ b/note-player.js @@ -30,16 +30,16 @@ let playParsedMidiMessage; } } - function playNote({ note, channel }) { - console.log('playNote', { note, channel }); + function playNote({ note, channel, velocity }) { + console.log('playNote', { note, channel, velocity }); if (channel === 10) { - padMapping[note](); + padMapping[note]({ velocity }); } else { // white and black keys stopNote({ note, channel }); const octave = Math.floor(note / 12); const noteIdx = note % 12; - activeNotes[note] = synth.playNote({ note: NOTES[noteIdx], octave }); + activeNotes[note] = synth.playNote({ note: NOTES[noteIdx], octave, velocity }); } } @@ -51,7 +51,7 @@ let playParsedMidiMessage; } else if (keyUp) { stopNote({ note, channel }); } else { - playNote({ note, channel }); + playNote({ note, channel, velocity }); } }; diff --git a/synth.js b/synth.js index e5c9658..54c5cde 100644 --- a/synth.js +++ b/synth.js @@ -6,9 +6,6 @@ function initSynth(){ const NOTES = [ 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B' ]; const audioContext = new (window.AudioContext || window.webkitAudioContext); - let masterGainNode = audioContext.createGain(); - masterGainNode.connect(audioContext.destination); - masterGainNode.gain.value = 1.0; function makeCustomWaveform() { const sineTerms = new Float32Array([0, 0, 1, 0, 1]); @@ -30,8 +27,12 @@ function initSynth(){ currentPatch = (currentPatch + 1) % patches.length; } - function playTone(freq, patch = patches[currentPatch]) { + function playTone({ freq, velocity }, patch = patches[currentPatch]) { const osc = audioContext.createOscillator(); + const masterGainNode = audioContext.createGain(); + masterGainNode.connect(audioContext.destination); + // masterGainNode.gain.value = 1.0; + masterGainNode.gain.value = velocity / 127; osc.connect(masterGainNode); if (patch.apply) { patch.apply(osc); @@ -43,10 +44,10 @@ function initSynth(){ return osc; } - function playNote({ note, octave }, patch = patches[currentPatch]) { - console.log('synth.playNote', { note, octave }); + function playNote({ note, octave, velocity }, patch) { + console.log('synth.playNote', { note, octave, velocity }); const freq = 440 * Math.pow(Math.pow(2, 1 / 12), ((octave - 4) * 12) + NOTES.indexOf(note)); - return playTone(freq); + return playTone({ freq, velocity }, patch); } return {