The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: anuradha on October 18, 2007, 04:44:10 AM

Title: mp3 in resource script
Post by: anuradha on October 18, 2007, 04:44:10 AM
hii
is it possiable to add a mp3 file using a resource script????
thanks
Title: Re: mp3 in resource script
Post by: evlncrn8 on October 18, 2007, 08:37:55 AM
yup...

i think you just make it RC_DATA and include the file into it

mymp3data RCDATA DISCARDABLE "mymp3file.mp3"

so the resource fill be called 'mymp3data'


Title: Re: mp3 in resource script
Post by: anuradha on November 01, 2007, 04:43:11 AM
hi
first thanks for the replyevlncrn8.
it worked.but the problem is I can't play that file using MCI functions.
is their any way I can Play this???
thanks
Title: Re: mp3 in resource script
Post by: evlncrn8 on November 01, 2007, 02:10:56 PM
yeh you need to dump it to a temp file, then mci can play from it...
Title: Re: mp3 in resource script
Post by: anuradha on November 05, 2007, 07:39:56 AM
hi
thanks for the tip.can you tell me how can I dump the .MP3?????
is their any API for that???
thanks.
Title: Re: mp3 in resource script
Post by: hutch-- on November 05, 2007, 07:41:43 AM
Just use a WriteFile operation from the start address of the embedded MP3 with the correct byte count for the API.
Title: Re: mp3 in resource script
Post by: evlncrn8 on November 05, 2007, 10:39:45 AM
FindResource -> SizeOfResource -> LoadREsource -> LockResource -> CreateFile -> WriteFile -> FlushFileBuffers -> CloseHandle -> FreeResource

though the FreeResource isn't needed for win32 apps.. some redundancy there, but i think that shows you the steps you have to go through
Title: Re: mp3 in resource script
Post by: OldTimer on November 05, 2007, 12:42:37 PM
Hi  anuradha,    this code snippet works under XP.  I don't delete the HDD file as the game uses a lot of FX samples.    I wrote this several years ago for my Grandson, he's now moved on to GameBoy and PS2 !

   I hope this is of some help.



.data
FileNameFX  db    'C:\TEMP\Music\tempFX.mp3',0
Mp3EndFX    dd    0
TimerFX     dd    0
mcihWndFX   dd    0
MusicData   db    'MUSIC',0   ; Resource description used in resource file
MusicOFF    dd    ?
.code


PlayFX      proc  param1      :DWORD      ;param1 is the resource ID

LOCAL ResourceHandle    :DWORD
LOCAL ResourceSize      :DWORD
LOCAL ResourceAddr      :DWORD
LOCAL hFile             :DWORD
LOCAL FileReturn        :DWORD

;----------------------------------
;  cancel currently playing track
;----------------------------------
    .if mcihWndFX>0
      invoke MCIWndDestroy, mcihWndFX       ; kill any sound effects
      mov mcihWndFX,0
      .if TimerFX>0
        invoke KillTimer, hWnd, TimerFX
        mov    TimerFX,0
      .endif
    .endif
;--------------------------------------
;  check that sounds are to be played
;--------------------------------------
      .if MusicOFF==1
        return 0
      .endif
;--------------------------
; get resource details
;---------------------------
      invoke FindResource,hInstance,param1,addr MusicData
      mov    ResourceHandle,eax
      .if eax==0
        ret
      .endif
      invoke SizeofResource,hInstance,ResourceHandle
      mov    ResourceSize,eax
      .if eax==0
        ret
      .endif
      invoke LoadResource,hInstance,ResourceHandle
      mov    ResourceAddr,eax
      .if eax==0
        ret
      .endif
;-----------------------------------------------
;  copy resource data to file on hdd
;--------------------------------------------
      invoke CreateFile,ADDR FileNameFX,GENERIC_WRITE,FILE_SHARE_READ,
                             NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
      .if eax==INVALID_HANDLE_VALUE
        ret
      .endif
      mov    hFile,eax

      invoke SetEndOfFile,hFile
      invoke WriteFile,hFile,ResourceAddr,ResourceSize,addr FileReturn,0
      invoke CloseHandle,hFile
      .if eax==0
        ret
      .endif
;--------------------------------------
;  now play the track using MCI
;-------------------------------------
      invoke MCIWndCreate,hWnd,hInstance,WS_CHILD or MCIWNDF_NOOPEN,\
                          addr FileNameFX
      mov mcihWndFX,eax
      pop eax     ;Fix up stack
      pop eax
      pop eax
      pop eax

      invoke MCIWndGetEnd, mcihWndFX
      mov    Mp3EndFX, eax

      invoke MCIWndPlay, mcihWndFX
      invoke SetTimer, hWnd, 1, 1000, addr TimerProcFX     ;start up timer
      mov TimerFX, eax

      ret
PlayFX      endp
; ##############################################################

TimerProcFX proc
    invoke MCIWndGetPosition, mcihWndFX

    .if eax==Mp3EndFX                     ;eax=secs to 3 dec places
      .if mcihWndFX>0
        invoke MCIWndDestroy, mcihWndFX
        mov mcihWndFX,0
      .endif
      invoke KillTimer, hWnd, TimerFX
      mov    TimerFX,0
    .endif

     ret

TimerProcFX   endp
; ##############################################################
Title: Re: mp3 in resource script
Post by: MichaelW on November 05, 2007, 03:16:44 PM
Per  MSDN: LoadResource (http://msdn2.microsoft.com/en-us/library/ms648046.aspx):

"The return type of LoadResource is HGLOBAL for backward compatibility, not because the function returns a handle to a global memory block. Do not pass this handle to the GlobalLock or GlobalFree function. To obtain a pointer to the resource data, call the LockResource function."

I have been assuming that the LockResource call was required to get usable pointer, but now that I test with an application that creates a font file from a resource, LoadResource and LockResource are both returning the same value, so the application works correctly without the call to LockResource.
Title: Re: mp3 in resource script
Post by: Vortex on November 05, 2007, 07:02:12 PM
Hi anuradha,

To embed your mp3 file into your main application, you can use the File Data Assembler tool fda.exe supplied with the Masm32 package. This method does not require the usage of resources.
Title: Re: mp3 in resource script
Post by: anuradha on December 20, 2007, 05:03:20 AM
hi
thank you all.

:U :U :U
Title: Re: mp3 in resource script
Post by: anuradha on January 02, 2008, 07:30:53 AM
hi
Okay I did it using evlncrn8's procedure. But the thing is that only part of
the mp3  is  dumped . first part of the file is missing. Is their any way to
over come this?
I'm give the source code . But its in C . I hope I'm not breaking the forum rules!
thanks  :(
anuradha
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Here the mp31 is the resource identifier

#include <windows.h>
#include <vfw.h>

HANDLE  hBag;
HMODULE hExe;
HANDLE  Bcount;
HANDLE  htrack;
HANDLE  hfile;

LPVOID  lpRes ;
char lpfname [] ="11.mp3";
int   buffer ,test ,bufferw;



int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{


htrack=FindResource(NULL,"mp31",RT_RCDATA);
Bcount=SizeofResource(NULL,htrack);
hBag=LoadResource(NULL,htrack);
lpRes=LockResource(hBag);

hfile =CreateFile(lpfname,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_NEW,
    FILE_ATTRIBUTE_NORMAL,NULL);

buffer=Bcount;
//bufferw=Bcount;

WriteFile(hfile,buffer,Bcount,Bcount,NULL);
FlushFileBuffers(hfile);
CloseHandle(hfile);





}
Title: Re: mp3 in resource script
Post by: Jackal on January 02, 2008, 10:55:31 AM
should it be


WriteFile(hfile,lpRes,Bcount,Bcount,NULL);
Title: Re: mp3 in resource script
Post by: anuradha on January 02, 2008, 04:27:18 PM
hi
Its working thanks.Can you explain it ?? why lpRes?
thanks.
Title: Re: mp3 in resource script
Post by: Vortex on January 02, 2008, 06:22:54 PM
lpRes=LockResource(hBag);

lpRes points the resource in memory, it's the address of the memory portion containing the resource.
Title: Re: mp3 in resource script
Post by: evlncrn8 on January 02, 2008, 06:24:35 PM
because...

lpRes=LockResource(hBag);

that...

lpRes is the return of LockResource (which returns the 'locked' memory address).. ie: the start of the resource
Title: Re: mp3 in resource script
Post by: anuradha on January 02, 2008, 06:59:33 PM
hi
okay I got it . Thank you both
anuradha  :U :U