Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetSpeechAsStreamAsync - NAudio #186

Open
Hantse opened this issue Dec 26, 2023 · 4 comments
Open

GetSpeechAsStreamAsync - NAudio #186

Hantse opened this issue Dec 26, 2023 · 4 comments

Comments

@Hantse
Copy link

Hantse commented Dec 26, 2023

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
Copy link
Owner

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
Copy link
Author

Hantse commented Dec 26, 2023

Hello,

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

@Hantse
Copy link
Author

Hantse commented Dec 26, 2023

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
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants