基于VisualC 2010开发基于Windows7的语音识别与语音合成-程序员投稿课案

发布时间:2018-10-21 11:31:06   来源:文档文库   
字号:

基于Visual C++2010开发基于Windows7的语音识别与语音合成

-----语音技术业内资深专家尹成力作

Windows7相比Vista已经进一步改善了语音技术的体验,改进了语音识别的识别率与语音合成的朗读声音效果。语音技术与多点触摸是Windows7最耀眼的技术靓点之一。

语音合成和语音识别技术是实现人机语音通信,建立一个有听和讲能力的口语系统所必需的两项关键技术。使电脑具有类似于人一样的说话能力,是当今时代信息产业的重要竞争市场。

语音合成,又称文语转换(Text to Speech)技术,能将任意文字信息实时转化为标准流畅的自然语音并朗读出来。它涉及声学、语言学、数字信号处理、计算机科学等多个学科,是中文信息处理领域的一项前沿技术,解决的主要问题就是如何将文字信息转化为可听的声音信息,即让机器像人一样开口说话。

语音识别,就是让机器通过分析和理解过程把语音信号转变为相应的文本或命令的尖端技术。语音识别是一门交叉学科,所涉及的领域包括:信号处理、模式识别、概率论和信息论、发声机理和听觉机理、人工智能等等。语音识别技术与语音合成技术一同成为实现人机语音通信,建立有听和讲能力的语音系统所必需的两项关键技术。

让我们来体验下用Visual C++2010开发Windows7的语音识别与语音合成的快感,欣赏微软最新的Windows7新技术给人们带来的便利。自己实现语音交互技术,让自己开发的软件(准备运行在Windows7上的)可以与用户进行语音交互。Windows7的语音识别与语音合成与VistaXP的有所不同,Windows7下开发的应用于VistaXP可能会遇到API兼容问题。

因为Windows7都已经附带了.Net平台,本文讲述用Visual C++2010开发基于CLR

语音技术应用程序,并同时提供了基于MFC程序兼容调用CLR的办法,无论Visual C++基于CLR或者MFC的程序,如果想升级到Windows7,都可以很快为自己添加语音识别功能,语音朗读功能。

Visual C++2010开发基于CLRWindows7语音技术

1.创建一个CLRWindows Forms Application的项目,名称命名为CSDN-speech

2.插入下列控件,如下图

3.单击Project ,选择项目属性

3.在项目属性中我们将看到下图所示

4.单击按钮,Add New Reference,添加System.Speech(版本为4.0)的调用

5.单击朗读按钮,创建朗读按钮触发程序

6.在程序初始化部分添加下列代码,以添加对于语音技术的引用 ,还有对于语言文化类的引用。

using namespace System::Speech;

using namespace System::Speech::Recognition;

using namespace System::Speech::Recognition::SrgsGrammar;

using namespace System::Speech::Synthesis;

using namespace System::Globalization;

7.在按钮“朗读下面添加下列代码”

SpeechSynthesizer ^synth;

synth =gcnew SpeechSynthesizer();

synth->Speak(this->textBox1->Text);

8.实验语音朗读,如下图,听听来自Windows7的语音朗读的声音效果

9.单击项目资源管理器CSDN-speech节点,添加一个类

10.添加一个纯c++

11.命名为yuyin

12.yuyin.h添加如下代码,代码具体意义见注释

#pragma once

using namespace System::Linq;

using namespace System::Text;

using namespace System::Speech;

using namespace System::Speech::Synthesis;

using namespace System::Speech::Recognition;

using namespace System::Globalization;

using namespace System::Windows::Forms;

namespace CSDNspeech {

public ref class yuyin

{

public:

SpeechRecognitionEngine ^recognizer; //定义语音识别器

DictationGrammar ^dictationGrammar;//定义语法

System::Windows::Forms::Control ^cDisplay; //定义一个关联文本控件

yuyin(array ^fg); //创建关键词语列表

private:

void InitializeSpeechRecognitionEngine(array ^fg);

public:

void BeginRec(Control ^tbResult); //开始语音识别,关联窗口控件

void over();//结束语音识别

virtual Grammar ^CreateCustomGrammar(array ^fg);

private:

void TurnSpeechRecognitionOn(); //开启语音识别

void TurnSpeechRecognitionOff();//关闭语音识别

void recognizer_SpeechHypothesized(System::Object ^sender, SpeechHypothesizedEventArgs ^e);

void recognizer_SpeechRecognized(System::Object ^sender, SpeechRecognizedEventArgs ^e); //语音识别器

void TurnDictationOn(); //打开词典

void TurnDictationOff();//关闭词典

};

}

13.yuyin.cpp代码更新为下列代码,代码具体含义见注释

#include "StdAfx.h"

#include "yuyin.h"

using namespace System;

using namespace System::Collections::Generic;

using namespace System::Linq;

using namespace System::Text;

using namespace System::Speech;

using namespace System::Speech::Synthesis;

using namespace System::Speech::Recognition;

using namespace System::Globalization;

using namespace System::Windows::Forms;

namespace CSDNspeech {

yuyin::yuyin(array ^fg) //fg为语音识别关键词语列表

{

CultureInfo ^myCIintl = gcnew CultureInfo("zh-CN", false);//创建简体中文标示

for each (RecognizerInfo ^config in SpeechRecognitionEngine::InstalledRecognizers()) //获取所有语音引擎

{

if (config->Culture->Equals(myCIintl) && config->Id == "MS-2052-80-DESK") //选择中文语音识别引擎

{

recognizer = gcnew SpeechRecognitionEngine(config);

break;

}

}

if (recognizer != nullptr)

{

InitializeSpeechRecognitionEngine(fg);//选择默认语音引擎

dictationGrammar = gcnew DictationGrammar();

}

else

{

MessageBox::Show("创建语音识别失败");

}

}

void yuyin::InitializeSpeechRecognitionEngine(array ^fg) //语音识别初始化

{

recognizer->SetInputToDefaultAudioDevice(); //选择默认语音设备

Grammar ^customGrammar = CreateCustomGrammar(fg);//定义语法

recognizer->UnloadAllGrammars();

recognizer->LoadGrammar(customGrammar);

recognizer->SpeechRecognized += gcnew EventHandler(this, &yuyin::recognizer_SpeechRecognized);

recognizer->SpeechHypothesized += gcnew EventHandler(this, &yuyin::recognizer_SpeechHypothesized);

}

void yuyin::BeginRec(Control ^tbResult)

{

TurnSpeechRecognitionOn();

TurnDictationOn();

cDisplay = tbResult;

}

void yuyin::over()

{

TurnSpeechRecognitionOff();

}

Grammar ^yuyin::CreateCustomGrammar(array ^fg) //创建语法

{

GrammarBuilder ^grammarBuilder = gcnew GrammarBuilder();

grammarBuilder->Append(gcnew Choices(fg));

return gcnew Grammar(grammarBuilder);

}

void yuyin::TurnSpeechRecognitionOn()

{

if (recognizer != nullptr)

{

recognizer->RecognizeAsync(RecognizeMode::Multiple);

}

else

{

MessageBox::Show("创建语音识别失败");

}

}

void yuyin::TurnSpeechRecognitionOff()

{

if (recognizer != nullptr)

{

recognizer->RecognizeAsyncStop();

TurnDictationOff();

}

else

{

MessageBox::Show("创建语音识别失败");

}

}

void yuyin::recognizer_SpeechHypothesized(System::Object ^sender, SpeechHypothesizedEventArgs ^e) //语音识别状态

{

}

void yuyin::recognizer_SpeechRecognized(System::Object ^sender, SpeechRecognizedEventArgs ^e) //显示语音识别结果

{

System::String ^text = e->Result->Text;

cDisplay->Text = text;

}

void yuyin::TurnDictationOn()

{

if (recognizer != nullptr)

{

recognizer->LoadGrammar(dictationGrammar);

}

else

{

MessageBox::Show("创建语音识别失败");

}

}

void yuyin::TurnDictationOff()

{

if (dictationGrammar != nullptr)

{

recognizer->UnloadGrammar(dictationGrammar);

}

else

{

MessageBox::Show("创建语音识别失败");

}

} }

14.Form1.h里面添加头文件支持

#include "yuyin.h"

15.单击开启语音识别,添加下列代码

array ^fg ;

fg = gcnew array {"尹成","中国","专家","技术"};

//定义一个优先识别的词组

yuyin^ sdk;

sdk =gcnew yuyin(fg);

sdk->BeginRec(this->textBox1);

16.按下F5,打开麦克风说话看效果

Visual C++2010MFC程序里兼容 CLR代码(MFC.Net4.0混合编程)可以在MFC里调用CLR代码(不局限语音朗读与语音合成,可以为老旧的MFC程序添加.Net4.0功能)

1.打开VS2010,创建一个CSDNdialogMFC 对话框程序,按照默认,直接点Finish

2.MFC对话框里面添加一个按钮,将名称改为”MFC里面玩CLR”,

3. 在配置属性中,选择general-》选择clc Configuration Properties/General/Common Language Runtime support

4.为项目添加一个项,选择创建一个Windows Form,命名为CSDN,

5.并插入一个按钮“我来自于MFCdiaolog

6.双击按钮“我来自于MFCdiaolog”添加代码

MessageBox::Show(L"我是CSDN著名技术尹成的杰作");

7.双击对话框的按钮”MFC里面玩CLR”,在头文件支持里面添加

#include"csdn.h"

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

using namespace csdndialog;

添加下列代码

csdn ^ ff=gcnew csdn();

ff->Show();

8.按下F5,编译执行,分别点击按钮看效果

读者看懂了整个例子,就知道如何在MFC里面调用CLR实现语音识别,语音朗读,同样.net4.0的所有功能都可以由MFC来调用,MFC是九阳神功,.Net是九阴真经,用吸星大法将两个绝世武功合二为一,必将无敌于天下,同时MFC的老代码可以借助.Net这个九阴真经武功恢复活力,在未来,Windows7必然占据操作系统的主流,MFC.net都是重要的开发平台,融合是一种趋势!

本文来源:https://www.2haoxitong.net/k/doc/76c0efe7c67da26925c52cc58bd63186bceb9231.html

《基于VisualC 2010开发基于Windows7的语音识别与语音合成-程序员投稿课案.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式