News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

can the default MASM package make use of Dsound?

Started by supercoollee, September 24, 2011, 03:20:08 PM

Previous topic - Next topic

supercoollee

if i want to play several sounds at the same time, with changing volume and changing pitch of the sound, must i use DirectSound?
i like the least dependancy possible - no need to install extra softwares, so i won't like OpenAL unless i have to.
if it can be achieved with the MASM package downloaded from this site, it's the best.
and is there any examples on using Dsound with ASM?

ToutEnMasm

You can't use DirectSound with the masm32 package.You need to have the directx header files translate for ml.
If you are enough easy with this sort of problems, you can download :
http://www.masm32.com/board/index.php?topic=5428.msg40500#msg40500
He will made it.
Directx isn't the only one who allow to play sound,there is also openal.a sample exist:
http://www.masm32.com/board/index.php?topic=16016.msg142657#msg142657


baltoro

The simplest audio sound function to use is: PlaySound Function, MSDN.
Here is the MSDN overview on playing WAVE files: Waveform Audio.
...As I recall, PlaySound plays the files sequentially, so it may not be what you want. If you have an audio editor, you can combine your files into one WAVE file.

And,...you can use MIDI,...but, it's not much fun (and, often sounds really crappy because it requires that a software synthesizer is installed). MIDI is a fairly primitive protocol, but, if you have your music data in a MIDI file format, this is pretty simple.
Baltoro

dedndave

you might be able to play simultaneous sounds with PlaySound by using the SND_ASYNC flag
however, that will not allow you to alter the pitch or volume of individual WAV files

can you be more specific as to what kinds of sounds you are trying to generate ?
are they simple tones ? - or complex music/voices ?

if they are simple tones, you may want to look into MIDI files
i was playing with them a while back - and it looks like you can create the MIDI in memory and play it
i never got around to making it work, but it looks do-able   :P

supercoollee

i am not trying to play MIDI. what i want to play are wave forms like car engine, gun shot ...
these require volume and pitch control.
playsound function isn't the way to go.

dedndave

i am not so sure that you need to modify pitch and volume
the WAV file stores that info
it's not like the old DOS days when you had to make the BIOS timer do all the work

you just need to find or create the appropriate sound files

here is an example...
http://www.masm32.com/board/index.php?topic=15882.msg131239#msg131239

supercoollee

i am making a 3D program. in a 3D space, sounds change volume and pitch because of distance and motion.
i am looking into the codes of uFmod, maybe i can make it work with WAV files.

dedndave

i see - that is an interesting idea, although maybe not easy to implement   :P

BogdanOntanu

Quote from: supercoollee on September 25, 2011, 12:17:52 PM
i am making a 3D program. in a 3D space, sounds change volume and pitch because of distance and motion.
i am looking into the codes of uFmod, maybe i can make it work with WAV files.

Yes you can use DirectSound and yes your should use DirectSound and other DirectX components for such tasks. DirectSound allows the maximum level of control over sound play in Windows and an assembly programmer would normally want this kind of control.

You will need to either find some DirectX/DirectSound include files OR to convert them yourself from C DirectX SDL .h header files to assembly .inc include files.

Conceptually you will need to do this in order to initialize the system:
1) Create_Direct_Sound_Object
2) Set_Direct_Sound_Cooperative_Level
3) Create_Primary_Sound_Buffer
4) Primary_Set_Format

Then you can create any number of secondary sound buffers and play them.
DirectSound will mix then over the primary buffer and you will hear the sounds.

There are advanced DirectSound 3D APIs that will allow you to mimic the effects you are after: ie play sounds relatively to position in 2D/3D space and to simulate motion and  Doppler effects

In order to play a sound you should do:

a) Create_Secondary_Sound_Buffer
b) Lock_Secondary_Sound_Buffer
... load wav here, modify sound in any ways you like
c) UnLock_Secondary_Sound_Buffer
d) Play_Secondary_Sound_Buffer

This has been tested and works ok since it is the sequence of operations I use in Hostile Encounter game. An yes initially it was done with MASM32 hence it is possible.

Good luck in implementing it ;)
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

supercoollee

i came up with these:

1) initialize my program, load a standard PCM wave file into memory, address of data stored in address - chutewav.pointer

2) actual codes for sound initialization: (lines for error checking are omitted)

   mov   esi,offset sndpointer
   xor   ebx,ebx
   invoke   DirectSoundCreate,ebx,esi,ebx

   mov eax,[esi]
   mov ecx,[eax]
   push   2         ;DSSCL_PRIORITY
   push   offset hwnd
   push   eax
   call   dword ptr [ecx+18h]   ;IDirectSound::SetCooperativeLevel

   mov   eax,[esi]
   push   ebx
   push   offset sndbufpointer1
   push   offset sndbufdesc
   mov   ecx,[eax]
   push   eax
   call   dword ptr [ecx+0ch]   ;IDirectSound::CreateSoundBuffer

   push   ebx
   push   ebx
   push   ebx
   push   offset chutewav.sndlockinfo2
   push   offset chutewav.sndlockinfo1
   push   [sndbufdesc+8]
   push   ebx
   mov   eax,sndbufpointer1
   push   eax
   mov   ecx,[eax]
   call   dword ptr [ecx+2ch]   ;IDirectSoundBuffer::Lock ,   entire file locked

   mov   edx,chutewav.pointer
;   add   edx,2ch                      ; can remove wav file header
   invoke   MemCopy,edx,chutewav.sndlockinfo1,[sndbufdesc+8]
   push   ebx
   push   ebx
   push   chutewav.sndlockinfo2
   push   chutewav.sndlockinfo1
   mov   eax,sndbufpointer1
   push   eax
   mov   ecx,[eax]
   call   dword ptr [ecx+4ch]   ;IDirectSoundBuffer::Unlock

3) unload wave file from memory, program initialization ends here


4) when a button is pushed, do this:

   push   0
   push   0
   push   0
   mov   eax,sndbufpointer1
   mov   ecx,[eax]
   push   eax
   call   dword ptr [ecx+30h]        ;idirectsoundbuffer::play


===============================================


wav format and buffer desciption like this:

pcm      dd   00020001h         ;wFormatTag WAVE_FORMAT_PCM, nChannels 2
      dd   mixrate
      dd   mixrate*4
      dd   00100004h                   ;wBitsPerSample 16, nBlockAlign 4
      dd   0

sndbufdesc     dd   20      ;DSBUFFERDESC1, for old DX version compatibility
      dd   00014000h            ;dwFlags DSBCAPS_STICKYFOCUS OR DSBCAPS_GETCURRENTPOSITION2
      dd   0000a700h            ;dwBufferBytes, A700h is a temporary figure for testing
      dd   0
      dd   pcm

===============================================

but when i push the button, i don't hear the sound.
i tried altering PCM data, the program will crash if the data are not like the ones shown above.
i checked the wave data and pointers in memory, they are correctly loaded.
what is missing here?

supercoollee

one thing strange, i read about the functions in MSDN. in C/C++ , the functions request 1 less parameter than in MASM.
such as IDirectSoundBuffer::Lock request 7 parameters while in MASM there is 1 more parameter - the last one pushed. ( works this way in uFmod )
why is that?

BogdanOntanu

Quote from: supercoollee on September 25, 2011, 04:03:52 PM
one thing strange, i read about the functions in MSDN. in C/C++ , the functions request 1 less parameter than in MASM.
such as IDirectSoundBuffer::Lock request 7 parameters while in MASM there is 1 more parameter - the last one pushed. ( works this way in uFmod )
why is that?

Because in C++ examples there is a hidden pointer to "this" that is pushed by the compiler by default and this operation is hidden from the programmer in order to "simplify" things :D

However if you will find a standard ANSI C example then you will notice the extra parameter to that function...

ASM is similar to plain C in many aspects ;)


Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

ToutEnMasm

Quote
one thing strange, i read about the functions in MSDN. in C/C++ , the functions request 1 less parameter than in MASM
Nothing strange,it is ppv (This)the one parameter added by Masm.
ppv is an indirect pointer on the structure of COM functions .
C++ don't show it,but pass it as masm do.He is in all com call.
To get the pointer with ppv,you do this:
    mov edx, ppvIUnknown
    mov edx, [edx]

ToutEnMasm


Siekmanski