Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

recording sound from microphone and playing it

recording sound from microphone and playing it

recording sound from microphone and playing it

i'm trying to record sound  from microphone and play it.
i'm using directSound.
i get an error while trying to copy the data from the capture buffer to a secondary buffer...problem with the stream.
when i try to play for the first time i only here a "bump" sound.
well thanks in advance...
here is the code:

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;

// Avoid name collision with System.Buffer
using Buffer = Microsoft.DirectX.DirectSound.Buffer;

namespace Sound
    public class Sound : System.Windows.Forms.Form
        // private fields
        private Device device;
        private Capture capture;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private Buffer buffer;
        private BufferDescription bufferDesc;
        private CaptureBuffer captureBuffer;
        private WaveFormat waveFormat;
        private CaptureBufferDescription captureBuffDesc;
        private MemoryStream stream;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private byte[] streamBuffer;
    public Sound()
        // Set up DirectSound
        // Load the sound
        // Set the cooperative level

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

    private void CreateBuffer()
        buffer = new Buffer(bufferDesc, this.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 ,                         // The window for the application
        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];        

    #region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
    private void InitializeComponent()
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.button4 = new System.Windows.Forms.Button();
    // button1
    this.button1.Location = new System.Drawing.Point(48, 32);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(208, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "Play Sound";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // button2
    this.button2.Location = new System.Drawing.Point(48, 72);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(208, 23);
    this.button2.TabIndex = 1;
    this.button2.Text = "Stop Sound";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // button3
    this.button3.Location = new System.Drawing.Point(48, 136);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(208, 23);
    this.button3.TabIndex = 2;
    this.button3.Text = "Start Recording";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    // button4
    this.button4.Location = new System.Drawing.Point(48, 176);
    this.button4.Name = "button4";
    this.button4.Size = new System.Drawing.Size(208, 23);
    this.button4.TabIndex = 3;
    this.button4.Text = "Stop Recording";
    this.button4.Click += new System.EventHandler(this.button4_Click);
    // Game
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Sound";
    this.Text = "Sound";


    public static void Main ()
      Sound s = new Sound();

    private void button1_Click( object sender, System.EventArgs e)
        this.captureBuffer.Read(0, this.stream, 100000, LockFlag.None);
        //i get an error here...
        buffer.Write(0, this.stream, (int)this.stream.Length, LockFlag.None);
        buffer.Play(0, BufferPlayFlags.Looping);

    private void button2_Click(object sender, System.EventArgs e)

    private void button3_Click(object sender, System.EventArgs e)

    private void button4_Click(object sender, System.EventArgs e)

RE: recording sound from microphone and playing it


before putting the Capture Buffer into the Secondary Buffer try playing a simple Wav file with the Secondary Buffer.
It will init the stream header.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close