Bagikan melalui


Mengelola masalah dengan input audio

Pelajari cara mengelola masalah dengan akurasi pengenalan ucapan yang disebabkan oleh kualitas input audio.

API Penting: SpeechRecognizer, RecognitionQualityDegrading, SpeechRecognitionAudioProblem

Menilai kualitas input audio

Saat pengenalan ucapan aktif, gunakan peristiwa RecognitionQualityDegrading dari pengenal ucapan Anda untuk menentukan apakah satu atau beberapa masalah audio mungkin mengganggu input ucapan. Argumen peristiwa (SpeechRecognitionQualityDegradingEventArgs) menyediakan properti Masalah , yang menjelaskan masalah yang terdeteksi dengan input audio.

Pengenalan dapat dipengaruhi oleh terlalu banyak kebisingan latar belakang, mikrofon yang dibisukan, dan volume atau kecepatan speaker.

Di sini, kami mengonfigurasi pengenal ucapan dan mulai mendengarkan peristiwa RecognitionQualityDegrading .

private async void WeatherSearch_Click(object sender, RoutedEventArgs e)
{
    // Create an instance of SpeechRecognizer.
    var speechRecognizer = new Windows.Media.SpeechRecognition.SpeechRecognizer();

    // Listen for audio input issues.
    speechRecognizer.RecognitionQualityDegrading += speechRecognizer_RecognitionQualityDegrading;

    // Add a web search grammar to the recognizer.
    var webSearchGrammar = new Windows.Media.SpeechRecognition.SpeechRecognitionTopicConstraint(Windows.Media.SpeechRecognition.SpeechRecognitionScenario.WebSearch, "webSearch");


    speechRecognizer.UIOptions.AudiblePrompt = "Say what you want to search for...";
    speechRecognizer.UIOptions.ExampleText = "Ex. 'weather for London'";
    speechRecognizer.Constraints.Add(webSearchGrammar);

    // Compile the constraint.
    await speechRecognizer.CompileConstraintsAsync();

    // Start recognition.
    Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeWithUIAsync();
    //await speechRecognizer.RecognizeWithUIAsync();

    // Do something with the recognition result.
    var messageDialog = new Windows.UI.Popups.MessageDialog(speechRecognitionResult.Text, "Text spoken");
    await messageDialog.ShowAsync();
}

Mengelola pengalaman pengenalan ucapan

Gunakan deskripsi yang disediakan oleh properti Masalah untuk membantu pengguna meningkatkan kondisi untuk pengenalan.

Di sini, kami membuat handler untuk peristiwa RecognitionQualityDegrading yang memeriksa tingkat volume rendah. Kami kemudian menggunakan objek SpeechSynthesizer untuk menyarankan agar pengguna mencoba berbicara lebih keras.

private async void speechRecognizer_RecognitionQualityDegrading(
    Windows.Media.SpeechRecognition.SpeechRecognizer sender,
    Windows.Media.SpeechRecognition.SpeechRecognitionQualityDegradingEventArgs args)
{
    // Create an instance of a speech synthesis engine (voice).
    var speechSynthesizer =
        new Windows.Media.SpeechSynthesis.SpeechSynthesizer();

    // If input speech is too quiet, prompt the user to speak louder.
    if (args.Problem == Windows.Media.SpeechRecognition.SpeechRecognitionAudioProblem.TooQuiet)
    {
        // Generate the audio stream from plain text.
        Windows.Media.SpeechSynthesis.SpeechSynthesisStream stream;
        try
        {
            stream = await speechSynthesizer.SynthesizeTextToStreamAsync("Try speaking louder");
            stream.Seek(0);
        }
        catch (Exception)
        {
            stream = null;
        }

        // Send the stream to the MediaElement declared in XAML.
        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
        {
            this.media.SetSource(stream, stream.ContentType);
        });
    }
}

Sampel