diff --git a/ATL.unit-test/IO/MetaData/Vorbis_OGG.cs b/ATL.unit-test/IO/MetaData/Vorbis_OGG.cs index e1748e88..932eb4b0 100644 --- a/ATL.unit-test/IO/MetaData/Vorbis_OGG.cs +++ b/ATL.unit-test/IO/MetaData/Vorbis_OGG.cs @@ -83,7 +83,7 @@ public void TagIO_R_VorbisOGG_simple_OnePager(Stream stream) } else { - theFile = new AudioDataManager(AudioDataIOFactory.GetInstance().GetFromMimeType("audio/ogg", "In-memory"), stream); + theFile = new AudioDataManager(AudioDataIOFactory.GetInstance().GetFromMimeType("audio/ogg", AudioDataIOFactory.IN_MEMORY), stream); } readExistingTagsOnFile(theFile); @@ -256,7 +256,7 @@ public void TagIO_RW_VorbisOGG_Empty(Stream stream) testFileLocation = ""; streamCopy = new MemoryStream(); stream.CopyTo(streamCopy); - theFile = new AudioDataManager(AudioDataIOFactory.GetInstance().GetFromMimeType(".ogg", "In-memory"), stream); + theFile = new AudioDataManager(AudioDataIOFactory.GetInstance().GetFromMimeType(".ogg", AudioDataIOFactory.IN_MEMORY), stream); } diff --git a/ATL/AudioData/AudioDataIOFactory.cs b/ATL/AudioData/AudioDataIOFactory.cs index 461e02c4..dac85445 100644 --- a/ATL/AudioData/AudioDataIOFactory.cs +++ b/ATL/AudioData/AudioDataIOFactory.cs @@ -39,6 +39,8 @@ public class AudioDataIOFactory : Factory /// public const int MAX_ALTERNATES = 2; + internal const string IN_MEMORY = "in-memory"; + // The instance of this factory private static AudioDataIOFactory theFactory = null; @@ -412,7 +414,7 @@ public IAudioDataIO GetFromStream(Stream s) s.Seek(0, SeekOrigin.Begin); byte[] data = new byte[32]; long offset = 0; - if (s.Read(data, 0, 32) < 32) return getFromFormat("in-memory", new AudioFormat(Format.UNKNOWN_FORMAT)); + if (s.Read(data, 0, 32) < 32) return getFromFormat(IN_MEMORY, new AudioFormat(Format.UNKNOWN_FORMAT)); // Hardcoded case of ID3v2 as it is the sole standard metadata system to appear at the beginning of file if (ID3v2.IsValidHeader(data)) { @@ -421,22 +423,22 @@ public IAudioDataIO GetFromStream(Stream s) int id3v2Size = StreamUtils.DecodeSynchSafeInt32(data2) + 10; // 10 being the size of the header s.Seek(id3v2Size, SeekOrigin.Begin); offset = s.Position; - if (s.Read(data, 0, 32) < 32) return getFromFormat("in-memory", new AudioFormat(Format.UNKNOWN_FORMAT)); + if (s.Read(data, 0, 32) < 32) return getFromFormat(IN_MEMORY, new AudioFormat(Format.UNKNOWN_FORMAT)); } try { List expensiveFormats = new List(); foreach (AudioFormat f in getFormats()) { - if (f.CheckHeader != null && f.CheckHeader(data)) return getFromFormat("in-memory", f); + if (f.CheckHeader != null && f.CheckHeader(data)) return getFromFormat(IN_MEMORY, f); if (f.SearchHeader != null) expensiveFormats.Add(f); } foreach (AudioFormat f in expensiveFormats) { s.Seek(offset, SeekOrigin.Begin); - if (f.SearchHeader(s)) return getFromFormat("in-memory", f); + if (f.SearchHeader(s)) return getFromFormat(IN_MEMORY, f); } - return getFromFormat("in-memory", new AudioFormat(Format.UNKNOWN_FORMAT)); + return getFromFormat(IN_MEMORY, new AudioFormat(Format.UNKNOWN_FORMAT)); } finally { diff --git a/ATL/AudioData/AudioFileIO.cs b/ATL/AudioData/AudioFileIO.cs index 8491bbf5..f0ac0cb7 100644 --- a/ATL/AudioData/AudioFileIO.cs +++ b/ATL/AudioData/AudioFileIO.cs @@ -74,7 +74,7 @@ public AudioFileIO(Stream stream, string mimeType, bool readEmbeddedPictures, bo bool found = false; while (!found && alternate < AudioDataIOFactory.MAX_ALTERNATES) { - audioData = mimeType.Length > 0 ? AudioDataIOFactory.GetInstance().GetFromMimeType(mimeType, "In-memory", alternate) : AudioDataIOFactory.GetInstance().GetFromStream(stream); + audioData = mimeType.Length > 0 ? AudioDataIOFactory.GetInstance().GetFromMimeType(mimeType, AudioDataIOFactory.IN_MEMORY, alternate) : AudioDataIOFactory.GetInstance().GetFromStream(stream); audioManager = new AudioDataManager(audioData, stream); found = audioManager.ReadFromFile(readEmbeddedPictures, readAllMetaFrames); alternate++; diff --git a/ATL/AudioData/IO/Helpers/BextTag.cs b/ATL/AudioData/IO/Helpers/BextTag.cs index 693fa7ab..5e709588 100644 --- a/ATL/AudioData/IO/Helpers/BextTag.cs +++ b/ATL/AudioData/IO/Helpers/BextTag.cs @@ -31,29 +31,19 @@ public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams read byte[] data = new byte[256]; // Description - if (source.Read(data, 0, 256) < 256) return; - var str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data).Trim()); - if (str.Length > 0) meta.SetMetaField("bext.description", str, readTagParams.ReadAllMetaFrames); + utf8FromStream(source, 256, data, meta, "bext.description", readTagParams.ReadAllMetaFrames); // Originator - if (source.Read(data, 0, 32) < 32) return; - str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data, 0, 32).Trim()); - if (str.Length > 0) meta.SetMetaField("bext.originator", str, readTagParams.ReadAllMetaFrames); + utf8FromStream(source, 32, data, meta, "bext.originator", readTagParams.ReadAllMetaFrames); // OriginatorReference - if (source.Read(data, 0, 32) < 32) return; - str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data, 0, 32).Trim()); - if (str.Length > 0) meta.SetMetaField("bext.originatorReference", str, readTagParams.ReadAllMetaFrames); + utf8FromStream(source, 32, data, meta, "bext.originatorReference", readTagParams.ReadAllMetaFrames); // OriginationDate - if (source.Read(data, 0, 10) < 10) return; - str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data, 0, 10).Trim()); - if (str.Length > 0) meta.SetMetaField("bext.originationDate", str, readTagParams.ReadAllMetaFrames); + utf8FromStream(source, 10, data, meta, "bext.originationDate", readTagParams.ReadAllMetaFrames); // OriginationTime - if (source.Read(data, 0, 8) < 8) return; - str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data, 0, 8).Trim()); - if (str.Length > 0) meta.SetMetaField("bext.originationTime", str, readTagParams.ReadAllMetaFrames); + utf8FromStream(source, 8, data, meta, "bext.originationTime", readTagParams.ReadAllMetaFrames); // TimeReference if (source.Read(data, 0, 8) < 8) return; @@ -75,29 +65,19 @@ public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams read meta.SetMetaField("bext.UMID", sbr.ToString(), readTagParams.ReadAllMetaFrames); // LoudnessValue - if (source.Read(data, 0, 2) < 2) return; - intData = StreamUtils.DecodeInt16(data); - meta.SetMetaField("bext.loudnessValue", (intData / 100.0).ToString(), readTagParams.ReadAllMetaFrames); + percent16FromStream(source, data, meta, "bext.loudnessValue", readTagParams.ReadAllMetaFrames); // LoudnessRange - if (source.Read(data, 0, 2) < 2) return; - intData = StreamUtils.DecodeInt16(data); - meta.SetMetaField("bext.loudnessRange", (intData / 100.0).ToString(), readTagParams.ReadAllMetaFrames); + percent16FromStream(source, data, meta, "bext.loudnessRange", readTagParams.ReadAllMetaFrames); // MaxTruePeakLevel - if (source.Read(data, 0, 2) < 2) return; - intData = StreamUtils.DecodeInt16(data); - meta.SetMetaField("bext.maxTruePeakLevel", (intData / 100.0).ToString(), readTagParams.ReadAllMetaFrames); + percent16FromStream(source, data, meta, "bext.maxTruePeakLevel", readTagParams.ReadAllMetaFrames); // MaxMomentaryLoudness - if (source.Read(data, 0, 2) < 2) return; - intData = StreamUtils.DecodeInt16(data); - meta.SetMetaField("bext.maxMomentaryLoudness", (intData / 100.0).ToString(), readTagParams.ReadAllMetaFrames); + percent16FromStream(source, data, meta, "bext.maxMomentaryLoudness", readTagParams.ReadAllMetaFrames); // MaxShortTermLoudness - if (source.Read(data, 0, 2) < 2) return; - intData = StreamUtils.DecodeInt16(data); - meta.SetMetaField("bext.maxShortTermLoudness", (intData / 100.0).ToString(), readTagParams.ReadAllMetaFrames); + percent16FromStream(source, data, meta, "bext.maxShortTermLoudness", readTagParams.ReadAllMetaFrames); // Reserved source.Seek(180, SeekOrigin.Current); @@ -113,11 +93,36 @@ public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams read if (data.Length < size) data = new byte[size]; if (source.Read(data, 0, size) < size) return; - str = Utils.StripEndingZeroChars(Utils.Latin1Encoding.GetString(data, 0, (int)(endPos - initialPos)).Trim()); + string str = Utils.StripEndingZeroChars(Utils.Latin1Encoding.GetString(data, 0, (int)(endPos - initialPos)).Trim()); if (str.Length > 0) meta.SetMetaField("bext.codingHistory", str, readTagParams.ReadAllMetaFrames); } } + private static void utf8FromStream( + Stream source, + int size, + byte[] buffer, + MetaDataIO meta, + string field, + bool readAllFrames) + { + if (source.Read(buffer, 0, size) < size) return; + var str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(buffer, 0, size).Trim()); + if (str.Length > 0) meta.SetMetaField(field, str, readAllFrames); + } + + private static void percent16FromStream( + Stream source, + byte[] buffer, + MetaDataIO meta, + string field, + bool readAllFrames) + { + if (source.Read(buffer, 0, 2) < 2) return; + var intData = StreamUtils.DecodeInt16(buffer); + meta.SetMetaField(field, (intData / 100.0).ToString(), readAllFrames); + } + /// /// Indicate whether the given Metadata I/O contains metadata relevant to the Bext format /// diff --git a/ATL/Entities/Track.cs b/ATL/Entities/Track.cs index 7b43098a..1f2b4bcb 100644 --- a/ATL/Entities/Track.cs +++ b/ATL/Entities/Track.cs @@ -18,8 +18,6 @@ namespace ATL /// public class Track { - private const string InMemoryPath = "In-memory"; - /// /// Basic constructor; does nothing else than instanciating the Track object /// @@ -47,7 +45,7 @@ public Track(Stream stream, string mimeType = "") { this.stream = stream; this.mimeType = mimeType; - Path = InMemoryPath; + Path = AudioDataIOFactory.IN_MEMORY; Update(); } @@ -451,7 +449,7 @@ protected void Update(bool onlyReadEmbeddedPictures = false) initialEmbeddedPictures.Clear(); Title = processString(metadata.Title); - if (Settings.UseFileNameWhenNoTitle && string.IsNullOrEmpty(Title) && Path != InMemoryPath) + if (Settings.UseFileNameWhenNoTitle && string.IsNullOrEmpty(Title) && Path != AudioDataIOFactory.IN_MEMORY) { Title = System.IO.Path.GetFileNameWithoutExtension(Path); } @@ -752,7 +750,7 @@ public bool SaveTo(Stream target, Action writeProgress = null) if (result) { this.stream = target; - this.Path = InMemoryPath; + this.Path = AudioDataIOFactory.IN_MEMORY; Update(); } @@ -835,7 +833,7 @@ public async Task SaveToAsync(Stream target, Action writeProgress = if (result) { this.stream = target; - this.Path = InMemoryPath; + this.Path = AudioDataIOFactory.IN_MEMORY; Update(); }