I'm making a program to view/extract data from some archive file format and I'm encountering some problems.
How can I play a waveform audio file directly from memory buffer?
Can I do it if that buffer really is a portion of a memory-mapped file?
I only used "PlaySound" function but it's not acceptable this time because I have to write a temp file for it.
Please help me, some example codes will be appreciated. ::)
This will play a sound directly from memory (GoAsm format):
invoke CreateFile,"C:\WinNT\Media\ir_begin.wav",GENERIC_READ,NULL,NULL,OPEN_EXISTING,NULL,NULL
mov [hFile],eax
invoke GetFileSize,[hFile],offset cbFile
mov [cbFile],eax
invoke GlobalAlloc,GMEM_ZEROINIT,eax
mov [pBuffer],eax
invoke ReadFile,[hFile],[pBuffer],[cbFile],offset cbRead,NULL
invoke CloseHandle,[hFile]
invoke PlaySound,[pBuffer],NULL,SND_MEMORY
From resources:
RC file
3000 WAVE DISCARDABLE "ir_begin.wav"
Code
invoke PlaySound,3000,[hInstance],SND_RESOURCE
Playing a memory mapped file:
invoke CreateFile,"ir_begin.wav",GENERIC_READ,NULL,NULL,OPEN_EXISTING,NULL,NULL
mov [hFile],eax
or eax,eax
js >NOFILE
invoke CreateFileMapping,[hFile],NULL,PAGE_READONLY,0,0,NULL
mov [hMapFile],eax
or eax,eax
jz >NOHANDLE
invoke MapViewOfFile,[hMapFile],FILE_MAP_READ,0,0,0
mov [pMapFile],eax
or eax,eax
jz >NOPOINTER
invoke PlaySound,[pMapFile],NULL,SND_MEMORY
invoke UnmapViewOfFile,[pMapFile]
NOPOINTER:
invoke CloseHandle,[hMapFile]
NOHANDLE:
invoke CloseHandle,[hFile]
NOFILE:
invoke ExitProcess, 0
Thanks, donkey.
Actually, I'm trying to "play" from a big file consists of several wave files chained togather (not compressed).
I used file-mapping to skip allocating memory and filling it with an extracted wave file from that one big file, so I'm not sure how to play it directly from memory buffer but I'll try your method right away.
- - - - - - - - - - - - - - - - - - - - -
I'm not so experienced in Win32 Assembly programming, but it's truly fun and interesting trying to resolve so many problems.
Yes, it works.
The main idea was "SND_MEMORY" flag, I didn't read all related documents before asking.
Then I made a function to display memory status (update every other 2 seconds).
The sound played nicely but the program quickly consumed ALL of physical memory available, the function remarked that the wave file must fit in available memory.
In my case the size of sound file is between 200KB up to 350MB, I can play a few big files before all of physical memory is consumed and some files is just too big to fit in memory!
I tried "waveOutWrite" but the result is even worse, seems I can't use it properly.
The nexe question is:
Is there any way to cleanup memory after the sound file is played using "PlaySound" function?
Maybe this is of some use for you:
http://members.home.nl/siekmanski/playsound.zip
It loads portions of a .wav file in a directsound buffer and plays it
you can change this in the source to read in chunks of .wav data in a loop, or all in once...
; Read the wave data into the soundbufffer
invoke mmioRead,hMmio,S_data,Numbytes ;Number of bytes to read from the file.