Windows 10 Speech: very basic code

No error handling, very dirty, just wanted to get something that produces sound.

MainPage.xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
 
 
 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <RelativePanel>
            <MediaElement x:Name="media" AutoPlay="False"/>
            <TextBox x:Name="textBox1" Text="My Dear Text" Margin="5"/>
            <Button x:Name="blueButton" Margin="5" Background="LightBlue" Content="ButtonRight" RelativePanel.RightOf="textBox1"/>
            <Button x:Name="orangeButton" Click="orangeButton_Click" Margin="5" Background="Orange" Content="ButtonBelow"
                    RelativePanel.RightOf="textBox1" RelativePanel.Below="blueButton"/>
            
        </RelativePanel>
    </Grid>
</Page>


		
		



using System;
using System.Collections.Generic;
using Windows.Media.SpeechSynthesis;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.ApplicationModel.Resources.Core;
 
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
 
namespace App1 {
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page {
 
        private SpeechSynthesizer synthesizer;
        private ResourceContext speechContext;
        private ResourceMap speechResourceMap;
 
        public static MainPage Current;
        public MainPage() {
            this.InitializeComponent();
            synthesizer = new SpeechSynthesizer();
            speechContext = ResourceContext.GetForCurrentView();
            speechContext.Languages = new string[] { SpeechSynthesizer.DefaultVoice.Language };
            speechResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("LocalizationTTSResources");
        }
 
        public List<Scenario> Scenarios
        {
            get { return this.scenarios; }
        }
 
        private async void orangeButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
 
            if (media.CurrentState.Equals(MediaElementState.Playing)) {
                media.Stop();
            }
            else {
                string text = textBox1.Text.ToString();
                if (!String.IsNullOrEmpty(text)) {
                    // Change the button label. You could also just disable the button if you don't want any user control.
 
 
                    try {
                        // Create a stream from the text. This will be played using a media element.
                        SpeechSynthesisStream synthesisStream = await synthesizer.SynthesizeTextToStreamAsync(text);
 
                        // Set the source and start playing the synthesized audio stream.
                        media.AutoPlay = true;
                        media.SetSource(synthesisStream, synthesisStream.ContentType);
                        media.Play();
                    }
                    catch (System.IO.FileNotFoundException) {
                        // If media player components are unavailable, (eg, using a N SKU of windows), we won't
                        // be able to start media playback. Handle this gracefully
 
                        var messageDialog = new Windows.UI.Popups.MessageDialog("Media player components unavailable");
                        await messageDialog.ShowAsync();
                    }
                    catch (Exception) {
                        // If the text is unable to be synthesized, throw an error message to the user.
 
                        media.AutoPlay = false;
                        var messageDialog = new Windows.UI.Popups.MessageDialog("Unable to synthesize text");
                        await messageDialog.ShowAsync();
                    }
                }
            }
 
 
 
 
        }
 
      
    }
}    


Ref SSML... this worked... and the difference between loud and soft is perceptible:
string Ssml =
     @"<speak version='1.0' " +
     "xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-GB'>" +
     "<prosody volume='x-loud'> This is extra loud volume. </prosody>";


This worked:
string Ssml =
               @"<speak version='1.0' " +
               "xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-GB'>" +
               "Hello <prosody contour='(0%,+80Hz) (10%,+80%) (40%,+80Hz)'>World</prosody> " +
               "<break time='500ms' />" +
               "Goodbye <prosody rate='slow' contour='(0%,+20Hz) (10%,+30%) (40%,+10Hz)'>World</prosody>" +
               "</speak>";
https://msdn.microsoft.com/en-us/library/windows.media.speechsynthesis.speechsynthesizer.aspx

ref ssml:
https://msdn.microsoft.com/en-us/library/jj127898.aspx

speechy10
Googling the above, see a lot of complaints about this. When I have time I will try this out:

I installed fresh Windows 10 and Visual Studio Community 2015, and the designer failed to load (for MainPage.xaml etc). I had to:

  1. enable developer mode in system settings (update section) as suggested in info dialog
  2. (re)install Visual C++ redistributable for VS 2015

But I don’t know which one exactly resolved the problem… Now the designer loads as expected. (I tried only C# universal app yet)

 

… and generally tidy up the post.

 


		
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s