Controlling volume


#1

Hi
I am putting together a scene that is going to utilise and object that has sound embedded - what I would like to do is to be able to move the object and have the sound get louder or quieter, depending on where the object is.

I’m guessing this can be achieved using a controller and adjusting the states and position etc? If I’m right a simple confirmation would be great to know in advance - but maybe somebody has a better idea of how this might be achieved?

Thanks


#2

Hi @technology,

Audio nodes don’t have a position property so this solution may not work.

Alternatively, you could use the position value returned by your object and set up calculations that would adjust the audio node’s volume property accordingly.

Hope this helps.

All the best,
Seb


#3

Thanks - what might be easier is just having buttons that play sound files - but how do I do this?


#4

You can create a button by setting up a pointerdown script on a node. You can then call the start() function on your Z.Audio node.

Hope this helps.

All the best,
Seb


#5

Ok but how, that doesn’t mean that much to me unfortunately


#6

Would this work?

parent.on(“pointerdown”, (e) => {
// Runs when pointerdown occurs on the parent node
// The argument e contains useful info about this event:
// https://docs.zap.works/studio/scripting/reference/object/events/pointerdown/

const Audio_2 = symbol.nodes.Audio_2;

symbol.controllers.Audio.elements.Play.on("active", () => {
	// Runs when active occurs on the Play element
	// https://docs.zap.works/studio/scripting/reference/element/events/active/

});

});


#7

You have the right idea with pointerdown event handler.

I’d move the variable declaration const Audio_2... above the pointerdown however, so that it’s accessible from other areas of your script.

Then, within the actual pointerdown event handler use the start() function mentioned above.

A basic example would look like this:

const Audio_2 = symbol.nodes.Audio_2;

parent.on("pointerdown", () => {
   Audio_2.start();
});

Feel free to check out our scripting documentation for more information on using Studio’s Typescript capabilities.

All the best,
Seb


#8

Excellent, that works, thanks


#9

Ok - so if I have 4 buttons that each play a different sound, how can I avoid them all playing over each other (other than putting a dsclaimer saying don’t push all the buttons at once)?