Duration of a Sound
Posted: Tue Jan 16, 2007 6:18 am
How do you detect the sounds duration after loading it with LoadSound()?
http://www.purebasic.com
https://www.purebasic.fr/english/
Fluid Byte wrote:I guess the return handle of LoadSound() can be used with DirectSound but unfortuneatly I'm not familar with this. But what I could do, is to show you how to use Windows' multimedia library functions to obtain the sound length.
Lemme know if your're interested.
Code: Select all
Structure WAVEFORMATEX
wFormatTag.w
nChannels.w
nSamplesPerSec.l
nAvgBytesPerSec.l
nBlockAlign.w
wBitsPerSample.w
cbSize.w
EndStructure
Define lpck.MMCKINFO, wfmt.WAVEFORMATEX
If InitSound() = 0
MessageRequester("Error","DirectX 7 isn't available or no sound card present.",16) : End
EndIf
Filename$ = OpenFileRequester("","","WAVE Files (*.wav)|*.wav",0)
If Filename$
HMMIO = mmioOpen_(Filename$,0,#MMIO_READ)
; FILE FORMAT
lpck\fccType = mmioStringToFOURCC_("WAVE",0)
Result = mmioDescend_(HMMIO,lpck,0,#MMIO_FINDRIFF)
If Result <> #MMSYSERR_NOERROR
MessageRequester("Error","Not a wave file!",16) : End
EndIf
; READ FORMAT CHUNK
lpck\ckid = mmioStringToFOURCC_("fmt",0)
Result = mmioDescend_(HMMIO,lpck,lpck,#MMIO_FINDCHUNK)
If Result <> #MMSYSERR_NOERROR
MessageRequester("Error","Couldn't get format chunk!",16) : End
EndIf
; READ COMPRESSION TYPE
mmioRead_(HMMIO,wfmt,lpck\ckSize)
If wfmt\wFormatTag ! #WAVE_FORMAT_PCM
MessageRequester("Error","Compressed wave files aren't supported!",16) : End
EndIf
; READ DATA CHUNK
lpck\ckid = mmioStringToFOURCC_("data",0)
Result = mmioDescend_(HMMIO,lpck,lpck,#MMIO_FINDCHUNK)
If Result <> #MMSYSERR_NOERROR
MessageRequester("Error","Couldn't get data chunk!",16) : End
EndIf
; CALCULATE SOUND LENGTH
Duration = lpck\ckSize / wfmt\nAvgBytesPerSec
Hours = (Duration / 3600)
Minutes = (Duration / 60) - (60 * Hours)
Seconds = Duration - (60 * (Duration / 60))
Millisecs = Int(lpck\ckSize / (wfmt\nAvgBytesPerSec / 1000))
Hours$ = Str(Hours) : Minutes$ = Str(Minutes) : Seconds$ = Str(Seconds)
If Seconds < 10 : Seconds$ = "0" + Seconds$ : EndIf
If Minutes < 10 : Minutes$ = "0" + Minutes$ : EndIf
If Hours < 10 : Hours$ = "0" + Hours$ : EndIf
Time$ = Hours$ + ":" + Minutes$ + ":" + Seconds$
mmioClose_(HMMIO,0)
; DEBUG INFO
Debug "Soundfile = " + Filename$
Debug "Duration = " + Str(Millisecs) + " Ms"
Debug "Time Format = " + Time$ + " (H:M:S)"
EndIfFluid Byte!![]()



Fluid Byte wrote:
me tooDerek wrote:OT. @Fluid Byte, like your smiles
.
.
.
.
. 
.
.
.
.
. 
.
.
.
.
. 
.
.
.
.
. 

Oh you little........I'll bet he'll kill me for that....



excuse dont speak englishakee wrote:How do you detect the sounds duration after loading it with LoadSound()?
-longueur= taille en octets du son;longueur en ms=1000*longueur / taux d'echantillonage
; temp=(1000*longueur/ Frequence )