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. Dismiss Notice

Audio Spectrum Data for System Audio

Discussion in 'Audio & Video' started by falkenbrew, Nov 16, 2021.

  1. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    Code (CSharp):
    1.  
    2.  
    3.     public AudioMixerGroup mixer;
    4.     const int sampleWindow = 512;
    5.  
    6.     float[] samples = new float[sampleWindow];
    7.  
    8. void InitListenToLocal() {
    9.  
    10.         Debug.Log("Init Microphone Spectrum");
    11.  
    12.         // Built-in Microphone is the first device found.
    13.         for (var i = 0; i <Microphone.devices.Length; i++)
    14.         {
    15.             string device = Microphone.devices[i];
    16.             //log found devices
    17.             Debug.Log(i+" -> " + device);
    18.         }
    19.  
    20.         if (audioSrc == null)
    21.             audioSrc = GetComponent<AudioSource>();
    22.         if (audioSrc == null)
    23.             audioSrc = gameObject.AddComponent<AudioSource>();
    24.  
    25.         string activeDevice = Microphone.devices[1];
    26.         audioSrc.clip = Microphone.Start(activeDevice, true, 1, 44100);
    27.         int micPos, k=0;
    28.         //this supposedly reduces lag
    29.         do {
    30.             micPos = Microphone.GetPosition(activeDevice);
    31.         } while (micPos<=0 && k++<100000);
    32.         Debug.Log("MIC POS " + micPos + " (" + k + ")");
    33.  
    34.         audioSrc.loop = true;
    35.         audioSrc.Play();
    36.         audioSrc.outputAudioMixerGroup = mixer;
    37.     }
    38.  
    39. void Update(){
    40.      audioSrc.GetSpectrumData(samples, 0, FFTWindow.BlackmanHarris);
    41.      ....
    42. }
    43.  
    This is my basic code to get what is running on the system. I use the Microphone class to stream a system device to an AudioClip connected to an AudioSource. This AudioSource is then played to an AudioMixerGroup (I call System) withing another AudioMixerGroup (I call Silent) where I have set the Attenuation to -80db. Otherwise I hear the System Audio as an Echo.

    This all works finally, but there is very noticeable lag. Is there a more direct way I can get spectral data from system audio?? I think buffering system audio into an audioclip and then playing it adds noticeable lag to the visualization and that's just not good enough.
     
    svfxanimationstudio likes this.
  2. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    Someone else just had the same problem and I gave them pretty much the answer I am using here.

    I'm now piping Spotify through "Virtual Audio Cable" https://vac.muzychenko.net/en/download.htm
    This allows me to grab the stream from a virtual device that is not actually putting out any sound. I can then play it myself from Unit and my visuals are perfectly in synch with my audio.

    https://dc5vkde3nc4sk.cloudfront.net/bug/audioRumbling211118_2.mp4

    Only problem is that the audio quality is reduced/cracks/rumbles and I have no idea why.
     
    svfxanimationstudio likes this.
  3. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    Seems like the cracking disappears, or at least is not as frequent if lengthSec parameter is increased. Currently using 10 and it's not cracking as far as I can tell.
    Code (CSharp):
    1. Microphone.Start(activeDevice, true, 10, 44100);
     
    svfxanimationstudio likes this.
  4. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    Ok, not true. I did notice some issues still, just much less frequent. I increased to 60 seconds and this is better, but the sounds starts a lot later. After a while I did notice a problem here too while piping spotify through the app. It suddenly jumped back about 10 seconds with some cracking, I'm guessing there is some issue with the looping, but I have no idea what I could do about it. I'm assuming all the cracking I have heard is related to this problem.
     
    svfxanimationstudio likes this.
  5. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    I've created a bug report and hope this problem will be solved