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 the time for a sound

Author
Message
Manticore Night
22
Years of Service
User Offline
Joined: 23rd Oct 2003
Location: Ouinnipeg
Posted: 26th Apr 2008 04:21
Hello, I'm making a rhythm game and I have a problem. I'm trying to get a music track to play and have an array of what the play should be pressing sync up to it. Unfortunately the array progresses much slower than the song. So I was wondering if some one knew how I could either get it so that the song progresses each time I increase a variable or if there is some way to get the time that the song has been playing for in whatever DB uses to measure it. Does anyone have any suggestions?

It's amazing how much TV has raised us. (Bart Simpson)

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 26th Apr 2008 04:38
hmm....
you can set the sound speed although this isn't usually good for judging bpm.
You could make a timer that starts when the music plays, or if you're really clever copy the sound to a memblock and use the actual data.

It is far better to complete a 10 line program than to start a 10,000 line program.
Manticore Night
22
Years of Service
User Offline
Joined: 23rd Oct 2003
Location: Ouinnipeg
Posted: 26th Apr 2008 05:01 Edited at: 26th Apr 2008 06:02
Memblock, that that sounds like a good idea. Too bad I could never figure those out. How would you go about doing that?

[edit] Oops thats not a very comprehensible sentence right there.
I mean how would I use a memblock to get the current time (were it is in the sound)?

It's amazing how much TV has raised us. (Bart Simpson)

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 26th Apr 2008 06:13
unfortunately I am no expert on memblocks...
but the basic idea is that you'd look at the soundwave data; picking out peaks and troughs would give you the bpm, or you could even program an automated game that calls for a key press at major peaks.
It all sounds good but you'd need to ask someone else for the how-to
sorry

It is far better to complete a 10 line program than to start a 10,000 line program.
Manticore Night
22
Years of Service
User Offline
Joined: 23rd Oct 2003
Location: Ouinnipeg
Posted: 26th Apr 2008 06:49 Edited at: 26th Apr 2008 07:17
Well, I tried something got a bug though. If there's anyone out there that knows a lot about memblocks. I could sure use some help.


What I'm trying to do is cut up the sound into each sample so I can play it along with a variable "time#".

It gives me a severe exeception! error.

It's amazing how much TV has raised us. (Bart Simpson)

Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 26th Apr 2008 11:46
I'm a bit sleepy at the moment but maybe I can give you a couple of ideas. Your use of the # seems a bit strange. Are you aware that whenever you have a variable with the # after it in DBC it is considered a floating point value? You can't dimension arrays (or you shouldn't be allowed to) with a float value. Even though I think DBC may let you get away with it, it's a bad habit to fall into and will bite you later on. I wouldn't be surprised if part of the sever exception comes from that because I don't know how memory would be allocated for such a call.

Anyway, digital music is sampled sound where a sample is the smallest unit representing an amplitude (volume) at a specific time in the entire sound file. A sample rate tells how many samples pass by in a second. The bit depth represents the resolution of the sample. The resolution can be thought of as how fine the increments between amplitudes can be. The frequency, is how often a certain arrangement of samples (usually in some kind of wave shape) occur within a second.

So, what does all this mean? The frequency won't really be meaningful for what you're trying to do so we can skip it for now. The bit depth in combination with sample rate and one thing I forgot to include is the channels - simultaneous occurances of samples (think of it as stereophonic or monophonic) - will determine the number of bytes that occur within a second. With this information, you can sync any time code you want with a certain number of bytes that you can calculate out. You can also do what Obese87 was saying where you search through the data and find occurances over or at certain amplitudes over a selected data sample. You look for repeating patterns of the same peaks or valleys within a tolerence. But that can get complicated.

Anyway, try to calculate out the time for a sound file you load into DBC. Use one that you actually know the time of and try to calculate it. The number of bytes per second should be
(bit depth/8)*channels*sample rate .

Then if you know the number of bytes for the whole file (if you make memblock from sound you can get the size of the memblock with
value=GET MEMBLOCK SIZE(<mem>-header which I think is 12)
divide that by the number of bytes you figured out per second.

If you come out with the length in seconds of the sound file, then you're on your way. But my eyes are closing right now so I'm not even sure how accurate what I typed is.

Enjoy your day.
jinzai
19
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 26th Apr 2008 12:03 Edited at: 26th Apr 2008 12:10
There is not a convenient way to do what you are asking, and cutting the thing up will not give you good results at all. To do things that way, you'd need to use DirectSound, and you have no easy way to do that, as it requires interface pointers, something you cannot dummy up in DBC, or DBP...you'd need a plug-in because it is a COM interface.

The best way to do what you are attempting is to maintain elapsed time in your program.

Assuming you are duplicating GuitarHero/RockBand type of functionality (Comparing user input to a bit in the music), you record the correct user input, along with the elapsed time and save that to a file (The floating point elapsed time, and button states at each frame.)

When playing it back with the track, you start the track and your timer, and then you can compare that with the file you saved earlier by loading it up before starting the music.

Its hard to explain, and difficult to setup...but it works great. I used a plug-in to handle the array and the file save/load, as it is alot faster that way.

Here is what I use for that:

Manticore Night
22
Years of Service
User Offline
Joined: 23rd Oct 2003
Location: Ouinnipeg
Posted: 26th Apr 2008 15:49 Edited at: 26th Apr 2008 16:13
to jinzai: I had the exact same idea. The only problem with it was that my program would run the timer at a different speed than the program I made to record the proper input to the file. The did you do the same thing as I did, or did you use a better way? If you used a better way could you tell me it?

your code just seems to be setting up a controller. Maybe I missunderstood, but weren't you talking about some kind of timer? I just woke up so this may not make sense either.

ps. I had no Idea you could use that kind of dimming (as whatever) in DB, thanks.

It's amazing how much TV has raised us. (Bart Simpson)

jinzai
19
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 26th Apr 2008 22:11 Edited at: 26th Apr 2008 22:13
I have another UDT that I use with the Windows function QueryPerformanceCounter, which is the equivalent to the DBP function perftimer(). I don't know if DBC has perftimer, but actually, timer() would be fine, as milliseconds is plenty of resolution.

Well, I trashed the latest (and greatest) version of that, but I compared the time to align the scan on playback. If you start the sound (I used mp3s) and the timer at the same time...it works great to just get the scan that is closest in time to the current scantime.

I wrote a small plug-in for 4 reasons:
1. Using dynamic arrays starts to slow down the frame rate considerably when you use array insert at bottom, so I offloaded that to use C++ vectors, which have a far smaller penalty for increasing the size of the array.
2. Loading the file for my test song took about 30 seconds using DBP, and less than 2 for C++.
3. I wanted my editor to have dialogs, which are easy to do in a plug-in.
4. I wanted to use DirectSound to get/set the headset audio, and also to make a drum kit for the RB drums. That worked out great, now I have a real drum kit for the drums that I can play using headphones. (Which did not stop my downstairs neighbors from calling the cops on me. Fortunately, they liked the idea...now I know how Tesla felt, or maybe not.)

Login to post a reply

Server time is: 2026-07-05 14:21:00
Your offset time is: 2026-07-05 14:21:00