-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·305 lines (253 loc) · 8.58 KB
/
index.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Virtual Theremin</title>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/tracking/build/tracking-min.js"></script>
<script src="js/MIDIUtils.js"></script>
<link rel="stylesheet" type="text/css" href="bower_components/normalize-css/normalize.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<!-- <div class="demo-title">
<p><a href="http://trackingjs.com" target="_parent">tracking.js</a> - choose the colors you want to detect through the controls on the right</p>
</div> -->
<div id="container" class="container">
<div class="content">
<header id="header" class="header">
<h1 id="title" class="title"> Virtual Theremin </h1>
</header>
<div id="sounds" class="sounds">
<div class="row">
<div class="col-md-3">
<input type="submit" id="sine" class="btn btn-sound" data-sound="sine" value="Sonido senoidal" onclick="setSound('sine');" />
</div>
<div class="col-md-3">
<input type="submit" id="square" class="btn btn-sound" data-sound="square" value="Sonido cuadrado" onclick="setSound('square');" />
</div>
<div class="col-md-3">
<input type="submit" id="sawtooth" class="btn btn-sound" data-sound="sawtooth" value="Sonido sierra" onclick="setSound('sawtooth');" />
</div>
<div class="col-md-3">
<input type="submit" id="triangle" class="btn btn-sound" data-sound="triangle" value="Sonido triangular" onclick="setSound('triangle');" />
</div>
</div>
</div>
<div class="row indicators">
<div class="col-md-1">
<img id="volume-png" src="img/volume.png" alt="Volume icon"/>
</div>
<div class="col-md-5">
<h2 id="volume"></h2>
</div>
<div class="col-md-5">
<h2 id="note" class="pull-right"></h2>
</div>
<div class="col-md-1">
<img id="pitch-png" src="img/pitch.png" alt="Pitch icon"/>
</div>
</div>
<div class="row hands">
<div class="col-md-6">
<img id="left-png" src="img/left.png" alt="Left hand"/>
</div>
<div class="col-md-6">
<img id="right-png" src="img/right.png" alt="Right hand"/>
</div>
</div>
<div class="row record-container">
<div class="col-lg-2 col-md-2 col-xs-3">
<input type="submit" id="record" class="btn btn-sound pull-left btn-record" data-sound="record" value="" onclick="record(this);" />
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="center-block">
<video id="video" width="1024" height="768" preload autoplay loop muted controls></video>
<canvas id="canvas" width="1024" height="768"></canvas>
</div>
</div>
</div>
</div>
<footer id="footer">
<p>© Amaia Eskisabel, Jorge Yagüe </p>
<!-- <h2>Recordings</h2>
<ul id="recordingslist"></ul> -->
</footer>
</div>
<!-- <button id="play-sound" onclick="start();">Play sound</button>
<button id="stop-sound" onclick="stop();">Stop sound</button>
<h2 id="note"></h2>
<h2 id="volume"></h2> -->
<script>
// Frecuencia del sonido
var maxFreq = 3000;
var minFreq = 65;
var maxVol = 1;
// Web Audio API
var audioCtx = new AudioContext();
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
oscillator.type = 'square'; // sine, square, sawtooth, triangle, custom
oscillator.frequency.value = minFreq;
oscillator.start();
setVolume(0);
var chunks = [];
var dest = audioCtx.createMediaStreamDestination();
var mediaRecorder = new MediaRecorder(dest.stream);
oscillator.connect(dest);
var stopped = true;
window.onload = function() {
var video = document.getElementById('video');
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var isTouching = false;
var touchTimeout;
var tracker = new tracking.ColorTracker(['cyan', 'yellow']);
tracking.track('#video', tracker, {
camera: true
});
tracker.on('track', function(event) {
context.clearRect(0, 0, canvas.width, canvas.height);
if (event.data.length === 0) {
// No targets were detected in this frame.
stop();
} else {
// Plots the detected targets here.
start();
event.data.forEach(function(rect) { // Eventos de detección
var vol = reverse(rect.y / canvas.height, maxVol, 0); // Volumen actual
var freq = reverse(rect.y / canvas.height * maxFreq, maxFreq, minFreq); // Frecuencia actual
checkTouching('sine', rect);
checkTouching('square', rect);
checkTouching('sawtooth', rect);
checkTouching('triangle', rect);
// checkTouching('record', rect);
if (rect.color === 'cyan') {
$('#left-png').hide('slow');
setVolume(vol);
}
if (rect.color === 'yellow') {
$('#right-png').hide('slow');
setFrequency(freq);
}
// Rectángulo alrededor de la detección
context.strokeStyle = rect.color;
context.strokeRect(rect.x, rect.y, rect.width, rect.height);
});
}
function checkTouching(button, rect) {
var realX = reverse(
rect.x + $('#canvas').offset().left,
$('#canvas').width(),
$('#canvas').offset().left
) + $('#canvas').offset().left;
var realY = rect.y + $('#canvas').offset().top;
var canvasXo = $('#' + button).offset().left;
var canvasYo = $('#' + button).offset().top;
var canvasXe = canvasXo + $('#' + button).width();
var canvasYe = canvasYo + $('#' + button).height();
// console.log('istouching ' + isTouching)
if (realX > canvasXo && realX < canvasXe && realY > canvasYo && realY < canvasYe) {
$('#' + button).css('background', 'red');
if (!isTouching) {
// console.log('touching')
isTouching = true;
// touchTimeout = setTimeout(function() {
setSound(button);
// }, 2000);
}
} else {
// console.log('not touching')
isTouching = false;
// clearTimeout(touchTimeout);
$('#' + button).css('background', '#2e8ece');
}
}
});
};
function frequencyToNote(freq) {
return MIDIUtils.noteNumberToName(MIDIUtils.frequencyToNoteNumber(freq));
}
// Invertir valores, por las coordenadas
function reverse(v, max, min) {
return (max + min) - v;
}
function setVolume(vol) {
gainNode.gain.value = vol;
$('#volume').text(Number(vol * 100).toFixed() + "%");
}
function setFrequency(freq) {
oscillator.frequency.value = freq;
$('#note').text(frequencyToNote(freq));
}
function start() {
setVolume(0.1);
}
function stop() {
setVolume(0);
}
function setSound(waveForm) {
oscillator.type = waveForm;
console.log('set sound' + waveForm);
}
// Recorder
var isRecording = false;
function record(button) {
if (isRecording) {
stopRecording(button);
} else {
startRecording(button);
}
}
function startRecording(button) {
console.log('Recording...')
isRecording = true;
mediaRecorder.start();
}
function stopRecording(button) {
console.log('Stop')
mediaRecorder.stop();
isRecording = false;
// // create WAV download link using audio data blob
// createDownloadLink();
}
mediaRecorder.ondataavailable = function(evt) {
// push each chunk (blobs) in an array
chunks.push(evt.data);
};
mediaRecorder.onstop = function(evt) {
// Make blob out of our blobs, and open it.
var blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' });
// window.location.href = URL.createObjectURL(blob);
createDownloadLink(blob);
};
function createDownloadLink(blob) {
console.log('Creating link')
stop();
var url = URL.createObjectURL(blob);
if (confirm('Esta opción todavía no funciona correctamente. Desea intentar la descarga de música igualmente?')) {
// window.location.href = URL.createObjectURL(blob);
window.open(url, '_blank');
} else {
}
// var li = document.createElement('li');
// var au = document.createElement('audio');
// var hf = document.createElement('a');
//
// au.controls = true;
// au.src = url;
// hf.href = url;
// hf.download = new Date().toISOString() + '.wav';
// hf.innerHTML = hf.download;
// li.appendChild(au);
// li.appendChild(hf);
// recordingslist.appendChild(li);
}
</script>
</body>
</html>