private void stopCaptureImpl() { try { if (_audioClient != null && _capClient != null) { _audioClient.Stop(); } // イベント発火 CaptureStoppedEventHandler del1 = CaptureStopped; if (del1 != null) { del1.Invoke(this, new CaptureStoppedEventArgs()); } DataUpdatedEventHandler del2 = DataUpdated; if (del2 != null) { del2.Invoke(this, new DataUpdatedEventArgs(new PitchResult(0, 0), ToneResult.Default)); } } catch (System.Runtime.InteropServices.COMException ex) { #warning エラー処理 throw new Exception(ex.Message); } }
// 今溜まっている分を解析する private void analyzeBuffer() { _prevAnalyzeTime = Environment.TickCount; _pitchAnalyzer.SampleFrequency = _capFormat.nSamplesPerSec; double[] buf = _buffer.ToArray(); for (int i = 0; i < buf.Length; i++) { buf[i] /= _capFormat.nChannels; } PitchResult result = _pitchAnalyzer.Analyze(buf); ToneResult tone = ToneAnalyzer.Analyze(result.Pitch, result.Clarity); // イベント発火 DataUpdatedEventHandler del = DataUpdated; if (del != null) { del.Invoke(this, new DataUpdatedEventArgs(result, tone)); } }
private void mainThread() { try { while (true) { Operation op = new Operation(Operation.OprationType.None); lock (_opQueue) { if (_opQueue.Count > 0) { op = _opQueue.Dequeue(); } } switch (op.OpType) { case Operation.OprationType.Dispose: // 終了条件 releaseDevice(); disposeImpl(); _capturing = false; return; case Operation.OprationType.UpdateDevices: // Device一覧アップデート条件 updateDeviceInfoImpl((bool)op.OpArgs[0]); break; case Operation.OprationType.SelectDevice: // Device選択条件 selectDeviceImpl(op.OpArgs[0] as string); _capturing = false; break; case Operation.OprationType.StartCapture: // 開始条件 startCaptureImpl(); _capturing = true; break; case Operation.OprationType.StopCapture: // 解放条件(stop) stopCaptureImpl(); _capturing = false; break; default: // その他(GetBuffer) { #region キャプチャ if (_capturing) { if (_capClient != null) { long curTick = Environment.TickCount; uint size = 0; try { size = _capClient.NextPacketSize; if (size == 0) // 音が無い or バッファが一定量溜まっていない { if (!_reset) { // データが来なくなってから一定時間 -> リセット if (curTick - _prevResetTime > RESETLIMIT) { _prevResetTime = curTick; resetImpl(); DataUpdatedEventHandler del = DataUpdated; if (del != null) { del.Invoke(this, new DataUpdatedEventArgs(new PitchResult(0, 0), ToneResult.Default)); } } } } else { _reset = false; _prevResetTime = curTick; try { while (_capClient.NextPacketSize > 0) { byte[] bytes; uint numFrames; AudioClientBufferFlags flags; UInt64 devicePosition; UInt64 qpcPosition; _capClient.GetBuffer(out bytes, out numFrames, out flags, out devicePosition, out qpcPosition); //if ((flags & AudioClientBufferFlags.DataDiscontinuity) != 0) // clearBuffer(); switch (_capFormat.wBitsPerSample) { case 8: get8bitBuf(bytes); break; case 16: get16bitBuf(bytes); break; case 24: get24bitBuf(bytes); break; case 32: get32bitBuf(bytes); break; } } } catch (Exception) { } truncateBuffer(); if (curTick - _prevAnalyzeTime > ANALYZESPAN) { analyzeBuffer(); } } } catch (System.Runtime.InteropServices.COMException ex) { #warning エラー処理 } } } #endregion } break; } Thread.Sleep(_sleepTime); } } catch (ThreadAbortException threadEx) { Console.WriteLine(threadEx.Message); } }