Search Unity

Audio Detect Microphone 2019.1.7f1

Discussion in 'Audio & Video' started by robert-durnin, Jul 5, 2019.

  1. robert-durnin

    robert-durnin

    Joined:
    Nov 21, 2018
    Posts:
    5
    I have been trying to use microphone and line-in as audio inputs to the Unity Editor (for a standalone application), and have so far been unable to start the microphone.

    Starting microphone failed: "Error initializing output device. " (60)
    UnityEngine.Microphone:Start(String, Boolean, Int32, Int32)

    The script is able to detect the connected devices, and to discover any information which can be queried (device caps, is playing, etc), but fails to start. The input is stereo, and I have attempted to send the audio through a virtual device (Voicemeeter Banana) as mono, but with the same result.

    I am using Windows 10, Unity 2019.1.7f1, and NVIDIA HD Audio (1.3.38.16).

    Script Logic C#:

    void Start ( )
    {
    AudioSource audio = GetComponent<AudioSource>();
    foreach (var m in Microphone.devices)
    {
    Debug.Log("PlayMicrophone: Start: Device found: " + m);
    }

    int minFreq;
    int maxFreq;
    var Device = Microphone.devices[0];
    Debug.Log("PlayMicrophone: Start: Microphone devices: " + Device);
    Microphone.GetDeviceCaps(Device, out minFreq, out maxFreq);

    Debug.Log("PlayMicrophone: Start: Device minFreq: " + minFreq);
    Debug.Log("PlayMicrophone: Start: Device maxFreq: " + maxFreq);

    Debug.Log("PlayMicrophone: Start: Device IsRecording: " + Microphone.IsRecording(Device));

    var targetFrequency = 2;
    var targetSampleSize = 1024;
    var recordFrequency = minFreq == 0 && maxFreq == 0 ? 44100: maxFreq;
    var recordSampleSize = recordFrequency / targetFrequency / targetSampleSize;
    Debug.Log("PlayMicrophone: Start: Microphone recordSampleSize: " + recordSampleSize);
    var sampleBuffer = new float[recordSampleSize];

    var clip = Microphone.Start(Device, true, 1, recordFrequency);
    // while (!(Microphone.GetPosition(null) > 0)){}
    // Debug.Log("start playing... position is " + Microphone.GetPosition(null));
    // audio.Play();
    }