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
2 changes: 2 additions & 0 deletions osu.Game.Rulesets.Mania/Objects/Drawables/DrawableHoldNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ private void load()
bodyPiece.CreateProxy(),
tailContainer.CreateProxy(),
});

slidingSample.BindAdjustments(Samples);
}

protected override void LoadComplete()
Expand Down
2 changes: 2 additions & 0 deletions osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSlider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ private void load()
foreach (var drawableHitObject in NestedHitObjects)
drawableHitObject.AccentColour.Value = colour.NewValue;
}, true);

slidingSample.BindAdjustments(Samples);
}

protected override JudgementResult CreateResult(Judgement judgement) => new OsuSliderJudgementResult(HitObject, judgement);
Expand Down
3 changes: 3 additions & 0 deletions osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSpinner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ private void load()
});

PositionBindable.BindValueChanged(pos => Position = pos.NewValue);

spinningSample.BindAdjustments(Samples);
maxBonusSample.BindAdjustments(Samples);
}

protected override void LoadComplete()
Expand Down
65 changes: 7 additions & 58 deletions osu.Game/Audio/AudioNormalization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Linq;
using ManagedBass;
using ManagedBass.Fx;
using osu.Framework.Audio.Mixing;
using osu.Framework.Audio.Track;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Database;
Expand All @@ -14,9 +11,9 @@
namespace osu.Game.Audio
{
/// <summary>
/// Audio Normalization Manager
/// Audio Normalization Data
/// </summary>
public class AudioNormalization : EmbeddedObject, IAudioNormalization, IEquatable<AudioNormalization>
public class AudioNormalization : EmbeddedObject, IEquatable<AudioNormalization>
{
/// <summary>
/// The target level for audio normalization
Expand Down Expand Up @@ -57,9 +54,9 @@ public AudioNormalization(BeatmapInfo beatmapInfo, BeatmapSetInfo beatmapSetInfo
return;
}

filepath = realmFileStore.Storage.GetFullPath(filepath);
using TrackLoudness loudness = new TrackLoudness(realmFileStore.Storage.GetStream(filepath));

float? integratedLoudness = new BassAudioNormalization(filepath).IntegratedLoudness;
float? integratedLoudness = loudness.GetIntegratedLoudness();

if (integratedLoudness == null)
{
Expand Down Expand Up @@ -93,57 +90,9 @@ public void PopulateSet(BeatmapInfo beatmapInfo, BeatmapSetInfo beatmapSetInfo)
}

/// <summary>
/// Convert integrated loudness to a volume offset
/// A volume offset that can be applied upon audio to reach <see cref="TARGET_LEVEL"/> (converted from integrated loudness).
/// </summary>
/// <param name="integratedLoudness">The integrated loudness value</param>
/// <returns>The volume offset needed to reach <see cref="TARGET_LEVEL"/></returns>
public static float IntegratedLoudnessToVolumeOffset(float integratedLoudness) => (float)Math.Pow(10, (TARGET_LEVEL - integratedLoudness) / 20);

/// <summary>
/// Get the loudness values of a <see cref="BeatmapInfo"/> and apply the audio normalization effect to an <see cref="IAudioMixer"/>
/// </summary>
/// <param name="beatmapInfo">The <see cref="BeatmapInfo"/> to get the loudness values of</param>
/// <param name="mixer">The <see cref="IAudioMixer"/> to apply the effect on</param>
public static void AddAudioNormalization(BeatmapInfo beatmapInfo, IAudioMixer mixer)
{
AudioNormalization? audioNormalizationModule = beatmapInfo.AudioNormalization;

VolumeParameters volumeParameters = new VolumeParameters
{
fTarget = audioNormalizationModule?.IntegratedLoudness != null ? IntegratedLoudnessToVolumeOffset((float)audioNormalizationModule.IntegratedLoudness) : 0.8f,
fCurrent = -1.0f,
fTime = 0.3f,
lCurve = 1,
lChannel = FXChannelFlags.All
};

addFx(volumeParameters, mixer);

Logger.Log("Normalization Status: " + (audioNormalizationModule?.IntegratedLoudness != null ? $"on ({Math.Round(IntegratedLoudnessToVolumeOffset((float)audioNormalizationModule.IntegratedLoudness) * 100)}%)" : "off"));
Comment thread
hwsmm marked this conversation as resolved.
}

/// <summary>
/// Add an effect to a <see cref="IAudioMixer"/>, replacing it if it already exists
/// </summary>
/// <param name="effectParameter">The <see cref="IEffectParameter"/> to add to the <paramref name="mixer"/></param>
/// <param name="mixer">The <see cref="IAudioMixer"/> to add the effect to</param>
private static void addFx(IEffectParameter effectParameter, IAudioMixer mixer)
{
IEffectParameter? effect = mixer.Effects.SingleOrDefault(e => e.FXType == effectParameter.FXType);

if (effect != null)
{
int i = mixer.Effects.IndexOf(effect);
mixer.Effects[i] = effectParameter;
}
else
{
mixer.Effects.Add(effectParameter);
}
}

/// <inheritdoc />
public bool Equals(IAudioNormalization? other) => other is AudioNormalization audioNormalization && Equals(audioNormalization);
public double? IntegratedLoudnessInVolumeOffset => IntegratedLoudness != null ? TrackLoudness.ConvertToVolumeOffset(TARGET_LEVEL, (float)IntegratedLoudness) : null;

/// <inheritdoc />
public bool Equals(AudioNormalization? other) => other?.IntegratedLoudness != null && IntegratedLoudness != null && Math.Abs((float)(IntegratedLoudness - other.IntegratedLoudness)) < 0.0000001;
Expand Down
65 changes: 65 additions & 0 deletions osu.Game/Audio/AudioNormalizationManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Configuration;

namespace osu.Game.Audio
{
/// <summary>
/// Manages audio normalization.
/// </summary>
public class AudioNormalizationManager
{
/// <summary>
/// Fallback volume for tracks that <see cref="TrackLoudness"/> failed to measure loudness.
/// </summary>
public const double FALLBACK_VOLUME = 0.8;

private readonly Bindable<bool> audioNormalizationSetting;

/// <summary>
/// Samples assigned to hitobjects needs to bind to this bindable to normalize their volume in line with track.
/// </summary>
public readonly BindableDouble SampleNormalizeVolume = new BindableDouble(1.0);

/// <summary>
/// Creates a new <see cref="AudioNormalizationManager"/>.
/// </summary>
/// <param name="config">An osu config to get beatmap hitsounds bindable from.</param>
/// <param name="beatmap">A bindable for beatmap.</param>
public AudioNormalizationManager(OsuConfigManager config, IBindable<WorkingBeatmap> beatmap)
{
audioNormalizationSetting = config.GetBindable<bool>(OsuSetting.AudioNormalization);

updateNormalization(audioNormalizationSetting.Value, beatmap.Value);
audioNormalizationSetting.BindValueChanged(change => updateNormalization(change.NewValue, beatmap.Value));
beatmap.BindValueChanged(ev => onBeatmapChanged(ev.OldValue, ev.NewValue));
}

private void updateNormalization(bool value, WorkingBeatmap current)
{
if (value)
{
SampleNormalizeVolume.BindTo(current.TrackNormalizeVolume);
current.EnableTrackNormlization();
Logger.Log($"Normalization value: {(int)(current.TrackNormalizeVolume.Value * 100)}%");
}
else
{
SampleNormalizeVolume.UnbindFrom(current.TrackNormalizeVolume);
SampleNormalizeVolume.Value = 1.0;
current.DisableTrackNormalization();
}
}

private void onBeatmapChanged(WorkingBeatmap oldBeatmap, WorkingBeatmap newBeatmap)
{
SampleNormalizeVolume.UnbindFrom(oldBeatmap.TrackNormalizeVolume);
updateNormalization(audioNormalizationSetting.Value, newBeatmap);
}
}
}
73 changes: 0 additions & 73 deletions osu.Game/Audio/BassAudioNormalization.cs

This file was deleted.

12 changes: 0 additions & 12 deletions osu.Game/Audio/IAudioNormalization.cs

This file was deleted.

13 changes: 13 additions & 0 deletions osu.Game/Beatmaps/WorkingBeatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
using JetBrains.Annotations;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics.Textures;
using osu.Framework.Logging;
using osu.Game.Audio;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.UI;
Expand Down Expand Up @@ -111,9 +113,20 @@ public Track LoadTrack()
waveform?.Dispose();
waveform = null;

track.AddAdjustment(AdjustableProperty.Volume, TrackNormalizeVolume);

return track;
}

// Normalization is here because it's not so good to apply normalization in global state through MusicController or TrackStore.
// Each beatmap has its own track and its own track loudness value.
public readonly BindableDouble TrackNormalizeVolume = new BindableDouble(AudioNormalizationManager.FALLBACK_VOLUME);

public void EnableTrackNormlization()
=> TrackNormalizeVolume.Value = BeatmapInfo.AudioNormalization?.IntegratedLoudnessInVolumeOffset ?? AudioNormalizationManager.FALLBACK_VOLUME;

public void DisableTrackNormalization() => TrackNormalizeVolume.Value = AudioNormalizationManager.FALLBACK_VOLUME;

public void PrepareTrackForPreview(bool looping, double offsetFromPreviewPoint = 0)
{
Track.Looping = looping;
Expand Down
3 changes: 3 additions & 0 deletions osu.Game/Configuration/OsuConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ protected override void InitialiseDefaults()

SetDefault(OsuSetting.AudioOffset, 0, -500.0, 500.0, 1);

SetDefault(OsuSetting.AudioNormalization, true);

// Input
SetDefault(OsuSetting.MenuCursorSize, 1.0f, 0.5f, 2f, 0.01f);
SetDefault(OsuSetting.GameplayCursorSize, 1.0f, 0.1f, 2f, 0.01f);
Expand Down Expand Up @@ -360,6 +362,7 @@ public enum OsuSetting
/// This is added to the audio track's current time. Higher values will cause gameplay to occur earlier, relative to the audio track.
/// </summary>
AudioOffset,
AudioNormalization,

VolumeInactive,
MenuMusic,
Expand Down
5 changes: 5 additions & 0 deletions osu.Game/Localisation/AudioSettingsStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public static class AudioSettingsStrings
/// </summary>
public static LocalisableString AudioOffset => new TranslatableString(getKey(@"audio_offset"), @"Audio offset");

/// <summary>
/// "Normalize Audio"
/// </summary>
public static LocalisableString AudioNormalization => new TranslatableString(getKey(@"audio_normalization"), @"Normalize Audio");

/// <summary>
/// "Play a few beatmaps to receive a suggested offset!"
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions osu.Game/OsuGameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ public virtual string Version

protected SafeAreaContainer SafeAreaContainer { get; private set; }

protected AudioNormalizationManager AudioNormalizationManager { get; private set; }

/// <summary>
/// For now, this is used as a source specifically for beat synced components.
/// Going forward, it could potentially be used as the single source-of-truth for beatmap timing.
Expand Down Expand Up @@ -365,6 +367,8 @@ private void load(ReadableKeyCombinationProvider keyCombinationProvider, Framewo
dependencies.Cache(previewTrackManager = new PreviewTrackManager(BeatmapManager.BeatmapTrackStore));
base.Content.Add(previewTrackManager);

dependencies.Cache(AudioNormalizationManager = new AudioNormalizationManager(LocalConfig, Beatmap));

base.Content.Add(MusicController = new MusicController());
dependencies.CacheAs(MusicController);

Expand Down
10 changes: 1 addition & 9 deletions osu.Game/Overlays/MusicController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging;
using osu.Framework.Threading;
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Rulesets.Mods;
Expand Down Expand Up @@ -64,18 +63,11 @@ public partial class MusicController : CompositeDrawable
[Resolved]
private RealmAccess realm { get; set; }

[Resolved]
private AudioManager audioManager { get; set; }

protected override void LoadComplete()
{
base.LoadComplete();

beatmap.BindValueChanged(b =>
{
changeBeatmap(b.NewValue);
AudioNormalization.AddAudioNormalization(b.NewValue.BeatmapInfo, audioManager.TrackMixer);
}, true);
beatmap.BindValueChanged(b => changeBeatmap(b.NewValue), true);
mods.BindValueChanged(_ => ResetTrackAdjustments(), true);
}

Expand Down
5 changes: 5 additions & 0 deletions osu.Game/Overlays/Settings/Sections/Audio/VolumeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ private void load(AudioManager audio, OsuConfigManager config)
KeyboardStep = 0.01f,
DisplayAsPercentage = true
},
new SettingsCheckbox
{
LabelText = AudioSettingsStrings.AudioNormalization,
Current = config.GetBindable<bool>(OsuSetting.AudioNormalization)
}
};
}

Expand Down
Loading