Skip to content

Commit 6364ec5

Browse files
author
Lauren McCarthy
committed
Merge pull request #286 from therewasaguy/p5SOUND
p5.sound update
2 parents 49713e3 + f62fa6c commit 6364ec5

File tree

23 files changed

+467
-524
lines changed

23 files changed

+467
-524
lines changed

examples/addons/p5.sound/FFT_freqRange/sketch.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
2-
* Display the average amount of energy (amplitude) across
3-
* a range of frequencies using the FFT methods analyze() and getFreq().
2+
* Display the average amount of energy (amplitude) across a range
3+
* of frequencies using the p5.FFT class and its methods analyze()
4+
* and getFreq().
45
*
56
* This example divides the frequency spectrum into eight bands.
67
*/
@@ -24,7 +25,7 @@ function setup() {
2425
noStroke();
2526
textAlign(CENTER);
2627

27-
fft = new FFT();
28+
fft = new p5.FFT();
2829

2930
p = createP(description);
3031
var p2 = createP('Description: Using getFreq(low, high) to measure amplitude within a range of frequencies.');
@@ -38,6 +39,7 @@ function draw() {
3839

3940
// Generate 8 bars to represent 8 different frequency ranges
4041
for (var i = 0; i < 8; i++){
42+
noStroke();
4143
fill((i*30) % 100 + 50, 195, (i*25 + 50) % 255 )
4244

4345
// Each bar has a unique frequency range
@@ -51,13 +53,17 @@ function draw() {
5153
// Rectangle height represents the average value of this frequency range
5254
var h = -height + map(freqValue, 0, 255, height, 0);
5355
rect((i+1)*width/8 - width/8, height, width/8, h);
54-
56+
stroke(255);
5557
text( loFreq.toFixed(0) +' Hz - ' + hiFreq.toFixed(0)+' Hz', (i+1)*width/8 - width/8/2, 30);
5658
}
5759
}
5860

5961
function keyPressed() {
60-
soundFile.pause();
62+
if (soundFile.isPlaying()){
63+
soundFile.pause();
64+
} else {
65+
soundFile.play();
66+
}
6167
}
6268

6369
// Change description text if the song is loading, playing or paused

examples/addons/p5.sound/FFT_frequency_spectrum/sketch.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* This example draws the frequency spectrum of a sound by using
3-
* the FFT object's analyze() method.
3+
* the p5.FFT object's analyze() method.
44
*
55
* FFT is a Fast Fourier Transform function that calculates
66
* amplitude across the frequency spectrum. The analyze() method returns
@@ -38,7 +38,7 @@ function setup() {
3838
// loop the sound file
3939
soundFile.loop();
4040

41-
fft = new FFT();
41+
fft = new p5.FFT();
4242

4343
// update description text
4444
p = createP(description);
@@ -96,5 +96,9 @@ function updateDescription() {
9696

9797
// pause the song if a key is pressed
9898
function keyPressed() {
99-
soundFile.pause();
99+
if (soundFile.isPlaying()){
100+
soundFile.pause();
101+
} else {
102+
soundFile.play();
103+
}
100104
}

examples/addons/p5.sound/FFT_waveform/sketch.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* DEMO: Draw the waveform of a sound as it plays using FFT.waveform()
2+
* DEMO: Draw the waveform of a sound as it plays using p5.FFT.waveform()
33
*/
44

55
var soundFile;
@@ -26,9 +26,9 @@ function setup() {
2626
* - Smoothing
2727
* - Length of the FFT's analyze/waveform array. Must be a power of two between 16 and 1024 (default).
2828
*/
29-
fft = new FFT(.99, fftBands);
29+
fft = new p5.FFT(.99, fftBands);
3030

31-
p = createP('press any key to pause');
31+
p = createP('press any key to pause / play');
3232
}
3333

3434
function draw() {
@@ -50,5 +50,9 @@ function draw() {
5050
}
5151

5252
function keyPressed() {
53-
soundFile.pause();
53+
if (soundFile.isPlaying() ) {
54+
soundFile.pause();
55+
} else {
56+
soundFile.play();
57+
}
5458
}

examples/addons/p5.sound/FFT_waveform_2/sketch.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* DEMO: Draw the waveform of a sound as it plays using FFT.waveform()
2+
* DEMO: Draw the waveform of a sound as it plays using p5.FFT.waveform()
33
*/
44

55
var soundFile;
@@ -12,7 +12,8 @@ var xOffset = 0;
1212
var waveform = [];
1313

1414
function preload() {
15-
soundFile = loadSound( ['../_files/lucky_dragons_-_power_melody.mp3','../_files/lucky_dragons_-_power_melody.ogg'] );
15+
soundFormats('ogg', 'mp3');
16+
soundFile = loadSound('../_files/lucky_dragons_-_power_melody');
1617
}
1718

1819
function setup() {
@@ -27,7 +28,7 @@ function setup() {
2728
soundFile.loop();
2829

2930
// Create an FFT object. Give it smoothing and fftSize
30-
fft = new FFT(.99, fftSize);
31+
fft = new p5.FFT(.99, fftSize);
3132
createP('Press spacebar to pause');
3233
}
3334

examples/addons/p5.sound/PulseFFT/sketch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ function setup() {
2828
ampSlider = createSlider(0.0, 100.0, amp*100);
2929

3030

31-
pulse = new Pulse(freq);
31+
pulse = new p5.Pulse(freq);
3232
pulse.amp(amp);
3333

3434
// create an fft to analyze the audio
35-
fft = new FFT();
35+
fft = new p5.FFT();
3636

3737
// begin sound
3838
toggleOsc();

examples/addons/p5.sound/amplitude_analysis/sketch.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* DEMO: Use Amplitude (volume) to change the size of an ellipse
2+
* DEMO: Use p5.Amplitude (volume) to change the size of an ellipse
33
*/
44

55
var size;
@@ -25,8 +25,8 @@ function setup() {
2525

2626
soundFile.loop();
2727

28-
// create a new Amplitude. Optionally, give it a 'smoothing' value betw 0.0 and .999
29-
amplitude = new Amplitude(smoothing);
28+
// create a new p5.Amplitude. Optionally, give it a 'smoothing' value betw 0.0 and .999
29+
amplitude = new p5.Amplitude(smoothing);
3030

3131
// instruction text
3232
description = 'Spacebar: pause/unpause the loop. <br>Press "N" to toggle Normalize';
@@ -64,7 +64,11 @@ function keyPressed(e) {
6464

6565
// spacebar pauses
6666
if (e.keyCode == 32) {
67-
soundFile.pause();
67+
if (soundFile.isPlaying()) {
68+
soundFile.pause();
69+
} else {
70+
soundFile.play();
71+
}
6872
}
6973

7074
// 'n' keypress toggles normalize on/off

examples/addons/p5.sound/envelope/sketch.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Attempting to convert Wilm's Envelope example from the Processing Handbook ex2
1+
// Adapting Wilm Thoben's Envelope example from the Processing Handbook ex2
22

33
/*
44
This sketch shows how to use envelopes and oscillators. Envelopes are pre-defined amplitude
@@ -21,8 +21,9 @@ var env;
2121
var attackTime = 0.1;
2222
var attackLevel = 0.7;
2323
var decayTime = 0.3;
24+
var decayLevel = 0.2;
2425
var sustainTime = 0.1;
25-
var sustainLevel = 0.2;
26+
var sustainLevel = decayLevel;
2627
var releaseTime = 0.5;
2728

2829
var midiSequence = [ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72 ];
@@ -39,11 +40,11 @@ function setup(){
3940

4041
trigger = millis();
4142

42-
triOsc = new TriOsc();
43+
triOsc = new p5.TriOsc();
4344
triOsc.amp(0);
4445
triOsc.start();
4546

46-
env = new Env(attackTime, attackLevel, decayTime, sustainLevel, sustainTime, releaseTime);
47+
env = new p5.Env(attackTime, attackLevel, decayTime, decayLevel, sustainTime, sustainLevel, releaseTime);
4748
fill(0);
4849
}
4950

examples/addons/p5.sound/envelopeOnOff/sketch.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Example: Create an Envelope to control oscillator amplitude.
2+
* Example: Create an Envelope (p5.Env) to control oscillator amplitude.
33
* Trigger the Attack portion of the envelope when the mouse is clicked.
44
* Trigger the Release when the mouse is released.
55
*/
@@ -11,8 +11,9 @@ var env;
1111
var attackTime = 0.1;
1212
var attackLevel = 0.7;
1313
var decayTime = 0.3;
14+
var decayLevel = 0.2;
1415
var sustainTime = 0.1;
15-
var sustainLevel = 0.2;
16+
var sustainLevel = decayLevel;
1617
var releaseTime = 0.5;
1718
var duration = 1000;
1819
// Set the note trigger
@@ -28,12 +29,13 @@ function setup(){
2829

2930
trigger = millis();
3031

31-
triOsc = new TriOsc();
32+
triOsc = new p5.TriOsc();
3233
triOsc.amp(0);
3334
triOsc.start();
3435

35-
env = new Env(attackTime, attackLevel, decayTime, sustainLevel, sustainTime, releaseTime);
36+
env = new p5.Env(attackTime, attackLevel, decayTime, decayLevel, sustainTime, sustainLevel, releaseTime);
3637
fill(0);
38+
createP('click mouse to triggerAttack, release mouse to triggerRelease')
3739
}
3840

3941
function draw(){

examples/addons/p5.sound/loadSound_callback/sketch.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
function setup() {
66
createCanvas(400,200);
7-
sample1 = loadSound(['../_files/Damscray_-_Dancing_Tiger_01.mp3', '../_files/Damscray_-_Dancing_Tiger_01.ogg'], soundReady);
7+
soundFormats('ogg', 'mp3');
8+
soundFile = loadSound('../_files/Damscray_-_Dancing_Tiger_01', soundReady);
89
}
910

1011
function soundReady(){
11-
sample1.rate(1.75);
12-
sample1.loop();
12+
soundFile.rate(1.75);
13+
soundFile.loop();
1314

14-
text('File is ready! Click to pause.', 50, 10);
15+
text('File is ready! Click to pause / unpause', 50, 10);
1516

1617
// draw the waveform
17-
peaks = sample1.getPeaks();
18+
peaks = soundFile.getPeaks();
1819
beginShape();
1920
for (i = 0; i< peaks.length; i++){
2021
vertex(map(i, 0, peaks.length, 0, width), map(peaks[i], -1, 1, height, 0) );
@@ -23,5 +24,9 @@ function soundReady(){
2324
}
2425

2526
function mousePressed(){
26-
sample1.pause();
27+
if (soundFile.isPlaying()){
28+
soundFile.pause();
29+
} else {
30+
soundFile.play();
31+
}
2732
}

examples/addons/p5.sound/loadSound_preload/sketch.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33

44

55
function preload(){
6-
sample1 = loadSound(['../_files/Damscray_-_Dancing_Tiger_01.mp3', '../_files/Damscray_-_Dancing_Tiger_01.ogg']);
6+
soundFormats('ogg', 'mp3');
7+
soundFile = loadSound('../_files/Damscray_-_Dancing_Tiger_01');
78
}
89

910
function setup() {
1011
createCanvas(400,200);
1112

12-
text('File is ready! Click to pause.', 50, 10);
13+
text('File is ready! Click to pause / play.', 50, 10);
1314

14-
sample1.rate(.8);
15-
sample1.reverseBuffer();
16-
sample1.loop();
15+
soundFile.rate(.8);
16+
soundFile.reverseBuffer();
17+
soundFile.loop();
1718

18-
peaks = sample1.getPeaks();
19+
peaks = soundFile.getPeaks();
1920
beginShape();
2021
for (i = 0; i< peaks.length; i++){
2122
vertex(map(i, 0, peaks.length, 0, width), map(peaks[i], -1, 1, height, 0) );
@@ -25,5 +26,9 @@ function setup() {
2526
}
2627

2728
function mousePressed(){
28-
sample1.pause();
29+
if (soundFile.isPlaying()){
30+
soundFile.pause();
31+
} else {
32+
soundFile.play();
33+
}
2934
}

0 commit comments

Comments
 (0)