I was getting into p5js and I was using the loadSound function to load an audio and play the audio on mouse click and it works fine. What I wanted to do was take it up a notch and load sound of every audio output playing in the tab that p5js is running and print out the amp values. To explain it a bit more for example let's say I have embedded a youtube video in the same site I have loaded my p5js, is there a way to get the audio playing in the youtube video and insert it in the loadSound function of the p5js and print out the amp variables. Not this is not only for a youtube embed but also every audio its playing in the same window. Comment if you have any questions. Any help is appreciated. Thanks in advance.
var song;
var fft;
function preload() {
song = loadSound('') // load the sound here inorder to be processed with p5js
}
var elem = document.getElementById("audioVisCanvas");
var width = window.getComputedStyle(elem, null).getPropertyValue("width");
var height = window.getComputedStyle(elem, null).getPropertyValue("height");
newWidth = width.replace('px', '');
newHeight = height.replace('px', '');
console.log(newWidth, newHeight)
function setup() {
var cnv = createCanvas(newWidth, newHeight);
cnv.parent("audioVisCanvas");
fft = new p5.FFT()
noLoop()
}
function draw() {
background(0);
stroke(255)
strokeWeight(3)
noFill()
fft.analyze()
amp = fft.getEnergy(20, 200)
console.log(amp) //console.log the amp variables of the audio that is playing ("in this case the youtube embeded video")
}
//start the youtube embed here
function mouseClicked() {
if (song.isPlaying()) {
song.pause()
noLoop()
} else {
song.play()
loop()
}
}
#audioVisCanvas {
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
}
<script defer src=https://cdn.JsDelivr.net/npm/p5></script>
<script defer src=https://cdn.JsDelivr.net/npm/p5/lib/addons/p5.dom.min.js></script>
<script defer src=https://cdn.JsDelivr.net/npm/p5/lib/addons/p5.sound.min.js></script>
<!--get the audio data and load it on p5js-->
<iframe width="1014" height="570" src="https://www.youtube.com/embed/JZjAg6fK-BQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<div id="audioVisCanvas"></div>