Advertisement
Omnikron13

Some music related PHP stuff

Jan 21st, 2014
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. <?php
  2.  
  3. class Interval {
  4.     protected static $intervals = array(
  5.         "Perfect Unison"        => 0,
  6.             "P1"                => 0,
  7.         "Minor second"          => 1,
  8.             "m2"                => 1,
  9.             "S"                 => 1,
  10.         "Major second"          => 2,
  11.             "M2"                => 2,
  12.             "T"                 => 2,
  13.         "Minor third"           => 3,
  14.             "m3"                => 3,
  15.         "Major third"           => 4,
  16.             "M3"                => 4,
  17.         "Perfect fourth"        => 5,
  18.             "P4"                => 5,
  19.         "Diminished fifth"      => 6,
  20.             "Augmented forth"   => 6,
  21.         "Perfect fifth"         => 7,
  22.             "P5"                => 7,
  23.         "Minor sixth"           => 8,
  24.             "m6"                => 8,
  25.         "Major sixth"           => 9,
  26.             "M6"                => 9,
  27.         "Minor seventh"         => 10,
  28.             "m7"                => 10,
  29.         "Major seventh"         => 11,
  30.             "M7"                => 11,
  31.         "Perfect octave"        => 12,
  32.             "P6"                => 12,
  33.     );
  34.     protected $size;
  35.     public function __get($name) {
  36.         switch ($name) {
  37.             case "Size":
  38.                 return $this->size;
  39.         }
  40.     }
  41.     public function __toString() {
  42.         return array_search($this->size, self::$intervals) ?: $this->size.'S';
  43.     }
  44.     public function __construct($size) {
  45.         if(is_string($size)) {
  46.             $size = self::$intervals[$size];
  47.         }
  48.         $this->size = $size;
  49.     }
  50. }
  51.  
  52. class Note {
  53.     const NotationRegex = '/^([CDFGA][#]?|[EB])(0|[1-9][0-9]*)$/i';
  54.     protected static $classes = array(
  55.         0   => 'C',
  56.         1   => 'C#',
  57.         2   => 'D',
  58.         3   => 'D#',
  59.         4   => 'E',
  60.         5   => 'F',
  61.         6   => 'F#',
  62.         7   => 'G',
  63.         8   => 'G#',
  64.         9   => 'A',
  65.         10  => 'A#',
  66.         11  => 'B',
  67.     );
  68.  
  69.     protected $uid;
  70.  
  71.     public function __get($name) {
  72.         switch ($name) {
  73.             case "ID":
  74.                 return $this->uid;
  75.             case "Class":
  76.                 return self::$classes[$this->uid%12];
  77.             case "Octave";
  78.                 return floor($this->uid/12);
  79.             case "Name";
  80.                 return $this->Class.$this->Octave;
  81.             default:
  82.                 throw new Exception("No such property: $name");
  83.         }
  84.     }
  85.  
  86.     public function __toString() {
  87.         return $this->Class;
  88.     }
  89.  
  90.     public function __construct($id) {
  91.         if(is_int($id)) {
  92.             $this->uid = $id;
  93.             return;
  94.         }
  95.         //Assume it is a string...
  96.         $this->uid = self::convertNotation($id);
  97.     }
  98.  
  99.     public function interval($note) {
  100.         return new Interval($note->ID - $this->uid);
  101.     }
  102.  
  103.     public function note($interval) {
  104.         return new Note($this->uid + $interval->Size);
  105.     }
  106.  
  107.     protected static function convertNotation($notation) {
  108.         preg_match(self::NotationRegex, $notation, &$matches);
  109.         return array_search($matches[1], self::$classes) + $matches[2] * 12;
  110.     }
  111. }
  112.  
  113. class Scale {
  114. }
  115.  
  116. $x = new Note("C2");
  117. $y = new Note("F#2");
  118.  
  119. echo "ID:       $x->ID\n";
  120. echo "Class:    $x\n";
  121. echo "Octave:   $x->Octave\n";
  122. echo "Name:     $x->Name\n";
  123.  
  124. echo $x->interval($y)."\n";
  125.  
  126. $i = new Interval("P5");
  127. echo "Interval:\t$i\n";
  128.  
  129. $z = $x->note($i);
  130. echo "Name:\t$z->Name\n";
  131.  
  132. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement