I’m having some trouble with using PCMReaderCallback. The callback is never called when the audioBuffer is filled. It should be called automatically by Unity. Here is a sample of my code:
private MemoryStream audioBuffer;
async void Start()
{
Debug.Log("Start");
audioBuffer = new MemoryStream();
// Initialize the audio clip and buffer
audioClip = AudioClip.Create("ElevenLabsTTS", 44100, 1, 44100, true, PcmReader);
await ConnectToWebSocket(); //Some logic is implemented here to send the message to web socket and receive a byte[] from it
audioSource.clip = audioClip;
audioSource.Play();
Debug.Log("Playing audio clip");
}
private void PcmReader(float[] data)
{
if (null == audioBuffer || audioBuffer.Length == 0) return;
// Create a binary reader for the memory buffer
using (BinaryReader reader = new BinaryReader(audioBuffer))
{
Debug.Log("audioBuffer is read");
for (int i = 0; i < data.Length; i++)
{
if (audioBuffer.Position < audioBuffer.Length)
{
// Read a 16-bit sample from the memory buffer
short sample = reader.ReadInt16();
// Convert the sample to a float in the range -1 to 1 and store it in the data array
data[i] = sample / 32768f;
}
else
{
// If there is no more data in the memory buffer, fill the rest of the data array with zeros
data[i] = 0f;
}
}
}
if (audioBuffer.Position >= audioBuffer.Length) audioBuffer.SetLength(0);
}
When the message is received from the socket, which is the audio of “Hello”, this audioBuffer is filled.
websocket.OnMessage += (bytes) =>
{
string message = Encoding.UTF8.GetString(bytes);
// Debug.Log("OnMessage : "+message);
var data = JsonUtility.FromJson<MessageData>(message);
if (data.audio != null)
{
byte[] audioData = System.Convert.FromBase64String(data.audio);
audioBuffer.Write(audioData);
Debug.Log("Added audio data");
}
};
At this point, PcmReader() should be called, but is not. What am I missing? Appreciate if anyone could help me here.
As per my understanding, PcmReader() should be called whenever audioBuffer is filled internally by Unity. This is not happening. The AudioSource is correctly configured and is not paused or stopped at any point. The audioClip Play() is called but no audio is observed.
user9319865 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.