예제 #1
0
 public void Dispose()
 {
     if (this.audioClientInterface != null)
     {
         if (this.audioClockClient != null)
         {
             this.audioClockClient.Dispose();
             this.audioClockClient = null;
         }
         if (this.audioRenderClient != null)
         {
             this.audioRenderClient.Dispose();
             this.audioRenderClient = null;
         }
         if (this.audioCaptureClient != null)
         {
             this.audioCaptureClient.Dispose();
             this.audioCaptureClient = null;
         }
         if (this.audioStreamVolume != null)
         {
             this.audioStreamVolume.Dispose();
             this.audioStreamVolume = null;
         }
         Marshal.ReleaseComObject(this.audioClientInterface);
         this.audioClientInterface = null;
         GC.SuppressFinalize(this);
     }
 }
예제 #2
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)
            {
                IntPtr buffer = capture.GetBuffer(out int framesAvailable, out AudioClientBufferFlags 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)
                {
                    DataAvailable?.Invoke(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();
            }
            DataAvailable?.Invoke(this, new WaveInEventArgs(recordBuffer, recordBufferOffset));
        }