forked from notator/WebMIDISynthHost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cwMonosynth.js
179 lines (157 loc) · 5.96 KB
/
cwMonosynth.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* copyright 2015 Chris Wilson, James Ingram
* https://github.com/cwilso
* http://james-ingram-act-two.de/
*
* Code licensed under MIT
*
* WebMIDI.cwMonosynth contains a CWMonosynth constructor.
*
* This encapsulates Chris Wilson's monosynth synthesizer from
* https://github.com/cwilso/monosynth
* making it usable on the web without having any attached MIDI hardware.
*/
/*jslint bitwise: false, nomen: true, plusplus: true, white: true */
/*global WebMIDI: false, window: false, document: false, performance: false, console: false, alert: false, XMLHttpRequest: false */
WebMIDI.namespace('WebMIDI.cwMonosynth');
WebMIDI.cwMonosynth = (function(document)
{
"use strict";
var
CMD = WebMIDI.constants.COMMAND,
// cwMonosynth (private)
context=null, // the Web Audio "context" object
//midiAccess=null, // the MIDIAccess object.
oscillator=null, // the single oscillator
envelope=null, // the envelope for the single oscillator
attack=0.05, // attack speed
release=0.05, // release speed
portamento=0.05, // portamento/glide speed
activeNotes = [], // the stack of actively-pressed keys
frequencyFromNoteNumber = function(note)
{
return 440 * Math.pow(2, (note - 69) / 12);
},
// for host
commands =
[
CMD.NOTE_OFF,
CMD.NOTE_ON
],
controls =
[
],
CWMonosynth = function()
{
if(!(this instanceof CWMonosynth))
{
return new CWMonosynth();
}
/** WebMIDIAPI §10 -- MIDIPort interface **/
Object.defineProperty(this, "id", { value: "monosynth1", writable: false });
Object.defineProperty(this, "manufacturer", { value: "chris wilson", writable: false });
Object.defineProperty(this, "name", { value: "monosynth (Chris Wilson)", writable: false });
Object.defineProperty(this, "type", { value: "output", writable: false });
Object.defineProperty(this, "version", { value: "1", writable: false });
Object.defineProperty(this, "ondisconnect", { value: null, writable: false }); // Do we need this at all? Is it correct to set it to null?
/*** Is this necessary? See https://github.com/WebAudio/web-midi-api/issues/110 ***/
Object.defineProperty(this, "removable", { value: true, writable: false });
/*** Extensions for software synths ***/
Object.defineProperty(this, "url", { value: "http://webaudiodemos.appspot.com/monosynth/", writable: false }); // The synth author's webpage hosting the synth.
Object.defineProperty(this, "commands", { value: commands, writable: false }); // The commands supported by this synth (see above).
Object.defineProperty(this, "controls", { value: controls, writable: false }); // The controls supported by this synth (see above).
Object.defineProperty(this, "isMultiChannel", { value: false, writable: false }); // If isMultiChannel is false, the synth ignores the channel nibble in MIDI messages
Object.defineProperty(this, "isPolyphonic", { value: false, writable: false }); // If isPolyphonic is false, the synth can only play one note at a time
},
API =
{
CWMonosynth: CWMonosynth // constructor
};
// end var
// The init function should be called immediately after the synth has been constructed.
CWMonosynth.prototype.init = function()
{
window.AudioContext = window.AudioContext || window.webkitAudioContext;
context = new window.AudioContext();
// set up the basic oscillator chain, muted to begin with.
oscillator = context.createOscillator();
oscillator.frequency.setValueAtTime(110, 0);
envelope = context.createGain();
oscillator.connect(envelope);
envelope.connect(context.destination);
envelope.gain.value = 0.0; // Mute the sound
oscillator.start(0); // Go ahead and start up the oscillator
console.log("cwMonosynth initialised.");
};
// WebMIDIAPI MIDIOutput send()
// This synth does not support timestamps.
// It also ignores both channel info and velocity.
CWMonosynth.prototype.send = function(message, ignoredTimestamp)
{
var
command = message[0] & 0xF0,
channel = message[0] & 0xF,
data1 = message[1],
data2 = message[2],
that = this;
function checkCommandExport(command)
{
var index = commands.indexOf(command);
if(index < 0)
{
throw "Error: ".concat("Command ", command.toString(10), " (0x", command.toString(16), ") is not being exported.");
}
}
function handleNoteOff(channel, data1, data2)
{
checkCommandExport(CMD.NOTE_OFF);
that.noteOff(data1);
console.log("cwMonosynth NoteOff:".concat(" channel:", channel, " note:", data1, " velocity:", data2, " (This synth ignores channel and velocity info.)"));
}
function handleNoteOn(channel, data1, data2)
{
checkCommandExport(CMD.NOTE_ON);
that.noteOn(data1);
console.log("cwMonosynth NoteOn:".concat(" channel:", channel, " note:", data1, " velocity:", data2, " (This synth ignores channel and velocity info.)"));
}
switch(command)
{
case CMD.NOTE_OFF:
checkCommandExport(CMD.NOTE_OFF);
handleNoteOff(channel, data1, data2);
break;
case CMD.NOTE_ON:
checkCommandExport(CMD.NOTE_ON);
handleNoteOn(channel, data1, data2);
break;
default:
throw "Error: ".concat("Command ", command.toString(10), " (0x", command.toString(16), ") is not defined.");
}
};
CWMonosynth.prototype.noteOn = function(noteNumber)
{
activeNotes.push(noteNumber);
oscillator.frequency.cancelScheduledValues(0);
oscillator.frequency.setTargetAtTime(frequencyFromNoteNumber(noteNumber), 0, portamento);
envelope.gain.cancelScheduledValues(0);
envelope.gain.setTargetAtTime(1.0, 0, attack);
};
CWMonosynth.prototype.noteOff = function(noteNumber)
{
var position = activeNotes.indexOf(noteNumber);
if(position !== -1)
{
activeNotes.splice(position, 1);
}
if(activeNotes.length === 0)
{ // shut off the envelope
envelope.gain.cancelScheduledValues(0);
envelope.gain.setTargetAtTime(0.0, 0, release);
} else
{
oscillator.frequency.cancelScheduledValues(0);
oscillator.frequency.setTargetAtTime(frequencyFromNoteNumber(activeNotes[activeNotes.length - 1]), 0, portamento);
}
};
return API;
}(document));