forked from wader/fq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wav.go
211 lines (183 loc) · 5.89 KB
/
wav.go
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
package riff
// http://soundfile.sapp.org/doc/WaveFormat/
// https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/wavdec.c
// https://tech.ebu.ch/docs/tech/tech3285.pdf
// http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
// TODO: audio/wav
// TODO: default little endian
import (
"embed"
"github.com/wader/fq/format"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
)
//go:embed wav.md
var wavFS embed.FS
var wavHeaderGroup decode.Group
var wavFooterGroup decode.Group
var wavDolbyMetadataGroup decode.Group
func init() {
interp.RegisterFormat(
format.WAV,
&decode.Format{
ProbeOrder: format.ProbeOrderBinFuzzy, // after most others (overlap some with webp)
Description: "WAV file",
Groups: []*decode.Group{format.Probe},
DecodeFn: wavDecode,
Dependencies: []decode.Dependency{
{Groups: []*decode.Group{format.ID3v2}, Out: &wavHeaderGroup},
{Groups: []*decode.Group{format.ID3v1, format.ID3v11}, Out: &wavFooterGroup},
{Groups: []*decode.Group{format.Dolby_Metadata}, Out: &wavDolbyMetadataGroup},
},
})
interp.RegisterFS(wavFS)
}
const (
formatExtensible = 0xfffe
)
var (
subFormatPCMBytes = [16]byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
subFormatIEEEFloat = [16]byte{0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
)
const wavRiffType = "WAVE"
var subFormatNames = scalar.RawBytesMap{
{Bytes: subFormatPCMBytes[:], Scalar: scalar.BitBuf{Sym: "pcm"}},
{Bytes: subFormatIEEEFloat[:], Scalar: scalar.BitBuf{Sym: "ieee_float"}},
}
func wavDecode(d *decode.D) any {
d.Endian = decode.LittleEndian
// there are wav files in the wild with id3v2 header id3v1 footer
_, _, _ = d.TryFieldFormat("header", &wavHeaderGroup, nil)
var riffType string
riffDecode(
d,
nil,
func(d *decode.D, path path) (string, int64) {
id := d.FieldUTF8("id", 4, scalar.ActualTrimSpace, chunkIDDescriptions)
const restOfFileLen = 0xffffffff
size := int64(d.FieldScalarUintFn("size", func(d *decode.D) scalar.Uint {
l := d.U32()
if l == restOfFileLen {
return scalar.Uint{Actual: l, DisplayFormat: scalar.NumberHex, Description: "Rest of file"}
}
return scalar.Uint{Actual: l, DisplayFormat: scalar.NumberDecimal}
}).Actual)
if size == restOfFileLen {
size = d.BitsLeft() / 8
}
return id, size
},
func(d *decode.D, id string, path path) (bool, any) {
switch id {
case "RIFF":
riffType = d.FieldUTF8("format", 4, d.StrAssert(wavRiffType))
return true, nil
case "LIST":
d.FieldUTF8("type", 4)
return true, nil
case "fmt":
audioFormat := d.FieldU16("audio_format", format.WAVTagNames)
d.FieldU16("num_channels")
d.FieldU32("sample_rate")
d.FieldU32("byte_rate")
d.FieldU16("block_align")
d.FieldU16("bits_per_sample")
if audioFormat == formatExtensible && d.BitsLeft() > 0 {
d.FieldU16("extension_size")
d.FieldU16("valid_bits_per_sample")
d.FieldU32("channel_mask")
d.FieldRawLen("sub_format", 16*8, subFormatNames)
}
return false, nil
case "data":
d.FieldRawLen("samples", d.BitsLeft())
return false, nil
case "fact":
d.FieldU32("sample_length")
return false, nil
case "smpl":
d.FieldU32("manufacturer")
d.FieldU32("product")
d.FieldU32("sample_period")
d.FieldU32("midi_unity_note")
d.FieldU32("midi_pitch_fraction")
d.FieldU32("smpte_format")
d.FieldU32("smpte_offset")
numSampleLoops := int(d.FieldU32("number_of_sample_loops"))
samplerDataBytes := int(d.FieldU32("sampler_data_bytes"))
d.FieldArray("samples_loops", func(d *decode.D) {
for i := 0; i < numSampleLoops; i++ {
d.FieldStruct("sample_loop", func(d *decode.D) {
d.FieldUTF8("id", 4)
d.FieldU32("type", scalar.UintMapSymStr{
0: "forward",
1: "forward_backward",
2: "backward",
})
d.FieldU32("start")
d.FieldU32("end")
d.FieldU32("fraction")
d.FieldU32("number_of_times")
})
}
})
d.FieldRawLen("sampler_data", int64(samplerDataBytes)*8)
return false, nil
case "bext":
d.FieldUTF8NullFixedLen("description", 256)
d.FieldUTF8NullFixedLen("originator", 32)
d.FieldUTF8NullFixedLen("originator_reference", 32)
d.FieldUTF8NullFixedLen("originator_date", 10)
d.FieldUTF8NullFixedLen("originator_time", 8)
d.FieldU32("time_reference_low")
d.FieldU32("time_reference_high")
d.FieldU16("version")
d.FieldRawLen("umid", 64*8)
d.FieldU16("loudness_value")
d.FieldU16("loudness_range")
d.FieldU16("max_true_peak_level")
d.FieldU16("max_momentary_loudness")
d.FieldU16("max_short_term_loudness")
d.FieldRawLen("reserved", 180*8)
d.FieldRawLen("coding_history", d.BitsLeft())
return false, nil
case "chna":
d.FieldU16("num_tracks")
d.FieldU16("num_uids")
d.FieldArray("audio_ids", func(d *decode.D) {
for !d.End() {
d.FieldStruct("audio_id", func(d *decode.D) {
d.FieldU16("track_index")
d.FieldUTF8("uid", 12)
d.FieldUTF8("track_format_id_reference", 14)
d.FieldUTF8("pack_format_id_reference", 11)
d.FieldRawLen("padding", 8)
})
}
})
return false, nil
case "axml":
d.FieldUTF8("xml", int(d.BitsLeft())/8)
return false, nil
case "dbmd":
// TEMP TEMP TEMP: delete old dolby.go and bring uncomment
// old_dbmdDecode(d) //
d.Format(&wavDolbyMetadataGroup, nil)
return false, nil
default:
if riffIsStringChunkID(id) {
d.FieldUTF8NullFixedLen("value", int(d.BitsLeft())/8)
return false, nil
}
d.FieldRawLen("data", d.BitsLeft())
return false, nil
}
},
)
if riffType != wavRiffType {
d.Errorf("wrong or no WAV riff type found (%s)", riffType)
}
_, _, _ = d.TryFieldFormat("footer", &wavFooterGroup, nil)
return nil
}