Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Cannot GetData from clip recorded from the microphone.

Discussion in 'Developer Preview Archive' started by PlanetTimmy, Dec 22, 2011.

  1. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Hi folks,
    I've been trying to capture data from the microphone, and I wish to analyse it. Using the new Microphone class I can record an audio clip, and I can play it back, but when I try and get the data from the clip, it never fills the array that I pass to it. Here is some code to demonstrate the behaviour:


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestMicRecord: MonoBehaviour
    5. {
    6.     string m_micSelected = "";
    7.     AudioClip m_clip;
    8.    
    9.     float m_volume = 0;
    10.    
    11.     IEnumerator RecordPlayAnalyse()
    12.     {
    13.         // record a two-second clip
    14.         m_clip = Microphone.Start(m_micSelected, false, 2, 22050);
    15.         yield return null;
    16.        
    17.         while (Microphone.IsRecording(m_micSelected))
    18.         {
    19.             yield return null;
    20.         }
    21.        
    22.         // play the clip back
    23.         audio.clip = m_clip;
    24.         audio.Play();
    25.         yield return null;
    26.        
    27.         while (audio.isPlaying)
    28.         {
    29.             yield return null;
    30.         }
    31.        
    32.        
    33.         float[] samples = new float[m_clip.samples];
    34.         // Uncomment next section to pre-fill array with 0.5
    35.         /*
    36.         for (int i = 0; i < m_clip.samples; ++i)
    37.         {
    38.             m_samples[i] = 0.5f;
    39.         }
    40.         */
    41.        
    42.         // calculate the RMS volume
    43.         m_clip.GetData(samples, 0);
    44.        
    45.         float totalSquared = 0.0f;
    46.         foreach(float sample in samples)
    47.         {
    48.             totalSquared += sample * sample;
    49.         }
    50.         float meanSquared = totalSquared / samples.Length;
    51.         m_volume = Mathf.Sqrt(meanSquared);
    52.     }
    53.    
    54.     void PresentMicChoice()
    55.     {
    56.        
    57.         foreach(string device in Microphone.devices)
    58.         {
    59.             if (GUILayout.Button("Start recording with " + device))
    60.             {
    61.                 m_micSelected = device;
    62.                 StartCoroutine(RecordPlayAnalyse());
    63.             }
    64.         }
    65.         GUILayout.Label("End");
    66.     }
    67.    
    68.     void OnGUI()
    69.     {
    70.         if (m_micSelected == "")
    71.         {
    72.             PresentMicChoice();
    73.         }
    74.         else
    75.         {
    76.             if (Microphone.IsRecording(m_micSelected))
    77.             {
    78.                 GUILayout.Label("Recording");
    79.             }
    80.             else if (audio.isPlaying)
    81.             {
    82.                 GUILayout.Label("Playing");
    83.             }
    84.             else
    85.             {
    86.                 GUILayout.Label("RMS Volume of sample: " + m_volume.ToString());
    87.             }
    88.         }      
    89.     }
    90. }
    91.  
    If you put this on an object along with an audio source component and run, the program should record two seconds worth of audio, play it back, and then calculate the RMS volume of the clip. However, the call to GetData never fills in any data. If I pre-fill my array with 0.5, the array is returned unchanged.

    Am I doing something wrong?

    I managed to get some sample data from a loaded-in clip.

    Cheers,

    Tim
     
  2. soren

    soren

    Joined:
    Feb 18, 2008
    Posts:
    123
    Hi Tim,
    It's a bug and will be fixed in the final 3.5 release. Sorry for the inconvenience!

    cheers,
    Søren
     
  3. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Thanks, Søren!
     
  4. rstorm000

    rstorm000

    Joined:
    Aug 19, 2010
    Posts:
    214
    You should be able to pass this sample data as a series of floats across the network and recreate it on the other side for voice communication then right? But you would need GetData() ?

    if so I would love to see this fix as soon as possible! =)

    Is there another way to do this with the current microphone recording?
     
  5. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,293
    No data here as well.
     
  6. echologin

    echologin

    Joined:
    Apr 11, 2010
    Posts:
    1,070
    Does anyone know if you use prime31's mic record plugin, can that loaded AudioClip work with GetData ?
    ( thought id ask b4 i buy the plugin, just to be able to keep working until this is fixed )
     
  7. Logistic-Win

    Logistic-Win

    Joined:
    Apr 4, 2009
    Posts:
    107
    is there any chance that this bug will be (or possibly can be) fixed before the final release? Is there anyway to tap on the shoulders of the Unity Dev Team to get a little feedback about the status of iOS Microphone Input functionality and possibly some pointers to the documentation for it? This is a big deal for me and ANYTHING that can get this particular feature working and in our hands sooner would be very much appreciated. I know that I am one of MANY developers that could use some focus put on this particular subject.
     
  8. Ayrik

    Ayrik

    Joined:
    Aug 31, 2008
    Posts:
    427
    The plugin simply creates a wav file, which you have to load with WWW (using file://) to get the AudioClip.
     
  9. soren

    soren

    Joined:
    Feb 18, 2008
    Posts:
    123
    Sorry, but have you even looked at the bundled Documentation (section Microphone)? The API is pretty straightforward. What exactly are you missing? (the bug mentioned here is minor with regards to functionality. You can use GetWaveData() on the audiosource playing the recorded clip, if you want the input level, instead).

    cheers,
     
  10. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    587
    Unity 3.5 beta says:

    `UnityEngine.AudioSource' does not contain a definition for `GetWaveData' and no extension method `GetWaveData' of type `UnityEngine.AudioSource' could be found (are you missing a using directive or an assembly reference?)

    =/
     
  11. soren

    soren

    Joined:
    Feb 18, 2008
    Posts:
    123
  12. PlanetTimmy

    PlanetTimmy

    Joined:
    Mar 13, 2010
    Posts:
    117
    Yay - this appears to be working with the new 3.5 release candidate. Now to see if I can get hold of that data in real-time.
     
  13. MSylvia

    MSylvia

    Joined:
    May 19, 2009
    Posts:
    42
    Is there a way to hear what your recording in real-time? Has anyone got that working yet. I've tried.

    Code (csharp):
    1.  
    2. audio.clip = Microphone.Start(micDevices[micDeviceIndex], false, 10, 44100);
    3. audio.Play();
    4.  
    but its sounds terrible. The playback once finished sounds clear though. a bug perhaps?
     
  14. soren

    soren

    Joined:
    Feb 18, 2008
    Posts:
    123
    You need to wait for Microphone.GetPosition() > 0, before playing.
     
  15. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,293
    Btw. this might be a stupid question but where can you download the latest version of 3.5?

    The update function inside Unity still states that the latest version is b6.
     
  16. MSylvia

    MSylvia

    Joined:
    May 19, 2009
    Posts:
    42
    Thanks, I realized after that I needed a buffer before attempting to play.
     
  17. natsuto

    natsuto

    Joined:
    Feb 8, 2012
    Posts:
    3
    Mining get there, anyway, about IOS microphone input function of the state, there may be some documentation pointers feedback on the shoulder of the unified development team?
     
unityunity