Determine total sound playing length (ms)
BlitzMax Forums/BlitzMax Beginners Area/Determine total sound playing length (ms)| 
 | ||
| Hi ! you use a sound with loadsound, now... (an .ogg file) Is there a command to dertermine the total sound length (in milliseconds) ? Many Thanks | 
| 
 | ||
| after a bit of investimigation: 
SuperStrict
Local sample:TAudioSample = LoadAudioSample("battle.ogg")
Local spm:Float = sample.hertz * 60
Local timeSeconds:Float = (sample.length / spm) * 60
Print timeSeconds
 | 
| 
 | ||
| Does that still work with variable bitrate OGG files? | 
| 
 | ||
| *EDIT* I just tested it with OGGs of different bitrates and the results are consistent, I can only suggest trying it. | 
| 
 | ||
| I just dug out a few VBR OGG files and tested it too - works fine. | 
| 
 | ||
| cool :) *EDIT* just for the hell of it, I converted it to a function: 
SuperStrict
Local sample:TAudioSample = LoadAudioSample("battle.ogg")
Local length:Float = getSampleLength(sample)
Print "Length in seconds: " + length
Print "Length: " + Int(Floor(length/60))+"m "+Int(length Mod 60)+"s"
Function getSampleLength:Float(sample:TAudioSample)
	Return Float( (sample.length / Float( (sample.hertz * 60) ) ) * 60)
End Function
 | 
| 
 | ||
| many thanks. but what's the difference between  s1 : TSound = loadsound and s2:TAudioSample = LoadAudioSample ? Later in your code could you play s2 with playsound ? Length don't exist with an TSound object ? | 
| 
 | ||
| 
SuperStrict
Local sample:TAudioSample = LoadAudioSample("battle.ogg")
Local length:Float = getSampleLength(sample)
Print "Length in seconds: " + length
Print "Length: " + Int(Floor(length/60))+"m "+Int(length Mod 60)+"s"
Local sound:TSound = LoadSound(sample)
Local channel:TChannel = PlaySound(sound)
While channel.playing()
Wend
Function getSampleLength:Float(sample:TAudioSample)
	Return Float( (sample.length / Float( (sample.hertz * 60) ) ) * 60)
End Function
 | 
| 
 | ||
| I am not a math expert but shouldn't the equation be Length/hertz? length ------- * 60 = Length/hertz 60*hertz | 
| 
 | ||
|  I am not a math expert but shouldn't the equation be Length/hertz? ... ..isn't that exactly what Perturbatio said? | 
| 
 | ||
| No. There is no need to multiply it by 60 at all. | 
| 
 | ||
|  No. There is no need to multiply it by 60 at all.  You're right of course, but in my defence, I wrote that code in a hurry and it changed several times before I got it right. |