Skip to content

Commit

Permalink
Add preview audio loading code (#274)
Browse files Browse the repository at this point in the history
* Set bass channel position

* Load preview audio and fade functions

* Try fix volume slide

* Adjust play function for fade
  • Loading branch information
RileyTheFox authored May 7, 2023
1 parent 01c54e9 commit c29e3f4
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 10 deletions.
44 changes: 34 additions & 10 deletions Assets/Script/Audio/Bass/BassAudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ManagedBass;
using UnityEngine;
using YARG.Serialization;
using YARG.Song;
using Debug = UnityEngine.Debug;

namespace YARG {
Expand Down Expand Up @@ -210,10 +211,8 @@ public void LoadMogg(XboxMoggData moggData, bool isSpeedUp) {
int moggOffset = moggData.MoggAddressAudioOffset;
long moggLength = moggData.MoggAudioLength;

//byte[] moggArray = File.ReadAllBytes(moggData.MoggPath)[moggOffset..];
byte[] moggArray = moggData.GetOggDataFromMogg();

//int moggStreamHandle = Bass.CreateStream(moggArray, 0, moggArray.Length, BassFlags.Prescan | BassFlags.Decode | BassFlags.AsyncFile);
int moggStreamHandle = Bass.CreateStream(moggArray, 0, moggLength, BassFlags.Prescan | BassFlags.Decode | BassFlags.AsyncFile);
if (moggStreamHandle == 0) {
Debug.LogError($"Failed to load mogg file or position: {Bass.LastError}");
Expand Down Expand Up @@ -247,14 +246,30 @@ public void UnloadSong() {
_mixer = null;
}

public void Play() {
public void LoadPreviewAudio(SongEntry song) {
if (song is ExtractedConSongEntry conSong) {
LoadMogg(conSong.MoggInfo, false);
} else {
LoadSong(AudioHelpers.GetSupportedStems(song.Location), false);
}

SetPosition(song.PreviewStartTimeSpan.TotalSeconds);
}

public void Play() => Play(false);

private void Play(bool fadeIn) {
// Don't try to play if there's no audio loaded or if it's already playing
if (!IsAudioLoaded || IsPlaying) {
return;
}

foreach (var channel in _mixer.Channels.Values) {
channel.SetVolume(channel.Volume);
if (fadeIn) {
channel.SetVolume(0);
} else {
channel.SetVolume(channel.Volume);
}
}
if (_mixer.Play() != 0) {
Debug.Log($"Play error: {Bass.LastError}");
Expand All @@ -275,6 +290,19 @@ public void Pause() {
IsPlaying = _mixer.IsPlaying;
}

public void FadeIn() {
Play(true);
if (IsPlaying) {
_mixer?.FadeIn();
}
}

public void FadeOut() {
if (IsPlaying) {
_mixer?.FadeOut();
}
}

public void PlaySoundEffect(SfxSample sample) {
var sfx = _sfxSamples[(int) sample];

Expand Down Expand Up @@ -311,9 +339,7 @@ public double GetVolumeSetting(SongStem stem) {
};
}

public void ApplyReverb(SongStem stem, bool reverb) {
_mixer?.GetChannel(stem)?.SetReverb(reverb);
}
public void ApplyReverb(SongStem stem, bool reverb) => _mixer?.GetChannel(stem)?.SetReverb(reverb);

public double GetPosition() {
if (_mixer is null)
Expand All @@ -322,9 +348,7 @@ public double GetPosition() {
return _mixer.GetPosition();
}

public void SetPosition(double position) {
throw new NotImplementedException();
}
public void SetPosition(double position) => _mixer?.SetPosition(position);

private void OnApplicationQuit() {
Unload();
Expand Down
2 changes: 2 additions & 0 deletions Assets/Script/Audio/Bass/BassHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace YARG {
public static class BassHelpers {

public const float SONG_VOLUME_MULTIPLIER = 0.7f;

public const int FADE_TIME_MILLISECONDS = 2000;

public const int REVERB_SLIDE_IN_MILLISECONDS = 300;
public const int REVERB_SLIDE_OUT_MILLISECONDS = 500;
Expand Down
15 changes: 15 additions & 0 deletions Assets/Script/Audio/Bass/BassMoggStem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public int Load(bool isSpeedUp, float speed) {
return 0;
}

public void FadeIn() {
double volumeSetting = _manager.GetVolumeSetting(Stem);

foreach (int channel in BassChannels) {
Bass.ChannelSetAttribute(channel, ChannelAttribute.Volume, 0);
Bass.ChannelSlideAttribute(channel, ChannelAttribute.Volume, (float)volumeSetting, BassHelpers.FADE_TIME_MILLISECONDS);
}
}

public void FadeOut() {
foreach (int channel in BassChannels) {
Bass.ChannelSlideAttribute(channel, ChannelAttribute.Volume, 0, BassHelpers.FADE_TIME_MILLISECONDS);
}
}

public void SetVolume(double newVolume) {
if (!_isLoaded) {
return;
Expand Down
10 changes: 10 additions & 0 deletions Assets/Script/Audio/Bass/BassStemChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ public int Load(bool isSpeedUp, float speed) {
return 0;
}

public void FadeIn() {
double volumeSetting = _manager.GetVolumeSetting(Stem);
Bass.ChannelSetAttribute(StreamHandle, ChannelAttribute.Volume, 0);
Bass.ChannelSlideAttribute(StreamHandle, ChannelAttribute.Volume, (float)volumeSetting, BassHelpers.FADE_TIME_MILLISECONDS);
}

public void FadeOut() {
Bass.ChannelSlideAttribute(StreamHandle, ChannelAttribute.Volume, 0, BassHelpers.FADE_TIME_MILLISECONDS);
}

public void SetVolume(double newVolume) {
if (StreamHandle == 0) {
return;
Expand Down
25 changes: 25 additions & 0 deletions Assets/Script/Audio/Bass/BassStemMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ public int Play(bool restart = false) {
return 0;
}

public void FadeIn() {
foreach (var channel in Channels.Values) {
channel.FadeIn();
}
}

public void FadeOut() {
foreach (var channel in Channels.Values) {
channel.FadeOut();
}
}

public int Pause() {
if (!IsPlaying) {
return 0;
Expand All @@ -137,6 +149,19 @@ public double GetPosition() {
return LeadChannel.GetPosition();
}

public void SetPosition(double position) {
if (LeadChannel is null) {
return;
}

foreach (var channel in Channels.Values) {
int handle = ((BassStemChannel)channel).StreamHandle;

long channelPosition = Bass.ChannelSeconds2Bytes(handle, position);
BassMix.ChannelSetPosition(handle, channelPosition);
}
}

public int AddChannel(IStemChannel channel) {
if (channel is not BassStemChannel bassChannel) {
throw new ArgumentException("Channel must be of type BassStemChannel");
Expand Down
6 changes: 6 additions & 0 deletions Assets/Script/Audio/Interfaces/IAudioManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using YARG.Serialization;
using YARG.Song;

namespace YARG {
public interface IAudioManager {
Expand Down Expand Up @@ -29,8 +30,13 @@ public interface IAudioManager {
public void LoadMogg(XboxMoggData moggData, bool isSpeedUp);
public void UnloadSong();

public void LoadPreviewAudio(SongEntry song);

public void Play();
public void Pause();

public void FadeIn();
public void FadeOut();

public void PlaySoundEffect(SfxSample sample);

Expand Down
3 changes: 3 additions & 0 deletions Assets/Script/Audio/Interfaces/IStemChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public interface IStemChannel : IDisposable {
public double Volume { get; }

public int Load(bool isSpeedUp, float speed);

public void FadeIn();
public void FadeOut();

public void SetVolume(double newVolume);

Expand Down
5 changes: 5 additions & 0 deletions Assets/Script/Audio/Interfaces/IStemMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ public interface IStemMixer : IDisposable {

public int Play(bool restart = false);

public void FadeIn();
public void FadeOut();

public int Pause();

public double GetPosition();

public void SetPosition(double position);

public int AddChannel(IStemChannel channel);
public int AddMoggChannel(IStemChannel channel, IList<float[]> matrixes);

Expand Down

0 comments on commit c29e3f4

Please sign in to comment.