Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: bx83 on Tue 15/05/2018 13:18:19

Title: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Tue 15/05/2018 13:18:19
Should I start music in Load or Afterfadein?
Is there a manual page with a list of things that are better off in each function?

It comes from the yet-again dreaded 'how do I transition between two music tracks of the same lengths' question.

BEACH is the beach, the first room, enters after the player starts the game.
CAVE is a cave to the left of the BEACH.

Both music tracks aBeachTheme and aBeachTheme_cave are the same length; they're the same *song* - aBeachTheme_cave have a low volume and 'muted' sound (beccause it's, you know, in a cave).
Both tracks are .OGG format
The Types->Music has a MaxChannels of 1 (so it will always smoothly transition as a new room is entered, and that areas music is played).

BEACH:

function room_FirstLoad()
{
    SetGameOption(OPT_CROSSFADEMUSIC, 1);
    music_chan=aBeachTheme.Play(eAudioPriorityNormal, eRepeat);
    sounds_chan=aOcean_crash.Play(eAudioPriorityNormal, eRepeat);
}

function room_AfterFadeIn()
{
if (cJulius.PreviousRoom == CAVE) { //cave
aWater_dripping.Stop();
cJulius.Walk(265, 500, eNoBlock, eWalkableAreas);
}


if (cJulius.PreviousRoom==CAVE || cJulius.PreviousRoom==FARM) {
beachThemePos=music_chan.Position;
music_chan=aBeachTheme.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
}
}


CAVE:

function room_Load()
{
if (cJulius.PreviousRoom == 2) {
beachThemePos=music_chan.Position;
music_chan=aBeachTheme_cave.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
}
}


HOWEVER - the CAVE and BEACH background music intermiddently stops, upon entering the room. It doesn't crossfade to nothing; just stops. This can happen the 6th or 7th time I enter, or the 2nd. Random.
What's odd is, when the music *is* working again, it will transition back to it at a different position to where I left off. So, it almost as if the beachThemePos is updating with each successive entry; but when it tried to play the music, a random bug hits, and the playing doesn't go.

Bonus question: which runs first, the Room_Load function, or the Room_FirstLoad function?
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Tue 15/05/2018 13:56:33
Sorry, I do not have time to answer the main question right now, but I can comment on this -

Quote from: bx83 on Tue 15/05/2018 13:18:19
Bonus question: which runs first, the Room_Load function, or the Room_FirstLoad function?

"First time enters room" event creates "Room_FirstLoad" function name by default, but that's a misleading name (and to think of it, should be fixed), because in reality it is "Room first time After Fade-in".
The order of their running is following:

* "Enters room before fade-in" (Room_Load)
* room transition effect
* "First time enters room" (Room_FirstLoad) <--- only if the room is entered for the first time
* "Enters room after fade-in" (Room_AfterFadeIn)

Manual page: http://www.adventuregamestudio.co.uk/manual/ags83.htm#topic78
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: morganw on Tue 15/05/2018 14:01:04
I think you are referencing a different channel sometimes, because you are storing the channel pointer in room_FirstLoad(), and then using it a later when the music may be on another channel. Setting max channels to 1 doesn't guarantee you are using the same channel again and again, just that (any) one channel will be available to use.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Tue 15/05/2018 14:08:08
Quote from: morganw on Tue 15/05/2018 14:01:04Setting max channels to 1 doesn't guarantee you are using the same channel again and again, just that (any) one channel will be available to use.

I think it does, Snarky did testing before which showed that. It appeared that "max channels" property is also acting as "reserved channels".

Also I've been checking the engine code now (https://github.com/adventuregamestudio/ags/blob/master/Engine/media/audio/audio.cpp#L135), and when AGS looks for a free channel for clip of type X, it always skips channels reserved for the particular types, so that might be true.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: morganw on Tue 15/05/2018 14:09:37
I don't think you can stay on the same channel if cross-fading, since you are playing two streams at once.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Tue 15/05/2018 14:11:49
Quote from: morganw on Tue 15/05/2018 14:09:37
I don't think you can stay on the same channel if cross-fading, since you are playing two streams at once.

There is a special channel for crossfading, it is separate from other channels. Iirc when crossfading starts, the second track is placed on a special channel, and when crossfade ends, it is moved to the normal channel previously occupied by the first track (https://github.com/adventuregamestudio/ags/blob/master/Engine/media/audio/audio.cpp#L939).
Probably this is also why only one crossfading may happen at once using built-in AGS functions.

PS. BTW, what is interesting, from the looks of the code I linked above, OPT_CROSSFADEMUSIC will work properly only if the music clip has built-in type "Music", and its channel is strictly id = 2.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: morganw on Tue 15/05/2018 14:48:35
I think to keep the the script pointer pointing at what you asked to be played (so you can set its volume etc), it would have to move what is playing to the crossfade channel, and then starting playing what is faded in on the (hopefully) original channel. So perhaps this channel is always id 2?
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Tue 15/05/2018 15:02:42
Quote from: morganw on Tue 15/05/2018 14:48:35
I think to keep the the script pointer pointing at what you asked to be played (as so you can set its volume etc), it would have to move what is playing to the crossfade channel, and then starting playing what is faded in on the (hopefully) original channel. So perhaps this channel is always id 2?

EDIT: Alright, all the following is not relevant to AudioChannels and AudioClips, I was looking into the old audio system instead.


From the code it looks like it does opposite: it keeps old music in its channel, and puts new music on crossfade channel.
https://github.com/adventuregamestudio/ags/blob/master/Engine/media/audio/audio.cpp#L1004
Then, when the old music is done fading out, it moves the new playing clip from crossfade channel to music channel.
https://github.com/adventuregamestudio/ags/blob/master/Engine/media/audio/audio.cpp#L939



But then, if it is so, this means that you cannot rely on AudioChannel returned in script at all when you have OPT_CROSSFADEMUSIC option set, because it may be either music channel (if no music is playing currently), or Crossfade channel (if some music was playing and crossfade started).

By "cannot rely" I mean that you cannot store it as "music_chan" and use it in the future. You may still use it to set volume etc though, but only immediately. Because if that was crossfade channel, then the clip will be soon removed from it.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: morganw on Tue 15/05/2018 17:14:48
bx83, could you try defining a global audio channel and use that across both rooms? That should rule out pointing to the wrong channel as the problem.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Tue 15/05/2018 19:52:36
Quote from: Crimson Wizard on Tue 15/05/2018 15:02:42
Quote from: morganw on Tue 15/05/2018 14:48:35
I think to keep the the script pointer pointing at what you asked to be played (as so you can set its volume etc), it would have to move what is playing to the crossfade channel, and then starting playing what is faded in on the (hopefully) original channel. So perhaps this channel is always id 2?

From the code it looks like it does opposite: it keeps old music in its channel, and puts new music on crossfade channel.


No, I was wrong. Did not understand the code properly perhaps, or maybe there is more than the code I saw.
It actually does what you said: always returns channel 2 from AudioClip.Play, and that channel has requested music in PlayingClip.

EDIT: Oh, the code I found previously was related to old audio system. The new audio system is doing this instead:
https://github.com/adventuregamestudio/ags/blob/master/Engine/media/audio/audio.cpp#L91

My test script:
Spoiler

Code (ags) Select

AudioChannel *mus_chan;
bool a;

function room_AfterFadeIn()
{
SetGameOption(OPT_CROSSFADEMUSIC, 1);
}

function on_key_press(eKeyCode key)
{
if (key == eKeySpace)
{
if (a)
mus_chan = aMusic1.Play();
else
mus_chan = aMusic2.Play();

int order;
if (a)
order = 1;
else
order = 2;

int playid;
if (mus_chan.PlayingClip == aMusic1)
playid = 1;
else
playid = 2;

player.SayBackground(String.Format("Ordered music = %d[mus_chan.ID = %d[music on chan = %d",
order,
mus_chan.ID,
playid
));
a = !a;
}
}

[close]

This is very good, otherwise it would be impossible to work with.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Wed 16/05/2018 03:43:20
Quote from: morganw on Tue 15/05/2018 17:14:48
bx83, could you try defining a global audio channel and use that across both rooms? That should rule out pointing to the wrong channel as the problem.

It already is:

(http://redrom.ltd/img/musicchan.png)
(http://redrom.ltd/img/soundchan.png)
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Wed 16/05/2018 05:43:24
I've now altered my code to just get rid off FirstLoad for simplicities sake.
I think I've discovered the error, but I don't know how to fix it.

When I run this code, all runs fine, no intermitent stops -- though, there are intermittent volume changes for music and sound. I haven't touched .Volume for any of these variables.
Sometimes, CAVE resets the music/sound volume for BEACH; but I can't see why CAVE is more 'stable'.

The default volume for both music and sounds was set to 100% inherited for both; just to make sure, I then changed it to 100% for both non-inherited. No difference.
They have 95 (music) and 99 (sounds) ReductionWhileSpeechPlaying

My code:

BEACH:

function room_Load()
{
if (GameStarted) {
beachThemePos=music_chan.Position;
oceanThemePos=sounds_chan.Position;
music_chan=aBeachTheme.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
sounds_chan=aOcean_crash.PlayFrom(oceanThemePos, eAudioPriorityNormal, eRepeat);
} else {
            SetGameOption(OPT_CROSSFADEMUSIC, 1);
    if (FutureTime) {
music_chan.Stop();
    } else {
        music_chan=aBeachTheme.Play(eAudioPriorityNormal, eRepeat);
        beachThemePos=music_chan.Position;
    }

            sounds_chan=aOcean_crash.Play(eAudioPriorityNormal, eRepeat);
    oceanThemePos=sounds_chan.Position;

    cJulius.x=436;
    cJulius.y=537;
    GameStarted=true;
}
}

function room_FirstLoad()
{
}

function room_AfterFadeIn()
{
if (cJulius.PreviousRoom == CAVE) { //cave
aWater_dripping.Stop();
cJulius.Walk(265, 500, eNoBlock, eWalkableAreas);
}
}


CAVE:

function room_Load()
{
if (FutureTime) {
    music_chan.Stop();
} else {
    beachThemePos=music_chan.Position;
    music_chan=aBeachTheme_cave.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
}

sounds_chan=aWater_dripping.Play(eAudioPriorityNormal, eRepeat);
}

function room_AfterFadeIn()
{
}


I ran a test to see which audiochannel ID and Volume were being used, by placing this immmediately in AfterFadein after each time music_chan or sounds_chan was invoked:
Display("music channel ID %d, sounds channel ID %d, volumes are %d and %d respectively",music_chan.ID, sounds_chan.ID,  music_chan.Volume, sounds_chan.Volume);

Sounds volume I decided to ignore, as there's a MaxChannel of 5 for sounds, so each test of sounds_chan.Volume got a different clip each time (I think? Anyway the channel bobbed around from 1 to 5 each room tresnition).

They were reporting channel ID 1 for music every time; and a volume of 6, for music.
This is a very low %, an didn't change; but the actual sound colming out of the speaker ranged from ~5 to 100 each transition.

Nothing was done to volume by me.
I changed the music_chan.Volume to 100 each room transition; this seemed to fix it.

Pointer variable corrupt/changing position in memory and reading garbage? Why only this room, though?
Or have I missed something? (feels like something huge.... :/)

This is a mess, but I don't know how or why. Once again, I'm happy to pay like US$1,000 to anyone who can debug the sound/music system. The above code is simplified, and leave out a lot of walking, animation commands, and etc. that's too complex to include here.

I mean, I don't want to beg, but this one simple 'OceanThemePos' has really screwed things up for me. I've tried like 50 different versions of the code, then always, *something* goes wrong at random times.
If you want me to include all code, unaltered, *and* a video, I will; or send you the code for the game as PM (CrimsonWizard and Snarky - or anyone else I could trust).

Truly, I'm at my wits end.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Wed 16/05/2018 07:01:31
Is there a way to reference channels in the style of Audiochannel* channel[1] or channel[5] or what have you?

I don't think I understand (stil!) the AudioChannel system.
For example, if your Type:Music has MaxChannels of 1, and Crossfade is set to MediumFade, can I just type:

aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat);

or must it be:

music_chan=aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat);

???

And if I want to find an AudioClip that's being player, ie.

if (sounds_chan.PlayingClip!=aOcean_crash_cave) {
    sounds_chan = aOcean_crash_cave.Play;
}

I should do the above to stop it playing twice, or three times, or 15 times etc?

There's no other way to check if an actual audioclip, referenced by it's name, is playing or not -- is this because you need the audiochannel pointer to find a specific audioclip; or it just grab's the clip's name from a list of playing tracks, and then returns a pointer to you?

I'm really very confused about thier functioning :/
For example, in the code above, would things work the same or better if I just made it:
aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat);
rather than:
music_chan=aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat);

???
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Wed 16/05/2018 07:03:44
The obvious suspicion is that it has to do with the VolumeReductionWhileSpeechPlaying. Try changing that to 0 to see if the problem goes away. I do remember I had to fix something about this bit of code in the cut-down project builds you sent me for the SpeechBubble development, but I don't remember exactly what I did, and it might have to do with some resources you removed (I don't think it even compiled when I got it).
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Wed 16/05/2018 07:32:07
Quote from: bx83 on Wed 16/05/2018 07:01:31
Is there a way to reference channels in the style of Audiochannel* channel[1] or channel[5] or what have you?

Yes. System.AudioChannels[i]

QuoteI don't think I understand (stil!) the AudioChannel system.

The way I like to think of it, the AudioChannels are like 8 record players. Each can play one record (AudioClip) at a time. You can ask to have some number of the record players (AudioChannels) assigned to a particular type of record (AudioType), and in that case, it reserves that many of them permanently (meaning those particular record players are only used for that type of record, and records of that type are always played on one of those particular players). When you play a record, it tells you which player it picked to play it, in case you want to adjust the volume, stop it, or whatnot.

QuoteFor example, if your Type:Music has MaxChannels of 1, and Crossfade is set to MediumFade, can I just type:

aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat);

or must it be:

music_chan=aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat);

???

You don't ever HAVE to store the audio channel pointer, and apart from that the two commands do exactly the same thing. It's only worth doing in case you want to either (1) check whether the clip actually started playing (if it fails for some reason, the channel it returns will be null), or (2) do something with the channel while the clip is playing (like adjust the volume).

If you're playing a music clip and you only have one music channel, the AudioChannel pointer should always be the same (contrary to what morganw says), so if you set it once you don't really ever need to do it again. (Though there are some edge-cases that I think could potentially cause problems, like restoring a save game with different audio settings in winsetup.)

QuoteAnd if I want to find an AudioClip that's being player, ie.

if (sounds_chan.PlayingClip!=aOcean_crash_cave) {
    sounds_chan = aOcean_crash_cave.Play;
}

I should do the above to stop it playing twice, or three times, or 15 times etc?

Huh? Have you got sounds set to only 1 channel? Otherwise the code above doesn't make much sense. Why would the clip play more than once in the first place?

QuoteThere's no other way to check if an actual audioclip, referenced by it's name, is playing or not -- is this because you need the audiochannel pointer to find a specific audioclip; or it just grab's the clip's name from a list of playing tracks, and then returns a pointer to you?

I'm really very confused about thier functioning :/

There's no built-in function to do it, but it's easy enough to loop through and check each AudioChannel.

QuoteFor example, in the code above, would things work the same or better if I just made it:
aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat);
rather than:
music_chan=aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat);

Work better in what way? It all depends on what you want to do and the rest of the code around it.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Wed 16/05/2018 08:46:01
Quote from: Snarky on Wed 16/05/2018 07:32:07
QuoteAnd if I want to find an AudioClip that's being player, ie.

if (sounds_chan.PlayingClip!=aOcean_crash_cave) {
    sounds_chan = aOcean_crash_cave.Play;
}

I should do the above to stop it playing twice, or three times, or 15 times etc?

Huh? Have you got sounds set to only 1 channel? Otherwise the code above doesn't make much sense. Why would the clip play more than once in the first place?

If I have 5 sound channels, then channel 1, 2, 3, etc can be taken up by aOcean_crash_cave. Or is the audioclip unique?

Okay well I need to do one of the following:
a) make maxaudiochannels 1 for both music and sounds; or
b) have a function which finds and then stops an audioclip for a given name

But I'm really warn out now, so I think I'll go off and kill myself :P

The mists of Audiochannel are beginning to clear, but it's still quite confusing. I mean, why the hell isn't it easy to just say: 'you know that audio clip you were just playing? well crossfade it into this one.'
or 'you know those sound effect clips you were just playing? well just cross-fade them nothing, and then start up these new ones.'
AND, at the default volume, etc.

I'll try the VolumeReductionWhileSpeechPlaying to 0 and see if that changes anything.

I've run into yet more problems crossfading to sounds and music clips for no known reason. They just immediately jump into the new track (and .Stop() the old one); or sometimes not! And they just never play.
Plus, this changes depending on whether I do a Display() of the audiochannel ID, volume etc; or not. So it seems to take some time to do the transition.... but not for the other rooms.

ps: is AudioClip.Stop() just a way of stopping and AudioClip cold, or does it crossfade to nothing? Not that this seems to have any relationship to what actually happens, but I was just wondering the correct version.

I'm not trying to be obnoxious, I'm really not, but this is getting self destroying. I mean I will literally pay you money to debug these rooms for me.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Wed 16/05/2018 09:20:46
Quote from: bx83 on Wed 16/05/2018 08:46:01
If I have 5 sound channels, then channel 1, 2, 3, etc can be taken up by aOcean_crash_cave. Or is the audioclip unique?

Well, it is unique, but it can be playing on multiple channels at the same time. Just like "a particular sprite" is unique, but can be assigned to different objects or character loops and displayed simultaneously.

My question was more about why it would be playing on multiple channels simultaneously in the first place. I guess if there's a long "tail" to the sound I can see having two waves playing simultaneously, but with a regularly repeating sound like a wave I would expect you to have pretty tight control over it (you wouldn't be randomly triggering wave sounds at lots of different points in the code, I hope).

QuoteOkay well I need to do one of the following:
a) make maxaudiochannels 1 for both music and sounds; or

Do you want to be able to play multiple sounds at the same time? (For example in order to crossfade them?) If yes, don't do this.

Quoteb) have a function which finds and then stops an audioclip for a given name

AudioClip.Stop(). But why do you need this?

QuoteBut I'm really warn out now, so I think I'll go off and kill myself :P

If you don't rush things, but just take the time to figure things step by step (for example with a test game), things are much easier. Trying to get something buggy to work as quickly as possible by just hacking away randomly without quite understanding what's going on is always frustrating. (That's what I had to do for the Awards client towards the end, since I had fatal bugs popping up at the last minute before a hard deadline, and it was intensely stressful and unpleasant.)

QuoteThe mists of Audiochannel are beginning to clear, but it's still quite confusing. I mean, why the hell isn't it easy to just say: 'you know that audio clip you were just playing? well crossfade it into this one.'
or 'you know those sound effect clips you were just playing? well just cross-fade them nothing, and then start up these new ones.'
AND, at the default volume, etc.

Referring to "that audio clip you were just playing" is exactly what the AudioChannel pointer you get from AudioClip.Play() is for. Most common operations you might want to do are not actually particularly difficult, but the way it works is not well explained in the manual, so it can be confusing.

Cross-fading is not that simple an operation in the first place, and cross-fading multiple tracks simultaneously can very easily fail in various ways (particularly because you run out of channels to do the mixing), so it's never going to be trivial to do.

QuoteI've run into yet more problems crossfading to sounds and music clips for no known reason.

"crossfading to sounds and music clips"? These are two entirely different things, though. With music you're using OPT_CROSSFADEMUSIC to do built-in crossfading (using the hidden crossfade channel Crimson Wizard explained). For sounds you have to do the crossfading manually using separate channels.

QuoteThey just immediately jump into the new track (and .Stop() the old one); or sometimes not! And they just never play.
Plus, this changes depending on whether I do a Display() of the audiochannel ID, volume etc; or not. So it seems to take some time to do the transition.... but not for the other rooms.

Can't comment without seeing your code.

Quoteps: is AudioClip.Stop() just a way of stopping and AudioClip cold, or does it crossfade to nothing? Not that this seems to have any relationship to what actually happens, but I was just wondering the correct version.

It just stops it. (Perhaps for a music track with OPT_CROSSFADEMUSIC on it fades it out, but I'm not betting on it.)
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Wed 16/05/2018 09:57:29
Quote from: Snarky on Wed 16/05/2018 09:20:46
Quote from: bx83 on Wed 16/05/2018 08:46:01
If I have 5 sound channels, then channel 1, 2, 3, etc can be taken up by aOcean_crash_cave. Or is the audioclip unique?

Well, it is unique, but it can be playing on multiple channels at the same time. Just like "a particular sprite" is unique, but can be assigned to different objects or character loops and displayed simultaneously.

My question was more about why it would be playing on multiple channels simultaneously in the first place. I guess if there's a long "tail" to the sound I can see having two waves playing simultaneously, but with a regularly repeating sound like a wave I would expect you to have pretty tight control over it (you wouldn't be randomly triggering wave sounds at lots of different points in the code, I hope).

Wave as in ocean wave or sound wave?
Consider:
I have 4 Sound channels (MaxChannels=4). Each time I enter a room, we play an ocean waves sound.
If I don't have the
ff (music_chan.PlayingClip!=aOcean) { aOcean.Play() );
then it will start to fill up the available audio channels with this clip each time I enter the room.

QuoteOkay well I need to do one of the following:
a) make maxaudiochannels 1 for both music and sounds; or

Do you want to be able to play multiple sounds at the same time? (For example in order to crossfade them?) If yes, don't do this.[/quote]

But how will not setting it to 1 allow me to crossfade? That's the opposite.

Quoteb) have a function which finds and then stops an audioclip for a given name

AudioClip.Stop(). But why do you need this?[/quote}

Sorry, meant to say 'crossfade to nothing' not 'stops'.
Quote
QuoteBut I'm really warn out now, so I think I'll go off and kill myself :P

If you don't rush things, but just take the time to figure things step by step (for example with a test game), things are much easier. Trying to get something buggy to work as quickly as possible by just hacking away randomly without quite understanding what's going on is always frustrating. (That's what I had to do for the Awards client towards the end, since I had fatal bugs popping up at the last minute before a hard deadline, and it was intensely stressful and unpleasant.)

Yeah, well I know what you mean :/


QuoteCross-fading is not that simple an operation in the first place, and cross-fading multiple tracks simultaneously can very easily fail in various ways (particularly because you run out of channels to do the mixing), so it's never going to be trivial to do.

Yeah but could it be trivial for just one music track and one sound track?
I get the feeling I'll need to build a function which loops through all the audio channels, crossfades ones I want closed to nothing, and the others to crossfade to new tracks with a Wait(10) pause (using Wait has helped me to solve a few problems recently.)

Quote"crossfading to sounds and music clips"? These are two entirely different things, though. With music you're using OPT_CROSSFADEMUSIC to do built-in crossfading (using the hidden crossfade channel Crimson Wizard explained). For sounds you have to do the crossfading manually using separate channels.

Is the hidden crossfade channel in addition to the MaxChannels we've set up? Or is it just one channel, used for each sound crossfading.
Are they crossfaded simultaneously? One at a time?....

Quote
QuoteThey just immediately jump into the new track (and .Stop() the old one); or sometimes not! And they just never play.
Plus, this changes depending on whether I do a Display() of the audiochannel ID, volume etc; or not. So it seems to take some time to do the transition.... but not for the other rooms.

Can't comment without seeing your code.

I can send you a link to the main project? It will be about 1GB but it will explain everything and allow you to see quickly what's wrong. It really only affects the first 3 rooms, which use the crossfading music/sounds run by the position of each (ie. beachThemePos) in an audiochannel (ie. music_chan).

Quote
Quoteps: is AudioClip.Stop() just a way of stopping and AudioClip cold, or does it crossfade to nothing? Not that this seems to have any relationship to what actually happens, but I was just wondering the correct version.

It just stops it. (Perhaps for a music track with OPT_CROSSFADEMUSIC on it fades it out, but I'm not betting on it.)

Well what about crossfading sound effects?
Sounds and Music both have a 'crossfade tracks' option in the editor, but I can only set it for music (OPT_CROSSFADEMUSIC).
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Wed 16/05/2018 10:57:06
Just so we're using the correct terminology: to "crossfade" is to fade out one track while fading in a different track: their volumes "cross". "Crossfade to nothing" is not a term, it's just called fading out.

Quote from: bx83 on Wed 16/05/2018 09:57:29
Wave as in ocean wave or sound wave?
Consider:
I have 4 Sound channels (MaxChannels=4). Each time I enter a room, we play an ocean waves sound.
If I don't have the
ff (music_chan.PlayingClip!=aOcean) { aOcean.Play() );
then it will start to fill up the available audio channels with this clip each time I enter the room.

What sound type is aOcean? If it's Music, then it won't be a problem because you only have one Music channel and this is what it will play. If it's Sound, then why are you looking at what's playing on music_chan? (Unless music_chan is one of the channels devoted to Sound clips, which would be really bad variable naming.) And if you have 4 sound channels, you can't just check whether it's playing on one particular channel, unless you specifically know that aOcean is playing on that particular channel and no other.

In summary, there's something very wrong with your logic here.

Quote
QuoteDo you want to be able to play multiple sounds at the same time? (For example in order to crossfade them?) If yes, don't do this.

But how will not setting it to 1 allow me to crossfade? That's the opposite.

It won't. That's why I said "If yes, don't do this".

Sorry, I misread. By not setting it to 1 you will have additional channels. You can use these channels to crossfade manually. In order to crossfade sounds, I believe this is the only way to do it.

Quote from: bx83 on Wed 16/05/2018 09:57:29
Quote from: Snarky on Wed 16/05/2018 09:20:46
It just stops it. (Perhaps for a music track with OPT_CROSSFADEMUSIC on it fades it out, but I'm not betting on it.)

Well what about crossfading sound effects?
Sounds and Music both have a 'crossfade tracks' option in the editor, but I can only set it for music (OPT_CROSSFADEMUSIC).

It's not documented, so figuring out how those two things interact would require either inspecting the engine code, or some systematic testing.

One possibility (and I'm JUST SPECULATING here) is that OPT_CROSSFADEMUSIC (which predates the new audio system) actually applies to all audio types that have have "crossfade tracks" set to true. However, there is still only one (hidden) channel for the built-in crossfading, so it will only be able to crossfade one clip at a time. If you try to use these settings to crossfade multiple clips simultaneously (both music and an audio clip, or two audio clips), things will go wrong. That could certainly explain some of the things you're experiencing (clips not playing, or clips stopping immediately rather than fading out).
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Wed 16/05/2018 11:04:21
Quote from: bx83 on Wed 16/05/2018 09:57:29
Well what about crossfading sound effects?
Sounds and Music both have a 'crossfade tracks' option in the editor, but I can only set it for music (OPT_CROSSFADEMUSIC).

Alright, this needs clarification.

There are two audio systems in the AGS, as Snarky already mentioned, and OPT_CROSSFADEMUSIC is from old system, but "Crossfade tracks" is from new system.
OPT_CROSSFADEMUSIC works only for music, but "Crossfade tracks" is for everything.
So, you don't need to set OPT_CROSSFADEMUSIC at all, instead set "Crossfade tracks" in every Audio Type that you want to crossfade.

However, there is only ONE built-in crossfade channel, which means that only one crossfade will be performed at a time.
If there was already some crossfade done at the moment when you begin new one, the previous crossfade will cancel, and the fading-out clip will stop immediately (however the previous fading-in clip will continue to gain volume normally)EDIT: hmm, just realized I am not so sure about this, needs to be checked out.
If this is not good for you, then you'd probably need to design your own crossfading system in script, using at least 2 channels per type you want to be able to crossfade.


Anyway, seeing all the trouble you are going through, my strongest advice is to actually make a small test game and write a script module that controls crossfading, and that you could later use in your game. Maybe even don't use two rooms, and make tracks switch by key press.
Designing and testing such script in actual big game usually brings additional complications, because there are more factors, you need to constantly get into right rooms to test it, and so on.
(It will also be much easier to share your game/code with others, since test game is smaller)
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Wed 16/05/2018 11:31:44
Great, thanks for the info/clarification, CW!

Agreed completely about using test games or prototypes. It's also one of Ben304's recommendations on the BCT podcast.

As for this case, I would recommend as a general approach: use built-in crossfading for music only. For sounds, write a module that does it manually. You probably don't even need to crossfade, just fade out one clip, stop it, then play and fade in the next. I even seem to remember recommending this to you a long time ago, bx83.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Wed 16/05/2018 11:49:08
Something that I just realized, I am not sure if the previous crossfade of one type actually fades-in correctly if replaced by the new one of another type. This needs to be tested also.

Quote from: Snarky on Wed 16/05/2018 11:31:44For sounds, write a module that does it manually. You probably don't even need to crossfade, just fade out one clip, stop it, then play and fade in the next. I even seem to remember recommending this to you a long time ago, bx83.
True, that would be much simplier. Also if they fade in/out in a short time, players likely won't notice a difference.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: selmiak on Wed 16/05/2018 11:59:08
Quote from: Crimson Wizard on Tue 15/05/2018 13:56:33
"First time enters room" event creates "Room_FirstLoad" function name by default, but that's a misleading name (and to think of it, should be fixed), because in reality it is "Room first time After Fade-in".
The order of their running is following:

* "Enters room before fade-in" (Room_Load)
* room transition effect
* "First time enters room" (Room_FirstLoad) <--- only if the room is entered for the first time
* "Enters room after fade-in" (Room_AfterFadeIn)

this is interesting and should be noted somewhere. I always figured Room_Load commands and loading things into memory happens while the screen is black when fading out. But then, there are different screen transition effects, even one effect for warping screens together without any moment of complete blackness throughout the transition.

would it be more precise to say it like this? :
* "cChar enters next room before even fading out the current room" (Room_Load)
* room transition effect
* "First time enters room" (Room_FirstLoad) <--- only if the room is entered for the first time
* "Enters room after fade-in" (Room_AfterFadeIn)

or more like
* "Engine loads room commands of the next room into memory before even fading out the current room" (Room_Load)
* room transition effect while cChar Sprite enters next room

the cChar doesn't suddenly disappear while fading out, does it?

where in that chain would a new music track (no sfx) start playing?
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: morganw on Wed 16/05/2018 12:33:55
The next thing I'm looking at (next month) are the templates and a test game, so I'll try and start with audio tests (since that was also the bit of the ScummVM port which I was looking at). I'm not sure if the behaviour that is confirmed is just for music tracks, but if max channels actually reserved each channel, what happens when it is greater than 1? Are we saying that music is special case, or that using the built-in crossfade for any type of audio is going to keep the channel pointer valid?
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Wed 16/05/2018 12:47:51
Quote from: Crimson Wizard on Wed 16/05/2018 11:49:08
Something that I just realized, I am not sure if the previous crossfade of one type actually fades-in correctly if replaced by the new one of another type. This needs to be tested also.

You mean if you have two different AudioTypes (both set to crossfade) with MaxChannels set to 0, and you're already playing clips of (let's say) AudioType1 on all the available channels, and then you ask to play a clip of AudioType2 with higher priority? I'm not even sure crossfading the clips is the desired behavior in this case. I'm inclined to think only clips of the same AudioType should be crossfaded with each other. And what if one AudioType is set to crossfade but the other not?

Quote from: morganw on Wed 16/05/2018 12:33:55
using the built-in crossfade for any type of audio is going to keep the channel pointer valid?

Surely this.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Wed 16/05/2018 12:52:08
Quote from: Snarky on Wed 16/05/2018 12:47:51
Quote from: Crimson Wizard on Wed 16/05/2018 11:49:08
Something that I just realized, I am not sure if the previous crossfade of one type actually fades-in correctly if replaced by the new one of another type. This needs to be tested also.

You mean if you have two different AudioTypes (both set to crossfade) with MaxChannels set to 0, and you're already playing clips of (let's say) AudioType1 on all the available channels, and then you ask to play a clip of AudioType2 with higher priority?

No, I mean if there is already crossfade of two clips of type 1 in progress, and you begin crossfading other two clips of type 2.
Assuming MaxChannels for each type is 1, for simplicity.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: morganw on Wed 16/05/2018 13:13:25
Quote from: Snarky on Wed 16/05/2018 12:47:51
Quote from: morganw on Wed 16/05/2018 12:33:55
using the built-in crossfade for any type of audio is going to keep the channel pointer valid?
Surely this.
Will have to test by trying to play a lower priority clip, because I don't think we should rule out getting null as the return.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Thu 17/05/2018 07:51:26
Thanks guys, it's all coming together... slowly.

Looks like I won't bother with AudioChannel pointers now unless for things like 'beachThemePos=music_chan.Position' and the crossfading I was talking about.

Also, the time it takes to crossfade multiple channels (ie. a sound and some music, and another sounds) can be controlled with just Wait(30), or some general Waiting. I've recently converted my rooms transition to 'instant', and tried to setup a walk of edge of screen to just-in-front-of-border, so every room transition feels more natural, and also, *takes some time that would normally be Blocking*. This seems to work well :)
Hopefully this approach works better consistently.

However, I'm still having problems with volume.
If I set music_chan.Volume=100, all is well; no phantom reduction and restoration of volume level. Upgraded to 3.4.1-p2, still has this problem. Anyway, a stop-gap for the moment.

But I *also* (I've just noticed) I'm having trouble with footstep sounds. They're only at 100% volume when I (sometimes) first enter a room (or, is this entering the area around the 'edge' of the room?...).
After this, they transiton to 0% volume or 8-10% volume, or whatever. One room it's totally silent, next room it's just audible, etc.

I've tried increasing the number of sound channels to 3 and 4, in case the footsteps audio takes up a channel; and also checked the footstep audio is correctly linked to the footsteps in question: but no dice, it's volume is still random.
Anyway: I thought of just setting the footstep volume to 100%, to act as stop-gap for that as well.

Which brings me finally to my question: HOW do you set the audiochannel volumes for footsteps? Are they on their own 'secret' channel (like crossfading)? Do they take up an additional channel, meaning I should increase my MaxChannels to, I don't know, 5?
Is it even possible to set a pointer to footsteps - after setting the footsteps with this kind of code:


function ChangeFootsteps_normal (int view) {
  ViewFrame *frame = Game.GetViewFrame(1, 0, 1);
  frame.LinkedAudio = aFootstep_normal_a;
  frame = Game.GetViewFrame(view, 1, 1);
  frame.LinkedAudio = aFootstep_normal_a;
  frame = Game.GetViewFrame(view, 2, 1);
  frame.LinkedAudio = aFootstep_normal_a;
  frame = Game.GetViewFrame(view, 3, 1);
...


Experience would tell me no, since the audiochannel pointer is always null, or doesn't seem to join to the footsteps.

:undecided:
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Thu 17/05/2018 08:56:07
There's a Character.ScaleVolume setting, which if set to true will adjust the volume of the character depending on its scaling. (Or at least it's supposed to; it's been broken in some versions in the past.) Maybe try to disable that.

There's otherwise no very convenient way to control the volume of footsteps or other audio linked to an animation frame. If you do a forum search, you'll see that the usual recommendation is to create a new audio type, set the type of the clips to that, and use Game.SetAudioTypeVolume().

I'm also curious why you're limiting the Sound channels at all. By setting MaxChannels to 0 it'll use as many as needed for Sound (out of the ones left over once the ones reserved for Speech, Music, etc. are accounted for).

Quote from: bx83 on Thu 17/05/2018 07:51:26
Also, the time it takes to crossfade multiple channels (ie. a sound and some music, and another sounds) can be controlled with just Wait(30), or some general Waiting. I've recently converted my rooms transition to 'instant', and tried to setup a walk of edge of screen to just-in-front-of-border, so every room transition feels more natural, and also, *takes some time that would normally be Blocking*. This seems to work well :)
Hopefully this approach works better consistently.

This doesn't sound like you've taken our advice. :-\ I can't be sure without seeing your code since the description is a bit unclear.

Quote from: bx83 on Thu 17/05/2018 07:51:26
However, I'm still having problems with volume.
If I set music_chan.Volume=100, all is well; no phantom reduction and restoration of volume level. Upgraded to 3.4.1-p2, still has this problem. Anyway, a stop-gap for the moment.

There's a mistake somewhere in your code, and going by the snippet you had in your last post, I'm guessing it has to do with overwriting the music_chan pointer, so that when you're trying to adjust the volume of other clips later on you're actually messing with the music volume.

Forcing the volume to 100 may fix this particular bug, but as long as the mistake is still there it could pop up again in other ways. It might even be what is causing the trouble with the footsteps.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Thu 17/05/2018 09:29:27
Quote from: bx83 on Thu 17/05/2018 07:51:26
However, I'm still having problems with volume.
If I set music_chan.Volume=100, all is well; no phantom reduction and restoration of volume level. Upgraded to 3.4.1-p2, still has this problem. Anyway, a stop-gap for the moment.

I've read the whole thread again, but for some reason still could not grasp what exactly happens with the volume in your game.

However, only yesterday Chicky has found following bug and posted it on Discord channel:

Quote
Seems with OPT_CROSSFADEMUSIC set to 1 AGS automatically resets volume and crossfades from 0 to 100, ignoring all volume commands until the crossfade has finished

Maybe it is related? Haven't tested this myself.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Thu 17/05/2018 12:44:02
Okay, all code:
room 2 is BEACH, to the right of CAVE
room 7 is CAVE
room 3 is FARMER, to the right of BEACH
room 4 is CROSSROADS, to the right of FARMER
room 32 is MOUNTAIN TOP, to the top of FARMER

so:
           _32_
7  |  2  |  3   |  4

I turned off the ScaleVolume=true; no difference, the sound of footsteps in inaudable in room 7, 2 and 3, but audible in room 32 and 4.

In room 3, FARMER
What I meant by the 'blocking pause' was the walking in from the edge of the screen initially in a room transition. If I place the Walk() code after track 1 (sound) crossfades:

sounds_chan=aOcean_crash_cave.PlayFrom(oceanThemePos, eAudioPriorityLow, eRepeat);


but before track 2 (music) crossfades:

aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat);


...this should work for giving an appropriate pause while crossfading 2 tracks at once; and will hopefuly have me removing the Wait(5) and Wait(30) commands.


Please IGNORE all comments, commented out snippets, 'cheats' etc, the whole thing is in flux. I've also removed all functions that have nothing to do with music, or were not touched by me while testing.
The room transition is now Instant (left a few SetNextScreenTransition(eTransitionInstant) in case i switch it back).
beachTheme is set as volume is 50 set in editor, beachTheme_Cave is set to 40. I also had to set their volumes manually in code or they had volume randomness or cut out intermitendly on room transition.

Also, might be easier to delete anything you don't see as significant.

aBeachTheme, aBeachTheme_cave, a4_mountainCave_92bpm, aSilence (1 second of ansolute silence, to fade out on), and aAfternoonSounds are Music.
aShake_matches, aOcean_crash (for room 2), aOcean_crash_cave (for room 7 and 4) are Sounds.

===========

ROOM 2 - BEACH (this is where the game starts)

function room_Load()
{
ChangeFootsteps_normal();
  //cJulius.ScaleVolume=true;

if (GameStarted) {
beachThemePos=music_chan.Position;
oceanThemePos=sounds_chan.Position;
music_chan=aBeachTheme.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
sounds_chan=aOcean_crash.PlayFrom(oceanThemePos, eAudioPriorityNormal, eRepeat);

} else {
  //SetGameOption(OPT_CROSSFADEMUSIC, 1);
Game.SetAudioTypeSpeechVolumeDrop(eAudioTypeMusic, 30);
Game.SetAudioTypeSpeechVolumeDrop(eAudioTypeSound, 40);

BeenToBeach=true;

if (FutureTime) {                                                    //LATER IN THE GAME, YOU RETURN TO THE BEACH IN AN UNSPECIFIED LATER TIME, TO SILENCE AND A DIFFERENT BACKGROUND
aSilence.Play(eAudioPriorityNormal, eOnce);
} else {
music_chan=aSilence.Play(eAudioPriorityLow, eOnce);
music_chan=aBeachTheme.Play(eAudioPriorityNormal, eRepeat);
beachThemePos=music_chan.Position;
}

sounds_chan=aOcean_crash.Play(eAudioPriorityNormal, eRepeat);
oceanThemePos=sounds_chan.Position;

cJulius.x=436;
cJulius.y=537;

GameStarted=true;
}

QuicksandConquered=true; //CHEAT                    //IN THE BEACH ROOM, YOU MUST WALK ACROSS QUICKSAND, WHICH IS IMPOSSIBLE WITHOUT SOLVING A PUZZLE; BUT IT WAS TOO HARD SOLVING THIS PUZZLE OVER AND OVER WHEN TESTING, SO I REMOVED IT
}

function room_AfterFadeIn()
{
if (cJulius.PreviousRoom == 3) { //farmer
aAfternoonSounds.Stop();
sounds_chan=aOcean_crash.PlayFrom(oceanThemePos, eAudioPriorityLow, eRepeat);
}

if (cJulius.PreviousRoom == 7) { //cave
aWater_dripping.Stop();
cJulius.Walk(265, 500, eNoBlock, eWalkableAreas);
}

music_chan.Volume=50;
}



function room_LeaveRight()
{
  if (cJulius.y<=470) {

    SetNextScreenTransition(eTransitionInstant);
    SetGameOption(OPT_CROSSFADEMUSIC, 1);

    cJulius.ChangeRoom(3, 90, 600, eDirectionDownRight);     //Farmer
  } else {
    cJulius.ChangeRoom(23, 164, 472, eDirectionRight);   //pixelated forest
  }
}


function room_LeaveLeft()
{
if (cJulius.y >= 240 && cJulius.y <= 520) {
cJulius.Walk(0, cJulius.y, eBlock, eWalkableAreas);
SetNextScreenTransition(eTransitionInstant);
cJulius.ChangeRoom(7, 900, 385);       //Cave
}
}
   
function room_LeaveBottom()
{
  cJulius.ChangeRoom(23, 120, 437);   //pixel forest
}



=====================


ROOM 7 - CAVE

function room_LeaveRight()
{
  if (cJulius.y<=405){
cJulius.Walk(1054, cJulius.y, eBlock, eAnywhere);
SetNextScreenTransition(eTransitionInstant);
cJulius.ChangeRoom(2, 2, 500, eDirectionRight); //beach
  }
}

function room_FirstLoad()
{
  BeenToCave=true;
}

function room_Load()
{
//braun fixes car
cBraun.LockView(67);
cBraun.Animate(0, 1, eRepeat, eNoBlock);

if (FutureTime) {
music_chan.Stop();
} else {
beachThemePos=music_chan.Position;
music_chan=aBeachTheme_cave.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
}

sounds_chan=aWater_dripping.Play(eAudioPriorityNormal, eRepeat);
}

function room_AfterFadeIn()
{
music_chan.Volume=40;
}


=====================

ROOM 3 - FARM (room 47 is 'farmer detail' - ignore)

function room_LeaveLeft()
{
  cJulius.ChangeRoom(2, 910, 330, eDirectionLeft);       //Beach
}

function room_LeaveRight()
{
  cJulius.ChangeRoom(4, 90, 560, eDirectionRight);         //Crossroads
}

function room_LeaveTop()
{
cJulius.Walk(654, 452, eBlock, eWalkableAreas);
cJulius.ChangeRoom(32, 196, 384, eDirectionRight);       //Mountaintop
}

function room_Load()
{
ChangeFootsteps_normal();

//oceanThemePos=sounds_chan.Position;


//Display("music channel ID %d, sounds channel ID %d, volumes are %d and %d respectively",music_chan.ID, sounds_chan.ID,  music_chan.Volume,  sounds_chan.Volume);
//Wait(30);
sounds_chan=aOcean_crash_cave.PlayFrom(oceanThemePos, eAudioPriorityLow, eRepeat);
//Display("music channel ID %d, sounds channel ID %d, volumes are %d and %d respectively",music_chan.ID, sounds_chan.ID,  music_chan.Volume,  sounds_chan.Volume);
Wait(5);
aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat);
//Display("music channel ID %d, sounds channel ID %d, volumes are %d and %d respectively",music_chan.ID, sounds_chan.ID,  music_chan.Volume,  sounds_chan.Volume);
Wait(30);
//REPLACE THIS WITH WALKING OFFSCREEN PART
}


function room_AfterFadeIn()
{
if (cJulius.PreviousRoom==2) { //beach
aOcean_crash.Stop();
}

if(cJulius.PreviousRoom==32) { //mountain cave
cJulius.Walk(426, 539, eBlock, eWalkableAreas);
}

music_chan.Volume=100;
}


============================

ROOM 4 - CROSSROADS

function room_LeaveLeft()
{
if (!smallone) {
    if (cJulius.y >= 370) {
      cJulius.ChangeRoom(3, 900, 600, eDirectionLeft);       //Farmer
    } else {
      cJulius.ChangeRoom(12, 935, 310, eDirectionDown);   //W village
    }
}
}

function room_Load()
{
aOcean_crash.Stop();
aOcean_crash_cave.Stop();

music_chan=aCrossroads.Play(eAudioPriorityNormal, eRepeat);
 
if (HasMace && dTrainer.GetOptionState(7)==eOptionOffForever) { //north village
cTrainer.Transparency=100; //disappear
}
}

function room_AfterFadeIn()
{
if (cJulius.PreviousRoom==8) { //south village
cJulius.Walk(470, 628, eBlock, eAnywhere); //walk up from south edge
}

 
}



==============

ROOM 32 - MOUNTAINTOP


function room_Load()
{
  ChangeFootsteps_snow();

    aOcean_crash_cave.Stop();
    aAfternoonSounds.Stop();
}

function room_AfterFadeIn()
{
    if (music_chan!=null) {
        if (music_chan.PlayingClip!=a4_mountainCave_92bpm) {
            music_chan = a4_mountainCave_92bpm.Play(eAudioPriorityNormal, eRepeat);   
        }
    } else {
        music_chan = a4_mountainCave_92bpm.Play(eAudioPriorityNormal, eRepeat);   
    }
}

function room_RepExec()
{
  if (GetWalkableAreaAt(cJulius.x, cJulius.y)==1) {
    ChangeFootsteps_snow();
  } else if (GetWalkableAreaAt(cJulius.x, cJulius.y)==2) {
    ChangeFootsteps_concrete();
  }
}


function room_LeaveLeft()
{
  cJulius.ChangeRoom(3, 654, 452, eDirectionDown);  //farmer
}



That's it, abridged. Hopefully you can pick up a massive error to do with setting volumes etc.

There are quite a number of non-natural transitions in the code; the only natural transition are anywhere you see:

SetNextScreenTransition(eTransitionInstant);


This has Julius wakling off a eg. a left edge, room transitons instantly, and then in the next he walks in from the eg. right edge, before finally stopping and returning control to the user.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Thu 17/05/2018 12:57:18
Here's a video of the above:
link here (http://redrom.ltd/img/1MINDEMO.webm)

You might want to listen on 100%+ volume, the footsteps are just audible in room 4 (crossroads).
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Sat 19/05/2018 06:45:56
Anyone...?
Anyway, I do understand AudioChannel and crossfade more. Pitty about footstep sounds but it will be fixed later I suppose.
Thankyou :)
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Sat 19/05/2018 07:20:00
The obvious difference between these rooms is that in 2,7 and 3 you're playing background sounds on sound_chan, while you're not doing so in rooms 4 and 32.

So if you still have the Sounds set to use only 1 channel, then it straightforwardly won't work, for obvious reasons.

Your game logic is also a bit of a mess. I think things would be a lot cleaner if you distinguished game-wide, permanent settings, like OPT_CROSSFADEMUSIC (which, as we've learned, shouldn't be used in the first place) and the screen transition style, from temporary state. Don't set these repeatedly whenever you change rooms, but once at game start.

Also, what purpose is there to lines like
Code (ags) Select
                        music_chan=aSilence.Play(eAudioPriorityLow, eOnce);
                        music_chan=aBeachTheme.Play(eAudioPriorityNormal, eRepeat);
                        beachThemePos=music_chan.Position;

in Room_Load? (1) You're first telling it to play one clip and then immediately telling it to play another, presumably of the same type. Why? Is this some hack to disable crossfading by interrupting it? If some of CW's concerns turn out to be correct, this could potentially lead to erratic volume changes, so... maybe don't? (2) Shouldn't oceanThemePos always be 0 since you're just starting to play it on the line above? (3) By the time you use it, it will be completely outdated anyway.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Sat 19/05/2018 10:33:04
I completely agree.
All of this is left over in code from revisions of revisions based on an imperfect knowledge of the sound system and partially done re-writing of various room's code.
I'll clean these up before putting more code here.

BTW are you planning on ever making more than one crossfade channel?

Also - if a programmer has to change the OPT_CROSSFADEMUSIC from 1 to 0 and then back again -  how should they do it if OPT_CROSSFADEMUSIC is not to be used anymore? How do you control *this* option (the one in the Editor, the 'Crossfade Tracks', as opposed to OPT_CROSSFADEMUSIC)? Like Game.SetAudioTypeCrossfade?
The reason I ask is this presents a pickle in my one piece of simultaneous dialogue in the whole game:


SetGameOption(OPT_CROSSFADEMUSIC, 0); //set crossfade to 'no crossfade' for sounds_chan (because the dialogue sound is set as a 'Music' or dialogue ("&123 This is a line") piece, and we want it played cleanly, not to crossfade in or out)

game.bgspeech_stay_on_display = 1;
        sounds_chan=aCombinedLineLibrary.Play(eAudioPriorityNormal, eOnce);    //<-- this is the combined speech of two players

cJulius.LockView(68);
cJulius.Animate(0, 4, eOnce, eNoBlock);        //animate 1st player's speech

cNerd.SayAtBubble(550, 270, "&110 We are very sorry to makers of Everyone Loves Raymond. He is not our own creation and wholly the property of 'Where's Lunch', 'Worldwide Pants', and 'HBO Independent Productions'. Thankyou for viewing our slap-stick play.");        //get 2nd player to say his line (???)

aCombinedLineLibrary.Stop();    //stop the combined speech line

cJulius.LockView(1);            //back to normal animation
cJulius.Animate(8, -7, eOnce, eBlock);   

cJulius.UnlockView();        //unlock view(s)

SetGameOption(OPT_CROSSFADEMUSIC, 1); //set crossfade back for sounds_chan


(obviously there is random confusing crap in there, but it works)
The above code uses the crossfade option to change it from 'slow' to 'no crossfade', and then back again... and it works! Somehow :/ ...... because according to you and the errors I was getting before, it shouldn't work.

I think was my reason for putting:

music_chan=aSilence.Play(eAudioPriorityLow, eOnce);
music_chan=aBeachTheme.Play(eAudioPriorityNormal, eRepeat);

...was an attempt to prime the music_chan pointer; removed it, still works, god knows what I was thinking.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Sat 19/05/2018 11:27:41
The obvious solution to the speech issue is to not have the audio type of the background speech clips be Music.

... I may regret this, because I think the best solution would be to clean up your code thoroughly, but I think there is a pretty simple workaround/fix.

As we have learned, AGS has 9 AudioChannels:

-1: Crossfade [reserved]
0: Speech [reserved]
1: Game-assigned
2: Game-assigned
3: Game-assigned
4: Game-assigned
5: Game-assigned
6: Game-assigned
7: Game-assigned

The way you have it set up now, if I understand it correctly, it's:

-1: Crossfade [reserved]
0: Speech [reserved]
1: Music
2: Sound
3: [Unused]
4: [Unused]
5: [Unused]
6: [Unused]
7: [Unused]

(I don't know for sure that it's precisely channel 1 for Music and Channel 2 for Sound, but it's something like this.)

Instead, I suggest you create a new AudioType, which you can call e.g. BackgroundSound. Set its MaxChannels property to 1 and crossfade to true. Set the MaxChannels property of Sound to 0 (unlimited) and crossfade to false. Change the AudioType of the background sounds to BackgroundSound. Change the AudioType of the background speech clips to Sound.

This way, the AudioChannel assignments should be something like:

-1: Crossfade [reserved]
0: Speech [reserved]
1: Music
2: BackgroundSound
3: [Free - Can be used for Sound]
4: [Free - Can be used for Sound]
5: [Free - Can be used for Sound]
6: [Free - Can be used for Sound]
7: [Free - Can be used for Sound]

This way, Music and BackgroundSound can still be crossfaded using the built-in feature (though not at the same time), background speech (as Sound clips) won't crossfade, you'll be able to play up to five Sound effects simultaneously (e.g. footsteps, background speech), and Sounds won't be affected by Music or BackgroundSound at all.

There's probably still some code that needs cleaning up so this will work bug-free, but it should simplify things a lot.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Sat 19/05/2018 12:31:31
This further gives me knowledge into Audio. Didn't know I could have 7 extra named types, and separate them into 7 different 'channels'.
I've got half a mind to do this:

-1: crossfades
0: Speech  (crossfade off, maxchan 1)
1: Music (crossfade on, maxchan 1)
2: Sounds_1 (crossfade on, maxchan 1)
3: Sounds_2 (crossfade on, maxchan 1)
4: Extra (crossfade off, maxchan 1)
5: free
6: free
7: free

Each room has music; so just set Music to new track, then it's the nex track, all smooth.
They also have 1 sound; I use Sounds_1, same thing.
It might have 1 extra sound: Sounds_2.
For temporary sounds (door-bell, jangling keys, etc. that just do the sound 'eOnce'), we have Extra
The other's probably won't be used.
I can track and separate all :)

And I can figure out how to drop in blocking pauses like Wait for 2 or 3 sounds, so all of them have a smooth crossfade if needed.

No more 'Music_chan' etc, we have audio types which themselves are 1 track maxchannel pointers to begin with :)
No possibility of having pointer's overwritten; these internal anyway and don't *need* a global variable.

Correct?
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Sat 19/05/2018 13:56:50

Quote from: bx83 on Sat 19/05/2018 12:31:31
No more 'Music_chan' etc, we have audio types which themselves are 1 track maxchannel pointers to begin with :)

No, they're not. AudioTypes are enums, and the mapping from AudioType to AudioChannel* is not accessible to the game script.

In order to adjust volumes etc. you'll still need AudioChannel pointers.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Sun 20/05/2018 05:56:07
Are there 7 channels in total, or per AudioType?
Will post updated code later, all... seems to work now.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Sun 20/05/2018 06:13:05
Quote from: bx83 on Sun 20/05/2018 05:56:07
Are there 7 channels in total, or per AudioType?

:shocked:
Well, let me ask you this: What are AudioChannels, and why are they limited to 7 (or 8, or 9, depending on how you count) in the first place?
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Mon 21/05/2018 00:41:00
I don't know if I follow you. Genuinely. There it is, I'm the stupidist person on the planet; but I really don't know.

AudioChannels are 7-9 separate channels for audio (AudioClips) to play on, of different AudioTypes.
I think????

for example:
-1: crossfade [hidden]
0: Speech
1: Music
2: Sound
3: Sound
4: Some_other_type
5: Some_other_type_as_well
6: [unused]
7: [unused]

Or: is it per *audiotype* - so:
7 channels for each Music, 7 channels for each set of Sounds, 7 channels for some_other_audio_type; etc.?

I don't know *why* you chose for them to be ~3bit (7-9 choices) rather than 64bit (2 or 4 billion choices). No-one's likely to play 200,000,000 sound effects and grow the exe file to 8gigabyte's or something, and crash the system.
The difference in program size is negigable in today's 64bit operating systems -- perhaps not in the early 90's, when many of the adventure games were made, but nowodays anyway.

I don't understand understand why there's only one crossfade channel - why not 7/8/9 also to mirror the AudioChannels? Why not a virtually *infinite* number of channels? Do they relate to a soundcard's minimum set-in-stone channels (so they'll work on any soundcard); or are they an arbitrary number?

I don't understand - sincerely, I yield to you. I give up. I've got it working; but don't really understand how. I just understand 10% of it now instead of 2% of it. Every time I think I do understand -- I don't.

For example:
When I set maxchannel as 1 for Music; it crossfades every time. It's always channel '1'. Simple.
When I set maxchannel for *Sounds* to 3 or 4; it crossfade 'almost' every time.
But then when I *add a pause* - blocking walking pause, or just a good ol 'Wait(45)', have space between Music crossfading and Sounds crossfading - it does work, every time.
Okay, so far so good.
BUT
When I have a blocking pause; and I set Sounds maxchannel's to *1*, it stops working as soon as I *move*.
If I set Sounds to maxchannels 2, it stops when I enter a room.
Maxchannels 3, it stops when I have 2 sounds playing (ie sound_chan=aSound1.Play(); aSound2.Play(); )
(and I have to have music_chan and sounds_chan global AudioChannel pointers to keep track of these two tracks between rooms, so I know their position)

So, in my mind I hatch a possibility, but it remains out of view. The possibility of it generating new channels for each new sound, but then always returning to the same pointer?... or something?... Something like this.
But in music_chan pointer/Music, this *doesn't* happen. What makes sound_chan pointer/Sounds different? I don't know, perhaps you can immediately solve this in 1 sentences, not sure.
This is only one poorly understood theory.
I will post code soon.

So as I say, I yield, I give up. It's too complicated; I don't understand how pointers relate to AudioChanels which relate to AudioClips and so on. I *thought* I did, but then every time I try something new, I don't!
I won't ask any more questions if you feel like you can't answer without some of this getting through my skull. There's 500 other things to debug in the game; just this is so misteriously hard for me (an only me! :P) to understand.

Perhaps you could link me to the page in the manual (for v3.x only) that exaplains this?
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Mon 21/05/2018 00:51:59
Quote from: bx83 on Mon 21/05/2018 00:41:00
AudioChannels are 7-9 separate channels for audio (AudioClips) to play on, of different AudioTypes.

There are 8 channels TOTAL, with 1 reserved for speech.

Quote from: bx83 on Mon 21/05/2018 00:41:00
I don't know *why* you chose for them to be ~3bit (7-9 choices) rather than 64bit
<...>
I don't understand understand why there's only one crossfade channel
<...>

You keep asking those "why" questions...
Once and for all: this program is over 20 years old. It was written by another person who is no longer coming here. It was started in times when he probably did not consider future problems very well, and system specifics were different. Over time he was adding more stuff while keeping old stuff in, and that complicated things further.
So why these channel limits are still there - we may only guess. Maybe he thought 8 channels will be enough. Maybe adding more crossfade channels into the old program with bad and overtwixed code was too difficult for him.
I do not think these are useful questions anyway, the useful question is whether it is possible to increase them, and the answer is yes. Another question is why this was not done yet, and the answer is "because it is not trivial to change that code, one would have to spend a fair amount of time to research it and find out how to do that correctly to not break anything, and no one bothered to do that yet".



Quote from: bx83 on Mon 21/05/2018 00:41:00
BUT
When I have a blocking pause; and I set Sounds maxchannel's to *1*, it stops working as soon as I *move*.

May that be related to footsteps? I thought you mentioned footsteps before. Are footsteps and "sounds" same type? Then since you limit sound channels to 1, they override previous sound.

Quote from: bx83 on Mon 21/05/2018 00:41:00If I set Sounds to maxchannels 2, it stops when I enter a room.
Maxchannels 3, it stops when I have 2 sounds playing (ie sound_chan=aSound1.Play(); aSound2.Play(); )

Same, maybe the number of currently playing sounds is simply larger than avaiable channels? It's hard to tell without researching all of your code.

Quote from: bx83 on Mon 21/05/2018 00:41:00
But in music_chan pointer/Music, this *doesn't* happen. What makes sound_chan pointer/Sounds different?

Come on, are you serious? The answer to mystery is right in your post: the number of sounds you play at the same time - that's what makes difference. You play only 1 music at a time on 1 restricted channel, so it is always same channel, and pointer always points to that one channel.
But with sounds - you have multiple channels and play multiple sounds at the same time. So at some conditions they ovewrite each other.
And when you are starting next sound - it will not necessarily take same channel again, it may take ANY of those channels, whichever is free at this point in time.


If you want *particular* type of sound (like ambient sound?) play all time no matter what, and potentially on the same channel, then you should create a separate AudioType for it and reserve 1 channel for it. Then it won't be overriden by other sounds.
But I guess, this is what Snarky suggested you to do only couple of days ago: http://www.adventuregamestudio.co.uk/forums/index.php?topic=56063.msg636586645#msg636586645


It seems to me that you overthink the unnecessary details too much, and keep trying to learn through experimentation with script, instead of studying theory first. This leads only to confusion.

Quote from: bx83 on Mon 21/05/2018 00:41:00
Perhaps you could link me to the page in the manual (for v3.x only) that exaplains this?

I tried to explain it on forums once: http://www.adventuregamestudio.co.uk/forums/index.php?topic=54062.msg636546051#msg636546051
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Mon 21/05/2018 06:38:15
Quote from: Crimson Wizard on Mon 21/05/2018 00:51:59
There are 8 channels TOTAL, with 1 reserved for speech.

Are you counting the crossfade channel in those 8 channels?

Quote from: Crimson Wizard on Mon 21/05/2018 00:51:59
You keep asking those "why" questions...
Once and for all: this program is over 20 years old. It was written by another person who is no longer coming here. It was started in times when he probably did not consider future problems very well, and system specifics were different. Over time he was adding more stuff while keeping old stuff in, and that complicated things further.
So why these channel limits are still there - we may only guess. Maybe he thought 8 channels will be enough. Maybe adding more crossfade channels into the old program with bad and overtwixed code was too difficult for him.

I don't think that's entirely fair. The number of channels is the number of sounds that can play at the same time (polyphony), and this has historically been one of the main limitations of computer-generated audio and one of the most important dimensions along which different sound cards have varied (https://en.wikipedia.org/wiki/Sound_card#Sound_channels_and_polyphony).

Software abstraction layers like DirectSound (or whatever Allegro uses) allow you to mix an "unlimited" number of sounds playing simultaneously, but on the hardware layer you're still limited (https://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.reference.dscaps(v=vs.85).aspx), so this requires software mixing. The more channels you use, the more processing and the more memory that downmixing is going to need. If you didn't limit it, at some point the program wouldn't be able to keep up any longer. A poorly written game (such as one that called AudioClip.Play() in repeatedly_execute(), or inside a loop), would very quickly bring down AGS.

So I think there are excellent reasons to set a limit. Of course, the limits that made sense 20 years ago are not necessarily ones we would set today.

(Here are a couple (https://www.recordingrevolution.com/optimize-computer-recording-mixing/) of articles (https://www.sweetwater.com/sweetcare/articles/hardware-recommendations-for-windows-based-daw-systems/) that talk about hardware requirements for sound mixing. One interesting quote: "after swapping out my old spinning drives with SSDs and putting [16 GB] RAM in my Mac Mini I was able to run a Pro Tools session with 50 audio tracks and 250 reverbs!!" Now Pro Tools does a lot more fancy audio processing than AGS, but still, those are some high hardware requirements even today.)

QuoteI do not think these are useful questions anyway, the useful question is whether it is possible to increase them, and the answer is yes. Another question is why this was not done yet, and the answer is "because it is not trivial to change that code, one would have to spend a fair amount of time to research it and find out how to do that correctly to not break anything, and no one bothered to do that yet".

And also, because it's still quite rare that people actually need more than 7 channels.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Mon 21/05/2018 07:19:16
bx83, in addition to what CW explained:

Quote from: bx83 on Mon 21/05/2018 00:41:00
For example:
When I set maxchannel as 1 for Music; it crossfades every time. It's always channel '1'. Simple.
When I set maxchannel for *Sounds* to 3 or 4; it crossfade 'almost' every time.
But then when I *add a pause* - blocking walking pause, or just a good ol 'Wait(45)', have space between Music crossfading and Sounds crossfading - it does work, every time.
Okay, so far so good.

As already discussed, you cannot do more than one crossfade at the same time. Therefore, I don't believe the bolded version is correct. I believe that what is actually happening (with 3 channels, let's say) is something like...

In the first room:

SoundChannel 1: Room1AmbientSound (repeating)
SoundChannel 2: Footstep (whenever you walk)
SoundChannel 3: [unused]

(SoundChannel 1-3 are the AudioChannels reserved for Sound)
And then when you enter the second room:

SoundChannel 1: Room1AmbientSound (repeating)
SoundChannel 2: Room2AmbientSound (repeating)
SoundChannel 3: Footstep (whenever you walk)

There's no crossfading, you just have both/all the sounds playing simultaneously. With the music and everything, and the Room2AmbientSound probably starting quite softly, you just might not notice.

If you then go back to the first room:

SoundChannel 1: Room1AmbientSound (repeating)
SoundChannel 2: Room2AmbientSound (repeating)
SoundChannel 3: Room1AmbientSound (repeating) - second copy!

You would probably notice the second copy of Room1AmbientSound, but now you've run out of Sound channels, so as soon as you walk, it will have to stop one of the tracks (I'm not sure how it picks it when they're all the same priority, but let's say it just takes the first). If you've set Sounds to crossfade, I'm guessing this will happen:

SoundChannel 1: Footstep (fading in)
SoundChannel 2: Room2AmbientSound (repeating)
SoundChannel 3: Room1AmbientSound (repeating)
CrossFadeChannel: Room1AmbientSound (fading out)

If the footstep actually fades in, the first one would probably be pretty much inaudible. However, since it's a short clip, it should finish before the next footstep starts:

SoundChannel 1: [unused]
SoundChannel 2: Room2AmbientSound (repeating)
SoundChannel 3: Room1AmbientSound (repeating)
CrossFadeChannel: Room1AmbientSound (fading out)

And so now we can play the next footstep (and any future footsteps) on SoundChannel 1 without needing to crossfade, and it will work fine.

However, if you start walking as soon as you enter the room, that first AmbientSound/Footstep crossfade is going to conflict with the Music crossfade, and things will go bad more noticeably.

So this explains why it sometimes seems to work and sometimes breaks.

Quote from: bx83 on Mon 21/05/2018 00:41:00
So, in my mind I hatch a possibility, but it remains out of view. The possibility of it generating new channels for each new sound, but then always returning to the same pointer?... or something?... Something like this.

No.

Do what I explained here (http://www.adventuregamestudio.co.uk/forums/index.php?topic=56063.msg636586645#msg636586645).
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Mon 21/05/2018 08:52:01
Good points Snarky and CrimsonWizard.
This comment was written originally after CrimsonWizards comment.
I will get to write *even more comments* after :P

Quote from: Crimson Wizard on Mon 21/05/2018 00:51:59
There are 8 channels TOTAL, with 1 reserved for speech.
Okay, remebered.

Quote
Once and for all: this program is over 20 years old. It was written by another person who is no longer coming here.
Okay, fair enough.

Quote
Quote from: bx83 on Mon 21/05/2018 00:41:00
BUT
When I have a blocking pause; and I set Sounds maxchannel's to *1*, it stops working as soon as I *move*.

May that be related to footsteps? I thought you mentioned footsteps before. Are footsteps and "sounds" same type? Then since you limit sound channels to 1, they override previous sound.

Footsteps are working correctly, I forgot to say. I created a FootPrint AudioType, then made all these sounds FootPrints, and set the volume manually; all good, they work now.

Quote
Quote from: bx83 on Mon 21/05/2018 00:41:00If I set Sounds to maxchannels 2, it stops when I enter a room.
Maxchannels 3, it stops when I have 2 sounds playing (ie sound_chan=aSound1.Play(); aSound2.Play(); )
Same, maybe the number of currently playing sounds is simply larger than avaiable channels? It's hard to tell without researching all of your code.

True, but in Music with maxchannel=1, this doesn't happen.
How many Sounds am I playing at once?
Well there is 1: aWavesCrash; and in the next room, aWavesCrash_Cave. They crossfade. This is controlled by the pointer oceanThemePos to keep track of the track's position.
This is exactly like Music with maxchanne=1, etc. - but it doesn't work.
Note: I have gotten rid of the 2nd track aWaveCrash_Cave (used for rooms 7/cave and 3/farmer), it was pointless; I'm now just controlling the volume so it's quiter the further (in rooms) yuo are away from it.
Yes - I tried increasing the wait pause between crossfades; at wait(100) it still cut out the new sound being introduced.
Yes - I've tried removing the .Volume to set the sound/music volume manually, and just relied on the default volumes set in the editor; doesn't work, volume goes bananas.

Here is my code:

AudioType Music has 1 channel, has crossfade
    aBeachTheme is set to volume 40, manually and in editor
    aBeachTheme_Cave (played in 7/cave) is set ot volume 30, manually and in editor

AudioType Sound has 1 channel, and crossfade
    aOceasn_crash has volume set to 7 (manually and in editor) in room 2/beach; set to 1 in 3/farmer and 7/cave, and stopped everywhere else.


From left to right order:

Room 7/CAVE:

function room_LeaveRight()        //to BEACH
{
  if (cJulius.y<=405){
cJulius.Walk(1054, cJulius.y, eBlock, eAnywhere);
beachThemePos=music_chan.Position;        //load up pointers for next room
oceanThemePos=sounds_chan.Position;
cJulius.ChangeRoom(2, 2, 500, eDirectionRight); //goto BEACH
  }
}

function room_Load()
{
        beachThemePos=music_chan.Position;
music_chan=aBeachTheme_cave.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
}

function room_AfterFadeIn()
{
music_chan.Volume=30;
sounds_chan.Volume=1;
cJulius.Walk(900, 385, eBlock, eAnywhere);
}


Room 2/BEACH:

function room_Load()
{
    ChangeFootsteps_normal();    //changes footsteps to normal_a and normal_b, which are set as AudioType FootPrints (works now)
    cJulius.ScaleVolume=true;    //turned this on again, works a treat :P

if (GameStarted) { //IF GAME IS STARTED
music_chan=aBeachTheme.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
sounds_chan=aOcean_crash.PlayFrom(oceanThemePos, eAudioPriorityNormal, eRepeat);

} else { //IF GAME *NOT* STARTED
Game.SetAudioTypeSpeechVolumeDrop(eAudioTypeMusic, 30);
Game.SetAudioTypeSpeechVolumeDrop(eAudioTypeSound, 40);
Game.SetAudioTypeVolume(eAudioTypeFootPrints, 30, eVolExistingAndFuture);
music_chan=aBeachTheme.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
GameStarted=true;
}
}

function room_AfterFadeIn()
{
if (cJulius.PreviousRoom == 3) { //farmer
aAfternoonSounds.Stop();    //stop the Music from rom farmer
cJulius.Walk(910, 330, eBlock, eAnywhere);
sounds_chan=aOcean_crash.PlayFrom(oceanThemePos, eAudioPriorityLow, eRepeat);
}

sounds_chan=aOcean_crash.PlayFrom(oceanThemePos, eAudioPriorityNormal, eRepeat);
sounds_chan.Volume=7;
}

function room_LeaveRight()
{
  if (cJulius.y<=470) {
    beachThemePos=music_chan.Position;    //load up pointers for room farmer
    oceanThemePos=sounds_chan.Position;
    cJulius.Walk(1024, cJulius.y, eBlock, eWalkableAreas);
    cJulius.Walk(1064, cJulius.y, eBlock, eAnywhere);
    cJulius.ChangeRoom(3, -20, 617, eDirectionDownRight);     //Farmer
  } else {
    cJulius.ChangeRoom(23, 164, 472, eDirectionRight);   //pixelated forest
  }
}


function room_LeaveLeft()
{
if (cJulius.y >= 240 && cJulius.y <= 520) {
cJulius.Walk(0, cJulius.y, eBlock, eWalkableAreas);
beachThemePos=music_chan.Position;    //load up pointers for room cave
oceanThemePos=sounds_chan.Position;
cJulius.ChangeRoom(7, 1044, 385);       //Cave
}
}


Room 3/FARMER:

function room_LeaveLeft()
{
cJulius.Walk(0, 606, eBlock, eWalkableAreas);
cJulius.Walk(-20, 606, eBlock, eAnywhere);
cJulius.ChangeRoom(2, 1064, 330, eDirectionLeft);       //Beach
}

function room_LeaveRight()
{
        cJulius.Walk(1044, cJulius.y, eBlock, eAnywhere);
cJulius.ChangeRoom(4, -20, 560, eDirectionRight);         //Crossroads
}

function room_LeaveTop()
{
cJulius.Walk(654, 452, eBlock, eWalkableAreas);
cJulius.ChangeRoom(32, 196, 384, eDirectionRight);       //Mountaintop
}

function room_Load()
{
sounds_chan.Volume=1;
}


function room_AfterFadeIn()
{
aAfternoonSounds.Play(eAudioPriorityNormal, eRepeat); //regardless of which room was last, start crossfade this now from whatever to aAfternoonSounds

if (cJulius.PreviousRoom==2) { //beach
cJulius.Walk(0, 617, eBlock, eAnywhere);
cJulius.Walk(90, 617, eBlock, eWalkableAreas);
}

if (cJulius.PreviousRoom==4) { //crossroads
cJulius.Walk(940, 600, eBlock, eAnywhere);
}

if(cJulius.PreviousRoom==32) { //mountain cave
cJulius.Walk(426, 539, eBlock, eWalkableAreas);
}

beachThemePos=music_chan.Position; //save for later when we re-enter room 2/Beach
music_chan.Volume=80;
}


Room 4/CROSSROADS:

function room_LeaveLeft()
{
        if (cJulius.y >= 370) {
            cJulius.Walk(-30, 560, eBlock, eAnywhere);
            cJulius.ChangeRoom(3, 1044, 600, eDirectionLeft);       //Farmer
        } else {
            cJulius.ChangeRoom(12, 935, 310, eDirectionDown);   //W village
        }
}

function room_LeaveRight()
{
  cJulius.ChangeRoom(24, 317, 690); //dark seeds, outside
}


function room_Load()
{
aOcean_crash.Stop();
      if (music_chan.PlayingClip!=aCrossroads) {
            music_chan=aCrossroads.Play(eAudioPriorityNormal, eRepeat);    //it's complicated why this is checking if itself is playing, but I'll explain if asked
}
}


Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Mon 21/05/2018 09:02:59
Quote from: Snarky on Mon 21/05/2018 07:19:16
However, if you start walking as soon as you enter the room, that first AmbientSound/Footstep crossfade is going to conflict with the Music crossfade, and things will go bad more noticeably.

But this happens in the first room (beach) of the entire game. As soon as I move, it cuts out; not on re-entering room.
However, I'm starting to feel a eureka coming on...
The last sound channel being overwritten by footsteps would explain this - for a small number of channels. What's the sound which plays when a character starts moving? Footsteps.
If footsteps and ambient sound are both Sound, then setting channel to 1 will automatically wipe on out.
Now, I've simplified (given footsteps it's own AudioType FoootPrints; gotten rid of crossfading sound and second sound (aDrippingWater) in cave) - it all works.
So you were right :P
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Mon 21/05/2018 09:40:40
Quote from: Snarky on Mon 21/05/2018 06:38:15
Quote from: Crimson Wizard on Mon 21/05/2018 00:51:59
There are 8 channels TOTAL, with 1 reserved for speech.

Are you counting the crossfade channel in those 8 channels?

8 channels for AudioTypes, and 1 for crossfade.

Quote from: Snarky on Mon 21/05/2018 06:38:15
Quote from: Crimson Wizard on Mon 21/05/2018 00:51:59
You keep asking those "why" questions...
Once and for all: this program is over 20 years old. It was written by another person who is no longer coming here. It was started in times when he probably did not consider future problems very well, and system specifics were different. Over time he was adding more stuff while keeping old stuff in, and that complicated things further.
So why these channel limits are still there - we may only guess. Maybe he thought 8 channels will be enough. Maybe adding more crossfade channels into the old program with bad and overtwixed code was too difficult for him.

I don't think that's entirely fair.

Yes, yes, this is not fair. SORRY. I am loosing my head every time someone starts asking "Why why why why why why why is this so". It drives me crazy. It's like asking "why you did not make it better". "Why you all are sitting here instead of fixing it". And I do not know what to answer! What could be the excuse? I was lazy? Incompetent? Gave up?
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Mon 21/05/2018 09:55:54
Crimson, I feel like this every day as I program the game :P

Don't worry - I'm never saying 'why are you so incompetant!'; just 'but why is this thing the way it is, I'm mystified :/' Sometimes I really don't know why something is the way it is; so I ask. If there wasn't this forum (and you, Snarky and Kris, and others to answer my complex and incredibly annoying questions), I wouldn't be able to learn the ags system :)

I'm slow on the uptake; and I'm learning, each day. This game will take about 2 years to write; the next one will take 3.5 weeks. I've learnt so much about the editor, as well as the whole organisation of the project, how to record and level speech soundclips from different studio recordings, how to do more with Photoshop, how animation is done and works, how to save the walkable areas masks, etc etc etc; it's basically like 1.5 years so far of just pure learning, about 5% actual programming :P
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Mon 21/05/2018 10:43:15
Yeah, CW! No one is blaming you for the AGS sound system... which â€" as I said in the other thread â€" I don't think is that bad, if only it were properly documented. Even the "new" system (which is what, ten years old at this point?) was created before you got involved in the AGS development, anyway. And improving the documentation is something that I have long wanted to do, but just never get around to, so that's more on me.

Also, a lot of that alternative audio API we discussed in the other thread would actually be possible to implement as a module (as a direct consequence of improvements you have made to the engine), so nobody can blame the engine developers that it's not available.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: ManicMatt on Mon 21/05/2018 13:59:36
Crimson, you know when I try and help people in the technical help threads even though I probably shouldn't.. well its because you're always on there helping people out and I want to lighten the load for you!

You're a legend, and you have more patience than I! So I hope you don't feel discouraged, we all know you work your ass off, for very little or nothing in return. So, thank you.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Mon 21/05/2018 14:05:40
Ok, this is getting too far, seriously, I again should not be posting what I posted, but it's too late to delete it.
Could we change subject, please?
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: ManicMatt on Mon 21/05/2018 14:12:24
You're only human, I just wanted you to know you're appreciated. Its all good.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Mon 21/05/2018 15:11:57
Quote from: Crimson Wizard on Mon 21/05/2018 14:05:40
Could we change subject, please?

All right. :)
Well, one question we never got sorted was quite how OPT_CROSSFADEMUSIC interacts with the Crossfade option on the AudioTypes, if bx83 is right that setting the OPT_CROSSFADEMUSIC value to 0 actually disabled crossfading temporarily. (And if there's any other way to control the crossfade setting from within the game.)
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Mon 21/05/2018 15:22:57
Quote from: Snarky on Mon 21/05/2018 15:11:57
Well, one question we never got sorted was quite how OPT_CROSSFADEMUSIC interacts with the Crossfade option on the AudioTypes, if bx83 is right that setting the OPT_CROSSFADEMUSIC value to 0 actually disabled crossfading temporarily. (And if there's any other way to control the crossfade setting from within the game.)

Oh, right.
When possible, AGS emulates old style script commands through the new system.
From my previous findings in the code it looks like changing OPT_CROSSFADEMUSIC actually sets "Crossfade speed" option in the audio type #2, which corresponds to the legacy "music" type.
In other words, if you delete or otherwise change default audio types you have in project, this may work differently.
For the reference, default audio types are:
#0: I thought it is "speech", but not sure if it is used in code at all.
#1: Ambient Sound
#2: Music
#3: Normal sound

Unfortunately, I do not see any other way to change "Crossfade" setting in script, although there does not seem to be anything to prevent changing it at runtime. So perhaps implementing such command was low priority.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Tue 22/05/2018 03:16:27
I have one more question to ask: Did anyone figure out the volume problem (of going crazy unless manually set everytime to something with channel.Volume= ?)
If you feel you've already answered this just say and I'll review.
My most recent working code is up; and the manual setting of volume is the only things that stops it going up and down.
Also I've changed VolumeReductionWhileSpeechIsPlaying in the editor, to 0 for all AudioTypes, and let it do it manually. Same problem.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Tue 22/05/2018 15:59:10
Quote from: bx83 on Tue 22/05/2018 03:16:27
I have one more question to ask: Did anyone figure out the volume problem (of going crazy unless manually set everytime to something with channel.Volume= ?)
If you feel you've already answered this just say and I'll review.
My most recent working code is up; and the manual setting of volume is the only things that stops it going up and down.
Also I've changed VolumeReductionWhileSpeechIsPlaying in the editor, to 0 for all AudioTypes, and let it do it manually. Same problem.

Could you explain what is going on with the volume again, in detail? I think got confused by this thread, because the code and audio settings were changed many times.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Snarky on Tue 22/05/2018 19:04:57
Yeah, I'm also not sure exactly what behavior you're talking about. Which AudioType volumes are going crazy again?

Quote from: Crimson Wizard on Mon 21/05/2018 15:22:57
For the reference, default audio types are:
#0: I thought it is "speech", but not sure if it is used in code at all.

I don't think there's an accessible AudioType for speech (or that you can ever access the audio clips), but AudioChannel 0 (IIRC) is always reserved for speech, and when speech is playing, AudioChannel.IsPlaying will return true.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: Crimson Wizard on Tue 22/05/2018 19:51:40
Quote from: Snarky on Tue 22/05/2018 19:04:57
I don't think there's an accessible AudioType for speech (or that you can ever access the audio clips), but AudioChannel 0 (IIRC) is always reserved for speech, and when speech is playing, AudioChannel.IsPlaying will return true.

Hmm, you can call "SetAudioTypeVolume" and "SetAudioTypeSpeechVolumeDrop" for audio type 0, but that won't do anything, because it first checks if there is a valid AudioClip associated with the sound.

OTOH, that never came to me that you can use AudioChannels[0].Volume. Does this mean you may play with speech volume? Also what will happen if you do AudioChannels[0].Stop()? This suddenly opens alot of unusual (and undocumented) options.
EDIT: Oh wait, speech is blocking, so that will work only in rep_exec_always. Probably.
Title: Re: Should I start Music in Room_load of Room_Afterfadein?
Post by: bx83 on Wed 23/05/2018 13:39:08
Don't worry guys; having been dragged to my whits end, so I just simplified: I made 'Sounds' AudioType 'Ambient' (only get's used in rooms 7,2,3); run the song straight off, so it's persistent; not buggery with PlayFrom, oceanThemePos etc; and have nice long pauses between when the Music starts amd the Ambient fades in (which is now only once when the game starts; elsewhere it's just .Stop'ed or .Play'ed - it's ambient after all, so no-one remebers where it begins and ends).

Got rid of reduce volume when speaking; I'm just playing the sound/music quietly and 'naturally', so the player doesn't consciously notice much about sound, and instead just 'eases into it'. Anyway, the sounds are not too loud.

Both Music and Ambient are set maxchannel 1, crossfade on.
All sound effects are 'Extra' (infinite channels, no crossfade)
'Footprints' sounds are set to a nice low volume, but always audible; and it works with ScaleVolume as well.

Now, not setting the volume manually work's fine - sound is now set to default volume in editor, and holds this throughout (which was the problem I was speaking about before - volume went up and down randomly).

I had too many things on Sound. Or something. I don't know. I'll post code tomorrow when I've tested it and I'm sure (this time - sure!!!) the room(s) works. I'm beginning to understand AudioChannel etc. more fully. I'm sorry for only being capable of half a thought at a time and constant experimentation with ever-changing code.