internal protected void RegisterFunctionHandler(string functionName, Func <JavaScriptValue[], JavaScriptValue> function)
        {
            if (FunctionHandlers.Any(x => x.FuntionName.Equals(functionName)))
            {
                throw new ArgumentException();
            }

            var handler = new JavaScriptExtensionFunctionHandler(functionName, function);

            FunctionHandlers.Add(handler);
        }
Beispiel #2
0
        public override bool ExecuteAction(ActionProgramRun ap)
        {
            string    say;
            Variables statementvars;

            if (FromString(userdata, out say, out statementvars))
            {
                string ctrl = ap.VarExist("SpeechDebug") ? ap["SpeechDebug"] : "";

                string    errlist = null;
                Variables vars    = ap.functions.ExpandVars(statementvars, out errlist);

                if (ctrl.Contains("SayLine"))
                {
                    ap.actioncontroller.LogLine("Say Command: " + userdata);
                    ap.actioncontroller.LogLine("Say Vars: " + vars.ToString(separ: Environment.NewLine));
                    System.Diagnostics.Debug.WriteLine("Say Vars: " + vars.ToString(separ: Environment.NewLine));
                }

                if (errlist == null)
                {
                    bool wait = vars.GetInt(waitname, 0) != 0;

                    string prior = (vars.Exists(priorityname) && vars[priorityname].Length > 0) ? vars[priorityname] : (ap.VarExist(globalvarspeechpriority) ? ap[globalvarspeechpriority] : "Normal");
                    AudioQueue.Priority priority = AudioQueue.GetPriority(prior);

                    string start  = vars.GetString(startname, checklen: true);
                    string finish = vars.GetString(finishname, checklen: true);
                    string voice  = (vars.Exists(voicename) && vars[voicename].Length > 0) ? vars[voicename] : (ap.VarExist(globalvarspeechvoice) ? ap[globalvarspeechvoice] : "Default");

                    int vol = vars.GetInt(volumename, -999);
                    if (vol == -999)
                    {
                        vol = ap.variables.GetInt(globalvarspeechvolume, 60);
                    }

                    int rate = vars.GetInt(ratename, -999);
                    if (rate == -999)
                    {
                        rate = ap.variables.GetInt(globalvarspeechrate, 0);
                    }

                    int queuelimitms = vars.GetInt(queuelimit, 0);

                    string culture = (vars.Exists(culturename) && vars[culturename].Length > 0) ? vars[culturename] : (ap.VarExist(globalvarspeechculture) ? ap[globalvarspeechculture] : "Default");

                    bool literal   = vars.GetInt(literalname, 0) != 0;
                    bool dontspeak = vars.Exists(dontspeakname) ? (vars.GetInt(dontspeakname, 0) != 0) : (ap.VarExist(globalvarspeechdisable) ? ap[globalvarspeechdisable].InvariantParseInt(0) != 0 : false);

                    string prefixsoundpath  = vars.GetString(prefixsound, checklen: true);
                    string postfixsoundpath = vars.GetString(postfixsound, checklen: true);
                    string mixsoundpath     = vars.GetString(mixsound, checklen: true);

                    Variables           globalsettings = ap.VarExist(globalvarspeecheffects) ? new Variables(ap[globalvarspeecheffects], Variables.FromMode.MultiEntryComma) : null;
                    SoundEffectSettings ses            = SoundEffectSettings.Set(globalsettings, vars); // work out the settings

                    if (queuelimitms > 0)
                    {
                        int queue = ap.actioncontroller.AudioQueueSpeech.InQueuems();

                        if (queue >= queuelimitms)
                        {
                            ap["SaySaid"] = "!LIMIT";
                            System.Diagnostics.Debug.WriteLine("Abort say due to queue being at " + queue);
                            return(true);
                        }
                    }

                    string expsay;
                    if (ap.functions.ExpandString(say, out expsay) != Functions.ExpandResult.Failed)
                    {
                        System.Diagnostics.Debug.WriteLine("Say wait {0}, vol {1}, rate {2}, queue {3}, priority {4}, culture {5}, literal {6}, dontspeak {7} , prefix {8}, postfix {9}, mix {10} starte {11}, finishe {12} , voice {13}, text {14}",
                                                           wait, vol, rate, queuelimitms, priority, culture, literal, dontspeak, prefixsoundpath, postfixsoundpath, mixsoundpath, start, finish, voice, expsay);

                        Random rnd = FunctionHandlers.GetRandom();

                        if (!literal)
                        {
                            expsay = expsay.PickOneOfGroups(rnd);       // expand grouping if not literal
                        }

                        ap["SaySaid"] = expsay;

                        if (ctrl.Contains("Global"))
                        {
                            ap.actioncontroller.SetPeristentGlobal("GlobalSaySaid", expsay);
                        }

                        if (ctrl.Contains("Print"))
                        {
                            ap.actioncontroller.LogLine("Say: " + expsay);
                        }

                        if (ctrl.Contains("Mute"))
                        {
                            return(true);
                        }

                        if (ctrl.Contains("DontSpeak"))
                        {
                            expsay = "";
                        }

                        if (dontspeak)
                        {
                            expsay = "";
                        }

                        AudioQueue.AudioSample mix = null, prefix = null, postfix = null;

                        if (mixsoundpath != null)
                        {
                            mix = ap.actioncontroller.AudioQueueSpeech.Generate(mixsoundpath);

                            if (mix == null)
                            {
                                ap.ReportError("Say could not create mix audio, check audio file format is supported and effects settings");
                                return(true);
                            }
                        }

                        if (prefixsoundpath != null)
                        {
                            prefix = ap.actioncontroller.AudioQueueSpeech.Generate(prefixsoundpath);

                            if (prefix == null)
                            {
                                ap.ReportError("Say could not create prefix audio, check audio file format is supported and effects settings");
                                return(true);
                            }
                        }

                        if (postfixsoundpath != null)
                        {
                            postfix = ap.actioncontroller.AudioQueueSpeech.Generate(postfixsoundpath);

                            if (postfix == null)
                            {
                                ap.ReportError("Say could not create postfix audio, check audio file format is supported and effects settings");
                                return(true);
                            }
                        }

                        // we entrust it to a Speach Queue (New Dec 20) as the synth takes an inordinate time to generate speech, it then calls back

                        ap.actioncontroller.SpeechSynthesizer.SpeakQueue(expsay, culture, voice, rate, (memstream) =>
                        {
                            // in a thread, invoke on UI thread to complete action, since these objects are owned by that thread

                            ap.actioncontroller.Form.Invoke((MethodInvoker) delegate
                            {
                                System.Diagnostics.Debug.Assert(Application.MessageLoop);       // double check!

                                AudioQueue.AudioSample audio = ap.actioncontroller.AudioQueueSpeech.Generate(memstream, ses, true);

                                if (audio != null)
                                {
                                    if (mix != null)
                                    {
                                        audio = ap.actioncontroller.AudioQueueSpeech.Mix(audio, mix);     // audio in MIX format
                                    }
                                    if (prefix != null)
                                    {
                                        audio = ap.actioncontroller.AudioQueueSpeech.Append(prefix, audio);        // audio in AUDIO format.
                                    }
                                    if (postfix != null)
                                    {
                                        audio = ap.actioncontroller.AudioQueueSpeech.Append(audio, postfix);         // Audio in P format
                                    }
                                    if (start != null)
                                    {
                                        audio.sampleStartTag = new AudioEvent {
                                            apr = ap, eventname = start, ev = ActionEvent.onSayStarted
                                        };
                                        audio.sampleStartEvent += Audio_sampleEvent;
                                    }

                                    if (wait || finish != null)       // if waiting, or finish call
                                    {
                                        audio.sampleOverTag = new AudioEvent()
                                        {
                                            apr = ap, wait = wait, eventname = finish, ev = ActionEvent.onSayFinished
                                        };
                                        audio.sampleOverEvent += Audio_sampleEvent;
                                    }

                                    ap.actioncontroller.AudioQueueSpeech.Submit(audio, vol, priority);
                                }
                            });
                        });

                        return(!wait);
                    }
                    else
                    {
                        ap.ReportError(expsay);
                    }
                }
                else
                {
                    ap.ReportError(errlist);
                }
            }
            else
            {
                ap.ReportError("Say command line not in correct format");
            }

            return(true);
        }