(defpackage lcwav (:use cl cl-user))
(in-package lcwav)

;;; Headers & define for quick libsndfile example
(ffi:clines "
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include <sndfile.h>

#define length 1000
")

;;; sndfile example variables
(ffi:clines "
SNDFILE *file;
SF_INFO info;
sf_count_t count, items;
short *result[length];
const char *filename;
int err, i;
")

(defun test-read () "
(test-read)
A minimal C program to read shorts from a wav and print them to 
standard out inside of (ffi:c-inline () () nil \"..\")
"
 (ffi:c-inline
  () () nil "

filename = \"beep.wav\";

memset(&info, 0, sizeof(info));

file = sf_open(filename, SFM_READ, &info);
if (file==NULL) {
        printf(\"Failed to open file\\n\");
        exit(1);
}

err = sf_error(file);
if (err != SF_ERR_NO_ERROR) {
        printf(\"File opened with error\\n\");
        exit(1);
}

items = length;
count = sf_read_short(file, result, items);
if (count != items) {
        printf(\"sf_read returned wrong count.\");
        exit(1);
}

if ( 0!=sf_close(file)) {
        printf(\"Failed to close file\");
        exit(1);
}

for (i=0; i<length/4; i++) printf(\"%d\\n\", result[i]);
return;
"))

;;;Call (test-read) and quit.
(test-read)
(si:quit)