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 **
- ** **
- ***********************************************************/
- using NAudio.Wave;
- using System;
- public class AudioLevelMeter
- {
- private readonly WaveInEvent waveIn;
- private readonly float[] buffer;
- private readonly int bufferSize;
- private readonly int channels;
- private readonly int channelsPerSpeaker;
- private readonly float maxLevel;
- public float[] ChannelLevels { get; private set; }
- public AudioLevelMeter(int deviceNumber, int bufferSize, int numChannels, float minLevel, float maxLevel)
- {
- waveIn = new WaveInEvent();
- waveIn.DeviceNumber = deviceNumber;
- waveIn.WaveFormat = new WaveFormat(44100, 16, numChannels);
- waveIn.DataAvailable += waveIn_DataAvailable;
- waveIn.StartRecording();
- this.bufferSize = bufferSize;
- channels = numChannels;
- channelsPerSpeaker = channels / (int)Math.Log(numChannels, 2);
- this.maxLevel = maxLevel - minLevel;
- buffer = new float[bufferSize];
- ChannelLevels = new float[channelsPerSpeaker];
- }
- private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
- {
- int bytesRead = 0;
- while (bytesRead < e.BytesRecorded)
- {
- buffer[bytesRead / 2] = BitConverter.ToInt16(e.Buffer, bytesRead) / 32768f;
- bytesRead += 2;
- }
- for (int i = 0; i < channelsPerSpeaker; i++)
- {
- float sum = 0;
- for (int j = i; j < buffer.Length; j += channelsPerSpeaker)
- {
- sum += Math.Abs(buffer[j]);
- }
- float average = sum / (buffer.Length / channelsPerSpeaker);
- float dbLevel = (float)(20 * Math.Log10(average));
- ChannelLevels[i] = (dbLevel < -maxLevel) ? 0 : (dbLevel > 0) ? 1 : (dbLevel + maxLevel) / maxLevel;
- }
- }
- public void Stop()
- {
- waveIn.StopRecording();
- }
- }
- /*
- The AudioLevelMeter constructor takes in four arguments: the device number, buffer size, number of channels, and the desired dB range. The minimum dB level is assumed to be -20 dB, and the maximum dB level is specified as an argument.
- The ChannelLevels property returns an array of floats representing the dB levels for each speaker. The values are normalized between 0 and 1, where 0 represents the minimum dB level (-20 dB) and 1 represents the maximum dB level (specified in the constructor). If the dB level is less than -20 dB, the value is set to 0. If the dB level is greater than the maximum dB level, the value is set to 1.
- Example usage of the AudioLevelMeter class:
- */
- AudioLevelMeter audioLevelMeter = new AudioLevelMeter(0, 4096, 2, -20, 4);
- while (true)
- {
- float[] levels = audioLevelMeter.ChannelLevels;
- Console.WriteLine($"Left: {levels[0]:F2}, Right: {levels[1]:F2}");
- Thread.Sleep(100);
- }
- /*
- The AudioLevelMeter is created with device number 0, a buffer size of 4096, 2 channels (for stereo audio), a minimum dB level of -20 dB, and a maximum dB level of 4 dB. The while loop outputs the dB levels for the left and right channels, normalized between 0 and 1. The program waits for 100 milliseconds between each update to avoid excessive CPU usage.
- To use the AudioLevelMeter with different audio configurations, you can modify the number of channels and the dB range in the constructor, and adjust the output accordingly. Here's an example usage for a 5.1 surround sound configuration:
- */
- AudioLevelMeter audioLevelMeter = new AudioLevelMeter(0, 4096, 6, -20, 4);
- while (true)
- {
- float[] levels = audioLevelMeter.ChannelLevels;
- Console.WriteLine($"LF: {levels[0]:F2}, RF: {levels[1]:F2}, C: {levels[2]:F2}, LFE: {levels[3]:F2}, LS: {levels[4]:F2}, RS: {levels[5]:F2}");
- Thread.Sleep(100);
- }
- /*
- In this example, the AudioLevelMeter is created with device number 0, a buffer size of 4096, 6 channels (for 5.1 surround sound), a minimum dB level of -20 dB, and a maximum dB level of 4 dB. The while loop outputs the dB levels for the left front, right front, center, LFE, left surround, and right surround channels, normalized between 0 and 1. The program waits for 100 milliseconds between each update to avoid excessive CPU usage.
- You can adjust the number of channels and the dB range to suit your needs for different audio configurations.
- */
Advertisement
Add Comment
Please, Sign In to add comment