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
4using System;
5using osu.Framework.Bindables;
6
7namespace osu.Framework.Audio.Sample
8{
9 public abstract class Sample : AudioCollectionManager<SampleChannel>, ISample
10 {
11 public const int DEFAULT_CONCURRENCY = 2;
12
13 public double Length { get; protected set; }
14
15 public Bindable<int> PlaybackConcurrency { get; } = new Bindable<int>(DEFAULT_CONCURRENCY);
16
17 internal Action<Sample> OnPlay;
18
19 public SampleChannel Play()
20 {
21 var channel = GetChannel();
22 channel.Play();
23 return channel;
24 }
25
26 public SampleChannel GetChannel()
27 {
28 if (IsDisposed)
29 throw new ObjectDisposedException(ToString(), "Can not get a channel from a disposed sample.");
30
31 var channel = CreateChannel();
32
33 if (channel != null)
34 channel.OnPlay = onPlay;
35
36 return channel;
37 }
38
39 private void onPlay(SampleChannel channel)
40 {
41 AddItem(channel);
42 OnPlay?.Invoke(this);
43 }
44
45 public override bool IsAlive => base.IsAlive && (!PendingActions.IsEmpty || Items.Count > 0);
46
47 /// <summary>
48 /// Creates a unique playback of this <see cref="Sample"/>.
49 /// </summary>
50 /// <returns>The <see cref="SampleChannel"/> for the playback.</returns>
51 protected abstract SampleChannel CreateChannel();
52 }
53}