Why does AudioClip.Create create silent clips?

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.

    private static void Play(AudioClip clip)
    {
        float[] data = new float[clip.samples];
        clip.GetData(data, 0);

        var newClip = AudioClip.Create("test", clip.samples, clip.channels, clip.frequency, false);
        newClip.SetData(data, 0);
        newClip.LoadAudioData();

        AudioUtility.PlayPreviewClip(newClip);
    }

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:

  • You can’t just save the AudioClip with AssetDatabase.CreateAsset, because you’ll be left with a silent clip.
  • You can’t preview the clip with AudioUtil, because according the feedback I got, an audio asset is required.

My feedback here is that:

  • The previewer shouldn’t require an asset, unless it’s renamed to PlayPreviewClipAsset.
  • The previewer should have been public .
  • When creating an AudioClip with AssetDatabase.CreateAsset, it should leave a working audio clip.
  • AssetDatabase.CreateAsset should not create an asset if it’s not really an asset (silent audio clip).
1 Like

you might want to update the title to reflect that it’s related to creating the audio asset in the editor

It still not possible to CreateAsset from AudioClip. Too bad :frowning:

Hi @steinbitglis_1 ,

To submit feedback or bugs, please follow the bug report procedure: Unity QA: Building quality with passion

Thank you!

@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.

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.

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)

AudioClip newAudioClip =
                    AudioClip.Create("source", _target.clipSamples.Length, _target.channels, _target.frequency,
                        false);
              
                newAudioClip.SetData(_target.clipSamples, 0);
                AssetDatabase.CreateAsset(newAudioClip, "Assets/testasset.asset");

the resulting asset is silent, and broken

Hi!
We got a pretty busy week but I’ll try to repro the issue and provide some guidance about this.

hey, did you have a chance to look into this?

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.

hey I’m still rooting for you on this one. Would be cool if it could get fixed

Yeah it’s been a while sorry… I’ll take a stab at it this week and share my findings here.

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.

4 Likes

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.

1 Like