Skip to content

Commit

Permalink
ElevenLabs-DotNet 1.3.2 (#15)
Browse files Browse the repository at this point in the history
- Added voice equality operators
- Added better parameter validation checks in Endpoints
  • Loading branch information
StephenHodgson authored May 12, 2023
1 parent 5f7f729 commit 3597639
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ElevenLabs-DotNet/ElevenLabs-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
<Company>RageAgainstThePixel</Company>
<Copyright>2023</Copyright>
<PackageTags>ElevenLabs, AI, ML, Voice, TTS</PackageTags>
<Version>1.3.0</Version>
<PackageReleaseNotes>Version 1.3.0
<Version>1.3.2</Version>
<PackageReleaseNotes>Version 1.3.2
- Added voice equality operators
- Added better parameter validation checks in Endpoints
Version 1.3.0
- Added ModelsEndpoint
- Updated TextToSpeech.TextToSpeechAsync with optional Model parameter. Defaults to eleven_monolingual_v1
Version 1.2.1
Expand Down
6 changes: 6 additions & 0 deletions ElevenLabs-DotNet/TextToSpeech/TextToSpeechEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public async Task<string> TextToSpeechAsync(string text, Voice voice, VoiceSetti
{
throw new ArgumentOutOfRangeException(nameof(text), $"{nameof(text)} cannot exceed 5000 characters");
}

if (voice == null)
{
throw new ArgumentNullException(nameof(voice));
}

var rootDirectory = (saveDirectory ?? Directory.GetCurrentDirectory()).CreateNewDirectory(nameof(ElevenLabs));
var downloadDirectory = rootDirectory.CreateNewDirectory("TextToSpeech");
var fileName = $"{text.GenerateGuid()}.mp3";
Expand Down
51 changes: 51 additions & 0 deletions ElevenLabs-DotNet/Voices/Voice.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -78,5 +79,55 @@ public Voice(string id)
public static Voice Sam { get; } = new Voice("yoZ06aMxZJJ28mfd3POQ");

#endregion Premade Voices

public bool Equals(Voice other)
{
if (ReferenceEquals(null, other))
{
return string.IsNullOrWhiteSpace(Id);
}

if (ReferenceEquals(this, other))
{
return true;
}

return Name == other.Name &&
Id == other.Id &&
Equals(Samples, other.Samples) &&
Category == other.Category &&
Equals(Labels, other.Labels) &&
PreviewUrl == other.PreviewUrl &&
Equals(AvailableForTiers, other.AvailableForTiers) &&
Equals(Settings, other.Settings);
}

public override bool Equals(object voice)
=> ReferenceEquals(this, voice) || voice is Voice other && Equals(other);

public override int GetHashCode()
=> HashCode.Combine(Name, Id, Samples, Category, Labels, PreviewUrl, AvailableForTiers, Settings);

public static bool operator !=(Voice left, Voice right) => !(left == right);

public static bool operator ==(Voice left, Voice right)
{
if (left is null && right is null)
{
return true;
}

if (left is null)
{
return string.IsNullOrWhiteSpace(right.Id);
}

if (right is null)
{
return string.IsNullOrWhiteSpace(left.Id);
}

return left.Equals(right);
}
}
}
40 changes: 40 additions & 0 deletions ElevenLabs-DotNet/Voices/VoicesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public async Task<VoiceSettings> GetDefaultVoiceSettingsAsync(CancellationToken
/// <returns><see cref="VoiceSettings"/>.</returns>
public async Task<VoiceSettings> GetVoiceSettingsAsync(string voiceId, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(voiceId))
{
throw new ArgumentNullException(nameof(voiceId));
}

var response = await Api.Client.GetAsync(GetUrl($"/{voiceId}/settings"), cancellationToken);
var responseAsString = await response.ReadAsStringAsync();
return JsonSerializer.Deserialize<VoiceSettings>(responseAsString, Api.JsonSerializationOptions);
Expand All @@ -96,6 +101,11 @@ public async Task<VoiceSettings> GetVoiceSettingsAsync(string voiceId, Cancellat
/// <returns><see cref="Voice"/>.</returns>
public async Task<Voice> GetVoiceAsync(string voiceId, bool withSettings = true, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(voiceId))
{
throw new ArgumentNullException(nameof(voiceId));
}

var response = await Api.Client.GetAsync(GetUrl($"/{voiceId}?with_settings={withSettings}"), cancellationToken);
var responseAsString = await response.ReadAsStringAsync();
return JsonSerializer.Deserialize<Voice>(responseAsString, Api.JsonSerializationOptions);
Expand All @@ -110,6 +120,11 @@ public async Task<Voice> GetVoiceAsync(string voiceId, bool withSettings = true,
/// <returns>True, if voice settings was successfully edited.</returns>
public async Task<bool> EditVoiceSettingsAsync(string voiceId, VoiceSettings voiceSettings, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(voiceId))
{
throw new ArgumentNullException(nameof(voiceId));
}

var payload = JsonSerializer.Serialize(voiceSettings).ToJsonStringContent();
var response = await Api.Client.PostAsync(GetUrl($"/{voiceId}/settings/edit"), payload, cancellationToken);
await response.ReadAsStringAsync();
Expand Down Expand Up @@ -229,6 +244,11 @@ public async Task<bool> EditVoiceAsync(Voice voice, IEnumerable<string> samplePa
/// <returns>True, if voice was successfully deleted.</returns>
public async Task<bool> DeleteVoiceAsync(string voiceId, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(voiceId))
{
throw new ArgumentNullException(nameof(voiceId));
}

var response = await Api.Client.DeleteAsync(GetUrl($"/{voiceId}"), cancellationToken);
await response.CheckResponseAsync(cancellationToken);
return response.IsSuccessStatusCode;
Expand All @@ -245,6 +265,16 @@ public async Task<bool> DeleteVoiceAsync(string voiceId, CancellationToken cance
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
public async Task<string> GetVoiceSampleAsync(string voiceId, string sampleId, string saveDirectory = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(voiceId))
{
throw new ArgumentNullException(nameof(voiceId));
}

if (string.IsNullOrWhiteSpace(sampleId))
{
throw new ArgumentNullException(nameof(sampleId));
}

var response = await Api.Client.GetAsync(GetUrl($"/{voiceId}/samples/{sampleId}/audio"), cancellationToken);
await response.CheckResponseAsync(cancellationToken);

Expand Down Expand Up @@ -291,6 +321,16 @@ public async Task<string> GetVoiceSampleAsync(string voiceId, string sampleId, s
/// <returns>True, if <see cref="Voice"/> <see cref="Sample"/> was successfully deleted.</returns>
public async Task<bool> DeleteVoiceSampleAsync(string voiceId, string sampleId, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(voiceId))
{
throw new ArgumentNullException(nameof(voiceId));
}

if (string.IsNullOrWhiteSpace(sampleId))
{
throw new ArgumentNullException(nameof(sampleId));
}

var response = await Api.Client.DeleteAsync(GetUrl($"/{voiceId}/samples/{sampleId}"), cancellationToken);
await response.CheckResponseAsync(cancellationToken);
return response.IsSuccessStatusCode;
Expand Down

0 comments on commit 3597639

Please sign in to comment.