473,473 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.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
3 4186
"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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
2
by: Kalpesh Modha | last post by:
Hello I am writing a program that makes and window popup and plays a sound repeatedly until the user does soemthing. however directsound for some reason does not play the sound if is does not...
0
by: Michael | last post by:
I created a simple program that should play a file chosen in the OpenFileDialog: For some reason the program fails to play any wav file I choose the debugger breaks on the line: buffer=new...
2
by: Ian | last post by:
Does anyone know of a way where you can bind one of the Audio objects from DirectX.AudioVidoPlayback to a DirectX.DirectSound object? I need to do this because I need to be able to play several video...
0
by: Szafranr | last post by:
Hi I have litle problem with DirectSound I set DirectSound.Device CooperativeLevel on main form (Me) and when the sound is playing and main form lost focus then the sound is mutting :-( How...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
1
by: Maciek | last post by:
I use DirectSound to record sound and voice. I would like compress this data in realtime, but I cannot found any solution. Thanks, Maciek
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.