无法在Linux上使用NAudio生成峰值。

huangapple go评论76阅读模式
英文:

Unable to generate peaks using NAudio on LINUX

问题

我正在尝试在Linux上使用dotnet core 6.0生成.mp3音频的波峰,使用以下代码来使用 wavesurfers.js中的音频:

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

    var max = 0.0f;
    // 波形最多宽度为4000像素:
    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);//峰值示例:0.342424,可以存储在数组或.peak文件中
        console.write(',');
        max = 0;

    }
}
Unhandled exception. System.DllNotFoundException: 无法加载共享库 'Msacm32.dll' 或其依赖项之一。为了帮助诊断加载问题,请考虑设置LD_DEBUG环境变量libMsacm32.dll:无法打开共享对象文件:没有那个文件或目录
   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

我了解到NAudio仅在Windows上工作。有些地方建议我可以使用NAudio.Core。NAudio.Core到底是什么?我对C#非常陌生,尝试了解如何使用NAudio.Core,但找不到任何文档或任何指导。

如果不可能,是否有其他方法或包可用于实现这一目标,最好是MIT许可证?(由于许可证限制,我无法使用audiowaveform)。

英文:

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 &lt; 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(&#39;,&#39;);
        max = 0;    
        
    }
}
Unhandled exception. System.DllNotFoundException: Unable to load shared library &#39;Msacm32.dll&#39; 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.&lt;Main&gt;$(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).

答案1

得分: 0

你可以直接使用 Mp3FileReader,并使用 NLayer 进行帧解压缩。更多信息请参见这里

以下是如何将MP3文件转换为WAV的代码片段:

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

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 =&gt; new Mp3FrameDecompressor(wf)))
{
    WaveFileWriter.CreateWaveFile(outfile, reader);
}

huangapple
  • 本文由 发表于 2023年2月27日 15:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定