Determine total sound playing length (ms)

BlitzMax Forums/BlitzMax Beginners Area/Determine total sound playing length (ms)

hub(Posted 2007) [#1]
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


Perturbatio(Posted 2007) [#2]
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



GfK(Posted 2007) [#3]
Does that still work with variable bitrate OGG files?


Perturbatio(Posted 2007) [#4]
*EDIT*
I just tested it with OGGs of different bitrates and the results are consistent, I can only suggest trying it.


GfK(Posted 2007) [#5]
I just dug out a few VBR OGG files and tested it too - works fine.


Perturbatio(Posted 2007) [#6]
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



hub(Posted 2007) [#7]
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 ?


Perturbatio(Posted 2007) [#8]
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



Jesse(Posted 2007) [#9]
I am not a math expert but shouldn't the equation be Length/hertz?

length
------- * 60 = Length/hertz
60*hertz


GfK(Posted 2007) [#10]
I am not a math expert but shouldn't the equation be Length/hertz?
...

..isn't that exactly what Perturbatio said?


Jesse(Posted 2007) [#11]
No. There is no need to multiply it by 60 at all.


Perturbatio(Posted 2007) [#12]
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.