xxxxxxxxxx
93
// How to use
// click and allow mic access
// select a question with keys 1-9
// press space to start recording
// press space again to end recording
//This code is super mess I'll clean up later
//There might also be some leftover stuff from a copy pasted example for recording if stuff doesn't appear to make sense/be used
let mic, recorder, soundFile;
let state = 0;
let questions = [];
let answers = [];
let answerNames = [];
let curQ;
let curQName;
let recording;
function preload() {
for (let i = 0; i < 9; i++) {
questions.push(loadSound('q-00' + (i + 1) + '.mp3'));
}
}
function setup() {
createCanvas(400, 400);
background(200);
fill(0);
text('Enable mic and click the mouse to begin recording', 20, 20);
mic = new p5.AudioIn();
mic.start();
recorder = new p5.SoundRecorder();
recorder.setInput(mic);
soundFile = new p5.SoundFile();
}
function draw() {
if (recording) {
background(240, 10, 10);
} else {
background(15, 230, 20);
}
}
function keyPressed() {
for (let i = 0; i < 9; i++) {
questions[i].stop();
}
if (key > '0' && key <= '9') {
print('Hello');
questions[key - 1].play();
curQ = key;
}
if (key === " " && !recording && mic.enabled) {
print('recording started');
recording = true;
curQName = curQ;
recorder.record(soundFile);
} else {
if (key === " " && recording) {
print('recording stopped');
recording = false;
recorder.stop();
answers.push(soundFile);
answerNames.push(curQName);
saveSound(answers[0], 'answer q' + answerNames[0])
}
}
}