-
-
Save druggedhippo/0a887973ee019dea1fc9e522f513b0f5 to your computer and use it in GitHub Desktop.
EDDI Amazon Polly Integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
// Nuget - AWSSDK.Polly | |
using Amazon.Polly; | |
using Amazon.Polly.Model; | |
// Nuget - NAudio.Core | |
using NAudio.Wave; | |
namespace EddiSpeechService.SpeechSynthesizers | |
{ | |
/* | |
To enable this you need to adjust SpeechService.cs to create and use this instance instead of the built in Windows TTS | |
*/ | |
class AmazonPollySynthesizer : IDisposable | |
{ | |
/** Generate these within the Amazon AWS console */ | |
string awsAccessKey = "XXXXXXXXXXXXX"; | |
string awsAccessSecret = "YYYYYYYYYYYYYYY"; | |
// Region endpoint, should choose closest to use | |
string regionEndpoint = "ap-southeast-2"; | |
// The voice must be one used by the engine, not all voices are suitable for each engine | |
VoiceId voiceID = VoiceId.Amy; | |
// Standard or neural | |
Engine engine = Engine.Neural; | |
AmazonPollyClient client; | |
public AmazonPollySynthesizer() | |
{ | |
var config = new AmazonPollyConfig(); | |
config.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(regionEndpoint); | |
client = new AmazonPollyClient(this.awsAccessKey, this.awsAccessSecret, config); | |
} | |
internal Stream Speak(VoiceDetails voiceDetails, string speech, SpeechServiceConfiguration Configuration) | |
{ | |
var synthesizeSpeechRequest = new SynthesizeSpeechRequest() | |
{ | |
Engine = engine, | |
OutputFormat = OutputFormat.Pcm, | |
VoiceId = voiceID, | |
TextType = TextType.Ssml, | |
Text = "<speak>" + speech + "</speak>", | |
}; | |
using (SynthesizeSpeechResponse synthesizeSpeechResponse = client.SynthesizeSpeech(synthesizeSpeechRequest)) | |
{ | |
using (RawSourceWaveStream w = new RawSourceWaveStream(synthesizeSpeechResponse.AudioStream, new WaveFormat(16000, 1))) | |
{ | |
MemoryStream ms = new MemoryStream(); | |
WaveFileWriter.WriteWavFileToStream(ms, w); | |
ms.Seek(0, SeekOrigin.Begin); | |
return ms; | |
} | |
} | |
} | |
public void Dispose() | |
{ | |
client.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment