Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************
- ** **
- ** Codesnippets written by Madwin for MMP 2023 **
- ** **
- ** Not to be used freely. the code still has errors **
- ** It's intended to be used as starting point. **
- ** Code has not been checked and might still need **
- ** (a lot of) work. **
- ** **
- ** © MadMedia Productions 2023 **
- ** **
- ***********************************************************/
- // C# class that emulates the Boss DD-500 digital delay and uses
- // CSCore to process live input and output in real-time:
- using CSCore;
- using CSCore.CoreAudioAPI;
- using CSCore.SoundIn;
- using CSCore.Streams;
- using System;
- class DDBoss500
- {
- private IWaveSource _delaySource;
- private ISoundIn _soundIn;
- private IWaveSource _finalSource;
- private readonly object _lockObject = new object();
- public void Start()
- {
- _soundIn = new WasapiLoopbackCapture();
- _soundIn.Initialize();
- _delaySource = new DelaySource(_soundIn.ToSampleSource());
- _finalSource = _delaySource.ToWaveSource();
- _soundIn.DataAvailable += SoundInOnDataAvailable;
- _soundIn.Start();
- }
- private void SoundInOnDataAvailable(object sender, DataAvailableEventArgs e)
- {
- lock (_lockObject)
- {
- _finalSource.Read(e.Data, e.Offset, e.ByteCount);
- }
- }
- public void Stop()
- {
- _soundIn.Stop();
- _soundIn.Dispose();
- _delaySource.Dispose();
- _finalSource.Dispose();
- }
- class DelaySource : SampleAggregatorBase
- {
- private readonly ISampleSource _source;
- private readonly CircularBuffer _buffer;
- private readonly int _delayInSamples = 44100 * 2; // 2 seconds delay
- private int _delayPosition;
- public DelaySource(ISampleSource source) : base(source)
- {
- _source = source;
- _buffer = new CircularBuffer(_delayInSamples * 2); // 2 channels
- }
- public override int Read(float[] buffer, int offset, int count)
- {
- var read = _source.Read(buffer, offset, count);
- for (var i = 0; i < read; i += 2)
- {
- _buffer.Write(buffer[i], buffer[i + 1]);
- buffer[i] = _buffer.Read(_delayPosition);
- buffer[i + 1] = _buffer.Read(_delayPosition + 1);
- _delayPosition = (_delayPosition + 2) % _buffer.Length;
- }
- return read;
- }
- }
- }
- /*
- This class creates an instance of the WasapiLoopbackCapture class from CSCore,
- which captures the audio output from the system. It then creates an instance of
- the DelaySource class, which is a custom ISampleSource that adds a delay to the
- audio stream. Finally, it connects the output of the delay source to the input
- of a WaveSource object, which is used to play the audio back through the system.
- The DelaySource class is where the magic happens. It keeps a circular buffer of
- audio samples, which it reads from and writes to in a loop. When it reads audio
- samples from the input, it writes them to the circular buffer and then reads
- samples from the buffer with a delay based on the _delayPosition variable.
- The delay position is incremented by 2 samples every time audio is read,
- so that the delay gradually increases over time.
- To use this class, you would simply create an instance of it
- and call its Start method:
- */
- var ddBoss500 = new DDBoss500();
- ddBoss500.Start();
- // And when you're done, call the Stop method to clean up:
- ddBoss500.Stop();
- /*
- Note that this is just a simple example and there are many ways
- you could customize this class to create different types of delays
- or add other effects.
- */
- ;l
Advertisement
Add Comment
Please, Sign In to add comment