gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Wait / Pause function?

#116936 - zohar - Wed Jan 31, 2007 4:34 am

Is there a predefined function in libnds that will "pause" processes for a set amount of time, and then continue? (If not, I'm sure it would be just a small snippet of code... but how would you do it?)

For example, I've created a virtual piano, and I can keep track of what notes are played in an array. I can playback all the notes, but the program plays them VERY quickly in succession. (sounds like a blur) I want to slow this down.

Any help appreciated. Thanks.

#116941 - GPFerror - Wed Jan 31, 2007 4:52 am

Code:
#define timers2ms(tlow,thigh)(tlow | (thigh<<16)) >> 5


void StartTicks(void)
{
   TIMER0_DATA=0;
   TIMER1_DATA=0;
   TIMER0_CR=TIMER_ENABLE|TIMER_DIV_1024;
   TIMER1_CR=TIMER_ENABLE|TIMER_CASCADE;
}

Uint32 GetTicks(void)
{
   return timers2ms(TIMER0_DATA, TIMER1_DATA);
}

void Delay(Uint32 ms)
{
   Uint32 now;
   now=timers2ms(TIMER0_DATA, TIMER1_DATA);
   while((Uint32)timers2ms(TIMER0_DATA, TIMER1_DATA)<now+ms);

}


this is how I do it, just initialize it by calling StartTicks() at beginning of main().

Troy(GPF)
http://gpf.dcemu.co.uk

#117061 - zohar - Thu Feb 01, 2007 12:10 am

I tried out your code and it works for the most part.

However, for some reason, sometimes the delays aren't perfect. There's an occassional shortened delay. Do you have any ideas why this is happening?

Here's a short snippet of how I'm using it:

Code:
         for(i = 0; i < rindex; i++)
         {
            Delay(500);
            switch ( record[i] )
            {
               case 0: playSound(&piano_c0); break;                  
               case 1: playSound(&piano_d0); break;
               case 2: playSound(&piano_e0); break;
               case 3: playSound(&piano_f0); break;
               case 4: playSound(&piano_g0); break;
               case 5: playSound(&piano_a0); break;
               case 6: playSound(&piano_b0); break;
               case 7: playSound(&piano_c1); break;
               default: break;
            }
         }

#117066 - tepples - Thu Feb 01, 2007 12:56 am

Try just playing the notes in order and calling swiWaitForVBlank() 12 times after each note.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#117073 - zohar - Thu Feb 01, 2007 1:22 am

tepples wrote:
Try just playing the notes in order and calling swiWaitForVBlank() 12 times after each note.


Any reason why it's 12 times? Why not more, or less?

#117078 - tepples - Thu Feb 01, 2007 1:44 am

One note per 12 vblanks is about 300 notes per minute, or 150 BPM if the notes are eighth notes. Fewer will result in a faster tempo; more will result in a slower tempo. (On the DS, the number of notes per minute is 3594 divided by the number of vblanks per note: 33554432 cycles per second times 60 seconds per minute divided by 560190 cycles per vblank.)
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#117110 - Vich - Thu Feb 01, 2007 10:52 am

My COW library has what you're looking for.

CowUtils.cpp
http://cow.svn.sourceforge.net/viewvc/cow/trunk/Source/COW/CowUtils.cpp?revision=2&view=markup
CowUtils.h
http://cow.svn.sourceforge.net/viewvc/cow/trunk/Source/COW/CowUtils.h?revision=2&view=markup

Its license is LGPL and compiles for Windows, Linux and the DS.
_________________
[project website] [personal website]