Madwin74

Boss AW-3 Dynamic Wah

May 9th, 2023
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.29 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 AW-3 Dynamic Wah pedal
  21. //  using CSCore to process the input and output signals in real-time
  22.  
  23.  
  24.  
  25. using CSCore;
  26. using CSCore.CoreAudioAPI;
  27. using CSCore.DSP;
  28. using CSCore.Streams;
  29. using CSCore.Streams.Effects;
  30. using System;
  31.  
  32. public class BossAW3DynamicWah
  33. {
  34.     private IWaveSource _source;
  35.     private BiQuadFilter _filter;
  36.     private Delay _delay;
  37.     private float _sensitivity;
  38.     private float _resonance;
  39.     private float _decay;
  40.     private float _delayTime;
  41.     private float _feedback;
  42.  
  43.     public BossAW3DynamicWah(IWaveSource source)
  44.     {
  45.         _source = source;
  46.         _filter = BiQuadFilter.LowpassIIR(44100, 1000, 1);
  47.         _delay = new Delay(44100);
  48.         _sensitivity = 0.5f;
  49.         _resonance = 0.5f;
  50.         _decay = 0.5f;
  51.         _delayTime = 0.5f;
  52.         _feedback = 0.5f;
  53.     }
  54.  
  55.     public float Sensitivity
  56.     {
  57.         get { return _sensitivity; }
  58.         set { _sensitivity = value; }
  59.     }
  60.  
  61.     public float Resonance
  62.     {
  63.         get { return _resonance; }
  64.         set { _resonance = value; }
  65.     }
  66.  
  67.     public float Decay
  68.     {
  69.         get { return _decay; }
  70.         set { _decay = value; }
  71.     }
  72.  
  73.     public float DelayTime
  74.     {
  75.         get { return _delayTime; }
  76.         set { _delayTime = value; }
  77.     }
  78.  
  79.     public float Feedback
  80.     {
  81.         get { return _feedback; }
  82.         set { _feedback = value; }
  83.     }
  84.  
  85.     public IWaveSource ToWaveSource()
  86.     {
  87.         return new EffectSource(_source)
  88.         {
  89.             SkipZeroSamples = true,
  90.             Effect = new SignalDetectorEffect(_sensitivity, 1),
  91.             Enabled = true
  92.         }.AppendSource(x => new BiQuadFilterSource(x, _filter))
  93.         .AppendSource(x => new DelaySource(x, _delay, _delayTime, _feedback))
  94.         .AppendSource(x => new ResamplerSource(x, new WaveFormat(48000, 16, 2)))
  95.         .ToWaveSource(16);
  96.     }
  97.  
  98.     private class SignalDetectorEffect : DmoEffect
  99.     {
  100.         private readonly float _threshold;
  101.  
  102.         public SignalDetectorEffect(float threshold, int channels)
  103.         {
  104.             _threshold = threshold;
  105.             Initialize(44100, 44100, channels, Dmo.Effect.WavesReverb);
  106.         }
  107.  
  108.         public override unsafe void Process(ProcessArgs e)
  109.         {
  110.             var input = e.Input;
  111.             var output = e.Output;
  112.             var count = e.SampleCount;
  113.  
  114.             for (int i = 0; i < count; i++)
  115.             {
  116.                 float sum = 0;
  117.                 for (int c = 0; c < input.Channels; c++)
  118.                 {
  119.                     sum += Math.Abs(input[c][i]);
  120.                 }
  121.                 if (sum / input.Channels > _threshold)
  122.                 {
  123.                     output.CopyTo(input, i);
  124.                 }
  125.                 else
  126.                 {
  127.                     output.Silence(input, i);
  128.                 }
  129.             }
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135. /*
  136. To use this class, you would first need to create an instance of it with an input
  137. wave source (such as a microphone or audio file). You can then adjust the various
  138. parameters of the effect (such as sensitivity, resonance, and delay time) and
  139. call the ToWaveSource() method to get a processed wave source that can be played
  140. or saved to a file. Here's an example of how you could use this class to process
  141. an input wave source and play the output in real-time:
  142. */
  143.  
  144.  
  145. var mmDevice = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
  146. var waveIn = new WasapiCapture(true, AudioClientShareMode.Shared, 100, new WaveFormat(44100, 16, 2), mmDevice);
  147. var bossAW3 = new BossAW3DynamicWah(waveIn.ToWaveSource());
  148.  
  149. var waveOut = new WasapiOut(true, AudioClientShareMode.Shared, 100, new WaveFormat(44100, 16, 2), mmDevice);
  150. waveOut.Initialize(bossAW3.ToWaveSource());
  151. waveOut.Play();
  152.  
  153. Console.ReadLine();
  154.  
  155. waveOut.Stop();
  156. waveOut.Dispose();
  157. waveIn.Dispose();
  158.  
  159. /*
  160. In this example, we first create an instance of the WasapiCapture class to
  161. capture audio from the default audio endpoint (specified by the MMDevice object).
  162. We then create an instance of the BossAW3DynamicWah class with the wave source
  163. from the WasapiCapture object, and initialize a WasapiOut object with the
  164. processed wave source from the BossAW3DynamicWah object. Finally, we start
  165. playing the output using the Play() method of the WasapiOut object, and wait
  166. for user input before stopping and disposing of the objects.
  167.  
  168. Note that this is just one example of how you could use the BossAW3DynamicWah
  169. class to process audio in real-time using CSCore. You could also save the output
  170. to a file, apply the effect to pre-recorded audio, or modify the class to add
  171. more features or effects.
  172. */
  173.  
  174. /*
  175. Here's an example of how you could use the BossAW3DynamicWah class with
  176. CSCore to process audio in real-time:
  177. */
  178.  
  179.  
  180. // Create an input wave source (in this case, the default microphone input)
  181. var microphone = new WasapiCapture(true, AudioClientShareMode.Shared, 100);
  182.  
  183. // Create an instance of the BossAW3DynamicWah class
  184. var wah = new BossAW3DynamicWah(microphone.ToSampleSource());
  185.  
  186. // Adjust the effect parameters as desired
  187. wah.Sensitivity = 0.7f;
  188. wah.Resonance = 0.4f;
  189. wah.Decay = 0.5f;
  190. wah.DelayTime = 0.2f;
  191. wah.Feedback = 0.5f;
  192.  
  193. // Get a new wave source that applies the effect in real-time
  194. var output = wah.ToWaveSource();
  195.  
  196. // Create an output wave source (in this case, the default audio output)
  197. var speaker = new WasapiOut();
  198.  
  199. // Start playing the output wave source in real-time
  200. speaker.Initialize(output);
  201. speaker.Play();
  202.  
  203. /*
  204. In this example, we first create an input wave source using WasapiCapture
  205. to capture audio from the default microphone input. We then create an instance
  206. of the BossAW3DynamicWah class and adjust its parameters as desired. Finally,
  207. we call ToWaveSource() to get a new wave source that applies the effect to the
  208. input in real-time, and initialize a WasapiOut object to play the output in
  209. real-time.
  210.  
  211. Note that this is just a basic example, and there are many other ways you
  212. could use CSCore and the BossAW3DynamicWah class to process audio in real-time.
  213.  
  214. Here's an example usage of the BossAW3DynamicWah class:
  215. */
  216.  
  217.  
  218. // Create an input wave source (in this case, a microphone capture)
  219. var capture = new WasapiCapture();
  220. capture.Initialize();
  221.  
  222. // Create a BossAW3DynamicWah instance with the input wave source
  223. var effect = new BossAW3DynamicWah(capture);
  224.  
  225. // Adjust the effect parameters as desired
  226. effect.Sensitivity = 0.75f;
  227. effect.Resonance = 0.25f;
  228. effect.Decay = 0.5f;
  229. effect.DelayTime = 0.25f;
  230. effect.Feedback = 0.5f;
  231.  
  232. // Get a new wave source with the effect applied
  233. var output = effect.ToWaveSource();
  234.  
  235. // Create an output wave writer (in this case, a WAV file)
  236. var writer = new WaveWriter("output.wav", output.WaveFormat);
  237.  
  238. // Process the input and output signals in real-time
  239. capture.DataAvailable += (sender, e) =>
  240. {
  241.     output.Write(e.Data, e.Offset, e.ByteCount);
  242.     writer.Write(e.Data, e.Offset, e.ByteCount);
  243. };
  244.  
  245. // Start capturing audio
  246. capture.Start();
  247.  
  248. // Wait for user input to stop capturing audio and exit the program
  249. Console.ReadLine();
  250.  
  251. // Stop capturing audio and dispose of resources
  252. capture.Stop();
  253. output.Dispose();
  254. writer.Dispose();
  255.  
  256. /*
  257. In this example, the program captures audio from the default audio input
  258. device (such as a microphone), applies the BossAW3DynamicWah effect to the
  259. signal in real-time, and writes the processed audio to a WAV file. The program
  260. will continue running until the user presses a key, at which point it will stop
  261. capturing audio and exit.
  262. */
Advertisement
Add Comment
Please, Sign In to add comment