It is currently Sun Sep 27, 2020 3:48 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: DirectSound Recording Demo
PostPosted: Mon May 26, 2008 3:10 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Oct 24, 2005 1:05 pm
Posts: 739
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:
;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.

Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 26, 2008 1:38 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 24, 2006 9:40 am
Posts: 314
Error "Can't do DirectSoundCaptureCreate : -2005401480"

Vista 64bit SP1 in administrator mode.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 26, 2008 2:15 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sun Apr 27, 2003 4:41 pm
Posts: 1661
Location: Germany
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:
#SAMPLE_RATE            = 44100
#BIT_DEPTH              = 16

_________________
Good programmers don't comment their code. It was hard to write, should be hard to read.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 26, 2008 4:08 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 24, 2006 9:40 am
Posts: 314
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:
#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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 26, 2008 5:07 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sun Apr 27, 2003 4:41 pm
Posts: 1661
Location: Germany
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 26, 2008 6:28 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Oct 24, 2005 1:05 pm
Posts: 739
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 27, 2008 3:30 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 24, 2006 9:40 am
Posts: 314
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 27, 2008 11:56 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Oct 24, 2005 1:05 pm
Posts: 739
Quote:
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: DirectSound Recording Demo
PostPosted: Tue Dec 03, 2013 7:51 pm 
Offline
New User
New User

Joined: Wed Mar 27, 2013 12:14 am
Posts: 4
Error "Can't do DirectSoundCaptureCreate : -2005401480"

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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye