`

基于vc++2008托管代码开发Windows Vista语音朗读

 
阅读更多

闲的无聊,锻炼一下托管c++,

看代码

#pragma once
#include "Window1.g.h"

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Text;
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Windows::Data;
using namespace System::Windows::Documents;
using namespace System::Windows::Input;
using namespace System::Windows::Media;
using namespace System::Windows::Media::Imaging;
using namespace System::Windows::Shapes;

using namespace System::Collections::ObjectModel;
using namespace System::Xml;

using namespace System::Speech::Synthesis;

namespace speechSynth
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>

//ORIGINAL LINE: public partial class Window1 : System.Windows.Window
//INSTANT C++ TODO TASK: C++ does not support 'partial' types. You must manually combine the entire Window1 type in one place.
public ref class Window1 : System::Windows::Window
{
private:
SpeechSynthesizer ^synth;

public:
Window1()
{
InitializeComponent();

synth = gcnew SpeechSynthesizer();
synth->StateChanged += gcnew EventHandler<StateChangedEventArgs^>(this, &Window1::synth_StateChanged);
synth->BookmarkReached += gcnew EventHandler<BookmarkReachedEventArgs^>(this, &Window1::synth_BookmarkReached);
synth->PhonemeReached += gcnew EventHandler<PhonemeReachedEventArgs^>(this, &Window1::synth_PhonemeReached);
synth->SpeakCompleted += gcnew EventHandler<SpeakCompletedEventArgs^>(this, &Window1::synth_SpeakCompleted);
synth->SpeakProgress += gcnew EventHandler<SpeakProgressEventArgs^>(this, &Window1::synth_SpeakProgress);
synth->SpeakStarted += gcnew EventHandler<SpeakStartedEventArgs^>(this, &Window1::synth_SpeakStarted);
synth->VisemeReached += gcnew EventHandler<VisemeReachedEventArgs^>(this, &Window1::synth_VisemeReached);
synth->VoiceChange += gcnew EventHandler<VoiceChangeEventArgs^>(this, &Window1::synth_VoiceChange);

this->btnSpeakText->Click += gcnew RoutedEventHandler(this, &Window1::btnSpeakText_Click);
this->btnWavFile->Click += gcnew RoutedEventHandler(this, &Window1::btnWavFile_Click);
this->btnVoices->Click += gcnew RoutedEventHandler(this, &Window1::btnVoices_Click);
this->btnSpeakSsml->Click += gcnew RoutedEventHandler(this, &Window1::btnSpeakSsml_Click);
this->btnPause->Click += gcnew RoutedEventHandler(this, &Window1::btnPause_Click);
this->btnResume->Click += gcnew RoutedEventHandler(this, &Window1::btnResume_Click);
this->btnPromptBuilder->Click += gcnew RoutedEventHandler(this, &Window1::btnPromptBuilder_Click);
this->btnToXml->Click += gcnew RoutedEventHandler(this, &Window1::btnToXml_Click);
this->btnSsmlPitch->Click += gcnew RoutedEventHandler(this, &Window1::btnSsmlPitch_Click);
this->btnCutIn->Click += gcnew RoutedEventHandler(this, &Window1::btnCutIn_Click);

this->sliderRate->ValueChanged += gcnew RoutedPropertyChangedEventHandler<double>(this, &Window1::sliderRate_ValueChanged);
this->sliderVolume->ValueChanged += gcnew RoutedPropertyChangedEventHandler<double>(this, &Window1::sliderVolume_ValueChanged);
}

private:
void btnCutIn_Click(System::Object ^sender, RoutedEventArgs ^e)
{
synth->SpeakAsyncCancelAll(); //stops current async call
synth->SpeakAsync("this is some interrupting text"); //just appends, unless AsyncCancelAll() called first
//synth.Speak("this is some interrupting text"); //does not interrupt async call
}

void btnSsmlPitch_Click(System::Object ^sender, RoutedEventArgs ^e)
{
XmlDocument ^xd = gcnew XmlDocument();
xd->Load("ssmlPitch.xml");
synth->SpeakSsmlAsync(xd->OuterXml);
}

#pragma region SYNTHESIZER_EVENTS
void synth_VoiceChange(System::Object ^sender, VoiceChangeEventArgs ^e)
{
System::Diagnostics::Debug::WriteLine("VoiceChange : " + e->Voice->Name);
}

void synth_VisemeReached(System::Object ^sender, VisemeReachedEventArgs ^e)
{
System::Diagnostics::Debug::WriteLine("VisemeReached : " + e->Viseme.ToString());
}

void synth_SpeakStarted(System::Object ^sender, SpeakStartedEventArgs ^e)
{
System::Diagnostics::Debug::WriteLine("SpeakStarted");
}

void synth_SpeakProgress(System::Object ^sender, SpeakProgressEventArgs ^e)
{
System::Diagnostics::Debug::WriteLine("SpeakProgress : " + e->AudioPosition.TotalSeconds.ToString());
}

void synth_SpeakCompleted(System::Object ^sender, SpeakCompletedEventArgs ^e)
{
System::Diagnostics::Debug::WriteLine("SpeakCompleted");
}

void synth_PhonemeReached(System::Object ^sender, PhonemeReachedEventArgs ^e)
{
System::Diagnostics::Debug::WriteLine("PhonemeReached : " + e->Phoneme->ToString());
}

void synth_BookmarkReached(System::Object ^sender, BookmarkReachedEventArgs ^e)
{
System::Diagnostics::Debug::WriteLine("BookmarkReached : " + e->Bookmark);
}

void synth_StateChanged(System::Object ^sender, StateChangedEventArgs ^e)
{
System::Diagnostics::Debug::WriteLine("StateChanged : " + e->State.ToString());
lblState->Content = e->State.ToString();
}
#pragma endregion

void btnToXml_Click(System::Object ^sender, RoutedEventArgs ^e)
{
PromptBuilder ^myPrompt = GetBuiltPrompt();
MessageBox::Show(myPrompt->ToXml());
}

void btnPromptBuilder_Click(System::Object ^sender, RoutedEventArgs ^e)
{
PromptBuilder ^pb = GetBuiltPrompt();

//Now let's get the synthesizer to render this message
//SpeechSynthesizer synth = new SpeechSynthesizer();
synth->SetOutputToDefaultAudioDevice();
synth->SpeakAsync(pb);
}

PromptBuilder ^GetBuiltPrompt()
{
//from http://msdn.microsoft.com/msdnmag/issues/06/01/speechinWindowsVista/

//This prompt is quite complicated
//So I'm going to build it first, and then render it.
PromptBuilder ^myPrompt = gcnew PromptBuilder();

//Start the main speaking style
PromptStyle ^mainStyle = gcnew PromptStyle();
mainStyle->Rate = PromptRate::Medium;
mainStyle->Volume = PromptVolume::Loud;
myPrompt->StartStyle(mainStyle);

//Alert the listener
myPrompt->AppendAudio(gcnew Uri("file://c://windows//media//notify.wav"), "Attention!");
myPrompt->AppendText("Here are some important messages.");

//Here's the first important message
myPrompt->AppendTextWithPronunciation("WinFX", "w?n?f?ks");
myPrompt->AppendText("is a great platform.");

//And the second one
myPrompt->AppendTextWithHint("ASP", SayAs::SpellOut);
myPrompt->AppendText("is an acronym for Active Server Pages. Whereas an ASP is a snake.");

myPrompt->AppendBreak();

//Let's emphasise how important these messages are
PromptStyle ^interimStyle = gcnew PromptStyle();
interimStyle->Emphasis = PromptEmphasis::Strong;
myPrompt->StartStyle(interimStyle);
myPrompt->AppendText("Please remember these two things.");
myPrompt->EndStyle();

//Then we can revert to the main speaking style
myPrompt->AppendBreak();
myPrompt->AppendText("Thank you");

myPrompt->EndStyle();
return myPrompt;
}

void btnResume_Click(System::Object ^sender, RoutedEventArgs ^e)
{
synth->Resume();
}

void btnPause_Click(System::Object ^sender, RoutedEventArgs ^e)
{
synth->Pause();
}

void btnSpeakSsml_Click(System::Object ^sender, RoutedEventArgs ^e)
{
//http://www.w3.org/TR/speech-synthesis/
synth->SetOutputToDefaultAudioDevice();

//XmlDocument xd = new XmlDocument();
////xd.Load("sampleSSML.xml"); //works
//xd.Load("ssmlPitch.xml"); //works
//synth.SpeakSsmlAsync(xd.OuterXml);

//XmlDocument xd = new XmlDocument();
////xd.Load("sampleSSML.xml"); //TODO doesn't work?
//xd.Load("ssmlPitch.xml"); //TODO doesn't work?
//PromptBuilder pb = new PromptBuilder();
//pb.AppendSsmlMarkup(xd.DocumentElement.OuterXml);
//synth.SpeakAsync(pb);

PromptBuilder ^pb = gcnew PromptBuilder();
pb->AppendText("blah");
pb->AppendSsml("sampleSSML.xml"); //works
//pb.AppendSsml("ssmlPitch.xml"); //TODO doesn't work
synth->SpeakAsync(pb);
}

void sliderVolume_ValueChanged(System::Object ^sender, RoutedPropertyChangedEventArgs<double> ^e)
{
synth->Volume = safe_cast<int>(sliderVolume->Value);
}

void sliderRate_ValueChanged(System::Object ^sender, RoutedPropertyChangedEventArgs<double> ^e)
{
synth->Rate = safe_cast<int>(sliderRate->Value);
}

void btnVoices_Click(System::Object ^sender, RoutedEventArgs ^e)
{
ReadOnlyCollection<InstalledVoice^> ^voices = synth->GetInstalledVoices();
System::String ^retVal = System::String::Empty;
for each (InstalledVoice ^iv in voices)
{
retVal += iv->VoiceInfo->Name + "/r/n";
}
MessageBox::Show(retVal);
}

void btnWavFile_Click(System::Object ^sender, RoutedEventArgs ^e)
{
synth->SetOutputToWaveFile("spoken.wav");
synth->Speak(GetText());
synth->SetOutputToNull();
MessageBox::Show("done");
}

void btnSpeakText_Click(System::Object ^sender, RoutedEventArgs ^e)
{
synth->SetOutputToDefaultAudioDevice();
synth->SpeakAsync(GetText());
}

System::String ^GetText()
{
return txtToSpeak->Text->Trim();
}

};
}

需要的留下EMAIL,我给大家发

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics