bio.h File Reference

Cross platform binary IO to process files in sphinx3 format. More...

`#include <stdio.h>`
`#include <stdarg.h>`
`#include <sphinxbase/sphinxbase_export.h>`
`#include <sphinxbase/prim_type.h>`
`#include <sphinxbase/byteorder.h>`

Go to the source code of this file.

## Macros

#define | BYTE_ORDER_MAGIC (0x11223344)
#define | REVERSE_SENSE_SWAP_INT16(x)
| "reversed senses" SWAP, ARCHAN: This is still incorporated in Sphinx 3 because lm3g2dmp used it.
#define | REVERSE_SENSE_SWAP_INT32(x)

## Functions

SPHINXBASE_EXPORT int32 | bio_readhdr (FILE *fp, char ***name, char ***val, int32 *swap)
| Read binary file format header: has the following format.
SPHINXBASE_EXPORT int32 | bio_writehdr_version (FILE *fp, char *version)
| Write a simple binary file header, containing only the version string.
SPHINXBASE_EXPORT int32 | bio_writehdr (FILE *fp,...)
| Write a simple binary file header with only byte order magic word.
SPHINXBASE_EXPORT void | bio_hdrarg_free (char **name, char **val)
| Free name and value strings previously allocated and returned by bio_readhdr.
SPHINXBASE_EXPORT int32 | bio_fread (void *buf, int32 el_sz, int32 n_el, FILE *fp, int32 swap, uint32 *chksum)
| Like fread but perform byteswapping and accumulate checksum (the 2 extra arguments).
SPHINXBASE_EXPORT int32 | bio_fwrite (const void *buf, int32 el_sz, int32 n_el, FILE *fp, int32 swap, uint32 *chksum)
| Like fwrite but perform byteswapping and accumulate checksum (the 2 extra arguments).
SPHINXBASE_EXPORT int32 | bio_fread_1d (void **buf, size_t el_sz, uint32 *n_el, FILE *fp, int32 sw, uint32 *ck)
| Read a 1-d array (fashioned after fread):
SPHINXBASE_EXPORT int32 | bio_fread_2d (void ***arr, size_t e_sz, uint32 *d1, uint32 *d2, FILE *fp, uint32 swap, uint32 *chksum)
| Read a 2-d matrix:
SPHINXBASE_EXPORT int32 | bio_fread_3d (void ****arr, size_t e_sz, uint32 *d1, uint32 *d2, uint32 *d3, FILE *fp, uint32 swap, uint32 *chksum)
| Read a 3-d array (set of matrices).
SPHINXBASE_EXPORT void | bio_verify_chksum (FILE *fp, int32 byteswap, uint32 chksum)
| Read and verify checksum at the end of binary file.
SPHINXBASE_EXPORT int | bio_fwrite_1d (void *arr, size_t e_sz, uint32 d1, FILE *fp, uint32 *chksum)
| Write a 1-d array.
SPHINXBASE_EXPORT int | bio_fwrite_3d (void ***arr, size_t e_sz, uint32 d1, uint32 d2, uint32 d3, FILE *fp, uint32 *chksum)
| Write a 3-d array (set of matrices).
SPHINXBASE_EXPORT int16 * | bio_read_wavfile (char const *directory, char const *filename, char const *extension, int32 header, int32 endian, size_t *nsamps)
| Read raw data from the wav file.

## Detailed Description

Cross platform binary IO to process files in sphinx3 format.

Definition in file bio.h.

## Macro Definition Documentation

## BYTE_ORDER_MAGIC

#define BYTE_ORDER_MAGIC (0x11223344)

Definition at line 92 of file bio.h.

## REVERSE_SENSE_SWAP_INT16

#define REVERSE_SENSE_SWAP_INT16 | ( |  | x | ) |

"reversed senses" SWAP, ARCHAN: This is still incorporated in Sphinx 3 because lm3g2dmp used it.

Don't think that I am very happy with it.

Definition at line 103 of file bio.h.

## REVERSE_SENSE_SWAP_INT32

#define REVERSE_SENSE_SWAP_INT32 | ( |  | x | ) |

Definition at line 104 of file bio.h.

## Function Documentation

## bio_fread()

SPHINXBASE_EXPORT int32 bio_fread | ( | void * | buf,
|  | int32 | el_sz,
|  | int32 | n_el,
|  | FILE * | fp,
|  | int32 | swap,
|  | uint32 * | chksum )

Like fread but perform byteswapping and accumulate checksum (the 2 extra arguments).

Returns
  unlike fread, returns -1 if required number of elements (n_el) not read; also, no byteswapping or checksum accumulation is performed in that case.

Parameters

  buf | In: buffer to write
  el_sz | In: element size
  n_el | In: number of elements
  fp | In: An input file pointer
  swap | In: Byteswap iff (swap != 0)
  chksum | In/Out: Accumulated checksum

Definition at line 326 of file bio.c.

References bio_fread().

Referenced by bio_fread(), bio_fread_1d(), bio_fread_2d(), bio_fread_3d(), and logmath_read().

## bio_fread_1d()

SPHINXBASE_EXPORT int32 bio_fread_1d | ( | void ** | buf,
|  | size_t | el_sz,
|  | uint32 * | n_el,
|  | FILE * | fp,
|  | int32 | sw,
|  | uint32 * | ck )

Read a 1-d array (fashioned after fread):

- 4-byte array size (returned in n_el)
- memory allocated for the array and read (returned in buf)

Byteswapping and checksum accumulation performed as necessary. Fails fatally if expected data not read.

Returns
  number of array elements allocated and read; -1 if error.

Parameters

  buf | Out: contains array data; allocated by this function; can be freed using ckd_free
  el_sz | In: Array element size
  n_el | Out: Number of array elements allocated/read
  fp | In: File to read
  sw | In: Byteswap iff (swap != 0)
  ck | In/Out: Accumulated checksum

Definition at line 364 of file bio.c.

References bio_fread(), bio_fread_1d(), ckd_calloc, and E_FATAL.

Referenced by bio_fread_1d(), bio_fread_2d(), and bio_fread_3d().

## bio_fread_2d()

SPHINXBASE_EXPORT int32 bio_fread_2d | ( | void *** | arr,
|  | size_t | e_sz,
|  | uint32 * | d1,
|  | uint32 * | d2,
|  | FILE * | fp,
|  | uint32 | swap,
|  | uint32 * | chksum )

Read a 2-d matrix:

- 4-byte # rows, # columns (returned in d1, d2, d3)
- memory allocated for the array and read (returned in buf)

Byteswapping and checksum accumulation performed as necessary. Fails fatally if expected data not read.

Returns
  number of array elements allocated and read; -1 if error.

Definition at line 384 of file bio.c.

References bio_fread(), bio_fread_1d(), bio_fread_2d(), ckd_alloc_2d_ptr, and E_ERROR_SYSTEM.

Referenced by bio_fread_2d().

## bio_fread_3d()

SPHINXBASE_EXPORT int32 bio_fread_3d | ( | void **** | arr,
|  | size_t | e_sz,
|  | uint32 * | d1,
|  | uint32 * | d2,
|  | uint32 * | d3,
|  | FILE * | fp,
|  | uint32 | swap,
|  | uint32 * | chksum )

Read a 3-d array (set of matrices).

- 4-byte # matrices, # rows, # columns (returned in d1, d2, d3)
- memory allocated for the array and read (returned in buf)

Byteswapping and checksum accumulation performed as necessary. Fails fatally if expected data not read.

Returns
  number of array elements allocated and read; -1 if error.

Definition at line 430 of file bio.c.

References bio_fread(), bio_fread_1d(), bio_fread_3d(), ckd_alloc_3d_ptr, and E_ERROR_SYSTEM.

Referenced by bio_fread_3d(), and feat_read_lda().

## bio_fwrite()

SPHINXBASE_EXPORT int32 bio_fwrite | ( | const void * | buf,
|  | int32 | el_sz,
|  | int32 | n_el,
|  | FILE * | fp,
|  | int32 | swap,
|  | uint32 * | chksum )

Like fwrite but perform byteswapping and accumulate checksum (the 2 extra arguments).

Returns
  the number of elemens written (like fwrite).

Parameters

  buf | In: buffer to write
  el_sz | In: element size
  n_el | In: number of elements
  fp | In: An input file pointer
  swap | In: Byteswap iff (swap != 0)
  chksum | In/Out: Accumulated checksum

Definition at line 342 of file bio.c.

References bio_fwrite(), ckd_calloc, and ckd_free().

Referenced by bio_fwrite(), bio_fwrite_1d(), bio_fwrite_3d(), and logmath_write().

## bio_fwrite_1d()

SPHINXBASE_EXPORT int bio_fwrite_1d | ( | void * | arr,
|  | size_t | e_sz,
|  | uint32 | d1,
|  | FILE * | fp,
|  | uint32 * | chksum )

Write a 1-d array.

Checksum accumulation performed as necessary.

Returns
  number of array elements successfully written or -1 if error.

Parameters

  arr | In: Data to write
  e_sz | In: Size of the elements in bytes
  d1 | In: First dimension
  fp | In: File to write to
  chksum | In/Out: Checksum accumulator

Definition at line 558 of file bio.c.

References bio_fwrite(), bio_fwrite_1d(), and E_ERROR_SYSTEM.

Referenced by bio_fwrite_1d(), and bio_fwrite_3d().

## bio_fwrite_3d()

SPHINXBASE_EXPORT int bio_fwrite_3d | ( | void *** | arr,
|  | size_t | e_sz,
|  | uint32 | d1,
|  | uint32 | d2,
|  | uint32 | d3,
|  | FILE * | fp,
|  | uint32 * | chksum )

Write a 3-d array (set of matrices).

Checksum accumulation performed as necessary.

Returns
  number of array elements successfully written or -1 if error.

Parameters

  arr | In: Data to write
  e_sz | In: Size of the elements in bytes
  d1 | In: First dimension
  d2 | In: Second dimension
  d3 | In: Third dimension
  fp | In: File to write to
  chksum | In/Out: Checksum accumulator

Definition at line 507 of file bio.c.

References bio_fwrite(), bio_fwrite_1d(), bio_fwrite_3d(), and E_ERROR_SYSTEM.

Referenced by bio_fwrite_3d().

## bio_hdrarg_free()

SPHINXBASE_EXPORT void bio_hdrarg_free | ( | char ** | name,
|  | char ** | val )

Free name and value strings previously allocated and returned by bio_readhdr.

Parameters

  name | In: Array previously returned by bio_readhdr
  val | In: Array previously returned by bio_readhdr

Definition at line 121 of file bio.c.

References bio_hdrarg_free(), and ckd_free().

Referenced by bio_hdrarg_free(), bio_readhdr(), feat_read_lda(), and logmath_read().

## bio_read_wavfile()

SPHINXBASE_EXPORT int16 * bio_read_wavfile | ( | char const * | directory,
|  | char const * | filename,
|  | char const * | extension,
|  | int32 | header,
|  | int32 | endian,
|  | size_t * | nsamps )

Read raw data from the wav file.

Returns
  pointer to the data.

Parameters

  directory | In: the folder where the file is located
  filename | In: the name of the file
  extension | In: file extension
  header | In: the size of the header to skip usually 44 bytes
  endian | In: endian of the data
  nsamps | Out: number of samples read

Definition at line 592 of file bio.c.

References bio_read_wavfile(), ckd_calloc, ckd_free(), E_ERROR_SYSTEM, and E_FATAL_SYSTEM.

Referenced by bio_read_wavfile().

## bio_readhdr()

SPHINXBASE_EXPORT int32 bio_readhdr | ( | FILE * | fp,
|  | char *** | name,
|  | char *** | val,
|  | int32 * | swap )

Read binary file format header: has the following format.

        s3
        <argument-name> <argument-value>
        <argument-name> <argument-value>
        ...
        endhdr
        4-byte byte-order word used to find file byte ordering relative to host machine.

Lines beginning with # are ignored. Memory for name and val allocated by this function; use bio_hdrarg_free to free them.

Returns
  0 if successful, -1 otherwise.

Parameters

  fp | In: File to read
  name | Out: array of argument name strings read
  val | Out: corresponding value strings read
  swap | Out: file needs byteswapping iff (*swap)

Definition at line 187 of file bio.c.

References bio_hdrarg_free(), bio_readhdr(), ckd_calloc, ckd_salloc, and E_ERROR.

Referenced by bio_readhdr(), feat_read_lda(), and logmath_read().

## bio_verify_chksum()

SPHINXBASE_EXPORT void bio_verify_chksum | ( | FILE * | fp,
|  | int32 | byteswap,
|  | uint32 | chksum )

Read and verify checksum at the end of binary file.

Fails fatally if there is a mismatch.

Parameters

  fp | In: File to read
  byteswap | In: Byteswap iff (swap != 0)
  chksum | In: Value to compare with checksum in file

Definition at line 492 of file bio.c.

References bio_verify_chksum(), and E_FATAL.

Referenced by bio_verify_chksum(), and logmath_read().

## bio_writehdr()

SPHINXBASE_EXPORT int32 bio_writehdr | ( | FILE * | fp,
|  |  | ... )

Write a simple binary file header with only byte order magic word.

Returns
  0 if successful, -1 otherwise.

Definition at line 155 of file bio.c.

References bio_writehdr(), and E_ERROR.

Referenced by bio_writehdr().

## bio_writehdr_version()

SPHINXBASE_EXPORT int32 bio_writehdr_version | ( | FILE * | fp,
|  | char * | version )

Write a simple binary file header, containing only the version string.

Also write the byte order magic word.

Returns
  0 if successful, -1 otherwise.

Parameters

  fp | Output: File to write
  version | Input: A string of version

Definition at line 137 of file bio.c.

References bio_writehdr_version().

Referenced by bio_writehdr_version().