예제 #1
0
        public static void start(IModHelper h)
        {
            Helper      = h;
            currentText = "";
            tmppath     = Path.Combine(Path.Combine(Environment.CurrentDirectory, "Content"), "TTS");

            ensureFolderStructureExists(Path.Combine(tmppath, "speech.mp3"));
            pc              = AWSHandler.getPollyClient();
            currentVoice    = VoiceId.Amy;
            lastText        = "";
            lastDialog      = "";
            lastHud         = "";
            speak           = false;
            runSpeech       = true;
            currentCulture  = CultureInfo.CreateSpecificCulture("en-us");
            culturelang     = "en";
            installedVoices = new List <string>();


            setupVoices();
            setVoice("default");

            speechThread = new Thread(t2sOut);
            speechThread.Start();
            GameEvents.QuarterSecondTick += GameEvents_QuarterSecondTick;

            MenuEvents.MenuClosed += MenuEvents_MenuClosed;

            //ControlEvents.KeyPressed += ControlEvents_KeyPressed;
        }
예제 #2
0
        public byte[] SynthesizeSpeech(VoiceId voiceId, string text, OutputFormat format = null, Engine engine = null)
        {
            if (format == null)
            {
                format = OutputFormat.Mp3;
            }
            var request = new SynthesizeSpeechRequest();

            request.OutputFormat = format;
            request.VoiceId      = voiceId;
            // Prefer neural voices.
            if (engine != null)
            {
                request.Engine = engine;
            }
            request.Text     = text;
            request.TextType = TextType.Ssml;
            var ms         = new MemoryStream();
            var response   = Client.SynthesizeSpeech(request);
            var bufferSize = 2 * 1024;
            var buffer     = new byte[bufferSize];
            int readBytes;
            var inputStream = response.AudioStream;

            while ((readBytes = inputStream.Read(buffer, 0, bufferSize)) > 0)
            {
                ms.Write(buffer, 0, readBytes);
            }
            var bytes = ms.ToArray();

            ms.Dispose();
            return(bytes);
        }
예제 #3
0
        public static void setVoice(string name, bool female = true)
        {
            speakerName = name;

            string t = PelicanTTSMod.i18n.Get(name);

            if (PelicanTTSMod.i18n.LocaleEnum == LocalizedContentManager.LanguageCode.en && PelicanTTSMod.config.Voices.ContainsKey(name))
            {
                t = PelicanTTSMod.config.Voices[name].Voice;
            }

            if (t.ToString() == "")
            {
                t = PelicanTTSMod.i18n.Get("default_" + (female ? "female" : "male"));
            }

            if (VoiceId.FindValue(t) is VoiceId vId1)
            {
                currentVoice = vId1;
            }
            else if (VoiceId.FindValue(PelicanTTSMod.i18n.Get("default")) is VoiceId vId2)
            {
                currentVoice = vId2;
            }
            else
            {
                speakerName  = "default";
                currentVoice = VoiceId.Salli;
            }
        }
예제 #4
0
        public async Task getVoice(CommandContext ctx)
        {
            var ActualVoice = VoiceId.FindValue(Settings.PSettings.PollyVoice);
            await ctx.RespondAsync("Current voice: " + ActualVoice);

            await ctx.Message.DeleteAsync();
        }
예제 #5
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (HasVoiceId)
            {
                hash ^= VoiceId.GetHashCode();
            }
            if (HasToken)
            {
                hash ^= Token.GetHashCode();
            }
            if (HasUrl)
            {
                hash ^= Url.GetHashCode();
            }
            if (HasJoinType)
            {
                hash ^= JoinType.GetHashCode();
            }
            if (HasMuteReason)
            {
                hash ^= MuteReason.GetHashCode();
            }
            if (_unknownFields != null)
            {
                hash ^= _unknownFields.GetHashCode();
            }
            return(hash);
        }
예제 #6
0
        public async Task UtterInStream(string text, VoiceId voice, Action <byte[], int> dataAvailable)
        {
            var req = new SynthesizeSpeechRequest();

            req.VoiceId      = voice;
            req.TextType     = TextType.Text;
            req.Text         = text;
            req.OutputFormat = OutputFormat.Pcm;
            req.SampleRate   = "16000";
            var response = await polly.SynthesizeSpeechAsync(req);

            using (MemoryStream ms = new MemoryStream())
            {
                response.AudioStream.CopyTo(ms);
                ms.Flush();
                ms.Position = 0;

                var buffer = new byte[1 * 640];
                int bytesRead;
                while ((bytesRead = await ms.ReadAsync(buffer, 0, buffer.Length)) > 0)
                {
                    dataAvailable(buffer, bytesRead);
                }
                ;
            }
        }
예제 #7
0
        public static void setVoice(string name, bool female = true)
        {
            speakerName = name;

            string t = PelicanTTSMod.i18n.Get(name);

            if (t.ToString() == "")
            {
                t = PelicanTTSMod.i18n.Get("default_" + (female ? "female" : "male"));
            }

            if (VoiceId.FindValue(t) is VoiceId vId1)
            {
                currentVoice = vId1;
            }
            else if (VoiceId.FindValue(PelicanTTSMod.i18n.Get("default")) is VoiceId vId2)
            {
                currentVoice = vId2;
            }
            else
            {
                speakerName  = "default";
                currentVoice = VoiceId.Salli;
            }
        }
예제 #8
0
        public async Task <string> Utter(string text, string dir, VoiceId voice)
        {
            string speaker = "Chatbot";

            ConsoleLogger.WriteLine(speaker, $"{text}");

            string fileName = (text + "-" + voice).GetMd5Hash() + ".mp3";

            string recordBasePath = $"wwwroot{Path.DirectorySeparatorChar}voice";
            string filePath       = $"{dir}{Path.DirectorySeparatorChar}{recordBasePath}{Path.DirectorySeparatorChar}{fileName}";

            if (File.Exists(filePath))
            {
                return($"/voice/{fileName}");
            }

            SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();

            sreq.Text         = text;
            sreq.OutputFormat = OutputFormat.Mp3;
            sreq.VoiceId      = voice;
            SynthesizeSpeechResponse sres = await polly.SynthesizeSpeechAsync(sreq);

            using (FileStream fileStream = File.Create(filePath))
            {
                sres.AudioStream.CopyTo(fileStream);
                fileStream.Flush();
                fileStream.Close();
            }

            return($"/voice/{fileName}");
        }
예제 #9
0
        public async Task JokeDump(CommandContext ctx)
        {
            var client  = new RestClient("https://icanhazdadjoke.com");
            var request = new RestRequest();

            request.Method = Method.GET;
            request.AddHeader("Accept", "text/plain");
            var response    = client.Execute(request);
            var ActualVoice = VoiceId.FindValue(Settings.PSettings.PollyVoice);
            // resolve a track from youtube
            //var myTrack = await audioService.GetTrackAsync("The noob song", SearchMode.YouTube);
            var SpeechResponse = await Program.Polly.SynthesizeSpeechAsync(new SynthesizeSpeechRequest
            {
                Engine       = Engine.Neural,
                LanguageCode = LanguageCode.EnUS,
                OutputFormat = OutputFormat.Mp3,
                SampleRate   = "24000",
                TextType     = TextType.Text,
                Text         = response.Content,
                VoiceId      = ActualVoice
            });

            var    g    = Guid.NewGuid();
            string path = $@"{g}.Mp3";
            //FileStream f = new IsolatedStorageFileStream(path, FileMode.CreateNew, FileAccess.ReadWrite);
            //await SpeechResponse.AudioStream.CopyToAsync(f);
            await ctx.RespondWithFileAsync(DateTime.UtcNow.ToString("F") + ".Mp3", SpeechResponse.AudioStream);

            await ctx.Message.DeleteAsync();

            //await ctx.Message.CreateReactionAsync(DiscordEmoji.FromName(ctx.Client, ":white_check_mark:"));
        }
예제 #10
0
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            var radioButton = (RadioButton)sender;

            var name = radioButton.Name;

            if (name == "Joanna")
            {
                VoiceName = VoiceId.Joanna;
            }

            if (name == "Salli")
            {
                VoiceName = VoiceId.Salli;
            }

            if (name == "Matthew")
            {
                VoiceName = VoiceId.Matthew;
            }

            if (name == "Justin")
            {
                VoiceName = VoiceId.Justin;
            }
        }
예제 #11
0
        public string GetCurrentFilePath(string root, string ext = "wav")
        {
            string dirname = Path.Combine(root, FolderName);

            if (!Directory.Exists(dirname))
            {
                return(null);
            }
            string path = Path.Combine(dirname, $"{VoiceId}.{ext}");

            if (File.Exists(path))
            {
                return(path);
            }
            var files = Directory.GetFiles(dirname, $"{VoiceId}_*.{ext}");

            if (files.Length == 1)
            {
                return(files[0]);
            }
            string voiceIdZen = VoiceId.ConvertNumHanToZen();

            path = Path.Combine(dirname, $"{voiceIdZen}.{ext}");
            if (File.Exists(path))
            {
                return(path);
            }
            files = Directory.GetFiles(dirname, $"{voiceIdZen}_*.{ext}");
            if (files.Length == 1)
            {
                return(files[0]);
            }
            return(null);
        }
예제 #12
0
        public static void start(IModHelper h)
        {
            Helper      = h;
            currentText = "";
            tmppath     = Path.Combine(Helper.DirectoryPath, "TTS");

            if (!Directory.Exists(tmppath))
            {
                Directory.CreateDirectory(tmppath);
            }

            if (pc == null)
            {
                pc = AWSHandler.getPollyClient();
            }

            currentVoice = VoiceId.Salli;
            lastText     = "";
            lastDialog   = "";
            lastHud      = "";
            speak        = false;
            runSpeech    = true;

            setVoice("default");

            speechThread = new Thread(t2sOut);
            speechThread.Start();
            h.Events.GameLoop.UpdateTicked += OnUpdateTicked;
            h.Events.Display.MenuChanged   += OnMenuChanged;
        }
예제 #13
0
        /// <summary>
        /// Get voice id from string name.
        /// List of voices available: https://docs.aws.amazon.com/polly/latest/dg/voicelist.html
        /// </summary>
        /// <param name="voice">Voice. Default is Joanna in English, US.</param>
        /// <returns></returns>
        static private VoiceId VoiceIdFromString(string voice = "")
        {
            VoiceId voiceId = VoiceId.Joanna; // Default

            switch (voice)
            {
            // English, US
            case "Salli": voiceId = VoiceId.Salli; break;             // Salli, Female

            case "Kimberly": voiceId = VoiceId.Kimberly; break;       // Kimberly, Female

            case "Kendra": voiceId = VoiceId.Kendra; break;           // Kendra, Female

            // case "Joanna": voiceId = VoiceId.Joanna; break;       // Joanna, Female
            case "Ivy": voiceId = VoiceId.Ivy; break;                 // Ivy, Female

            case "Matthew": voiceId = VoiceId.Matthew; break;         // Matthew, Male

            case "Justin": voiceId = VoiceId.Justin; break;           // Justin, Male

            case "Joey": voiceId = VoiceId.Joey; break;               // Joey, Male

            // English, British
            case "Emma": voiceId = VoiceId.Emma; break;               // Emma, Female

            case "Amy": voiceId = VoiceId.Amy; break;                 // Amy, Female

            case "Brian": voiceId = VoiceId.Brian; break;             // Brian, Female
            }

            return(voiceId);
        }
        public PollySpeechSynthesizer(IAmazonPolly amazonPolly, IOptions <PollySpeechSynthesizerConfiguration> configOptions)
        {
            if (configOptions != null && configOptions.Value != null)
            {
                voiceId = VoiceId.FindValue(configOptions.Value.VoiceId);
            }

            _amazonPolly = amazonPolly;
        }
예제 #15
0
        private void Speak(string message, VoiceId voiceId, AmazonPollyClient pollyClient)
        {
            var audioConfiguration = configurationManager.LoadConfiguration <AudioConfiguration>();

            audioConfiguration.InitializeConfiguration();

            const int volume = 100;

            if (pollyClient == null)
            {
                pollyClient = GetPollyClient();
            }

            if (voiceId == null)
            {
                voiceId = pollyClient.DescribeVoices(new Amazon.Polly.Model.DescribeVoicesRequest {
                    LanguageCode = "de-DE"
                }).Voices.FirstOrDefault()?.Id;

                if (voiceId == null)
                {
                    return;
                }
            }

            string ttsPath = Path.Combine(soundPathProvider.Path, "tts");

            if (!Directory.Exists(ttsPath))
            {
                Directory.CreateDirectory(ttsPath);
            }

            var synthesizeResponse = pollyClient.SynthesizeSpeech(new Amazon.Polly.Model.SynthesizeSpeechRequest()
            {
                OutputFormat = OutputFormat.Mp3, Text = message, VoiceId = voiceId
            });
            string ttsFilePath = Path.Combine(ttsPath, "tts.mp3");

            using var fileStream = new FileStream(ttsFilePath, FileMode.Create);
            synthesizeResponse.AudioStream.CopyTo(fileStream);

            synthesizeResponse.AudioStream.Close();
            synthesizeResponse.AudioStream.Dispose();

            using var reader       = new MediaFoundationReader(ttsFilePath);
            using var volumeStream = new WaveChannel32(reader);
            using var outputStream = new WasapiOut(audioConfiguration.SelectedSoundCommandDevice, NAudio.CoreAudioApi.AudioClientShareMode.Shared, false, 10);
            volumeStream.Volume    = NormalizeVolume(volume);
            outputStream.Init(volumeStream);
            outputStream.Play();

            Thread.Sleep(reader.TotalTime.Add(TimeSpan.FromMilliseconds(100)));

            outputStream.Stop();
        }
예제 #16
0
 protected internal static void BuildModel(ModelBuilder modelBuilder)
 {
     modelBuilder.Entity <AllowedConversationVoice>(entity =>
     {
         entity.HasKey(nameof(ChannelId), nameof(VoiceId));
         entity.Property(e => e.VoiceId).HasConversion(
             v => v.ToString(),
             v => VoiceId.FindValue(v)
             );
     });
 }
예제 #17
0
        public async Task speakssml(CommandContext ctx, [RemainingText] string textToSpeak)
        {
            try
            {
                var player = Program.audioService.GetPlayer <QueuedLavalinkPlayerV2>(ctx.Guild.Id)
                             ?? await Program.audioService.JoinAsync <QueuedLavalinkPlayerV2>(ctx.Guild.Id,
                                                                                              ctx.Member.VoiceState.Channel.Id);

                var ActualVoice = VoiceId.FindValue(Settings.PSettings.PollyVoice);
                // resolve a track from youtube
                //var myTrack = await audioService.GetTrackAsync("The noob song", SearchMode.YouTube);
                foreach (var user in ctx.Message.MentionedUsers)
                {
                    Console.WriteLine(user.Mention.ToString());
                    var DisMem = await ctx.Guild.GetMemberAsync(user.Id);

                    var callout = DisMem.Nickname.IsNullOrEmpty() ? DisMem.DisplayName : DisMem.Nickname;
                    textToSpeak = textToSpeak.Replace(user.Mention.ToString(), callout);
                }

                var SpeechResponse = await Program.Polly.SynthesizeSpeechAsync(new SynthesizeSpeechRequest
                {
                    Engine       = Engine.Standard,
                    LanguageCode = LanguageCode.EnUS,
                    OutputFormat = OutputFormat.Mp3,
                    SampleRate   = "24000",
                    TextType     = TextType.Ssml,
                    Text         = textToSpeak,
                    VoiceId      = ActualVoice
                });

                var        g    = Guid.NewGuid();
                string     path = $@"C:\temp\{g}.Mp3";
                FileStream f    = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite);
                await SpeechResponse.AudioStream.CopyToAsync(f);

                f.Flush();
                f.Close();
                var track = await Program.audioService.GetTrackAsync(HttpUtility.UrlEncode(path));

                // play track
                await player.PlayAsync(track);

                await ctx.Message.DeleteAsync();

                //await ctx.Message.CreateReactionAsync(DiscordEmoji.FromName(ctx.Client, ":white_check_mark:"));
            }
            catch (Exception e)
            {
                Console.WriteLine(textToSpeak);
            }
        }
        public async Task <Stream> SynthesizeTextToStreamAsync(IVoice voice, string text)
        {
            var pollyVoice = (AmazonPollyVoice)voice;
            var request    = new SynthesizeSpeechRequest()
            {
                Text         = text,
                VoiceId      = VoiceId.FindValue(pollyVoice.VoiceId),
                OutputFormat = OutputFormat.Mp3
            };

            var response = await _client.SynthesizeSpeechAsync(request);

            return(response.AudioStream);
        }
예제 #19
0
 public static void setVoiceById(string id)
 {
     if (VoiceId.FindValue(id) is VoiceId vId1)
     {
         currentVoice = vId1;
     }
     else if (VoiceId.FindValue(PelicanTTSMod.i18n.Get("default")) is VoiceId vId2)
     {
         currentVoice = vId2;
     }
     else
     {
         speakerName  = "default";
         currentVoice = VoiceId.Salli;
     }
 }
예제 #20
0
        public async Task Say(Engine engine, VoiceId voice, int sampleRate, float volume, IList <string> lexicons, TextSource source, string text)
        {
            var req = new SynthesizeSpeechRequest
            {
                Text         = text,
                VoiceId      = voice,
                Engine       = engine,
                OutputFormat = OutputFormat.Mp3,
                SampleRate   = sampleRate.ToString(),
                LexiconNames = lexicons.Where(l => !string.IsNullOrEmpty(l)).ToList(),
                TextType     = TextType.Ssml,
            };

            SynthesizeSpeechResponse res;

            try
            {
                res = await this.client.SynthesizeSpeechAsync(req);
            }
            catch (LexiconNotFoundException e)
            {
                if (lexicons.Any())
                {
                    PluginLog.LogError(e, "A lexicon could not be found, retrying without any lexicons... Additional data: {@Data}", e.Data);
                    await Say(engine, voice, sampleRate, volume, Array.Empty <string>(), source, text);
                }
                else
                {
                    PluginLog.LogError(e, "A lexicon could not be found... but we aren't using any lexicons? Additional data: {@Data}", e.Data);
                }

                return;
            }
            catch (Exception e)
            {
                PluginLog.LogError(e, "Synthesis request failed in {0}.", nameof(PollyClient));
                return;
            }

            var responseStream = new MemoryStream();
            await res.AudioStream.CopyToAsync(responseStream);

            responseStream.Seek(0, SeekOrigin.Begin);

            this.soundQueue.EnqueueSound(responseStream, source, volume);
        }
예제 #21
0
 protected internal static void BuildModel(ModelBuilder modelBuilder)
 {
     modelBuilder.Entity <Reward>(entity =>
     {
         entity.Property(e => e.IsConversation).HasDefaultValue(true);
         entity.Property(e => e.IsSubOnly).HasDefaultValue(false);
         entity.Property(e => e.Cooldown).HasDefaultValue(0);
         entity.Property(e => e.DefaultPlaybackSpeed).HasDefaultValue(1.0f);
         entity.Property(e => e.VoiceId).HasConversion(
             v => v.ToString(),
             v => VoiceId.FindValue(v)
             );
         entity.Property(e => e.VoiceEngine).HasConversion(
             e => e.ToString(),
             e => Engine.FindValue(e)
             );
     });
 }
예제 #22
0
        public async Task speakdump(CommandContext ctx, [RemainingText] string textToSpeak)
        {
            try
            {
                var ActualVoice = VoiceId.FindValue(Settings.PSettings.PollyVoice);
                // resolve a track from youtube
                //var myTrack = await audioService.GetTrackAsync("The noob song", SearchMode.YouTube);
                foreach (var user in ctx.Message.MentionedUsers)
                {
                    Console.WriteLine(user.Mention.ToString());
                    var DisMem = await ctx.Guild.GetMemberAsync(user.Id);

                    var callout = DisMem.Nickname.IsNullOrEmpty() ? DisMem.DisplayName : DisMem.Nickname;
                    textToSpeak = textToSpeak.Replace(user.Mention.ToString(), callout);
                }
                var SpeechResponse = await Program.Polly.SynthesizeSpeechAsync(new SynthesizeSpeechRequest
                {
                    Engine       = Engine.Neural,
                    LanguageCode = LanguageCode.EnUS,
                    OutputFormat = OutputFormat.Mp3,
                    SampleRate   = "24000",
                    TextType     = TextType.Text,
                    Text         = textToSpeak,
                    VoiceId      = ActualVoice
                });

                var    g    = Guid.NewGuid();
                string path = $@"{g}.Mp3";
                //FileStream f = new IsolatedStorageFileStream(path, FileMode.CreateNew, FileAccess.ReadWrite);
                //await SpeechResponse.AudioStream.CopyToAsync(f);
                await ctx.RespondWithFileAsync(DateTime.UtcNow.ToString("F") + ".Mp3", SpeechResponse.AudioStream);

                await ctx.Message.DeleteAsync();

                //await ctx.Message.CreateReactionAsync(DiscordEmoji.FromName(ctx.Client, ":white_check_mark:"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #23
0
 public void Voice(VoiceId v)
 {
     if (v == VoiceId.Get)
     {
         audioSource.clip = getVoice;
         audioSource.Play();
     }
     if (v == VoiceId.Jump)
     {
         audioSource.clip = jumpVoice;
         audioSource.Play();
     }
     if (v == VoiceId.Damage)
     {
         int n;
         n = Random.Range(0, 4);             // Random.Range(0, damageVoice.Length) と書くと安全
         audioSource.clip = damageVoice[n];
         audioSource.Play();
     }
 }
예제 #24
0
        public async Task Joke(CommandContext ctx)
        {
            var client  = new RestClient("https://icanhazdadjoke.com");
            var request = new RestRequest();

            request.Method = Method.GET;
            request.AddHeader("Accept", "text/plain");
            var response = client.Execute(request);
            var player   = Program.audioService.GetPlayer <QueuedLavalinkPlayerV2>(ctx.Guild.Id)
                           ?? await Program.audioService.JoinAsync <QueuedLavalinkPlayerV2>(ctx.Guild.Id, ctx.Member.VoiceState.Channel.Id);

            var ActualVoice = VoiceId.FindValue(Settings.PSettings.PollyVoice);
            // resolve a track from youtube
            //var myTrack = await audioService.GetTrackAsync("The noob song", SearchMode.YouTube);
            var SpeechResponse = await Program.Polly.SynthesizeSpeechAsync(new SynthesizeSpeechRequest
            {
                Engine       = Engine.Neural,
                LanguageCode = LanguageCode.EnUS,
                OutputFormat = OutputFormat.Mp3,
                SampleRate   = "24000",
                TextType     = TextType.Text,
                Text         = response.Content,
                VoiceId      = ActualVoice
            });

            var        g    = Guid.NewGuid();
            string     path = $@"C:\temp\{g}.Mp3";
            FileStream f    = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite);
            await SpeechResponse.AudioStream.CopyToAsync(f);

            f.Flush();
            f.Close();
            var track = await Program.audioService.GetTrackAsync(HttpUtility.UrlEncode(path));

            // play track
            await player.PlayAsync(track);

            await ctx.Message.DeleteAsync();

            //await ctx.Message.CreateReactionAsync(DiscordEmoji.FromName(ctx.Client, ":white_check_mark:"));
        }
예제 #25
0
        public async Task <byte[]> Speech(string query, string languageCode) // Получение аудио-файла озвученного слова (через api Amazon Polly)
        {
            var lang = _languageService.GetLanguage(languageCode);

            var client = new AmazonPollyClient(_awsPollyOptions.Value.awsAccessKeyId, _awsPollyOptions.Value.awsSecretAccessKey, RegionEndpoint.USEast2);

            var synthesizeSpeechRequest = new SynthesizeSpeechRequest()
            {
                OutputFormat = OutputFormat.Mp3,
                LanguageCode = lang.LanguageCode,
                VoiceId      = VoiceId.FindValue(lang.VoiceId),
                Text         = query
            };

            var synthesizeSpeechResponse = await client.SynthesizeSpeechAsync(synthesizeSpeechRequest);

            var inputStream = synthesizeSpeechResponse.AudioStream;

            MemoryStream mem = new MemoryStream();

            inputStream.CopyTo(mem);
            return(mem.ToArray());
        }
예제 #26
0
 public async Task <SynthesizeSpeechResponse> Synthesize(string text, VoiceId voiceId, Engine engine, TextType textType = null)
 {
     if (text.Trim().ToLowerInvariant().StartsWith("ssml: "))
     {
         text     = text.Trim()[6..];
예제 #27
0
        public static void setVoice(string name)
        {
            speakerName = name;
            NPC speaker = Game1.getCharacterFromName(name);

            if (speaker != null && speaker.Gender == 0)
            {
                if (speaker.Age == 0)
                {
                    currentVoice = VoiceId.Joey;
                }
                else
                {
                    currentVoice = VoiceId.Joey;
                }
            }


            if (speaker != null && speaker.Gender == 1)
            {
                if (speaker.Age == 0)
                {
                    currentVoice = VoiceId.Kendra;
                }
                else
                {
                    currentVoice = VoiceId.Salli;
                }
            }


            switch (name)
            {
            case "Elliot": currentVoice = VoiceId.Geraint; break;

            case "Sam": currentVoice = VoiceId.Russell; break;

            case "Emily": currentVoice = VoiceId.Emma; break;

            case "Haley": currentVoice = VoiceId.Emma; break;

            case "Harvey": currentVoice = VoiceId.Matthew; break;

            case "George": currentVoice = VoiceId.Brian; break;

            case "Linus": currentVoice = VoiceId.Brian; break;

            case "Lewis": currentVoice = VoiceId.Brian; break;

            case "Governor": currentVoice = VoiceId.Brian; break;

            case "Grandpa": currentVoice = VoiceId.Brian; break;

            case "Clint": currentVoice = VoiceId.Brian; break;

            case "Willy": currentVoice = VoiceId.Brian; break;

            case "Wizard": currentVoice = VoiceId.Geraint; break;

            case "Pierre": currentVoice = VoiceId.Matthew; break;

            case "Gunther": currentVoice = VoiceId.Brian; break;

            case "Govenor": currentVoice = VoiceId.Brian; break;

            case "Marlon": currentVoice = VoiceId.Brian; break;

            case "Morris": currentVoice = VoiceId.Geraint; break;

            case "Mister Qi": currentVoice = VoiceId.Geraint; break;

            case "Gil": currentVoice = VoiceId.Brian; break;

            case "Penny": currentVoice = VoiceId.Amy; break;

            case "Evelyn": currentVoice = VoiceId.Amy; break;

            case "Jas": currentVoice = VoiceId.Ivy; break;

            case "Jodi": currentVoice = VoiceId.Nicole; break;

            case "Marnie": currentVoice = VoiceId.Kimberly; break;

            case "Pam": currentVoice = VoiceId.Kimberly; break;

            case "Sandy": currentVoice = VoiceId.Raveena; break;

            case "Vincent": currentVoice = VoiceId.Justin; break;

            case "default": currentVoice = VoiceId.Salli; break;


            default: break;
            }
        }
예제 #28
0
 private void femaleVoice_CheckedChanged(object sender, EventArgs e)
 {
     _voiceId = VoiceId.Tatyana;
 }
예제 #29
0
        /// <summary>
        /// PollyNotes-DictateFunction
        ///
        /// This lambda function is integrated with the following API methods:
        /// /notes/{id}/POST
        ///
        /// This function does the following:
        ///
        /// 1. Takes a JSON payload from API gateway and converts it into a DictateRequest
        /// 2. Queries DynamoDB for the note from the request to fetch the note text
        /// 3. Calls the Polly synthensize_speech API to convert text to speech
        /// 4. Stores the resulting audio in an MP3 file
        /// 5. Uploads the MP3 file to S3
        /// 6. Creates a pre-signed URL for the MP3 file
        /// 7. Returns the pre-signed URL to API Gateway
        /// </summary>
        /// <param name="request">DictateRequest containing the voiceId and the note to create an mp3 file from</param>
        /// <param name="context">Lambda context</param>
        /// <returns>string of the URL for the pre-signed mp3 file from S3</returns>
        public string FunctionHandler(DictateRequest request, ILambdaContext context)
        {
            // The note object contains the voiceId, userId and noteId from the /notes/{id}/POST
            // {
            //   "voiceId": "...",
            //     "note": {
            //       "userId": "...",
            //       "noteId": "..."
            //     }
            // }

            Console.WriteLine("Initiating PollyNotes-DictateFunction...");
            Console.WriteLine("DictateRequest received: " + JsonConvert.SerializeObject(request));

            // Get the name of the bucketName from the environment variable MP3_BUCKET_NAME
            string bucketName = Environment.GetEnvironmentVariable("MP3_BUCKET_NAME");

            // Create the DynamoDB client and the Context for Object Persistence Model
            AmazonDynamoDBClient client     = new AmazonDynamoDBClient();
            DynamoDBContext      ddbcontext = new DynamoDBContext(client);

            // Use the LoadAsync method to fetch all of the attributes of the note from the request from DynamoDB and wait
            // This is really to get the note attribute from the userId and noteId of the request
            var ddbTask = ddbcontext.LoadAsync(request.note);

            ddbTask.Wait();

            // The result will be stored in note
            Note note;

            // If there are no Exceptions
            if (ddbTask.Exception == null)
            {
                Console.WriteLine("Successfully executed LoadAsync with userId: " + request.note.userId + " and noteId: " + request.note.noteId);

                // Set the note variable to the result of the LoadAsync from DynamoDB
                note = ddbTask.Result;
            }
            else
            {
                // There was an exception, log the entry data and the exception
                Console.WriteLine("Unable to LoadAsync note with userId: " + request.note.userId + " and noteId: " + request.note.noteId);
                Console.WriteLine(ddbTask.Exception);

                // Return an empty string
                return("");
            }

            // Invoke Polly API, which will transform text into audio using the note we fetched from DynamoDB and the voiceId from the request
            var polly = new AmazonPollyClient();
            SynthesizeSpeechRequest speechRequest = new SynthesizeSpeechRequest
            {
                OutputFormat = OutputFormat.Mp3,
                Text         = note.note,
                VoiceId      = VoiceId.FindValue(request.voiceId)
            };
            var pollyTask = polly.SynthesizeSpeechAsync(speechRequest);

            pollyTask.Wait();
            Console.WriteLine("Successfully synthesized the Note text");

            // Save the audio stream returned by Amazon Polly on Lambda's temp directory '/tmp'
            string path = Path.Combine(
                Path.GetTempPath(),
                bucketName,
                request.note.userId);
            string filename = Path.Combine(path, request.note.noteId + ".mp3");

            Directory.CreateDirectory(path);
            using (FileStream file = new FileStream(filename, FileMode.Create, System.IO.FileAccess.Write))
            {
                pollyTask.Result.AudioStream.CopyTo(file);
            }
            Console.WriteLine("Successfully saved the Polly AudioStream to " + filename);

            // Upload our local file to S3
            var s3     = new AmazonS3Client();
            var s3Task = s3.PutObjectAsync(new PutObjectRequest
            {
                BucketName = bucketName,
                Key        = String.Format("{0}/{1}.mp3", request.note.userId, request.note.noteId),
                FilePath   = filename
            });

            s3Task.Wait();
            Console.WriteLine("Successfully uploaded the MP3 file to S3");

            // Generate a pre-signed URL so that we can securely access our MP3
            string url = s3.GetPreSignedURL(new GetPreSignedUrlRequest
            {
                BucketName = bucketName,
                Key        = String.Format("{0}/{1}.mp3", request.note.userId, request.note.noteId),
                Expires    = DateTime.Now + TimeSpan.FromHours(1)
            });

            Console.WriteLine("Successfully generated a pre-signed URL for the MP3 file: " + url);

            // Return the presigned URL to API Gateway
            return(url);
        }
예제 #30
0
        internal static void configSay(string name, string voice, string text, int rate = -1, float pitch = -1, float volume = -1)
        {
            Task.Run(() =>
            {
                currentVoice = VoiceId.FindValue(voice);
                tmppath      = Path.Combine(PelicanTTSMod._helper.DirectoryPath, "TTS");

                if (pc == null)
                {
                    pc = AWSHandler.getPollyClient();
                }

                bool mumbling    = PelicanTTSMod.config.MumbleDialogues;
                string language1 = "<lang xml:lang=\"" + getLanguageCode() + "\">";
                string language2 = "</lang>";

                text = text.Replace("0g", "0 gold").Replace("< ", " ").Replace("` ", "  ").Replace("> ", " ").Replace('^', ' ').Replace(Environment.NewLine, " ").Replace("$s", "").Replace("$h", "").Replace("$g", "").Replace("$e", "").Replace("$u", "").Replace("$b", "").Replace("$8", "").Replace("$l", "").Replace("$q", "").Replace("$9", "").Replace("$a", "").Replace("$7", "").Replace("<", "").Replace("$r", "").Replace("[", "<").Replace("]", ">");
                text = language1 + text + language2;

                bool neural = shouldUseNeuralEngine(voice, out string v);

                if (!neural && voice != v)
                {
                    currentVoice = VoiceId.FindValue(v);
                }

                bool useNeuralEngine = !mumbling && neural;

                var amzeffectIn  = mumbling ? "<amazon:effect phonation='soft'><amazon:effect vocal-tract-length='-20%'>" : "<amazon:auto-breaths><amazon:effect phonation='soft'>";
                var amzeffectOut = mumbling ? "</amazon:effect></amazon:effect>" : "</amazon:effect></amazon:auto-breaths>";

                if (mumbling)
                {
                    text = @"<speak>" + (useNeuralEngine ? "" : amzeffectIn) + Dialogue.convertToDwarvish(text) + (useNeuralEngine ? "" : amzeffectOut) + @"</speak>";
                }
                else
                {
                    text = @"<speak>" + (useNeuralEngine ? "" : amzeffectIn) + "<prosody rate='" + (rate == -1 ? PelicanTTSMod.config.Rate : rate) + "%'>" + text + @"</prosody>" + (useNeuralEngine ? "" : amzeffectOut) + "</speak>";
                }

                int hash = (text + (useNeuralEngine ? "-neural" : "")).GetHashCode();
                if (!Directory.Exists(Path.Combine(tmppath, name)))
                {
                    Directory.CreateDirectory(Path.Combine(tmppath, name));
                }

                string file            = Path.Combine(Path.Combine(tmppath, name), "speech_" + currentVoice.Value + (mumbling ? "_mumble_" : "_") + hash + ".wav");
                SoundEffect nextSpeech = null;

                if (!File.Exists(file))
                {
                    SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();
                    sreq.Text                     = text;
                    sreq.TextType                 = TextType.Ssml;
                    sreq.OutputFormat             = OutputFormat.Ogg_vorbis;
                    sreq.Engine                   = useNeuralEngine ? Engine.Neural : Engine.Standard;
                    sreq.VoiceId                  = currentVoice;
                    SynthesizeSpeechResponse sres = pc.SynthesizeSpeech(sreq);
                    using (var memStream = new MemoryStream())
                    {
                        sres.AudioStream.CopyTo(memStream);
                        nextSpeech = Convert(memStream, file);
                    }
                }
                using (FileStream stream = new FileStream(file, FileMode.Open))
                    nextSpeech = SoundEffect.FromStream(stream);

                if (currentSpeech != null)
                {
                    currentSpeech.Stop();
                }

                currentSpeech = nextSpeech.CreateInstance();

                speak = false;
                currentSpeech.Pitch  = (mumbling ? 0.5f : pitch == -1 ? PelicanTTSMod.config.Voices[name].Pitch : pitch);
                currentSpeech.Volume = volume == -1 ? PelicanTTSMod.config.Volume : volume;
                currentSpeech.Play();
            });
        }