public void CLearDataAvailableEvent() { if (DataAvailable == null) { return; } Delegate[] dels = DataAvailable.GetInvocationList(); foreach (Delegate del in dels) { object delObj = del.GetType().GetProperty("Method").GetValue(del, null); string funcName = (string)delObj.GetType().GetProperty("Name").GetValue(delObj, null);////方法名 Console.WriteLine(funcName); DataAvailable -= del as EventHandler <InfraredSensorDataEventArgs>; } }
public void OnDataAvailable(object sender, WaveInEventArgs e) { _waitingForData = false; var outPos = 0; var floatIndex = 0; try { CheckBuffers(e.BytesRecorded); // Note: Data ratio of stereo 32-bit 48KHz to mono 16-bit 8KHz is 24:1 //var bytesReceived = e.BytesRecorded; //var outputSize = bytesReceived / 24; //var outArray = new byte[(int)Math.Ceiling(outputSize / 2d) * 2]; // Note: Data ratio of stereo 32-bit 48KHz to mono 8-bit 8KHz is 48:1 var bytesReceived = e.BytesRecorded; var outputSize = bytesReceived / 48; var outArray = new byte[(int)Math.Ceiling(outputSize / 2d) * 2]; // 48KHz // 32Kbps //var i = 0; //var j = 0; //var k = 0; //var o = new float[e.BytesRecorded / 8]; //while (i < e.BytesRecorded) //{ // var left = BitConverter.ToSingle(e.Buffer, i); // var right = BitConverter.ToSingle(e.Buffer, i + 4); // var mono = (left + right) * 0.5F; // o[j] = mono; // i = i + 8; // j = j + 1; //} //var result = Downsample(o); //for (k = 0; k < result.Length; k++) //{ // outArray[k] = MuLawEncoder.LinearToMuLawSample((short) (result[k] * MaxValue)); //} // #1 Resample to 8KHz var waveBuffer = Downsample(e); while (floatIndex < waveBuffer.FloatBufferCount) { // #2 Convert to Mono var leftSample = waveBuffer.FloatBuffer[floatIndex++]; var rightSample = waveBuffer.FloatBuffer[floatIndex++]; var monoSample = ConvertToMono(leftSample, rightSample); // #3 Convert to short and then mu-law outArray[outPos++] = MuLawEncoder.LinearToMuLawSample((short)(monoSample * MaxValue)); } if (DataAvailable != null) { foreach (var delDelegate in DataAvailable.GetInvocationList()) { delDelegate.DynamicInvoke(this, new DataEventArgs(outArray, outPos)); //24)); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.WriteLine($"{nameof(outPos)}: {outPos}, {nameof(floatIndex)}: {floatIndex}"); throw; } _waitingForData = true; }