Speech Synthesis (Text to Speech) In Windows 8.1 Store Apps

Windows 8.1 adds the ability to perform text to speech using the Windows.Media.SpeechSynthesis namespace.

As a basic example, to say “Hello World” in the default voice:

var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();

Windows.Media.SpeechSynthesis.SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello World");

var mediaElement = new MediaElement();
mediaElement.SetSource(stream, stream.ContentType);
mediaElement.Play();

You can specify a different speech voice by setting the SpeechSynthesizer’s Voice property.

To get a list of all the installed speech synthesis engines voices that are on the local machine, you can use the SpeechSynthesizer.AllVoices property.

In the example below, we loop through all the installed voices, and ask them to say “Hello World” followed by their names (DisplayName).

var synth = new SpeechSynthesizer();

 foreach (var voice in SpeechSynthesizer.AllVoices)
 {
     synth.Voice = voice;

     var text = "Hello World, I'm " + voice.DisplayName;

     var stream = await synth.SynthesizeTextToStreamAsync(text);

     var me = new MediaElement();
     me.SetSource(stream, stream.ContentType);                
     me.Play();

     await Task.Delay(3000);
 }

SHARE:

Merging IEnumerable Sequences in C# with LINQ

The Zip method allows us to join IEnumerable sequences together by interleaving the elements in the sequences.

Zip is an extension method on IEnumerable. For example to zip together a collection of names with ages we could write:

var names = new [] {"John", "Sarah", "Amrit"};

var ages = new[] {22, 58, 36};

var namesAndAges = names.Zip(ages, (name, age) => name + " " + age);    

This would produce an IEnumerable<string> containing three elements:

  • “John 22”
  • “Sarah 58”
  • “Amrit 36”

If one sequence is shorter that the other, the “zipping” will stop when the end of the shorter sequence is reached. So if we added an extra name:

var names = new [] {"John", "Sarah", "Amrit", "Bob"};

We’d end up with the same result as before, “Bob” wouldn’t be used as there isn’t a corresponding age for him.

We can also create objects in our lambda, this example shows how to create an IEnumerable of two-element Tuples:

var names = new [] {"John", "Sarah", "Amrit"};

var ages = new[] {22, 58, 36};

var namesAndAges = names.Zip(ages, (name, age) => Tuple.Create(name, age)); 

This will produce an IEnumerable<Tuple<String,Int32>> that contains three Tuples, with each Tuple holding a name and age.

If you want to fill in the gaps in your C# knowledge be sure to check out my C# Tips and Traps training course from Pluralsight – get started with a free trial.

SHARE:

Updating Your Windows 8 App to Windows 8.1: Dealing With Coloured Fonts

With Windows 8.1, font characters (glyphs) can now contain coloured layers.

In a Windows 8.0 app, this XAML:

<TextBlock>$★☺☹❤✈⛱⛺⛵⚽✘✔☀⛅☕⛄❓♀♂❋❄✹</TextBlock>

Will produce the following (black and white) characters:

image

In Windows 8.1 this behaviour is different by default.

More...

SHARE:

Three Things I’ve Learned Being a Pluralsight Course Author

Up to this point I’ve produced 7 Pluralsight courses, these are three things I learned along the way:

Be Flexible with Plans

In our industry, “plan the work, and work the plan” is rarely successful – look at the rise of agile software development. So too in producing courses.

Just because I plan to record today, doesn’t mean mother nature won’t throw a wild (noisy) storm at me, or that the new houses being built close by won’t require a thunderous earth-moving machine to be used, or that there wont be a biplane show nearby.

This is definitely a valuable lesson in general: when we grasp desperately at our plans and treat them as if our lives depend on them, we just create unnecessary stress for ourselves. Sometimes you just have to push through regardless, and sometimes you have to let it go; wisdom is knowing which approach to take.

Celebrate Milestones

Life is a series of highs and lows, peaks and troughs. If we don’t celebrate and recognise the highs then all we’re left with are the troughs, the lows. Clearly it’s important to recognise both. For example when C# Tips and Traps got into the top 10 leaderboard, this was a major high so I made sure I celebrated the fact.

The Right Tools

When creating anything the right tools make all the difference. For example I started recording my first course using a Rode NT1-A which is a beautiful microphone for recording rich vocals in music but not great when it comes to sound isolation. I quickly moved to the USB Rode Podcaster which is pretty great – it’s frequency response is tailored for voice and has a fairly narrow end-address pickup that greatly reduces background noise.

The right tools really do make such a difference in all the things we do, as does the right mindset.

I hope to get to ten Pluralsight courses by the end of next year, you can check out my current Pluralsight courses on my author page.

SHARE:

Auto-Generating Sequences of Integer Values in C#

When we need to generate a sequence of integer values, we can do it manually using some kind of loop, or we make use of the Enumerable.Range method.

The following code illustrates:

var oneToTen = Enumerable.Range(1, 10);

int[] twentyToThirty = Enumerable.Range(20, 11).ToArray();

List<int> oneHundredToOneThirty = Enumerable.Range(100, 31).ToList();

We can also use the results of .Range and transform them in some way, for example to get the letters of the alphabet we could write something like this:

var alphabet = Enumerable.Range(0, 26).Select(i => Convert.ToChar('A' + i));

This will generate an IEnumerable<char> containing the letters A through Z.

If you want to fill in the gaps in your C# knowledge be sure to check out my C# Tips and Traps training course from Pluralsight – get started with a free trial.

SHARE:

Updating Your Windows 8 App to Windows 8.1: Handling Widths and Replacing Snapped Mode

Windows 8.1 allows users to resize Windows more freely than Windows 8.0.

In Windows 8.0 there was the concept of snapped, filled and full screen viewstates. These concepts are no more in Windows 8.1 apps.

By default, Windows 8.1 apps have a minimum default width of 500px wide. This can be overridden and changed to 320px wide by setting it in the Package.appxmanifest file:

<ApplicationView MinWidth="width320" />

You can also use the designer:

setting min windows 8.1 app width

Depending on how you designed your app you may have more or less work to do; for example if you used a lot of fixed widths and positions you may have to adopt a more “responsive” layout strategy. If your existing app is more or less responsive already then you’ll probably have less work to do.

More...

SHARE:

Automated Acceptance Testing with SpecFlow and Gherkin Course

With my new Pluralsight course you’ll learn how to narrow the gap between the business/customer and the development team by creating business-readable, automated tests.

The course covers how to install SpecFlow in Visual Studio, the Gherkin business-readable domain specific language (DSL), and how to create code-automation from this natural language.

You can find the course on my Pluralsight author page.

You can start watching with a Pluralsight free trial.

SHARE:

Dear Windows Phone Back Button

It seems that the back button is the latest hip thing to complain about with Windows Phone, with rumours about Microsoft dropping the hardware back button and then not.

As a Windows Phone app developer I absolutely do not want to have to add back buttons to all my pages in my apps – that would be horrible…

One of the other rumours is that physical buttons may no longer be required, but instead the Back, Start, and Search buttons could be made virtual and appear at the bottom of the actual touch screen itself. But if this were to happen, given that screen size and resolutions remain the same then it would have to mean that apps would have less vertical space to present themselves in. While this might work, and enable more manufacturers to make cheaper phones, I still like my Lumia hardware back button – perhaps higher-end handsets will continue with the hardware buttons.

SHARE:

Creating Inline Data Driven Tests in xUnit

xUnit.net allows the creation of data-driven tests. These kind of tests get their test data from outside the test method code via parameters added to the test method signature.

Say we had to test a Calculator class and check it’s add method returned the correct results for six different test cases. Without data-driven tests we’d either have to write six separates tests (with almost identical code) or some loop inside our test method containing an assert.

Regular xUnit.net test methods are identified by applying the [Fact] attribute. Data-driven tests instead use the [Theory] attribute.

To get data-driven features and the [Theory] attribute, install the xUnit.net Extensions NuGet package in addition to the standard xUnit.net package.

Creating Inline Data-Driven Tests

The [InlineData] attribute allows us to specify test data that gets passed to the parameters of test method.

So for our Calculator add test we’d start by defining the test method:

More...

SHARE: