protected override void FillBuffer(float[] buffer, int offset, int count) { if (Write && InputSignal.Value != null && FWriter != null) { var byteCount = count * 2; if (FByteBuffer.Length < byteCount) { FByteBuffer = new byte[byteCount]; } //read bytes from input FWave16Provider.Read(FByteBuffer, 0, byteCount); //write to stream FWriter.Write(FByteBuffer, 0, byteCount); SamplesWritten += count; FFlushCounter += count; if (FFlushCounter >= 32768) { FWriter.Flush(); FFlushCounter = 0; } FLastWriteState = true; } else { FFlushCounter = 0; if (FLastWriteState) { FWriter.Flush(); FLastWriteState = false; } } }
void sourceInStream_DataAvailable(object sender, WaveInEventArgs e) { if (waveWriter != null) { waveWriter.Write(e.Buffer, 0, e.BytesRecorded); waveWriter.Flush(); } }
private void sourceStream_DataAvailable(object sender, WaveInEventArgs e) { if (waveWriter == null) { return; } waveWriter.Write(e.Buffer, 0, e.BytesRecorded); waveWriter.Flush(); }
private void WaveInEvent_DataAvailable(object sender, WaveInEventArgs e) { if (wavWriter == null) { return; } #pragma warning disable CS0618 // Type or member is obsolete wavWriter.WriteData(e.Buffer, 0, e.BytesRecorded); #pragma warning restore CS0618 // Type or member is obsolete wavWriter.Flush(); }
void waveInSource_RecordingStopped(object sender, StoppedEventArgs e) { //throw new NotImplementedException(); //waveInSource.StopRecording(); //MessageBox.Show("aaaaaaaaaaaaaaaaaaaaaaa"); if (waveFileWriter != null) { waveFileWriter.Flush(); waveFileWriter.Dispose(); waveFileWriter = null; } }
/* * Using the NuGet package NAudio, it records one second of audio with a sampling rate based on the fft * It outputs the file into temp.wav for later use */ private void btnRecord_Click(object sender, EventArgs e) { int bytesRecorded = 0; btnRecord.Enabled = false; wi = new WaveIn(); wi.WaveFormat = new WaveFormat(fft.samplingRate * 5, 1); wi.RecordingStopped += new EventHandler <StoppedEventArgs>(wi_RecordingStopped); wi.DataAvailable += (s, a) => { if (wfw != null) { bytesRecorded += a.BytesRecorded; wfw.Write(a.Buffer, 0, a.BytesRecorded); wfw.Flush(); } if (bytesRecorded > fft.getDataSize() * 4) { wi.StopRecording(); } }; wfw = new WaveFileWriter("temp.wav", wi.WaveFormat); try { wi.StartRecording(); txtOutput.AppendText("Recording Started\r\n"); } catch (Exception ex) { txtOutput.AppendText("\r\n" + ex.Message); } }
public void FlushUpdatesHeaderEvenIfDisposeNotCalled() { var ms = new MemoryStream(); var testSequence = new byte[] { 0x1, 0x2, 0xFF, 0xFE }; var testSequence2 = new byte[] { 0x3, 0x4, 0x5 }; var writer = new WaveFileWriter(new IgnoreDisposeStream(ms), new WaveFormat(16000, 24, 1)); writer.Write(testSequence, 0, testSequence.Length); writer.Flush(); // BUT NOT DISPOSED // another write that was not flushed writer.Write(testSequence2, 0, testSequence2.Length); // check the Reader can read it ms.Position = 0; using (var reader = new WaveFileReader(ms)) { Assert.AreEqual(16000, reader.WaveFormat.SampleRate, "Sample Rate"); Assert.AreEqual(24, reader.WaveFormat.BitsPerSample, "Bits Per Sample"); Assert.AreEqual(1, reader.WaveFormat.Channels, "Channels"); Assert.AreEqual(testSequence.Length, reader.Length, "File Length"); var buffer = new byte[600]; // 24 bit audio, block align is 3 int read = reader.Read(buffer, 0, buffer.Length); Assert.AreEqual(testSequence.Length, read, "Data Length"); for (int n = 0; n < read; n++) { Assert.AreEqual(testSequence[n], buffer[n], "Byte " + n); } } writer.Dispose(); // to stop the finalizer from moaning }
void waveInSource_RecordingStopped(object sender, StoppedEventArgs e) { //throw new NotImplementedException(); //waveInSource.StopRecording(); //MessageBox.Show("aaaaaaaaaaaaaaaaaaaaaaa"); if (waveFileWriter != null) { waveFileWriter.Flush(); waveFileWriter.Dispose(); waveFileWriter = null; RecordFlagModel.Flag = true; //在这里交窗口进行关闭掉 this.Dispose(); this.Close(); } }
private void waveMaxSample(WaveInEventArgs e, Action <float> handler) { if (waveWriter != null) //Aliving waveWrter(filewriter), write all bytes in buffer { waveWriter.Write(e.Buffer, 0, e.BytesRecorded); waveWriter.Flush(); } float maxRecorded = 0.0f; for (int i = 0; i < e.BytesRecorded; i += 2) //loop for bytes { //convert to float short sample = (short)((e.Buffer[i + 1] << 8) | e.Buffer[i + 0]); var sample32 = sample / 32768f; if (sample32 < 0) { sample32 = -sample32; // alter to absolute value } if (sample32 > maxRecorded) { maxRecorded = sample32; //update maximum } } handler(maxRecorded); //pass the handle }
public override SyncAnnotation SynthesizeText(string text, WaveFileWriter writer, string src = "") { if (text == null) { throw new ArgumentNullException(nameof(text)); } var startOffset = writer.TotalTime; var audioStream = new MemoryStream(); Synthesizer.SetOutputToAudioStream( audioStream, new SpeechAudioFormatInfo( writer.WaveFormat.SampleRate, (AudioBitsPerSample)writer.WaveFormat.BitsPerSample, (AudioChannel)writer.WaveFormat.Channels)); Synthesizer.SelectVoice(Voice.Name); Synthesizer.Speak(text); Synthesizer.SetOutputToNull(); audioStream.WriteTo(writer); writer.Flush(); return(new SyncAnnotation() { ClipBegin = startOffset, ClipEnd = writer.TotalTime, Src = src }); }
static void Main(string[] args) { var option = new Dictionary <string, object>() { { "spd", 5 }, // 语速 { "vol", 5 }, // 音量 { "per", 0 } // 发音人,4:情感度丫丫童声 }; var text = "滴,滴,答,10,9,8,7,6,5,4,3,2,1"; WaveFileWriter writer = null; Stream output = null; foreach (var s in text.Split(',')) { var result = _ttsClient.Synthesis(s, option); if (!result.Success) { Console.WriteLine(result.ErrorMsg); } var ret = WriteSpeech(result.Data, 1, ref writer); if (ret != null) { output = ret; } } writer?.Flush(); output?.Seek(0, SeekOrigin.Begin); using (var fs = new FileStream("test.wav", FileMode.Create)) { output?.CopyTo(fs); fs.Close(); } writer?.Close(); Console.ReadLine(); }
private void mainWaveIn_DataAvailable(object sender, WaveInEventArgs e) { if (recWaveWriter == null) { return; } recWaveWriter.Write(e.Buffer, 0, e.BytesRecorded); recWaveWriter.Flush(); float max = 0; var buffer = new WaveBuffer(e.Buffer); // interpret as 32 bit floating point audio for (int index = 0; index < e.BytesRecorded / 4; index++) { var sample = buffer.FloatBuffer[index]; // absolute value if (sample < 0) { sample = -sample; } // is this the max value? if (sample > max) { max = sample; } } volumeSliderMeter.Volume = max; }
public WavCharactersProvider(IConfiguration configuration) { var speechConfig = configuration.GetSection("SpeechConfig").Get <SpeechConfig>(); charactersWavBytes = new Dictionary <char, byte[]>(); var characters = new[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; byte[] readBuffer = new byte[16 * 1024]; foreach (var character in characters) { //FileNotFoundException can be thrown using var waveFileReader = new WaveFileReader(Path.Combine(speechConfig.CharactersPath, $"{character}.wav")); using var ms = new MemoryStream(); using var waveFileWriter = new WaveFileWriter(ms, waveFileReader.WaveFormat); int read; while ((read = waveFileReader.Read(readBuffer, 0, readBuffer.Length)) > 0) { waveFileWriter.Write(readBuffer, 0, read); } waveFileWriter.Flush(); charactersWavBytes[character] = ms.ToArray(); } }
private static byte[] MP3BytesToWavBytes(byte[] input, WaveFormat outputFormat = null) { byte[] retval = null; using (MemoryStream outputStream = new MemoryStream()) using (MemoryStream inputStream = new MemoryStream(input)) using (WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(inputStream))) { if (outputFormat == null) { outputFormat = waveStream.WaveFormat; } using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, outputFormat)) { byte[] bytes = new byte[waveStream.Length]; waveStream.Position = 0; waveStream.Read(bytes, 0, (int)waveStream.Length); waveFileWriter.Write(bytes, 0, bytes.Length); waveFileWriter.Flush(); outputStream.Position = 0; retval = outputStream.ToArray(); } } return(retval); }
private void InitWaveFileWriter(int rate, int bits, int channels) { //Init Wave File Writer fs = File.Open(this._OutputLocation, FileMode.OpenOrCreate, FileAccess.Write); WaveFileOut = new WaveFileWriter(fs, new WaveFormat(rate, bits, channels)); WaveFileOut.Flush(); }
/// <summary> /// Finish recording and close WAV file. /// </summary> public void StopRecording() { if (!_isRecording) { return; } if (_captureInput != null) { _captureInput.DataAvailable -= _OnDataAvailable; _captureInput.StopRecording(); GC.SuppressFinalize(_captureInput); _captureInput.Dispose(); _captureInput = null; } if (_waveWriter != null) { _waveWriter.Flush(); _waveWriter.Close(); _waveWriter.Dispose(); GC.SuppressFinalize(_waveWriter); Debug.WriteLine("Recording finished.\n\tFile=" + _tempWavFilePath + "\n"); _waveWriter = null; } _isRecording = false; _state = RecorderState.RecordingFinsihed; Debug.WriteLine("Recording of " + _tempWavFilePath + " stopped"); }
WAV ConvertMP3DataToWAV(byte[] mp3Data) { using (MemoryStream mp3Stream = new MemoryStream(mp3Data)) { using (Mp3FileReader mp3Audio = new Mp3FileReader(mp3Stream)) { using (WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(mp3Audio)) { using (MemoryStream outputStream = new MemoryStream()) { using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat)) { byte[] bytes = new byte[waveStream.Length]; waveStream.Position = 0; waveStream.Read(bytes, 0, Convert.ToInt32(waveStream.Length)); waveFileWriter.Write(bytes, 0, bytes.Length); waveFileWriter.Flush(); } return(new WAV(outputStream.ToArray())); } } } } }
public static void OnDataAvailable(object sender, WaveInEventArgs e) { if (Recording && WaveFile != null) { WaveFile.Write(e.Buffer, 0, e.BytesRecorded); WaveFile.Flush(); } else { float max = 0; // interpret as 16 bit audio for (int index = 0; index < e.BytesRecorded; index += 2) { short sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]); // to floating point var sample32 = sample / 32768f; // absolute value if (sample32 < 0) { sample32 = -sample32; } // is this the max value? if (sample32 > max) { max = sample32; } CurrentInputLevel = max * 100; EventSystem.Publish(new AudioUpdated() { RecordingLevel = CurrentInputLevel }); } } }
private void Start() { var deviceIdx = DeviceComboBox.SelectedIndex; if (int.TryParse(SampleRateTextBox.Text, out var sampleRate) && deviceIdx >= 0 && deviceIdx < WaveIn.DeviceCount) { memoryStream = new MemoryStream(); waveIn = new WaveIn { DeviceNumber = deviceIdx, WaveFormat = new WaveFormat(sampleRate, 16, WaveIn.GetCapabilities(deviceIdx).Channels) }; memoryStream = new MemoryStream(); waveFileWriter = new WaveFileWriter(memoryStream, waveIn.WaveFormat); waveIn.DataAvailable += (sender, args) => { waveFileWriter.Write(args.Buffer, 0, args.BytesRecorded); waveFileWriter.Flush(); }; waveIn.StartRecording(); } }
/// <summary> /// This method uses NAudio to create .wav file on disk using a given AudioClip. /// </summary> /// <param name="filePath">Path with filename</param> /// <param name="audio">AudioClip to export</param> /// <returns>Returns if the AudioClip could was written to a file on disk.</returns> public bool TryWriteAudioClipToFile(AudioClip audio, string filePath) { try { WaveFormat format = new WaveFormat(audio.frequency, audio.channels); using (WaveFileWriter writer = new WaveFileWriter(File.Create(filePath), format)) { float[] buffer = new float[audio.samples]; if (audio.GetData(buffer, 0)) { writer.WriteSamples(buffer, 0, audio.samples); writer.Flush(); return(true); } } } catch (UnauthorizedAccessException ex) { Debug.LogErrorFormat("Could not write to disk, not authorized\n{0}", ex.Message); } catch (Exception ex) { Debug.LogErrorFormat("Unknown exception occurred: '{0}'", ex.Message); } return(false); }
/// <summary> /// スピーカーへ出力する処理 /// </summary> /// <param name="provider"></param> async Task SendVoice(BufferedWaveProvider provider) { while (play) //再生可能な間ずっとループ処理 { try { buffer.TryDequeue(out WP[] wp); // 分析結果を取り出す if (wp == null) { // 再生するものが無ければ少し待つ await Task.Delay(20); continue; } converter.ParameterToSignal.AddParameter(wp); converter.ParameterToSignal.Synthsize(); // 音声信号の合成(この行をコメントアウトしても、同じ処理が次の行のReadSignalで実行されます) float[] signal = converter.ParameterToSignal.ReadSignal(); byte[] Bbuffer = FloatToByte(signal); provider.AddSamples(Bbuffer, 0, Bbuffer.Length); // providerに合成結果を流し込む // 音声をファイル(スピーカー.wav)に保存 waveWriter_Out.Write(Bbuffer, 0, Bbuffer.Length); waveWriter_Out.Flush(); } catch (Exception e) { ErrorLog("音声合成時に失敗しました。", e); } } }
/// <summary> /// マイク入力に対する処理 /// </summary> /// <param name="ee">ここに音声信号が入ってくる</param> private void ReceiveVoice(object obj, WaveInEventArgs ee) { try { // 音声をファイル(マイク.wav)に保存 waveWriter_In.Write(ee.Buffer, 0, ee.Buffer.Length); waveWriter_In.Flush(); float[] signal = ByteToFloat(ee.Buffer); // マイクからの音声信号 converter.SignalToParameter.AddSignal(signal); converter.SignalToParameter.Analyze(); // 音声信号の分析(この行をコメントアウトしても、同じ処理が次の行のReadParameterで実行されます) WP[] wp = converter.SignalToParameter.ReadParameter(); // 分析結果の取得 // 音声の加工 ピッチとフォルマントの変更 for (int i = 0; i < wp.Length; i++) { wp[i].f0 *= prate; float[] temp = new float[wp[i].spectrogram.Length]; for (int j = 0; j < wp[i].spectrogram.Length; j++) { temp[j] = wp[i].spectrogram[(int)Math.Min(j / srate, wp[i].spectrogram.Length - 1)]; } wp[i].spectrogram = temp; } buffer.Enqueue(wp); // 分析結果(加工済み)をバッファに入れておく SendVoiceで処理される } catch (Exception e) { ErrorLog("音声分析時に失敗しました。", e); } }
public Task Process(string[] lines, string output) { void TaskRun() { if (_waveFormat == null) { _logger.Error("Need init."); return; } using (var stream = new MemoryStream()) using (var writer = new WaveFileWriter(stream, _waveFormat)) { var lrc = new StringBuilder(); var span = TimeSpan.Zero; foreach (var line in lines) { var items = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (items.Length < 1) { _logger.Warn($"无效的行: {line}"); continue; } var second = 0; string speech; if (items.Length > 1) { second = int.Parse(items[1]); speech = $"{items[0]},{second}秒,Go"; } else { speech = items[0]; } lrc.AppendLine($"[{span:mm\\:ss}.00]{speech}"); second = ProcessLine(speech, second, writer); span += TimeSpan.FromSeconds(second); } File.WriteAllText(Path.ChangeExtension(output, ".lrc"), lrc.ToString()); writer.Flush(); if (Path.GetExtension(output).ToLower() == ".mp3") { stream.Seek(0, SeekOrigin.Begin); using (var mp3 = new LameMP3FileWriter(output, _waveFormat, LAMEPreset.ABR_128)) { stream.CopyTo(mp3); } } else { File.WriteAllBytes(output, stream.ToArray()); } _logger.Debug("生成完成."); } } return(Task.Run(TaskRun)); }
private void InitSound() { var deviceIndex = 0; //Device Info if (WaveIn.DeviceCount > 0) { var deviceInfo = WaveIn.GetCapabilities(0); this.label1.Text = String.Format("Device {0}: {1}, {2} channels", deviceIndex, deviceInfo.ProductName, deviceInfo.Channels); _wi = new WaveIn() { DeviceNumber = deviceIndex }; //mike receive data _wi.DataAvailable += (ss, ee) => { for (int index = 0; index < ee.BytesRecorded; index += 2) { short sample = (short)((ee.Buffer[index + 1] << 8) | ee.Buffer[index + 0]); float sample32 = 0; if (sample > 0) { sample32 = sample / 32767f; } if (sample < 0) { sample32 = sample / 32768f; } _recorded1.Add(sample32); if (_recorded1.Count == _takeDataSize) { _p1.UpdatePlot(_recorded1); _p2.UpdateFFTPlot(_recorded1, _sampleRate); _recorded1.Clear(); } //Analyze Do here //ee.Buffer[index + 1] = AnalyzedData; //ee.Buffer[index + 0] = AnalyzedData; } _writer.Write(ee.Buffer, 0, ee.BytesRecorded); _writer.Flush(); }; _wi.WaveFormat = new WaveFormat(sampleRate: _sampleRate, channels: deviceInfo.Channels); //save file var sPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\file.wav"; _writer = new WaveFileWriter(sPath, _wi.WaveFormat); } else { RecordButton.Enabled = false; } }
/// <summary> /// Merge two mp3 files and put the merged content in the specified path. /// </summary> /// <param name="fileName1"></param> /// <param name="fileName2"></param> /// <param name="outputDirectory"></param> public static void Mp3Merge(string fileName1, string fileName2, string outputDirectory) { var fileA = new AudioFileReader(fileName1); // Calculate our buffer size, since we're normalizing our samples to floats // we'll need to account for that by dividing the file's audio byte count // by its bit depth / 8. var bufferA = new float[fileA.Length / (fileA.WaveFormat.BitsPerSample / 8)]; // Now let's populate our buffer with samples. fileA.Read(bufferA, 0, bufferA.Length); // Do it all over again for the other file. var fileB = new AudioFileReader(fileName2); var bufferB = new float[fileB.Length / (fileB.WaveFormat.BitsPerSample / 8)]; fileB.Read(bufferB, 0, bufferB.Length); // Calculate the largest file var maxLen = (long)Math.Max(bufferA.Length, bufferB.Length); var final = new byte[maxLen]; // For now, mix data to a wav file. using (MemoryStream ms = new MemoryStream()) { var writer = new WaveFileWriter(ms, fileA.WaveFormat); for (var i = 0; i < maxLen; i++) { float a, b; if (i < bufferA.Length) { // Reduce the amplitude of the sample by 2 // to avoid clipping. a = bufferA[i];// / 2; } else { a = 0; } if (i < bufferB.Length) { b = bufferB[i];// / 2; } else { b = 0; } writer.WriteSample(a + b); writer.Flush(); } ms.Seek(0, SeekOrigin.Begin); final = ms.ToArray(); } ConvertWavStreamToMp3File(outputDirectory, final); MessageBox.Show("Finished"); }
//Event, writes data from buffer private static void waveSource_DataAvailable(object sender, WaveInEventArgs e) { if (_waveFile != null) { _waveFile.Write(e.Buffer, 0, e.BytesRecorded); _waveFile.Flush(); } }
private void DataAvailable(object sender, WaveInEventArgs e) { if (_waveWriter != null) { _waveWriter.Write(e.Buffer, 0, e.BytesRecorded); _waveWriter.Flush(); } }
private void WaveIn_DataAvailable(object sender, WaveInEventArgs e) { if (waveFile != null) { waveFile.Write(e.Buffer, 0, e.BytesRecorded); waveFile.Flush(); } }
private void data(object s, WaveInEventArgs e) { if (writer != null) // se a stream de escrita estiver instanciada { writer.Write(e.Buffer, 0, e.Buffer.Length); // escrever os bytes writer.Flush(); } }
public static void writeWav(string path, WaveFormat format, float[] samples) { using (WaveFileWriter writer = new WaveFileWriter(path, format)) { writer.WriteSamples(samples, 0, samples.Length); writer.Flush(); } }