KaitaiStructWorkshop/08_enums.ksy

154 lines
3.3 KiB
Plaintext

meta:
id: wav
file-extension: wav
encoding: ascii
endian: le
seq:
- id: magic
# Could assert using contents here, but
# just make sections conditional later
#contents: "RIFF"
# Not using contents means we can give this a type
# Big endian, because we store the 4-character string
# 'RIFF' (0x52 0x49 0x46 0x46) as enum value 0x52494646
type: u4be
# Integers can have an optional enum key
enum: fourcc
- id: size
type: u4
- id: wave_str
# Same as the magic field
#contents: "WAVE"
type: u4be
enum: fourcc
- id: sections
type: section
repeat: eos
# Only try to decode this if this looks like a wave file
if: 'magic == fourcc::riff and wave_str == fourcc::wave'
instances: {}
types:
section:
seq:
- id: name
type: u4be # Big endian, same reason as above
enum: fourcc
- id: size
type: u4
- id: content
size: size
type:
switch-on: name
cases:
'fourcc::fmt': fmt
'fourcc::data': data
'fourcc::fact': fact
'fourcc::peak': peak
'fourcc::list': list
fmt:
seq:
- id: type
type: u2
enum: encoding
- id: channels
type: u2
- id: samplerate
type: u4
- id: datarate
type: u4
doc: Might also be datarate per channel
- id: bytes_per_sample
type: u2
- id: bits_per_unit
type: u2
data:
seq:
- id: content
type:
switch-on: _root.sections[0].name
cases:
'fourcc::fmt': sample(_root.sections[0].content.as<fmt>)
_: u1
repeat: eos
types:
sample:
params:
- id: fmt
type: fmt
seq:
- id: data
type:
switch-on: fmt.type
cases:
'encoding::float': float_type
'encoding::int': int_type
repeat: expr
repeat-expr: _root.sections[0].content.\
as<fmt>.channels
types:
float_type:
seq:
- id: data
type:
switch-on: _parent.fmt.bits_per_unit
cases:
32: f4
64: f8
int_type:
seq:
- id: data
type:
switch-on: _parent.fmt.bits_per_unit
cases:
8: u1
16: s2
32: s4
fact:
seq:
- id: samples
type: u4
peak:
seq:
- id: unknown_0
type: u4
- id: timestamp
type: u4
- id: peak
type: f4
- id: index
type: u4
list:
seq:
- id: info_str
type: str
size: 4
- id: contents
repeat: eos
type: content
types:
content:
seq:
- id: name
type: str
size: 4
- id: size
type: u4
- id: content
type: strz
size: size
enums:
fourcc:
0x52494646: 'riff'
0x57415645: 'wave'
0x666d7420: 'fmt'
0x66616374: 'fact'
0x5045414b: 'peak'
0x64617461: 'data'
0x4c495354: 'list'
encoding:
1: 'int'
3: 'float'
doc: Hello, world
doc-ref: https://example.com