Madwin74

Xtortion Emulator

May 16th, 2023
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.47 KB | Source Code | 0 0
  1. /***********************************************************
  2.  ***********************************************************
  3.  **                                                       **
  4.  **   Codesnippets written by Madwin for MMP 2023         **
  5.  **                                                       **
  6.  **   Not to be used freely. the code still has errors    **
  7.  **   It's intended to be used as starting point.         **
  8.  **   Code has not been checked and might still need      **
  9.  **   (a lot of) work.                                    **
  10.  **                                                       **
  11.  **   ℗ & © MadMedia Productions 2023                     **
  12.  **                                                       **
  13.  ***********************************************************
  14.  ***********************************************************
  15.  **                                                       **
  16.  **   -- Disclaimer: --                                   **
  17.  **                                                       **
  18.  **   If code is used in private projects and/or for      **
  19.  **   study purposes please mention resource. Any other   **
  20.  **   use -whether commercial or non-commercial- is not   **
  21.  **   condoned by the author and violates his intent.     **
  22.  **                                                       **
  23.  **   [email protected]                             **
  24.  **                                                       **
  25.  ***********************************************************
  26.  ***********************************************************/
  27.  
  28.  
  29.  
  30. using System;
  31. using System.Collections.Generic;
  32. using CSCore;
  33. using CSCore.Codecs.WAV;
  34. using CSCore.CoreAudioAPI;
  35. using CSCore.SoundIn;
  36. using CSCore.SoundOut;
  37.  
  38. namespace BossXTortionEmulator
  39. {
  40.     public class XTortionEmulator
  41.     {
  42.         private const int BufferSize = 4096;
  43.         private readonly WaveIn waveIn;
  44.         private readonly WasapiOut waveOut;
  45.         private readonly byte[] inputBuffer;
  46.         private readonly byte[] outputBuffer;
  47.         private readonly List<short> distortionBuffer;
  48.         private readonly double level;
  49.         private readonly double punch;
  50.         private readonly double contour;
  51.         private readonly double distortion;
  52.  
  53.         public XTortionEmulator(double level, double punch, double contour, double distortion)
  54.         {
  55.             inputBuffer = new byte[BufferSize];
  56.             outputBuffer = new byte[BufferSize];
  57.             distortionBuffer = new List<short>();
  58.             this.level = level;
  59.             this.punch = punch;
  60.             this.contour = contour;
  61.             this.distortion = distortion;
  62.  
  63.             waveIn = new WaveInEvent();
  64.             waveOut = new WasapiOut();
  65.  
  66.             waveIn.Initialize();
  67.             waveIn.DataAvailable += WaveInOnDataAvailable;
  68.  
  69.             waveOut.Initialize(waveIn.WaveFormat);
  70.         }
  71.  
  72.         public void Start()
  73.         {
  74.             waveIn.Start();
  75.             waveOut.Play();
  76.         }
  77.  
  78.         public void Stop()
  79.         {
  80.             waveIn.Stop();
  81.             waveOut.Stop();
  82.         }
  83.  
  84.         private void WaveInOnDataAvailable(object sender, WaveInEventArgs e)
  85.         {
  86.             int bytesRead = e.BytesRecorded;
  87.  
  88.             // Process input buffer
  89.             for (int i = 0; i < bytesRead / 2; i++)
  90.             {
  91.                 short sample = BitConverter.ToInt16(e.Buffer, i * 2);
  92.                 distortionBuffer.Add(sample);
  93.             }
  94.  
  95.             // Apply distortion effect
  96.             for (int i = 0; i < distortionBuffer.Count; i++)
  97.             {
  98.                 short sample = distortionBuffer[i];
  99.  
  100.                 // Apply level
  101.                 sample = (short)(sample * level);
  102.  
  103.                 // Apply punch
  104.                 sample = (short)(sample * (1.0 + punch));
  105.  
  106.                 // Apply contour
  107.                 double t = ((double)i / distortionBuffer.Count) * contour;
  108.                 sample = (short)(sample * (1.0 - t) + sample * t * t);
  109.  
  110.                 // Apply distortion
  111.                 sample = (short)(sample * distortion);
  112.  
  113.                 // Apply hard clipping
  114.                 if (sample > short.MaxValue)
  115.                     sample = short.MaxValue;
  116.                 else if (sample < short.MinValue)
  117.                     sample = short.MinValue;
  118.  
  119.                 outputBuffer[i * 2] = (byte)(sample & 0xff);
  120.                 outputBuffer[i * 2 + 1] = (byte)(sample >> 8);
  121.             }
  122.  
  123.             // Play output buffer
  124.             waveOut.Write(outputBuffer, 0, bytesRead);
  125.  
  126.             // Clear distortion buffer
  127.             distortionBuffer.Clear();
  128.         }
  129.     }
  130.  
  131.     public class Program
  132.     {
  133.         public static void Main(string[] args)
  134.         {
  135.             double level = 0.8; // Adjust the level parameter (0.0 to 1.0)
  136.             double punch = 0.2; // Adjust the punch parameter (-1.0 to 1.0)
  137.             double contour = 0.5; // Adjust the contour parameter (0.0 to 1.0)
  138.             double distortion = 1.5; // Adjust the distortion parameter
  139.  
  140.             var emulator = new XTortionEmulator(level, punch, contour, distortion);
  141.             emulator.Start();
  142.  
  143.             Console.WriteLine("Press any key to stop...");
  144.             Console.ReadKey();
  145.             emulator.Stop();
  146.         }
  147.     }
  148.  
  149. }
  150.  
  151. /*
  152.  
  153. The `XTortionEmulator` class accepts four parameters: `level`, `punch`, `contour`, and `distortion`.
  154. These parameters control the specific characteristics of the distortion effect.
  155.  
  156. - `level`: Controls the overall volume level of the output. Adjust the `level` parameter between 0.0
  157.     and 1.0 to set the desired volume level.
  158. - `punch`: Adds punch or emphasis to the audio signal. Adjust the `punch` parameter between -1.0 and 1.0,
  159.     where negative values reduce the punch and positive values increase it.
  160. - `contour`: Adjusts the contour or shape of the audio waveform. Adjust the `contour` parameter between
  161.     0.0 and 1.0 to set the desired contour effect.
  162. - `distortion`: Controls the amount of distortion applied to the audio. Adjust the `distortion` parameter
  163.     to set the desired distortion level.
  164.  
  165. In the `Main` method, you can modify these parameters to experiment with different settings.
  166. Feel free to adjust the values based on your preferences.
  167.  
  168. Once you run the code, it will start emulating the Boss XTortion XT-2 pedal with the specified parameters.
  169. Press any key to stop the emulation.
  170.  
  171. Remember to add the `CSCore` NuGet package to your project before running the code.
  172.  
  173. */
Advertisement
Add Comment
Please, Sign In to add comment