title: Capture Sound from microphone using Directx DirectSound
body: <p>I am creating a simple application that records input from the microphone and store it into array of bytes. So I have searched a lot about this and eventually ended up using Directx DirectSound. Here is the code I am using:</p>
  
  <pre><code>using Microsoft.DirectX;
  using Microsoft.DirectX.DirectSound;
  
  private Thread CaptureSoundThread = null;
  public CaptureBuffer applicationBuffer = null;
  private SecondaryBuffer soundBuffer = null;
  private Device soundDevice = null;
  
  private void Form1_Load(object sender, EventArgs e)
  {
      soundDevice = new Device();
      soundDevice.SetCooperativeLevel(this, CooperativeLevel.Normal);
  
      // Set up our wave format to 44,100Hz, with 16 bit resolution
      WaveFormat wf = new WaveFormat();
      wf.FormatTag = WaveFormatTag.Pcm;
      wf.SamplesPerSecond = 44100;
      wf.BitsPerSample = 16;
      wf.Channels = 1;
      wf.BlockAlign = (short)(wf.Channels * wf.BitsPerSample / 8);
      wf.AverageBytesPerSecond = wf.SamplesPerSecond * wf.BlockAlign;
  
      int samplesPerUpdate = 512;
  
      // Create a buffer with 2 seconds of sample data
      BufferDescription bufferDesc = new BufferDescription(wf);
      bufferDesc.BufferBytes = samplesPerUpdate * wf.BlockAlign * 2;
      bufferDesc.ControlPositionNotify = true;
      bufferDesc.GlobalFocus = true;
  
      soundBuffer = new SecondaryBuffer(bufferDesc, soundDevice);
  }
  
  private void button1_Click(object sender, EventArgs e)
  {
      CaptureSoundThread = new Thread(new ThreadStart(WaitThread));
      CaptureSoundThread.Start();
  }
  
  private void WaitThread()
  {
      while (true)
      {
          byte[] CaptureData = null;
          CaptureData = (byte[])applicationBuffer.Read(0,
          typeof(byte), LockFlag.None);
          soundBuffer.Write(0, CaptureData, LockFlag.None);
          // Start it playing
          soundBuffer.Play(0, BufferPlayFlags.Looping);
      }
  }
  </code></pre>
  
  <p>But when I try to run the application, I get this annoying error:</p>
  
  <pre><code>BadImageFormatException
  
  Could not load file or assembly 'Microsoft.DirectX.DirectSound.dll' or one
  of its dependencies.  is not a valid Win32 application. (Exception from
  HRESULT: 0x800700C1)
  </code></pre>
  
  <p>I actually had to download the Microsoft.DirectX.DirectSound.dll from the internet because I couldn't find them in the Visual Studio assemblies.</p>
  
  <p>EDIT : I JUST SOLVED THAT by reading this article : <a href="http://www.codeproject.com/Articles/383138/BadImageFormatException-x86-i-x64" rel="nofollow">http://www.codeproject.com/Articles/383138/BadImageFormatException-x86-i-x64</a></p>