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

Fix wrong bytes per sample in TrackBass #6039

Merged
merged 1 commit into from
Oct 31, 2023
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
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 @@
{
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 @@
// 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 Expand Up @@ -208,7 +214,7 @@
{
base.UpdateState();

bool running = isRunningState(bassMixer.ChannelIsActive(this));

Check failure on line 217 in osu.Framework/Audio/Track/TrackBass.cs

View workflow job for this annotation

GitHub Actions / Test Results (Linux, MultiThreaded, Release)

osu.Framework.Tests.Audio.BassAudioMixerTest ► TestChannelDoesNotPlayIfReachedEndAndMovedMixers

Failed test found in: TestResults-Linux-MultiThreaded-Release.trx Error: TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
Raw output
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
--TearDown
   at osu.Framework.Audio.Track.TrackBass.UpdateState() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/Track/TrackBass.cs:line 217
   at osu.Framework.Audio.AudioComponent.Update() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/AudioComponent.cs:line 86
   at osu.Framework.Audio.AudioCollectionManager`1.UpdateChildren() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/AudioCollectionManager.cs:line 54
   at osu.Framework.Audio.AudioComponent.Update() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/AudioComponent.cs:line 88
   at osu.Framework.Audio.AudioCollectionManager`1.UpdateChildren() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/AudioCollectionManager.cs:line 54
   at osu.Framework.Audio.AudioComponent.Update() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/AudioComponent.cs:line 88
   at osu.Framework.Tests.Audio.BassTestComponents.Dispose() in /home/runner/work/osu-framework/osu-framework/osu.Framework.Tests/Audio/BassTestComponents.cs:line 96
   at osu.Framework.Tests.Audio.BassAudioMixerTest.Teardown() in /home/runner/work/osu-framework/osu-framework/osu.Framework.Tests/Audio/BassAudioMixerTest.cs:line 40

Check failure on line 217 in osu.Framework/Audio/Track/TrackBass.cs

View workflow job for this annotation

GitHub Actions / Test Results (Linux, SingleThread, Release)

osu.Framework.Tests.Audio.BassAudioMixerTest ► TestChannelDoesNotPlayIfReachedEndAndMovedMixers

Failed test found in: TestResults-Linux-SingleThread-Release.trx Error: TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
Raw output
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
--TearDown
   at osu.Framework.Audio.Track.TrackBass.UpdateState() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/Track/TrackBass.cs:line 217
   at osu.Framework.Audio.AudioComponent.Update() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/AudioComponent.cs:line 86
   at osu.Framework.Audio.AudioCollectionManager`1.UpdateChildren() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/AudioCollectionManager.cs:line 54
   at osu.Framework.Audio.AudioComponent.Update() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/AudioComponent.cs:line 88
   at osu.Framework.Audio.AudioCollectionManager`1.UpdateChildren() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/AudioCollectionManager.cs:line 54
   at osu.Framework.Audio.AudioComponent.Update() in /home/runner/work/osu-framework/osu-framework/osu.Framework/Audio/AudioComponent.cs:line 88
   at osu.Framework.Tests.Audio.BassTestComponents.Dispose() in /home/runner/work/osu-framework/osu-framework/osu.Framework.Tests/Audio/BassTestComponents.cs:line 96
   at osu.Framework.Tests.Audio.BassAudioMixerTest.Teardown() in /home/runner/work/osu-framework/osu-framework/osu.Framework.Tests/Audio/BassAudioMixerTest.cs:line 40

// because device validity check isn't done frequently, when switching to "No sound" device,
// there will be a brief time where this track will be stopped, before we resume it manually (see comments in UpdateDevice(int).)
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
Loading