Skip to content

Commit

Permalink
Merge pull request #6213 from bdach/waveform-lacklustre-error-checking
Browse files Browse the repository at this point in the history
Check BASS error codes more diligently when retrieving waveform data
  • Loading branch information
smoogipoo committed Mar 13, 2024
2 parents c17d3f8 + 95e5757 commit 94f2e77
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions osu.Framework/Audio/Track/Waveform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,30 @@ public Waveform(Stream? data)
int decodeStream = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode | BassFlags.Float, fileCallbacks.Callbacks, fileCallbacks.Handle);
if (decodeStream == 0)
{
logBassError("could not create stream");
return;
}
float[]? sampleBuffer = null;
try
{
Bass.ChannelGetInfo(decodeStream, out ChannelInfo info);
if (!Bass.ChannelGetInfo(decodeStream, out ChannelInfo info))
{
logBassError("could not retrieve channel information");
return;
}
long length = Bass.ChannelGetLength(decodeStream);
if (length < 0)
{
logBassError("could not retrieve channel length");
return;
}
// Each "point" is generated from a number of samples, each sample contains a number of channels
int samplesPerPoint = (int)(info.Frequency * resolution * info.Channels);
Expand All @@ -127,6 +143,13 @@ public Waveform(Stream? data)
while (length > 0)
{
length = Bass.ChannelGetData(decodeStream, sampleBuffer, bytesPerIteration);
if (length < 0 && Bass.LastError != Errors.Ended)
{
logBassError("could not retrieve sample data");
return;
}
int samplesRead = (int)(length / bytes_per_sample);
// Each point is composed of multiple samples
Expand Down Expand Up @@ -157,9 +180,20 @@ public Waveform(Stream? data)
}
}
Bass.ChannelSetPosition(decodeStream, 0);
if (!Bass.ChannelSetPosition(decodeStream, 0))
{
logBassError("could not reset channel position");
return;
}
length = Bass.ChannelGetLength(decodeStream);
if (length < 0)
{
logBassError("could not retrieve channel length");
return;
}
// Read FFT data
float[] bins = new float[fft_bins];
int currentPoint = 0;
Expand All @@ -168,6 +202,13 @@ public Waveform(Stream? data)
while (length > 0)
{
length = Bass.ChannelGetData(decodeStream, bins, (int)fft_samples);
if (length < 0 && Bass.LastError != Errors.Ended)
{
logBassError("could not retrieve FFT data");
return;
}
currentByte += length;
float lowIntensity = computeIntensity(info, bins, low_min, mid_min);
Expand All @@ -191,7 +232,9 @@ public Waveform(Stream? data)
}
finally
{
Bass.StreamFree(decodeStream);
if (!Bass.StreamFree(decodeStream))
logBassError("failed to free decode stream");
fileCallbacks.Dispose();
data.Dispose();
Expand All @@ -201,6 +244,8 @@ public Waveform(Stream? data)
ArrayPool<float>.Shared.Return(sampleBuffer);
}
}, cancelSource.Token);

void logBassError(string reason) => Logger.Log($"BASS failure while reading waveform: {reason} ({Bass.LastError})");
}

private float computeIntensity(ChannelInfo info, float[] bins, float startFrequency, float endFrequency)
Expand Down

0 comments on commit 94f2e77

Please sign in to comment.