|
| 1 | +# VoiceLive Session Command Methods |
| 2 | + |
| 3 | +This document describes the new convenience methods added to the `VoiceLiveSession` class to provide a more developer-friendly API similar to the OpenAI SDK. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The following convenience methods have been added to provide an easier way to send control messages to the VoiceLive service without requiring developers to manually construct and populate `ClientEvent` classes. |
| 8 | + |
| 9 | +## New Methods Added |
| 10 | + |
| 11 | +### Audio Stream Management |
| 12 | + |
| 13 | +#### `ClearStreamingAudioAsync` / `ClearStreamingAudio` |
| 14 | +Clears all input audio currently being streamed. |
| 15 | + |
| 16 | +```csharp |
| 17 | +// Async version |
| 18 | +await session.ClearStreamingAudioAsync(cancellationToken); |
| 19 | + |
| 20 | +// Sync version |
| 21 | +session.ClearStreamingAudio(cancellationToken); |
| 22 | +``` |
| 23 | + |
| 24 | +**Underlying ClientEvent:** `ClientEventInputAudioClear` |
| 25 | + |
| 26 | +### Audio Turn Management |
| 27 | + |
| 28 | +#### `StartAudioTurnAsync` / `StartAudioTurn` |
| 29 | +Starts a new audio input turn with a unique identifier. |
| 30 | + |
| 31 | +```csharp |
| 32 | +string turnId = Guid.NewGuid().ToString(); |
| 33 | + |
| 34 | +// Async version |
| 35 | +await session.StartAudioTurnAsync(turnId, cancellationToken); |
| 36 | + |
| 37 | +// Sync version |
| 38 | +session.StartAudioTurn(turnId, cancellationToken); |
| 39 | +``` |
| 40 | + |
| 41 | +**Underlying ClientEvent:** `ClientEventInputAudioTurnStart` |
| 42 | + |
| 43 | +#### `AppendAudioToTurnAsync` / `AppendAudioToTurn` |
| 44 | +Appends audio data to an ongoing input turn. Available in two overloads for different audio data types. |
| 45 | + |
| 46 | +```csharp |
| 47 | +string turnId = "some-turn-id"; |
| 48 | +byte[] audioData = GetAudioBytes(); |
| 49 | +BinaryData audioBinary = BinaryData.FromBytes(audioData); |
| 50 | + |
| 51 | +// With byte array - Async |
| 52 | +await session.AppendAudioToTurnAsync(turnId, audioData, cancellationToken); |
| 53 | + |
| 54 | +// With byte array - Sync |
| 55 | +session.AppendAudioToTurn(turnId, audioData, cancellationToken); |
| 56 | + |
| 57 | +// With BinaryData - Async |
| 58 | +await session.AppendAudioToTurnAsync(turnId, audioBinary, cancellationToken); |
| 59 | + |
| 60 | +// With BinaryData - Sync |
| 61 | +session.AppendAudioToTurn(turnId, audioBinary, cancellationToken); |
| 62 | +``` |
| 63 | + |
| 64 | +**Underlying ClientEvent:** `ClientEventInputAudioTurnAppend` |
| 65 | + |
| 66 | +#### `EndAudioTurnAsync` / `EndAudioTurn` |
| 67 | +Marks the end of an audio input turn. |
| 68 | + |
| 69 | +```csharp |
| 70 | +string turnId = "some-turn-id"; |
| 71 | + |
| 72 | +// Async version |
| 73 | +await session.EndAudioTurnAsync(turnId, cancellationToken); |
| 74 | + |
| 75 | +// Sync version |
| 76 | +session.EndAudioTurn(turnId, cancellationToken); |
| 77 | +``` |
| 78 | + |
| 79 | +**Underlying ClientEvent:** `ClientEventInputAudioTurnEnd` |
| 80 | + |
| 81 | +#### `CancelAudioTurnAsync` / `CancelAudioTurn` |
| 82 | +Cancels an in-progress input audio turn. |
| 83 | + |
| 84 | +```csharp |
| 85 | +string turnId = "some-turn-id"; |
| 86 | + |
| 87 | +// Async version |
| 88 | +await session.CancelAudioTurnAsync(turnId, cancellationToken); |
| 89 | + |
| 90 | +// Sync version |
| 91 | +session.CancelAudioTurn(turnId, cancellationToken); |
| 92 | +``` |
| 93 | + |
| 94 | +**Underlying ClientEvent:** `ClientEventInputAudioTurnCancel` |
| 95 | + |
| 96 | +### Avatar Management |
| 97 | + |
| 98 | +#### `ConnectAvatarAsync` / `ConnectAvatar` |
| 99 | +Connects and provides the client's SDP (Session Description Protocol) for avatar-related media negotiation. |
| 100 | + |
| 101 | +```csharp |
| 102 | +string clientSdp = GetClientSdpOffer(); |
| 103 | + |
| 104 | +// Async version |
| 105 | +await session.ConnectAvatarAsync(clientSdp, cancellationToken); |
| 106 | + |
| 107 | +// Sync version |
| 108 | +session.ConnectAvatar(clientSdp, cancellationToken); |
| 109 | +``` |
| 110 | + |
| 111 | +**Underlying ClientEvent:** `ClientEventSessionAvatarConnect` |
| 112 | + |
| 113 | +## Complete Audio Turn Example |
| 114 | + |
| 115 | +Here's a complete example showing how to use the audio turn management methods: |
| 116 | + |
| 117 | +```csharp |
| 118 | +using Azure.AI.VoiceLive; |
| 119 | + |
| 120 | +public async Task HandleAudioTurnAsync(VoiceLiveSession session, Stream audioStream) |
| 121 | +{ |
| 122 | + string turnId = Guid.NewGuid().ToString(); |
| 123 | + |
| 124 | + try |
| 125 | + { |
| 126 | + // Start the audio turn |
| 127 | + await session.StartAudioTurnAsync(turnId); |
| 128 | + |
| 129 | + // Read and append audio data in chunks |
| 130 | + byte[] buffer = new byte[4096]; |
| 131 | + int bytesRead; |
| 132 | + |
| 133 | + while ((bytesRead = await audioStream.ReadAsync(buffer, 0, buffer.Length)) > 0) |
| 134 | + { |
| 135 | + byte[] audioChunk = new byte[bytesRead]; |
| 136 | + Array.Copy(buffer, audioChunk, bytesRead); |
| 137 | + |
| 138 | + await session.AppendAudioToTurnAsync(turnId, audioChunk); |
| 139 | + } |
| 140 | + |
| 141 | + // End the audio turn |
| 142 | + await session.EndAudioTurnAsync(turnId); |
| 143 | + } |
| 144 | + catch (Exception ex) |
| 145 | + { |
| 146 | + // Cancel the turn if something goes wrong |
| 147 | + await session.CancelAudioTurnAsync(turnId); |
| 148 | + throw; |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +## Design Principles |
| 154 | + |
| 155 | +These methods follow the established patterns in the VoiceLive SDK: |
| 156 | + |
| 157 | +1. **Both sync and async versions** are provided for all methods |
| 158 | +2. **Proper parameter validation** using `Argument.AssertNotNull` and `Argument.AssertNotNullOrEmpty` |
| 159 | +3. **Disposal checking** using `ThrowIfDisposed()` |
| 160 | +4. **Consistent naming** that describes the action rather than just mirroring the event type |
| 161 | +5. **Comprehensive documentation** with parameter descriptions and exception information |
| 162 | +6. **JSON serialization** for sending commands, consistent with existing methods |
| 163 | + |
| 164 | +## Previously Existing Methods |
| 165 | + |
| 166 | +The following convenience methods were already available and remain unchanged: |
| 167 | + |
| 168 | +- **Audio Buffer Management:** `SendInputAudioAsync`, `ClearInputAudioAsync`, `CommitInputAudioAsync` |
| 169 | +- **Session Configuration:** `ConfigureSessionAsync`, `ConfigureConversationSessionAsync`, `ConfigureTranscriptionSessionAsync` |
| 170 | +- **Item Management:** `AddItemAsync`, `RequestItemRetrievalAsync`, `DeleteItemAsync`, `TruncateConversationAsync` |
| 171 | +- **Response Management:** `StartResponseAsync`, `CancelResponseAsync` |
| 172 | + |
| 173 | +The new methods complement these existing ones to provide comprehensive coverage of all available `ClientEvent` types. |
0 commit comments