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.

Audio > Krawall "crunching"

#40906 - Ultima2876 - Sat Apr 23, 2005 1:57 pm

I've had some problems with Krawall lately. I'm using this code to fade out (to black):

Code:
void fade_to_black(int frames, int sound, u16 blend)
   {
         int i = 0;
         REG_BLDCNT = BLD_DEC | blend;
         REG_BLDY = BLD_EVY(0);
         for (i = 0; i < frames; i++)
         {
            if (sound == 1)
               {
                  kramWorker();
                  i++;
               }
            WaitForVsync();
            switch (frames)
               {
                  case 1: REG_BLDY = BLD_EVY(16 * i / 1); break;
                  case 15: REG_BLDY = BLD_EVY(16 * i / 15); break;
                  case 30: REG_BLDY = BLD_EVY(16 * i / 30); break;
                  case 45: REG_BLDY = BLD_EVY(16 * i / 45); break;
                  case 60: REG_BLDY = BLD_EVY(16 * i / 60); break;
                  case 75: REG_BLDY = BLD_EVY(16 * i / 75); break;
                  case 90: REG_BLDY = BLD_EVY(16 * i / 90); break;
                  case 105: REG_BLDY = BLD_EVY(16 * i / 105); break;
                  case 120: REG_BLDY = BLD_EVY(16 * i / 120); break;
                  case 135: REG_BLDY = BLD_EVY(16 * i / 135); break;
                  case 150: REG_BLDY = BLD_EVY(16 * i / 150); break;
                  case 165: REG_BLDY = BLD_EVY(16 * i / 165); break;
                  case 180: REG_BLDY = BLD_EVY(16 * i / 180); break;
                  case 195: REG_BLDY = BLD_EVY(16 * i / 195); break;
                  case 210: REG_BLDY = BLD_EVY(16 * i / 210); break;
                  case 225: REG_BLDY = BLD_EVY(16 * i / 225); break;
                  case 240: REG_BLDY = BLD_EVY(16 * i / 240); break;
                  case 255: REG_BLDY = BLD_EVY(16 * i / 255); break;
               }
            }
      REG_BLDY = BLD_EVY(16);
   }


However, when I'm fading with sound, the sound sometimes "crunches", making a horrible crunching noise, or fuzz. This seems to be completely random, as sometimes it works perfectly, others it onl crunches a tiny bit, and sometimes it even crunches horribly the whole way through the fade.

Is there any solution to this? (using High Quality mode in Krawall makes no difference) Is there a more efficient way to fade to black while providing a number of frames to fade over (the number of frames will only ever be 1 or a multiple of 15). Do you think it is my fade function using too much CPU time?

Thanks.

#40913 - Lord Graga - Sat Apr 23, 2005 2:29 pm

You should allways have kramWorker() in an interrupt. Try that.

The whole(switch) statement looks horribly ineffective, both codewise and speedwise. Not that it matters when you are only doing so little, but it's sort of bad style :P

#40922 - Ultima2876 - Sat Apr 23, 2005 3:54 pm

Yeah, I know, I don't like the switch either. It actually used to just be a:

REG_BLDY = BLD_EVY(16 * i / frames);

but I thought that would be slower due to dividing by a variable. Should I just change it back?

I'll try that with the interrupt, by the way =P


Last edited by Ultima2876 on Sat Apr 23, 2005 4:05 pm; edited 1 time in total

#40924 - Quirky - Sat Apr 23, 2005 3:58 pm

Check your .map file or use objdump on the elf - I think you'll find that non-shifting divides by a constant pull in the (slowish) newlib divide routine anyway. That has been my experience anyway.

I wouldn't worry about speed in this case though, as you aren't doing much per-frame. Only be concerned if you wanted to keep your rom size down (for multiboot say) by not using unecessary lib calls.

#41933 - kusma - Tue May 03, 2005 12:12 pm

you could easily store the reciprocals in a lut here, and use a table-lookup + a multiply.