-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsynth.cpp
More file actions
65 lines (49 loc) · 1.39 KB
/
Copy pathsynth.cpp
File metadata and controls
65 lines (49 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "sinetable.h"
#include <avr/io.h>
#include <avr/interrupt.h>
// Variable to hold the next note that will be generated
static volatile uint8_t next_note;
// Variable to hold the next sample that will be output
static volatile uint8_t next_sample;
// Variable to hold the amplitude of the next sample
static volatile uint8_t next_amplitude;
void synth_amplitude(uint8_t amplitude) {
next_amplitude = amplitude;
}
void synth_enable(void) {
TCCR1 = (1 << CS11) | (1 << CS00);
TIMSK = (1 << OCIE1A);
}
void synth_disable() {
TCCR1 &= ~((1 << CS11) | (1 << CS00));
TIMSK &= ~(1 << OCIE1A);
}
void synth_init(void) {
OCR1A = 0x0f; // set PWM carrier frequency
next_amplitude = 0xff; // max volume, plz
}
void synth_start_note(uint8_t note) {
synth_enable();
next_note = note;
}
static volatile uint16_t carrier_inc;
static volatile uint16_t carrier_pos = 0;
static volatile uint8_t amplitude = 0xff;
// hack to prevent increment
void synth_stop_note(void) {
synth_disable();
next_note = 0;
carrier_pos = 0;
}
void synth_generate(uint8_t note) {
uint16_t cpos = 0;
carrier_inc = note;
carrier_pos += carrier_inc;
cpos = carrier_pos & SINETABLE_MASK;
next_sample = (pgm_read_byte(&sinetable[cpos]) * next_amplitude) >> 8;
}
ISR(TIM1_COMPA_vect) {
//if(!synth_ready) return
synth_generate(next_note);
OCR0A = next_sample;
}