private void Recorder_OnPeakVolumeChanged(object sender, OnPeakVolumeChangedArgs e) { if (stopForm != null) { stopForm.UpdateVolumeBar(e.Volume); } }
protected virtual void PeakVolumeChanged(OnPeakVolumeChangedArgs e) { EventHandler <OnPeakVolumeChangedArgs> handler = OnPeakVolumeChanged; if (handler != null) { handler(this, e); } }
private void audioSource_DataAvailable(object sender, WaveInEventArgs e) { int vol = 0; if (waveFile != null) { if (Mute) { waveFile.Write(new byte[e.BytesRecorded], 0, e.BytesRecorded); } else { waveFile.Write(e.Buffer, 0, e.BytesRecorded); } waveFile.Flush(); } var signalled = WaitHandle.WaitAny(new WaitHandle[] { videoFrameWritten, stopThread }); if (signalled == 0) { if (audioSource.WaveFormat.BitsPerSample == 32) { if (Mute) { audioStream.WriteBlock(new byte[e.BytesRecorded / 2], 0, e.BytesRecorded / 2); } else { byte[] newArray16Bit = new byte[e.BytesRecorded / 2]; short two; float value; for (int i = 0, j = 0; i < e.BytesRecorded; i += 4, j += 2) { value = (BitConverter.ToSingle(e.Buffer, i)); two = (short)(value * short.MaxValue); newArray16Bit[j] = (byte)(two & 0xFF); newArray16Bit[j + 1] = (byte)((two >> 8) & 0xFF); } audioStream.WriteBlock(newArray16Bit, 0, e.BytesRecorded / 2); float max = 0; for (int index = 0; index < e.BytesRecorded / 2; index += 2) { short sample = (short)((newArray16Bit[index + 1] << 8) | newArray16Bit[index + 0]); var sample32 = sample / 32768f; if (sample32 < 0) { sample32 = -sample32; } if (sample32 > max) { max = sample32; } } vol = (int)(100 * max); } } else { if (Mute) { audioStream.WriteBlock(new byte[e.BytesRecorded], 0, e.BytesRecorded); } else { audioStream.WriteBlock(e.Buffer, 0, e.BytesRecorded); float max = 0; for (int index = 0; index < e.BytesRecorded; index += 2) { short sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]); var sample32 = sample / 32768f; if (sample32 < 0) { sample32 = -sample32; } if (sample32 > max) { max = sample32; } } vol = (int)(100 * max); } } audioBlockWritten.Set(); } OnPeakVolumeChangedArgs opvcArgs = new OnPeakVolumeChangedArgs() { Volume = vol }; PeakVolumeChanged(opvcArgs); }