protected virtual async Task <bool> SetupConversationAsync(DetectionOrigin signalOrigin)
        {
            var session = await this.agentSessionManager.GetSessionAsync();

            try
            {
                if (signalOrigin == DetectionOrigin.FromPushToTalk)
                {
                    await this.dialogAudioInput.InitializeFromNowAsync();
                }
                else
                {
                    await this.dialogAudioInput.InitializeFromAgentSessionAsync(session);
                }
            }
            catch (Exception ex)
            {
                this.logger.Log($"Unable to acquire MVA 1st-pass audio. Rejecting signal.\n{ex.HResult}: {ex.Message}");
                await this.FinishConversationAsync();

                return(false);
            }

            this.dialogBackend.SetAudioSource(this.dialogAudioInput);
            await this.dialogBackend.InitializeAsync(await this.keywordRegistration.GetConfirmationKeywordFileAsync());

            return(true);
        }
 private void OnSessionSignalRejected(DetectionOrigin origin)
 {
     this.kwsPerformanceLogger.LogSignalReceived("SWKWS", "R", "2", KwsPerformanceLogger.KwsEventFireTime.Ticks, KwsPerformanceLogger.KwsStartTime.Ticks, DateTime.Now.Ticks);
     this.logger.Log(LogMessageLevel.SignalDetection, $"Session singal rejected, Signal Origin: {origin}");
     this.StopFailsafeTimer();
     this.SignalRejected?.Invoke(origin);
 }
 private void OnSignalRejected(DetectionOrigin origin)
 {
     if (!this.HasReachedForeground)
     {
         this.logger.Log($"Exiting application forcibly.");
         WindowService.CloseWindow();
     }
 }
 private void OnSignalRejected(DetectionOrigin origin)
 {
     if (!this.HasReachedForeground)
     {
         this.logger.Log(LogMessageLevel.ConversationalAgentSignal, $"Exiting application forcibly.");
         WindowService.CloseWindow();
     }
 }
        private async void OnSignalConfirmed(DetectionOrigin origin)
        {
            if (!this.HasReachedForeground)
            {
                // At this point, an agent may choose to take whatever action it'd like to for a
                // confirmed signal in a background task. Audio should already be flowing. Most
                // commonly, this is when an application can be brought to the foreground.
                var session = await this.agentSessionManager.GetSessionAsync();

                await session.RequestForegroundActivationAsync();
            }
        }
        /// <summary>
        /// Entry point to begin a conversation (one or multiple turns) for a dialog manager.
        /// </summary>
        /// <param name="signalOrigin"> The entry point for the activation signal. </param>
        /// <param name="signalVerificationRequired">
        ///     Whether or not this conversation's signal should be validated before taking user
        ///     facing action.
        /// </param>
        /// <returns> A task that completes once the conversation is started. </returns>
        public virtual async Task StartConversationAsync(
            DetectionOrigin signalOrigin,
            bool signalVerificationRequired)
        {
            var setupSuccessful = await this.SetupConversationAsync(signalOrigin);

            if (!setupSuccessful)
            {
                this.logger.Log($"DialogManager2::SetupConversationAsync didn't succeed in setting up a conversation (see earlier errors). Aborting conversation.");
                return;
            }

            await this.StartTurnAsync(signalVerificationRequired);
        }
        private void OnSessionSignalConfirmed(IAgentSessionWrapper session, DetectionOrigin origin)
        {
            this.StopFailsafeTimer();

            this.logger.Log($"Confirmed signal received, IsUserAuthenticated={session.IsUserAuthenticated.ToString(null)}");
            if (!session.IsUserAuthenticated)
            {
                // This is a launch over the lock screen. It may be prudent to serialize state
                // and relaunch to ensure a fresh and accurate windowing layout.
                // https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.core.coreapplication.requestrestartasync
            }

            this.SignalConfirmed?.Invoke(origin);
        }
        /// <summary>
        /// Processes a 1st-stage activation signal as received by the conversational agent
        /// activation runtime.
        /// </summary>
        /// <param name="detectionOrigin"> The entry point through which handler received the activation signal (e.g. via background task or in-app event handler). </param>
        public async void HandleSignalDetection(
            DetectionOrigin detectionOrigin = DetectionOrigin.FromBackgroundTask)
        {
            KwsPerformanceLogger.KwsEventFireTime = TimeSpan.FromTicks(DateTime.Now.Ticks);
            var now = DateTime.Now;

            if (this.lastSignalReceived.HasValue && now.Subtract(this.lastSignalReceived.Value).TotalMilliseconds < MinimumSignalSeparation.TotalMilliseconds)
            {
                this.logger.Log(LogMessageLevel.SignalDetection, $"Ignoring signal received so soon after previous!");
                return;
            }

            this.lastSignalReceived       = now;
            this.LastDetectedSignalOrigin = detectionOrigin;

            var session = await this.agentSessionManager.GetSessionAsync();

            var signalName = (detectionOrigin == DetectionOrigin.FromPushToTalk)
                ? "Push to talk" : session.SignalName;

            this.logger.Log(LogMessageLevel.SignalDetection, $"HandleSignalDetection, '{signalName}', {detectionOrigin.ToString()}");

            var canSkipVerification =
                detectionOrigin == DetectionOrigin.FromPushToTalk ||
                !session.IsSignalVerificationRequired ||
                !LocalSettingsHelper.EnableSecondStageKws;

            this.signalNeedsVerification = !canSkipVerification;

            this.SignalReceived?.Invoke(detectionOrigin, this.signalNeedsVerification);

            this.kwsPerformanceLogger.LogSignalReceived(KwsPerformanceLogger.Spotter, "A", "1", KwsPerformanceLogger.KwsEventFireTime.Ticks, KwsPerformanceLogger.KwsStartTime.Ticks, DateTime.Now.Ticks);

            if (this.signalNeedsVerification)
            {
                KwsPerformanceLogger.KwsEventFireTime = TimeSpan.FromTicks(DateTime.Now.Ticks);
                this.StartFailsafeTimer();
            }
            else
            {
                this.OnSessionSignalConfirmed(session, detectionOrigin);
            }
        }
 /// <summary>
 /// Processes a 1st-stage activation signal as received by the conversational agent
 /// activation runtime.
 /// </summary>
 /// <param name="detectionOrigin"> The entry point through which handler received the activation signal (e.g. via background task or in-app event handler). </param>
 public void HandleSignalDetection(DetectionOrigin detectionOrigin = DetectionOrigin.FromBackgroundTask)
 {
     this.signalDetectionHelper.HandleSignalDetection(detectionOrigin);
 }
 private void OnSessionSignalRejected(DetectionOrigin origin)
 {
     this.StopFailsafeTimer();
     this.SignalRejected?.Invoke(origin);
 }