Skip to content

Commit

Permalink
Added FixMath to Oscil.h
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcombriat committed Dec 19, 2023
1 parent 8127b4f commit 73273a5
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Oscil.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#endif
#include "MozziGuts.h"
#include "mozzi_fixmath.h"
#include "FixMath.h"
#include "mozzi_pgmspace.h"

#ifdef OSCIL_DITHER_PHASE
Expand Down Expand Up @@ -154,6 +155,19 @@ class Oscil
}


/** Returns the next sample given a phase modulation value.
@param phmod_proportion a phase modulation value given as a proportion of the wave. The
phmod_proportion parameter is a SFixMath<15,16> fixed-point number where the fractional part represents almost -1 to almost 1, modulating the phase by one whole table length in
each direction.
@return a sample from the table.
*/
inline
int8_t phMod(SFixMath<15,16> phmod_proportion)
{
return phMod(phmod_proportion.asRaw());
}


/** Set the oscillator frequency with an unsigned int. This is faster than using a
float, so it's useful when processor time is tight, but it can be tricky with
low and high frequencies, depending on the size of the wavetable being used. If
Expand Down Expand Up @@ -207,6 +221,19 @@ class Oscil
}
}

/** Set the frequency using UFixMath<24,8> fixed-point number format.
This might be faster than the float version for setting low frequencies such as
1.5 Hz, or other values which may not work well with your table size. A UFixMath<24,8>
representation of 1.5 is 384 (ie. 1.5 * 256). Can't be used with UPDATE_RATE
less than 64 Hz.
@param frequency in UFixMath<24,8> fixed-point number format.
*/
inline
void setFreq(UFixMath<24,8> frequency)
{
setFreq_Q24n8(frequency.asRaw());
}


/** Set the frequency using Q16n16 fixed-point number format. This is useful in
combination with Q16n16_mtof(), a fast alternative to mtof(), using Q16n16
Expand All @@ -230,6 +257,22 @@ class Oscil
phase_increment_fractional = ((unsigned long)frequency) / (UPDATE_RATE/NUM_TABLE_CELLS);
}
}


/** Set the frequency using UFixMath<16,16> fixed-point number format. This is useful in
combination with Q16n16_mtof(), a fast alternative to mtof(), using UFixMath<16,16>
fixed-point format instead of fractional numbers.
@note This should work OK with tables 2048 cells or smaller and
frequencies up to 4096 Hz. Can't be used with UPDATE_RATE less than 64 Hz.
@note This didn't run faster than float last time it was tested, after 2014 code changes. Need to see if 2014 changes improved or worsened performance.
@param frequency in UFixMath<16,16> fixed-point number format.
*/
inline
void setFreq(UFixMath<16,16> frequency)
{
setFreq_Q16n16(frequency.asRaw());
}

/*
inline
void setFreqMidi(int8_t note_num) {
Expand Down

0 comments on commit 73273a5

Please sign in to comment.