0

I am streaming audio via websocket from IBM Watson TTS,
currently I already have the audio playing from the stream with NAudio,
but I would like to shift out NAudio for the builtin audioclip
( NAudio is great, but would like to be compatible with Oculus lipsync plus other plugins using the audioclip )

Solution below with NAudio,

The websocket is adding the received bytes to the MemoryStream

private MemoryStream ms = new MemoryStream();

websocket.OnMessage += async (bytes) =>
{
    var pos = ms.Position;
    ms.Position = ms.Length;
    ms.Write(bytes, 0, bytes.Length);
    ms.Position = pos;

    if (!isPlaying)
    {
        StartCoroutine(PlayAudioStream());
    }
}

The playback is currently handled by NAudio, I would like the shift to a AudioClip if possible,

using (var blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new RawSourceWaveStream(ms, new WaveFormat(22050, 16, 1)))))
{
    var aggregator = new SampleAggregator(blockAlignedStream.ToSampleProvider());
    aggregator.NotificationCount = blockAlignedStream.WaveFormat.SampleRate / 50;
    using (var wo = new WaveOutEvent())
    {
        isPlaying = true;
        wo.Init(aggregator);
        wo.Play();

        while (wo.PlaybackState == PlaybackState.Playing) //&& !disconnected
        {
            yield return new WaitForEndOfFrame();
        }
        wo.Dispose();
        aggregator.Reset();
        aggregator = null;
    }
}
bomanden
  • 314
  • 1
  • 2
  • 16
  • Do you have a problem or are you asking if it's possible? What have you tried and what errors did you get? – Chuck Oct 01 '22 at 16:56
  • Hi, my problem is I would like to change out NAudio for Audioclip, For compatibility with plugins like Oculus lipsync etc. So yes my question is how to a have an Audioclip receive streamed audio. – bomanden Oct 02 '22 at 10:12
  • https://forum.unity.com/threads/byte-to-audioclip.911723/ read this thread, there is a solution to stream into audio clip – Nikolay Gonza Oct 05 '22 at 14:48

0 Answers0