I am totally a non-musical person and I have no idea how to find a solution for this. I have a simple CSS animation that I want to fire via Javascript every time a thunder (peak of the sound bar) is detected in the audio playing in the background.
I have found all sorts guides with javascript regarding audio sync, but they all relate to creating global audio bars for the whole background noise, while I just want for one particular one.
CSS Animation:
<style>
div {
width: 100px;
height: 100px;
background: red;
position: relative;
animation: mymove 5s 1 ease-in-out;
}
@keyframes mymove {
from {top: 0px;}
to {top: 200px;}
}
</style>
<div></div>
<script>
window.onload = function() {
new Audio("./070115-glorious-early-morning-t-storm-19045.mp3").play();
};
</script>
Audio Track: https://pixabay.com/sound-effects/070115-glorious-early-morning-t-storm-19045/
Thank you!
2
Answers
Using the
AudioContent
you can analyze the audio data in real-time byAnalyserNode
withaudioContext.createAnalyser();
, where you check if the threshold of the audio ( at this moment ) exceeds a specific value, then run the animation.To do this, you’ll need to analyze the audio data in real-time to detect peaks and then trigger the CSS animation, which you can do with AudioContext. I have
console.log()
your Peak, which is more than 17. And also according to the new browser policy, the user must first interact with the DOM before playing any Audio element. So that’s Why I have also used click EventListener to make it work.Styling part:
HTML part:
JavaScript part:
Hope it helps.