PHP code example of chaloman / tonal-php

1. Go to this page and download the library: Download chaloman/tonal-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chaloman / tonal-php example snippets


use Chaloman\Tonal\Note;

// Get note properties
$note = Note::get('C#4');
$note->name;    // "C#4"
$note->pc;      // "C#" (pitch class)
$note->letter;  // "C"
$note->acc;     // "#"
$note->octave;  // 4
$note->midi;    // 61
$note->freq;    // 277.18...

// Transpose notes
Note::transpose('C4', '3M');  // "E4"
Note::transpose('D4', '-2M'); // "C4"

// Get enharmonic equivalents
Note::enharmonic('C#');  // "Db"
Note::enharmonic('Db');  // "C#"

// Simplify notes
Note::simplify('E##'); // "F#"

use Chaloman\Tonal\Interval;

// Get interval properties
$interval = Interval::get('3M');
$interval->name;     // "3M"
$interval->semitones; // 4
$interval->quality;  // "M"
$interval->num;      // 3

// Calculate distance between notes
Interval::distance('C4', 'E4'); // "3M"

// Add intervals
Interval::add('3M', '3m'); // "5P"

// Simplify compound intervals
Interval::simplify('9M'); // "2M"

// Invert intervals
Interval::invert('3M'); // "6m"

use Chaloman\Tonal\Chord;

// Get chord properties
$chord = Chord::get('Cmaj7');
$chord->name;      // "C major seventh"
$chord->symbol;    // "Cmaj7"
$chord->tonic;     // "C"
$chord->type;      // "major seventh"
$chord->notes;     // ["C", "E", "G", "B"]
$chord->intervals; // ["1P", "3M", "5P", "7M"]

// Get chord notes with octave
Chord::get('Dm7/F')->notes; // ["F", "A", "C", "D"]

// Transpose chords
Chord::transpose('Cmaj7', '5P'); // "Gmaj7"

// Detect chords from notes
use Chaloman\Tonal\ChordDetect;
ChordDetect::detect(['C', 'E', 'G']);     // ["CM", "Em#5/C", ...]
ChordDetect::detect(['D', 'F', 'A', 'C']); // ["Dm7", ...]

use Chaloman\Tonal\Scale;

// Get scale properties
$scale = Scale::get('C major');
$scale->name;      // "C major"
$scale->tonic;     // "C"
$scale->type;      // "major"
$scale->notes;     // ["C", "D", "E", "F", "G", "A", "B"]
$scale->intervals; // ["1P", "2M", "3M", "4P", "5P", "6M", "7M"]

// Get scale notes
Scale::get('D dorian')->notes; // ["D", "E", "F", "G", "A", "B", "C"]

// Detect scales from notes
Scale::detect(['C', 'D', 'E', 'F', 'G', 'A', 'B']); // ["C major", "C ionian", ...]

// Find chords in a scale
Scale::chords('C major'); // ["C", "Dm", "Em", "F", "G", "Am", "Bdim"]

use Chaloman\Tonal\Key;

// Major keys
$key = Key::majorKey('C');
$key->tonic;       // "C"
$key->type;        // "major"
$key->scale;       // ["C", "D", "E", "F", "G", "A", "B"]
$key->chords;      // ["C", "Dm", "Em", "F", "G", "Am", "Bdim"]

// Minor keys
$key = Key::minorKey('A');
$key->natural->scale;  // ["A", "B", "C", "D", "E", "F", "G"]
$key->harmonic->scale; // ["A", "B", "C", "D", "E", "F", "G#"]
$key->melodic->scale;  // ["A", "B", "C", "D", "E", "F#", "G#"]

use Chaloman\Tonal\Mode;

// Get mode properties
$mode = Mode::get('dorian');
$mode->name;      // "dorian"
$mode->intervals; // ["1P", "2M", "3m", "4P", "5P", "6M", "7m"]
$mode->triad;     // "m"
$mode->seventh;   // "m7"

// Get notes of a mode
Mode::notes('dorian', 'D'); // ["D", "E", "F", "G", "A", "B", "C"]

// Get all mode names
Mode::names(); // ["ionian", "dorian", "phrygian", ...]

use Chaloman\Tonal\Progression;

// Convert roman numerals to chords
Progression::fromRomanNumerals('C', ['I', 'IV', 'V', 'I']);
// ["C", "F", "G", "C"]

// Convert chords to roman numerals
Progression::toRomanNumerals('C', ['C', 'Dm', 'G', 'C']);
// ["I", "IIm", "V", "I"]

use Chaloman\Tonal\Midi;

// Convert between MIDI and notes
Midi::toMidi('C4');       // 60
Midi::midiToNoteName(60); // "C4"

// Convert between MIDI and frequency
Midi::midiToFreq(69);  // 440.0
Midi::freqToMidi(440); // 69

use Chaloman\Tonal\RomanNumeral;

$rn = RomanNumeral::get('bVII');
$rn->name;     // "bVII"
$rn->roman;    // "VII"
$rn->interval; // "7m"
$rn->acc;      // "b"
$rn->major;    // true
bash
composer