Пример #1
0
        public static void ProcessVoiceTextMessage(string text)
        {
            // If <message.
            if (!text.Contains("<message"))
            {
                return;
            }
            var v = Serializer.DeserializeFromXmlString <message>(text);
            // Get default values by NPC or Player name.
            var overrideVoice = SettingsFile.Current.Defaults.FirstOrDefault(x => string.Equals(x.name, v.name, StringComparison.InvariantCultureIgnoreCase));

            // If defaults were found then fill missing values.
            if (overrideVoice != null)
            {
                v.UpdateMissingValuesFrom(overrideVoice);
            }

            // commands.
            var   cmd = v.command.ToLower();
            sound snd;

            switch (cmd)
            {
            case MessageCommands.Player:
                if (v.name != null)
                {
                    var playerNames = v.name.Split(',').Select(x => x.Trim()).ToArray();
                    _PlayerName        = playerNames.FirstOrDefault();
                    _PlayerNameChanged = playerNames.Skip(1).FirstOrDefault();
                    _PlayerClass       = playerNames.Skip(2).FirstOrDefault();
                }
                break;

            case MessageCommands.Add:
                if (addMessage == null)
                {
                    addMessage = new message();
                }
                addMessage.UpdateMissingAndChangedValuesFrom(v, true);
                break;

            case MessageCommands.Sound:
                var groupName2 = string.IsNullOrEmpty(v.group)
                                                ? SettingsManager.Options.DefaultIntroSoundComboBox ?? ""
                                                : v.group;
                // Find intro sound.
                snd = SettingsFile.Current.Sounds.FirstOrDefault(x => x.group.ToUpper() == groupName2.ToUpper());
                // If sound was not found then...
                if (snd == null)
                {
                    // Add sound to the list.
                    snd       = new sound();
                    snd.group = v.group;
                    SettingsFile.Current.Sounds.Add(snd);
                }
                if (snd.enabled)
                {
                    // Get WAV name/path.
                    var wavToPlay = string.IsNullOrEmpty(snd.file) ? groupName2 : snd.file;
                    wavToPlay = MainHelper.ConvertFromSpecialFoldersPattern(wavToPlay);
                    var stream = GetIntroSound(wavToPlay);
                    if (stream == null && System.IO.File.Exists(wavToPlay))
                    {
                        stream = new System.IO.FileStream(wavToPlay, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    }
                    OnEvent(IntroSoundSelected, snd);
                    if (stream != null)
                    {
                        AddIntroSoundToPlayList(wavToPlay, v.group, stream);
                    }
                }
                break;

            case MessageCommands.Play:
                if (addMessage == null)
                {
                    addMessage = new message();
                }
                addMessage.UpdateMissingAndChangedValuesFrom(v, true);
                var am = addMessage;
                // Clear buffer.
                addMessage = null;

                // Set gender: Male, Female or Neutral.
                MessageGender amGender;
                if (!Enum.TryParse(am.gender, out amGender))
                {
                    amGender = SettingsManager.Options.GenderComboBoxText;
                }
                am.gender = amGender.ToString();

                // Select voice. ----------------------------------------------------------------------------------------------------
                var selectedVoice = SelectVoice(am.name, am.language, amGender);

                // Set pitch.
                var pitchIsValid = int.TryParse(am.pitch, out _Pitch);
                if (!pitchIsValid)
                {
                    _Pitch = MainHelper.GetNumber(SettingsManager.Options.PitchMin, SettingsManager.Options.PitchMax, "pitch", am.name);
                }
                if (_Pitch < -10)
                {
                    _Pitch = -10;
                }
                if (_Pitch > 10)
                {
                    _Pitch = 10;
                }
                // Set PitchComment.
                _PitchComment = _Pitch >= 0 ? -1 : 1;

                // Set rate.
                var rateIsValid = int.TryParse(am.rate, out _Rate);
                if (!rateIsValid)
                {
                    _Rate = MainHelper.GetNumber(SettingsManager.Options.RateMin, SettingsManager.Options.RateMax, "rate", am.name);
                }
                if (_Rate < -10)
                {
                    _Rate = -10;
                }
                if (_Rate > 10)
                {
                    _Rate = 10;
                }

                // Set effect.
                var _effect = string.IsNullOrEmpty(am.effect) ? "Default" : am.effect;
                if (cmd != MessageCommands.Save)
                {
                    OnEvent(EffectsPresetSelected, _effect);
                }

                // Set volume.
                int _volume;
                var volumeIsValid = int.TryParse(am.volume, out _volume);
                if (!volumeIsValid)
                {
                    _volume = 100;
                }
                if (_volume < 0)
                {
                    _volume = 0;
                }
                if (_volume > 100)
                {
                    _volume = 100;
                }

                bool amSound;
                bool.TryParse(am.sound, out amSound);
                if (amSound)
                {
                    var groupName = string.IsNullOrEmpty(am.group)
                                                        ? SettingsManager.Options.DefaultIntroSoundComboBox ?? ""
                                                        : am.group;
                    // Find intro sound.
                    snd = SettingsFile.Current.Sounds.FirstOrDefault(x => x.group.ToUpper() == groupName.ToUpper());
                    // If sound was not found then...
                    if (snd == null)
                    {
                        // Add sound to the list.
                        snd       = new sound();
                        snd.group = am.group;
                        SettingsFile.Current.Sounds.Add(snd);
                    }
                    if (snd.enabled)
                    {
                        // Get WAV name/path.
                        var wavToPlay = string.IsNullOrEmpty(snd.file) ? groupName : snd.file;
                        wavToPlay = MainHelper.ConvertFromSpecialFoldersPattern(wavToPlay);
                        var stream = GetIntroSound(wavToPlay);
                        if (stream == null && System.IO.File.Exists(wavToPlay))
                        {
                            stream = new System.IO.FileStream(wavToPlay, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        }
                        OnEvent(IntroSoundSelected, snd);
                        if (stream != null)
                        {
                            AddIntroSoundToPlayList(wavToPlay, am.group, stream);
                        }
                    }
                }
                // Decode complete message part.
                var allText = System.Web.HttpUtility.HtmlDecode(am.part);
                // Add silence before message.
                var programName      = Program.MonitorItem.Name;
                var silenceIntBefore = SettingsManager.Options.AddSilenceBeforeMessage;
                if (silenceIntBefore > 0)
                {
                    AddTextToPlaylist(programName, "<silence msec=\"" + silenceIntBefore.ToString() + "\" />", true, am.group);
                }
                // Add actual message to the playlist
                AddTextToPlaylist(programName, allText, true, am.group,
                                  // Supply NCP properties.
                                  am.name, am.gender, am.effect, _volume,
                                  // Supply Player properties.
                                  _PlayerName, _PlayerNameChanged, _PlayerClass,
                                  // Pass information required to pick correct synthesizer i.e. Local, Amazon or Google voice.
                                  selectedVoice == null ? null : selectedVoice.SourceKeys
                                  );
                // Add silence after message.
                var silenceIntAfter = (int)SettingsManager.Options.AddSilenceAfterMessage;
                if (silenceIntAfter > 0)
                {
                    AddTextToPlaylist(programName, "<silence msec=\"" + silenceIntAfter.ToString() + "\" />", true, am.group);
                }
                break;

            case MessageCommands.Stop:
                StopPlayer(v.group);
                break;

            case MessageCommands.Save:
                // If name supplied then...
                if (!string.IsNullOrEmpty(v.name))
                {
                    // Save into default
                    SettingsManager.Current.UpsertDefaultsRecord(v);
                }
                break;

            default:
                break;
            }
            OnEvent(ProcessedMessage, v);
        }
Пример #2
0
        // Set voice.
        static InstalledVoiceEx SelectVoice(string name, string language, MessageGender gender)
        {
            // Get only enabled voices.
            var data = InstalledVoices.Where(x => x.Enabled).ToArray();
            InstalledVoiceEx voice = null;
            var culture            = Capturing.message.GetCultureInfo(language);

            // Order voices by putting matching gender with highest value first.
            OrderVoicesByGender(ref data, gender);
            var missing = "";

            missing += "There are no voices enabled in \"{0}\" column with value \"{1}\". ";
            missing += "Set popularity value to 100 ( normal usage ) or 101 ( normal usage / favourite ) for at least one voice.";
            // Initial choice will be all enabled voices.
            InstalledVoiceEx[] choice = data;
            InstalledVoiceEx[] tmp;
            // If voice name was supplied then...
            if (!string.IsNullOrEmpty(name))
            {
                // Select voices by name if exists ("IVONA 2 Amy").
                tmp = data.Where(x => string.Equals(x.Name, name, StringComparison.InvariantCulture)).ToArray();
                // If choice available then...
                if (tmp.Length > 0)
                {
                    choice = tmp;
                }
                else
                {
                    OnHelp(missing, "Name", name);
                }
            }
            // Filter by gender (more important than by culture).
            tmp = FilterVoicesByGender(choice, gender);
            // If choice available then...
            if (tmp.Length > 0)
            {
                choice = tmp;
            }
            else
            {
                OnHelp(missing, "Gender", gender);
                // Order by Male as default.
                OrderVoicesByGender(ref data, MessageGender.Male);
            }
            // If culture supplied.
            if (culture != null)
            {
                tmp = FilterVoicesByCulture(choice, culture);
                // If choice available then...
                if (tmp.Length > 0)
                {
                    choice = tmp;
                }
                else
                {
                    OnHelp(missing, "Culture", language);
                }
            }
            // If nothing to choose from then...
            if (choice.Length == 0)
            {
                return(null);
            }
            // Generate number for selecting voice.
            var number = MainHelper.GetNumber(0, choice.Length - 1, "name", name);

            voice = choice[number];
            if (SelectedVoice != voice)
            {
                OnEvent(VoiceChanged, voice);
            }
            return(voice);
        }
Пример #3
0
        public static ItemPlayOptions GetPlayOptions(string name           = null, string language = null, string gender = null,
                                                     string overridePitch  = null,
                                                     string overrideRate   = null,
                                                     string overrideVolume = null,
                                                     string effect         = null
                                                     )
        {
            var ipo = new ItemPlayOptions();
            // Set gender: Male, Female or Neutral.
            MessageGender amGender;

            if (!Enum.TryParse(gender, out amGender))
            {
                amGender = SettingsManager.Options.GenderComboBoxText;
            }
            ipo.Gender = amGender;
            if (name == null)
            {
                ipo.Voice = SelectedVoice;
            }
            else
            {
                ipo.Voice = SelectVoice(name, language, amGender);
            }
            // Set pitch.
            int _pitch;
            var pitchIsValid = int.TryParse(overridePitch, out _pitch);

            if (!pitchIsValid)
            {
                _pitch = MainHelper.GetNumber(SettingsManager.Options.PitchMin, SettingsManager.Options.PitchMax, "pitch", name);
            }
            if (_pitch < -10)
            {
                _pitch = -10;
            }
            if (_pitch > 10)
            {
                _pitch = 10;
            }
            ipo.Pitch = _pitch;
            // Set rate.
            int _rate;
            var rateIsValid = int.TryParse(overrideRate, out _rate);

            if (!rateIsValid)
            {
                _rate = MainHelper.GetNumber(SettingsManager.Options.RateMin, SettingsManager.Options.RateMax, "rate", name);
            }
            if (_rate < -10)
            {
                _rate = -10;
            }
            if (_rate > 10)
            {
                _rate = 10;
            }
            ipo.Rate = _rate;
            // Set volume.
            int _volume;
            var volumeIsValid = int.TryParse(overrideVolume, out _volume);

            if (!volumeIsValid)
            {
                _volume = 100;
            }
            if (_volume < 0)
            {
                _volume = 0;
            }
            if (_volume > 100)
            {
                _volume = 100;
            }
            ipo.Volume = _volume;
            // Set effect.
            string _effect;

            if (string.IsNullOrEmpty(effect))
            {
                _effect = "Default";
            }
            else
            {
                _effect = effect;
                if (SelectedEffect != effect)
                {
                    OnEvent(EffectsPresetSelected, effect);
                }
            }
            ipo.Effect = _effect;
            return(ipo);
        }