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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
/***************************************************************************
copyright : (C) 2005 by Brian Nickel
email : brian.nickel@gmail.com
based on : mpegfile.cpp from TagLib
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
***************************************************************************/
using System.Collections;
using System;
namespace TagLib.Mpeg
{
[SupportedMimeType("taglib/mp3", "mp3")]
[SupportedMimeType("audio/x-mp3")]
[SupportedMimeType("application/x-id3")]
[SupportedMimeType("audio/mpeg")]
[SupportedMimeType("audio/x-mpeg")]
[SupportedMimeType("audio/x-mpeg-3")]
[SupportedMimeType("audio/mpeg3")]
[SupportedMimeType("audio/mp3")]
public class File : TagLib.File
{
//////////////////////////////////////////////////////////////////////////
// private properties
//////////////////////////////////////////////////////////////////////////
private Id3v2.Tag id3v2_tag;
private Ape.Tag ape_tag;
private Id3v1.Tag id3v1_tag;
private CombinedTag tag;
private Properties properties;
//////////////////////////////////////////////////////////////////////////
// public methods
//////////////////////////////////////////////////////////////////////////
public File (string file, Properties.ReadStyle properties_style) : base (file)
{
id3v2_tag = null;
ape_tag = null;
id3v1_tag = null;
tag = new CombinedTag ();
properties = null;
try {Mode = AccessMode.Read;}
catch {return;}
Read (properties_style);
Mode = AccessMode.Closed;
}
public File (string file) : this (file, Properties.ReadStyle.Average)
{
}
public void Save (TagTypes types, bool strip_others)
{
if (types == TagTypes.NoTags && strip_others) {
if(!Strip (TagTypes.AllTags)) {
throw new ApplicationException("Could not strip tags");
}
return;
}
if (id3v2_tag == null && id3v1_tag == null && ape_tag == null) {
if (strip_others) {
if(!Strip (TagTypes.AllTags)) {
throw new ApplicationException("Could not strip tags");
}
}
return;
}
if(IsReadOnly) {
throw new ReadOnlyException();
}
Mode = AccessMode.Write;
// Create the tags if we've been asked to. Copy the values from the tag that
// does exist into the new tag.
if ((types & TagTypes.Id3v2) != 0 && id3v1_tag != null)
TagLib.Tag.Duplicate (id3v1_tag, GetTag (TagTypes.Id3v2, true), false);
if ((types & TagTypes.Id3v1) != 0 && id3v2_tag != null)
TagLib.Tag.Duplicate (id3v2_tag, GetTag (TagTypes.Id3v1, true), false);
bool success = true;
if ((TagTypes.Id3v2 & types) != 0 && id3v2_tag != null && !id3v2_tag.IsEmpty)
{
long id3v2_location = FindId3v2 ();
int id3v2_size = 0;
if (id3v2_location < 0)
id3v2_location = 0;
else
{
Seek (id3v2_location);
Id3v2.Header header = new Id3v2.Header (ReadBlock ((int) Id3v2.Header.Size));
if (header.TagSize == 0)
Debugger.Debug ("Mpc.File.Save() -- Id3v2 header is broken. Ignoring.");
else
id3v2_size = (int) header.CompleteTagSize;
}
Insert (id3v2_tag.Render (), id3v2_location, id3v2_size);
}
else if (strip_others)
success = Strip (TagTypes.Id3v2) && success;
if((TagTypes.Id3v1 & types) != 0 && id3v1_tag != null && !id3v1_tag.IsEmpty)
{
long id3v1_location = FindId3v1 ();
if (id3v1_location < 0)
Seek (0, System.IO.SeekOrigin.End);
else
Seek (id3v1_location);
WriteBlock (id3v1_tag.Render ());
}
else if (strip_others)
success = Strip (TagTypes.Id3v1, false) && success;
// Dont save an APE-tag unless one has been created
if((TagTypes.Ape & types) != 0 && ape_tag != null && !ape_tag.IsEmpty)
{
long ape_location = FindApe (FindId3v1 () >= 0);
long ape_size = 0;
if (ape_location < 0)
ape_location = Length;
else
{
Seek (ape_location);
ape_size = (new Ape.Footer (ReadBlock ((int) Ape.Footer.Size))).CompleteTagSize;
ape_location = ape_location + Ape.Footer.Size - ape_size;
}
Insert (ape_tag.Render (), ape_location, ape_size);
}
else if(strip_others)
success = Strip (TagTypes.Ape, false) && success;
Mode = AccessMode.Closed;
if(!success) {
throw new ApplicationException("Cannot write tags");
}
}
public void Save (TagTypes tags)
{
Save (tags, true);
}
public override void Save ()
{
Save (TagTypes.AllTags);
}
public override TagLib.Tag GetTag (TagTypes type, bool create)
{
switch (type)
{
case TagTypes.Id3v1:
{
if (create && id3v1_tag == null)
{
id3v1_tag = new Id3v1.Tag ();
TagLib.Tag.Duplicate (tag, id3v1_tag, true);
tag.SetTags (id3v2_tag, ape_tag, id3v1_tag);
}
return id3v1_tag;
}
case TagTypes.Id3v2:
{
if (create && id3v2_tag == null)
{
id3v2_tag = new Id3v2.Tag ();
TagLib.Tag.Duplicate (tag, id3v2_tag, true);
tag.SetTags (id3v2_tag, ape_tag, id3v1_tag);
}
return id3v2_tag;
}
case TagTypes.Ape:
{
if (create && ape_tag == null)
{
ape_tag = new Ape.Tag ();
TagLib.Tag.Duplicate (tag, ape_tag, true);
tag.SetTags (id3v2_tag, ape_tag, id3v1_tag);
}
return ape_tag;
}
default:
return null;
}
}
public bool Strip (TagTypes types, bool free_memory)
{
AccessMode original_mode = Mode;
if (IsReadOnly)
{
Debugger.Debug ("Mpeg.File.Strip() - Cannot strip tags from a read only file.");
return false;
}
try {Mode = AccessMode.Write;}
catch
{
Debugger.Debug ("Mpeg.File.Strip() - Cannot strip tags from a read only file.");
return false;
}
if ((types & TagTypes.Id3v2) != 0)
{
long id3v2_location = FindId3v2 ();
if (id3v2_location >= 0)
{
Seek (id3v2_location);
Id3v2.Header header = new Id3v2.Header (ReadBlock ((int) Id3v2.Header.Size));
if (header.TagSize == 0)
Debugger.Debug ("Mpc.File.Save() -- Id3v2 header is broken. Ignoring.");
else
RemoveBlock (id3v2_location, (int) header.CompleteTagSize);
}
if (free_memory)
id3v2_tag = null;
}
long id3v1_location = FindId3v1 ();
if ((types & TagTypes.Id3v1) != 0)
{
if (id3v1_location >= 0)
{
Truncate (id3v1_location);
id3v1_location = -1;
}
if (free_memory)
id3v1_tag = null;
}
if ((types & TagTypes.Ape) != 0)
{
long ape_location = FindApe (id3v1_location >= 0);
if (ape_location != -1)
{
Seek (ape_location);
int ape_size = (int) (new Ape.Footer (ReadBlock ((int) Ape.Footer.Size))).CompleteTagSize;
ape_location = ape_location + Ape.Footer.Size - ape_size;
RemoveBlock (ape_location, ape_size);
}
if (free_memory)
ape_tag = null;
}
tag.SetTags (id3v2_tag, ape_tag, id3v1_tag);
Mode = original_mode;
return true;
}
public bool Strip (TagTypes types)
{
return Strip (types, true);
}
public bool Strip ()
{
return Strip (TagTypes.AllTags);
}
public long NextFrameOffset (long position)
{
ByteVector buffer = (byte) 0;
bool previous_partial_synch_match = false;
while (buffer.Count > 0)
{
Seek (position);
buffer = ReadBlock ((int) BufferSize);
if (previous_partial_synch_match && SecondSynchByte (buffer [0]))
return position - 1;
for (int i = 0; i < buffer.Count - 1; i++)
if (buffer [i] == 0xFF && SecondSynchByte (buffer [i + 1]))
return position + i;
if (buffer [buffer.Count - 1] == 0xFF)
previous_partial_synch_match = true;
position += BufferSize;
}
return -1;
}
public long PreviousFrameOffset (long position)
{
bool previous_partial_synch_match = false;
while (position - BufferSize > BufferSize)
{
position -= BufferSize;
Seek (position);
ByteVector buffer = ReadBlock ((int) BufferSize);
if (previous_partial_synch_match && SecondSynchByte (buffer [(int) BufferSize - 1]))
return position + BufferSize - 1;
// If the amount of data is smaller than an MPEG header (4 bytes) there's no
// chance of this being valid.
if (buffer.Count < 4)
return -1;
for (int i = buffer.Count - 2; i >= 0; i--)
if (buffer [i] == 0xFF && SecondSynchByte (buffer [i + 1]))
return position + i;
if (buffer [0] == 0xFF)
previous_partial_synch_match = true;
}
return -1;
}
//////////////////////////////////////////////////////////////////////////
// public properties
//////////////////////////////////////////////////////////////////////////
public long FirstFrameOffset
{
get
{
// This is kind of bad, because the ID3 tag could be anywhere (and
// under the old method, if it were an end tag there wouldn't be any
// room to find a frame. So this is just a "good guess".
return NextFrameOffset (id3v2_tag != null ? id3v2_tag.Header.CompleteTagSize : 0);
}
}
public long LastFrameOffset
{
get
{
long id3v1_location = FindId3v1 ();
return PreviousFrameOffset ((id3v1_location >= 0) ? id3v1_location - 1 : Length);
}
}
public override TagLib.Tag Tag {get {return tag;}}
public override AudioProperties AudioProperties {get {return properties;}}
//////////////////////////////////////////////////////////////////////////
// private methods
//////////////////////////////////////////////////////////////////////////
private void Read (Properties.ReadStyle properties_style)
{
// Look for an ID3v2 tag
long id3v2_location = FindId3v2 ();
if (id3v2_location >= 0)
id3v2_tag = new Id3v2.Tag (this, id3v2_location);
// Look for an ID3v1 tag
long id3v1_location = FindId3v1 ();
if (id3v1_location >= 0)
id3v1_tag = new Id3v1.Tag (this, id3v1_location);
// Look for an APE tag
long ape_location = FindApe (id3v1_location >= 0);
if (ape_location >= 0)
ape_tag = new Ape.Tag (this, ape_location);
tag.SetTags (id3v2_tag, ape_tag, id3v1_tag);
GetTag (TagTypes.Id3v2, true);
if (properties_style != Properties.ReadStyle.None)
properties = new Properties (this, properties_style);
}
private long FindId3v2 ()
{
// This method is based on the contents of TagLib::File::find(), but because
// of some subtlteies -- specifically the need to look for the bit pattern of
// an MPEG sync, it has been modified for use here.
if (IsValid && Id3v2.Header.FileIdentifier.Count <= BufferSize)
{
// The position in the file that the current buffer starts at.
long buffer_offset = 0;
ByteVector buffer;
// These variables are used to keep track of a partial match that happens at
// the end of a buffer.
int previous_partial_match = -1;
bool previous_partial_synch_match = false;
// Save the location of the current read pointer. We will restore the
// position using seek() before all returns.
long original_position = Tell;
// Start the search at the beginning of the file.
Seek (0);
// This loop is the crux of the find method. There are three cases that we
// want to account for:
// (1) The previously searched buffer contained a partial match of the search
// pattern and we want to see if the next one starts with the remainder of
// that pattern.
//
// (2) The search pattern is wholly contained within the current buffer.
//
// (3) The current buffer ends with a partial match of the pattern. We will
// note this for use in the next itteration, where we will check for the rest
// of the pattern.
for (buffer = ReadBlock ((int) BufferSize); buffer.Count > 0; buffer = ReadBlock ((int) BufferSize))
{
// (1) previous partial match
if(previous_partial_synch_match && SecondSynchByte (buffer [0]))
return -1;
if (previous_partial_match >= 0 && (int) BufferSize > previous_partial_match)
{
int pattern_offset = (int) (BufferSize - previous_partial_match);
if (buffer.ContainsAt (Id3v2.Header.FileIdentifier, 0, pattern_offset))
{
Seek (original_position);
return buffer_offset - BufferSize + previous_partial_match;
}
}
// (2) pattern contained in current buffer
long location = buffer.Find (Id3v2.Header.FileIdentifier);
if (location >= 0)
{
Seek (original_position);
return buffer_offset + location;
}
int first_synch_byte = buffer.Find ((byte) 0xFF);
// Here we have to loop because there could be several of the first
// (11111111) byte, and we want to check all such instances until we find
// a full match (11111111 111) or hit the end of the buffer.
while (first_synch_byte >= 0)
{
// if this *is not* at the end of the buffer
if (first_synch_byte < (int) buffer.Count - 1)
{
if (SecondSynchByte (buffer [first_synch_byte + 1]))
{
// We've found the frame synch pattern.
Seek (original_position);
return -1;
}
else
{
// We found 11111111 at the end of the current buffer indicating a
// partial match of the synch pattern. The find() below should
// return -1 and break out of the loop.
previous_partial_synch_match = true;
}
}
// Check in the rest of the buffer.
first_synch_byte = buffer.Find ((byte) 0xFF, first_synch_byte + 1);
}
// (3) partial match
previous_partial_match = buffer.EndsWithPartialMatch (Id3v2.Header.FileIdentifier);
buffer_offset += BufferSize;
}
Seek (original_position);
}
return -1;
}
private long FindId3v1 ()
{
if (IsValid)
{
Seek (-128, System.IO.SeekOrigin.End);
long p = Tell;
if (ReadBlock (3) == Id3v1.Tag.FileIdentifier)
return p;
}
return -1;
}
private long FindApe (bool has_id3v1)
{
if (IsValid)
{
if (has_id3v1)
Seek (-160, System.IO.SeekOrigin.End);
else
Seek (-32, System.IO.SeekOrigin.End);
long p = Tell;
if (ReadBlock (8) == Ape.Tag.FileIdentifier)
return p;
}
return -1;
}
private bool SecondSynchByte (byte b)
{
return b >= 0xE0;
}
}
}
|