diff --git a/ElevenLabs-DotNet/Voices/VoicesEndpoint.cs b/ElevenLabs-DotNet/Voices/VoicesEndpoint.cs index 5b8628c..acdd203 100644 --- a/ElevenLabs-DotNet/Voices/VoicesEndpoint.cs +++ b/ElevenLabs-DotNet/Voices/VoicesEndpoint.cs @@ -130,7 +130,7 @@ public async Task GetVoiceAsync(string voiceId, bool withSettings = false /// /// Edit your settings for a specific voice. /// - /// Id of the voice settings to edit. + /// The id of the voice settings to edit. /// . /// Optional, . /// True, if voice settings was successfully edited. @@ -203,59 +203,14 @@ public async Task AddVoiceAsync(string name, IEnumerable samplePa return await GetVoiceAsync(voiceResponse.VoiceId, cancellationToken: cancellationToken).ConfigureAwait(false); } - /// - /// Add a new voice to your collection of voices in VoiceLab from a stream - /// - /// Name of the voice you want to add. - /// Collection of samples as an array of bytes to be used for the new voice - /// Optional, labels for the new voice. - /// Optional, . - public async Task AddVoiceAsync(string name, IEnumerable sampleDatums, IReadOnlyDictionary labels = null, CancellationToken cancellationToken = default) - { - var form = new MultipartFormDataContent(); - - if (string.IsNullOrWhiteSpace(name)) - { - throw new ArgumentNullException(nameof(name)); - } - - form.Add(new StringContent(name), "name"); - - if (sampleDatums != null) - { - int fileItr = 0; - foreach (byte[] datum in sampleDatums) - { - try - { - form.Add(new ByteArrayContent(datum), "files", $"file-{fileItr++}"); - } - catch (Exception e) - { - Console.WriteLine(e); - } - } - } - - if (labels != null) - { - form.Add(new StringContent(JsonSerializer.Serialize(labels)), "labels"); - } - - var response = await client.Client.PostAsync(GetUrl("/add"), form, cancellationToken).ConfigureAwait(false); - var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false); - var voiceResponse = JsonSerializer.Deserialize(responseAsString, ElevenLabsClient.JsonSerializationOptions); - return await GetVoiceAsync(voiceResponse.VoiceId, cancellationToken: cancellationToken).ConfigureAwait(false); - } - - /// - /// Add a new voice to your collection of voices in VoiceLab from a stream - /// - /// Name of the voice you want to add. - /// Collection of samples as a stream to be used for the new voice - /// Optional, labels for the new voice. - /// Optional, . - public async Task AddVoiceAsync(string name, IEnumerable sampleStreams, IReadOnlyDictionary labels = null, CancellationToken cancellationToken = default) + /// + /// Add a new voice to your collection of voices in VoiceLab from a stream + /// + /// Name of the voice you want to add. + /// Collection of samples as an array of bytes to be used for the new voice + /// Optional, labels for the new voice. + /// Optional, . + public async Task AddVoiceAsync(string name, IEnumerable samples, IReadOnlyDictionary labels = null, CancellationToken cancellationToken = default) { var form = new MultipartFormDataContent(); @@ -264,25 +219,71 @@ public async Task AddVoiceAsync(string name, IEnumerable sampleSt throw new ArgumentNullException(nameof(name)); } + if (samples == null) + { + throw new ArgumentNullException(nameof(samples)); + } + form.Add(new StringContent(name), "name"); - if (sampleStreams != null) + var fileItr = 0; + + foreach (var content in samples) { - int fileItr = 0; - foreach (Stream voiceStream in sampleStreams) + try { - try - { - using (MemoryStream ms = new MemoryStream()) - { - await voiceStream.CopyToAsync(ms, cancellationToken); - form.Add(new ByteArrayContent(ms.ToArray()),"files", $"file-{fileItr++}"); - } - } - catch (Exception e) - { - Console.WriteLine(e); - } + form.Add(new ByteArrayContent(content), "files", $"file-{fileItr++}"); + } + catch (Exception e) + { + Console.WriteLine(e); + } + } + + if (labels != null) + { + form.Add(new StringContent(JsonSerializer.Serialize(labels)), "labels"); + } + + var response = await client.Client.PostAsync(GetUrl("/add"), form, cancellationToken).ConfigureAwait(false); + var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false); + var voiceResponse = JsonSerializer.Deserialize(responseAsString, ElevenLabsClient.JsonSerializationOptions); + return await GetVoiceAsync(voiceResponse.VoiceId, cancellationToken: cancellationToken).ConfigureAwait(false); + } + + /// + /// Add a new voice to your collection of voices in VoiceLab from a stream + /// + /// Name of the voice you want to add. + /// Collection of samples as a stream to be used for the new voice + /// Optional, labels for the new voice. + /// Optional, . + public async Task AddVoiceAsync(string name, IEnumerable sampleStreams, IReadOnlyDictionary labels = null, CancellationToken cancellationToken = default) + { + var form = new MultipartFormDataContent(); + + if (string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentNullException(nameof(name)); + } + + if (sampleStreams == null) + { + throw new ArgumentNullException(nameof(sampleStreams)); + } + + form.Add(new StringContent(name), "name"); + var fileItr = 0; + + foreach (var voiceStream in sampleStreams) + { + try + { + form.Add(new StreamContent(voiceStream), "files", $"file-{fileItr++}"); + } + catch (Exception e) + { + Console.WriteLine(e); } }