/// <summary> /// Create a new image capture object. /// </summary> /// <param name="capture">The capture provider.</param> public SoundCapture(CaptureSoundSample capture) { if (capture == null) { throw new ArgumentException("The capture parameter must be set to a valid Filter.\n"); } _capture = capture; OnCreated(); }
/// <summary> /// Start sample sound capture continuous. /// </summary> /// <param name="capture">The capture provider.</param> /// <param name="captureHandler">The capture method where image data is to be written to.</param> /// <param name="soundCaptureType">The sound capture type.</param> private void StartSampleSoundCaptureContinuous(CaptureSoundSample capture, Action <MemoryStream> captureHandler, SoundCaptureType soundCaptureType = SoundCaptureType.Wav) { MemoryStream stream = new MemoryStream(); byte[] buffer = null; int sampleSize = 0; IntPtr ip = IntPtr.Zero; // While not shutting down, and still capturing. while (!_stop) { try { // capture image ip = capture.GetSnapshotSoundPointer(); // Allocate the memory byte size. sampleSize = (int)capture.Channels * capture.SampleRate; buffer = new byte[sampleSize]; // Copy the sound data to the buffer. Marshal.Copy(ip, buffer, 0, buffer.Length); // Format the PCM raw sound data. FormatSound(stream, capture, buffer, soundCaptureType); // Send the sound to the client. captureHandler(stream); // Empty the stream stream.SetLength(0); } catch (Exception) { } finally { if (ip != IntPtr.Zero) { Marshal.FreeCoTaskMem(ip); ip = IntPtr.Zero; } } } try { } finally { if (stream != null) { stream.Close(); } } }
/// <summary> /// Format the raw PCM sound data. /// </summary> /// <param name="stream">The memory stream that will contain the sound data.</param> /// <param name="capture">The capture provider.</param> /// <param name="rawPCMSound">The raw PCM sound data.</param> /// <param name="soundCaptureType">The sound capture type.</param> private void FormatSound(MemoryStream stream, CaptureSoundSample capture, byte[] rawPCMSound, SoundCaptureType soundCaptureType = SoundCaptureType.Wav) { switch (soundCaptureType) { case SoundCaptureType.Pcm: // Write the raw PCm sound data. stream.Write(rawPCMSound, 0, rawPCMSound.Length); stream.Flush(); break; case SoundCaptureType.Wav: default: // Write the wave formatted data from the raw PCM sound data. WaveStructure waveStructure = WaveStructure.CreateDefaultStructure(capture.Channels, capture.SampleRate, capture.BitsPerSample, rawPCMSound); WaveFormat waveFormat = new WaveFormat(); waveFormat.Write(stream, waveStructure); break; } }
/// <summary> /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// </summary> protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this.disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { if (_capture != null) { _capture.Dispose(); } } // Call the appropriate methods to clean up // unmanaged resources here. _capture = null; // Note disposing has been done. disposed = true; } }