Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson committed Oct 27, 2023
1 parent 7a2687f commit 425d170
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions ElevenLabs-DotNet/Voices/VoicesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,27 @@ public async Task<Voice> AddVoiceAsync(string name, IEnumerable<string> samplePa

if (samplePaths != null)
{
var path = samplePaths.ToList();
var path = samplePaths.Where(path => !string.IsNullOrWhiteSpace(path)).ToList();

if (path.Any())
{
foreach (var sample in path)
{
if (string.IsNullOrWhiteSpace(sample))
if (!File.Exists(sample))
{
Console.WriteLine($"No sample clip found at {sample}!");
continue;
}

var fileStream = File.OpenRead(sample);
var stream = new MemoryStream();
await fileStream.CopyToAsync(stream, cancellationToken).ConfigureAwait(false);
form.Add(new ByteArrayContent(stream.ToArray()), "files", Path.GetFileName(sample));
await fileStream.DisposeAsync().ConfigureAwait(false);
await stream.DisposeAsync().ConfigureAwait(false);
try
{
var fileBytes = await File.ReadAllBytesAsync(sample, cancellationToken);
form.Add(new ByteArrayContent(fileBytes), "files", Path.GetFileName(sample));
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
Expand Down Expand Up @@ -204,23 +208,27 @@ public async Task<bool> EditVoiceAsync(Voice voice, IEnumerable<string> samplePa

if (samplePaths != null)
{
var path = samplePaths.ToList();
var path = samplePaths.Where(path => !string.IsNullOrWhiteSpace(path)).ToList();

if (path.Any())
{
foreach (var sample in path)
{
if (string.IsNullOrWhiteSpace(sample))
if (!File.Exists(sample))
{
Console.WriteLine($"No sample clip found at {sample}!");
continue;
}

var fileStream = File.OpenRead(sample);
var stream = new MemoryStream();
await fileStream.CopyToAsync(stream, cancellationToken).ConfigureAwait(false);
form.Add(new ByteArrayContent(stream.ToArray()), "files", Path.GetFileName(sample));
await fileStream.DisposeAsync().ConfigureAwait(false);
await stream.DisposeAsync().ConfigureAwait(false);
try
{
var fileBytes = await File.ReadAllBytesAsync(sample, cancellationToken);
form.Add(new ByteArrayContent(fileBytes), "files", Path.GetFileName(sample));
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
Expand Down Expand Up @@ -277,22 +285,10 @@ public async Task<VoiceClip> DownloadVoiceSampleAudioAsync(Voice voice, Sample s

var response = await Api.Client.GetAsync(GetUrl($"/{voice.Id}/samples/{sample.Id}/audio"), cancellationToken).ConfigureAwait(false);
await response.CheckResponseAsync(cancellationToken).ConfigureAwait(false);
var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
var memoryStream = new MemoryStream();
byte[] clipData;

try
{
await responseStream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);
clipData = memoryStream.ToArray();
}
finally
{
await memoryStream.DisposeAsync().ConfigureAwait(false);
await responseStream.DisposeAsync().ConfigureAwait(false);
}

return new VoiceClip(sample.Id, string.Empty, voice, clipData);
await using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
await using var memoryStream = new MemoryStream();
await responseStream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);
return new VoiceClip(sample.Id, string.Empty, voice, memoryStream.ToArray());
}

/// <summary>
Expand Down

0 comments on commit 425d170

Please sign in to comment.