Two amplitude objects listening to two separate loaded sounds

Hi!
I’m trying to figure out a way to have two separate sound files loaded and playing at the same time connect to two separate amplitude objects. At the moment all the sounds just go straight to the first amplitude object declared and the p5.Sound connect function that should be able to link them separately doesn’t have enough documentation or examples.
Do any of you have experience figuring this stuff out?

Here is a simplified version of my code:


let sound1;
let sound2;

var sound1_Amp;
var sound2_Amp;

var sound1_Vol;
var sound2_Vol;

function preload(){
    sound1 = loadSound ('assets/sound-1.mp3');
    sound2 = loadSound('assets/sound-2.mp3');
}

function setup() {

    sound1_Amp = new p5.Amplitude();
    sound1.connect(sound1_Amp);

    sound2_Amp = new p5.Amplitude();
    sound2.connect(sound2_Amp);
    
    createCanvas(windowWidth, windowHeight);
}

function draw() {
    background(0);

    sound1_Vol = sound1_Amp.getLevel();
    sound2_Vol = sound2_Amp.getLevel();
    
    fill(255, 0, 0);
    ellipse(100, 100, 50 + sound1_Vol*500);

    fill(0, 255,0);
    ellipse(400, 100, 50 + sound2_Vol*500);
}

Thanks for reading, any help would be greatly appreciated!

1 Like

you need to use setInput
https://editor.p5js.org/micuat/sketches/SEmDwkB4PQ
(by the way it works on chrome but doesn’t seem to work on firefox)

described here
https://p5js.org/reference/#/p5.Amplitude

(I personally found p5.sound a bit confusing because they take care of “everything” so unconnected nodes are automatically connected to the master out or amplitude this case, which is sometimes problematic. You can also use tone.js, for example, as an alternative)

3 Likes

oh my gosh thank you so much for the super quick response! :hatched_chick:

no problem, also I found the firefox bug is reported here but not solved yet

by the way I recommend posting a link to web editor if you have questions (also when you open an issue on github) so others don’t have to create their own sketch!

2 Likes