Float Array To AudioClip

Hey guys, I’m using the OpenAI REST API to make TTS for one of my projects that will run on WebGL. The API sent to me a PCM file 16-bit, 24kHz. I managed to convert it into a float array, but when I try to convert it to AudioClip, I get only TV static noises…

Here is my code; I’m not using the API right now. I’m trying to focus only on the conversion right now.

Btw my Debug.Log($“Samples: {floatList_1.Count}, seconds: {(floatList_1.Count / 24000f):0.000}”); is displaying a number way shorter than my PCM file length, but I don’t know why.

     private IEnumerator LoadPCM()
    {
        string uri = $"{Application.streamingAssetsPath}/audio.pcm";
        using (UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(uri, AudioType.UNKNOWN))
        {
            yield return request.SendWebRequest();

            audioByteArray = request.downloadHandler.data;
        }
        floatList = SoundHelper.ByteArrayToFloatArray(audioByteArray).ToList();
        OwnAudioClip = AudioClip.Create("PCM", floatList.ToArray().Length, 1, 24000, false, false);
        GetComponent<AudioSource>().clip = OwnAudioClip;
        GetComponent<AudioSource>().Play();
        Debug.Log($"Samples: {floatList_1.Count}, seconds: {(floatList_1.Count / 24000f):0.000}");
    }

    public static float[] ByteArrayToFloatArray(Byte[] receivedBytes) 
    {
        float[] samples = new float[receivedBytes.Length / 2];

        const float maxVal = 32768.0f; 

        for (int i = 0; i < samples.Length; i++)
        {
            short int16sample = System.BitConverter.ToInt16(receivedBytes, i * 2);
            samples[i] = (float)int16sample / maxVal;
        }

        return samples;
    }

I would write it to a file and look at it… it’s probably not what you expect if it’s way shorter.

I’ve seen APIs that return secondary URI locators that you then need to use a second retrieval to get the actual payload.

Either way, writing it to a file and then importing it in something like Audacity is a handy way to eliminate Unity and your networking call from the problem and get the core data transfer working fully.

Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

And setting up a proxy can be very helpful too, in order to compare traffic:

https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity