binary-io.scm 8.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
;;; read/write binary (sound) files
;;;
;;; names are read|write b|l int|float n, 
;;;   so read-bint32 reads the next 4 bytes from the current input port,
;;;   interpreting them as a big-endian 32-bit integer

(provide 'snd-binary-io.scm)


;;; -------- strings (0-terminated)

12
(define (io-read-string)
13 14 15 16 17 18
  (do ((chars ())
       (c (read-byte) (read-byte)))
      ((or (eof-object? c)
	   (= c 0))
       (reverse (apply string chars)))
    (set! chars (cons (integer->char c) chars))))
19

20
(define (io-write-string str)
21 22
  (format () "~{~A~}" str)
  ;(for-each write-char str)
23 24 25 26 27
  (write-byte 0))


;;; -------- strings (unterminated)

28
(define* (read-chars (len 4))
29 30
  (read-string len))
#|
31 32 33 34
  (do ((str (make-string len))
       (i 0 (+ i 1)))
      ((= i len) str)
    (set! (str i) (read-char))))
35
|#
36 37
	 
(define (write-chars str)
38 39
  (format () "~{~A~}" str))
;  (for-each write-char str))
40 41 42 43 44


;;; -------- 16-bit ints

(define (read-bint16)
45
  (let ((int (+ (ash (read-byte) 8) (read-byte)))) ; this depends on arg evaluation left->right
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    (if (> int 32767)
	(- int 65536)
	int)))

(define (read-lint16)
  (let ((int (+ (read-byte) (ash (read-byte) 8))))
    (if (> int 32767)
	(- int 65536)
	int)))

(define (write-bint16 int)
  (write-byte (logand (ash int -8) #xff))
  (write-byte (logand int #xff)))

(define (write-lint16 int)
  (write-byte (logand int #xff))
  (write-byte (logand (ash int -8) #xff)))


;;; -------- 32-bit ints

(define (read-bint32)
  (let ((int (+ (ash (read-byte) 24) (ash (read-byte) 16) (ash (read-byte) 8) (read-byte))))
    (if (> int 2147483647)
	(- int 4294967296)
      int)))

(define (read-lint32)
  (let ((int (+ (read-byte) (ash (read-byte) 8) (ash (read-byte) 16) (ash (read-byte) 24))))
    (if (> int 2147483647)
	(- int 4294967296)
      int)))

(define (write-bint32 int)
80 81 82 83
  (for-each write-byte (vector (logand (ash int -24) 255) 
			       (logand (ash int -16) 255) 
			       (logand (ash int -8) 255) 
			       (logand int 255))))
84
(define (write-lint32 int)
85 86 87 88
  (for-each write-byte (vector (logand int 255) 
			       (logand (ash int -8) 255) 
			       (logand (ash int -16) 255) 
			       (logand (ash int -24) 255))))
89 90 91 92 93


;;; -------- 64-bit ints

(define (read-bint64)
94 95 96 97
  (do ((int 0)
       (i 56 (- i 8)))
      ((< i 0) int)
    (set! int (logior int (ash (read-byte) i)))))
98 99

(define (read-lint64)
100 101 102 103
  (do ((int 0)
       (i 0 (+ i 8)))
      ((= i 64) int)
    (set! int (logior int (ash (read-byte) i)))))
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    
(define (write-bint64 int)
  (do ((i 56 (- i 8)))
      ((< i 0))
    (write-byte (logand (ash int (- i)) #xff))))

(define (write-lint64 int)
  (do ((i 0 (+ i 8)))
      ((= i 64))
    (write-byte (logand (ash int (- i)) #xff))))


;;; -------- 32-bit floats (IEEE 754, sign + 23(+1) bits significand + 8 bits exponent)

(define (int_to_float32 int)
  (if (zero? int)
      0.0
      (* (if (zero? (ash int -31)) 1.0 -1.0) 
	 (expt 2 (- (logand (ash int -23) #xff) 127)) 
	 (logior #x800000 (logand int #x7fffff)) 
	 (expt 2 -23))))

(define (read-bfloat32)
  (int_to_float32 (read-bint32)))

(define (read-lfloat32)
  (int_to_float32 (read-lint32)))

(define (float64_to_int32 flt)
133 134 135 136 137 138 139 140 141 142 143
  (let ((data (integer-decode-float flt)))
    (let ((signif (car data))
	  (expon (cadr data))
	  (sign (caddr data)))
      (if (= expon signif 0)
	  0
	  ;; we're assuming floats are (64-bit) doubles in s7, so this is coercing to a 32-bit float in a sense
	  ;;   this causes some round-off error
	  (logior (if (negative? sign) #x80000000 0)
		  (ash (+ expon 179) 23)   ; 179 = (+ 52 127)
		  (logand (ash signif -29) #x7fffff))))))
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

(define (write-bfloat32 flt)
  (write-bint32 (float64_to_int32 flt)))

(define (write-lfloat32 flt)
  (write-lint32 (float64_to_int32 flt)))



;;; -------- 64-bit floats (IEEE 754, sign + 52(+1) bits significand + 11 bits exponent)

(define (int_to_float64 int)
  (if (zero? int)
      0.0
      (* (if (zero? (ash int -63)) 1.0 -1.0) 
	 (expt 2 (- (logand (ash int -52) #x7ff) 1023)) 
	 (logior #x10000000000000 (logand int #xfffffffffffff))
	 (expt 2 -52))))

(define (read-bfloat64)
  (int_to_float64 (read-bint64)))

(define (read-lfloat64)
  (int_to_float64 (read-lint64)))

(define (float64_to_int64 flt)
170 171 172 173 174 175 176 177 178
  (let ((data (integer-decode-float flt)))
    (let ((signif (car data))
	  (expon (cadr data))
	  (sign (caddr data)))
      (if (= expon signif 0)
	  0
	  (logior (if (negative? sign) #x8000000000000000 0)
		  (ash (+ expon 1075) 52) ; 1075 = (+ 52 1023)
		  (logand signif #xfffffffffffff))))))
179 180 181 182 183 184 185 186 187 188 189 190

(define (write-bfloat64 flt)
  (write-bint64 (float64_to_int64 flt)))

(define (write-lfloat64 flt)
  (write-lint64 (float64_to_int64 flt)))



;;; -------- 80-bit floats (IEEE 754, sign + 63(+1) bits significand + 15 bits exponent, needed for aifc headers)

(define (read-bfloat80->int)
191
  (let ((exponent 0)
192 193
	(sign 0)
	(buf (make-vector 10)))
194 195 196
    (do ((i 0 (+ i 1)))
	((= i 10))
      (set! (buf i) (read-byte)))
197 198 199
    (set! exponent (logior (ash (buf 0) 8) (buf 1)))
    (set! sign (if (not (= (logand exponent #x8000) 0)) 1 0))
    (set! exponent (logand exponent #x7FFF))
200 201
    (let ((mant1 (+ (ash (buf 2) 24) (ash (buf 3) 16) (ash (buf 4) 8) (buf 5)))
	  (mant0 (+ (ash (buf 6) 24) (ash (buf 7) 16) (ash (buf 8) 8) (buf 9))))
202
      (if (= mant1 mant0 exponent sign 0) 
203 204
	  0
	  (round (* (if (= sign 1) -1 1)
205
		    (expt 2.0 (- exponent 16383.0))
206 207
		    (+ (* (expt 2.0 -31.0) mant1)
		       (* (expt 2.0 -63.0) mant0))))))))
208 209

(define (write-int->bfloat80 val)
210
  (let ((exponent 0)    
211 212 213 214 215 216
	(sign 0)
	(mant1 0)
	(mant0 0))
    (if (negative? val) 
	(begin
	  (set! sign 1) 
217
	  (set! val (- val))))
218 219
    (if (not (zero? val))
	(begin
220 221
	  (set! exponent (round (+ (log val 2.0) 16383.0)))
	  (set! val (* val (expt 2 (- 16414 exponent)))) ; 16414 = (+ 16383 31)
222 223 224
	  (set! mant1 (floor val))
	  (set! val (- val mant1))
	  (set! mant0 (floor (* val (expt 2 32))))))
225 226
    (write-byte (logior (ash sign 7) (ash exponent -8)))
    (write-byte (logand exponent #xFF))
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    (do ((i 2 (+ i 1))
	 (j 24 (- j 8)))
	((= i 6))
      (write-byte (logand (ash mant1 (- j)) #xFF)))
    (do ((i 6 (+ i 1))
	 (j 24 (- j 8)))
	((= i 10))
      (write-byte (logand (ash mant0 (- j)) #xFF)))))



;;; -------- "au" (NeXT/Sun) header

(define (read-au-header file)
  (with-input-from-file file
    (lambda ()
243
      (let ((magic (read-chars)))
244
	(if (not (string=? magic ".snd"))
245
	    (error 'bad-header "~A is not an au file" file)
246 247 248 249
	    (let* ((data-location (read-bint32))
		   (data-size (read-bint32))
		   (sample-type (read-bint32))
		   (srate (read-bint32))
250 251
		   (chns (read-bint32)))
	      (list magic data-location data-size sample-type srate chns (io-read-string)))))))) ; io-read-string = comment
252 253 254

(define (write-au-header file chns srate data-size sample-type comment) ; data-size in bytes
  ;; common sample-types: 1 mulaw, 2 linear_8, 3 linear_16, 4 linear_24, 5 linear_32, 6 float, 5 double, 27 alaw
255 256 257
  (with-output-to-file file
    (lambda ()
      (let* ((comlen (length comment))
258
	     (data-location (+ 24 (* 4 (floor (+ 1 (/ comlen 4)))))))
259
	(write-chars ".snd")
260
	(for-each write-bint32 (vector data-location data-size sample-type srate chns))
261 262 263 264 265 266 267 268
	(let ((curloc 24))
	  (if (> comlen 0)
	      (begin
		(io-write-string comment)
		(set! curloc (+ curloc comlen 1)))) ; io-write-string adds a trailing 0
	  (do ((i curloc (+ i 1)))
	      ((>= i data-location))
	    (write-byte 0)))))))
269 270


271 272 273
(define (read-aif-header file)
  (let ((data-location 0)
	(data-size 0)
274
	(sample-type 0)
275
	(srate 0)
276
	(chns 0)
277 278 279
	(current-location 0))
    (with-input-from-file file
      (lambda ()
280
	(let ((magic (read-chars)))
281
	  (if (not (string=? magic "FORM"))
282 283
	      (error 'bad-header "~A is not an aif file: ~S" file magic)
	      (let ((size (read-bint32)) ; why was this commented out? the size is the 4 bytes after "FORM"
284
		    (magic (read-chars)))
285
		(set! current-location 12)
286
		(if (not (member magic '("AIFF" "AIFC") string=?))
287
		    (error 'bad-header "~A is not an aif file: ~S" file magic)
288 289 290 291
		    ;; now look for the "SSND" and "COMM" chunks
		    (call-with-exit
		     (lambda (return)
		       (let loop ()
292
			 (let ((chunk (read-chars))
293
			       (chunk-size (read-bint32)))
294 295 296 297 298
			   (if (odd? chunk-size) (set! chunk-size (+ chunk-size 1)))
			   (if (string=? chunk "SSND")
			       (begin
				 (set! data-location (+ 16 current-location (read-bint32)))
				 (if (> srate 0)
299
				     (return (list magic data-location data-size sample-type srate chns))))
300
			       (if (string=? chunk "COMM")
301 302 303 304
				   (let ((len 0))
				     (set! chns (read-bint16))
				     (set! len (read-bint32))
				     (set! sample-type (read-bint16))
305
				     (set! srate (read-bfloat80->int))
306
				     (set! data-size (* len chns sample-type 1/8))
307
				     (if (> data-location 0)
308
					 (return (list magic data-location data-size sample-type srate chns))))
309 310 311 312 313 314
				   (do ((i 0 (+ i 1))) ; here we really need built-in file IO stuff!
				       ((= i chunk-size))
				     (if (eof-object? (read-byte))
					 (return 'bad-header)))))
			   (set! current-location (+ 8 chunk-size))
			   (loop)))))))))))))