Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ class Sample
}


/** Resets the phase (the playhead) to the start position, which will be 0 unless set to another value with setStart();
*/
/**
* Resets the phase (the playhead) to the start position, which will be 0 unless set to another value with setStart();
* Also unpauses playback if it's paused.
*/
inline
void start()
{
phase_fractional = startpos_fractional;
if(paused) setPaused(false);
}


Expand All @@ -114,6 +117,20 @@ class Sample
start();
}

/** Puts playback of the sample on pause or unpauses it.
* When paused, "next()" does not advance position and returns 0.
* @param to_pause whether to pause or unpause, default true
*/
inline
void setPaused(bool to_pause = true)
{
paused = to_pause;
}

/** Checks whether playback is paused. */
inline
bool isPaused() { return paused; }


/** Sets the end position in samples from the beginning of the sound.
@param end position in samples.
Expand Down Expand Up @@ -164,6 +181,8 @@ class Sample
inline
int8_t next() { // 4us

if (paused) return 0;

if (phase_fractional>endpos_fractional){
if (looping) {
phase_fractional = startpos_fractional + (phase_fractional - endpos_fractional);
Expand Down Expand Up @@ -192,7 +211,7 @@ class Sample
*/
inline
boolean isPlaying(){
return phase_fractional<endpos_fractional;
return phase_fractional<endpos_fractional && !paused;
}


Expand Down Expand Up @@ -315,6 +334,7 @@ static const uint8_t ADJUST_FOR_NUM_TABLE_CELLS = (NUM_TABLE_CELLS<2048) ? 8 : 0
volatile unsigned long phase_increment_fractional;
const int8_t * table;
bool looping;
bool paused{false};
unsigned long startpos_fractional, endpos_fractional;
};

Expand Down
Loading