Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Audio Adding AudioMixer, AudioMixer groups programatically or any alternative

Discussion in 'Audio & Video' started by JMota7, Apr 5, 2023.

  1. JMota7

    JMota7

    Joined:
    Nov 15, 2017
    Posts:
    6
    Hi everyone,

    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?

    Thanks a lot!
     
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    305
    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.

    Code (CSharp):
    1. [RequireComponent(typeof(AudioSource))]
    2. public class LinkToPitchManager: MonoBehaviour
    3. {
    4.     public PitchManager pitchManager; // could be a singleton, ScriptableObject, something set manually, etc..
    5.  
    6.     private void OnEnable()
    7.     {
    8.         pitchManager.Register(GetComponent<AudioSource>());
    9.     }
    10.  
    11.     public void Unregister(AudioSource source)
    12.     {
    13.         pitchManager.Unregister(GetComponent<AudioSource>());
    14.     }
    15. }
    16.  
    17. public class PitchManager : MonoBehaviour
    18. {
    19.     private List<AudioSource> audioSources = new List<AudioSource>();
    20.  
    21.     public void Register(AudioSource source)
    22.     {
    23.         if (!audioSources.Contains(source))
    24.         {
    25.             audioSources.Add(source);
    26.         }
    27.     }
    28.  
    29.     public void Unregister(AudioSource source)
    30.     {
    31.         if (audioSources.Contains(source))
    32.         {
    33.             audioSources.Remove(source);
    34.         }
    35.     }
    36.  
    37.     public void UpdatePitch(float newPitch)
    38.     {
    39.         foreach (AudioSource source in audioSources)
    40.         {
    41.             source.pitch = newPitch;
    42.         }
    43.     }
    44. }
     
  3. jmota_vbw

    jmota_vbw

    Joined:
    Nov 16, 2022
    Posts:
    1
    Thanks for your answer!

    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...
     
    Last edited: Apr 13, 2023
  4. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    305
    Mmmmm... yes...

    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.

    *evil laughter heard in distant tunnels*
     
    jmota_vbw likes this.