xxxxxxxxxx
126
let mic, recorder;
let soundFile1;
let soundFile2;
let state = 0;
let recorded;
let sf;
//let play = false;
function setup() {
let cnv = createCanvas(100, 100);
//cnv.mousePressed(canvasPressed);
background(220);
textAlign(CENTER, CENTER);
// create an audio in
mic = new p5.AudioIn();
// prompts user to enable their browser mic
mic.start();
// create a sound recorder
recorder = new p5.SoundRecorder();
// connect the mic to the recorder
recorder.setInput(mic);
// this sound file will be used to
// playback & save the recording
soundFile1 = new p5.SoundFile();
soundFile2 = new p5.SoundFile();
text('press arrow to record', width/2, height/2);
}
function record(sf){
background(220)
text('press arrow to record', width/2, height/2);
// ensure audio is enabled
userStartAudio();
// make sure user enabled the mic
// record to our p5.SoundFile
recorder.record(sf,2, displayDone);
// else if (state === 2) {
// background(0,255,0);
// // stop recorder and
// // send result to soundFile
// recorder.stop();
// text('Done! Tap to play and download', width/2, height/2, width - 20);
// }
}
function displayDone(){
background(255,0,0);
text('Recording!', width/2, height/2);
//state = 2;
background(0,255,0);
text('Done! Tap to play and download', width/2, height/2, width - 20);
}
function playAudio(sf) {
//if(play == true)
sf.play();
}
function keyPressed() {
if (keyCode === ENTER) {
console.log(soundFile1)
playAudio(soundFile1)
//play = true;
}
if (keyCode === SHIFT) {
playAudio(soundFile2)
}
if (keyCode === UP_ARROW) {
record(soundFile1)
}
if (keyCode === DOWN_ARROW) {
record(soundFile2)
}
}
function canvasPressed() {
// ensure audio is enabled
userStartAudio();
// make sure user enabled the mic
if (state === 0 && mic.enabled) {
// record to our p5.SoundFile
recorder.record(soundFile1);
background(255,0,0);
text('Recording!', width/2, height/2);
state = 2;
}
else if (state === 2) {
background(0,255,0);
// stop recorder and
// send result to soundFile
recorder.stop();
text('Done! Tap to play and download', width/2, height/2, width - 20);
//state = 3;
//playAudio(soundFile);
}
// else if (state === 3) {
// soundFile.play(); // play the result!
// //save(soundFile, 'mySound.wav');
// //state++;
// }
}