- 是否有任何與音訊/程式設計相關的堆疊交換網站?
- 我正在嘗試在WinForms中製作波形。
- 我應該使用什麼演算法?
例如,如果每個像素(垂直線)有200個樣本,我應該繪製那部分200個樣本中最低和最高的樣本嗎?還是應該繪製低位和高位樣本的平均值?也許用不同的顏色繪製兩者都可以?
例如,如果每個像素(垂直線)有200個樣本,我應該繪製那部分200個樣本中最低和最高的樣本嗎?還是應該繪製低位和高位樣本的平均值?也許用不同的顏色繪製兩者都可以?
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strPath = Server.MapPath("audio/060.mp3");
string SongID = "2";
byte[] bytes = File.ReadAllBytes(strPath);
WriteToFile(SongID,strPath, bytes);
Response.Redirect("Main.aspx");
}
private void WriteToFile(string SongID, string strPath, byte[] Buffer)
{
try
{
int samplesPerPixel = 128;
long startPosition = 0;
//FileStream newFile = new FileStream(GeneralUtils.Get_SongFilePath() + "/" + strPath, FileMode.Create);
float[] data = FloatArrayFromByteArray(Buffer);
Bitmap bmp = new Bitmap(1170, 200);
int BORDER_WIDTH = 5;
int width = bmp.Width - (2 * BORDER_WIDTH);
int height = bmp.Height - (2 * BORDER_WIDTH);
NAudio.Wave.Mp3FileReader reader = new NAudio.Wave.Mp3FileReader(strPath, wf => new NAudio.FileFormats.Mp3.DmoMp3FrameDecompressor(wf));
NAudio.Wave.WaveChannel32 channelStream = new NAudio.Wave.WaveChannel32(reader);
int bytesPerSample = (reader.WaveFormat.BitsPerSample / 8) * channelStream.WaveFormat.Channels;
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
Pen pen1 = new Pen(Color.Gray);
int size = data.Length;
string hexValue1 = "#009adf";
Color colour1 = System.Drawing.ColorTranslator.FromHtml(hexValue1);
pen1.Color = colour1;
Stream wavestream = new NAudio.Wave.Mp3FileReader(strPath, wf => new NAudio.FileFormats.Mp3.DmoMp3FrameDecompressor(wf));
wavestream.Position = 0;
int bytesRead1;
byte[] waveData1 = new byte[samplesPerPixel * bytesPerSample];
wavestream.Position = startPosition + (width * bytesPerSample * samplesPerPixel);
for (float x = 0; x < width; x++)
{
short low = 0;
short high = 0;
bytesRead1 = wavestream.Read(waveData1, 0, samplesPerPixel * bytesPerSample);
if (bytesRead1 == 0)
break;
for (int n = 0; n < bytesRead1; n += 2)
{
short sample = BitConverter.ToInt16(waveData1, n);
if (sample < low) low = sample;
if (sample > high) high = sample;
}
float lowPercent = ((((float)low) - short.MinValue) / ushort.MaxValue);
float highPercent = ((((float)high) - short.MinValue) / ushort.MaxValue);
float lowValue = height * lowPercent;
float highValue = height * highPercent;
g.DrawLine(pen1, x, lowValue, x, highValue);
}
}
string filename = Server.MapPath("image/060.png");
bmp.Save(filename);
bmp.Dispose();
}
catch (Exception e)
{
}
}
public float[] FloatArrayFromStream(System.IO.MemoryStream stream)
{
return FloatArrayFromByteArray(stream.GetBuffer());
}
public float[] FloatArrayFromByteArray(byte[] input)
{
float[] output = new float[input.Length / 4];
for (int i = 0; i < output.Length; i++)
{
output[i] = BitConverter.ToSingle(input, i * 4);
}
return output;
}
}
尝试访问dsp.stackexchange.com
每像素200个样本时,有几种方法可尝试。无论您选择什么方法,最好将每条垂直线都绘制在0的上方和下方,即分别处理正样本值和负样本值。可能最简单的是只计算RMS。在这样低的分辨率下,峰值通常会给您提供一个误导波形的表示。
如果有人遇到这个问题:
您可以将每像素样本视为缩放级别,在更高级别(缩小更多)上,您可能希望出于性能原因对其进行子采样。
您很可能希望有一个适合屏幕的固定宽度来绘制,并使用虚拟滚动(这样您就不会有数百万像素的绘图区域)。
您可以通过迭代音频数据并使用以下公式计算每个像素的值:跳过(滚动位置*每像素样本)+(像素*每像素样本)获取每像素样本。 这允许以高效的无限缩放和滚动方式进行操作,因为您只读取和绘制填充视图所需的最小量。 滚动宽度是通过音频数据长度/每像素样本计算的。
音频样本通常以两种方式显示,即样本范围的峰值或rms值。 rms值通过将样本范围内所有值的平方求和,将总和除以样本长度,rms值是其平方根(rms略高于平均值,是感知响度的良好指标)。
您可以通过多种方式提高性能,例如增加子采样(导致失去细节),限制滚动和使绘图请求可取消,以防在渲染之前出现新的滚动。
只是为了记录,如果你想让音频文件填满输出图像的宽度
samplesPerPixel = (reader.Length / bytesPerSample) / width ;