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 AW-3 Dynamic Wah pedal
- // using CSCore to process the input and output signals in real-time
- using CSCore;
- using CSCore.CoreAudioAPI;
- using CSCore.DSP;
- using CSCore.Streams;
- using CSCore.Streams.Effects;
- using System;
- public class BossAW3DynamicWah
- {
- private IWaveSource _source;
- private BiQuadFilter _filter;
- private Delay _delay;
- private float _sensitivity;
- private float _resonance;
- private float _decay;
- private float _delayTime;
- private float _feedback;
- public BossAW3DynamicWah(IWaveSource source)
- {
- _source = source;
- _filter = BiQuadFilter.LowpassIIR(44100, 1000, 1);
- _delay = new Delay(44100);
- _sensitivity = 0.5f;
- _resonance = 0.5f;
- _decay = 0.5f;
- _delayTime = 0.5f;
- _feedback = 0.5f;
- }
- public float Sensitivity
- {
- get { return _sensitivity; }
- set { _sensitivity = value; }
- }
- public float Resonance
- {
- get { return _resonance; }
- set { _resonance = value; }
- }
- public float Decay
- {
- get { return _decay; }
- set { _decay = value; }
- }
- public float DelayTime
- {
- get { return _delayTime; }
- set { _delayTime = value; }
- }
- public float Feedback
- {
- get { return _feedback; }
- set { _feedback = value; }
- }
- public IWaveSource ToWaveSource()
- {
- return new EffectSource(_source)
- {
- SkipZeroSamples = true,
- Effect = new SignalDetectorEffect(_sensitivity, 1),
- Enabled = true
- }.AppendSource(x => new BiQuadFilterSource(x, _filter))
- .AppendSource(x => new DelaySource(x, _delay, _delayTime, _feedback))
- .AppendSource(x => new ResamplerSource(x, new WaveFormat(48000, 16, 2)))
- .ToWaveSource(16);
- }
- private class SignalDetectorEffect : DmoEffect
- {
- private readonly float _threshold;
- public SignalDetectorEffect(float threshold, int channels)
- {
- _threshold = threshold;
- Initialize(44100, 44100, channels, Dmo.Effect.WavesReverb);
- }
- public override unsafe void Process(ProcessArgs e)
- {
- var input = e.Input;
- var output = e.Output;
- var count = e.SampleCount;
- for (int i = 0; i < count; i++)
- {
- float sum = 0;
- for (int c = 0; c < input.Channels; c++)
- {
- sum += Math.Abs(input[c][i]);
- }
- if (sum / input.Channels > _threshold)
- {
- output.CopyTo(input, i);
- }
- else
- {
- output.Silence(input, i);
- }
- }
- }
- }
- }
- /*
- To use this class, you would first need to create an instance of it with an input
- wave source (such as a microphone or audio file). You can then adjust the various
- parameters of the effect (such as sensitivity, resonance, and delay time) and
- call the ToWaveSource() method to get a processed wave source that can be played
- or saved to a file. Here's an example of how you could use this class to process
- an input wave source and play the output in real-time:
- */
- var mmDevice = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
- var waveIn = new WasapiCapture(true, AudioClientShareMode.Shared, 100, new WaveFormat(44100, 16, 2), mmDevice);
- var bossAW3 = new BossAW3DynamicWah(waveIn.ToWaveSource());
- var waveOut = new WasapiOut(true, AudioClientShareMode.Shared, 100, new WaveFormat(44100, 16, 2), mmDevice);
- waveOut.Initialize(bossAW3.ToWaveSource());
- waveOut.Play();
- Console.ReadLine();
- waveOut.Stop();
- waveOut.Dispose();
- waveIn.Dispose();
- /*
- In this example, we first create an instance of the WasapiCapture class to
- capture audio from the default audio endpoint (specified by the MMDevice object).
- We then create an instance of the BossAW3DynamicWah class with the wave source
- from the WasapiCapture object, and initialize a WasapiOut object with the
- processed wave source from the BossAW3DynamicWah object. Finally, we start
- playing the output using the Play() method of the WasapiOut object, and wait
- for user input before stopping and disposing of the objects.
- Note that this is just one example of how you could use the BossAW3DynamicWah
- class to process audio in real-time using CSCore. You could also save the output
- to a file, apply the effect to pre-recorded audio, or modify the class to add
- more features or effects.
- */
- /*
- Here's an example of how you could use the BossAW3DynamicWah class with
- CSCore to process audio in real-time:
- */
- // Create an input wave source (in this case, the default microphone input)
- var microphone = new WasapiCapture(true, AudioClientShareMode.Shared, 100);
- // Create an instance of the BossAW3DynamicWah class
- var wah = new BossAW3DynamicWah(microphone.ToSampleSource());
- // Adjust the effect parameters as desired
- wah.Sensitivity = 0.7f;
- wah.Resonance = 0.4f;
- wah.Decay = 0.5f;
- wah.DelayTime = 0.2f;
- wah.Feedback = 0.5f;
- // Get a new wave source that applies the effect in real-time
- var output = wah.ToWaveSource();
- // Create an output wave source (in this case, the default audio output)
- var speaker = new WasapiOut();
- // Start playing the output wave source in real-time
- speaker.Initialize(output);
- speaker.Play();
- /*
- In this example, we first create an input wave source using WasapiCapture
- to capture audio from the default microphone input. We then create an instance
- of the BossAW3DynamicWah class and adjust its parameters as desired. Finally,
- we call ToWaveSource() to get a new wave source that applies the effect to the
- input in real-time, and initialize a WasapiOut object to play the output in
- real-time.
- Note that this is just a basic example, and there are many other ways you
- could use CSCore and the BossAW3DynamicWah class to process audio in real-time.
- Here's an example usage of the BossAW3DynamicWah class:
- */
- // Create an input wave source (in this case, a microphone capture)
- var capture = new WasapiCapture();
- capture.Initialize();
- // Create a BossAW3DynamicWah instance with the input wave source
- var effect = new BossAW3DynamicWah(capture);
- // Adjust the effect parameters as desired
- effect.Sensitivity = 0.75f;
- effect.Resonance = 0.25f;
- effect.Decay = 0.5f;
- effect.DelayTime = 0.25f;
- effect.Feedback = 0.5f;
- // Get a new wave source with the effect applied
- var output = effect.ToWaveSource();
- // Create an output wave writer (in this case, a WAV file)
- var writer = new WaveWriter("output.wav", output.WaveFormat);
- // Process the input and output signals in real-time
- capture.DataAvailable += (sender, e) =>
- {
- output.Write(e.Data, e.Offset, e.ByteCount);
- writer.Write(e.Data, e.Offset, e.ByteCount);
- };
- // Start capturing audio
- capture.Start();
- // Wait for user input to stop capturing audio and exit the program
- Console.ReadLine();
- // Stop capturing audio and dispose of resources
- capture.Stop();
- output.Dispose();
- writer.Dispose();
- /*
- In this example, the program captures audio from the default audio input
- device (such as a microphone), applies the BossAW3DynamicWah effect to the
- signal in real-time, and writes the processed audio to a WAV file. The program
- will continue running until the user presses a key, at which point it will stop
- capturing audio and exit.
- */
Advertisement
Add Comment
Please, Sign In to add comment