Skip to content

Commit

Permalink
Add Name to samples and channels
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Aug 30, 2022
1 parent a961c2e commit 32e7571
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 13 deletions.
2 changes: 1 addition & 1 deletion osu.Framework.Tests/Audio/SampleChannelVirtualTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SampleChannelVirtualTest
[SetUp]
public void Setup()
{
sample = new SampleVirtual();
sample = new SampleVirtual("virtual");
updateSample();
}

Expand Down
4 changes: 2 additions & 2 deletions osu.Framework.Tests/Audio/TrackVirtualTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TrackVirtualTest
[SetUp]
public void Setup()
{
track = new TrackVirtual(60000);
track = new TrackVirtual(60000, "virtual");
updateTrack();
}

Expand All @@ -45,7 +45,7 @@ public void TestStart()
public void TestStartZeroLength()
{
// override default with custom length
track = new TrackVirtual(0);
track = new TrackVirtual(0, "virtual");

track.Start();
updateTrack();
Expand Down
5 changes: 5 additions & 0 deletions osu.Framework/Audio/Mixing/IAudioChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace osu.Framework.Audio.Mixing
/// </summary>
public interface IAudioChannel
{
/// <summary>
/// A name identifying this sample internally.
/// </summary>
string Name { get; }

/// <summary>
/// The mixer in which all audio produced by this channel should be routed into.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions osu.Framework/Audio/Sample/ISample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace osu.Framework.Audio.Sample
/// </summary>
public interface ISample : IAdjustableAudioComponent
{
/// <summary>
/// A name identifying this sample internally.
/// </summary>
string Name { get; }

/// <summary>
/// The length in milliseconds of this <see cref="ISample"/>.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions osu.Framework/Audio/Sample/ISampleChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace osu.Framework.Audio.Sample
/// </summary>
public interface ISampleChannel : IHasAmplitudes
{
/// <summary>
/// A name identifying this sample internally.
/// </summary>
string Name { get; }

/// <summary>
/// Starts or resumes playback. Has no effect if this <see cref="ISampleChannel"/> is already playing.
/// </summary>
Expand Down
8 changes: 7 additions & 1 deletion osu.Framework/Audio/Sample/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ public abstract class Sample : AudioCollectionManager<SampleChannel>, ISample
{
public const int DEFAULT_CONCURRENCY = 2;

public double Length { get; protected set; }
public string Name { get; }

protected Sample(string name)
{
Name = name;
}

public double Length { get; protected set; }
public Bindable<int> PlaybackConcurrency { get; } = new Bindable<int>(DEFAULT_CONCURRENCY);

internal Action<Sample> OnPlay;
Expand Down
1 change: 1 addition & 0 deletions osu.Framework/Audio/Sample/SampleBass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal sealed class SampleBass : Sample
private readonly BassAudioMixer mixer;

internal SampleBass(SampleBassFactory factory, BassAudioMixer mixer)
: base(factory.Name)
{
this.factory = factory;
this.mixer = mixer;
Expand Down
9 changes: 8 additions & 1 deletion osu.Framework/Audio/Sample/SampleBassFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ namespace osu.Framework.Audio.Sample
/// </summary>
internal class SampleBassFactory : AudioCollectionManager<AdjustableAudioComponent>
{
/// <summary>
/// A name identifying the sample to be created by this factory.
/// </summary>
public string Name { get; }

public int SampleId { get; private set; }

public override bool IsLoaded => SampleId != 0;
Expand All @@ -32,11 +37,13 @@ internal class SampleBassFactory : AudioCollectionManager<AdjustableAudioCompone
private NativeMemoryTracker.NativeMemoryLease? memoryLease;
private byte[]? data;

public SampleBassFactory(byte[] data, BassAudioMixer mixer)
public SampleBassFactory(byte[] data, string name, BassAudioMixer mixer)
{
this.data = data;
this.mixer = mixer;

Name = name;

EnqueueAction(loadSample);

PlaybackConcurrency.BindValueChanged(updatePlaybackConcurrency);
Expand Down
7 changes: 7 additions & 0 deletions osu.Framework/Audio/Sample/SampleChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public abstract class SampleChannel : AdjustableAudioComponent, ISampleChannel,
{
internal Action<SampleChannel>? OnPlay;

public string Name { get; }

protected SampleChannel(string name)
{
Name = name;
}

public virtual void Play()
{
if (IsDisposed)
Expand Down
1 change: 1 addition & 0 deletions osu.Framework/Audio/Sample/SampleChannelBass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public override bool Looping
/// </summary>
/// <param name="sample">The <see cref="SampleBass"/> to create the channel from.</param>
public SampleChannelBass(SampleBass sample)
: base(sample.Name)
{
this.sample = sample;

Expand Down
5 changes: 5 additions & 0 deletions osu.Framework/Audio/Sample/SampleChannelVirtual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ internal class SampleChannelVirtual : SampleChannel

public override bool Playing => playing;

public SampleChannelVirtual(string name)
: base(name)
{
}

protected override void UpdateState()
{
base.UpdateState();
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Audio/Sample/SampleStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Sample Get(string name)
this.LogIfNonBackgroundThread(name);

byte[] data = store.Get(name);
factory = factories[name] = data == null ? null : new SampleBassFactory(data, (BassAudioMixer)mixer) { PlaybackConcurrency = { Value = PlaybackConcurrency } };
factory = factories[name] = data == null ? null : new SampleBassFactory(data, name, (BassAudioMixer)mixer) { PlaybackConcurrency = { Value = PlaybackConcurrency } };

if (factory != null)
AddItem(factory);
Expand Down
7 changes: 6 additions & 1 deletion osu.Framework/Audio/Sample/SampleVirtual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace osu.Framework.Audio.Sample
/// </summary>
public sealed class SampleVirtual : Sample
{
protected override SampleChannel CreateChannel() => new SampleChannelVirtual();
protected override SampleChannel CreateChannel() => new SampleChannelVirtual(Name);

public SampleVirtual(string name = "virtual")
: base(name)
{
}
}
}
3 changes: 2 additions & 1 deletion osu.Framework/Audio/Track/ITrackStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public interface ITrackStore : IAdjustableResourceStore<Track>
/// Retrieve a <see cref="TrackVirtual"/> with no audio device backing.
/// </summary>
/// <param name="length">The length of the virtual track.</param>
/// <param name="name">A name to identify the virtual track internally.</param>
/// <returns>A new virtual track.</returns>
Track GetVirtual(double length = double.PositiveInfinity);
Track GetVirtual(double length = double.PositiveInfinity, string name = "virtual");
}
}
7 changes: 7 additions & 0 deletions osu.Framework/Audio/Track/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public abstract class Track : AdjustableAudioComponent, ITrack, IAudioChannel

public virtual bool Looping { get; set; }

public string Name { get; }

protected Track(string name)
{
Name = name;
}

/// <summary>
/// Reset this track to a logical default state.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion osu.Framework/Audio/Track/TrackBass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ public sealed class TrackBass : Track, IBassAudio, IBassAudioChannel
/// Constructs a new <see cref="TrackBass"/> from provided audio data.
/// </summary>
/// <param name="data">The sample data stream.</param>
/// <param name="name">A name identifying the track internally.</param>
/// <param name="quick">If true, the track will not be fully loaded, and should only be used for preview purposes. Defaults to false.</param>
internal TrackBass(Stream data, bool quick = false)
internal TrackBass(Stream data, string name, bool quick = false)
: base(name)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
Expand Down
6 changes: 3 additions & 3 deletions osu.Framework/Audio/Track/TrackStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ internal TrackStore([NotNull] IResourceStore<byte[]> store, [NotNull] AudioMixer
(store as ResourceStore<byte[]>)?.AddExtension(@"mp3");
}

public Track GetVirtual(double length = double.PositiveInfinity)
public Track GetVirtual(double length = double.PositiveInfinity, string name = "virtual")
{
if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(TrackStore)}");

var track = new TrackVirtual(length);
var track = new TrackVirtual(length, name);
AddItem(track);
return track;
}
Expand All @@ -47,7 +47,7 @@ public Track Get(string name)
if (dataStream == null)
return null;

TrackBass trackBass = new TrackBass(dataStream);
TrackBass trackBass = new TrackBass(dataStream, name);

mixer.Add(trackBass);
AddItem(trackBass);
Expand Down
3 changes: 2 additions & 1 deletion osu.Framework/Audio/Track/TrackVirtual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public sealed class TrackVirtual : Track

private double seekOffset;

public TrackVirtual(double length)
public TrackVirtual(double length, string name = "virtual")
: base(name)
{
Length = length;
}
Expand Down
4 changes: 4 additions & 0 deletions osu.Framework/Graphics/Audio/DrawableSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public DrawableSample(ISample sample, bool disposeSampleOnDisposal = true)
{
this.sample = sample;

Name = sample.Name;

PlaybackConcurrency.BindTo(sample.PlaybackConcurrency);
}

Expand All @@ -47,6 +49,8 @@ public SampleChannel GetChannel()
return channel;
}

string ISample.Name => sample.Name;

public double Length => sample.Length;

public Bindable<int> PlaybackConcurrency { get; } = new Bindable<int>(Sample.DEFAULT_CONCURRENCY);
Expand Down

0 comments on commit 32e7571

Please sign in to comment.