make-sample-format.rkt
#lang racket/base

(require (prefix-in sample-format- (only-in "sample-format-struct.rkt" names))
         (planet synx/maker:2) 
         (prefix-in t: (planet bzlib/template:1:0))
         racket/runtime-path
         racket/string
         compiler/xform
         racket/struct-info)

(define-runtime-path location ".")

(define field-types
  (make-immutable-hash
   '((bits . int)
     (rate . int)
     (channels . int)
     (byte_format . int)
     (matrix . string))))

(define arguments
  (string-join   
   (for/list ((name sample-format-names)
              (i (in-naturals)))
     (let ((type (hash-ref field-types name))
            (name (symbol->string name))
            (i (number->string i)))
      (string-append
       "/**************************** " name "************/\n\t"
       "thing = scheme_struct_ref(sample_format,"i");\n\t"
       "if(!SCHEME_" 
       (cond
         ((eq? type 'int) "INTP")
         ((eq? type 'string) "BYTE_STRINGP")
         (else (error type)))
       "(thing))\n\t"
       "\tscheme_wrong_type(\"" name "\",\""(symbol->string type)"\", "i", argc, argv);\n\t"
       "self->"name" = SCHEME_"
       (cond
         ((eq? type 'int) "INT_VAL")
         ((eq? type 'string) "BYTE_STR_VAL")
         (else (error type)))
       "(thing);")))
   "\n\n\t"))

(define step-1 (build-path location "compiled" "sample-format.c"))
     
(depending-on step-1
              (list (build-path location "sample-format.c.in") (build-path location "make-sample-format.rkt"))
              (λ (out ins)
                (with-output-to-file
                    out
                  #:exists 'replace
                  (λ ()
                    (with-input-from-file
                        (car ins)
                      (λ ()
                        (write-string
                         (t:iqs (current-input-port)
                                `(arguments . ,arguments)))))))))
  
(define step-2 (build-path location "compiled" "sample-format.3m.c"))

(depending-on step-2
              (list step-1)
              (λ (out ins)
                (xform #f (path->string (car ins)) (path->string out) null)))

(library location "sample-format" step-2)