Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by maddogedge · Apr 25, 2020 at 03:08 AM · updatesoundaudiosourcemicrophonegetspectrumdata

GetSpectrumData doesn't update

Hi,

Thanks for looking at this post! I'm a beginner looking for help in getting a Microphone / GetSpectrumData code working.

In 2017 I made a simple script to get microphone audio and use GetSpectrumData. The code worked fine at the time.

Come 2020, I've updated to the latest version of Unity but I can no longer get the code to run. After crawling through various official guides and forums it seems like my code should run ok, but it is not working.

Specifically, there are no run errors, but the output variable spectrum is not updating each update with dynamic values. Instead _spectrum returns with the same data roughly following a power-law (and not resembling the audio - see cyan line in image). This suggests to be the GetSpectrumData part of the code is working, but perhaps my Microphone is not working?

alt text

Here's the code. I'd appreciate any advice on how to fix it or how to debug it further.

Thanks in advance!! best, Rob

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [RequireComponent(typeof(AudioSource))]
 
 public class Notable : MonoBehaviour {
 
     public AudioSource _audioSrc;
     public float[] _spectrum = new float[512];
 
     // Use this for initialization
     void Start () {
 
         foreach (string device in Microphone.devices) {
                         Debug.Log ("Name: " + device);
                 }
 
         if (_audioSrc == null)
             _audioSrc = GetComponent<AudioSource>();
         
         if (_audioSrc == null) 
             _audioSrc = gameObject.AddComponent<AudioSource>();
         
         _audioSrc.clip = Microphone.Start("Built-in Microphone", true, 1, 44100);
         while (!(Microphone.GetPosition(null) > 0)) { } // this line removes lag.
         _audioSrc.loop = true;
         _audioSrc.Play();
 
     }
 
     // Update is called once per frame
     void Update () {
 
         Debug.Log ("Spec0 is "+ _spectrum[0]);
         _audioSrc.GetSpectrumData(_spectrum,0,FFTWindow.Rectangular);
         Debug.Log ("SpecUpdt is "+ _spectrum[0]);
 
         int i = 1;
         while ( i < _spectrum.Length-1) {
             Debug.DrawLine( new Vector3(i-1,_spectrum[i]+10,0), new Vector3(i, _spectrum[i+1] + 10, 0),Color.red);
             Debug.DrawLine( new Vector3(i-1,Mathf.Log(_spectrum[i-1])+10,2), new Vector3(i,Mathf.Log(_spectrum[i]) + 10,2),Color.cyan);
             Debug.DrawLine( new Vector3(Mathf.Log(i-1),_spectrum[i-1]-10,1), new Vector3(Mathf.Log(i),_spectrum[i]-10,1),Color.green);
             Debug.DrawLine( new Vector3(Mathf.Log(i-1),Mathf.Log (_spectrum[i-1]),3), new Vector3(Mathf.Log(i),Mathf.Log (_spectrum[i]),3),Color.yellow);
             i++;
         }
 
     }
     
 }
 


screenshot-2020-04-24-at-220453.png (258.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by maddogedge · Apr 25, 2020 at 04:18 PM

Problem solved.

I had to do two things.

First I had to add mic permissions for unityhub https://forum.unity.com/threads/microphone-audio-is-silent.786497/

Secondly I had to unmute the microphone to get the audio. I'm now dealing with this aspect of it (using the Mixer solution.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Bunny83 · Apr 25, 2020 at 10:42 AM

I don't see anything wrong with your code or your result. You do know that GetSpectrumData does not return the actual audio data but that it calculates the fast fourier transform of the audio? You print the first element of the spectrum which is pretty pointless since the first element is 0Hz, so the DC content of your signal which should be around 0 anyways. Also note that you only analyse the first channel of the audio source. Depending on the microphone and the audio hardware / driver it's possible that one channel is empty since microphones are essentially mono in nature. Usually the mono signal is either distributed evenly across both channels or just a single channel or a more expensive microphone might have two or more actual microphones and record actual stereo.


Have you tried inspecting GetOutputData? If the actual PCM signal is there, the FFT (GetSpectrumData) should work as expected. I haven't played around with FFTs for a while. However I've implemented my own FFT some time ago and I got the same result as Unity's GetSpectrumData. Though my routine actually provides the FFT as a complex number array (as it's meant to be).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maddogedge · Apr 25, 2020 at 03:14 PM 0
Share

Thanks a lot for your reply. GetOutputData returns all zeros! I've had a look at other mic channels but no joy.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get microphone FFT input without playback 2 Answers

Analyze Frequency from Microphone Jack only 0 Answers

Calling sound once in update 1 Answer

Mac built-in microphone stopped sending data to Unity. 0 Answers

Microphone input volume to axis value translation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges