Skip to content

Commit

Permalink
Fix WASM audio MP3
Browse files Browse the repository at this point in the history
Increases the stack size to accommodate for the greedy MP3 decoder.

Discussion here: https://2dimensions.slack.com/archives/CLLCU09T6/p1712597103737719

Diffs=
cf43d9fdf Fix WASM audio MP3 (#6995)
0bc446fad negative speed fix (#6982)

Co-authored-by: Luigi Rosso <luigi-rosso@users.noreply.github.com>
  • Loading branch information
luigi-rosso and luigi-rosso committed Apr 9, 2024
1 parent 2acceb1 commit 4ede8c8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f832e26172cfbd11d52114ebb667e13418a5b1cd
cf43d9fdf8ab24146fd3c350121d200c43ca544d
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.13.2

- Support for asset audio volume.
- Fixed an issue with audio decoder in web build.

## 0.13.1

Expand Down
2 changes: 1 addition & 1 deletion lib/src/rive_core/animation/animation_state_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AnimationStateInstance extends StateInstance<AnimationState> {
.apply(animationInstance.time, coreContext: core, mix: mix);

@override
bool get keepGoing => animationInstance.keepGoing;
bool get keepGoing => animationInstance.shouldKeepGoing(state.speed);

@override
void clearSpilledTime() => animationInstance.clearSpilledTime();
Expand Down
8 changes: 8 additions & 0 deletions lib/src/rive_core/animation/linear_animation_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ class LinearAnimationInstance {
(directedSpeed > 0 && _time < animation.endSeconds) ||
(directedSpeed < 0 && _time > animation.startSeconds);

// We estimate whether the animation should keep playing using the speed
// multiplier provided by the caller
bool shouldKeepGoing(double speedMultiplier) {
return animation.loop != Loop.oneShot ||
(speedMultiplier * directedSpeed > 0 && _time < animation.endSeconds) ||
(speedMultiplier * directedSpeed < 0 && _time > animation.startSeconds);
}

/// Apply the changes incurred during advance, also automatically fires any
/// accrued events.
void apply(CoreContext core, {double mix = 1}) {
Expand Down
18 changes: 8 additions & 10 deletions lib/src/rive_core/audio_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AudioPlayer {
final List<AudioSound> _sounds = [];
int _soundStartTime = 0;
Duration _soundDuration = Duration.zero;
Duration? _soundEndTime;

Timer? _timer;

AudioPlayer({required AudioEngine? engine}) : _engine = engine;
Expand Down Expand Up @@ -72,7 +72,7 @@ class AudioPlayer {
_soundDuration = source.duration;
_soundStartTime = engineTime -
(startTime.inMicroseconds * 1e-6 * engine.sampleRate).round();
_soundEndTime = endTime;

_timer ??= Timer.periodic(const Duration(milliseconds: 0), _frameCallback);
}

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

Expand All @@ -109,22 +110,19 @@ class AudioPlayer {
: (time.value.inMicroseconds / _soundDuration.inMicroseconds)
.clamp(0, 1);

if (_soundEndTime != null) {
if (time.value > _soundEndTime!) {
stop();
}
}
var completed = _sounds.where((sound) => sound.completed).toList();

if (time.value > _soundDuration && _sounds.length == 1) {
stop();
_sounds.removeWhere((sound) => sound.completed);
for (final sound in completed) {
sound.dispose();
}
}

void stop() {
isPlaying.value = false;
_timer?.cancel();
_timer = null;
_soundEndTime = null;

time.value = Duration.zero;
normalizedTime.value = 0;
for (final sound in _sounds) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies:
http: ">=0.13.3 <2.0.0"
meta: ^1.3.0
plugin_platform_interface: ^2.0.2
rive_common: 0.4.4
rive_common: 0.4.5
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 4ede8c8

Please sign in to comment.