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 Discussion / Getting Audio Position..

Author
Message
Grumpy Jedi
14
Years of Service
User Offline
Joined: 16th Jan 2010
Location:
Posted: 16th Jan 2010 11:03
I have a question about obtaining the position in bytes an audio file is at when you stop it, so that you can restart it using the Play Sound command from that position at a later time.

The syntax for the command is...
PLAY SOUND Sound Number, Start Position

Start Position is specified in Bytes.

Now bearing in mind that I can't use the Pause and Resume commands for what I'm doing. How can I obtain the current position in bytes, of a sound that is playing, prior to stopping it, for later use.

Or if that's not possible is there another way to track the current position of a sound, perhaps by time? Do sound files play at a certain # of bytes per second? If so, is it dependent on their bitrate, and is there a table for looking that up somewhere?

Thanks.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 16th Jan 2010 18:08 Edited at: 16th Jan 2010 18:18
Hello, welcome!

I haven't thought about this for a little while but let's see:

a few definitions first:
time = a floating point value of seconds

bit depth = the amplitude resolution; quality; usually 8, 16, or 24 but I think the max in DBC is 16.

Channels = mono , or stereo = 1 or 2 channels

Sample Rate = how many small pieces of sound pass through the sound generator in a second . Often stated as frequency this is not the PITCH of a sound but the rate at which the information in the file processed. The higher the rate, the more sound information per second, thus the better quality. CD quality is 44,100 samples per second. DVD sound quality averages about 96,000 samples per second

bytes=((time in seconds) X (bit depth / 8) X (channels) X (sample rate)) + header size

header = the file header. Can differ between sound formats. Once loaded into DBC, I'm not sure how the header is treated unless the sound is converted to a memblock in which case the header is 12 bytes.

so if you had a monophonic sound playing at a sample rate of 44.1khz for 2 seconds with a bit quality of 16 the byte position would be

bytes = (2 * (16/8) * 1 * 44100) + (header size)

I'm not sure if once the sound is loaded it is converted to a memblock format internally with a custom header in which case the header size is 12 bytes . But the header size might be 0 depending on how the sound is stored internally. The calculation is based on the file itself (RIFF .wav) . So it's probably a safe bet to leave off the header size in the calculation. You can experiment a little bit to see how close the position is.

Enjoy your day.
Grumpy Jedi
14
Years of Service
User Offline
Joined: 16th Jan 2010
Location:
Posted: 17th Jan 2010 08:33
Thanks for the reply Latch. Lots of great information there.

I've run into a problem. I plugged in the formula, checked the math, all is well there..

However when attempting to call Play Sound using the start position parameter, it always starts from the beginning of the sound.

Is the Start Position parameter broken in Dark Basic Classic?

I tried the command and parameter in DBPro and it started the sound at the proper position with no problems.

However attempting the same command in DB Classic ignores the start position parameter and plays the sound from the beginning.

How can I fix this?
Grumpy Jedi
14
Years of Service
User Offline
Joined: 16th Jan 2010
Location:
Posted: 18th Jan 2010 09:05
I did some searching and apparently the Play Sound command for DB Classic is bugged

Info in this thread http://forum.thegamecreators.com/?m=forum_view&t=34472&b=10

This throws a huge wrench in my plans.

Is there any other way to restart a sound from a specified position from within DB Classic without using the Play Sound command?
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 18th Jan 2010 16:06 Edited at: 18th Jan 2010 16:18
Quote: "Is there any other way to restart a sound from a specified position from within DB Classic without using the Play Sound command? "

Yes, there are a few tricks I can think of while I'm writing this. Just to note, there are a few buggy sound/music controls in DBC. Some have to do with the midi setup on the computer, some have to do with actual commands not working right like PLAY SOUND (which I hadn't tried the positioning part until now!)

Here's Something that you may want to try: save a copy of your sound as a monophonic sound. Then load the copy using LOAD 3DSOUND. This will force the sound to use Direct Sound as opposed to windows multimedia (it may be using direct sound anyway but this will just make sure as a test). Then try playing your sound with positioning. Also try loading the monophonic copy using the regular load sound command and see if that makes any difference. I mention the mono sound because in order to get spacial positing in Direct Sound (in DBC) using any of the 3d sound position commands or EAX effects, the sound has to be monophonic. Because of this, play sound might be temperamental in playing back portions.

Let me know how the above goes. Give me a couple of days, as an alternative to creating custom sound files representing portions of the desired playback using memblocks (which was one of the ideas that popped into my head), I think Windows itself can help us out.

Enjoy your day.
Grumpy Jedi
14
Years of Service
User Offline
Joined: 16th Jan 2010
Location:
Posted: 19th Jan 2010 18:38
Thanks for taking the time to help me with this Latch.

I tried monophonic sounds with both Load 3DSound and Load Sound. Both made no difference and still failed to start from a preset position.

I need to be able to restart a sound within approx 5ms or less of the position it's stopped at.

I'm thinking the timing needed may be too precise when factoring in other things such as program loop time.

I'm leaning toward scrapping this particular part of the program and going in a different direction.

However, I'm still curious how...

Quote: " "Windows itself can help us out" "
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 20th Jan 2010 03:45
i got an idea. since you already have the position of the sound. why not. make a temporary memblock,take the rest of the sound that u havent played and the header and send it to the temporary memblock. then, make a sound from the temporary memblock and play it, then delete it and resume playing the original sound.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 20th Jan 2010 22:41 Edited at: 20th Jan 2010 22:45
Quote: "However, I'm still curious how...

Quote: " "Windows itself can help us out" ""

Sorry, I've been a little busy lately. Windows has some built in media commands that are quite useful and easy to use. There is a dll called winmm.dll and should be included with each windows version since around 3.1 or 95 .

There is a command mciSendStringA that would allow one to control audio and video through DarkBASIC .

I started poking around with this yesterday and was successful in getting DBC to load sounds, play them, pause them, resume... the only problem occurred when I'd exit (F12) out of DBC while testing. If I didn't remember to send a command to close out all of the sounds that I had loaded or played, an instance of DB.exe would remain active in the background - only visible if you looked at the processes running. I didn't notice until after I ran a few tests my computer started crawling to a halt and then eventually crashed. When I looked to see what was going on, there were many DB.exes running as processes and I couldn't stop them!

So, I decided to make a custom dll that uses the winmm commands. That way, if my dll is loaded into DBC, upon exit, the DLL will automatically run some cleanup to make sure all the sounds are turned off properly. I hadn't really intended to make a DLL, but now that I've started I may as well add at least some of the basic sound functionality you can find in DBC. One thing that I put in my dll that I like, is a get_sound_position() function that returns the current position in milliseconds. Can be very useful and it seems like it's something you were after in your original post. There's also a working play from to function that plays positions using milliseconds.

If I have a little time today to mess around, I'll probably have the dll and a DBC library of functions ready to be played with and will post it if you or anybody else is still interested.

Enjoy your day.
Grumpy Jedi
14
Years of Service
User Offline
Joined: 16th Jan 2010
Location:
Posted: 21st Jan 2010 10:18
Wow that sounds great Latch!

That would make the DB Classic sound functions complete!

Quote: "There's also a working play from to function that plays positions using milliseconds.
"


Does your play from to function return an error if you input a "to" value longer than the sound? If so, is a Get_Sound_Length() function that returns sound length in milliseconds possible?
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Jan 2010 18:01 Edited at: 23rd Jan 2010 11:09
Quote: "Does your play from to function return an error if you input a "to" value longer than the sound? If so, is a Get_Sound_Length() function that returns sound length in milliseconds possible?"

Yeah, there is a get_sound_length command. And also if the to position is < the from position, it will just play to the end of the sound. And let me test if the to position is greater than the length of the song. I didn't put any checks in for lengths greater than the song length because I was relying on MCI(the windows media commands) to handle that. But anyway, testing now...

Ok just tested it, if the from or the to position is greater than the length of the song an error is generated and reported. If they are less than 0, then the song plays from beginning to end.

Sorry, I've been busy lately and haven't had a lot of time to play with this. it's pretty much ready to go, I was just hoping to get a cool demo for it created, but really don't have any ideas that are simple and still showcase all of the options. Hopefully, I will have something posted up today.

[EDIT]
Ok, the dll can be found here:

jkkaudio.dll

Enjoy your day.
Grumpy Jedi
14
Years of Service
User Offline
Joined: 16th Jan 2010
Location:
Posted: 24th Jan 2010 13:17
Great stuff Latch,

Thanks for all your help with this and for making a dll I'm sure many will find useful.

I decided to scrap a couple of things in my program, because of timing issues, that ended up creating a headache if task switching was thrown into the works.

So I won't end up using the dll this time. But I will put it to good use in the future

Login to post a reply

Server time is: 2024-04-23 13:41:48
Your offset time is: 2024-04-23 13:41:48