Search Unity

Audio Microphone -> GetSpectrumData not updating?

Discussion in 'Audio & Video' started by maddogedge, Apr 22, 2020.

  1. maddogedge

    maddogedge

    Joined:
    Apr 22, 2020
    Posts:
    5
    Hi everyone,

    Noob here, and I'd like to get your help on GetSpectrumData using a microphone input.

    In short summary, when calling GetSpectrumData, the array is not updating.

    In 2017 I made a short .cs code which accessed audio from a microphone and took a spectrum of the signal. The code worked great at the time, but now its 2020 and I've updated Unity to the latest version (2019.3.9f1), the code no longer works...

    GetSpectrumData seems to have changed (no longer returns spectrum array for example) so I have tried to adapt my code for these new changes, but I still can't get it to work.

    I'm not getting any bugs, but the spectrum is not updating more than the first update. The first spectrum produced also does not seem to pertain to the microphone audio recorded.

    I think therefore the problem is I cannot get audio from the microphone.

    It's highly likely I've made a basic error given I am new to CS and Unity.

    Here's the code.
    Any suggestions on how to further debug this would be appreciated.
    Would love to know your thoughts!

    Thanks, Rob


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [RequireComponent(typeof(AudioSource))]
    6.  
    7. public class Notable : MonoBehaviour {
    8.  
    9.     public AudioSource _audioSrc;
    10.     public float[] _spectrum = new float[512];
    11.  
    12.     void Start () {
    13.  
    14.         foreach (string device in Microphone.devices) {
    15.                         Debug.Log ("Name: " + device);
    16.                 }
    17.         // Built-in Microphone is the first device found.
    18.  
    19.         if (_audioSrc == null)
    20.             _audioSrc = GetComponent<AudioSource>();
    21.         if (_audioSrc == null)
    22.             _audioSrc = gameObject.AddComponent<AudioSource>();
    23.  
    24.         _audioSrc.clip = Microphone.Start("Built-in Microphone", true, 1, 44100);
    25.         while (!(Microphone.GetPosition(null) > 0)) { } // this line removes lag.
    26.         _audioSrc.loop = true;
    27.         _audioSrc.Play();
    28.  
    29.     }
    30.  
    31.  
    32.     void Update () {
    33.         _audioSrc.GetSpectrumData(_spectrum,0,FFTWindow.BlackmanHarris);
    34.        // printing _spectrum here gives the same decimal values each time.
    35.     }
    36.  
    37. }
    38.  
     
    Last edited: Apr 22, 2020
  2. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    Was just looking to do something similar. Replace "Built-in Microphone" with Microphone.devices[0].
    What did you base the rest of your code on?
    edit: Necro... it's 2021 now...