Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: units refactoring and minor improvements #27

Merged
merged 3 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions sample/SampleConsoleApp/Demos/TimeDemo.cs

This file was deleted.

31 changes: 31 additions & 0 deletions sample/SampleConsoleApp/Demos/UnitsDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using DarkMusicConcepts;

namespace SampleConsoleApp.Demos;
internal class UnitsDemo : Demo
{
public override void Run()
{
PrintHeader("Units");

PrintSubHeader("Overview");

Print(MidiNumber.Min);
Print(MidiNumber.Max);
Print(MidiNumber.From(127));
Print(MidiNumber.IsValidValue(666));
Print(MidiNumber.From(127) > MidiNumber.From(60));
Print(MidiNumber.From(127) >= MidiNumber.From(60));
Print(MidiNumber.From(127) < MidiNumber.From(60));
Print(MidiNumber.From(127) <= MidiNumber.From(60));

PrintSubHeader("Time");

Print(Time.QuarterNote);
Print(Time.WholeNote);
Print(Time.Bar);
Print(Time.QuarterNote.GetDuration(Bpm.From(155)));
Print(Time.QuarterNote.Ticks);
Print(Time.QuarterNote * 3);
Print(Time.HalfNoteDotted + Time.Bar - (Time.SixteenthNote * 8));
}
}
2 changes: 1 addition & 1 deletion sample/SampleConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
new IntervalsDemo(),
new ScalesDemo(),
new ChordsDemo(),
new TimeDemo()
new UnitsDemo()
};

foreach (var demo in demos)
Expand Down
36 changes: 8 additions & 28 deletions src/DarkMusicConcepts.Units/Bpm.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
namespace DarkMusicConcepts;

public class Bpm : Unit<double, Bpm>
/// <summary>
/// <para>Beats per minute.</para>
/// <para>In musical terminology, tempo (Italian, 'time'; plural tempos, or tempi from the Italian plural) also known as beats per minute, is the speed or pace of a given composition. In classical music, tempo is typically indicated with an instruction at the start of a piece (often using conventional Italian terms) and is usually measured in beats per minute (or bpm). In modern classical compositions, a "metronome mark" in beats per minute may supplement or replace the normal tempo marking, while in modern genres like electronic dance music, tempo will typically simply be stated in BPM.</para>
/// </summary>
public class Bpm : Unit<double, Bpm>, IUnit<double, Bpm>
{
public const double MinValue = 0;
public const double MaxValue = double.MaxValue;

public static Bpm Min { get; } = From(MinValue);
public static Bpm Max { get; } = From(MaxValue);
public static double MinValue { get; } = 0;
public static double MaxValue { get; } = double.MaxValue;

private Bpm(double value) : base(value)
{
}

protected override double GetMinValue() => MinValue;
protected override double GetMaxValue() => MaxValue;

public static Bpm From(double value)
{
var bpm = new Bpm(value);

bpm.Validate();

return bpm;
}

public static bool TryFrom(double value, out Bpm bpm)
{
var x = new Bpm(value);

bpm = x.TryValidate()
? x
: null!;

return bpm is not null;
}
static Bpm IUnit<double, Bpm>.Create(double value) => new(value);
}
37 changes: 5 additions & 32 deletions src/DarkMusicConcepts.Units/Frequency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,16 @@
/// <para>Note frequencies are mathematically related to each other, and are defined around the central note, A4 (A @ OneLine octave) <see href="http://en.wikipedia.org/wiki/Piano_key_frequencies"/>. The current "standard pitch" or modern "concert pitch" for this note is 440 Hz. The formula for frequency calculatio is:</para>
/// <para>f = 2^n/12 * 440Hz(n is the pitch distance to A4 or A @ OneLine).</para>
/// </summary>
public class Frequency : Unit<double, Frequency>
public class Frequency : Unit<double, Frequency>, IUnit<double, Frequency>
{
public const double MinValue = 0;
public const double MaxValue = double.MaxValue;

public static Frequency Min { get; } = From(MinValue);
public static Frequency Max { get; } = From(MaxValue);
public static double MinValue { get; } = 0;
public static double MaxValue { get; } = double.MaxValue;

private Frequency(double value) : base(value)
{
}

protected override double GetMinValue() => MinValue;
protected override double GetMaxValue() => MaxValue;

public override string ToString()
{
return $"{Value} Hz";
}

public static Frequency From(double value)
{
var frequency = new Frequency(value);

frequency.Validate();
static Frequency IUnit<double, Frequency>.Create(double value) => new(value);

return frequency;
}

public static bool TryFrom(double value, out Frequency frequency)
{
var x = new Frequency(value);

frequency = x.TryValidate()
? x
: null!;

return frequency is not null;
}
public override string ToString() => $"{Value} Hz";
}
11 changes: 11 additions & 0 deletions src/DarkMusicConcepts.Units/IUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DarkMusicConcepts;

public interface IUnit<TValue, TThis>
where TValue : IComparable, IComparable<TValue>, IEquatable<TValue>
{
public static abstract TValue MinValue { get; }
public static abstract TValue MaxValue { get; }

internal static abstract TThis Create(TValue value);
public static abstract bool IsValidValue(TValue? value);
}
32 changes: 4 additions & 28 deletions src/DarkMusicConcepts.Units/MidiNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,14 @@
/// <para>In MIDI, each note is assigned a numeric value, which is transmitted with any Note-On/Off message. Middle C has a reference value of 60. The MIDI standard only says that the note number 60 is a C, it does not say of which octave.</para>
/// <para>Due to the limitation of MIDI number's range (0-127), not all existing notes can be assigned one. There are more than 128 notes.</para>
/// </summary>
public class MidiNumber : Unit<int, MidiNumber>
public class MidiNumber : Unit<int, MidiNumber>, IUnit<int, MidiNumber>
{
public const int MinValue = 0;
public const int MaxValue = 127;

public static MidiNumber Min { get; } = From(MinValue);
public static MidiNumber Max { get; } = From(MaxValue);
public static int MinValue { get; } = 0;
public static int MaxValue { get; } = 127;

private MidiNumber(int value) : base(value)
{
}

protected override int GetMinValue() => MinValue;
protected override int GetMaxValue() => MaxValue;

public static MidiNumber From(int value)
{
var midiNumber = new MidiNumber(value);

midiNumber.Validate();

return midiNumber;
}

public static bool TryFrom(int value, out MidiNumber midiNumber)
{
var x = new MidiNumber(value);

midiNumber = x.TryValidate()
? x
: null!;

return midiNumber is not null;
}
static MidiNumber IUnit<int, MidiNumber>.Create(int value) => new(value);
}
34 changes: 5 additions & 29 deletions src/DarkMusicConcepts.Units/MidiVelocity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,14 @@
/// <para>MIDI velocity indicates how hard the key was struck when the note was played, which usually corresponds to the note's loudness.</para>
/// <para>The MIDI velocity range is from 0–127, with 127 being the loudest.</para>
/// </summary>
public class MidiVelocity : Unit<int, MidiVelocity>
public class MidiVelocity : Unit<int, MidiVelocity>, IUnit<int, MidiVelocity>
{
public const int MinValue = 0;
public const int MaxValue = 127;

public static MidiVelocity Min { get; } = From(MinValue);
public static MidiVelocity Max { get; } = From(MaxValue);
public static int MinValue { get; } = 0;
public static int MaxValue { get; } = 127;

private MidiVelocity(int value) : base(value)
{
}

protected override int GetMinValue() => MinValue;
protected override int GetMaxValue() => MaxValue;

public static MidiVelocity From(int value)
{
var midiVelocity = new MidiVelocity(value);

midiVelocity.Validate();

return midiVelocity;
}

public static bool TryFrom(int value, out MidiVelocity midiVelocity)
{
var x = new MidiVelocity(value);

midiVelocity = x.TryValidate()
? x
: null!;

return midiVelocity is not null;
}
}
static MidiVelocity IUnit<int, MidiVelocity>.Create(int value) => new(value);
}
Loading