重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML
作者:webabcd
介绍
重新想象 Windows 8.1 Store Apps 之 TTS(Text To Speech)
- Speak Text
- Speak SSML
示例
1、演示如何通过 TTS 朗读一段文本,以及如何将其保存为音频文件
SpeakText.xaml
<Page x:Class="Windows81.TTS.SpeakText" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows81.TTS" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <!--用于播放音频--> <MediaElement Name="mediaElement" AutoPlay="False" /> <TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" /> <!--需要被 TTS 的文本--> <TextBlock Name="lblText" FontSize="14.667" Margin="0 10 0 0" Text="TTS 是 Text To Speech 的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话。" /> <!--开始 TTS--> <Button Name="btnSpeakText" Content="Speek Text" Click="btnSpeakText_Click" Margin="0 10 0 0" /> <!--保存 TTS 转换出的音频文件--> <Button Name="btnSaveAudio" Content="Save Audio" Click="btnSaveAudio_Click" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
SpeakText.xaml.cs
/* * 演示如何通过 TTS 朗读一段文本,以及如何将其保存为音频文件 * * 注:Windows Phone 中的“TTS, 语音识别, 语音命令”请参见:http://www.cnblogs.com/webabcd/archive/2014/01/02/3501356.html */ using System; using System.Collections.Generic; using Windows.Media.SpeechSynthesis; using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Streams; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; namespace Windows81.TTS { public sealed partial class SpeakText : Page { // TTS 引擎 private SpeechSynthesizer _synthesizer = new SpeechSynthesizer(); public SpeakText() { this.InitializeComponent(); lblMsg.Text = "系统支持的语音有:"; // TTS 引擎所支持的全部语音信息 var voices = SpeechSynthesizer.AllVoices; foreach (VoiceInformation voice in voices) { lblMsg.Text += voice.DisplayName + ", "; } lblMsg.Text += Environment.NewLine; // 获取或设置当前 TTS 引擎所使用的语音 lblMsg.Text += "当前 TTS 使用的语音是:" + _synthesizer.Voice.DisplayName; // SpeechSynthesizer 实现了 IDisposable 接口 // _synthesizer.Dispose(); this.mediaElement.CurrentStateChanged += mediaElement_CurrentStateChanged; } void mediaElement_CurrentStateChanged(object sender, RoutedEventArgs e) { if (mediaElement.CurrentState == MediaElementState.Paused) { btnSpeakText.IsEnabled = true; } } // 播放 TTS 音频 private async void btnSpeakText_Click(object sender, RoutedEventArgs e) { // TTS 引擎生成的音频流 SpeechSynthesisStream synthesisStream; try { // 通过 TTS 引擎将字符串转换成音频流 synthesisStream = await _synthesizer.SynthesizeTextToStreamAsync(lblText.Text); } catch (Exception ex) { lblMsg.Text = ex.ToString(); synthesisStream = null; btnSpeakText.IsEnabled = true; } // 播放音频流 mediaElement.AutoPlay = true; mediaElement.SetSource(synthesisStream, synthesisStream.ContentType); mediaElement.Play(); } // 保存 TTS 音频 private async void btnSaveAudio_Click(object sender, RoutedEventArgs e) { FileSavePicker savePicker = new FileSavePicker(); savePicker.DefaultFileExtension = ".wav"; savePicker.FileTypeChoices.Add("Audio file", new List<string>() { ".wav" }); StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { btnSaveAudio.IsEnabled = false; SpeechSynthesisStream synthesisStream; try { synthesisStream = await _synthesizer.SynthesizeTextToStreamAsync(lblText.Text); } catch (Exception ex) { lblMsg.Text = ex.ToString(); synthesisStream = null; btnSaveAudio.IsEnabled = true; } // 保存音频数据到文件 Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(4096); IRandomAccessStream writeStream = (IRandomAccessStream)await file.OpenAsync(FileAccessMode.ReadWrite); IOutputStream outputStream = writeStream.GetOutputStreamAt(0); DataWriter dataWriter = new DataWriter(outputStream); while (synthesisStream.Position < synthesisStream.Size) { await synthesisStream.ReadAsync(buffer, 4096, InputStreamOptions.None); dataWriter.WriteBuffer(buffer); } dataWriter.StoreAsync().AsTask().Wait(); outputStream.FlushAsync().AsTask().Wait(); btnSaveAudio.IsEnabled = true; } } } }
2、演示如何通过 TTS 朗读 SSML 协议文档,以及如何将其保存为音频文件
SpeakSSML.xaml
<Page x:Class="Windows81.TTS.SpeakSSML" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows81.TTS" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <!--用于播放音频--> <MediaElement Name="mediaElement" AutoPlay="False" /> <TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" /> <!--开始 TTS--> <Button Name="btnSpeakSSML" Content="Speek SSML" Click="btnSpeakSSML_Click" Margin="0 10 0 0" /> <!--保存 TTS 转换出的音频文件--> <Button Name="btnSaveAudio" Content="Save Audio" Click="btnSaveAudio_Click" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
SpeakSSML.xaml.cs
/* * 演示如何通过 TTS 朗读 SSML 协议文档,以及如何将其保存为音频文件 * * 注:Windows Phone 中的“TTS, 语音识别, 语音命令”请参见:http://www.cnblogs.com/webabcd/archive/2014/01/02/3501356.html * * * 另外: * 1、SSML - Speech Synthesis Markup Language * 2、微软关于 ssml 的说明:http://msdn.microsoft.com/en-us/library/hh361578 * 3、W3C 关于 ssml 的说明:http://www.w3.org/TR/speech-synthesis/ */ using System; using System.Collections.Generic; using Windows.Media.SpeechSynthesis; using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Streams; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; namespace Windows81.TTS { public sealed partial class SpeakSSML : Page { // TTS 引擎 private SpeechSynthesizer _synthesizer = new SpeechSynthesizer(); public SpeakSSML() { this.InitializeComponent(); lblMsg.Text = "系统支持的语音有:"; // TTS 引擎所支持的全部语音信息 var voices = SpeechSynthesizer.AllVoices; foreach (VoiceInformation voice in voices) { lblMsg.Text += voice.DisplayName + ", "; } lblMsg.Text += Environment.NewLine; // 获取或设置当前 TTS 引擎所使用的语音 lblMsg.Text += "当前 TTS 使用的语音是:" + _synthesizer.Voice.DisplayName; // SpeechSynthesizer 实现了 IDisposable 接口 // _synthesizer.Dispose(); this.mediaElement.CurrentStateChanged += mediaElement_CurrentStateChanged; } void mediaElement_CurrentStateChanged(object sender, RoutedEventArgs e) { if (mediaElement.CurrentState == MediaElementState.Paused) { btnSpeakSSML.IsEnabled = true; } } // 播放 TTS 音频(SSML) private async void btnSpeakSSML_Click(object sender, RoutedEventArgs e) { // TTS 引擎生成的音频流 SpeechSynthesisStream synthesisStream; try { // 构造一个 SSML 协议文档 string ssml = "<speak version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"zh-CN\">"; // 中文 ssml += "<voice gender=\"male\">"; // 男声 ssml += "<prosody rate=\"-50%\">"; // 语速放慢 50% ssml += "TTS 是 Text To Speech 的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话。"; ssml += "</prosody>"; ssml += "</voice>"; ssml += "</speak>"; // 通过 TTS 引擎将 SSML 协议文档转换成音频流 synthesisStream = await _synthesizer.SynthesizeSsmlToStreamAsync(ssml); } catch (Exception ex) { lblMsg.Text = ex.ToString(); synthesisStream = null; btnSpeakSSML.IsEnabled = true; } // 播放音频流 mediaElement.AutoPlay = true; mediaElement.SetSource(synthesisStream, synthesisStream.ContentType); mediaElement.Play(); } // 保存 TTS 音频(SSML) private async void btnSaveAudio_Click(object sender, RoutedEventArgs e) { FileSavePicker savePicker = new FileSavePicker(); savePicker.DefaultFileExtension = ".wav"; savePicker.FileTypeChoices.Add("Audio file", new List<string>() { ".wav" }); StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { btnSaveAudio.IsEnabled = false; SpeechSynthesisStream synthesisStream; try { // 构造一个 SSML 协议文档 string ssml = "<speak version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"zh-CN\">"; // 中文 ssml += "<voice gender=\"male\">"; // 男声 ssml += "<prosody rate=\"-50%\">"; // 语速放慢 50% ssml += "TTS 是 Text To Speech 的缩写,即“从文本到语音”,是人机对话的一部分,让机器能够说话。"; ssml += "</prosody>"; ssml += "</voice>"; ssml += "</speak>"; // 通过 TTS 引擎将 SSML 协议文档转换成音频流 synthesisStream = await _synthesizer.SynthesizeSsmlToStreamAsync(ssml); } catch (Exception ex) { lblMsg.Text = ex.ToString(); synthesisStream = null; btnSaveAudio.IsEnabled = true; } // 保存音频数据到文件 Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(4096); IRandomAccessStream writeStream = (IRandomAccessStream)await file.OpenAsync(FileAccessMode.ReadWrite); IOutputStream outputStream = writeStream.GetOutputStreamAt(0); DataWriter dataWriter = new DataWriter(outputStream); while (synthesisStream.Position < synthesisStream.Size) { await synthesisStream.ReadAsync(buffer, 4096, InputStreamOptions.None); dataWriter.WriteBuffer(buffer); } dataWriter.StoreAsync().AsTask().Wait(); outputStream.FlushAsync().AsTask().Wait(); btnSaveAudio.IsEnabled = true; } } } }
OK
[源码下载]
重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML,布布扣,bubuko.com
重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML