From 1bd2ff5e0836b894f176c715b9c71117f85ae542 Mon Sep 17 00:00:00 2001 From: kevinstadler Date: Wed, 4 Oct 2023 15:56:08 +0200 Subject: [PATCH] Add AudioSample.playMode() setters --- src/processing/sound/AudioSample.java | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/processing/sound/AudioSample.java b/src/processing/sound/AudioSample.java index 2985e3b..a3e7fe0 100644 --- a/src/processing/sound/AudioSample.java +++ b/src/processing/sound/AudioSample.java @@ -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 @@ -749,6 +753,37 @@ protected boolean checkStartFrame(int startFrame, boolean verbose) { } } + public static int SUSTAIN = 0; + public static int RESTART = 1; + public static int UNTILDONE = 2; + + /** + * 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"); + } + } + + 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. *