For Windows only, Play Avi or Wave files from "Ram"

Share your advanced PureBasic knowledge/code with the community.
mpz
Enthusiast
Enthusiast
Posts: 497
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

For Windows only, Play Avi or Wave files from "Ram"

Post by mpz »

Hi,

i found here a 10 years old code to play a wave file from ram with the mci commands. I have changes the code to play an avi file in a purebasic window from ram too.

P.S. I think the idea of the code can be used to make a streaming video over internet...

Greetings Michael

Old Wavecode

Code: Select all

;
;Don't forget, the wav is already loaded in memory (ReadData()).
Global *mm_Data, mm_size.l, mm_4.l, mm_opn
Declare P_mm_IO(*lpM.MMIOINFO,mesg.l,param1.l,param2.l)

*mm_Data = ?Inc
mm_size =  ?ExitProc - ?Inc

;*mm_Data is pointer to the memory location of the loaded wav-file
;mm_size is the file size
mm_4.l=mmioStringToFOURCC_("ABC",#MMIO_TOUPPER)
;could be anything other than "ABC", but not more than 3.
mm_opn=0
;Now the private IOroutine is being loaded:
mmioInstallIOProc_(mm_4,@P_mm_IO(),#MMIO_INSTALLPROC)
;Now an almost normal mci open command: but note the 'filename':
;..it's the extension "ABC+" which makes mci to use your private
;..routine P_mm_IO() for IO with wav at memory address *mm_Data:

uu.l=mciSendString_("open Sound.ABC+ type waveaudio alias test",0,0,0)

;from here on, you do mci commands (play [from], pause, status,..) as usual!
;...

mciSendString_("play test",0,0,0)
Delay (40000)

;...
;when finished, close this way:
mciSendString_("close test",0,0,0)
mmioInstallIOProc_(mm_4,#Null,#MMIO_REMOVEPROC)


;and here the routine:
Procedure P_mm_IO(*lpM.MMIOINFO,mesg.l,lparam1.l,lparam2.l)
Select mesg
Case #MMIOM_OPEN
If mm_opn=1: ProcedureReturn 0: EndIf
mm_opn=1
*lpM\lDiskOffset = 0:ProcedureReturn 0
Case #MMIOM_CLOSE: ProcedureReturn 0
Case #MMIOM_READ
CopyMemory(*mm_Data + *lpM\lDiskOffset,lParam1, lParam2)
*lpM\lDiskOffset + lParam2;
ProcedureReturn lParam2
Case #MMIOM_SEEK
Select lParam2
Case #SEEK_SET
*lpM\lDiskOffset = lParam1
Case #SEEK_CUR
*lpM\lDiskOffset + lParam1
Case #SEEK_END
*lpM\lDiskOffset = fileSize - 1 - lParam1;
EndSelect
ProcedureReturn *lpM\lDiskOffset;
Default
ProcedureReturn -1
EndSelect
ProcedureReturn
EndProcedure

DataSection
   Inc:
   IncludeBinary "F:\FanFare3.wav"
   ExitProc:
EndDataSection

;../tomio
New Avi Code

Code: Select all

;
;Don't forget, the wav is already loaded in memory (ReadData()).
Global *mm_Data, mm_size.l, mm_4.l, mm_opn
Declare P_mm_IO(*lpM.MMIOINFO,mesg.l,param1.l,param2.l)

width = 400
height = 260

hw1=OpenWindow(0, 100, 100,width , height, "PureBasicWindow", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)

*generic_bank=AllocateMemory(4096)

*mm_Data = ?Inc
mm_size =  ?ExitProc - ?Inc

;*mm_Data is pointer to the memory location of the loaded wav-file
;mm_size is the file size
mm_4.l=mmioStringToFOURCC_("ABC",#MMIO_TOUPPER)
;could be anything other than "ABC", but not more than 3.
mm_opn=0
;Now the private IOroutine is being loaded:
mmioInstallIOProc_(mm_4,@P_mm_IO(),#MMIO_INSTALLPROC)
;Now an almost normal mci open command: but note the 'filename':
;..it's the extension "ABC+" which makes mci to use your private
;..routine P_mm_IO() for IO with wav at memory address *mm_Data:

uu.l=mciSendString_("open film.ABC+ type AVIVideo alias test",0,0,0)

uu.l=mciSendString_("window test handle " + Str(hw1),0,0,0) ; Put Film in Purebasic Windows
;uu.l=mciSendString_("put test destination at 0 0 "+Str(width/2)+" "+Str(height/2),0,0,0) ; show the film in a Part of the Windows

;from here on, you do mci commands (play [from], pause, status,..) as usual!
;...
;...
;when finished, close this way:

mciSendString_("play test",0,0,0)
;mciSendString_("play test fullscreen",0,0,0) ; <- fullscreenmode

Repeat
   Event = WaitWindowEvent()

   If Event = #PB_Event_CloseWindow  ; If the user has pressed on the close button
     Quit = 1
   EndIf
Until Quit = 1

mciSendString_("close test",0,0,0)
mmioInstallIOProc_(mm_4,#Null,#MMIO_REMOVEPROC)
FreeMemory (*generic_bank)

;and here the routine:
Procedure P_mm_IO(*lpM.MMIOINFO,mesg.l,lparam1.l,lparam2.l)
Select mesg
Case #MMIOM_OPEN
If mm_opn=1: ProcedureReturn 0: EndIf
mm_opn=1
*lpM\lDiskOffset = 0:ProcedureReturn 0
Case #MMIOM_CLOSE: ProcedureReturn 0
Case #MMIOM_READ
CopyMemory(*mm_Data+*lpM\lDiskOffset,lParam1, lParam2)
*lpM\lDiskOffset + lParam2;
ProcedureReturn lParam2
Case #MMIOM_SEEK
Select lParam2
Case #SEEK_SET
*lpM\lDiskOffset = lParam1
Case #SEEK_CUR
*lpM\lDiskOffset + lParam1
Case #SEEK_END
*lpM\lDiskOffset = fileSize - 1 - lParam1;
EndSelect
ProcedureReturn *lpM\lDiskOffset;
Default
ProcedureReturn -1
EndSelect
ProcedureReturn
EndProcedure

;../tomio

DataSection
   Inc:
   IncludeBinary "f:\Urlaub\P1000230_AVI.avi"
   ExitProc:
EndDataSection
Working on - MP3D Library - PB 5.73 version ready for download
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: For Windows only, Play Avi or Wave files from "Ram"

Post by IdeasVacuum »

That's very interesting, thanks for sharing mpz.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Bo Marchais
User
User
Posts: 61
Joined: Sun Apr 03, 2016 12:03 am

Re: For Windows only, Play Avi or Wave files from "Ram"

Post by Bo Marchais »

Very nice.
Any chance of a demo with many wav/avi/mp3 files loaded, and then played in a loop by random choice?
(with the loading part)

I'm very interested to see such examples, because some day I would like to keep a bunch of audio (or even video) snippets in ram and trigger them back with a click of the mouse... I'm curious how quickly I can change the sounds. For example in a game where a ball is falling down and hits different things (the sound would change with each item). For example if it hits a bell or a stick, the sound would be different than if it hit a chicken!
Post Reply