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 **
- ** **
- ***********************************************************
- ***********************************************************
- ** **
- ** -- Disclaimer: -- **
- ** **
- ** If code is used in private projects and/or for **
- ** study purposes please mention resource. Any other **
- ** use -whether commercial or non-commercial- is not **
- ** condoned by the author and violates his intent. **
- ** **
- ** [email protected] **
- ** **
- ***********************************************************
- ***********************************************************/
- using System;
- using System.Collections.Generic;
- using CSCore;
- using CSCore.Codecs.WAV;
- using CSCore.CoreAudioAPI;
- using CSCore.SoundIn;
- using CSCore.SoundOut;
- namespace BossXTortionEmulator
- {
- public class XTortionEmulator
- {
- private const int BufferSize = 4096;
- private readonly WaveIn waveIn;
- private readonly WasapiOut waveOut;
- private readonly byte[] inputBuffer;
- private readonly byte[] outputBuffer;
- private readonly List<short> distortionBuffer;
- private readonly double level;
- private readonly double punch;
- private readonly double contour;
- private readonly double distortion;
- public XTortionEmulator(double level, double punch, double contour, double distortion)
- {
- inputBuffer = new byte[BufferSize];
- outputBuffer = new byte[BufferSize];
- distortionBuffer = new List<short>();
- this.level = level;
- this.punch = punch;
- this.contour = contour;
- this.distortion = distortion;
- waveIn = new WaveInEvent();
- waveOut = new WasapiOut();
- waveIn.Initialize();
- waveIn.DataAvailable += WaveInOnDataAvailable;
- waveOut.Initialize(waveIn.WaveFormat);
- }
- public void Start()
- {
- waveIn.Start();
- waveOut.Play();
- }
- public void Stop()
- {
- waveIn.Stop();
- waveOut.Stop();
- }
- private void WaveInOnDataAvailable(object sender, WaveInEventArgs e)
- {
- int bytesRead = e.BytesRecorded;
- // Process input buffer
- for (int i = 0; i < bytesRead / 2; i++)
- {
- short sample = BitConverter.ToInt16(e.Buffer, i * 2);
- distortionBuffer.Add(sample);
- }
- // Apply distortion effect
- for (int i = 0; i < distortionBuffer.Count; i++)
- {
- short sample = distortionBuffer[i];
- // Apply level
- sample = (short)(sample * level);
- // Apply punch
- sample = (short)(sample * (1.0 + punch));
- // Apply contour
- double t = ((double)i / distortionBuffer.Count) * contour;
- sample = (short)(sample * (1.0 - t) + sample * t * t);
- // Apply distortion
- sample = (short)(sample * distortion);
- // Apply hard clipping
- if (sample > short.MaxValue)
- sample = short.MaxValue;
- else if (sample < short.MinValue)
- sample = short.MinValue;
- outputBuffer[i * 2] = (byte)(sample & 0xff);
- outputBuffer[i * 2 + 1] = (byte)(sample >> 8);
- }
- // Play output buffer
- waveOut.Write(outputBuffer, 0, bytesRead);
- // Clear distortion buffer
- distortionBuffer.Clear();
- }
- }
- public class Program
- {
- public static void Main(string[] args)
- {
- double level = 0.8; // Adjust the level parameter (0.0 to 1.0)
- double punch = 0.2; // Adjust the punch parameter (-1.0 to 1.0)
- double contour = 0.5; // Adjust the contour parameter (0.0 to 1.0)
- double distortion = 1.5; // Adjust the distortion parameter
- var emulator = new XTortionEmulator(level, punch, contour, distortion);
- emulator.Start();
- Console.WriteLine("Press any key to stop...");
- Console.ReadKey();
- emulator.Stop();
- }
- }
- }
- /*
- The `XTortionEmulator` class accepts four parameters: `level`, `punch`, `contour`, and `distortion`.
- These parameters control the specific characteristics of the distortion effect.
- - `level`: Controls the overall volume level of the output. Adjust the `level` parameter between 0.0
- and 1.0 to set the desired volume level.
- - `punch`: Adds punch or emphasis to the audio signal. Adjust the `punch` parameter between -1.0 and 1.0,
- where negative values reduce the punch and positive values increase it.
- - `contour`: Adjusts the contour or shape of the audio waveform. Adjust the `contour` parameter between
- 0.0 and 1.0 to set the desired contour effect.
- - `distortion`: Controls the amount of distortion applied to the audio. Adjust the `distortion` parameter
- to set the desired distortion level.
- In the `Main` method, you can modify these parameters to experiment with different settings.
- Feel free to adjust the values based on your preferences.
- Once you run the code, it will start emulating the Boss XTortion XT-2 pedal with the specified parameters.
- Press any key to stop the emulation.
- Remember to add the `CSCore` NuGet package to your project before running the code.
- */
Advertisement
Add Comment
Please, Sign In to add comment