Basic DirectSound question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott H

    #1

    Basic DirectSound question

    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.Memor yStream


    Now some code to make it happen:


    DSdev.SetCooper ativeLevel(Me.H andle, CooperativeLeve l.Normal)
    IOstream = New System.IO.Memor yStream(22000)

    Dim x As Integer
    For x = 1 To 20000
    IOstream.WriteB yte(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
  • Armin Zingler

    #2
    Re: Basic DirectSound question

    "Scott H" <x@x.com> schrieb[color=blue]
    > 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.Memor yStream
    >
    >
    > Now some code to make it happen:
    >
    >
    > DSdev.SetCooper ativeLevel(Me.H andle, CooperativeLeve l.Normal)
    > IOstream = New System.IO.Memor yStream(22000)
    >
    > Dim x As Integer
    > For x = 1 To 20000
    > IOstream.WriteB yte(Int(Rnd() * 254))
    > Next[/color]

    IOstream.positi on = 0
    [color=blue]
    > DSbuffer = New SecondaryBuffer (IOstream, DSdev) '<<<< ERROR[/color]

    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.publi c.win32.program mer.directx.man aged


    Armin

    Comment

    • Scott H

      #3
      Re: Basic DirectSound question


      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@free net.de> wrote:
      [color=blue]
      >IOstream.posit ion = 0
      >[color=green]
      >> DSbuffer = New SecondaryBuffer (IOstream, DSdev) '<<<< ERROR[/color]
      >
      >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.publ ic.win32.progra mmer.directx.ma naged
      >
      >
      >Armin[/color]

      Comment

      • Scott H

        #4
        Re: Basic DirectSound question

        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 BufferDescripti on
        Private DSformat As WaveFormat

        'CODE IN SOME SUB/FUNCTION
        DSdev.SetCooper ativeLevel(Me.H andle, CooperativeLeve l.Normal)

        'define the wave format
        DSformat = New WaveFormat()
        DSformat.BitsPe rSample = 8
        DSformat.Channe ls = 1
        DSformat.BlockA lign = 1
        DSformat.Format Tag = WaveFormatTag.P cm
        DSformat.Sample sPerSecond = 8000
        DSformat.Averag eBytesPerSecond = DSformat.Sample sPerSecond *
        DSformat.BlockA lign

        'buffer description
        DSdes = New BufferDescripti on(DSformat)
        DSdes.BufferByt es = 3 * DSformat.Averag eBytesPerSecond

        'create the buffer
        DSbuffer = New
        Microsoft.Direc tX.DirectSound. SecondaryBuffer (DSdes, DSdev)

        'generate ramdom data (white noise)
        Dim rawsamples(2205 0) 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.Entire Buffer)

        'play audio buffer
        DSbuffer.Play(0 , BufferPlayFlags .Default)

        Comment

        Working...