Speak Text, Speak SSML

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;
            }
        }
    }
}

时间: 2024-11-08 19:16:31

Speak Text, Speak SSML的相关文章

重新想象 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/pre

我的Windows Phone 8

学习Windows Phone 8开发,将自己收集到的学习资料做一个汇总(不断更新). 我的Windows Phone应用 DotaMax MyAppToStart 麦子学院 WP8.1版(UI简陋) WP7学习博客 整理Windows Phone 7 教程1.0 超过500篇文章   作者:机微网 WP7 开发31日谈 WP8学习视频 传智播客 .NET视频 主要初学: C#编程基础 C#面向对象基础 WPF教程 WP7移动开发 WP8.1移动开发 麦子学院(免费观看,需要注册账号)  作者:

重新想象 Windows 8.1 Store Apps 系列文章索引

[源码下载] [重新想象 Windows 8 Store Apps 系列文章] 作者:webabcd 1.重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 介绍重新想象 Windows 8.1 Store Apps 之新增控件 AppBar - 应用程序栏控件(新增了 AppBarButton, AppBarToggleButton, AppBarSeparator) CommandBar - 应用程序栏控件(AppBar 简

我(webabcd)的文章索引

[最后更新:2014.08.28] 重新想象 Windows Store Apps 系列文章 重新想象 Windows 8 Store Apps 系列文章 重新想象 Windows 8 Store Apps (1) - 控件之文本控件: TextBlock, TextBox, PasswordBox, RichEditBox, RichTextBlock, RichTextBlockOverflow 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button,

行为委托,简洁的 对象关联 编码风格

//来自http://www.qdfuns.com/notes/31180/55aa17cafe6e64eed5fba661cc018570.html //父类 function Foo(who) { this.me = who; } Foo.prototype.identify = function() { return "I am: " + this.me; } function Bar(who) { //继承父类的属性 Foo.call(this,who); } //继承父类的方

speechSynthesis

/* $timeout.cancel(timer); */window.speechSynthesis.onvoiceschanged = function(e) { voices = speechSynthesis.getVoices();};window.speechSynthesis.onvoiceschanged(); var msg = new SpeechSynthesisUtterance();function speak(text) { msg.text = text; msg.

java程序

This XML file does not appear to have any style information associated with it. The document tree is shown below.博客园_首页代码改变世界uuid:5de59c50-a92f-4447-96ed-ab86451ed183;id=61182014-07-27T11:59:08Zfeed.cnblogs.comhttp://www.cnblogs.com/jianyus/p/3865670

且听且读 听书软件

文字显示内核已经告一段落,现在文K可以方便的呈现多彩效果,突然又想,文字显示已经极好了,如果配上文字语音岂不更妙.因此,便开始了语音开发且读且听软件. 且读且听软件与其它听说软件的区别:不仅仅听,且相应字段呈现PPT视觉特效. 第一步下载:Speech SDK5.1 http://microsoft-speech-sdk.software.informer.com/5.1/ 点击,将其解压到某个文件夹下,然后点击安装,默认安装在C盘下 注:只安装上面文件的话,则只支持英文朗读. 要想支持中文朗读

Samples for Parallel Programming with the .NET Framework

The .NET Framework 4 includes significant advancements for developers writing parallel and concurrent applications, including Parallel LINQ (PLINQ), the Task Parallel Library (TPL), new thread-safe collections, and a variety of new coordination and s