🔍

SPEECH IN : SPEECH OUT


<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/addons/p5.sound.min.js"></script>

    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />

  </head>
  <body>
    
    <script src="https://sfxr.me/riffwave.js"></script>

    <script src="https://sfxr.me/sfxr.js"></script>

    <script src="sketch.js"></script>
    
    <input type="submit" id="startbtn" value="start">
  
</body>
</html>



let attack = 0; // anstieg
let sustain = 0; // halten
let decay = 0; // abfall


function newTalk(){
  
  sound['wave_type']= int(random()*2)+1;
  
  basefreq = random()*.4+.2;
  sound['p_base_freq']= basefreq;
  
   sound['p_freq_ramp']= random()*.8+.1;
  
  sound['p_vib_strength']=  random()*.7;
  sound['p_vib_speed']=  random()*1.1;
  
  sound['p_repeat_speed']=  random()*.9+.1;
  
  
  
  sound['p_env_attack']= random()*.3+.03;
  sound['p_env_sustain']= random()*.3+.1;
  sound['p_env_decay']= random()*.1+.4;
  sound['p_env_punch']= random()*.8+.1;
  
  
  
   sound['p_arp_speed']=  random()*.4+1.1;
  sound['p_arp_mod']=  random()*.4+.5;
  
  
 
  
  
  var s = new SoundEffect(sound).generate();
// returns a webaudio object if supported, or an Audio object
s.getAudio().play();
  
  
  console.log("TALK");
  speak_trickle=1;

}
 

var sound = {
  "oldParams": true,
  "wave_type": 1,
  "p_env_attack": 0,
  "p_env_sustain": 0.31718502829007483,
  "p_env_punch": 0,
  "p_env_decay": 0.2718540993592685,
  "p_base_freq": 0.26126191208337196,
  "p_freq_limit": 0,
  "p_freq_ramp": 0.43787689856926615,
  "p_freq_dramp": 0,
  "p_vib_strength": 0,
  "p_vib_speed": 0,
  "p_arp_mod": 0,
  "p_arp_speed": 0,
  "p_duty": 1,
  "p_duty_ramp": 0,
  "p_repeat_speed": 0.7558565452384385,
  "p_pha_offset": 0,
  "p_pha_ramp": 0,
  "p_lpf_freq": 1,
  "p_lpf_ramp": 0,
  "p_lpf_resonance": 0,
  "p_hpf_freq": 0,
  "p_hpf_ramp": 0,
  "sound_vol": 0.25,
  "sample_rate": 44100,
  "sample_size": 8
};






// MIC INPUT HYSTERESIS TUTORIAL
// microphone input is observed to trigger random 8bit sound

var fft, mic, amp ;
let listening = false;

let smooth_centroid = 1;

let speak_trickle = 0;

let base_noise_level = 0;

let kickin_threshold = .015;
let is_speaking = false;
 
let diff_sum = 0;

let prev_energy = 0;
let  energy = 0;


function setup() {
  createCanvas(400, 400);
  
  // audio input (microphone)
    mic = new p5.AudioIn();
    mic.stop();
    
  
   fft = new p5.FFT();
    // microphone volume
   // amp = new p5.Amplitude(.9);
    fft.setInput(mic);
  
  startbtn = document.getElementById("startbtn");
  startbtn.addEventListener("click",startToTalk);
  
  noStroke();
  
}

function draw() {
  
  
  if (listening === true && mic.enabled) {
  
    let spectrum = fft.analyze();
    
     if(spectrum == null){ return;}
    
    
      energy =  fft.getEnergy("mid") /255 ;
    
    base_noise_level = lerp( base_noise_level, energy, 0.04);
    
     let centr =  fft.getCentroid();
    
    let mean_freq_index = centr/(22050/spectrum.length);
    
    let centroidplot = map(log(mean_freq_index), 0, log(spectrum.length), 0, 1);
    
     background(220);
    
    if(centroidplot >0){
      
      smooth_centroid = lerp(smooth_centroid,centroidplot,.4);
        
    }
   
   /*
    
    fill(262);
    rect(0,centroidplot*height,width,8);
    
    fill(66);
    rect(0,smooth_centroid*height,width,2);
    */
    
    
     fill(181);
    rect(0, energy*height,width,8);
    
    
    let diff = (energy-base_noise_level);
    
    text(diff_sum,22,22);
    
    
    // if there is something above hyteresis > add up to sum!
    if(diff>0){diff_sum +=diff*.6;}
     
    if( speak_trickle<.1 ){
    
          if(diff_sum<0.03 && is_speaking ){

           // console.log("SILENTIO");
            is_speaking = false;

          //  console.log(smooth_centroid);
            newTalk();

          }
    }// endif speaktricke
    
    if(diff_sum>.3 && is_speaking == false){
    
      console.log("KICKIN");
      is_speaking = true;
    
    }
    
    
    speak_trickle *=.99; // avoid 
    
    diff_sum *=.93;
    
    
     fill(2);
     rect(0,diff_sum*height,width,8);
    fill("#FF1111"); 
    rect(0,base_noise_level*height,width,2);
    
    prev_energy = energy;
     
    
  }else{
  background(120);
  }  
}


function startToTalk(){

  userStartAudio();
  listening = true;
  mic.start();

}