본문으로 바로가기

파일의 IT 블로그

  1. Home
  2. 프로그래밍/C#
  3. [C#] NAUDIO 로 마이크 볼륨레벨 가져오기

[C#] NAUDIO 로 마이크 볼륨레벨 가져오기

· 댓글개 · KRFile

 

            var waveIn = new NAudio.Wave.WaveInEvent();
            waveIn.DeviceNumber = 0;
            waveIn.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1);
            waveIn.DataAvailable += WaveIn_DataAvailable;
            waveIn.StartRecording();

 

private void WaveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            //if (isRecording)
            //{
            //    writer.Write(args.Buffer, 0, args.BytesRecorded);
            //}

            float max = 0;
            // interpret as 16 bit audio
            for (int index = 0; index < e.BytesRecorded; index += 2)
            {
                short sample = (short)((e.Buffer[index + 1] << 8) |
                                        e.Buffer[index + 0]);
                // to floating point
                var sample32 = sample / 32768f;
                // absolute value 
                if (sample32 < 0) sample32 = -sample32;
                // is this the max value?
                if (sample32 > max) max = sample32;
            }
            float level = 100 * max;
            this.Invoke(new Action(
                 delegate ()
                 {
                     progressBar1.Value = Convert.ToInt32(level);
                 }));
        }

max는 0.0~1.0의 실수값이다.

 

참고

https://github.com/naudio/NAudio/blob/master/Docs/RecordingLevelMeter.md

 

naudio/NAudio

Audio and MIDI library for .NET. Contribute to naudio/NAudio development by creating an account on GitHub.

github.com

 

SNS 공유하기
💬 댓글 개
이모티콘창 닫기
울음
안녕
감사해요
당황
피폐

이모티콘을 클릭하면 댓글창에 입력됩니다.