Skip to content

Commit

Permalink
Fixed pause bug
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed Apr 20, 2023
1 parent 0e50024 commit be9eb5f
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions Assets/Script/Audio/Bass/BassStemMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
namespace YARG {
public class BassStemMixer : IStemMixer {

public int StemsLoaded { get; private set; }
public int StemsLoaded { get; private set; }

public bool IsPlaying { get; private set; }

public IStemChannel LeadChannel { get; private set; }

private readonly Dictionary<SongStem, IStemChannel> _channels;

private int _mixerHandle;

private bool _disposed;

public BassStemMixer() {
_channels = new Dictionary<SongStem, IStemChannel>();
StemsLoaded = 0;
IsPlaying = false;
}

~BassStemMixer() {
Dispose(false);
}
Expand All @@ -33,23 +33,24 @@ public bool Create() {
if (_mixerHandle != 0) {
return true;
}

int mixer = BassMix.CreateMixerStream(44100, 2, BassFlags.Default);
if (mixer == 0) {
return false;
}

// Mixer processing threads (for some reason this attribute is undocumented in ManagedBass?)
Bass.ChannelSetAttribute(mixer, (ChannelAttribute) 86017, 2);

_mixerHandle = mixer;
return true;
}

public int Play(bool restart = false) {
if (IsPlaying)
if (IsPlaying) {
return 0;

}

if (!Bass.ChannelPlay(_mixerHandle, restart)) {
return (int) Bass.LastError;
}
Expand All @@ -63,11 +64,13 @@ public int Pause() {
if (!IsPlaying) {
return 0;
}

if (!Bass.ChannelPause(_mixerHandle)) {
return (int) Bass.LastError;
}

IsPlaying = false;

return 0;
}

Expand All @@ -83,19 +86,19 @@ public int AddChannel(IStemChannel channel) {
if (channel is not BassStemChannel bassChannel) {
throw new ArgumentException("Channel must be of type BassStemChannel");
}

if (_channels.ContainsKey(channel.Stem)) {
return 0;
}

if (!BassMix.MixerAddChannel(_mixerHandle, bassChannel.StreamHandle, BassFlags.Default)) {
return (int) Bass.LastError;
}

_channels.Add(channel.Stem, channel);
StemsLoaded++;
if(channel.LengthD > LeadChannel?.LengthD || LeadChannel is null) {

if (channel.LengthD > LeadChannel?.LengthD || LeadChannel is null) {
LeadChannel = channel;
}

Expand All @@ -106,22 +109,22 @@ public bool RemoveChannel(IStemChannel channel) {
if (channel is not BassStemChannel bassChannel) {
throw new ArgumentException("Channel must be of type BassStemChannel");
}

if (!_channels.ContainsKey(channel.Stem)) {
return false;
}

if (!BassMix.MixerRemoveChannel(bassChannel.StreamHandle)) {
return false;
}

_channels.Remove(channel.Stem);
StemsLoaded--;
if(channel == LeadChannel) {

if (channel == LeadChannel) {
// Update lead channel
foreach(var c in _channels.Values) {
if(c.LengthD > LeadChannel?.LengthD) {
foreach (var c in _channels.Values) {
if (c.LengthD > LeadChannel?.LengthD) {
LeadChannel = c;
}
}
Expand All @@ -133,31 +136,31 @@ public bool RemoveChannel(IStemChannel channel) {
public IStemChannel GetChannel(SongStem stem) {
return !_channels.ContainsKey(stem) ? null : _channels[stem];
}

public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing) {
if (!_disposed) {
// Free managed resources here
if (disposing) {
foreach(var channel in _channels.Values) {
foreach (var channel in _channels.Values) {
channel.Dispose();
}

_channels.Clear();
}

// Free unmanaged resources here
if (_mixerHandle != 0) {
Bass.StreamFree(_mixerHandle);
}

_disposed = true;
}
}
}

}

0 comments on commit be9eb5f

Please sign in to comment.