2024-03-29 14:11:36 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2024-03-29 12:41:44 +01:00
|
|
|
#include <sys/param.h>
|
2024-03-26 15:58:45 +01:00
|
|
|
#include "sequencer.h"
|
|
|
|
#include "scheduler.h"
|
|
|
|
#include "psg.h"
|
|
|
|
|
|
|
|
|
2024-04-17 15:30:45 +02:00
|
|
|
uint8_t slots;
|
2024-03-26 22:12:46 +01:00
|
|
|
|
2024-03-26 15:58:45 +01:00
|
|
|
void sequencerInit() {
|
2024-04-17 15:30:45 +02:00
|
|
|
slots = 0;
|
2024-03-26 15:58:45 +01:00
|
|
|
}
|
|
|
|
|
2024-04-16 10:16:56 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wswitch"
|
|
|
|
#pragma GCC diagnostic ignored "-Wreturn-type"
|
2024-04-10 14:00:12 +02:00
|
|
|
static uint16_t calcLength(t_melodies *m, t_noteLength l) {
|
|
|
|
switch (l) {
|
|
|
|
case e_L_1:
|
|
|
|
return m->quarterLength << 2;
|
|
|
|
case e_L_1_2:
|
|
|
|
return m->quarterLength << 1;
|
|
|
|
case e_L_1_4:
|
|
|
|
return m->quarterLength;
|
|
|
|
case e_L_1_8:
|
|
|
|
return m->quarterLength >> 1;
|
|
|
|
case e_L_1_16:
|
|
|
|
return m->quarterLength >> 2;
|
|
|
|
case e_L_1_32:
|
|
|
|
return m->quarterLength >> 4;
|
|
|
|
}
|
|
|
|
}
|
2024-04-16 10:16:56 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2024-03-29 14:11:36 +01:00
|
|
|
|
|
|
|
|
2024-04-10 14:00:12 +02:00
|
|
|
void sequencerExec(void *handle) {
|
|
|
|
t_melodies *melodies = (t_melodies*) handle;
|
2024-03-29 14:11:36 +01:00
|
|
|
|
2024-03-29 13:29:53 +01:00
|
|
|
for (uint8_t channel = 0; channel < melodies->numOfMelodies; channel++) {
|
2024-03-29 12:41:44 +01:00
|
|
|
t_melody *melody = &(melodies->melodies[channel]);
|
|
|
|
|
|
|
|
switch (melody->state) {
|
|
|
|
case e_Init:
|
|
|
|
melody->state = e_PlayTone;
|
|
|
|
break;
|
|
|
|
case e_PlayTone:
|
2024-03-29 13:04:05 +01:00
|
|
|
if (melody->tones[melody->idx].length == e_L_SyncMark) {
|
|
|
|
if (melodies->sync == 0) {
|
|
|
|
melodies->sync = melodies->numOfMelodies;
|
|
|
|
}
|
|
|
|
melodies->sync -= 1;
|
|
|
|
melody->state = e_Sync;
|
2024-04-10 12:58:30 +02:00
|
|
|
} else if (melody->tones[melody->idx].length == e_L_HoldMark) {
|
|
|
|
melody->state = e_Hold;
|
2024-04-09 18:37:53 +02:00
|
|
|
} else if (melody->tones[melody->idx].length == e_L_StopMark) {
|
|
|
|
melody->state = e_Terminate;
|
2024-03-29 13:04:05 +01:00
|
|
|
} else {
|
|
|
|
if (melody->tones[melody->idx].length == e_L_EndMark) {
|
|
|
|
melody->idx = 0;
|
|
|
|
}
|
2024-05-22 11:55:47 +02:00
|
|
|
psgPlayTone(melodies->chip, channel, *(melodies->p_amplitude), melody->tones[melody->idx].octave, melody->tones[melody->idx].note);
|
2024-04-10 14:00:12 +02:00
|
|
|
melody->lengthCnt = (melody->tones[melody->idx].staccato) ?
|
|
|
|
(calcLength(melodies, melody->tones[melody->idx].length) / 2) :
|
|
|
|
calcLength(melodies, melody->tones[melody->idx].length);
|
2024-03-29 13:04:05 +01:00
|
|
|
melody->state = e_HoldTone;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case e_Sync:
|
|
|
|
if (melodies->sync == 0) {
|
|
|
|
melody->state = e_SeparateTone;
|
2024-03-29 12:41:44 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case e_HoldTone:
|
|
|
|
melody->lengthCnt -= 1;
|
|
|
|
if (melody->lengthCnt == 0) {
|
|
|
|
melody->state = (melody->tones[melody->idx].staccato) ? e_StaccatoBreak : e_SeparateTone;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case e_StaccatoBreak:
|
2024-04-25 12:12:52 +02:00
|
|
|
psgPlayTone(melodies->chip, channel, 0, e_O_Null, e_Pause);
|
2024-04-10 14:00:12 +02:00
|
|
|
melody->lengthCnt = calcLength(melodies, melody->tones[melody->idx].length) / 2;
|
2024-03-29 12:41:44 +01:00
|
|
|
melody->state = e_HoldStaccatoBreak;
|
|
|
|
break;
|
|
|
|
case e_HoldStaccatoBreak:
|
|
|
|
melody->lengthCnt -= 1;
|
|
|
|
if (melody->lengthCnt == 0) {
|
|
|
|
melody->state = e_SeparateTone;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case e_SeparateTone:
|
|
|
|
if (! (melody->tones[melody->idx].legato)) {
|
2024-04-25 12:12:52 +02:00
|
|
|
psgPlayTone(melodies->chip, channel, 0, e_O_Null, e_Pause);
|
2024-03-29 12:41:44 +01:00
|
|
|
}
|
|
|
|
melody->idx += 1;
|
|
|
|
melody->state = e_PlayTone;
|
|
|
|
break;
|
2024-04-10 12:58:30 +02:00
|
|
|
case e_Hold:
|
2024-04-26 12:55:21 +02:00
|
|
|
psgPlayTone(melodies->chip, channel, 0, e_O_Null, e_Pause);
|
2024-04-10 12:58:30 +02:00
|
|
|
break;
|
2024-04-09 18:37:53 +02:00
|
|
|
case e_Terminate:
|
|
|
|
schDel(melodies->taskId);
|
2024-04-26 12:55:21 +02:00
|
|
|
psgPlayTone(melodies->chip, channel, 0, e_O_Null, e_Pause);
|
2024-04-17 15:30:45 +02:00
|
|
|
slots &= ~(melodies->slotMask);
|
2024-04-09 18:37:53 +02:00
|
|
|
break;
|
2024-03-29 12:41:44 +01:00
|
|
|
}
|
2024-03-26 15:58:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 15:30:45 +02:00
|
|
|
void sequencerPlayMelodies(t_melodies *melodies) {
|
2024-04-25 12:12:52 +02:00
|
|
|
melodies->slotMask = (1 << melodies->chip);
|
|
|
|
|
2024-04-17 15:30:45 +02:00
|
|
|
if ((slots & melodies->slotMask) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
slots |= melodies->slotMask;
|
2024-03-29 12:41:44 +01:00
|
|
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
|
|
|
melodies->melodies[i].idx = 0;
|
|
|
|
melodies->melodies[i].lengthCnt = 0;
|
|
|
|
melodies->melodies[i].state = e_Init;
|
|
|
|
}
|
2024-03-29 13:04:05 +01:00
|
|
|
melodies->sync = 0;
|
2024-04-10 14:00:12 +02:00
|
|
|
melodies->quarterLength = 60000 / melodies->pace / SEQUENCER_PERIOD; // duration of a 1/4 tone in ms
|
2024-03-26 23:13:59 +01:00
|
|
|
|
2024-04-09 18:37:53 +02:00
|
|
|
melodies->taskId = schAdd(sequencerExec, (void*) melodies, 0, SEQUENCER_PERIOD);
|
2024-03-26 15:58:45 +01:00
|
|
|
}
|
|
|
|
|
2024-04-15 17:10:33 +02:00
|
|
|
void sequencerStopMelodies(t_melodies *melodies) {
|
|
|
|
schDel(melodies->taskId);
|
2024-04-25 12:12:52 +02:00
|
|
|
slots &= ~(melodies->slotMask);
|
|
|
|
for (uint8_t channel = 0; channel < melodies->numOfMelodies; channel++) {
|
|
|
|
psgPlayTone(melodies->chip, channel, 0, e_O_Null, e_Pause);
|
|
|
|
}
|
2024-04-15 17:10:33 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 12:45:39 +02:00
|
|
|
void sequencerChangePace(t_melodies *melodies) {
|
|
|
|
melodies->quarterLength = 60000 / melodies->pace / SEQUENCER_PERIOD; // duration of a 1/4 tone in ms
|
|
|
|
}
|
|
|
|
|