bin_mdef.h
Go to the documentation of this file.
/* -*- c-file-style: "linux" -*- */
/* ====================================================================
* Copyright (c) 2005 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* This work was supported in part by funding from the Defense Advanced
* Research Projects Agency and the National Science Foundation of the
* United States of America, and the CMU Sphinx Speech Consortium.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
*/
#ifndef __BIN_MDEF_H__
#define __BIN_MDEF_H__
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* SphinxBase headers. */
#include <sphinxbase/mmio.h>
#include <sphinxbase/cmd_ln.h>
#include <pocketsphinx_export.h>
#include "mdef.h"
#define BIN_MDEF_FORMAT_VERSION 1
/* Little-endian machines will write "BMDF" to disk, big-endian ones "FDMB". */
#define BIN_MDEF_NATIVE_ENDIAN 0x46444d42 /* 'BMDF' in little-endian order */
#define BIN_MDEF_OTHER_ENDIAN 0x424d4446 /* 'BMDF' in big-endian order */
#ifdef __GNUC__
#define __ATTRIBUTE_PACKED __attribute__((packed))
#else
#define __ATTRIBUTE_PACKED
#endif
typedef struct mdef_entry_s mdef_entry_t;
struct mdef_entry_s {
int32 ssid;
int32 tmat;
/* FIXME: is any of this actually necessary? */
union {
struct {
uint8 filler;
uint8 reserved[3];
} ci;
struct {
uint8 wpos;
uint8 ctx[3];
} cd;
} info;
} __ATTRIBUTE_PACKED;
#define BAD_SSID 0xffff
#define BAD_SENID 0xffff
typedef struct cd_tree_s cd_tree_t;
struct cd_tree_s {
int16 ctx;
int16 n_down;
union {
int32 pid;
int32 down;
} c;
};
typedef struct bin_mdef_s bin_mdef_t;
struct bin_mdef_s {
int refcnt;
int32 n_ciphone;
int32 n_phone;
int32 n_emit_state;
int32 n_ci_sen;
int32 n_sen;
int32 n_tmat;
int32 n_sseq;
int32 n_ctx;
int32 n_cd_tree;
int16 sil;
mmio_file_t *filemap;
char **ciname;
cd_tree_t *cd_tree;
mdef_entry_t *phone;
uint16 **sseq;
uint8 *sseq_len;
/* These two are not stored on disk, but are generated at load time. */
int16 *cd2cisen;
int16 *sen2cimap;
enum { BIN_MDEF_FROM_TEXT, BIN_MDEF_IN_MEMORY, BIN_MDEF_ON_DISK } alloc_mode;
};
#define bin_mdef_is_fillerphone(m,p) (((p) < (m)->n_ciphone) \
? (m)->phone[p].info.ci.filler \
: (m)->phone[(m)->phone[p].info.cd.ctx[0]].info.ci.filler)
#define bin_mdef_is_ciphone(m,p) ((p) < (m)->n_ciphone)
#define bin_mdef_n_ciphone(m) ((m)->n_ciphone)
#define bin_mdef_n_phone(m) ((m)->n_phone)
#define bin_mdef_n_sseq(m) ((m)->n_sseq)
#define bin_mdef_n_emit_state(m) ((m)->n_emit_state)
#define bin_mdef_n_emit_state_phone(m,p) ((m)->n_emit_state ? (m)->n_emit_state \
: (m)->sseq_len[(m)->phone[p].ssid])
#define bin_mdef_n_sen(m) ((m)->n_sen)
#define bin_mdef_n_tmat(m) ((m)->n_tmat)
#define bin_mdef_pid2ssid(m,p) ((m)->phone[p].ssid)
#define bin_mdef_pid2tmatid(m,p) ((m)->phone[p].tmat)
#define bin_mdef_silphone(m) ((m)->sil)
#define bin_mdef_sen2cimap(m,s) ((m)->sen2cimap[s])
#define bin_mdef_sseq2sen(m,ss,pos) ((m)->sseq[ss][pos])
#define bin_mdef_pid2ci(m,p) (((p) < (m)->n_ciphone) ? (p) \
: (m)->phone[p].info.cd.ctx[0])
POCKETSPHINX_EXPORT
bin_mdef_t *bin_mdef_read(cmd_ln_t *config, const char *filename);
POCKETSPHINX_EXPORT
bin_mdef_t *bin_mdef_read_text(cmd_ln_t *config, const char *filename);
POCKETSPHINX_EXPORT
int bin_mdef_write(bin_mdef_t *m, const char *filename);
POCKETSPHINX_EXPORT
int bin_mdef_write_text(bin_mdef_t *m, const char *filename);
bin_mdef_t *bin_mdef_retain(bin_mdef_t *m);
int bin_mdef_free(bin_mdef_t *m);
int bin_mdef_ciphone_id(bin_mdef_t *m,
const char *ciphone);
int bin_mdef_ciphone_id_nocase(bin_mdef_t *m,
const char *ciphone);
/* Return value: READ-ONLY ciphone string name for the given ciphone id */
const char *bin_mdef_ciphone_str(bin_mdef_t *m,
int32 ci);
/* Return value: phone id for the given constituents if found, else -1 */
int bin_mdef_phone_id(bin_mdef_t *m,
int32 b,
int32 l,
int32 r,
int32 pos);
/* Look up a phone id, backing off to other word positions. */
int bin_mdef_phone_id_nearest(bin_mdef_t * m, int32 b,
int32 l, int32 r, int32 pos);
int bin_mdef_phone_str(bin_mdef_t *m,
int pid,
char *buf);
#ifdef __cplusplus
}; /* extern "C" */
#endif /* __cplusplus */
#endif /* __BIN_MDEF_H__ */
bin_mdef_s::n_ciphone
int32 n_ciphone
Number of base (CI) phones.
Definition: bin_mdef.h:119
bin_mdef_s
Definition: bin_mdef.h:117
bin_mdef_ciphone_str
const char * bin_mdef_ciphone_str(bin_mdef_t *m, int32 ci)
In: ciphone id for which name wanted.
Definition: bin_mdef.c:736
mdef.h
Model definition.
bin_mdef_phone_str
int bin_mdef_phone_str(bin_mdef_t *m, int pid, char *buf)
Create a phone string for the given phone (base or triphone) id in the given buf.
Definition: bin_mdef.c:867
bin_mdef_s::sseq
uint16 ** sseq
Unique senone sequences (2D array built at load time)
Definition: bin_mdef.h:134
cd_tree_s::pid
int32 pid
Phone ID (leafnode)
Definition: bin_mdef.h:108
bin_mdef_s::n_ctx
int32 n_ctx
Number of phones of context.
Definition: bin_mdef.h:126
bin_mdef_s::cd_tree
cd_tree_t * cd_tree
Tree mapping CD phones to phone IDs.
Definition: bin_mdef.h:132
bin_mdef_s::sil
int16 sil
CI phone ID for silence.
Definition: bin_mdef.h:128
bin_mdef_s::n_phone
int32 n_phone
Number of base (CI) phones + (CD) triphones.
Definition: bin_mdef.h:120
bin_mdef_read
POCKETSPHINX_EXPORT bin_mdef_t * bin_mdef_read(cmd_ln_t *config, const char *filename)
Read a binary mdef from a file.
Definition: bin_mdef.c:322
bin_mdef_ciphone_id_nocase
int bin_mdef_ciphone_id_nocase(bin_mdef_t *m, const char *ciphone)
Case-insensitive context-independent phone lookup.
Definition: bin_mdef.c:713
bin_mdef_s::sseq_len
uint8 * sseq_len
Number of states in each sseq (NULL for homogeneous)
Definition: bin_mdef.h:135
bin_mdef_read_text
POCKETSPHINX_EXPORT bin_mdef_t * bin_mdef_read_text(cmd_ln_t *config, const char *filename)
Read a text mdef from a file (creating an in-memory binary mdef).
Definition: bin_mdef.c:65
bin_mdef_s::n_sen
int32 n_sen
Number of senones (CI+CD)
Definition: bin_mdef.h:123
mdef_entry_s
Definition: bin_mdef.h:73
bin_mdef_s::cd2cisen
int16 * cd2cisen
Parent CI-senone id for each senone.
Definition: bin_mdef.h:138
bin_mdef_ciphone_id
int bin_mdef_ciphone_id(bin_mdef_t *m, const char *ciphone)
Context-independent phone lookup.
Definition: bin_mdef.c:690
mdef_entry_s::ci
struct mdef_entry_s::@0::@1 ci
< CI phone information - attributes (just "filler" for now)
bin_mdef_s::n_sseq
int32 n_sseq
Number of unique senone sequences.
Definition: bin_mdef.h:125
bin_mdef_phone_id
int bin_mdef_phone_id(bin_mdef_t *m, int32 b, int32 l, int32 r, int32 pos)
In: Word position.
Definition: bin_mdef.c:744
bin_mdef_free
int bin_mdef_free(bin_mdef_t *m)
Release a pointer to a binary mdef.
Definition: bin_mdef.c:271
bin_mdef_s::n_tmat
int32 n_tmat
Number of transition matrices.
Definition: bin_mdef.h:124
bin_mdef_s::n_emit_state
int32 n_emit_state
Number of emitting states per phone (0 for heterogeneous)
Definition: bin_mdef.h:121
cd_tree_s
Definition: bin_mdef.h:104
bin_mdef_s::alloc_mode
enum bin_mdef_s::@4 alloc_mode
Allocation mode for this object.
bin_mdef_s::n_cd_tree
int32 n_cd_tree
Number of nodes in cd_tree (below)
Definition: bin_mdef.h:127
mdef_entry_s::ctx
uint8 ctx[3]
quintphones will require hacking
Definition: bin_mdef.h:86
cd_tree_s::down
int32 down
Next level of the tree (offset from start of cd_trees)
Definition: bin_mdef.h:109
bin_mdef_s::phone
mdef_entry_t * phone
All phone structures.
Definition: bin_mdef.h:133
bin_mdef_s::sen2cimap
int16 * sen2cimap
Parent CI-phone for each senone (CI or CD)
Definition: bin_mdef.h:139
bin_mdef_retain
bin_mdef_t * bin_mdef_retain(bin_mdef_t *m)
Retain a pointer to a bin_mdef_t.
Definition: bin_mdef.c:264
bin_mdef_s::n_ci_sen
int32 n_ci_sen
Number of CI senones; these are the first.
Definition: bin_mdef.h:122
cd_tree_s::ctx
int16 ctx
Context (word position or CI phone)
Definition: bin_mdef.h:105
cd_tree_s::n_down
int16 n_down
Number of children (0 for leafnode)
Definition: bin_mdef.h:106
bin_mdef_s::filemap
mmio_file_t * filemap
File map for this file (if any)
Definition: bin_mdef.h:130
mdef_entry_s::ssid
int32 ssid
Senone sequence ID.
Definition: bin_mdef.h:74
bin_mdef_write_text
POCKETSPHINX_EXPORT int bin_mdef_write_text(bin_mdef_t *m, const char *filename)
Write a binary mdef to a text file.
Definition: bin_mdef.c:605
bin_mdef_s::ciname
char ** ciname
CI phone names.
Definition: bin_mdef.h:131
mdef_entry_s::tmat
int32 tmat
Transition matrix ID.
Definition: bin_mdef.h:75
bin_mdef_write
POCKETSPHINX_EXPORT int bin_mdef_write(bin_mdef_t *m, const char *filename)
Write a binary mdef to a file.
Definition: bin_mdef.c:521