Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Code Review: WasapiCapture.CaptureThread(AudioClient client)

$
0
0

Hey

I am studing the WasapiCapture class and I got a question about the CaptureThread().

 

 

    public class WasapiCapture : IWaveIn
    {
        private AudioClient audioClient;

        // Note the parameter
        public void StartRecording()
        {
            // ...
            ThreadStart start = delegate { this.CaptureThread(audioClient); };
            // ...
        }

        // client == audioClient
        // shouldn't the "audioClient" be replaced with the "client"
        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);
                }
            }
            finally
            {
                client.Stop();

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

            System.Diagnostics.Debug.WriteLine("stop wasapi");
        }
    }

As I understand the CaptureThread takes AudioClient as a parameter for encapsulation mean but once is used the class field "audioClient.BufferSize" instead of the local "client.BufferSize". Is there any specific reason for doing this or might it be it there unnoticed? Probably this will not cause any issue right now. I decided to mention it for the sake of code readability (i.e. understanding).

In any case I suggest to consider small change in the coding style. The class field names could use underscore prefix like "_audioClient". In this case you do not have to worry to mix local variables with fields or use "this" keyword. The "this" keyword in this project is redundant and in most cases it is safe to remove it (in 512 cases to be exact).

Just trying to be helpful for the sake of the good project.

 


Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>