Skip to content

Commit

Permalink
whammy pitch shift in bass (YARC-Official#487) (YARC-Official#499)
Browse files Browse the repository at this point in the history
Co-authored-by: RileyTheFox <[email protected]>
  • Loading branch information
EliteAsian123 and RileyTheFox authored Jun 27, 2023
1 parent 51290a2 commit 5ff25cd
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Assets/Script/Audio/Bass/BassAudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class BassAudioManager : MonoBehaviour, IAudioManager {
public float CurrentPositionF => (float) GetPosition();
public float AudioLengthF { get; private set; }

[Range(0, 1)]
public float whammyPitchPercent;

public event Action SongEnd {
add {
if (_mixer is null) {
Expand All @@ -55,6 +58,15 @@ public event Action SongEnd {

private IStemMixer _mixer;

private void Update() {
if (_mixer is null || _mixer.Channels.Count == 0) {
return;
}

foreach (var channel in _mixer.Channels) {
channel.Value.SetWhammyPitch(whammyPitchPercent);
}
}

private void Awake() {
SupportedFormats = new[] {
Expand Down
6 changes: 6 additions & 0 deletions Assets/Script/Audio/Bass/BassMoggStemChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public void SetReverb(bool reverb) {
}
}

public void SetWhammyPitch(float percent) {
foreach (var channel in _channels) {
channel.SetWhammyPitch(percent);
}
}

public double GetPosition() {
return _leadChannel.GetPosition();
}
Expand Down
57 changes: 57 additions & 0 deletions Assets/Script/Audio/Bass/BassStemChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,23 @@ public event Action ChannelEnd {

private int _sourceHandle;

private int _pitchFxHandle;
private int _pitchFxReverbHandle;

private bool _isReverbing;
private bool _disposed;

#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
private PitchShiftWindowsParameters _pitchParams = new() {
#else
private PitchShiftParameters _pitchParams = new() {
#endif
fPitchShift = 1,
fSemitones = 0,
lFFTsize = 2048,
lOsamp = 32,
};

public BassStemChannel(IAudioManager manager, string path, SongStem stem) {
_manager = manager;
_path = path;
Expand Down Expand Up @@ -134,6 +148,18 @@ public int Load(float speed) {
Bass.ChannelSetAttribute(StreamHandle, ChannelAttribute.Volume, _manager.GetVolumeSetting(Stem));
Bass.ChannelSetAttribute(ReverbStreamHandle, ChannelAttribute.Volume, 0);

_pitchFxHandle = Bass.ChannelSetFX(StreamHandle, EffectType.PitchShift, 0);

if (_pitchFxHandle == 0) {
Debug.LogError("Failed to add pitchshift: " + Bass.LastError);
}

_pitchFxReverbHandle = Bass.ChannelSetFX(ReverbStreamHandle, EffectType.PitchShift, 0);

if (_pitchFxReverbHandle == 0) {
Debug.LogError("Failed to add pitchshift: " + Bass.LastError);
}

if (!Mathf.Approximately(speed, 1f)) {
// Gets relative speed from 100% (so 1.05f = 5% increase)
float percentageSpeed = Math.Abs(speed) * 100;
Expand Down Expand Up @@ -249,6 +275,37 @@ public void SetReverb(bool reverb) {
}
}

private float lastShift;

public void SetWhammyPitch(float percent) {
if (Stem != SongStem.Guitar && Stem != SongStem.Bass && Stem != SongStem.Rhythm)
return;

percent = Mathf.Clamp(percent, 0f, 1f);

float shift = Mathf.Pow(2, (-2 * percent) / 12);

if (Math.Abs(shift - lastShift) < float.Epsilon) {
return;
}

lastShift = shift;

Debug.Log(shift);

_pitchParams.fPitchShift = shift;

Debug.Log(_pitchFxHandle);

if (!Bass.FXSetParameters(_pitchFxHandle, _pitchParams)) {
Debug.LogError("Failed to set params (normal fx): " + Bass.LastError);
}

if (!Bass.FXSetParameters(_pitchFxReverbHandle, _pitchParams)) {
Debug.LogError("Failed to set params (reverb fx): " + Bass.LastError);
}
}

public double GetPosition() {
return Bass.ChannelBytes2Seconds(StreamHandle, Bass.ChannelGetPosition(StreamHandle));
}
Expand Down
41 changes: 41 additions & 0 deletions Assets/Script/Audio/Bass/PitchShiftWindowsParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Runtime.InteropServices;
using ManagedBass;

/// <summary>
/// Parameters for PitchShift Effect.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public class PitchShiftWindowsParameters : IEffectParameter
{
/// <summary>
/// A factor value which is between 0.5 (one octave down) and 2 (one octave up) (1 won't change the pitch, default).
/// </summary>
public float fPitchShift;

/// <summary>
/// Semitones (0 won't change the pitch). Default = 0.
/// </summary>
public float fSemitones;

/// <summary>
/// Defines the FFT frame size used for the processing. Typical values are 1024, 2048 (default) and 4096, max is 8192.
/// </summary>
/// <remarks>It may be any value up to 8192 but it MUST be a power of 2.</remarks>
public int lFFTsize;

/// <summary>
/// Is the STFT oversampling factor which also determines the overlap between adjacent STFT frames. Default = 8.
/// </summary>
/// <remarks>It should at least be 4 for moderate scaling ratios. A value of 32 is recommended for best quality (better quality = higher CPU usage).</remarks>
public int lOsamp;

/// <summary>
/// A <see cref="FXChannelFlags" /> flag to define on which channels to apply the effect. Default: <see cref="FXChannelFlags.All"/>
/// </summary>
public FXChannelFlags lChannel = FXChannelFlags.All;

/// <summary>
/// Gets the <see cref="EffectType"/>.
/// </summary>
public EffectType FXType => EffectType.PitchShift;
}
3 changes: 3 additions & 0 deletions Assets/Script/Audio/Bass/PitchShiftWindowsParameters.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Assets/Script/Audio/Interfaces/IStemChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface IStemChannel : IDisposable {

public void SetReverb(bool reverb);

public void SetWhammyPitch(float percent);

public double GetPosition();
public void SetPosition(double position);

Expand Down

0 comments on commit 5ff25cd

Please sign in to comment.