2024-08-28 16:08:12 +00:00
|
|
|
using OpenAI.Audio;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.OpenAI.Providers.Audio;
|
|
|
|
|
|
|
|
|
|
public partial class AudioCompletionProvider
|
|
|
|
|
{
|
2024-08-28 18:43:37 +00:00
|
|
|
public async Task<BinaryData> GenerateAudioFromTextAsync(string text)
|
2024-08-28 16:08:12 +00:00
|
|
|
{
|
|
|
|
|
var audioClient = ProviderHelper.GetClient(Provider, _model, _services)
|
|
|
|
|
.GetAudioClient(_model);
|
|
|
|
|
|
2024-08-28 18:43:37 +00:00
|
|
|
var (voice, options) = PrepareGenerationOptions();
|
2024-10-02 18:00:23 +00:00
|
|
|
var result = await audioClient.GenerateSpeechAsync(text, voice, options);
|
2024-08-28 16:08:12 +00:00
|
|
|
return result.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 18:43:37 +00:00
|
|
|
private (GeneratedSpeechVoice, SpeechGenerationOptions) PrepareGenerationOptions()
|
2024-08-28 16:08:12 +00:00
|
|
|
{
|
2024-08-28 18:43:37 +00:00
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
var voice = GetVoice(state.GetState("speech_generate_voice"));
|
|
|
|
|
var format = GetSpeechFormat(state.GetState("speech_generate_format"));
|
|
|
|
|
var speed = GetSpeed(state.GetState("speech_generate_speed"));
|
2024-08-28 16:08:12 +00:00
|
|
|
|
2024-08-28 18:43:37 +00:00
|
|
|
var options = new SpeechGenerationOptions
|
|
|
|
|
{
|
|
|
|
|
ResponseFormat = format,
|
2024-10-02 18:00:23 +00:00
|
|
|
SpeedRatio = speed
|
2024-08-28 16:08:12 +00:00
|
|
|
};
|
2024-08-28 18:43:37 +00:00
|
|
|
|
|
|
|
|
return (voice, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private GeneratedSpeechVoice GetVoice(string input)
|
|
|
|
|
{
|
|
|
|
|
var value = !string.IsNullOrEmpty(input) ? input : "alloy";
|
|
|
|
|
|
|
|
|
|
GeneratedSpeechVoice voice;
|
|
|
|
|
switch (value)
|
|
|
|
|
{
|
|
|
|
|
case "echo":
|
|
|
|
|
voice = GeneratedSpeechVoice.Echo;
|
|
|
|
|
break;
|
|
|
|
|
case "fable":
|
|
|
|
|
voice = GeneratedSpeechVoice.Fable;
|
|
|
|
|
break;
|
|
|
|
|
case "onyx":
|
|
|
|
|
voice = GeneratedSpeechVoice.Onyx;
|
|
|
|
|
break;
|
|
|
|
|
case "nova":
|
|
|
|
|
voice = GeneratedSpeechVoice.Nova;
|
|
|
|
|
break;
|
|
|
|
|
case "shimmer":
|
|
|
|
|
voice = GeneratedSpeechVoice.Shimmer;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
voice = GeneratedSpeechVoice.Alloy;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return voice;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private GeneratedSpeechFormat GetSpeechFormat(string input)
|
|
|
|
|
{
|
|
|
|
|
var value = !string.IsNullOrEmpty(input) ? input : "mp3";
|
|
|
|
|
|
|
|
|
|
GeneratedSpeechFormat format;
|
|
|
|
|
switch (value)
|
|
|
|
|
{
|
|
|
|
|
case "wav":
|
|
|
|
|
format = GeneratedSpeechFormat.Wav;
|
|
|
|
|
break;
|
|
|
|
|
case "opus":
|
|
|
|
|
format = GeneratedSpeechFormat.Opus;
|
|
|
|
|
break;
|
|
|
|
|
case "aac":
|
|
|
|
|
format = GeneratedSpeechFormat.Aac;
|
|
|
|
|
break;
|
|
|
|
|
case "flac":
|
|
|
|
|
format = GeneratedSpeechFormat.Flac;
|
|
|
|
|
break;
|
|
|
|
|
case "pcm":
|
|
|
|
|
format = GeneratedSpeechFormat.Pcm;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
format = GeneratedSpeechFormat.Mp3;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return format;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float? GetSpeed(string input)
|
|
|
|
|
{
|
|
|
|
|
if (!float.TryParse(input, out var speed))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return speed;
|
2024-08-28 16:08:12 +00:00
|
|
|
}
|
|
|
|
|
}
|