Page 1 of 1

Duration of a Sound

Posted: Tue Jan 16, 2007 6:18 am
by akee
How do you detect the sounds duration after loading it with LoadSound()?

Posted: Wed Jan 17, 2007 5:42 pm
by Fluid Byte
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.

Posted: Thu Jan 18, 2007 4:07 am
by akee
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... :) Thanks in advance. ;)

Posted: Thu Jan 18, 2007 3:48 pm
by Fluid Byte
Maybe a bit of an overkill but here you go:

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
Hope it helps a little. :)

Posted: Thu Jan 18, 2007 4:12 pm
by traumatic

Posted: Thu Jan 18, 2007 5:11 pm
by akee
Thanks Fluid Byte... :)

Posted: Fri Feb 02, 2007 1:26 pm
by Fluid Byte
@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!

Posted: Fri Feb 02, 2007 1:42 pm
by traumatic
Fluid Byte! :shock:

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.

Posted: Fri Feb 02, 2007 3:27 pm
by Fluid Byte
Fluid Byte! :shock:


Well, well... Image

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

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

Posted: Fri Feb 02, 2007 3:45 pm
by traumatic
Hey no problem, just ask anything you want. I'm always glad if I can help.

Posted: Fri Feb 02, 2007 3:47 pm
by akee
Looks like Dr. Dri has already answered my question. :)


http://www.purebasic.fr/english/viewtopic.php?t=23336

Posted: Fri Feb 02, 2007 3:49 pm
by Derek
OT. @Fluid Byte, like your smiles
Fluid Byte wrote:
Image
Image
Image

Posted: Fri Feb 02, 2007 4:22 pm
by Kaeru Gaman
Derek wrote:OT. @Fluid Byte, like your smiles
me too

Image . Image . Image . Image . Image . Image

Image . Image . Image . Image . Image . Image

Image . Image . Image . Image . Image . Image

Image . Image . Image . Image . Image . Image

.....I'll bet he'll kill me for that.... Image

Posted: Fri Feb 02, 2007 6:17 pm
by Fluid Byte
....I'll bet he'll kill me for that....
Oh you little.... Image

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

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! Image

Re: Duration of a Sound

Posted: Sun Feb 04, 2007 12:35 pm
by dobro
akee wrote:How do you detect the sounds duration after loading it with LoadSound()?
excuse dont speak english

pour la longueur en milliseconde =
;longueur en ms=1000*longueur / taux d'echantillonage
-longueur= taille en octets du son
-taux d'echantillonage en hz

en plus clair:
; temp=(1000*longueur/ Frequence )
:D