Sparks Music Library is a library of classes built for recognition, extraction and transposition of musical chords.
It is very common to find transposition solutions that reduce the note mapping to the twelve possible sounds to be reproduced in the western scale, making it contain sharps or flats, but not both. This can limit the user's desire to see enharmony equivalent to that generated by transposition. For example, the desired result may be the harmonic field of Bb
, but what you get is the harmonic field of A#
.
Both Bb
and A#
are called enharmonic chords (they produce the same sound but have different names) and can play different roles depending on the context. For this reason, the simplified result of these transposition solutions can result in an unwanted result. That's where Sparks Music Library finds space, trying to respect the role played by the chord when considering results in flat, sharp, double flat and double sharp.
There are two ways for you to add this library to your project: through Nuget and manually.
- Make sure you've already added the https://nuget.pkg.github.com/davidsonbrsilva/index.json package source:
dotnet nuget list source
- If you don't, add the following package source:
dotnet nuget add source https://nuget.pkg.github.com/davidsonbrsilva/index.json -n github.com/davidsonbrsilva
- Then browse to your project directory and add the package reference:
dotnet add package SparksMusic.Library
- Clone the repository:
git clone https://github.com/davidsonbrsilva/sparks-music-library.git
- Access the project root folder:
cd sparks-music-library
- Build the library:
dotnet build -c Release
The library files will be generated and it will be ready for usage.
For tests, run the command:
dotnet test
For start, import the library SparksMusic.Library
to your project:
using SparksMusic.Library;
Most SparksMusic Library methods throw exceptions on operations and must be caught by try-catch
statements. For the sake of simplicity, consider that all of the following calls are wrapped in try-catch
statements:
try
{
// SparksMusic Library operations here
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Use method TransposeUp
if you want to transpose chords up and TransposeDown
if you want to transpose chords down. Both receive as parameters the character string of the chord to be transposed and the amount of semitones for the transposition.
Transpose up
Chord transposed = Transposer.TransposeUp("A", 2);
Console.WriteLine(transposed); // B
Transpose down
Chord transposed = Transposer.TransposeDown("A", 2);
Console.WriteLine(transposed); // G
Other method overloads take note, chord, and chord list as parameters.
Use the GetChromaticCorrespondent
method to get the chromatic equivalent of the given note. For example, A#
is returned as Bb
and vice versa.
Note note = new Note(NoteLetter.A, Accident.Sharp);
Note correspondent = Transposer.GetChromaticCorrespondent(note);
Console.WriteLine(correspondent); // Bb
Notes without accidentals are returned as the input. For example A
is returned as A
.
Note note = new Note(NoteLetter.A, Accident.None);
Note correspondent = Transposer.GetChromaticCorrespondent(note);
Console.WriteLine(correspondent); // A
Use the ExtractChords
method if you want to extract valid chords from a string.
// This method extract the valid chords in the provided input text.
// In this case, only G#°, F#m7 and B/A are valid chords.
List<Chord> extractedChords = Transposer.ExtractChords("My chord sequence is G#° F#m7 Fº+ F#m7/A### B/A");
foreach (var chord in extractedChords)
{
Console.WriteLine(chord);
}
// Output:
// G#°
// F#m7
// B/A
Use the IsChord
method if you want to check if the given string matches a valid chord.
Console.WriteLine(Transposer.IsChord("A")); // True
Console.WriteLine(Transposer.IsChord("H")); // False
Console.WriteLine(Transposer.IsChord("A#m7")); // True
Console.WriteLine(Transposer.IsChord("A+°")); // False
Use the GetValidChords
method if you want to get a list of valid chord objects from a list of strings.
List<string> chordsList = new List<string>{ "A", "H", "A#m7", "A#º" };
List<Chord> validChords = Transposer.GetValidChords(chordsList);
foreach (Chord chord in validChords)
{
Console.WriteLine(chord);
}
// Output:
// A
// A#m7
Use the HasDifferentChromaticPole
method if you want to check if two notes have different chromatic pole. For example, two notes A#
and Bb
return false
by the method, as the first belongs to the sharp chromatic pole and the second flat.
Note note1 = new Note(NoteLetter.A, Accident.Sharp);
Note note2 = new Note(NoteLetter.B, Accident.Flat);
Note note3 = new Note(NoteLetter.C, Accident.None);
Note note4 = new Note(NoteLetter.C, Accident.Sharp);
Console.WriteLine(Transposer.HasDifferentChromaticPole(note1, note2)); // True
Console.WriteLine(Transposer.HasDifferentChromaticPole(note1, note3)); // False
Console.WriteLine(Transposer.HasDifferentChromaticPole(note1, note4)); // False
Use the Optimize
method if you want to apply optimizations to the chord. Currently, the method performs optimization if the note and chord inversion are of different chromatic poles and returns the new optimized chord (for example, A#/Db
becomes A#/C#
).
Chord optimized = Transposer.Optimize("A#/Db");
Console.WriteLine(optimized); // A#/C#
If necessary, contact davidsonbruno@outlook.com.
MIT Copyright (c) 2021 Davidson Bruno