Search code examples
c#linux.net-corenaudiowavesurfer.js

Unable to generate peaks using NAudio on LINUX


I am trying to generate peaks (of .mp3 audio) using NAudio on Linux dotnet core 6.0 using this code (shown below) to be used in wavesurfers.js:

using(var reader = new AudioFileReader(file))
{
    var samples = reader.Length / (reader.WaveFormat.Channels * reader.WaveFormat.BitsPerSample / 8);

    var max = 0.0f;
    // waveform will be a maximum of 4000 pixels wide:
    var batch = (int)Math.Max(40, samples / 4000);
  
    float[] buffer = new float[batch];
    int read;
    
    while((read = reader.Read(buffer,0,batch)) == batch)
    {
        for(int n = 0; n < read; n++)
        {
            max = Math.Max(Math.Abs(buffer[n]), max);
        };
        console.write(max);//peak ex: 0.342424, this can be store in array or .peak file 
      console.write(',');
        max = 0;    
        
    }
}
Unhandled exception. System.DllNotFoundException: Unable to load shared library 'Msacm32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libMsacm32.dll: cannot open shared object file: No such file or directory
   at NAudio.Wave.Compression.AcmInterop.acmFormatSuggest2(IntPtr hAcmDriver, IntPtr sourceFormatPointer, IntPtr destFormatPointer, Int32 sizeDestFormat, AcmFormatSuggestFlags suggestFlags)
   at NAudio.Wave.Compression.AcmStream.SuggestPcmFormat(WaveFormat compressedFormat)
   at NAudio.Wave.AcmMp3FrameDecompressor..ctor(WaveFormat sourceFormat)
   at NAudio.Wave.Mp3FileReader.CreateAcmFrameDecompressor(WaveFormat mp3Format)
   at NAudio.Wave.Mp3FileReaderBase..ctor(Stream inputStream, FrameDecompressorBuilder frameDecompressorBuilder, Boolean ownInputStream)
   at NAudio.Wave.Mp3FileReader..ctor(String mp3FileName)
   at NAudio.Wave.AudioFileReader.CreateReaderStream(String fileName)
   at NAudio.Wave.AudioFileReader..ctor(String fileName)
   at Program.<Main>$(String[] args) in /home/adil/Blob Test/Program.cs:line 288

I came to know NAudio works only on Windows. Some places suggested I can use NAudio.Core. What exactly is NAudio.Core ? I am extremally new to C# and was trying to understand how I can use NAudio.Core but couldn't find and documentation or any direction on it.

If not possible, is there any other method's or packages that could be used to achieve this preferably MIT ? (I can't use audiowaveform due to licensing restriction).


Solution

  • You can directly use Mp3FileReader and use NLayer for the frame decompressor. More info here.

    This code snippet shows how to use it to convert an MP3 file to WAV:

    using (var reader = new Mp3FileReader(infile, wf => new Mp3FrameDecompressor(wf)))
    {
        WaveFileWriter.CreateWaveFile(outfile, reader);
    }