Example #1
0
        public void Load(Uri uri)
        {
            Dispose();
            _uri        = uri;
            _waveSource = CSCore.Codecs.CodecFactory.Instance.GetCodec(_uri)
                          .ToSampleSource()
                          .ToMono()
                          .ToWaveSource();

            spectrumProvider = new BasicSpectrumProvider(_waveSource.WaveFormat.Channels,
                                                         _waveSource.WaveFormat.SampleRate,
                                                         CSCore.DSP.FftSize.Fft4096);

            //the SingleBlockNotificationStream is used to intercept the played samples
            var notificationSource = new SingleBlockNotificationStream(_waveSource.ToSampleSource());

            //pass the intercepted samples as input data to the spectrumprovider (which will calculate a fft based on them)
            notificationSource.SingleBlockRead += (s, a) => SpectrumProvider.Add(a.Left, a.Right);
            _waveSource = notificationSource.ToWaveSource(16);
            // Load the sample source
            var ws = CSCore.Codecs.CodecFactory.Instance.GetCodec(_uri);

            _sampleSource = ws.ToSampleSource();
            RaiseSourceEvent(SourceEventType.Loaded);
            hasMedia = true;
            LoadSoundOut();
        }
Example #2
0
        public void InitialiseAudioProgram()
        {
            _soundIn = new WasapiLoopbackCapture();
            _soundIn.Initialize();

            var           soundInSource = new SoundInSource(_soundIn);
            ISampleSource source        = soundInSource.ToSampleSource();

            var spectrumProvider = new SpectrumProvider(2, 48000, FftSize.Fft4096);

            _spectrum = new LineSpectrum(spectrumProvider, _barCount);
            var notificationSource = new SingleBlockNotificationStream(source);

            notificationSource.SingleBlockRead += (s, a) => spectrumProvider.Add(a.Left, a.Right);

            _source = notificationSource.ToWaveSource(16);

            // Read from the source otherwise SingleBlockRead is never called
            byte[] buffer = new byte[_source.WaveFormat.BytesPerSecond / 2];
            soundInSource.DataAvailable += (src, evt) =>
            {
                int read;
                while ((read = _source.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ;
                }
            };

            _soundIn.Start();

            for (int i = 0; i < MatrixCount; i++)
            {
                _Programs[i] = i == 0 ? AudioSequence().GetEnumerator() : null;
            }
        }
 private unsafe void ProcessFrameOutput(AudioFrame frame)
 {
     using (var buffer = frame.LockBuffer(AudioBufferAccessMode.Read))
         using (var reference = buffer.CreateReference())
         {
             // Get hold of the buffer pointer.
             ((IMemoryBufferByteAccess)reference).GetBuffer(out var dataInBytes, out var capacityInBytes);
             var dataInFloat = (float *)dataInBytes;
             for (var n = 0; n < _audioGraph.SamplesPerQuantum; n += 2)
             {
                 SpectrumProvider.Add(dataInFloat[n], dataInFloat[n + 1]);
             }
         }
 }
Example #4
0
        void SetupSampleSource(ISampleSource sampleSource)
        {
            FftSize fftSize = FftSize.Fft4096;

            SpectrumProvider spectrumProvider = new SpectrumProvider(sampleSource.WaveFormat.Channels,
                sampleSource.WaveFormat.SampleRate, fftSize);

            //spectrum = new BlobSpectrum(this, spectrumProvider, FftSize.Fft4096);
            spectrum = new RLSpectrum(this, spectrumProvider, FftSize.Fft4096);

            SingleBlockNotificationStream notificationSource = new SingleBlockNotificationStream(sampleSource);
            notificationSource.SingleBlockRead += (s, a) => spectrumProvider.Add(a.Left, a.Right);

            source = notificationSource.ToWaveSource(16);
        }
Example #5
0
        public void LoadFile(string filePath, int resoulution = 9, FftSize fftSize = FftSize.Fft256)
        {
            var source = CodecFactory.Instance.GetCodec(filePath).ToSampleSource();

            NotificationStream = new SingleBlockNotificationStream(source);
            NotificationStream.SingleBlockRead += (s, a) =>
            {
                SpectrumProvider.Add(a.Left, a.Right);
            };

            SpectrumProvider = new BasicSpectrumProvider(source.WaveFormat.Channels, source.WaveFormat.SampleRate, fftSize);
            Spectrum         = new SpectrumBase()
            {
                SpectrumProvider   = SpectrumProvider,
                SpectrumResolution = resoulution,
                MaximumFrequency   = 20000,
                MinimumFrequency   = 20,
                ScalingStrategy    = ScalingStrategy.Linear,
                UseAverage         = true,
            };
        }
Example #6
0
        public List <TimeSpectrumData> GenerateSpectrumDataNewWay(int stepDurationMs = 10, int smoothnessWindow = 3)
        {
            var streamLength = NotificationStream.Length;
            var samples = new float[32 * 1024];
            int samplesRead = -1, totalSamplesRead = 0;

            var result = new List <TimeSpectrumData>();

            NotificationStream.Position = 0;
            while (NotificationStream.Position < streamLength)
            {
                var lastTimeRead = NotificationStream.GetMilliseconds(NotificationStream.Position);

                while (NotificationStream.GetMilliseconds(NotificationStream.Position) - lastTimeRead < stepDurationMs & samplesRead != 0)
                {
                    samplesRead = NotificationStream.Read(samples, 0, samples.Length);
                }

                for (int i = 0; i < samplesRead; i += NotificationStream.WaveFormat.Channels)
                {
                    SpectrumProvider.Add(samples[i], NotificationStream.WaveFormat.Channels > 1 ? samples[i + 1] : 0);

                    if (NotificationStream.GetMilliseconds(totalSamplesRead + i) - lastTimeRead >= stepDurationMs)
                    {
                        result.Add(GetTimeSpectrumData());
                        lastTimeRead = NotificationStream.GetMilliseconds(totalSamplesRead + i);
                    }
                }

                totalSamplesRead += samplesRead;
                if (samplesRead == 0)
                {
                    break;
                }
            }

            SmoothDataAcrossTime(result, smoothnessWindow);
            return(result);
        }