Skip to content

Commit

Permalink
Use Clear instead of Flush in TrackSDL2Players
Browse files Browse the repository at this point in the history
  • Loading branch information
hwsmm committed Jan 2, 2024
1 parent a2681a5 commit 5e73ed8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion osu.Framework/Audio/ResamplingPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected virtual double GetProcessingLatency()
return resampler.GetCurrentLatency() * 1000.0d;
}

public virtual void Flush()
public virtual void Clear()
{
resampler?.Reset();
}
Expand Down
26 changes: 12 additions & 14 deletions osu.Framework/Audio/Track/TempoSDL2AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public TempoSDL2AudioPlayer(int rate, byte channels, int samples)
/// <param name="samples">Needed sample count</param>
private void fillSamples(int samples)
{
if (soundTouch == null)
if (soundTouch == null || tempo == 1.0f)
return;

while (!base.Done && soundTouch.AvailableSamples < samples)
Expand Down Expand Up @@ -104,15 +104,14 @@ private void setTempo(double tempo)
if (temp >= 0)
AudioDataPosition = temp;
}

Reset(false);
soundTouch = null;
return;
}
else
{
double tempochange = Math.Clamp((Math.Abs(tempo) - 1.0d) * 100.0d, -95, 5000);
soundTouch.TempoChange = tempochange;
}

double tempochange = Math.Clamp((Math.Abs(tempo) - 1.0d) * 100.0d, -95, 5000);
soundTouch.TempoChange = tempochange;
FillRequiredSamples();
Clear();
}
}

Expand All @@ -123,7 +122,7 @@ private void setTempo(double tempo)
/// <returns>The number of samples put</returns>
public override int GetRemainingSamples(float[] ret)
{
if (soundTouch == null)
if (soundTouch == null || tempo == 1.0f)
return base.GetRemainingSamples(ret);

if (RelativeRate == 0)
Expand All @@ -132,9 +131,7 @@ public override int GetRemainingSamples(float[] ret)
int expected = ret.Length / SrcChannels;

if (!doneFilling && soundTouch.AvailableSamples < expected)
{
fillSamples(expected);
}

int got = soundTouch.ReceiveSamples(ret, expected);

Expand All @@ -147,6 +144,7 @@ public override int GetRemainingSamples(float[] ret)
public override void Reset(bool resetPosition = true)
{
base.Reset(resetPosition);

doneFilling = false;
donePlaying = false;
}
Expand All @@ -161,10 +159,10 @@ protected int GetTempoLatencyInSamples()

protected override double GetProcessingLatency() => base.GetProcessingLatency() + (double)GetTempoLatencyInSamples() / SrcRate * 1000.0d;

public override void Flush()
public override void Clear()
{
base.Flush();
soundTouch?.Flush();
base.Clear();
soundTouch?.Clear();
}

public override void Seek(double seek)
Expand Down
18 changes: 6 additions & 12 deletions osu.Framework/Audio/Track/TrackSDL2AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Buffers;
using System.Threading;
using osu.Framework.Logging;

namespace osu.Framework.Audio.Track
{
Expand Down Expand Up @@ -200,9 +199,6 @@ protected override int GetRemainingRawFloats(float[] data, int offset, int neede
AudioDataPosition += read;
}

if (read < needed && isLoading)
Logger.Log("Track underrun!");

if (ReversePlayback ? AudioDataPosition <= 0 : AudioDataPosition >= audioDataLength && !isLoading)
done = true;

Expand Down Expand Up @@ -243,6 +239,8 @@ public virtual void Reset(bool resetPosition = true)
SaveSeek = 0;
Seek(0);
}

Clear();
}

/// <summary>
Expand Down Expand Up @@ -278,7 +276,7 @@ public virtual void Seek(double seek)
{
SaveSeek = 0;
AudioDataPosition = Math.Clamp(tmp, 0, Math.Max(0, audioDataLength - 1));
Flush();
Clear();
}
}

Expand All @@ -288,14 +286,10 @@ protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
if (dataRented && AudioData != null)
ArrayPool<float>.Shared.Return(AudioData);

AudioData = null;
}
if (dataRented && AudioData != null)
ArrayPool<float>.Shared.Return(AudioData);

AudioData = null;
disposedValue = true;
}
}
Expand Down

0 comments on commit 5e73ed8

Please sign in to comment.