home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 25 / amigaformatcd25.iso / websites / amidoom / adoom_src-0.7.lha / ADoom_src / amiga_music.s < prev    next >
Text File  |  1998-01-16  |  42KB  |  1,907 lines

  1.         mc68020
  2.         multipass
  3. ;;;        debug    on,lattice4
  4.  
  5. ; 2 channel .MUS player
  6. ;
  7. ; Derived by Peter McGavin,
  8. ; mostly by cutting and pasting large chunks of code from Amiga .MUS player
  9. ; originally by Joseph Fenton, Microcode Solutions, jlfenton@ctaz.com
  10. ; and his brother Michael.
  11.  
  12. ; void I_InitMusic (void);
  13.  
  14. ; void I_ShutdownMusic (void);
  15.  
  16. ; void I_SetMusicVolume (int volume);    // Volume.
  17.  
  18. ; void I_PauseSong (int handle);    // PAUSE game handling.
  19.  
  20. ; void I_ResumeSong (int handle);
  21.  
  22. ; int I_RegisterSong (void *data);    // Registers a song handle to song data.
  23.  
  24. ;// Called by anything that wishes to start music.
  25. ;//  plays a song, and when the song is done,
  26. ;//  starts playing it again in an endless loop.
  27. ;// Horrible thing to do, considering.
  28. ; void I_PlaySong (int handle, int looping);
  29.  
  30. ; void I_StopSong (int handle);        // Stops a song over 3 seconds.
  31.  
  32. ; void I_UnRegisterSong (int handle);    // See above (register), then think backwards
  33.  
  34. ;------------------------------------------------------------------------
  35.         xdef    @I_InitMusic      ; Allocate audio channels and clear them.
  36.         xdef    @I_ShutdownMusic  ; Clear channels and free them.
  37.         xdef    @I_SetMusicVolume ; Do code equiv to J_VolBox.
  38.         xdef    @I_PauseSong      ; Do code equiv to DoPause.
  39.         xdef    @I_ResumeSong      ; Goes to J_PlayBox equiv code.
  40.         xdef    @I_RegisterSong      ; Do code equiv to LoadMUS; no need to actually load it
  41.                       ; as we are passed a pointer to it here.
  42.         xdef    @I_PlaySong      ; Do code equiv to J_PlayBox.
  43.         xdef    @I_StopSong      ; Do code equiv to J_StopBox.
  44.         xdef    @I_UnRegisterSong ; Do code equiv to FreeUpMUS.
  45.  
  46. ;------------------------------------------------------------------------
  47.         xref    _SysBase
  48.         xref    _DOSBase
  49.         xref    _GfxBase
  50.         xref    _custom
  51.         xref    _snd_MusicVolume
  52.         xref    _gametic
  53.         xref    _I_Error
  54.         xref    @M_CheckParm
  55.  
  56. ;------------------------------------------------------------------------
  57.  
  58. TICRATE        equ    35            ; see doomdef.h
  59.  
  60. CALLSYS        macro    ;FunctionName
  61.         jsr    _LVO\1(a6)
  62.         endm
  63.  
  64. CALLEXE        macro    ;FunctionName
  65.         movea.l    (_SysBase),a6
  66.         jsr    _LVO\1(a6)
  67.         endm
  68.  
  69. CALLDOS        macro    ;FunctionName
  70.         movea.l    (_DOSBase),a6
  71.         jsr    _LVO\1(a6)
  72.         endm
  73.  
  74. _LVOCreatePool    equ    -696            ; not in Macro68's autoincludes
  75. _LVODeletePool    equ    -702            ; not in Macro68's autoincludes
  76.  
  77. ;------------------------------------------------------------------------
  78. ; void I_InitMusic (void);
  79. ; Allocate audio channels and clear them.
  80.  
  81. @I_InitMusic    movem.l    d0-d7/a0-a6,-(sp)
  82.  
  83.         movea.l    #musicarg,a0
  84.         jsr    @M_CheckParm
  85.         tst.l    d0
  86.         beq    .exit
  87.  
  88.         suba.l    a1,a1            ; this task
  89.         CALLEXE FindTask
  90.         move.l    d0,AudioTask        ; set task to signal
  91.  
  92.         move.w    #162,(per)
  93.         move.w    #325,(per2)        ; 11025 Hz, NTSC
  94.         move.l    (_GfxBase),a6
  95.         move.w    (gb_DisplayFlags,a6),d0
  96.         btst    #PALn,d0
  97.         beq.b    2$
  98.         move.w    #161,(per)
  99.         move.w    #322,(per2)        ; 11025 Hz, PAL
  100. 2$
  101.         moveq    #0,d0
  102.         moveq    #0,d1
  103.         lea    AudioName,a0
  104.         lea    AudioIO,a1
  105.         clr.l    ioa_Length(a1)        ; don't allocate channels yet
  106.         CALLEXE OpenDevice
  107.         tst.l    d0
  108.         beq    1$
  109.  
  110.         move.l    #cantopenaud,-(sp)
  111.         jsr    _I_Error
  112.         bra    .exit
  113.  
  114. 1$        move.b    #-1,AudioOK
  115.         bsr    AllocChannels        ; allocate channels
  116. .exit
  117.         movem.l    (sp)+,d0-d7/a0-a6
  118.         rts
  119.  
  120. ;------------------------------------------------------------------------
  121. ; void I_ShutdownMusic (void);
  122. ; Clear channels and free them.
  123.  
  124. @I_ShutdownMusic
  125.         movem.l    d0-d7/a0-a6,-(sp)
  126.         tst.b    AudioOK
  127.         beq    .exit
  128.         bsr    @I_StopSong
  129.         bsr    @I_UnRegisterSong    ; kill old mus
  130.         bsr    FreeChannels
  131.         lea    AudioIO,a1
  132.         CALLEXE CloseDevice
  133.         clr.b    AudioCh
  134.         clr.b    AudioOK
  135. .exit        movem.l    (sp)+,d0-d7/a0-a6
  136.         rts
  137.  
  138. ;------------------------------------------------------------------------
  139. ; void I_SetMusicVolume (int volume);    // Volume.
  140. ; Do code equiv to J_VolBox.
  141.  
  142. @I_SetMusicVolume
  143.         movem.l    d0-d7/a0-a6,-(sp)
  144.         tst.b    AudioOK
  145.         beq    .exit
  146.  
  147.         move.l    d0,(_snd_MusicVolume)
  148.         lsl.w    #3,d0            ; * 8
  149.         move.w    d0,AudioCh2Vol
  150.         move.w    d0,AudioCh3Vol
  151. .exit        movem.l    (sp)+,d0-d7/a0-a6
  152.         rts
  153.  
  154. ;------------------------------------------------------------------------
  155. ; void I_PauseSong (int handle);    // PAUSE game handling.
  156. ; Do code equiv to DoPause.
  157.  
  158. @I_PauseSong    movem.l    d0-d7/a0-a6,-(sp)
  159.         tst.b    AudioOK
  160.         beq    .done
  161.  
  162.         btst    #1,Flags        ; file loaded?
  163.         beq.b    .done
  164.         bset    #0,MusFlag        ; stop
  165.         bclr    #2,Flags        ; playing?
  166.         beq.b    .done
  167. .stop        btst    #0,MusFlag        ; music stopped?
  168.         bne.b    .stop
  169.  
  170.         cmpi.b    #12,AudioCh        ; locked?
  171.         bne.b    .done
  172.         lea    _custom,a0        ; kill sound
  173.         moveq    #0,d0
  174.         move.w    d0,aud2+ac_vol(a0)
  175.         move.w    d0,aud3+ac_vol(a0)
  176.  
  177. .done        movem.l    (sp)+,d0-d7/a0-a6
  178.         rts
  179.  
  180. ;------------------------------------------------------------------------
  181. ; void I_ResumeSong (int handle);
  182. ; Goes to J_PlayBox equiv code.
  183.  
  184. @I_ResumeSong    movem.l    d0-d7/a0-a6,-(sp)
  185.         tst.b    AudioOK
  186.         beq    .exit
  187.  
  188.         btst    #1,Flags        ; file loaded?
  189.         beq.b    .nofile
  190.  
  191.         cmpi.b    #12,AudioCh        ; got all channels?
  192.         beq.b    .allch
  193.         bsr    AllocChannels        ; try to alloc, first
  194.         cmpi.b    #12,AudioCh        ; now?
  195.         beq.b    .allch
  196.  
  197.         move.l    #cantgetchan,-(sp)
  198.         jsr    _I_Error
  199.         bra    .exit
  200.  
  201. .allch        bset    #2,Flags        ; playing?
  202.         beq.b    .ok
  203. .nofile        rts
  204.  
  205. .ok        movea.l    MusPtr,a0
  206.         moveq    #0,d1
  207.         move.b    8(a0),d1        ; d1 = # of channels
  208.  
  209.         moveq    #0,d0
  210.         move.w    6(a0),d0        ; score start
  211.         ror.w    #8,d0
  212.         adda.l    d0,a0            ; a0 = start
  213.         bclr    #3,Flags        ; paused?
  214.         bne    .paused
  215.  
  216.         move.l    a0,MusIndex        ; MusIndex = start
  217.  
  218.         move.l    #QuietInst,Channel0
  219.         move.l    #QuietInst,Channel1
  220.         move.l    #QuietInst,Channel2
  221.         move.l    #QuietInst,Channel3
  222.         move.l    #QuietInst,Channel4
  223.         move.l    #QuietInst,Channel5
  224.         move.l    #QuietInst,Channel6
  225.         move.l    #QuietInst,Channel7
  226.         move.l    #QuietInst,Channel8
  227.         move.l    #QuietInst,Channel9
  228.         move.l    #QuietInst,Channel10
  229.         move.l    #QuietInst,Channel11
  230.         move.l    #QuietInst,Channel12
  231.         move.l    #QuietInst,Channel13
  232.         move.l    #QuietInst,Channel14
  233.         move.l    #QuietInst,Channel15
  234.  
  235.         clr.l    MusDelay        ; MusDelay = 0
  236.         clr.b    MusFlag            ; enable music
  237.         clr.l    VoiceAvail        ; all voices available
  238.  
  239.         clr.l    MyTicks            ; reset my timer
  240.  
  241. .paused        CALLEXE Disable
  242.         lea    _custom,a0
  243.         move.w    #$8200,intena(a0)    ; int enable
  244.         move.w    #$800c,dmacon(a0)    ; enable dma
  245.         bsr    AudioINT2        ; start audio
  246.         CALLSYS    Enable
  247.  
  248. .exit        movem.l    (sp)+,d0-d7/a0-a6
  249.         rts
  250.  
  251. ;------------------------------------------------------------------------
  252. ; int I_RegisterSong (void *data);    // Registers a song handle to song data.
  253. ; Do code equiv to LoadMUS; no need to actually load it
  254. ; as we are passed a pointer to it here.
  255.  
  256. @I_RegisterSong
  257.         movem.l    d0-d7/a0-a6,-(sp)
  258.         tst.b    AudioOK
  259.         beq    .done2
  260.  
  261.         move.l    a0,-(sp)        ; save data ptr
  262.  
  263.         bsr    @I_StopSong        ; stop current mus
  264.         bsr    @I_UnRegisterSong    ; kill old mus
  265.  
  266.         movea.l    (sp)+,a0
  267.         move.l    a0,MUSMemPtr
  268.  
  269.         cmpi.l    #$4D55531A,(a0)        ; "MUS",26
  270.         bne    .uerror            ; not a mus file
  271.  
  272.         move.l    MUSMemPtr(pc),MusPtr
  273.  
  274.         bsr    TestMUSFile
  275.         beq    .berror
  276.  
  277. ;--------
  278.  
  279.         move.l    InstrFile,d1
  280.         move.l    #MODE_OLDFILE,d2
  281.         CALLDOS    Open
  282.         move.l    d0,InstrHandle
  283.         beq    .midierror
  284.  
  285.         move.l    #MEMF_CLEAR,d0        ; any memory
  286.         move.l    #65536,d1        ; puddle size
  287.         move.l    #32768,d2        ; threshold size
  288.         bsr    CreatePool
  289.         move.l    a0,InstrPool        ; this was d0
  290.         beq    .merror
  291.  
  292.         movea.l    a0,a2            ; so was this
  293.         movea.l    MusPtr,a3
  294.  
  295.         move.w    #255,d0
  296.         lea    Instruments,a0
  297. .setinstr    move.l    #QuietInst,(a0)+
  298.         dbra    d0,.setinstr
  299.  
  300.         move.w    $C(a3),d4        ; instrCnt
  301.         ror.w    #8,d4
  302.         subq.w    #1,d4            ; for dbra
  303.  
  304.         lea    $10(a3),a3        ; instruments[]
  305.  
  306. .instrloop    moveq    #14,d0
  307.         movea.l    a2,a0
  308.         bsr    AllocPooled
  309.  
  310.         moveq    #0,d2
  311.         move.b    (a3)+,d2        ; instrument #
  312.         moveq    #0,d1
  313.         move.b    (a3)+,d1        ; offset to next instr. #
  314.         adda.l    d1,a3            ; skip it (whatever it is?)
  315.  
  316.         lea    Instruments,a0
  317.         move.l    d0,(a0,d2.w*4)
  318.         beq    .merror
  319.  
  320.         movea.l    d0,a4            ; instrument record
  321.  
  322.         bftst    (validInstr){d2:1}
  323.         beq    .next            ; no instrument
  324.  
  325.         move.l    InstrHandle,d1
  326.         lsl.l    #2,d2
  327.         moveq    #OFFSET_BEGINNING,d3
  328.         CALLDOS    Seek
  329.  
  330.         move.l    InstrHandle,d1
  331.         move.l    a4,d2
  332.         moveq    #4,d3
  333.         CALLSYS    Read            ; get instrument offset
  334.         addq.l    #1,d0
  335.         beq    .ferror            ; can't read file
  336.  
  337.         move.l    InstrHandle,d1
  338.         move.l    (a4),d2
  339.         moveq    #OFFSET_BEGINNING,d3
  340.         CALLSYS    Seek
  341.  
  342.         move.l    InstrHandle,d1
  343.         move.l    a4,d2
  344.         moveq    #14,d3
  345.         CALLSYS    Read            ; get instrument header
  346.         addq.l    #1,d0
  347.         beq    .ferror            ; can't read file
  348.  
  349.         move.l    in_Length(a4),d0
  350.         swap    d0
  351.         movea.l    a2,a0
  352.         bsr    AllocPooled
  353.         move.l    d0,in_Wave(a4)        ; wave data buffer
  354.         beq    .merror
  355.  
  356.         move.l    InstrHandle,d1
  357.         move.l    d0,d2
  358.         move.l    in_Length(a4),d3
  359.         swap    d3
  360.         CALLDOS    Read            ; get instrument samples
  361.         addq.l    #1,d0
  362.         beq    .ferror            ; can't read file
  363.  
  364.         move.b    #1,in_Flags(a4)
  365. .next        dbra    d4,.instrloop
  366.  
  367.         bset    #1,Flags        ; file loaded
  368.         bsr    FillEventBlocks
  369.  
  370. .done        btst    #1,Flags        ; success?
  371.         bne.b    .exit
  372.         bsr    @I_UnRegisterSong    ; kill MUS and instruments
  373.  
  374. .exit        move.l    InstrHandle,d1
  375.         beq.b    .done2
  376.         CALLDOS    Close
  377.         clr.l    InstrHandle
  378.  
  379. .done2        moveq    #1,d0            ; return handle=1
  380.         movem.l    (sp)+,d0-d7/a0-a6
  381.         rts
  382.  
  383. ;---------
  384.  
  385. .midierror    move.l    #midifileproblem,-(sp)
  386.         jsr    _I_Error
  387.         bra    .done
  388.  
  389. .ferror        move.l    #dosproblem,-(sp)
  390.         jsr    _I_Error
  391.         bra    .done
  392.  
  393. .merror        move.l    #memproblem,-(sp)
  394.         jsr    _I_Error
  395.         bra    .done
  396.  
  397. .uerror        move.l    #notmusfile,-(sp)
  398.         jsr    _I_Error
  399.         bra    .done
  400.  
  401. .berror        move.l    #damagedfile,-(sp)
  402.         jsr    _I_Error
  403.         bra    .done
  404.  
  405. ;------------------------------------------------------------------------
  406. ;// Called by anything that wishes to start music.
  407. ;//  plays a song, and when the song is done,
  408. ;//  starts playing it again in an endless loop.
  409. ;// Horrible thing to do, considering.
  410. ; void I_PlaySong (int handle, int looping);
  411. ; Do code equiv to J_PlayBox.
  412.  
  413. @I_PlaySong    movem.l    d0-d7/a0-a6,-(sp)
  414.         tst.b    AudioOK
  415.         beq    .exit
  416.  
  417.         move.l    #TICRATE*30,d0
  418.         add.l    (_gametic),d0
  419.         move.l    d0,(musicdies)
  420.  
  421.         bsr    @I_ResumeSong
  422.  
  423. .exit        movem.l    (sp)+,d0-d7/a0-a6
  424.         rts
  425.  
  426. ;------------------------------------------------------------------------
  427. ; void I_StopSong (int handle);        // Stops a song over 3 seconds.
  428. ; Do code equiv to J_StopBox.
  429.  
  430. @I_StopSong    movem.l    d0-d7/a0-a6,-(sp)
  431.  
  432.         move.l    #0,(looping)
  433.         move.l    #0,(musicdies)
  434.  
  435.         tst.b    AudioOK
  436.         beq    .exit
  437.  
  438.         bsr    @I_PauseSong
  439.         bsr    KillAllAud
  440.  
  441. .exit        movem.l    (sp)+,d0-d7/a0-a6
  442.         rts
  443.  
  444. ;------------------------------------------------------------------------
  445. ; void I_UnRegisterSong (int handle);    // See above (register), then think backwards
  446. ; Do code equiv to FreeUpMUS.
  447.  
  448. @I_UnRegisterSong
  449.         movem.l    d0-d7/a0-a6,-(sp)
  450.  
  451.         tst.b    AudioOK
  452.         beq    .free
  453.  
  454.         bclr    #1,Flags        ; still have anything?
  455.         beq.b    .free
  456.  
  457.         clr.l    MUSMemPtr
  458.         clr.l    MUSMemSize
  459.  
  460. .instr        tst.l    InstrPool
  461.         beq.b    .free
  462.  
  463.         movea.l    InstrPool,a0
  464.         bsr    DeletePool
  465.         clr.l    InstrPool
  466.  
  467. .free        movem.l    (sp)+,d0-d7/a0-a6
  468.         rts
  469.  
  470. ;------------------------------------------------------------------------
  471. ;------------------------------------------------------------------------
  472.  
  473. FillEventBlocks    lea    EventBlocks,a4
  474.         movea.l    MusPtr(pc),a0
  475.         move.w    6(a0),d0
  476.         ror.w    #8,d0
  477.         lea    (a0,d0.w),a2        ; a2 = start
  478.         movea.l    a2,a1            ; a1 = a2
  479. .loop        bsr    NextDelay
  480.         move.l    a1,d0
  481.         sub.l    a2,d0            ; d0 = ptr - start
  482.         move.w    d0,(a4)+        ; store
  483.         addq.l    #1,d1
  484.         bne.b    .loop
  485.         clr.w    (a4)            ; end of offsets
  486.         rts
  487.  
  488. NextDelay    bsr.b    MyNextEvent
  489.         cmpi.b    #6,d1            ; score end
  490.         beq.b    .dd
  491.         tst.b    d0
  492.         bpl.b    NextDelay
  493.  
  494.         moveq    #0,d1            ; time = 0
  495. .d1        move.b    (a1)+,d0        ; get byte
  496.         bpl.b    .d2
  497.         andi.w    #$7F,d0            ; kill sign bit
  498.         or.b    d0,d1            ; time = time + 7 bits
  499.         lsl.l    #7,d1            ; * 128
  500.         bra.b    .d1            ; get next 7 bits
  501. .d2        or.b    d0,d1            ; time = time + last 7 bits
  502.         rts
  503.  
  504. .dd        moveq    #-1,d1
  505.         rts
  506.  
  507. MyNextEvent    move.b    (a1)+,d0        ; d0 = event
  508.         moveq    #$70,d1
  509.         and.b    d0,d1            ; d1 = type<<4
  510.         bne.b    .e1
  511.  
  512. .e0        addq.l    #1,a1            ; + value
  513.         rts
  514.  
  515. .e1        lsr.b    #4,d1            ; d1 = type
  516.         cmpi.b    #6,d1            ; score end?
  517.         bne.b    .e2
  518.         subq.l    #1,a1
  519. .ed        rts
  520. .e2        cmpi.b    #5,d1            ; no event
  521.         beq.b    .ed
  522.         cmpi.b    #7,d1            ; no event
  523.         beq.b    .ed
  524.         cmpi.b    #1,d1            ; play
  525.         beq.b    .ep
  526.         cmpi.b    #4,d1            ; change?
  527.         bne.b    .e0
  528.         addq.l    #2,a1            ; + value1, value2
  529.         rts
  530. .ep        moveq    #0,d4
  531.         tst.b    (a1)+            ; + note
  532.         bmi.b    .e0            ; (+ volume, if b7 set)
  533.         rts
  534.  
  535.         moveq    #1,d0
  536.         rts
  537.  
  538. ;------------------------------------------------------------------------
  539.  
  540. TestMUSFile    movea.l    MusPtr(pc),a0
  541.         move.l    MUSMemSize(pc),d3    ; d3 = total file size
  542.         moveq    #0,d0
  543.         move.w    4(a0),d0
  544.         beq    .fail
  545.         ror.w    #8,d0            ; score length
  546.         moveq    #0,d1
  547.         move.w    6(a0),d1
  548.         ror.w    #8,d1            ; score start
  549.         cmpi.w    #18,d1            ; start < 18? (1 instr.)
  550.         blt    .fail
  551.         add.l    d1,d0            ; d0 = total size
  552.  
  553. ;        cmp.l    d0,d3            ; = file size?
  554. ;        bne    .fail
  555.  
  556.         move.l    d0,d3
  557.         move.l    d3,MUSMemSize
  558.  
  559.         move.w    12(a0),d2
  560.         beq.b    .fail
  561.         ror.w    #8,d2            ; d2 = instr. count
  562.         subq.w    #1,d2
  563.         lea    16(a0),a1        ; a1 = * instr. list
  564. .loop        addq.l    #1,a1            ; skip instr. value
  565.         moveq    #0,d0
  566.         move.b    (a1)+,d0        ; d0 = offset to next instr.
  567.         adda.l    d0,a1            ; skip info (?)
  568.         dbra    d2,.loop        ; next
  569.         move.l    a1,d0            ; d0 = * data following list
  570.         sub.l    a0,d0            ; - file start
  571.         cmp.l    d0,d1            ; = start?
  572.         bne.b    .fail
  573.         move.b    -1(a0,d3.l),d0        ; get last byte
  574.         lsr.b    #4,d0
  575.         cmpi.b    #6,d0            ; last byte = $6x? (end)
  576.         bne.b    .fail
  577.         moveq    #1,d0            ; file okay
  578.         rts
  579. .fail        moveq    #0,d0            ; yikes!
  580.         rts
  581.  
  582. ;------------------------------------------------------------------------
  583. ;------------------------------------------------------------------------
  584.  
  585. ; stop, clear, turn off
  586.  
  587. KillAllAud    cmpi.b    #12,AudioCh        ; locked?
  588.         bne.b    .vk
  589.  
  590.         lea    _custom,a0
  591.         move.l    #ClearBuf,aud2(a0)    ; re-init
  592.         move.w    #80,aud2+ac_len(a0)
  593.         move.w    (per,pc),aud2+ac_per(a0)
  594.         move.l    #ClearBuf,aud3(a0)
  595.         move.w    #80,aud3+ac_len(a0)
  596.         move.w    (per,pc),aud3+ac_per(a0)
  597.  
  598. .vk        clr.b    Voice0+vc_Flags        ; disable voices
  599.         clr.b    Voice1+vc_Flags
  600.         clr.b    Voice2+vc_Flags
  601.         clr.b    Voice3+vc_Flags
  602.         clr.b    Voice4+vc_Flags
  603.         clr.b    Voice5+vc_Flags
  604.         clr.b    Voice6+vc_Flags
  605.         clr.b    Voice7+vc_Flags
  606.         clr.b    Voice8+vc_Flags
  607.         clr.b    Voice9+vc_Flags
  608.         clr.b    Voice10+vc_Flags
  609.         clr.b    Voice11+vc_Flags
  610.         clr.b    Voice12+vc_Flags
  611.         clr.b    Voice13+vc_Flags
  612.         clr.b    Voice14+vc_Flags
  613.         clr.b    Voice15+vc_Flags
  614.  
  615.         rts
  616.  
  617. ;------------------------------------------------------------------------
  618. ;------------------------------------------------------------------------
  619.  
  620. AllocChannels    lea    AudioIO,a1
  621.         move.w    #ADCMD_ALLOCATE,IO_COMMAND(a1)
  622.         move.b    #ADIOF_NOWAIT+IOF_QUICK,IO_FLAGS(a1)
  623.         move.l    #AudioAlloc,ioa_Data(a1)
  624.         move.l    #1,ioa_Length(a1)
  625.         movea.l    IO_DEVICE(a1),a6
  626.         jsr    DEV_BEGINIO(a6)
  627.         tst.l    d0
  628.         beq    1$
  629.  
  630.         move.l    #cantgetchan,-(sp)
  631.         jsr    _I_Error
  632.         bra    .exit
  633.  
  634. 1$        lea    AudioIO,a1
  635.         move.w    #ADCMD_LOCK,IO_COMMAND(a1)
  636.         CALLEXE SendIO
  637.  
  638.         move.l    AudioIO+IO_UNIT,d0
  639.         move.b    d0,AudioCh
  640.         cmpi.b    #12,d0            ; all channels?
  641.         beq    2$
  642.  
  643.         move.l    #cantgetchan,-(sp)
  644.         jsr    _I_Error
  645.         bra    .exit
  646.  
  647. 2$        lea    _custom,a0
  648.         move.w    #$0600,intena(a0)    ; kill int enable
  649.         move.w    #$0600,intreq(a0)    ; kill request
  650.         move.w    #$00FF,adkcon(a0)    ; kill modulation
  651.         move.w    #$000C,dmacon(a0)    ; disable dma
  652.  
  653.         move.l    #ClearBuf,aud2(a0)
  654.         move.w    #80,aud2+ac_len(a0)
  655.         move.w    (per,pc),aud2+ac_per(a0)
  656.         move.w    #0,aud2+ac_vol(a0)
  657.  
  658.         move.l    #ClearBuf,aud3(a0)
  659.         move.w    #80,aud3+ac_len(a0)
  660.         move.w    (per,pc),aud3+ac_per(a0)
  661.         move.w    #0,aud3+ac_vol(a0)
  662.  
  663.         moveq    #INTB_AUD2,d0
  664.         lea    AInt2,a1
  665.         CALLEXE SetIntVector
  666.         move.l    d0,OldAInt2
  667.  
  668. .exit        rts
  669.  
  670. ;------------------------------------------------------------------------
  671.  
  672. FreeChannels    cmpi.b    #12,AudioCh
  673.         bne.b    .exit            ; no channels
  674.  
  675.         lea    _custom,a0
  676.         move.w    #$0600,intena(a0)    ; kill int enable
  677.         move.w    #$0600,intreq(a0)    ; kill request
  678.  
  679.         moveq    #INTB_AUD2,d0
  680.         movea.l    OldAInt2,a1
  681.         CALLEXE SetIntVector
  682.  
  683.         moveq    #$000c,d0
  684.         move.w    d0,_custom+dmacon    ; dma off
  685.         lea    AudioIO,a1
  686.         move.w    #ADCMD_FREE,IO_COMMAND(a1)
  687.         move.b    #IOF_QUICK,IO_FLAGS(a1)
  688.         movea.l    IO_DEVICE(a1),a6
  689.         jsr    DEV_BEGINIO(a6)
  690.         clr.l    AudioIO+IO_UNIT
  691.         clr.b    AudioCh
  692.  
  693. .exit        rts
  694.  
  695. ;--------------------------------------------------------------------
  696.  
  697.         cnop    0,16
  698.  
  699. AudioINT2    movem.l    d2-d6/a2-a4/a6,-(a7)
  700.  
  701.         btst    #0,MusFlag
  702.         beq.b    .cont
  703.  
  704.         lea    _custom,a0
  705.         move.w    #$0600,intena(a0)    ; kill int enable
  706.         move.w    #$0600,intreq(a0)    ; kill request
  707.  
  708.         clr.b    MusFlag
  709.         bra    .exit
  710.  
  711. .cont        subq.l    #1,MusDelay
  712.         bpl.b    .setaudio
  713.  
  714.         bsr    NextEvent
  715.  
  716. .setaudio    bchg    #1,MusFlag        ; switch buffers
  717.  
  718.         btst    #6,Flags        ; gadget down?
  719.         bne.b    .gad
  720.         addq.l    #1,MyTicks        ; increment my timer
  721.  
  722. .gad        lea    Voice0,a0        ; music voices
  723.         lea    AudioCh2Buf,a2
  724.         bsr    FillBuffer
  725.  
  726.         lea    _custom,a0
  727.         move.w    #INTF_AUD2,intreq(a0)
  728.  
  729.         move.l    AudioCh2Ptr,aud2(a0)
  730.         move.w    #40,aud2+ac_len(a0)        ; 80 samples
  731.         move.w    (per2,pc),aud2+ac_per(a0)    ; 11048Hz
  732.         move.w    AudioCh2Vol,aud2+ac_vol(a0)
  733.  
  734.         move.l    AudioCh2Ptr,aud3(a0)
  735.         move.w    #40,aud3+ac_len(a0)        ; 80 samples
  736.         move.w    (per2,pc),aud3+ac_per(a0)    ; 11048Hz
  737.         move.w    AudioCh2Vol,aud3+ac_vol(a0)
  738.  
  739. .exit        movem.l    (a7)+,d2-d6/a2-a4/a6
  740.         moveq    #0,d0
  741.         rts
  742.  
  743. ;------------------------------------------------------------------------
  744.  
  745.         CNOP    0,16
  746.  
  747. FillBuffer    moveq    #0,d0
  748.         btst    #1,MusFlag
  749.         beq.b    .swap
  750.         move.l    #80,d0
  751. .swap        movea.l    (a2),a4            ; chip buffer
  752.         adda.l    d0,a4
  753.         move.l    a4,4(a2)        ; AudioChxPtr
  754.  
  755.         lea    tempAudio,a1
  756.         moveq    #4,d0
  757. .cloop        clr.l    (a1)+
  758.         clr.l    (a1)+
  759.         clr.l    (a1)+
  760.         clr.l    (a1)+
  761.         dbra    d0,.cloop
  762.  
  763. .next        move.l    vc_Next(a0),d0        ; next voice
  764.         bne.b    .chkvoice
  765.  
  766.         lea    tempAudio,a1
  767.         moveq    #4,d0
  768. .mloop        move.l    (a1)+,(a4)+
  769.         move.l    (a1)+,(a4)+
  770.         move.l    (a1)+,(a4)+
  771.         move.l    (a1)+,(a4)+
  772.         dbra    d0,.mloop
  773.         rts
  774.  
  775. .chkvoice    movea.l    d0,a0
  776.         btst    #0,vc_Flags(a0)
  777.         beq.b    .next            ; not enabled
  778.  
  779.  
  780. ;------------------
  781. ; do voice
  782.  
  783.         lea    tempAudio,a1
  784.  
  785.         btst    #1,vc_Flags(a0)
  786.         beq.b    .1            ; not releasing
  787.  
  788.         subq.b    #1,vc_Vol(a0)
  789.         bpl.b    .1
  790.         clr.b    vc_Vol(a0)
  791.         clr.b    vc_Flags(a0)        ; voice off
  792.         bra.b    .next
  793.  
  794. .1        move.b    vc_Vol(a0),d5
  795.         bfffo    d5{24:8},d5        ; 0-127 -> 32-24
  796.         subi.b    #24,d5            ;          8-0
  797.         addq.b    #1,d5            ; /2
  798.  
  799.         movem.l    vc_Index(a0),d1-d4    ; index,step,loop,length
  800.  
  801.         movea.l    vc_Channel(a0),a3
  802.         move.l    ch_Pitch(a3),d0
  803.         muls.l    d0,d6:d2
  804.         move.w    d6,d2
  805.         swap    d2
  806.         add.l    vc_Step(a0),d2        ; final sample rate
  807.  
  808.         movea.l    vc_Wave(a0),a3        ; sample data
  809.  
  810.         moveq    #79,d6            ; 80 samples
  811.  
  812. .floop        moveq    #0,d0
  813.         swap    d1
  814.         move.b    (a3,d1.w),d0        ; sample
  815.         swap    d1
  816.         asr.b    d5,d0
  817.         add.b    d0,(a1)+
  818.  
  819.         add.l    d2,d1
  820.         cmp.l    d4,d1
  821.         blo.b    .2
  822.         sub.l    d4,d1
  823.         add.l    d3,d1
  824.         tst.l    d3
  825.         beq.b    .3            ; no looping
  826. .2        dbra    d6,.floop
  827.         bra.b    .done            ; done with voice 1
  828.  
  829. ; ran out of data
  830. .3        clr.b    vc_Flags(a0)        ; voice off
  831.  
  832. .done        move.l    d1,vc_Index(a0)
  833.         bra    .next
  834.  
  835. ;------------------------------------------------------------------------
  836.  
  837.         CNOP    0,16
  838.  
  839. NextEvent    movea.l    MusIndex,a1
  840.  
  841. .0        move.b    (a1)+,d0        ; get next event
  842.         move.b    d0,d1
  843.         lsr.b    #3,d1
  844.         andi.w    #$E,d1            ; d1 = event type * 2
  845.         lea    EventTable,a0
  846.         move.w    (a0,d1.w),d1
  847.         jsr    (a0,d1.w)        ; do event
  848.         tst.b    d0
  849.         bpl.b    .0            ; more events
  850.  
  851.         moveq    #0,d1            ; time = 0
  852. .1        move.b    (a1)+,d0        ; get byte
  853.         bpl.b    .2
  854.  
  855.         andi.w    #$7F,d0            ; kill sign bit
  856.         or.b    d0,d1            ; time = time + 7 bits
  857.         lsl.l    #7,d1            ; * 128
  858.         bra.b    .1            ; get next 7 bits
  859.  
  860. .2        or.b    d0,d1            ; time = time + last 7 bits
  861.         subq.l    #1,d1            ; delay = time - 1
  862.         bmi.b    .0            ; (no delay)
  863.  
  864.         btst    #6,Flags        ; gadget down?
  865.         beq.b    .nogad
  866.         btst    #7,Flags
  867.         beq.b    .rev
  868.         add.l    d1,MyTicks        ; add to my timer
  869.         addq.l    #1,MyTicks        ; + 1
  870.         lsr.l    #3,d1            ; delay = delay / 8
  871.         bra.b    .nogad
  872. .rev        bsr.b    PrevBlock
  873.  
  874. .nogad        move.l    d1,MusDelay        ; store delay
  875.         move.l    a1,MusIndex        ; store index
  876.         rts
  877.  
  878. PrevBlock    move.l    PrevDelay,d0
  879.         sub.l    d0,MyTicks        ; sub previous value
  880.         addq.l    #1,d1
  881.         move.l    d1,PrevDelay
  882.         move.l    a1,d1            ; d1 = current ptr
  883.         movea.l    MusPtr(pc),a0
  884.         move.w    6(a0),d0
  885.         ror.w    #8,d0
  886.         lea    (a0,d0.w),a0        ; a0 = start
  887.         cmp.l    a0,d1            ; at start?
  888.         beq.b    .done
  889.         lea    EventBlocks,a1
  890. .loop        moveq    #0,d0
  891.         move.w    (a1)+,d0        ; offset
  892.         beq.b    .done            ; (not found)
  893.         add.l    a0,d0            ; ptr
  894.         cmp.l    d1,d0            ; = current?
  895.         bne.b    .loop
  896.         subq.l    #4,a1
  897.         cmpa.l    #EventBlocks,a1        ; 2nd block?
  898.         bls.b    .done
  899.         moveq    #0,d0
  900.         move.w    -(a1),d0        ; offset prev block
  901.         add.l    a0,d0
  902.         movea.l    d0,a1            ; ptr = prev block
  903.         moveq    #0,d1            ; delay = 0
  904.         rts
  905. .done        movea.l    a0,a1            ; ptr = start
  906.         moveq    #0,d1            ; delay = 0
  907.         clr.l    MyTicks            ; reset timer
  908.         rts
  909.  
  910. ;------------------------------------------------------------------------
  911.  
  912. Release        moveq    #15,d1
  913.         and.b    d0,d1            ; d1 = channel
  914.  
  915.         lea    Channels,a0
  916.         movea.l    (a0,d1.w*4),a0        ; channel record
  917.         movea.l    ch_Map(a0),a0        ; channel map
  918.  
  919.         move.b    (a1)+,d1        ; note #
  920.         moveq    #0,d2
  921.         move.b    (a0,d1.w),d2        ; voice #
  922.         beq.b    .exit            ; no mapping
  923.  
  924.         clr.b    (a0,d1.w)        ; clear mapping
  925.         move.l    VoiceAvail,d3
  926.         bclr    d2,d3            ; voice free for use
  927.         move.l    d3,VoiceAvail
  928.  
  929.         lea    Voices,a0
  930.         movea.l    (a0,d2.w*4),a0        ; voice
  931.         bset    #1,vc_Flags(a0)        ; do release
  932.  
  933. .exit        rts
  934.  
  935. ;------------------------------------------------------------------------
  936.  
  937. PlayNote    moveq    #15,d1
  938.         and.b    d0,d1            ; d1 = channel
  939.  
  940.         lea    Channels,a0
  941.         movea.l    (a0,d1.w*4),a2        ; channel record
  942.         movea.l    ch_Map(a2),a0        ; channel map
  943.  
  944.         moveq    #-1,d2            ; no volume change
  945.         move.b    (a1)+,d1        ; note #
  946.         bclr    #7,d1
  947.         beq.b    .getvc            ; no volume
  948.  
  949.         moveq    #0,d2
  950.         move.b    (a1)+,d2        ; volume
  951.  
  952. .getvc        moveq    #0,d3
  953.         move.l    VoiceAvail,d4
  954. .vloop        bset    d3,d4
  955.         beq.b    .foundfree
  956.         addq.b    #1,d3
  957.         cmpi.b    #16,d3
  958.         bne.b    .vloop
  959. ; no free voices
  960.         rts
  961.  
  962.  
  963. .foundfree    move.b    d3,(a0,d1.w)        ; voice mapping
  964.         move.l    d4,VoiceAvail
  965.  
  966.         lea    Voices,a0
  967.         movea.l    (a0,d3.w*4),a3        ; voice
  968.         btst    #7,vc_Flags(a3)
  969.         bne.b    .exit            ; sfx using channel
  970.  
  971.         tst.b    d2
  972.         bmi.b    .skip
  973.         move.b    d2,ch_Vol(a2)        ; new channel volume
  974. .skip        move.b    ch_Vol(a2),vc_Vol(a3)
  975.  
  976.         moveq    #15,d2
  977.         and.b    d0,d2
  978.         cmpi.b    #15,d2
  979.         beq.b    .percussion
  980.  
  981.         move.l    ch_Instr(a2),a4        ; instrument record
  982.  
  983.         lea    NoteTable,a0
  984.         moveq    #72,d2            ; middle c
  985.         sub.b    in_Base(a4),d2
  986.         add.b    d1,d2
  987.         move.l    (a0,d2.w*4),vc_Step(a3)    ; step value for note
  988.  
  989.         clr.l    vc_Index(a3)
  990.  
  991.         move.l    a2,vc_Channel(a3)    ; back link (for pitch wheel)
  992.  
  993.         move.l    in_Wave(a4),vc_Wave(a3)
  994.         move.l    in_Loop(a4),vc_Loop(a3)
  995.         move.l    in_Length(a4),vc_Length(a3)
  996.         move.b    in_Flags(a4),vc_Flags(a3)
  997. .exit        rts
  998.  
  999. ; for the percussion channel, the note played sets the percussion instrument
  1000.  
  1001. .percussion    move.l    #65536,vc_Step(a3)    ; sample rate always 1.0
  1002.  
  1003.         clr.l    vc_Index(a3)
  1004.  
  1005.         move.l    a2,vc_Channel(a3)    ; back link
  1006.  
  1007.         addi.b    #100,d1            ; percussion instruments
  1008.  
  1009.         lea    Instruments,a0
  1010.         move.l    (a0,d1.w*4),a0        ; instrument record
  1011.         move.l    in_Wave(a0),vc_Wave(a3)
  1012.         move.l    in_Loop(a0),vc_Loop(a3)
  1013.         move.l    in_Length(a0),vc_Length(a3)
  1014.         move.b    in_Flags(a0),vc_Flags(a3)
  1015.         rts
  1016.  
  1017. ;------------------------------------------------------------------------
  1018.  
  1019. Pitch        moveq    #15,d1
  1020.         and.b    d0,d1            ; d1 = channel
  1021.  
  1022.         lea    Channels,a0
  1023.         movea.l    (a0,d1.w*4),a2        ; channel record
  1024.  
  1025.         moveq    #0,d1
  1026.         move.b    (a1)+,d1        ; pitch wheel setting
  1027.         lea    PitchTable,a0
  1028.         move.l    (a0,d1.w*4),ch_Pitch(a2)
  1029.         rts
  1030.  
  1031. ;------------------------------------------------------------------------
  1032.  
  1033. Tempo        addq.l    #1,a1            ; skip value
  1034.         rts
  1035.  
  1036. ;------------------------------------------------------------------------
  1037.  
  1038. ChangeCtrl    moveq    #15,d1
  1039.         and.b    d0,d1            ; d1 = channel
  1040.  
  1041.         lea    Channels,a0
  1042.         movea.l    (a0,d1.w*4),a2        ; channel
  1043.  
  1044.         move.b    (a1)+,d1        ; get controller
  1045.  
  1046.         moveq    #0,d2
  1047.         move.b    (a1)+,d2        ; value
  1048.  
  1049.         tst.b    d1
  1050.         bne.b    .1
  1051.  
  1052. ; set channel instrument
  1053.  
  1054.         lea    Instruments,a0
  1055.         move.l    (a0,d2.w*4),ch_Instr(a2)
  1056.         bne.b    .0
  1057.         move.l    #QuietInst,ch_Instr(a2)
  1058. .0        rts
  1059.  
  1060. .1        cmpi.b    #3,d1            ; volume?
  1061.         bne.b    .2
  1062.  
  1063. ; set channel volume
  1064.  
  1065.         move.b    d2,ch_Vol(a2)
  1066.         rts
  1067.  
  1068. .2        cmpi.b    #4,d1            ; pan?
  1069.         bne.b    .exit
  1070.  
  1071. ; set channel pan
  1072.  
  1073.         move.b    d2,ch_Pan(a2)
  1074. .exit        rts
  1075.  
  1076. ;------------------------------------------------------------------------
  1077.  
  1078. NoEvent        rts
  1079.  
  1080. ;------------------------------------------------------------------------
  1081.  
  1082. EndScore    btst    #4,Flags        ; loop?
  1083.         beq.b    .loop
  1084.  
  1085.         lea    _custom,a0
  1086.         move.w    #$0600,intena(a0)    ; kill int enable
  1087.         move.w    #$0600,intreq(a0)    ; kill request
  1088.         clr.b    MusFlag            ; clear mus flag
  1089.         bclr    #2,Flags        ; kill playing flag
  1090.         moveq    #0,d0
  1091.         move.w    d0,aud2+ac_vol(a0)    ; kill old audio
  1092.         move.w    d0,aud3+ac_vol(a0)
  1093.         bsr    KillAllAud
  1094.         addq.l    #8,a7            ; pop NextEvent, AudioInt
  1095.         movem.l    (a7)+,d2-d6/a2-a4/a6    ; return from interrupt
  1096.         moveq    #0,d0
  1097.         rts
  1098.  
  1099. .loop        movea.l    MusPtr,a1
  1100.         moveq    #0,d1
  1101.         move.w    6(a1),d1        ; score start
  1102.         ror.w    #8,d1
  1103.         adda.l    d1,a1            ; a1 = start
  1104.  
  1105.         clr.l    MyTicks            ; reset my timer
  1106.         rts
  1107.  
  1108. ;------------------------------------------------------------------------
  1109. ;--------------------------------------------------------------------
  1110.  
  1111.         cnop    0,4
  1112.  
  1113. CreatePool    movea.l    _SysBase,a1
  1114.         cmpi.w    #39,LIB_VERSION(a1)
  1115.         blt.b    .nopools        ; change to bra for debugging
  1116.  
  1117.         move.l    a6,-(sp)
  1118.         movea.l    a1,a6
  1119.         CALLSYS    CreatePool
  1120.         movea.l    (sp)+,a6
  1121.         rts
  1122.  
  1123. .nopools    movem.l    d2-d7/a2-a6,-(sp)
  1124.         move.l    d0,d4            ; memory attributes
  1125.         move.l    d1,d3            ; amount to allocate when low
  1126.         move.l    d2,d5            ; size of when not to use pool
  1127.  
  1128.         exg.l    d0,d1            ; swap flags and size
  1129.         movea.l    a1,a6
  1130.         CALLSYS    AllocMem        ; get first block
  1131.         movea.l    d0,a0
  1132.         tst.l    d0
  1133.         beq.b    .exit            ; no memory!
  1134.  
  1135.         movem.l    d3-d5,(a0)        ; puddleSize, Flags,Threshold
  1136.         clr.l    12(a0)            ; no next block
  1137.         lea    24(a0),a1        ; first free location here
  1138.         move.l    a1,16(a0)
  1139.         subi.l    #24,d3            ; for header info
  1140.         move.l    d3,20(a0)        ; amount free in this block
  1141.  
  1142. .exit        movem.l    (sp)+,d2-d7/a2-a6
  1143.         rts
  1144.  
  1145.         cnop    0,4
  1146.  
  1147. DeletePool    movea.l    _SysBase,a1
  1148.         cmpi.w    #39,LIB_VERSION(a1)
  1149.         blt.b    .nopools        ; change to bra for debugging
  1150.  
  1151.         move.l    a6,-(sp)
  1152.         movea.l    a1,a6
  1153.         CALLSYS    DeletePool
  1154.         movea.l    (sp)+,a6
  1155.         rts
  1156.  
  1157. .nopools    movem.l    d2-d7/a2-a6,-(sp)
  1158.         move.l    a0,d2            ; first block
  1159.         beq.b    .exit            ; safety check
  1160.  
  1161.         movea.l    a1,a6
  1162.  
  1163. .loop        movea.l    d2,a1            ; pointer to block
  1164.         move.l    (a1),d0            ; size of block
  1165.         move.l    12(a1),d2        ; next block
  1166.         CALLSYS    FreeMem
  1167.         tst.l    d2
  1168.         bne.b    .loop
  1169.  
  1170. .exit        movem.l    (sp)+,d2-d7/a2-a6
  1171.         rts
  1172.  
  1173.         cnop    0,4
  1174.  
  1175. AllocPooled    movea.l    _SysBase,a1
  1176.         cmpi.w    #39,LIB_VERSION(a1)
  1177.         blt.b    .nopools        ; change to bra for debugging
  1178.  
  1179.         move.l    a6,-(sp)
  1180.         movea.l    a1,a6
  1181.         CALLSYS    AllocPooled
  1182.         movea.l    (sp)+,a6
  1183.         rts
  1184.  
  1185. .nopools    movem.l    d2-d7/a2-a6,-(sp)
  1186.         move.l    a0,d2
  1187.         beq.b    .exit            ; safety check
  1188.  
  1189.         addq.l    #3,d0
  1190.         andi.b    #$FC,d0            ; long align size
  1191.  
  1192.         movea.l    a1,a6
  1193.  
  1194.         cmp.l    8(a0),d0        ; check threshold
  1195.         blt.b    .chkpuddles        ; allocate from puddles
  1196.  
  1197.         addi.l    #24,d0            ; for header
  1198.         move.l    d0,d3            ; save size
  1199.         move.l    4(a0),d1        ; mem attrs
  1200.         CALLSYS    AllocMem
  1201.         movea.l    d0,a0
  1202.         tst.l    d0
  1203.         beq.b    .exit            ; no memory
  1204.  
  1205.         move.l    d3,(a0)            ; size of block
  1206.         clr.l    20(a0)            ; no free space in here
  1207.  
  1208.         movea.l    d2,a1            ; pool header
  1209.         move.l    12(a1),d1
  1210.         move.l    a0,12(a1)        ; splice in block
  1211.         move.l    d1,12(a0)        ; relink next block
  1212.         lea    24(a0),a0        ; skip over header
  1213.  
  1214. .exit        move.l    a0,d0
  1215.         movem.l    (sp)+,d2-d7/a2-a6
  1216.         rts
  1217.  
  1218.         cnop    0,4
  1219.  
  1220. .chkpuddles    cmp.l    20(a0),d0        ; check free space
  1221.         blt.b    .gotspace
  1222.  
  1223.         movea.l    12(a0),a0        ; next block
  1224.         move.l    a0,d1
  1225.         bne.b    .chkpuddles
  1226.  
  1227. ; not enough free space in existing puddles, create another
  1228.  
  1229.         move.l    d0,d6            ; save size
  1230.  
  1231.         movea.l    d2,a0            ; pool header
  1232.         movem.l    (a0),d3-d5
  1233.         movem.l    (a0),d0-d1
  1234.         CALLSYS    AllocMem        ; get block
  1235.         movea.l    d0,a0
  1236.         tst.l    d0
  1237.         beq.b    .out            ; no memory!
  1238.  
  1239.         movea.l    d2,a1            ; pool header
  1240.         movem.l    d3-d5,(a0)        ; puddleSize, Flags,Threshold
  1241.         move.l    12(a1),12(a0)        ; next block
  1242.         move.l    a0,12(a1)        ; splice in block
  1243.         lea    24(a0),a1        ; first free location here
  1244.         move.l    a1,16(a0)
  1245.         subi.l    #24,d3            ; for header info
  1246.         move.l    d3,20(a0)        ; amount free in this block
  1247.  
  1248.         move.l    d6,d0            ; restore size
  1249.  
  1250. .gotspace    sub.l    d0,20(a0)        ; sub from amount free
  1251.         bmi.b    .err            ; threshold >= puddlesize!
  1252.  
  1253.         move.l    16(a0),a1        ; free space
  1254.         add.l    d0,16(a0)        ; next free space
  1255.  
  1256.         movea.l    a1,a0
  1257.         bra.b    .out
  1258.  
  1259. .err        add.l    d0,20(a0)        ; restore free space
  1260.         moveq    #0,d0
  1261.         suba.l    a0,a0            ; no memory
  1262.  
  1263. .out        move.l    a0,d0
  1264.         movem.l    (sp)+,d2-d7/a2-a6
  1265.         rts
  1266.  
  1267. ;------------------------------------------------------------------------
  1268. ;------------------------------------------------------------------------
  1269. ;------------------------------------------------------------------------
  1270. ;------------------------------------------------------------------------
  1271.  
  1272. MUSMemPtr    dc.l    0
  1273. MUSMemSize    dc.l    0
  1274.  
  1275. ;------------------------------------------------------------------------
  1276.  
  1277. midifileproblem    dc.b    "Error opening MIDI_Instruments file!",0
  1278.  
  1279. dosproblem    dc.b    "Error reading MIDI_Instruments file!",0
  1280.  
  1281. memproblem    dc.b    "Not enough memory for music!",0
  1282.  
  1283. notmusfile    dc.b    "Music lump in WAD file is not .MUS format!",0
  1284.  
  1285. damagedfile    dc.b    "Damaged MIDI_Instruments or WAD file!",0
  1286.  
  1287. cantopenaud    dc.b    "Can't open audio.device for music!",0
  1288.  
  1289. cantgetchan    dc.b    "Can't allocate channels for music!",0
  1290.  
  1291.         cnop    0,4
  1292.  
  1293. ;------------------------------------------------------------------------
  1294. ; bit 0 = close, bit 1 = file loaded, bit 2 = playing, bit 3 = paused
  1295. ; bit 4 = loop, bit 5 = attention, bit 6 = gadget down, bit 7 = rev/FF
  1296. Flags        dc.b    0
  1297. Flags2        dc.b    0            ; backup of Flags
  1298.  
  1299. ; bit 0,1 = dotick
  1300. Flags3        dc.b    0
  1301.  
  1302.         cnop    0,4
  1303. per        dc.w    0
  1304. per2        dc.w    0    ; period for 11048 Hz
  1305.  
  1306. ;------------------------------------------------------------------------
  1307.         cnop    0,4
  1308.  
  1309. EventTable    dc.w    Release-EventTable
  1310.         dc.w    PlayNote-EventTable
  1311.         dc.w    Pitch-EventTable
  1312.         dc.w    Tempo-EventTable
  1313.         dc.w    ChangeCtrl-EventTable
  1314.         dc.w    NoEvent-EventTable
  1315.         dc.w    EndScore-EventTable
  1316.         dc.w    NoEvent-EventTable
  1317.  
  1318. ;------------------------------------------------------------------------
  1319.  
  1320.         cnop    0,4
  1321.  
  1322. looping        dc.l    0
  1323. musicdies    dc.l    -1
  1324.  
  1325. MyTicks        dc.l    0
  1326.  
  1327. PrevDelay    dc.l    0
  1328.  
  1329. AudioPort    dc.l    0,0
  1330.         dc.b    NT_MSGPORT,0        ; LN_TYPE, LN_PRI
  1331.         dc.l    0            ; LN_NAME
  1332.         dc.b    PA_SIGNAL        ; mp_Flags
  1333.         dc.b    0            ; mp_SigBit
  1334. AudioTask    dc.l    0            ; mp_SigTask
  1335. .1        dc.l    .2
  1336. .2        dc.l    0
  1337.         dc.l    .1
  1338.  
  1339.         cnop    0,4
  1340.  
  1341. AudioIO        dc.l    0,0
  1342.         dc.b    NT_REPLYMSG,0        ; LN_TYPE, LN_PRI
  1343.         dc.l    0            ; LN_NAME
  1344.         dc.l    AudioPort        ; mn_ReplyPort
  1345.         dc.w    68            ; mn_Length
  1346.         dc.l    0            ; IO_DEVICE
  1347.         dc.l    0            ; IO_UNIT
  1348.         dc.w    0            ; IO_COMMAND
  1349.         dc.b    0            ; IO_FLAGS
  1350.         dc.b    0            ; IO_ERROR
  1351.         dc.w    0            ; ioa_AllocKey
  1352.         dc.l    0            ; ioa_Data
  1353.         dc.l    0            ; ioa_Length
  1354.         dc.w    0            ; ioa_Period
  1355.         dc.w    0            ; ioa_Volume
  1356.         dc.w    0            ; ioa_Cycles
  1357.         dc.l    0,0            ; ioa_WriteMsg
  1358.         dc.b    0,0
  1359.         dc.l    0
  1360.         dc.l    0
  1361.         dc.w    0
  1362.  
  1363.         cnop    0,4
  1364.  
  1365. AInt2        dc.l    0,0
  1366.         dc.b    NT_INTERRUPT,0        ; LN_TYPE, LN_PRI
  1367.         dc.l    TPortName        ; LN_NAME
  1368.         dc.l    0            ; IS_DATA
  1369.         dc.l    AudioINT2        ; IS_CODE
  1370.  
  1371. MusPtr        dc.l    0
  1372. MusIndex    dc.l    0
  1373. MusDelay    dc.l    0
  1374. OldAInt2    dc.l    0
  1375.  
  1376. AudioAlloc    dc.b    $0c            ; Amiga channels to allocate
  1377.  
  1378. AudioName    dc.b    'audio.device',0
  1379. TPortName    dc.b    'ADoom-MUS',0
  1380. AudioOK        dc.b    0
  1381. AudioCh        dc.b    0
  1382. musicarg    dc.b    "-music",0
  1383.  
  1384. ;--------------------------------------
  1385.  
  1386.         CNOP    0,4
  1387.  
  1388. ; bit set if voice is in use (0-15=music voices,16-31=sfx voices)
  1389.  
  1390. VoiceAvail    dc.l    0
  1391.  
  1392.  
  1393. ; bit 0 = stop processing, bit 1 = buffer indicator
  1394.  
  1395. MusFlag        dc.b    0
  1396.  
  1397. ;--------------------------------------------------------------------
  1398.  
  1399.         CNOP    0,4
  1400.  
  1401. NoteTable    dc.l    65536/64,69433/64,73562/64,77936/64,82570/64,87480/64,92682/64,98193/64,104032/64,110218/64,116772/64,123715/64
  1402.         dc.l    65536/32,69433/32,73562/32,77936/32,82570/32,87480/32,92682/32,98193/32,104032/32,110218/32,116772/32,123715/32
  1403.         dc.l    65536/16,69433/16,73562/16,77936/16,82570/16,87480/16,92682/16,98193/16,104032/16,110218/16,116772/16,123715/16
  1404.         dc.l    65536/8,69433/8,73562/8,77936/8,82570/8,87480/8,92682/8,98193/8,104032/8,110218/8,116772/8,123715/8
  1405.         dc.l    65536/4,69433/4,73562/4,77936/4,82570/4,87480/4,92682/4,98193/4,104032/4,110218/4,116772/4,123715/4
  1406.         dc.l    65536/2,69433/2,73562/2,77936/2,82570/2,87480/2,92682/2,98193/2,104032/2,110218/2,116772/2,123715/2
  1407.         dc.l    65536,69433,73562,77936,82570,87480,92682,98193,104032,110218,116772,123715
  1408.         dc.l    65536*2,69433*2,73562*2,77936*2,82570*2,87480*2,92682*2,98193*2,104032*2,110218*2,116772*2,123715*2
  1409.         dc.l    65536*4,69433*4,73562*4,77936*4,82570*4,87480*4,92682*4,98193*4,104032*4,110218*4,116772*4,123715*4
  1410.         dc.l    65536*8,69433*8,73562*8,77936*8,82570*8,87480*8,92682*8,98193*8,104032*8,110218*8,116772*8,123715*8
  1411.         dc.l    65536*16,69433*16,73562*16,77936*16,82570*16,87480*16,92682*16,98193*16
  1412.  
  1413. ;------------------------------------------------------------------------
  1414.  
  1415. PitchTable:
  1416.  
  1417. pitch_ix    SET    128
  1418.  
  1419.         REPT    128
  1420.         dc.l    -3678*pitch_ix/64
  1421. pitch_ix    SET    pitch_ix-1
  1422.         ENDR
  1423.  
  1424.         REPT    128
  1425.         dc.l    3897*pitch_ix/64
  1426. pitch_ix    SET    pitch_ix+1
  1427.         ENDR
  1428.  
  1429. ;------------------------------------------------------------------------
  1430.  
  1431.         CNOP    0,4
  1432.  
  1433. AudioCh2Buf    dc.l    chipbuffer2
  1434. AudioCh2Ptr    dc.l    chipbuffer2
  1435. AudioCh2Vol    dc.w    64
  1436.  
  1437.         CNOP    0,4
  1438.  
  1439. AudioCh3Buf    dc.l    chipbuffer3
  1440. AudioCh3Ptr    dc.l    chipbuffer3
  1441. AudioCh3Vol    dc.w    64
  1442.  
  1443. ;------------------------------------------------------------------------
  1444.  
  1445.         STRUCTURE MusChannel,0
  1446.         APTR    ch_Instr
  1447.         APTR    ch_Map
  1448.         ULONG    ch_Pitch
  1449.         BYTE    ch_Vol
  1450.         BYTE    ch_Pan
  1451.  
  1452.  
  1453.         CNOP    0,4
  1454.  
  1455. Channels    dc.l    Channel0,Channel1,Channel2,Channel3
  1456.         dc.l    Channel4,Channel5,Channel6,Channel7
  1457.         dc.l    Channel8,Channel9,Channel10,Channel11
  1458.         dc.l    Channel12,Channel13,Channel14,Channel15
  1459.  
  1460.  
  1461.         CNOP    0,4
  1462.  
  1463. Channel0    dc.l    0        ; instrument
  1464.         dc.l    Channel0Map    ; note to voice map
  1465.         dc.l    0        ; pitch wheel setting
  1466.         dc.b    0        ; volume
  1467.         dc.b    0        ; pan setting
  1468.  
  1469.         CNOP    0,4
  1470.  
  1471. Channel1    dc.l    0        ; instrument
  1472.         dc.l    Channel1Map    ; note to voice map
  1473.         dc.l    0        ; pitch wheel setting
  1474.         dc.b    0        ; volume
  1475.         dc.b    0        ; pan setting
  1476.  
  1477.         CNOP    0,4
  1478.  
  1479. Channel2    dc.l    0        ; instrument
  1480.         dc.l    Channel2Map    ; note to voice map
  1481.         dc.l    0        ; pitch wheel setting
  1482.         dc.b    0        ; volume
  1483.         dc.b    0        ; pan setting
  1484.  
  1485.         CNOP    0,4
  1486.  
  1487. Channel3    dc.l    0        ; instrument
  1488.         dc.l    Channel3Map    ; note to voice map
  1489.         dc.l    0        ; pitch wheel setting
  1490.         dc.b    0        ; volume
  1491.         dc.b    0        ; pan setting
  1492.  
  1493.         CNOP    0,4
  1494.  
  1495. Channel4    dc.l    0        ; instrument
  1496.         dc.l    Channel4Map    ; note to voice map
  1497.         dc.l    0        ; pitch wheel setting
  1498.         dc.b    0        ; volume
  1499.         dc.b    0        ; pan setting
  1500.  
  1501.         CNOP    0,4
  1502.  
  1503. Channel5    dc.l    0        ; instrument
  1504.         dc.l    Channel5Map    ; note to voice map
  1505.         dc.l    0        ; pitch wheel setting
  1506.         dc.b    0        ; volume
  1507.         dc.b    0        ; pan setting
  1508.  
  1509.         CNOP    0,4
  1510.  
  1511. Channel6    dc.l    0        ; instrument
  1512.         dc.l    Channel6Map    ; note to voice map
  1513.         dc.l    0        ; pitch wheel setting
  1514.         dc.b    0        ; volume
  1515.         dc.b    0        ; pan setting
  1516.  
  1517.         CNOP    0,4
  1518.  
  1519. Channel7    dc.l    0        ; instrument
  1520.         dc.l    Channel7Map    ; note to voice map
  1521.         dc.l    0        ; pitch wheel setting
  1522.         dc.b    0        ; volume
  1523.         dc.b    0        ; pan setting
  1524.  
  1525.         CNOP    0,4
  1526.  
  1527. Channel8    dc.l    0        ; instrument
  1528.         dc.l    Channel8Map    ; note to voice map
  1529.         dc.l    0        ; pitch wheel setting
  1530.         dc.b    0        ; volume
  1531.         dc.b    0        ; pan setting
  1532.  
  1533.         CNOP    0,4
  1534.  
  1535. Channel9    dc.l    0        ; instrument
  1536.         dc.l    Channel9Map    ; note to voice map
  1537.         dc.l    0        ; pitch wheel setting
  1538.         dc.b    0        ; volume
  1539.         dc.b    0        ; pan setting
  1540.  
  1541.         CNOP    0,4
  1542.  
  1543. Channel10    dc.l    0        ; instrument
  1544.         dc.l    Channel10Map    ; note to voice map
  1545.         dc.l    0        ; pitch wheel setting
  1546.         dc.b    0        ; volume
  1547.         dc.b    0        ; pan setting
  1548.  
  1549.         CNOP    0,4
  1550.  
  1551. Channel11    dc.l    0        ; instrument
  1552.         dc.l    Channel11Map    ; note to voice map
  1553.         dc.l    0        ; pitch wheel setting
  1554.         dc.b    0        ; volume
  1555.         dc.b    0        ; pan setting
  1556.  
  1557.         CNOP    0,4
  1558.  
  1559. Channel12    dc.l    0        ; instrument
  1560.         dc.l    Channel12Map    ; note to voice map
  1561.         dc.l    0        ; pitch wheel setting
  1562.         dc.b    0        ; volume
  1563.         dc.b    0        ; pan setting
  1564.  
  1565.         CNOP    0,4
  1566.  
  1567. Channel13    dc.l    0        ; instrument
  1568.         dc.l    Channel13Map    ; note to voice map
  1569.         dc.l    0        ; pitch wheel setting
  1570.         dc.b    0        ; volume
  1571.         dc.b    0        ; pan setting
  1572.  
  1573.         CNOP    0,4
  1574.  
  1575. Channel14    dc.l    0        ; instrument
  1576.         dc.l    Channel14Map    ; note to voice map
  1577.         dc.l    0        ; pitch wheel setting
  1578.         dc.b    0        ; volume
  1579.         dc.b    0        ; pan setting
  1580.  
  1581.         CNOP    0,4
  1582.  
  1583. Channel15    dc.l    0        ; instrument
  1584.         dc.l    Channel15Map    ; note to voice map
  1585.         dc.l    0        ; pitch wheel setting
  1586.         dc.b    0        ; volume
  1587.         dc.b    0        ; pan setting
  1588.  
  1589.  
  1590.         CNOP    0,4
  1591.  
  1592. Channel0Map    dcb.b    128,0
  1593. Channel1Map    dcb.b    128,0
  1594. Channel2Map    dcb.b    128,0
  1595. Channel3Map    dcb.b    128,0
  1596. Channel4Map    dcb.b    128,0
  1597. Channel5Map    dcb.b    128,0
  1598. Channel6Map    dcb.b    128,0
  1599. Channel7Map    dcb.b    128,0
  1600. Channel8Map    dcb.b    128,0
  1601. Channel9Map    dcb.b    128,0
  1602. Channel10Map    dcb.b    128,0
  1603. Channel11Map    dcb.b    128,0
  1604. Channel12Map    dcb.b    128,0
  1605. Channel13Map    dcb.b    128,0
  1606. Channel14Map    dcb.b    128,0
  1607. Channel15Map    dcb.b    128,0
  1608.  
  1609. ;--------------------------------------
  1610.  
  1611.         STRUCTURE AudioVoice,0
  1612.         APTR    vc_Next
  1613.         APTR    vc_Channel
  1614.         APTR    vc_Wave
  1615.         ULONG    vc_Index
  1616.         ULONG    vc_Step
  1617.         ULONG    vc_Loop
  1618.         ULONG    vc_Length
  1619.         BYTE    vc_Vol
  1620.         BYTE    vc_Flags    ; b7 = SFX, b1 = RLS, b0 = EN
  1621.  
  1622.         CNOP    0,4
  1623.  
  1624. Voices        dc.l    Voice0,Voice1,Voice2,Voice3
  1625.         dc.l    Voice4,Voice5,Voice6,Voice7
  1626.         dc.l    Voice8,Voice9,Voice10,Voice11
  1627.         dc.l    Voice12,Voice13,Voice14,Voice15
  1628.  
  1629.         CNOP    0,4
  1630.  
  1631. Voice0        dc.l    Voice1
  1632.         dc.l    0        ; channel back-link
  1633.         dc.l    0        ; instrument wave data
  1634.         dc.l    0        ; sample index
  1635.         dc.l    0        ; sample rate
  1636.         dc.l    0        ; instrument loop point
  1637.         dc.l    0        ; instrument data length
  1638.         dc.b    0        ; voice volume scale
  1639.         dc.b    0        ; voice flags
  1640.  
  1641.         CNOP    0,4
  1642.  
  1643. Voice1        dc.l    Voice2
  1644.         dc.l    0        ; channel back-link
  1645.         dc.l    0        ; instrument wave data
  1646.         dc.l    0        ; sample index
  1647.         dc.l    0        ; sample rate
  1648.         dc.l    0        ; instrument loop point
  1649.         dc.l    0        ; instrument data length
  1650.         dc.b    0        ; voice volume scale
  1651.         dc.b    0        ; voice flags
  1652.  
  1653.         CNOP    0,4
  1654.  
  1655. Voice2        dc.l    Voice3
  1656.         dc.l    0        ; channel back-link
  1657.         dc.l    0        ; instrument wave data
  1658.         dc.l    0        ; sample index
  1659.         dc.l    0        ; sample rate
  1660.         dc.l    0        ; instrument loop point
  1661.         dc.l    0        ; instrument data length
  1662.         dc.b    0        ; voice volume scale
  1663.         dc.b    0        ; voice flags
  1664.  
  1665.         CNOP    0,4
  1666.  
  1667. Voice3        dc.l    Voice4
  1668.         dc.l    0        ; channel back-link
  1669.         dc.l    0        ; instrument wave data
  1670.         dc.l    0        ; sample index
  1671.         dc.l    0        ; sample rate
  1672.         dc.l    0        ; instrument loop point
  1673.         dc.l    0        ; instrument data length
  1674.         dc.b    0        ; voice volume scale
  1675.         dc.b    0        ; voice flags
  1676.  
  1677.         CNOP    0,4
  1678.  
  1679. Voice4        dc.l    Voice5
  1680.         dc.l    0        ; channel back-link
  1681.         dc.l    0        ; instrument wave data
  1682.         dc.l    0        ; sample index
  1683.         dc.l    0        ; sample rate
  1684.         dc.l    0        ; instrument loop point
  1685.         dc.l    0        ; instrument data length
  1686.         dc.b    0        ; voice volume scale
  1687.         dc.b    0        ; voice flags
  1688.  
  1689.         CNOP    0,4
  1690.  
  1691. Voice5        dc.l    Voice6
  1692.         dc.l    0        ; channel back-link
  1693.         dc.l    0        ; instrument wave data
  1694.         dc.l    0        ; sample index
  1695.         dc.l    0        ; sample rate
  1696.         dc.l    0        ; instrument loop point
  1697.         dc.l    0        ; instrument data length
  1698.         dc.b    0        ; voice volume scale
  1699.         dc.b    0        ; voice flags
  1700.  
  1701.         CNOP    0,4
  1702.  
  1703. Voice6        dc.l    Voice7
  1704.         dc.l    0        ; channel back-link
  1705.         dc.l    0        ; instrument wave data
  1706.         dc.l    0        ; sample index
  1707.         dc.l    0        ; sample rate
  1708.         dc.l    0        ; instrument loop point
  1709.         dc.l    0        ; instrument data length
  1710.         dc.b    0        ; voice volume scale
  1711.         dc.b    0        ; voice flags
  1712.  
  1713.         CNOP    0,4
  1714.  
  1715. Voice7        dc.l    Voice8
  1716.         dc.l    0        ; channel back-link
  1717.         dc.l    0        ; instrument wave data
  1718.         dc.l    0        ; sample index
  1719.         dc.l    0        ; sample rate
  1720.         dc.l    0        ; instrument loop point
  1721.         dc.l    0        ; instrument data length
  1722.         dc.b    0        ; voice volume scale
  1723.         dc.b    0        ; voice flags
  1724.  
  1725.         CNOP    0,4
  1726.  
  1727. Voice8        dc.l    Voice9
  1728.         dc.l    0        ; channel back-link
  1729.         dc.l    0        ; instrument wave data
  1730.         dc.l    0        ; sample index
  1731.         dc.l    0        ; sample rate
  1732.         dc.l    0        ; instrument loop point
  1733.         dc.l    0        ; instrument data length
  1734.         dc.b    0        ; voice volume scale
  1735.         dc.b    0        ; voice flags
  1736.  
  1737.         CNOP    0,4
  1738.  
  1739. Voice9        dc.l    Voice10
  1740.         dc.l    0        ; channel back-link
  1741.         dc.l    0        ; instrument wave data
  1742.         dc.l    0        ; sample index
  1743.         dc.l    0        ; sample rate
  1744.         dc.l    0        ; instrument loop point
  1745.         dc.l    0        ; instrument data length
  1746.         dc.b    0        ; voice volume scale
  1747.         dc.b    0        ; voice flags
  1748.  
  1749.         CNOP    0,4
  1750.  
  1751. Voice10        dc.l    Voice11
  1752.         dc.l    0        ; channel back-link
  1753.         dc.l    0        ; instrument wave data
  1754.         dc.l    0        ; sample index
  1755.         dc.l    0        ; sample rate
  1756.         dc.l    0        ; instrument loop point
  1757.         dc.l    0        ; instrument data length
  1758.         dc.b    0        ; voice volume scale
  1759.         dc.b    0        ; voice flags
  1760.  
  1761.         CNOP    0,4
  1762.  
  1763. Voice11        dc.l    Voice12
  1764.         dc.l    0        ; channel back-link
  1765.         dc.l    0        ; instrument wave data
  1766.         dc.l    0        ; sample index
  1767.         dc.l    0        ; sample rate
  1768.         dc.l    0        ; instrument loop point
  1769.         dc.l    0        ; instrument data length
  1770.         dc.b    0        ; voice volume scale
  1771.         dc.b    0        ; voice flags
  1772.  
  1773.         CNOP    0,4
  1774.  
  1775. Voice12        dc.l    Voice13
  1776.         dc.l    0        ; channel back-link
  1777.         dc.l    0        ; instrument wave data
  1778.         dc.l    0        ; sample index
  1779.         dc.l    0        ; sample rate
  1780.         dc.l    0        ; instrument loop point
  1781.         dc.l    0        ; instrument data length
  1782.         dc.b    0        ; voice volume scale
  1783.         dc.b    0        ; voice flags
  1784.  
  1785.         CNOP    0,4
  1786.  
  1787. Voice13        dc.l    Voice14
  1788.         dc.l    0        ; channel back-link
  1789.         dc.l    0        ; instrument wave data
  1790.         dc.l    0        ; sample index
  1791.         dc.l    0        ; sample rate
  1792.         dc.l    0        ; instrument loop point
  1793.         dc.l    0        ; instrument data length
  1794.         dc.b    0        ; voice volume scale
  1795.         dc.b    0        ; voice flags
  1796.  
  1797.         CNOP    0,4
  1798.  
  1799. Voice14        dc.l    Voice15
  1800.         dc.l    0        ; channel back-link
  1801.         dc.l    0        ; instrument wave data
  1802.         dc.l    0        ; sample index
  1803.         dc.l    0        ; sample rate
  1804.         dc.l    0        ; instrument loop point
  1805.         dc.l    0        ; instrument data length
  1806.         dc.b    0        ; voice volume scale
  1807.         dc.b    0        ; voice flags
  1808.  
  1809.         CNOP    0,4
  1810.  
  1811. Voice15        dc.l    0
  1812.         dc.l    0        ; channel back-link
  1813.         dc.l    0        ; instrument wave data
  1814.         dc.l    0        ; sample index
  1815.         dc.l    0        ; sample rate
  1816.         dc.l    0        ; instrument loop point
  1817.         dc.l    0        ; instrument data length
  1818.         dc.b    0        ; voice volume scale
  1819.         dc.b    0        ; voice flags
  1820.  
  1821. ;--------------------------------------
  1822.  
  1823.         STRUCTURE InstrumentRec,0
  1824.         APTR    in_Wave
  1825.         ULONG    in_Loop
  1826.         ULONG    in_Length
  1827.         BYTE    in_Flags
  1828.         BYTE    in_Base
  1829.  
  1830.  
  1831.         CNOP    0,4
  1832.  
  1833. Instruments    dcb.l    256,0
  1834.  
  1835.         CNOP    0,4
  1836.  
  1837. QuietInst    dc.l    0
  1838.         dc.l    0
  1839.         dc.l    0
  1840.         dc.b    0
  1841.         dc.b    0
  1842.  
  1843.  
  1844.         CNOP    0,4
  1845.  
  1846. InstrHandle    dc.l    0
  1847. InstrFile    dc.l    InstrName
  1848. InstrPool    dc.l    0
  1849.  
  1850. InstrName    dc.b    'PROGDIR:MIDI_Instruments',0
  1851.  
  1852.         CNOP    0,4
  1853.  
  1854. validInstr    dc.b    %11111111    ; (00-07) Piano
  1855.         dc.b    %11111111    ; (08-0F) Chrom Perc
  1856.         dc.b    %11111111    ; (10-17) Organ
  1857.         dc.b    %11111111    ; (18-1F) Guitar
  1858.         dc.b    %11111111    ; (20-27) Bass
  1859.         dc.b    %11111111    ; (28-2F) Strings
  1860.         dc.b    %11111111    ; (30-37) Ensemble
  1861.         dc.b    %11111111    ; (38-3F) Brass
  1862.         dc.b    %11111111    ; (40-47) Reed
  1863.         dc.b    %11111111    ; (48-4F) Pipe
  1864.         dc.b    %11111111    ; (50-57) Synth Lead
  1865.         dc.b    %11111111    ; (58-5F) Synth Pad
  1866.         dc.b    %11111111    ; (60-67) Synth Effects
  1867.         dc.b    %11111111    ; (68-6F) Ethnic
  1868.         dc.b    %11111111    ; (70-77) Percussive
  1869.         dc.b    %11111111    ; (78-7F) SFX
  1870.         dc.b    %00000001    ; (80-87) invalid,Drum
  1871.         dc.b    %11111111    ; (88-8F) Drums/Clap/Hi-Hat
  1872.         dc.b    %11111111    ; (90-97) Hi-Hats/Toms/Cymb1
  1873.         dc.b    %11111111    ; (98-9F) Cymbals/Bells/Slap
  1874.         dc.b    %11111111    ; (A0-A7) Bongos/Congas/Timb
  1875.         dc.b    %11111111    ; (A8-AF) Agogo/Whistles/Gui
  1876.         dc.b    %11111100    ; (B0-B7) Claves/Block/Trian
  1877.         dc.b    %00000000    ; (B8-BF) invalid
  1878.         dc.b    %00000000    ; (C0-C7)
  1879.         dc.b    %00000000    ; (C8-CF)
  1880.         dc.b    %00000000    ; (D0-D7)
  1881.         dc.b    %00000000    ; (D8-DF)
  1882.         dc.b    %00000000    ; (E0-E7)
  1883.         dc.b    %00000000    ; (E8-EF)
  1884.         dc.b    %00000000    ; (F0-F7)
  1885.         dc.b    %00000000    ; (F8-FF)
  1886.  
  1887. ;--------------------------------------------------------------------
  1888.         section    PlayMusChip,data_c
  1889.  
  1890. ClearBuf    dcb.b    160,0
  1891.  
  1892.  
  1893. chipbuffer2    dcb.b    320,0
  1894. chipbuffer3    dcb.b    320,0
  1895.  
  1896. ;------------------------------------------------------------------------
  1897.         section    PlayMusBSS,bss
  1898.  
  1899. EventBlocks    ds.w    32768
  1900.  
  1901.  
  1902. tempAudio    ds.b    160
  1903.  
  1904. ;------------------------------------------------------------------------
  1905.  
  1906.         end
  1907.