Ejemplo n.º 1
0
 /// <summary>
 /// Dispose
 /// </summary>
 public void Dispose()
 {
     if (audioClientInterface != null)
     {
         if (audioClockClient != null)
         {
             audioClockClient.Dispose();
             audioClockClient = null;
         }
         if (audioRenderClient != null)
         {
             audioRenderClient.Dispose();
             audioRenderClient = null;
         }
         if (audioCaptureClient != null)
         {
             audioCaptureClient.Dispose();
             audioCaptureClient = null;
         }
         if (audioStreamVolume != null)
         {
             audioStreamVolume.Dispose();
             audioStreamVolume = null;
         }
         Marshal.ReleaseComObject(audioClientInterface);
         audioClientInterface = null;
         GC.SuppressFinalize(this);
     }
 }
Ejemplo n.º 2
0
        private void ReadNextPacket(AudioCaptureClient capture)
        {
            IntPtr buffer;
            int    framesAvailable;
            AudioClientBufferFlags flags;
            int packetSize         = capture.GetNextPacketSize();
            int recordBufferOffset = 0;

            //Debug.WriteLine(string.Format("packet size: {0} samples", packetSize / 4));
            while (packetSize != 0)
            {
                buffer = capture.GetBuffer(out framesAvailable, out flags);

                int bytesAvailable = framesAvailable * bytesPerFrame;

                //Debug.WriteLine(string.Format("got buffer: {0} frames", framesAvailable));

                // if not silence...
                if ((flags & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent)
                {
                    Marshal.Copy(buffer, recordBuffer, recordBufferOffset, bytesAvailable);
                }
                else
                {
                    Array.Clear(recordBuffer, recordBufferOffset, bytesAvailable);
                }
                recordBufferOffset += bytesAvailable;
                capture.ReleaseBuffer(framesAvailable);
                packetSize = capture.GetNextPacketSize();
            }
            if (DataAvailable != null)
            {
                DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset));
            }
        }
Ejemplo n.º 3
0
        private void DoRecording(AudioClient client)
        {
            int  bufferSize                       = client.BufferSize;
            long num                              = (long)(10000000.0 * (double)bufferSize / (double)this.waveFormat.SampleRate);
            int  millisecondsTimeout              = (int)(num / 10000L / 2L);
            int  millisecondsTimeout2             = (int)(3L * num / 10000L);
            AudioCaptureClient audioCaptureClient = client.AudioCaptureClient;

            client.Start();
            if (this.isUsingEventSync)
            {
            }
            while (!this.requestStop)
            {
                bool flag = true;
                if (this.isUsingEventSync)
                {
                    flag = this.frameEventWaitHandle.WaitOne(millisecondsTimeout2, false);
                }
                else
                {
                    Thread.Sleep(millisecondsTimeout);
                }
                if (!this.requestStop && flag)
                {
                    this.ReadNextPacket(audioCaptureClient);
                }
            }
        }
Ejemplo n.º 4
0
        public override void Exit()
        {
            capClient.Dispose();
            capClient = null;

            audioClient.Stop();
            audioClient.Dispose();
            audioClient = null;
        }
Ejemplo n.º 5
0
        private void ReadNextPacket(AudioCaptureClient capture)
        {
            int packetSize         = capture.GetNextPacketSize();
            int recordBufferOffset = 0;

            //Debug.WriteLine(string.Format("packet size: {0} samples", packetSize / 4));

            while (packetSize != 0)
            {
                int framesAvailable;
                AudioClientBufferFlags flags;
                IntPtr buffer = capture.GetBuffer(out framesAvailable, out flags);

                int bytesAvailable = framesAvailable * bytesPerFrame;

                // apparently it is sometimes possible to read more frames than we were expecting?
                // fix suggested by Michael Feld:
                int spaceRemaining = Math.Max(0, recordBuffer.Length - recordBufferOffset);
                if (spaceRemaining < bytesAvailable && recordBufferOffset > 0)
                {
                    if (DataAvailable != null)
                    {
                        DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset));
                    }
                    recordBufferOffset = 0;
                }

                if ((flags & AudioClientBufferFlags.DataDiscontinuity) == AudioClientBufferFlags.DataDiscontinuity)
                {
                    Debug.WriteLine("DataDiscontinuity flagged!");
                }

                // if not silence...
                if ((flags & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent)
                {
                    Marshal.Copy(buffer, recordBuffer, recordBufferOffset, bytesAvailable);
                }
                else
                {
                    Array.Clear(recordBuffer, recordBufferOffset, bytesAvailable);
                }
                recordBufferOffset += bytesAvailable;
                capture.ReleaseBuffer(framesAvailable);
                packetSize = capture.GetNextPacketSize();
            }
            if (DataAvailable != null)
            {
                DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset));
            }
        }
Ejemplo n.º 6
0
        private void CaptureThread(AudioClient client)
        {
            Debug.WriteLine(client.BufferSize);
            int bufferFrameCount = audioClient.BufferSize;

            // Calculate the actual duration of the allocated buffer.
            long actualDuration = (long)((double)REFTIMES_PER_SEC *
                                         bufferFrameCount / WaveFormat.SampleRate);
            int sleepMilliseconds = (int)(actualDuration / REFTIMES_PER_MILLISEC / 2);

            AudioCaptureClient capture = client.AudioCaptureClient;

            client.Start();

            try
            {
                Debug.WriteLine(string.Format("sleep: {0} ms", sleepMilliseconds));
                while (!this.stop)
                {
                    Thread.Sleep(sleepMilliseconds);
                    ReadNextPacket(capture);
                }

                client.Stop();

                if (RecordingStopped != null)
                {
                    RecordingStopped(this, EventArgs.Empty);
                }
            }
            finally
            {
                if (capture != null)
                {
                    capture.Dispose();
                }
                if (client != null)
                {
                    client.Dispose();
                }

                client  = null;
                capture = null;
            }

            System.Diagnostics.Debug.WriteLine("stop wasapi");
        }
Ejemplo n.º 7
0
        private void DoRecording(AudioClient client)
        {
            Debug.WriteLine(String.Format("Client buffer frame count: {0}", client.BufferSize));
            int bufferFrameCount = client.BufferSize;

            // Calculate the actual duration of the allocated buffer.
            long actualDuration = (long)((double)REFTIMES_PER_SEC *
                                         bufferFrameCount / waveFormat.SampleRate);
            int sleepMilliseconds = (int)(actualDuration / REFTIMES_PER_MILLISEC / 2);
            int waitMilliseconds  = (int)(3 * actualDuration / REFTIMES_PER_MILLISEC);

            AudioCaptureClient capture = client.AudioCaptureClient;

            client.Start();

            if (isUsingEventSync)
            {
                Debug.WriteLine(string.Format("wait: {0} ms", waitMilliseconds));
            }
            else
            {
                Debug.WriteLine(string.Format("sleep: {0} ms", sleepMilliseconds));
            }

            while (!this.requestStop)
            {
                bool readBuffer = true;
                if (isUsingEventSync)
                {
                    readBuffer = frameEventWaitHandle.WaitOne(waitMilliseconds, false);
                }
                else
                {
                    Thread.Sleep(sleepMilliseconds);
                }

                // If still playing and notification is ok
                if (!this.requestStop && readBuffer)
                {
                    ReadNextPacket(capture);
                }
            }
        }
Ejemplo n.º 8
0
        private void DoRecording(AudioClient client)
        {
            Debug.WriteLine(client.BufferSize);
            int bufferFrameCount = audioClient.BufferSize;

            // Calculate the actual duration of the allocated buffer.
            long actualDuration = (long)((double)REFTIMES_PER_SEC *
                                         bufferFrameCount / WaveFormat.SampleRate);
            int sleepMilliseconds = (int)(actualDuration / REFTIMES_PER_MILLISEC / 2);

            AudioCaptureClient capture = client.AudioCaptureClient;

            client.Start();
            Debug.WriteLine(string.Format("sleep: {0} ms", sleepMilliseconds));
            while (!this.stop)
            {
                Thread.Sleep(sleepMilliseconds);
                ReadNextPacket(capture);
            }
        }
Ejemplo n.º 9
0
        private void ReadNextPacket(AudioCaptureClient capture)
        {
            int nextPacketSize = capture.GetNextPacketSize();
            int num            = 0;

            while (nextPacketSize != 0)
            {
                int num2;
                AudioClientBufferFlags audioClientBufferFlags;
                IntPtr buffer = capture.GetBuffer(out num2, out audioClientBufferFlags);
                int    num3   = num2 * this.bytesPerFrame;
                int    num4   = Math.Max(0, this.recordBuffer.Length - num);
                if (num4 < num3 && num > 0)
                {
                    if (this.DataAvailable != null)
                    {
                        this.DataAvailable(this, new WaveInEventArgs(this.recordBuffer, num));
                    }
                    num = 0;
                }
                if ((audioClientBufferFlags & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent)
                {
                    Marshal.Copy(buffer, this.recordBuffer, num, num3);
                }
                else
                {
                    Array.Clear(this.recordBuffer, num, num3);
                }
                num += num3;
                capture.ReleaseBuffer(num2);
                nextPacketSize = capture.GetNextPacketSize();
            }
            if (this.DataAvailable != null)
            {
                this.DataAvailable(this, new WaveInEventArgs(this.recordBuffer, num));
            }
        }
Ejemplo n.º 10
0
        private void CaptureThread(AudioClient client)
        {
            Debug.WriteLine(client.BufferSize);
            int bufferFrameCount = audioClient.BufferSize;

            // Calculate the actual duration of the allocated buffer.
            var actualDuration = (long)((double)REFTIMES_PER_SEC *
                                        bufferFrameCount / WaveFormat.SampleRate);
            var sleepMilliseconds = (int)(actualDuration / REFTIMES_PER_MILLISEC / 2);

            AudioCaptureClient capture = client.AudioCaptureClient;

            client.Start();

            try
            {
                Debug.WriteLine(string.Format("sleep: {0} ms", sleepMilliseconds));
                while (!stop)
                {
                    Thread.Sleep(sleepMilliseconds);
                    ReadNextPacket(capture);
                }
            }
            finally
            {
                client.Stop();

                if (RecordingStopped != null)
                {
                    RecordingStopped(this, EventArgs.Empty);
                }
                // don't dispose - the AudioClient only gets disposed when WasapiCapture is disposed
            }

            Debug.WriteLine("stop wasapi");
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Dispose
 /// </summary>
 public void Dispose()
 {
     if (audioClientInterface != null)
     {
         if (audioRenderClient != null)
         {
             audioRenderClient.Dispose();
             audioRenderClient = null;
         }
         if (audioCaptureClient != null)
         {
             audioCaptureClient.Dispose();
             audioCaptureClient = null;
         }
         Marshal.ReleaseComObject(audioClientInterface);
         audioClientInterface = null;
         GC.SuppressFinalize(this);
     }
 }
        private void ReadNextPacket(AudioCaptureClient capture)
        {
            IntPtr buffer;
            int framesAvailable;
            AudioClientBufferFlags flags;
            int packetSize = capture.GetNextPacketSize();
            int recordBufferOffset = 0;
            //Debug.WriteLine(string.Format("packet size: {0} samples", packetSize / 4));

            while (packetSize != 0)
            {
                buffer = capture.GetBuffer(out framesAvailable, out flags);

                int bytesAvailable = framesAvailable * bytesPerFrame;

                // apparently it is sometimes possible to read more frames than we were expecting?
                // fix suggested by Michael Feld:
                int spaceRemaining = Math.Max(0, recordBuffer.Length - recordBufferOffset);
                if (spaceRemaining < bytesAvailable && recordBufferOffset > 0)
                {
                    if (DataAvailable != null)
                        DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset));
                    recordBufferOffset = 0;
                }

                // if not silence...
                if ((flags & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent)
                {
                    Marshal.Copy(buffer, recordBuffer, recordBufferOffset, bytesAvailable);
                }
                else
                {
                    Array.Clear(recordBuffer, recordBufferOffset, bytesAvailable);
                }
                recordBufferOffset += bytesAvailable;
                capture.ReleaseBuffer(framesAvailable);
                packetSize = capture.GetNextPacketSize();
            }
            if (DataAvailable != null)
            {
                DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset));
            }
        }
Ejemplo n.º 13
0
        private void ReadNextPacket(AudioCaptureClient capture)
        {
            IntPtr buffer;
            int framesAvailable;
            AudioClientBufferFlags flags;
            int packetSize = capture.GetNextPacketSize();
            int recordBufferOffset = 0;
            //Debug.WriteLine(string.Format("packet size: {0} samples", packetSize / 4));

            while (packetSize != 0)
            {
                buffer = capture.GetBuffer(out framesAvailable, out flags);

                int bytesAvailable = framesAvailable * bytesPerFrame;

                //Debug.WriteLine(string.Format("got buffer: {0} frames", framesAvailable));

                // if not silence...
                if ((flags & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent)
                {
                    Marshal.Copy(buffer, recordBuffer, recordBufferOffset, bytesAvailable);
                }
                else
                {
                    Array.Clear(recordBuffer, recordBufferOffset, bytesAvailable);
                }
                recordBufferOffset += bytesAvailable;
                capture.ReleaseBuffer(framesAvailable);
                packetSize = capture.GetNextPacketSize();
            }


            if (DataAvailable != null)
            {
                DataAvailable(this, new WaveInEventArgs(recordBuffer, recordBufferOffset));
            }
        }
Ejemplo n.º 14
0
        private void InitializeAudioClient()
        {
            var enumerator = new MMDeviceEnumerator();
            var captureDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
            audioClient = captureDevice.AudioClient;

            int recordBufferLength = samplingRate; // 20ms worth of recording
            recordBuffer = new float[recordBufferLength * channelCount];

            long requestedDuration = 10000 * period * 2;

            audioClient.Initialize(AudioClientShareMode.Shared,
                AudioClientStreamFlags.Loopback,
                requestedDuration,
                0,
                WaveFormat.CreateIeeeFloatWaveFormat(samplingRate, channelCount),
                Guid.Empty);

            capClient = audioClient.AudioCaptureClient;
            audioClient.Start();
        }
Ejemplo n.º 15
0
        private void ReadNextPacket2(AudioCaptureClient capture1, AudioCaptureClient capture2)
        {
            int packetSize1         = capture1.GetNextPacketSize();
            int packetSize2         = capture2.GetNextPacketSize();
            int recordBufferOffset  = 0;
            int recordBufferOffset2 = 0;

            while (packetSize1 != 0 || packetSize2 != 0)
            {
                int framesAvailable1;
                AudioClientBufferFlags flags1;
                int framesAvailable2;
                AudioClientBufferFlags flags2;
                IntPtr buffer1 = capture1.GetBuffer(out framesAvailable1, out flags1);
                IntPtr buffer2 = capture2.GetBuffer(out framesAvailable2, out flags2);

                int bytesAvailable1 = framesAvailable1 * bytesPerFrame;
                int bytesAvailable2 = framesAvailable2 * bytesPerFrame2;

                // apparently it is sometimes possible to read more frames than we were expecting?
                // fix suggested by Michael Feld:
                int spaceRemaining  = Math.Max(0, recordBuffer.Length - recordBufferOffset);
                int spaceRemaining2 = Math.Max(0, recordBuffer2.Length - recordBufferOffset2);
                if ((spaceRemaining < bytesAvailable1 && recordBufferOffset > 0) || (spaceRemaining2 < bytesAvailable2 && recordBufferOffset2 > 0))
                {
                    int outputIndex    = 0;
                    int maxLen         = Math.Max(recordBufferOffset, recordBufferOffset2);
                    var destWaveBuffer = new WaveBuffer(new byte[maxLen]);

                    for (int i = 0; i < maxLen; i += 4)
                    {
                        float a, b;

                        if (i < recordBuffer.Length)
                        {
                            a = BitConverter.ToSingle(recordBuffer, i);
                        }
                        else
                        {
                            a = 0;
                        }

                        if (i < recordBuffer2.Length)
                        {
                            b = BitConverter.ToSingle(recordBuffer2, i);
                        }
                        else
                        {
                            b = 0;
                        }

                        float c = a + b;
                        if (c >= 1.0f)
                        {
                            c = 1.0f;
                        }
                        else if (c <= -1.0f)
                        {
                            c = -1.0f;
                        }
                        destWaveBuffer.ShortBuffer[outputIndex++] = (short)(c * 32767);
                    }
                    if (DataAvailable != null)
                    {
                        DataAvailable(this, new WaveInEventArgs(destWaveBuffer.ByteBuffer, maxLen / 2));
                    }
                    recordBufferOffset  = 0;
                    recordBufferOffset2 = 0;
                }

                // if not silence...
                if (packetSize1 != 0 && (flags1 & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent)
                {
                    Marshal.Copy(buffer1, recordBuffer, recordBufferOffset, bytesAvailable1);
                }
                if (packetSize2 != 0 && (flags2 & AudioClientBufferFlags.Silent) != AudioClientBufferFlags.Silent)
                {
                    Marshal.Copy(buffer2, recordBuffer2, recordBufferOffset2, bytesAvailable2);
                }
                if ((flags1 & AudioClientBufferFlags.Silent) == AudioClientBufferFlags.Silent)
                {
                    Array.Clear(recordBuffer, recordBufferOffset, bytesAvailable1);
                }
                if ((flags2 & AudioClientBufferFlags.Silent) == AudioClientBufferFlags.Silent)
                {
                    Array.Clear(recordBuffer2, recordBufferOffset2, bytesAvailable2);
                }

                recordBufferOffset  += bytesAvailable1;
                recordBufferOffset2 += bytesAvailable2;

                capture1.ReleaseBuffer(framesAvailable1);
                capture2.ReleaseBuffer(framesAvailable2);
                packetSize1 = capture1.GetNextPacketSize();
                packetSize2 = capture2.GetNextPacketSize();
            }

            if (DataAvailable != null)
            {
                int outputIndex    = 0;
                int maxLen         = Math.Max(recordBufferOffset, recordBufferOffset2);
                var destWaveBuffer = new WaveBuffer(new byte[maxLen / 2]);

                for (int i = 0; i < maxLen; i += 4)
                {
                    float a, b;

                    if (i < recordBuffer.Length)
                    {
                        a = BitConverter.ToSingle(recordBuffer, i);
                    }
                    else
                    {
                        a = 0;
                    }

                    if (i < recordBuffer2.Length)
                    {
                        b = BitConverter.ToSingle(recordBuffer2, i);
                    }
                    else
                    {
                        b = 0;
                    }

                    float c = a + b;
                    if (c >= 1.0f)
                    {
                        c = 1.0f;
                    }
                    else if (c <= -1.0f)
                    {
                        c = -1.0f;
                    }
                    destWaveBuffer.ShortBuffer[outputIndex++] = (short)(c * 32767);
                }

                DataAvailable(this, new WaveInEventArgs(destWaveBuffer.ByteBuffer, maxLen / 2));
            }
        }