Skip to content

Commit

Permalink
ElevenLabs-DotNet 1.2.0 (#13)
Browse files Browse the repository at this point in the history
- added ability to create voice from Id
- refactored internal extension classes
- fixed auth parsing
- added ability to load configuration file from specific path
- added optional parameter deleteCachedFile to TextToSpeechEndpoint.TextToSpeechAsync. Default is false
  • Loading branch information
StephenHodgson authored Apr 30, 2023
1 parent b87d8b3 commit 8aa912c
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/Publish-Nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ on:

env:
DOTNET_VERSION: ${{ github.event.inputs.dotnet-version || '6.0.x' }}
PACKAGE_VERSION: ''

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>disable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<ImplicitUsings>false</ImplicitUsings>
<IsPackable>false</IsPackable>
<SignAssembly>false</SignAssembly>
<LangVersion>latest</LangVersion>
<RootNamespace>ElevenLabs.Tests.Proxy</RootNamespace>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task Test_01_TextToSpeech()
var voice = (await ElevenLabsClient.VoicesEndpoint.GetAllVoicesAsync()).FirstOrDefault();
Assert.NotNull(voice);
var defaultVoiceSettings = await ElevenLabsClient.VoicesEndpoint.GetDefaultVoiceSettingsAsync();
var clipPath = await ElevenLabsClient.TextToSpeechEndpoint.TextToSpeechAsync("The quick brown fox jumps over the lazy dog.", voice, defaultVoiceSettings);
var clipPath = await ElevenLabsClient.TextToSpeechEndpoint.TextToSpeechAsync("The quick brown fox jumps over the lazy dog.", voice, defaultVoiceSettings, deleteCachedFile: true);
Assert.NotNull(clipPath);
Console.WriteLine(clipPath);
}
Expand Down
20 changes: 13 additions & 7 deletions ElevenLabs-DotNet/ElevenLabs-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@
<Authors>Stephen Hodgson</Authors>
<Title>ElevenLabs-DotNet</Title>
<Company>RageAgainstThePixel</Company>
<Version>1.1.1</Version>
<Copyright>2023</Copyright>
<PackageTags>ElevenLabs, AI, ML, Voice, TTS</PackageTags>
<PackageReleaseNotes>Release 1.1.0
<Version>1.2.0</Version>
<PackageReleaseNotes>Version 1.2.0
- Added ability to create voice from Id
- Refactored internal extension classes
- Fixed auth parsing
- Added ability to load configuration file from specific path
- Added optional parameter deleteCachedFile to TextToSpeechEndpoint.TextToSpeechAsync. Default is false
Version 1.1.0
- Added support for ElevenLabs-DotNet-Proxy
Release 1.0.4
Version 1.0.4
- Updated docs
- Removed exception when sample item path is null or whitespace
Release 1.0.3
Version 1.0.3
- Updated DownloadHistoryItemsAsync to download all items if no ids are specified
- Updated docs
Release 1.0.2
Version 1.0.2
- Added VoiceGenerationEndpoint
- Added unit tests for voice design and instant voice cloning
- Updated docs
Release 1.0.1
Version 1.0.1
- Updated docs
Release 1.0.0
Version 1.0.0
- Initial Release!
</PackageReleaseNotes>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand Down
19 changes: 17 additions & 2 deletions ElevenLabs-DotNet/ElevenLabsAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ public static ElevenLabsAuthentication LoadFromEnv()
return string.IsNullOrEmpty(apiKey) ? null : new ElevenLabsAuthentication(apiKey);
}

/// <summary>
/// Attempts to load api keys from a specified configuration file.
/// </summary>
/// <param name="path">The specified path to the configuration file.</param>
/// <returns>
/// Returns the loaded <see cref="ElevenLabsAuthentication"/> any api keys were found,
/// or <see langword="null"/> if it was not successful in finding a config
/// (or if the config file didn't contain correctly formatted API keys)
/// </returns>
public static ElevenLabsAuthentication LoadFromPath(string path)
=> LoadFromDirectory(Path.GetDirectoryName(path), Path.GetFileName(path), false);

/// <summary>
/// Attempts to load api keys from a configuration file, by default ".elevenlabs" in the current directory,
/// optionally traversing up the directory tree.
Expand All @@ -90,7 +102,10 @@ public static ElevenLabsAuthentication LoadFromEnv()
/// </returns>
public static ElevenLabsAuthentication LoadFromDirectory(string directory = null, string filename = ".elevenlabs", bool searchUp = true)
{
directory ??= Environment.CurrentDirectory;
if (string.IsNullOrWhiteSpace(directory))
{
directory = Environment.CurrentDirectory;
}

AuthInfo authInfo = null;

Expand Down Expand Up @@ -119,7 +134,7 @@ public static ElevenLabsAuthentication LoadFromDirectory(string directory = null
{
var parts = line.Split('=', ':');

for (var i = 0; i < parts.Length; i++)
for (var i = 0; i < parts.Length - 1; i++)
{
var part = parts[i];
var nextPart = parts[i + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace ElevenLabs
namespace ElevenLabs.Extensions
{
internal static class HttpResponseMessageExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Security.Cryptography;
using System.Text;

namespace ElevenLabs
namespace ElevenLabs.Extensions
{
internal static class StringExtensions
{
Expand Down
1 change: 1 addition & 0 deletions ElevenLabs-DotNet/History/HistoryEndpoint.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 ElevenLabs.Extensions;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
Expand Down
21 changes: 8 additions & 13 deletions ElevenLabs-DotNet/TextToSpeech/TextToSpeechEndpoint.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 ElevenLabs.Extensions;
using ElevenLabs.Voices;
using System;
using System.IO;
Expand All @@ -25,9 +26,10 @@ public TextToSpeechEndpoint(ElevenLabsClient api) : base(api) { }
/// <param name="voice"><see cref="Voice"/> to use.</param>
/// <param name="voiceSettings">Optional, <see cref="VoiceSettings"/> that will override the default settings in <see cref="Voice.Settings"/>.</param>
/// <param name="saveDirectory">Optional, The save directory to save the audio clip.</param>
/// <param name="deleteCachedFile">Optional, deletes the cached file for this text string. Default is false.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>Downloaded clip path.</returns>
public async Task<string> TextToSpeechAsync(string text, Voice voice, VoiceSettings voiceSettings = null, string saveDirectory = null, CancellationToken cancellationToken = default)
public async Task<string> TextToSpeechAsync(string text, Voice voice, VoiceSettings voiceSettings = null, string saveDirectory = null, bool deleteCachedFile = false, CancellationToken cancellationToken = default)
{
if (text.Length > 5000)
{
Expand All @@ -39,6 +41,11 @@ public async Task<string> TextToSpeechAsync(string text, Voice voice, VoiceSetti
var fileName = $"{text.GenerateGuid()}.mp3";
var filePath = Path.Combine(downloadDirectory, fileName);

if (File.Exists(filePath) && deleteCachedFile)
{
File.Delete(filePath);
}

if (!File.Exists(filePath))
{
var defaultVoiceSettings = voiceSettings ?? voice.Settings ?? await Api.VoicesEndpoint.GetDefaultVoiceSettingsAsync(cancellationToken);
Expand Down Expand Up @@ -70,17 +77,5 @@ public async Task<string> TextToSpeechAsync(string text, Voice voice, VoiceSetti

return filePath;
}

/// <summary>
/// Converts text into speech using a voice of your choice and returns audio as an audio stream.
/// </summary>
/// <param name="text">Text input to synthesize speech for.</param>
/// <param name="voice"><see cref="Voice"/> to use.</param>
/// <param name="voiceSettings">Optional, <see cref="VoiceSettings"/> that will override the default settings in <see cref="voice"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
public Task StreamTextToSpeechAsync(string text, Voice voice, VoiceSettings voiceSettings = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
}
1 change: 1 addition & 0 deletions ElevenLabs-DotNet/Users/UserEndpoint.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 ElevenLabs.Extensions;
using System.Text.Json;
using System.Threading.Tasks;

Expand Down
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 ElevenLabs.Extensions;
using ElevenLabs.Voices;
using System;
using System.IO;
Expand Down
5 changes: 5 additions & 0 deletions ElevenLabs-DotNet/Voices/Voice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace ElevenLabs.Voices
{
public sealed class Voice
{
public Voice(string id)
{
Id = id;
}

public static implicit operator string(Voice voice) => voice.Id;

[JsonInclude]
Expand Down
1 change: 1 addition & 0 deletions ElevenLabs-DotNet/Voices/VoicesEndpoint.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 ElevenLabs.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down

0 comments on commit 8aa912c

Please sign in to comment.