pace calculation works
This commit is contained in:
parent
48b9fc7578
commit
9328e22425
@ -898,7 +898,7 @@ const t_tone tetris3[] = {
|
||||
t_melodies tetrisTheme = {
|
||||
.melodies = { { .amplitude = 3, .tones = tetris1 }, { .amplitude = 3, .tones = tetris2 }, { .amplitude = 3, .tones = tetris3 } },
|
||||
.numOfMelodies = 3,
|
||||
.pace = 4
|
||||
.pace = 160
|
||||
};
|
||||
|
||||
void melodyInit() {
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/param.h>
|
||||
#include "sequencer.h"
|
||||
#include "scheduler.h"
|
||||
@ -9,8 +11,24 @@ void sequencerInit() {
|
||||
}
|
||||
|
||||
void sequencerExec(void *handle) {
|
||||
static uint16_t lengths[e_L_LengthEnd];
|
||||
t_melodies *melodies = (t_melodies*) handle;
|
||||
|
||||
if (melodies->firstRun) {
|
||||
melodies->firstRun = false;
|
||||
|
||||
lengths[e_L_1_4] = 60000 / melodies->pace; // duration of a 1/4 tone in ms
|
||||
lengths[e_L_1_2] = lengths[e_L_1_4] << 1;
|
||||
lengths[e_L_1] = lengths[e_L_1_4] << 2;
|
||||
lengths[e_L_1_8] = lengths[e_L_1_4] >> 1;
|
||||
lengths[e_L_1_16] = lengths[e_L_1_4] >> 2;
|
||||
lengths[e_L_1_32] = lengths[e_L_1_4] >> 4;
|
||||
|
||||
for (uint8_t i = 0; i < e_L_LengthEnd; i++) {
|
||||
lengths[i] /= SEQUENCER_PERIOD;
|
||||
}
|
||||
}
|
||||
|
||||
for (uint8_t channel = 0; channel < melodies->numOfMelodies; channel++) {
|
||||
t_melody *melody = &(melodies->melodies[channel]);
|
||||
|
||||
@ -31,7 +49,7 @@ void sequencerExec(void *handle) {
|
||||
melody->idx = 0;
|
||||
}
|
||||
psgPlayTone(channel, melody->tones[melody->idx].octave, melody->tones[melody->idx].note);
|
||||
melody->lengthCnt = (melody->tones[melody->idx].staccato) ? (melody->tones[melody->idx].length / 2) : melody->tones[melody->idx].length;
|
||||
melody->lengthCnt = (melody->tones[melody->idx].staccato) ? (lengths[melody->tones[melody->idx].length] / 2) : lengths[melody->tones[melody->idx].length];
|
||||
melody->state = e_HoldTone;
|
||||
}
|
||||
break;
|
||||
@ -48,7 +66,7 @@ void sequencerExec(void *handle) {
|
||||
break;
|
||||
case e_StaccatoBreak:
|
||||
psgPlayTone(channel, e_O_Null, e_Pause);
|
||||
melody->lengthCnt = melody->tones[melody->idx].length / 2;
|
||||
melody->lengthCnt = lengths[melody->tones[melody->idx].length] / 2;
|
||||
melody->state = e_HoldStaccatoBreak;
|
||||
break;
|
||||
case e_HoldStaccatoBreak:
|
||||
@ -75,7 +93,8 @@ uint16_t sequencerPlayMelodies(t_melodies *melodies) {
|
||||
melodies->melodies[i].state = e_Init;
|
||||
}
|
||||
melodies->sync = 0;
|
||||
melodies->firstRun = true;
|
||||
|
||||
return schAdd(sequencerExec, (void*) melodies, 0, melodies->pace);
|
||||
return schAdd(sequencerExec, (void*) melodies, 0, SEQUENCER_PERIOD);
|
||||
}
|
||||
|
||||
|
@ -7,14 +7,15 @@
|
||||
#include "psg.h"
|
||||
|
||||
typedef enum {
|
||||
e_L_EndMark = 0,
|
||||
e_L_SyncMark = 1,
|
||||
e_L_1 = 320,
|
||||
e_L_1_2 = 160,
|
||||
e_L_1_4 = 80,
|
||||
e_L_1_8 = 40,
|
||||
e_L_1_16 = 20,
|
||||
e_L_1_32 = 10,
|
||||
e_L_1 = 0,
|
||||
e_L_1_2 = 1,
|
||||
e_L_1_4 = 2,
|
||||
e_L_1_8 = 3,
|
||||
e_L_1_16 = 4,
|
||||
e_L_1_32 = 5,
|
||||
e_L_LengthEnd = 6,
|
||||
e_L_EndMark = 254,
|
||||
e_L_SyncMark = 255,
|
||||
} t_noteLength;
|
||||
|
||||
typedef struct {
|
||||
@ -43,16 +44,17 @@ typedef struct {
|
||||
const t_tone *tones;
|
||||
} t_melody;
|
||||
|
||||
#define SEQUENCER_PERIOD 4 // ms
|
||||
#define NUM_OF_CHANNELS 3
|
||||
typedef struct {
|
||||
uint8_t numOfMelodies;
|
||||
uint8_t pace;
|
||||
bool firstRun;
|
||||
uint8_t pace; // quarter notes per minute
|
||||
uint8_t sync;
|
||||
t_melody melodies[NUM_OF_CHANNELS];
|
||||
} t_melodies;
|
||||
|
||||
void sequencerInit();
|
||||
uint16_t sequencerPlayMelody(t_melody *melody);
|
||||
uint16_t sequencerPlayMelodies(t_melodies *melodies);
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user