Search Unity

Capturing Microphone Volume

Discussion in 'Scripting' started by Cclark31, Feb 7, 2018.

  1. Cclark31

    Cclark31

    Joined:
    Feb 7, 2018
    Posts:
    5
    I'm trying to make a single sphere grow and shrink in size based off of the microphone input volume. I can get it to grow but not to shrink. I'm pretty new at this so any help would be much appreciated.

    This is my script for the size:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Size : MonoBehaviour {
    6.    
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.  
    17.  
    18.         transform.localScale += new Vector3 (MicInput.MicLoudness, MicInput.MicLoudness, MicInput.MicLoudness);
    19.  
    20.  
    21.     }
    22. }
    23.  

    And this is the script that it is referencing:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MicInput : MonoBehaviour {
    6.  
    7.         public static float MicLoudness;
    8.  
    9.         private string _device;
    10.  
    11.         //mic initialization
    12.         void InitMic(){
    13.             if(_device == null) _device = Microphone.devices[0];
    14.             _clipRecord = Microphone.Start(_device, true, 999, 44100);
    15.         }
    16.  
    17.         void StopMicrophone()
    18.         {
    19.             Microphone.End(_device);
    20.         }
    21.  
    22.  
    23.         AudioClip _clipRecord = new AudioClip();
    24.         int _sampleWindow = 128;
    25.  
    26.         //get data from microphone into audioclip
    27.         float  LevelMax()
    28.         {
    29.             float levelMax = 0;
    30.             float[] waveData = new float[_sampleWindow];
    31.             int micPosition = Microphone.GetPosition(null)-(_sampleWindow+1); // null means the first microphone
    32.             if (micPosition < 0) return 0;
    33.             _clipRecord.GetData(waveData, micPosition);
    34.             // Getting a peak on the last 128 samples
    35.             for (int i = 0; i < _sampleWindow; i++) {
    36.                 float wavePeak = waveData[i] * waveData[i];
    37.                 if (levelMax < wavePeak) {
    38.                     levelMax = wavePeak;
    39.                 }
    40.             }
    41.             return levelMax;
    42.         }
    43.  
    44.  
    45.  
    46.         void Update()
    47.         {
    48.             // levelMax equals to the highest normalized value power 2, a small number because < 1
    49.             // pass the value to a static var so we can access it from anywhere
    50.             MicLoudness = LevelMax ();
    51.         }
    52.  
    53.         bool _isInitialized;
    54.         // start mic when scene starts
    55.         void OnEnable()
    56.         {
    57.             InitMic();
    58.             _isInitialized=true;
    59.         }
    60.  
    61.         //stop mic when loading a new level or quit application
    62.         void OnDisable()
    63.         {
    64.             StopMicrophone();
    65.         }
    66.  
    67.         void OnDestroy()
    68.         {
    69.             StopMicrophone();
    70.         }
    71.  
    72.  
    73.         // make sure the mic gets started & stopped when application gets focused
    74.         void OnApplicationFocus(bool focus) {
    75.             if (focus)
    76.             {
    77.                 Debug.Log("Focus");
    78.  
    79.                 if(!_isInitialized){
    80.                     Debug.Log("Init Mic");
    81.                     InitMic();
    82.                     _isInitialized=true;
    83.                 }
    84.             }    
    85.             if (!focus)
    86.             {
    87.                 Debug.Log("Pause");
    88.                 StopMicrophone();
    89.                 Debug.Log("Stop Mic");
    90.                 _isInitialized=false;
    91.  
    92.             }
    93.         }
    94.     }
    95.  
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Try assigning the value to localScale rather than " += ".
     
  3. Cclark31

    Cclark31

    Joined:
    Feb 7, 2018
    Posts:
    5
    Sorry, so what would that look like? The syntax has been killing me
     
  4. Cclark31

    Cclark31

    Joined:
    Feb 7, 2018
    Posts:
    5
    Oh my god I got it. Just 1 "=". Thank you so much for you help!
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    :) no problem.
     
  6. rahulpatil6220375

    rahulpatil6220375

    Joined:
    Dec 10, 2018
    Posts:
    19
    hello sir it is give an error please help
     
    Last edited: Jan 3, 2019