[英]Unable to get wave form image for audio using NAudio
Unable to get wave form image for smaller duration audio stream using this code. 使用此代码无法获得持续时间较短的音频流的波形图像。 I am getting totally blank image.
我的图像完全空白。 Is there any way to get correct wave form image for smaller duration audio stream.
有什么方法可以为较短持续时间的音频流获取正确的波形图像。 I am using NAudio's AudioFileReader function here.
我在这里使用NAudio的AudioFileReader函数。
Bitmap bim = new Bitmap(1800,200);
System.Drawing.Graphics g = Graphics.FromImage(bim);
using (var reader = new AudioFileReader("D:\\Test-Songs\\DawnJay.mp3"))
{
var samples = reader.Length / (reader.WaveFormat.Channels * reader.WaveFormat.BitsPerSample / 8);
var f = 0.0f;
var max = 0.0f;
// waveform will be a maximum of 4000 pixels wide:
var batch = (int)Math.Max(40, samples / 4000);
var mid = 100;
var yScale = 100;
float[] buffer = new float[batch];
int read;
var xPos = 0;
Pen pen = new Pen(Color.Red, 2.0f);
g.Clear(Color.Black);
while ((read = reader.Read(buffer, 0, batch)) == batch)
{
for (int n = 0; n < read; n++)
{
max = Math.Max(Math.Abs(buffer[n]), max);
}
int X1 = xPos;
int X2 = xPos;
float Y1 = mid + (max * yScale);
float Y2 = mid - (max * yScale);
g.DrawLine(pen,X1, Y1, X2, Y2);
max = 0;
xPos++;
}
}
bim.Save("D:\\Images\\waveform.png");
Your code here: 您的代码在这里:
var batch = (int)Math.Max(40, samples / 4000);
This says that you are going to accept a minimum of 40 samples per column. 这表示每列至少要接受40个样本。 For a small file this might mean that your data gets reduced to only a small number of columns of data in the output bitmap.
对于小文件,这可能意味着您的数据在输出位图中仅减少为少量的数据列。 If you are then scaling that data down to fit into a display area on screen, your audio data might disappear.
如果然后按比例缩小该数据以适合屏幕上的显示区域,则音频数据可能会消失。
Try changing your minimum number of samples per block to a smaller value, which should give you a chance at actually visualizing small audio files. 尝试将每个块的最小样本数更改为较小的值,这将为您提供实际可视化小型音频文件的机会。 You should probably do full Min-Max calculations, or else your plots for very small files will look totally wrong.
您可能应该进行完整的最小-最大计算,否则您对于非常小的文件的绘图将看起来完全错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.