Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/feature/sound/playback/idle_watch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'dart:async';

/// Calls [onIdle] once something has stayed idle for [after], unless it got
/// busy again before.
class IdleWatch {
IdleWatch({required this.after, required this.onIdle});

final Duration after;
final void Function() onIdle;

Timer? _timer;

/// Starts counting, from the start again if it already counts.
void idle() {
_timer?.cancel();
_timer = Timer(after, () {
_timer = null;
onIdle();
});
}

/// Stops counting.
void busy() {
_timer?.cancel();
_timer = null;
}

void dispose() => busy();
}
63 changes: 63 additions & 0 deletions lib/feature/sound/playback/playback_timeline.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/// Where a playback is in its file, in seconds of the file's own time, read
/// against a clock that runs in seconds, such as the `currentTime` of an
/// audio context.
///
/// A rate above 1 plays the file faster, so its position runs ahead of the
/// clock. Every position stays within the file's [duration].
class PlaybackTimeline {
PlaybackTimeline({required this.duration});

/// The length of the file in seconds.
final double duration;

/// The position at [_since] on the clock.
double _position = 0;
double _since = 0;
double _rate = 1;
bool _paused = true;

/// Whether it holds its position; also before [start].
bool get isPaused => _paused;

/// Runs from [from] at [rate], starting at [at] on the clock.
void start({required double at, required double from, required double rate}) {
_position = _clamp(from);
_since = at;
_rate = rate;
_paused = false;
}

/// Holds the position it has at [at].
void pause({required double at}) {
_position = positionAt(at);
_since = at;
_paused = true;
}

/// Runs on from the held position, starting at [at].
void resume({required double at}) {
_since = at;
_paused = false;
}

/// Moves to [position] at [at], running on from there unless paused.
void seek(double position, {required double at}) {
_position = _clamp(position);
_since = at;
}

/// Runs at [rate] from [at] on.
void changeRate(double rate, {required double at}) {
_position = positionAt(at);
_since = at;
_rate = rate;
}

/// The position at [at] on the clock.
double positionAt(double at) {
if (_paused) return _position;
return _clamp(_position + (at - _since) * _rate);
}

double _clamp(double position) => position.clamp(0, duration).toDouble();
}
4 changes: 3 additions & 1 deletion lib/feature/sound/playback/sound_playback_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ class SoundPlaybackController extends ChangeNotifier {
void dispose() {
_disposed = true;
_generation++;
player.stop();
player
..stop()
..dispose();
super.dispose();
}
}
4 changes: 4 additions & 0 deletions lib/feature/sound/playback/sound_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ abstract interface class SoundPlayer {
/// [play] takes them.
void adjust({required double volume, required double pitch});

/// Stops what plays and frees what the player holds, such as the
/// browser's audio. The player is not used afterwards.
void dispose();

/// How far the file that plays is, in the file's own time; zero when none
/// plays.
Duration get position;
Expand Down
5 changes: 4 additions & 1 deletion lib/feature/sound/playback/sound_player_stub.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:stelaris/api/service/sound/sound_file_loader.dart';
import 'package:stelaris/feature/sound/playback/sound_player.dart';

/// Outside the web build there is no audio element to play with.
/// Outside the web build there is no Web Audio to play with.
SoundPlayer createPlatformSoundPlayer() => _UnsupportedSoundPlayer();

class _UnsupportedSoundPlayer implements SoundPlayer {
Expand All @@ -28,6 +28,9 @@ class _UnsupportedSoundPlayer implements SoundPlayer {
@override
void adjust({required double volume, required double pitch}) {}

@override
void dispose() {}

@override
Duration get position => Duration.zero;
}
Loading
Loading