OpenAI-API-dotnet icon indicating copy to clipboard operation
OpenAI-API-dotnet copied to clipboard

GetSpeechAsStreamAsync - NAudio

Open Hantse opened this issue 1 year ago • 4 comments

Hello,

I try to used the GetSpeechAsStreamAsync but, i didn't find any solution/configuration to play received sound, it's always a "blank noise" output. I've try with NAudio.

var audioStream = await api.TextToSpeech.GetSpeechAsStreamAsync("Hello world !", OpenAI_API.Audio.TextToSpeechRequest.Voices.Nova);

WaveFormat waveFormat = new WaveFormat(24000, 16, 2); 

using (WaveOutEvent waveOut = new WaveOutEvent())
{
    using (StreamWaveProvider provider = new StreamWaveProvider(audioStream, waveFormat))
    {
        waveOut.Init(provider);
        waveOut.Play();

        while (waveOut.PlaybackState == PlaybackState.Playing)
        {
            System.Threading.Thread.Sleep(100);
        }
    }
}

public class StreamWaveProvider : WaveStream
{
    private Stream sourceStream;
    private WaveFormat waveFormat;

    public StreamWaveProvider(Stream sourceStream, WaveFormat waveFormat)
    {
        this.sourceStream = sourceStream;
        this.waveFormat = waveFormat;
    }

    public override WaveFormat WaveFormat => this.waveFormat;

    public override long Length => sourceStream.Length;

    public override long Position
    {
        get => sourceStream.Position;
        set => sourceStream.Position = value;
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return sourceStream.Read(buffer, offset, count);
    }
}

I've miss something in audio configuration ? In stream processing ? Any idea ?

Kr,

Hantse avatar Dec 26 '23 11:12 Hantse

NAudio is not good at playing streamed audio that's in MP3 format. Try saving it to disk first and having NAudio play it back from disk. Unfortunately OpenAI does not let you request audio to be output in wave format directly.

It is theoretically possible to use NAudio to convert it to wave format in memory, but it's a lot more work and I personally haven't been able to make it work either. You're going to have to do some deep dives on Google to figure that out.

OkGoDoIt avatar Dec 26 '23 17:12 OkGoDoIt

Hello,

Thx for response ! And you have already success to read stream flow in audio ouput ? With another lib than NAudio ?

Hantse avatar Dec 26 '23 17:12 Hantse

With CSCORE :)

async Task PlayAudioStreamAsync(Stream audioStream)
{
    // Assurez-vous que le flux est positionné au début
    if (audioStream.CanSeek)
        audioStream.Position = 0;

    // Utilisez CSCore pour lire le flux
    using (var soundOut = new WasapiOut())
    {
        // Utilisez MediaFoundationDecoder pour décoder le flux MP3
        using (var mp3Stream = new MediaFoundationDecoder(audioStream))
        {
            soundOut.Initialize(mp3Stream);
            soundOut.Play();

            // Attendre que la lecture soit terminée
            while (soundOut.PlaybackState != PlaybackState.Stopped)
            {
                await Task.Delay(100); // Ne pas utiliser Thread.Sleep dans une application async
            }
        }
    }
}

Hantse avatar Dec 26 '23 18:12 Hantse

With NAudio I just save it to disk and play it that way. Is CSCORE working for you? Can I close this issue as resolved or is there something I need to do on my end?

OkGoDoIt avatar Jan 27 '24 05:01 OkGoDoIt