OkGoDoIt / OpenAI-API-dotnet

An unofficial C#/.NET SDK for accessing the OpenAI GPT-3 API
https://www.nuget.org/packages/OpenAI/
Other
1.85k stars 428 forks source link

GetSpeechAsStreamAsync - NAudio #186

Open Hantse opened 9 months ago

Hantse commented 9 months ago

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,

OkGoDoIt commented 9 months ago

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.

Hantse commented 9 months ago

Hello,

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

Hantse commented 9 months ago

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
            }
        }
    }
}
OkGoDoIt commented 8 months ago

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?