Skip to content

Commit a4affd2

Browse files
(Gsoc'21)💚Fixes mic issues in recorder example
1 parent a74dd7e commit a4affd2

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

examples/record/sketch.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ var mic, recorder, soundFile;
77
var state = 0; // mousePress will increment from Record, to Stop, to Play
88

99
function setup() {
10-
createCanvas(400,400);
10+
createCanvas(400, 400);
1111
background(200);
1212
fill(0);
1313
text('Enable mic and click the mouse to begin recording', 20, 20);
1414

1515
// create an audio in
1616
mic = new p5.AudioIn();
1717

18-
// users must manually enable their browser microphone for recording to work properly!
19-
mic.start();
20-
2118
// create a sound recorder
2219
recorder = new p5.SoundRecorder();
2320

@@ -30,25 +27,23 @@ function setup() {
3027

3128
function mousePressed() {
3229
// use the '.enabled' boolean to make sure user enabled the mic (otherwise we'd record silence)
33-
if (state === 0 && mic.enabled) {
34-
35-
// Tell recorder to record to a p5.SoundFile which we will use for playback
36-
recorder.record(soundFile);
37-
38-
background(255,0,0);
39-
text('Recording now! Click to stop.', 20, 20);
40-
state++;
41-
}
42-
43-
else if (state === 1) {
30+
if (state === 0) {
31+
// users must manually enable their browser microphone for recording to work properly!
32+
mic.start(function() {
33+
// Tell recorder to record to a p5.SoundFile which we will use for playback
34+
recorder.record(soundFile);
35+
36+
background(255, 0, 0);
37+
text('Recording now! Click to stop.', 20, 20);
38+
state++;
39+
});
40+
} else if (state === 1) {
4441
recorder.stop(); // stop recorder, and send the result to soundFile
45-
46-
background(0,255,0);
42+
mic.dispose();
43+
background(0, 255, 0);
4744
text('Recording stopped. Click to play', 20, 20);
4845
state++;
49-
}
50-
51-
else if (state === 2) {
46+
} else if (state === 2) {
5247
soundFile.play(); // play the result!
5348
}
5449
}

index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ <h2>p5.sound
6565
<div><a href="examples/pause_soundfile">pause_soundfile</a></div>
6666
<div><a href="examples/peakDetect">peakDetect</a></div>
6767
<div><a href="examples/peakDetect_basic">peakDetect_basic</a></div>
68-
<div><a href="examples/peakDetection_offline">peakDetection_offline</a></div>
6968
<div><a href="examples/play_soundfile">play_soundfile</a></div>
7069
<div><a href="examples/playbackRate">playbackRate</a></div>
7170
<div><a href="examples/playbackRatePart">playbackRatePart</a></div>

src/looper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ class Score {
387387
constructor() {
388388
// for all of the arguments
389389
this.parts = [];
390-
this.currentPart = new Array(arguments.length);;
390+
this.currentPart = new Array(arguments.length);
391391

392392
var thisScore = this;
393393
for (var i in arguments) {
@@ -396,7 +396,7 @@ class Score {
396396
this.parts[i].onended = function () {
397397
thisScore.resetPart(i);
398398
playNextPart(thisScore);
399-
};
399+
};
400400
}
401401
this.looping = false;
402402
}

src/soundRecorder.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ const ac = p5sound.audiocontext;
3131
* // create an audio in
3232
* mic = new p5.AudioIn();
3333
*
34-
* // prompts user to enable their browser mic
35-
* mic.start();
36-
*
3734
* // create a sound recorder
3835
* recorder = new p5.SoundRecorder();
3936
*
@@ -51,22 +48,26 @@ const ac = p5sound.audiocontext;
5148
* // ensure audio is enabled
5249
* userStartAudio();
5350
*
54-
* // make sure user enabled the mic
55-
* if (state === 0 && mic.enabled) {
56-
*
57-
* // record to our p5.SoundFile
58-
* recorder.record(soundFile);
51+
* if (state === 0) {
52+
* // make sure user enabled the mic by prompting to enable their browser mic
53+
* // start recording after the mic is enabled
54+
* mic.start(function() {
55+
* // record to our p5.SoundFile
56+
* recorder.record(soundFile);
5957
*
60-
* background(255,0,0);
61-
* text('Recording!', width/2, height/2);
62-
* state++;
58+
* background(255,0,0);
59+
* text('Recording!', width/2, height/2);
60+
* state++;
61+
* });
6362
* }
6463
* else if (state === 1) {
6564
* background(0,255,0);
6665
*
6766
* // stop recorder and
6867
* // send result to soundFile
6968
* recorder.stop();
69+
* // stop browser from accessing the mic
70+
* mic.dispose();
7071
*
7172
* text('Done! Tap to play and download', width/2, height/2, width - 20);
7273
* state++;

0 commit comments

Comments
 (0)