Madwin74

10 Band EQ

May 7th, 2023 (edited)
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.59 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. //  10-band equalizer with a range of 20Hz to 20KHz in C# using NAudio
  16. //  with a stream as source and real-time processing:
  17.  
  18.  
  19.  
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25. using NAudio.Wave;
  26.  
  27. namespace TenBandEqualizer
  28. {
  29.     class Program
  30.     {
  31.         static void Main(string[] args)
  32.         {
  33.             // Create an input stream from a file or other source
  34.             WaveStream inputStream = new WaveFileReader("input.wav");
  35.  
  36.             // Create a 10-band equalizer with a range of 20Hz to 20KHz
  37.             Equalizer equalizer = new Equalizer(inputStream.WaveFormat.SampleRate, inputStream.WaveFormat.Channels, 10, 20, 20000);
  38.  
  39.             // Create a source stream for the equalizer
  40.             ISampleProvider source = inputStream.ToSampleProvider();
  41.  
  42.             // Connect the equalizer to the source stream
  43.             equalizer.SetSource(source);
  44.  
  45.             // Create an output stream to write the equalized audio to
  46.             WaveFileWriter outputWriter = new WaveFileWriter("output.wav", equalizer.WaveFormat);
  47.  
  48.             // Create a buffer to hold the equalized audio data
  49.             byte[] buffer = new byte[equalizer.WaveFormat.AverageBytesPerSecond / 4];
  50.  
  51.             // Loop through the input stream and process the audio in real-time
  52.             while (inputStream.Position < inputStream.Length)
  53.             {
  54.                 int bytesRead = inputStream.Read(buffer, 0, buffer.Length);
  55.  
  56.                 // Process the audio through the equalizer
  57.                 int samples = bytesRead / sizeof(float);
  58.                 equalizer.Process(buffer, samples);
  59.  
  60.                 // Write the equalized audio to the output stream
  61.                 outputWriter.Write(buffer, 0, bytesRead);
  62.             }
  63.  
  64.             // Clean up resources
  65.             outputWriter.Dispose();
  66.             inputStream.Dispose();
  67.             equalizer.Dispose();
  68.         }
  69.     }
  70.  
  71.     class Equalizer : ISampleProvider, IDisposable
  72.     {
  73.         private readonly BiQuadFilter[,] filters;
  74.         private readonly ISampleProvider source;
  75.  
  76.         public WaveFormat WaveFormat { get { return source.WaveFormat; } }
  77.  
  78.         public Equalizer(int sampleRate, int channels, int bands, float minFreq, float maxFreq)
  79.         {
  80.             filters = new BiQuadFilter[bands, channels];
  81.             source = null;
  82.  
  83.             // Calculate the center frequencies of each band
  84.             float[] centerFreqs = new float[bands];
  85.             float logMin = (float)Math.Log10(minFreq);
  86.             float logMax = (float)Math.Log10(maxFreq);
  87.             float delta = (logMax - logMin) / (bands - 1);
  88.             for (int i = 0; i < bands; i++)
  89.             {
  90.                 float logFreq = logMin + i * delta;
  91.                 centerFreqs[i] = (float)Math.Pow(10, logFreq);
  92.             }
  93.  
  94.             // Create a filter for each band and channel
  95.             for (int i = 0; i < bands; i++)
  96.             {
  97.                 for (int j = 0; j < channels; j++)
  98.                 {
  99.                     filters[i, j] = BiQuadFilter.PeakingEQ(sampleRate, centerFreqs[i], 0.5f, 0);
  100.                 }
  101.             }
  102.         }
  103.  
  104.         public void SetSource(ISampleProvider source)
  105.         {
  106.             this.source = source;
  107.         }
  108.  
  109.         public int Read(float[] buffer, int offset, int count)
  110.         {
  111.             int samplesRead = source.Read(buffer, offset, count);
  112.  
  113.             Process(buffer, samplesRead);
  114.  
  115.             return samplesRead;
  116.         }
  117.  
  118.         public void Process(byte[] buffer, int samples)
  119.         {
  120.             // Convert the byte array to a float array
  121.         float[] floatBuffer = new float[samples];
  122.         Buffer.BlockCopy(buffer, 0, floatBuffer, 0, samples * sizeof(float));
  123.  
  124.         // Process each channel and band separately
  125.         int channels = source.WaveFormat.Channels;
  126.         int bands = filters.GetLength(0);
  127.         for (int i = 0; i < channels; i++)
  128.         {
  129.             for (int j = 0; j < bands; j++)
  130.             {
  131.                 BiQuadFilter filter = filters[j, i];
  132.                 for (int k = 0; k < samples; k++)
  133.                 {
  134.                     floatBuffer[k * channels + i] = filter.Transform(floatBuffer[k * channels + i]);
  135.                 }
  136.             }
  137.         }
  138.  
  139.         // Convert the float array back to a byte array
  140.         Buffer.BlockCopy(floatBuffer, 0, buffer, 0, samples * sizeof(float));
  141.     }
  142.  
  143.     public void Dispose()
  144.     {
  145.         if (source != null && source is IDisposable)
  146.         {
  147.             ((IDisposable)source).Dispose();
  148.         }
  149.         for (int i = 0; i < filters.GetLength(0); i++)
  150.         {
  151.             for (int j = 0; j < filters.GetLength(1); j++)
  152.             {
  153.                 filters[i, j].Dispose();
  154.             }
  155.         }
  156.     }
  157. }
  158.  
  159. }
  160.  
  161.  
  162.  
  163. /*
  164. This code creates a class called `Equalizer` which implements the
  165. `ISampleProvider` interface. The `Equalizer` class contains an array
  166. of `BiQuadFilter` objects which represent the filters for each band
  167. and channel. The `SetSource` method is used to connect the `Equalizer`
  168. to a source `ISampleProvider`, and the `Process` method is used to
  169. apply the equalization to the audio data.
  170.  
  171. The main method of the `Program` class creates an input stream from a file,
  172. creates an instance of the `Equalizer` class, sets the source of the
  173. `Equalizer` to the input stream, creates an output stream for the equalized
  174. audio data, and then loops through the input stream, processing the audio in
  175. real-time and writing the equalized audio to the output stream.
  176.  
  177. Note that this code assumes that the input audio is in the `WaveFormat`
  178. specified by the `Equalizer` constructor. If the input audio is in a different
  179. format, you may need to add additional code to convert the audio data to the
  180. correct format before processing it through the equalizer.
  181. */
Advertisement
Add Comment
Please, Sign In to add comment