Skip to content

Commit b2886ba

Browse files
author
Lauren McCarthy
committed
Merge pull request #277 from shiffman/soundexamplefixes
small changes to sound examples
2 parents 74489cf + bf22792 commit b2886ba

File tree

2 files changed

+15
-36
lines changed

2 files changed

+15
-36
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ var freqSlider, freqLabel, ampLabel, ampSlider, button;
55

66
var osc;
77
var freq = 220; // current frequency (updated by slider)
8-
var amp = .5;
8+
var amp = 0.5;
99
var fft;
1010

1111

1212
var oscOn = false;
1313

1414
function setup() {
1515
createCanvas(800,400);
16-
background(30);
17-
stroke(255);
18-
strokeWeight(10);
1916

2017
freqLabel = createP('Frequency: ');
2118
freqSlider = createSlider(1, 700, freq);
@@ -47,7 +44,7 @@ function setup() {
4744
}
4845

4946
function draw() {
50-
background(0);
47+
background(30);
5148

5249
amp = ampSlider.value()/100;
5350
osc.amp(amp);
@@ -63,6 +60,8 @@ function draw() {
6360
waveform = fft.waveform();
6461

6562
// draw the shape of the waveform
63+
stroke(255);
64+
strokeWeight(10);
6665
beginShape();
6766
for (var i = 0; i<waveform.length; i++){
6867
var x = map(i, 0, waveform.length, 0, width);

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

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,58 +13,38 @@ function preload() {
1313

1414
function setup() {
1515
createCanvas(400, 400);
16-
background(0, 0, 0);
1716

1817
soundFile.volume = .6;
1918

2019
// create the ball
21-
ball = ellipse()
22-
ball.x = width/2;
23-
ball.y = height/2;
24-
ball.speed = 7;
25-
ellipse(ball.x, ball.y, 100, 100);
20+
ball = {
21+
x: width/2,
22+
y: height/2,
23+
speed: 7
24+
}
2625
}
2726

2827
function draw() {
29-
background(0, 0, 0);
28+
background(0);
3029

3130
ball.x += ball.speed;
3231

3332

3433
// when the ball hits the wall...
35-
if (ball.x > width) {
36-
37-
// map the ball's x location to a panning degree (float between -1.0 and 1.0)
38-
var panning = map(ball.x, 0., width,-1.0, 1.0);
39-
soundFile.pan(panning);
40-
41-
42-
// set a random playback speed for the sound
43-
var newSpeed = Math.random();
44-
ball.speed = -(ball.speed);
45-
soundFile.rate(newSpeed);
46-
47-
// play the sound
48-
soundFile.play();
49-
}
50-
51-
if (ball.x < 0) {
34+
if (ball.x > width || ball.x < 0) {
5235

5336
// map the ball's x location to a panning degree (float between -1.0 and 1.0)
54-
var panning = map(ball.x, 0., width,-1.0, 1.0);
37+
var panning = map(ball.x, 0, width, -1, 1);
5538
soundFile.pan(panning);
5639

5740
// set a random playback speed for the sound
58-
var newSpeed = Math.random();
59-
ball.speed = -(ball.speed);
41+
var newSpeed = random(1);
42+
ball.speed = -ball.speed;
6043
soundFile.rate(newSpeed);
6144

6245
// play the sound
6346
soundFile.play();
64-
6547
}
66-
67-
ellipse(ball.x, ball.y, 100, 100)
68-
48+
ellipse(ball.x, ball.y, 100, 100);
6949
}
7050

0 commit comments

Comments
 (0)