Trying to change font weight based on volume of sound input

sorry this is my first time posting, not sure if I’m doing this right. I’m trying to use the number of the sound input to select a font from an array but it won’t change the font even though it says the variable is changing.

Here’s my code:

var myRec = new p5.SpeechRec(‘en-US’, parseResult); // new P5.SpeechRec object
myRec.continuous = true; // do continuous recognition
myRec.interimResults = true; // allow partial recognition (faster, less accurate)

let mic;
let v = 0;
let font = [‘SpaceCowgirl-Thin.otf’,‘SpaceCowgirl-Light.otf’,‘SpaceCowgirl-Regular.otf’, ‘SpaceCowgirl-Medium.otf’, ‘SpaceCowgirl-Bold.otf’, ‘SpaceCowgirl-Black.otf’];
let f=0;
let t=‘SpaceCowgirl-Bold.otf’;

// function preload() {
// myFont= loadFont(t);
// }
function setup() {
let cnv = createCanvas(windowWidth, windowHeight);
cnv.mousePressed(userStartAudio);
myRec.onResult = parseResult; // now in the constructor
myRec.start(); // start engine
background(220);
textSize(100);

fill(255,0,0);
mic = new p5.AudioIn();
mic.start();
myFont= loadFont(t);
}

function haw() {

// background(220);
t= font[f];
micLevel = mic.getLevel();
v = micLevel*10000;
console.log(‘v =’,v);
console.log(‘mic =’,micLevel);

if(2>v>0) f=0;
else if(3>v>2) f=1;
else if(4>v>3) f=2;
else if(5>v>4) f=3;
else if(6>v>5) f=4;
else if(v>6) f=5;

textFont(myFont);
text(“HAW”,mouseX,mouseY);
console.log('font ',myFont);
console.log(‘f=’,f);
console.log(‘t=’,t);
}

function parseResult()
{
// recognition system will often append words into phrases.
// so hack here is to only use the last word:
var mostrecentword = myRec.resultString.split(’ ').pop();
if(mostrecentword.indexOf(“yee”)!==-1) { haw(); }
else if(mostrecentword.indexOf(“ye”)!==-1) { haw(); }
else if(mostrecentword.indexOf(“you”)!==-1) { haw(); } else if(mostrecentword.indexOf(“yi”)!==-1) { haw();}
else if(mostrecentword.indexOf(“yeet”)!==-1) { haw();}
console.log(mostrecentword);
}

hi @frogyolk,

first please follow this instruction to format-your-code
Second you are not changing the font in your code, only using the one from setup.

Cheers
— mnse

Hi @frogyolk. To format your code, edit your message, select your whole code, and press the </> button, like so:

ezgif.com-gif-maker (1)

3 Likes