Continuing the discussion... Kong Sound

Continuing the discussion from [WIP] Kong:

Trying to get this working - but it ain’t! :frowning_face:

…if I manually edit in the durations, ie.

0x2A, 0x80, 0x1C, 0x84, 0x0E, 0x88, 0x0E, 0x8A, 0x0E, 0x88, 0x00

everything works fine, but trying to get it working without them:

0x2A, 0x80, 0x1C, 0x84, 0x0E, 0x88, 0x8A, 0x88, 0x00

What I want is for the duration 0x0E to apply to all of the last 3 notes… what am I doing wrong?

#include <Arduboy2.h>

Arduboy2 arduboy;

BeepPin1 beep1;
BeepPin2 beep2;

bool     tonesPlaying = false;
uint8_t   *tonesArray = 0;
uint8_t       counter = 0;
uint8_t      duration = 0;
uint8_t durationIndex = 0;

const uint8_t barrels_BGM[] =
{
  0x2A, 0x80, 0x1C, 0x84, 0x0E, 0x88, 0x8A, 0x88, 0x00
};

const float freqKong[] =
{
  220, 233.08, 246.94, 261.63, 277.18, 293.67, 311.13, 329.63, 349.23, 369.99, 392, 415.3,
  110, 116.54, 123.47, 130.81, 138.59, 146.83, 155.56, 164.81, 174.61, 185, 196, 207.65
};

void toneKong(uint8_t *tones)
{
  uint8_t temp, freqIndex;

  counter = 0;
  tonesArray = tones; // set to start of sequence array

  durationIndex = counter;
  duration = *(tonesArray + counter++);

  temp = *(tonesArray + counter++);
  freqIndex = (temp & 0x0F) + (((temp & B00110000) >> 4) * 12);
  beep1.tone(beep1.freq(freqKong[freqIndex]));

  if (*(tonesArray + counter) & B11000000)
  {
    temp = *(tonesArray + counter++);
    freqIndex = (temp & 0x0F) + (((temp & B00110000) >> 4) * 12);
    beep2.tone(beep2.freq(freqKong[freqIndex]));
  }

  tonesPlaying = true;
}

void timerKong()
{
  uint8_t temp, freqIndex;

  if (duration == 0)
  {
    if (tonesPlaying)
    {
      temp = *(tonesArray + counter);

      if (temp == 0)
      {
        tonesPlaying = false; // "end of sequence" marker
      }
      else
      {
        if (!(temp & B10000000))
        {
          duration = temp;
          durationIndex = counter;
          counter++;
          temp = *(tonesArray + counter++);
        }
        else
        {
          duration = *(tonesArray + durationIndex);
          counter++;
        }

        freqIndex = (temp & 0x0F) + (((temp & B00110000) >> 4) * 12);

        if ((temp & 0x0F) == 12) // NOTE_REST
        {
          beep1.noTone();
        }
        else
        {
          beep1.tone(beep1.freq(freqKong[freqIndex]));
        }

        if (*(tonesArray + counter) & B11000000)
        {
          temp = *(tonesArray + counter++);
          freqIndex = (temp & 0x0F) + (((temp & B00110000) >> 4) * 12);

          if ((temp & 0x0F) == 12) // NOTE_REST
          {
            beep2.noTone();
          }
          else
          {
            beep2.tone(beep2.freq(freqKong[freqIndex]));
          }
        }
      }
    }
  }
  else if (--duration == 0)
  {
    beep1.noTone();
    beep2.noTone();
  }
}

void setup()
{
  arduboy.begin();
  arduboy.setFrameRate(60);

  beep1.begin();
  beep2.begin();

  toneKong(barrels_BGM);
}

void loop()
{
  if (!arduboy.nextFrame())
  {
    return;
  }

  timerKong(); // handle tone duration

  if (!tonesPlaying)
  {
    toneKong(barrels_BGM);
  }
}

@uXe, I am sorry to tell you that after fixing some bugs I simply do not have enough room for these sound effects. :frowning:

You might be better than me at saving some memory and I would love to see you squeeze this into my code base if you can. Code can be found here > https://github.com/Press-Play-On-Tape/DonkeyKong

All those auto &arduboy = this->context.arduboy; have bound to be wasting space on your flash. Typically you’d just declare arduboy a global and extern it everywhere.

In a codebase this large probably tons of places you could save space.

No doubt. I have used all of the tricks in my bag but I am sure there are plenty more!

1 Like

Just for interests sake, I changes the definition of Arduboy2Ext to extern and modified all of the other .h files to suit. The compilation went from 28,494 to 28,864 (370 bytes more!) .

Like you, I was expecting a reduction!

That seems strange? Do you have the source code from both so I can compile them side by side and disassemble and see what’s going on?