wav file reading Failure... Help needed

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Ok, now I need some help with the binary reader. ITS NOT READING ANYTHING. All it gives me is a 0. I have hexadecimal stored values in my wav file its supposed to read from but its not reading anything. Heres the code (Skip to the "public Form1()" part
where it reads wav files...):

<div style="color:black; background-color:white
<pre><span style="color:blue using System;
<span style="color:blue using System.Collections.Generic;
<span style="color:blue using System.ComponentModel;
<span style="color:blue using System.Data;
<span style="color:blue using System.Drawing;
<span style="color:blue using System.Linq;
<span style="color:blue using System.Text;
<span style="color:blue using System.Windows.Forms;
<span style="color:blue using System.IO;
<span style="color:blue using System.Media;
<span style="color:blue using System.Diagnostics;

<span style="color:blue namespace AutoTapTap1
{
<span style="color:blue public <span style="color:blue partial <span style="color:blue class Form1 : Form
{
<span style="color:blue #region Var Init
<span style="color:blue #region sp, fs and br
<span style="color:blue public SoundPlayer sp = <span style="color:blue new SoundPlayer(<span style="color:#a31515 "C:/users/matthew/documents/Noteflight wav file exports/SMSB.wav");
<span style="color:blue public FileStream fs = <span style="color:blue new FileStream(<span style="color:#a31515 "C:/users/matthew/documents/Noteflight wav file exports/SMSB.wav", FileMode.Open);
<span style="color:blue public BinaryReader br;
<span style="color:blue #endregion
<span style="color:blue #region Used for wav file specs reading
<span style="color:blue public <span style="color:blue byte[] temp = <span style="color:blue new <span style="color:blue byte[1000];
<span style="color:blue public <span style="color:blue byte[] temp2 = <span style="color:blue new <span style="color:blue byte[1000];
<span style="color:blue public <span style="color:blue byte[] temp3 = <span style="color:blue new <span style="color:blue byte[1000];
<span style="color:blue #endregion
<span style="color:blue public <span style="color:blue bool IsStereo = <span style="color:blue true; <span style="color:green //True for stereo, false for mono
<span style="color:blue #region Samples and bytes, bytes and samples
<span style="color:blue public <span style="color:blue int SamplesPerSecond;
<span style="color:blue public <span style="color:blue int SamplesPer10Mil;
<span style="color:blue public <span style="color:blue int BytesPerSample;
<span style="color:blue public <span style="color:blue long bytes = 36;
<span style="color:blue #endregion
<span style="color:blue public Frequency f = Frequency.same;
<span style="color:blue public <span style="color:blue bool SongIsReadyToPlay = <span style="color:blue false;<span style="color:green //This is true after 10 seconds of ElapsedMilliseconds of the timer
<span style="color:blue #region Randomness
<span style="color:blue public Random rndseed = <span style="color:blue new Random();
<span style="color:blue public Random rnd;
<span style="color:blue #endregion
<span style="color:blue public <span style="color:blue bool SecRun = <span style="color:blue false;
<span style="color:blue #region ti-madness!
<span style="color:blue public <span style="color:blue int ti;
<span style="color:blue public <span style="color:blue int ti2;
<span style="color:blue public <span style="color:blue int ti3;
<span style="color:blue public <span style="color:blue int ti4;
<span style="color:blue public <span style="color:blue int ti5;
<span style="color:blue public <span style="color:blue int ti6;
<span style="color:blue public <span style="color:blue int ti7;
<span style="color:blue public <span style="color:blue int ti8;
<span style="color:blue public <span style="color:blue int ti9;
<span style="color:blue public <span style="color:blue int ti10;
<span style="color:blue public <span style="color:blue int ti11;
<span style="color:blue public <span style="color:blue int ti12;
<span style="color:blue public <span style="color:blue int ti13;
<span style="color:blue public <span style="color:blue int ti14;
<span style="color:blue public <span style="color:blue int ti15;
<span style="color:blue public <span style="color:blue int ti16;
<span style="color:blue #endregion
<span style="color:blue public Stopwatch timer = <span style="color:blue new Stopwatch();
<span style="color:blue public <span style="color:blue int bytediff = 0;
<span style="color:blue public <span style="color:blue bool firstrun2 = <span style="color:blue true;
<span style="color:blue #endregion
<span style="color:blue #region The form1 initialisation function
<span style="color:blue public Form1()
{
InitializeComponent();
<span style="color:blue this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer, <span style="color:blue true);
br = <span style="color:blue new BinaryReader(fs);
<span style="color:blue #region Wav file stats reading
<span style="color:green //Read Samples per second
br.Read(temp, 25, 1);
br.Read(temp2, 24, 1);
SamplesPerSecond = temp[0] + (temp2[0] * 0x100);
MessageBox.Show(<span style="color:#a31515 "Samples/Second: " + SamplesPerSecond);

<span style="color:green //Samples per 10 milliseconds
SamplesPer10Mil = SamplesPerSecond / 100;

<span style="color:green //Read stereoism
br.Read(temp, 22, 1);
<span style="color:blue if (temp[0] == 1)
{
IsStereo = <span style="color:blue false; MessageBox.Show(<span style="color:#a31515 "Using mono");
}
<span style="color:blue if (temp[0] == 2)
{
IsStereo = <span style="color:blue true; MessageBox.Show(<span style="color:#a31515 "Using Stereo");
}

<span style="color:green //Read Bytes per sample
br.Read(temp, 32, 1);
BytesPerSample = temp[0];
<span style="color:blue #endregion
fs.Flush();
br.ReadBytes(36);
timer.Start();
rnd = <span style="color:blue new Random(rndseed.Next());
<span style="color:blue if (BytesPerSample == 1) { MessageBox.Show(<span style="color:#a31515 "Using one"); }
<span style="color:blue else <span style="color:blue if (BytesPerSample == 2) { MessageBox.Show(<span style="color:#a31515 "Using two"); }
<span style="color:blue else <span style="color:blue if (BytesPerSample == 4) { MessageBox.Show(<span style="color:#a31515 "Using four"); }
<span style="color:blue else { MessageBox.Show(<span style="color:#a31515 "DEBUG ERROR: wrong bytespersample reading!"); }
}
<span style="color:blue #endregion
<span style="color:blue #region The OnPaint function
<span style="color:blue private <span style="color:blue void OnPaint(<span style="color:blue object sender, PaintEventArgs e)
{
<span style="color:blue #region SongIsReadyToPlay thing...
<span style="color:blue if (timer.ElapsedMilliseconds > 1000)
{
<span style="color:blue if (firstrun2)
{
firstrun2 = <span style="color:blue false;
sp.Play();
}
}
<span style="color:blue #endregion
Graphics g = e.Graphics;
<span style="color:blue #region timerthing...
<span style="color:blue if (SongIsReadyToPlay)
{
<span style="color:blue if (timer.ElapsedMilliseconds % 10 == 0)
{
<span style="color:blue #region one
<span style="color:blue if (BytesPerSample == 1)
{
ti = br.ReadByte();
ti2 = br.ReadByte();
ti3 = br.ReadByte();
ti4 = br.ReadByte();
br.ReadBytes(SamplesPer10Mil * BytesPerSample - 4);
<span style="color:blue if (SecRun)
{
<span style="color:blue #region Frequency checking (and volume also....)
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff > 40)
f = Frequency.higher;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff > 80)
f = Frequency.vhigh;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 40)
f = Frequency.lower;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 80)
f = Frequency.vlow;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 4 && Math.Abs(ti - ti2) - bytediff > -4)
f = Frequency.same;
<span style="color:blue if (ti < 4 || ti2 < 4 || ti3 < 4 || ti4 < 4)
f = Frequency.nulled;
<span style="color:blue if (Math.Abs(ti - ti3) < 20 && Math.Abs(ti2 - ti4) < 20)
f = Frequency.doublenote;
<span style="color:blue if (ti > 240 || ti2 > 240 || ti3 > 240 || ti4 > 240)
f = Frequency.BLAST;
<span style="color:blue #endregion
}
bytediff = Math.Abs(ti - ti2);
SecRun = <span style="color:blue true;
g.FillRectangle(<span style="color:blue new SolidBrush(Color.AliceBlue), 0, 0, 50, (ti / 256.0f) * 550);
}
<span style="color:blue #endregion
<span style="color:blue #region two mono
<span style="color:blue else <span style="color:blue if (BytesPerSample == 2 && IsStereo == <span style="color:blue false)
{
<span style="color:blue #region INIT!!!
ti = br.ReadByte();<span style="color:green //Chunk1 lb
ti2 = br.ReadByte();<span style="color:green //Chunk1 hb
ti3 = br.ReadByte();<span style="color:green //2lb
ti4 = br.ReadByte();<span style="color:green //2hb
ti5 = br.ReadByte();<span style="color:green //3lb
ti6 = br.ReadByte();<span style="color:green //3hb
ti7 = br.ReadByte();<span style="color:green //4lb
ti8 = br.ReadByte();<span style="color:green //4hb
<span style="color:blue #endregion
<span style="color:blue #region Amplitude init (the real one). Values are upon 65536
ti = ti + ti2 * 0x100;
ti2 = ti3 + ti4 * 0x100;
ti3 = ti5 + ti6 * 0x100;
ti4 = ti7 + ti8 * 0x100;
<span style="color:blue #endregion
br.ReadBytes(SamplesPer10Mil * BytesPerSample - 8);
<span style="color:blue if (SecRun)
{
<span style="color:blue #region Frequency checking (and volume also....)
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff > 10240)
f = Frequency.higher;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff > 20480)
f = Frequency.vhigh;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 10240)
f = Frequency.lower;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 20480)
f = Frequency.vlow;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 1024 && Math.Abs(ti - ti2) - bytediff > -1024)
f = Frequency.same;
<span style="color:blue if (ti < 1024 || ti2 < 1024 || ti3 < 1024 || ti4 < 1024)
f = Frequency.nulled;
<span style="color:blue if (Math.Abs(ti - ti3) < 5120 && Math.Abs(ti2 - ti4) < 5120)
f = Frequency.doublenote;
<span style="color:blue if (ti > 61440 || ti2 > 61440 || ti3 > 61440 || ti4 > 61440)
f = Frequency.BLAST;
<span style="color:blue #endregion
}
bytediff = Math.Abs(ti - ti2);
SecRun = <span style="color:blue true;
g.FillRectangle(<span style="color:blue new SolidBrush(Color.Blue), 0, 0, 50, (ti / 65536.0f) * 550);
}
<span style="color:blue #endregion
<span style="color:blue #region two stereo
<span style="color:blue else <span style="color:blue if (BytesPerSample == 2 && IsStereo)
{
ti = br.ReadByte();
br.ReadByte();<span style="color:green //Well, its stereo and thats no reason to make tappers stereo...
ti2 = br.ReadByte();
br.ReadByte();
ti3 = br.ReadByte();
br.ReadByte();
ti4 = br.ReadByte();
br.ReadByte();
br.ReadBytes(SamplesPer10Mil * BytesPerSample - 8);
<span style="color:blue if (SecRun)
{
<span style="color:blue #region Frequency checking (and volume also....)
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff > 40)
f = Frequency.higher;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff > 80)
f = Frequency.vhigh;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 40)
f = Frequency.lower;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 80)
f = Frequency.vlow;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 4 && Math.Abs(ti - ti2) - bytediff > -4)
f = Frequency.same;
<span style="color:blue if (ti < 4 || ti2 < 4 || ti3 < 4 || ti4 < 4)
f = Frequency.nulled;
<span style="color:blue if (Math.Abs(ti - ti3) < 20 && Math.Abs(ti2 - ti4) < 20)
f = Frequency.doublenote;
<span style="color:blue if (ti > 240 || ti2 > 240 || ti3 > 240 || ti4 > 240)
f = Frequency.BLAST;
<span style="color:blue #endregion
}
bytediff = Math.Abs(ti - ti2);
SecRun = <span style="color:blue true;
g.FillRectangle(<span style="color:blue new SolidBrush(Color.AliceBlue), 0, 0, 50, (ti / 256) * 550);
}
<span style="color:blue #endregion
<span style="color:blue #region four
<span style="color:blue else <span style="color:blue if (BytesPerSample == 4)
{
<span style="color:blue #region INIT!!!
ti = br.ReadByte();<span style="color:green //Chunk1 lb
ti2 = br.ReadByte();<span style="color:green //Chunk1 hb
br.ReadBytes(2);<span style="color:green //THIS IS STEREO!!!!!!
ti3 = br.ReadByte();<span style="color:green //2lb
ti4 = br.ReadByte();<span style="color:green //2hb
br.ReadBytes(2);
ti5 = br.ReadByte();<span style="color:green //3lb
ti6 = br.ReadByte();<span style="color:green //3hb
br.ReadBytes(2);
ti7 = br.ReadByte();<span style="color:green //4lb
ti8 = br.ReadByte();<span style="color:green //4hb
br.ReadBytes(2);
<span style="color:blue #endregion
<span style="color:blue #region Amplitude init (the real one). Values are upon 65536
ti = ti + ti2 * 0x100;
ti2 = ti3 + ti4 * 0x100;
ti3 = ti5 + ti6 * 0x100;
ti4 = ti7 + ti8 * 0x100;
<span style="color:blue #endregion
br.ReadBytes(SamplesPer10Mil * BytesPerSample - 16);
<span style="color:blue if (SecRun)
{
<span style="color:blue #region Frequency checking (and volume also....)
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff > 10240)
f = Frequency.higher;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff > 20480)
f = Frequency.vhigh;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 10240)
f = Frequency.lower;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 20480)
f = Frequency.vlow;
<span style="color:blue if (Math.Abs(ti - ti2) - bytediff < 1024 && Math.Abs(ti - ti2) - bytediff > -1024)
f = Frequency.same;
<span style="color:blue if (ti < 1024 || ti2 < 1024 || ti3 < 1024 || ti4 < 1024)
f = Frequency.nulled;
<span style="color:blue if (Math.Abs(ti - ti3) < 5120 && Math.Abs(ti2 - ti4) < 5120)
f = Frequency.doublenote;
<span style="color:blue if (ti > 61440 || ti2 > 61440 || ti3 > 61440 || ti4 > 61440)
f = Frequency.BLAST;
<span style="color:blue #endregion
}
bytediff = Math.Abs(ti - ti2);
SecRun = <span style="color:blue true;
g.FillRectangle(<span style="color:blue new SolidBrush(Color.AliceBlue), 0, 0, 50, (ti / 65536.0f) * 550);
}
<span style="color:blue #endregion
<span style="color:blue else
{
MessageBox.Show(<span style="color:#a31515 "Youre not using a wav file!!!");
}
}
}
<span style="color:blue #endregion
System.Threading.Thread.Sleep(0);
pictureBox1.Invalidate();
}
<span style="color:blue #endregion
<span style="color:blue #region enums
<span style="color:blue #region Frequency enum
<span style="color:blue public <span style="color:blue enum Frequency
{
higher,
lower,
same,<span style="color:green // About there (+- 4 amplitude for 8-bit, +- 1024)
vhigh,
vlow,
nulled,<span style="color:green //Almost zero amplitude (less than 4/256; less than 1024/65536)
BLAST,<span style="color:green //Very high amplitude (more than 240/256; more than 61440/65536)
doublenote<span style="color:green //Scans a continuing pattern AKA: 141 25 158 32 175 39 (Separate notes: 141 158 175, 25 32 39)
}
<span style="color:blue #endregion
<span style="color:blue #region TapperRail enum
<span style="color:blue public <span style="color:blue enum TapperRail
{
left,
centre,
right,
lc,
lr,
cr,
ALLMUAHAHA
}
<span style="color:blue #endregion
<span style="color:blue #endregion
<span style="color:blue #region Other functions
<span style="color:blue #endregion
}
}

[/code]

I suppose it has something to do with the BinaryReader. With the debugger, I sense that the BinaryReader.Read() function is not actually what I think it was. The second parameter of the Read() function is the index. And, any wave files sample
rate in Hz is the 24th index and 25th index. Once I take consideration of C# zero-basness, its 23&24. So, anyone can explain to me how it works?
<
Have a Byte on some Pi! www.hexprogramming.blogspot.com<br/>

View the full article
 
Back
Top