'===========================================================================
' Subject: PLAY LARGE .WAV FILES              Date: 06-08-96 (17:33)       
'  Author: Mervyn Baldwin                     Code: QB, QBasic, PDS        
'  Origin: comp.lang.basic.misc             Packet: SOUND.ABC
'===========================================================================
'Some time ago somebody posted here (or alt.basic) a program which 
'played  Ding.WAV in QuickBASIC. Didn't work for bigger files than 
'DING and didn't work at all in QBasic. Here's a fix. Plays the 
'biggest WAV file I have (almost 2MB) in QBasic.

'------------------------------------------------------
DECLARE SUB SetVoice (OnOff%)
  CLS
 VocFile$ = "g:\audio\admz9515.WAV"       ' input-file

'NB a WAV file on CD. Almost 2Mb.

  VocFile% = FREEFILE
  delay% = 11                      ' value for delay
 OPEN VocFile$ FOR BINARY AS #VocFile%
 Bytes& = LOF(VocFile%)          ' number of bytes
 BytesRemaining& = Bytes&        ' number of remaining bytes 
 BufferMax% = 19000             ' largest buffer

'The maximum buffer size in QBasic is about 19K.
' in QB45 this can be at least &H7F00 bytes (over 32K).
 
  Buffer$ = SPACE$(BufferMax%)    ' create buffer
  SetVoice 1                      ' Soundblaster on

  DO
     BytesRemaining& = BytesRemaining& - BufferLen%
     IF BytesRemaining& = 0 THEN EXIT DO ' nothing left over?
     IF BytesRemaining& > BufferMax% THEN ' how many bytes?
        BufferLen% = BufferMax%          '
     ELSE
        BufferLen% = BytesRemaining&     ' remaining (<BufferMax%)..
        'Buffer$ = SPACE$(BufferLen%)     ' ..throw it into SB :-)

'NB line remmed out - works OK without it.
    
     END IF

     GET #VocFile%, , Buffer$            ' read buffer
     DEF SEG = VARSEG(Buffer$)           ' get address of buffer
     Voff& = SADD(Buffer$)               ' .

'NB Voff% changed to Voff& throughout  
    
     FOR t% = 1 TO BufferLen%            ' output od {bufferlen%}
         'FOR qq% = 1 TO Delay: NEXT qq% ' delay
         WAIT &H22C, &H80, &HFF         ' wait for data-ready
         OUT &H22C, &H10
         WAIT &H22C, &H80, &HFF
         OUT &H22C, PEEK(Voff&)
         Voff& = Voff& + 1
     NEXT t%

  LOOP WHILE INKEY$ = ""

  SetVoice 0                            ' SB off
  CLOSE #VocFile%                       ' close file
  END                                   ' .. good bye :-)

SUB SetVoice (OnOff%)
    IF OnOff% THEN
       WAIT &H22C, &H80, &HFF       ' wait for data-ready on SB
       OUT &H22C, &HD1              ' ON
    ELSE
       WAIT &H22C, &H80, &HFF
       OUT &H22C, &HD3              ' OFF
    END IF
END SUB
'--------------------------------------------------------
'Apologies to whoever devised this that I have forgotton his name.
'If anybody can figure out how to avoid the occasional hiccup as, I 
'guess, the buffer is filled, I would be pleased to hear of it.

'-- 
'Best wishes,
'Mervyn Baldwin.
'vyn@abaldwin.demon.co.uk

