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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Audio I can't find a working way to capture the microphone input volume (2019.1)

Discussion in 'Audio & Video' started by ArnoBen, May 16, 2019.

  1. ArnoBen

    ArnoBen

    Joined:
    Apr 24, 2019
    Posts:
    13
    Greetings,
    I've been looking for 1 hour on how to get the mic input volume but every single solution I found does not work for some reason.
    The most popular script by far is this one :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class MicInput : MonoBehaviour {
    5.         public static float MicLoudness;
    6.         private string _device;
    7.         //mic initialization
    8.         void InitMic(){
    9.             if(_device == null) _device = Microphone.devices[0];
    10.             _clipRecord = Microphone.Start(_device, true, 999, 44100);
    11.         }
    12.         void StopMicrophone()
    13.         {
    14.             Microphone.End(_device);
    15.         }
    16.         AudioClip _clipRecord = new AudioClip();
    17.         int _sampleWindow = 128;
    18.         //get data from microphone into audioclip
    19.         float  LevelMax()
    20.         {
    21.             float levelMax = 0;
    22.             float[] waveData = new float[_sampleWindow];
    23.             int micPosition = Microphone.GetPosition(null)-(_sampleWindow+1); // null means the first microphone
    24.             if (micPosition < 0) return 0;
    25.             _clipRecord.GetData(waveData, micPosition);
    26.             // Getting a peak on the last 128 samples
    27.             for (int i = 0; i < _sampleWindow; i++) {
    28.                 float wavePeak = waveData[i] * waveData[i];
    29.                 if (levelMax < wavePeak) {
    30.                     levelMax = wavePeak;
    31.                 }
    32.             }
    33.             return levelMax;
    34.         }
    35.         void Update()
    36.         {
    37.             // levelMax equals to the highest normalized value power 2, a small number because < 1
    38.             // pass the value to a static var so we can access it from anywhere
    39.             MicLoudness = LevelMax ();
    40.         }
    41.         bool _isInitialized;
    42.         // start mic when scene starts
    43.         void OnEnable()
    44.         {
    45.             InitMic();
    46.             _isInitialized=true;
    47.         }
    48.         //stop mic when loading a new level or quit application
    49.         void OnDisable()
    50.         {
    51.             StopMicrophone();
    52.         }
    53.         void OnDestroy()
    54.         {
    55.             StopMicrophone();
    56.         }
    57.         // make sure the mic gets started & stopped when application gets focused
    58.         void OnApplicationFocus(bool focus) {
    59.             if (focus)
    60.             {
    61.                 Debug.Log("Focus");
    62.                 if(!_isInitialized){
    63.                     Debug.Log("Init Mic");
    64.                     InitMic();
    65.                     _isInitialized=true;
    66.                 }
    67.             }  
    68.             if (!focus)
    69.             {
    70.                 Debug.Log("Pause");
    71.                 StopMicrophone();
    72.                 Debug.Log("Stop Mic");
    73.                 _isInitialized=false;
    74.             }
    75.         }
    76.     }
    But as soon as I paste it, I get an error on the line 16 because there are no AudioClip constructor that doesn't take arguments.
    I have tried to simply remove the "= new AudioClip()" part and the Microphone.MicLoudness is just gibberish, it absolutely doesn't reflect my microphone volume, and yes I am sure I'm using the right microphone, it's the default one. I don't even know if it's because of the AudioClip() error. Is this related to 2019.1 ?

    Thank you in advance for any help, it's becoming frustrating to take this long for what seems to be an absolutely trivial need.
     
  2. ArnoBen

    ArnoBen

    Joined:
    Apr 24, 2019
    Posts:
    13
    Ok so it appears that the problem comes from the fact that I'm using Google Cloud Speech recognition which gets in conflict with this script getting the microphone. I now have to find out how to get the mic volume inside this mess...

    Also, without GCSpeechRecognition it works fine when I simply remove the "= new AudioClip()" part.
     
    marief_unity likes this.