Skip to content

Commit

Permalink
Fix stem volume settings while paused (YARC-Official#156)
Browse files Browse the repository at this point in the history
* Fix stems not adjusting volumes correctly after editing settings while paused

* Adjusted localisation of some volume settings
  • Loading branch information
RileyTheFox authored Apr 21, 2023
1 parent d6d14e9 commit 521603f
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 28 deletions.
41 changes: 27 additions & 14 deletions Assets/Script/Audio/Bass/BassAudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ public class BassAudioManager : MonoBehaviour, IAudioManager {

public bool IsAudioLoaded { get; private set; }
public bool IsPlaying { get; private set; }

public double MasterVolume { get; private set; }
public double SfxVolume { get; private set; }

public double CurrentPositionD => GetPosition();
public double AudioLengthD { get; private set; }

public float CurrentPositionF => (float) GetPosition();
public float AudioLengthF { get; private set; }

public double[] stemVolumes;
public double sfxVolume;

private int opusHandle;
private double[] _stemVolumes;

private int _opusHandle;

private IStemMixer _mixer;

Expand All @@ -43,19 +45,19 @@ private void Awake() {
".opus",
};

stemVolumes = new double[AudioHelpers.SupportedStems.Count];
_stemVolumes = new double[AudioHelpers.SupportedStems.Count];

_sfxSamples = new ISampleChannel[AudioHelpers.SfxPaths.Count];

opusHandle = 0;
_opusHandle = 0;
}

public void Initialize() {
Debug.Log("Initializing BASS...");
string bassPath = GetBassDirectory();
string opusLibDirectory = Path.Combine(bassPath, "bassopus");

opusHandle = Bass.PluginLoad(opusLibDirectory);
_opusHandle = Bass.PluginLoad(opusLibDirectory);
Bass.Configure(Configuration.IncludeDefaultDevice, true);

Bass.UpdatePeriod = 5;
Expand Down Expand Up @@ -98,8 +100,8 @@ public void Unload() {

UnloadSong();

Bass.PluginFree(opusHandle);
opusHandle = 0;
Bass.PluginFree(_opusHandle);
_opusHandle = 0;

// Free SFX samples
foreach (var sample in _sfxSamples) {
Expand Down Expand Up @@ -206,6 +208,9 @@ public void Play() {
return;
}

foreach (var channel in _mixer.Channels.Values) {
channel.SetVolume(channel.Volume);
}
if (_mixer.Play() != 0) {
Debug.Log($"Play error: {Bass.LastError}");
}
Expand Down Expand Up @@ -234,25 +239,33 @@ public void PlaySoundEffect(SfxSample sample) {
public void SetStemVolume(SongStem stem, double volume) {
var channel = _mixer.GetChannel(stem);

// Multiply volume inputted by the stem's volume setting (e.g. 0.5 * 0.5 = 0.25)
channel?.SetVolume(volume * stemVolumes[(int) stem]);
channel?.SetVolume(volume);
}

public void UpdateVolumeSetting(SongStem stem, double volume) {
switch (stem)
{
case SongStem.Master:
Bass.GlobalStreamVolume = (int) (10_000 * volume);
MasterVolume = volume;
Bass.GlobalStreamVolume = (int) (10_000 * MasterVolume);
break;
case SongStem.Sfx:
sfxVolume = volume;
SfxVolume = volume;
break;
default:
stemVolumes[(int) stem] = volume;
_stemVolumes[(int) stem] = volume;
break;
}
}

public double GetVolumeSetting(SongStem stem) {
return stem switch {
SongStem.Master => MasterVolume,
SongStem.Sfx => SfxVolume,
_ => _stemVolumes[(int) stem]
};
}

public void ApplyReverb(SongStem stem, bool reverb) {
_mixer?.GetChannel(stem)?.SetReverb(reverb);
}
Expand Down
8 changes: 5 additions & 3 deletions Assets/Script/Audio/Bass/BassSampleChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ public class BassSampleChannel : ISampleChannel {

public SfxSample Sample { get; }

private readonly BassAudioManager _manager;
private readonly IAudioManager _manager;
private readonly string _path;
private readonly int _playbackCount;

private int _sfxHandle;

private bool _disposed;

public BassSampleChannel(BassAudioManager manager, string path, int playbackCount, SfxSample sample) {
public BassSampleChannel(IAudioManager manager, string path, int playbackCount, SfxSample sample) {
_manager = manager;
_path = path;
_playbackCount = playbackCount;
Expand Down Expand Up @@ -45,7 +45,9 @@ public void Play() {
return;

int channel = Bass.SampleGetChannel(_sfxHandle);
Bass.ChannelSetAttribute(channel, ChannelAttribute.Volume, _manager.sfxVolume * AudioHelpers.SfxVolume[(int) Sample]);

double volume = _manager.GetVolumeSetting(SongStem.Sfx) * AudioHelpers.SfxVolume[(int) Sample];
Bass.ChannelSetAttribute(channel, ChannelAttribute.Volume, volume);

Bass.ChannelPlay(channel);
}
Expand Down
36 changes: 30 additions & 6 deletions Assets/Script/Audio/Bass/BassStemChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,30 @@ public class BassStemChannel : IStemChannel {
public SongStem Stem { get; }
public double LengthD { get; private set; }

public double Volume { get; private set; }

public int StreamHandle { get; private set; }

private readonly string _path;
private readonly BassAudioManager _manager;
private readonly IAudioManager _manager;

private readonly Dictionary<EffectType, int> _effects;
private readonly Dictionary<DSPType, int> _dspHandles;

private readonly DSPProcedure _dspGain;

private double _lastStemVolume;

private bool _disposed;

public BassStemChannel(BassAudioManager manager, string path, SongStem stem) {
public BassStemChannel(IAudioManager manager, string path, SongStem stem) {
_manager = manager;
_path = path;
Stem = stem;


Volume = 1;

_lastStemVolume = _manager.GetVolumeSetting(Stem);
_effects = new Dictionary<EffectType, int>();
_dspHandles = new Dictionary<DSPType, int>();

Expand All @@ -40,6 +47,7 @@ public BassStemChannel(BassAudioManager manager, string path, SongStem stem) {
Dispose(false);
}


public int Load(bool isSpeedUp, float speed) {
if (StreamHandle != 0) {
return 0;
Expand All @@ -53,7 +61,7 @@ public int Load(bool isSpeedUp, float speed) {
const BassFlags flags = BassFlags.SampleOverrideLowestVolume | BassFlags.Decode | BassFlags.FxFreeSource;
StreamHandle = BassFx.TempoCreate(streamHandle, flags);

Bass.ChannelSetAttribute(StreamHandle, ChannelAttribute.Volume, _manager.stemVolumes[(int) Stem]);
Bass.ChannelSetAttribute(StreamHandle, ChannelAttribute.Volume, _manager.GetVolumeSetting(Stem));

if (isSpeedUp) {
// Gets relative speed from 100% (so 1.05f = 5% increase)
Expand All @@ -74,12 +82,28 @@ public int Load(bool isSpeedUp, float speed) {
return 0;
}

public void SetVolume(double volume) {
public void SetVolume(double newVolume) {
if (StreamHandle == 0) {
return;
}

double volumeSetting = _manager.GetVolumeSetting(Stem);

double oldBassVol = _lastStemVolume * Volume;
double newBassVol = volumeSetting * newVolume;

// Values are the same, no need to change
if (Math.Abs(oldBassVol - newBassVol) < double.Epsilon) {
Debug.Log($"{Stem} values same");
return;
}

Debug.Log($"Updated {Stem} volume to {newVolume}");

Volume = newVolume;
_lastStemVolume = volumeSetting;

Bass.ChannelSetAttribute(StreamHandle, ChannelAttribute.Volume, volume);
Bass.ChannelSetAttribute(StreamHandle, ChannelAttribute.Volume, newBassVol);
}

public void SetReverb(bool reverb) {
Expand Down
5 changes: 3 additions & 2 deletions Assets/Script/Audio/Bass/BassStemMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using System.Collections.Generic;
using ManagedBass;
using ManagedBass.Mix;
using UnityEngine;

namespace YARG {
public class BassStemMixer : IStemMixer {

public int StemsLoaded { get; private set; }

public bool IsPlaying { get; private set; }

public IReadOnlyDictionary<SongStem, IStemChannel> Channels => _channels;

public IStemChannel LeadChannel { get; private set; }

Expand Down
5 changes: 5 additions & 0 deletions Assets/Script/Audio/Interfaces/IAudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public interface IAudioManager {

public bool IsAudioLoaded { get; }
public bool IsPlaying { get; }

public double MasterVolume { get; }
public double SfxVolume { get; }

public double CurrentPositionD { get; }
public double AudioLengthD { get; }
Expand All @@ -32,6 +35,8 @@ public interface IAudioManager {
public void SetStemVolume(SongStem stem, double volume);

public void UpdateVolumeSetting(SongStem stem, double volume);

public double GetVolumeSetting(SongStem stem);

public void ApplyReverb(SongStem stem, bool reverb);

Expand Down
4 changes: 3 additions & 1 deletion Assets/Script/Audio/Interfaces/IStemChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ public interface IStemChannel : IDisposable {
public SongStem Stem { get; }
public double LengthD { get; }
public float LengthF => (float)LengthD;

public double Volume { get; }

public int Load(bool isSpeedUp, float speed);

public void SetVolume(double volume);
public void SetVolume(double newVolume);

public void SetReverb(bool reverb);

Expand Down
3 changes: 3 additions & 0 deletions Assets/Script/Audio/Interfaces/IStemMixer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace YARG {
public interface IStemMixer : IDisposable {
Expand All @@ -7,6 +8,8 @@ public interface IStemMixer : IDisposable {

public bool IsPlaying { get; }

public IReadOnlyDictionary<SongStem, IStemChannel> Channels { get; }

public IStemChannel LeadChannel { get; }

public bool Create();
Expand Down
4 changes: 2 additions & 2 deletions Assets/Settings/Localization/Settings_en-US.asset
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ MonoBehaviour:
m_Metadata:
m_Items: []
- m_Id: 3645699926646784
m_Localized: Master Music Volume
m_Localized: Master Volume
m_Metadata:
m_Items: []
- m_Id: 3645822882668544
Expand All @@ -103,7 +103,7 @@ MonoBehaviour:
m_Metadata:
m_Items: []
- m_Id: 3646318347411456
m_Localized: Other Instruments Volume
m_Localized: Song Volume
m_Metadata:
m_Items: []
- m_Id: 3646366187642880
Expand Down

0 comments on commit 521603f

Please sign in to comment.