Skip to content

🎵 KeyTools is a library that allows you to convert musical keys between notations. In addition, KeyTools allows you to calculate matching keys for harmonic mixing.

Notifications You must be signed in to change notification settings

iammordaty/key-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iammordaty/key-tools

KeyTools is a library that allows you to convert musical keys between notations. In addition, KeyTools allows you to calculate matching keys for harmonic mixing.

Supported notations:

  • Camelot Key
  • Open Key
  • Musical
  • Musical used by Beatport
  • Musical used by Essentia streaming extractor

KeyTools is based on the code written by @mossspence, which can be found here.

Table of Contents

Installation

The easiest way to install this library is via composer:

$ composer require iammordaty/key-tools

Requirements

  • PHP 7.1 and higher

Usage

The following example shows how to calculate a new key.

use KeyTools\KeyTools;

$keyTools = new KeyTools();

echo $keyTools->calculateKey('3A'); // "3A"
echo $keyTools->calculateKey('3A', 1); // "4A"
echo $keyTools->calculateKey('3A', 2); // "5A"
echo $keyTools->calculateKey('3A', -1); // "2A"
echo $keyTools->calculateKey('3A', 0, true); // "3B"

KeyTools can return keys with a leading zero – just set the parameter leading_zero to true, as in the following example. Please note that, this setting applies only to Camelot Key and Open Key notations.

use KeyTools\KeyTools;

$keyTools = new KeyTools([ 
    'leading_zero' => true, 
    'notation' => KeyTools::NOTATION_CAMELOT_KEY, 
]);

echo $keyTools->calculateKey('3A'); // "03A"
echo $keyTools->calculateKey('3A', 1); // "04A"
echo $keyTools->calculateKey('3A', 2); // "05A"
echo $keyTools->calculateKey('3A', -1); // "02A"
echo $keyTools->calculateKey('3A', 0, true); // "03B"

To calculate new key, you can also use shorthand methods:

echo $keyTools->noChange('3A'); // "3A"
echo $keyTools->perfectFifth('3A'); // "4A"
echo $keyTools->wholeStep('3A'); // "5A"
echo $keyTools->perfectFourth('3A'); // "2A"
echo $keyTools->relativeMinorToMajor('3A'); // "3B"

Also, conversion of keys between notations is easy:

echo $keyTools->convertKeyToNotation('Fmin', KeyTools::NOTATION_CAMELOT_KEY); // "4A"
echo $keyTools->convertKeyToNotation('Fmin', KeyTools::NOTATION_OPEN_KEY); // "9M"
echo $keyTools->convertKeyToNotation('Fmin', KeyTools::NOTATION_MUSICAL); // = "Fm"

KeyTools allows key and notation validation by suitable methods...

$key = 'Fmin';
$notation = KeyTools::NOTATION_CAMELOT_KEY;

$keyTools = new KeyTools();

if (!$keyTools->isValidKey($key)) {
    exit('Invalid key');
}

if (!$keyTools->isSupportedNotation($notation)) {
    exit('Unsupported notation');
}

echo $keyTools->convertKeyToNotation($key, $notation); // "4A"

... or by throwing appropriate exceptions:

use KeyTools\Exception\InvalidKeyException;
use KeyTools\Exception\UnsupportedNotationException;

$key = 'Fmin';
$notation = KeyTools::NOTATION_CAMELOT_KEY;

try {
    $keyTools = new KeyTools();

    echo $keyTools->convertKeyToNotation($key, $notation); // "4A"
} catch (InvalidKeyException | UnsupportedNotationException $e) {
    echo $e->getMessage();
}

Tests

Copy phpunit.xml.dist file to phpunit.xml and use PHPUnit to run tests:

$ ./vendor/bin/phpunit

Further information

License

iammordaty/key-tools is licensed under the MIT License.

About

🎵 KeyTools is a library that allows you to convert musical keys between notations. In addition, KeyTools allows you to calculate matching keys for harmonic mixing.

Topics

Resources

Stars

Watchers

Forks

Languages