Exemplo n.º 1
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;
            }
        }
Exemplo n.º 2
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;
            }
        }