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

Bug Short sounds (ex: 1/4 s) plays twice the first time, if the audioClip is created in streaming mode.

Discussion in 'Audio & Video' started by zugolbr, Nov 23, 2021.

  1. zugolbr

    zugolbr

    Joined:
    Nov 23, 2021
    Posts:
    6
    Hi, I wonder if I am missing something here, or if this is a bug to report...

    Creating a short AudioClip in code, as in https://docs.unity3d.com/ScriptReference/AudioClip.Create.html, causes the sound to be played twice first, and once (as expected) on subsequent Play() calls.

    To reproduce, simply create an empty 3d project, attach a script to the camera, and paste the following:


    Code (CSharp):
    1. public class NewBehaviourScript : MonoBehaviour
    2. {
    3.     /// <summary>
    4.     /// This example is closely based on the Unity documentation for AudioClip.Create: https://docs.unity3d.com/ScriptReference/AudioClip.Create.html
    5.     /// Short sounds (ex: 1/4 s) plays twice the first time, if the audioClip is created in streaming mode.
    6.     /// Unity 2020.3.17f1, default empty 3d project, with one single script added (this one).
    7.     /// </summary>
    8.     void Start()
    9.     {
    10.         // Create a 1/4 s sound. (if I use a 1s sound -samplerate / 1-, everything works fine)
    11.         _Pcm = new float[samplerate / 4];
    12.  
    13.         // Fill the first half with a 440Hz frequency, leave the second half silent.
    14.         int count = 0;
    15.         while (count < _Pcm.Length / 2)
    16.         {
    17.             _Pcm[count] = Mathf.Sin(2 * Mathf.PI * frequency * position / samplerate);
    18.             position++;
    19.             count++;
    20.         }
    21.  
    22.         _AudioSource = Camera.main.gameObject.AddComponent<AudioSource>();
    23.  
    24.         // Creating the audioClip in streaming mode causes the issue.
    25.         _AudioSource.clip = AudioClip.Create("", _Pcm.Length, channels: 1, samplerate, stream: true, OnAudioRead, OnAudioSetPosition);
    26.  
    27.         // If instead of using streaming, I pass the pcm data to SetData(...), everything works fine.
    28.         //_AudioSource.clip = AudioClip.Create("", _Pcm.Length, channels: 1, samplerate, stream: false);
    29.         //_AudioSource.clip.SetData(_Pcm, 0);
    30.     }
    31.  
    32.     void OnAudioRead(float[] data)
    33.     {
    34.         Array.Copy(_Pcm, position, data, 0, data.Length);
    35.         position += data.Length;
    36.     }
    37.  
    38.     void OnAudioSetPosition(int newPosition)
    39.     {
    40.         position = newPosition;
    41.     }
    42.  
    43.     public int position = 0;
    44.     public int samplerate = 44100;
    45.     public float frequency = 440;
    46.  
    47.     AudioSource _AudioSource;
    48.     float[] _Pcm;
    49.  
    50.     void Update()
    51.     {
    52.         // In stream mode, the first F2 hit plays the sound twice, while all the following F2 hits play the sound only once (as expected).
    53.         if (Input.GetKeyDown(KeyCode.F2))
    54.         {
    55.             _AudioSource.Play();
    56.         }
    57.     }
    58. }
     
  2. zugolbr

    zugolbr

    Joined:
    Nov 23, 2021
    Posts:
    6
    Just tried with Unity 2020.3.23f1: same behavior.