Duration of a Sound
Duration of a Sound
How do you detect the sounds duration after loading it with LoadSound()?
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
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.
Lemme know if your're interested.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
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.
Sure I'm interested.... That's why I posted the request in the first place...


- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
Maybe a bit of an overkill but here you go:
Hope it helps a little. 
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)"
EndIf

Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Maybe http://purebasic.fr/english/viewtopic.php?t=13282 and
http://purebasic.fr/english/viewtopic.php?t=20841 could be of
some use for you as well.
http://purebasic.fr/english/viewtopic.php?t=20841 could be of
some use for you as well.
Good programmers don't comment their code. It was hard to write, should be hard to read.
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
@traumatic:
This some excellent code but I have some general questions which you can maybe answer:
1.) Where do these interfaces come from? Is this built into Windows or do they come from a application like Windows Media Player?
2.) In wich form they are distributed? Single or multiple DLL's? Some sort of custom library?
3.) Does a program like Windows Media Player uses all of these interfaces like in your example or just some of them?
4.) Since wich version of Windows these intefaces have been available respectivley are there differences between the amount of command between the versions?
Thanks in advance!
This some excellent code but I have some general questions which you can maybe answer:
1.) Where do these interfaces come from? Is this built into Windows or do they come from a application like Windows Media Player?
2.) In wich form they are distributed? Single or multiple DLL's? Some sort of custom library?
3.) Does a program like Windows Media Player uses all of these interfaces like in your example or just some of them?
4.) Since wich version of Windows these intefaces have been available respectivley are there differences between the amount of command between the versions?
Thanks in advance!
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Fluid Byte! 
It's part of DX, the MediaPlayer just happens to use the same,
so in case you don't have MediaPlayer installed, these methods will work
anyway.
I guess all of this is avaible since DirectX 6.1 but I don't know for sure.
Please look up the interfaces and methods that interest you in MSDN.
There should be "available OS'" etc. tagged underneath each entry.
As the shown code interfaces PureBasic's Functions, it's safe to assume
it works with DX7, as PB uses that. That would mean it works on every
MS OS except for NT4.
Hope that helps.

It's part of DX, the MediaPlayer just happens to use the same,
so in case you don't have MediaPlayer installed, these methods will work
anyway.
I guess all of this is avaible since DirectX 6.1 but I don't know for sure.
Please look up the interfaces and methods that interest you in MSDN.
There should be "available OS'" etc. tagged underneath each entry.
As the shown code interfaces PureBasic's Functions, it's safe to assume
it works with DX7, as PB uses that. That would mean it works on every
MS OS except for NT4.
Hope that helps.
Good programmers don't comment their code. It was hard to write, should be hard to read.
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
Fluid Byte!![]()
Well, well...

Since it's all based on DX it makes all my other questions redundant...

I think I was just a little consfused when posting this. Sorry for bugging you!

Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Looks like Dr. Dri has already answered my question. 
http://www.purebasic.fr/english/viewtopic.php?t=23336

http://www.purebasic.fr/english/viewtopic.php?t=23336
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
Oh you little........I'll bet he'll kill me for that....

Well, actually I stole them myself from http://isnichwahr.de/

BUT
if I'm not mistaken these are also the default smilies for MSN Messenger. At least for older versions. Haven't used it for while.
PS: I wrote a little application in PB for pasting the desired smiley image URL directly in the edit window of Firefox. I'm not that stupid and retype the URL everytime, just for the sake of entertaiment!

Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Re: Duration of a Sound
excuse dont speak englishakee wrote:How do you detect the sounds duration after loading it with LoadSound()?
pour la longueur en milliseconde =
-longueur= taille en octets du son;longueur en ms=1000*longueur / taux d'echantillonage
-taux d'echantillonage en hz
en plus clair:
; temp=(1000*longueur/ Frequence )
