Add strophe 1 of the song

This commit is contained in:
Lukas Fürderer 2020-11-29 23:49:58 +01:00
parent 5581c3e1d9
commit 0a62b730f3
2 changed files with 125 additions and 19 deletions

41
audio.c
View File

@ -31,36 +31,27 @@ static struct AudioState sAudioState;
bool bPerformAudioStep()
{
bool bSuccess = true;
struct AudioState *psState = &sAudioState;
psState = psState;
sAudioState.highState = !sAudioState.highState;
if (sAudioState.highState)
{
DAC_SetValue(512);
if (sAudioState.u16RemainingWaves == 0)
{
LPC_TIM0->MR0 = sAudioState.u32EndBreak;
}
}
else
{
DAC_SetValue(0);
if (sAudioState.u16RemainingWaves > 0)
{
sAudioState.u16RemainingWaves--;
DAC_SetValue(512);
}
else
{
if (FREQ(*sAudioState.cpu32NextAction) == 0)
{
sAudioState.highState = true;
// GOTO
sAudioState.highState = false;
sAudioState.cpu32NextAction = cppu32JumpLabels[*sAudioState.cpu32NextAction];
bSuccess = false;
}
else if (FREQ(*sAudioState.cpu32NextAction) == 1)
{
sAudioState.highState = true;
// CALL
sAudioState.highState = false;
sAudioState.cpu32Stack[sAudioState.u32StackPointer] = sAudioState.cpu32NextAction + 1;
sAudioState.cpu32NextAction = cppu32JumpLabels[WAVES(*sAudioState.cpu32NextAction)];
sAudioState.u32StackPointer++;
@ -68,13 +59,22 @@ bool bPerformAudioStep()
}
else if (FREQ(*sAudioState.cpu32NextAction) == 2)
{
sAudioState.highState = true;
// RETURN
sAudioState.highState = false;
sAudioState.u32StackPointer--;
sAudioState.cpu32NextAction = sAudioState.cpu32Stack[sAudioState.u32StackPointer];
bSuccess = false;
}
else if (FREQ(*sAudioState.cpu32NextAction) == 3)
{
// PAUSE
sAudioState.highState = false;
LPC_TIM0->MR0 = WAVES(*sAudioState.cpu32NextAction) << 12;
sAudioState.cpu32NextAction++;
}
else
{
DAC_SetValue(512);
uint32_t u32Frequency = FREQ(*sAudioState.cpu32NextAction);
LPC_TIM0->MR0 = u32Frequency * 2;
uint32_t u32Waves = WAVES(*sAudioState.cpu32NextAction);
@ -84,6 +84,15 @@ bool bPerformAudioStep()
}
}
}
else
{
DAC_SetValue(0);
sAudioState.u16RemainingWaves--;
if (sAudioState.u16RemainingWaves == 0)
{
LPC_TIM0->MR0 = sAudioState.u32EndBreak;
}
}
return bSuccess;
}
@ -100,7 +109,7 @@ void TIMER0_IRQHandler()
// Initializes the interrupt-driven audio output
void vStartAudio()
{
sAudioState.highState = true;
sAudioState.highState = false;
sAudioState.u16RemainingWaves = 0;
sAudioState.cpu32NextAction = cpu32EntryPoint;
sAudioState.u32StackPointer = 0;

103
melody.c
View File

@ -14,6 +14,9 @@ This file contains the main app function and init.
// duration: total duration as multiple of 40ns
#define TONE(freq, duration) ((((uint32_t)freq & 0xffff) << 16) | (((uint32_t)duration / (uint32_t)freq) & 0xffff))
// duration: pause duration as multiple of 40ns
#define PAUSE(duration) (0x30000 | ((duration >> 10) & 0xffff))
const uint32_t TACT = 2120 * 1000 * 25; // 2.12 seconds for one tact
const uint32_t GIS2 = 30098;
@ -29,14 +32,20 @@ const uint32_t RETURN = 0x20000;
const uint32_t refrain[];
const uint32_t quadrupel[];
const uint32_t *cpu32EntryPoint = refrain;
const uint32_t strophe1[];
const uint32_t main_melody[];
const uint32_t *cpu32EntryPoint = main_melody;
const uint32_t GOTO_REFRAIN = 0;
const uint32_t CALL_REFRAIN = CALL + 0;
const uint32_t CALL_QUADRUPEL = CALL + 1;
const uint32_t CALL_STROPHE1 = CALL + 2;
const uint32_t GOTO_MAIN_MELODY = 3;
const uint32_t *jumplabels[] = {
refrain,
quadrupel,
strophe1,
main_melody,
};
const uint32_t **cppu32JumpLabels = jumplabels;
@ -96,5 +105,93 @@ const uint32_t refrain[] = {
TONE(DIS, TACT / 4),
TONE(CIS, TACT / 2),
GOTO_REFRAIN,
RETURN,
};
const uint32_t strophe1[] = {
PAUSE(TACT / 4),
TONE(B, TACT / 8),
TONE(C, TACT / 8),
TONE(CIS, TACT / 8),
TONE(CIS, TACT / 8),
TONE(DIS, TACT / 8),
TONE(C, TACT * 3 / 16), // 1/8 tact before, 1/16 tact after
TONE(B, TACT / 16),
TONE(GIS, TACT * 7 / 8),
PAUSE(TACT / 8),
TONE(B, TACT / 8),
TONE(B, TACT / 8),
TONE(C, TACT / 8),
TONE(CIS, TACT / 8),
TONE(B, TACT / 8),
PAUSE(TACT / 8),
TONE(GIS, TACT / 8),
TONE(GIS2,TACT / 4),
TONE(GIS2,TACT / 8),
TONE(DIS, TACT * 5 / 8),
PAUSE(TACT / 8),
TONE(B, TACT / 8),
TONE(B, TACT / 8),
TONE(C, TACT / 8),
TONE(CIS, TACT / 8),
TONE(B, TACT / 8),
TONE(CIS, TACT / 8),
TONE(DIS, TACT / 8),
PAUSE(TACT / 8),
TONE(C, TACT / 8),
TONE(B, TACT / 8),
TONE(C, TACT / 16),
TONE(B, TACT / 16),
TONE(GIS, TACT / 2),
PAUSE(TACT / 8),
TONE(B, TACT / 8),
TONE(B, TACT / 8),
TONE(C, TACT / 8),
TONE(CIS, TACT / 8),
TONE(B, TACT / 8),
TONE(GIS, TACT / 4),
TONE(DIS, TACT / 8),
TONE(DIS, TACT / 8),
TONE(DIS, TACT / 8),
TONE(F, TACT / 8),
TONE(DIS, TACT / 2),
TONE(CIS, TACT * 5 / 8),
TONE(DIS, TACT / 8),
TONE(F, TACT / 8),
TONE(CIS, TACT / 8),
TONE(DIS, TACT / 8),
TONE(DIS, TACT / 8),
TONE(DIS, TACT / 8),
TONE(F, TACT / 8),
TONE(DIS, TACT / 4),
TONE(GIS, TACT / 4),
PAUSE(TACT / 4),
TONE(B, TACT / 8),
TONE(C, TACT / 8),
TONE(CIS, TACT / 8),
TONE(B, TACT / 8),
PAUSE(TACT / 8),
TONE(DIS, TACT / 8),
TONE(F, TACT / 8),
TONE(DIS, TACT * 3 / 8),
RETURN,
};
const uint32_t main_melody[] = {
CALL_STROPHE1,
CALL_REFRAIN,
GOTO_MAIN_MELODY,
};