/// ------------------------------------------------------------------------------------ /// <summary> /// /// </summary> /// <param name="maxMinutes">REVIW: why does this max time even exist? I don't see that it affects buffer size</param> /// ------------------------------------------------------------------------------------ public AudioRecorder(int maxMinutes) { _maxMinutes = maxMinutes; SampleAggregator = new SampleAggregator(); SampleAggregator.MaximumCalculated += delegate { _peakLevelEventArgs.Level = SampleAggregator.maxValue; if (PeakLevelChanged != null) { PeakLevelChanged.BeginInvoke(this, _peakLevelEventArgs, null, null); } }; RecordingFormat = new WaveFormat(44100, 1); }
/// ------------------------------------------------------------------------------------ /// <summary> /// /// </summary> /// <param name="maxMinutes">REVIW: why does this max time even exist? I don't see that it affects buffer size</param> /// ------------------------------------------------------------------------------------ public AudioRecorder(int maxMinutes) { _maxMinutes = maxMinutes; SampleAggregator = new SampleAggregator(); SampleAggregator.MaximumCalculated += delegate { _peakLevelEventArgs.Level = SampleAggregator.maxValue; if (PeakLevelChanged != null) PeakLevelChanged.BeginInvoke(this, _peakLevelEventArgs, null, null); }; RecordingFormat = new WaveFormat(44100, 1); }
/// ------------------------------------------------------------------------------------ protected virtual void waveIn_DataAvailable(object sender, WaveInEventArgs e) { /*original from codeplex * byte[] buffer = e.Buffer; * int bytesRecorded = e.BytesRecorded; * WriteToFile(buffer, bytesRecorded); * * for (int index = 0; index < e.BytesRecorded; index += 2) * { * short sample = (short)((buffer[index + 1] << 8) | * buffer[index + 0]); * float sample32 = sample / 32768f; * _sampleAggregator.Add(sample32); * } */ //David's version: lock (this) { var buffer = e.Buffer; int bytesRecorded = e.BytesRecorded; bool hitMaximumFileSize = false; if (_recordingState == RecordingState.Recording || _recordingState == RecordingState.RequestedStop) { Debug.WriteLine("Writing " + bytesRecorded + " bytes of data to file"); hitMaximumFileSize = !WriteToFile(buffer, bytesRecorded); } var bytesPerSample = _waveIn.WaveFormat.BitsPerSample / 8; // It appears the data only occupies 2 bytes of those in a sample and that // those 2 are always the last two in each sample. The other bytes are zero // filled. Therefore, when getting those two bytes, the first index into a // sample needs to be 0 for 16 bit samples, 1 for 24 bit samples and 2 for // 32 bit samples. I'm not sure what to do for 8 bit samples. I could never // figure out the correct conversion of a byte in an 8 bit per sample buffer // to a float sample value. However, I doubt folks are going to be recording // at 8 bits/sample so I'm ignoring that problem. for (var index = bytesPerSample - 2; index < bytesRecorded - 1; index += bytesPerSample) { var sample = (short)((buffer[index + 1] << 8) | buffer[index]); var sample32 = sample / 32768f; SampleAggregator.Add(sample32); } if (_fileWriterThread == null) { return; } // Only fire the progress event every 10th of a second. var currRecordedTime = (double)_bytesRecorded / _recordingFormat.AverageBytesPerSecond; if (currRecordedTime - _prevRecordedTime >= 0.05d) { _prevRecordedTime = currRecordedTime; _recProgressEventArgs.RecordedLength = TimeSpan.FromSeconds(currRecordedTime); if (RecordingProgress != null) { RecordingProgress.BeginInvoke(this, _recProgressEventArgs, null, null); } } if (RecordingState == RecordingState.RequestedStop) { if (DateTime.Now > _recordingStopTime.AddSeconds(2) || hitMaximumFileSize || _recordingStartTime.AddSeconds(currRecordedTime) >= _recordingStopTime) { Debug.WriteLine("Transition to monitoring from DataAvailable"); TransitionFromRecordingToMonitoring(); } } } }