Game Development Community

AlxGetStreamDuration() and alxGetWaveLen()

by Gellyware · in Torque Game Builder · 09/15/2006 (6:45 pm) · 11 replies

Anyone have a practical example of how these two functions work?

alxGetStreamDuration() and alxGetWaveLen()




I've tried the following with no success:

new AudioProfile(smoothJazz1)
{
filename = "~/data/audio/smoothJazz1.ogg";
description = "AudioNonLooping";
preload = true;
};


%songName = "~/data/audio/smoothJazz1.ogg";
%songLength = alxGetWaveLen( %songName );




note: songLength returns -1;


I'm trying to find out the length of an audio sample that is loaded.

#1
09/16/2006 (3:53 pm)
Anyone?
#2
09/16/2006 (4:30 pm)
Gellyware, try playing the audio file, then passing the handle to alxGetStreamDuration/alxGetWaveLen(). Fior example:

new AudioProfile(smoothJazz1)
{
filename = "~/data/audio/smoothJazz1.ogg";
description = "AudioNonLooping";
preload = true;
};

%handle = alxPlay(smoothJazz1);
echo(alxGetStreamDuration(%handle));
#3
09/16/2006 (5:45 pm)
AlxGetStreamDuration Still returning -1 on this end.
alxGetWaveLen returns 0.



Have you had any luck assigning a handle?



From the online docs:

alxGetStreamDuration( handle )

Purpose
Use the alxGetStreamDuration function to determine the length of a previously set up sound in seconds.

Syntax
handle - The ID (a non-negative integer) corresponding to a previously set up sound source.

Returns
Returns -1 for invalid handle, and 0.0 to N.M for valid handle indicating length of scheduled sound in seconds.

See Also
alxGetStreamPosition



I'm not quite sure what this part means exactly under the handle syntax:
"The ID (a non-negative integer) corresponding to a previously set up sound source."

What is a previously set up sound source? Wouldn't the way that I showed above with the datablock be a previously set up sound source?
#4
09/16/2006 (6:09 pm)
Gellyware, there could be two issues that would cause it to return -1 or 0.

1) Your AudioDescription (in your example called 'AudioNonLooping') might not be specifying to stream your audio file. Make sure you have 'isStreaming = true;' in it. But be warned streaming short audio files doesn't work in Torque (ie, if the clip is only a second or so long it probably won't play in streaming mode). Streaming large files, such as music, work great, but sound effects generally should not be streamed.

2) Torque can't find your audio file specified in your AudioProfile. After the '%handle = alxPlay(smoothJazz1);' line try echoing out the value of %handle. If it's 0 then it couldn't play the file. If you can hear the clip play, then %handle should be a value greater than 0.

BTW, when it says "The ID (a non-negative integer) corresponding to a previously set up sound source." it is talking about the ID handle returned when calling alxPlay.

- Houdini
#5
09/16/2006 (6:21 pm)
Thanks Robert for taking some time to help with this.

I'm still not getting far with it. Here is my audio profile:

new AudioDescription(AudioNonLooping)
{
     volume = 1.0;
     isLooping= false;
     isStreaming = true;
     is3D = false;
     type = $GuiAudioType;
};

here is the smooth jazz 1 profile

new AudioProfile(smoothJazz1)
{
     filename = "~/data/audio/smoothJazz1.ogg";
     description = "AudioNonLooping";
     preload = false;
};

and here is the code for trying to get the length of the song:

$game.musicChannel = alxPlay($game.musicSamplePlaying);
      
      //find out how long the music sample is in millesecs
      %songLength =  alxGetStreamDuration(  $game.musicChannel   );

      echo(%songLength); //returns -1


The music does play fine, but since I can't get the length of the song, I can't schedule the 'music jukebox' to play another song.

The $game.musicSamplePlaying is simply a randomized song (smoothJazz1 through smoothJazz5)
#6
09/17/2006 (4:23 pm)
Does anyone know how to get the alxGetStreamDuration() function to work?

Please post a working example if you have one.
#7
09/18/2006 (1:30 pm)
Is GG Support out there? Ive tried to figure out how to get a songs length for days now, tried all of the help on the site that I could find, all documentation, and countless hours of experimentation.

How can I get the length of an ogg music file so that I can schedule another song to come on when the first one is finished playing?
#8
09/18/2006 (3:26 pm)
AlxGetStreamDuration returns the number of seconds the music lasts. So you want to set a schedule for that * 1000 which will be pretty much exactly at the end of that song. This works for me.. of course there is probably a better/more flexible way to do it but here is a very simple method.

function load_bg_music( %i ){              
    if ( !%i ) %i = 0;    
    switch( %i ){
      case 0: 
          $bg_music = alxPlay( track0 );         
      case 1:             
          $bg_music = alxPlay( track1 );         
      default: 
           %i = -1;   //or alxStop($bg_music) or whatever you want.
    }     
    %i++;       
    $change_music = schedule( alxGetStreamDuration(  $bg_music ) * 1000 , 0, "load_bg_music", %i );     
}
#9
09/18/2006 (10:01 pm)
Thanks for the example Joe.

Can you show me how you define track0 and track1?
#10
09/18/2006 (10:37 pm)
Sure it's just an AudioProfile.
$channel1     = 1; 
new AudioDescription(stream){
   volume   = 1.0 ;
   isLooping= 0;
   is3D     = 0;
   isStreaming =  1 ; 
   preload     =  0 ;
   type        = $channel1;   
};

new AudioProfile( track0 ){
   volume   =  1.0 ;
   filename = "~/data/audio/song1.ogg"; 
   description =  stream; 
};

new AudioProfile( track1 ){
   volume   =  1.0 ;
   filename = "~/data/audio/song2.ogg"; 
   description =  stream; 
};
#11
09/19/2006 (11:58 am)
Hi Joe, That worked like a charm (not sure what is different compared to what I had above).

Thank you :)