static void Main(string[] args) { // for recording waveFileWriter = new WaveFileWriter(@"C:\rec\out.wav", new WaveFormat(44100, 2)); var sound = new MySound(); sound.SetWaveFormat(44100, 2); sound.init(); waveOut = new WaveOut(); waveOut.Init(sound); waveOut.Play(); ConsoleKeyInfo keyInfo; bool loop = true; while (loop) { keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.Q) { waveOut.Stop(); waveOut.Dispose(); waveFileWriter.Close(); waveFileWriter.Dispose(); loop = false; } } }
private void Source_RecordingStopped(object sender, StoppedEventArgs e) { if (this.InvokeRequired) { this.BeginInvoke(new EventHandler <StoppedEventArgs>(Source_RecordingStopped), sender, e); } else { if (source != null) // 关闭录音对象 { this.source.Dispose(); this.source = null; } if (encoder != null)//关闭文件流 { encoder.Close(); encoder.Dispose(); encoder = null; } if (e.Exception != null) { MessageBox.Show(String.Format("出现问题 {0}", e.Exception.Message)); } } }
private void sourceStreamDataAvailable(object sender, WaveInEventArgs e) { string path = outputPath + "wavsam" + nFiles + ".wav"; writer = new WaveFileWriter(path, waveFormat); writer.Write(e.Buffer, 0, e.Buffer.Length); writer.Flush(); writer.Close(); nFiles++; Process process = new Process(); process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = "praatcon.exe"; String male; if (isMale) male = "yes"; else male = "no"; process.StartInfo.Arguments = "extract_measures.praat " + path + " " + male; // process.StartInfo.RedirectStandardOutput = true; process.Start(); process.WaitForExit(); ResultEventArgs args = new ResultEventArgs(); // args.text = output; OnResults(args); // args.nWords = err.Length; }
/// <summary> /// Stop the streaming to disk and clean up /// </summary> public void StopStreamingToDisk() { streamToDisk = false; if (writer != null) { writer.Close(); } }
//Event arguments to stop recording private void wavSource_RecordingStopped(object sender, EventArgs e) { wavSource.Dispose(); wavSource = null; wavFile.Close(); wavFile.Dispose(); wavFile = null; }
public static void ProcessAudioWave(string fileIn, string fileOut, float newTempo = 1, float newPitch = 1, float newRate = 1) { WaveFileReader reader = new WaveFileReader(fileIn); int numChannels = reader.WaveFormat.Channels; if (numChannels > 2) throw new Exception("SoundTouch supports only mono or stereo."); int sampleRate = reader.WaveFormat.SampleRate; int bitPerSample = reader.WaveFormat.BitsPerSample; const int BUFFER_SIZE = 1024 * 16; SoundStretcher stretcher = new SoundStretcher(sampleRate, numChannels); WaveFileWriter writer = new WaveFileWriter(fileOut, new WaveFormat(sampleRate, 16, numChannels)); stretcher.Tempo = newTempo; stretcher.Pitch = newPitch; stretcher.Rate = newRate; byte[] buffer = new byte[BUFFER_SIZE]; short[] buffer2 = null; if (bitPerSample != 16 && bitPerSample != 8) { throw new Exception("Not implemented yet."); } if (bitPerSample == 8) { buffer2 = new short[BUFFER_SIZE]; } bool finished = false; while (true) { int bytesRead = 0; if (!finished) { bytesRead = reader.Read(buffer, 0, BUFFER_SIZE); if (bytesRead == 0) { finished = true; stretcher.Flush(); } else { if (bitPerSample == 16) { stretcher.PutSamplesFromBuffer(buffer, 0, bytesRead); } else if (bitPerSample == 8) { for (int i = 0; i < BUFFER_SIZE; i++) buffer2[i] = (short)((buffer[i] - 128) * 256); stretcher.PutSamples(buffer2); } } } bytesRead = stretcher.ReceiveSamplesToBuffer(buffer, 0, BUFFER_SIZE); writer.Write(buffer, 0, bytesRead); if (finished && bytesRead == 0) break; } reader.Close(); writer.Close(); }
XSample ConvertSample(int count, SSampleHeader sh, byte [] sample, Zone izone) { // Indices in sf2 are numbers of samples, not byte length. So double them. var xs = new XSample (); xs.Extension = ".wav"; xs.LoopStart =(sh.StartLoop - sh.Start); xs.LoopEnd = (sh.EndLoop - sh.Start); int sampleModes = izone.SampleModes (); xs.LoopMode = sampleModes == 0 ? InstrumentSampleLoopMode.Off : InstrumentSampleLoopMode.Forward; xs.Name = String.Format ("Sample{0:D02} ({1})", count, sh.SampleName); xs.BaseNote = (sbyte) izone.OverridingRootKey (); // xs.Volume = (izone.VelocityRange () & 0xFF00 >> 8); // low range if (xs.BaseNote == 0) xs.BaseNote = (sbyte) sh.OriginalPitch; //Console.WriteLine ("{0} ({1}/{2}/{3}/{4}) {5}:{6}:{7}:{8}", xs.Name, sh.Start, sh.StartLoop, sh.EndLoop, sh.End, sh.SampleRate != 0xAC44 ? sh.SampleRate.ToString () : "", sh.OriginalPitch != 60 ? sh.OriginalPitch.ToString () : "", sh.PitchCorrection != 0 ? sh.PitchCorrection.ToString () : "", sampleModes); xs.FileName = xs.Name + ".wav"; var ms = new MemoryStream (); var wfw = new WaveFileWriter (ms, new WaveFormat ((int) sh.SampleRate, 16, 1)); wfw.WriteData (sample, 2 * (int) sh.Start, 2 * (int) (sh.End - sh.Start)); wfw.Close (); xs.Buffer = ms.ToArray (); return xs; }
void ExportWAVBtnClick(object sender, EventArgs e) { if(saveFileDialog1.ShowDialog() == DialogResult.OK) { byte[] buf = MakeByteBuf(); WaveFileWriter wfw = new WaveFileWriter(saveFileDialog1.FileName, new WaveFormat(samplingRate, 8, 1)); wfw.WriteData(buf, 0, buf.Length); wfw.Close(); } }
/// <summary> /// Stop the streaming to disk and clean up /// </summary> public void StopStreamingToDisk() { streamToDisk = false; writer?.Close(); }
private void ButtonSave_Click(object sender, EventArgs e) { this.buttonPause.Enabled = false; this.buttonStart.Enabled = false; this.buttonStop.Enabled = false; this.buttonNewEvent.Enabled = false; this.buttonClear.Enabled = false; this.buttonSave.Enabled = false; WaveFileWriter writer = new WaveFileWriter(this.testName + ".wav", this.waveFormat); foreach(DataRow row in dataSet.WaveTable.Rows) { writer.WriteByte((byte)row["Amplitude"]); } writer.Close(); var result = new StringBuilder(); for (int i = 0; i < dataSet.WaveTable.Columns.Count; i++) { result.Append(dataSet.WaveTable.Columns[i].ColumnName); result.Append(i == dataSet.WaveTable.Columns.Count - 1 ? "\n" : ","); } foreach (DataRow row in dataSet.WaveTable.Rows) { for (int i = 0; i < dataSet.WaveTable.Columns.Count; i++) { result.Append(row[i].ToString()); result.Append(i == dataSet.WaveTable.Columns.Count - 1 ? "\n" : ","); } } StreamWriter waveData = System.IO.File.CreateText(this.testName + "-wave.csv"); waveData.Write(result.ToString()); waveData.Close(); result = new StringBuilder(); for (int i = 0; i < dataSet.EventTable.Columns.Count; i++) { result.Append(dataSet.EventTable.Columns[i].ColumnName); result.Append(i == dataSet.EventTable.Columns.Count - 1 ? "\n" : ","); } foreach (DataRow row in dataSet.EventTable.Rows) { for (int i = 0; i < dataSet.EventTable.Columns.Count; i++) { result.Append(row[i].ToString()); result.Append(i == dataSet.EventTable.Columns.Count - 1 ? "\n" : ","); } } StreamWriter eventData = System.IO.File.CreateText(this.testName + "-event.csv"); eventData.Write(result.ToString()); eventData.Close(); this.buttonPause.Enabled = false; this.buttonStart.Enabled = true; this.buttonStop.Enabled = false; this.buttonNewEvent.Enabled = false; this.buttonClear.Enabled = true; this.buttonSave.Enabled = false; }
// Генерация WAV-файла public void WordToWAV() { // Считываем параметры с движков регуляторов numberofwords = trackBar_N.Value; interval = trackBar_Pause.Value; speed = trackBar_Speed.Value; tone = trackBar_Tone.Value; // Выбираем папку для звуковых файлов string fileName = GetAudioFolderName(); string fileNameOld = fileName; // Сохраним для переноса файлов из корня (в WinXP) if (fileName == "cancel") // Если папка не выбрана, то выходим return; if (alternative_mp3_flag) // В Windows-XP сохраняем в корень диска { fileName = fileName.Remove(fileName.IndexOf(":")+1); } // Текущая дата string currentDate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString(); string currentTime = DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(); // Добавляем дату и время к именам звуковых файлов string audioFileName = originalAudioFileName + "-" + currentDate + currentTime; // Добавляем к папке имя звуковых файлов fileName = fileName + audioFileName; fileNameOld = fileNameOld + audioFileName; // Выставить параметры WAV-файла WaveFormat waveFormat = new WaveFormat(8000, 16, 2); int one_milli_second = 2 * waveFormat.SampleRate / 1000; float amplitude = 0.25f; float frequency = tone; using (WaveFileWriter writer = new WaveFileWriter(fileName + ".wav", waveFormat)) { // Воспроизведение слова int dot = speed_calibr * one_milli_second / speed; // Длительность точки int dash = 3 * dot; // Длительность тире int dash_conjoint = dash; // Промежуток между слитными буквами типа <KN> // Если стоит "галка", то использовать паузу перед началом if (checkBox_StartPause.Checked) { startpause_flag = true; // Использовать паузу WavPause(writer, startpause * 1000); // Задержка перед началом передачи } // Выводим случайные слова из массива Random rnd = new Random(); if (non_random_flag) { numberofwords = AllWords.Length; // При отмене случайного выбора слов выводим все слова } // один раз for (int i = 1; i <= numberofwords; i++) { int num; if (!non_random_flag) { num = rnd.Next(0, AllWords.Length); // Номер случайного слова } else { num = i - 1; } string word = AllWords[num].ToUpper(); // Получаем случайное слово в верхнем // регистре for (int j = 0; j < word.Length; j++) // Перебираем буквы в слове { // Воспроизводим букву char symbol = word[j]; // Слитные буквы для <AS> и т.п. if (symbol == '<') dash_conjoint = dot; // "<" - слитное слово if (symbol == '>') { dash_conjoint = dash; // ">" - раздельное слово WavPause(writer, dash_conjoint); } // Цифры if (symbol == '0') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == '1') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == '2') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == '3') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == '4') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == '5') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == '6') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == '7') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == '8') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == '9') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } // Буквы if (symbol == 'A' || symbol == 'А') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'B' || symbol == 'Б') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'C' || symbol == 'Ц') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'D' || symbol == 'Д') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'E' || symbol == 'Е' || symbol == 'Ё') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'F' || symbol == 'Ф') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'G' || symbol == 'Г') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'H' || symbol == 'Х') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'I' || symbol == 'И') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'J' || symbol == 'Й') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'K' || symbol == 'К') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'L' || symbol == 'Л') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'M' || symbol == 'М') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'N' || symbol == 'Н') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'O' || symbol == 'О') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'P' || symbol == 'П') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'Q' || symbol == 'Щ') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'R' || symbol == 'Р') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'S' || symbol == 'С') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'T' || symbol == 'Т') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'U' || symbol == 'У') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'V' || symbol == 'Ж') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'W' || symbol == 'В') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'X' || symbol == 'Ь') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'Y' || symbol == 'Ы') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'Z' || symbol == 'З') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'Ч') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'Ш') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'Э') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'Ю') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } if (symbol == 'Я') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dash_conjoint); } // Символы if (symbol == '=') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); Thread.Sleep(dash); } if (symbol == '?') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); Thread.Sleep(dash); } if (symbol == '/') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); Thread.Sleep(dash); } if (symbol == '@') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); Thread.Sleep(dash); } if (symbol == '.' && russianpunktuation_flag == true) { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); Thread.Sleep(dash); } if (symbol == '.' && russianpunktuation_flag == false) { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); Thread.Sleep(dash); } if (symbol == ',' && russianpunktuation_flag == true) { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); Thread.Sleep(dash); } if (symbol == ',' && russianpunktuation_flag == false) { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); Thread.Sleep(dash); } if (symbol == ';') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); Thread.Sleep(dash); } if (symbol == ':') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); Thread.Sleep(dash); } if (symbol == '!' && russianpunktuation_flag == true) { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); Thread.Sleep(dash); } if (symbol == '!' && russianpunktuation_flag == false) { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); Thread.Sleep(dash); } if (symbol == '-') { WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); Thread.Sleep(dash); } if (symbol == '\'') { WavPlay(writer, dot, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dash, amplitude, frequency); WavPause(writer, dot); WavPlay(writer, dot, amplitude, frequency); Thread.Sleep(dash); } } // End For j if (non_random_flag) { screen_Out(word); // вывод на label непрерывно } else { screen_Out_By_10(i, word); // вывод на label по 10 слов } int dynamic_interval = 1; // Длительность динамического интервала между словами if (dynamic_interval_flag) { dynamic_interval = word.Length; } WavPause(writer, interval * dash * dynamic_interval); // Пауза между словами } // End for(i) writer.Close(); writer.Dispose(); } // end using // Конвертим WAV-файл в MP3-файл if (File.Exists(fileName + ".wav")) ConvertWavToMP3(fileName); else { if (english_flag) MessageBox.Show("No exist WAV-file to conver in MP3."); else MessageBox.Show(fileName + ".wav ", "Нет WAV-файла для конвертации в MP3."); } // Переместить звуковые файлы if (alternative_mp3_flag) { try { // MessageBox.Show("Конвертим с задержкой MP3."); // Не успевает LAME отрабоать - заддерка нужна Thread.Sleep(5000); // 5 секунд if (File.Exists(fileName + ".mp3")) { // Удаляем старые файлы if (File.Exists(fileNameOld + ".mp3 ")) File.Delete(fileNameOld + ".mp3 "); //Переносим файлы из корня диска в папку, указанную пользователем File.Move(fileName + ".mp3 ", fileNameOld + ".mp3"); } else MessageBox.Show(fileName + ".mp3 ", "Нет MP3-файла!"); if (File.Exists(fileName + ".wav")) { //MessageBox.Show(fileName + ".wav ", "Старый файл WAV"); //MessageBox.Show(fileNameOld + ".wav ", "Новый файл WAV"); // MessageBox.Show(fileName + ".mp3 ", "Старый файл MP3"); // MessageBox.Show(fileNameOld + ".mp3 ", "Новый файл MP3"); // Удаляем старые файлы if (File.Exists(fileNameOld + ".wav ")) File.Delete(fileNameOld + ".wav "); //Переносим файлы из корня диска в папку, указанную пользователем File.Move(fileName + ".wav ", fileNameOld + ".wav"); } } catch (Exception e) { MessageBox.Show(e.ToString(), "The process move a audiofiles failed!"); } } }
private void waveIn_RecordingStopped(object sender, EventArgs e) { if (writeToFile == 1) { waveIn.Dispose(); waveIn = null; writer.Close(); writer = null; int bytes_to_read = (rec_times + 1) * 6400; byte[] wav_bytes = new byte[bytes_to_read]; WaveFileReader wfr = new WaveFileReader("file.wav"); if (wfr.Length < bytes_to_read) { wfr.Read(wav_bytes, 0, (int)wfr.Length); } else { wfr.Position = wfr.Length - 1 - bytes_to_read; wfr.Read(wav_bytes, 0, bytes_to_read); } wfr.Dispose(); wfr.Close(); WaveIn second_waveIn = new WaveIn(); second_waveIn.DeviceNumber = 0; second_waveIn.WaveFormat = new WaveFormat(16000, 2); WaveFileWriter second_writer = new WaveFileWriter("cmd.wav", second_waveIn.WaveFormat); second_waveIn.StartRecording(); second_writer.Write(wav_bytes, 0, bytes_to_read); second_waveIn.StopRecording(); second_waveIn.Dispose(); second_waveIn = null; second_writer.Close(); second_writer = null; listBox1.Items.Add("CONVERTING"); listBox1.SelectedIndex = listBox1.Items.Count - 1; Wav2Flac("cmd.wav", "file.flac"); result = GoogleSpeechRequest(16000); string res = result; int k = res.IndexOf("utterance\":\"") + "utterance\":\"".Length; int k1 = res.IndexOf("\"", k + 1); string cmd = res.Substring(k, k1 - k); listBox1.Items.Add("RECOGNIZED"); richTextBox1.Text += cmd + "\n"; File.Delete("cmd.wav"); rec_times = 0; writeToFile = 0; } else if (writeToFile == 0) { waveIn.Dispose(); waveIn = null; writer.Close(); writer = null; } }
//Microphone. //http://mark-dot-net.blogspot.com/2011/04/how-to-use-wavefilewriter.html public void recordInput() { Console.WriteLine("Now recording..."); waveSource = new WaveInEvent(); waveSource.WaveFormat = new WaveFormat(16000, 1); waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable); waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped); //string tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".wav"); string tempFile = Path.Combine(@"C:\Users\Nick\Desktop", "test.wav"); waveFile = new WaveFileWriter(tempFile, waveSource.WaveFormat); waveSource.StartRecording(); Thread.Sleep(3000); waveSource.StopRecording(); waveSource.Dispose(); waveSource = null; waveFile.Close(); Console.WriteLine("Finished record"); }
private static byte[] GetWaveDataStream(string mlvFileName) { MLVCachedReader cache = GetReader(mlvFileName); Stream mem = new MemoryStream(); MLVTypes.mlv_wavi_hdr_t header = cache.handler.WaviHeader; WaveFormat fmt = new WaveFormat((int)header.samplingRate, header.bitsPerSample, header.channels); cache.handler.WaveProvider = new BufferedWaveProvider(fmt); cache.handler.WaveProvider.BufferLength = 20 * 1024 * 1024; WaveFileWriter wr = new WaveFileWriter(mem, fmt); lock (cache) { foreach (var frame in cache.reader.AudfXrefList.Keys) { /* seek to the correct block */ cache.reader.CurrentBlockNumber = cache.reader.GetAudioFrameBlockNumber(frame); cache.handler.AudfHeader.blockSize = 0; cache.reader.ReadBlock(); /* now the VIDF should be read correctly */ if (cache.handler.AudfHeader.blockSize == 0) { throw new InvalidDataException("Requested audio frame " + frame + " but the index points us wrong"); } /* save into wave file stream */ byte[] data = new byte[cache.handler.WaveProvider.BufferedBytes]; cache.handler.WaveProvider.Read(data, 0, data.Length); wr.Write(data, 0, data.Length); } } mem.Seek(0, SeekOrigin.Begin); byte[] buffer = new byte[mem.Length]; mem.Read(buffer, 0, buffer.Length); mem.Close(); wr.Close(); return buffer; }
private byte[] Wav2Flac(MemoryStream waveStreamMemory) { if (waveStreamMemory.Length >= 3200) // 0 { Log.Write("Ева услышала Вас.", Log.Category.Information); byte[] Array = null; WaveFileWriter writer = new WaveFileWriter("C:\\rec_temp.wav", waveInEvent.WaveFormat); writer.Write(waveStreamMemory.ToArray(), 0, waveStreamMemory.ToArray().Length); writer.Close(); IAudioSource audioSource = new WAVReader("C:\\rec_temp.wav", null); AudioPCMConfig audioPCMConfig = new AudioPCMConfig(16, 1, 16000); AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); //AudioBuffer buffMemory = new AudioBuffer(audioPCMConfig, waveStreamMemory.ToArray(), Convert.ToInt32(waveStreamMemory.Length) / audioPCMConfig.BlockAlign); FlakeWriter flakewriter = new FlakeWriter(null, audioSource.PCM); FlakeWriter audioDest = flakewriter; while (audioSource.Read(buff, -1) != 0) { audioDest.Write(buff); } if (flakewriter.BufferMemory != null) { Array = flakewriter.BufferMemory.ToArray(); } audioDest.Close(); audioSource.Close(); return Array; } return null; }