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 **
- ** **
- ***********************************************************/
- // 10-band equalizer with a range of 20Hz to 20KHz in C# using NAudio
- // with a stream as source and real-time processing:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using NAudio.Wave;
- namespace TenBandEqualizer
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Create an input stream from a file or other source
- WaveStream inputStream = new WaveFileReader("input.wav");
- // Create a 10-band equalizer with a range of 20Hz to 20KHz
- Equalizer equalizer = new Equalizer(inputStream.WaveFormat.SampleRate, inputStream.WaveFormat.Channels, 10, 20, 20000);
- // Create a source stream for the equalizer
- ISampleProvider source = inputStream.ToSampleProvider();
- // Connect the equalizer to the source stream
- equalizer.SetSource(source);
- // Create an output stream to write the equalized audio to
- WaveFileWriter outputWriter = new WaveFileWriter("output.wav", equalizer.WaveFormat);
- // Create a buffer to hold the equalized audio data
- byte[] buffer = new byte[equalizer.WaveFormat.AverageBytesPerSecond / 4];
- // Loop through the input stream and process the audio in real-time
- while (inputStream.Position < inputStream.Length)
- {
- int bytesRead = inputStream.Read(buffer, 0, buffer.Length);
- // Process the audio through the equalizer
- int samples = bytesRead / sizeof(float);
- equalizer.Process(buffer, samples);
- // Write the equalized audio to the output stream
- outputWriter.Write(buffer, 0, bytesRead);
- }
- // Clean up resources
- outputWriter.Dispose();
- inputStream.Dispose();
- equalizer.Dispose();
- }
- }
- class Equalizer : ISampleProvider, IDisposable
- {
- private readonly BiQuadFilter[,] filters;
- private readonly ISampleProvider source;
- public WaveFormat WaveFormat { get { return source.WaveFormat; } }
- public Equalizer(int sampleRate, int channels, int bands, float minFreq, float maxFreq)
- {
- filters = new BiQuadFilter[bands, channels];
- source = null;
- // Calculate the center frequencies of each band
- float[] centerFreqs = new float[bands];
- float logMin = (float)Math.Log10(minFreq);
- float logMax = (float)Math.Log10(maxFreq);
- float delta = (logMax - logMin) / (bands - 1);
- for (int i = 0; i < bands; i++)
- {
- float logFreq = logMin + i * delta;
- centerFreqs[i] = (float)Math.Pow(10, logFreq);
- }
- // Create a filter for each band and channel
- for (int i = 0; i < bands; i++)
- {
- for (int j = 0; j < channels; j++)
- {
- filters[i, j] = BiQuadFilter.PeakingEQ(sampleRate, centerFreqs[i], 0.5f, 0);
- }
- }
- }
- public void SetSource(ISampleProvider source)
- {
- this.source = source;
- }
- public int Read(float[] buffer, int offset, int count)
- {
- int samplesRead = source.Read(buffer, offset, count);
- Process(buffer, samplesRead);
- return samplesRead;
- }
- public void Process(byte[] buffer, int samples)
- {
- // Convert the byte array to a float array
- float[] floatBuffer = new float[samples];
- Buffer.BlockCopy(buffer, 0, floatBuffer, 0, samples * sizeof(float));
- // Process each channel and band separately
- int channels = source.WaveFormat.Channels;
- int bands = filters.GetLength(0);
- for (int i = 0; i < channels; i++)
- {
- for (int j = 0; j < bands; j++)
- {
- BiQuadFilter filter = filters[j, i];
- for (int k = 0; k < samples; k++)
- {
- floatBuffer[k * channels + i] = filter.Transform(floatBuffer[k * channels + i]);
- }
- }
- }
- // Convert the float array back to a byte array
- Buffer.BlockCopy(floatBuffer, 0, buffer, 0, samples * sizeof(float));
- }
- public void Dispose()
- {
- if (source != null && source is IDisposable)
- {
- ((IDisposable)source).Dispose();
- }
- for (int i = 0; i < filters.GetLength(0); i++)
- {
- for (int j = 0; j < filters.GetLength(1); j++)
- {
- filters[i, j].Dispose();
- }
- }
- }
- }
- }
- /*
- This code creates a class called `Equalizer` which implements the
- `ISampleProvider` interface. The `Equalizer` class contains an array
- of `BiQuadFilter` objects which represent the filters for each band
- and channel. The `SetSource` method is used to connect the `Equalizer`
- to a source `ISampleProvider`, and the `Process` method is used to
- apply the equalization to the audio data.
- The main method of the `Program` class creates an input stream from a file,
- creates an instance of the `Equalizer` class, sets the source of the
- `Equalizer` to the input stream, creates an output stream for the equalized
- audio data, and then loops through the input stream, processing the audio in
- real-time and writing the equalized audio to the output stream.
- Note that this code assumes that the input audio is in the `WaveFormat`
- specified by the `Equalizer` constructor. If the input audio is in a different
- format, you may need to add additional code to convert the audio data to the
- correct format before processing it through the equalizer.
- */
Advertisement
Add Comment
Please, Sign In to add comment