C#实现wav波形图
2015-05-08 21:41
681 查看
看了一下网上的资料,实现不难,接下来要研究fft
归一按照网上资料,补码/32768
程序分作了两部分,wav格式的定义网上资料很多
读取wav,保存音频数据到txt
读取txt,显示波形
归一按照网上资料,补码/32768
程序分作了两部分,wav格式的定义网上资料很多
读取wav,保存音频数据到txt
using System.IO; using System; using System.Text; namespace 音频处理 { class Program { const int byteSample = 2; const int dataPosition = 40; //0x16 2byte 0002 双声道 //0x22 2byte 0010 16位 //0x18 4byte 0000AC44 44100采样率 static void Main(string[] args) { byte[] length = new byte[4]; FileStream fs = new FileStream("test.wav", FileMode.Open, FileAccess.Read); fs.Position = dataPosition; fs.Read(length, 0, 4); byte[] content = new byte[getHexToInt(length)]; string[] sample = new string[content.Length / byteSample]; fs.Read(content, 0, content.Length); getHex(content); sample = getSample(content); StreamWriter sw = new StreamWriter("data.txt", true, Encoding.Default); foreach (string i in sample) { sw.Flush(); sw.WriteLine(i); } sw.Close(); } static int getHexToInt(byte[] x) { string retValue = ""; for (int i = x.Length - 1; i >= 0; i--) { retValue += x[i].ToString("X"); } return Convert.ToInt32(retValue, 16); } static void getHex(byte[] x) { byte tmp; for (int i = 0; i < x.Length; i++) { tmp = Convert.ToByte(x[i].ToString("X"), 16); x[i] = tmp; } } static string[] getSample(byte[] x) { string[] retValue = new string[x.Length / byteSample]; for (int i = 0; i < retValue.Length; i++) { for (int j = (i + 1) * byteSample - 1; j >= i * byteSample; j--) { retValue[i] += x[j].ToString("X"); } retValue[i] = ((double)Convert.ToInt16(retValue[i], 16) / 32768).ToString("F4"); } return retValue; } } }
读取txt,显示波形
using System; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Collections.Generic; using System.Threading; namespace LoadDataView { public partial class Form1 : Form { const bool leftStatus = false; public Form1() { InitializeComponent(); new Thread(new ThreadStart(() => { this.Invoke(new MethodInvoker(() => { label1.Text = leftStatus ? "左声道" : "右声道"; })); List<double> list = new List<double>(); StreamReader sr = new StreamReader("data.txt", Encoding.Default); int m = 0; while (!sr.EndOfStream) { if (leftStatus) { if (m % 2 == 0) list.Add(double.Parse(sr.ReadLine())); } else { if (m % 2 != 0) list.Add(double.Parse(sr.ReadLine())); } m++; } sr.Close(); Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.DrawLine(new Pen(Color.Black, 5), new Point(20, 20), new Point(20, 540)); g.DrawLine(new Pen(Color.Black, 5), new Point(20, 290), new Point(1600, 290)); int k = list.Count / 1550; for (int i = 0; i < list.Count; i++) { g.DrawLine(new Pen(Color.Green, 1), new Point(20 + i / k, 290), new Point(20 + i / k, 290 + (int)(list[i] * 250 * 2))); } this.pictureBox1.Image = bitmap; })).Start(); } private void Form1_Resize(object sender, EventArgs e) { this.Width = 1650; this.Height = 600; } } }
相关文章推荐
- C# 实现wav 文件读取并绘制波形图
- 基于C#的波形显示控件的实现
- C#实现WAV音频单声道提取
- 用C#实现多种方式播放Wav声音
- C#实现用于操作wav声音文件的类实例
- C# 利用MSChart实现波形分析
- 基于C#的波形显示控件的实现
- C#实现Wav文件截取、淡入淡出处理(PCM 格式)
- C#实现WAV音频单声道提取
- c++&c#实现拼接wav和mp3文件
- 关于c#实现计算机音频接口输出固定频率波形的一些问题
- C# 实现Domain Login 验证用户权限与Domain Mode读取
- C#语音实现
- C#实现Windows 服务的制作安装和删除
- 全面剖析C#接口编程之实现接口 (2)
- C#打印实现
- C# 实现系统热键盘注册的类
- C#中实现byte[]与任意对象互换(服务端通讯专用)
- 用C# 实现CS模式下软件自动在线升级
- 重温数据结构系列随笔:单链表(c#模拟实现)