Form1.cs
上传用户:cookies0
上传日期:2022-07-28
资源大小:284k
文件大小:2k
源码类别:

语音合成与识别

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using SpeechLib;
  9. namespace tryreco
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void Form1_Load(object sender, EventArgs e)
  18.         {
  19.         }
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             SpRecognition cc = new SpRecognition();
  23.             cc.BeginRec(textBox1);
  24.         }
  25.         private void button2_Click_1(object sender, EventArgs e)
  26.         {
  27.             SpRecognition cc = new SpRecognition();
  28.             cc.CloseRec();
  29.         }
  30.         private void button3_Click(object sender, EventArgs e)
  31.         {
  32.             SpVoiceClass voice = new SpVoiceClass();
  33.             voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(3);
  34.             voice.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFDefault); ;
  35.         }
  36.         private void button4_Click(object sender, EventArgs e) 
  37.         {
  38.             SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
  39.             SpVoice voice = new SpVoice();
  40.             SaveFileDialog dialog = new SaveFileDialog();
  41.             dialog.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
  42.             dialog.Title = "保存WAV文件";
  43.             dialog.FilterIndex = 2;
  44.             dialog.RestoreDirectory = true;
  45.             if (dialog.ShowDialog() == DialogResult.OK)
  46.             {
  47.                 SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
  48.                 SpFileStream spFileStream = new SpFileStream();
  49.                 spFileStream.Open(dialog.FileName, spFileMode, false);
  50.                 voice.AudioOutputStream = spFileStream;
  51.                 voice.Speak(textBox1.Text, spFlags);
  52.                 voice.WaitUntilDone(1000);
  53.                 //上面两句一定要写上,否则产生的文件没有声音
  54.                 //WaitUntilDone的后面的smTimeout是一个int型
  55.                 spFileStream.Close();
  56.             }
  57.         }
  58.     }
  59. }