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

Using a float list for AudioClip.GetData() and AudioClip.SetData()???

Discussion in 'Scripting' started by EnsurdFrndship, Jul 4, 2012.

  1. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    Hello,
    I want to work with a sound buffer in such a way where the size of the buffer gets determined by the size of the clip that gets recorded by the microphone, and isn't specified during initialization. A float list would be best for that, and since I am dealing with a huge buffer (a 10 second maximum sound clip being the maximum size of 44100 frequency x 2 channels x 10 seconds), I would like to make the buffered list at a max size of 882000 because I notice Unity crashes if I try to make the max size 15 seconds. How can I use a floated list for the GetData and SetData functions instead of using a float[] array, and how come other sound editing software programs can work with even minutes of sound without even crashing?

    Thank you,
    Michael S. Lowe
     
  2. michael-bartnett

    michael-bartnett

    Joined:
    Jul 8, 2012
    Posts:
    8
    Not sure if you've solved your problem yet, got here looking for more info on AudioClip.SetData() also (it's not very clear how we set number of channels for a generated clip, or if that's even possible).

    If you're working with audio, it's best to not use anything more than a native array to process the audio. The overhead of a list is often undesirable in audio applications. If you really need a List<float>, you can get away with it by using the ToArray() method of the List class. The reason that sound editing programs are able to process to much audio so quickly is that they are constantly streaming the sound.

    If you look in the playback/render settings of most major Digital Audio Workstations, you'll see an audio buffer size setting ranging from 128 to 512 (or sometimes more, Flash takes 2048 samples). These programs never process more than a fraction of a second of audio at a time, but are constantly running, advancing the playback head 128 samples at a time. Breaking it up like this enables you to interact with the mix in realtime (like recording something over it) and gives the thread scheduler an opportunity to let the GUI update run. If you also limit yourself to a set number of samples, you can easily use an array, not having to worry about dynamically resizing it. Just keep old sample data around and store a `last_index` variable;

    The wikipedia article on the Short Time Fourier Transform is a intimidating, but the Example section illustrates the concept well enough:
    http://en.wikipedia.org/wiki/Short-time_Fourier_transform#Example
     
    Last edited: Jul 9, 2012