21 #include "mozzi_fixmath.h"
23 #include "mozzi_pgmspace.h"
25 #ifdef OSCIL_DITHER_PHASE
26 #include "mozzi_rand.h"
30 #define OSCIL_F_BITS 16
31 #define OSCIL_F_BITS_AS_MULTIPLIER 65536
34 #define OSCIL_PHMOD_BITS 16
60 template <u
int16_t NUM_TABLE_CELLS, u
int16_t UPDATE_RATE>
71 Oscil(
const int8_t * TABLE_NAME):table(TABLE_NAME)
113 phase_fractional = (uint32_t)phase << OSCIL_F_BITS;
124 phase_fractional = phase;
133 return phase_fractional;
152 return FLASH_OR_RAM_READ<const int8_t>(table + (((phase_fractional+(phmod_proportion * NUM_TABLE_CELLS))>>OSCIL_F_BITS) & (NUM_TABLE_CELLS - 1)));
162 template <
int8_t NI,
int8_t NF, u
int8_t RANGE>
164 int8_t
phMod(SFix<NI,NF,RANGE> phmod_proportion)
166 return phMod(SFix<15,16>(phmod_proportion).asRaw());
178 int8_t
phMod(SFix<15,16> phmod_proportion)
180 return phMod(phmod_proportion.asRaw());
197 phase_increment_fractional = ((uint32_t)frequency) * ((OSCIL_F_BITS_AS_MULTIPLIER*NUM_TABLE_CELLS)/UPDATE_RATE);
209 phase_increment_fractional = (uint32_t)((((
float)NUM_TABLE_CELLS * frequency)/UPDATE_RATE) * OSCIL_F_BITS_AS_MULTIPLIER);
219 template <
int8_t NI,
int8_t NF, u
int64_t RANGE>
245 if ((256UL*NUM_TABLE_CELLS) >= UPDATE_RATE) {
246 phase_increment_fractional = ((uint32_t)frequency) * ((256UL*NUM_TABLE_CELLS)/UPDATE_RATE);
248 phase_increment_fractional = ((uint32_t)frequency) / (UPDATE_RATE/(256UL*NUM_TABLE_CELLS));
259 template <u
int64_t RANGE>
283 if (NUM_TABLE_CELLS >= UPDATE_RATE) {
284 phase_increment_fractional = ((uint32_t)frequency) * (NUM_TABLE_CELLS/UPDATE_RATE);
286 phase_increment_fractional = ((uint32_t)frequency) / (UPDATE_RATE/NUM_TABLE_CELLS);
299 template <u
int64_t RANGE>
314 template <
int8_t NI,
int8_t NF, u
int64_t RANGE>
335 return FLASH_OR_RAM_READ<const int8_t>(table + (index & (NUM_TABLE_CELLS - 1)));
355 return ((uint32_t)frequency) * ((OSCIL_F_BITS_AS_MULTIPLIER*NUM_TABLE_CELLS)/UPDATE_RATE);
365 phase_increment_fractional = phaseinc_fractional;
375 static const uint8_t ADJUST_FOR_NUM_TABLE_CELLS = (NUM_TABLE_CELLS<2048) ? 8 : 0;
381 void incrementPhase()
384 phase_fractional += phase_increment_fractional;
393 #ifdef OSCIL_DITHER_PHASE
394 return FLASH_OR_RAM_READ<const int8_t>(table + (((phase_fractional + ((
int)(
xorshift96()>>16))) >> OSCIL_F_BITS) & (NUM_TABLE_CELLS - 1)));
396 return FLASH_OR_RAM_READ<const int8_t>(table + ((phase_fractional >> OSCIL_F_BITS) & (NUM_TABLE_CELLS - 1)));
402 uint32_t phase_fractional;
403 uint32_t phase_increment_fractional;
404 const int8_t * table;
Oscil plays a wavetable, cycling through the table to generate an audio or control signal.
void setFreq(UFix< NI, NF, RANGE > frequency)
Set the frequency using UFix<NI,NF> fixed-point number format.
void setTable(const int8_t *TABLE_NAME)
Change the sound table which will be played by the Oscil.
void setFreq(int frequency)
Set the oscillator frequency with an unsigned int.
void setFreq(SFix< NI, NF, RANGE > frequency)
Set the frequency using SFix<NI,NF> fixed-point number format.
uint32_t phaseIncFromFreq(int frequency)
phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies.
int8_t phMod(Q15n16 phmod_proportion)
Returns the next sample given a phase modulation value.
void setFreq(UFix< 16, 16, RANGE > frequency)
Set the frequency using UFix<16,16> fixed-point number format.
int8_t next()
Updates the phase according to the current frequency and returns the sample at the new phase position...
void setFreq_Q16n16(Q16n16 frequency)
Set the frequency using Q16n16 fixed-point number format.
void setFreq(UFix< 24, 8, RANGE > frequency)
Set the frequency using UFix<24,8> fixed-point number format.
int8_t phMod(SFix< NI, NF, RANGE > phmod_proportion)
Returns the next sample given a phase modulation value.
int8_t atIndex(unsigned int index)
Returns the sample at the given table index.
void setPhaseFractional(uint32_t phase)
Set the phase of the Oscil.
void setFreq(float frequency)
Set the oscillator frequency with a float.
void setPhase(unsigned int phase)
Set the phase of the Oscil.
void setFreq_Q24n8(Q24n8 frequency)
Set the frequency using Q24n8 fixed-point number format.
void setPhaseInc(uint32_t phaseinc_fractional)
Set a specific phase increment.
int8_t phMod(SFix< 15, 16 > phmod_proportion)
Returns the next sample given a phase modulation value.
uint32_t getPhaseFractional()
Get the phase of the Oscil in fractional format.
Oscil(const int8_t *TABLE_NAME)
Constructor.
int32_t Q15n16
signed fractional number using 15 integer bits and 16 fractional bits, represents -32767....
uint32_t Q24n8
unsigned fractional number using 24 integer bits and 8 fractional bits, represents 0 to 16777215
uint32_t Q16n16
unsigned fractional number using 16 integer bits and 16 fractional bits, represents 0 to 65535....
uint32_t xorshift96()
Random number generator.