Esempio n. 1
0
 public AudioCaptureAgent()
 {
     _capture   = new NAudio.CoreAudioApi.WasapiLoopbackCapture();
     _processor = new AudioCaptureProcessor();
     _capture.RecordingStopped += AudioCaptureProcessor.handleEndOfAudio;
     _capture.DataAvailable    += _processor.handleAudioData;
     _capture.StartRecording();
 }
Esempio n. 2
0
        public void handleAudioData(object sender, WaveInEventArgs args)
        {
            NAudio.CoreAudioApi.WasapiLoopbackCapture capture = (NAudio.CoreAudioApi.WasapiLoopbackCapture)sender;

            //left channel = first 4 bytes;
            //right channel = next 4;
            for (int i = 0; i < args.BytesRecorded; i += 8)
            {
                if (currentIdx < buffer.Length)
                {
                    float left     = BitConverter.ToSingle(args.Buffer, i);
                    float right    = BitConverter.ToSingle(args.Buffer, i + 4);
                    float combined = (left + right) / 2;
                    buffer[currentIdx++] = new System.Numerics.Complex(combined, 0);
                }
                else
                {
                    currentIdx = 0;
                    int sampleRate = capture.WaveFormat.SampleRate;
                    Fourier.Radix2Forward(buffer, FourierOptions.Default);
                    double BASS  = 0;
                    double MIDS  = 0;
                    double TREBS = 0;

                    for (int m = 0; m < buffer.Length; m++)
                    {
                        double f_m = m / (buffer.Length / (double)sampleRate);

                        if (f_m < vlb)
                        {
                            BASS += buffer[m].Magnitude;
                        }
                        else if (f_m < mb)
                        {
                            BASS += buffer[m].Magnitude;
                        }
                        else if (f_m < hb)
                        {
                            BASS += buffer[m].Magnitude;
                        }
                        else if (f_m < lmr)
                        {
                            MIDS += buffer[m].Magnitude;
                        }
                        else if (f_m < mmr)
                        {
                            MIDS += buffer[m].Magnitude;
                        }
                        else if (f_m < hmr)
                        {
                            MIDS += buffer[m].Magnitude;
                        }
                        else if (f_m < tr)
                        {
                            TREBS += buffer[m].Magnitude;
                        }
                        else
                        {
                            break;
                        }
                    }

                    //if (FrequencyDataAvailable != null)
                    //{
                    //    FrequencyDataAvailable(this, new FrequencyEventArgs(new FrequencyInfo(BASS, MIDS, TREBS)));
                    //}
                }
            }
        }