Skip to content

davidsonbrsilva/sparks-music-library

Repository files navigation

Sparks Music Library

License Code Size Status

[Veja em Português]

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.

Table of contents

Requirements

Install

There are two ways for you to add this library to your project: through Nuget and manually.

Nuget install

  1. Make sure you've already added the https://nuget.pkg.github.com/davidsonbrsilva/index.json package source:
dotnet nuget list source
  1. 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
  1. Then browse to your project directory and add the package reference:
dotnet add package SparksMusic.Library

Manual install

  1. Clone the repository:
git clone https://github.com/davidsonbrsilva/sparks-music-library.git
  1. Access the project root folder:
cd sparks-music-library
  1. Build the library:
dotnet build -c Release

The library files will be generated and it will be ready for usage.

Tests

For tests, run the command:

dotnet test

Usage quick guide

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);
}

Transposition

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.

Getting chromatic matching

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

Extraction

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

Recognition

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

Optimization

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#

Contact

If necessary, contact davidsonbruno@outlook.com.

License

MIT Copyright (c) 2021 Davidson Bruno