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 EH-2 enhancer and processes
- // the signal in real-time using the CSCore library:
- csharp
- using CSCore;
- using CSCore.CoreAudioAPI;
- using CSCore.DSP;
- using CSCore.Streams;
- using System;
- public class BossEH2Enhancer : IDisposable
- {
- private readonly IWaveSource _source;
- private readonly IWaveSource _processedSource;
- private readonly Single[] _inputBuffer;
- private readonly Single[] _outputBuffer;
- private readonly BiQuadFilter _lowPassFilter;
- private readonly BiQuadFilter _highPassFilter;
- private readonly Equalizer _equalizer;
- private readonly Single _gain;
- public BossEH2Enhancer(IWaveSource source, Single lowCutoff, Single highCutoff, EqualizerBand[] equalizerBands, Single gain)
- {
- _source = source;
- _lowPassFilter = BiQuadFilter.LowpassFilter(source.WaveFormat.SampleRate, lowCutoff, 0.707f);
- _highPassFilter = BiQuadFilter.HighpassFilter(source.WaveFormat.SampleRate, highCutoff, 0.707f);
- _equalizer = new Equalizer(source.WaveFormat.SampleRate, equalizerBands);
- _gain = gain;
- _inputBuffer = new Single[source.WaveFormat.BytesToSamples(source.WaveFormat.WaveFormatBytes)];
- _outputBuffer = new Single[_inputBuffer.Length];
- _processedSource = new SingleBlockNotificationStream(new WaveToSampleProvider(new SingleChannelToMonoSource(new SoundTouchWaveSource(new WdlResamplingSampleSource(source.ToSampleSource()), SoundTouch.DefaultConfig())))), Process);
- }
- public IWaveSource ProcessedSource
- {
- get { return _processedSource; }
- }
- private void Process(ReadContext context)
- {
- int samplesRead = _source.Read(_inputBuffer, 0, _inputBuffer.Length);
- if (samplesRead == 0)
- {
- context.Completed();
- return;
- }
- for (int i = 0; i < samplesRead; i++)
- {
- Single sample = _inputBuffer[i];
- // Apply the high-pass filter
- sample = _highPassFilter.Process(sample);
- // Apply the low-pass filter
- sample = _lowPassFilter.Process(sample);
- // Apply the equalizer
- sample = _equalizer.Process(sample);
- // Apply the gain
- sample *= _gain;
- _outputBuffer[i] = sample;
- }
- context.Write(_outputBuffer, 0, samplesRead);
- }
- public void Dispose()
- {
- _processedSource.Dispose();
- _source.Dispose();
- }
- }
- /*
- To use this class, you can create an instance of it with the appropriate
- parameters, like this:
- */
- IWaveSource source = new WaveFileReader("input.wav");
- EqualizerBand[] equalizerBands = new[]
- {
- new EqualizerBand { Frequency = 100, GainDB = -3 },
- new EqualizerBand { Frequency = 1000, GainDB = 3 },
- new EqualizerBand { Frequency = 4000, GainDB = -3 },
- new EqualizerBand { Frequency = 16000, GainDB = 6 }
- };
- BossEH2Enhancer enhancer = new BossEH2Enhancer(source, 200, 8000, equalizerBands, 1.5f);
- IWaveSource processedSource = enhancer.ProcessedSource;
- /*
- This code creates an instance of WaveFileReader to read an input WAV file,
- creates an array of EqualizerBand bands to define the equalizer settings,
- creates an instance of BossEH2Enhancer with the appropriate parameters, and
- retrieves the processed output source using the ProcessedSource property.
- The BossEH2Enhancer class works by processing the input source data in real-time
- as it is read, applying a high-pass filter, low-pass filter, equalizer, and gain
- to each sample. The processed data is then written to the output buffer and
- passed back to the caller.
- You can then use the processedSource object to play or save the processed audio
- data as needed. Don't forget to call Dispose() on the enhancer object when you
- are finished with it to clean up any resources it has allocated.
- Note that this code assumes that you have added the CSCore library to your project
- and have included the necessary using statements at the top of your code file.
- */
Advertisement
Add Comment
Please, Sign In to add comment