Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
Add Neural Text to Speech demo (#135)
Browse files Browse the repository at this point in the history
* Add NeuralTTS demo

* small fix

* NeuralTTS demo: add docs

* minor improvements

Co-authored-by: Albert Davletov <v-adavle@microsoft.com>
  • Loading branch information
Albert Davletov and Albert Davletov committed Jun 22, 2021
1 parent 946719f commit e572314
Show file tree
Hide file tree
Showing 10 changed files with 726 additions and 0 deletions.
Binary file added Documentation/NeuralTextToSpeech.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions Documentation/NeuralTextToSpeech.md
@@ -0,0 +1,20 @@
# Neural Text to Speech

![alt text](https://github.com/Microsoft/Cognitive-Samples-IntelligentKiosk/blob/master/Documentation/NeuralTextToSpeech.jpg "Neural Text to Speech")

Text-to-speech converts input text into human-like synthesized speech using Speech Synthesis Markup Language (SSML). Use neural voices, which are human-like voices powered by deep neural networks.

# Neural voices

We specified some neural voices in the NeuralVoices.json file. You can add more neural voices. For a full list of neural voices, see [supported languages](https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/language-support#neural-voices).


# Key Source Code

* [NeuralTTS](../Kiosk/Views/NeuralTTS/NeuralTTS.xaml.cs): Main page that drives the demo.

* [NeuralTTSDataLoader](../Kiosk/Views/NeuralTTS/NeuralTTSDataLoader.cs): A class used to save and load cached audio results.

* [NeuralVoices.json](../Kiosk/Views/NeuralTTS/NeuralVoices.json): A JSON file that contains a list of the available neural voices for this demo.

* [Authentication](../Kiosk/Views/NeuralTTS/Authentication.cs): The text-to-speech REST API requires an Authorization header. This class has logic to get a service access token.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions Kiosk/IntelligentKioskSample.csproj
Expand Up @@ -128,6 +128,7 @@
<Content Include="Assets\DemoGallery\Image Collection Insights.jpg" />
<Content Include="Assets\DemoGallery\InsuranceClaimAutomation.jpg" />
<Content Include="Assets\DemoGallery\Mall Kiosk.jpg" />
<Content Include="Assets\DemoGallery\Neural Text To Speech.jpg" />
<Content Include="Assets\DemoGallery\Realtime Crowd Insights.jpg" />
<Content Include="Assets\DemoGallery\Realtime Driver Monitoring.jpg" />
<Content Include="Assets\DemoGallery\Realtime Image Classification Demo.jpg" />
Expand Down Expand Up @@ -165,6 +166,9 @@
<Content Include="Views\TextAnalyticsExplorer\TextSamples.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Views\NeuralTTS\NeuralVoices.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Compile Include="App.xaml.cs">
Expand Down Expand Up @@ -413,6 +417,11 @@
<DependentUpon>MallKioskPage.xaml</DependentUpon>
</Compile>
<Compile Include="Views\MallKioskDemoConfig\MallKioskDemoSettings.cs" />
<Compile Include="Views\NeuralTTS\Authentication.cs" />
<Compile Include="Views\NeuralTTS\NeuralTTS.xaml.cs">
<DependentUpon>NeuralTTS.xaml</DependentUpon>
</Compile>
<Compile Include="Views\NeuralTTS\NeuralTTSDataLoader.cs" />
<Compile Include="Views\RealTimeDemo.xaml.cs">
<DependentUpon>RealTimeDemo.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -786,6 +795,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\NeuralTTS\NeuralTTS.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\RealTimeDemo.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
68 changes: 68 additions & 0 deletions Kiosk/Views/NeuralTTS/Authentication.cs
@@ -0,0 +1,68 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Cognitive Services: http://www.microsoft.com/cognitive
//
// Microsoft Cognitive Services Github:
// https://github.com/Microsoft/Cognitive
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace IntelligentKioskSample.Views.NeuralTTS
{
public class Authentication
{
private const string SubscriptionKeyHeaderName = "Ocp-Apim-Subscription-Key";
private const string TokenTemplate = "https://{0}.api.cognitive.microsoft.com/sts/v1.0/issueToken";

private Uri tokenUri;
private string subscriptionKey;

public Authentication(string subscriptionKey, string region)
{
this.subscriptionKey = subscriptionKey;
this.tokenUri = new Uri(string.Format(TokenTemplate, region));
}

public async Task<string> RetrieveNewTokenAsync()
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add(SubscriptionKeyHeaderName, this.subscriptionKey);
HttpResponseMessage response = await client.PostAsync(this.tokenUri, new StringContent(string.Empty)).ConfigureAwait(false);

return response.IsSuccessStatusCode
? await response.Content.ReadAsStringAsync().ConfigureAwait(false)
: throw new WebException(response.ReasonPhrase);
}
}
}
}
88 changes: 88 additions & 0 deletions Kiosk/Views/NeuralTTS/NeuralTTS.xaml
@@ -0,0 +1,88 @@
<Page
x:Class="IntelligentKioskSample.Views.NeuralTTS.NeuralTTSPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctl="using:IntelligentKioskSample.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Name="page"
Loaded="OnPageLoaded"
mc:Ignorable="d">

<Page.Resources>
<ctl:ReverseBooleanConverter x:Key="reverseBooleanConverter"/>
<ctl:CollectionCountToVisibilityConverter x:Key="collectionCountToVisibilityConverter"/>
</Page.Resources>

<Grid x:Name="MainGrid" Background="#FF292929">

<Grid.RowDefinitions>
<RowDefinition Height="0.05*" />
<RowDefinition Height="auto" />
<RowDefinition Height="0.05*" />
<RowDefinition />
<RowDefinition Height="0.05*" />
</Grid.RowDefinitions>

<Grid Margin="24,0,24,0" Grid.Row="1" MaxWidth="800">
<StackPanel>
<TextBlock TextAlignment="Center" Text="Neural Text to Speech" Foreground="White" Style="{StaticResource HeaderTextBlockStyle}"/>

<Grid Grid.Row="1" Margin="0,36,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="inputTextBox" MaxLength="500" TextWrapping="Wrap" Header="Input text:" Text="Hello everyone!" MaxWidth="800" Height="120" AcceptsReturn="True"/>

<Grid Grid.Column="1" Margin="12,0,0,0">
<ComboBox Grid.Column="3" x:Name="voiceComboBox" VerticalAlignment="Top" Header="Voice:" Width="225" ItemsSource="{Binding ElementName=page, Path=AvailableVoices}" DisplayMemberPath="Id" />
<Button Margin="0,6,0,0" Grid.Column="1" x:Name="speakButton" Click="SpeakButtonClicked" VerticalAlignment="Bottom" Content="Speak" HorizontalAlignment="Stretch" IsEnabled="{Binding ElementName=progressRing, Path=IsActive, Converter={StaticResource reverseBooleanConverter}}" />
</Grid>
</Grid>
</StackPanel>
</Grid>

<Grid Grid.Row="1" >
<ProgressRing x:Name="progressRing" Grid.Column="1" Width="75" Height="75" Foreground="White" />
</Grid>

<MediaPlayerElement x:Name="mediaPlayerElement" Visibility="Collapsed" AutoPlay="False"/>

<Grid Grid.Row="3" MaxWidth="840" >
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="20,10,0,0" Visibility="{Binding ElementName=page, Path=CachedResults.Count, Converter={StaticResource collectionCountToVisibilityConverter}}" >
<TextBlock Text="Cached Results:" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Button Background="Transparent" VerticalAlignment="Center" Click="ClearAllCachedResultsButtonClicked">
<SymbolIcon Symbol="Delete"/>
</Button>
</StackPanel>
<ListView Grid.Row="2" Margin="0,10,0,0" x:Name="cachedResultsListView" ItemsSource="{Binding CachedResults, ElementName=page}" SelectionMode="None" IsItemClickEnabled="False" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid Opacity="0.5" Margin="0,6,0,6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Background="Transparent" VerticalAlignment="Center" Click="PlayCachedResultButtonClicked">
<SymbolIcon Symbol="Play"/>
</Button>
<Button Grid.Column="1" Margin="12,0,12,0" Background="Transparent" VerticalAlignment="Center" Click="DeleteCachedResultButtonClicked">
<SymbolIcon Symbol="Delete"/>
</Button>
<TextBlock Grid.Column="2" TextWrapping="WrapWholeWords" VerticalAlignment="Center">
<Run Text="{Binding Text}"/>
<Run Text="[" /><Run Text="{Binding VoiceId}" /><Run Text="]" />
</TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Grid>
</Page>

0 comments on commit e572314

Please sign in to comment.