Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose audio mixers to mods #16771

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
8 changes: 5 additions & 3 deletions osu.Game/Audio/Effects/AudioFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AudioFilter : Component, ITransformableFilter
/// </summary>
public const int MAX_LOWPASS_CUTOFF = 22049; // nyquist - 1hz

private readonly AudioMixer mixer;
private readonly IAudioMixer mixer;
private readonly BQFParameters filter;
private readonly BQFType type;

Expand Down Expand Up @@ -45,7 +45,7 @@ public int Cutoff
/// </summary>
/// <param name="mixer">The mixer this effect should be applied to.</param>
/// <param name="type">The type of filter (e.g. LowPass, HighPass, etc)</param>
public AudioFilter(AudioMixer mixer, BQFType type = BQFType.LowPass)
public AudioFilter(IAudioMixer mixer, BQFType type = BQFType.LowPass)
{
this.mixer = mixer;
this.type = type;
Expand Down Expand Up @@ -131,7 +131,9 @@ private void ensureDetached()
if (!isAttached)
return;

Debug.Assert(mixer.Effects.Contains(filter));
// Do not assert for the existence of the filter in mixer
// There are cases where effects are cleared from the mixer's side,
// causing the filter to still think it's attached when it isn't
mixer.Effects.Remove(filter);
isAttached = false;
}
Expand Down
15 changes: 14 additions & 1 deletion osu.Game/Overlays/MusicController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Mixing;
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
Expand Down Expand Up @@ -55,6 +56,15 @@ public class MusicController : CompositeDrawable
[Resolved]
private RealmAccess realm { get; set; }

private readonly DrawableAudioMixer drawableAudioMixer;

public IAudioMixer AudioMixer => drawableAudioMixer;
frenzibyte marked this conversation as resolved.
Show resolved Hide resolved

public MusicController()
{
AddInternal(drawableAudioMixer = new DrawableAudioMixer { Name = $"{nameof(MusicController)} {nameof(DrawableAudioMixer)}" });
}

[BackgroundDependencyLoader]
private void load()
{
Expand Down Expand Up @@ -328,7 +338,7 @@ private void changeTrack()

if (queuedTrack == CurrentTrack)
{
AddInternal(queuedTrack);
drawableAudioMixer.Add(queuedTrack);
queuedTrack.VolumeTo(0).Then().VolumeTo(1, 300, Easing.Out);
}
else
Expand Down Expand Up @@ -389,11 +399,14 @@ public void ResetTrackAdjustments()
CurrentTrack.RemoveAllAdjustments(AdjustableProperty.Frequency);
CurrentTrack.RemoveAllAdjustments(AdjustableProperty.Tempo);
CurrentTrack.RemoveAllAdjustments(AdjustableProperty.Volume);
drawableAudioMixer.Effects.Clear();

if (allowTrackAdjustments)
{
foreach (var mod in mods.Value.OfType<IApplicableToTrack>())
mod.ApplyToTrack(CurrentTrack);
foreach (var mod in mods.Value.OfType<IApplicableToTrackMixer>())
mod.ApplyToTrackMixer(drawableAudioMixer);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions osu.Game/Rulesets/Mods/IApplicableToTrackMixer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.Mixing;

namespace osu.Game.Rulesets.Mods
{
/// <summary>
/// An interface for mods that add effects to the track mixer.
/// </summary>
public interface IApplicableToTrackMixer : IApplicableMod
{
void ApplyToTrackMixer(IAudioMixer mixer);
}
}
2 changes: 2 additions & 0 deletions osu.Game/Screens/Play/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,8 @@ public override void OnEntering(IScreen last)
musicController.ResetTrackAdjustments();
foreach (var mod in GameplayState.Mods.OfType<IApplicableToTrack>())
mod.ApplyToTrack(musicController.CurrentTrack);
foreach (var mod in GameplayState.Mods.OfType<IApplicableToTrackMixer>())
mod.ApplyToTrackMixer(musicController.AudioMixer);

updateGameplayState();

Expand Down