/
VorbisComment.cs
408 lines (362 loc) · 14.7 KB
/
VorbisComment.cs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using FlacLibSharp.Helpers;
namespace FlacLibSharp
{
/// <summary>
/// The value or values of a single Vorbis Comment Field.
/// </summary>
public class VorbisCommentValues : List<string>
{
/// <summary>
/// Creates an empty vorbis comment.
/// </summary>
public VorbisCommentValues() { }
/// <summary>
/// Creates a vorbis comment with one value.
/// </summary>
public VorbisCommentValues(string value)
{
this.Add(value);
}
/// <summary>
/// Creates a vorbis comment with the given values.
/// </summary>
public VorbisCommentValues(IEnumerable<string> values)
{
this.AddRange(values);
}
/// <summary>
/// The first value of all values.
/// </summary>
public string Value {
get
{
if (this.Count == 0) { return string.Empty; }
return this[0];
}
set
{
if (this.Count == 0) { this.Add(value); }
else { this[0] = value; }
}
}
/// <summary>
/// If the comment has at least one value, the first value is returned, otherwise an empty string.
/// </summary>
public override string ToString()
{
return this.Value;
}
}
/// <summary>
/// A metadata block containing the "Vorbis Comment" (artist, title, ...)
/// </summary>
public class VorbisComment : MetadataBlock, IEnumerable<KeyValuePair<string, VorbisCommentValues>>
{
// Vorbis format: http://www.xiph.org/vorbis/doc/v-comment.html
private Dictionary<string, VorbisCommentValues> comments;
private string vendor;
/// <summary>
/// Initializes a Vorbis comment block, without any content.
/// </summary>
public VorbisComment()
{
this.Header.Type = MetadataBlockHeader.MetadataBlockType.VorbisComment;
this.comments = new Dictionary<string, VorbisCommentValues>(StringComparer.OrdinalIgnoreCase);
this.vendor = string.Empty;
}
/// <summary>
/// Loads the Vorbis from a block of data.
/// </summary>
/// <param name="data"></param>
public override void LoadBlockData(byte[] data)
{
UInt32 vendorLength = BinaryDataHelper.GetUInt32(BinaryDataHelper.SwitchEndianness(data, 0, 4), 0);
this.vendor = Encoding.UTF8.GetString(BinaryDataHelper.GetDataSubset(data, 4, (int)vendorLength));
int startOfComments = 4 + (int)vendorLength;
UInt32 userCommentListLength = BinaryDataHelper.GetUInt32(BinaryDataHelper.SwitchEndianness(data, startOfComments, 4), 0);
// Start of comments actually four bytes further (first piece is the count of items in the list)
startOfComments += 4;
for (UInt32 i = 0; i < userCommentListLength; i++)
{
UInt32 commentLength = BinaryDataHelper.GetUInt32(BinaryDataHelper.SwitchEndianness(data, startOfComments, 4), 0);
string comment = Encoding.UTF8.GetString(BinaryDataHelper.GetDataSubset(data, startOfComments + 4, (int)commentLength));
// We're moving on in the array ...
startOfComments += 4 + (int)commentLength;
AddComment(comment);
}
// All done, note that FLAC doesn't have the "fraiming bit" for vorbis ...
}
/// <summary>
/// Will write the data describing this metadata to the given stream.
/// </summary>
/// <param name="targetStream">Stream to write the data to.</param>
public override void WriteBlockData(Stream targetStream)
{
uint totalLength = 0;
long headerPosition = targetStream.Position;
this.Header.WriteHeaderData(targetStream);
// Write the vendor string (first write the length as a 32-bit uint and then the actual bytes
byte[] vendorData = System.Text.Encoding.UTF8.GetBytes(this.vendor);
byte[] number = BinaryDataHelper.GetBytesUInt32((uint)vendorData.Length);
targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4);
targetStream.Write(vendorData, 0, vendorData.Length);
totalLength += 4 + (uint)vendorData.Length;
// In FlacLibSharp a single comment can have multiple values, but
// in the FLAC format each value is a comment by itself. So
// we can't use this.comments.Count, since 1 comment could have 10
// values, which results in 10 comments in the FLAC file.
var totalValueCount = 0;
foreach(var comment in this.comments)
{
foreach (var value in comment.Value)
{
totalValueCount++;
}
}
number = BinaryDataHelper.GetBytesUInt32((uint)totalValueCount);
targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4);
totalLength += 4;
foreach (var comment in this.comments)
{
foreach (var value in comment.Value)
{
string commentText = string.Format("{0}={1}", comment.Key, value);
byte[] commentData = System.Text.Encoding.UTF8.GetBytes(commentText);
number = BinaryDataHelper.GetBytesUInt32((uint)commentData.Length);
targetStream.Write(BinaryDataHelper.SwitchEndianness(number, 0, 4), 0, 4);
targetStream.Write(commentData, 0, commentData.Length);
totalLength += 4 + (uint)commentData.Length;
}
}
long endPosition = targetStream.Position;
targetStream.Seek(headerPosition, SeekOrigin.Begin);
this.Header.MetaDataBlockLength = totalLength;
this.Header.WriteHeaderData(targetStream);
targetStream.Seek(endPosition, SeekOrigin.Begin);
// Note: FLAC does NOT have the framing bit for vorbis so we don't have to write this.
}
/// <summary>
/// Returns an enumerator that iterates through all VorbisComments.
/// </summary>
public IEnumerator<KeyValuePair<string, VorbisCommentValues>> GetEnumerator()
{
return this.comments.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through all VorbisComments.
/// </summary>
IEnumerator IEnumerable.GetEnumerator()
{
return this.comments.GetEnumerator();
}
/// <summary>
/// Adds a comment to the list of vorbis comments.
/// </summary>
/// <param name="comment">Comment in the format "FIELDNAME=VALUE".</param>
protected void AddComment(string comment)
{
int splitIndex = comment.IndexOf('=');
string key = comment.Substring(0, splitIndex);
string value = comment.Substring(splitIndex + 1);
AddComment(key, value);
}
protected void AddComment(string fieldName, VorbisCommentValues values)
{
if (this.comments.ContainsKey(fieldName))
{
this.comments[fieldName].AddRange(values);
}
else
{
this.comments.Add(fieldName, values);
}
}
/// <summary>
/// Adds a comment to the list of vorbis comments.
/// </summary>
protected void AddComment(string fieldName, string value)
{
if (this.comments.ContainsKey(fieldName))
{
this.comments[fieldName].Add(value);
} else
{
this.comments.Add(fieldName, new VorbisCommentValues(value));
}
}
/// <summary>
/// The Vendor of the Flac file.
/// </summary>
public string Vendor { get { return this.vendor; } }
/// <summary>
/// Get one of the vorbis comments.
/// </summary>
/// <param name="key">The key of the vorbis comment field.</param>
/// <returns>The value of the vorbis comment field.</returns>
public VorbisCommentValues this[string key]
{
get
{
if (!this.comments.ContainsKey(key))
{
this.comments.Add(key, new VorbisCommentValues());
}
return this.comments[key];
}
set
{
if (!this.comments.ContainsKey(key))
{
this.comments.Add(key, value);
} else
{
this.comments[key] = value;
}
}
}
/// <summary>
/// Checks whether a field with the given key is present in the Vorbis Comment data.
/// </summary>
/// <param name="key">The key of the vorbis comment field.</param>
/// <returns>True if such a field is available.</returns>
public bool ContainsField(string key)
{
return this.comments.ContainsKey(key);
}
/// <summary>
/// Removes all Vorbis Comment values for the given key.
/// </summary>
/// <param name="key">The key of the vorbis comment field to be removed.</param>
/// <remarks>Does nothing if no Vorbis Comments with the key are found.</remarks>
public void Remove(string key)
{
if (this.comments.ContainsKey(key))
{
this.comments.Remove(key);
}
}
/// <summary>
/// Removes the given value from the VorbisComment.
/// </summary>
/// <param name="key">The key of the vorbis comment field to be removed.</param>
/// <remarks>Does nothing if no Vorbis Comments with the key are found.</remarks>
public void Remove(string key, string value)
{
if (this.comments.ContainsKey(key))
{
for(var i = this.comments[key].Count - 1; i >= 0; i--)
{
if (this.comments[key][i].Equals(value, StringComparison.OrdinalIgnoreCase))
{
this.comments[key].RemoveAt(i);
}
}
}
}
/// <summary>
/// Adds a new Vorbis Comment with the given key and a single value.
/// </summary>
/// <param name="key">The key of the vorbis comment field to be added.</param>
/// <param name="value">The value for this comment.</param>
/// <remarks>If a tag with the key already exists, the value is appended.</remarks>
public void Add(string key, string value)
{
if (this.ContainsField(key))
{
this[key].Add(value);
} else
{
this[key] = new VorbisCommentValues(value);
}
}
/// <summary>
/// Adds a new Vorbis Comment with the given key and a list of values.
/// </summary>
/// <param name="key">The key of the vorbis comment field to be removed.</param>
/// <param name="values">The values for this comment.</param>
/// <remarks>If a tag with the key already exists, the values are appended.</remarks>
public void Add(string key, IEnumerable<string> values)
{
if (this.ContainsField(key))
{
this[key].AddRange(values);
} else
{
this[key] = new VorbisCommentValues(values);
}
}
/// <summary>
/// Replaces all the values for a given tag with the given value.
/// </summary>
/// <param name="key">The key of the vorbis comment field to be replaced.</param>
/// <param name="values">The values for this comment.</param>
public void Replace(string key, string value)
{
this[key] = new VorbisCommentValues(value);
}
/// <summary>
/// Replaces all the values for a given tag with the given value.
/// </summary>
/// <param name="key">The key of the vorbis comment field to be replaced.</param>
/// <param name="values">The values for this comment.</param>
public void Replace(string key, IEnumerable<string> values)
{
this[key] = new VorbisCommentValues(values);
}
/// <summary>
/// Gets or sets the Artist if available.
/// </summary>
public VorbisCommentValues Artist {
get { return this["ARTIST"]; }
set { this["ARTIST"] = value; }
}
/// <summary>
/// Gets or sets the Title if available.
/// </summary>
public VorbisCommentValues Title {
get { return this["TITLE"]; }
set { this["TITLE"] = value; }
}
/// <summary>
/// Gets or sets the Album if available.
/// </summary>
public VorbisCommentValues Album {
get { return this["ALBUM"]; }
set { this["ALBUM"] = value; }
}
/// <summary>
/// Gets or sets the Date if available.
/// </summary>
public VorbisCommentValues Date {
get { return this["DATE"]; }
set { this["DATE"] = value; }
}
/// <summary>
/// Gets or sets the Tacknumber if available.
/// </summary>
public VorbisCommentValues TrackNumber {
get { return this["TRACKNUMBER"]; }
set { this["TRACKNUMBER"] = value; }
}
/// <summary>
/// Gets or sets the Genre if available.
/// </summary>
public VorbisCommentValues Genre {
get { return this["GENRE"]; }
set { this["GENRE"] = value; }
}
/// <summary>
/// Gets or sets the embedded CueSheet if available.
/// </summary>
public VorbisCommentValues CueSheet
{
get { return this["CUESHEET"]; }
set { this["CUESHEET"] = value; }
}
}
}