Madwin74

Boss Enhancer

May 9th, 2023
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 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. //  C# class that emulates the BOSS EH-2 enhancer and processes
  18. //  the signal in real-time using the CSCore library:
  19.  
  20. csharp
  21.  
  22. using CSCore;
  23. using CSCore.CoreAudioAPI;
  24. using CSCore.DSP;
  25. using CSCore.Streams;
  26. using System;
  27.  
  28. public class BossEH2Enhancer : IDisposable
  29. {
  30.     private readonly IWaveSource _source;
  31.     private readonly IWaveSource _processedSource;
  32.  
  33.     private readonly Single[] _inputBuffer;
  34.     private readonly Single[] _outputBuffer;
  35.  
  36.     private readonly BiQuadFilter _lowPassFilter;
  37.     private readonly BiQuadFilter _highPassFilter;
  38.     private readonly Equalizer _equalizer;
  39.  
  40.     private readonly Single _gain;
  41.  
  42.     public BossEH2Enhancer(IWaveSource source, Single lowCutoff, Single highCutoff, EqualizerBand[] equalizerBands, Single gain)
  43.     {
  44.         _source = source;
  45.  
  46.         _lowPassFilter = BiQuadFilter.LowpassFilter(source.WaveFormat.SampleRate, lowCutoff, 0.707f);
  47.         _highPassFilter = BiQuadFilter.HighpassFilter(source.WaveFormat.SampleRate, highCutoff, 0.707f);
  48.         _equalizer = new Equalizer(source.WaveFormat.SampleRate, equalizerBands);
  49.  
  50.         _gain = gain;
  51.  
  52.         _inputBuffer = new Single[source.WaveFormat.BytesToSamples(source.WaveFormat.WaveFormatBytes)];
  53.         _outputBuffer = new Single[_inputBuffer.Length];
  54.  
  55.         _processedSource = new SingleBlockNotificationStream(new WaveToSampleProvider(new SingleChannelToMonoSource(new SoundTouchWaveSource(new WdlResamplingSampleSource(source.ToSampleSource()), SoundTouch.DefaultConfig())))), Process);
  56.     }
  57.  
  58.     public IWaveSource ProcessedSource
  59.     {
  60.         get { return _processedSource; }
  61.     }
  62.  
  63.     private void Process(ReadContext context)
  64.     {
  65.         int samplesRead = _source.Read(_inputBuffer, 0, _inputBuffer.Length);
  66.  
  67.         if (samplesRead == 0)
  68.         {
  69.             context.Completed();
  70.             return;
  71.         }
  72.  
  73.         for (int i = 0; i < samplesRead; i++)
  74.         {
  75.             Single sample = _inputBuffer[i];
  76.  
  77.             // Apply the high-pass filter
  78.             sample = _highPassFilter.Process(sample);
  79.  
  80.             // Apply the low-pass filter
  81.             sample = _lowPassFilter.Process(sample);
  82.  
  83.             // Apply the equalizer
  84.             sample = _equalizer.Process(sample);
  85.  
  86.             // Apply the gain
  87.             sample *= _gain;
  88.  
  89.             _outputBuffer[i] = sample;
  90.         }
  91.  
  92.         context.Write(_outputBuffer, 0, samplesRead);
  93.     }
  94.  
  95.     public void Dispose()
  96.     {
  97.         _processedSource.Dispose();
  98.         _source.Dispose();
  99.     }
  100. }
  101.  
  102.  
  103. /*
  104. To use this class, you can create an instance of it with the appropriate
  105. parameters, like this:
  106. */
  107.  
  108.  
  109. IWaveSource source = new WaveFileReader("input.wav");
  110. EqualizerBand[] equalizerBands = new[]
  111. {
  112.     new EqualizerBand { Frequency = 100, GainDB = -3 },
  113.     new EqualizerBand { Frequency = 1000, GainDB = 3 },
  114.     new EqualizerBand { Frequency = 4000, GainDB = -3 },
  115.     new EqualizerBand { Frequency = 16000, GainDB = 6 }
  116. };
  117.  
  118. BossEH2Enhancer enhancer = new BossEH2Enhancer(source, 200, 8000, equalizerBands, 1.5f);
  119. IWaveSource processedSource = enhancer.ProcessedSource;
  120.  
  121.  
  122. /*
  123. This code creates an instance of WaveFileReader to read an input WAV file,
  124. creates an array of EqualizerBand bands to define the equalizer settings,
  125. creates an instance of BossEH2Enhancer with the appropriate parameters, and
  126. retrieves the processed output source using the ProcessedSource property.
  127.  
  128. The BossEH2Enhancer class works by processing the input source data in real-time
  129. as it is read, applying a high-pass filter, low-pass filter, equalizer, and gain
  130. to each sample. The processed data is then written to the output buffer and
  131. passed back to the caller.
  132.  
  133. You can then use the processedSource object to play or save the processed audio
  134. data as needed. Don't forget to call Dispose() on the enhancer object when you
  135. are finished with it to clean up any resources it has allocated.
  136.  
  137. Note that this code assumes that you have added the CSCore library to your project
  138. and have included the necessary using statements at the top of your code file.
  139. */
Advertisement
Add Comment
Please, Sign In to add comment