コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: rustforfuture/Wale
        private void Equal()
        {
            //CSCore.SoundOut.WaveOutDevice.DefaultDevice;
            const string    filename   = @"C:\Temp\test.mp3";
            EventWaitHandle waitHandle = new AutoResetEvent(false);

            try
            {
                //create a source which provides audio data
                using (var source = CodecFactory.Instance.GetCodec(filename))
                {
                    //create the equalizer.
                    //You can create a custom eq with any bands you want, or you can just use the default 10 band eq.
                    Equalizer equalizer = Equalizer.Create10BandEqualizer(FluentExtensions.ToSampleSource(source));

                    //create a soundout to play the source
                    ISoundOut soundOut;
                    if (WasapiOut.IsSupportedOnCurrentPlatform)
                    {
                        soundOut = new WasapiOut();
                    }
                    else
                    {
                        soundOut = new DirectSoundOut();
                    }

                    soundOut.Stopped += (s, e) => waitHandle.Set();

                    IWaveSource finalSource = equalizer.ToWaveSource(16); //since the equalizer is a samplesource, you have to convert it to a raw wavesource
                    soundOut.Initialize(finalSource);                     //initialize the soundOut with the previously created finalSource
                    soundOut.Play();

                    /*
                     * You can change the filter configuration of the equalizer at any time.
                     */
                    equalizer.SampleFilters[0].AverageGainDB = 20; //eq set the gain of the first filter to 20dB (if needed, you can set the gain value for each channel of the source individually)

                    //wait until the playback finished
                    //of course that is optional
                    waitHandle.WaitOne();

                    //remember to dispose and the soundout and the source
                    soundOut.Dispose();
                }
            }
            catch (NotSupportedException ex)
            {
                Console.WriteLine("Fileformat not supported: " + ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unexpected exception: " + ex.Message);
            }
        }
コード例 #2
0
        public AudioProcessor(Publisher publisher, string outlet = null)
        {
            using WasapiCapture capture = new WasapiLoopbackCapture(CAPTURE_LATENCY);
            capture.Initialize();
            channelNum       = capture.WaveFormat.Channels;
            systemSampleRate = capture.WaveFormat.SampleRate;

            using SoundInSource captureSource =
                      new SoundInSource(capture)
                  {
                      FillWithZeros = false
                  };
            using SimpleNotificationSource notificationSource =
                      new SimpleNotificationSource(FluentExtensions.ToSampleSource(captureSource))
                  {
                      Interval = PROCESS_WINDOW_LENGTH
                  };

            InitializeMonoBuffers(monoBuffers, channelNum, notificationSource.BlockCount);
            blockBuffer       = new float[notificationSource.BlockCount * channelNum];
            lpf               = new LowpassFilter(systemSampleRate, LFE_CUTOFF);
            MonoPulseDetector =
                new SimplePulseDetector(monoBuffers, lfeProvided: false, biQuadFilter: lpf);
            localisationer = new Localisationer(monoBuffers);
            if (channelNum > 2)
            {
                LFEPulseDetector =
                    new SimplePulseDetector(monoBuffers, lfeProvided: true);
            }

            capture.DataAvailable += (s, e) =>
            {
                while (notificationSource.Read(blockBuffer, 0, notificationSource.BlockCount * channelNum) > 0)
                {
                    monoBuffers = Deinterlacing(monoBuffers,
                                                blockBuffer,
                                                channelNum);
                    if (LFEPulseDetector != null)
                    {
                        bool m = MonoPulseDetector.Predict();
                        bool l = LFEPulseDetector.Predict();
                        if (m || l)
                        {
                            double angle = localisationer.GetLoudestAngle();
#if DEBUG
                            Console.Clear();
                            Console.WriteLine($"LFE Level: {LFEPulseDetector.CurrentReading:F3}, LFE Threshold: {LFEPulseDetector.CurrentThreshold:F3}");
                            Console.WriteLine($"Mixed Level: {MonoPulseDetector.CurrentReading:F3}, Mixed Threshold: {MonoPulseDetector.CurrentThreshold:F3}");
                            Console.WriteLine($"Impulse Detected - Mono:{m}, LFE:{l}, Angle: {angle:F3}, Hit Count:{hitCount}");
#endif
                            if (publisher != null && outlet != null)
                            {
                                publisher.Publish(outlet, $"{m}|{l}|{angle:F3}");
                            }

                            hitCount++;
                        }
                    }
                    else
                    {
                        if (MonoPulseDetector.Predict())
                        {
                            double angle = localisationer.GetLoudestAngle();
#if DEBUG
                            Console.Clear();
                            Console.WriteLine($"Level: {MonoPulseDetector.CurrentReading:F3}, Threshold: {MonoPulseDetector.CurrentThreshold:F3}");
                            Console.WriteLine($"Impulse Detected - Mono, Angle:{angle:F3}, Hit Count:{hitCount}");
#endif
                            if (publisher != null && outlet != null)
                            {
                                publisher.Publish(outlet, $"True|False|{angle:F3}");
                            }

                            hitCount++;
                        }
                    }
                }
            };

            StartCapturingAndHold(capture);
        }
コード例 #3
0
        public AudioProcessor(Publisher publisher, string outlet = null)
        {
            WasapiCapture capture = new WasapiLoopbackCapture(CAPTURE_LATENCY);

            capture.Initialize();
            channelNum       = capture.WaveFormat.Channels;
            systemSampleRate = capture.WaveFormat.SampleRate;

            SoundInSource captureSource =
                new SoundInSource(capture)
            {
                FillWithZeros = false
            };
            SimpleNotificationSource notificationSource =
                new SimpleNotificationSource(FluentExtensions.ToSampleSource(captureSource))
            {
                Interval = PROCESS_WINDOW_LENGTH
            };

            InitializeMonoBuffers(pcmBuffers, channelNum, notificationSource.BlockCount);
            blockBuffer = new float[notificationSource.BlockCount * channelNum];

            capture.DataAvailable += (s, e) =>
            {
                while (notificationSource.Read(blockBuffer, 0, notificationSource.BlockCount * channelNum) > 0)
                {
                    // Extracted audio signal
                    pcmBuffers = Deinterlacing(pcmBuffers,
                                               blockBuffer,
                                               channelNum);

                    CSCore.Utils.Complex[] data = new CSCore.Utils.Complex[pcmBuffers[0].Length];


                    for (int i = 0; i < pcmBuffers.Count; i++)
                    {
                        data[i].Real      = pcmBuffers[0][i];
                        data[i].Imaginary = 0;
                    }

                    //DetectPitch(pcmBuffers);

                    counter++;
                    if (counter % 30 == 0)
                    {
                        Console.Clear();
                        FastFourierTransformation.Fft(data, 8);

                        //Console.WriteLine(data.Length);
                        foreach (var d in data)
                        {
                            Console.Write(Math.Round(d.Value, 4) + " ");
                        }
                        Console.WriteLine();
                    }


                    // TODO: Implement your model
                    //publisher.Publish(outlet, "OUTPUT MESSAGE TO NEXT OUTLET");
                }
            };

            StartCapturingAndHold(capture);
        }