DirectSound : pb to play audio stream

OKAÏDO

Newcomer
Joined
Nov 30, 2003
Messages
1
Location
PARIS
I have a pb with DirectSound.

I would like to make a mini synthetizer. I can't create a SecondaryBuffer with a MemoryStream :
  • C# : public SecondaryBuffer(Stream source, BufferDescription desc, Device parent);
  • VB.NET : Public Sub SecondaryBuffer(ByVal source As Stream, ByVal desc As BufferDescription, ByVal parent As Device)

A System.ArgumentException is thrown when I create the SecondaryBuffer.

Here's the code :

C#:
// stereo.
private const short CHANNELS = 2;

// 16 bits.
private const short BITS_PER_SAMPLE = 16;

// 2 bytes per sample.
private const int BYTES_PER_SAMPLE = BITS_PER_SAMPLE / 8;

// 44.1 kHz.
private const int SAMPLES_PER_SECOND = 44100;

// 176400 b/s.
private const int AVERAGE_BYTES_PER_SECOND = SAMPLES_PER_SECOND * CHANNELS * BYTES_PER_SAMPLE;

// 1 second buffer.
private const int BUFFER_SAMPLES = SAMPLES_PER_SECOND;

// 176400 bytes buffer.
private const int BUFFER_BYTES = BUFFER_SAMPLES * CHANNELS * BYTES_PER_SAMPLE;

// 4 bytes : stereo 16 bits sample size.
private const short BLOCK_ALIGN = CHANNELS * BYTES_PER_SAMPLE;

// sound device.
private Device m_dsDevice = null;

// buffer stream.
private MemoryStream m_stream;

// sound buffer.
private SecondaryBuffer m_dsBuffer = null;

// Synthetizer constructor.
// control is a valid System.Windows.Forms.Form.
public Synthetizer(Control control)
{
	// sound device creation.
	m_dsDevice = new Device();
	m_dsDevice.SetCooperativeLevel(control, CooperativeLevel.Priority);

	// buffer stream creation.
	m_stream = new MemoryStream();
	m_stream.SetLength(BUFFER_BYTES);

	// buffer infos.
	BufferDescription dsBuffDesc = new BufferDescription();
	WaveFormat dsFormat = new WaveFormat();
	dsFormat.BitsPerSample = BITS_PER_SAMPLE;
	dsFormat.SamplesPerSecond = SAMPLES_PER_SECOND;
	dsFormat.Channels = CHANNELS;
	dsFormat.AverageBytesPerSecond = AVERAGE_BYTES_PER_SECOND;
	dsFormat.BlockAlign = BLOCK_ALIGN;
	dsFormat.FormatTag = WaveFormatTag.Pcm;
	dsBuffDesc.Format = dsFormat;
	dsBuffDesc.BufferBytes = BUFFER_BYTES;
	dsBuffDesc.ControlPositionNotify = true;
	dsBuffDesc.CanGetCurrentPosition = true;

	// sound buffer creation.
	// System.ArgumentException on this line.
	m_dsBuffer = new SecondaryBuffer(m_stream, dsBuffDesc, m_dsDevice);
}

Please help me !
 
OKAIDO,
Have you set the variable:
m_stream = new MemoryStream();
to a value?
Also, is the device you are creating the stream on capable of the dsBuffdesc?

Jon
 
Back
Top