Pausing and resuming MIDI playback

by Richard Russell, May 2006

BBC BASIC for Windows allows you to play a MIDI file using the *PLAY command. Whilst you can terminate playback before the song has finished by using the SOUND OFF statement, there is no built-in mechanism for pausing playback.

The program below makes it possible to pause and resume playback. For the purposes of the example a key-press is used, but the code can be adapted to control playback from any other source. For example in a game you may wish to pause the music whilst a high-score table is displayed.

        *PLAY C:\Windows\Media\Flourish.mid
 
        MCI_PLAY   = &806
        MCI_PAUSE  = &809
 
        PRINT "Press a key to pause and unpause..."
        pause% = FALSE
        REPEAT
          IF GET THEN
            pause% = NOT pause%
            IF pause% THEN
              PROCmcicommand(@midi%, MCI_PAUSE, 0, 0)
              PRINT "Paused"
            ELSE
              PROCmcicommand(@midi%, MCI_PLAY, 0, 0)
              PRINT "Resumed"
            ENDIF
          ENDIF
        UNTIL FALSE
        END
 
        DEF PROCmcicommand(D%, C%, F%, S%)
        LOCAL K%, M%, O%, P%, T%
        DIM P% LOCAL 50
        [OPT 2
        .K% push S%:push F%:push C%:push D%:call "mciSendCommand"
        ret 16
        .M% cmp dword [esp+8],&500 : jz K%
        jmp [^O%]
        ]
        SWAP T%,@midi%
        SYS "GetWindowLong", @hwnd%, -4 TO O%
        SYS "SetWindowLong", @hwnd%, -4, M%
        SYS "SendMessage", @hwnd%, &500, 0, 0
        SYS "SetWindowLong", @hwnd%, -4, O%
        SYS "SendMessage", @hwnd%, 0, 0
        SWAP T%,@midi%
        ENDPROC

You may need to change the MIDI file specification if your PC doesn't have Flourish.mid in that location.