Example #1
0
        /// <summary>
        /// Creates a buffer for capturing waveform audio.
        /// </summary>
        /// <param name="capture">a reference to an instance of <see cref="DirectSoundCapture"/></param>
        /// <param name="description">a <see cref="SharpDX.DirectSound.CaptureBufferDescription"/> structure containing values for the capture buffer being created. </param>
        /// <returns>No documentation.</returns>
        /// <unmanaged>HRESULT IDirectSoundCapture::CreateCaptureBuffer([In] LPCDSCBUFFERDESC pcDSCBufferDesc,[Out] LPDIRECTSOUNDCAPTUREBUFFER* ppDSCBuffer,[None] IUnknown* pUnkOuter)</unmanaged>
        public CaptureBuffer(DirectSoundCapture capture, CaptureBufferDescription description) : base(IntPtr.Zero)
        {
            CaptureBufferBase captureBuffer;

            capture.CreateCaptureBuffer(description, out captureBuffer, null);
            using (captureBuffer)
            {
                NativePointer = captureBuffer.NativePointer;
                QueryInterfaceFrom(this);
            }
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SharpDX.DirectSound.FullDuplex" /> class.
 /// </summary>
 /// <param name="captureDevice" />
 /// <param name="playbackDevice" />
 /// <param name="captureDescription" />
 /// <param name="bufferDescription" />
 /// <param name="windowHandle" />
 /// <param name="level" />
 /// <param name="captureBuffer" />
 /// <param name="secondaryBuffer" />
 public FullDuplex(Guid captureDevice, Guid playbackDevice, CaptureBufferDescription captureDescription, SoundBufferDescription bufferDescription, IntPtr windowHandle, CooperativeLevel level, out CaptureBuffer captureBuffer, out SecondarySoundBuffer secondaryBuffer)
 {
     DSound.FullDuplexCreate(captureDevice, playbackDevice, captureDescription, bufferDescription, windowHandle, (int)level, this,
                             out captureBuffer, out secondaryBuffer, null);
 }
        /// <summary>
        ///   Worker thread.
        /// </summary>
        /// 
        private void WorkerThread()
        {
            // Get the selected capture device
            DirectSoundCapture captureDevice = new DirectSoundCapture(device);


            // Set the capture format
            var bitsPerSample = Signal.GetSampleSize(sampleFormat);
            WaveFormat format = WaveFormat.CreateCustomFormat(sampleFormat.ToWaveFormat(), sampleRate, 1,
                sampleRate * bitsPerSample / 8, bitsPerSample / 8, bitsPerSample);

            // Setup the capture buffer
            CaptureBufferDescription captureBufferDescription = new CaptureBufferDescription();
            captureBufferDescription.Format = format;
            captureBufferDescription.BufferBytes = 2 * desiredCaptureSize * format.BlockAlign;
            captureBufferDescription.Flags |= CaptureBufferCapabilitiesFlags.WaveMapped;
            captureBufferDescription.Flags &= ~CaptureBufferCapabilitiesFlags.ControlEffects;

            CaptureBuffer captureBuffer = null;
            NotificationPosition[] notifications = new NotificationPosition[2];

            try
            {
                captureBuffer = new CaptureBuffer(captureDevice, captureBufferDescription);

                // Setup the notification positions
                int bufferPortionSize = captureBuffer.Capabilities.BufferBytes / 2;
                notifications[0] = new NotificationPosition();
                notifications[0].Offset = bufferPortionSize - 1;
                notifications[0].WaitHandle = new AutoResetEvent(false);
                notifications[1] = new NotificationPosition();
                notifications[1].Offset = bufferPortionSize - 1 + bufferPortionSize;
                notifications[1].WaitHandle = new AutoResetEvent(false);
                captureBuffer.SetNotificationPositions(notifications);

                // Make a copy of the wait handles
                WaitHandle[] waitHandles = new WaitHandle[notifications.Length];
                for (int i = 0; i < notifications.Length; i++)
                    waitHandles[i] = notifications[i].WaitHandle;



                // Start capturing
                captureBuffer.Start(true);


                if (sampleFormat == SampleFormat.Format32BitIeeeFloat)
                {
                    float[] currentSample = new float[desiredCaptureSize];

                    while (!stopEvent.WaitOne(0, true))
                    {
                        int bufferPortionIndex = WaitHandle.WaitAny(waitHandles);
                        captureBuffer.Read(currentSample, 0, currentSample.Length, bufferPortionSize * bufferPortionIndex, LockFlags.None);
                        OnNewFrame(currentSample);
                    }
                }
                else if (sampleFormat == SampleFormat.Format16Bit)
                {
                    short[] currentSample = new short[desiredCaptureSize];

                    while (!stopEvent.WaitOne(0, true))
                    {
                        int bufferPortionIndex = WaitHandle.WaitAny(waitHandles);
                        captureBuffer.Read(currentSample, 0, currentSample.Length, bufferPortionSize * bufferPortionIndex, LockFlags.None);
                        OnNewFrame(currentSample);
                    }
                }
            }
            catch (Exception ex)
            {
                if (AudioSourceError != null)
                    AudioSourceError(this, new AudioSourceErrorEventArgs(ex.Message));
                else throw;
            }
            finally
            {
                if (captureBuffer != null)
                {
                    captureBuffer.Stop();
                    captureBuffer.Dispose();
                }

                if (captureDevice != null)
                    captureDevice.Dispose();

                for (int i = 0; i < notifications.Length; i++)
                    if (notifications[i].WaitHandle != null)
                        notifications[i].WaitHandle.Close();
            }
        }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SharpDX.DirectSound.FullDuplex" /> class.
 /// </summary>
 /// <param name="captureDevice" />
 /// <param name="playbackDevice" />
 /// <param name="captureDescription" />
 /// <param name="bufferDescription" />
 /// <param name="windowHandle" />
 /// <param name="level" />
 /// <param name="captureBuffer" />
 /// <param name="secondaryBuffer" />
 public FullDuplex(Guid captureDevice, Guid playbackDevice, CaptureBufferDescription captureDescription, SoundBufferDescription bufferDescription, IntPtr windowHandle, CooperativeLevel level, out CaptureBuffer captureBuffer, out SecondarySoundBuffer secondaryBuffer)
 {
     DSound.FullDuplexCreate(captureDevice, playbackDevice, captureDescription, bufferDescription, windowHandle, (int) level, this,
                             out captureBuffer, out secondaryBuffer, null);
 }