Skip to content

Commit

Permalink
fix: reduce audio polling
Browse files Browse the repository at this point in the history
Addresses #411

I cannot reproduce the 100% CPU utilization; however, maybe this is a Flutter FFI bug with a specific version or environment.

@luigi-rosso, in the linked issue, you mention a `TrackingAudioPlayer.` It's not clear to me when we would need that at this stage of the audio capabilities we provide.

Currently polling every second. This is arbitrary, any suggestions on a value?

Diffs=
eccfca528 fix: reduce audio polling (#8238)
4566a1208 Clone draw for cache optimizations (#8176)
56511a285 Fix layout drag/drop offset (#8220)
f0c58200a fix text origin offset (#8223)
35734d2d5 change dash formula precision (#8219)
be2c46a3e add bindable layout properties (#8212)
cea19ecc6 add new properties to data bind (#8211)
e67109195 Layout text size fix (#8209)
77b9ee56d Add linux build step for rive_native changes (#8194)
37663bd03 advance iterator until distance does not equal 0 (#8165)
1713983ab runtime dashing! (#8191)
d0d1dbf33 fix dash glitch (#8188)
8a974784d Dashing (through the snow) (#8093)
9e4b81762 Update player to run without the python server (#8175)
aa91ffe21 Rename gpuAtomicResolve -> atomicResolve, et. al. (#8168)
adc240554 Overhaul Vulkan synchronization (#8166)
9dd20080c Remove Skia from ios_tests and quit building it on many runners (#8144)
5cdcc183f Nnnn data context fixes (#8148)
7e8100fee Colinear and bounds ffi (#8158)
15d3e9961 add support for elastic interpolation (#8154)
95e58ca40 editor: Stage UI for N-Slicing (#8136)
5719c5662 Add contour measure to Rive Native (#8145)
8441098ec adding length getter to dash path effect (#8140)

Co-authored-by: Gordon <[email protected]>
  • Loading branch information
HayesGordon and HayesGordon committed Sep 28, 2024
1 parent 11f0c50 commit d9df7cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
95cad5c6055750ff6b1c033f903967e6b9c22bb6
eccfca52894894d158761f902f46ed48b0794991
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.13.14

- Reduce audio polling and unneeded runtime calculations around audio. Resolves issue [411](https://github.com/rive-app/rive-flutter/issues/411)

## 0.13.13

- Update Android `minSdkVersion` from 16 to 19
Expand Down
24 changes: 6 additions & 18 deletions lib/src/rive_core/audio_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import 'package:rive/src/rive_core/assets/audio_asset.dart';
import 'package:rive_common/rive_audio.dart';

class AudioPlayer {
final ValueNotifier<Duration> time = ValueNotifier(Duration.zero);
final ValueNotifier<double> normalizedTime = ValueNotifier(0);
final ValueNotifier<bool> isPlaying = ValueNotifier(false);
AudioEngine? _engine;
AudioEngine? get engine => _engine;
final List<AudioSound> _sounds = [];
int _soundStartTime = 0;
Duration _soundDuration = Duration.zero;

Timer? _timer;

Expand Down Expand Up @@ -69,10 +65,7 @@ class AudioPlayer {
sound.volume = volume;
}
_sounds.add(sound);
_soundDuration = source.duration;
_soundStartTime = engineTime -
(startTime.inMicroseconds * 1e-6 * engine.sampleRate).round();
_timer ??= Timer.periodic(const Duration(milliseconds: 0), _frameCallback);
_timer ??= Timer.periodic(const Duration(seconds: 1), _frameCallback);
}

bool playSource(AudioAsset? audio) {
Expand All @@ -92,7 +85,7 @@ class AudioPlayer {
sound.volume = audio?.volume ?? 1;
}
_sounds.add(sound);
_timer ??= Timer.periodic(const Duration(milliseconds: 0), _frameCallback);
_timer ??= Timer.periodic(const Duration(seconds: 1), _frameCallback);
return true;
}

Expand All @@ -101,28 +94,23 @@ class AudioPlayer {
if (engine == null) {
return;
}
var elapsedAudioFrames = engine.timeInFrames - _soundStartTime;
time.value = Duration(
microseconds: (elapsedAudioFrames / engine.sampleRate / 1e-6).floor());
normalizedTime.value = _soundDuration == Duration.zero
? 0
: (time.value.inMicroseconds / _soundDuration.inMicroseconds)
.clamp(0, 1);

var completed = _sounds.where((sound) => sound.completed).toList();

_sounds.removeWhere((sound) => sound.completed);
for (final sound in completed) {
sound.dispose();
}
if (_sounds.isEmpty) {
_timer?.cancel();
_timer = null;
}
}

void stop() {
isPlaying.value = false;
_timer?.cancel();
_timer = null;
time.value = Duration.zero;
normalizedTime.value = 0;
for (final sound in _sounds) {
sound.stop();
sound.dispose();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: rive
version: 0.13.13
version: 0.13.14
homepage: https://rive.app
description: Rive Flutter Runtime. This package provides runtime functionality for playing back and interacting with animations built with the Rive editor available at https://rive.app.
repository: https://github.com/rive-app/rive-flutter
Expand Down

0 comments on commit d9df7cd

Please sign in to comment.