16 #include "mozzi_fixmath.h"
17 #include "mozzi_pgmspace.h"
20 #define SAMPLE_F_BITS 16
21 #define SAMPLE_F_BITS_AS_MULTIPLIER 65536
25 #define SAMPLE_PHMOD_BITS 16
27 enum interpolation {INTERP_NONE, INTERP_LINEAR};
47 template <
unsigned int NUM_TABLE_CELLS,
unsigned int UPDATE_RATE, u
int8_t INTERP=INTERP_NONE>
59 Sample(
const int8_t * TABLE_NAME):table(TABLE_NAME),endpos_fractional((unsigned long) NUM_TABLE_CELLS << SAMPLE_F_BITS)
71 Sample():endpos_fractional((unsigned long) NUM_TABLE_CELLS << SAMPLE_F_BITS)
94 startpos_fractional = (
unsigned long) startpos << SAMPLE_F_BITS;
103 phase_fractional = startpos_fractional;
124 endpos_fractional = (
unsigned long) end << SAMPLE_F_BITS;
133 startpos_fractional = 0;
134 endpos_fractional = (
unsigned long) NUM_TABLE_CELLS << SAMPLE_F_BITS;
167 if (phase_fractional>endpos_fractional){
169 phase_fractional = startpos_fractional + (phase_fractional - endpos_fractional);
175 if(INTERP==INTERP_LINEAR){
177 unsigned int index = phase_fractional >> SAMPLE_F_BITS;
178 out = FLASH_OR_RAM_READ<const int8_t>(table + index);
179 int8_t difference = FLASH_OR_RAM_READ<const int8_t>((table + 1) + index) - out;
180 int8_t diff_fraction = (int8_t)(((((
unsigned int) phase_fractional)>>8)*difference)>>8);
181 out += diff_fraction;
183 out = FLASH_OR_RAM_READ<const int8_t>(table + (phase_fractional >> SAMPLE_F_BITS));
195 return phase_fractional<endpos_fractional;
226 phase_increment_fractional = ((((
unsigned long)NUM_TABLE_CELLS<<ADJUST_FOR_NUM_TABLE_CELLS)*frequency)/UPDATE_RATE) << (SAMPLE_F_BITS - ADJUST_FOR_NUM_TABLE_CELLS);
238 phase_increment_fractional = (
unsigned long)((((
float)NUM_TABLE_CELLS * frequency)/UPDATE_RATE) * SAMPLE_F_BITS_AS_MULTIPLIER);
254 phase_increment_fractional = (((((
unsigned long)NUM_TABLE_CELLS<<ADJUST_FOR_NUM_TABLE_CELLS)>>3)*frequency)/(UPDATE_RATE>>6))
255 << (SAMPLE_F_BITS - ADJUST_FOR_NUM_TABLE_CELLS - (8-3+6));
266 return FLASH_OR_RAM_READ<const int8_t>(table + index);
283 return (((
unsigned long)frequency * NUM_TABLE_CELLS)/UPDATE_RATE) << SAMPLE_F_BITS;
293 phase_increment_fractional = phaseinc_fractional;
302 static const uint8_t ADJUST_FOR_NUM_TABLE_CELLS = (NUM_TABLE_CELLS<2048) ? 8 : 0;
308 void incrementPhase()
310 phase_fractional += phase_increment_fractional;
314 volatile unsigned long phase_fractional;
315 volatile unsigned long phase_increment_fractional;
316 const int8_t * table;
318 unsigned long startpos_fractional, endpos_fractional;
Sample is like Oscil, it plays a wavetable.
void setStart(unsigned int startpos)
Sets the starting position in samples.
int8_t next()
Returns the sample at the current phase position, or 0 if looping is off and the phase overshoots the...
Sample(const int8_t *TABLE_NAME)
Constructor.
unsigned long phaseIncFromFreq(unsigned int frequency)
phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies.
void setLoopingOn()
Turns looping on.
void start()
Resets the phase (the playhead) to the start position, which will be 0 unless set to another value wi...
void setFreq(float frequency)
Set the sample frequency with a float.
int8_t atIndex(unsigned int index)
Returns the sample at the given table index.
void rangeWholeSample()
Sets the start and end points to include the range of the whole sound table.
void setFreq_Q24n8(Q24n8 frequency)
Set the frequency using Q24n8 fixed-point number format.
void setEnd(unsigned int end)
Sets the end position in samples from the beginning of the sound.
void setFreq(int frequency)
Set the oscillator frequency with an unsigned int.
void setPhaseInc(unsigned long phaseinc_fractional)
Set a specific phase increment.
void start(unsigned int startpos)
Sets a new start position plays the sample from that position.
void setLoopingOff()
Turns looping off.
void setTable(const int8_t *TABLE_NAME)
Change the sound table which will be played by the Sample.
boolean isPlaying()
Checks if the sample is playing by seeing if the phase is within the limits of its end position.
uint32_t Q24n8
unsigned fractional number using 24 integer bits and 8 fractional bits, represents 0 to 16777215