DirectSound Recording Demo

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

DirectSound Recording Demo

Post by chris319 »

Here is a very simple program which demonstrates DirectSound audio recording with PureBasic. It records 10 seconds of audio in stereo from the default recording device at a sample rate of 48 kHz and a bit depth of 24 bits, then saves the contents of the capture buffer as a wav file named "test.wav". No window is opened as none is needed.

Please report any problems. Thank you.

EDITED ON 6/1/2008: Set default audio settings to 16 bits/44100 Hz

Code: Select all

;DIRECTSOUND RECORD PROGRAM
;TESTED ON PUREBASIC 4.20
;chris319
;5/25/2008

;THIS PROGRAM RECORDS 10 SECONDS OF AUDIO IN STEREO
;FROM THE DEFAULT RECORDING DEVICE, THEN SAVES THE
;AUDIO AS A WAV FILE NAMED "test.wav". YOU CAN FOOL
;AROUND WITH CHANNELS, SAMPLE RATE AND BIT DEPTH HERE:

#CHANNELS               = 2
#SAMPLE_RATE            = 44100
#BIT_DEPTH              = 16

#WAVE_FORMAT_PCM        = $0001

#DS_OK = 0 
#LOOP_FLAG = 0
#DSCBLOCK_ENTIREBUFFER = 1
#SECONDS = 10

Global my_wfe.WAVEFORMATEX ;THE FAMILIAR WAVEFORMATEX STRUCTURE
my_wfe\wFormatTag      = #WAVE_FORMAT_PCM
my_wfe\nChannels       = #CHANNELS         ;dwPrimaryChannels; 
my_wfe\nSamplesPerSec  = #SAMPLE_RATE      ;dwPrimaryFreq; 
my_wfe\wBitsPerSample  = #BIT_DEPTH        ;dwPrimaryBitRate; 
my_wfe\nBlockAlign     = (my_wfe\wBitsPerSample / 8 * my_wfe\nChannels) 
my_wfe\nAvgBytesPerSec = (my_wfe\nSamplesPerSec * my_wfe\nBlockAlign) 
my_wfe\cbSize = 0

Global *bufptr ;POINTER TO CAPTURE BUFFER
Global bufsize.l ;SIZE OF CAPTURE BUFFER

;DIRECTSOUND BUFFER
Structure DSCBUFFERDESC
  dwSize.l            ; Size of the structure, in bytes. This member must be initialized before the structure is used. 
  dwFlags.l           ; Flags specifying the capabilities of the buffer 
  dwBufferBytes.l     ; Size of capture buffer to create, in bytes.
  dwReserved.l        ; Must be 0 
  *lpwfxFormat        ; Address of a WAVEFORMATEX or WAVEFORMATEXTENSIBLE structure specifying the waveform format for the buffer. 
EndStructure 


Procedure Delete(*obj.IUnknown) 
  ProcedureReturn *Obj\Release() 
EndProcedure 

Procedure Error_Msg(String.s) 
 MessageRequester("Error",String.s,0) 
 End 
EndProcedure 

Procedure File_Save()
;SAVE BUFFER AS A WAV FILE

If CreateFile(1, "test.wav") = 0
  MessageRequester("Error", "Unable to create file.", #MB_ICONERROR)
  End
  ProcedureReturn
EndIf

subchunk1size.l = SizeOf(WAVEFORMATEX)
subchunk2size.l = bufsize
chunksize = 4 + (8 + SizeOf(WAVEFORMATEX)) + (8 + subchunk2size)

samprate.l = my_wfe\nSamplesPerSec
byterate.l = my_wfe\nSamplesPerSec * my_wfe\nAvgBytesPerSec ;IS THIS RIGHT?
blockalign.w = my_wfe\nChannels * (my_wfe\wBitsPerSample / 8)
bitspersample.w = my_wfe\wBitsPerSample

my_WFE\cbSize = 0
chunksize = chunksize + my_WFE\cbSize

my_WFE\wFormatTag = #WAVE_FORMAT_PCM

;WRITE WAV HEADER
WriteString(1, "RIFF") ; 4 bytes
WriteLong(1, chunksize) ; 4 bytes
WriteString(1, "WAVE") ; 4 bytes
WriteString(1, "fmt ") ; 4 bytes
WriteLong(1, subchunk1size) ; 4 bytes
WriteData(1, my_WFE, SizeOf(WAVEFORMATEX))
;END OF WAVEFORMATEX STRUCTURE

WriteString(1, "data", #PB_Ascii) ; 4 bytes
WriteLong(1, subchunk2size) ; 4 bytes
;END OF FILE HEADER

;WRITE AUDIO DATA AFTER WAV HEADER
WriteData(1, *bufptr, bufsize)
CloseFile(1)

EndProcedure

;CREATE DIRECTSOUND CAPTURE OBJECT 
*DirectSound.IDirectSoundCapture
result.l = DirectSoundCaptureCreate_(0, @*DirectSound, 0)
If Result <> #DS_OK 
 Error_Msg("Can't do DirectSoundCaptureCreate : " + Str(Result.l)) 
EndIf 

;SET UP CAPTURE BUFFER 
dscbd.DSCBUFFERDESC                         ; Set up structure
dscbd\dwSize        = SizeOf(DSCBUFFERDESC) ; Save structure size 
dscbd\dwFlags       = 0                     ; It is the primary Buffer (see DSound.h) 
dscbd\dwBufferBytes = my_wfe\nAvgBytesPerSec * #SECONDS
dscbd\dwReserved    = 0
dscbd\lpwfxFormat   = @my_WFE
 
result = *DirectSound\CreateCaptureBuffer(@dscbd, @*pDSCB.IDirectSoundCaptureBuffer, 0)
If result <> #DS_OK 
 Error_Msg("Can't set up directsound buffer : " + Str(Result)) 
EndIf 

;START RECORDING
result = *pDSCB.IDirectSoundCaptureBuffer\Start(#LOOP_FLAG)
If result <> #DS_OK 
  Error_Msg("Can't start : " + Str(Result))
Else
  Debug "Recording started"
EndIf 

;RECORD FOR SPECIFIED NUMBER OF SECONDS
Delay(#SECONDS * 1000)

;STOP RECORDING
result = *pDSCB.IDirectSoundCaptureBuffer\Stop()
If result <> #DS_OK 
  Error_Msg("Can't stop : " + Str(Result)) 
Else
  Debug "Recording stopped"
EndIf 

;LOCK THE BUFFER BEFORE SAVING
result = *pDSCB.IDirectSoundCaptureBuffer\Lock(0, my_wfe\nAvgBytesPerSec * seconds, @*bufptr, @bufsize, 0, 0, #DSCBLOCK_ENTIREBUFFER)
If result <> #DS_OK 
  Error_Msg("Can't lock : " + Str(Result)) 
EndIf 

;SAVE BUFFER CONTENTS AS A WAV FILE
Debug "Saving"
File_Save()

;UNLOCK THE BUFFER
result = *pDSCB.IDirectSoundCaptureBuffer\Unlock(*bufptr, bufsize, 0, 0)
If result <> #DS_OK 
  Error_Msg("Can't unlock : " + Str(Result)) 
EndIf 

Debug "Normal program termination"

End
Last edited by chris319 on Sun Jun 01, 2008 9:39 pm, edited 1 time in total.
Ramihyn_
Enthusiast
Enthusiast
Posts: 314
Joined: Fri Feb 24, 2006 9:40 am

Post by Ramihyn_ »

Error "Can't do DirectSoundCaptureCreate : -2005401480"

Vista 64bit SP1 in administrator mode.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Ramihyn_ wrote:Error "Can't do DirectSoundCaptureCreate : -2005401480"

Vista 64bit SP1 in administrator mode.
Does your soundcard support 48kHz/24bit ?
Does it work when you change the following constants?

Code: Select all

#SAMPLE_RATE            = 44100
#BIT_DEPTH              = 16
Good programmers don't comment their code. It was hard to write, should be hard to read.
Ramihyn_
Enthusiast
Enthusiast
Posts: 314
Joined: Fri Feb 24, 2006 9:40 am

Post by Ramihyn_ »

traumatic wrote:
Ramihyn_ wrote:Error "Can't do DirectSoundCaptureCreate : -2005401480"

Vista 64bit SP1 in administrator mode.
Does your soundcard support 48kHz/24bit ?
Does it work when you change the following constants?

Code: Select all

#SAMPLE_RATE            = 44100
#BIT_DEPTH              = 16
It gives the same error with 44Khz/16Bit as the 48khz/24bit setting.

I dont have a dedicated soundcard in that machine and use the Mainboards audio connectors. The Mainboard is a Gigabyte GA-P35-S3G and the block diagram/manual say that the sound is created by the ICH9 Chip which seems to contain a Realtek AL662 codec.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Hmm... it works fine here, maybe chris319 can say something more to this... :)
Good programmers don't comment their code. It was hard to write, should be hard to read.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Post by chris319 »

Ramihyn_ wrote:Error "Can't do DirectSoundCaptureCreate : -2005401480"

Vista 64bit SP1 in administrator mode.
Which version of PureBasic are you running? This code was developed on 32-bit Vista with SP1 and PureBasic 4.20. I can't imagine why it would work with 32-bit Vista but not 64-bit.
Ramihyn_
Enthusiast
Enthusiast
Posts: 314
Joined: Fri Feb 24, 2006 9:40 am

Post by Ramihyn_ »

chris319 wrote:Which version of PureBasic are you running? This code was developed on 32-bit Vista with SP1 and PureBasic 4.20. I can't imagine why it would work with 32-bit Vista but not 64-bit.
The source header said "4.20" so i ran it on 4.20B6 as i dont have the final installed. The error number indicates a sound driver error and i will try to re-install sound drivers when i have time - but i doubt it is a corrupt driver as this is a "clean" installation and sound output works well. Sound recording doesnt work with goldwave either on this machine.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Post by chris319 »

Sound recording doesnt work with goldwave either on this machine.
That's a clue. Do you have a primary recording device set up? What does Goldwave say when you examine the devices?
yazak
New User
New User
Posts: 4
Joined: Wed Mar 27, 2013 12:14 am

Re: DirectSound Recording Demo

Post by yazak »

Error "Can't do DirectSoundCaptureCreate : -2005401480"

This error is caused by not having a microphone plugged into your computer :shock:
Post Reply