Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/002-Amplitude-VisualizingLoudness/sketch.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
async function setup() {
sound = loadSound('https://tonejs.github.io/audio/berklee/gong_1.mp3');
soundy = await loadSound('https://tonejs.github.io/audio/berklee/gong_1.mp3');
createCanvas(400, 400);
textAlign(CENTER);
fill(255);

amp = new p5.Amplitude();
sound.connect(amp);
soundy.connect(amp);

describe('The color of the background changes based on the amplitude of the sound.');
}

function mousePressed() {
sound.play();
soundy.play();
}

function draw() {
Expand Down
2 changes: 1 addition & 1 deletion examples/006-EnvelopeAndfilter/sketch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let osc, env;
let noise, env;

function setup() {
let cnv = createCanvas(100, 100);
Expand Down
15 changes: 10 additions & 5 deletions examples/008_b-FFT-WaveForm-VisualizeSoundFile/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async function setup(){
soundFile = await loadSound('assets/chime.mp3')
cnv.mouseClicked(togglePlay);
filter = new p5.Biquad(800, 'lowpass');
filter.res(18);
filter.res(30);
verb = new p5.Reverb(0.850);
fft = new p5.FFT(32);
soundFile.disconnect();
Expand All @@ -21,15 +21,19 @@ function draw(){
background(220);
let spectrum = fft.analyze();
noStroke();

push();
fill(255, 0, 0);

for (let i = 0; i < spectrum.length; i++) {
let x = map(i, 0, spectrum.length, 0, width);
let h = -height + map(spectrum[i], 0, 0.1, height, -1000);
rect(x, height, width / spectrum.length, h )
}
pop();

let waveform = fft.waveform();

push();
noFill();
beginShape();
stroke(20);
Expand All @@ -39,10 +43,11 @@ function draw(){
vertex(x,y);
}
endShape();

pop();

textAlign(CENTER);
text('tap to play', width/2, 20);
filter.freq(map(mouseX, 0, width, 200, 18000))
text('click to start playback', width/2, 20);
filter.freq(map(mouseX, 0, width, 40, 14000))
describe('The sketch displays the frequency spectrum and waveform of the sound that plays.');
}

Expand Down
45 changes: 29 additions & 16 deletions examples/019-p5-to-tone/sketch.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
let synth, ctx

function setup() {
createCanvas(400, 400);
//get the p5.sound.js Audio Context
ctx = getAudioContext()
//set the Tone.js Audio Context to match the p5.sound.js context
Tone.setContext(ctx)
//create a new MembraneSynth from the Tone.js library
synth = new Tone.MembraneSynth();
//create a new p5.sound.js Reverb effect
rev = new p5.Reverb(3)
//connect the MembraneSynth to the Reverb
rev.setInput(synth)
}

function draw() {
background(220);
textAlign(CENTER);
textWrap(WORD);
textSize(13);
text('click and drag the mouse', width/2, 150);

//initiallize the oscillator
beep = new p5.Oscillator();

describe('A grey sketch that demonstrates how to use the Oscillator class in p5.sound.');
}

function mousePressed() {
synth.triggerAttackRelease("C2", "8n");
beep.start();
}

function mouseReleased() {
beep.stop();
}

function draw() {
background(220);
let frequency = map(mouseX, 0, width, 440, 880);
let amp = map(mouseY, 0, height, 1, 0);
beep.freq(frequency);
beep.amp(amp);
if (beep.started) {
text('Frequency: ' + frequency.toFixed(0) + 'Hz', 0, height/2, width);
text('Amplitude: ' + amp.toFixed(2), 0, height/2 + 20, width);
}
else {
text('click and drag the mouse to change the frequency and amplitude values', 0, height/2, width);
}
}
8 changes: 4 additions & 4 deletions examples/020-p5-to-tone-panning/sketch.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
let sound_location = 0

let panner
function setup() {
createCanvas(400, 400)
// get and set the "Audio Context"
ctx = getAudioContext()
Tone.setContext(ctx)

synthy = new Tone.MonoSynth()
panny = new p5.Panner()
panner = new p5.Panner()

// connect a Tone.js audio node to a p5 sound effect
panny.setInput(synthy)
//panner.setInput(synthy)
}

function draw() {
Expand All @@ -20,7 +20,7 @@ function draw() {

function mousePressed() {
sound_location = random(-1,1)
panny.pan(sound_location)
panner.pan(sound_location)
synthy.triggerAttackRelease("D#5", (1.5))
}

16 changes: 16 additions & 0 deletions examples/027-3D-Sound-Source/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en"><head>
<script src="https://cdn.jsdelivr.net/npm/p5@2/lib/p5.js"></script>
<script src="../../dist/p5.sound.js"></script>

<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">

</head>
<body>
<main>
</main>
<script src="sketch.js"></script>


</body></html>
41 changes: 41 additions & 0 deletions examples/027-3D-Sound-Source/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
let sample, spatializer, x, y, z;

let randomTime = 0;

async function setup() {
sample = await loadSound("../../sounds/drums.mp3");
sample.loop(true);
createCanvas(100, 100, WEBGL);

camera(0, 0, 200);

angleMode(DEGREES);
describe("a sketch that plays processes an audio file with a reverb effect.");

spatializer = new p5.Panner3D();
spatializer.maxDist(100);
sample.disconnect();
sample.connect(spatializer);

}

function mousePressed() {
if (!sample.isPlaying()) {
sample.play();
}
else {
x = random(-10, 10);
y = random(-10, 10);
z = random(0, 10);

spatializer.set(x, y, z);
}
}

function draw() {
background(220);
push()
translate(x, y, z);
box(5, 5, 5);
pop()
}
7 changes: 7 additions & 0 deletions examples/027-3D-Sound-Source/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
html, body {
margin: 0;
padding: 0;
}
canvas {
display: block;
}
Binary file added examples/028-SoundFileRate/assets/chime.mp3
Binary file not shown.
Binary file added examples/028-SoundFileRate/assets/drums.mp3
Binary file not shown.
16 changes: 16 additions & 0 deletions examples/028-SoundFileRate/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en"><head>
<script src="https://cdn.jsdelivr.net/npm/p5@2/lib/p5.js"></script>
<script src="../../dist/p5.sound.js"></script>

<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">

</head>
<body>
<main>
</main>
<script src="sketch.js"></script>


</body></html>
30 changes: 30 additions & 0 deletions examples/028-SoundFileRate/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
async function setup() {
sample = await loadSound("assets/drums.mp3");
sample.loop(true);
createCanvas(100, 100);
textAlign(CENTER);
textWrap(WORD);
textSize(10);
describe("a sketch that changes the playback rate of a soundfile");
}

function draw() {
background(220);
rate = map(mouseX, 0, width, 0.5, 2);
sample.rate(rate);
if (!sample.isPlaying()) {
text("click to play the sound, move your mouse to change the playback rate", 0, 20, width);
}
else {
text("Playback Rate: " + rate.toFixed(2), 0, 20, width);
}
}

function mousePressed() {
if(!sample.isPlaying()) {
sample.play();
}
else {
sample.stop();
}
}
7 changes: 7 additions & 0 deletions examples/028-SoundFileRate/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
html, body {
margin: 0;
padding: 0;
}
canvas {
display: block;
}
Binary file added examples/029-AmplitudeAnalysis/assets/chime.mp3
Binary file not shown.
Binary file added examples/029-AmplitudeAnalysis/assets/drums.mp3
Binary file not shown.
16 changes: 16 additions & 0 deletions examples/029-AmplitudeAnalysis/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en"><head>
<script src="https://cdn.jsdelivr.net/npm/p5@2/lib/p5.js"></script>
<script src="../../dist/p5.sound.js"></script>

<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">

</head>
<body>
<main>
</main>
<script src="sketch.js"></script>


</body></html>
41 changes: 41 additions & 0 deletions examples/029-AmplitudeAnalysis/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
let amplitude = 0;

async function setup() {
sample = await loadSound("assets/drums.mp3");
sample.loop(true);

createCanvas(100, 100);
textAlign(CENTER);
textWrap(WORD);
textSize(8);
describe("a sketch that visualizes the loudness of a soundfile with smoothing control");

analyze = new p5.Amplitude();
sample.connect(analyze);
}

function draw() {
background(amplitude * 512, 0, 0);

amplitude = analyze.getLevel();

fill(255);
if (!sample.isPlaying()) {
text("click to play the sound, move your mouse to adjust smoothing", 0, 20, width);
}
else {
text("Amplitude: " + amplitude.toFixed(2), 0, 20, width);
}
smoothing = map(mouseX, 0, width, 0, 1);
analyze.smooth(smoothing);
text("Smoothing: " + smoothing.toFixed(2), 0, 60, width);
}

function mousePressed() {
if(!sample.isPlaying()) {
sample.play();
}
else {
sample.stop();
}
}
7 changes: 7 additions & 0 deletions examples/029-AmplitudeAnalysis/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
html, body {
margin: 0;
padding: 0;
}
canvas {
display: block;
}
Binary file added examples/030-AmplitudeSetInput/assets/chime.mp3
Binary file not shown.
Binary file added examples/030-AmplitudeSetInput/assets/drums.mp3
Binary file not shown.
16 changes: 16 additions & 0 deletions examples/030-AmplitudeSetInput/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en"><head>
<script src="https://cdn.jsdelivr.net/npm/p5@2/lib/p5.js"></script>
<script src="../../dist/p5.sound.js"></script>

<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">

</head>
<body>
<main>
</main>
<script src="sketch.js"></script>


</body></html>
Loading