// OLD methods (did not work with event handlers in connection with the unmanaged code...) /* * private void GenerateSoundSnippet() * { * int numberOfBytesRecorded = waveHeader.bytesRecorded; * if (numberOfBytesRecorded > 0) * { * byte[] newSoundData = new byte[numberOfBytesRecorded]; * Marshal.Copy(waveHeader.dataPointer, newSoundData, 0, numberOfBytesRecorded); * totalNumberOfBytesRecorded += numberOfBytesRecorded; * OnSnippetRecorded(newSoundData, totalNumberOfBytesRecorded); * } * } * * private void OnSnippetRecorded(byte[] recordedBytes, int numberofBytes) * { * if (SnippetRecorded != null) * { * WAVRecorderEventArgs e = new WAVRecorderEventArgs(recordedBytes, numberofBytes); * EventHandler<WAVRecorderEventArgs> handler = SnippetRecorded; * handler(this, e); * } * } * * private void OnRecordingStopped() * { * if (RecordingStopped != null) * { * EventHandler handler = RecordingStopped; * handler(this, EventArgs.Empty); * } * } */ #endregion #region Public methods public void Start() { recordingIndex = 0; timeRecordingList = new List <Tuple <int, DateTime, byte[]> >(); storageCount = (int)Math.Round(storageDuration * numberOfSnippetsPerSecond); isRecording = true; waveIn = new AudioRecordingDelegate(callbackWaveIn); waveHandle = new IntPtr(); InitializeWaveFormat(); bufferLength = (uint)(waveFormat.averageBytesPerSecond / numberOfSnippetsPerSecond); headerPin = GCHandle.Alloc(waveHeader, GCHandleType.Pinned); buffer = new byte[bufferLength]; bufferPin = GCHandle.Alloc(buffer, GCHandleType.Pinned); waveInOpen(ref waveHandle, (uint)deviceId, ref waveFormat, Marshal.GetFunctionPointerForDelegate(waveIn), (uint)0, (uint)CALLBACK_FUNCTION); SetupBuffer(); waveInStart(waveHandle); }
/* SetupWaveIn * * Setups and starts recording, globals used to pass data into a WaveReader object * * Return void */ private void setupWaveIn() { waveIn = this.callbackWaveIn; handle = new IntPtr(); WAVEFORMAT format; fmtCode = format.wFormatTag = 1; channels = format.nChannels = 1; format.nSamplesPerSec = 11025; sampleRate = (int)format.nSamplesPerSec; bitDepth = format.wBitsPerSample = 8; fmtBlockAlign = format.nBlockAlign = 1; // Convert.ToUInt16(format.nChannels * format.wBitsPerSample); format.nAvgBytesPerSec = 11025; fmtAvgBPS = (int)format.nAvgBytesPerSec; //format.nSamplesPerSec * format.nBlockAlign; bufferLength = 16384; // format.nAvgBytesPerSec / 800; format.cbSize = 0; buffer = new byte[bufferLength]; bufferPin = GCHandle.Alloc(buffer, GCHandleType.Pinned); //4294967295 = WAVE_MAPPER aka unsigned int (-1) int i = waveInOpen(ref handle, 4294967295, ref format, Marshal.GetFunctionPointerForDelegate(waveIn), 0, CALLBACK_FUNCTION); if (i != 0) { this.Text = "Error: waveInOpen"; return; } setupBuffer(); i = waveInStart(handle); if (i != 0) { this.Text = "Error: waveInStart" + i; return; } SystemSounds.Beep.Play(); }
private void setupWaveIn() { waveIn = this.callbackWaveIn; handle = new IntPtr(); WAVEFORMAT format; format.wFormatTag = 1; format.nChannels = 1; format.nSamplesPerSec = 11025; format.wBitsPerSample = 8; format.nBlockAlign = 1; // Convert.ToUInt16(format.nChannels * format.wBitsPerSample); format.nAvgBytesPerSec = 11025; //format.nSamplesPerSec * format.nBlockAlign; bufferLength = 16384; // format.nAvgBytesPerSec / 800; format.cbSize = 0; buffer = new byte[bufferLength]; bufferPin = GCHandle.Alloc(buffer, GCHandleType.Pinned); //4294967295 = WAVE_MAPPER aka unsigned int (-1) // int i = waveInOpen(ref handle, ((unchecked uint)-1), ref format, Marshal.GetFunctionPointerForDelegate(waveIn), 0, CALLBACK_FUNCTION); uint wavemapper = (unchecked ((uint)-1)); int i = waveInOpen(ref handle, wavemapper, ref format, Marshal.GetFunctionPointerForDelegate(waveIn), 0, CALLBACK_FUNCTION); if (i != 0) { this.Text = "Error: waveInOpen"; return; } setupBuffer(); // setupBuffer();//2nd buffer i = waveInStart(handle); if (i != 0) { this.Text = "Error: waveInStart" + i; } }