Skip to content

Commit

Permalink
Merge pull request #32 from dimhotepus/automated-code-cleanup
Browse files Browse the repository at this point in the history
Run automated code cleanup
  • Loading branch information
juliusfriedman committed Nov 25, 2023
2 parents 3b6f122 + 0691e11 commit 7582c40
Show file tree
Hide file tree
Showing 334 changed files with 6,568 additions and 7,950 deletions.
35 changes: 11 additions & 24 deletions Codecs/Audio/AC3/BitStreamInformation.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Media.Codecs.Audio.Ac3
{
Expand All @@ -19,27 +15,18 @@ public static int GetNumberOfChannels(byte acMod, bool lfeOn)
/// </summary>
public static int GetNumberOfFullRangeChannels(byte acMod)
{
switch (acMod)
return acMod switch
{
case 0:
return 2;
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
case 4:
return 3;
case 5:
return 4;
case 6:
return 4;
case 7:
return 5;
default:
throw new ArgumentException("Invalid audio coding mode");
}
0 => 2,
1 => 1,
2 => 2,
3 => 3,
4 => 3,
5 => 4,
6 => 4,
7 => 5,
_ => throw new ArgumentException("Invalid audio coding mode"),
};
}

}
Expand Down
2 changes: 0 additions & 2 deletions Codecs/Audio/AC3/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
Expand Down
8 changes: 1 addition & 7 deletions Codecs/Audio/AC3/SyncFrame.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Media.Codecs.Audio.Ac3
namespace Media.Codecs.Audio.Ac3
{
public class SyncFrame
{
Expand Down
90 changes: 33 additions & 57 deletions Codecs/Audio/AC3/SyncInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

using System;
using System;

namespace Media.Codecs.Audio.Ac3
{
public class SyncInfo
Expand All @@ -14,68 +14,44 @@ public class SyncInfo

public static byte GetSampleRateCode(int sampleRate)
{
switch (sampleRate)
return sampleRate switch
{
case 48000:
return 0;
case 44100:
return 1;
case 32000:
return 2;
default:
throw new ArgumentException("Invalid sample rate");
}
48000 => 0,
44100 => 1,
32000 => 2,
_ => throw new ArgumentException("Invalid sample rate"),
};
}

/// <returns>Bitrate in kbit/s</returns>
public static int GetBitRate(byte frameSizeCode)
{
switch (frameSizeCode / 2)
return (frameSizeCode / 2) switch
{
case 0:
return 32;
case 1:
return 40;
case 2:
return 48;
case 3:
return 56;
case 4:
return 64;
case 5:
return 80;
case 6:
return 96;
case 7:
return 112;
case 8:
return 128;
case 9:
return 160;
case 10:
return 192;
case 11:
return 224;
case 12:
return 256;
case 13:
return 320;
case 14:
return 384;
case 15:
return 448;
case 16:
return 512;
case 17:
return 576;
case 18:
return 640;
default:
throw new ArgumentException("Invalid frame size code");
}
0 => 32,
1 => 40,
2 => 48,
3 => 56,
4 => 64,
5 => 80,
6 => 96,
7 => 112,
8 => 128,
9 => 160,
10 => 192,
11 => 224,
12 => 256,
13 => 320,
14 => 384,
15 => 448,
16 => 512,
17 => 576,
18 => 640,
_ => throw new ArgumentException("Invalid frame size code"),
};
}

static int[,] frameSizeCodeTable = new int[,]
private static readonly int[,] frameSizeCodeTable = new int[,]
{
{96, 69, 64},
{96, 70, 64},
Expand Down Expand Up @@ -133,9 +109,9 @@ public static int GetFrameSize(byte sampleRateCode, byte frameSizeCode)
{
throw new ArgumentException("Invalid frame size code");
}

int sampleRateIndex = 2 - sampleRateCode;
//*2
//*2
return frameSizeCodeTable[frameSizeCode, sampleRateIndex] << 1;
}
}
Expand Down
1 change: 0 additions & 1 deletion Codecs/Audio/Aac/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down
8 changes: 1 addition & 7 deletions Codecs/Audio/Alaw/ALawCodec.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Media.Codecs.Audio.Alaw
namespace Media.Codecs.Audio.Alaw
{
public class ALawCodec : /*: Media.Codec.Codec,*/ IAudioCodec
{
Expand Down
2 changes: 1 addition & 1 deletion Codecs/Audio/Alaw/ALawEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static byte LinearToALawSample(short sample)
sample = cClip;
if (sample >= 256)
{
exponent = (int)ALawCompressTable[(sample >> 8) & 0x7F];
exponent = ALawCompressTable[(sample >> 8) & 0x7F];
mantissa = (sample >> (exponent + 3)) & 0x0F;
compressedByte = (byte)((exponent << 4) | mantissa);
}
Expand Down
1 change: 0 additions & 1 deletion Codecs/Audio/Alaw/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down
36 changes: 16 additions & 20 deletions Codecs/Audio/AudioBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
using Media.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace Media.Codecs.Audio
{
Expand Down Expand Up @@ -43,7 +40,7 @@ public class AudioBuffer : Media.Codec.MediaBuffer
/// <param name="sampleRate">The rate at which the audio will be played each second (hZ)</param>
/// <param name="bitsPerComponent">The amount of bits </param>
/// <returns>The amount of bytes required.</returns>
static int CalculateSize(int numberOfSamples, int channels, int sampleRate, int bitsPerComponent)
private static int CalculateSize(int numberOfSamples, int channels, int sampleRate, int bitsPerComponent)
{
return Math.Abs(numberOfSamples * (sampleRate / (Common.Binary.BitsToBytes(bitsPerComponent) * channels)));
}
Expand Down Expand Up @@ -86,7 +83,7 @@ public AudioBuffer(AudioFormat audioFormat, int numberOfSamples = 1, bool should
/// <summary>
/// The number of different speakers for which data can be found in the sample.
/// </summary>
public int Channels { get { return MediaFormat.Components.Length ; } }
public int Channels { get { return MediaFormat.Components.Length; } }

/// <summary>
/// Indicates if the sample contains data for only 1 speaker.
Expand Down Expand Up @@ -216,10 +213,9 @@ public MemorySegment GetSampleData(int sampleIndex, int channel)
int bytesPerSample = AudioFormat.Length;
int offset = CalculateSampleDataOffset(sampleIndex, channel);

if (offset + bytesPerSample > Data.Count)
throw new ArgumentException("The requested sample data is outside the bounds of the buffer.");

return new MemorySegment(Data.Array, Data.Offset + offset, bytesPerSample);
return offset + bytesPerSample > Data.Count
? throw new ArgumentException("The requested sample data is outside the bounds of the buffer.")
: new MemorySegment(Data.Array, Data.Offset + offset, bytesPerSample);
}

#endregion
Expand All @@ -236,12 +232,12 @@ internal class AudioUnitTests
public static void Test_AudioFormat_AudioBuffer_Constructor()
{
//Construct a new AudioFormat with one component, sampled at 8000hz, all samples are signed 8 bit and in little endian order.
Media.Codecs.Audio.AudioFormat audioFormat = new Codecs.Audio.AudioFormat(8000, true, Common.Binary.ByteOrder.Little, Media.Codec.DataLayout.Packed, new Media.Codec.MediaComponent[]{
new Media.Codec.MediaComponent(0, 8)
Media.Codecs.Audio.AudioFormat audioFormat = new(8000, true, Common.Binary.ByteOrder.Little, Media.Codec.DataLayout.Packed, new Media.Codec.MediaComponent[]{
new(0, 8)
});

//Could be given in place to the constructor.
using (Media.Codecs.Audio.AudioBuffer audio = new Codecs.Audio.AudioBuffer(audioFormat))
using (Media.Codecs.Audio.AudioBuffer audio = new(audioFormat))
{
if (audio.Channels != 1) throw new System.InvalidOperationException();

Expand All @@ -254,19 +250,19 @@ public static void Test_AudioFormat_AudioBuffer_Constructor()
public static void Test_AudioTransformer()
{
//Construct a new AudioFormat with one component, sampled at 8000hz, all samples are signed 8 bit and in little endian order.
Media.Codecs.Audio.AudioFormat audioFormat = new Codecs.Audio.AudioFormat(8000, true, Common.Binary.ByteOrder.Little, Media.Codec.DataLayout.Packed, new Media.Codec.MediaComponent[]{
new Media.Codec.MediaComponent(0, 8)
Media.Codecs.Audio.AudioFormat audioFormat = new(8000, true, Common.Binary.ByteOrder.Little, Media.Codec.DataLayout.Packed, new Media.Codec.MediaComponent[]{
new(0, 8)
});

//Could be given in place to the constructor.
using (Media.Codecs.Audio.AudioBuffer source = new Codecs.Audio.AudioBuffer(audioFormat))
using (Media.Codecs.Audio.AudioBuffer source = new(audioFormat))
{
//Example for upsampling by a factor of 2
int sampleFactor = 2;

using (Media.Codecs.Audio.AudioBuffer destination = new Codecs.Audio.AudioBuffer(new Codecs.Audio.AudioFormat(source.SampleRate * sampleFactor, source.AudioFormat.IsSigned, source.AudioFormat.ByteOrder, source.DataLayout, audioFormat.Components)))
using (Media.Codecs.Audio.AudioBuffer destination = new(new Codecs.Audio.AudioFormat(source.SampleRate * sampleFactor, source.AudioFormat.IsSigned, source.AudioFormat.ByteOrder, source.DataLayout, audioFormat.Components)))
{
using (Media.Codecs.Audio.AudioTransformer audioTransformer = new Codecs.Audio.AudioTransformer(source, destination, sampleFactor, TransformationQuality.None))
using (Media.Codecs.Audio.AudioTransformer audioTransformer = new(source, destination, sampleFactor, TransformationQuality.None))
{
audioTransformer.Transform();
}
Expand All @@ -275,14 +271,14 @@ public static void Test_AudioTransformer()
//Example for downsample by a factor of 2
sampleFactor = -2;

using (Media.Codecs.Audio.AudioBuffer destination = new Codecs.Audio.AudioBuffer(new Codecs.Audio.AudioFormat(Math.Abs(source.SampleRate * sampleFactor), source.AudioFormat.IsSigned, source.AudioFormat.ByteOrder, source.DataLayout, audioFormat.Components)))
using (Media.Codecs.Audio.AudioBuffer destination = new(new Codecs.Audio.AudioFormat(Math.Abs(source.SampleRate * sampleFactor), source.AudioFormat.IsSigned, source.AudioFormat.ByteOrder, source.DataLayout, audioFormat.Components)))
{
using (Media.Codecs.Audio.AudioTransformer audioTransformer = new Codecs.Audio.AudioTransformer(source, destination, sampleFactor, TransformationQuality.None))
using (Media.Codecs.Audio.AudioTransformer audioTransformer = new(source, destination, sampleFactor, TransformationQuality.None))
{
audioTransformer.Transform();
}
}
}
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion Codecs/Audio/AudioCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class AudioCodec : Media.Codec.Codec, IAudioCodec
public AudioCodec(string name, Common.Binary.ByteOrder defaultByteOrder = Common.Binary.ByteOrder.Unknown, int defaultComponentCount = 0, int defaultBitsPerComponent = 0)
: base(name, Codec.MediaType.Audio, defaultByteOrder, defaultComponentCount, defaultBitsPerComponent)
{

}
}
}
16 changes: 8 additions & 8 deletions Codecs/Audio/AudioFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public static AudioFormat FilterChannels(AudioFormat other, int componentIndex,
{
int otherComponentsLength = other.Components.Length;

if (otherComponentsLength == 1) return other;

if (componentIndex > otherComponentsLength || componentCount + componentIndex > otherComponentsLength) throw new System.ArgumentOutOfRangeException("componentIndex");

return new AudioFormat(other.SampleRate, other.IsSigned, other.ByteOrder, other.DataLayout, System.Linq.Enumerable.Skip(other.Components, componentIndex).Take(componentCount));
return otherComponentsLength == 1
? other
: componentIndex > otherComponentsLength || componentCount + componentIndex > otherComponentsLength
? throw new System.ArgumentOutOfRangeException("componentIndex")
: new AudioFormat(other.SampleRate, other.IsSigned, other.ByteOrder, other.DataLayout, System.Linq.Enumerable.Skip(other.Components, componentIndex).Take(componentCount));
}

public static AudioFormat Packed(AudioFormat other)
Expand All @@ -111,7 +111,7 @@ public static AudioFormat SemiPlanar(AudioFormat other)
return new AudioFormat(Codec.MediaFormat.SemiPlanar(other));
}

public static AudioFormat WithSampleRate(int sampleRate, AudioFormat other) => new AudioFormat(sampleRate, other.IsSigned, other.IsBigEndian, other.DataLayout);
public static AudioFormat WithSampleRate(int sampleRate, AudioFormat other) => new(sampleRate, other.IsSigned, other.IsBigEndian, other.DataLayout);

#endregion

Expand Down Expand Up @@ -147,7 +147,7 @@ public static AudioFormat SemiPlanar(AudioFormat other)
/// <summary>
/// Gets the amount of channels.
/// </summary>
public int Channels => Components.Length;
public int Channels => Components.Length;

/// <summary>
/// Gets the number of bits per sample.
Expand Down Expand Up @@ -202,7 +202,7 @@ public AudioFormat(int sampleRate, bool signed, bool bigEndian, Codec.DataLayout
/// Clones an AudioFormat
/// </summary>
/// <param name="format">The AudioFormat to clone</param>
internal protected AudioFormat(AudioFormat format)
protected internal AudioFormat(AudioFormat format)
: this(format.SampleRate, format.IsSigned, format.ByteOrder, format.DataLayout)
{

Expand Down
2 changes: 1 addition & 1 deletion Codecs/Audio/AudioTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public override void Dispose()
public class AudioTransformer : AudioTransformation
{
// The upsampling or downsampling factor
private int sampleRateFactor;
private readonly int sampleRateFactor;

public AudioTransformer(AudioBuffer source, AudioBuffer destination, int sampleRateFactor, Codec.TransformationQuality quality = Codec.TransformationQuality.Unspecified, bool shouldDispose = true)
: base(source, destination, quality, shouldDispose)
Expand Down
Loading

0 comments on commit 7582c40

Please sign in to comment.