C#實現wav波形圖

來源:互聯網
上載者:User

標籤:

看了一下網上的資料,實現不難,接下來要研究fft

歸一按照網上資料,補碼/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波形圖

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.