Playing Background Ambient Sounds

So, I’d like to have ambient background sounds for my rooms, from the very beginning of my project, as they boost my own imagination too!

I found an extension by Daniel Stelzer from github that allows me to loop sounds instead of just playing them, and it seems to work just fine:

[code]Include Music by Daniel Stelzer.

Sound of television is the file “television.ogg”.
Sound of rain is the file “rain.ogg”.


.

Before going to the Living Room:
loop sound of television;[/code]

However, sometimes adjacent rooms have the same sound, and I don’t want to restart the sound if it is already playing.

So I need to have some kind of variable “currently playing sound”, which I set upon playing, and check it against the new sound to play, ideally wrapped into some kind of a “function”.

I’m stuck at phase one, since “Sound currently playing is a file that varies.” or something similar, is not accepted.

I’m a beginner so I can’t grasp the nuances yet of how to translate this into actual Inform 7 code. Perhaps someone else has ran into this same issue, and has the code for this (or can wrap it up quickly)?

Does:

Sound currently playing is a text that varies.

work?

Yes I had “a text” in mind, but can I obtain the textual representation (filename) from “a file”?

The phrase you’re looking for is “a sound name that varies”. This isn’t documented very well.

[code]The currently playing sound is a sound name that varies.

To play (the music - a sound name) without restarting:
unless the currently playing sound is the music:
now the currently playing sound is the music;
loop the music.[/code]

Note that there’s no equivalent of “nothing” for sound names, so the “currently playing sound” will start out with some actual value. To avoid bugs, I’d recommend setting it to some sound that will not be playing in your first room. Alternately, you can use a little bit of I6 hackery.

To decide which sound name is the/an/-- invalid sound: (- (-1) -). When play begins: now the currently playing sound is an invalid sound.

Works like a charm, thanks!

No problem! If you want a nice fade-in, you’ll also want to “introduce the music” instead of “loop the music”, and potentially add some checks in case players move too quickly.

That sounds great (literally) but I’m not sure how to add those checks in… If I indeed, move too quickly, the wrong sound ends up playing.

Here’s the simplest way to do it.

To safely introduce (the music - a sound name):
    unless a crossfade is in progress:
        introduce the music;
    otherwise:
        loop the music on the main music channel.

This will make the new music interrupt any fade that’s currently in progress. A bit messy, but it means you’ll always be hearing the right music.

Great, I don’t need anything more sophisticated. (I had to modify “crossfade is in process” --> “crossfade flag is true” to make it compile)

Sorry, that was an autocorrect error: should have been “crossfade is in progress”, not “process”. But that’s completely equivalent to “crossfade flag is true” so go with whichever is easier to read.