Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Interval {
- protected static $intervals = array(
- "Perfect Unison" => 0,
- "P1" => 0,
- "Minor second" => 1,
- "m2" => 1,
- "S" => 1,
- "Major second" => 2,
- "M2" => 2,
- "T" => 2,
- "Minor third" => 3,
- "m3" => 3,
- "Major third" => 4,
- "M3" => 4,
- "Perfect fourth" => 5,
- "P4" => 5,
- "Diminished fifth" => 6,
- "Augmented forth" => 6,
- "Perfect fifth" => 7,
- "P5" => 7,
- "Minor sixth" => 8,
- "m6" => 8,
- "Major sixth" => 9,
- "M6" => 9,
- "Minor seventh" => 10,
- "m7" => 10,
- "Major seventh" => 11,
- "M7" => 11,
- "Perfect octave" => 12,
- "P6" => 12,
- );
- protected $size;
- public function __get($name) {
- switch ($name) {
- case "Size":
- return $this->size;
- }
- }
- public function __toString() {
- return array_search($this->size, self::$intervals) ?: $this->size.'S';
- }
- public function __construct($size) {
- if(is_string($size)) {
- $size = self::$intervals[$size];
- }
- $this->size = $size;
- }
- }
- class Note {
- const NotationRegex = '/^([CDFGA][#]?|[EB])(0|[1-9][0-9]*)$/i';
- protected static $classes = 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',
- );
- protected $uid;
- public function __get($name) {
- switch ($name) {
- case "ID":
- return $this->uid;
- case "Class":
- return self::$classes[$this->uid%12];
- case "Octave";
- return floor($this->uid/12);
- case "Name";
- return $this->Class.$this->Octave;
- default:
- throw new Exception("No such property: $name");
- }
- }
- public function __toString() {
- return $this->Class;
- }
- public function __construct($id) {
- if(is_int($id)) {
- $this->uid = $id;
- return;
- }
- //Assume it is a string...
- $this->uid = self::convertNotation($id);
- }
- public function interval($note) {
- return new Interval($note->ID - $this->uid);
- }
- public function note($interval) {
- return new Note($this->uid + $interval->Size);
- }
- protected static function convertNotation($notation) {
- preg_match(self::NotationRegex, $notation, &$matches);
- return array_search($matches[1], self::$classes) + $matches[2] * 12;
- }
- }
- class Scale {
- }
- $x = new Note("C2");
- $y = new Note("F#2");
- echo "ID: $x->ID\n";
- echo "Class: $x\n";
- echo "Octave: $x->Octave\n";
- echo "Name: $x->Name\n";
- echo $x->interval($y)."\n";
- $i = new Interval("P5");
- echo "Interval:\t$i\n";
- $z = $x->note($i);
- echo "Name:\t$z->Name\n";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement