-
Notifications
You must be signed in to change notification settings - Fork 46
/
list_chunk.go
205 lines (193 loc) · 5.57 KB
/
list_chunk.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
package wav
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/go-audio/riff"
)
var (
// See http://bwfmetaedit.sourceforge.net/listinfo.html
markerIART = [4]byte{'I', 'A', 'R', 'T'}
markerISFT = [4]byte{'I', 'S', 'F', 'T'}
markerICRD = [4]byte{'I', 'C', 'R', 'D'}
markerICOP = [4]byte{'I', 'C', 'O', 'P'}
markerIARL = [4]byte{'I', 'A', 'R', 'L'}
markerINAM = [4]byte{'I', 'N', 'A', 'M'}
markerIENG = [4]byte{'I', 'E', 'N', 'G'}
markerIGNR = [4]byte{'I', 'G', 'N', 'R'}
markerIPRD = [4]byte{'I', 'P', 'R', 'D'}
markerISRC = [4]byte{'I', 'S', 'R', 'C'}
markerISBJ = [4]byte{'I', 'S', 'B', 'J'}
markerICMT = [4]byte{'I', 'C', 'M', 'T'}
markerITRK = [4]byte{'I', 'T', 'R', 'K'}
markerITRKBug = [4]byte{'i', 't', 'r', 'k'}
markerITCH = [4]byte{'I', 'T', 'C', 'H'}
markerIKEY = [4]byte{'I', 'K', 'E', 'Y'}
markerIMED = [4]byte{'I', 'M', 'E', 'D'}
)
// DecodeListChunk decodes a LIST chunk
func DecodeListChunk(d *Decoder, ch *riff.Chunk) error {
if ch == nil {
return fmt.Errorf("can't decode a nil chunk")
}
if d == nil {
return fmt.Errorf("nil decoder")
}
if ch.ID == CIDList {
// read the entire chunk in memory
buf := make([]byte, ch.Size)
var err error
if _, err = ch.Read(buf); err != nil {
return fmt.Errorf("failed to read the LIST chunk - %w", err)
}
r := bytes.NewReader(buf)
// INFO subchunk
scratch := make([]byte, 4)
if _, err = r.Read(scratch); err != nil {
return fmt.Errorf("failed to read the INFO subchunk - %w", err)
}
if !bytes.Equal(scratch, CIDInfo[:]) {
// "expected an INFO subchunk but got %s", string(scratch)
// TODO: support adtl subchunks
ch.Drain()
return nil
}
if d.Metadata == nil {
d.Metadata = &Metadata{}
}
// the rest is a list of string entries
var (
id [4]byte
size uint32
)
readSubHeader := func() error {
if err := binary.Read(r, binary.BigEndian, &id); err != nil {
return err
}
return binary.Read(r, binary.LittleEndian, &size)
}
// This checks and stops early if just a word alignment byte remains to avoid
// an io.UnexpectedEOF error from readSubHeader.
// TODO(steve): Remove the checks from the for statement if ch.Size is changed
// to not include the padding byte.
for rem := ch.Size - 4; rem > 1; rem -= int(size) + 8 {
if err = readSubHeader(); err != nil {
if errors.Is(err, io.EOF) {
// All done.
break
}
return fmt.Errorf("read sub header: %w", err)
}
if cap(scratch) >= int(size) {
if len(scratch) != int(size) {
// Resize scratch.
scratch = scratch[:size]
}
} else {
// Expand scratch capacity.
scratch = append(make([]byte, int(size)-cap(scratch)), scratch[:cap(scratch)]...)
}
if _, err := r.Read(scratch); err != nil {
return fmt.Errorf("read sub header %s data %v: %w", id, scratch, err)
}
switch id {
case markerIARL:
d.Metadata.Location = nullTermStr(scratch)
case markerIART:
d.Metadata.Artist = nullTermStr(scratch)
case markerISFT:
d.Metadata.Software = nullTermStr(scratch)
case markerICRD:
d.Metadata.CreationDate = nullTermStr(scratch)
case markerICOP:
d.Metadata.Copyright = nullTermStr(scratch)
case markerINAM:
d.Metadata.Title = nullTermStr(scratch)
case markerIENG:
d.Metadata.Engineer = nullTermStr(scratch)
case markerIGNR:
d.Metadata.Genre = nullTermStr(scratch)
case markerIPRD:
d.Metadata.Product = nullTermStr(scratch)
case markerISRC:
d.Metadata.Source = nullTermStr(scratch)
case markerISBJ:
d.Metadata.Subject = nullTermStr(scratch)
case markerICMT:
d.Metadata.Comments = nullTermStr(scratch)
case markerITRK, markerITRKBug:
d.Metadata.TrackNbr = nullTermStr(scratch)
case markerITCH:
d.Metadata.Technician = nullTermStr(scratch)
case markerIKEY:
d.Metadata.Keywords = nullTermStr(scratch)
case markerIMED:
d.Metadata.Medium = nullTermStr(scratch)
}
}
}
ch.Drain()
return nil
}
func encodeInfoChunk(e *Encoder) []byte {
if e == nil || e.Metadata == nil {
return nil
}
buf := bytes.NewBuffer(nil)
writeSection := func(id [4]byte, val string) {
buf.Write(id[:])
binary.Write(buf, binary.LittleEndian, uint32(len(val)+1))
buf.Write(append([]byte(val), 0x00))
}
if e.Metadata.Artist != "" {
writeSection(markerIART, e.Metadata.Artist)
}
if e.Metadata.Comments != "" {
writeSection(markerICMT, e.Metadata.Comments)
}
if e.Metadata.Copyright != "" {
writeSection(markerICOP, e.Metadata.Copyright)
}
if e.Metadata.CreationDate != "" {
writeSection(markerICRD, e.Metadata.CreationDate)
}
if e.Metadata.Engineer != "" {
writeSection(markerIENG, e.Metadata.Engineer)
}
if e.Metadata.Technician != "" {
writeSection(markerITCH, e.Metadata.Technician)
}
if e.Metadata.Genre != "" {
writeSection(markerIGNR, e.Metadata.Genre)
}
if e.Metadata.Keywords != "" {
writeSection(markerIKEY, e.Metadata.Keywords)
}
if e.Metadata.Medium != "" {
writeSection(markerIMED, e.Metadata.Medium)
}
if e.Metadata.Title != "" {
writeSection(markerINAM, e.Metadata.Title)
}
if e.Metadata.Product != "" {
writeSection(markerIPRD, e.Metadata.Product)
}
if e.Metadata.Subject != "" {
writeSection(markerISBJ, e.Metadata.Subject)
}
if e.Metadata.Software != "" {
writeSection(markerISFT, e.Metadata.Software)
}
if e.Metadata.Source != "" {
writeSection(markerISRC, e.Metadata.Source)
}
if e.Metadata.Location != "" {
writeSection(markerIARL, e.Metadata.Location)
}
if e.Metadata.TrackNbr != "" {
writeSection(markerITRK, e.Metadata.TrackNbr)
}
return append(CIDInfo, buf.Bytes()...)
}