Skip to content

Commit

Permalink
sample rate a struct
Browse files Browse the repository at this point in the history
it's okay to make a breaking change like this because no one uses this
  • Loading branch information
spannerisms committed Jun 25, 2024
1 parent 52159b9 commit caaab29
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
5 changes: 3 additions & 2 deletions BRRSuite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>git</RepositoryType>
<SignAssembly>False</SignAssembly>
<AssemblyVersion>1.0.1</AssemblyVersion>
<FileVersion>1.0.1</FileVersion>
<AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>1.1.0</FileVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<EnableNETAnalyzers>False</EnableNETAnalyzers>
Expand All @@ -24,6 +24,7 @@
<PackageTags>snes;brr;audio;spc700</PackageTags>
<Product>BRR Suite</Product>
<Company />
<Version>$(AssemblyVersion)</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
2 changes: 1 addition & 1 deletion Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static int Clamp(int v) {
/// <param name="v"></param>
/// <returns>A new 15-bit value.</returns>
public static int Clip(int v) => v switch {
> 0x7FFF => v - 2, // equivalent to (p + 0x7FFF) & 0x7FFF
> 0x7FFF => (v + 0x7FFF) & 0x7FFF,
< -0x8000 => 0, // clipped to 0
> 0x3FFF => v - 0x8000, // [4000,7FFF] => [-4000,-1]
< -0x4000 => v + 0x8000, // [-8000,-4001] => [0,-3FFF]
Expand Down
25 changes: 12 additions & 13 deletions SampleRate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
/// <summary>
/// Represents an audio sample rate given in hertz.
/// </summary>
public sealed class SampleRate : IComparable<SampleRate> {
public readonly struct SampleRate : IComparable<SampleRate> {
/// <summary>
/// Gets the number of samples per second expressed by this frequency.
/// </summary>
public int Frequency { get; }
public readonly int Frequency { get; }

/// <summary>
/// Gets the frequency expressed as and rounded to the nearest kilohertz.
Expand All @@ -19,11 +19,6 @@ public sealed class SampleRate : IComparable<SampleRate> {
/// </summary>
public decimal Cram => 32000M / Frequency;

/// <summary>
/// Gets the value of this frequency with units (Hz).
/// </summary>
public string Name { get; }

/// <summary>
/// Creates a new instance of the <see cref="SampleRate"/> class with the specified frequency.
/// </summary>
Expand All @@ -34,12 +29,11 @@ public SampleRate(int frequency) {
throw new ArgumentException("Frequency should be a positive, non-zero value.");
}

Name = $"{frequency} Hz";
Frequency = frequency;
}

/// <inheritdoc cref="object.ToString()"/>
public override string ToString() => Name;
public override string ToString() => $"{Frequency} Hz";

/// <summary>
/// Calculates the ratio representing this sample rate resampled to the given target frequency.
Expand All @@ -57,10 +51,7 @@ public decimal ResampleTo(SampleRate targetFrequency) {

/// <inheritdoc cref="IComparable.CompareTo(object?)"/>
/// <exception cref="ArgumentNullException">If <paramref name="other"/> is <see langword="null"/>.</exception>
public int CompareTo(SampleRate? other) {
if (other is null) {
throw new ArgumentNullException(nameof(other), "Comparator argument was null.");
}
public int CompareTo(SampleRate other) {
return Frequency.CompareTo(other.Frequency);
}

Expand Down Expand Up @@ -88,4 +79,12 @@ public int CompareTo(SampleRate? other) {
/// Represents a sample rate of 44100 Hz, the standard for CD-quality audio.
/// </summary>
public static readonly SampleRate SR44100 = new(44100);



#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static explicit operator SampleRate(int sr) => new(sr);

public static implicit operator int(SampleRate sr) => sr.Frequency;
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
}

0 comments on commit caaab29

Please sign in to comment.