Search Unity

Microphone input not working: GetPosition hangs

Discussion in 'Audio & Video' started by drhawley, Mar 15, 2016.

  1. drhawley

    drhawley

    Joined:
    Mar 9, 2016
    Posts:
    1
    I'm new and following a few tutorials on getting basic microphone input working, but....it doesn't.

    Notably,
    https://support.unity3d.com/hc/en-u...-to-playback-a-Microphone-input-in-real-time-
    ...but the code
    Code (CSharp):
    1. while (!(Microphone.GetPosition(null) > 0)){}
    ...causes the system to hang. I have the "Built-in Microphone" enabled on my Mac, and it's the first device on the list of devices, so "null" is appropriate, but GetPosition never changes state. What am I missing?

    I saw a post about needing to get user permission even for standalone apps...
    http://answers.unity3d.com/questions/633084/mac-built-in-microphone-not-working.html
    ...so I inserted the relevant code, but when I run, no dialog box appears asking for permission.

    It seems from my searching around, that many people are using code by Kappine at...
    http://www.kaappine.fi/tutorials/using-microphone-input-in-unity3d/
    ...so here is my code, copied from that with a couple other diagnostic lines added (added because the original code just hung as mentioned earlier). I have this script attached to an empty GameObject which then has an AudioSource linked to it. It compiles and "runs" but, yeah, gets stuck.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5. public class MicrophoneInput : MonoBehaviour {
    6.     public float sensitivity = 100;
    7.     public float loudness = 0;
    8.  
    9.     bool playing = false;
    10.  
    11.     void Start() {
    12.         GetComponent<AudioSource>().clip = Microphone.Start(null, true, 10, 44100);
    13.         GetComponent<AudioSource>().loop = true; // Set the AudioClip to loop
    14.         GetComponent<AudioSource>().mute = true; // Mute the sound, we don't want the player to hear it
    15.          while(!(Microphone.GetPosition(null)>0)){//Waituntiltherecordinghas started
    16.         //also doesn't work: // while(GetComponent<AudioSource>().clip.loadState!=AudioDataLoadState.Loaded){
    17.              /*if(Microphone.GetPosition(null)<=0){
    18.                  print("waiting...");
    19.                return;
    20.                }*/
    21.          }
    22.         GetComponent<AudioSource>().Play(); // Play the audio source!
    23.         playing = true;
    24.     }
    25.  
    26.     void Update(){
    27.         loudness = GetAveragedVolume() * sensitivity;
    28.     }
    29.  
    30.  
    31.     float GetAveragedVolume()
    32.     {
    33.         float[] data = new float[256];
    34.         float a = 0;
    35.         GetComponent<AudioSource>().GetOutputData(data,0);
    36.         foreach(float s in data)
    37.         {
    38.             a += Mathf.Abs(s);
    39.         }
    40.         return a/256;
    41.     }
    42. }
    43.  
    Any help would be appreciated.

    For what it's worth: The intended application is an educational utility for me and a few physics students, to make a pitch-tracking strobelight to use for demos & experiments. Unity is just of a few platforms we're considering for implementing this (e.g., Max for Live, or WebGL+WebAudio). Not going to be making any money off this, so we'd rather learn to write the code ourselves than buy some $$ asset. ;-)

    Details: Unity version 5.3.3f1 Personal, Mac OS X 10.10.5.
     
    Last edited: Mar 15, 2016