Пример #1
0
        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;
            }
        }