How to mute all sound

I want to have a button at the footer or the header to mute and unmute all sound. I have seen in chapbook’s guide that i should set: “sound.mute” to true, but i cant make it work. Where should i place it? At the vars section? As an insert?
Is it exposed in javascript?

1 Like

Did you take a peek at the Cookbook example of interacting with a sound while it plays?

https://twinery.org/cookbook/audio/chapbook/chapbook_audio.html

Muting sounds like it should work globally across all sounds being played, but it might have to be applied to each sound variable that could exist though. I’m not sure at the moment. However, even if that was the case, because all sounds exist off the sound object, we can find any children that exist, but I’m hoping that we can apply mute to the parent sound object itself.

Anyway, It’ll be a while before I can look into this. In the meantime, you know my thought process though. Let me know if you make any headway. :slight_smile:

Hopefully, someone with more brains than me chimes in soon. :wink:

1 Like

I think the stumbling block is a “mute sound/stop sound” (sound effect or ambient sound in Chapbook) macro in most cases will end the currently playing sounds, but if another sound is cued later it will play until another mute macro is activated.

Also I remember playing with Sugarcube audio and (if I recall correctly) I think that “mute” was different from “stop” in that you could mute a running sound and it would continue actually playing so if it was unmuted later you’d hear a later part of the sound media - it didn’t actually stop the needle on the record/stop the media from playing, it just set the volume of it to 0.

If you’re looking for an option for the player to basically choose “sound yes/no?” usually it will require a variable like $sound=true and every time a sound cue happens it needs to check that variable to decide whether to play or not.

That was one of my favorite AXMA features - within the published UI the player had a permanent “sound off” toggle that systematically just wouldn’t play any audio from the game and it didn’t require checks for every sound.

You might be able to also get away with - if you can set a global default volume level for all sounds and then make sure none of your sounds specify a custom level, you could ostensibly have your “don’t use sound” toggle just set all the game volume to 0 and “use sound” set it to full volume; having the volume muted would run the sounds like normal but not audibly.

There may be a plugin that can do this based on Twine flavor but I don’t know enough about them specifically to be aware of it.

2 Likes

Right on cue. :wink:


I did look into the source code on GitHub and found this link…

chapbook/src/runtime/sound/index.js at main · klembot/chapbook · GitHub

…so it looks like there are some global sound variables to adjust. You should be able to just use sound.mute = true; in JavaScript or sound.mute: true in the var section. Read the documentation in the source file link and all will be revealed. :slight_smile:


Edit: I’m such a moron. It’s right in the guide (Audio):

To set the master sound volume for your story, change the state variable sound.volume to a decimal between 0 and 1. 0 is muted, and 1 is full volume. You can also temporarily mute all sound by setting sound.mute (note the lack of a D at the end) to true. The advantage of using sound.mute is that it allows you to toggle between no sound and a previously-set volume.

1 Like

The recipe to mute all described in the guide doesn’t seem to work (or i dont understand it). And in javascript the sound object contains only properties, no methods. An url property and a playing property for each caches sound. I can modify the .playing property manually but nothing happens (as far as i can see)

1 Like

Chapbook dumps audio into a hidden <div> and references it when needed. I can open the browser Inspector and insert a muted attribute and the sound mutes fine.

<div hidden="true" data-cb-sounds>
     <audio src="drums.mp3" loop></audio>
</div>

We can write code to affect this HTML, but it’s really strange that the built-in mute doesn’t seem to work.

I can mute an ambient sound object (“noise” in this case) directly though with sound.ambient.noise.muted: true in the vars section of another passage. I can also set it in JavaScript with engine.state.set('sound.ambient.noise.muted', true);

I can make a function in the story JavaScript to seek out all audio and video elements:

window.muteAll = function() {
  document.querySelectorAll("video, audio").forEach((elem) => elem.muted = true);
}

…and call it from a JavaScript modifier on another passage:

[javascript]
  muteAll();
[continue]

…and it shuts everything up and sends them to their rooms without supper.

I see logic in the source code that leads me to believe you should be able to mute just the ambient sounds and not just all sounds, for example. Something is not quite right and I think Chapbook has some unfinished/untested audio code.

Obi-wan @klembot Kenobi, you’re our only hope. :wink:

2 Likes

Easy peasy! Thanks Hall! You made my day!!

1 Like