;
; Example Program: Usage of "mplayer.o"
; SKELETON
; ******************************************************************
; FOR ADVANCED ASSEMBLER PROGRAMMERS WHO ARE TAKING OVER THE MACHINE
; ******************************************************************
; Written in 1989 by DIRE CRACKS Inc.

; xrefs

; THE SOUND DATA:
; These Labels are contained in MusicMaker-created object files

  xref _PINSDAT ; when crunched          -> (           )
; xref _INSTDAT ; when not crunched      -> ( created by)
  xref _MELODAT ;                        -> ( using "O" )

; PLAYER ROUTINES
  xref _generalsndinit,_generalsndreset,_soundon,_soundoff,_decrunchinstrs
  xref _setvolume,_maketables,_setmixbuffers,_obtainmixbuflen

; FLAGS
  xref _fadeflag,_fadefinished,_channelsfinished

; THE INTERRUPT PROGRAM
  xref _generalplay

; CODE START ---------------------------------------

; EXAMPLE for calling the fibonacci-decruncher (when using packed instrs)
  move.l 4,a6
  move.l #160000,d0  ; maximum instruments length
  move.l #$10001,d1  ; need no chipmem in 8-channel-player
  jsr -198(a6)
  move.l d0,mymem

  lea.l _PINSDAT,a0     ;
  move.l d0,a1          ;
  jsr _decrunchinstrs   ;

; NOW HAVE SOME MIXBUFFERS:
; (YOU CAN/MUST/SHOULD ALSO CALL AllocMem HERE - CHIPMEM REQUIRED !!!)
 lea.l _MELODAT,a0
 jsr _obtainmixbuflen
 lea.l $60000,a0        ; $60000 is unused in normally config'd AMIGA
 moveq.l #8-1,d1        ; 
mixfieldbuildloop:
  move.l a0,-(a7)
  adda.w d0,a0
 dbra d1,mixfieldbuildloop
 move.l a7,a0
 jsr _setmixbuffers
 lea.l 8*4(a7),a7

; WELL, HOW 'BOUT SOME TABLES (REQUIRED BY MULTI-CHANNEL-PLAYER)
; NEED NOT NECESSARILY BE IN CHIP-MEM
  lea.l $70000,a0 ; free space of 256+(64*2)+(65*32*2)+(d0*d1) bytes
  moveq.l #64,d0  ;     -specifies volume resolution.      /\__________
  moveq.l #-1,d1  ;     -in this case: table unpacked:    /            \
  jsr _maketables ;      memory requirement is: packed 128,  unpacked 256

; EXAMPLE for calling FINAL sound initialization
  moveq.l #0,d0         ; play 0=loop, <>0=oneshot
  move.l mymem(PC),a1   ; if instrs are not crunched: lea.l _INSTDAT,a1
  lea.l _MELODAT,a2
  lea.l myprivateroutine,a3
  jsr _generalsndinit

; THIS SETS UP MY OWN LEVEL 4 INTERRUPT HANDLER
; EVEN THOUGH THIS IS A NON-SYSTEM-VERSION, leave system alive ... !
  move.l $70,oldint4
  move.l #_generalplay,$70
  move.w #$0780,$dff09c
  move.w #$0780,$dff09a

; SWITCHING sound ON
  jsr _soundon

waitmouse:

;; IN CASE OF ONESHOT: WAIT FOR CHANNELS TO FINISH
;    cmpi.b #$ff,_channelsfinished
;    beq.s soundfinished

    btst.b #7,$bfe001   ; in fact it's the joystick
  bne.s waitmouse

;  IN CASE YOU WANT TO SET A NEW VOLUME-LEVEL:
; moveq.l #63,d0
; jsr _setvolume
;  THIS SETS THE GENERAL VOLUME TO THE HALF OF THE ORIGINAL.
;  TO SET IT TO MAXIMUM, SET IT TO 127.
;  THE GIVEN VALUE'S RANGE GOES FROM 0 TO 127.
;  !! YOU SHOULD CALL THIS ROUTINE O-N-L-Y WHEN PLAYER IS RUNNING !!

; FADE SOUND OUT
;  move.b #120,_fadeflag   ; fadespeed 50(=very fast) - 120 (slow)

; WAIT FOR FADE TO FINISH
;waitforfade:
;    tst.b _fadefinished
;  beq.s waitforfade

; SWITCHING SOUND OFF IMMEDIATLY
soundfinished:
  jsr _soundoff

; SWITCHING LOW PASS FILTER TO ORIG STATE
  bclr.b #1,$bfe001

; IF YOU WANT YOUR SOUND START AGAIN
; DO THE FOLLOWING:
; moveq.l #0,d0  OR  moveq.l #1,d0   FOR LOOP/ONESHOT
; jsr _generalsndreset
; jsr _soundon
; AND THEN AS DONE BEFORE.

; IF YOU WANT TO PLAY ANOTHER SOUND, YOU MUST CALL
; jsr _generalsndinit  INSTEAD OF _generalsndreset
; WITH THE REGISTERS SET AS FAR ABOVE.
; SINCE THERE'S NO _generalsndremove LIKE IN THE sysplayer IT'S NOT
; REQUIRED IN THIS VERSION THOUGH ...

; THIS IS MY EXIT-STUFF
  move.l oldint4(PC),$70

  move.l $4,a6
  move.l #160000,d0
  move.l mymem(PC),a1
  jsr -210(a6)
  moveq.l #0,d0
  rts

; ---------------
; MYPRIVATEROUTINE, called, when "$" is in a macro.
; gets pointer to itself in a0, and called-count.w in d0
; SHOULD NOT TAKE THAT LONG (called from a VERY time critical level4 int!!!)
myprivateroutine:
 move.w #$0f00,$dff180
 rts ; always ends up by this !

; ---------------

mymem:   dc.l 0
oldint4: dc.l 0

 END

