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.
- 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
- Examples in action
- Method list
- Possible error messages
- Latest changes
- Rate us
- Support
- Awards
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
- Set flat notation
- Set sharp notation
- Get scale types
- Get chord types
- Get errors
- Add new scale
- Add new chord
- Get array with scale notes by scale name and type
- Get array with chord notes by chord name and type
- Get array with scale names and types from notes
- Get array with chord names and types from notes
- Get array with scales that suits specified chords
- Get array with chords that suits specified scale
Get all notes
| Method name | get_notes() |
| Description | Returns 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
| Method name | set_flat() |
| Description | Sets notation to using flat symbol for semitones when using method get_notes |
Set sharp notation
| Method name | set_sharp() |
| Description | Sets notation to using sharp symbol for semitones when using method get_notes |
Get scale types
| Method name | get_scale_types() |
| Description | Returns 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
| Method name | get_chord_types() |
| Description | Returns 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
| Method name | get_errors() |
| Description | Returns an array of errors |
| Example input | Trying to get chord with name "chord" and type "type" |
| Example output |
Array
(
[0] => Invalid chord name
)
|
Add new scale
| Method name | add_scale_type($type, $pattern_array) |
| Description | Adds 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
| Method name | add_chord_type($type, $pattern_array) |
| Description | Adds 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
| Method name | get_scale_by_name($name, $type, $transpose = 0) |
| Description | Get 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:
|
| 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
| Method name | get_chord_by_name($name, $type, $transpose = 0) |
| Description | Input 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:
|
| 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
| Method name | get_scales_by_notes($notes) |
| Description | Input 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
| Method name | get_chords_by_notes($notes) |
| Description | Input 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
| Method name | get_scales_by_chords($chords) |
| Description | Input 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
| Method name | get_chords_by_scale($scale_name, $scale_type) |
| Description | Input 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 text | Meaning | Solution |
| Scale type name already used | Trying to add new scale type, but it already exists. | Specify another scale type name |
| Chord type name already used | Trying to add new chord type, but it already exists. | Specify another chord type name |
| Invalid scale name | Specifying scale name, that doesn't exist | Use only scale names provided by get_notes() method |
| Invalid scale type | Specifying scale type, that doesn't exist | Use only scale types provided by get_scale_types() method |
| Invalid chord name | Specifying chord name, that doesn't exist | Use only chord names provided by get_notes() method |
| Invalid chord type | Specifying chord type, that doesn't exist | Use 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
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










