0
How do I connect the audio input from the microphone to the Audiocontext API’s Analyser to get the frequency in Hertz?
Javascript:
let display = document.getElementById("display");
let ctx = new AudioContext();
var analyser = ctx.createAnalyser();
analyser.connect(ctx.destination);
navigator.mediaDevices
/* Solicita permissão de acesso */
.getUserMedia({ audio: { echoCancellation: true } })
/* Cria um fonte de stream */
.then(stream => {
let source = ctx.createMediaStreamSource(stream);
source.connect(analyser);
display.innerText = analyser.getFloatFrequencyData()[0];
})
.catch(err => {
console.log("Microfone negado");
});
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API JS</title>
</head>
<body>
<!-- <audio id="playerAudio" src="" autoplay controls></audio> -->
<p id="display">0</p>
<script src="/script.js"></script>
</body>
</html>
I’m not able to do this work, if I connect the variable stream
of then()
directly to an audio object (with bausa and mute control...) I can hear at the same time I record, but I cannot intercept with the Analyser to get the Hertz audio.