Esempio n. 1
0
        /// <summary>
        /// Queries microphone status via the AudioCaptureControl global instance and constructs
        /// a UIAudioStatus object representing current state.
        /// </summary>
        /// <returns> A UIAudioStatus representing the state of the microphone. </returns>
        public static async Task <UIAudioStatus> GetMicrophoneStatusAsync()
        {
            var control = await AudioCaptureControl.GetInstanceAsync();

            var capabilityStatus = control.MicrophoneCapability.CheckAccess();

            string        glyph;
            Color         color;
            List <string> statusText = new List <string>();

            if (capabilityStatus == AppCapabilityAccessStatus.UserPromptRequired)
            {
                glyph = Glyphs.Cancel;
                color = Colors.Red;
                statusText.Add("Microphone permissions have not yet been prompted.");
            }
            else if (capabilityStatus != AppCapabilityAccessStatus.Allowed)
            {
                glyph = Glyphs.Cancel;
                color = Colors.Red;
                statusText.Add("Microphone permission is denied.");
            }
            else if (!control.HasAudioInputAvailable)
            {
                glyph = Glyphs.Cancel;
                color = Colors.Red;
                statusText.Add("No audio input device is present.");
            }
            else if (control.CaptureMuted)
            {
                glyph = Glyphs.Microphone;
                color = Colors.Red;
                statusText.Add("Microphone is muted and keywords can't be heard.");
            }
            else if (control.CaptureVolumeLevel < 10f)
            {
                glyph = Glyphs.Microphone;
                color = Colors.Red;
                statusText.Add("Microphone volume is very low and keywords may not be heard.");
            }
            else if (capabilityStatus == AppCapabilityAccessStatus.Allowed)
            {
                glyph = Glyphs.Microphone;
                color = Colors.Green;
                statusText.Add(MicrophoneAvailable);
            }
            else
            {
                glyph = Glyphs.Cancel;
                color = Colors.Red;
            }

            return(new UIAudioStatus(glyph, color, statusText));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MainPage"/> class.
        /// </summary>
        public MainPage()
        {
            this.logger = LogRouter.GetClassLogger();

            this.InitializeComponent();

            this.app = App.Current as App;
            this.app.HasReachedForeground = true;

            this.services            = this.app.Services;
            this.dialogManager       = this.services.GetRequiredService <IDialogManager>();
            this.keywordRegistration = this.services.GetRequiredService <IKeywordRegistration>();
            this.agentSessionManager = this.services.GetRequiredService <IAgentSessionManager>();

            this.informationLogs = new HashSet <TextBlock>();
            this.errorLogs       = new HashSet <TextBlock>();
            this.noiseLogs       = new HashSet <TextBlock>();

            // Ensure that we restore the full view (not the compact mode) upon foreground launch
            _ = this.UpdateViewStateAsync();

            // Ensure we have microphone permissions and that we pop a consent dialog if the user
            // hasn't already given an explicit yes/no.
            _ = Task.Run(async() =>
            {
                var control = await AudioCaptureControl.GetInstanceAsync();
                await control.MicrophoneCapability.RequestAccessAsync();
            });

            // Kick off the registration and/or retrieval of the 1st-stage keyword information
            _ = this.DoKeywordSetupAsync();

            // Populate the drop-down list for TTS audio output formats and select the current choice
            var supportedFormats = DirectLineSpeechAudio.SupportedOutputFormats;

            foreach (var entry in supportedFormats)
            {
                this.OutputFormatComboBox.Items.Add(entry.Label);
            }

            this.OutputFormatComboBox.SelectedItem = this.OutputFormatComboBox.Items.FirstOrDefault(item =>
                                                                                                    item.ToString() == LocalSettingsHelper.OutputFormat.Label);

            // Wire a few pieces of UI handling that aren't trivially handled by XAML bindings
            this.AddUIHandlersAsync();

            // Ensure consistency between a few dependent controls and their settings
            this.UpdateUIBasedOnToggles();

            this.Conversations = new ObservableCollection <Conversation>();

            this.ChatHistoryListView.ContainerContentChanging += this.OnChatHistoryListViewContainerChanging;
        }
        /// <summary>
        /// Queries the Conversational Agent Platform for the current state of the selected
        /// configuration and generates a UIAudioStatus object representing the state of voice
        /// activation.
        /// </summary>
        /// <returns> A UIAudioStatus representing current voice activation state. </returns>
        public static async Task <UIAudioStatus> GetVoiceActivationStatusAsync()
        {
            var services            = (App.Current as App).Services;
            var keywordRegistration = services.GetRequiredService <IKeywordRegistration>();
            var agentSessionManager = services.GetRequiredService <IAgentSessionManager>();

            var session = await agentSessionManager.GetSessionAsync();

            var config = await keywordRegistration.GetOrCreateKeywordConfigurationAsync();

            var audioControl = await AudioCaptureControl.GetInstanceAsync();

            string        glyph  = Glyphs.Cancel;
            Color         color  = Colors.Red;
            List <string> status = new List <string>();

            if (session == null)
            {
                status.Add("Unable to obtain agent session. Please verify registration.");
            }

            if (config == null)
            {
                status.Add("No valid keyword configuration. Please check your source code configuration.");
            }

            if (!config.AvailabilityInfo.HasPermission)
            {
                status.Add("Voice activation permissions are currently denied.");
            }

            if (!config.AvailabilityInfo.HasSystemResourceAccess)
            {
                status.Add("Voice activation is unavailable. Please verify against keyword conflicts.");
            }

            if (!config.AvailabilityInfo.IsEnabled)
            {
                status.Add("Voice activation is programmatically disabled by the app.");
            }

            if (!config.IsActive)
            {
                status.Add("Voice activation is unavailable for an unknown reason.");
            }

            if (audioControl.CaptureMuted || audioControl.CaptureVolumeLevel < 5f)
            {
                status.Add("Voice activation is available but may be degraded due to microphone state.");
            }

            if (!MVARegistrationHelpers.IsBackgroundTaskRegistered)
            {
                status.Add("Background task is not configured and voice activation will only work while the application is already active.");
            }

            if (VoiceActivationIsPowerRestricted())
            {
                status.Add("The system is currently power restricted and voice activation may not be available.");
            }

            if (config.AvailabilityInfo.IsEnabled && MVARegistrationHelpers.IsBackgroundTaskRegistered)
            {
                glyph = Glyphs.FeedbackApp;
                color = Colors.Green;
                status.Add(VoiceActivationEnabledMessage);
            }
            else
            {
                glyph = Glyphs.Warning;
                color = Colors.DarkOrange;
            }

            return(new UIAudioStatus(glyph, color, status));
        }
Esempio n. 4
0
        /// <summary>
        /// Queries the Conversational Agent Platform for the current state of the selected
        /// configuration and generates a UIAudioStatus object representing the state of voice
        /// activation.
        /// </summary>
        /// <returns> A UIAudioStatus representing current voice activation state. </returns>
        public static async Task <UIAudioStatus> GetVoiceActivationStatusAsync()
        {
            var services            = (App.Current as App).Services;
            var keywordRegistration = services.GetRequiredService <IKeywordRegistration>();
            var agentSessionManager = services.GetRequiredService <IAgentSessionManager>();

            var session = await agentSessionManager.GetSessionAsync();

            var configs = await keywordRegistration.GetOrCreateKeywordConfigurationsAsync();

            var audioControl = await AudioCaptureControl.GetInstanceAsync();

            string        glyph  = Glyphs.Cancel;
            Color         color  = Colors.Red;
            List <string> status = new List <string>();
            ActivationSignalDetectionConfiguration swKeywordConfiguration      = null;
            ActivationSignalDetectionConfiguration hwKeywordConfiguration      = null;
            ActivationSignalDetectionConfiguration defaultKeywordConfiguration = null;

            foreach (var configuration in configs)
            {
                string modelDataType = await configuration.GetModelDataTypeAsync();

                if (string.IsNullOrEmpty(modelDataType))
                {
                    hwKeywordConfiguration = configuration;
                }
                else
                {
                    swKeywordConfiguration = configuration;
                }
            }

            if (swKeywordConfiguration != null)
            {
                defaultKeywordConfiguration = swKeywordConfiguration;
            }
            else if (hwKeywordConfiguration != null)
            {
                defaultKeywordConfiguration = hwKeywordConfiguration;
            }

            if (session == null)
            {
                status.Add("Unable to obtain agent session. Please verify registration.");
            }
            else if (configs.Count == 0)
            {
                status.Add("No valid keyword configuration. Please check your source code configuration.");
            }
            else if (swKeywordConfiguration != null && !swKeywordConfiguration.AvailabilityInfo.HasPermission)
            {
                status.Add("Voice activation permissions are currently denied.");
            }
            else if (hwKeywordConfiguration != null && !hwKeywordConfiguration.AvailabilityInfo.HasPermission)
            {
                status.Add("Voice activation permissions are currently denied.");
            }
            else if (audioControl.CaptureMuted || audioControl.CaptureVolumeLevel < 5f)
            {
                glyph = Glyphs.Warning;
                color = Colors.DarkOrange;
                status.Add("Voice activation is available but may be degraded due to microphone state.");
            }
            else if (!MVARegistrationHelpers.IsBackgroundTaskRegistered)
            {
                glyph = Glyphs.Warning;
                color = Colors.DarkOrange;
                status.Add("Background task is not configured and voice activation will only work while the application is already active.");
            }
            else if (VoiceActivationIsPowerRestricted())
            {
                glyph = Glyphs.Warning;
                color = Colors.DarkOrange;
                status.Add("The system is currently power restricted and voice activation may not be available.");
            }
            else if (swKeywordConfiguration != null && swKeywordConfiguration.AvailabilityInfo.IsEnabled && MVARegistrationHelpers.IsBackgroundTaskRegistered)
            {
                glyph = Glyphs.FeedbackApp;
                color = Colors.Green;
                status.Add(VoiceActivationEnabledMessage);
            }
            else if (hwKeywordConfiguration != null && hwKeywordConfiguration.AvailabilityInfo.IsEnabled && MVARegistrationHelpers.IsBackgroundTaskRegistered)
            {
                glyph = Glyphs.FeedbackApp;
                color = Colors.Green;
                status.Add(VoiceActivationEnabledMessage);
            }
            else
            {
                glyph = Glyphs.Warning;
                color = Colors.DarkOrange;
            }

            if (swKeywordConfiguration != null)
            {
                string configStatus = GetKeywordConfigurationStatusDescription(swKeywordConfiguration, "swKeywordConfig");
                if (!string.IsNullOrEmpty(configStatus))
                {
                    status.Add(configStatus);
                }
            }

            if (hwKeywordConfiguration != null)
            {
                string configStatus = GetKeywordConfigurationStatusDescription(hwKeywordConfiguration, "hwKeywordConfig");
                if (!string.IsNullOrEmpty(configStatus))
                {
                    status.Add(configStatus);
                }
            }

            return(new UIAudioStatus(glyph, color, status));
        }