How do I set the data of an AudioClip using a ReadOnlySpan of floats? Span version not available

Hello!
I am trying to set the data of an audioclip using a ReadOnlySpan because the documentation for audioClip.SetData() reccomended this as it leads to better performance.

Documentation for Unity6 (6000):
“For best performance, the Span version is preferred as it does not require allocating managed memory.”

I tried doing it exactly as the documentation did it in the example code, but the problem is that the ReadOnlySpan override of the SetData method is not accepted as a valid override. The only one showing up is the one using a normal float array, and I naturally get the “Cannot convert Unity.Collections.NativeArray to float[ ]”. I also tried using a ReadOnlySpan of floats directly, and this naturally doesn’t work either.

Any ideas to why I can’t acces the ReadOnlySpan version of the method? I tried searching it up but I couldn’t find any leads.

Here is the code I tried to replicate taken from the example in the documentation:

 public class Example : MonoBehaviour
{
    // Read all the samples from the clip and halve the gain
    void Start()
    {
        AudioSource audioSource = GetComponent<AudioSource>();
        var numSamples = audioSource.clip.samples * audioSource.clip.channels;
        var samples = new NativeArray<float>(numSamples, Allocator.Temp);
        audioSource.clip.GetData(samples, 0);

        for (int i = 0; i < samples.Length; ++i)
        {
            samples[i] = samples[i] * 0.5f;
        }

        audioSource.clip.SetData(samples, 0);
    }
}

(This is my first time asking a question, please correct me if I posted at the wrong place or should do it differently, Thank You :))

I think you need to use one of the conversion methods: Unity - Scripting API: Unity.Collections.NativeArray_1.AsReadOnly

A NativeArray is not a Span/ReadOnlySpan and doesn’t have an implicit operator.

Or just use a Span instead of a NativeArray.

1 Like

Sorry I think I formulated the question a bit poorly, but I found out why it didn’t work now.

The problem was that the span version of the SetData method seemingly didn’t exist. The reason was that it did in fact not exist as I thought I was using Unity 6 (6000) version, when I in reality was using Unity 2021. I don’t know how I overlooked this, but at least the problem is solved.

One note about the implicit operator though. I found through my searching that there used to be no implicit operator from NativeArray to ReadOnlySpan, but this has changed and there now is one.

This can be seen in the NativeArray source code. Here is the excerpt containing the definition:

public static implicit operator Span<T>(in NativeArray<T> source)
{
    return source.AsSpan();
}

public static implicit operator ReadOnlySpan<T>(in NativeArray<T> source)
{
    return source.AsReadOnlySpan();
}

Anyways thank you for your response, I don’t think the question was really answerable as I accidentaly proveded incorrect information about the version I was using

1 Like