A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4#nullable enable
5
6using System;
7using System.Threading.Tasks;
8using osu.Framework.Audio.Mixing;
9using osu.Framework.Statistics;
10using osu.Framework.Audio.Track;
11
12namespace osu.Framework.Audio.Sample
13{
14 public abstract class SampleChannel : AdjustableAudioComponent, ISampleChannel, IAudioChannel
15 {
16 internal Action<SampleChannel>? OnPlay;
17
18 public virtual void Play()
19 {
20 if (IsDisposed)
21 throw new ObjectDisposedException(ToString(), "Can not play disposed sample channels.");
22
23 Played = true;
24 OnPlay?.Invoke(this);
25 }
26
27 public virtual void Stop()
28 {
29 }
30
31 protected override void UpdateState()
32 {
33 FrameStatistics.Increment(StatisticsCounterType.SChannels);
34 base.UpdateState();
35 }
36
37 public bool Played { get; private set; }
38
39 public abstract bool Playing { get; }
40
41 public virtual bool Looping { get; set; }
42
43 public override bool IsAlive => base.IsAlive && Playing;
44
45 public virtual ChannelAmplitudes CurrentAmplitudes { get; } = ChannelAmplitudes.Empty;
46
47 #region Mixing
48
49 protected virtual AudioMixer? Mixer { get; set; }
50
51 AudioMixer? IAudioChannel.Mixer
52 {
53 get => Mixer;
54 set => Mixer = value;
55 }
56
57 Task IAudioChannel.EnqueueAction(Action action) => EnqueueAction(action);
58
59 #endregion
60
61 protected override void Dispose(bool disposing)
62 {
63 if (!IsDisposed)
64 Stop();
65
66 base.Dispose(disposing);
67 }
68 }
69}