separate synth code from main

This commit is contained in:
twobit 2012-08-06 02:52:03 +02:00
parent e84aab7164
commit e007b86e92
3 changed files with 50 additions and 48 deletions

View File

@ -5,52 +5,8 @@
#include <stdlib.h> #include <stdlib.h>
#include "main.h" #include "main.h"
#include "synth.h"
volatile uint8_t sample_pending;
// sample rate is 8M / (5 * 64) = 25000
enum {
synth_channel_count = 2
};
typedef struct {
uint16_t phase;
uint16_t speed;
} synth_channel_t;
typedef struct {
synth_channel_t channels[synth_channel_count];
} synth_t;
static synth_t synth;
static void synth_init(void)
{
// some test values
synth.channels[0].phase = 0;
synth.channels[0].speed = 1153;
synth.channels[1].phase = 0;
synth.channels[1].speed = 1728;
}
static inline uint16_t synth_mix(void)
{
uint16_t output = 0;
for (int i = 0; i < synth_channel_count; i++) {
synth_channel_t *chan = &synth.channels[i];
chan->phase += chan->speed;
output += ((chan->phase >> 8) & 0xff) / 2;
}
return output;
}
static void init_sampletimer(void) static void init_sampletimer(void)
{ {
@ -104,7 +60,6 @@ static void init_motor(void)
int main(void) int main(void)
{ {
@ -155,5 +110,3 @@ ISR(TIMER0_COMPA_vect)
} }

45
firmware/synth.c Normal file
View File

@ -0,0 +1,45 @@
#include <inttypes.h>
// sample rate is 8M / (3 * 64)
enum {
synth_channel_count = 2
};
typedef struct {
uint16_t phase;
uint16_t speed;
} synth_channel_t;
typedef struct {
synth_channel_t channels[synth_channel_count];
} synth_t;
static synth_t synth;
void synth_init(void)
{
// some test values
synth.channels[0].phase = 0;
synth.channels[0].speed = 1153;
synth.channels[1].phase = 0;
synth.channels[1].speed = 1728;
}
uint16_t synth_mix(void)
{
uint16_t output = 0;
for (int i = 0; i < synth_channel_count; i++) {
synth_channel_t *chan = &synth.channels[i];
chan->phase += chan->speed;
output += ((chan->phase >> 8) & 0xff) / 2;
}
return output;
}

4
firmware/synth.h Normal file
View File

@ -0,0 +1,4 @@
void synth_init(void);
uint16_t synth_mix(void);