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;
}