AGS audiochannel question - PlayFrom and PositionMS

Started by bx83, Mon 02/10/2017 11:55:35

Previous topic - Next topic

bx83

Hi

I've written the folowing code for 2 rooms, Cave and Beach. Player starts out in Beach. The same song (or a very similar version called a1_beachTheme_5_0_CAVE) plays in Cave at exactly the same position; I keep the global variable beachThemePos to track the current position in the audio.

My question is:
a) why does it crash with 'beachThemePos' directly after I play 'chan1=a1_beachTheme_5_......'  ?  I initalise the variable; still crashes. I make sure MaxChannels is 50; still crashes.
b) how do I keep track of a track's position number? I thought it was by typing beachThemePos=chan1.PositionMs, but obviously not. It gives a position in the current track - but can I track this to be the one playing 'a1_beachTheme_...'? If a track is position [4], how do I make sure when I seek, it always does this to position 4? adiochannel.seek[4]? No idea how this works.

Beach:
Code: ags

function room_FirstLoad()
{
    SetGameOption(OPT_CROSSFADEMUSIC, 2);
}
 
function room_AfterFadeIn()
{
  if (cJulius.PreviousRoom == CAVE) {
    aWater_dripping.Stop();
  }
 
  if (chan1==null) {
    chan1=aOcean_crash.PlayFrom(oceanThemePos, eAudioPriorityHigh, eRepeat);
    chan1=a1_beachTheme_5_0.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);    //run the first time I'm in Bach -- ie. after game loads
  } else {
    chan1=a1_beachTheme_5_0.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);    //runs every time after that
  }             
}
 
function room_RepExec()
{
  beachThemePos=chan1.PositionMs;
}



Cave:
Code: ags

function room_AfterFadeIn()
{
  if (cJulius.PreviousRoom == BEACH) {
    chan1=a1_beachTheme_5_0_CAVE.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
  } 
        
  if (chan1.PlayingClip!=aWater_dripping) {
    chan1=aWater_dripping.Play(eAudioPriorityNormal, eRepeat);
  }
}
 
function room_RepExec()
{
  beachThemePos=chan1.PositionMs;
}

Snarky

Quote from: bx83 on Mon 02/10/2017 11:55:35
My question is:
a) why does it crash with 'beachThemePos' directly after I play 'chan1=a1_beachTheme_5_......'  ?  I initalise the variable; still crashes.

Almost certainly because chan1 is null (the error message should say what the problem is â€" why don't people read error messages?). Probably room_RepExec() runs a few times before room_AfterFadeIn()?

QuoteI make sure MaxChannels is 50; still crashes.

MaxChannels can't be 50, because there are only 8 audio channels in total.

Quoteb) how do I keep track of a track's position number? I thought it was by typing beachThemePos=chan1.PositionMs, but obviously not.

Yeah, that seems fine. Of course, you might not even really need a variable when you can just check chan1.PositionMs directly.

QuoteIt gives a position in the current track - but can I track this to be the one playing 'a1_beachTheme_...'?

It gives the position in the track currently playing on chan1. You have to keep track of whether this is actually the track you're interested in. Usually you only have one track for music (MaxChannels=1 for the Music AudioType), so whenever you play a new music track it will stop the old track (perhaps after crossfading out) and start the new track on the same channel.

QuoteIf a track is position [4], how do I make sure when I seek, it always does this to position 4? adiochannel.seek[4]? No idea how this works.

Huh? What does "position[4]" mean? You mean that chan1.PositionMs is 4? To play a track from a certain position, you use PlayFrom(), basically just like in your example, except that I would use AudioChannel.Position rather than PositionMs, since that's the form PlayFrom() expects. This only works as long as the two clips have the same file type and exactly the same length.

bx83

Here's the final working code:

Beach:
Code: ags


function room_FirstLoad()
{
  SetGameOption(OPT_CROSSFADEMUSIC, 2);
  chan1=aOcean_crash.Play(eAudioPriorityNormal, eRepeat);
  chan2=a1_beachTheme_5_0.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
}

function room_AfterFadeIn()
{
	if (cJulius.PreviousRoom == CAVE) {
		aWater_dripping.Stop();
		if (chan2!=null) {
			chan2=a1_beachTheme_5_0.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
		}
	}
	
}


Cave:
Code: ags

	
function room_AfterFadeIn()
{
	if (cJulius.PreviousRoom == BEACH) {
		chan2=a1_beachTheme_5_0_CAVE.PlayFrom(beachThemePos, eAudioPriorityNormal, eRepeat);
	} 
	
	if (chan1.PlayingClip!=aWater_dripping) {
		chan1=aWater_dripping.Play(eAudioPriorityNormal, eRepeat);
	}

}

function room_RepExec()
{
	beachThemePos=chan2.Position+1100;
}

SMF spam blocked by CleanTalk