summaryrefslogtreecommitdiff
path: root/python/examples/kaitai/creative_voice_file.py
blob: 3ee67227cfeb597d9d3846aeb984fd5ae24d275b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

from pkg_resources import parse_version
from .kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO
from enum import Enum
import collections


if parse_version(ks_version) < parse_version('0.7'):
    raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version))

class CreativeVoiceFile(KaitaiStruct):
    """Creative Voice File is a container file format for digital audio
    wave data. Initial revisions were able to support only unsigned
    8-bit PCM and ADPCM data, later versions were revised to add support
    for 16-bit PCM and a-law / u-law formats.
    
    This format was actively used in 1990s, around the advent of
    Creative's sound cards (Sound Blaster family). It was a popular
    choice for a digital sound container in lots of games and multimedia
    software due to simplicity and availability of Creative's recording
    / editing tools.
    
    .. seealso::
       Source - https://wiki.multimedia.cx/index.php?title=Creative_Voice
    """

    class BlockTypes(Enum):
        terminator = 0
        sound_data = 1
        sound_data_cont = 2
        silence = 3
        marker = 4
        text = 5
        repeat_start = 6
        repeat_end = 7
        extra_info = 8
        sound_data_new = 9

    class Codecs(Enum):
        pcm_8bit_unsigned = 0
        adpcm_4bit = 1
        adpcm_2_6bit = 2
        adpcm_2_bit = 3
        pcm_16bit_signed = 4
        alaw = 6
        ulaw = 7
        adpcm_4_to_16bit = 512
    SEQ_FIELDS = ["magic", "header_size", "version", "checksum", "blocks"]
    def __init__(self, _io, _parent=None, _root=None):
        self._io = _io
        self._parent = _parent
        self._root = _root if _root else self
        self._debug = collections.defaultdict(dict)

    def _read(self):
        self._debug['magic']['start'] = self._io.pos()
        self.magic = self._io.ensure_fixed_contents(b"\x43\x72\x65\x61\x74\x69\x76\x65\x20\x56\x6F\x69\x63\x65\x20\x46\x69\x6C\x65\x1A")
        self._debug['magic']['end'] = self._io.pos()
        self._debug['header_size']['start'] = self._io.pos()
        self.header_size = self._io.read_u2le()
        self._debug['header_size']['end'] = self._io.pos()
        self._debug['version']['start'] = self._io.pos()
        self.version = self._io.read_u2le()
        self._debug['version']['end'] = self._io.pos()
        self._debug['checksum']['start'] = self._io.pos()
        self.checksum = self._io.read_u2le()
        self._debug['checksum']['end'] = self._io.pos()
        self._debug['blocks']['start'] = self._io.pos()
        self.blocks = []
        i = 0
        while not self._io.is_eof():
            if not 'arr' in self._debug['blocks']:
                self._debug['blocks']['arr'] = []
            self._debug['blocks']['arr'].append({'start': self._io.pos()})
            _t_blocks = self._root.Block(self._io, self, self._root)
            _t_blocks._read()
            self.blocks.append(_t_blocks)
            self._debug['blocks']['arr'][len(self.blocks) - 1]['end'] = self._io.pos()
            i += 1

        self._debug['blocks']['end'] = self._io.pos()

    class BlockMarker(KaitaiStruct):
        """
        .. seealso::
           Source - https://wiki.multimedia.cx/index.php?title=Creative_Voice#Block_type_0x04:_Marker
        """
        SEQ_FIELDS = ["marker_id"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['marker_id']['start'] = self._io.pos()
            self.marker_id = self._io.read_u2le()
            self._debug['marker_id']['end'] = self._io.pos()


    class BlockSilence(KaitaiStruct):
        """
        .. seealso::
           Source - https://wiki.multimedia.cx/index.php?title=Creative_Voice#Block_type_0x03:_Silence
        """
        SEQ_FIELDS = ["duration_samples", "freq_div"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['duration_samples']['start'] = self._io.pos()
            self.duration_samples = self._io.read_u2le()
            self._debug['duration_samples']['end'] = self._io.pos()
            self._debug['freq_div']['start'] = self._io.pos()
            self.freq_div = self._io.read_u1()
            self._debug['freq_div']['end'] = self._io.pos()

        @property
        def sample_rate(self):
            if hasattr(self, '_m_sample_rate'):
                return self._m_sample_rate if hasattr(self, '_m_sample_rate') else None

            self._m_sample_rate = (1000000.0 / (256 - self.freq_div))
            return self._m_sample_rate if hasattr(self, '_m_sample_rate') else None

        @property
        def duration_sec(self):
            """Duration of silence, in seconds."""
            if hasattr(self, '_m_duration_sec'):
                return self._m_duration_sec if hasattr(self, '_m_duration_sec') else None

            self._m_duration_sec = (self.duration_samples / self.sample_rate)
            return self._m_duration_sec if hasattr(self, '_m_duration_sec') else None


    class BlockSoundDataNew(KaitaiStruct):
        """
        .. seealso::
           Source - https://wiki.multimedia.cx/index.php?title=Creative_Voice#Block_type_0x09:_Sound_data_.28New_format.29
        """
        SEQ_FIELDS = ["sample_rate", "bits_per_sample", "num_channels", "codec", "reserved", "wave"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['sample_rate']['start'] = self._io.pos()
            self.sample_rate = self._io.read_u4le()
            self._debug['sample_rate']['end'] = self._io.pos()
            self._debug['bits_per_sample']['start'] = self._io.pos()
            self.bits_per_sample = self._io.read_u1()
            self._debug['bits_per_sample']['end'] = self._io.pos()
            self._debug['num_channels']['start'] = self._io.pos()
            self.num_channels = self._io.read_u1()
            self._debug['num_channels']['end'] = self._io.pos()
            self._debug['codec']['start'] = self._io.pos()
            self.codec = KaitaiStream.resolve_enum(self._root.Codecs, self._io.read_u2le())
            self._debug['codec']['end'] = self._io.pos()
            self._debug['reserved']['start'] = self._io.pos()
            self.reserved = self._io.read_bytes(4)
            self._debug['reserved']['end'] = self._io.pos()
            self._debug['wave']['start'] = self._io.pos()
            self.wave = self._io.read_bytes_full()
            self._debug['wave']['end'] = self._io.pos()


    class Block(KaitaiStruct):
        SEQ_FIELDS = ["block_type", "body_size1", "body_size2", "body"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['block_type']['start'] = self._io.pos()
            self.block_type = KaitaiStream.resolve_enum(self._root.BlockTypes, self._io.read_u1())
            self._debug['block_type']['end'] = self._io.pos()
            if self.block_type != self._root.BlockTypes.terminator:
                self._debug['body_size1']['start'] = self._io.pos()
                self.body_size1 = self._io.read_u2le()
                self._debug['body_size1']['end'] = self._io.pos()

            if self.block_type != self._root.BlockTypes.terminator:
                self._debug['body_size2']['start'] = self._io.pos()
                self.body_size2 = self._io.read_u1()
                self._debug['body_size2']['end'] = self._io.pos()

            if self.block_type != self._root.BlockTypes.terminator:
                self._debug['body']['start'] = self._io.pos()
                _on = self.block_type
                if _on == self._root.BlockTypes.sound_data_new:
                    self._raw_body = self._io.read_bytes(self.body_size)
                    io = KaitaiStream(BytesIO(self._raw_body))
                    self.body = self._root.BlockSoundDataNew(io, self, self._root)
                    self.body._read()
                elif _on == self._root.BlockTypes.repeat_start:
                    self._raw_body = self._io.read_bytes(self.body_size)
                    io = KaitaiStream(BytesIO(self._raw_body))
                    self.body = self._root.BlockRepeatStart(io, self, self._root)
                    self.body._read()
                elif _on == self._root.BlockTypes.marker:
                    self._raw_body = self._io.read_bytes(self.body_size)
                    io = KaitaiStream(BytesIO(self._raw_body))
                    self.body = self._root.BlockMarker(io, self, self._root)
                    self.body._read()
                elif _on == self._root.BlockTypes.sound_data:
                    self._raw_body = self._io.read_bytes(self.body_size)
                    io = KaitaiStream(BytesIO(self._raw_body))
                    self.body = self._root.BlockSoundData(io, self, self._root)
                    self.body._read()
                elif _on == self._root.BlockTypes.extra_info:
                    self._raw_body = self._io.read_bytes(self.body_size)
                    io = KaitaiStream(BytesIO(self._raw_body))
                    self.body = self._root.BlockExtraInfo(io, self, self._root)
                    self.body._read()
                elif _on == self._root.BlockTypes.silence:
                    self._raw_body = self._io.read_bytes(self.body_size)
                    io = KaitaiStream(BytesIO(self._raw_body))
                    self.body = self._root.BlockSilence(io, self, self._root)
                    self.body._read()
                else:
                    self.body = self._io.read_bytes(self.body_size)
                self._debug['body']['end'] = self._io.pos()


        @property
        def body_size(self):
            """body_size is a 24-bit little-endian integer, so we're
            emulating that by adding two standard-sized integers
            (body_size1 and body_size2).
            """
            if hasattr(self, '_m_body_size'):
                return self._m_body_size if hasattr(self, '_m_body_size') else None

            if self.block_type != self._root.BlockTypes.terminator:
                self._m_body_size = (self.body_size1 + (self.body_size2 << 16))

            return self._m_body_size if hasattr(self, '_m_body_size') else None


    class BlockRepeatStart(KaitaiStruct):
        """
        .. seealso::
           Source - https://wiki.multimedia.cx/index.php?title=Creative_Voice#Block_type_0x06:_Repeat_start
        """
        SEQ_FIELDS = ["repeat_count_1"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['repeat_count_1']['start'] = self._io.pos()
            self.repeat_count_1 = self._io.read_u2le()
            self._debug['repeat_count_1']['end'] = self._io.pos()


    class BlockSoundData(KaitaiStruct):
        """
        .. seealso::
           Source - https://wiki.multimedia.cx/index.php?title=Creative_Voice#Block_type_0x01:_Sound_data
        """
        SEQ_FIELDS = ["freq_div", "codec", "wave"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['freq_div']['start'] = self._io.pos()
            self.freq_div = self._io.read_u1()
            self._debug['freq_div']['end'] = self._io.pos()
            self._debug['codec']['start'] = self._io.pos()
            self.codec = KaitaiStream.resolve_enum(self._root.Codecs, self._io.read_u1())
            self._debug['codec']['end'] = self._io.pos()
            self._debug['wave']['start'] = self._io.pos()
            self.wave = self._io.read_bytes_full()
            self._debug['wave']['end'] = self._io.pos()

        @property
        def sample_rate(self):
            if hasattr(self, '_m_sample_rate'):
                return self._m_sample_rate if hasattr(self, '_m_sample_rate') else None

            self._m_sample_rate = (1000000.0 / (256 - self.freq_div))
            return self._m_sample_rate if hasattr(self, '_m_sample_rate') else None


    class BlockExtraInfo(KaitaiStruct):
        """
        .. seealso::
           Source - https://wiki.multimedia.cx/index.php?title=Creative_Voice#Block_type_0x08:_Extra_info
        """
        SEQ_FIELDS = ["freq_div", "codec", "num_channels_1"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['freq_div']['start'] = self._io.pos()
            self.freq_div = self._io.read_u2le()
            self._debug['freq_div']['end'] = self._io.pos()
            self._debug['codec']['start'] = self._io.pos()
            self.codec = KaitaiStream.resolve_enum(self._root.Codecs, self._io.read_u1())
            self._debug['codec']['end'] = self._io.pos()
            self._debug['num_channels_1']['start'] = self._io.pos()
            self.num_channels_1 = self._io.read_u1()
            self._debug['num_channels_1']['end'] = self._io.pos()

        @property
        def num_channels(self):
            """Number of channels (1 = mono, 2 = stereo)."""
            if hasattr(self, '_m_num_channels'):
                return self._m_num_channels if hasattr(self, '_m_num_channels') else None

            self._m_num_channels = (self.num_channels_1 + 1)
            return self._m_num_channels if hasattr(self, '_m_num_channels') else None

        @property
        def sample_rate(self):
            if hasattr(self, '_m_sample_rate'):
                return self._m_sample_rate if hasattr(self, '_m_sample_rate') else None

            self._m_sample_rate = (256000000.0 / (self.num_channels * (65536 - self.freq_div)))
            return self._m_sample_rate if hasattr(self, '_m_sample_rate') else None