;; -*- Mode: LISP; Syntax: Common-Lisp; Package: (USER); -*-
;
; Multi-Modal Recorder
;
; Start: 4/17 1995
;

(defun init-record ()
  "Turns off display, sets up the right file, etc."
  (setf *display-on* nil)
  (setf *freq* POLLED)
  (open-body-socket t)
  (setf out-file (open "out" :direction :output
                       :if-exists :overwrite
                       :element-type '(unsigned-byte 8)
                       :if-does-not-exist :create))
  )

(defun record ()
  (if (init-record)
      (print "READY."))
  (setf num-of-bytes 
        (let ((item 0)(result 0))
	  (mapc #'(lambda (item) 
		    (setf result (+ result item))) num-vals-list) (* 2 result)))
  (setf run nil)
  (if (y-or-n-p "Record Now?")
      (progn
        (print "To stop recording, press any key.")     
        (do ((foo 1)) (run t) ;main record loop
	  (send-item 1 (body-stream *socket-obj1*))
          (if (listen (body-stream *socket-obj1*))     
              (dotimes (count (+ TIME-bytes num-of-bytes) foo) 
                (write-byte (read-byte (body-stream *socket-obj1*)) out-file)))    
#| Fix intonation later....
            (loop ;Intonation loop
             (unless (listen (inton-stream *socket-obj1*))
               (return))
             (let ((in (read-line (inton-stream *socket-obj1*)) 'list)))
             (if in (push (list (first in) (second in))
		          (inton-data *socket-obj1*))))) 
|#
       ;To stop recording, press any key.
          (if (listen *standard-input*) 
	      (setf run t))
          )))
  (if (close out-file) 
      (print "OK. Stored in file 'out'."))
  (close-body-stream)
  )

(defvar gest-space1 "/ahi/ahi2/users/kris/outfiles/gest-space1")
(defvar gaze-hands1 "/ahi/ahi2/users/kris/outfiles/gaze-hands1")
(defvar gaze-agent1 "/ahi/ahi2/users/kris/outfiles/gaze-agent1")
(defvar body-agent1 "/ahi/ahi2/users/kris/outfiles/body-agent1")

(defun read-back (filename)
  "Opens a stream to a file and reads from it."
  (setf in (open filename :direction :input 
                :element-type '(unsigned-byte 8)))
  (setf (body-stream *socket-obj1*) in)
  (setf *freq* 2)  ;Makes refresh-body-data do the right thing.
  (setf run nil)
  (print 'ready)(read-line)
  (do ((foo 1))(run foo)
    (refresh-body-data)
;    (print (aref *body* 0))
;    (print (aref *body* 1))
    (if (listen *standard-input*) 
	(setf run t))
;    (sleep 2)
;(print (aref *body* 0))
;        (print (aref *body* 1))
;	(print in)(int-to-bin in)
        )
  (close in)
  )



