This is working when using getUserMedia as input. I’m trying to modify it to use use mp3 as source instead of getUserMedia, but I’m not too familiar with the Web Audio API so I’m missing something and getting Uncaught TypeError: Failed to execute 'connect' on 'AudioNode': Overload resolution failed.
AudioAnalyzer.prototype.initWithoutStream = function () {
/// use mp3 as source instead of browser microphone
var audio = new Audio();
audio.src = '/try.mp3';
audio.controls = true;
audio.autoplay = true;
document.body.appendChild(audio);
////
const audioCtx = new (
window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.msAudioContext)();
this.gain = audioCtx.createGain();
this.gain.gain.value = 70.;
const lowPass = audioCtx.createBiquadFilter();
lowPass.type = "lowpass";
lowPass.frequency.value = 1000;
const highPass = audioCtx.createBiquadFilter();
highPass.type = "highpass";
highPass.frequency.value = 20000;
this.analyzer = audioCtx.createAnalyser();
this.analyzer.fftSize = this.FFT_SIZE;
// Connecting analyzer to Audio mp3 source causes an error
// const mediaStreamSource = audioCtx.createMediaStreamSource(stream);
var source = audioCtx.createMediaElementSource(audio);
source.connect(this.analyser); // ERROR
this.analyser.connect(audioCtx.destination);
////////////////////////
// Node tree
// mediaStreamSource.connect(this.gain);
source.connect(this.gain);
this.gain.connect(lowPass);
this.gain.connect(highPass);
lowPass.connect(this.analyzer);
highPass.connect(this.analyzer);
this.reset();
this.audioBuffer = new Uint8Array(this.analyzer.frequencyBinCount);
this.isInit = true;
this.isPulse = true;
};