;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; LUSH Lisp Universal Shell
;;;   Copyright (C) 2002 Leon Bottou, Yann Le Cun, AT&T Corp, NECI.
;;; Includes parts of TL3:
;;;   Copyright (C) 1987-1999 Leon Bottou and Neuristique.
;;; Includes selected parts of SN3.2:
;;;   Copyright (C) 1991-2001 AT&T Corp.
;;;
;;; Includes information derived from the GSL (Gnu Scientific Library)
;;;   header files which are copyrighted and distributed under the GPL
;;;   by their respective authors.
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(libload "htk/htk-config")



#? *** htk_train

;; dummy function that adds HTK C header files in C file
(eval `(de dummy_htk_train () ,htk-cpheader-cmd ()))

#? * (dummy_htk_train)
#? * (inittrain)
#? * (createsequence <x> <blksize>)
#? * (storeitem <seq> <item>)
#? * (deleteitem <seq> <i>)
#? * (getitem <seq> <i>)
#? * (sequencemean <ss> <mean>)
#? * (sequencecov <ss> <ck> <cov> <mean>)
#? * (createsegstore <x> <obs> <seglen> <result>)
#? * (loadsegment <ss> <start> <end> <pbuf>)
#? * (numsegs <ss>)
#? * (seglength <ss> <i>)
#? * (getsegobs <ss> <i> <j> <result>)
#? * (flatcluster <x> <vpool> <nc> <dck> <cck> <dcov>)
#? * (treecluster <x> <vpool> <nc> <dck> <cck> <dcov>)
#? * (freeclusterset <cs>)
#? * (showclusterset <cs>)
#? * (attachaccs <hset> <x>)
#? * (zeroaccs <hset>)
#? * (showaccs <hset>)
#? * (resetprecomps <hset>)
#? * (resethmmprecomps <hmm> <nstreams>)
#? * (dumpaccs <hset> <fname> <n>)
#? * (loadaccs <hset> <fname> <result>)

;; ********************************************************


(de InitTrain (  )
    #{ InitTrain(  );  #} 
    ()
)


(de CreateSequence (  x  blksize )
    ((-gptr- "MemHeap *") x) 
    ((-int-) blksize)  
    (gptr   #{ CreateSequence(  $x   ,$blksize  )  #} )          ;; Sequence -> gptr
)


(de StoreItem (  seq  item )
    ((-gptr- "SequenceInfo *") seq) 
    ((-gptr-) item)  
    #{ StoreItem(  $seq   ,$item  );  #} 
    ()
)


(de DeleteItem (  seq  i )
    ((-gptr- "SequenceInfo *") seq) 
    ((-int-) i)  
    #{ DeleteItem(  $seq   ,$i  );  #} 
    ()
)


(de GetItem (  seq  i )
    ((-gptr- "SequenceInfo *") seq) 
    ((-int-) i)  
    (gptr   #{ GetItem(  $seq   ,$i  )  #} )
)


(de SequenceMean (  ss  mean )
    ((-gptr- "SequenceInfo *") ss) 
    ((-gptr- "float *") mean)  
    #{ SequenceMean(  $ss   ,$mean  );  #} 
    ()
)


(de SequenceCov (  ss  ck  cov  mean )
    ((-gptr- "SequenceInfo *") ss) 
    ((-int-) ck)                               ;; CovKind (enum) -> int 
    ((-gptr- "float *") cov)                                   ;; Covariance (union of pointers ) -> gptr
    ((-gptr- "float *") mean)  
    #{ SequenceCov(  $ss   ,$ck   ,(Covariance)$cov   ,$mean  );  #} 
    ()
)


(de CreateSegStore (  x  obs  seglen result)
    ((-gptr- "MemHeap *") x) 
    ((-gptr- "Observation *") obs) 
    ((-int-) seglen)  
    ((-gptr- "SegStore *") result)       ;; return struct -> pointer 
    #{ {
    SegStore tmp;
    tmp = CreateSegStore(  $x   ,*$obs   ,$seglen  );
    memcpy($result, &tmp, sizeof(SegStore));
    } #}
    ()
)


(de LoadSegment (  ss  start  end  pbuf )
    ((-gptr- "SegStoreRec *") ss) 
    ((-double-) start) 
    ((-double-) end) 
    ((-gptr- "struct _ParmBuf *") pbuf)  
    #{ LoadSegment(  $ss   ,$start   ,$end   ,$pbuf  );  #} 
    ()
)


(de NumSegs (  ss )
    ((-gptr- "SegStoreRec *") ss)  
    (int   #{ NumSegs(  $ss  )  #} )
)


(de SegLength (  ss  i )
    ((-gptr- "SegStoreRec *") ss) 
    ((-int-) i)  
    (int   #{ SegLength(  $ss   ,$i  )  #} )
)


(de GetSegObs (  ss  i  j  result)
    ((-gptr- "SegStoreRec *") ss) 
    ((-int-) i) 
    ((-int-) j)  
    ((-gptr- "Observation *") result)
    #{ {
    Observation tmp;
    tmp = GetSegObs(  $ss   ,$i   ,$j  );
    memcpy($result, &tmp, sizeof(Observation));
    } #}
    ()
)


(de FlatCluster (  x  vpool  nc  dck  cck  dcov )
    ((-gptr- "MemHeap *") x) 
    ((-gptr- "SequenceInfo *") vpool) 
    ((-int-) nc) 
    ((-int-) dck)                               ;; CovKind (enum) -> int 
    ((-int-) cck)                               ;; CovKind (enum) -> int 
    ((-gptr- "float *") dcov)                    ;; Covariance (union of pointers ) -> gptr
    (gptr #{ FlatCluster(  $x   ,$vpool   ,$nc   ,$dck   ,$cck   ,(Covariance)$dcov  )  #} )            ;; ClusterSet *   -> gptr
)


(de TreeCluster (  x  vpool  nc  dck  cck  dcov )
    ((-gptr- "MemHeap *") x) 
    ((-gptr- "SequenceInfo *") vpool) 
    ((-int-) nc) 
    ((-int-) dck)                               ;; CovKind (enum) -> int 
    ((-int-) cck)                               ;; CovKind (enum) -> int 
    ((-gptr- "float *") dcov)                    ;; Covariance ( union of pointers) -> ptr
    (gptr #{ TreeCluster(  $x   ,$vpool   ,$nc   ,$dck   ,$cck   , (Covariance)$dcov  )  #} )            ;; ClusterSet *   -> gptr
)


(de FreeClusterSet (  cs )
    ((-gptr- "ClusterSet *") cs)  
    #{ FreeClusterSet(  $cs  );  #} 
    ()
)


(de ShowClusterSet (  cs )
    ((-gptr- "ClusterSet *") cs)  
    #{ ShowClusterSet(  $cs  );  #} 
    ()
)


(de AttachAccs (  hset  x )
    ((-gptr- "HMMSet *") hset) 
    ((-gptr- "MemHeap *") x)  
    #{ AttachAccs(  $hset   ,$x  );  #} 
    ()
)


(de ZeroAccs (  hset )
    ((-gptr- "HMMSet *") hset)  
    #{ ZeroAccs(  $hset  );  #} 
    ()
)


(de ShowAccs (  hset )
    ((-gptr- "HMMSet *") hset)  
    #{ ShowAccs(  $hset  );  #} 
    ()
)


(de ResetPreComps (  hset )
    ((-gptr- "HMMSet *") hset)  
    #{ ResetPreComps(  $hset  );  #} 
    ()
)


(de ResetHMMPreComps (  hmm  nstreams )
    ((-gptr- "HMMDef *") hmm) 
    ((-int-) nstreams)  
    #{ ResetHMMPreComps(  $hmm   ,$nstreams  );  #} 
    ()
)


(de DumpAccs (  hset  fname  n )
    ((-gptr- "HMMSet *") hset) 
    ((-gptr- "char *") fname) 
    ((-int-) n)  
    (gptr   #{ DumpAccs(  $hset   ,$fname   ,$n  )  #} )          ;; FILE * -> gptr
)


(de LoadAccs (  hset  fname result )
    ((-gptr- "HMMSet *") hset) 
    ((-gptr- "char *") fname)  
    ((-gptr- "Source *") result)                       ;; return struct -> pointer 
    #{ {
    Source tmp;
    tmp = LoadAccs(  $hset   ,$fname  );
    memcpy($result, &tmp, sizeof(Source));
    } #}
    ()
)

;; ********************************************************

(dhc-make-with-libs () (list htk-libhtk)
  dummy_htk_train
  inittrain
  createsequence
  storeitem
  deleteitem
  getitem
  sequencemean
  sequencecov
  createsegstore
  loadsegment
  numsegs
  seglength
  getsegobs
  flatcluster
  treecluster
  freeclusterset
  showclusterset
  attachaccs
  zeroaccs
  showaccs
  resetprecomps
  resethmmprecomps
  dumpaccs
  loadaccs
)
