/// <summary> /// Get the sound type. /// </summary> /// <param name="soundType">The sound type.</param> /// <returns>The sound type index.</returns> public static int GetSoundTypeInt32(SoundCaptureType soundType) { // Select the sound type. switch (soundType) { case SoundCaptureType.Pcm: return(0); case SoundCaptureType.Wav: default: return(1); } }
/// <summary> /// Start sample sound capture only on request. /// </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 StartSampleSoundCaptureSingle(CaptureSoundSample capture, Action <MemoryStream> captureHandler, SoundCaptureType soundCaptureType = SoundCaptureType.Wav) { MemoryStream stream = new MemoryStream(); byte[] buffer = null; int sampleSize = 0; IntPtr ip = IntPtr.Zero; 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; } 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> /// Start the capture continuous process. /// </summary> /// <param name="captureHandler">The capture method where sound data is to be written to.</param> /// <param name="soundCaptureType">The sound capture type.</param> public void StartContinuous(Action <MemoryStream> captureHandler, SoundCaptureType soundCaptureType = SoundCaptureType.Wav) { _stop = false; StartSampleSoundCaptureContinuous(_capture, captureHandler, soundCaptureType); }