Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeugma440 committed Nov 8, 2024
1 parent 97c1842 commit f1c056b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 45 deletions.
4 changes: 2 additions & 2 deletions ATL.unit-test/IO/MetaData/Vorbis_OGG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}


Expand Down
12 changes: 7 additions & 5 deletions ATL/AudioData/AudioDataIOFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class AudioDataIOFactory : Factory<AudioFormat>
/// </summary>
public const int MAX_ALTERNATES = 2;

internal const string IN_MEMORY = "in-memory";

// The instance of this factory
private static AudioDataIOFactory theFactory = null;

Expand Down Expand Up @@ -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))
{
Expand All @@ -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<AudioFormat> expensiveFormats = new List<AudioFormat>();
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
{
Expand Down
2 changes: 1 addition & 1 deletion ATL/AudioData/AudioFileIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
67 changes: 36 additions & 31 deletions ATL/AudioData/IO/Helpers/BextTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}

/// <summary>
/// Indicate whether the given Metadata I/O contains metadata relevant to the Bext format
/// </summary>
Expand Down
10 changes: 4 additions & 6 deletions ATL/Entities/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ namespace ATL
/// </summary>
public class Track
{
private const string InMemoryPath = "In-memory";

/// <summary>
/// Basic constructor; does nothing else than instanciating the Track object
/// </summary>
Expand Down Expand Up @@ -47,7 +45,7 @@ public Track(Stream stream, string mimeType = "")
{
this.stream = stream;
this.mimeType = mimeType;
Path = InMemoryPath;
Path = AudioDataIOFactory.IN_MEMORY;
Update();
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -752,7 +750,7 @@ public bool SaveTo(Stream target, Action<float> writeProgress = null)
if (result)
{
this.stream = target;
this.Path = InMemoryPath;
this.Path = AudioDataIOFactory.IN_MEMORY;
Update();
}

Expand Down Expand Up @@ -835,7 +833,7 @@ public async Task<bool> SaveToAsync(Stream target, Action<float> writeProgress =
if (result)
{
this.stream = target;
this.Path = InMemoryPath;
this.Path = AudioDataIOFactory.IN_MEMORY;
Update();
}

Expand Down

0 comments on commit f1c056b

Please sign in to comment.