-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathAudioMixer.cs
97 lines (77 loc) · 3.53 KB
/
AudioMixer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using ManagedBass;
using osu.Framework.Extensions.ObjectExtensions;
namespace osu.Framework.Audio.Mixing
{
/// <summary>
/// Mixes together multiple <see cref="IAudioChannel"/>s into one output.
/// </summary>
public abstract class AudioMixer : AdjustableAudioComponent, IAudioMixer
{
public readonly string Identifier;
private readonly AudioMixer? fallbackMixer;
/// <summary>
/// Creates a new <see cref="AudioMixer"/>.
/// </summary>
/// <param name="fallbackMixer">A fallback <see cref="AudioMixer"/>, which <see cref="IAudioChannel"/>s are moved to if removed from this one.
/// A <c>null</c> value indicates this is the fallback <see cref="AudioMixer"/>.</param>
/// <param name="identifier">An identifier displayed on the audio mixer visualiser.</param>
protected AudioMixer(AudioMixer? fallbackMixer, string identifier)
{
this.fallbackMixer = fallbackMixer;
Identifier = identifier;
}
public void Add(IAudioChannel channel)
{
channel.EnqueueAction(() =>
{
if (channel.Mixer == this)
return;
// Ensure the channel is removed from its current mixer.
channel.Mixer?.Remove(channel, false);
if (channel is IAdjustableAudioComponent adj)
adj.BindAdjustments(this);
AddInternal(channel);
channel.Mixer = this;
});
}
public void Remove(IAudioChannel channel) => Remove(channel, true);
public abstract void AddEffect(IEffectParameter effect, int priority = 0);
public abstract void RemoveEffect(IEffectParameter effect);
public abstract void UpdateEffect(IEffectParameter effect);
/// <summary>
/// Removes an <see cref="IAudioChannel"/> from the mix.
/// </summary>
/// <param name="channel">The <see cref="IAudioChannel"/> to remove.</param>
/// <param name="returnToDefault">Whether <paramref name="channel"/> should be returned to the default mixer.</param>
protected void Remove(IAudioChannel channel, bool returnToDefault)
{
// If this is the default mixer, prevent removal.
if (returnToDefault && fallbackMixer == null)
return;
channel.EnqueueAction(() =>
{
if (channel.Mixer != this)
return;
RemoveInternal(channel);
channel.Mixer = null;
if (channel is IAdjustableAudioComponent adj)
adj.UnbindAdjustments(this);
// Add the channel back to the default mixer so audio can always be played.
if (returnToDefault)
fallbackMixer.AsNonNull().Add(channel);
});
}
/// <summary>
/// Adds an <see cref="IAudioChannel"/> to the mix.
/// </summary>
/// <param name="channel">The <see cref="IAudioChannel"/> to add.</param>
protected abstract void AddInternal(IAudioChannel channel);
/// <summary>
/// Removes an <see cref="IAudioChannel"/> from the mix.
/// </summary>
/// <param name="channel">The <see cref="IAudioChannel"/> to remove.</param>
protected abstract void RemoveInternal(IAudioChannel channel);
}
}