different waveforms

This commit is contained in:
twobit 2012-08-06 03:35:28 +02:00
parent fa40862b31
commit fa4e023f21
2 changed files with 42 additions and 6 deletions

View File

@ -35,7 +35,7 @@ OPT = s
##########################################################################################################
# List C source files here. (C dependencies are automatically generated.)
SRC = main.c
SRC = main.c synth.c
##########################################################################################################

View File

@ -4,13 +4,26 @@
enum {
synth_channel_count = 2
synth_channel_count = 2,
};
enum {
WAVE_PULSE,
WAVE_SAW,
};
typedef struct {
uint8_t wave;
uint16_t phase;
uint16_t speed;
uint32_t pulse_width;
uint8_t pulse_sweep;
uint8_t level; // envelop level
} synth_channel_t;
typedef struct {
@ -20,24 +33,47 @@ typedef struct {
static synth_t synth;
void synth_init(void)
{
// some test values
synth.channels[0].phase = 0;
synth.channels[0].wave = WAVE_PULSE;
synth.channels[0].speed = 1153;
synth.channels[0].pulse_sweep = 0;
synth.channels[0].pulse_width = 1 << 31;
synth.channels[1].phase = 0;
synth.channels[1].wave = WAVE_SAW;
synth.channels[1].speed = 1728;
}
uint16_t synth_mix(void)
{
uint16_t output = 0;
for (int i = 0; i < synth_channel_count; i++) {
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;
chan->pulse_width += chan->pulse_sweep << 8;
uint8_t amp;
switch(chan->wave) {
case WAVE_PULSE:
amp = -(chan->phase < (chan->pulse_width >> 16));
break;
case WAVE_SAW:
amp = (chan->phase >> 8);
break;
default:
amp = 0;
break;
}
output += (amp & 0xff) / 2;
}
return output;