Skip to content

Commit

Permalink
Fix wrong bytes per sample in TrackBass
Browse files Browse the repository at this point in the history
Waveform will also no longer bring the constant from TrackBass since Waveform still uses float samples.
  • Loading branch information
hwsmm committed Oct 30, 2023
1 parent f1981a3 commit 9756229
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 9 additions & 3 deletions osu.Framework/Audio/Track/TrackBass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace osu.Framework.Audio.Track
{
public sealed class TrackBass : Track, IBassAudio, IBassAudioChannel
{
public const int BYTES_PER_SAMPLE = 4;

private Stream? dataStream;

/// <summary>
Expand Down Expand Up @@ -106,14 +104,22 @@ internal TrackBass(Stream data, string name, bool quick = false)
// will be -1 in case of an error
double seconds = Bass.ChannelBytes2Seconds(activeStream, byteLength);
int channels = 2;
if (Bass.ChannelGetInfo(activeStream, out ChannelInfo info))
channels = info.Channels;
bool success = seconds >= 0;
if (success)
{
Length = seconds * 1000;
// Bass uses 16-bit samples by default if neither BassFlags.Byte nor BassFlags.Float is specified
const int bytes_per_sample = 2;
// Bass does not allow seeking to the end of the track, so the last available position is 1 sample before.
lastSeekablePosition = Bass.ChannelBytes2Seconds(activeStream, byteLength - BYTES_PER_SAMPLE) * 1000;
lastSeekablePosition = Bass.ChannelBytes2Seconds(activeStream, byteLength - bytes_per_sample * channels) * 1000;
isLoaded = true;
Expand Down
8 changes: 5 additions & 3 deletions osu.Framework/Audio/Track/Waveform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public Waveform(Stream? data)
fileCallbacks = new FileCallbacks(new DataStreamFileProcedures(data));
const int bytes_per_sample = 4;
int decodeStream = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode | BassFlags.Float, fileCallbacks.Callbacks, fileCallbacks.Handle);
float[]? sampleBuffer = null;
Expand All @@ -102,7 +104,7 @@ public Waveform(Stream? data)
// Each "point" is generated from a number of samples, each sample contains a number of channels
int samplesPerPoint = (int)(info.Frequency * resolution * info.Channels);
int bytesPerPoint = samplesPerPoint * TrackBass.BYTES_PER_SAMPLE;
int bytesPerPoint = samplesPerPoint * bytes_per_sample;
int pointCount = (int)(length / bytesPerPoint);
Expand All @@ -111,15 +113,15 @@ public Waveform(Stream? data)
// Each iteration pulls in several samples
int bytesPerIteration = bytesPerPoint * points_per_iteration;
sampleBuffer = ArrayPool<float>.Shared.Rent(bytesPerIteration / TrackBass.BYTES_PER_SAMPLE);
sampleBuffer = ArrayPool<float>.Shared.Rent(bytesPerIteration / bytes_per_sample);
int pointIndex = 0;
// Read sample data
while (length > 0)
{
length = Bass.ChannelGetData(decodeStream, sampleBuffer, bytesPerIteration);
int samplesRead = (int)(length / TrackBass.BYTES_PER_SAMPLE);
int samplesRead = (int)(length / bytes_per_sample);
// Each point is composed of multiple samples
for (int i = 0; i < samplesRead && pointIndex < pointCount; i += samplesPerPoint)
Expand Down

0 comments on commit 9756229

Please sign in to comment.