Skip to content

Commit 3297754

Browse files
committed
Add getBufferCapacityInFrames and fix buffer slider logic
1 parent be31d24 commit 3297754

5 files changed

Lines changed: 31 additions & 17 deletions

File tree

samples/powerplay/src/main/cpp/PowerPlayJNI.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ Java_com_google_oboe_samples_powerplay_engine_PowerPlayAudioPlayer_setBufferSize
265265
return player.setBufferSizeInFrames(bufferSizeInFrames);
266266
}
267267

268+
/**
269+
* Native (JNI) implementation of PowerPlayAudioPlayer.getBufferSizeInFramesNative()
270+
*/
271+
JNIEXPORT jint JNICALL
272+
Java_com_google_oboe_samples_powerplay_engine_PowerPlayAudioPlayer_getBufferCapacityInFramesNative(
273+
JNIEnv *env,
274+
jobject) {
275+
return player.getBufferCapacityInFrames();
276+
}
268277

269278
#ifdef __cplusplus
270279
}

samples/powerplay/src/main/cpp/PowerPlayMultiPlayer.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,6 @@ int32_t PowerPlayMultiPlayer::setBufferSizeInFrames(int32_t requestedFrames) {
212212
return static_cast<int32_t>(Result::ErrorNull);
213213
}
214214

215-
// Determine target size: Use maximum capacity if 0 or less is requested.
216-
const int32_t capacity = mAudioStream->getBufferCapacityInFrames();
217-
218215
__android_log_print(ANDROID_LOG_INFO, TAG, "Requesting buffer size: %d frames (Input: %d)",
219216
requestedFrames, requestedFrames);
220217

@@ -227,10 +224,15 @@ int32_t PowerPlayMultiPlayer::setBufferSizeInFrames(int32_t requestedFrames) {
227224
}
228225

229226
// Return the actual value granted by the hardware.
230-
const int32_t actualFrames = result.value();
231-
__android_log_print(ANDROID_LOG_INFO, TAG, "Buffer size set to %d frames (Max Capacity: %d)",
232-
actualFrames, capacity);
233-
234-
return actualFrames;
227+
__android_log_print(ANDROID_LOG_INFO, TAG, "Buffer size set to %d frames.", result.value());
228+
return result.value();
235229
}
236230

231+
int32_t PowerPlayMultiPlayer::getBufferCapacityInFrames() {
232+
if (mAudioStream == nullptr) {
233+
__android_log_print(ANDROID_LOG_ERROR, TAG, "getBufferCapacityInFrames: Stream is null");
234+
return static_cast<int32_t>(Result::ErrorNull);
235+
}
236+
237+
return mAudioStream->getBufferCapacityInFrames();
238+
}

samples/powerplay/src/main/cpp/PowerPlayMultiPlayer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class PowerPlayMultiPlayer : public iolib::SimpleMultiPlayer {
4848

4949
int32_t setBufferSizeInFrames(int32_t bufferSizeInFrames);
5050

51+
int32_t getBufferCapacityInFrames();
52+
5153
private:
5254
class MyPresentationCallback : public oboe::AudioStreamPresentationCallback {
5355
public:

samples/powerplay/src/main/kotlin/com/google/oboe/samples/powerplay/MainActivity.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,24 +341,22 @@ class MainActivity : ComponentActivity() {
341341
horizontalAlignment = Alignment.CenterHorizontally
342342
) {
343343
val requestedFrames = remember { mutableIntStateOf(0) }
344-
val actualSize = remember { mutableIntStateOf(0) }
344+
val actualFrames = remember { mutableIntStateOf(0) }
345345

346346
Slider(
347347
value = sliderPosition,
348348
onValueChange = { newValue ->
349349
sliderPosition = newValue
350-
requestedFrames.intValue = (sliderPosition * sampleRate).toInt()
350+
requestedFrames.intValue = sliderPosition.toInt()
351351
},
352352
onValueChangeFinished = {
353-
requestedFrames.intValue = (sliderPosition * sampleRate).toInt()
354-
actualSize.intValue =
355-
player.setBufferSizeInFrames(requestedFrames.intValue)
353+
requestedFrames.intValue = sliderPosition.toInt()
354+
actualFrames.value = player.setBufferSizeInFrames(requestedFrames.intValue)
356355
},
357-
valueRange = 0f..30f,
356+
valueRange = 0f..player.getBufferCapacityInFrames().toFloat(),
358357
)
359358

360-
val actualSeconds =
361-
if (sampleRate > 0) actualSize.intValue.toDouble() / sampleRate else 0.0
359+
val actualSeconds = actualFrames.value.toDouble() / sampleRate
362360
val formattedSeconds = "%.3f".format(actualSeconds)
363361

364362
Column(horizontalAlignment = Alignment.CenterHorizontally) {
@@ -368,7 +366,7 @@ class MainActivity : ComponentActivity() {
368366
style = MaterialTheme.typography.bodySmall
369367
)
370368
Text(
371-
text = "Actual: ${actualSize.intValue} Frames ($formattedSeconds seconds)",
369+
text = "Actual: ${actualFrames.value} Frames ($formattedSeconds seconds)",
372370
color = Color.Black,
373371
style = MaterialTheme.typography.bodySmall
374372
)

samples/powerplay/src/main/kotlin/com/google/oboe/samples/powerplay/engine/PowerPlayAudioPlayer.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class PowerPlayAudioPlayer() : DefaultLifecycleObserver {
9191
*/
9292
fun setBufferSizeInFrames(bufferSizeInFrames: Int): Int = setBufferSizeInFramesNative(bufferSizeInFrames)
9393

94+
fun getBufferCapacityInFrames(): Int = getBufferCapacityInFramesNative()
95+
9496
/**
9597
* Native functions.
9698
* Load the library containing the native code including the JNI functions.
@@ -113,6 +115,7 @@ class PowerPlayAudioPlayer() : DefaultLifecycleObserver {
113115
private external fun isMMapEnabledNative(): Boolean
114116
private external fun isMMapSupportedNative(): Boolean
115117
private external fun setBufferSizeInFramesNative(bufferSizeInFrames: Int): Int
118+
private external fun getBufferCapacityInFramesNative(): Int
116119

117120
/**
118121
* Companion

0 commit comments

Comments
 (0)