Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overloads to VoicesEndpoint that don't require use of the filesystem #40

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions ElevenLabs-DotNet-Tests/Test_Fixture_02_VoicesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,43 @@ public async Task Test_05_AddVoice()
}

[Test]
public async Task Test_06_EditVoice()
public async Task Test_06_AddVoiceFromByteArray()
{
Assert.NotNull(ElevenLabsClient.VoicesEndpoint);
var testLabels = new Dictionary<string, string>
{
{ "accent", "american" }
};
var clipPath = Path.GetFullPath("../../../Assets/test_sample_01.ogg");
byte[] clipData = await File.ReadAllBytesAsync(clipPath);
var result = await ElevenLabsClient.VoicesEndpoint.AddVoiceAsync("Test Voice", new[] { clipData }, testLabels);
Assert.NotNull(result);
Console.WriteLine($"{result.Name}");
Assert.IsNotEmpty(result.Samples);
}


[Test]
public async Task Test_07_AddVoiceFromStream()
{
Assert.NotNull(ElevenLabsClient.VoicesEndpoint);
var testLabels = new Dictionary<string, string>
{
{ "accent", "american" }
};
var clipPath = Path.GetFullPath("../../../Assets/test_sample_01.ogg");

using (FileStream fs = File.OpenRead(clipPath))
{
var result = await ElevenLabsClient.VoicesEndpoint.AddVoiceAsync("Test Voice", new[] { fs }, testLabels);
Assert.NotNull(result);
Console.WriteLine($"{result.Name}");
Assert.IsNotEmpty(result.Samples);
}
}

[Test]
public async Task Test_08_EditVoice()
{
Assert.NotNull(ElevenLabsClient.VoicesEndpoint);
var results = await ElevenLabsClient.VoicesEndpoint.GetAllVoicesAsync();
Expand All @@ -106,7 +142,7 @@ public async Task Test_06_EditVoice()
}

[Test]
public async Task Test_07_GetVoiceSample()
public async Task Test_09_GetVoiceSample()
{
Assert.NotNull(ElevenLabsClient.VoicesEndpoint);
var results = await ElevenLabsClient.VoicesEndpoint.GetAllVoicesAsync();
Expand All @@ -124,7 +160,7 @@ public async Task Test_07_GetVoiceSample()
}

[Test]
public async Task Test_08_DeleteVoiceSample()
public async Task Test_10_DeleteVoiceSample()
{
Assert.NotNull(ElevenLabsClient.VoicesEndpoint);
var results = await ElevenLabsClient.VoicesEndpoint.GetAllVoicesAsync();
Expand All @@ -143,7 +179,7 @@ public async Task Test_08_DeleteVoiceSample()
}

[Test]
public async Task Test_09_DeleteVoice()
public async Task Test_11_DeleteVoice()
{
Assert.NotNull(ElevenLabsClient.VoicesEndpoint);
var results = await ElevenLabsClient.VoicesEndpoint.GetAllVoicesAsync();
Expand Down
94 changes: 94 additions & 0 deletions ElevenLabs-DotNet/Voices/VoicesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,100 @@ public async Task<Voice> AddVoiceAsync(string name, IEnumerable<string> samplePa
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<VoiceResponse>(responseAsString, ElevenLabsClient.JsonSerializationOptions);
return await GetVoiceAsync(voiceResponse.VoiceId, cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Add a new voice to your collection of voices in VoiceLab from a stream
/// </summary>
/// <param name="name">Name of the voice you want to add.</param>
/// <param name="sampleDatums">Collection of samples as an array of bytes to be used for the new voice</param>
/// <param name="labels">Optional, labels for the new voice.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
public async Task<Voice> AddVoiceAsync(string name, IEnumerable<byte[]> sampleDatums, IReadOnlyDictionary<string, string> 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<VoiceResponse>(responseAsString, ElevenLabsClient.JsonSerializationOptions);
return await GetVoiceAsync(voiceResponse.VoiceId, cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Add a new voice to your collection of voices in VoiceLab from a stream
/// </summary>
/// <param name="name">Name of the voice you want to add.</param>
/// <param name="sampleStreams">Collection of samples as a stream to be used for the new voice</param>
/// <param name="labels">Optional, labels for the new voice.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
public async Task<Voice> AddVoiceAsync(string name, IEnumerable<Stream> sampleStreams, IReadOnlyDictionary<string, string> 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 (sampleStreams != null)
{
int fileItr = 0;
foreach (Stream voiceStream in sampleStreams)
{
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);
}
}
}

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<VoiceResponse>(responseAsString, ElevenLabsClient.JsonSerializationOptions);
Expand Down
Loading