How do you change volume of sound while playing it (preferrably using JS)

I would like to use JS because I play the sound using JS. BTW, is there a JS Snap! manual? Where can I see it?

I dunno anything about sound, but I can answer this part: The closest there is to a program logic manual (that's what we called things like that at IBM, back in the day (everyone my age in the computer business has war stories from IBM)) is a long text comment at the front of each JavaScript source file. The place to start is morphic.js, although as we speak Jens is entirely rewriting Morphic so the existing one is obsolescent.

Oh. So, for now, I can piece through that to find out, but it's useless later? Okay. Thanks!

Nope. Says nothing about sound. Ah, well. Tell @jens to document it please in the new Morphic for me. I'll have to create adaptive music later. Thanks for the help. I'll keep reading through Morphic for now cause it seems interesting.

No, either I misspoke or you slightly misunderstood.

If you want to understand how Snap! works, you should read the comments in all the source files. Since Snap! is written on top of Morphic, morphic.js is the preferred starting point for that reading; it's hard to make sense of the other files if you don't at least sort-of understand Morphic. When the new Morphic is finished, the details in the current morphic.js will change, but your understanding of Snap! won't be entirely obsolete.

If all you want to do is see how Snap! treats sounds, grep for "sound" in the sources!


Is api.js the JS I would look at for the run JS block?

Do you know how to search a group of files for a text? If not, what operating system does your computer use?

The first thing to do is look in objects.js for the block name. You'll find a data structure that will tell you the name of the JS procedure that implements that block. Then you look through all the .js files for that name.

I don't! I didn't know you could do that! Windows 7. Going to update to 8.1 soon. I've been searching every single file for now.

Ah, I was afraid you'd say that. I could tell you how to do it in Linux or MacOS, but I have to guess about Windows, namely, open a Command Prompt (does that still exist?) and try the command

grep the-text-you-want *.js

after steering yourself into the directory with the Snap! source files.

Any Windows users here who can give a better answer?

We have a command prompt, but no grep. I tried it. "There is no batch file of command script" or whatever popped up.

I'm trying to decipher FOR and FINDSTR to make my own GREP. Not going well.

Ya know, I should play around with stuff before I ask questions. (sound).audio.volume was my first try and it worked! Now to write some fading... I will look through that documentation, @bh. Thanks for showing me that.

Hi @snap0maker0,
I don't understand this question. We have blocks to do this, haven't we?

"change volume by" and "set volume to" can change volume dynamically (while playing a sound). Why do you need JS code?

Anyway, here JS code is easy. Every sprite has its own volume and it's directly a property of the sprite (this.volume). But to interact directly with sounds playing, just use the function this.setVolume(vol).


Hi @jguille2.
I need JS. I played with JS, and I don't want to track something I did in JS and blocks, juggling the two. I also need to use JS to play it because the sound will be played in sync with another. My sound is also two channels. JS code is the same for one or two channels, but working with that list in blocks is weird.

Then, you can use this.setVolume(volume) to do it. But it depends on the code you have used to play sounds. If you are using pure JS (creating an audioContext aside Snap! morphic structures) then you don't need Snap! functions, just JS docs.


Thanks! I use some Timeout and a for loop to make a simple fade in. Now I can get some crossfades and finish this adaptive audio project.

Alternatively, after loading Snap! in your browser (Chrome, FireFox, or Edge) then open the developer tools and use the search tab.

Is this topic yet another candidate to be moved to the new AT category?