From 630d94c1eea1a8379aaa76c2ddcae862710a4061 Mon Sep 17 00:00:00 2001 From: Valentin Ochs Date: Wed, 30 Dec 2020 08:15:28 +0100 Subject: [PATCH] Comments and stuff --- 02_preamble.ksy | 5 +++++ 03_sections.ksy | 3 +++ 05_data.ksy | 3 +++ 08_enums.ksy | 33 +++++++++++++++++++-------------- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/02_preamble.ksy b/02_preamble.ksy index cc697bc..af21f6f 100644 --- a/02_preamble.ksy +++ b/02_preamble.ksy @@ -7,11 +7,16 @@ seq: - id: magic type: str size: 4 + # We don't actually handle invalid files here. + # If we wanted, we could also use this key: + # contents: "RIFF" + # But then we could not use the type key... - id: size type: u4 - id: wave_str type: str size: 4 + # Same as with the magic field instances: {} types: {} enums: {} diff --git a/03_sections.ksy b/03_sections.ksy index a00a422..dbd8fac 100644 --- a/03_sections.ksy +++ b/03_sections.ksy @@ -14,6 +14,7 @@ seq: size: 4 - id: sections type: section + # Repeat until the end of the stream repeat: eos instances: {} types: @@ -26,6 +27,8 @@ types: type: u4 - id: content size: size + # No type yet - just read in a byte array of the given + # size, which refers to the size field just before enums: {} doc: Hello, world doc-ref: https://example.com diff --git a/05_data.ksy b/05_data.ksy index a778444..f02ce95 100644 --- a/05_data.ksy +++ b/05_data.ksy @@ -59,6 +59,9 @@ types: seq: - id: data type: + # I don't mention this in the video, but you + # can use arithmetic expressions here as well + # I just did this to show value instances :) switch-on: _root.sections[0].content.\ as.combined_type cases: diff --git a/08_enums.ksy b/08_enums.ksy index e3f9186..a448c93 100644 --- a/08_enums.ksy +++ b/08_enums.ksy @@ -8,24 +8,32 @@ seq: # 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! + type: u4be # Big endian, same reason as above enum: fourcc - id: size type: u4 @@ -43,7 +51,7 @@ types: seq: - id: type type: u2 - enum: encoding_type_enum + enum: encoding - id: channels type: u2 - id: samplerate @@ -61,24 +69,21 @@ types: type: switch-on: _root.sections[0].name cases: - 'fourcc::fmt': sample(_root.sections[0].content.as.type, _root.sections[0].content.as.bits_per_unit) + 'fourcc::fmt': sample(_root.sections[0].content.as) _: u1 repeat: eos types: sample: params: - - id: encoding_type - type: u2 - enum: encoding_type_enum - - id: encoding_size - type: u2 + - id: fmt + type: fmt seq: - id: data type: - switch-on: encoding_type + switch-on: fmt.type cases: - 'encoding_type_enum::float': float_type - 'encoding_type_enum::int': int_type + 'encoding::float': float_type + 'encoding::int': int_type repeat: expr repeat-expr: _root.sections[0].content.\ as.channels @@ -87,7 +92,7 @@ types: seq: - id: data type: - switch-on: _parent.encoding_size + switch-on: _parent.fmt.bits_per_unit cases: 32: f4 64: f8 @@ -95,7 +100,7 @@ types: seq: - id: data type: - switch-on: _parent.encoding_size + switch-on: _parent.fmt.bits_per_unit cases: 8: u1 16: s2 @@ -142,7 +147,7 @@ enums: 0x5045414b: 'peak' 0x64617461: 'data' 0x4c495354: 'list' - encoding_type_enum: + encoding: 1: 'int' 3: 'float' doc: Hello, world