Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Threading;
- using System.Windows.Forms;
- using NAudio.Wave;
- using YandexAPI.SpeechKitCloud;
- using YandexAPI.SpeechKitCloud.ASR;
- using YandexAPI.SpeechKitCloud.TTS;
- using MySql.Data.MySqlClient;
- using System.Data;
- using System.Media;
- using System.Windows;
- using System.Threading.Tasks;
- using System.Net;
- using WMPLib;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- static string name;
- WindowsMediaPlayer audio = new WindowsMediaPlayer();
- string connStr = "server=localhost;user=root;database=smarthome;port=3306;password=root;SslMode=none";
- YandexASR asr = new YandexASR("key");
- YandexTTS tts = new YandexTTS("key");
- private WaveIn waveIn;
- private ParserCommand parser;
- public Form1()
- {
- InitializeComponent();
- waveIn = new WaveIn();
- waveIn.DeviceNumber = 0;
- waveIn.DataAvailable += DataAvailable;
- waveIn.RecordingStopped += RecordingStopped;
- waveIn.WaveFormat = new WaveFormat(8000, 1);
- parser = new ParserCommand(waveIn.WaveFormat);
- waveIn.StartRecording();
- }
- private void DataAvailable(object sender, WaveInEventArgs e)
- {
- if (InvokeRequired)
- {
- BeginInvoke(new EventHandler<WaveInEventArgs>(DataAvailable), sender, e);
- }
- else
- {
- parser.ParseAudioSegment(e);
- if (File.Exists("1.wav"))
- {
- byte[] voice = File.ReadAllBytes("1.wav");
- AsrResponse response = asr.VoiceToText(voice, Topic.Queries, Lang.Ru, AudioFormat.WAV); //Получаем варианты текста из голоса
- foreach (Variant variant in response.Variants)
- textBox1.Text = variant.Text;
- try { File.Delete("1.wav"); } catch { }
- if (textBox1.TextLength > 1)
- {
- //создаем параметризированный запрос
- MySqlConnection conn = new MySqlConnection(connStr);
- // устанавливаем соединение с БД
- conn.Open();
- // запрос
- string sql = "SELECT answer FROM wordbook WHERE question = "" + textBox1.Text + """;
- // объект для выполнения SQL-запроса
- MySqlCommand command = new MySqlCommand(sql, conn);
- // выполняем запрос и получаем ответ
- try { name = command.ExecuteScalar().ToString(); } catch { Console.WriteLine("Я хз ,что ответить"); return; }
- // выводим ответ в консоль
- Console.WriteLine(name);
- // закрываем соединение с БД
- conn.Close();
- byte[] answer = tts.GetVoice(name, Lang.Ru);
- MemoryStream stream = new MemoryStream(answer);
- Button2_Click(button2, null);
- tts.SaveAudio(answer, "answer", "", Format.WAV);
- audio.URL = "answer.wav";
- audio.PlayStateChange += new _WMPOCXEvents_PlayStateChangeEventHandler(audio_PlayStateChange);
- audio.controls.play();
- }
- }
- }
- }
- void audio_PlayStateChange(int NewState)
- {
- if (NewState == (int)WMPPlayState.wmppsMediaEnded)
- {
- audio.close();
- audio = null;
- Button2_Click(button2, null);
- try
- {
- System.Diagnostics.Process[] prc = System.Diagnostics.Process.GetProcessesByName("wmplayer");
- if (prc.Length > 0)
- prc[prc.Length - 1].Kill();
- File.Delete("answer.wav");
- }
- catch (Exception)
- {
- }
- }
- }
- private void RecordingStopped(object sender, EventArgs e)
- {
- if (InvokeRequired)
- {
- BeginInvoke(new EventHandler(RecordingStopped), sender, e);
- }
- else
- {
- waveIn.Dispose();
- waveIn = null;
- }
- }
- private void Button2_Click(object sender, EventArgs e)
- {
- if (waveIn == null)
- {
- waveIn = new WaveIn();
- waveIn.DeviceNumber = 0;
- waveIn.DataAvailable += DataAvailable;
- waveIn.RecordingStopped += RecordingStopped;
- waveIn.WaveFormat = new WaveFormat(8000, 1);
- waveIn.StartRecording();
- button2.Text = "Стоп";
- }
- else
- {
- waveIn.StopRecording();
- button2.Text = "Старт";
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment