From 48fc56102e0095b00ab7ed41d5a12473dcbe1ea8 Mon Sep 17 00:00:00 2001 From: Lewis Moten Date: Wed, 1 May 2024 17:37:49 -0400 Subject: [PATCH] improve sample rate --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6dca49c..7c8ae74 100644 --- a/index.js +++ b/index.js @@ -87,7 +87,8 @@ function handleListeningCheckbox(e) { microphoneStream = stream; microphoneNode = audioContext.createMediaStreamSource(stream); analyser = audioContext.createAnalyser(); - analyser.fftSize = 2 ** 12; + analyser.smoothingTimeConstant = 0; + analyser.fftSize = 2 ** 15; microphoneNode.connect(analyser); requestAnimationFrame(analyzeAudio); } @@ -166,7 +167,7 @@ function evaluateBit(highBits, lowBits) { if(high || low) { const now = performance.now(); if(bitStarted) { - if(now - bitStarted > FREQUENCY_DURATION) { + if(now - bitStarted >= FREQUENCY_DURATION) { received(evaluateBit(bitHighStrength, bitLowStrength)); bitHighStrength.length = 0; bitLowStrength.length = 0; @@ -174,6 +175,8 @@ function evaluateBit(highBits, lowBits) { } } else { bitStarted = now; + bitHighStrength.length = 0; + bitLowStrength.length = 0; } bitHighStrength.push(amplitude(FREQUENCY_HIGH)); bitLowStrength.push(amplitude(FREQUENCY_LOW));