Skip to content

Commit

Permalink
feat: configurable audio normalization switch
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Sep 16, 2023
1 parent 126d601 commit c325911
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -264,5 +264,6 @@
"use_system_title_bar": "Use system title bar",
"crunching_results": "Crunching results...",
"search_to_get_results": "Search to get results",
"use_amoled_dark_theme": "Use AMOLED (Pitch Black) dark theme"
"use_amoled_dark_theme": "Use AMOLED (Pitch Black) dark theme",
"normalize_audio": "Normalize audio"
}
7 changes: 7 additions & 0 deletions lib/pages/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,13 @@ class SettingsPage extends HookConsumerWidget {
},
trailing: const Icon(SpotubeIcons.angleRight),
),
SwitchListTile(
secondary: const Icon(SpotubeIcons.playlistRemove),
title: Text(context.l10n.normalize_audio),
subtitle: Text(context.l10n.blacklist_description),
value: preferences.normalizeAudio,
onChanged: preferences.setNormalizeAudio,
),
],
),
SectionCardWithHeading(
Expand Down
15 changes: 15 additions & 0 deletions lib/provider/user_preferences_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:spotube/components/settings/color_scheme_picker_dialog.dart';
import 'package:spotube/models/matched_track.dart';
import 'package:spotube/provider/palette_provider.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
import 'package:spotube/services/audio_player/audio_player.dart';

import 'package:spotube/utils/persisted_change_notifier.dart';
import 'package:spotube/utils/platform.dart';
Expand Down Expand Up @@ -70,6 +71,8 @@ class UserPreferences extends PersistedChangeNotifier {

bool amoledDarkTheme;

bool normalizeAudio;

final Ref ref;

UserPreferences(
Expand All @@ -92,6 +95,7 @@ class UserPreferences extends PersistedChangeNotifier {
this.youtubeApiType = YoutubeApiType.youtube,
this.systemTitleBar = false,
this.amoledDarkTheme = false,
this.normalizeAudio = true,
}) : super() {
if (downloadLocation.isEmpty && !kIsWeb) {
_getDefaultDownloadDirectory().then(
Expand Down Expand Up @@ -219,6 +223,13 @@ class UserPreferences extends PersistedChangeNotifier {
updatePersistence();
}

void setNormalizeAudio(bool normalize) {
normalizeAudio = normalize;
audioPlayer.setAudioNormalization(normalize);
notifyListeners();
updatePersistence();
}

Future<String> _getDefaultDownloadDirectory() async {
if (kIsAndroid) return "/storage/emulated/0/Download/Spotube";

Expand Down Expand Up @@ -285,6 +296,9 @@ class UserPreferences extends PersistedChangeNotifier {
setSystemTitleBar(systemTitleBar);

amoledDarkTheme = map["amoledDarkTheme"] ?? amoledDarkTheme;

normalizeAudio = map["normalizeAudio"] ?? normalizeAudio;
audioPlayer.setAudioNormalization(normalizeAudio);
}

@override
Expand All @@ -309,6 +323,7 @@ class UserPreferences extends PersistedChangeNotifier {
"youtubeApiType": youtubeApiType.name,
'systemTitleBar': systemTitleBar,
"amoledDarkTheme": amoledDarkTheme,
"normalizeAudio": normalizeAudio,
};
}

Expand Down
4 changes: 0 additions & 4 deletions lib/services/audio_player/audio_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ abstract class AudioPlayerInterface {
)
// _justAudio = !_mkSupportedPlatform ? ja.AudioPlayer() : null
{
//? Normalizing the audio
(_mkPlayer.platform as mk.NativePlayer)
.setProperty('af', 'dynaudnorm=g=5:f=250:r=0.9:p=0.5');

_mkPlayer.stream.error.listen((event) {
Catcher.reportCheckedError(event, StackTrace.current);
});
Expand Down
4 changes: 4 additions & 0 deletions lib/services/audio_player/audio_player_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,8 @@ class SpotubeAudioPlayer extends AudioPlayerInterface
// await _justAudio!.setLoopMode(loop.toLoopMode());
// }
}

Future<void> setAudioNormalization(bool normalize) async {
await _mkPlayer.setAudioNormalization(normalize);
}
}
10 changes: 10 additions & 0 deletions lib/services/audio_player/mk_state_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,14 @@ class MkPlayerWithState extends Player {
index: newMedias.indexOf(_playlist!.medias[_playlist!.index]),
);
}

NativePlayer get nativePlayer => platform as NativePlayer;

Future<void> setAudioNormalization(bool normalize) async {
if (normalize) {
await nativePlayer.setProperty('af', 'dynaudnorm=g=5:f=250:r=0.9:p=0.5');
} else {
await nativePlayer.setProperty('af', '');
}
}
}
33 changes: 22 additions & 11 deletions untranslated_messages.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,56 @@
{
"bn": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
],

"ca": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
],

"de": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
],

"es": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
],

"fr": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
],

"hi": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
],

"ja": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
],

"pl": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
],

"pt": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
],

"ru": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
],

"zh": [
"use_amoled_dark_theme"
"use_amoled_dark_theme",
"normalize_audio"
]
}

0 comments on commit c325911

Please sign in to comment.