A basic HTML5 audio element with a horizontal meter
The web audio API context is loading.
<p>The web audio API context is <span id="ctx-status">loading</span>. <button id="ctx-button">Loading</button></p>
<audio id="the-audio" preload="metadata" crossorigin="anonymous" controls="controls">
<source src="https://assets.rpy.xyz/testmedia/semper_fidelis.mp3" type="audio/mpeg" />
</audio>
<div id="peak-meter" style="height: 80px"></div>
const audioCtx = new AudioContext();
const audioElement = document.getElementById('the-audio');
const meterElement = document.getElementById('peak-meter');
const sourceNode = audioCtx.createMediaElementSource(audioElement);
sourceNode.connect(audioCtx.destination);
const ctxStatus = document.getElementById('ctx-status');
const buttonElement = document.getElementById('ctx-button');
function updateAudioCtxStatus() {
ctxStatus.innerText = audioCtx.state;
if (audioCtx.state === 'suspended') {
buttonElement.innerText = 'Resume';
} else {
buttonElement.innerText = 'Suspend';
}
}
setInterval(updateAudioCtxStatus, 1000);
buttonElement.addEventListener('click', () => {
if (audioCtx.state === 'suspended') {
audioCtx.resume().then(updateAudioCtxStatus);
} else {
audioCtx.suspend().then(updateAudioCtxStatus);
}
});
const test = new webAudioPeakMeter.WebAudioPeakMeter(sourceNode, meterElement);