Hello Sound
Click to start music, click again to switch to microphone.
This is actually a p5 version of the Processing sketch below. One slight difference: the web version uses mouse / touch where the Processing version uses key presses. I used my processing-p5-convert converter, followed by some manual changes due to differences between the Sound libraries of Processing and p5.
Some things to note about this example:
- ParticleGenerator is a separate class, with
setPosition()
anddisplay()
functions - audio input can come from a music file or from the microphone
- the audio input is connected to an Amplitude object
- in
draw()
:- get the current audio level from the Amplitude object (between 0 and 1)
- use the audio level when we draw to screen:
- audio level is shown as a rectangle on the left side
- ParticleGenerator
display()
uses the audio level to change the face size and the particle rate
//
// hello_sound
//
import processing.sound.*;
AudioIn microphone;
SoundFile file;
Amplitude amp;
ParticleGenerator particleGenerator;
boolean showLevel = true;
void setup()
{
size(800, 800);
noCursor();
microphone = new AudioIn(this, 0);
microphone.start();
file = new SoundFile(this, "deeper_understanding_60.mp3");
file.play();
amp = new Amplitude(this);
amp.input(file);
particleGenerator = new ParticleGenerator();
}
void draw()
{
background(0);
float level = amp.analyze();
if (showLevel)
rect(0, height*(1-level), 50, height*(1-level));
particleGenerator.setPosition(mouseX, mouseY);
particleGenerator.display(level);
}
void keyPressed()
{
if (key == 'l')
{
showLevel = !showLevel;
particleGenerator.displayParticleCount = !particleGenerator.displayParticleCount;
}
else if (key == 'f')
{
file.play();
amp.input(file);
}
else if (key == 'm')
{
file.pause();
amp.input(microphone);
}
}
In case you’re curious, the music track Deeper Understanding is one of several tracks from SistaSara released with a Creative Commons license.