'===========================================================================
' Subject: BACKGROUND .CMF PLAYER             Date: 05-16-97 (13:18)       
'  Author: Bill McDonald                      Code: QB, PDS                
'  Origin: FidoNet QUIK_BAS Echo            Packet: SOUND.ABC
'===========================================================================
'Here is the Background .CMF player. You need SBFMDRV.COM
'------------------------------------
DECLARE FUNCTION MusicDone% ()
DECLARE SUB StopMusic ()
DECLARE SUB PlayMusic (FileName$)
            'RPLAY written by Rich Dersheimer
            'Modified for Games By Bill McDonald
            ' I put the program into subs.. and made a better format
      
       DEFINT A-Z
        '$INCLUDE: 'QB.BI'
        DIM SHARED iReg AS RegTypeX, oReg AS RegTypeX, Offset
 
        RANDOMIZE TIMER
        PlayMusic "C:\Sound\TheCloud.CMF"
        
        CLS
        DO
        LOOP UNTIL INKEY$ <> "" OR MusicDone% > 0
        StopMusic
        END
 
FUNCTION MusicDone%
IF PEEK(Offset) = 0 THEN MusicDone% = 1 ELSE MusicDone% = 0 ' Check for end of
music fileEND FUNCTION
 
SUB PlayMusic (FileName$)
        '********* Check for the SBFMDRV driver ***************
        DEF SEG = 0
        SegmentCheck = PEEK(512) + PEEK(513) * 256
        AddressCheck = PEEK(514) + PEEK(515) * 256
        DEF SEG
 
        IF SegmentCheck = 0 AND AddressCheck = 0 THEN
            PRINT                                ' This is a check of
            PRINT                                ' the interrupt vector
            PRINT "  SBFMDRV.COM is not loaded!" ' at address 80h.  It
            PRINT                                ' MAY keep your program
            END                                  ' from locking up if
        END IF                                   ' SBFMDRV is not loaded
        '******************************************************
 
 
        '********* Get the file name **************************
        IF FileName$ = "" THEN
            EXIT SUB
        END IF
        IF INSTR(FileName$, ".") = 0 THEN
            FileName$ = FileName$ + ".CMF"
        END IF
 
        '******************************************************
 
 
        '********* Load in the file ***************************
        OPEN FileName$ FOR BINARY AS #1
        FileLength& = LOF(1)
 
        IF FileLength& > 32767 THEN
            PRINT
            PRINT
            PRINT "     "; FileName$;
            PRINT " is too big for to Play!"
            PRINT
            END
        END IF
 
        A$ = STRING$(FileLength&, "r")
        GET #1, 1, A$
        CLOSE #1
 
        IF LEFT$(A$, 4) <> "CTMF" THEN
            PRINT
            PRINT
            PRINT "     "; FileName$;
            PRINT " does not appear to be a CMF file!"
            PRINT
            END
        END IF
        '******************************************************
 
       
        '********* Find the file in memory ********************
        Offset = SADD(A$)           ' offset in the segment
        Segment = VARSEG(A$)        ' segment of the file
                                    ' this is different QB 4.5
 
        DEF SEG = Segment           ' DON'T change the segment
                                    ' while the music is playing!
 
 
        NumberIns = PEEK(Offset + &H24) + PEEK(Offset + &H25) * 256
        InsOffset = PEEK(Offset + 6) + PEEK(Offset + 7) * 256
        InsStart = Offset + InsOffset
        MusicOffset = PEEK(Offset + 8) + PEEK(Offset + 9) * 256
        MusicStart = Offset + MusicOffset
        Ticks = PEEK(Offset + &HC) + PEEK(Offset + &HD) * 256
        '******************************************************
 
 
        '***** Tell soundblaster where to return info *********
        iReg.bx = 1                     ' I'm sending the return
        iReg.dx = Segment               ' info to the first byte
        iReg.ax = Offset                ' of the music file area
        INTERRUPTX &H80, iReg, oReg
        '******************************************************
 
       
        '************ Setup instruments ***********************
        IF NumberIns > 0 AND NumberIns < 17 THEN
            iReg.bx = 2
            iReg.cx = NumberIns         ' if you get an invalid
            iReg.dx = Segment           ' # of instruments, use
            iReg.ax = InsStart          ' default instruments
            INTERRUPTX &H80, iReg, oReg
        END IF
        '******************************************************
 
 
        '************* Set System Clock Rate ******************
        iReg.bx = 3                     ' I'm not really sure
        iReg.ax = &HFFFF                ' you have to do this
        INTERRUPTX &H80, iReg, oReg     ' but it doesn't hurt
        '******************************************************
 
 
        '************* Set Driver Clock Rate ******************
        iReg.bx = 4
        iReg.ax = (1193180 / Ticks)
        INTERRUPTX &H80, iReg, oReg
        '******************************************************
 
 
        '************* Play some music! ***********************
        iReg.bx = 6
        iReg.dx = Segment
        iReg.ax = MusicStart
        INTERRUPTX &H80, iReg, oReg
        '******************************************************
END SUB
 
SUB StopMusic
        '********* Stop music *********************************
        iReg.bx = 7
        INTERRUPTX &H80, iReg, oReg
        '******************************************************
        '********* Reset driver *******************************
        iReg.bx = 8
        INTERRUPTX &H80, iReg, oReg
        '******************************************************
 
        DEF SEG
END SUB
