(FILECREATED "19-Jul-84 17:40:28" {ERIS}<SPEECH>ROACH>AUDIO.FPKG;1 6017   

      changes to:  (RECORDS AUDIOBLK)
		   (FNS AUDIO.INIT AUDIO.RESET AUDIO.PLAY AUDIO.PAGE AUDIO.ZEROPAGE AUDIO.RECORD 
			AUDIO.RECORDFN \AUDIO))


(* Copyright (c)  by NIL. All rights reserved.)

(PRETTYCOMPRINT AUDIOCOMS)

(RPAQQ AUDIOCOMS ((PROPS (.AUDIO. DOPVAL))
		  (INITVARS (AUDIO.BUFFER NIL)
			    (AUDIO.BUFFER.SIZE 8)
			    (AUDIO.CLOCK NIL))
		  (RECORDS AUDIOBLK)
		  (FNS AUDIO.INIT AUDIO.RESET AUDIO.PLAY AUDIO.PAGE AUDIO.ZEROPAGE AUDIO.RECORD 
		       AUDIO.RECORDFN \AUDIO)
		  (P (COND ((NOT (CCODEP (GETD '\AUDIO)))
			    (COMPILE '\AUDIO)))
		     (COND ((NULL AUDIO.BUFFER)
			    (AUDIO.INIT))))))

(PUTPROPS .AUDIO. DOPVAL (1 AUDIO))

(RPAQ? AUDIO.BUFFER NIL)

(RPAQ? AUDIO.BUFFER.SIZE 8)

(RPAQ? AUDIO.CLOCK NIL)
[DECLARE: EVAL@COMPILE 

(BLOCKRECORD AUDIOBLK ((OUT0 WORD)
		       (OUTN WORD)
		       (MCPAGE BYTE)
		       (MCWORD BYTE)
		       (INDELTA WORD)))
]
(DEFINEQ

(AUDIO.INIT
  (LAMBDA NIL                                                (* kbr: "19-Jul-84 17:39")
    (PROG NIL
          (SETQ AUDIO.BUFFER (\VAG2 63 0))
          (FOR I FROM 0 TO AUDIO.BUFFER.SIZE DO (\NEWPAGE (AUDIO.PAGE AUDIO.BUFFER I)))
          (\LOCKPAGES AUDIO.BUFFER AUDIO.BUFFER.SIZE)
          (AUDIO.RESET))))

(AUDIO.RESET
  (LAMBDA NIL                                                (* kbr: "19-Jul-84 17:39")
    (PROG NIL
          (FOR I FROM 0 TO (SUB1 AUDIO.BUFFER.SIZE) DO (AUDIO.ZEROPAGE AUDIO.BUFFER I))
          (replace (AUDIOBLK OUT0) of AUDIO.BUFFER with 256)
          (replace (AUDIOBLK OUTN) of AUDIO.BUFFER with 2303)
          (replace (AUDIOBLK MCPAGE) of AUDIO.BUFFER with 0)
          (replace (AUDIOBLK MCWORD) of AUDIO.BUFFER with 0)
          (replace (AUDIOBLK INDELTA) of AUDIO.BUFFER with 0))))

(AUDIO.PLAY
  (LAMBDA (FILE)                                             (* kbr: "19-Jul-84 17:39")
                                                             (* Play FILE. For good results%, FILE should be on 
							     {CORE}. *)
    (PROG (STREAM PAGESIZE N LPAGE LEND)
          (SETQ STREAM (OPENSTREAM FILE 'INPUT))
          (AUDIO.RESET)
          (SETQ PAGESIZE (IQUOTIENT (IPLUS (GETEOFPTR STREAM)
					   511)
				    512))
          (SETQ N (IMAX AUDIO.BUFFER.SIZE PAGESIZE))
          (FOR I FROM 0 TO (SUB1 N) DO (\READPAGES STREAM I (AUDIO.PAGE AUDIO.BUFFER I)))
          (SETQ LPAGE 0)
          (SETQ LEND (IMOD PAGESIZE AUDIO.BUFFER.SIZE))
          (RESETFORM (\AUDIO AUDIO.BUFFER)
		     (FOR I FROM (ADD1 N) TO PAGESIZE
			DO (WHILE (EQ (fetch (AUDIOBLK MCPAGE) of AUDIO.BUFFER)
				      LPAGE)
			      DO                             (* Wait for Microcode to finish LPAGE *))
			   (\READPAGES STREAM (SUB1 I)
				       (AUDIO.PAGE AUDIO.BUFFER LPAGE))
			   (SETQ LPAGE (IMOD (ADD1 LPAGE)
					     AUDIO.BUFFER.SIZE)))
		     (UNTIL (EQ (fetch (AUDIOBLK MCPAGE) of AUDIO.BUFFER)
				LEND)
			DO                                   (* Wait for Microcode to finish remaining pages *)
			    ))
          (CLOSEF STREAM))))

(AUDIO.PAGE
  (LAMBDA (BASE N)                                           (* kbr: "19-Jul-84 17:39")
                                                             (* Return Nth page of BASE *)
    (\ADDBASE BASE (LLSH N 8))))

(AUDIO.ZEROPAGE
  (LAMBDA (BASE N)                                           (* kbr: "19-Jul-84 17:39")
                                                             (* Zero out Nth page of BASE *)
    (PROG NIL
          (SETQ BASE (AUDIO.PAGE BASE N))
          (\ZEROWORDS BASE (\ADDBASE BASE 256)))))

(AUDIO.RECORD
  (LAMBDA (FILE NSEC)                                        (* kbr: "19-Jul-84 17:39")
    (COND
      ((NULL NSEC)
	(SETQ NSEC 5)))
    (PROG (STREAM COP)
          (SETQ STREAM (OPENSTREAM FILE 'OUTPUT))
          (AUDIO.RESET)
          (SETQ COP 1)
          (OR (EQ NSEC T)
	      (\TRUNCATEFILE STREAM (ITIMES 16 NSEC)
			     0))                             (* Preallocate space)
          (AUDIO.RECORDFN T)                                 (* Wait for user to tell you to start)
          (RESETFORM (\AUDIO AUDIO.BUFFER)
		     (FOR F FROM 0 WHILE (AUDIO.RECORDFN NIL)
			DO (WHILE (IEQP COP (fetch (AUDIOBLK MCPAGE) of AUDIO.BUFFER)))
			   (\WRITEPAGES STREAM F (AUDIO.PAGE AUDIO.BUFFER COP))
			   (AUDIO.ZEROPAGE AUDIO.BUFFER COP)
			   (COND
			     ((IEQP COP AUDIO.BUFFER.SIZE)
			       (SETQ COP 1))
			     (T (SETQ COP (ADD1 COP))))
			FINALLY (SETQ COP (LLSH F 9))))
          (\SETEOFPTR STREAM COP)
          (CLOSEF FILE))))

(AUDIO.RECORDFN
  (LAMBDA (STARTP)
    (DECLARE (GLOBALVARS AUDIO.CLOCK))                       (* kbr: "19-Jul-84 17:39")
    (PROG NIL
          (COND
	    (STARTP (PROMPTPRINT "Press and hold CTRL key to record.")
		    (UNTIL (KEYDOWNP 'CTRL))
		    (PROMPTPRINT "Recording in progress...")
		    (SETQ AUDIO.CLOCK (CLOCK)))
	    ((KEYDOWNP 'CTRL)                                (* Continue to record. *)
	      (RETURN T))
	    (T (PROMPTPRINT "Recording complete. Duration: ")
	       (PRINTOUT PROMPTWINDOW (FQUOTIENT (CLOCKDIFFERENCE AUDIO.CLOCK)
						 1000.0))
	       " seconds." T)))))

(\AUDIO
  (LAMBDA (BUF)                                              (* kbr: "19-Jul-84 17:39")
                                                             (* Any odd pointer turns off the audio ucode)
    (OR (.AUDIO. (OR BUF (\ADDBASE AUDIO.BUFFER 1)))
	(SHOULDNT "No audio microcode"))
    (\ADDBASE AUDIO.BUFFER 1)))
)
(COND ((NOT (CCODEP (GETD '\AUDIO)))
       (COMPILE '\AUDIO)))
(COND ((NULL AUDIO.BUFFER)
       (AUDIO.INIT)))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (1003 5882 (AUDIO.INIT 1013 . 1370) (AUDIO.RESET 1372 . 1972) (AUDIO.PLAY 1974 . 3327) (
AUDIO.PAGE 3329 . 3564) (AUDIO.ZEROPAGE 3566 . 3885) (AUDIO.RECORD 3887 . 4922) (AUDIO.RECORDFN 4924
 . 5545) (\AUDIO 5547 . 5880)))))
STOP