-3

Using this code (mostly just added the irvine32.inc to the github code here: https://gist.github.com/michaellindahl/7782978

TITLE MASM PlaySound                        (PlaySoundExample.asm)

includelib winmm.lib
INCLUDE Irvine32.inc
INCLUDE macros.inc
PlaySound PROTO,
        pszSound:PTR BYTE, 
        hmod:DWORD, 
        fdwSound:DWORD

.data
deviceConnect BYTE "DeviceConnect",0
SND_ALIAS    DWORD 00010000h
SND_RESOURCE DWORD 00040005h
SND_FILENAME DWORD 00020000h
file BYTE "t.wav",0

.code
main PROC
     INVOKE PlaySound, OFFSET deviceConnect, NULL, SND_ALIAS
     INVOKE PlaySound, OFFSET file, NULL, SND_FILENAME

    exit
main ENDP
END main
  • BTW, you don't need to store your constants in `.data`, you could define them as assemble-time constants like `SND_ALIAS = 00010000h` or with `equ`. Loading them from memory for every call is silly when they're the same size as an address. – Peter Cordes Dec 03 '20 at 17:00
  • that's fine and everything but i just need to know how to get this code to run in a way that the code doesn't stop until the entire .wav is played to progress – Satoko EntertainMent Dec 03 '20 at 17:03
  • That's why I said "BTW" and posted it as a comment, not an answer. The obvious approach would be to compile some C that calls it, and disassemble it. Or just write a C program that prints the constant with `printf("%x", SND_ASYNC);` – Peter Cordes Dec 03 '20 at 17:05
  • I should've clarified that I already tried to get the hex value, used cout< – Satoko EntertainMent Dec 03 '20 at 17:10
  • It looks like a flag that you might OR with other flags. (Another reason to use `=` constants, not data in memory). IDK, consult the docs for PlaySound, and/or look for working C examples. – Peter Cordes Dec 03 '20 at 17:15
  • MASM32 already has `SND_ASYNC` defined in include/windows.inc. And there is at least one example in the examples directory that uses it. – Michael Dec 03 '20 at 17:18
  • if i were to rewrite this code after including windows.inc, can you guide me how I'd manage that since my code is giving me a ridiculous amount of errors the minute i include windows.inc – Satoko EntertainMent Dec 03 '20 at 17:45

1 Answers1

1

You need to add SND_ASYNC flag additionally.

The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.

In MASM32, you need to OR their binary bits, so your function arg has both bits set like SND_ASYNC | SND_FILENAME in C.

#define SND_ASYNC           0x0001 
#define SND_FILENAME    0x00020000L

Code Sample:

.model flat, stdcall
option casemap: none

include windows.inc
include kernel32.inc
include user32.inc

includelib kernel32.lib
includelib user32.lib
includelib Winmm.lib

PlaySoundA PROTO,
        pszSound:PTR BYTE, 
        hmod:DWORD, 
        fdwSound:DWORD

.data
file BYTE "t.wav",0

szCaption   db  "Hello", 0
szText      db  "Hello World!", 0

.code
main PROC
     INVOKE PlaySoundA, OFFSET file, NULL, 20001H      ; SND_ASYNC | SND_FILENAME
     INVOKE MessageBox, NULL, addr szText, addr szCaption, MB_OK
     INVOKE ExitProcess, 0
main ENDP

END main
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Strive Sun
  • 5,988
  • 1
  • 9
  • 26
  • Thanks for this!!! I just figured out the SND_ASYNC value this morning when I printed "printf("%x", SND_FILENAME | SND_ASYNC);" which gave 20001H just like you are using (didn't know they were both being AND) , but when I ran the code INVOKE PlaySound, OFFSET file, NULL, 20001H myself it didn't play any sound, will try to run your code with the additional libraries then update. Guess it wouldn't work properly with just the Irvine library – Satoko EntertainMent Dec 04 '20 at 12:24
  • 1
    Interesting problem, I was having a "constant expected" issue in winextra.inc, the original code had: alrt_eventname WCHAR [EVLEN + 1] dup(?), alrt_servicename WCHAR [SNLEN + 1] dup(?) in the STD_ALERT struct which i fixed by removing the brackets. However when your code finally worked it only works for the duration of the message display box and doesn't stop until the message box has been removed. I just wanted an implementation where I could continue to push register values and do the other masm working while the music plays in the background – Satoko EntertainMent Dec 04 '20 at 12:51
  • 1
    In asm you should be able to define constants with `=` and then combine them with `|` in the source, just like in C. No need to hard-code an opaque `20001H` magic constant, especially without a comment. – Peter Cordes Dec 05 '20 at 15:45
  • @SatokoEntertainMent `MessageBox` is just an example (delete it at any time), to show that you can play wav files and pop up dialog box at the same time. You can add other operations that need to be completed below the `PlaySoundA` code. – Strive Sun Dec 07 '20 at 01:48
  • @SatokoEntertainMent You can also pause it at any time, using `INVOKE PlaySoundA, NULL, NULL, 20001H` – Strive Sun Dec 07 '20 at 01:51