forked from go-audio/wav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_chunk.go
183 lines (174 loc) · 4.83 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
package wav
import (
"bytes"
"encoding/binary"
"fmt"
"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 - %v", 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 - %v", 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)
}
for err == nil {
err = readSubHeader()
if err != nil {
break
}
scratch = make([]byte, size)
r.Read(scratch)
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()...)
}