Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeugma440 committed Feb 26, 2024
1 parent 98efc95 commit fb8c742
Show file tree
Hide file tree
Showing 26 changed files with 147 additions and 165 deletions.
4 changes: 2 additions & 2 deletions ATL/AudioData/IO/FLAC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ partial class FLAC : VorbisTagHolder, IMetaDataIO, IAudioDataIO
private const byte FLAG_LAST_METADATA_BLOCK = 0x80;


private FlacHeader header = null;
private FlacHeader header;

private AudioDataManager.SizeInfo sizeInfo;

Expand Down Expand Up @@ -467,7 +467,7 @@ private int writePictureBlock(BinaryWriter w, PictureInfo picture, bool isLastMe
w.Write(new byte[] { 0, 0, 0 }); // Placeholder for 24-bit integer that will be rewritten at the end of the method

var dataPos = w.BaseStream.Position;
vorbisTag.WritePicture(w, picture.PictureData, picture.MimeType, picture.PicType.Equals(PictureInfo.PIC_TYPE.Unsupported) ? picture.NativePicCode : ID3v2.EncodeID3v2PictureType(picture.PicType), picture.Description);
VorbisTag.WritePicture(w, picture.PictureData, picture.MimeType, picture.PicType.Equals(PictureInfo.PIC_TYPE.Unsupported) ? picture.NativePicCode : ID3v2.EncodeID3v2PictureType(picture.PicType), picture.Description);

var finalPos = w.BaseStream.Position;
w.BaseStream.Seek(sizePos, SeekOrigin.Begin);
Expand Down
2 changes: 1 addition & 1 deletion ATL/AudioData/IO/GYM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private bool readHeader(BufferedBinaryReader source, ReadTagParams readTagParams
}
}

private uint calculateDuration(BufferedBinaryReader source, uint loopStart, uint nbLoops)
private static uint calculateDuration(BufferedBinaryReader source, uint loopStart, uint nbLoops)
{
long streamSize = source.Length;
uint frameIndex = 0;
Expand Down
13 changes: 6 additions & 7 deletions ATL/AudioData/IO/Helpers/BextTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static class BextTag
/// </summary>
public const string CHUNK_BEXT = "bext";

private static readonly byte[] CR_LF = new byte[2] { 13, 10 };
private static readonly byte[] CR_LF = { 13, 10 };

/// <summary>
/// Read a bext chunk from the given source into the given Metadata I/O, using the given read parameters
Expand All @@ -28,12 +28,11 @@ internal static class BextTag
/// <param name="readTagParams">Read parameters to use</param>
public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams readTagParams)
{
string str;
byte[] data = new byte[256];

// Description
source.Read(data, 0, 256);
str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data).Trim());
var str = Utils.StripEndingZeroChars(Encoding.UTF8.GetString(data).Trim());
if (str.Length > 0) meta.SetMetaField("bext.description", str, readTagParams.ReadAllMetaFrames);

// Originator
Expand Down Expand Up @@ -147,7 +146,7 @@ public static int ToStream(BinaryWriter w, bool isLittleEndian, MetaDataIO meta)

// Text values
string description = Utils.ProtectValue(meta.GeneralDescription);
if (0 == description.Length && additionalFields.Keys.Contains("bext.description")) description = additionalFields["bext.description"];
if (0 == description.Length && additionalFields.TryGetValue("bext.description", out var field)) description = field;

WavHelper.writeFixedTextValue(description, 256, w);
WavHelper.writeFixedFieldTextValue("bext.originator", additionalFields, 32, w);
Expand All @@ -160,7 +159,7 @@ public static int ToStream(BinaryWriter w, bool isLittleEndian, MetaDataIO meta)
WavHelper.writeFieldIntValue("bext.version", additionalFields, w, (ushort)0);

// UMID
if (additionalFields.Keys.Contains("bext.UMID"))
if (additionalFields.ContainsKey("bext.UMID"))
{
if (Utils.IsHex(additionalFields["bext.UMID"]))
{
Expand Down Expand Up @@ -196,9 +195,9 @@ public static int ToStream(BinaryWriter w, bool isLittleEndian, MetaDataIO meta)

// CodingHistory
byte[] textData = Array.Empty<byte>();
if (additionalFields.Keys.Contains("bext.codingHistory"))
if (additionalFields.TryGetValue("bext.codingHistory", out var additionalField))
{
textData = Utils.Latin1Encoding.GetBytes(additionalFields["bext.codingHistory"]);
textData = Utils.Latin1Encoding.GetBytes(additionalField);
w.Write(textData);
}
w.Write(new byte[] { 13, 10 } /* CR LF */);
Expand Down
6 changes: 3 additions & 3 deletions ATL/AudioData/IO/Helpers/WavHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public static void readInt16(Stream source, MetaDataIO meta, string fieldName, b
/// <param name="paddingByte">Padding value to use (default : 0x00)</param>
public static void writeFixedFieldTextValue(string field, IDictionary<string, string> additionalFields, int length, BinaryWriter w, byte paddingByte = 0)
{
if (additionalFields.Keys.Contains(field))
if (additionalFields.TryGetValue(field, out var additionalField))
{
writeFixedTextValue(additionalFields[field], length, w, paddingByte);
writeFixedTextValue(additionalField, length, w, paddingByte);
}
else
{
Expand Down Expand Up @@ -186,7 +186,7 @@ public static void writeFieldIntValue(string field, IDictionary<string, string>
/// <param name="defaultValue">Default value to use in case no value is found in the given Map; type determines the type of the written value</param>
public static void writeField100DecimalValue(string field, IDictionary<string, string> additionalFields, BinaryWriter w, object defaultValue)
{
if (additionalFields.Keys.Contains(field))
if (additionalFields.ContainsKey(field))
{
if (Utils.IsNumeric(additionalFields[field]))
{
Expand Down
2 changes: 1 addition & 1 deletion ATL/AudioData/IO/Helpers/XmpTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static XmlArray createXmlArray()
|| e.Equals("RDF:SEQ", StringComparison.OrdinalIgnoreCase)
|| e.Equals("RDF:ALT", StringComparison.OrdinalIgnoreCase)
),
e => false
_ => false
);
result.setStructuralAttributes(ATTRIBUTES);
return result;
Expand Down
20 changes: 8 additions & 12 deletions ATL/AudioData/IO/MIDI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,10 @@ protected override bool read(Stream source, ReadTagParams readTagParams)

// Ready to read header data...
source.Read(buffer, 0, buffer.Length);
if ((buffer[0] != 0) ||
(buffer[1] != 0) ||
(buffer[2] != 0) ||
(buffer[3] != 6)
)
if (buffer[0] != 0 ||
buffer[1] != 0 ||
buffer[2] != 0 ||
buffer[3] != 6)
{
LogDelegator.GetLogDelegate()(Log.LV_ERROR, "Wrong MIDI header");
return false;
Expand All @@ -469,7 +468,6 @@ protected override bool read(Stream source, ReadTagParams readTagParams)

tempo = 0; // maybe (hopefully!) overwritten by parseTrack

int trackSize;
int nbTrack = 0;
comment = new StringBuilder("");

Expand All @@ -491,7 +489,7 @@ protected override bool read(Stream source, ReadTagParams readTagParams)

// trackSize is stored in big endian -> needs inverting
source.Read(buffer, 0, 4);
trackSize = StreamUtils.DecodeBEInt32(buffer);
var trackSize = StreamUtils.DecodeBEInt32(buffer);

byte[] trackData = new byte[trackSize];
source.Read(trackData, 0, trackSize);
Expand Down Expand Up @@ -954,15 +952,13 @@ private MidiTrack parseTrack(byte[] data, int trackNumber)
// variable length string to int (+repositioning)
//---------------------------------------------------------------
//# TO LOOK AFTER CAREFULLY <.<
private int readVarLen(ref byte[] data, ref int pos)
private static int readVarLen(ref byte[] data, ref int pos)
{
int value;
int c;

value = data[pos++];
int value = data[pos++];
if ((value & 0x80) != 0)
{
value &= 0x7F;
int c;
do
{
c = data[pos++];
Expand Down
14 changes: 7 additions & 7 deletions ATL/AudioData/IO/MP4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using static ATL.AudioData.FileStructureHelper;
using System.Linq;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Xml;
using static ATL.TagData;

namespace ATL.AudioData.IO
Expand Down Expand Up @@ -1423,7 +1421,7 @@ private void readTag(BinaryReader source, ReadTagParams readTagParams)
{
var picturePosition = takePicturePosition(picType);

int dataSize = (int)Math.Max(0, pictureSize - 16);
int dataSize = Math.Max(0, (int)pictureSize - 16);
if (readTagParams.ReadPictures)
{
PictureInfo picInfo = PictureInfo.fromBinaryData(source.BaseStream, dataSize, picType, getImplementedTagType(), dataClass, picturePosition);
Expand Down Expand Up @@ -1522,11 +1520,13 @@ private void setXtraField(string ID, string data, bool readAllMetaFrames)
}
}

private Uuid readUuid(Stream source)
private static Uuid readUuid(Stream source)
{
Uuid result = new Uuid();
result.size = navigateToAtom(source, "uuid");
result.position = source.Position - 8;
Uuid result = new Uuid
{
size = navigateToAtom(source, "uuid"),
position = source.Position - 8
};
if (result.size >= 16 + 8)
{
Span<byte> key = new byte[16];
Expand Down
2 changes: 1 addition & 1 deletion ATL/AudioData/IO/MPEGaudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ private double getDuration()
return 0;
}

private ChannelsArrangement getChannelsArrangement(FrameHeader frame)
private static ChannelsArrangement getChannelsArrangement(FrameHeader frame)
{
switch (frame.ModeID)
{
Expand Down
12 changes: 6 additions & 6 deletions ATL/AudioData/IO/Ogg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private sealed class VorbisHeader

public void Reset()
{
ID = new byte[0];
ID = Array.Empty<byte>();
Array.Clear(BitstreamVersion, 0, BitstreamVersion.Length);
ChannelMode = 0;
SampleRate = 0;
Expand Down Expand Up @@ -245,7 +245,7 @@ private sealed class OpusHeader

public void Reset()
{
ID = new byte[0];
ID = Array.Empty<byte>();
Version = 0;
OutputChannelCount = 0;
PreSkip = 0;
Expand Down Expand Up @@ -364,7 +364,7 @@ public override IList<Format> MetadataFormats
// ---------------------------------------------------------------------------

// Read total samples of OGG file, which are located on the very last page of the file
private ulong getSamples(BufferedBinaryReader source)
private static ulong getSamples(BufferedBinaryReader source)
{
byte typeFlag;
byte[] lacingValues = new byte[255];
Expand Down Expand Up @@ -912,7 +912,7 @@ private byte[] buildSegmentsTable(long newTagSize, int commentsHeader_nbSegments
}

// Resize the whole in-memory stream once and for all to avoid multiple reallocations while repaging
private void resizeMemStream(Stream memStream, int commentsHeader_nbSegments, int setupHeader_nbSegments)
private static void resizeMemStream(Stream memStream, int commentsHeader_nbSegments, int setupHeader_nbSegments)
{
int nbPageHeaders = (int)Math.Ceiling((commentsHeader_nbSegments + setupHeader_nbSegments) / 255.0);
int totalPageHeadersSize = (nbPageHeaders * 27) + commentsHeader_nbSegments + setupHeader_nbSegments;
Expand Down Expand Up @@ -976,7 +976,7 @@ private int repageMemStream(
}

// Generate CRC32 of created pages
private void generatePageCrc32(Stream s, IList<KeyValuePair<long, int>> pageHeaderOffsets)
private static void generatePageCrc32(Stream s, IEnumerable<KeyValuePair<long, int>> pageHeaderOffsets)
{
byte[] data = Array.Empty<byte>();
foreach (KeyValuePair<long, int> kv in pageHeaderOffsets)
Expand All @@ -991,7 +991,7 @@ private void generatePageCrc32(Stream s, IList<KeyValuePair<long, int>> pageHead
}
}

private bool renumberRemainingPages(Stream s, long nextPageOffset, int writtenPages)
private static bool renumberRemainingPages(Stream s, long nextPageOffset, int writtenPages)
{
OggPageHeader header = new OggPageHeader();
byte[] data = Array.Empty<byte>();
Expand Down
39 changes: 19 additions & 20 deletions ATL/AudioData/IO/PSF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public static bool IsValidHeader(byte[] data)
return StreamUtils.ArrBeginsWith(data, PSF_FORMAT_TAG);
}

private bool readHeader(Stream source, ref PSFHeader header)
private static bool readHeader(Stream source, ref PSFHeader header)
{
byte[] buffer = new byte[4];
source.Read(header.FormatTag, 0, 3);
Expand All @@ -208,7 +208,7 @@ private bool readHeader(Stream source, ref PSFHeader header)
}
}

private string readPSFLine(Stream source, Encoding encoding)
private static string readPSFLine(Stream source, Encoding encoding)
{
long lineStart = source.Position;
long lineEnd;
Expand Down Expand Up @@ -301,7 +301,7 @@ private bool readTag(Stream source, ref PSFTag tag, ReadTagParams readTagParams)
}
}

private double parsePSFDuration(string durationStr)
private static double parsePSFDuration(string durationStr)
{
string hStr = "";
string mStr = "";
Expand All @@ -310,49 +310,49 @@ private double parsePSFDuration(string durationStr)
double result = 0;

// decimal
var sepIndex = durationStr.LastIndexOf(".");
if (-1 == sepIndex) sepIndex = durationStr.LastIndexOf(",");
var sepIndex = durationStr.LastIndexOf('.');
if (-1 == sepIndex) sepIndex = durationStr.LastIndexOf(',');

if (-1 != sepIndex)
{
sepIndex++;
dStr = durationStr.Substring(sepIndex, durationStr.Length - sepIndex);
durationStr = durationStr.Substring(0, Math.Max(0, sepIndex - 1));
durationStr = durationStr[..Math.Max(0, sepIndex - 1)];
}


// seconds
sepIndex = durationStr.LastIndexOf(":");
sepIndex = durationStr.LastIndexOf(':');

sepIndex++;
sStr = durationStr.Substring(sepIndex, durationStr.Length - sepIndex);

durationStr = durationStr.Substring(0, Math.Max(0, sepIndex - 1));
durationStr = durationStr[..Math.Max(0, sepIndex - 1)];

// minutes
if (durationStr.Length > 0)
{
sepIndex = durationStr.LastIndexOf(":");
sepIndex = durationStr.LastIndexOf(':');

sepIndex++;
mStr = durationStr.Substring(sepIndex, durationStr.Length - sepIndex);

durationStr = durationStr.Substring(0, Math.Max(0, sepIndex - 1));
durationStr = durationStr[..Math.Max(0, sepIndex - 1)];
}

// hours
if (durationStr.Length > 0)
{
sepIndex = durationStr.LastIndexOf(":");
sepIndex = durationStr.LastIndexOf(':');

sepIndex++;
hStr = durationStr.Substring(sepIndex, durationStr.Length - sepIndex);
}

if (dStr != "") result = result + (Int32.Parse(dStr) * 100);
if (sStr != "") result = result + (Int32.Parse(sStr) * 1000);
if (mStr != "") result = result + (Int32.Parse(mStr) * 60000);
if (hStr != "") result = result + (Int32.Parse(hStr) * 3600000);
if (dStr != "") result += (int.Parse(dStr) * 100);
if (sStr != "") result += (int.Parse(sStr) * 1000);
if (mStr != "") result += (int.Parse(mStr) * 60000);
if (hStr != "") result += (int.Parse(hStr) * 3600000);

return result;
}
Expand All @@ -368,7 +368,6 @@ public bool Read(Stream source, SizeInfo sizeInfo, ReadTagParams readTagParams)

protected override bool read(Stream source, ReadTagParams readTagParams)
{
bool result = true;
PSFHeader header = new PSFHeader();
PSFTag tag = new PSFTag();

Expand All @@ -394,7 +393,7 @@ protected override bool read(Stream source, ReadTagParams readTagParams)
version = header.VersionByte;
BitRate = AudioDataSize * 8 / Duration;

return result;
return true;
}

protected override int write(TagData tag, Stream s, string zone)
Expand Down Expand Up @@ -447,7 +446,7 @@ private int write(TagData tag, BinaryWriter w)
return result;
}

private void writeTextFrame(BinaryWriter writer, string frameCode, string text)
private static void writeTextFrame(BinaryWriter writer, string frameCode, string text)
{
string[] textLines;
if (text.Contains(Environment.NewLine))
Expand All @@ -457,7 +456,7 @@ private void writeTextFrame(BinaryWriter writer, string frameCode, string text)
}
else
{
textLines = new string[1] { text };
textLines = new[] { text };
}

foreach (string s in textLines)
Expand Down Expand Up @@ -492,7 +491,7 @@ private TagData prepareRemove()
foreach (MetaFieldInfo fieldInfo in GetAdditionalFields())
{
var fieldCode = fieldInfo.NativeFieldCode.ToLower();
if (!fieldCode.StartsWith("_") && !playbackFrames.Contains(fieldCode))
if (!fieldCode.StartsWith('_') && !playbackFrames.Contains(fieldCode))
{
MetaFieldInfo emptyFieldInfo = new MetaFieldInfo(fieldInfo);
emptyFieldInfo.MarkedForDeletion = true;
Expand Down
Loading

0 comments on commit fb8c742

Please sign in to comment.