Halftone.php
changeset 7 3a98ce7762a0
parent 6 07ceb3e35e25
equal deleted inserted replaced
6:07ceb3e35e25 7:3a98ce7762a0
    75 	return key_to_name(name_to_key($chord), ACC_SHARP);
    75 	return key_to_name(name_to_key($chord), ACC_SHARP);
    76 }
    76 }
    77 
    77 
    78 function detect_key($chord_list)
    78 function detect_key($chord_list)
    79 {
    79 {
       
    80 	global $circle_of_fifths;
       
    81 	
    80 	$majors = array();
    82 	$majors = array();
    81 	$minors = array();
    83 	$minors = array();
    82 	$sharp_or_flat = ACC_SHARP;
    84 	$sharp_or_flat = ACC_SHARP;
    83 	// sus4 chords are also a great indicator since they are almost always
    85 	// sus4 chords are also a great indicator since they are almost always
    84 	// used exclusively on the fifth
    86 	// used exclusively on the fifth
   125 	{
   127 	{
   126 		if ( $count < 1 )
   128 		if ( $count < 1 )
   127 			unset($majors[$key]);
   129 			unset($majors[$key]);
   128 	}
   130 	}
   129 	*/
   131 	*/
       
   132 	// go through the circle of fifths, and if both its fourth and fifth are in the song but the root is not, add it
       
   133 	foreach ( $circle_of_fifths as $key )
       
   134 	{
       
   135 		$consonants = get_consonants($key);
       
   136 		if ( isset($majors[ key_to_name($consonants['fourth']) ]) &&
       
   137 			 isset($majors[ key_to_name($consonants['fifth']) ]) &&
       
   138 		 	!isset($majors[ key_to_name($consonants['first']) ]) )
       
   139 			// I call this the Kutless Exception. The song does not contain its root chord. This just adds
       
   140 			// that root to the list of possibilities, and it needs to score high enough to beat out the
       
   141 			// others.
       
   142 			$majors[ key_to_name($key) ] = 0;
       
   143 	}
   130 	// now we go through each of the detected major chords, calculate its consonants, and determine how many of its consonants are present in the song.
   144 	// now we go through each of the detected major chords, calculate its consonants, and determine how many of its consonants are present in the song.
   131 	$scores = array();
   145 	$scores = array();
   132 	foreach ( $majors as $key => $count )
   146 	foreach ( $majors as $key => $count )
   133 	{
   147 	{
   134 		$scores[$key] = 0;
   148 		$scores[$key] = 0;
       
   149 		
   135 		$consonants = get_consonants(name_to_key($key));
   150 		$consonants = get_consonants(name_to_key($key));
       
   151 		
   136 		if ( isset($majors[key_to_name($consonants['fourth'])]) )
   152 		if ( isset($majors[key_to_name($consonants['fourth'])]) )
   137 			$scores[$key] += 2;
   153 			$scores[$key] += 2;
   138 		if ( isset($majors[key_to_name($consonants['fifth'])]) )
   154 		if ( isset($majors[key_to_name($consonants['fifth'])]) )
   139 			$scores[$key] += $have_sus4 === key_to_name($consonants['fifth']) ? 4 : 2;
   155 			$scores[$key] += $have_sus4 === key_to_name($consonants['fifth']) ? 4 : 2;
   140 		if ( isset($majors[key_to_name($consonants['minors'][0])]) )
   156 		if ( isset($majors[key_to_name($consonants['minors'][0])]) )