-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
136 lines (118 loc) · 3.63 KB
/
app.js
File metadata and controls
136 lines (118 loc) · 3.63 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
/*
* This file is part of Zpeech.
*
* Copyright 2014 Zengularity
*
* Zpeech is free software: you can redistribute it and/or modify
* it under the terms of the AFFERO GNU General Public License as published by
* the Free Software Foundation.
*
* Zpeech is distributed "AS-IS" AND WITHOUT ANY WARRANTY OF ANY KIND,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* NON-INFRINGEMENT, OR FITNESS FOR A PARTICULAR PURPOSE. See
* the AFFERO GNU General Public License for the complete license terms.
*
* You should have received a copy of the AFFERO GNU General Public License
* along with Zpeech. If not, see <http://www.gnu.org/licenses/agpl-3.0.html>
*/
var SAMPLES = 1024;
var REFRESH_RATE = 50;
var AudioContext = require("audiocontext");
var _ = require("lodash");
var dat = require("dat-gui");
var Qajax = require("qajax");
var formantsToVowels = require("./src/formantsToVowels");
var extractFormants = require("./src/extractFormants");
var getMicrophone = require("./src/getMicrophone");
var Spectrum = require("./src/spectrum");
if (!AudioContext) {
alert("no AudioContext support available in your browser.");
throw new Error("AudioContext not supported");
}
var ctx = new AudioContext();
var analyzer = ctx.createAnalyser();
var maybeMicrophone = getMicrophone(ctx, 1.5).then(function (mic) {
mic.connect(analyzer);
return mic;
});
var array = new Uint8Array(SAMPLES);
analyzer.smoothingTimeConstant = 0.7;
analyzer.fftSize = SAMPLES * 2;
function indexToFrequency (i) {
return i * ctx.sampleRate / analyzer.fftSize;
}
function valueToPercent (value) {
return value / 256;;
}
var greFormants = {
i: [100, 3379],
e: [430, 2336],
u: [200, 1500],
a: [827, 1542],
o: [562, 3577],
œ: [695, 1840]
};
var formantsParams = greFormants;
var f1Params = {};
var f2Params = {};
_.each(formantsParams, function (f, v) {
f1Params[v] = f[0];
f2Params[v] = f[1];
});
var formants = [];
var $vowel_container = document.getElementById("vowel");
var $vowels = _.map(_.range(0, 3), function (i) {
var node = document.createElement("span");
node.className = "vowel";
$vowel_container.appendChild(node);
return node;
});
var $f1 = document.getElementById("f1");
var $f2 = document.getElementById("f2");
var sendLetter = function(){}/*
_.throttle(function(l){
return Qajax("http://10.0.25.2:9000/letter?q="+l);
}, 100);*/
maybeMicrophone.then(function(mic){
setInterval(function(){
analyzer.getByteFrequencyData(array);
formants = extractFormants(array, indexToFrequency, valueToPercent);
$f1.textContent = "~"+Math.round(formants[0].freq);
$f2.textContent = "~"+Math.round(formants[1].freq);
var vowels = formantsToVowels(formants, f1Params, f2Params);
if (vowels[0]) sendLetter(vowels[0][0]);
_.each($vowels, function ($vowel, i) {
var vowel = vowels[i];
if (vowel) {
$vowel.textContent = vowel[0];
$vowel.classList.remove("hidden");
}
else {
$vowel.classList.add("hidden");
}
});
}, REFRESH_RATE);
});
(function(canvas) {
var canvasCtx = canvas.getContext("2d");
var spectrum = new Spectrum(canvasCtx, array);
function loop () {
requestAnimationFrame(function () {
loop();
spectrum.render(_.map(formants, function (f) {
return Math.floor(f.freq * analyzer.fftSize / ctx.sampleRate);
}));
});
}
maybeMicrophone.then(loop);
}(document.getElementById("frequencies")));
var gui = new dat.GUI();
var folder;
folder = gui.addFolder('f1');
_.each(f1Params, function (f, v) {
folder.add(f1Params, v, 0, 3000);
});
folder = gui.addFolder('f2');
_.each(f2Params, function (f, v) {
folder.add(f2Params, v, 500, 5000);
});