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;
105 phase_fractional = startpos_fractional;
141 endpos_fractional = (
unsigned long) end << SAMPLE_F_BITS;
150 startpos_fractional = 0;
151 endpos_fractional = (
unsigned long) NUM_TABLE_CELLS << SAMPLE_F_BITS;
184 if (paused)
return 0;
185 if (phase_fractional>=endpos_fractional){
187 phase_fractional = startpos_fractional + (phase_fractional - endpos_fractional);
193 if(INTERP==INTERP_LINEAR){
195 unsigned int index = phase_fractional >> SAMPLE_F_BITS;
196 out = FLASH_OR_RAM_READ<const int8_t>(table + index);
197 int8_t difference = FLASH_OR_RAM_READ<const int8_t>((table + 1) + index) - out;
198 int8_t diff_fraction = (int8_t)(((((
unsigned int) phase_fractional)>>8)*difference)>>8);
199 out += diff_fraction;
201 out = FLASH_OR_RAM_READ<const int8_t>(table + (phase_fractional >> SAMPLE_F_BITS));
213 return phase_fractional<endpos_fractional && !paused;
244 phase_increment_fractional = ((((
unsigned long)NUM_TABLE_CELLS<<ADJUST_FOR_NUM_TABLE_CELLS)*frequency)/UPDATE_RATE) << (SAMPLE_F_BITS - ADJUST_FOR_NUM_TABLE_CELLS);
256 phase_increment_fractional = (
unsigned long)((((
float)NUM_TABLE_CELLS * frequency)/UPDATE_RATE) * SAMPLE_F_BITS_AS_MULTIPLIER);
272 phase_increment_fractional = (((((
unsigned long)NUM_TABLE_CELLS<<ADJUST_FOR_NUM_TABLE_CELLS)>>3)*frequency)/(UPDATE_RATE>>6))
273 << (SAMPLE_F_BITS - ADJUST_FOR_NUM_TABLE_CELLS - (8-3+6));
284 return FLASH_OR_RAM_READ<const int8_t>(table + index);
301 return (((
unsigned long)frequency * NUM_TABLE_CELLS)/UPDATE_RATE) << SAMPLE_F_BITS;
311 phase_increment_fractional = phaseinc_fractional;
320 static const uint8_t ADJUST_FOR_NUM_TABLE_CELLS = (NUM_TABLE_CELLS<2048) ? 8 : 0;
326 void incrementPhase()
328 phase_fractional += phase_increment_fractional;
332 volatile unsigned long phase_fractional;
333 volatile unsigned long phase_increment_fractional;
334 const int8_t * table;
337 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.
bool isPaused() const
Checks whether playback is paused.
void setFreq(int frequency)
Set the oscillator frequency with an unsigned int.
void setPhaseInc(unsigned long phaseinc_fractional)
Set a specific phase increment.
void setPaused(bool to_pause=true)
Puts playback of the sample on pause or unpauses it.
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