Pages

luni, 12 august 2024

FASM : play a wav file with assembly programming language.

... this source code in FASM will play a hexa file using a window with two buttons, see my comments to deal with 'how to do it' ... :
; sound player example , tested with tada.wav from windows os
; convert wav file with
; tomeko.net/online_tools/file_to_hex.php online tool
; to text from tool , then add 'db ' in the front of Sound.inc text file like this:
; db 0x52, 0x49, 0x46, 0x46,
; this : 0x52, 0x49, 0x46, 0x46, is header for RIFF WAV file format

format PE GUI 4.0
entry start

include 'INCLUDE\win32a.inc'

ID_CAPTION         = 101
ID_MESSAGE         = 102
ID_ICONERROR       = 201
ID_ICONINFORMATION = 202
ID_ICONQUESTION    = 203
ID_ICONWARNING     = 204
ID_TOPMOST         = 301

section '.text' code readable executable

  start:

        invoke  GetModuleHandle,0
        invoke  DialogBoxParam,eax,37,HWND_DESKTOP,DialogProc,0
        invoke ExitProcess,0

proc DialogProc hwnddlg,msg,wparam,lparam
        push    ebx esi edi
        cmp     [msg],WM_INITDIALOG
        je      .wminitdialog
        cmp     [msg],WM_COMMAND
        je      .wmcommand
        cmp     [msg],WM_CLOSE
        je      .wmclose
        xor     eax,eax
        jmp     .finish
  .wminitdialog:

        jmp     .processed
  .wmcommand:
        cmp     [wparam],BN_CLICKED shl 16 + IDCANCEL    ; stop
        je      .player_stop

        cmp     [wparam],BN_CLICKED shl 16 + IDOK
        je      .player_play

        jmp     .processed

  .player_stop:
        invoke play, NULL, NULL, NULL     ; stop
        jmp     .processed

  .player_play:
        invoke play, sndFile, NULL, 0x0004 or 0x0001 ; 0x0004 = SND_MEMORY + ASYNC = 0x0001
        jmp     .processed

  .wmclose:
        invoke  EndDialog,[hwnddlg],0

  .processed:
        mov     eax,1
  .finish:
        pop     edi esi ebx
        ret
endp

section '.bss' readable writeable

  sndFile :
           ;db 'Sound.wav',0
  include 'Sound.inc';
section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL',\
          winmm,'winmm.dll'

  import winmm,\
          play, 'PlaySound'

  import kernel,\
         GetModuleHandle,'GetModuleHandleA',\
         ExitProcess,'ExitProcess'

  import user,\
         DialogBoxParam,'DialogBoxParamA',\
         EndDialog,'EndDialog'

section '.rsrc' resource data readable

  directory RT_DIALOG,dialogs

  resource dialogs,\
           37,LANG_ENGLISH+SUBLANG_DEFAULT,demonstration

  dialog demonstration,'SoundPlayer / _ (typedef)',70,76,110,30,WS_CAPTION+WS_POPUP+WS_SYSMENU+DS_MODALFRAME
    dialogitem 'BUTTON','Play',IDOK,10,10,45,15,WS_VISIBLE
    dialogitem 'BUTTON','Stop',IDCANCEL,54,10,45,15,WS_VISIBLE
  enddialog