varapiKey=Environment.GetEnvironmentVariable("ELEVENLABS_API_KEY")is{Length:>0}apiKeyValue?apiKeyValue:thrownewAssertInconclusiveException("ELEVENLABS_API_KEY environment variable is not found.");// Create the generated realtime client and connect with the API key in the URL.usingvarclient=newElevenLabsRealtimeClient();awaitclient.ConnectAsync(newUri($"wss://api.elevenlabs.io/v1/speech-to-text/realtime?xi_api_key={apiKey}"));// Load a WAV file and extract raw PCM16 audio bytes.byte[]wavBytes=awaitFile.ReadAllBytesAsync(Path.Combine(AppContext.BaseDirectory,"Resources","hello-in-russian-24k-pcm16.wav"));var(pcmSamples,sampleRate,_)=ReadWavPcm16(wavBytes);// Send audio chunks using the typed SendInputAudioChunkAsync method.constintsamplesPerChunk=12000;for(varoffset=0;offset<pcmSamples.Length;offset+=samplesPerChunk){varcount=Math.Min(samplesPerChunk,pcmSamples.Length-offset);varbytes=newbyte[count*2];Buffer.BlockCopy(pcmSamples,offset*2,bytes,0,bytes.Length);varisLastChunk=offset+count>=pcmSamples.Length;awaitclient.SendInputAudioChunkAsync(newInputAudioChunkPayload{AudioBase64=Convert.ToBase64String(bytes),SampleRate=sampleRate,Commit=isLastChunk,});}// Receive typed server events via the discriminated ServerEvent union.usingvarcts=newCancellationTokenSource(TimeSpan.FromSeconds(30));varreceivedSessionStarted=false;string?sessionId=null;string?transcript=null;awaitforeach(varserverEventinclient.ReceiveUpdatesAsync(cts.Token)){if(serverEvent.IsSessionStarted){receivedSessionStarted=true;sessionId=serverEvent.SessionStarted?.SessionId;Console.WriteLine($"Session started: {sessionId}");}elseif(serverEvent.IsPartialTranscript){Console.WriteLine($"Partial: {serverEvent.PartialTranscript?.Text}");}elseif(serverEvent.IsCommittedTranscript){transcript=serverEvent.CommittedTranscript?.Text;Console.WriteLine($"Final: {transcript}");break;}elseif(serverEvent.IsCommittedTranscriptWithTimestamps){transcript=serverEvent.CommittedTranscriptWithTimestamps?.Text;Console.WriteLine($"Final (with timestamps): {transcript}");break;}elseif(serverEvent.IsError){}}