/// <returns>負の場合WASAPIエラーコード。成功の場合0。</returns> public int PlaylistCreateStart(int deviceId, ContentList.AudioFile af) { int ercd = 0; mWasapi.Stop(); mWasapi.Unsetup(); ChangeState(State.Loading); mWasapi.ClearPlayList(); // 最初に再生する曲 af ercd = Setup(deviceId, af.Pcm); if (ercd < 0) { Console.WriteLine("E: PlaybackController::Play({0}) failed {1:X8}", deviceId, ercd); ChangeState(State.Stopped); return(ercd); } mWasapi.AddPlayPcmDataStart(); return(0); }
private void BwStartTesting_DoWork(object sender, DoWorkEventArgs e) { //Console.WriteLine("BwStartTesting_DoWork()"); var args = e.Argument as StartTestingArgs; var r = new StartTestingResult(); r.result = false; r.text = "StartTesting failed!\n"; e.Result = r; PreparePcmData(args); System.GC.Collect(); lock (mLock) { int hr = 0; // 録音 mCapturedBytes = 0; mReceivedBytes = 0; mLevelMeter = new LevelMeter(mPref.RecSampleFormat, args.numChannels, mPref.PeakHoldSeconds, mPref.RecWasapiBufferSizeMS * 0.001, mPref.ReleaseTimeDbPerSec); hr = mWasapiRec.Setup(mRecDeviceIdx, WasapiCS.DeviceType.Rec, WasapiCS.StreamType.PCM, mPref.SampleRate, mPref.RecSampleFormat, args.numChannels, args.recDwChannelMask, WasapiCS.MMCSSCallType.Enable, WasapiCS.MMThreadPriorityType.None, WasapiCS.SchedulerTaskType.ProAudio, WasapiCS.ShareMode.Exclusive, mPref.RecDataFeedMode, mPref.RecWasapiBufferSizeMS, ZERO_FLUSH_MILLISEC, TIME_PERIOD, true); if (hr < 0) { r.result = false; r.text = string.Format(Properties.Resources.msgRecSetupError, mPref.SampleRate, mPref.RecSampleFormat, args.numChannels, mPref.RecDataFeedMode, mPref.RecWasapiBufferSizeMS, WasapiCS.GetErrorMessage(hr)) + "\n"; e.Result = r; StopUnsetup(); return; } // 再生 hr = mWasapiPlay.Setup(mPlayDeviceIdx, WasapiCS.DeviceType.Play, WasapiCS.StreamType.PCM, mPref.SampleRate, mPref.PlaySampleFormat, args.numChannels, args.playDwChannelMask, WasapiCS.MMCSSCallType.Enable, WasapiCS.MMThreadPriorityType.None, WasapiCS.SchedulerTaskType.ProAudio, WasapiCS.ShareMode.Exclusive, mPref.PlayDataFeedMode, mPref.PlayWasapiBufferSizeMS, ZERO_FLUSH_MILLISEC, TIME_PERIOD, true); if (hr < 0) { mWasapiPlay.Unsetup(); r.result = false; r.text = string.Format(Properties.Resources.msgPlaySetupError, mPref.SampleRate, mPref.PlaySampleFormat, args.numChannels, mPref.PlayDataFeedMode, mPref.PlayWasapiBufferSizeMS) + "\n"; e.Result = r; return; } var ss = mWasapiPlay.GetSessionStatus(); mWasapiPlay.ClearPlayList(); mWasapiPlay.AddPlayPcmDataStart(); mWasapiPlay.AddPlayPcmData(0, mPcmPlay.GetSampleLargeArray()); mWasapiPlay.AddPlayPcmDataEnd(); mWasapiPlay.SetPlayRepeat(true); var playAttr = mWasapiPlay.GetDeviceAttributes(mPlayDeviceIdx); var recAttr = mWasapiRec.GetDeviceAttributes(mRecDeviceIdx); r.result = true; r.text = string.Format(Properties.Resources.msgTestStarted, mPref.SampleRate, mPcmPlay.NumFrames / mPref.SampleRate, mPcmPlay.NumFrames * 0.001 * 0.001); r.text += string.Format(Properties.Resources.msgPlaySettings, mPref.PlaySampleFormat, mPref.PlayWasapiBufferSizeMS, mPref.PlayDataFeedMode, playAttr.Name); r.text += string.Format(Properties.Resources.msgRecSettings, mPref.RecSampleFormat, mPref.RecWasapiBufferSizeMS, mPref.RecDataFeedMode, recAttr.Name); e.Result = r; } }
private void BwStartTesting_DoWork(object sender, DoWorkEventArgs e) { //Console.WriteLine("BwStartTesting_DoWork()"); var r = new StartTestingResult(); r.result = false; r.text = "StartTesting failed!\n"; e.Result = r; PreparePcmData(); System.GC.Collect(); lock (mLock) { int hr = 0; int playDwChannelMask = WasapiCS.GetTypicalChannelMask(NUM_CHANNELS); hr = mWasapiPlay.Setup(mPlayDeviceIdx, WasapiCS.DeviceType.Play, WasapiCS.StreamType.PCM, mSampleRate, mPlaySampleFormat, NUM_CHANNELS, playDwChannelMask, WasapiCS.MMCSSCallType.Enable, WasapiCS.MMThreadPriorityType.None, WasapiCS.SchedulerTaskType.ProAudio, WasapiCS.ShareMode.Exclusive, mPlayDataFeedMode, mPlayBufferMillisec, ZERO_FLUSH_MILLISEC, TIME_PERIOD, true); if (hr < 0) { mWasapiPlay.Unsetup(); r.result = false; r.text = string.Format(Properties.Resources.msgPlaySetupError, mSampleRate, mPlaySampleFormat, NUM_CHANNELS, mPlayDataFeedMode, mPlayBufferMillisec); e.Result = r; return; } var ss = mWasapiPlay.GetSessionStatus(); /* * { * var data = mPcmSync.GetSampleLargeArray(); * var trimmed = new LargeArray<byte>(ss.EndpointBufferFrameNum * mPcmSync.BitsPerFrame / 8); * trimmed.CopyFrom(data, 0, 0, trimmed.LongLength); * mPcmSync.SetSampleLargeArray(ss.EndpointBufferFrameNum, trimmed); * } * { * var data = mPcmReady.GetSampleLargeArray(); * var trimmed = new LargeArray<byte>(ss.EndpointBufferFrameNum * mPcmReady.BitsPerFrame / 8); * trimmed.CopyFrom(data, 0, 0, trimmed.LongLength); * mPcmReady.SetSampleLargeArray(ss.EndpointBufferFrameNum, trimmed); * } */ mWasapiPlay.ClearPlayList(); mWasapiPlay.AddPlayPcmDataStart(); mWasapiPlay.AddPlayPcmData((int)AudioContentType.SYNC, mPcmSync.GetSampleLargeArray()); mWasapiPlay.AddPlayPcmData((int)AudioContentType.READY, mPcmReady.GetSampleLargeArray()); mWasapiPlay.AddPlayPcmData((int)AudioContentType.TEST, mPcmTest.GetSampleLargeArray()); mWasapiPlay.AddPlayPcmDataEnd(); mWasapiPlay.SetPlayRepeat(false); // SYNCを連続再生する。 mWasapiPlay.ConnectPcmDataNext((int)AudioContentType.SYNC, (int)AudioContentType.SYNC); // READYの再生が終わったらTESTの再生が始まり、TESTの再生が終わったら再生が終了する。 // 録音 mCapturedBytes = 0; hr = mWasapiRec.Setup(mRecDeviceIdx, WasapiCS.DeviceType.Rec, WasapiCS.StreamType.PCM, mSampleRate, mRecSampleFormat, NUM_CHANNELS, mRecDwChannelMask, WasapiCS.MMCSSCallType.Enable, WasapiCS.MMThreadPriorityType.None, WasapiCS.SchedulerTaskType.ProAudio, WasapiCS.ShareMode.Exclusive, mRecDataFeedMode, mRecBufferMillisec, ZERO_FLUSH_MILLISEC, TIME_PERIOD, true); if (hr < 0) { r.result = false; r.text = string.Format(Properties.Resources.msgRecSetupError, mSampleRate, mRecSampleFormat, NUM_CHANNELS, mRecDataFeedMode, mRecBufferMillisec); e.Result = r; StopUnsetup(); return; } var playAttr = mWasapiPlay.GetDeviceAttributes(mPlayDeviceIdx); var recAttr = mWasapiRec.GetDeviceAttributes(mRecDeviceIdx); r.result = true; r.text = string.Format(Properties.Resources.msgTestStarted, mSampleRate, mNumTestFrames / mSampleRate, mNumTestFrames * 0.001 * 0.001); r.text += string.Format(Properties.Resources.msgPlaySettings, mPlaySampleFormat, mPlayBufferMillisec, mPlayDataFeedMode, playAttr.Name); r.text += string.Format(Properties.Resources.msgRecSettings, mRecSampleFormat, mRecBufferMillisec, mRecDataFeedMode, recAttr.Name); e.Result = r; } }
//========================================================================================================= private void buttonStart_Click(object sender, RoutedEventArgs e) { if (!UpdateTestParamsFromUI()) { return; } PreparePcmData(); lock (mLock) { int hr = 0; hr = mWasapiPlay.Setup(listBoxPlayDevices.SelectedIndex, WasapiCS.DeviceType.Play, WasapiCS.StreamType.PCM, mSampleRate, mPlaySampleFormat, NUM_CHANNELS, WasapiCS.MMCSSCallType.Enable, WasapiCS.SchedulerTaskType.ProAudio, WasapiCS.ShareMode.Exclusive, mPlayDataFeedMode, mPlayBufferMillisec, 1000, 10000); if (hr < 0) { MessageBox.Show(string.Format(Properties.Resources.msgPlaySetupError, mSampleRate, mPlaySampleFormat, NUM_CHANNELS, mPlayDataFeedMode, mPlayBufferMillisec)); mWasapiPlay.Unsetup(); return; } var ss = mWasapiPlay.GetSessionStatus(); { var data = mPcmSync.GetSampleArray(); var trimmed = new byte[ss.EndpointBufferFrameNum * mPcmSync.BitsPerFrame / 8]; Array.Copy(data, trimmed, trimmed.Length); mPcmSync.SetSampleArray(ss.EndpointBufferFrameNum, trimmed); } { var data = mPcmReady.GetSampleArray(); var trimmed = new byte[ss.EndpointBufferFrameNum * mPcmReady.BitsPerFrame / 8]; Array.Copy(data, trimmed, trimmed.Length); mPcmReady.SetSampleArray(ss.EndpointBufferFrameNum, trimmed); } mWasapiPlay.ClearPlayList(); mWasapiPlay.AddPlayPcmDataStart(); mWasapiPlay.AddPlayPcmData(0, mPcmSync.GetSampleArray()); mWasapiPlay.AddPlayPcmData(1, mPcmReady.GetSampleArray()); mWasapiPlay.AddPlayPcmData(2, mPcmTest.GetSampleArray()); mWasapiPlay.AddPlayPcmDataEnd(); mWasapiPlay.SetPlayRepeat(false); mWasapiPlay.ConnectPcmDataNext(0, 0); hr = mWasapiPlay.StartPlayback(0); mPlayWorker.RunWorkerAsync(); // 録音 mCapturedBytes = 0; hr = mWasapiRec.Setup(listBoxRecDevices.SelectedIndex, WasapiCS.DeviceType.Rec, WasapiCS.StreamType.PCM, mSampleRate, mRecSampleFormat, NUM_CHANNELS, WasapiCS.MMCSSCallType.Enable, WasapiCS.SchedulerTaskType.ProAudio, WasapiCS.ShareMode.Exclusive, mRecDataFeedMode, mRecBufferMillisec, 1000, 10000); if (hr < 0) { MessageBox.Show(string.Format(Properties.Resources.msgRecSetupError, mSampleRate, mRecSampleFormat, NUM_CHANNELS, mRecDataFeedMode, mRecBufferMillisec)); StopUnsetup(); return; } var playAttr = mWasapiPlay.GetDeviceAttributes(listBoxPlayDevices.SelectedIndex); var recAttr = mWasapiRec.GetDeviceAttributes(listBoxRecDevices.SelectedIndex); textBoxLog.Text += string.Format(Properties.Resources.msgTestStarted, mSampleRate, mNumTestFrames / mSampleRate); textBoxLog.Text += string.Format(Properties.Resources.msgPlaySettings, mPlaySampleFormat, mPlayBufferMillisec, mPlayDataFeedMode, playAttr.Name); textBoxLog.Text += string.Format(Properties.Resources.msgRecSettings, mRecSampleFormat, mRecBufferMillisec, mRecDataFeedMode, recAttr.Name); textBoxLog.ScrollToEnd(); groupBoxPcmDataSettings.IsEnabled = false; groupBoxPlayback.IsEnabled = false; groupBoxRecording.IsEnabled = false; buttonStart.IsEnabled = false; buttonStop.IsEnabled = true; // SYNC失敗タイマーのセット mSyncTimeout.Start(); hr = mWasapiRec.StartRecording(); mRecWorker.RunWorkerAsync(); mState = State.Syncing; } }
public void ClearPlayList() { PcmDataListForDisp.Clear(); PcmDataListForPlay.Clear(); wasapi.ClearPlayList(); }