我正在尝试在 Linux dotnet core 6.0 上使用 NAudio 生成峰值(.mp3 音频),使用此代码(如下所示)在 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
我开始知道 NAudio 只能在 Windows 上运行。有些地方建议我可以使用 NAudio.Core。 NAudio.Core 到底是什么?我对 C# 非常陌生,试图了解如何使用 NAudio.Core,但找不到相关文档或任何说明。
如果不可能,是否有任何其他方法或包可以用来实现这一目标,最好是 MIT ? (由于许可限制,我无法使用音频波形)。
首先,nuget安装NLayer
https://github.com/naudio/NLayer
然后
using NAudio.Wave;
using NLayer.NAudioSupport;
var fileName = "your_file.mp3";
var builder = new Mp3FileReader.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(wf));
var reader = new Mp3FileReaderBase(fileName, builder);
问题就解决了。