1
A
回答
0
這將使用n音訊生成的音頻文件波形...
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;
}
}
相關問題
- 1. 使用C#顯示音頻波形
- 2. 創建三角形音頻波形
- 3. 讀取音頻文件並用Java顯示波形。
- 4. 從音頻文件中創建平坦的聲波(波形)圖像
- 5. WPF庫顯示音頻波形
- 6. 如何用自定義頻率音/波創建.wav文件?
- 7. 在android中創建音頻文件的波形
- 8. 比較兩個音頻文件波形
- 9. 如何在Flash中創建音樂播放器來創建音頻文件的波形?
- 10. 有沒有可以從音頻文件(mp3)中創建波形圖像的庫?
- 11. 從YouTube視頻生成音頻波形
- 12. 從matlab錄製的波形音頻文件的聲音不同
- 13. 從PCM文件或FFT顯示波形
- 14. 從字符串創建波形文件
- 15. 如何在Android上創建音頻示波器
- 16. 如何使用VBA播放波形文件或聲音文件
- 17. 如何繪製音頻波形?
- 18. 如何使用J2me midp2.0在手機中播放音頻文件?
- 19. 如何獲取mp3或音頻文件的波形?
- 20. 使用as3創建正弦波音頻 - 掃描上下頻率
- 21. 使用J2ME播放音頻
- 22. 音頻波形可視化
- 23. 生成音頻波形
- 24. OpenGL音頻波形渲染
- 25. 音頻波形匹配
- 26. 從音頻文件生成波形圖像
- 27. 在MATLAB中刪除波形音頻文件中的噪音
- 28. 創建音頻波形可擦洗時間線。 Swift 3 MacOS
- 29. iOS:創建簡單的音頻波形動畫
- 30. 如何使用處理反轉音頻波形