Skip to content

Commit b147315

Browse files
committed
Make AudioSample.isPlaying() check whether the sample has finished (fixes #39)
Bump version to 2.2.2 (15)
1 parent 7a56aa4 commit b147315

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ authors=The Processing Foundation
44
url=https://processing.org/reference/libraries/sound/index.html
55
sentence=Provides a simple way to work with audio.
66
paragraph=
7-
version=14
8-
prettyVersion=2.2.1
7+
version=15
8+
prettyVersion=2.2.2
99
lastUpdated=0
1010
minRevision=228
1111
maxRevision=0

src/processing/sound/AudioSample.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.jsyn.unitgen.VariableRateDataReader;
55
import com.jsyn.unitgen.VariableRateMonoReader;
66
import com.jsyn.unitgen.VariableRateStereoReader;
7+
import com.softsynth.shared.time.TimeStamp;
78

89
import processing.core.PApplet;
910

@@ -120,11 +121,26 @@ protected void initiatePlayer() {
120121
// as long as their dataQueue is empty
121122
super.play(); // doesn't actually start playback, just adds the (silent) units
122123

123-
// this is kinda hack-ish, should maybe replace with its own boolean (and
124-
// overwrite public boolean isPlaying() to return that boolean instead)
124+
// as a consequence, the AudioSample class manages its isPlaying status
125+
// explicitly (see the overridden public boolean isPlaying() method below)
125126
this.isPlaying = false;
126127
}
127128

129+
/**
130+
* Check whether this audiosample is currently playing.
131+
* @Override
132+
**/
133+
public boolean isPlaying() {
134+
// this used to just return dataQueue.hasMore(), but this raises false
135+
// positives as .hasMore() still returns true for a split second after
136+
// dataQueue.clear() has been called. the AudioSample class therefore
137+
// manually sets the isPlaying boolean to true when playback commences,
138+
// but every time the user queries the playing status we check if the
139+
// queue has actually run out in the meantime.
140+
this.isPlaying = this.isPlaying & this.player.dataQueue.hasMore();
141+
return this.isPlaying;
142+
}
143+
128144
/**
129145
* Change the amplitude/volume of this audiosample.
130146
*
@@ -380,7 +396,8 @@ private void playInternal() {
380396

381397
private void playInternal(int startFrame, int numFrames) {
382398
this.setStartFrameCountOffset();
383-
this.player.dataQueue.queue(this.sample, startFrame, numFrames);
399+
// only queueImmediate() guarantees that a directly subsequent call to .hasMore() returns true
400+
this.player.dataQueue.queueImmediate(this.sample, startFrame, numFrames, new TimeStamp(0.0));
384401
this.isPlaying = true;
385402
}
386403

@@ -617,7 +634,7 @@ public void pause() {
617634
this.startFrame = this.positionFrame();
618635
this.setStartFrameCountOffset();
619636
} else {
620-
Engine.printWarning("audio sample is already paused");
637+
Engine.printWarning("trying to pause an audio sample that is not playing");
621638
}
622639
}
623640

0 commit comments

Comments
 (0)