Skip to content

Commit

Permalink
Add AudioSample.playMode() setters
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinstadler committed Oct 4, 2023
1 parent 63ed663 commit e6e2470
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/processing/sound/AudioSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class AudioSample extends SoundObject {
protected FloatSample sample;
protected VariableRateDataReader player;

// what to do when a sample that's already playing is triggered again.
// default is 'sustain'
protected int playMode = 0;

// cued frame index of this sample
protected int startFrame = 0;
// DataReader's queue status, required for accurate computation of playback
Expand Down Expand Up @@ -749,6 +753,34 @@ protected boolean checkStartFrame(int startFrame, boolean verbose) {
}
}

/**
* The play mode determines what happens to a an AudioSample (or SoundFile)
* when it is triggered while in the middle of playback. In sustain mode (the
* default), playback will continue simultaneous to the new playback. In
* restart mode, play() will stop playback and start over. With untilDone, a
* sound will play only if it's not already playing.
*/
void playMode(int mode) {
if (mode >= SUSTAIN && mode <= UNTILDONE) {
this.playMode = mode;
} else {
Engine.printError("invalid play mode. needs to be one of SUSTAIN, RESTART or UNTILDONE");
}
}

// for compatibility with p5.js
void playMode(String mode) {
if (mode.equalsIgnoreCase("sustain")) {
this.playMode(SUSTAIN);
} else if (mode.equalsIgnoreCase("restart")) {
this.playMode(RESTART);
} else if (mode.equalsIgnoreCase("untildone")) {
this.playMode(UNTILDONE);
} else {
Engine.printError("invalid play mode. needs to be one of \"sustain\", \"restart\" or \"untildone\"");
}
}

/**
* Get the current sample data and write it into the given array.
*
Expand Down

0 comments on commit e6e2470

Please sign in to comment.