-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,58 @@ | ||
/** | ||
* @name Frequency Spectrum | ||
* @description <p>Visualize the frequency spectrum of live audio input.</p> | ||
* <p><em><span class="small"> To run this example locally, you will need the | ||
* <a href="http://p5js.org/reference/#/libraries/p5.sound">p5.sound library</a> | ||
* and a running <a href="https://github.com/processing/p5.js/wiki/Local-server">local server</a>.</span></em></p> | ||
*/ | ||
var mic, fft, | ||
labelScale = 3, | ||
userInteracted = false; // need to wait for user interaction before using WebAudio https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio | ||
|
||
function setupEqualizer() { | ||
createCanvas(350, 100); | ||
noFill(); | ||
$('.equalizer').append($('.p5Canvas').remove()) | ||
$('.eq-btn').hide() | ||
|
||
userInteracted = true; | ||
var myp5 = new p5( function(sketch) { | ||
|
||
mic = new p5.AudioIn(); | ||
mic.start(); | ||
fft = new p5.FFT(); | ||
fft.setInput(mic); | ||
} | ||
var x = 100; | ||
var y = 100; | ||
|
||
function draw() { | ||
if (userInteracted) { | ||
var scale = 1; | ||
background(200); | ||
|
||
text(parseInt(map(mouseX, 0, width, 20, 15000 / scale)) + " Hz", mouseX - 10, mouseY - 20); | ||
var spectrum = fft.analyze(); | ||
|
||
beginShape(); | ||
vertex(0, height * 0.9); | ||
for (i = 0; i < spectrum.length / scale; i++) { | ||
vertex(i * scale, map(spectrum[i], 0, 255, height * 0.8, 0)); | ||
} | ||
vertex(width, height * 0.9); | ||
endShape(); | ||
|
||
var freq | ||
// draw labels | ||
for (i = 0; i < spectrum.length / scale; i += width / labelScale) { | ||
freq = parseInt(map(i, 0, 1024, 20, 15000)); | ||
text(freq, map(i, 0, 1024 / scale, 0, width), height * 0.97); | ||
fill(0, 100); | ||
sketch.setup = function() { | ||
|
||
var ac = sketch.getAudioContext(); | ||
|
||
ac.suspend().then(function() { | ||
|
||
sketch.userStartAudio($('.btn-begin'), function() { | ||
mic = new p5.AudioIn(); | ||
mic.start(); | ||
fft = new p5.FFT(); | ||
fft.setInput(mic); | ||
|
||
userInteracted = true; | ||
}); | ||
}); | ||
|
||
sketch.createCanvas(350, 100); | ||
sketch.noFill(); | ||
}; | ||
|
||
sketch.draw = function() { | ||
if (userInteracted) { | ||
var scale = 1; | ||
sketch.background(200); | ||
|
||
sketch.text(parseInt(sketch.map(sketch.mouseX, 0, sketch.width, 20, 15000 / scale)) + " Hz", sketch.mouseX - 10, sketch.mouseY - 20); | ||
var spectrum = fft.analyze(); | ||
|
||
sketch.beginShape(); | ||
sketch.vertex(0, sketch.height * 0.9); | ||
for (i = 0; i < spectrum.length / scale; i++) { | ||
sketch.vertex(i * scale, sketch.map(spectrum[i], 0, 255, sketch.height * 0.8, 0)); | ||
} | ||
sketch.vertex(sketch.width, sketch.height * 0.9); | ||
sketch.endShape(); | ||
|
||
var freq | ||
// draw labels | ||
for (i = 0; i < spectrum.length / scale; i += sketch.width / labelScale) { | ||
freq = parseInt(sketch.map(i, 0, 1024, 20, 15000)); | ||
sketch.text(freq, sketch.map(i, 0, 1024 / scale, 0, sketch.width), sketch.height * 0.97); | ||
sketch.fill(0, 100); | ||
} | ||
} | ||
} | ||
} | ||
}, $('.equalizer')[0]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters