-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmidisoftpedal.js
100 lines (90 loc) · 2.61 KB
/
midisoftpedal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* MidiSoftPedal v1.0 - https://github.com/hhromic/midi-utils-js
* A very simple event-driven MIDI soft pedal emulator for JavaScript.
* MIT license
* Hugo Hromic - http://github.com/hhromic
*
* @requires EventEmitter
*/
/*jslint nomen: true*/
;(function () {
'use strict';
/**
* Represents a MIDI soft pedal emulator.
*
* @public
* @constructor
*/
function MidiSoftPedal() {
EventEmitter.call(this);
this._softenFactor = 2/3;
this._pressed = 0x0000;
}
// We are an event emitter
MidiSoftPedal.prototype = Object.create(EventEmitter.prototype);
// Shortcuts to improve speed and size
var proto = MidiSoftPedal.prototype;
/**
* Gets the current soften factor of the pedal.
*
* @public
* @returns {number} - the current soften factor [0,1].
*/
proto.getSoftenFactor = function () {
return this._softenFactor;
}
/**
* Sets the soften factor for the pedal.
*
* @public
* @param {number} softenFactor - the soften factor to set [0,1].
*/
// Set the soften factor [0,1] for the pedal
proto.setSoftenFactor = function (softenFactor) {
if (softenFactor >= 0 && softenFactor <= 1)
this._softenFactor = softenFactor;
}
/**
* Simulates pressing the soft pedal.
*
* @public
* @param {number} channel - the MIDI channel.
*/
proto.press = function (channel) {
this._pressed |= 1 << (channel & 0xF);
}
/**
* Simulates releasing (depressing) the soft pedal.
*
* @public
* @param {number} channel - the MIDI channel.
*/
proto.release = function (channel) {
this._pressed &= ~(1 << (channel & 0xF));
}
/**
* Processes and forwards a MIDI Note-On message.
*
* @public
* @param {number} channel - the MIDI channel.
* @param {number} note - the MIDI note.
* @param {number} velocity - the velocity of the MIDI note.
*/
proto.noteOn = function (channel, note, velocity) {
if ((this._pressed >> (channel & 0xF)) & 1)
this.emit('note-on', channel, note, Math.round(velocity * this._softenFactor));
this.emit('note-on', channel, note, velocity);
}
// Expose either via AMD, CommonJS or the global object
if (typeof define === 'function' && define.amd) {
define(function () {
return MidiSoftPedal;
});
}
else if (typeof module === 'object' && module.exports) {
module.exports = MidiSoftPedal;
}
else {
this.MidiSoftPedal = MidiSoftPedal;
}
}.call(this));