Search Unity

Recording Frequency on iOS

Discussion in 'Audio & Video' started by LANGALab, Dec 12, 2014.

  1. LANGALab

    LANGALab

    Joined:
    Aug 20, 2014
    Posts:
    2
    Hey everyone,

    I'm creating an app where I record voices. The recordings must be in 16000Hz, but when I play them back, they sound as if they're 8000Hz.

    This only occurs on iOS. When running in the editor, audio records and plays back in 16000Hz without issue.

    Has anyone had similar experience?
     
  2. Veemix

    Veemix

    Joined:
    Dec 5, 2014
    Posts:
    25
    Can you give an example of the code you are using for audio recording and playback?
     
  3. LANGALab

    LANGALab

    Joined:
    Aug 20, 2014
    Posts:
    2
    Sure, I've snipped a bunch of stuff together. Hopefully it's understandable.

    Basically I have a looping clip being recorded to acting as a buffer. Then when we tap "start", it begins to take samples from the buffer. Each chunk of samples is added to a temp array. When we tap "stop", the temp array is saved to a new Audio Clip. Then we can tap "Play" to have the clip play.

    Code (CSharp):
    1. //This is running constantly, looping as an audio buffer (frequency is 16000)
    2. audio.clip = Microphone.Start( _microphone, true, 120, frequency );
    3.  
    4. //this loops to pull out samples
    5. audio.clip.GetData( audioFloats, readHead );
    6. readHead = ( readHead + nFloatsToGet ) % audio.clip.samples;
    7.  
    8. //this takes every chunk of samples and puts them in an array that is saved
    9. tArray = new float[sArray.Length + _array.Length];
    10.         Array.Copy( sArray, tArray, sArray.Length );
    11.         Array.Copy( _array, 0, tArray, sArray.Length, _array.Length );
    12.         sArray = tArray;
    13.  
    14. //save the latest data we pulled from the buffer
    15. recentClip = AudioClip.Create( "speechPlaybackClip", sArray.Length, 1, 16000, false, false );
    16. recentClip.SetData( sArray, 0 );
    17.  
    18. //play audio
    19. audio.PlayOneShot( Speech.getRecentClip() );