I use the following code to convert .wav audio file to bitmap or spectrogram but get white noise.
private void button1_Click(object sender, EventArgs e)
{
System.IO.Stream load_wav = new System.IO.MemoryStream();
openFileDialog1.Filter = "WAV|*.wav";
openFileDialog1.ShowDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((load_wav = openFileDialog1.OpenFile()) != null)
{
using (load_wav)
{
using (WaveFileReader reader = new WaveFileReader(load_wav))
{
int BitsPerSample;
int SampleRate;
BitsPerSample = reader.WaveFormat.BitsPerSample;
SampleRate = reader.WaveFormat.SampleRate;
string config = SampleRate.ToString();
// Write sample config to .ini
System.IO.StreamWriter file = new System.IO.StreamWriter(".\\config.ini");
file.WriteLine(config);
file.Close();
if (BitsPerSample == 16) // Check 16bit
{
// Read WAV
byte[] in_buffer = new byte[reader.Length];
int read = reader.Read(in_buffer, 0, in_buffer.Length);
MessageBox.Show("Load OK");
// Convert byte array to stream
Bitmap output = new Bitmap(600, 600);
Rectangle rect = new Rectangle(0, 0, output.Width, output.Height);
BitmapData bmpData = output.LockBits(rect, ImageLockMode.ReadWrite, output.PixelFormat);
IntPtr ptr = bmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(in_buffer, 0, ptr, in_buffer.Length);
output.UnlockBits(bmpData);
pictureBox1.Image = output;
output.Save(".\\output.bmp");
}
else
{
MessageBox.Show("16 bit WAV files only :-(");
}