-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_2DNote.js
More file actions
255 lines (235 loc) · 8.07 KB
/
_2DNote.js
File metadata and controls
255 lines (235 loc) · 8.07 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*! LICENSE: https://github.com/hchiam/_2DNote/blob/master/LICENSE */
// example usage: _2DNote.play(...) or _2DNote.update(...)
var _2DNote = (function () {
var audioContext = new (AudioContext || webkitAudioContext)();
var note = null;
var viewXRange = [0, document.documentElement.clientWidth];
var viewYRange = [0, document.documentElement.clientHeight];
var comfyFrequencyRange = [150, 400];
var comfyVolumeRange = [0, 0.5]; // technically gain (ranges from 0 to 1)
var panRange = [-1, 1];
function setAs2DArea(e, callbackUponUpdate, setupExitDetection) {
setupExitDetection = setupExitDetection || true;
// e = event or element
// example usage: _2DNote.setAs2DArea(document.getElementById('2d-area', callbackUponUpdate));
this.callbackUponUpdate = callbackUponUpdate;
var element = e ? e : document.body;
element.addEventListener("mousedown", this.play.bind(this));
element.addEventListener("mouseup", this.stop.bind(this));
element.addEventListener("mousemove", this.update.bind(this));
element.addEventListener("touchstart", this.play.bind(this));
element.addEventListener("touchend", this.stop.bind(this));
element.addEventListener("touchmove", this.update.bind(this));
if (setupExitDetection) this.setupExitedViewDetection(element);
}
function play(e, setupExitDetection) {
setupExitDetection = setupExitDetection || true;
// e = event or element
// example usage: <body onmousedown="_2DNote.play(event);" style="width: 100vw; height: 100vh;" ontouchstart="_2DNote.play(event);"></body>
this.stop();
if (setupExitDetection) this.setupExitedViewDetection(e);
var frequency = this.getFrequency(e);
var volume = this.getVolume(e);
var pan = this.getPan(e);
var volumeSetup = this.audioContext.createGain();
volumeSetup.connect(this.audioContext.destination);
volumeSetup.gain.value = isNaN(volume) ? 0.5 : volume;
var oscillator = this.audioContext.createOscillator();
oscillator.type = "sine";
oscillator.frequency.value = isNaN(frequency) ? 400 : frequency;
oscillator.connect(volumeSetup);
// instead of oscillator.connect(this.audioContext.destination);
var panner = this.audioContext.createStereoPanner();
if (pan) panner.pan.value = pan;
volumeSetup.connect(panner);
panner.connect(this.audioContext.destination);
oscillator.start();
// var delayThatAvoidsCrazyReverbs = 1;
// oscillator.stop(this.audioContext.currentTime + delayThatAvoidsCrazyReverbs);
this.note = {
oscillator: oscillator,
volumeSetup: volumeSetup,
panner: panner,
};
}
var callbackUponUpdate = null;
function update(e, callback) {
// e = event or element
if (!this.note) return;
var frequency = this.getFrequency(e);
var volume = this.getVolume(e);
var pan = this.getPan(e);
var volumeSetup = this.note.volumeSetup;
volumeSetup.gain.value = volume;
var oscillator = this.note.oscillator;
oscillator.frequency.value = frequency;
var panner = this.note.panner;
panner.pan.value = pan;
if (callback) {
callback(volume, frequency, pan);
} else if (this.callbackUponUpdate) {
this.callbackUponUpdate(volume, frequency, pan);
}
}
function stop() {
// example usage: <body onmouseup="_2DNote.stop();" style="width: 100vw; height: 100vh;" ontouchend="_2DNote.stop();"></body>
if (this.note == null) return;
var oscillator = this.note.oscillator;
oscillator.stop(this.audioContext.currentTime);
this.note = null;
}
function setupExitedViewDetection(e) {
// TODO: only continue if detect does not already exist
var element = e ? e : document.body;
// console.log(element);
if (element.removeEventListener && element.addEventListener) {
element.removeEventListener("mouseleave", this.warnExitedView);
element.removeEventListener("touchcancel", this.warnExitedView);
element.addEventListener("mouseleave", this.warnExitedView);
element.addEventListener("touchcancel", this.warnExitedView);
}
}
function warnExitedView(e) {
var element = e ? e : document.body;
var screenWidth = element.clientWidth;
var screenHeight = element.clientHeight;
var simulatedCenterClick = {
// center: guaranteed != edge
currentTarget: true,
clientX: screenWidth / 2,
clientY: screenHeight / 2,
};
_2DNote.play(simulatedCenterClick);
setTimeout(function () {
_2DNote.stop();
if (element.removeEventListener) {
element.removeEventListener("mouseleave", _2DNote.warnExitedView);
element.removeEventListener("touchcancel", _2DNote.warnExitedView);
}
}, 100);
}
function getFrequency(e) {
// e = event or element
var x = this.getX(e);
var frequency = this.normalize(
x,
this.viewXRange,
this.comfyFrequencyRange
);
return frequency;
}
function getVolume(e) {
// e = event or element
var y = this.getY(e);
// technically getting gain (which ranges 0 to 1)
var volume = this.normalize(y, this.viewYRange, this.comfyVolumeRange);
return volume;
}
function getPan(e) {
// e = event or element
var x = this.getX(e);
// technically getting gain (which ranges 0 to 1)
var pan = this.normalize(x, this.viewXRange, this.panRange);
return pan;
}
function getX(e) {
// e = event or element
var isMouseEvent = e.currentTarget && e.clientX;
var isTouchEvent = e.touches;
if (isMouseEvent) {
return e.clientX;
} else if (isTouchEvent) {
return e.touches[0].clientX;
} else {
// element
return e.offsetLeft;
}
}
function getY(e) {
// e = event or element
var isMouseEvent = e.currentTarget && e.clientY;
var isTouchEvent = e.touches;
if (isMouseEvent) {
return e.clientY;
} else if (isTouchEvent) {
return e.touches[0].clientY;
} else {
// element
return e.offsetTop;
}
}
function normalize(value, inputRange, outputRange) {
var inputRangeMin = inputRange[0];
var inputRangeMax = inputRange[1];
var outputRangeMin = outputRange[0];
var outputRangeMax = outputRange[1];
var inputBias = value - inputRangeMin;
var ratioAdjustment =
(outputRangeMax - outputRangeMin) / (inputRangeMax - inputRangeMin);
var outputBias = outputRangeMin;
var output = inputBias * ratioAdjustment + outputBias;
var clampedOutput = Math.min(
Math.max(output, outputRangeMin),
outputRangeMax
);
return clampedOutput;
}
function copy() {
/**
* If you want to play multiple notes at the same time,
* you can use this to create more instances.
*/
return recursiveDeepCopy(this);
function recursiveDeepCopy(object) {
if (object === null || typeof object !== "object") {
return object;
} else if (Array.isArray(object)) {
var arrayCopy = [];
object.forEach(function (element) {
arrayCopy.push(recursiveDeepCopy(element));
});
return arrayCopy;
} else {
var objectCopy = {};
for (var property in object) {
if (object.hasOwnProperty(property)) {
if (property === "audioContext") {
objectCopy.audioContext = new AudioContext();
} else {
objectCopy[property] = recursiveDeepCopy(object[property]);
}
}
}
return objectCopy;
}
}
}
return {
audioContext: audioContext,
note: note,
viewXRange: viewXRange,
viewYRange: viewYRange,
comfyFrequencyRange: comfyFrequencyRange,
comfyVolumeRange: comfyVolumeRange,
panRange: panRange,
setAs2DArea: setAs2DArea,
play: play,
update: update,
stop: stop,
setupExitedViewDetection: setupExitedViewDetection,
warnExitedView: warnExitedView,
getFrequency: getFrequency,
getVolume: getVolume,
getPan: getPan,
getX: getX,
getY: getY,
normalize: normalize,
copy: copy,
};
})();
if (typeof window !== "undefined") {
window._2DNote = _2DNote;
}
if (typeof module !== "undefined") {
module.exports = _2DNote;
}