Unity3d how to play streamed audio via Audioclip?

788 Views Asked by At

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

There are 0 best solutions below