fix(player): readable speed and pitch sliders

The speed slider stepped by 0.05x (31 stops) and pitch by half a
semitone (49 stops), which drew the track as a dense dotted line.
Speed now steps by 0.1x (16 stops) with the quick choices still
offering 0.75x and 1.25x, and pitch by whole semitones (25 stops);
half semitones saved by earlier builds round to whole ones.

The quick choices use the button group's disabled style when the
output cannot change speed, and Material's compact player shows the
speed and pitch label that the full player's format row carries.
This commit is contained in:
zarzet committed 2026-11-11 15:34:04 +07:00
1 parent 9ece12f97d
commit 23c74e4977
6 files changed
+121 -71

No files matched your search

+6 -2
View File
@@ -55,8 +55,12 @@ double playbackSpeedFromJson(Object? value) => value is num && value.isFinite
/// Largest pitch shift either way, in semitones.
const double maxPlaybackPitch = 12;
/// The pitch shift in whole semitones, the steps a key changes by.
double playbackPitchFromJson(Object? value) => value is num && value.isFinite
? value.toDouble().clamp(-maxPlaybackPitch, maxPlaybackPitch)
? value.toDouble().roundToDouble().clamp(
-maxPlaybackPitch,
maxPlaybackPitch,
)
: 0;
/// Smallest and largest Interface size, as a factor of the normal size.
@@ -177,7 +181,7 @@ class AppSettings {
/// follow the speed, as a record played faster or slower.
final bool playbackKeepPitch;
/// Pitch shift in semitones on top of [playbackKeepPitch], from
/// Pitch shift in whole semitones on top of [playbackKeepPitch], from
/// -[maxPlaybackPitch] to [maxPlaybackPitch]; 0 is the original key.
@JsonKey(fromJson: playbackPitchFromJson)
final double playbackPitch;
+17 -19
View File
@@ -17,13 +17,19 @@ import 'package:mornye/widgets/slider_value_header.dart';
/// The quick speed choices under the speed slider.
const playbackTempoQuickSpeeds = [0.75, 1.0, 1.25, 1.5];
/// [speed] on the slider's 0.05 grid.
/// Speed slider steps of 0.1x from 0.5x to 2x. Fewer stops keep the track
/// readable; the quick choices add 0.75x and 1.25x between them.
const _speedDivisions = 15;
/// Pitch slider steps of one semitone from -12 to +12.
const _pitchDivisions = 24;
/// [speed] on the 0.05 grid the slider steps and quick choices share.
double _snapSpeed(double speed) =>
playbackSpeedFromJson((speed * 20).round() / 20);
/// [semitones] on the slider's half-semitone grid.
double _snapPitch(double semitones) =>
playbackPitchFromJson((semitones * 2).round() / 2);
/// [semitones] in whole semitones.
double _snapPitch(double semitones) => playbackPitchFromJson(semitones);
/// [speed] with its unit, such as `1.25x`, in the locale's number format.
String playbackSpeedLabel(AppLocalizations l10n, double speed) =>
@@ -31,7 +37,7 @@ String playbackSpeedLabel(AppLocalizations l10n, double speed) =>
NumberFormat('0.##', l10n.localeName).format(speed),
);
/// [semitones] with its sign, such as `+2`, `−1.5` (a real minus sign) or
/// [semitones] with its sign, such as `+2`, `−1` (a real minus sign) or
/// `0`, in the locale's number format.
String playbackPitchLabel(AppLocalizations l10n, double semitones) {
final number = NumberFormat('0.#', l10n.localeName).format(semitones.abs());
@@ -140,6 +146,7 @@ class _PlaybackTempoControlsState extends ConsumerState<PlaybackTempoControls> {
);
final quickChoices = SettingsSegmentedChoice<double>(
selected: speed,
enabled: speedEnabled,
semanticLabel: l10n.playbackTempoSpeed,
onChanged: (value) {
setState(() => _speedDraft = null);
@@ -203,7 +210,7 @@ class _PlaybackTempoControlsState extends ConsumerState<PlaybackTempoControls> {
value: speed,
min: minPlaybackSpeed,
max: maxPlaybackSpeed,
divisions: 30,
divisions: _speedDivisions,
label: speedLabel,
semanticLabel: l10n.playbackTempoSpeed,
semanticFormatterCallback: (value) =>
@@ -223,17 +230,7 @@ class _PlaybackTempoControlsState extends ConsumerState<PlaybackTempoControls> {
: null,
),
SizedBox(height: tokens.gapSm),
// The segmented choice has no disabled look of its own.
IgnorePointer(
ignoring: !speedEnabled,
child: ExcludeFocus(
excluding: !speedEnabled,
child: Opacity(
opacity: speedEnabled ? 1 : 0.38,
child: quickChoices,
),
),
),
quickChoices,
],
),
),
@@ -265,7 +262,7 @@ class _PlaybackTempoControlsState extends ConsumerState<PlaybackTempoControls> {
value: pitch,
min: -maxPlaybackPitch,
max: maxPlaybackPitch,
divisions: 48,
divisions: _pitchDivisions,
centered: true,
label: pitchLabel,
semanticLabel: l10n.playbackTempoPitch,
@@ -325,7 +322,8 @@ class _PlaybackTempoControlsState extends ConsumerState<PlaybackTempoControls> {
}
/// The speed and pitch in Now Playing while either differs from normal,
/// beside the audio format; a tap opens [showPlaybackTempoSheet].
/// beside the audio format and in Material's compact player; a tap opens
/// [showPlaybackTempoSheet].
class PlaybackTempoBadge extends ConsumerWidget {
const PlaybackTempoBadge({super.key, required this.colorScheme});
+3
View File
@@ -13,6 +13,7 @@ import 'package:mornye/widgets/audio_quality_badges.dart';
import 'package:mornye/widgets/expressive_icon_button.dart';
import 'package:mornye/widgets/expressive_playback_transport.dart';
import 'package:mornye/widgets/playback_seek_slider.dart';
import 'package:mornye/widgets/playback_tempo_sheet.dart';
/// Material's compact player: idle lyrics shrink the full player to this
/// smaller one (title, timeline and transport, with the times beside the
@@ -140,6 +141,8 @@ class PlayerCompactBar extends ConsumerWidget {
],
),
),
// The full player's format row folds away with it.
PlaybackTempoBadge(colorScheme: colorScheme),
AppIconButton(
tooltip: context.l10n.nowPlayingUpNext,
color: colorScheme.onSurfaceVariant,
+5 -2
View File
@@ -64,12 +64,15 @@ void main() {
final changed = defaults.copyWith(
playbackSpeed: 1.25,
playbackKeepPitch: false,
playbackPitch: -3.5,
playbackPitch: -3,
);
final restored = AppSettings.fromJson(changed.toJson());
expect(restored.playbackSpeed, 1.25);
expect(restored.playbackKeepPitch, isFalse);
expect(restored.playbackPitch, -3.5);
expect(restored.playbackPitch, -3);
// Half semitones from earlier builds restore as whole ones.
expect(AppSettings.fromJson({'playbackPitch': 2.5}).playbackPitch, 3);
expect(AppSettings.fromJson({'playbackPitch': -1.4}).playbackPitch, -1);
expect(restored.copyWith(locale: 'id').playbackSpeed, 1.25);
for (final invalid in ['', double.nan, double.infinity]) {
+18 -7
View File
@@ -125,8 +125,8 @@ void main() {
expect(playbackSpeedLabel(en, 1.25), '1.25x');
expect(playbackSpeedLabel(id, 1.25), '1,25x');
expect(playbackPitchLabel(en, 2), '+2');
expect(playbackPitchLabel(en, -1.5), '−1.5');
expect(playbackPitchLabel(id, -1.5), '−1,5');
expect(playbackPitchLabel(en, -3), '−3');
expect(playbackPitchLabel(id, -12), '−12');
expect(playbackPitchLabel(en, 0), '0');
expect(
playbackTempoSummary(en, speed: 1, keepPitch: true, pitch: 0),
@@ -141,8 +141,8 @@ void main() {
'−1 semitone',
);
expect(
playbackTempoSummary(en, speed: 1, keepPitch: true, pitch: 1.5),
'+1.5 semitones',
playbackTempoSummary(en, speed: 1, keepPitch: true, pitch: 3),
'+3 semitones',
);
expect(
playbackTempoSummary(en, speed: 1, keepPitch: false, pitch: 0),
@@ -174,6 +174,9 @@ void main() {
expect(_value('+2'), findsOneWidget);
expect(_slider(tester, _speedSlider).value, 1.25);
expect(_slider(tester, _pitchSlider).value, 2);
// Few enough stops to read: 0.1x and one semitone apart.
expect(_slider(tester, _speedSlider).divisions, 15);
expect(_slider(tester, _pitchSlider).divisions, 24);
expect(_keepPitch(tester).value, isFalse);
expect(_reset(tester).onPressed, isNotNull);
@@ -206,7 +209,7 @@ void main() {
);
final saved = container.read(settingsProvider).playbackSpeed;
expect(saved, greaterThan(1.4));
expect(saved * 20, closeTo((saved * 20).roundToDouble(), 1e-9));
expect(saved * 10, closeTo((saved * 10).roundToDouble(), 1e-9));
expect(playbackSpeed.value, saved);
expect(_reset(tester).onPressed, isNotNull);
expect(tester.takeException(), isNull);
@@ -232,7 +235,7 @@ void main() {
);
final saved = container.read(settingsProvider).playbackPitch;
expect(saved, lessThan(-5));
expect(saved * 2, closeTo((saved * 2).roundToDouble(), 1e-9));
expect(saved, saved.roundToDouble());
expect(
_value(
playbackPitchLabel(lookupAppLocalizations(const Locale('en')), saved),
@@ -286,7 +289,15 @@ void main() {
expect(_slider(tester, _pitchSlider).onChanged, isNull);
expect(_keepPitch(tester).enabled, isFalse);
expect(_reset(tester).onPressed, isNull);
// The quick choices ignore taps.
// The quick choices show their disabled style and ignore taps.
expect(
tester
.widget<SettingsSegmentedChoice<double>>(
find.byType(SettingsSegmentedChoice<double>),
)
.enabled,
isFalse,
);
await tester.tap(find.text('1.5x').first, warnIfMissed: false);
await tester.pumpAndSettle();
expect(container.read(settingsProvider).playbackSpeed, 1.25);
+72 -41
View File
@@ -3,7 +3,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mornye/l10n/app_localizations.dart';
import 'package:mornye/models/settings.dart';
import 'package:mornye/providers/music_player_provider.dart';
import 'package:mornye/providers/settings_provider.dart';
import 'package:mornye/theme/app_theme.dart';
import 'package:mornye/utils/playback_seek_preview.dart';
import 'package:mornye/widgets/playback_seek_slider.dart';
@@ -23,59 +25,73 @@ class _Player extends MusicPlayerController {
Future<void> previous() async => calls.add('previous');
}
void main() {
testWidgets('the compact player keeps the transport, timeline and queue', (
tester,
) async {
final player = _Player();
final seekPreview = PlaybackSeekPreview();
addTearDown(seekPreview.dispose);
var queueOpened = 0;
final theme = AppTheme.light();
await tester.pumpWidget(
ProviderScope(
overrides: [
playbackPlayingProvider.overrideWith((ref) => true),
playbackLoadingProvider.overrideWith((ref) => false),
playbackPositionProvider.overrideWith(
(ref) => const Duration(seconds: 30),
),
musicPlayerControllerProvider.overrideWithValue(player),
],
child: MaterialApp(
theme: theme,
locale: const Locale('en'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
// The playing timeline keeps moving; the player rests here.
home: TickerMode(
enabled: false,
child: Scaffold(
body: Align(
alignment: Alignment.bottomCenter,
child: PlayerCompactBar(
mediaItem: const MediaItem(
id: 'song',
title: 'Song',
artist: 'Artist',
duration: Duration(minutes: 2),
),
seekPreview: seekPreview,
colorScheme: theme.colorScheme,
onOpenQueue: () => queueOpened++,
Future<void> _pumpBar(
WidgetTester tester, {
required _Player player,
required VoidCallback onOpenQueue,
AppSettings settings = const AppSettings(),
}) async {
final seekPreview = PlaybackSeekPreview();
addTearDown(seekPreview.dispose);
final theme = AppTheme.light();
await tester.pumpWidget(
ProviderScope(
overrides: [
initialSettingsProvider.overrideWithValue(settings),
playbackPlayingProvider.overrideWith((ref) => true),
playbackLoadingProvider.overrideWith((ref) => false),
playbackPositionProvider.overrideWith(
(ref) => const Duration(seconds: 30),
),
musicPlayerControllerProvider.overrideWithValue(player),
],
child: MaterialApp(
theme: theme,
locale: const Locale('en'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
// The playing timeline keeps moving; the player rests here.
home: TickerMode(
enabled: false,
child: Scaffold(
body: Align(
alignment: Alignment.bottomCenter,
child: PlayerCompactBar(
mediaItem: const MediaItem(
id: 'song',
title: 'Song',
artist: 'Artist',
duration: Duration(minutes: 2),
),
seekPreview: seekPreview,
colorScheme: theme.colorScheme,
onOpenQueue: onOpenQueue,
),
),
),
),
),
);
),
);
}
const _tempoBadge = ValueKey('playback-tempo-badge');
void main() {
testWidgets('the compact player keeps the transport, timeline and queue', (
tester,
) async {
final player = _Player();
var queueOpened = 0;
await _pumpBar(tester, player: player, onOpenQueue: () => queueOpened++);
expect(find.text('Song'), findsOneWidget);
expect(find.text('Artist'), findsOneWidget);
expect(find.text('0:30'), findsOneWidget);
expect(find.text('2:00'), findsOneWidget);
expect(find.byType(PlaybackSeekSlider), findsOneWidget);
// Normal speed and pitch show no label.
expect(find.byKey(_tempoBadge), findsNothing);
final l10n = AppLocalizations.of(tester.element(find.text('Song')));
await tester.tap(find.byTooltip(l10n.nowPlayingPreviousTrack));
await tester.tap(find.byTooltip(l10n.actionPause));
@@ -85,4 +101,19 @@ void main() {
expect(queueOpened, 1);
expect(tester.takeException(), isNull);
});
testWidgets('the compact player shows a changed speed and pitch', (
tester,
) async {
await _pumpBar(
tester,
player: _Player(),
onOpenQueue: () {},
settings: const AppSettings(playbackSpeed: 1.25, playbackPitch: -2),
);
expect(find.byKey(_tempoBadge), findsOneWidget);
expect(find.text('1.25x · −2'), findsOneWidget);
expect(tester.takeException(), isNull);
});
}