PLAY‐SOUND routine - KVonGit/zil-stuff GitHub Wiki

Note
Never print any text from a callback routine! In fact, the smart people actually say to never do anything important from a callback routine in SOUND.
soundlib.zil
;"TO USE: You must add `<ZIP-OPTIONS SOUND>` to your main game file!"
;"PLAY-SOUND sound-id, [volume], [repeat], [callback]"
;"repeat -1 is infinite loop"
;"An infinite loop will NEVER FINISH PLAYING. It must be interrupted by another call to SOUND.
... (That means it will never call a callback routine.)
... NEVER put anything important in a callback routine!!! It's only garnish!"
;"sound plays once with repeat 0 (does not repeat)"
;"sound plays twice total with repeat 1 (it repeats once after playing the first time)"
;"sound plays three times total with repeat 2, etc."
<ROUTINE PLAY-SOUND (ID "OPT" (VOL -1) (REP 0) CALLBACK "AUX" REP-BIT)
<COND
(<G? .REP -1>
<SET REP <+ .REP 1>>
)
>
<SET REP-BIT <* .REP 256>>
<COND
(<ASSIGNED? CALLBACK>
<SOUND .ID 2 <+ .VOL .REP-BIT> .CALLBACK>
)
(ELSE
<SOUND .ID 2 <+ .VOL .REP-BIT>>
)
>
>
<ROUTINE LOAD-SOUND (ID)
<SOUND .ID 1>
>
<ROUTINE STOP-SOUND ()
<SOUND 0 3> ;"STOP ANY PLAYING SOUND BY TRIGGERING 'STOP LAST PLAYED SOUND'"
>
<ROUTINE CLEAN-SOUND-BUFFER ()
<SOUND 0 4> ;"JUST IN CASE IT ACTUALLY DOES SOMETHING TO HELP PERFORMANCE? =)"
>