Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Implement music theory to generate scale and chord notes

Music theory class implements music theory for generating scales and chords based on interval patterns between notes. User can add custom scale and chord patterns.

This class can:
  • generate scale notes by provided scale name and type
  • generate chord notes by provided chord name and type
  • transpose scales
  • transpose chords
  • generate all chords that include provided notes
  • generate all scales that include provided notes
  • generate all scales that suits provided chords
  • generate all chords for provided scale

Contents

Download

Example codes

<?php
//declaring class instance
include("./music_theory.php");
$mt = new music_theory();

//sharp notation will be used
$mt->set_sharp();

/**************** Get scale by name *************************/
echo "<p>Get Scales, input: (C minor):</p>";

//get result array
$arr = $mt->get_scale_by_name("C", "minor");

//check if there were any errors
$errors = $mt->get_errors();
if(!empty($errors))
{
    foreach($errors as $error)
    {
        echo "<p>".$error."</p>";
    }
}
else
{
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}

/**************** Get chord by name *************************/

echo "<p>Get Chord, input: (C major):</p>";

//get result array
$arr = $mt->get_chord_by_name("C", "major", 1);

//check if there were any errors
$errors = $mt->get_errors();
if(!empty($errors))
{
    foreach($errors as $error)
    {
        echo "<p>".$error."</p>";
    }
}
else
{
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}

/**************** Get chords by ņotes *************************/

echo "<p>Get Chords by notes, input:(C, A, F):</p>";

//get result array
$arr = $mt->get_chords_by_notes(array("c","a","f"));

//check if there were any errors
$errors = $mt->get_errors();
if(!empty($errors))
{
    foreach($errors as $error)
    {
        echo "<p>".$error."</p>";
    }
}
else
{
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}

/**************** Get scales by ņotes *************************/

echo "<p>Get Scales by notes, input: (C, A, F):</p>";

//get result array
$arr = $mt->get_scales_by_notes(array("c","a","f"));

//check if there were any errors
$errors = $mt->get_errors();
if(!empty($errors))
{
    foreach($errors as $error)
    {
        echo "<p>".$error."</p>";
    }
}
else
{
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}

/**************** Get scales by chords *************************/

echo "<p>Get Scales by chords, input (C major and A minor):</p>";

$chords = array(array("name" => "C", "type" => "major")
        , array("name" => "A", "type" => "minor"));

//get result array
$arr = $mt->get_scales_by_chords($chords);

//check if there were any errors
$errors = $mt->get_errors();
if(!empty($errors))
{
    foreach($errors as $error)
    {
        echo "<p>".$error."</p>";
    }
}
else
{
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}

/**************** Get scales by chords *************************/

echo "<p>Get Chords by scale, input: (C minor):</p>";

//get result array
$arr = $mt->get_chords_by_scale("C", "minor");

//check if there were any errors
$errors = $mt->get_errors();
if(!empty($errors))
{
    foreach($errors as $error)
    {
        echo "<p>".$error."</p>";
    }
}
else
{
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}
?>

Examples in action

Example scripts provided with package in action:

Method list

Get all notes

Back to method list

Method nameget_notes()
DescriptionReturns array of note names
Example output
Array
(
    [0] => C
    [1] => C#
    [2] => D
    [3] => D#
    [4] => E
    [5] => F
    [6] => F#
    [7] => G
    [8] => G#
    [9] => A
    [10] => A#
    [11] => B
)

Set flat notation

Back to method list

Method nameset_flat()
DescriptionSets notation to using flat symbol for semitones when using method get_notes

Set sharp notation

Back to method list

Method nameset_sharp()
DescriptionSets notation to using sharp symbol for semitones when using method get_notes

Get scale types

Back to method list

Method nameget_scale_types()
DescriptionReturns an array of all predefined and user defined scale types
Example output
Array
(
    [0] => major
    [1] => ionian
    [2] => minor
    [3] => aeolian
    [4] => dorian
    [5] => phrygian
    [6] => lydian
    [7] => mixolydian
    [8] => locrian
    [9] => melodic minor asc
    [10] => melodic minor desc
    [11] => chromatic
    [12] => pentatonic major
    [13] => whole tone
    [14] => pentatonic minor
    [15] => pentatonic blues
    [16] => pentatonic neutral
    [17] => lydian augmented
    [18] => lydian minor
    [19] => lydian diminished
    [20] => major blues
    [21] => dominant pentatonic
    [22] => blues
)

Get chord types

Back to method list

Method nameget_chord_types()
DescriptionReturns an array of all predefined and user defined chord types
Example output
Array
(
    [0] => major
    [1] => minor
    [2] => 5
    [3] => aug
    [4] => dim
    [5] => 7
    [6] => sus4
    [7] => sus2
    [8] => 7sus4
    [9] => 6
    [10] => maj7
    [11] => 9
    [12] => add9
    [13] => m6
    [14] => m7
    [15] => mmaj7
    [16] => m9
    [17] => 11
    [18] => 13
    [19] => 6add9
    [20] => -5
    [21] => 7-5
    [22] => 7maj5
    [23] => maj9
)

Get errors

Back to method list

Method nameget_errors()
DescriptionReturns an array of errors
Example inputTrying to get chord with name "chord" and type "type"
Example output
Array
(
    [0] => Invalid chord name
)

Add new scale

Back to method list

Method nameadd_scale_type($type, $pattern_array)
DescriptionAdds user defined scale type
Input parameters

string $type - name of the scale type, like dorian (can't overwrite predefined names)

array $pattern_array - array with intervals between notes (for example major scale:(array(2,2,1,2,2,2,1)))

Example input

$type = "major"

$pattern_array = array(2,2,1,2,2,2,1)

Example output

Will add new scale type to defined scales

But of course this example will generate error, because this scale type is already defines in class:

Array
(
    [0] => Scale type name already used
)

Add new chord

Back to method list

Method nameadd_chord_type($type, $pattern_array)
DescriptionAdds user defined chord type
Input parameters

string $type - name of the chord type, like minor (can't overwrite predefined names)

array $pattern_array - array with intervals between notes (for example major chords:(array(4,3)))

Example input

$type = "major"

$pattern_array = array(4,3)

Example output

Will add new chord type to defined chords

But of course this example will generate error, because this chord type is already defines in class:

Array
(
    [0] => Chord type name already used
)

Get array with scale notes by scale name and type

Back to method list

Method nameget_scale_by_name($name, $type, $transpose = 0)
DescriptionGet scale notes from scale name and type
Input parameters

string $name - name of the scale, example C#

string $type - type of the scale, example major

int $transpose - transpose scale:

  • 0 - doesn't transpose
  • positive values - transposes up
  • negative values - transposes down
Example input

$name = "C"

$type = "ionian"

$transpose = -1

Example output
Array
(
    [0] => B
    [1] => C#
    [2] => D#
    [3] => E
    [4] => F#
    [5] => G#
    [6] => A#
    [7] => B
)

Get array with chord notes by chord name and type

Back to method list

Method nameget_chord_by_name($name, $type, $transpose = 0)
DescriptionInput chord name and type and get array of notes for this chord
Input parameters

string $name - name of the chord, example C#

string $type - type of the chord, example major

int $transpose - transpose chord:

  • 0 - doesn't transpose
  • positive values - transposes up
  • negative values - transposes down
Example input

$name = "C"

$type = "major"

$transpose = 2

Example output
Array
(
    [0] => D
    [1] => F#
    [2] => A
)

Get array with scale names and types from notes

Back to method list

Method nameget_scales_by_notes($notes)
DescriptionInput array with note names, and get array with scale names and types, which include all provided notes. If no scales found, empty array is returned
Input parameters

array $notes - note names, example array("C","D","E","G#","C#")

Example input

$notes = array("C","D","E","G#","C#")

Example output
Array
(
    [0] => Array
        (
            [name] => C
            [type] => chromatic
        )

    [1] => Array
        (
            [name] => C#
            [type] => chromatic
        )

    [2] => Array
        (
            [name] => D
            [type] => chromatic
        )

    [3] => Array
        (
            [name] => D#
            [type] => chromatic
        )

    [4] => Array
        (
            [name] => E
            [type] => chromatic
        )

    [5] => Array
        (
            [name] => F
            [type] => chromatic
        )

    [6] => Array
        (
            [name] => F#
            [type] => chromatic
        )

    [7] => Array
        (
            [name] => F#
            [type] => lydian minor
        )

    [8] => Array
        (
            [name] => G
            [type] => chromatic
        )

    [9] => Array
        (
            [name] => G#
            [type] => chromatic
        )

    [10] => Array
        (
            [name] => A
            [type] => chromatic
        )

    [11] => Array
        (
            [name] => A#
            [type] => chromatic
        )

    [12] => Array
        (
            [name] => B
            [type] => chromatic
        )

)

Get array with chord names and types from notes

Back to method list

Method nameget_chords_by_notes($notes)
DescriptionInput array with note names, and get array with chord names and types, which include all provided notes. If no chords found, empty array is returned
Input parameters

array $notes - note names, example array("A","F","C", "E")

Example input

$notes = array("A","F","C", "E")

Example output
Array
(
    [0] => Array
        (
            [name] => C
            [type] => 13
        )

    [1] => Array
        (
            [name] => D
            [type] => m9
        )

    [2] => Array
        (
            [name] => F
            [type] => maj7
        )

    [3] => Array
        (
            [name] => F
            [type] => maj9
        )

    [4] => Array
        (
            [name] => G
            [type] => 13
        )

)

Get array with scales that suits specified chords

Back to method list

Method nameget_scales_by_chords($chords)
DescriptionInput array with chord names and types and get array with scale names and types under which chords can be played. If nothing found, empty array is returned
Input parameters

array $chords - chord names and types, example array(array("name" => "C", "type" => "major"), array("name" => "A", "type" => "minor"))

Example input

$notes = array(array("name" => "C", "type" => "major"), array("name" => "A", "type" => "minor"))

Example output
Array
(
    [0] => Array
        (
            [name] => C
            [type] => major
        )

    [1] => Array
        (
            [name] => C
            [type] => ionian
        )

    [2] => Array
        (
            [name] => C
            [type] => lydian
        )

    [3] => Array
        (
            [name] => C
            [type] => mixolydian
        )

    [4] => Array
        (
            [name] => C
            [type] => chromatic
        )

    [5] => Array
        (
            [name] => C
            [type] => pentatonic major
        )

    [6] => Array
        (
            [name] => C
            [type] => major blues
        )

    [7] => Array
        (
            [name] => C#
            [type] => chromatic
        )

    [8] => Array
        (
            [name] => D
            [type] => minor
        )

    [9] => Array
        (
            [name] => D
            [type] => aeolian
        )
	...
)

Get array with chords that suits specified scale

Back to method list

Method nameget_chords_by_scale($scale_name, $scale_type)
DescriptionInput array with scale names and types and get array with chord names and types that can be played under specified scale. If nothing found, empty array is returned
Input parameters

string $scale_name - scale name, example C

string $scale_type - scale type example "ionian"

Example input

$scale_name = "C"

$scale_type = "ionian"

Example output
Array
(
    [0] => Array
        (
            [name] => C
            [type] => major
        )

    [1] => Array
        (
            [name] => C
            [type] => 5
        )

    [2] => Array
        (
            [name] => C
            [type] => sus4
        )

    [3] => Array
        (
            [name] => C
            [type] => sus2
        )

    [4] => Array
        (
            [name] => C
            [type] => 6
        )

    [5] => Array
        (
            [name] => C
            [type] => maj7
        )

    [6] => Array
        (
            [name] => C
            [type] => add9
        )

    [7] => Array
        (
            [name] => C
            [type] => 6add9
        )

    [8] => Array
        (
            [name] => C
            [type] => maj9
        )

    [9] => Array
        (
            [name] => D
            [type] => minor
        )
	...
)

Possible error messages

List of all errors and meanings

Error textMeaningSolution
Scale type name already usedTrying to add new scale type, but it already exists.Specify another scale type name
Chord type name already usedTrying to add new chord type, but it already exists.Specify another chord type name
Invalid scale nameSpecifying scale name, that doesn't existUse only scale names provided by get_notes() method
Invalid scale typeSpecifying scale type, that doesn't existUse only scale types provided by get_scale_types() method
Invalid chord nameSpecifying chord name, that doesn't existUse only chord names provided by get_notes() method
Invalid chord typeSpecifying chord type, that doesn't existUse only chord types provided by get_chord_types() method

Latest changes

09.12.2010 - Fixed bug with flat notation

11.12.2010 - Comments fixed in main class

Rate us

Like our script? Rate it at PHP > Hot Scripts

Scripts.com
RATE ME!

Try it out and Rate on PHPclasses.org

Support

PHP classes support forum or comments below

Awards

Music theory is the winner of the PHP Programming Innovation Award, thanks everyone for your support


You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus