AudioClip.Create(); can't play any clips shorter than 1 second

Oh sorry after many hours of running print statistics on all aspects of the audioclip.create function, and finding that it prints within the function in 10x 4096 size sample buffers, except that unity doesnt update it’s frames until it has called 44100 audio samples, i coded something lame and the first half of this question i just wrote the Sample Rate wrong.

I am still confused about the 2nd half and checking figuring out how to buffer audio.

I took the AudioClip.Create(); script from reference

I only changed the sample length to 4410 samples, and printed AudioClip.length.

The sound plays the 4410 sample in a loop for one second, and the AudioClip length prints 1, i.e. 1 second.

if i want to run a 22100 sample sound for example, for some reason the AudioClip.Create is making a sound 1 second long:

    #pragma strict
    // Creates a 1 sec long audioclip, with a 440hz sinoid
    var position: int = 0;
    var sampleRate : int = 0;
    var frequency : float = 440;
    function Start () {

       //MAKE AUDIO FILE OF 0.1 SECONDS''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        var myClip = AudioClip.Create("MySinoid", 4410.0, 1, 4410.0, false, true, OnAudioRead, OnAudioSetPosition);
        sampleRate = AudioSettings.outputSampleRate;
        audio.clip = myClip;
        print("----is length------ "+audio.clip.length);
      //IT PRINTS THAT THE AUDIO CLIP LENGTH IS 1 SECOND''''''''''''''''''''''''''''''''''''''''''''''''''''
        audio.Play();
    }
    function OnAudioRead(data:float[])
    {
        print ( "data.Length " + data.Length +  " pos " + position +" ms " + ( DateTime.Now.Millisecond  ) );
        for (var count = 0; count < data.Length; count++)
        {
            data[count] = Mathf.Sign(Mathf.Sin(2 * Mathf.PI * frequency * position / sampleRate));
            position++;
        }
    }
    function OnAudioSetPosition(newPosition:int)
    {
        position = newPosition;
    }

I also tried sending data from a stream computed array to the audioclip.create’s PCMReaderCallback function, which prints that it runs 10 times at different DateTime.Now.Milliseconds intervals calling 4096 audiobits on every call, within a single frame. I tried to send data to it in buffer segments, to make a diffusion delay with various DSP functions, and it prints 10 times 4096 print results, but it doesnt update the frame in between the 10 data calls, so i can’t stream audio to the audioclip create function in buffers smaller than 44100 float arrays, i.e. 1 second delay, because i can’t update the audio sound it is reading from more often that 44100 buffer lengths, because no frame happens while audioclip create is reading it’s 44100 numbers from the array, which means that i have to make multiple small audiocreate files, except it does a kind of error.

Hi!

First, in AudioClip.Create, you are passing 4410 both as clip number of samples and frequency( equivalent to sample rate here ). I guess 4410 is a typo, and you mean 44100. Then, duration will always equal numberOfSamples/frequency, so if you need a half second clip, create it with 22050 samples at 44100 Hz.

Second, for any kind of audio manipulation, don’t use the PCMReaderCallback, but OnAudioFilterRead, which runs on the audio thread and is meant for that very purpose.

Cheers,

Gregzo

And of course, if you are generating a sine, no need to create a clip at all: just generate it in the OnAudioFilterRead callback. Be aware that it runs on the audio thread, so most UnityEngine methods will be unavailable.

Cheers,

Gregzo

Thanks Gregzo!!! that’s Soooo great. I didn’t know about OnAudioFilterRead what an amazing thing for unity to write into the unityengine, very nice! omg!!!