Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

DirectSound - recording sound from microphone and playing it

TaLzTaLz Posts: 2Member
hello,
i am trying to record sound from a microphone and play it.
i created a capture object..recorded sound and transfered it to a secondary buffer via memory stream.
when i try to play the secondary buffer i get a short "bump" sound...after checking with the debugger i noticed that it passes the "secondaryBuffer.play" line very fast (not really playing).
the buffer size is 60000 bytes long.
the capture buffer, memory buffer (for the stream) and the secondary buffer are all the same size so i don't need any notifications.
i am writing my program in c#.
in need of help,
TaLz

Comments

  • TaLzTaLz Posts: 2Member
    i think I have a problem with the stream I'm using to transfer data between the captureBuffer and the buffer I use to play the sound with.

    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;

    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()
    {
    InitializeComponent();
    // Set up DirectSound
    CreateDevice();
    // Load the sound
    CreateWaveFormat();
    CreateBufferDescription();
    CreateBuffer();

    // Set the cooperative level
    SetCooperativeLevel();

    CreateCapture();
    CreateCapureDescription();
    CreateCaptureBuffer();
    CreateStreamBuffer();
    CreateStream();
    }

    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()
    {
    device.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];
    for(int i=0; i<100000; i++)
    streamBuffer[i] = 0;
    }



    #region Windows Form Designer generated code
    ///<summary>
    /// 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();
    this.SuspendLayout();
    //
    // 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.Controls.Add(this.button4);
    this.Controls.Add(this.button3);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Name = "Sound";
    this.Text = "Sound";
    this.ResumeLayout(false);

    }
    #endregion

    public static void Main ()
    {
    Sound s = new Sound();
    s.Show();
    Application.Run(s);
    }

    private void button1_Click( object sender, System.EventArgs e)
    {
    this.captureBuffer.Read(0, this.stream, 100000, LockFlag.None);
    buffer.Write(0, this.stream, (int)this.stream.Length, LockFlag.EntireBuffer);
    buffer.Play(0, BufferPlayFlags.Looping);
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
    buffer.Stop();
    }

    private void button3_Click(object sender, System.EventArgs e)
    {
    this.captureBuffer.Start(true);
    }

    private void button4_Click(object sender, System.EventArgs e)
    {
    this.captureBuffer.Stop();
    }


    }
    }



  • A. L.A. L. Posts: 7Member
    : i think I have a problem with the stream I'm using to transfer data between the captureBuffer and the buffer I use to play the sound with.
    :
    : 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;
    :
    : 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()
    : {
    : InitializeComponent();
    : // Set up DirectSound
    : CreateDevice();
    : // Load the sound
    : CreateWaveFormat();
    : CreateBufferDescription();
    : CreateBuffer();
    :
    : // Set the cooperative level
    : SetCooperativeLevel();
    :
    : CreateCapture();
    : CreateCapureDescription();
    : CreateCaptureBuffer();
    : CreateStreamBuffer();
    : CreateStream();
    : }
    :
    : 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()
    : {
    : device.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];
    : for(int i=0; i<100000; i++)
    : streamBuffer[i] = 0;
    : }
    :
    :
    :
    : #region Windows Form Designer generated code
    : ///<summary>
    : /// 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();
    : this.SuspendLayout();
    : //
    : // 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.Controls.Add(this.button4);
    : this.Controls.Add(this.button3);
    : this.Controls.Add(this.button2);
    : this.Controls.Add(this.button1);
    : this.Name = "Sound";
    : this.Text = "Sound";
    : this.ResumeLayout(false);
    :
    : }
    : #endregion
    :
    : public static void Main ()
    : {
    : Sound s = new Sound();
    : s.Show();
    : Application.Run(s);
    : }
    :
    : private void button1_Click( object sender, System.EventArgs e)
    : {
    : this.captureBuffer.Read(0, this.stream, 100000, LockFlag.None);
    : buffer.Write(0, this.stream, (int)this.stream.Length, LockFlag.EntireBuffer);
    : buffer.Play(0, BufferPlayFlags.Looping);
    : }
    :
    : private void button2_Click(object sender, System.EventArgs e)
    : {
    : buffer.Stop();
    : }
    :
    : private void button3_Click(object sender, System.EventArgs e)
    : {
    : this.captureBuffer.Start(true);
    : }
    :
    : private void button4_Click(object sender, System.EventArgs e)
    : {
    : this.captureBuffer.Stop();
    : }
    :
    :
    : }
    : }
    :
    :
    :
    I know it's kind of late, but I just came across this post. I'm interested in capturing sounds also, but from the sound card. Has anyone solved the above problem? I would really appreciate any replies.
  • schiftschift Posts: 4Member
    This post has been deleted.
Sign In or Register to comment.