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

AudioClip:GetData invalid parameter

Discussion in 'Audio & Video' started by Hindi, Jun 3, 2015.

  1. Hindi

    Hindi

    Joined:
    Apr 14, 2013
    Posts:
    59
    I'm trying to obtain a real time audio feedback on a microphone. The code above is working but after a while I'm having the following error :

    I didn't find much about those errors, any idea ?

    Code (CSharp):
    1.  
    2.   public void restart()
    3.   {
    4.     microphone_name = DEFAULT_DEVICE_NAME;
    5.  
    6.     int i = 0;
    7.     foreach(var d in Microphone.devices)
    8.     {
    9.       if(i++ == microphone_index)
    10.       {
    11.         microphone_name = d;
    12.  
    13.       }
    14.     }
    15.     audio.clip = Microphone.Start(microphone_name, false, 100, 44100);
    16.     audio.Play();
    17.     play();
    18.   }
    19.  
    20.  
    21.   void Update ()
    22.   {
    23.     int newOffset = Microphone.GetPosition(microphone_name);
    24.       int nbSamplesToRetrieve;
    25.       if (newOffset < sampleOffset)
    26.           nbSamplesToRetrieve = newOffset + audio.clip.samples - sampleOffset;
    27.       else
    28.           nbSamplesToRetrieve = newOffset - sampleOffset;
    29.       float[] samples = new float[nbSamplesToRetrieve];
    30.       float sampleRate = audio.clip.frequency;
    31.  
    32.     audio.clip.GetData(samples, sampleOffset);
    33.  
    34.       var pitch = autoCorrelate( samples, sampleRate );
    35.  
    36.        if (pitch != NOT_ENOUGH_DATA)
    37.        {
    38.           sampleOffset = newOffset;
    39.            if (pitch >= 0)
    40.            {
    41.                //detected a pitch
    42.                currentPitch = pitch;
    43.            }
    44.            else
    45.            {
    46.                //did not detect anything
    47.                currentPitch = -1;
    48.            }
    49.       }
    50.   }
     
  2. IvanAlvarez

    IvanAlvarez

    Joined:
    Apr 30, 2013
    Posts:
    9
    Any solutions for this?