How to Create Wave Data From Buffers in VB6
In VB6, you can use DirectX to create audio, video and graphics Windows applications. The DirectX SDK is free from Microsoft website. The DirectSound object is the main audio device object. It can be used for sound effects in games, and it can handle multiple sounds at one time. Control the audio system through DirectSound such as panning and volume. In VB6, you can create a project to read buffer data into a WAV file via the DirectX objects.
Instructions
-
-
1
Click “Start,” “All Programs” and “Visual Basic” to open VB6. Click “File” and “New” to create a new project. Enter a name for the project. Specify a location for the project files.
-
2
Click “Project” and “References” to add DirectX reference for the project. Tick “DirectX 8 for Visual Basic Type Library” on the “References” interface. Click “OK.”
-
-
3
Define variables for the project. Add the following code to the Form1.vb:
Dim dx As New DirectX8
Dim ds As DirectSound8
Dim buffer1 As DirectSoundSecondaryBuffer8
Dim buffer2 As DSBUFFERDESC -
4
Initialize DirectSound via code:
Set dx=New DirectX8
Set ds=dx.DirectSoundCreate(“”) -
5
Create a buffer via code:
Buffer2.fxFormat.nFormatTag = WAVE_FORMAT_PCM
Buffer2.fxFormat.nSize = 0
Buffer2.fxFormat.lExtra = 0
Buffer2.fxFormat.nChannels = 1
Buffer2.fxFormat.lSamplesPerSec = SRATE
Buffer2.fxFormat.nBitsPerSample = 16
Buffer2.fxFormat.nBlockAlign = 2
Buffer2.fxFormat.lAvgBytesPerSec = 2 * SRATE
Buffer2.lFlags = 0
Buffer2.lBufferBytes = 2 * DUR * SRATE
Set dsToneBuffer = DS.CreateSoundBuffer(buffer2) -
6
Create a tone and write to dsToneBuffer:
Dim i
For i = 0 To DUR * SRATE
sbuf(i) = 10000 * Sin(2 * PI * FREQ * i / SRATE)
Next i
dsToneBuffer.WriteBuffer 0, 2 * DUR * SRATE, sbuf(0), DSBLOCK_DEFAULT -
7
Write the buffer data to a WAV file:
DS.CreateSoundBufferToFile(App.Path & "\my.wav", dsToneBuffer)
-
1
References
- Photo Credit Jupiterimages/Photos.com/Getty Images