By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,556 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,556 IT Pros & Developers. It's quick & easy.

Basic DirectSound question

P: n/a
I'm just starting to learn directaudio, and want to start off not with
playing a WAV, thats all too easy, I want to generate the waveform, so
to start off I'm trying to generate white noise, using the code below,
but its erroring with a "Index out of range" Exception on the line of
code inditcated:
Private DSdev As New Device
Private DSbuffer As SecondaryBuffer
Private IOstream As System.IO.MemoryStream
Now some code to make it happen:
DSdev.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)
IOstream = New System.IO.MemoryStream(22000)

Dim x As Integer
For x = 1 To 20000
IOstream.WriteByte(Int(Rnd() * 254))
Next

DSbuffer = New SecondaryBuffer(IOstream, DSdev) '<<<< ERROR

If Not DSbuffer Is Nothing Then DSbuffer.Play(0,
BufferPlayFlags.Default)

----------------------
Is this the right way to go about generating white noise?

Thanks in advance,
Scott
Feb 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Scott H" <x@x.com> schrieb
I'm just starting to learn directaudio, and want to start off not
with playing a WAV, thats all too easy, I want to generate the
waveform, so to start off I'm trying to generate white noise, using
the code below, but its erroring with a "Index out of range"
Exception on the line of code inditcated:
Private DSdev As New Device
Private DSbuffer As SecondaryBuffer
Private IOstream As System.IO.MemoryStream
Now some code to make it happen:
DSdev.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)
IOstream = New System.IO.MemoryStream(22000)

Dim x As Integer
For x = 1 To 20000
IOstream.WriteByte(Int(Rnd() * 254))
Next
IOstream.position = 0
DSbuffer = New SecondaryBuffer(IOstream, DSdev) '<<<< ERROR


This solves the index out of range exception. Despite, I get another
exception, here, that I can not solve with a lot debugging.

You should try the MDX group @
microsoft.public.win32.programmer.directx.managed
Armin

Feb 19 '06 #2

P: n/a

Thanks for your help anyway, Its a step closer, I'll try the other
newsgroup.
On Sun, 19 Feb 2006 15:30:56 +0100, "Armin Zingler"
<az*******@freenet.de> wrote:
IOstream.position = 0
DSbuffer = New SecondaryBuffer(IOstream, DSdev) '<<<< ERROR


This solves the index out of range exception. Despite, I get another
exception, here, that I can not solve with a lot debugging.

You should try the MDX group @
microsoft.public.win32.programmer.directx.manag ed
Armin

Feb 19 '06 #3

P: n/a
Success!
It seems alot of people get stuck there, I found a C# example,
converted it to VB and got lots of errors, so just "jigged" around
with it a bit, and finally got it going, playing 1 second of white
noise!!!

Here's the code, for Armin and anyone else who wants to do this.

'DEFINITIONS:
Private DSdev As New Device
Private DSbuffer As SecondaryBuffer
Private DSdes As BufferDescription
Private DSformat As WaveFormat

'CODE IN SOME SUB/FUNCTION
DSdev.SetCooperativeLevel(Me.Handle, CooperativeLevel.Normal)

'define the wave format
DSformat = New WaveFormat()
DSformat.BitsPerSample = 8
DSformat.Channels = 1
DSformat.BlockAlign = 1
DSformat.FormatTag = WaveFormatTag.Pcm
DSformat.SamplesPerSecond = 8000
DSformat.AverageBytesPerSecond = DSformat.SamplesPerSecond *
DSformat.BlockAlign

'buffer description
DSdes = New BufferDescription(DSformat)
DSdes.BufferBytes = 3 * DSformat.AverageBytesPerSecond

'create the buffer
DSbuffer = New
Microsoft.DirectX.DirectSound.SecondaryBuffer(DSde s, DSdev)

'generate ramdom data (white noise)
Dim rawsamples(22050) As Byte
Dim rnd1 = New System.Random()

Dim i As Integer
For i = 0 To 22050
rawsamples(i) = rnd1.Next(255)
Next i

' load audio samples to secondary buffer
DSbuffer.Write(0, rawsamples, LockFlag.EntireBuffer)

'play audio buffer
DSbuffer.Play(0, BufferPlayFlags.Default)
Feb 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.