SPEECH IN : SPEECH OUT

this one needs :

// ----------------------------------------------------
// realtime speech recognition
// and speech synthesis
// with p5.speech.js
// ----------------------------------------------------

let speechrec = new p5.SpeechRec(); // reference object for speech recognition

// setup the speech recognition ----------
speechrec.continuous = false; // continous recognition yes/no
speechrec.interimResults = true; // show immediate output
speechrec.default_language = "de-DE"; // default recognition language

let speechsynth = new p5.Speech(0, synthIsLoaded); //reference object of speech synth
let mic; // microphe object

let words = []; // our temporary dictionary

let is_recording = false; // is_recording indicator

let interim_txt = "";
let result_txt = "";
let truism_txt = "";

function setup() {
  createCanvas(400, 400);
  mic = new p5.AudioIn();

  speechsynth.speak("Lass loslegen...");

  // add callback functions to recognition object
  speechrec.onStart = startRecog;
  speechrec.onResult = haveResult;
  speechrec.onEnd = finishRecog;
}

function synthIsLoaded() {
  console.log("synth is loaded with those voices: ");
  console.log(speechsynth.voices);
}

function draw() {
  if (is_recording) {
    background(220, 22, 22);
  } else {
    background(255);
  }

  textStyle(BOLD);
  fill(244);
  textAlign(LEFT);
  textSize(14);
  text(interim_txt, 20, 20, width-40, height / 2);
  
  fill(11);
  textSize(12);
  text(result_txt, 20, 120, width-40, height / 2);
  
  
  fill(222,11,11);
  textSize(12);
  text(truism_txt, 20, 320, width-40, height / 2);
  

  showBrain();
}

function mousePressed() {
  if (!is_recording) {
    is_recording = true;
     mic.start();
    speechrec.start();
  }
}


// ----------------------
// CALLBACK FUNCTIONS FOR RECOGNITION
// --------------------------

function startRecog() {
  console.log("start recgoc");
}

function haveResult() {
  
  // save all regognized results over here
  interim_txt = speechrec.resultString;

  // if confidence of machine is above 50%
  if (speechrec.resultConfidence > 0.5) {
    result_txt = speechrec.resultString;
  }
}

function finishRecog() {
  
  console.log("finished regognition");
  is_recording = false;
 
  learn_from_spoken_word(result_txt);
  truism_txt = returnNewTruism();
  
  
  // uncomment for random voice (weird!)
  //let rvid = int(random(speechsynth.voices.length));
  // speechsynth.setVoice(rvid); 
  
  
  mic.stop(); // shutdown mic 
  speechsynth.speak(truism_txt);
}




// ----------------------
// LOGIC FUNCTION
// --------------------------


function learn_from_spoken_word(_str) {
  
  let carr = _str.split(" ");

  for (let i = 0; i < carr.length; i++) {
    words.push(carr[i]);
  }
}

function showBrain() {
  
  let _str = "";
  // go thru our temporaray word brain and show stuff
  for (let i = 0; i < words.length; i++) {
   
    
    _str +=  words[i] + ", ";
  }
  
   textSize(8);
   fill(0);
   textAlign(LEFT);
   text(_str, 20, 220,width-40,height/2);
  
}

function returnNewTruism() {
  let rstr = ""; // the strign we return
  let randomWordCount = int(random() * 9 + 1);

  for (let i = 0; i < randomWordCount; i++) {
    rstr += random(words) + " ";
  }

  

  return rstr;
}