一個非官方的Elevenlabs語音綜合統一遊戲引擎的RESTFULS客戶端。
基於Elevenlabs-Dotnet
我不隸屬於Elevenlabs,需要具有API訪問的帳戶。
所有版權,商標,徽標和資產都是其各自所有者的財產。
需要統一2021.3 LT或更高。
推薦的安裝方法是Unity軟件包管理器和OpenUPM。
Package Manager
OpenUPMhttps://package.openupm.comcom.rest.elevenlabscom.utilitiesMy RegistriesElevenLabs包https://github.com/RageAgainstThePixel/com.rest.elevenlabs.git#upm注意:此存儲庫對其他存儲庫有依賴性!您有責任自己添加它們。
有4種提供您的API鍵的方法,按優先順序:
var api = new ElevenLabsClient ( "yourApiKey" ) ;或手動創建ElevenLabsAuthentication對象
var api = new ElevenLabsClient ( new ElevenLabsAuthentication ( "yourApiKey" ) ) ; 您可以將密鑰直接保存到Assets/Resources文件夾中的可腳本對像中。
您可以使用Project Pane的上下文菜單創建一個新的,並創建一個新的ElevenLabsConfiguration腳本對象。

嘗試從配置文件,默認情況下加載API鍵,默認.elevenlabs下。
要創建一個配置文件,請創建一個名為.elevenlabs的新文本文件,並包含該行:
{
"apiKey" : " yourApiKey " ,
}您還可以通過調用身份驗證中的靜態方法直接加載文件:
var api = new ElevenLabsClient ( new ElevenLabsAuthentication ( ) . LoadFromDirectory ( "your/path/to/.elevenlabs" ) ) ; ; 使用系統的環境變量指定使用的API鍵。
ELEVEN_LABS_API_KEY用於API鍵。 var api = new ElevenLabsClient ( new ElevenLabsAuthentication ( ) . LoadFromEnvironment ( ) ) ;直接在前端應用程序中使用ElevenLabs-dotnet或com.rest.Rest.REST.REST.REST.REST.EREVENLABS軟件包可能會暴露您的API鍵和其他敏感信息。為了減輕這種風險,建議建立一個中間API,該API代表您的前端應用程序向Elevenlab提出請求。該庫可以用於前端和中間主機配置,以確保與Elevenlabs API的安全通信。
在前端示例中,您需要使用首選的OAuth提供商對用戶進行安全身份驗證。對用戶進行身份驗證後,將您的自定義驗證令牌與後端上的API密鑰交換。
請按照以下步驟:
ElevenLabsAuthentication對象並傳遞自定義令牌。ElevenLabsSettings對象,並指定中間API所在的域。auth和settings對像傳遞到ElevenLabsClient構造函數。這是如何設置前端的一個示例:
var authToken = await LoginAsync ( ) ;
var auth = new ElevenLabsAuthentication ( authToken ) ;
var settings = new ElevenLabsSettings ( domain : "api.your-custom-domain.com" ) ;
var api = new ElevenLabsClient ( auth , settings ) ;此設置允許您的前端應用程序與您的後端安全通信,該後端將使用Elevenlabs-dotnet-Proxy,然後將請求轉發到Eleevenlabs API。這樣可以確保您的Elevenlabs API密鑰和其他敏感信息在整個過程中保持安全。
在此示例中,我們演示瞭如何在新的ASP.NET Core Web應用程序中設置和使用ElevenLabsProxyStartup 。代理服務器將處理身份驗證和轉發請求到ElevenLabs API,以確保您的API鍵和其他敏感信息保持安全。
Install-Package ElevenLabs-DotNet-Proxydotnet add package ElevenLabs-DotNet-Proxy<PackageReference Include="ElevenLabs-DotNet-Proxy" />AbstractAuthenticationFilter繼承的新類,並覆蓋ValidateAuthenticationAsync方法。這將實現您將用於檢查內部服務器的用戶IAuthenticationFilter令牌。Program.cs中,通過調用ElevenLabsProxyStartup.CreateDefaultHost方法來創建一個新的代理Web應用程序,將您的自定義AuthenticationFilter作為類型參數傳遞。ElevenLabsAuthentication和ElevenLabsClientSettings 。 public partial class Program
{
private class AuthenticationFilter : AbstractAuthenticationFilter
{
public override async Task ValidateAuthenticationAsync ( IHeaderDictionary request )
{
await Task . CompletedTask ; // remote resource call
// You will need to implement your own class to properly test
// custom issued tokens you've setup for your end users.
if ( ! request [ "xi-api-key" ] . ToString ( ) . Contains ( TestUserToken ) )
{
throw new AuthenticationException ( "User is not authorized" ) ;
}
}
}
public static void Main ( string [ ] args )
{
var client = new ElevenLabsClient ( ) ;
var proxy = ElevenLabsProxyStartup . CreateDefaultHost < AuthenticationFilter > ( args , client ) ;
proxy . Run ( ) ;
}
}設置代理服務器後,您的最終用戶現在可以向代理API而不是直接向ElevenLabs API提出身份驗證的請求。代理服務器將處理身份驗證和轉發請求到ElevenLabs API,以確保您的API鍵和其他敏感信息保持安全。
您可以使用ElevenLabs儀表板在編輯器中從ElevenLabs網站上執行所有相同的操作!
Window/Dashboards/ElevenLabs

就像在Elevenlabs網站上一樣,您可以使用可用的聲音合成新的音頻剪輯。此工具使其更加方便,因為剪輯會自動下載並導入您的項目,可以使用!

就像在ElevenLabs網站上一樣,您可以直接在編輯器中管理所有聲音。

選擇Create New Voice將顯示一個彈出窗口,您可以在其中使用ElevenLabs生成模型設計全新的聲音。

此外,您還可以從示例錄音中克隆聲音。

您還可以訪問所有生成的樣本的完整列表,即準備下載。

將文本轉換為語音。
var api = new ElevenLabsClient ( ) ;
var text = "The quick brown fox jumps over the lazy dog." ;
var voice = ( await api . VoicesEndpoint . GetAllVoicesAsync ( ) ) . FirstOrDefault ( ) ;
var request = new TextToSpeechRequest ( voice , text ) ;
var voiceClip = await api . TextToSpeechEndpoint . TextToSpeechAsync ( request ) ;
audioSource . PlayOneShot ( voiceClip . AudioClip ) ;注意:如果要將語音剪輯保存到項目中,則需要將其從緩存路徑複製到項目中指定的位置:
voiceClip . CopyIntoProject ( editorDownloadDirectory ) ; 將文字流到語音。
var api = new ElevenLabsClient ( ) ;
var text = "The quick brown fox jumps over the lazy dog." ;
var voice = ( await api . VoicesEndpoint . GetAllVoicesAsync ( ) ) . FirstOrDefault ( ) ;
var partialClips = new Queue < VoiceClip > ( ) ;
var request = new TextToSpeechRequest ( voice , message , model : Model . EnglishTurboV2 , outputFormat : OutputFormat . PCM_44100 ) ;
var voiceClip = await api . TextToSpeechEndpoint . StreamTextToSpeechAsync ( request , partialClip =>
{
// Note: check demo scene for best practices
// on how to handle playback with OnAudioFilterRead
partialClips . Enqueue ( partialClip ) ;
} ) ;訪問用戶或Elevenlab創建的聲音。
在公共語音庫中獲取共享聲音列表。
var api = new ElevenLabsClient ( ) ;
var results = await ElevenLabsClient . SharedVoicesEndpoint . GetSharedVoicesAsync ( ) ;
foreach ( var voice in results . Voices )
{
Debug . Log ( $ " { voice . OwnerId } | { voice . VoiceId } | { voice . Date } | { voice . Name } " ) ;
} 獲取所有可用聲音的列表。
var api = new ElevenLabsClient ( ) ;
var allVoices = await api . VoicesEndpoint . GetAllVoicesAsync ( ) ;
foreach ( var voice in allVoices )
{
Debug . Log ( $ " { voice . Id } | { voice . Name } | similarity boost: { voice . Settings ? . SimilarityBoost } | stability: { voice . Settings ? . Stability } " ) ;
} 獲取全局默認語音設置。
var api = new ElevenLabsClient ( ) ;
var result = await api . VoicesEndpoint . GetDefaultVoiceSettingsAsync ( ) ;
Debug . Log ( $ "stability: { result . Stability } | similarity boost: { result . SimilarityBoost } " ) ; var api = new ElevenLabsClient ( ) ;
var voice = await api . VoicesEndpoint . GetVoiceAsync ( "voiceId" ) ;
Debug . Log ( $ " { voice . Id } | { voice . Name } | { voice . PreviewUrl } " ) ; 為特定語音編輯設置。
var api = new ElevenLabsClient ( ) ;
var success = await api . VoicesEndpoint . EditVoiceSettingsAsync ( voice , new VoiceSettings ( 0.7f , 0.7f ) ) ;
Debug . Log ( $ "Was successful? { success } " ) ; var api = new ElevenLabsClient ( ) ;
var labels = new Dictionary < string , string >
{
{ "accent" , "american" }
} ;
var audioSamplePaths = new List < string > ( ) ;
var voice = await api . VoicesEndpoint . AddVoiceAsync ( "Voice Name" , audioSamplePaths , labels ) ; var api = new ElevenLabsClient ( ) ;
var labels = new Dictionary < string , string >
{
{ "age" , "young" }
} ;
var audioSamplePaths = new List < string > ( ) ;
var success = await api . VoicesEndpoint . EditVoiceAsync ( voice , audioSamplePaths , labels ) ;
Debug . Log ( $ "Was successful? { success } " ) ; var api = new ElevenLabsClient ( ) ;
var success = await api . VoicesEndpoint . DeleteVoiceAsync ( voiceId ) ;
Debug . Log ( $ "Was successful? { success } " ) ; 訪問您的樣本,克隆聲音時創建的樣本。
var api = new ElevenLabsClient ( ) ;
var voiceClip = await api . VoicesEndpoint . DownloadVoiceSampleAsync ( voice , sample ) ; var api = new ElevenLabsClient ( ) ;
var success = await api . VoicesEndpoint . DeleteVoiceSampleAsync ( voiceId , sampleId ) ;
Debug . Log ( $ "Was successful? { success } " ) ;配音將音頻或視頻文件提供給給定的語言。
var api = new ElevenLabsClient ( ) ;
// from URI
var request = new DubbingRequest ( new Uri ( "https://youtu.be/Zo5-rhYOlNk" ) , "ja" , "en" , 1 , true ) ;
// from file
var request = new DubbingRequest ( filePath , "es" , "en" , 1 ) ;
var metadata = await api . DubbingEndpoint . DubAsync ( request , progress : new Progress < DubbingProjectMetadata > ( metadata =>
{
switch ( metadata . Status )
{
case "dubbing" :
Debug . Log ( $ "Dubbing for { metadata . DubbingId } in progress... Expected Duration: { metadata . ExpectedDurationSeconds : 0.00 } seconds" ) ;
break ;
case "dubbed" :
Debug . Log ( $ "Dubbing for { metadata . DubbingId } complete in { metadata . TimeCompleted . TotalSeconds : 0.00 } seconds!" ) ;
break ;
default :
Debug . Log ( $ "Status: { metadata . Status } " ) ;
break ;
}
} ) ) ; 返回有關配音項目的元數據,包括是否仍在進行中。
var api = new ElevenLabsClient ( ) ;
var metadata = api . await GetDubbingProjectMetadataAsync ( "dubbing - id");返回下載的配音文件路徑。
重要的
視頻將以MP4格式返回,並且音頻只會在MP3中返回配音。
var dubbedClipPath = await ElevenLabsClient . DubbingEndpoint . GetDubbedFileAsync ( metadata . DubbingId , request . TargetLanguage ) ;
var dubbedClip = await Rest . DownloadAudioClipAsync ( $ "file:// { dubbedClipPath } " , AudioType . MPEG ) ;
audioSource . PlayOneShot ( dubbedClip ) ; 返回所需格式的配音的轉錄本。
var srcFile = new FileInfo ( audioPath ) ;
var transcriptPath = new FileInfo ( $ " { srcFile . FullName } .dubbed. { request . TargetLanguage } .srt" ) ;
var transcriptFile = await ElevenLabsClient . DubbingEndpoint . GetTranscriptForDubAsync ( metadata . DubbingId , request . TargetLanguage ) ;
await File . WriteAllTextAsync ( transcriptPath . FullName , transcriptFile ) ; 刪除配音項目。
var api = new ElevenLabsClient ( ) ;
await api . DubbingEndpoint . DeleteDubbingProjectAsync ( "dubbing-id" ) ;將文本轉換為聲音並使用有史以來最高級的AI音頻模型的API。
var api = new ElevenLabsClient ( ) ;
var request = new SoundGenerationRequest ( "Star Wars Light Saber parry" ) ;
var clip = await api . SoundGenerationEndpoint . GenerateSoundAsync ( request ) ;訪問您先前合成的音頻剪輯,包括其元數據。
獲取有關您生成的所有音頻的元數據。
var api = new ElevenLabsClient ( ) ;
var historyItems = await api . HistoryEndpoint . GetHistoryAsync ( ) ;
foreach ( var item in historyItems . OrderBy ( historyItem => historyItem . Date ) )
{
Debug . Log ( $ " { item . State } { item . Date } | { item . Id } | { item . Text . Length } | { item . Text } " ) ;
} 獲取有關特定項目的信息。
var api = new ElevenLabsClient ( ) ;
var historyItem = await api . HistoryEndpoint . GetHistoryItemAsync ( voiceClip . Id ) ; var api = new ElevenLabsClient ( ) ;
var voiceClip = await api . HistoryEndpoint . DownloadHistoryAudioAsync ( historyItem ) ; 下載最後100個歷史記錄項目或指定項目的收集。
var api = new ElevenLabsClient ( ) ;
var voiceClips = await api . HistoryEndpoint . DownloadHistoryItemsAsync ( ) ;注意:要將剪輯直接複製到您的項目中,您可以調用:
VoiceClipUtilities . CopyIntoProject ( editorDownloadDirectory , downloadItems . ToArray ( ) ) ; var api = new ElevenLabsClient ( ) ;
var success = await api . HistoryEndpoint . DeleteHistoryItemAsync ( historyItem ) ;
Debug . Log ( $ "Was successful? { success } " ) ;訪問您的用戶信息和訂閱狀態。
通過ElevenLabs獲取有關您的用戶帳戶的信息。
var api = new ElevenLabsClient ( ) ;
var userInfo = await api . UserEndpoint . GetUserInfoAsync ( ) ; 獲取有關您使用ElevenLabs訂閱的信息。
var api = new ElevenLabsClient ( ) ;
var subscriptionInfo = await api . UserEndpoint . GetSubscriptionInfoAsync ( ) ;