Duration of a Sound

Just starting out? Need help? Post your questions and find answers here.
akee
Enthusiast
Enthusiast
Posts: 498
Joined: Wed Aug 18, 2004 9:52 am
Location: Penang, Malaysia

Duration of a Sound

Post by akee »

How do you detect the sounds duration after loading it with LoadSound()?
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
akee
Enthusiast
Enthusiast
Posts: 498
Joined: Wed Aug 18, 2004 9:52 am
Location: Penang, Malaysia

Post 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. ;)
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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. :)
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Good programmers don't comment their code. It was hard to write, should be hard to read.
akee
Enthusiast
Enthusiast
Posts: 498
Joined: Wed Aug 18, 2004 9:52 am
Location: Penang, Malaysia

Post by akee »

Thanks Fluid Byte... :)
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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!
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post 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.
Good programmers don't comment their code. It was hard to write, should be hard to read.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Hey no problem, just ask anything you want. I'm always glad if I can help.
Good programmers don't comment their code. It was hard to write, should be hard to read.
akee
Enthusiast
Enthusiast
Posts: 498
Joined: Wed Aug 18, 2004 9:52 am
Location: Penang, Malaysia

Post by akee »

Looks like Dr. Dri has already answered my question. :)


http://www.purebasic.fr/english/viewtopic.php?t=23336
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

OT. @Fluid Byte, like your smiles
Fluid Byte wrote:
Image
Image
Image
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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
oh... and have a nice day.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Duration of a Sound

Post 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
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Post Reply