Hello!

Does anybody knows, how to init the DirectSoundDll an how to load and play a simple wavefile via DirectSound ?
Is there a little example everywhere?

Thanks, Nordwind.
Posted on 2001-12-26 08:30:43 by Nordwind64
After you init, you must load a wave file and place it in a secondarry buffer via: Lock-mem copy-Unlock . I recomend leting the primary sound buffer play forever and mixing diffrent secondary buffers into it.

Init can be done like this:



;************************************************
; DIRECT SOUND INIT
;*************************************************


extern DirectSoundCreate:PROC

extern DirectSoundEnumerateA:PROC


.data
sound_init_ok dd 0

lp_dsound_object dd 0

lp_dsound_buffer dd 0

sz_data db "data"
.code




Direct_Sound_Init:

call Create_Direct_Sound_Object

;================================
; test for success
; and dont do inits if no sound
;================================
mov eax,[sound_init_ok]
cmp eax,0
jz End_Direct_Sound_Init

;=============================
; continue intitializations
;=============================
call Set_Direct_Sound_Cooperative_Level

call Create_Primary_Sound_Buffer

call Play_Primary_Sound_Buffer

End_Direct_Sound_Init:

ret







;================================
; Create a DirectSound Object.
;================================
.data
szDirectSoundCreateFail db "Cant Create Direct Sound Object!",0
.code

Create_Direct_Sound_Object:
;---------------------------
mov eax,0
push eax

mov eax,offset lp_dsound_object
push eax

mov eax,0
push eax

call DirectSoundCreate

;call DirectSoundCreate, NULL, offset lp_dsound_object,NULL

cmp eax,DS_OK
jnz no_sound_init

;===============================
; flag we have sound enabled
;===============================
mov eax,1
mov [sound_init_ok],eax
ret

;====================================
; flag we have no sound
;====================================
no_sound_init:
mov eax,0
mov [sound_init_ok],eax
; call Fail, hwndmain, offset szDirectSoundCreateFail
ret


;============================
; Set Cooperative Level (greedly!)
;============================
.data
szDirectSoundCooperativeFail db "Cant Set Sound Cooperative Level",0
.code

Set_Direct_Sound_Cooperative_Level:
;-----------------------------------

; mov eax,DSSCL_EXCLUSIVE
mov eax,DSSCL_NORMAL
; mov eax,DSSCL_WRITEPRIMARY

push eax

mov eax,[hwndmain]
push eax

mov eax, [lp_dsound_object]
push eax
mov eax, [eax]
call [eax + DSO_SetCooperativeLevel]

; take action if it fails
.IF eax != DS_OK
call Fail, hwndmain, offset szDirectSoundCooperativeFail
jmp end_loop
.ENDIF

ret

;===============================
; Create Primary Sound Buffer
;===============================
.data

ds_buffdesc_primary DSBUFFERDESC4 <0>

szCreatePrimarySoundBufferFail db "Create Primary Sound Buffer Failed!",0

lp_dsbuff_primary dd 0
.code

Create_Primary_Sound_Buffer:

; fill descriptor structure
;=======================================
mov eax,size DSBUFFERDESC4
mov ds_buffdesc_primary.dwSize,eax

mov eax,DSBCAPS_PRIMARYBUFFER
mov ds_buffdesc_primary.dwFlags,eax

; parameters for create
;========================================
mov eax,0
push eax ;pkunker

mov eax,offset lp_dsbuff_primary ;pointer
push eax

mov eax,offset ds_buffdesc_primary ;descriptor
push eax

mov eax, [lp_dsound_object]
push eax
mov eax, [eax]
call [eax + DSO_CreateSoundBuffer]

; take action if it fails
;===========================
.IF eax != DS_OK
call Fail, hwndmain, offset szCreatePrimarySoundBufferFail
jmp end_loop
.ENDIF

ret

;=============================
; Lock Primary Sound Buffer
;=============================

.data
lp_pointer01 dd 0
lp_bytes01 dd 0

lp_pointer02 dd 0
lp_bytes02 dd 0
.code

Lock_Primary_Sound_Buffer:
mov eax,0 ;dwflags
push eax

mov eax,offset lp_bytes02
push eax

mov eax,offset lp_pointer02
push eax

mov eax,offset lp_bytes01
push eax

mov eax,offset lp_pointer01
push eax

mov eax,4096 ;bytes to lock
push eax

mov eax,0 ;position in buffer to lock
push eax


mov eax, [lp_dsbuff_primary]
push eax
mov eax, [eax]
call [eax + DSB_Lock]

; take action if it fails
;===========================
.IF eax != DS_OK
call Fail, hwndmain, offset szCreatePrimarySoundBufferFail
jmp end_loop
.ENDIF

ret


;================================
; UNLock Primary Sound Buffer
;================================
UnLock_Primary_Sound_Buffer:
mov eax,offset lp_bytes02
push eax

mov eax,offset lp_pointer02
push eax

mov eax,offset lp_bytes01
push eax

mov eax,offset lp_pointer01
push eax

mov eax, [lp_dsbuff_primary]
push eax
mov eax, [eax]
call [eax + DSB_UnLock]

; take action if it fails
;===========================
.IF eax != DS_OK
call Fail, hwndmain, offset szCreatePrimarySoundBufferFail
jmp end_loop
.ENDIF

ret


;==========================
; Play Primary Sound Buffer
;==========================
.data
szPlayPrimaryFail db "Play Primary Fail",0
.code

Play_Primary_Sound_Buffer:

mov eax,DSBPLAY_LOOPING ;dwFlags
push eax

mov eax,0
push eax

mov eax,0
push eax

mov eax, [lp_dsbuff_primary]
push eax
mov eax, [eax]
call [eax + DSB_Play]

; take action if it fails
;===========================
.IF eax != DS_OK
call Fail, hwndmain, offset szPlayPrimaryFail
jmp end_loop
.ENDIF

ret






;======================
; Release Direct Sound Object
;======================
Release_Direct_Sound_Objects:
cmp [sound_init_ok],1
jnz end_release_direct_sound_objects

mov eax, [lp_dsound_object]
push eax
mov eax, [eax]
call [eax + DSO_Release] ; IUnknown Release Method

end_release_direct_sound_objects:
ret




Posted on 2001-12-26 09:50:56 by BogdanOntanu
Here is how to play secondary sound buffer after init

(this are source samples from HE :) )
Please note that you can find more detailed examples, including .WAV file load in Chriss Hobbs's SpaceTris Example
that was posted here on this forum (a link). If you can not find it, ask and i will post it again...



;************************************************
; DIRECT SOUND CORE
;************************************************


;========================================
; Create Secondary Sound Buffer 01
;========================================
.data

ds_buffdesc_secondary01 DSBUFFERDESC4 <0>

szCreateSecondarySoundBufferFail db "Create Secondary Sound Buffer01 Failed!",0

lp_dsbuff_secondary01 dd 0

wfx_secondary01 WAVEFORMATEX <0>

bytes_per_sample equ 1

.code

Create_Secondary_Sound_Buffer01:
;===============================

; fill the wformatex structure
;====================================

mov [wfx_secondary01.dwSize],size WAVEFORMATEX
mov [wfx_secondary01.wFormatTag], WAVE_FORMAT_PCM
mov [wfx_secondary01.nChannels], 1
mov [wfx_secondary01.nSamplesPerSec], 22050
mov [wfx_secondary01.nBlockAlign],1
mov [wfx_secondary01.nAvgBytesPerSec],1*22050
mov [wfx_secondary01.wBitsPerSample],8

mov [ds_buffdesc_secondary01.lpwfxFormat],offset wfx_secondary01

; fill the dsound buffer descriptor structure
;============================================
mov eax,size DSBUFFERDESC4
mov ds_buffdesc_secondary01.dwSize,eax

; mov eax,DSBCAPS_GETCURRENTPOSITION2 or DSBCAPS_GLOBALFOCUS
; mov eax,DSBCAPS_STATIC
mov eax,DSBCAPS_CTRLDEFAULT

mov ds_buffdesc_secondary01.dwFlags,eax

mov eax,[length_wave01]
add eax,16
mov ds_buffdesc_secondary01.dwBufferBytes,eax

; parameters for CreateBuffer
;==========================================
mov eax,0
push eax ;pkunker=NULL

mov eax,offset lp_dsbuff_secondary01 ;pointer buffer
push eax

mov eax,offset ds_buffdesc_secondary01 ;pointer buffer descriptor
push eax

mov eax, [lp_dsound_object]
push eax
mov eax, [eax]
call [eax + DSO_CreateSoundBuffer]

; take action if it fails
;===========================
.IF eax != DS_OK
call Fail, hwndmain, offset szCreateSecondarySoundBufferFail
jmp end_loop
.ENDIF

ret







;========================================
; Lock Secondary Sound Buffer
;========================================

.data
lp_pointer01b dd 0
lp_bytes01b dd 0

lp_pointer02b dd 0
lp_bytes02b dd 0
.code

Lock_Secondary_Sound_Buffer:
;===============================

mov eax,0 ;dwflags
push eax

mov eax,offset lp_bytes02b
push eax

mov eax,offset lp_pointer02b
push eax

mov eax,offset lp_bytes01b
push eax

mov eax,offset lp_pointer01b
push eax

; mov eax,22050*3 ;bytes to lock (3s)
mov eax,[length_wave01] ;bytes to lock (3s)
push eax

mov eax,0 ;position in buffer to lock
push eax


mov eax, [lp_dsbuff_secondary01]
push eax
mov eax, [eax]
call [eax + DSB_Lock]

; take action if it fails
;===========================
.IF eax != DS_OK
call Fail, hwndmain, offset szCreatePrimarySoundBufferFail
jmp end_loop
.ENDIF

ret


;========================================
; UNLock Secondary Sound Buffer
;========================================
UnLock_Secondary_Sound_Buffer:
;===============================

mov eax,[lp_bytes02b]
push eax

mov eax,[lp_pointer02b]
push eax

mov eax,[lp_bytes01b]
push eax

mov eax,[lp_pointer01b]
push eax

mov eax, [lp_dsbuff_secondary01]
push eax
mov eax, [eax]
call [eax + DSB_UnLock]

; take action if it fails
;===========================
.IF eax != DS_OK
call Fail, hwndmain, offset szCreatePrimarySoundBufferFail
jmp end_loop
.ENDIF

ret




;=========================================
; Play Secondary Sound Buffer
; in: lp_dsbuff_secondary
;=========================================
.data
szPlaySecondaryFail db "Play Secondary Fail",0
type_play dd 0 ;0 sau DSBPLAY_LOOPING
.code

Play_Secondary_Sound_Buffer:
;===============================

mov eax,[type_play] ;dwFlags
push eax

mov eax,0
push eax

mov eax,0
push eax

mov eax, [lp_dsbuff_secondary01]
push eax
mov eax, [eax]
call [eax + DSB_Play]

; take action if it fails
;===========================
.IF eax != DS_OK
call Fail, hwndmain, offset szPlaySecondaryFail
jmp end_loop
.ENDIF

ret



;========================================


Stop_Secondary_Sound_Buffer:
;===============================
mov eax, [lp_dsbuff_secondary01]
push eax
mov eax, [eax]
call [eax + DSB_Stop]

; take action if it fails
;===========================
.IF eax != DS_OK
call Fail, hwndmain, offset szPlaySecondaryFail
jmp end_loop
.ENDIF

ret

Goto_Start_Secondary_Sound_Buffer:
;===============================
mov eax,0 ;start position
push eax

mov eax, [lp_dsbuff_secondary01]
push eax
mov eax, [eax]
call [eax + DSB_SetCurrentPosition]

; take action if it fails
;===========================
.IF eax != DS_OK
call Fail, hwndmain, offset szPlaySecondaryFail
jmp end_loop
.ENDIF

ret

Posted on 2001-12-26 09:55:31 by BogdanOntanu
Hallo!

Thank you very much for your great help !!!
And yes, were can I find more infos?

Greetings, Nordwind
Posted on 2001-12-26 16:15:21 by Nordwind64