tone-generator/tone.hpp

40 lines
595 B
C++

#pragma once
#include <inttypes.h>
#include <avr/pgmspace.h>
#include "util.hpp"
extern const PROGMEM int8_t TONE_LOOKUP[4][256];
struct Tone {
enum Type {
tt_sine = 0,
tt_saw = 1,
tt_square = 2,
tt_triangle = 3,
};
uint32_t phase;
uint16_t frequency;
uint8_t amplitude;
Type mode;
inline bool active() const AW_IN {
return amplitude > 0;
}
inline void update(uint32_t t) AW_IN {
phase += t * frequency;
}
inline int get() const AW_IN {
int8_t v = pgm_read_byte_near(&TONE_LOOKUP[mode][(uint8_t)(phase >> 12)]);
return (int)v * amplitude;
}
};