1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * ====================================================================
38 * bio.h -- Sphinx-3 binary file I/O functions.
40 * **********************************************
41 * CMU ARPA Speech Project
43 * Copyright (c) 1996 Carnegie Mellon University.
44 * ALL RIGHTS RESERVED.
45 * **********************************************
49 * Revision 1.8 2005/06/21 20:40:46 arthchan2003
50 * 1, Fixed doxygen documentation, 2, Add the $ keyword.
52 * Revision 1.5 2005/06/13 04:02:57 archan
53 * Fixed most doxygen-style documentation under libs3decoder.
55 * Revision 1.4 2005/05/10 21:21:52 archan
56 * Three functionalities added but not tested. Code on 1) addition/deletion of LM in mode 4. 2) reading text-based LM 3) Converting txt-based LM to dmp-based LM.
58 * Revision 1.3 2005/03/30 01:22:46 archan
59 * Fixed mistakes in last updates. Add
62 * 28-Apr-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
73 /* Win32/WinCE DLL gunk */
74 #include <sphinxbase/sphinxbase_export.h>
75 #include <sphinxbase/prim_type.h>
76 #include <sphinxbase/byteorder.h>
79 * \brief Cross platform binary IO to process files in sphinx3 format.
92 #define BYTE_ORDER_MAGIC (0x11223344)
94 /** "reversed senses" SWAP, ARCHAN: This is still incorporated in
95 Sphinx 3 because lm3g2dmp used it. Don't think that I am very
99 #define REVERSE_SENSE_SWAP_INT16(x) x = ( (((x)<<8)&0x0000ff00) | (((x)>>8)&0x00ff) )
100 #define REVERSE_SENSE_SWAP_INT32(x) x = ( (((x)<<24)&0xff000000) | (((x)<<8)&0x00ff0000) | \
101 (((x)>>8)&0x0000ff00) | (((x)>>24)&0x000000ff) )
103 #define REVERSE_SENSE_SWAP_INT16(x)
104 #define REVERSE_SENSE_SWAP_INT32(x)
111 * Read binary file format header: has the following format
114 * <argument-name> <argument-value>
115 * <argument-name> <argument-value>
118 * 4-byte byte-order word used to find file byte ordering relative to host machine.
120 * Lines beginning with # are ignored.
121 * Memory for name and val allocated by this function; use bio_hdrarg_free to free them.
122 * @return: 0 if successful, -1 otherwise.
125 int32 bio_readhdr (FILE *fp, /**< In: File to read */
126 char ***name, /**< Out: array of argument name strings read */
127 char ***val, /**< Out: corresponding value strings read */
128 int32 *swap /**< Out: file needs byteswapping iff (*swap) */
131 * Write a simple binary file header, containing only the version string. Also write
132 * the byte order magic word.
133 * @return: 0 if successful, -1 otherwise.
136 int32 bio_writehdr_version (FILE *fp, /**< Output: File to write */
137 char *version /**< Input: A string of version */
142 int32 bio_writehdr(FILE *fp, ...);
145 * Free name and value strings previously allocated and returned by bio_readhdr.
148 void bio_hdrarg_free (char **name, /**< In: Array previously returned by bio_readhdr */
149 char **val /**< In: Array previously returned by bio_readhdr */
153 * Like fread but perform byteswapping and accumulate checksum (the 2 extra arguments).
154 * But unlike fread, returns -1 if required number of elements (n_el) not read; also,
155 * no byteswapping or checksum accumulation is performed in that case.
158 int32 bio_fread (void *buf,
161 FILE *fp, /**< In: An input file pointer */
162 int32 swap, /**< In: Byteswap iff (swap != 0) */
163 uint32 *chksum /**< In/Out: Accumulated checksum */
167 * Like fwrite but perform byteswapping and accumulate checksum (the 2 extra arguments).
168 * @return the number of elemens written (like fwrite).
171 int32 bio_fwrite(void *buf,
174 FILE *fp, /**< In: An input file pointer */
175 int32 swap, /**< In: Byteswap iff (swap != 0) */
176 uint32 *chksum /**< In/Out: Accumulated checksum */
180 * Read a 1-d array (fashioned after fread):
182 * - 4-byte array size (returned in n_el)
183 * - memory allocated for the array and read (returned in buf)
185 * Byteswapping and checksum accumulation performed as necessary.
186 * Fails fatally if expected data not read.
187 * Return value: number of array elements allocated and read; -1 if error.
190 int32 bio_fread_1d (void **buf, /**< Out: contains array data; allocated by this
191 function; can be freed using ckd_free */
192 size_t el_sz, /**< In: Array element size */
193 uint32 *n_el, /**< Out: Number of array elements allocated/read */
194 FILE *fp, /**< In: File to read */
195 int32 sw, /**< In: Byteswap iff (swap != 0) */
196 uint32 *ck /**< In/Out: Accumulated checksum */
202 * - 4-byte # rows, # columns (returned in d1, d2, d3)
203 * - memory allocated for the array and read (returned in buf)
205 * Byteswapping and checksum accumulation performed as necessary.
206 * Fails fatally if expected data not read.
207 * Return value: number of array elements allocated and read; -1 if error.
210 int32 bio_fread_2d(void ***arr,
219 * Read a 3-d array (set of matrices)
221 * - 4-byte # matrices, # rows, # columns (returned in d1, d2, d3)
222 * - memory allocated for the array and read (returned in buf)
224 * Byteswapping and checksum accumulation performed as necessary.
225 * Fails fatally if expected data not read.
226 * Return value: number of array elements allocated and read; -1 if error.
229 int32 bio_fread_3d(void ****arr,
239 * Read and verify checksum at the end of binary file. Fails fatally if there is
243 void bio_verify_chksum (FILE *fp, /**< In: File to read */
244 int32 byteswap, /**< In: Byteswap iff (swap != 0) */
245 uint32 chksum /**< In: Value to compare with checksum in file */
250 * Read raw data from the wav file.
252 * @param directory the folder where the file is located
253 * @param filename the name of the file
254 * @param extension file extension
255 * @param header the size of the header to skip usually 44 bytes.
256 * @param endian Endian of the data
257 * @param nsamps number of samples read
258 * @return pointer to the data
261 bio_read_wavfile(char const *directory,
262 char const *filename,
263 char const *extension,