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

Setting Audio Clip Length Unity

Discussion in 'Scripting' started by alistair21299, Jun 8, 2022.

  1. alistair21299

    alistair21299

    Joined:
    Aug 17, 2020
    Posts:
    2
    Hello All,

    I am currently trying to create a audio clip from an float array. However the length of the clip created is always 0.01. Is there a way to increase the length of the Audio Clip so that it can create a clip of about 10 seconds?

    I am using an audio buffer which as a queue like structure to add the float data into the audio clip

    _audioClip = AudioClip.Create(clipName,
    320,
    1, 44100, true,
    OnAudioRead);
    Debug.Log("Length "+_audioClip.length);
    aud.clip = _audioClip;
    aud.loop = true;
    //aud.Play();

    private void OnAudioRead(float[] data)
    {
    for (var i = 0; i < data.Length; i++)
    {
    lock (audioBuffer)
    {
    if (audioBuffer.Count > 0)
    {
    data = audioBuffer.Get();
    readCount += 1;
    }
    }
    }

    Debug.LogFormat("buffer length remains: {0}", writeCount - readCount);
    }
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    Please use code tags.

    The second argument to AudioClip.Create is the total number of samples. You've set it to 320, at 44100 samples per second that results in a total length of 0.007 seconds. You'll need to set the number of samples to how long you want your clip to be (e.g.
    samples = duration * samples per second
    ).
     
    alistair21299 likes this.
  3. alistair21299

    alistair21299

    Joined:
    Aug 17, 2020
    Posts:
    2
    Thanks for the help. It worked for me but now I'm getting another error which I can't seem to understand. By any chance could you please explain what could the error possibly be?

    This error occurs when calling AudioClip GetData from unity
     

    Attached Files:

  4. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    It means AudioClip.Create was called with its 5th parameter (streaming) set to true but the 6th parameter (pcmreadercallback) was not set or a null value was given. I can't say why it happens just based on this information, though.