Skip to content

Commit d044a76

Browse files
committed
ANDROID: sound items made complete within the audio callback
1 parent caff310 commit d044a76

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/platform/android/jni/audio.cpp

+23-25
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@
2121
constexpr int32_t AUDIO_SAMPLE_RATE = 44100;
2222
constexpr float PI2 = 2.0f * M_PI;
2323
constexpr int SILENCE_BEFORE_STOP = kMillisPerSecond * 5;
24-
constexpr int MAX_RAMP = 450;
24+
constexpr int MAX_RAMP = 250;
25+
constexpr int MAX_QUEUE_SIZE = 250;
2526
constexpr int RAMP_SCALE = 10;
2627
int instances = 0;
2728

2829
struct Sound {
29-
Sound(int blockSize, bool fadeIn, int frequency, int millis, int volume);
30+
Sound(bool fadeIn, int frequency, int millis, int volume);
3031
~Sound();
3132

3233
bool ready() const;
3334
float sample();
34-
void sync(Sound *previous, bool lastSound, int blockSize);
35+
void sync(Sound *previous, bool lastSound);
3536

3637
private:
3738
uint32_t _duration;
@@ -45,7 +46,7 @@ struct Sound {
4546
float _phase;
4647
};
4748

48-
Sound::Sound(int blockSize, bool fadeIn, int frequency, int millis, int volume) :
49+
Sound::Sound(bool fadeIn, int frequency, int millis, int volume) :
4950
_duration(millis),
5051
_samples(AUDIO_SAMPLE_RATE * millis / 1000),
5152
_sampled(0),
@@ -61,10 +62,6 @@ Sound::Sound(int blockSize, bool fadeIn, int frequency, int millis, int volume)
6162
_fadeIn = std::min((int)_samples / RAMP_SCALE, MAX_RAMP);
6263
} else if (frequency == 0) {
6364
_rest = std::min((int)_samples / RAMP_SCALE, MAX_RAMP);
64-
// align sample size with burst-size
65-
if (_rest != 0 && _samples > blockSize) {
66-
_samples = (_samples / blockSize) * blockSize;
67-
}
6865
}
6966
}
7067

@@ -104,23 +101,21 @@ float Sound::sample() {
104101
// fadeOut to silence
105102
result *= (float)(_samples - _sampled) / (float)_fadeOut;
106103
}
104+
107105
return result;
108106
}
109107

110108
//
111109
// Continues the same phase for the previous sound
112110
//
113-
void Sound::sync(Sound *previous, bool lastSound, int blockSize) {
111+
void Sound::sync(Sound *previous, bool lastSound) {
114112
_phase = previous->_phase;
115113
if (_rest != 0) {
116114
// for fadeOut/In for adjoining silence
117115
_increment = previous->_increment;
118116
}
119117
if (lastSound) {
120118
// fade out non-silent sound to silence
121-
if (_samples > blockSize) {
122-
_samples = (_samples / blockSize) * blockSize;
123-
}
124119
_fadeOut = _samples / RAMP_SCALE;
125120
if (_fadeOut > MAX_RAMP) {
126121
_fadeOut = MAX_RAMP;
@@ -163,7 +158,7 @@ Audio::~Audio() {
163158
// Play a sound with the given specification
164159
//
165160
void Audio::play(int frequency, int millis, int volume, bool background) {
166-
if (_stream != nullptr && millis > 0) {
161+
if (_stream != nullptr && millis > 0 && _queue.size() < MAX_QUEUE_SIZE) {
167162
add(frequency, millis, volume);
168163
if (_stream->getState() != StreamState::Started) {
169164
trace("Start audio");
@@ -192,22 +187,25 @@ void Audio::clearSoundQueue() {
192187
// Callback to play the next block of audio
193188
//
194189
DataCallbackResult Audio::onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numFrames) {
195-
DataCallbackResult result = DataCallbackResult::Continue;
196190
Sound *sound = front();
197191
auto *buffer = (float *)audioData;
198-
if (sound == nullptr) {
199-
for (int i = 0; i < numFrames; ++i) {
192+
for (int i = 0; i < numFrames; ++i) {
193+
if (sound == nullptr) {
200194
buffer[i] = 0;
201-
}
202-
// continue filling with silence in case the script requests further sounds
203-
if (_startNoSound != 0 && maGetMilliSecondCount() - _startNoSound > SILENCE_BEFORE_STOP) {
204-
result = DataCallbackResult::Stop;
205-
}
206-
} else {
207-
for (int i = 0; i < numFrames; ++i) {
195+
} else {
208196
buffer[i] = sound->sample();
197+
if (!sound->ready()) {
198+
sound = front();
199+
}
209200
}
210201
}
202+
203+
DataCallbackResult result;
204+
if (sound == nullptr && _startNoSound != 0 && maGetMilliSecondCount() - _startNoSound > SILENCE_BEFORE_STOP) {
205+
result = DataCallbackResult::Stop;
206+
} else {
207+
result = DataCallbackResult::Continue;
208+
}
211209
return result;
212210
}
213211

@@ -216,7 +214,7 @@ DataCallbackResult Audio::onAudioReady(AudioStream *oboeStream, void *audioData,
216214
//
217215
void Audio::add(int frequency, int millis, int volume) {
218216
std::lock_guard<std::mutex> lock(_lock);
219-
_queue.push(new Sound(_stream->getFramesPerBurst(), _queue.empty(), frequency, millis, volume));
217+
_queue.push(new Sound(_queue.empty(), frequency, millis, volume));
220218
_startNoSound = 0;
221219
}
222220

@@ -236,7 +234,7 @@ Sound *Audio::front() {
236234
_queue.pop();
237235
auto *next = _queue.front();
238236
if (next != nullptr) {
239-
next->sync(result, _queue.size() == 1, _stream->getFramesPerBurst());
237+
next->sync(result, _queue.size() == 1);
240238
}
241239
result = nullptr;
242240
}

0 commit comments

Comments
 (0)