Example 2: Voice Design. * * This method demonstrates how you can create a custom voice via the API. * First, synthesize speech by specifying a `description` prompt and characteristic * sample text. Specify the generation_id of the resulting audio in a subsequent * call to create a voice. Then, future calls to tts endpoints can specify the * voice by name or generation_id.
()
| 99 | * voice by name or generation_id. |
| 100 | */ |
| 101 | static async Task Example2Async() |
| 102 | { |
| 103 | Console.WriteLine("Example 2: Voice Design - Creating a custom voice..."); |
| 104 | |
| 105 | var result1 = await _client!.Tts.SynthesizeJsonAsync(new PostedTts |
| 106 | { |
| 107 | Utterances = new List<PostedUtterance> |
| 108 | { |
| 109 | new PostedUtterance |
| 110 | { |
| 111 | Description = "Crisp, upper-class British accent with impeccably articulated consonants and perfectly placed vowels. Authoritative and theatrical, as if giving a lecture.", |
| 112 | Text = "The science of speech. That's my profession; also my hobby. Happy is the man who can make a living by his hobby!" |
| 113 | } |
| 114 | }, |
| 115 | NumGenerations = 2, |
| 116 | StripHeaders = true, |
| 117 | }); |
| 118 | |
| 119 | Console.WriteLine("Example 2: Synthesizing voice options for voice creation..."); |
| 120 | using var audioPlayer = StartAudioPlayer(); |
| 121 | await audioPlayer.StartAsync(); |
| 122 | |
| 123 | int sampleNumber = 1; |
| 124 | var generationsList = result1.Generations.ToList(); |
| 125 | foreach (var generation in generationsList) |
| 126 | { |
| 127 | audioPlayer.WriteAudio(Convert.FromBase64String(generation.Audio)); |
| 128 | Console.WriteLine($"Playing option {sampleNumber}..."); |
| 129 | sampleNumber++; |
| 130 | } |
| 131 | await audioPlayer.StopAsync(); |
| 132 | |
| 133 | // Prompt user to select which voice they prefer |
| 134 | Console.WriteLine("\nWhich voice did you prefer?"); |
| 135 | Console.WriteLine($"1. First voice (generation ID: {generationsList[0].GenerationId})"); |
| 136 | Console.WriteLine($"2. Second voice (generation ID: {generationsList[1].GenerationId})"); |
| 137 | |
| 138 | string? userChoice; |
| 139 | int selectedIndex; |
| 140 | do |
| 141 | { |
| 142 | Console.Write("Enter your choice (1 or 2): "); |
| 143 | userChoice = Console.ReadLine(); |
| 144 | } while (!int.TryParse(userChoice, out selectedIndex) || (selectedIndex != 1 && selectedIndex != 2)); |
| 145 | |
| 146 | var selectedGenerationId = generationsList[selectedIndex - 1].GenerationId; |
| 147 | Console.WriteLine($"Selected voice option {selectedIndex} (generation ID: {selectedGenerationId})"); |
| 148 | |
| 149 | // Save the selected voice |
| 150 | var voiceName = $"higgins-{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}"; |
| 151 | await _client!.Tts.Voices.CreateAsync(new PostedVoice |
| 152 | { |
| 153 | Name = voiceName, |
| 154 | GenerationId = selectedGenerationId, |
| 155 | }); |
| 156 | |
| 157 | Console.WriteLine($"Created voice: {voiceName}"); |
| 158 |
nothing calls this directly
no test coverage detected