Skip to content

Commit 989ffa9

Browse files
(Gsoc'21)💚changed state variable to 2 booleans in soundRecorder example
1 parent 1cdf255 commit 989ffa9

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

examples/record/sketch.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
let mic, recorder, soundFile;
66

77
let state = 0; // mousePress will increment from state = 0 (Record), to 1(Stop), to 2(Play)
8-
8+
let isRecordingStarted = false;
9+
let isResultPlayed = false;
910

1011
function setup() {
1112
createCanvas(400, 400);
@@ -29,23 +30,23 @@ function setup() {
2930
function mousePressed() {
3031
userStartAudio();
3132
// use the '.enabled' boolean to make sure user enabled the mic (otherwise we'd record silence)
32-
if (state === 0) {
33+
if (!isRecordingStarted && !isResultPlayed) {
3334
// users must manually enable their browser microphone for recording to work properly!
3435
mic.start(function () {
3536
// Tell recorder to record to a p5.SoundFile which we will use for playback
3637
recorder.record(soundFile);
3738

3839
background(255, 0, 0);
3940
text('Recording now! Click to stop.', 20, 20);
40-
state++;
41+
isRecordingStarted = true;
4142
});
42-
} else if (state === 1) {
43+
} else if (isRecordingStarted && !isResultPlayed) {
4344
recorder.stop(); // stop recorder, and send the result to soundFile
4445
mic.dispose();
4546
background(0, 255, 0);
4647
text('Recording stopped. Click to play', 20, 20);
47-
state++;
48-
} else if (state === 2) {
48+
isResultPlayed = true;
49+
} else if (isRecordingStarted && isResultPlayed) {
4950
soundFile.play(); // play the result!
5051
}
5152
}

src/soundRecorder.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ const ac = p5sound.audiocontext;
2020
* @example
2121
* <div><code>
2222
* let mic, recorder, soundFile;
23-
* // mousePress will increment from state = 0 (Record), to 1(Stop), to 2(Play)
24-
* 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;
2527
*
2628
* function setup() {
2729
* let cnv = createCanvas(100, 100);
@@ -49,7 +51,7 @@ const ac = p5sound.audiocontext;
4951
* // ensure audio is enabled
5052
* userStartAudio();
5153
*
52-
* if (state === 0) {
54+
* if (!isRecordingStarted && !isResultPlayed) {
5355
* // make sure user enabled the mic by prompting to enable their browser mic
5456
* // start recording after the mic is enabled
5557
* mic.start(function() {
@@ -58,10 +60,10 @@ const ac = p5sound.audiocontext;
5860
*
5961
* background(255,0,0);
6062
* text('Recording!', width/2, height/2);
61-
* state++;
63+
* isRecordingStarted = true;
6264
* });
6365
* }
64-
* else if (state === 1) {
66+
* else if (isRecordingStarted && !isResultPlayed) {
6567
* background(0,255,0);
6668
*
6769
* // stop recorder and
@@ -71,10 +73,10 @@ const ac = p5sound.audiocontext;
7173
* mic.dispose();
7274
*
7375
* text('Done! Tap to play and download', width/2, height/2, width - 20);
74-
* state++;
76+
* isResultPlayed = true;
7577
* }
7678
*
77-
* else if (state === 2) {
79+
* else if (isRecordingStarted && isResultPlayed) {
7880
* soundFile.play(); // play the result!
7981
* save(soundFile, 'mySound.wav');
8082
* }

0 commit comments

Comments
 (0)