Guest User

Untitled

a guest
May 20th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Windows.Forms;
  5. using NAudio.Wave;
  6. using YandexAPI.SpeechKitCloud;
  7. using YandexAPI.SpeechKitCloud.ASR;
  8. using YandexAPI.SpeechKitCloud.TTS;
  9. using MySql.Data.MySqlClient;
  10. using System.Data;
  11. using System.Media;
  12. using System.Windows;
  13. using System.Threading.Tasks;
  14. using System.Net;
  15. using WMPLib;
  16.  
  17. namespace WindowsFormsApplication1
  18. {
  19.  
  20. public partial class Form1 : Form
  21. {
  22. static string name;
  23. WindowsMediaPlayer audio = new WindowsMediaPlayer();
  24. string connStr = "server=localhost;user=root;database=smarthome;port=3306;password=root;SslMode=none";
  25. YandexASR asr = new YandexASR("key");
  26. YandexTTS tts = new YandexTTS("key");
  27. private WaveIn waveIn;
  28. private ParserCommand parser;
  29.  
  30. public Form1()
  31. {
  32. InitializeComponent();
  33. waveIn = new WaveIn();
  34. waveIn.DeviceNumber = 0;
  35. waveIn.DataAvailable += DataAvailable;
  36. waveIn.RecordingStopped += RecordingStopped;
  37. waveIn.WaveFormat = new WaveFormat(8000, 1);
  38. parser = new ParserCommand(waveIn.WaveFormat);
  39. waveIn.StartRecording();
  40. }
  41.  
  42. private void DataAvailable(object sender, WaveInEventArgs e)
  43. {
  44. if (InvokeRequired)
  45. {
  46. BeginInvoke(new EventHandler<WaveInEventArgs>(DataAvailable), sender, e);
  47. }
  48. else
  49. {
  50. parser.ParseAudioSegment(e);
  51. if (File.Exists("1.wav"))
  52. {
  53. byte[] voice = File.ReadAllBytes("1.wav");
  54. AsrResponse response = asr.VoiceToText(voice, Topic.Queries, Lang.Ru, AudioFormat.WAV); //Получаем варианты текста из голоса
  55. foreach (Variant variant in response.Variants)
  56. textBox1.Text = variant.Text;
  57. try { File.Delete("1.wav"); } catch { }
  58. if (textBox1.TextLength > 1)
  59. {
  60. //создаем параметризированный запрос
  61. MySqlConnection conn = new MySqlConnection(connStr);
  62. // устанавливаем соединение с БД
  63. conn.Open();
  64. // запрос
  65. string sql = "SELECT answer FROM wordbook WHERE question = "" + textBox1.Text + """;
  66. // объект для выполнения SQL-запроса
  67. MySqlCommand command = new MySqlCommand(sql, conn);
  68. // выполняем запрос и получаем ответ
  69. try { name = command.ExecuteScalar().ToString(); } catch { Console.WriteLine("Я хз ,что ответить"); return; }
  70. // выводим ответ в консоль
  71. Console.WriteLine(name);
  72. // закрываем соединение с БД
  73. conn.Close();
  74.  
  75. byte[] answer = tts.GetVoice(name, Lang.Ru);
  76.  
  77. MemoryStream stream = new MemoryStream(answer);
  78.  
  79. Button2_Click(button2, null);
  80. tts.SaveAudio(answer, "answer", "", Format.WAV);
  81.  
  82. audio.URL = "answer.wav";
  83. audio.PlayStateChange += new _WMPOCXEvents_PlayStateChangeEventHandler(audio_PlayStateChange);
  84.  
  85. audio.controls.play();
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. }
  95. }
  96.  
  97. }
  98. }
  99. void audio_PlayStateChange(int NewState)
  100. {
  101. if (NewState == (int)WMPPlayState.wmppsMediaEnded)
  102. {
  103. audio.close();
  104. audio = null;
  105. Button2_Click(button2, null);
  106.  
  107. try
  108. {
  109. System.Diagnostics.Process[] prc = System.Diagnostics.Process.GetProcessesByName("wmplayer");
  110. if (prc.Length > 0)
  111. prc[prc.Length - 1].Kill();
  112. File.Delete("answer.wav");
  113. }
  114. catch (Exception)
  115. {
  116. }
  117. }
  118. }
  119.  
  120. private void RecordingStopped(object sender, EventArgs e)
  121. {
  122. if (InvokeRequired)
  123. {
  124. BeginInvoke(new EventHandler(RecordingStopped), sender, e);
  125. }
  126. else
  127. {
  128. waveIn.Dispose();
  129. waveIn = null;
  130. }
  131. }
  132.  
  133. private void Button2_Click(object sender, EventArgs e)
  134. {
  135. if (waveIn == null)
  136. {
  137. waveIn = new WaveIn();
  138. waveIn.DeviceNumber = 0;
  139. waveIn.DataAvailable += DataAvailable;
  140. waveIn.RecordingStopped += RecordingStopped;
  141. waveIn.WaveFormat = new WaveFormat(8000, 1);
  142.  
  143. waveIn.StartRecording();
  144. button2.Text = "Стоп";
  145.  
  146. }
  147. else
  148. {
  149. waveIn.StopRecording();
  150. button2.Text = "Старт";
  151. }
  152. }
  153. }
  154. }
Add Comment
Please, Sign In to add comment