-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8601a00
commit 97a18f8
Showing
20 changed files
with
551 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
HarmonyDB.Theory/HarmonyDB.Theory.Chords.Tests/AllChordsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using HarmonyDB.Theory.Chords.Options; | ||
using HarmonyDB.Theory.Chords.Parsers; | ||
using Xunit.Abstractions; | ||
|
||
namespace HarmonyDB.Theory.Chords.Tests; | ||
|
||
public class AllChordsTests(ITestOutputHelper testOutputHelper) | ||
{ | ||
[Fact] | ||
public void LittleChordsFailed() | ||
{ | ||
var success = 0; | ||
var failure = 0; | ||
foreach (var (chord, count) in Resources.AllChords) | ||
{ | ||
try | ||
{ | ||
ChordParser.Parse(chord, ChordParsingOptions.MostForgiving); | ||
success += count; | ||
} | ||
catch | ||
{ | ||
failure += count; | ||
testOutputHelper.WriteLine($"{count}\t{chord}"); | ||
} | ||
} | ||
|
||
testOutputHelper.WriteLine($"Success: {success}, failure: {failure}."); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
HarmonyDB.Theory/HarmonyDB.Theory.Chords.Tests/HarmonyDB.Theory.Chords.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="dataset.json" /> | ||
<None Remove="Resources\AllChords.json" /> | ||
<None Remove="Resources\AllChords.json.gz" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="Resources\AllChords.json.gz" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\HarmonyDB.Theory.Chords\HarmonyDB.Theory.Chords.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
19 changes: 19 additions & 0 deletions
19
HarmonyDB.Theory/HarmonyDB.Theory.Chords.Tests/Resources.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.IO.Compression; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
|
||
namespace HarmonyDB.Theory.Chords.Tests; | ||
|
||
public static class Resources | ||
{ | ||
private const string AllChordsResourceName = "HarmonyDB.Theory.Chords.Tests.Resources.AllChords.json.gz"; | ||
|
||
static Resources() | ||
{ | ||
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(AllChordsResourceName)!; | ||
using var gzip = new GZipStream(stream, CompressionMode.Decompress); | ||
AllChords = JsonSerializer.Deserialize<Dictionary<string, int>>(gzip)!; | ||
} | ||
|
||
public static IReadOnlyDictionary<string, int> AllChords { get; } | ||
} |
Binary file added
BIN
+100 KB
HarmonyDB.Theory/HarmonyDB.Theory.Chords.Tests/Resources/AllChords.json.gz
Binary file not shown.
18 changes: 18 additions & 0 deletions
18
HarmonyDB.Theory/HarmonyDB.Theory.Chords/HarmonyDB.Theory.Chords.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<PackageReadmeFile>readme.md</PackageReadmeFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\nuget readme.md" Pack="true" Link="readme.md" PackagePath="\readme.md" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\OneShelf.Common\OneShelf.Common\OneShelf.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace HarmonyDB.Theory.Chords.Models; | ||
|
||
public record Chord(NoteRepresentation RootRepresentation, NoteRepresentation? BassRepresentation, string ChordType) | ||
{ | ||
public Note Root = RootRepresentation.Note; | ||
public Note? Bass = BassRepresentation?.Note; | ||
} |
23 changes: 23 additions & 0 deletions
23
HarmonyDB.Theory/HarmonyDB.Theory.Chords/Models/ChordParseResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using HarmonyDB.Theory.Chords.Models.Enums; | ||
|
||
namespace HarmonyDB.Theory.Chords.Models; | ||
|
||
public record ChordParseResult | ||
{ | ||
public ChordParseResult(ChordParseResultType resultType) | ||
{ | ||
if (resultType == ChordParseResultType.Success) | ||
throw new ArgumentOutOfRangeException(nameof(resultType), resultType, "Use a different constructor."); | ||
|
||
ResultType = resultType; | ||
} | ||
|
||
public ChordParseResult(Chord chord) | ||
{ | ||
ResultType = ChordParseResultType.Success; | ||
Chord = chord; | ||
} | ||
|
||
public ChordParseResultType ResultType { get; } | ||
public Chord? Chord { get; } | ||
} |
7 changes: 7 additions & 0 deletions
7
HarmonyDB.Theory/HarmonyDB.Theory.Chords/Models/Enums/Alteration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace HarmonyDB.Theory.Chords.Models.Enums; | ||
|
||
public enum Alteration : byte | ||
{ | ||
Flat = 0, | ||
Sharp = 1, | ||
} |
7 changes: 7 additions & 0 deletions
7
HarmonyDB.Theory/HarmonyDB.Theory.Chords/Models/Enums/ChordParseResultType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace HarmonyDB.Theory.Chords.Models.Enums; | ||
|
||
public enum ChordParseResultType | ||
{ | ||
SpecialNoChord, | ||
Success, | ||
} |
19 changes: 19 additions & 0 deletions
19
HarmonyDB.Theory/HarmonyDB.Theory.Chords/Models/Enums/HHandling.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace HarmonyDB.Theory.Chords.Models.Enums; | ||
|
||
public enum HHandling | ||
{ | ||
/// <summary> | ||
/// English variant. B = H = Si = A##. A# = Bb. | ||
/// </summary> | ||
BMeansH, | ||
|
||
/// <summary> | ||
/// German variant. B = A#. H = B# = Si. | ||
/// </summary> | ||
BbMeansH, | ||
|
||
/// <summary> | ||
/// Strict English variant. H is prohibited. | ||
/// </summary> | ||
HProhibited, | ||
} |
12 changes: 12 additions & 0 deletions
12
HarmonyDB.Theory/HarmonyDB.Theory.Chords/Models/Enums/NaturalNote.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace HarmonyDB.Theory.Chords.Models.Enums; | ||
|
||
public enum NaturalNote : byte | ||
{ | ||
A = 0, | ||
B = 2, | ||
C = 3, | ||
D = 5, | ||
E = 7, | ||
F = 8, | ||
G = 10, | ||
} |
23 changes: 23 additions & 0 deletions
23
HarmonyDB.Theory/HarmonyDB.Theory.Chords/Models/Enums/NaturalNoteRepresentation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace HarmonyDB.Theory.Chords.Models.Enums; | ||
|
||
public enum NaturalNoteRepresentation : sbyte | ||
{ | ||
A = 0, | ||
B = 2, | ||
C = 3, | ||
D = 5, | ||
E = 7, | ||
F = 8, | ||
G = 10, | ||
|
||
H = -1, | ||
GermanB = -2, | ||
|
||
La = -52, | ||
Si = -51, | ||
Do = -50, | ||
Re = -49, | ||
Mi = -48, | ||
Fa = -47, | ||
Sol = -46, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace HarmonyDB.Theory.Chords.Models; | ||
|
||
public readonly record struct Note | ||
{ | ||
public Note(byte value) | ||
{ | ||
// ReSharper disable once ConditionIsAlwaysTrueOrFalse | ||
if (value < NoteConstants.MinNoteValue || value > NoteConstants.MaxNoteValue) | ||
throw new ArgumentOutOfRangeException(nameof(value), value, $"A value between {NoteConstants.MinNoteValue} and {NoteConstants.MaxNoteValue} (inclusive) is expected."); | ||
|
||
Value = value; | ||
} | ||
|
||
public byte Value { get; } | ||
|
||
public static Note Normalized(int value) => new(Normalize(value)); | ||
|
||
public static byte Normalize(int notes) => (byte)((notes % NoteConstants.TotalNotes + NoteConstants.TotalNotes) % NoteConstants.TotalNotes); | ||
} |
62 changes: 62 additions & 0 deletions
62
HarmonyDB.Theory/HarmonyDB.Theory.Chords/Models/NoteConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using HarmonyDB.Theory.Chords.Models.Enums; | ||
|
||
namespace HarmonyDB.Theory.Chords.Models; | ||
|
||
public static class NoteConstants | ||
{ | ||
public const byte MinNoteValue = 0; | ||
public const byte MaxNoteValue = 11; | ||
|
||
public static readonly Note MinNote = new(MinNoteValue); | ||
public static readonly Note MaxNote = new(MaxNoteValue); | ||
|
||
public static readonly Note GermanBNote = new(1); | ||
|
||
public const byte TotalNotes = 12; | ||
|
||
public const char MinNaturalNoteChar = 'A'; | ||
public const char MaxNaturalNoteChar = 'G'; | ||
|
||
public const char HSynonymChar = 'H'; | ||
public const NaturalNote HSynonym = NaturalNote.B; | ||
|
||
public const char FlatSymbol = 'b'; | ||
public const char SharpSymbol = '#'; | ||
|
||
public const char FlatUnicodeSymbol = '♭'; | ||
public const char SharpUnicodeSymbol = '♯'; | ||
|
||
public const string NoChord = "N.C."; | ||
|
||
public static readonly IReadOnlyList<string> NoChordVariants = | ||
[ | ||
NoChord, | ||
"N.C", | ||
"NC", | ||
"N.С.", // russian C | ||
]; | ||
|
||
public static readonly IReadOnlyList<char> FlatSymbols = new List<char> { FlatSymbol, FlatUnicodeSymbol }; | ||
public static readonly IReadOnlyList<char> SharpSymbols = new List<char> { SharpSymbol, SharpUnicodeSymbol }; | ||
|
||
public static IReadOnlyList<char> NaturalNoteChars = | ||
[ | ||
'A', 'B', 'C', 'D', 'E', 'F', 'G', | ||
]; | ||
|
||
public static IReadOnlyList<string> NaturalNoteSolfegeNames = | ||
[ | ||
"LA", "SI", "DO", "RE", "MI", "FA", "SOL", | ||
]; | ||
|
||
public static IReadOnlyList<NaturalNote> NaturalNotes = | ||
[ | ||
NaturalNote.A, | ||
NaturalNote.B, | ||
NaturalNote.C, | ||
NaturalNote.D, | ||
NaturalNote.E, | ||
NaturalNote.F, | ||
NaturalNote.G, | ||
]; | ||
} |
15 changes: 15 additions & 0 deletions
15
HarmonyDB.Theory/HarmonyDB.Theory.Chords/Models/NoteRepresentation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using HarmonyDB.Theory.Chords.Models.Enums; | ||
|
||
namespace HarmonyDB.Theory.Chords.Models; | ||
|
||
public record NoteRepresentation(NaturalNoteRepresentation NaturalNoteRepresentation, int Sharps, int Flats) | ||
{ | ||
public Note Note => Note.Normalized((NaturalNoteRepresentation switch | ||
{ | ||
>= 0 => (byte)NaturalNoteRepresentation, | ||
NaturalNoteRepresentation.GermanB => NoteConstants.GermanBNote.Value, | ||
NaturalNoteRepresentation.H => (byte)NoteConstants.HSynonym, | ||
>= NaturalNoteRepresentation.La and <= NaturalNoteRepresentation.Sol => NaturalNoteRepresentation - NaturalNoteRepresentation.La, | ||
_ => throw new ArgumentOutOfRangeException(nameof(NaturalNoteRepresentation), NaturalNoteRepresentation, "The representation is out of range."), | ||
}) + Sharps - Flats); | ||
} |
19 changes: 19 additions & 0 deletions
19
HarmonyDB.Theory/HarmonyDB.Theory.Chords/Options/ChordParsingOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace HarmonyDB.Theory.Chords.Options; | ||
|
||
public class ChordParsingOptions | ||
{ | ||
public static readonly ChordParsingOptions Default = new(); | ||
|
||
public static readonly ChordParsingOptions MostForgiving = new() | ||
{ | ||
ForgiveSameBass = true, | ||
ForgiveEdgeWhitespaces = true, | ||
NoteParsingOptions = NoteParsingOptions.MostForgiving, | ||
ForgiveRoundBraces = true, | ||
}; | ||
|
||
public bool ForgiveSameBass { get; set; } | ||
public bool ForgiveEdgeWhitespaces { get; set; } | ||
public NoteParsingOptions NoteParsingOptions { get; set; } = NoteParsingOptions.Default; | ||
public bool ForgiveRoundBraces { get; set; } | ||
} |
Oops, something went wrong.