Help please - Streaming from microphone to speaker RRS feed

  • Question

  • Hi,
    I really need some help please..
    I am learning how to write a program that stream from microphone to speaker. (Play out the sound from microphone).
    I capture the data using CaptureBuffer and i know that in order to play the data out, I need to put the data into SecondaryBuffer.. But how to do it, I am not too sure..

    Below is the code that I tried to do:
    After the program is started, I am suppose to click on a button and i will be able to hear the things i say into the microphone. But instead, when i click on the button, the program hang for about 5 seconds and then an error occur at "secondaryBuffer = new SecondaryBuffer(stream, device)" saying "Index was outside the bounds of the array."

    Any idea how I can make it work..? I have been on it for more than 7 hours and i have ran out of ideas.. Thanks..

    Code Snippet

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using Microsoft.DirectX;
    using System.IO;
    using Microsoft.DirectX.DirectSound;
    using System.Threading;

    using Buffer = Microsoft.DirectX.DirectSound.Buffer;

    namespace Sound
        public partial class Sound : Form
            // private fields
            private Thread _captureThread;

            private Device device;
            private Capture capture;
            private Buffer buffer;
            private SecondaryBuffer secondaryBuffer;
            private BufferDescription bufferDesc;
            private CaptureBuffer captureBuffer;
            private WaveFormat waveFormat;
            private CaptureBufferDescription captureBuffDesc;
            private MemoryStream stream;
            private byte[] streamBuffer;

            public Sound()


            private void button1_Click(object sender, EventArgs e)
                _captureThread = new Thread(new ThreadStart(CaptureThread));

            private void CreateDevice()
                device = new Device();

            private void CreateBufferDescription()
                bufferDesc = new BufferDescription();
                bufferDesc.Format = waveFormat;
                bufferDesc.BufferBytes = 100000;
                bufferDesc.ControlPositionNotify = true;
                bufferDesc.ControlFrequency = true;
                bufferDesc.ControlPan = true;
                bufferDesc.ControlVolume = true;

            private void SetCooperativeLevel()
                this,CooperativeLevel.Priority // The cooperative level

            private void CreateWaveFormat()
                waveFormat = new WaveFormat();
                waveFormat.BitsPerSample = 8;
                waveFormat.BlockAlign = 1;
                waveFormat.Channels = 1;
                waveFormat.AverageBytesPerSecond = 20500;
                waveFormat.SamplesPerSecond = 20500;
                waveFormat.FormatTag = WaveFormatTag.Pcm;

            private void CreateCapture()
                capture = new Capture();

            private void CreateCapureDescription()
                captureBuffDesc = new CaptureBufferDescription();
                captureBuffDesc.BufferBytes = 100000;
                captureBuffDesc.Format = this.waveFormat;

            private void CreateCaptureBuffer()
                captureBuffer = new CaptureBuffer(captureBuffDesc, capture);

            private void CreateStream()
                stream = new MemoryStream(streamBuffer);

            private void CreateStreamBuffer()
                streamBuffer = new byte[100000];
                for (int i = 0; i < 100000; i++)
                    streamBuffer[i] = 0;

            private void CaptureThread()

                while (true)
                    byte[] data = (byte[])captureBuffer.Read(0, typeof(byte), LockFlag.None, 100000);
                    MemoryStream stream = new MemoryStream(data, false);

                    buffer = new Buffer(bufferDesc, device);
                    buffer.Write(0, stream, (int)stream.Length, LockFlag.EntireBuffer);

                    secondaryBuffer = new SecondaryBuffer(stream, device);
                    secondaryBuffer.Play(0, BufferPlayFlags.Default);


    Friday, May 2, 2008 9:23 PM

All replies