Skip to content

Commit 0490632

Browse files
author
Guillermo Montecinos
authored
Merge pull request #628 from satyasaibhushan/fixes_soundrecorder_mic_issues
(Gsoc'21) (Week-2) Fixes mic issues in recorder example
2 parents a74dd7e + 594f8ea commit 0490632

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

examples/record/sketch.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22
// We need p5.AudioIn (mic / sound source), p5.SoundRecorder
33
// (records the sound), and a p5.SoundFile (play back).
44

5-
var mic, recorder, soundFile;
5+
let mic, recorder, soundFile;
66

7-
var state = 0; // mousePress will increment from Record, to Stop, to Play
7+
let isRecordingStarted = false;
8+
let isResultPlayed = false;
89

910
function setup() {
10-
createCanvas(400,400);
11+
createCanvas(400, 400);
1112
background(200);
1213
fill(0);
1314
text('Enable mic and click the mouse to begin recording', 20, 20);
1415

1516
// create an audio in
1617
mic = new p5.AudioIn();
1718

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

@@ -29,26 +27,25 @@ function setup() {
2927
}
3028

3129
function mousePressed() {
30+
userStartAudio();
3231
// 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) {
32+
if (!isRecordingStarted && !isResultPlayed) {
33+
// users must manually enable their browser microphone for recording to work properly!
34+
mic.start(function () {
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+
isRecordingStarted = true;
41+
});
42+
} else if (isRecordingStarted && !isResultPlayed) {
4443
recorder.stop(); // stop recorder, and send the result to soundFile
45-
46-
background(0,255,0);
44+
mic.dispose();
45+
background(0, 255, 0);
4746
text('Recording stopped. Click to play', 20, 20);
48-
state++;
49-
}
50-
51-
else if (state === 2) {
47+
isResultPlayed = true;
48+
} else if (isRecordingStarted && isResultPlayed) {
5249
soundFile.play(); // play the result!
5350
}
54-
}
51+
}

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: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ const ac = p5sound.audiocontext;
2020
* @example
2121
* <div><code>
2222
* let mic, recorder, soundFile;
23-
* let state = 0;
23+
* // keeps record if recording is started
24+
* let isRecordingStarted = false;
25+
* // keeps record if the recorded result is played
26+
* let isResultPlayed = false;
2427
*
2528
* function setup() {
2629
* let cnv = createCanvas(100, 100);
@@ -31,9 +34,6 @@ const ac = p5sound.audiocontext;
3134
* // create an audio in
3235
* mic = new p5.AudioIn();
3336
*
34-
* // prompts user to enable their browser mic
35-
* mic.start();
36-
*
3737
* // create a sound recorder
3838
* recorder = new p5.SoundRecorder();
3939
*
@@ -51,31 +51,34 @@ const ac = p5sound.audiocontext;
5151
* // ensure audio is enabled
5252
* userStartAudio();
5353
*
54-
* // make sure user enabled the mic
55-
* if (state === 0 && mic.enabled) {
56-
*
57-
* // record to our p5.SoundFile
58-
* recorder.record(soundFile);
54+
* if (!isRecordingStarted && !isResultPlayed) {
55+
* // make sure user enabled the mic by prompting to enable their browser mic
56+
* // start recording after the mic is enabled
57+
* mic.start(function() {
58+
* // record to our p5.SoundFile
59+
* recorder.record(soundFile);
5960
*
60-
* background(255,0,0);
61-
* text('Recording!', width/2, height/2);
62-
* state++;
61+
* background(255,0,0);
62+
* text('Recording!', width/2, height/2);
63+
* isRecordingStarted = true;
64+
* });
6365
* }
64-
* else if (state === 1) {
66+
* else if (isRecordingStarted && !isResultPlayed) {
6567
* background(0,255,0);
6668
*
6769
* // stop recorder and
6870
* // send result to soundFile
6971
* recorder.stop();
72+
* // stop browser from accessing the mic
73+
* mic.dispose();
7074
*
7175
* text('Done! Tap to play and download', width/2, height/2, width - 20);
72-
* state++;
76+
* isResultPlayed = true;
7377
* }
7478
*
75-
* else if (state === 2) {
79+
* else if (isRecordingStarted && isResultPlayed) {
7680
* soundFile.play(); // play the result!
7781
* save(soundFile, 'mySound.wav');
78-
* state++;
7982
* }
8083
* }
8184
* </div></code>

0 commit comments

Comments
 (0)