(bind-func AudioBuffer_from_aiff
(lambda (filepath)
(let ((filedata (sys_slurp_file filepath)))
(if (null? filedata)
(begin (println "aiff file error: could not open file")
null)
(let ((common (iffchunk_find_dumb filedata "COMM"))
(sounddata (iffchunk_find_dumb filedata "SSND")))
;; pull the data out of the common chunk
(let ((nchan (i16toi64 (swapi16 (pref (cast (pref-ptr common IFF_CHUNK_HEADER_SIZE) i16*) 0))))
(nframes (i32toi64 (swapi32 (pref (cast (pref-ptr common (+ IFF_CHUNK_HEADER_SIZE 2)) i32*) 0))))
(bitdepth (i16toi64 (swapi16 (pref (cast (pref-ptr common (+ IFF_CHUNK_HEADER_SIZE 6)) i16*) 0))))
(samplerate (begin
(swap_bytes_inplace (pref-ptr common (+ IFF_CHUNK_HEADER_SIZE 8)) 10)
(fp80ptrtod (pref-ptr common (+ IFF_CHUNK_HEADER_SIZE 8)))))
(ab (AudioBuffer nframes nchan samplerate))
(abres (AudioBuffer_set_filepath ab (Str filepath)))
(res (audiofile_copy_samples (pref-ptr sounddata (+ IFF_CHUNK_HEADER_SIZE 8))
(AudioBuffer_data ab)
(* nchan nframes)
bitdepth
WAVE_FORMAT_PCM ;; AIFF is always PCM
#f)))
(free filedata) ;; why does this cause a crash?
(if res ab null)))))))