Continuing the discussion from [WIP] Kong:
Trying to get this working - but it ain’t!
…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);
}
}