forked from borismus/spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g-oscillator.html
219 lines (193 loc) · 5.39 KB
/
g-oscillator.html
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
<link rel="import" href="./bower_components/polymer/polymer.html">
<link href='http://fonts.googleapis.com/css?family=Inconsolata' rel='stylesheet' type='text/css'>
<polymer-element name="g-oscillator" attributes="log speed">
<template>
<style>
canvas {
position: absolute;
width: 100%;
height: 100%;
}
</style>
<canvas id="freq"
on-mousedown={{onMouseDown}}
on-mouseup={{onMouseUp}}
on-mousemove={{onMouseMove}}
on-mouseout={{onMouseOut}}
on-touchstart={{onTouchStart}}
on-touchend={{onTouchEnd}}
on-touchmove={{onTouchMove}}></canvas>
</template>
<script>
// Assumes context is an AudioContext defined outside of this class.
Polymer('g-oscillator', {
gainAmount: 0.2,
history: [],
log: false,
speed: 2,
fadeTime: 0.01,
attachedCallback: function() {
this.loop();
},
loop: function() {
this.width = window.innerWidth;
this.height = window.innerHeight;
this.addHistory(this.lastY);
var canvas = this.$.freq;
var ctx = canvas.getContext('2d');
canvas.width = this.width;
canvas.height = this.height;
var size = 6;
var nyquist = context.sampleRate/2;
for (var i = 0; i < this.history.length; i++) {
var y = this.history[i];
if (y === null) {
continue;
}
var x = this.width - (this.history.length - i - 1) * this.speed;
// TODO(smus): Elimiate fudge factor.
ctx.fillStyle = 'red';
ctx.fillRect(x - size/2, y - size/2, size, size);
}
if (this.osc_) {
// Label the point.
var label = this.formatFrequency_(this.lastFreq);
ctx.font = '20px Inconsolata'
ctx.fillText(label, this.lastX, this.lastY);
}
requestAnimationFrame(this.loop.bind(this));
},
formatFrequency_: function(freq) {
return Math.round(freq) + ' Hz';
},
onMouseDown: function(event) {
console.log('md');
if (this.osc_) {
// There can be only one oscillator.
return;
}
this.updatePointer(event);
// Create an oscillator.
this.osc_ = this.createOscillator_();
this.lastFreq = this.getLastFrequency();
this.osc_.frequency.value = this.lastFreq;
},
onMouseUp: function() {
console.log('mu');
this.deleteOscillator_();
this.updatePointer(null);
this.lastFreq = null;
},
onMouseMove: function(event) {
console.log('mm');
if (this.osc_) {
this.updatePointer(event);
this.lastFreq = this.getLastFrequency();
this.osc_.frequency.value = this.lastFreq;
}
},
onMouseOut: function(event) {
console.log('mo');
this.deleteOscillator_();
this.updatePointer(null);
this.lastFreq = null;
},
onTouchStart: function(event) {
event.preventDefault();
console.log('ts');
if (this.osc_) {
return;
}
this.updatePointer(event);
// Create an oscillator.
this.osc_ = this.createOscillator_();
this.lastFreq = this.getLastFrequency();
this.osc_.frequency.value = this.lastFreq;
},
onTouchMove: function(event) {
event.preventDefault();
console.log('tm');
if (this.osc_) {
this.updatePointer(event);
this.lastFreq = this.getLastFrequency();
this.osc_.frequency.value = this.lastFreq;
}
},
onTouchEnd: function(event) {
event.preventDefault();
console.log('te');
this.updatePointer(null);
this.deleteOscillator_();
this.lastFreq = null;
},
updatePointer: function(event) {
event = event || {};
var type = event.type || '';
if (type.indexOf('mouse') == 0) {
this.lastX = event.pageX;
this.lastY = event.pageY;
} else if (type.indexOf('touch') == 0) {
this.lastX = event.touches[0].pageX;
this.lastY = event.touches[0].pageY;
} else {
this.lastX = null;
this.lastY = null;
}
},
addHistory: function(freq) {
if (this.history.length > 100) {
this.history.splice(0, 1);
}
this.history.push(freq);
},
getLastFrequency: function() {
var x = this.lastX;
var y = this.lastY;
// Linearly divide the Y axis and assign frequency.
var percent = 1 - (y / this.height);
var nyquist = context.sampleRate/2;
if (this.log) {
percent = this.logScale_(percent * 1000, 1000) / 1000;
}
return percent * nyquist;
},
createOscillator_: function() {
// Create a gain node.
var gain = context.createGain();
gain.gain.value = 0;
gain.connect(context.destination);
// TODO(smus): Clean this up!
this.gain_ = gain;
// Create an oscillator and connect it through the gain.
var osc = context.createOscillator();
osc.connect(gain);
// Start it with a fade-in.
gain.gain.linearRampToValueAtTime(this.gainAmount,
context.currentTime + this.fadeTime);
osc.start(0);
return osc;
},
deleteOscillator_: function() {
if (this.osc_) {
var endTime = context.currentTime + this.fadeTime;
this.gain_.gain.linearRampToValueAtTime(0, endTime);
this.osc_.stop(endTime);
this.osc_ = null;
}
},
/**
* Given an index and the total number of entries, return the
* log-scaled value.
*/
logScale_: function(index, total, opt_base) {
var base = opt_base || 2;
var logmax = this.logBase(total + 1, base);
var exp = logmax * index / total;
return Math.pow(base, exp) - 1;
},
logBase: function(val, base) {
return Math.log(val) / Math.log(base);
},
});
</script>
</polymer-element>