Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Detecting Length Of A Song

Author
Message
eatsNoobs
14
Years of Service
User Offline
Joined: 30th Apr 2012
Location:
Posted: 22nd May 2012 17:24
Is it possible in dark basic pro to detect the length of an mp3 song and assign that length to a variable?
cyril
18
Years of Service
User Offline
Joined: 6th Aug 2007
Location: 7 miles away from big ben
Posted: 22nd May 2012 18:25 Edited at: 22nd May 2012 18:48
There is not a direct way in darkbasic pro but you can detect whether the music is playing with the command : [Music Playing(musicnumber)]

You can then start a timer by passing the [Timer()] to a variable and check each cycle with the [Music Playing(musicnumber)] until the music ends in which case the [music Playing will return 0, once this has happens you can use the Timer()-Theoldtime to give you the duration.

Of course this is a slow method; so you can speed it up using the [set music speed] command and then convert to time obtained by multiplying it by the multiple of speed.

IE: If the speed was 1000%, you would multiply the duration value by 10 to obtain the true duration or

100*10=speed for duration*10=True duration

10 is the factor.

Word of warning though, if you choose to speed up the music by a factor, remember that the program also takes some time, so the higher the factor the faster but more inaccurate the result will be; The lower the factor the slower but more accurate the result will be.

before playing the music to test it's length turn the volume down.

Edit: here's the code this is without the speed factor though:



Edit 2: altered the code to return seconds instead of milliseconds
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 22nd May 2012 21:57
If it's a constant bit-rate MP3, you could calculate it from the size of the file too.

For example 10MB file at 128kbps
10,000,000 * 8 = 80,000,000 bits (ignore 1024 for simplicity)
/ 128,000 =
625 seconds

TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 22nd May 2012 22:19
@ Fallout

That would only be an estimation though, because the MP3 is compressed...

I suggest doing it with the FMOD dll. I've attached it and the help files, but I have no clue how to make it work in DBP. And before someone ignorant and incapable of using the forum search comes along and tries to tell me it's impossible, I'll link you to a thread that very well made it possible : http://forum.thegamecreators.com/?m=forum_view&t=140341&b=1

The function you're after is Channel::getLength()

TheComet

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd May 2012 22:55
Quote: "That would only be an estimation though, because the MP3 is compressed..."


Well it's the bit-rate after compression that is kept constant, so it should still be fairly accurate.

[b]
Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 22nd May 2012 23:17
Yeah, the bitrate is the data/second of compressed audio.

Kalimba.mp3 in the Win7 Music Samples folder (I assume everyone has that) works out like this:

Size on disk: 8,414,449 = 67315592 bits
192kbps MP3 = 192000 (so divide by that)
= 350 seconds = 5:50

Windows Media Player reports it as 5:48

The discrepancy might be in the header. You might be able to subtract that if you knew how big it was, but it's fairly accurate.

TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 22nd May 2012 23:52
Huh, you're right... I didn't expect the results to be that accurate.

TheComet

Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 23rd May 2012 00:02 Edited at: 23rd May 2012 00:10
My think Tank project uses the timer(). I just fade the volume down before the song ends, and the next song is blended into it. All I did was time the songs with my watch. If you get to the Firestarter song (about 6:07) , I also timed the flamethrower to the music as well.

http://www.youtube.com/watch?v=zBtt-iVqvFE

Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 23rd May 2012 08:36
Quote: "Huh, you're right... I didn't expect the results to be that accurate."


I've spent too much time encoding MP3s my friend!

Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 23rd May 2012 11:06 Edited at: 23rd May 2012 21:33
With the magic of good ol' MCI, you can get the song length of any type of media - assuming the CODEC is installed on the Windows System. MP3 is, I think, on everything from Windows 98 and up.

[EDIT]
One major thing I left out in the original post: the filename and/or path has to be enclosed in double quotes when it is send to the winmm.dll. This can be done as follows:

filename$=chr$(34)+"mymp3.mp3"+chr$(34)

[/EDIT]

Here ya go - should be a universal function. You can take out or the error checking or modify what happens if there is an error. This is written for DBC but should work in DBPro. The function is the work horse:



Enjoy your day.
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 23rd May 2012 11:34
Quote: "I've spent too much time encoding MP3s my friend! "


Dwwaa, silly me, I even listen to your music! Speaking of which, are you ever going to upload those MP3's? The youtube quality just doesn't cut it.

@ Latch

That is amazing... You're one of the few people that really knows how to use the Windows DLLs. There should be a Latch-Pack or something containing all of those examples.

TheComet

Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 23rd May 2012 12:15
@TheComet - They are uploaded. I just don't like the post the link on YouTube. http://www.hangfiresoftware.com/fallout.html

Yep, Latch's solution is the winner.

Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 23rd May 2012 21:56
Oops! I left out a very important part, the file name or complete path and file name has to be contained in double quotes when sent to the DLL so it can be read as a string correctly. I've edited the code to reflect that.

Quote: "You're one of the few people that really knows how to use the Windows DLLs."


I just know a couple of tricks when it comes to something I was interested in before - like music stuff and DB for example.

Enjoy your day.
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 24th May 2012 00:43
Quote: "I just know a couple of tricks when it comes to something I was interested in before - like music stuff and DB for example."


And it has been a huge help for everyone. Seriously, you should put together that Latchpack and upload it. ^^

@ Fallout

Just a heads up, your links are all broken on my computer because it uses a backslash in the link. If I change it to a forward slash it works. Anyway, thanks for directing me to that, I'm doing my best to download them all. Can't wait to listen to some of the new things you made.

TheComet

Fallout
23
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 24th May 2012 08:50
Quote: "Just a heads up, your links are all broken on my computer because it uses a backslash in the link. If I change it to a forward slash it works. Anyway, thanks for directing me to that, I'm doing my best to download them all. Can't wait to listen to some of the new things you made."


Ahh. Thanks for the heads up. I don't send many people that way, but I'll sort it out. As for 'new things', a lot of that stuff is up to 10 years old, so don't expect the quality of the stuff on YouTube.

Login to post a reply

Server time is: 2026-07-22 00:35:42
Your offset time is: 2026-07-22 00:35:42