private void CompareRecordedData() { if (mState != State.RecCompleted) { textBoxLog.Text += Properties.Resources.msgCompareCaptureTooSmall; textBoxLog.ScrollToEnd(); return; } textBoxLog.Text += Properties.Resources.msgCompareStarted; mPcmRecorded = new PcmDataLib.PcmData(); mPcmRecorded.SetFormat(NUM_CHANNELS, WasapiCS.SampleFormatTypeToUseBitsPerSample(mRecSampleFormat), WasapiCS.SampleFormatTypeToValidBitsPerSample(mRecSampleFormat), mSampleRate, PcmDataLib.PcmData.ValueRepresentationType.SInt, mCapturedBytes / NUM_CHANNELS / (WasapiCS.SampleFormatTypeToUseBitsPerSample(mRecSampleFormat) / 8)); mPcmRecorded.SetSampleLargeArray(mCapturedPcmData); // 開始合図位置compareStartFrameをサーチ long compareStartFrame = -1; switch (mRecSampleFormat) { case WasapiCS.SampleFormatType.Sint16: for (long pos = 0; pos < mPcmRecorded.NumFrames; ++pos) { if (0x00030000 == mPcmRecorded.GetSampleValueInInt32(0, pos)) { compareStartFrame = pos; break; } } break; case WasapiCS.SampleFormatType.Sint24: case WasapiCS.SampleFormatType.Sint32V24: for (long pos = 0; pos < mPcmRecorded.NumFrames; ++pos) { if (0x00000300 == mPcmRecorded.GetSampleValueInInt32(0, pos)) { compareStartFrame = pos; break; } } break; default: System.Diagnostics.Debug.Assert(false); break; } if (compareStartFrame < 0) { textBoxLog.Text += Properties.Resources.msgCompareStartNotFound; textBoxLog.ScrollToEnd(); return; } compareStartFrame += mPcmReady.NumFrames; if (mPcmRecorded.NumFrames - compareStartFrame < mNumTestFrames) { textBoxLog.Text += Properties.Resources.msgCompareCaptureTooSmall; textBoxLog.ScrollToEnd(); return; } // 送信データmPcmTestと受信データmPcmRecordedを比較 long numTestBytes = mNumTestFrames * NUM_CHANNELS * (WasapiCS.SampleFormatTypeToValidBitsPerSample(mRecSampleFormat) / 8); long differentBytes = 0; for (long pos = 0; pos < mNumTestFrames; ++pos) { for (int ch = 0; ch < NUM_CHANNELS; ++ch) { if (mPcmTest.GetSampleValueInInt32(ch, pos) != mPcmRecorded.GetSampleValueInInt32(ch, pos + compareStartFrame)) { ++differentBytes; } } } if (0 < differentBytes) { textBoxLog.Text += string.Format(Properties.Resources.msgCompareDifferent, (double)numTestBytes / 1024.0 / 1024.0, (double)numTestBytes * 8L * 0.001 * 0.001, (double)mNumTestFrames / mSampleRate, differentBytes, mWasapiRec.GetCaptureGlitchCount()); textBoxLog.ScrollToEnd(); return; } else { textBoxLog.Text += string.Format(Properties.Resources.msgCompareIdentical, (double)numTestBytes / 1024.0 / 1024.0, (double)numTestBytes * 8L * 0.001 * 0.001, (double)mNumTestFrames / mSampleRate); textBoxLog.ScrollToEnd(); } mWasapiRec.ResetCaptureGlitchCount(); }
public long GetCaptureGlitchCount() { return(mWasapi.GetCaptureGlitchCount()); }
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); }