Madwin74

Boss DD500 Digital Delay

May 9th, 2023
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.06 KB | Source Code | 0 0
  1. /***********************************************************
  2.  **                                                       **
  3.  **   Codesnippets written by Madwin for MMP 2023         **
  4.  **                                                       **
  5.  **   Not to be used freely. the code still has errors    **
  6.  **   It's intended to be used as starting point.         **
  7.  **   Code has not been checked and might still need      **
  8.  **   (a lot of) work.                                    **
  9.  **                                                       **
  10.  **   © MadMedia Productions 2023                         **
  11.  **                                                       **
  12.  ***********************************************************/
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. //  C# class that emulates the Boss DD-500 digital delay and uses
  21. //  CSCore to process live input and output in real-time:
  22.  
  23.  
  24.  
  25. using CSCore;
  26. using CSCore.CoreAudioAPI;
  27. using CSCore.SoundIn;
  28. using CSCore.Streams;
  29. using System;
  30.  
  31. class DDBoss500
  32. {
  33.     private IWaveSource _delaySource;
  34.     private ISoundIn _soundIn;
  35.     private IWaveSource _finalSource;
  36.     private readonly object _lockObject = new object();
  37.  
  38.     public void Start()
  39.     {
  40.         _soundIn = new WasapiLoopbackCapture();
  41.         _soundIn.Initialize();
  42.         _delaySource = new DelaySource(_soundIn.ToSampleSource());
  43.         _finalSource = _delaySource.ToWaveSource();
  44.         _soundIn.DataAvailable += SoundInOnDataAvailable;
  45.         _soundIn.Start();
  46.     }
  47.  
  48.     private void SoundInOnDataAvailable(object sender, DataAvailableEventArgs e)
  49.     {
  50.         lock (_lockObject)
  51.         {
  52.             _finalSource.Read(e.Data, e.Offset, e.ByteCount);
  53.         }
  54.     }
  55.  
  56.     public void Stop()
  57.     {
  58.         _soundIn.Stop();
  59.         _soundIn.Dispose();
  60.         _delaySource.Dispose();
  61.         _finalSource.Dispose();
  62.     }
  63.  
  64.     class DelaySource : SampleAggregatorBase
  65.     {
  66.         private readonly ISampleSource _source;
  67.         private readonly CircularBuffer _buffer;
  68.         private readonly int _delayInSamples = 44100 * 2; // 2 seconds delay
  69.         private int _delayPosition;
  70.  
  71.         public DelaySource(ISampleSource source) : base(source)
  72.         {
  73.             _source = source;
  74.             _buffer = new CircularBuffer(_delayInSamples * 2); // 2 channels
  75.         }
  76.  
  77.         public override int Read(float[] buffer, int offset, int count)
  78.         {
  79.             var read = _source.Read(buffer, offset, count);
  80.  
  81.             for (var i = 0; i < read; i += 2)
  82.             {
  83.                 _buffer.Write(buffer[i], buffer[i + 1]);
  84.                 buffer[i] = _buffer.Read(_delayPosition);
  85.                 buffer[i + 1] = _buffer.Read(_delayPosition + 1);
  86.                 _delayPosition = (_delayPosition + 2) % _buffer.Length;
  87.             }
  88.  
  89.             return read;
  90.         }
  91.     }
  92. }
  93.  
  94. /*
  95. This class creates an instance of the WasapiLoopbackCapture class from CSCore,
  96. which captures the audio output from the system. It then creates an instance of
  97. the DelaySource class, which is a custom ISampleSource that adds a delay to the
  98. audio stream. Finally, it connects the output of the delay source to the input
  99. of a WaveSource object, which is used to play the audio back through the system.
  100.  
  101. The DelaySource class is where the magic happens. It keeps a circular buffer of
  102. audio samples, which it reads from and writes to in a loop. When it reads audio
  103. samples from the input, it writes them to the circular buffer and then reads
  104. samples from the buffer with a delay based on the _delayPosition variable.
  105. The delay position is incremented by 2 samples every time audio is read,
  106. so that the delay gradually increases over time.
  107.  
  108. To use this class, you would simply create an instance of it
  109. and call its Start method:
  110. */
  111.  
  112.  
  113. var ddBoss500 = new DDBoss500();
  114. ddBoss500.Start();
  115.  
  116. // And when you're done, call the Stop method to clean up:
  117.  
  118.  
  119.  
  120. ddBoss500.Stop();
  121.  
  122. /*
  123. Note that this is just a simple example and there are many ways
  124. you could customize this class to create different types of delays
  125. or add other effects.
  126. */
  127. ;l
Advertisement
Add Comment
Please, Sign In to add comment