C#实现wav波形图

看了一下网上的资料,实现不难,接下来要研究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 list = new List();
                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;
        }
    }
}


你可能感兴趣的:(大数据,神经网络,深度学习,机器学习)