var myRec = new p5.SpeechRec();
myRec.interimResults = true;
var acceptableWords = ["wow", "please", "all right", "continue", "good", "okay", "that's", "go on", "alright", "oh no", "I am sorry", "I'm sorry"];
var mostRecentSpokenWord;
var mostRecentConfidence;
function initializeMySpeechRecognizer(){
mySpeechRecognizer = new p5.SpeechRec('en-US');
mySpeechRecognizer.continuous = true;
mySpeechRecognizer.interimResults = false;
mySpeechRecognizer.onResult = parseResult;
mySpeechRecognizer.start();
console.log(mySpeechRecognizer);
for (var i = 0; i < 49; i++) {
recordings[i] = loadSound(i.toString()+'.mp3');;
photo = loadImage("shannonphoto.jpg");
createCanvas(windowWidth, windowHeight);
stringOnScreen = "ready!";
button = createButton('<p style="color:black; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; line-height: 1.0;">Go to NYT Daily Podcast</p>');
button.position(windowWidth-170,height-50);
button.mousePressed(webpage);
initializeMySpeechRecognizer();
link("https://www.nytimes.com/2017/10/18/podcasts/the-daily/factory-jobs.html");
function link(href, target) {
if (target !== null) window.open(href, target);
else window.location = href;
image(photo, width/2, height/2-60, photo.width/2, photo.height/2);
text("Click on Screen to listen to Shannon Mulcahy tell her story", width/2, height/2+128);
text("Please respond intermittently to show that you are listening, as if you are an interviewer yourself", width/2, height/2+150);
text("Adapted from the Oct. 18, 2017 episode of The New York Times Daily Podcast", width/2, height/2+200);
ellipse(width/2, 0, 100, 100);
ellipse(width/2, height, 100, 100);
if (!recordings[curPlaying].isPlaying() && curPlaying < 5) {
if (Math.sqrt(Math.pow(mouseX - width/2, 2) + Math.pow(mouseX - height, 2))) {
text("(Don\'t forget to say something like 'okay,' 'continue,' or 'go on,' to show that you are listening)", width/2, height-100);
text("You may click screen to re-calibrate mic if it doesn't respond", width/2, height-84);
mostRecentConfidence = mySpeechRecognizer.resultConfidence;
if (mostRecentConfidence > 0.3){
var curString = mySpeechRecognizer.resultString;
for (var i = 0; i < acceptableWords.length; i++) {
if (curString.includes(acceptableWords[i])) {
mostRecentSpokenWord = mySpeechRecognizer.resultString.split(' ').pop();
bFoundRhymeWithMostRecentWord = false;
micLevel = mic.getLevel();
var size = map(micLevel, 0, 1, 120, 400);
if (recordings[curPlaying].isPlaying()) {
ellipse(width/2, height, size, size);
ellipse(width/2, height, 100, 100);
ellipse(width/2, height, 100, 100);
amplitude.setInput(recordings[curPlaying]);
var audioLevel = amplitude.getLevel();
var size = map(audioLevel, 0, 1, 120, 400);
if (recordings[curPlaying].isPlaying()) {
ellipse(width/2, 0, size, size);
ellipse(width/2, 0, 100, 100);
if (millis() - lastSpoken < timeLimit && curPlaying < 48) {
recordings[curPlaying].play();
ellipse(width/2, 0, 100, 100);
function mousePressed() {
amplitude = new p5.Amplitude();
recordings[curPlaying].play();
initializeMySpeechRecognizer();