Search Unity

Question Why does AudioClip.Create create silent clips?

Discussion in 'Audio & Video' started by steinbitglis, Mar 16, 2021.

  1. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    I don't seem to understand how AudioClip.Create is supposed to work.
    This doesn't seem to play any thing. The original plays, but not the copy.

    Code (CSharp):
    1.     private static void Play(AudioClip clip)
    2.     {
    3.         float[] data = new float[clip.samples];
    4.         clip.GetData(data, 0);
    5.  
    6.         var newClip = AudioClip.Create("test", clip.samples, clip.channels, clip.frequency, false);
    7.         newClip.SetData(data, 0);
    8.         newClip.LoadAudioData();
    9.  
    10.         AudioUtility.PlayPreviewClip(newClip);
    11.     }
     
  2. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    So I want to leave some feedback for Unity, because I got feedback some from QA.

    When you use
    AudioClip.Create
    (without the callbacks) and
    SetData
    to set the data:
    1. You can't just save the
      AudioClip
      with
      AssetDatabase.CreateAsset
      , because you'll be left with a silent clip.
    2. You can't preview the clip with
      AudioUtil
      , because according the feedback I got, an audio asset is required.
    My feedback here is that:
    1. The previewer shouldn't require an asset, unless it's renamed to PlayPreviewClipAsset.
    2. The previewer should have been public.
    3. When creating an
      AudioClip
      with
      AssetDatabase.CreateAsset
      , it should leave a working audio clip.
    4. AssetDatabase.CreateAsset should not create an asset if it's not really an asset (silent audio clip).
     
    droni likes this.
  3. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    you might want to update the title to reflect that it's related to creating the audio asset in the editor
     
  4. mindego

    mindego

    Joined:
    Jul 10, 2022
    Posts:
    2
    It still not possible to CreateAsset from AudioClip. Too bad :(
     
  5. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    416
  6. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
    @SeventhString I've been doing that for a decade. This post is two years old. The bug was opened and closed back in 2021, the ticket # is 1322096. Nothing has been fixed as far as I know. Correct me if I'm wrong.
     
  7. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    416
    Hi sir! I could not find the ticket you mention :\
    If you have a link to the issue tracker or any other reference, I'd be happy to dig and try to find what happened with that.
     
  8. asger60

    asger60

    Joined:
    Mar 27, 2013
    Posts:
    45
    I'm having this issue as well. I'm unable to save instances of audioclips
    I'm doing this: (_target.clipSamples is a float array of samples from an existing audioclip)
    Code (CSharp):
    1. AudioClip newAudioClip =
    2.                     AudioClip.Create("source", _target.clipSamples.Length, _target.channels, _target.frequency,
    3.                         false);
    4.              
    5.                 newAudioClip.SetData(_target.clipSamples, 0);
    6.                 AssetDatabase.CreateAsset(newAudioClip, "Assets/testasset.asset");
    the resulting asset is silent, and broken
     
    Last edited: May 11, 2023
  9. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    416
    Hi!
    We got a pretty busy week but I'll try to repro the issue and provide some guidance about this.
     
  10. asger60

    asger60

    Joined:
    Mar 27, 2013
    Posts:
    45
    hey, did you have a chance to look into this?
     
  11. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    416
    Sorry I haven't yet, I did setup a repro project and made it happen but didn't debug it yet. I'll bring that back near the top of the pile for this week.
     
  12. asger60

    asger60

    Joined:
    Mar 27, 2013
    Posts:
    45
    hey I'm still rooting for you on this one. Would be cool if it could get fixed
     
  13. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    416
    Yeah it's been a while sorry... I'll take a stab at it this week and share my findings here.
     
  14. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    416
    Alright here it is! My understanding of this problem is that it is more about the realm of influence of these functions than a bug in Unity.

    The purpose of `AudioClip.Create` is to create on the fly a runtime object wrapping audio data. Interestingly enough, the AudioClip object itself does not own the audio data, only references it. This means it can only be bound to runtime generated or pre-serialized memory.

    On the other side, `AssetDatabase.CreateAsset(audioClip, targetPath)` only creates/serializes the AudioClip, NOT the associated audio data. Also asset creation only occurs in the context of edition, and I believe it's not even taking place during the Editor Play Mode.

    So these two do not really fit in the same workflow. If your goal is to create new assets, you would have to write a file to disk and then import it with `AssetDatabase` to add it to the project. If the point is to create files on the fly at runtime, it would still be about writing a file to disk but then loading it with a WebRequest.

    We could certainly argue that it's not how it should happen and submit a bug report to the Assets team, but as there is a kind of workaround, I doubt they would engage with this soon.
     
    Last edited: Aug 2, 2023
    bestknighter and steinbitglis like this.
  15. asger60

    asger60

    Joined:
    Mar 27, 2013
    Posts:
    45
    Hey, thanks for having a look at the issue.
    I feel like I have an understanding of the issue now, although I think it's a bit strange that the audioclip is not containing the clip data itself. There's probably a good reason for it I guess.
    For now my workaround has been to just store the clip data in an array in a scriptable object. That works, but I can't preview it, and all that stuff. Actually creating a file on disk and loading it, would be a better option. I'll try it.

    Thanks for you help on this.
     
    SeventhString likes this.