A cross platform Audiobookshelf client for Android and iOS
Dart
Add per-item EQ and friendly frequency labels
Barnabasba6a973d7268
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex ebec2538..57bf96da 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -1558,6 +1558,7 @@ class AudioPlayerService extends ChangeNotifier {final bookSpeed = await PlayerSettings.getBookSpeed(speedKey);final speed = bookSpeed ?? await PlayerSettings.getDefaultSpeed();await _player!.setSpeed(speed);+ await EqualizerService().switchItem(speedKey);debugPrint('[Player] Starting local playback at ${speed}x');// Re-activate audio session before play so the first playback event// reaches the audio_service iOS plugin with an active session.@@ -1666,6 +1667,7 @@ class AudioPlayerService extends ChangeNotifier {final bookSpeed = await PlayerSettings.getBookSpeed(itemId);final speed = bookSpeed ?? await PlayerSettings.getDefaultSpeed();await _player!.setSpeed(speed);+ await EqualizerService().switchItem(itemId);debugPrint('[Player] Starting cached session playback at ${speed}x');try { (await AudioSession.instance).setActive(true); } catch (_) {}_player!.play();@@ -1858,6 +1860,7 @@ class AudioPlayerService extends ChangeNotifier {final bookSpeed = await PlayerSettings.getBookSpeed(itemId);final speed = bookSpeed ?? await PlayerSettings.getDefaultSpeed();await _player!.setSpeed(speed);+ await EqualizerService().switchItem(itemId);debugPrint('[Player] Starting stream playback at ${speed}x');// Re-activate audio session before play (see local playback comment above)try { (await AudioSession.instance).setActive(true); } catch (_) {}@@ -1954,6 +1957,7 @@ class AudioPlayerService extends ChangeNotifier {final bookSpeed = await PlayerSettings.getBookSpeed(itemId);final speed = bookSpeed ?? await PlayerSettings.getDefaultSpeed();await _player!.setSpeed(speed);+ await EqualizerService().switchItem(itemId);debugPrint('[Player] Transcoded playback starting at ${speed}x');try { (await AudioSession.instance).setActive(true); } catch (_) {}_player!.play();diff --git a/lib/services/equalizer_service.dart b/lib/services/equalizer_service.dartindex d7e90981..ee574080 100644--- a/lib/services/equalizer_service.dart+++ b/lib/services/equalizer_service.dart@@ -24,6 +24,8 @@ class EqualizerService extends ChangeNotifier {double _virtualizer = 0.0; // 0.0–1.0double _loudnessGain = 0.0; // 0.0–1.0bool _mono = false;+ bool _perItem = false;+ String? _currentItemId;// Built-in presets (EQ curve shapes)static const Map<String, List<double>> presets = {@@ -49,6 +51,7 @@ class EqualizerService extends ChangeNotifier {double get virtualizer => _virtualizer;double get loudnessGain => _loudnessGain;bool get mono => _mono;+ bool get perItem => _perItem;/// Initialize — try to connect to platform EQ, fall back to software presets.Future<void> init() async {@@ -242,9 +245,75 @@ class EqualizerService extends ChangeNotifier {} catch (_) {}}+ /// Toggle per-item EQ scoping.+ Future<void> setPerItem(bool value) async {+ _perItem = value;+ await ScopedPrefs.setBool('eq_perItem', value);+ notifyListeners();+ }++ /// Switch EQ to a new item. Saves current settings under the old item,+ /// then loads the new item's settings (or defaults to EQ off).+ Future<void> switchItem(String itemId) async {+ if (!_perItem) {+ _currentItemId = itemId;+ return;+ }+ if (_currentItemId != null && _currentItemId != itemId) {+ await _saveItemSettings(_currentItemId!);+ }+ _currentItemId = itemId;+ await _loadItemSettings(itemId);+ if (_enabled) {+ _applyCurrentSettings();+ } else {+ _resetPlatform();+ }+ notifyListeners();+ }+// ── Persistence ──+ String _itemKey(String key, String itemId) => 'eq_${itemId}_$key';++ Future<void> _loadItemSettings(String itemId) async {+ final hasItemEq = await ScopedPrefs.containsKey(_itemKey('enabled', itemId));+ if (!hasItemEq) {+ _enabled = false;+ _activePreset = 'flat';+ _bassBoost = 0.0;+ _virtualizer = 0.0;+ _loudnessGain = 0.0;+ _mono = false;+ _bandLevels = List.filled(_bandLevels.length, 0.0);+ return;+ }+ _enabled = await ScopedPrefs.getBool(_itemKey('enabled', itemId)) ?? false;+ _activePreset = await ScopedPrefs.getString(_itemKey('preset', itemId)) ?? 'flat';+ _bassBoost = await ScopedPrefs.getDouble(_itemKey('bassBoost', itemId)) ?? 0.0;+ _virtualizer = await ScopedPrefs.getDouble(_itemKey('virtualizer', itemId)) ?? 0.0;+ _loudnessGain = await ScopedPrefs.getDouble(_itemKey('loudnessGain', itemId)) ?? 0.0;+ _mono = await ScopedPrefs.getBool(_itemKey('mono', itemId)) ?? false;+ final bandStr = await ScopedPrefs.getString(_itemKey('bands', itemId));+ if (bandStr != null) {+ _bandLevels = bandStr.split(',').map((s) => double.tryParse(s) ?? 0.0).toList();+ } else {+ _bandLevels = List.filled(_bandLevels.length, 0.0);+ }+ }++ Future<void> _saveItemSettings(String itemId) async {+ await ScopedPrefs.setBool(_itemKey('enabled', itemId), _enabled);+ await ScopedPrefs.setString(_itemKey('preset', itemId), _activePreset);+ await ScopedPrefs.setDouble(_itemKey('bassBoost', itemId), _bassBoost);+ await ScopedPrefs.setDouble(_itemKey('virtualizer', itemId), _virtualizer);+ await ScopedPrefs.setDouble(_itemKey('loudnessGain', itemId), _loudnessGain);+ await ScopedPrefs.setBool(_itemKey('mono', itemId), _mono);+ await ScopedPrefs.setString(_itemKey('bands', itemId), _bandLevels.map((l) => l.toStringAsFixed(1)).join(','));+ }+Future<void> _loadSettings() async {+ _perItem = await ScopedPrefs.getBool('eq_perItem') ?? false;_enabled = await ScopedPrefs.getBool('eq_enabled') ?? false;_activePreset = await ScopedPrefs.getString('eq_preset') ?? 'flat';_bassBoost = await ScopedPrefs.getDouble('eq_bassBoost') ?? 0.0;@@ -268,6 +337,9 @@ class EqualizerService extends ChangeNotifier {await ScopedPrefs.setDouble('eq_loudnessGain', _loudnessGain);await ScopedPrefs.setBool('eq_mono', _mono);await ScopedPrefs.setString('eq_bands', _bandLevels.map((l) => l.toStringAsFixed(1)).join(','));+ if (_perItem && _currentItemId != null) {+ await _saveItemSettings(_currentItemId!);+ }}/// Formatted frequency label.@@ -275,4 +347,12 @@ class EqualizerService extends ChangeNotifier {if (hz >= 1000) return '${(hz / 1000).toStringAsFixed(hz % 1000 == 0 ? 0 : 1)}k';return '${hz}Hz';}++ String freqName(int hz) {+ if (hz <= 100) return 'Sub';+ if (hz <= 400) return 'Bass';+ if (hz <= 1500) return 'Mids';+ if (hz <= 6000) return 'High';+ return 'Air';+ }}diff --git a/lib/widgets/equalizer_sheet.dart b/lib/widgets/equalizer_sheet.dartindex 5d14bf2f..b486a53a 100644--- a/lib/widgets/equalizer_sheet.dart+++ b/lib/widgets/equalizer_sheet.dart@@ -244,6 +244,37 @@ class _EqualizerSheetContentState extends State<_EqualizerSheetContent> {),),+ const SizedBox(height: 8),++ // Per-item EQ toggle+ Container(+ padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 6),+ decoration: BoxDecoration(+ color: cs.onSurface.withValues(alpha: 0.04),+ borderRadius: BorderRadius.circular(14),+ ),+ child: Row(+ children: [+ Icon(Icons.library_music_rounded, size: 18,+ color: _eq.perItem ? accent.withValues(alpha: 0.7) : cs.onSurface.withValues(alpha: 0.2)),+ const SizedBox(width: 10),+ Expanded(+ child: Text('Per-book EQ', style: TextStyle(+ color: _eq.perItem ? cs.onSurface.withValues(alpha: 0.7) : cs.onSurface.withValues(alpha: 0.24),+ fontSize: 12, fontWeight: FontWeight.w500)),+ ),+ Transform.scale(+ scale: 0.8,+ child: Switch(+ value: _eq.perItem,+ activeTrackColor: accent,+ onChanged: (v) => _eq.setPerItem(v),+ ),+ ),+ ],+ ),+ ),+const SizedBox(height: 20),// Reset button@@ -326,6 +357,9 @@ class _EQBandSlider extends StatelessWidget {Text(label, style: TextStyle(color: enabled ? cs.onSurfaceVariant : cs.onSurface.withValues(alpha: 0.15),fontSize: 10, fontWeight: FontWeight.w500)),+ Text(eq.freqName(frequency), style: TextStyle(+ color: enabled ? cs.onSurfaceVariant.withValues(alpha: 0.5) : cs.onSurface.withValues(alpha: 0.1),+ fontSize: 9, fontWeight: FontWeight.w400)),],);}