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 910111213141516171819202122
usingvarclient=newOpenAiClient(apiKey);usingvarmemoryStream=newMemoryStream();awaitforeach(varstreamEventinclient.Audio.CreateSpeechAsEventStreamAsync(model:CreateSpeechRequestModel.Gpt4oMiniTts,input:"Hello! This is a text-to-speech test.",voice:AnyOf<AnyOf<VoiceIdsShared?,CreateSpeechRequestVoice?>?,CreateSpeechRequestVoice2>.FromValue1(AnyOf<VoiceIdsShared?,CreateSpeechRequestVoice?>.FromValue1((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.");