This example assumes using tryAGI.OpenAI; is in scope and apiKey contains your tryAGI.OpenAI API key.
1 2 3 4 5 6 7 8 9101112131415161718192021
usingvarclient=newOpenAiClient(apiKey);usingvarmemoryStream=newMemoryStream();awaitforeach(varstreamEventinclient.Audio.CreateSpeechAsync(model:CreateSpeechRequestModel.Gpt4oMiniTts,input:"Hello! This is a text-to-speech test.",voice:(VoiceIdsShared)VoiceIdsSharedEnum.Alloy,responseFormat:CreateSpeechRequestResponseFormat.Mp3,speed:1.0,streamFormat:CreateSpeechRequestStreamFormat.Sse)){if(streamEvent.SpeechAudioDeltais{}delta){byte[]chunk=Convert.FromBase64String(delta.Audio);memoryStream.Write(chunk,0,chunk.Length);}}byte[]audio=memoryStream.ToArray();Console.WriteLine($"Generated {audio.Length} bytes of audio.");