doubleaa93

PHP Chord Transposer

Jun 19th, 2012
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //require_once("dompdf/dompdf_config.inc.php");
  3.  
  4. class Transposer
  5. {
  6.     private $type = 'sharps';
  7.     private $notes = array(
  8.         'scale' => array(
  9.           'C'  => 1,
  10.           'C#' => 2,
  11.           'Db' => 2,
  12.           'D'  => 3,
  13.           'D#' => 4,
  14.           'Eb' => 4,
  15.           'E'  => 5,
  16.           'Fb' => 5,
  17.           'F'  => 6,
  18.           'F#' => 7,
  19.           'Gb' => 7,
  20.           'G'  => 8,
  21.           'G#' => 9,
  22.           'Ab' => 9,
  23.           'A'  => 10,
  24.           'A#' => 11,
  25.           'Bb' => 11,
  26.           'B'  => 12,
  27.           'Cb' => 12
  28.         ),
  29.       'flats'  => array(1 => 'C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'),
  30.       'sharps' => array(1 => 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B')
  31.    );
  32.    private $search = '`([ABCDEFG][b#]?(?=\s(?![a-zH-Z])|(?=(2|5|6|7|9|11|13|6\/9|7\-5|7\-9|7\#5|7\#9|7‌​\+5|7\+9|7b5|7b9|7sus2|7sus4|add2|add4|add9|aug|dim|dim7|m\|maj7|m6|m7|m7b5|m9|m1‌​1|m13|maj7|maj9|maj11|maj13|mb5|m|sus|sus2|sus4|\))(?=(\s|\/)))|(?=(\/|\.|-|\(|\)))))`';
  33.    private $search2 = '`([ABCDEFG][b#]?[m]?[\(]?(2|5|6|7|9|11|13|6\/9|7\-5|7\-9|7\#5|7\#9|7\+5|7\+9|7b5|7b9|7sus2|7sus4|add2|add4|add9|aug|dim|dim7|m\|maj7|m6|m7|m7b5|m9|m11|m13|maj7|maj9|maj11|maj13|mb5|m|sus|sus2|sus4)?(\))?)(?=\s|\.|\)|-|\/)`';  
  34.    private $song;
  35.    private $steps;
  36.    private $formattedChords = array();
  37.    private $replacementChords = array();
  38.    
  39.     public function __construct($song_,$steps_) {
  40.         $this->song = $song_;
  41.         $this->steps = $steps_;      
  42.         preg_match_all($this->search, $this->song, $song_chords);
  43.         //print_r($u = array_unique($song_chords[0]))."\n";
  44.         $u = array_unique($song_chords[0]);
  45.         foreach ($u as $chord){
  46.             if (strlen($chord) > 1  && ($chord{1} == "b" || $chord{1} == "#"))
  47.                 array_push($this->formattedChords,substr($chord,0, 2));
  48.             else
  49.                 array_push($this->formattedChords,substr($chord,0, 1));
  50.         }
  51.         //print_r($this->formattedChords)."\n";
  52.         $this->song = preg_replace($this->search,'|$1|',$this->song);
  53.         foreach($this->formattedChords as $note) {
  54.             $len = strlen($note);
  55.             $len = $len - 1;
  56.             switch ($note{$len}) {
  57.                 case "b":            
  58.                     $this->transpose($note,'flats',$this->steps);
  59.                     break;         
  60.                 case "#":
  61.                     $this->transpose($note,'sharps',$this->steps);
  62.                     break;
  63.                 default:
  64.                     $this->transpose($note,'sharps',$this->steps);
  65.             }
  66.         }
  67.         foreach($this->formattedChords as &$note){
  68.             $note = "/\|".$note."\|/";
  69.         }
  70.         $this->song = preg_replace($this->formattedChords, $this->replacementChords, $this->song);
  71.          $html= preg_replace($this->search2,'<b>$1</b>',$this->song);
  72.          $old = array("<pre>", "\r", "\n", "</pre>");
  73.           $new = array("<pre><span>", "", "</span>\n<span>", "</span></pre>");
  74.  
  75.  
  76.          $html ='<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><html><body><pre>'.$html.'</pre></body></html>';
  77.  
  78. $html = str_replace($old, $new, $html);
  79. echo $html = str_replace("<span></span>","<span> </span>",$html);
  80. //$dompdf = new DOMPDF();
  81. //$dompdf->load_html($html);
  82. //$dompdf->render();
  83. //strlen($html);
  84. //$dompdf->stream("sample.pdf");
  85.     }
  86.    
  87.     public function transpose($note,$types,$steps){
  88.        
  89.         if(isset($this->notes['scale'][$note]))
  90.         {
  91.             $ix = $this->notes['scale'][$note];
  92.         }
  93.         else
  94.         {
  95.             user_error("Invalid note '$note'");
  96.             return false;
  97.         }
  98.         $ixNew = $ix + $steps;
  99.         if(!isset($this->notes[$types][$ixNew])) {
  100.             $ixNew += ($ixNew > 0) ? -12 : 12;
  101.             if(!isset($this->notes[$types][$ixNew]))
  102.                 throw new Exception("My math skills suck! $note : $steps : $ix : $ixNew");
  103.         }
  104.         array_push($this->replacementChords,$this->notes[$types][$ixNew]);
  105.         //echo "/|".$note."|/";
  106.     }
  107. }
  108.  
  109.  
  110.       //$new_song = preg_replace($u, $new, $song);
  111.    
  112.         //echo $clean = str_replace('|', '', $new_song);
  113.        
  114. $song = "
  115. God is able - Hillsong
  116.       B
  117. God is able
  118.        F#
  119. He will never fail
  120. Abm E
  121. He is almighty God
  122.             B
  123. Greater than all we need
  124.             F#
  125. Greater than all we have
  126. Abm E F#
  127. He has done great things
  128.  
  129. Chorus:
  130.       B
  131. Lifted up
  132.             F#
  133. Defeated the grave
  134.          Abm
  135. Raised to life
  136.           E
  137. Our God is able
  138.       B/D#
  139. In His name
  140.       F#
  141. We overcome
  142.        Abm
  143. For the Lord
  144.    F# E
  145. Our God is able
  146.  
  147. Verse 2:
  148. God is with us
  149. God is on our side
  150. He will make a way
  151. Far above all we know
  152. Far above all we hope
  153. He has done great things
  154.  
  155. Bridge:
  156.       B
  157. God is with us
  158.        F#
  159. He will go before
  160.        Abm
  161. He will never leave us
  162.        E
  163. He will never leave us
  164.  
  165.       B/D#
  166. God is for us
  167.       F#
  168. He has open arms
  169.        Abm F#
  170. He will never fail us
  171.               E
  172. He will never fail us
  173.  
  174. ";
  175. $test = new Transposer($song,2);
  176.  
  177.  
  178. ?>
Advertisement
Add Comment
Please, Sign In to add comment