Initial import to Tizen
[profile/ivi/pocketsphinx.git] / scripts / s3mixw.py
1 # Copyright (c) 2006 Carnegie Mellon University
2 #
3 # You may copy and modify this freely under the same terms as
4 # Sphinx-III
5
6 """Read/write Sphinx-III Gaussian mixture weight files.
7
8 This module reads and writes the Gaussian mixture weight files used by
9 SphinxTrain, Sphinx-III, and PocketSphinx.
10 """
11
12 __author__ = "David Huggins-Daines <dhuggins@cs.cmu.edu>"
13 __version__ = "$Revision: 8994 $"
14
15 from s3file import S3File, S3File_write
16
17 def open(filename, mode="rb"):
18     if mode in ("r", "rb"):
19         return S3MixwFile(filename, mode)
20     elif mode in ("w", "wb"):
21         return S3MixwFile_write(filename, mode)
22     else:
23         raise Exception, "mode must be 'r', 'rb', 'w', or 'wb'"
24
25 class S3MixwFile(S3File):
26     "Read Sphinx-III format mixture weight files"
27     def __init__(self, file, mode):
28         S3File.__init__(self, file, mode)
29         self._params = self._load()
30
31     def readgauheader(self):
32         if self.fileattr["version"] != "1.0":
33             raise Exception("Version mismatch: must be 1.0 but is "
34                             + self.fileattr["version"])
35
36     def _load(self):
37         self.readgauheader()
38         self.fh.seek(self.data_start, 0)
39         return self.read3d()
40
41 class S3MixwFile_write(S3File_write):
42     "Write Sphinx-III format mixture weight files"
43
44     def writeall(self, stuff):
45         self.write3d(stuff)