Skip to content

Commit

Permalink
Fine tuning the coefficients, and fixed some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosrafaelgn committed Feb 2, 2015
1 parent 51e6aee commit 974298e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
16 changes: 8 additions & 8 deletions Program.js
@@ -1,4 +1,4 @@
//
//
// GraphicalFilterEditor is distributed under the FreeBSD License
//
// Copyright (c) 2012-2015, Carlos Rafael Gimenes das Neves
Expand Down Expand Up @@ -149,16 +149,16 @@ Program.prototype = {
return true;
},
getAttribsAndUniforms: function (src, attribs, uniforms, utypes) {
//esse é um parser bem simples, que apenas procura por linhas iniciadas por "uniform",
//considera apenas comentários do tipo //
// This is a very simple parser, which handles only lines starting with "uniform",
// with "attribute or lines starting with a // comment
var i, ii, line, lines = src.split("\n"), tokens, token, currentType, breakNow, lastChar;
for (i = 0; i < lines.length; i++) {
//remove os espaços do começo e do final da linha
// Remove extra spaces from the beginning and from the end of the line
line = lines[i].replace(/^\s+|\s+$/g, "");
if (line.substr(0, 7) === "uniform") {
//não considera os tabs (por hora)
// We do not consider the possibility of "\t" separating tokens (for now)
tokens = line.split(" ");
//pula o uniform e o tipo
// Skip the "uniform" token and store the current type to be used aftwerwards
currentType = tokens[1];
for (ii = 2; ii < tokens.length; ii++) {
token = tokens[ii];
Expand All @@ -173,9 +173,9 @@ Program.prototype = {
if (breakNow) break;
}
} else if (line.substr(0, 9) === "attribute") {
//não considera os tabs (por hora)
// We do not consider the possibility of "\t" separating tokens (for now)
tokens = line.split(" ");
//pula o attribute e o tipo
// Skip the "attribute" token and store the current type to be used aftwerwards
for (ii = 2; ii < tokens.length; ii++) {
token = tokens[ii];
if (token === ";") break;
Expand Down
36 changes: 19 additions & 17 deletions SoundParticles.js
Expand Up @@ -52,16 +52,16 @@ function SoundParticles(audioContext, graphicEqualizer) {
COLORS_B = function (A, B) { mthis.COLORS[(3 * A) + 2] = B; };
this.analyzerL = audioContext.createAnalyser();
this.analyzerL.fftSize = 1024;
this.analyzerL.maxDecibels = -20;
this.analyzerL.minDecibels = -60;
this.analyzerL.maxDecibels = -12;
this.analyzerL.minDecibels = -45;
this.analyzerL.smoothingTimeConstant = 0;
this.analyzerR = audioContext.createAnalyser();
this.analyzerR.fftSize = 1024;
this.analyzerR.maxDecibels = -20;
this.analyzerR.minDecibels = -60;
this.analyzerR.maxDecibels = -12;
this.analyzerR.minDecibels = -45;
this.analyzerR.smoothingTimeConstant = 0;
this.commonCoefNew = 0.0625 / 16.0;
this.processedData = new Uint8Array(512);
this.processedDataR = new Uint8Array(512);
this.fft = new Float32Array(256);
this.analyze = function () { return SoundParticles.prototype.realAnalyze.apply(mthis); };
this.canvas = null;
Expand Down Expand Up @@ -185,7 +185,7 @@ SoundParticles.prototype = {
if (!this.ctx) {
this.canvas = document.createElement("canvas");
this.canvas.width = 512;
this.canvas.height = 512;
this.canvas.height = 320;
this.canvas.style.position = "relative";
this.canvas.style.margin = "0px";
this.canvas.style.padding = "0px";
Expand Down Expand Up @@ -226,7 +226,7 @@ SoundParticles.prototype = {
"gl_FragColor = vec4(vColor.r * a, vColor.g * a, vColor.b * a, 1.0); }", false, false);
this.program.use();
this.program.texColor(0);
this.program.aspect(1, 1);
this.program.aspect(320.0 / 512.0, 1);

var gl = this.ctx, glBuf0, glBuf1, glTex, glVerticesRect, glTexCoordsRect;

Expand Down Expand Up @@ -327,21 +327,23 @@ SoundParticles.prototype = {
realAnalyze: function () {
if (!this.alive) return false;

var time = Date.now(), delta = (time - this.lastTime) * 0.001, gl = this.ctx, a,
avg, p = 0, c, ic, i, idx, last = 44, last2 = 116, coefNew = this.commonCoefNew * (time - this.lastTime),
coefOld = 1.0 - coefNew, processedData = this.processedData, fft = this.fft,
var time = Date.now(), delta = (time - this.lastTime), gl = this.ctx, a,
avg, p = 0, c, ic, i, idx, last = 44, last2 = 116, coefNew = (0.0625 / 16.0) * delta,
coefOld = 1.0 - coefNew, processedData = this.processedData, processedDataR = this.processedDataR, fft = this.fft,
BG_COLUMNS = this.BG_COLUMNS, BG_PARTICLES_BY_COLUMN = this.BG_PARTICLES_BY_COLUMN, COLORS = this.COLORS,
program = this.program, bgPos = this.bgPos, bgSpeedY = this.bgSpeedY, bgColor = this.bgColor,
bgTheta = this.bgTheta, MAX = Math.max;

delta *= 0.001;
this.lastTime = time;

// http://www.w3.org/TR/webaudio/
// http://webaudio.github.io/web-audio-api/#widl-AnalyserNode-getByteTimeDomainData-void-Uint8Array-array
this.analyzerL.getByteFrequencyData(processedData);
// Use only the first 256 amplitudes (DC to 11kHz, considering SR = 44kHz)
this.analyzerR.getByteFrequencyData(processedDataR);
// Use only the first 256 amplitudes (which convers DC to 11025Hz, considering a sample rate of 44100Hz)
for (i = 0; i < 256; i++) {
c = processedData[i];
c = MAX(processedData[i], processedDataR[i]);
ic = ((c < 8) ? 0.0 : c);
idx = fft[i];
if (ic < idx)
Expand All @@ -354,27 +356,27 @@ SoundParticles.prototype = {

i = 2;
for (c = 0; c < BG_COLUMNS; c++) {
// Increase the amplitudes in order to improve the effect
// Increase the amplitudes as the frequency increases, in order to improve the effect
if (i < 6) {
a = processedData[i] / 255.0;
i++;
} else if (i < 20) {
a = ((processedData[i] + processedData[i + 1]) >> 1) / 255.0;
a = 1.5 * ((processedData[i] + processedData[i + 1]) >> 1) / 255.0;
i += 2;
} else if (i < 36) {
a = ((processedData[i] + processedData[i + 1] + processedData[i + 2] + processedData[i + 3]) >> 2) / 255.0;
a = 1.5 * ((processedData[i] + processedData[i + 1] + processedData[i + 2] + processedData[i + 3]) >> 2) / 255.0;
i += 4;
} else if (i < 100) {
avg = 0;
for (; i < last; i++)
avg = MAX(avg, processedData[i]);
a = avg / 255.0;
a = 2.0 * avg / 255.0;
last += 8;
} else {
avg = 0;
for (; i < last2; i++)
avg = MAX(avg, processedData[i]);
a = avg / 255.0;
a = 2.5 * avg / 255.0;
last2 += 16;
}

Expand Down
19 changes: 9 additions & 10 deletions index.html
Expand Up @@ -474,22 +474,21 @@
</p>
<div class="SEP"></div>
<p>This is a test for a JavaScript graphical filter editor, created by me
(Carlos Rafael Gimenes das Neves - <a href="http://twitter.com/carlosrafaelgn">@carlosrafaelgn</a>,
<code><script type="text/javascript">//<![CDATA[
"use strict";
document.write(["c", "a", "r", "l", "o", "s", "r", "a", "f", "a", "e", "l", ".", "prog"].join(""));
document.write(" at ");
document.write([" ", "g", "m", "a", "il", " dot "].join(""));
document.write(["c", "o", "m"].join(""));
//]]></script></code>), based on my old C++ graphic equalizer.</p>
(Carlos Rafael Gimenes das Neves - <a href="http://twitter.com/carlosrafaelgn">@carlosrafaelgn</a>, <code id="shdg3724"></code>), based on my old C++ graphic equalizer.</p>
<p>Check out the <a href="https://github.com/carlosrafaelgn/GraphicalFilterEditor">source</a> for more information! This test uses <a href="http://www.w3.org/TR/webaudio/">Web Audio API</a>, <a href="http://www.w3.org/TR/FileAPI/">File API</a> and <a href="http://www.w3.org/TR/workers/">Web Worker API</a> and requires a <a href="http://caniuse.com/audio-api">compliant browser</a> to run properly. In <a href="https://wiki.mozilla.org/WebAudio_API_Rollout_Status">Firefox 23 and 24</a>, Web Audio API must be enabled using <code>about:config</code>.</p>
<p>Please, load files with a sample rate of 44100Hz or 48000Hz (the filter itself supports any sample rate, this is just due to AudioContext).</p>
<p>Check out the functions <code>main()</code> and <code>updateConnections()</code> to see how to have stereo output using two analyzers!</p>
<p>If running this locally in Chrome, it must be started with the command-line option <code>--allow-file-access-from-files</code> otherwise you will not be able to load any files!</p>
<p>* Playing files via streaming was tested on Chrome v29.0.1547.76 and later, and on Firefox Nightly v27.0a1 (2013-10-04). If any error happens, or you hear no difference when changing the filter, please, load the entire file into memory before playing.</p>
<p>** Chrome v30 has apparently stopped to support loading large files into memory. If any error happens, or only a small portion of the file is played/filtered, please, use either Chrome v29/v32 or Firefox v26/v27+.</p>
<p>** Chrome v30 has apparently stopped to support loading large files into memory. If any error happens, or only a small portion of the file is played/filtered, please, use either Chrome v29/v32+ or Firefox v26/v27+.</p>

<script type="text/javascript">//<![CDATA[
"use strict"; main(); //]]></script>
"use strict";
document.getElementById("shdg3724").appendChild(document.createTextNode(
["c", "a", "r", "l", "o", "s", "r", "a", "f", "a", "e", "l", ".", "prog"].join("") +
"\u0040" +
["g", "m", "a", "i", "l", "\u002e"].join("") +
["c", "o", "m"].join("")));
main(); //]]></script>
</body>
</html>

0 comments on commit 974298e

Please sign in to comment.