I want to modify the pitch from multiple AudioSources in a scene, these AudioSources are attached to GameObjects that are instantiated and the AudioSource components are attached programatically. So, after struggling with this for a while, I tried to use Unity AudioMixer and its groups, but I could not instantiate at runtime those AudioMixers and after reading the API, it seems that it is on purpose.
So, I would like to change the pitch programmatically without altering any AudioClip. Is that possible using AudioMixers? or using an alternative?
I’d suggest you have a kind of PitchManager (or any relevant audio management class in your context) to which your sources would subscribe/unsubscribe in their Enable/Disable methods.
[RequireComponent(typeof(AudioSource))]
public class LinkToPitchManager: MonoBehaviour
{
public PitchManager pitchManager; // could be a singleton, ScriptableObject, something set manually, etc..
private void OnEnable()
{
pitchManager.Register(GetComponent<AudioSource>());
}
public void Unregister(AudioSource source)
{
pitchManager.Unregister(GetComponent<AudioSource>());
}
}
public class PitchManager : MonoBehaviour
{
private List<AudioSource> audioSources = new List<AudioSource>();
public void Register(AudioSource source)
{
if (!audioSources.Contains(source))
{
audioSources.Add(source);
}
}
public void Unregister(AudioSource source)
{
if (audioSources.Contains(source))
{
audioSources.Remove(source);
}
}
public void UpdatePitch(float newPitch)
{
foreach (AudioSource source in audioSources)
{
source.pitch = newPitch;
}
}
}
Sorry, but I didn’t explain myself properly. The thing is, I’m trying to change the pitch of an audio signal without changing its speed. So, as far as I know, this needs a pitch shifter to not alter the speed of the audio instead of changing it through the AudioSource. Does there exist a solution for this?
I’m starting to use FMOD, but I don’t know if I’m using a sledgehammer to crack a nut…
You might have to stretch out before getting on this!
I don’t think there is anything like this on the Asset Store. The shortest path I’d see is writing a C++ DLL wrapping an external audio library (SoundTouch?) and then exposing it so it alters your audio data in OnAudioFilterRead or as an AudioMixer native audio plugin.