Skip to content

Commit

Permalink
Some audio engine tweaks (YARC-Official#130)
Browse files Browse the repository at this point in the history
* Fix localisation for chipmunk speed (it didn't save wtf unity?)

* Set mixer threads to 2, auto free source stream from BassFX stream, reverb research
  • Loading branch information
RileyTheFox authored Apr 19, 2023
1 parent 26732ca commit 45a30ca
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
72 changes: 54 additions & 18 deletions Assets/Script/Audio/BassAudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class BassAudioManager : MonoBehaviour, IAudioManager {
private Dictionary<int, Dictionary<EffectType, int>> stemEffects;
private Dictionary<int, int> stemGainDsps;

private const EffectType REVERB_TYPE = EffectType.DXReverb;

private void Awake() {
SupportedFormats = new[] {
".ogg",
Expand Down Expand Up @@ -171,6 +173,10 @@ public void LoadSong(IEnumerable<string> stems, bool isSpeedUp) {
UnloadSong();

mixerHandle = BassMix.CreateMixerStream(44100, 2, BassFlags.Default);

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

StemsLoaded = 0;

foreach (string stemPath in stems) {
Expand All @@ -193,7 +199,8 @@ public void LoadSong(IEnumerable<string> stems, bool isSpeedUp) {
}

// Create BassFX Tempo handle
int tempoHandle = BassFx.TempoCreate(streamHandle, BassFlags.SampleOverrideLowestVolume | BassFlags.Decode);
const BassFlags flags = BassFlags.SampleOverrideLowestVolume | BassFlags.Decode | BassFlags.FxFreeSource;
int tempoHandle = BassFx.TempoCreate(streamHandle, flags);

// Apply volume setting to stream
Bass.ChannelSetAttribute(tempoHandle, ChannelAttribute.Volume, stemVolumes[stemIndex]);
Expand Down Expand Up @@ -332,34 +339,25 @@ public void ApplyReverb(SongStem stem, bool reverb) {

if (reverb) {
// Reverb already applied
if (stemEffects[stemHandle].ContainsKey(EffectType.DXReverb))
if (stemEffects[stemHandle].ContainsKey(REVERB_TYPE))
return;

// Set reverb FX
int reverbHandle = Bass.ChannelSetFX(stemHandle, EffectType.DXReverb, 0);
int reverbHandle = AddReverbToChannel(stemHandle);
int gainDspHandle = Bass.ChannelSetDSP(stemHandle, dspGain);

var reverbParams = new DXReverbParameters {
fInGain = 0.0f,
fReverbMix = -5f,
fReverbTime = 1000.0f,
fHighFreqRTRatio = 0.001f
};

Bass.FXSetParameters(reverbHandle, reverbParams);

stemEffects[stemHandle].Add(EffectType.DXReverb, reverbHandle);
stemEffects[stemHandle].Add(REVERB_TYPE, reverbHandle);
stemGainDsps.Add(stemHandle, gainDspHandle);
} else {
// No reverb is applied
if (!stemEffects[stemHandle].ContainsKey(EffectType.DXReverb)) {
if (!stemEffects[stemHandle].ContainsKey(REVERB_TYPE)) {
return;
}

Bass.ChannelRemoveFX(stemHandle, stemEffects[stemHandle][EffectType.DXReverb]);
Bass.ChannelRemoveFX(stemHandle, stemEffects[stemHandle][REVERB_TYPE]);
Bass.ChannelRemoveDSP(stemHandle, stemGainDsps[stemHandle]);

stemEffects[stemHandle].Remove(EffectType.DXReverb);
stemEffects[stemHandle].Remove(REVERB_TYPE);
stemGainDsps.Remove(stemHandle);
}
}
Expand All @@ -376,6 +374,45 @@ private void OnApplicationQuit() {
Unload();
}

private int AddReverbToChannel(int stemHandle) {
// Set reverb FX
int reverbHandle = Bass.ChannelSetFX(stemHandle, REVERB_TYPE, 0);
if (reverbHandle == 0) {
Debug.Log($"Reverb set fx error: {(int)Bass.LastError}");
}

IEffectParameter reverbParams;

switch (REVERB_TYPE) {
case EffectType.DXReverb:
reverbParams = new DXReverbParameters {
fInGain = 0.0f,
fReverbMix = -5f,
fReverbTime = 1000.0f,
fHighFreqRTRatio = 0.001f
};
break;
case EffectType.Freeverb:
reverbParams = new ReverbParameters() {
fDryMix = 1f,
fWetMix = 2f,
fRoomSize = 0.5f,
fDamp = 0.2f,
fWidth = 1.0f,
lMode = 0
};
break;
default:
throw new ArgumentOutOfRangeException();
}

if (!Bass.FXSetParameters(reverbHandle, reverbParams)) {
Debug.Log($"Set param error: {(int)Bass.LastError}");
}

return reverbHandle;
}

private static unsafe void GainDSP(int handle, int channel, IntPtr buffer, int length, IntPtr user) {
var bufferPtr = (float*) buffer;
int samples = length / 4;
Expand All @@ -390,8 +427,7 @@ private static double GetAudioLengthInSeconds(int channel) {
double seconds = Bass.ChannelBytes2Seconds(channel, length);
return seconds;
}



private static string GetBassDirectory() {
string pluginDirectory = Path.Combine(Application.dataPath, "Plugins");

Expand Down
4 changes: 4 additions & 0 deletions Assets/Settings/Localization/Settings Shared Data.asset
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ MonoBehaviour:
m_Key: rhythmVolume
m_Metadata:
m_Items: []
- m_Id: 8393437626548224
m_Key: useChipmunkSpeed
m_Metadata:
m_Items: []
m_Metadata:
m_Items: []
m_KeyGenerator:
Expand Down
4 changes: 4 additions & 0 deletions Assets/Settings/Localization/Settings_en-US.asset
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ MonoBehaviour:
m_Localized: Rhythm Volume
m_Metadata:
m_Items: []
- m_Id: 8393437626548224
m_Localized: Chipmunk Speedups
m_Metadata:
m_Items: []
references:
version: 2
RefIds: []

0 comments on commit 45a30ca

Please sign in to comment.