Skip to content

Commit

Permalink
Merge pull request #729 from Maverick87Shaka/master
Browse files Browse the repository at this point in the history
Adding support for Microsoft Azure
  • Loading branch information
ciribob authored Nov 18, 2024
2 parents 3af942e + 4763939 commit 30b510f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
56 changes: 53 additions & 3 deletions DCS-SR-ExternalAudio/Audio/AudioGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
using Google.Cloud.TextToSpeech.V1;
using Grpc.Core;

using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using Ciribob.DCS.SimpleRadio.Standalone.Common.Network;


namespace Ciribob.DCS.SimpleRadio.Standalone.ExternalAudioClient.Audio
{
Expand Down Expand Up @@ -116,7 +120,7 @@ private byte[] GoogleTTS(string msg)
}
}

AudioConfig config = new AudioConfig
Google.Cloud.TextToSpeech.V1.AudioConfig config = new Google.Cloud.TextToSpeech.V1.AudioConfig
{
AudioEncoding = AudioEncoding.Linear16,
SampleRateHertz = INPUT_SAMPLE_RATE
Expand Down Expand Up @@ -155,14 +159,56 @@ private byte[] GoogleTTS(string msg)
}
return new byte[0];
}


private async Task<byte[]> AzureTTS(string msg)
{
try
{
string[] connstring = opts.AzureCredentials.Split(';');
string speechKey = connstring[0];
string speechRegion = connstring[1];

var speechConfig = SpeechConfig.FromSubscription(speechKey, speechRegion);
// Set either the `SpeechSynthesisVoiceName` or `SpeechSynthesisLanguage`.
//speechConfig.SpeechSynthesisLanguage = "en-US";
speechConfig.SpeechSynthesisLanguage = opts.Voice.Substring(0, 5);
//speechConfig.SpeechSynthesisVoiceName = "en-US-AvaMultilingualNeural";
speechConfig.SpeechSynthesisVoiceName = opts.Voice;

var speechSynthesizer = new Microsoft.CognitiveServices.Speech.SpeechSynthesizer(speechConfig, null);

var result = await speechSynthesizer.SpeakTextAsync(msg);
var stream = AudioDataStream.FromResult(result);

var tempFile = Path.GetTempFileName();
await stream.SaveToWaveFileAsync(tempFile);

byte[] bytes = null;
using (var reader = new WaveFileReader(tempFile))
{
bytes = new byte[reader.Length];
var read = reader.Read(bytes, 0, bytes.Length);
Logger.Info($"Success with Azure TTS - read {read} bytes");
}

//cleanup
File.Delete(tempFile);

return bytes;
}
catch (Exception ex)
{
Logger.Error(ex, $"Error with Azure Text to Speech: {ex.Message}");
}
return new byte[0];
}


private byte[] LocalTTS(string msg)
{
try
{
using (var synth = new SpeechSynthesizer())
using (var synth = new System.Speech.Synthesis.SpeechSynthesizer())
using (var stream = new MemoryStream())
{
if (opts.Voice == null || opts.Voice.Length == 0)
Expand Down Expand Up @@ -298,6 +344,10 @@ public List<byte[]> GetOpusBytes()
{
resampledBytes = GoogleTTS(msg);
}
else if (!string.IsNullOrEmpty(opts.AzureCredentials))
{
resampledBytes = AzureTTS(msg).GetAwaiter().GetResult();
}
else
{
resampledBytes = LocalTTS(msg);
Expand Down
7 changes: 7 additions & 0 deletions DCS-SR-ExternalAudio/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public class Options
Required = false)]
public string GoogleCredentials { get; set; }

[Option('a', "azureCredentials",
HelpText = "Use your Azure AI Speech key and region, separated by semicolon",
Required = false)]
public string AzureCredentials { get; set; }

[Option('L', "latitude",
HelpText = "Latitude of transmission",
Required = false, Default = 0.0)]
Expand Down Expand Up @@ -272,6 +277,8 @@ private static void HandleParseError(IEnumerable errs)

Console.WriteLine($"Example:\n --text=\"I want any female voice with a German accent \" --freqs=251.0 --modulations=AM --coalition=1 --gender=male --locale=de-DE --googleCredentials=\"C:\\\\folder\\\\credentials.json\" ");

Console.WriteLine($"Example:\n --text=\"I want any female voice with a German accent \" --freqs=251.0 --modulations=AM --coalition=1 --gender=male --locale=de-DE --googleCredentials=\"xxxxxxxxxxxxxxxxxx;westeurope\" ");


}

Expand Down
5 changes: 5 additions & 0 deletions DCS-SR-ExternalAudio/DCS-SR-ExternalAudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.6.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CognitiveServices.Speech.csharp, Version=1.40.0.28, Culture=neutral, PublicKeyToken=d2e6dcccb609e663, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CognitiveServices.Speech.1.40.0\lib\net462\Microsoft.CognitiveServices.Speech.csharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Win32.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -336,9 +339,11 @@
<Error Condition="!Exists('..\packages\Grpc.Core.2.46.3\build\net45\Grpc.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Grpc.Core.2.46.3\build\net45\Grpc.Core.targets'))" />
<Error Condition="!Exists('..\packages\Fody.2.5.0\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.2.5.0\build\Fody.targets'))" />
<Error Condition="!Exists('..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.CognitiveServices.Speech.1.40.0\build\net462\Microsoft.CognitiveServices.Speech.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.CognitiveServices.Speech.1.40.0\build\net462\Microsoft.CognitiveServices.Speech.targets'))" />
</Target>
<Import Project="..\packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets" Condition="Exists('..\packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets')" />
<Import Project="..\packages\Grpc.Core.2.46.3\build\net45\Grpc.Core.targets" Condition="Exists('..\packages\Grpc.Core.2.46.3\build\net45\Grpc.Core.targets')" />
<Import Project="..\packages\Fody.2.5.0\build\Fody.targets" Condition="Exists('..\packages\Fody.2.5.0\build\Fody.targets')" />
<Import Project="..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets" Condition="Exists('..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets')" />
<Import Project="..\packages\Microsoft.CognitiveServices.Speech.1.40.0\build\net462\Microsoft.CognitiveServices.Speech.targets" Condition="Exists('..\packages\Microsoft.CognitiveServices.Speech.1.40.0\build\net462\Microsoft.CognitiveServices.Speech.targets')" />
</Project>
1 change: 1 addition & 0 deletions DCS-SR-ExternalAudio/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<package id="Grpc.Core" version="2.46.3" targetFramework="net48" />
<package id="Grpc.Core.Api" version="2.48.0" targetFramework="net48" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.CognitiveServices.Speech" version="1.40.0" targetFramework="net48" />
<package id="Microsoft.NETCore.Platforms" version="6.0.5" targetFramework="net48" />
<package id="Microsoft.Win32.Primitives" version="4.3.0" targetFramework="net48" />
<package id="NAudio.Vorbis" version="1.2.0" targetFramework="net48" />
Expand Down

0 comments on commit 30b510f

Please sign in to comment.