Home All Groups Group Topic Archive Search About

Basic DirectSound question

Author
19 Feb 2006 12:00 PM
Scott H
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

Author
19 Feb 2006 2:30 PM
Armin Zingler
Show quote Hide quote
"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
Author
19 Feb 2006 5:36 PM
Scott H
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.nospam@freenet.de> wrote:

Show quoteHide quote
>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
Author
19 Feb 2006 6:19 PM
Scott H
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(DSdes, 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)