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 async track Start/Stop/Play/Restart methods #5294

Merged
merged 6 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions osu.Framework/Audio/Track/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Threading.Tasks;
using osu.Framework.Audio.Mixing;
using osu.Framework.Extensions;

namespace osu.Framework.Audio.Track
{
Expand All @@ -13,8 +14,8 @@ public abstract class Track : AdjustableAudioComponent, ITrack, IAudioChannel
public event Action? Completed;
public event Action? Failed;

protected virtual void RaiseCompleted() => Completed?.Invoke();
protected virtual void RaiseFailed() => Failed?.Invoke();
protected void RaiseCompleted() => Completed?.Invoke();
protected void RaiseFailed() => Failed?.Invoke();

public virtual bool IsDummyDevice => true;

Expand All @@ -38,12 +39,14 @@ public virtual void Reset()
/// <summary>
/// Restarts this track from the <see cref="RestartPoint"/> while retaining adjustments.
/// </summary>
public virtual void Restart()
public void Restart() => RestartAsync().WaitSafely();

public Task RestartAsync() => EnqueueAction(() =>
{
Stop();
Seek(RestartPoint);
Start();
}
});

public virtual void ResetSpeedAdjustments()
{
Expand Down Expand Up @@ -82,15 +85,15 @@ public double Length
/// <returns>Whether the seek was successful.</returns>
public abstract bool Seek(double seek);

public virtual void Start()
{
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not start disposed tracks.");
}
public abstract Task<bool> SeekAsync(double seek);

public virtual void Stop()
{
}
public abstract Task StartAsync();

public abstract void Start();

public abstract Task StopAsync();

public abstract void Stop();

public abstract bool IsRunning { get; }

Expand Down
25 changes: 12 additions & 13 deletions osu.Framework/Audio/Track/TrackBass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,16 @@ protected override void UpdateState()

public override bool IsDummyDevice => false;

public override void Stop()
{
base.Stop();

StopAsync().WaitSafely();
}
public override void Stop() => StopAsync().WaitSafely();

public Task StopAsync() => EnqueueAction(() =>
public override Task StopAsync()
{
stopInternal();
isRunning = isPlayed = false;
});
return EnqueueAction(() =>
Comment on lines +225 to +227
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about not calling the base method... Both here and in TrackVirtual's overrides... If I were to guess, you did this because you don't want the overhead.

I suppose it's fine for now... It's basically a smoking gun if Track itself ever gets changed to do something (like a disposal check) there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's a bit weird. Could change Stop to be abstract instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably all of them abstract yeah? I mean the existing TrackVirtual.Start() also elides the base call which does an IsDisposed check. Going by that, it should just be moved to TrackBass.

{
stopInternal();
isRunning = isPlayed = false;
});
}

private void stopInternal()
{
Expand All @@ -251,12 +249,13 @@ private void setDirection(bool reverse)

public override void Start()
{
base.Start();
if (IsDisposed)
throw new ObjectDisposedException(ToString(), "Can not start disposed tracks.");

StartAsync().WaitSafely();
}

public Task StartAsync() => EnqueueAction(() =>
public override Task StartAsync() => EnqueueAction(() =>
{
if (startInternal())
isRunning = isPlayed = true;
Expand Down Expand Up @@ -291,7 +290,7 @@ public override bool Looping

public override bool Seek(double seek) => SeekAsync(seek).GetResultSafely();

public async Task<bool> SeekAsync(double seek)
public override async Task<bool> SeekAsync(double seek)
{
// At this point the track may not yet be loaded which is indicated by a 0 length.
// In that case we still want to return true, hence the conservative length.
Expand Down
18 changes: 18 additions & 0 deletions osu.Framework/Audio/Track/TrackVirtual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using System;
using System.Threading.Tasks;
using osu.Framework.Timing;

namespace osu.Framework.Audio.Track
Expand Down Expand Up @@ -34,6 +35,23 @@ public override bool Seek(double seek)
return seekOffset == seek;
}

public override Task<bool> SeekAsync(double seek)
{
return Task.FromResult(Seek(seek));
}

public override Task StartAsync()
{
Start();
return Task.CompletedTask;
}

public override Task StopAsync()
{
Stop();
return Task.CompletedTask;
}

public override void Start()
{
if (Length == 0)
Expand Down
9 changes: 9 additions & 0 deletions osu.Framework/Graphics/Audio/DrawableTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Threading.Tasks;
using osu.Framework.Audio;
using osu.Framework.Audio.Mixing;
using osu.Framework.Audio.Track;
Expand Down Expand Up @@ -93,6 +94,14 @@ public void ResetSpeedAdjustments()
RemoveAllAdjustments(AdjustableProperty.Tempo);
}

public Task RestartAsync() => track.RestartAsync();

public Task<bool> SeekAsync(double seek) => track.SeekAsync(seek);

public Task StartAsync() => track.StartAsync();

public Task StopAsync() => track.StopAsync();

public bool Seek(double seek) => track.Seek(seek);

public void Start() => track.Start();
Expand Down