C # wav Waveform
After reading the online materials, it is not difficult to implement the function. Next we will study the fft function.
Return to Alibaba Cloud by completing the code/32768 according to the online information
The program is divided into two parts. The definition of wav format has a lot of online information.
Read wav and save the audio data to txt.
Using System. IO; using System. text; namespace audio processing {class Program {const int byteSample = 2; const int dataPosition = 40; // 0x16 2 byte 0002 dual-channel // 0x22 2 byte 0010 16-bit // 0x18 4 byte merge ac44 44100 sampling rate 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 ;}}}
Read txt and display Waveforms
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? "Left channel": "right channel" ;})); 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 ;}}}