Madwin74

GetPeaklevels

May 4th, 2023 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 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.  
  16. using NAudio.Wave;
  17.  
  18. public static double GetPeakLevel(string audioFilePath)
  19. {
  20.     var audioFileReader = new AudioFileReader(audioFilePath);
  21.  
  22.     // Find the highest peak sample value
  23.     var maxSample = 0.0;
  24.     var buffer = new float[audioFileReader.WaveFormat.SampleRate * audioFileReader.WaveFormat.Channels];
  25.     while (audioFileReader.Read(buffer, 0, buffer.Length) > 0)
  26.     {
  27.         for (var i = 0; i < buffer.Length; i++)
  28.         {
  29.             var sample = Math.Abs(buffer[i]);
  30.             if (sample > maxSample)
  31.             {
  32.                 maxSample = sample;
  33.             }
  34.         }
  35.     }
  36.  
  37.     // Convert the highest peak sample value to dB
  38.     var peakLevel = 20.0 * Math.Log10(maxSample / audioFileReader.WaveFormat.SampleRate);
  39.     return peakLevel;
  40. }
  41.  
  42. /*
  43. To use this method, simply call GetPeakLevel with the path to your audio file. The method
  44. will return the peak level in dB as a double.
  45. */
  46.  
  47. using NAudio.Wave;
  48.  
  49. public static double GetPeakLevel(string audioFilePath)
  50. {
  51.     var maxSample = 0.0;
  52.  
  53.     using (var audioFileReader = new WaveFileReader(audioFilePath))
  54.     {
  55.         var buffer = new float[audioFileReader.WaveFormat.SampleRate * audioFileReader.WaveFormat.Channels];
  56.         var bytesRead = 0;
  57.         do
  58.         {
  59.             bytesRead = audioFileReader.Read(buffer, 0, buffer.Length);
  60.             for (var i = 0; i < bytesRead; i++)
  61.             {
  62.                 var sample = Math.Abs(buffer[i]);
  63.                 if (sample > maxSample)
  64.                 {
  65.                     maxSample = sample;
  66.                 }
  67.             }
  68.         } while (bytesRead > 0);
  69.     }
  70.  
  71.     // Convert the highest peak sample value to dB
  72.     var peakLevel = 20.0 * Math.Log10(maxSample);
  73.     return peakLevel;
  74. }
  75.  
  76.  
  77. /*
  78. This method uses a WaveFileReader object to read the audio file in smaller chunks instead
  79. of reading the entire file into memory. The WaveFileReader object is automatically disposed
  80. of when the using block ends, ensuring that any unmanaged resources are properly released.
  81. The rest of the method is the same as the previous version, converting the highest peak
  82. sample value to dB and returning the result as a double.
  83. */
Advertisement
Add Comment
Please, Sign In to add comment