;; helper function for adding sample data to sampler
;; this assumes stereo files at the moment!!
;; passing 0 for length means read to end of file.
;;
;; for example:
;; ;; this reads the whole file into index 60
;; (set-sampler-data sampler "/tmp/piano-C.aif" 60 0 0)
;; ;; this reads 5000 frames starting 1000 frames into the file
;; (set-sampler-data sampler "/tmp/piano-C.aif" 60 1000 5000)
(bind-func set_sample_data_
(lambda (inst:[SAMPLE,SAMPLE,i64,i64,SAMPLE*]* filename index:i64 offset lgth bank:i32 print_p:i64)
(let ((info:SF_INFO* (salloc))
(audiofile (sf_open filename SFM_READ info))
(channels:i64 (convert (tref info 2)))
(num (if (= 0 lgth)
(- (tref info 0) offset)
lgth)))
(if (not (null? audiofile))
(let ((adat:SAMPLE* (halloc (* num channels)))
(samples:|20,|128,SAMPLE*||* (inst.samples))
(samples_channels:|20,|128,i64||* (inst.samples_channels))
(samples_length:|20,|128,i64||* (inst.samples_length))
(samples_read (sf_read_file_into_buffer filename adat (* offset channels) num (i64toi1 print_p)))
(olddat (if (<> 0 (aref (aref-ptr samples_length bank) index))
(bitcast (aref (aref-ptr samples bank) index) i8*)
null)))
;;(printf "%lld samples read into: %lld\n" samples_read index)
(aset! (aref-ptr samples_length bank) index (/ samples_read channels)) ;num)
(aset! (aref-ptr samples_channels bank) index (convert channels))
(aset! (aref-ptr samples bank) index adat)
(sf_close audiofile)
(if (not (null? olddat)) (free olddat))
1)
(begin
(printf "libsndfile error: could not read audio file at '%s' (%s)\n" filename (sf_strerror audiofile))
(sf_close audiofile)
0)))))