private bool WriteWavFile(WavData wavData, string path) { bool rv = true; try { using (BinaryWriter bw1 = new BinaryWriter(File.Open(path, FileMode.Create))) { wavData.Write(bw1); } } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); rv = false; } return(rv); }
private void SaveRecordedData() { var bytes = wasapi.GetCapturedData(mCapturedPcmData); var nFrames = bytes / WasapiCS.SampleFormatTypeToUseBytesPerSample(mSampleFormat) / mNumChannels; if (nFrames == 0) { return; } textBoxLog.Text += string.Format("captured frames={0} ({1:F1} seconds) glichCount={2}\r\n", nFrames, (double)nFrames / mSamplingFrequency, wasapi.GetCaptureGlitchCount()); Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.DefaultExt = ".wav"; dlg.Filter = "WAVEファイル|*.wav"; Nullable <bool> result = dlg.ShowDialog(); if (result != true) { return; } // あとで本のサイズに戻す。 var originalSize = mCapturedPcmData.Length; Array.Resize(ref mCapturedPcmData, (int)bytes); mWavData = new WavData(); mWavData.Set(mNumChannels, WasapiCS.SampleFormatTypeToUseBytesPerSample(mSampleFormat) * 8, WasapiCS.SampleFormatTypeToValidBitsPerSample(mSampleFormat), mSamplingFrequency, SampleFormatToVRT(mSampleFormat), nFrames, mCapturedPcmData); try { using (BinaryWriter w = new BinaryWriter(File.Open(dlg.FileName, FileMode.Create))) { mWavData.Write(w); textBoxLog.Text += string.Format("ファイル保存成功: {0}\r\n", dlg.FileName); } } catch (Exception ex) { string s = string.Format("E: ファイル保存失敗: {0}\r\n{1}\r\n", dlg.FileName, ex); textBoxLog.Text += s; MessageBox.Show(s); } slider1.Value = 0; label1.Content = "0/0"; Array.Resize(ref mCapturedPcmData, originalSize); }
private bool SaveWav(int sampleRate, int bitsPerSample, List <PcmSamples1Channel> samples, string path) { bool ret = true; WavData wav = new WavData(); wav.Create(sampleRate, bitsPerSample, samples); try { using (BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.CreateNew))) { wav.Write(bw); } } catch (Exception ex) { resultString = ex.ToString(); ret = false; } return(ret); }
static void Main(string[] args) { OptionInfo optionInfo = new OptionInfo(2); ParseOptions(ref optionInfo, args); WavData ch0Wav = ReadWavFromFile(optionInfo.channels[0].chFileName); WavData ch1Wav = ReadWavFromFile(optionInfo.channels[1].chFileName); if (ch0Wav.NumSamples != ch1Wav.NumSamples) { Console.WriteLine("E: NumSamples mismatch. ch0.numSamples={0}, ch1.numSamples={1}", ch0Wav.NumSamples, ch1Wav.NumSamples); PrintUsageAndExit(); } WavData writeWav = new WavData(); List <PcmSamples1Channel> channels = new List <PcmSamples1Channel>(); for (int i = 0; i < 2; ++i) { channels.Add(new PcmSamples1Channel(ch0Wav.NumSamples, ch0Wav.BitsPerSample)); } int ch0UseCh = optionInfo.channels[0].chFileUseCh; int ch1UseCh = optionInfo.channels[1].chFileUseCh; for (int i = 0; i < ch0Wav.NumSamples; ++i) { channels[0].Set16(i, ch0Wav.Sample16Get(ch0UseCh, i)); channels[1].Set16(i, ch1Wav.Sample16Get(ch1UseCh, i)); } writeWav.Create(ch0Wav.SampleRate, ch0Wav.BitsPerSample, channels); try { using (BinaryWriter bw = new BinaryWriter(File.Open(optionInfo.outputWavFileName, FileMode.CreateNew))) { writeWav.Write(bw); } } catch (Exception ex) { Console.WriteLine("E: {0}", ex); PrintUsageAndExit(); } }
private void DoWork(object o, DoWorkEventArgs args) { Console.WriteLine("DoWork started\n"); int count = 0; while (!asio.Run()) { ++count; Console.WriteLine("\nForm1.DoWork() count={0} m_seconds={1}", count, m_seconds); int percent = 100 * count / m_seconds; if (100 < percent) { percent = 100; } } int[] recordedData = asio.RecordedDataGet(m_inputChannelNum, m_seconds * SAMPLE_RATE); PcmSamples1Channel ch0 = new PcmSamples1Channel(m_seconds * SAMPLE_RATE, 16); int max = 0; int min = 0; for (int i = 0; i < recordedData.Length; ++i) { if (max < recordedData[i]) { max = recordedData[i]; } if (recordedData[i] < min) { min = recordedData[i]; } } Console.WriteLine("max={0} min={1}", max, min); if (max < -min) { max = -min; } double mag = 32767.0 / max; Console.WriteLine("mag={0}", mag); for (int i = 0; i < recordedData.Length; ++i) { ch0.Set16(i, (short)(recordedData[i] * mag)); } List<PcmSamples1Channel> chList = new List<PcmSamples1Channel>(); chList.Add(ch0); WavData wd = new WavData(); wd.Create(SAMPLE_RATE, 16, chList); using (BinaryWriter bw = new BinaryWriter(File.Open(m_writeFilePath, FileMode.Create))) { wd.Write(bw); } args.Result = 0; Console.WriteLine("DoWork end\n"); }
private void DoWork(object o, DoWorkEventArgs args) { Console.WriteLine("DoWork started\n"); int count = 0; while (!asio.Run()) { ++count; Console.WriteLine("\nForm1.DoWork() count={0} m_seconds={1}", count, m_seconds); int percent = 100 * count / m_seconds; if (100 < percent) { percent = 100; } } int[] recordedData = asio.RecordedDataGet(m_inputChannelNum, m_seconds * SAMPLE_RATE); PcmSamples1Channel ch0 = new PcmSamples1Channel(m_seconds * SAMPLE_RATE, 16); int max = 0; int min = 0; for (int i = 0; i < recordedData.Length; ++i) { if (max < recordedData[i]) { max = recordedData[i]; } if (recordedData[i] < min) { min = recordedData[i]; } } Console.WriteLine("max={0} min={1}", max, min); if (max < -min) { max = -min; } double mag = 32767.0 / max; Console.WriteLine("mag={0}", mag); for (int i = 0; i < recordedData.Length; ++i) { ch0.Set16(i, (short)(recordedData[i] * mag)); } List <PcmSamples1Channel> chList = new List <PcmSamples1Channel>(); chList.Add(ch0); WavData wd = new WavData(); wd.Create(SAMPLE_RATE, 16, chList); using (BinaryWriter bw = new BinaryWriter(File.Open(m_writeFilePath, FileMode.Create))) { wd.Write(bw); } args.Result = 0; Console.WriteLine("DoWork end\n"); }
private bool WriteWavFile(WavData wavData, string path) { Console.WriteLine(rm.GetString("WriteWavFileStarted"), path); bool rv = true; try { using (BinaryWriter bw1 = new BinaryWriter(File.Open(path, FileMode.Create))) { wavData.Write(bw1); } } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); rv = false; } return rv; }
private void SaveRecordedData() { var bytes = wasapi.GetCapturedData(mCapturedPcmData); var nFrames = bytes / WasapiCS.SampleFormatTypeToUseBytesPerSample(mSampleFormat) / mNumChannels; if (nFrames == 0) { return; } textBoxLog.Text += string.Format("captured frames={0} ({1:F1} seconds) glichCount={2}\r\n", nFrames, (double)nFrames / mSamplingFrequency, wasapi.GetCaptureGlitchCount()); Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.DefaultExt = ".wav"; dlg.Filter = "WAVEファイル|*.wav"; Nullable<bool> result = dlg.ShowDialog(); if (result != true) { return; } // あとで本のサイズに戻す。 var originalSize = mCapturedPcmData.Length; Array.Resize(ref mCapturedPcmData, (int)bytes); mWavData = new WavData(); mWavData.Set(mNumChannels, WasapiCS.SampleFormatTypeToUseBytesPerSample(mSampleFormat) * 8, WasapiCS.SampleFormatTypeToValidBitsPerSample(mSampleFormat), mSamplingFrequency, SampleFormatToVRT(mSampleFormat), nFrames, mCapturedPcmData); try { using (BinaryWriter w = new BinaryWriter(File.Open(dlg.FileName, FileMode.Create))) { mWavData.Write(w); textBoxLog.Text += string.Format("ファイル保存成功: {0}\r\n", dlg.FileName); } } catch (Exception ex) { string s = string.Format("E: ファイル保存失敗: {0}\r\n{1}\r\n", dlg.FileName, ex); textBoxLog.Text += s; MessageBox.Show(s); } slider1.Value = 0; label1.Content = "0/0"; Array.Resize(ref mCapturedPcmData, originalSize); }