1
Answer

Recording Sound Using Direct Sound !

Hi , What is gonna wrong in this code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using System.IO;
using Buffer = Microsoft.DirectX.DirectSound.SecondaryBuffer;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
CaptureBuffer captureBuffer;
CaptureBufferDescription captureDes = new CaptureBufferDescription();
WaveFormat waveFormat = new WaveFormat();
Capture captureDevice = new Capture();
Device sndDevice;
SecondaryBuffer buffer;
StreamWriter wStream;
StreamReader wReader;
char[] bytes;
char[] bytesRead;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{


captureDes.BufferBytes = 192000;
waveFormat.BitsPerSample = 8;
waveFormat.Channels = 1;
waveFormat.SamplesPerSecond = 22050;
waveFormat.BlockAlign = (short)(waveFormat.Channels * (waveFormat.BitsPerSample/8));
waveFormat.AverageBytesPerSecond = waveFormat.BlockAlign * waveFormat.SamplesPerSecond;
captureDes.Format = waveFormat;
waveFormat.BlockAlign = 1;
captureDes.Format = waveFormat;
captureDes.BufferBytes = 100000;
captureDes.ControlEffects = false;
captureDes.WaveMapped = true;
captureBuffer = new Microsoft.DirectX.DirectSound.CaptureBuffer(captureDes, captureDevice);
bytes = new char[100000];
bytesRead = new char[100000];
sndDevice = new Device();
sndDevice.SetCooperativeLevel(this, CooperativeLevel.Normal);


}

private void btnPlay_Click(object sender, EventArgs e)
{
wReader = new StreamReader("sap.wav");
while (wReader.EndOfStream == false)
{
wReader.Read(bytesRead, 0, 100000);
}
buffer = new SecondaryBuffer("sap.wav", sndDevice);
buffer.Play(0, BufferPlayFlags.Looping);
}

private void btnStop_Click(object sender, EventArgs e)
{
captureBuffer.Stop();
wStream.Close();
}

private void btnRecord_Click(object sender, EventArgs e)
{
wStream = new StreamWriter("sap.wav");
captureBuffer.Start(true);
for (int i = 0; i < 100000; i++)
{
bytes[i] = (char)captureBuffer.Caps.BufferBytes;
wStream.Write(bytes[i]);
}
}
}
}

Thanks in advance .
SAP
Answers (1)