Halftone.php
author Dan Fuhry <dan@enanocms.org>
Fri, 12 Oct 2012 22:58:08 -0400
changeset 7 3a98ce7762a0
parent 6 07ceb3e35e25
permissions -rw-r--r--
Fixed key detection for Kutless songs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     1
<?php
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     2
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     3
/**!info**
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     4
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     5
  "Plugin Name"  : "Halftone",
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     6
  "Plugin URI"   : "http://enanocms.org/plugin/halftone",
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     7
  "Description"  : "Allows semantic input and transposition of chord sheets.",
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     8
  "Author"       : "Dan Fuhry",
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     9
  "Version"      : "0.1",
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    10
  "Author URI"   : "http://enanocms.org/",
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    11
  "Version list" : ['0.1']
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    12
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    13
**!*/
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    14
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    15
$plugins->attachHook('render_wikiformat_posttemplates', 'halftone_process_tags($text);');
3
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
    16
$plugins->attachHook('html_attribute_whitelist', '$whitelist["halftone"] = array("title", "transpose");');
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    17
$plugins->attachHook('session_started', 'register_special_page(\'HalftoneRender\', \'Halftone AJAX render handler\', false);');
6
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
    18
$plugins->attachHook('render_getpage_norender', 'halftone_set_keys_from_tpl_vars($text);');
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    19
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    20
define('KEY_C', 0);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    21
define('KEY_D', 2);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    22
define('KEY_E', 4);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    23
define('KEY_F', 5);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    24
define('KEY_G', 7);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    25
define('KEY_A', 9);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    26
define('KEY_B', 11);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    27
define('KEY_C_SHARP', 1);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    28
define('KEY_E_FLAT', 3);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    29
define('KEY_F_SHARP', 6);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    30
define('KEY_G_SHARP', 8);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    31
define('KEY_B_FLAT', 10);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    32
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    33
define('ACC_FLAT', -1);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    34
define('ACC_SHARP', 1);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    35
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    36
$circle_of_fifths = array(KEY_C, KEY_G, KEY_D, KEY_A, KEY_E, KEY_B, KEY_F_SHARP, KEY_C_SHARP, KEY_G_SHARP, KEY_E_FLAT, KEY_B_FLAT, KEY_F);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    37
$accidentals = array(
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    38
	KEY_C => ACC_FLAT,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    39
	KEY_G => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    40
	KEY_D => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    41
	KEY_A => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    42
	KEY_E => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    43
	KEY_B => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    44
	KEY_F_SHARP => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    45
	KEY_C_SHARP => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    46
	KEY_G_SHARP => ACC_FLAT,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    47
	KEY_E_FLAT => ACC_FLAT,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    48
	KEY_B_FLAT => ACC_FLAT,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    49
	KEY_F => ACC_FLAT
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    50
);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    51
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    52
function get_consonants($root_key)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    53
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    54
	global $circle_of_fifths;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    55
	$first = $root_key;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    56
	$key = array_search($root_key, $circle_of_fifths);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    57
	$fourth = $circle_of_fifths[(($key - 1) + count($circle_of_fifths)) % count($circle_of_fifths)];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    58
	$fifth = $circle_of_fifths[($key + 1) % count($circle_of_fifths)];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    59
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    60
	$minor1 = $circle_of_fifths[($key + 2) % count($circle_of_fifths)];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    61
	$minor2 = $circle_of_fifths[($key + 3) % count($circle_of_fifths)];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    62
	$minor3 = $circle_of_fifths[($key + 4) % count($circle_of_fifths)];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    63
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    64
	$result = array(
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    65
			'first' => $first,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    66
			'fourth' => $fourth,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    67
			'fifth' => $fifth,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    68
			'minors' => array($minor1, $minor2, $minor3)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    69
		);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    70
	return $result;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    71
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    72
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    73
function get_sharp($chord)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    74
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    75
	return key_to_name(name_to_key($chord), ACC_SHARP);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    76
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    77
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    78
function detect_key($chord_list)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    79
{
7
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
    80
	global $circle_of_fifths;
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
    81
	
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    82
	$majors = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    83
	$minors = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    84
	$sharp_or_flat = ACC_SHARP;
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
    85
	// sus4 chords are also a great indicator since they are almost always
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
    86
	// used exclusively on the fifth
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
    87
	$have_sus4 = false;
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    88
	// index which chords are used in the song
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    89
	foreach ( $chord_list as $chord )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    90
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    91
		// discard bass note
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    92
		list($chord) = explode('/', $chord);
5
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    93
		// skip chord if it has a "!"
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    94
		if ( $chord{0} == '!' )
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    95
		{
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    96
			continue;
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    97
		}
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    98
		
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    99
		$match = array();
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   100
		preg_match('/((?:[Mm]?7?|2|5|6|add9|sus4|[Mm]aj[79]|dim|aug)?)$/', $chord, $match);
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   101
		if ( !empty($match[1]) )
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   102
		{
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   103
			$chord = str_replace_once($match[1], '', $chord);
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   104
			if ( $match[1] === 'sus4' )
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   105
				$have_sus4 = $chord;
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   106
		}
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   107
		$sharp_or_flat = get_sharp($chord) == $chord ? ACC_SHARP : ACC_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   108
		$chord = get_sharp($chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   109
		if ( $match[1] == 'm' || $match[1] == 'm7' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   110
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   111
			// minor chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   112
			if ( !isset($minors[$chord]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   113
				$minors[$chord] = 0;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   114
			$minors[$chord]++;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   115
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   116
		else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   117
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   118
			// major chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   119
			if ( !isset($majors[$chord]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   120
				$majors[$chord] = 0;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   121
			$majors[$chord]++;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   122
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   123
	}
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   124
	/*
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   125
	// remove very low scorers
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   126
	foreach ( $majors as $key => $count )
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   127
	{
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   128
		if ( $count < 1 )
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   129
			unset($majors[$key]);
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   130
	}
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   131
	*/
7
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   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
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   133
	foreach ( $circle_of_fifths as $key )
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   134
	{
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   135
		$consonants = get_consonants($key);
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   136
		if ( isset($majors[ key_to_name($consonants['fourth']) ]) &&
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   137
			 isset($majors[ key_to_name($consonants['fifth']) ]) &&
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   138
		 	!isset($majors[ key_to_name($consonants['first']) ]) )
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   139
			// I call this the Kutless Exception. The song does not contain its root chord. This just adds
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   140
			// that root to the list of possibilities, and it needs to score high enough to beat out the
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   141
			// others.
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   142
			$majors[ key_to_name($key) ] = 0;
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   143
	}
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   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.
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   145
	$scores = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   146
	foreach ( $majors as $key => $count )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   147
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   148
		$scores[$key] = 0;
7
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   149
		
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   150
		$consonants = get_consonants(name_to_key($key));
7
3a98ce7762a0 Fixed key detection for Kutless songs
Dan Fuhry <dan@enanocms.org>
parents: 6
diff changeset
   151
		
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   152
		if ( isset($majors[key_to_name($consonants['fourth'])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   153
			$scores[$key] += 2;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   154
		if ( isset($majors[key_to_name($consonants['fifth'])]) )
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   155
			$scores[$key] += $have_sus4 === key_to_name($consonants['fifth']) ? 4 : 2;
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   156
		if ( isset($majors[key_to_name($consonants['minors'][0])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   157
			$scores[$key] += 1;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   158
		if ( isset($majors[key_to_name($consonants['minors'][1])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   159
			$scores[$key] += 2;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   160
		if ( isset($majors[key_to_name($consonants['minors'][2])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   161
			$scores[$key] += 1;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   162
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   163
	$winner_val = -1;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   164
	$winner_key = '';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   165
	foreach ( $scores as $key => $score )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   166
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   167
		if ( $score > $winner_val )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   168
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   169
			$winner_val = $score;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   170
			$winner_key = $key;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   171
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   172
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   173
	$winner_key = key_to_name(name_to_key($winner_key), $sharp_or_flat);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   174
	return $winner_key;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   175
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   176
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   177
function key_to_name($root_key, $accidental = ACC_SHARP)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   178
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   179
	switch($root_key)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   180
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   181
		case KEY_C:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   182
			return 'C';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   183
		case KEY_D:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   184
			return 'D';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   185
		case KEY_E:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   186
			return 'E';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   187
		case KEY_F:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   188
			return 'F';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   189
		case KEY_G:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   190
			return 'G';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   191
		case KEY_A:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   192
			return 'A';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   193
		case KEY_B:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   194
			return 'B';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   195
		case KEY_C_SHARP:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   196
			return $accidental == ACC_FLAT ? 'Db' : 'C#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   197
		case KEY_E_FLAT:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   198
			return $accidental == ACC_FLAT ? 'Eb' : 'D#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   199
		case KEY_F_SHARP:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   200
			return $accidental == ACC_FLAT ? 'Gb' : 'F#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   201
		case KEY_G_SHARP:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   202
			return $accidental == ACC_FLAT ? 'Ab' : 'G#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   203
		case KEY_B_FLAT:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   204
			return $accidental == ACC_FLAT ? 'Bb' : 'A#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   205
		default:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   206
			return false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   207
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   208
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   209
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   210
function name_to_key($name)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   211
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   212
	switch($name)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   213
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   214
		case 'C': return KEY_C;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   215
		case 'D': return KEY_D;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   216
		case 'E': return KEY_E;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   217
		case 'F': return KEY_F;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   218
		case 'G': return KEY_G;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   219
		case 'A': return KEY_A;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   220
		case 'B': return KEY_B;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   221
		case 'C#': case 'Db': return KEY_C_SHARP;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   222
		case 'D#': case 'Eb': return KEY_E_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   223
		case 'F#': case 'Gb': return KEY_F_SHARP;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   224
		case 'G#': case 'Ab': return KEY_G_SHARP;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   225
		case 'A#': case 'Bb': return KEY_B_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   226
		default: return false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   227
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   228
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   229
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   230
function prettify_accidentals($chord)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   231
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   232
	if ( count(explode('/', $chord)) > 1 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   233
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   234
		list($upper, $lower) = explode('/', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   235
		return prettify_accidentals($upper) . '/' . prettify_accidentals($lower);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   236
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   237
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   238
	if ( strlen($chord) < 2 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   239
		return $chord;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   240
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   241
	if ( $chord{1} == 'b' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   242
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   243
		$chord = $chord{0} . '&flat;' . substr($chord, 2);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   244
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   245
	else if ( $chord{1} == '#' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   246
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   247
		$chord = $chord{0} . '&sharp;' . substr($chord, 2);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   248
	}
5
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   249
	return ltrim($chord, '!');
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   250
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   251
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   252
function transpose_chord($chord, $increment, $accidental = false)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   253
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   254
	global $circle_of_fifths;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   255
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   256
	if ( count(explode('/', $chord)) > 1 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   257
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   258
		list($upper, $lower) = explode('/', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   259
		return transpose_chord($upper, $increment, $accidental) . '/' . transpose_chord($lower, $increment, $accidental);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   260
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   261
	// shave off any wacky things we're doing to the chord (minor, seventh, etc.)
5
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   262
	$prechord = '';
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   263
	if ( $chord{0} == '!' )
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   264
	{
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   265
		$prechord = '!';
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   266
		$chord = substr($chord, 1);
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   267
	}
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   268
	preg_match('/((?:[Mm]?7?|2|5|6|add9|sus4|[Mm]aj[79]|dim|aug)?)$/', $chord, $match);
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   269
	// find base chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   270
	if ( !empty($match[1]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   271
		$chord = str_replace($match[1], '', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   272
	// what's our accidental? allow it to be specified, and autodetect if it isn't
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   273
	if ( !$accidental )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   274
		$accidental = strstr($chord, '#') ? ACC_SHARP : ACC_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   275
	// convert to numeric value
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   276
	$key = name_to_key($chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   277
	if ( $key === false )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   278
		// should never happen
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   279
		return "[TRANSPOSITION FAILED: " . $chord . $match[1] . "]";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   280
	// transpose
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   281
	$key = (($key + $increment) + count($circle_of_fifths)) % count($circle_of_fifths);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   282
	// return result
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   283
	$kname = key_to_name($key, $accidental);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   284
	if ( !$kname )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   285
		// again, should never happen
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   286
		return "[TRANSPOSITION FAILED: " . $chord . $match[1] . " + $increment (-&gt;$key)]";
5
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   287
	$result = $prechord . $kname . $match[1];
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   288
	// echo "$chord{$match[1]} + $increment = $result<br />";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   289
	return $result;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   290
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   291
6
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   292
function halftone_set_keys_from_tpl_vars(&$text)
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   293
{
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   294
	global $db, $session, $paths, $template, $plugins; // Common objects
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   295
	
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   296
	// did they specify a key?
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   297
	if ( !isset($template->tpl_strings['key']) )
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   298
	{
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   299
		return false;
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   300
	}
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   301
	
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   302
	// is the key valid?
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   303
	if ( !is_string(key_to_name($template->tpl_strings['key'])) )
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   304
	{
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   305
		return false;
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   306
	}
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   307
	
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   308
	if ( preg_match_all('/<halftone(.*?)>(.+?)<\/halftone>/s', $text, $matches) )
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   309
	{
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   310
		foreach ( $matches[0] as $i => $whole_match )
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   311
		{
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   312
			$attribs = decodeTagAttributes($matches[1][$i]);
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   313
			$attribs['transpose'] = $template->tpl_strings['key'];
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   314
			
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   315
			// re-encode tag attributes
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   316
			$attribs_encoded = '';
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   317
			foreach ( $attribs as $k => $v )
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   318
			{
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   319
				$attribs_encoded .= sprintf(" %s=\"%s\"", $k, htmlspecialchars($v));
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   320
			}
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   321
			
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   322
			$new_match = str_replace_once('<halftone', "<halftone{$attribs_encoded}", str_replace_once($matches[1][$i], '', $whole_match));
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   323
			$text = str_replace_once($whole_match, $new_match, $text);
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   324
		}
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   325
	}
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   326
}
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   327
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   328
function halftone_process_tags(&$text)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   329
{
3
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   330
	global $circle_of_fifths;
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   331
	
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   332
	static $css_added = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   333
	if ( !$css_added )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   334
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   335
		global $template;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   336
		$template->preload_js(array('jquery', 'jquery-ui'));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   337
		$template->add_header('
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   338
			<style type="text/css">
1
87dfd1a261bd Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
   339
				div.halftone {
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   340
					page-break-before: always;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   341
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   342
				span.halftone-line {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   343
					display: block;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   344
					padding-top: 10pt;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   345
					position: relative; /* allows the absolute positioning in chords to work */
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   346
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   347
				span.halftone-chord {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   348
					position: absolute;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   349
					top: 0pt;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   350
					color: rgb(27, 104, 184);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   351
				}
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   352
				span.halftone-line.labeled span.halftone-chord {
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   353
					position: static;
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   354
				}
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   355
				span.halftone-chord.sequential {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   356
					padding-left: 20pt;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   357
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   358
				div.halftone-key-select {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   359
					float: right;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   360
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   361
			</style>
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   362
			<script type="text/javascript">
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   363
				addOnloadHook(function()
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   364
					{
2
2f3b76a405ce Fixed issue where js would break on pages with no halftone blocks
Dan Fuhry <dan@enanocms.org>
parents: 1
diff changeset
   365
						var first_ht = $("div.halftone").get(0);
2f3b76a405ce Fixed issue where js would break on pages with no halftone blocks
Dan Fuhry <dan@enanocms.org>
parents: 1
diff changeset
   366
						if ( first_ht )
2f3b76a405ce Fixed issue where js would break on pages with no halftone blocks
Dan Fuhry <dan@enanocms.org>
parents: 1
diff changeset
   367
							first_ht.style.pageBreakBefore = "auto";
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   368
						$("select.halftone-key").change(function()
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   369
							{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   370
								var me = this;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   371
								var src = $(this.parentNode.parentNode).attr("halftone:src");
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   372
								ajaxPost(makeUrlNS("Special", "HalftoneRender", "transpose=" + $(this).val()) + "&tokey=" + $("option:selected", this).attr("halftone:abs"), "src=" + encodeURIComponent(src), function(ajax)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   373
									{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   374
										if ( ajax.readyState == 4 && ajax.status == 200 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   375
										{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   376
											var $songbody = $("div.halftone-song", me.parentNode.parentNode);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   377
											$songbody.html(ajax.responseText);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   378
										}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   379
									});
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   380
							});
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   381
					});
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   382
			</script>
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   383
			');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   384
		$css_added = true;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   385
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   386
	if ( preg_match_all('/<halftone(.*?)>(.+?)<\/halftone>/s', $text, $matches) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   387
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   388
		foreach ( $matches[0] as $i => $whole_match )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   389
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   390
			$attribs = decodeTagAttributes($matches[1][$i]);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   391
			$song_title = isset($attribs['title']) ? $attribs['title'] : 'Untitled song';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   392
			$chord_list = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   393
			$inner = trim($matches[2][$i]);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   394
			$song = halftone_render_body($inner, $chord_list);
3
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   395
			
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   396
			$src = base64_encode($whole_match);
3
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   397
			$origkey = $key = name_to_key(detect_key($chord_list));
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   398
			if ( isset($attribs['transpose']) && is_int($tokey = name_to_key($attribs['transpose'])) )
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   399
			{
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   400
				// re-render in new key
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   401
				$transpose = $tokey - $key;
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   402
				$song = halftone_render_body($inner, $chord_list, $tokey, $transpose);
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   403
				$key = $tokey;
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   404
			}
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   405
			$select = '<select class="halftone-key">';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   406
			for ( $i = 0; $i < 12; $i++ )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   407
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   408
				$label = in_array($i, array(KEY_C_SHARP, KEY_E_FLAT, KEY_F_SHARP, KEY_G_SHARP, KEY_B_FLAT)) ? sprintf("%s/%s", key_to_name($i, ACC_SHARP), key_to_name($i, ACC_FLAT)) : key_to_name($i);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   409
				$label = prettify_accidentals($label);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   410
				$sel = $key == $i ? ' selected="selected"' : '';
3
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   411
				$select .= sprintf("<option%s value=\"%d\" halftone:abs=\"%d\">%s</option>", $sel, $i - $origkey, $i, $label);
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   412
			}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   413
			$select .= '</select>';
1
87dfd1a261bd Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
   414
			$headid = 'song:' . sanitize_page_id($song_title);
87dfd1a261bd Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
   415
			$text = str_replace_once($whole_match, "<div id=\"$headid\" class=\"halftone\" halftone:src=\"$src\"><div class=\"halftone-key-select\">$select</div><h1 class=\"halftone-title\">$song_title</h1>\n\n<div class=\"halftone-song\">\n" . $song . "</div></div>", $text);
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   416
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   417
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   418
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   419
3
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   420
function halftone_render_body($inner, &$chord_list, $inkey = false, $transpose = 0)
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   421
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   422
	global $accidentals;
1
87dfd1a261bd Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
   423
	$song = '<div class="section">';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   424
	$chord_list = array();
3
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   425
	$transpose = isset($_GET['transpose']) ? intval($_GET['transpose']) : $transpose;
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   426
	$transpose_accidental = $inkey ? $accidentals[$inkey] : false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   427
	foreach ( explode("\n", $inner) as $line )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   428
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   429
		$chordline = false;
5
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   430
		$chords_regex = '/(\((?:\!?[A-G][#b]?(?:[Mm]?7?|2|5|6|add9|sus4|[Mm]aj[79]|dim|aug)?(?:\/[A-G][#b]?)?)\))/';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   431
		$line_split = preg_split($chords_regex, $line, -1, PREG_SPLIT_DELIM_CAPTURE);
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   432
		$line_pattern = '';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   433
		if ( preg_match_all($chords_regex, $line, $chords) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   434
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   435
			// this is a line with lyrics + chords
5
c36fbf04faac Added "!" trick to give hints to the key detection algorithm. It's all Chris Tomlin's fault.
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
   436
			// echo out the line, adding spans around chords.
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   437
			$line_final = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   438
			$last_was_chord = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   439
			foreach ( $line_split as $entry )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   440
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   441
				if ( preg_match($chords_regex, $entry) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   442
				{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   443
					if ( $last_was_chord )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   444
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   445
						while ( !($pop = array_pop($line_final)) );
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   446
						$new_entry = preg_replace('#</span>$#', '', $pop);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   447
						$new_entry .= str_repeat('&nbsp;', 4);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   448
						$new_entry .= prettify_accidentals($chord_list[] = transpose_chord(trim($entry, '()'), $transpose, $transpose_accidental)) . '</span>';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   449
						$line_final[] = $new_entry;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   450
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   451
					else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   452
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   453
						$line_final[] = '<span class="halftone-chord">' . prettify_accidentals($chord_list[] = transpose_chord(trim($entry, '()'), $transpose, $transpose_accidental)) . '</span>';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   454
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   455
					$last_was_chord = true;
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   456
					$line_pattern .= 'c';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   457
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   458
				else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   459
				{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   460
					if ( trim($entry) != "" )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   461
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   462
						$last_was_chord = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   463
						$line_final[] = $entry;
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   464
						$line_pattern .= 'w';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   465
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   466
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   467
			}
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   468
			$class_append = preg_match('/^w?c+$/', $line_pattern) ? ' labeled' : '';
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   469
			$song .= '<span class="halftone-line' . $class_append . '">' . implode("", $line_final) . "</span>\n";
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   470
		}
6
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   471
		else if ( preg_match('/^=\s*(.+?)\s*=\r?$/', $line, $match) )
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   472
		{
1
87dfd1a261bd Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
   473
			$song .= "</div>\n<div class=\"section\">\n== {$match[1]} ==\n\n";
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   474
		} 
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   475
		else if ( trim($line) == '' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   476
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   477
			continue;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   478
		}
1
87dfd1a261bd Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
   479
		else if ( preg_match('/^\[(.+)\]$/', trim($line), $match) )
87dfd1a261bd Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
   480
		{
87dfd1a261bd Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
   481
			$song .= "<br /><strong>Jump to:</strong> {$match[1]}\n";
87dfd1a261bd Several minor CSS changes to allow better integration with a theme designed for stage use and printing
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
   482
		}
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   483
		else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   484
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   485
			$song .= "$line<br />\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   486
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   487
	}
6
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   488
	$song .= '</div>';
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   489
	
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   490
	//header('Content-type: text/plain');
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   491
	//die($song);
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   492
	
07ceb3e35e25 Added support for the render_getpage_norender hook, allowing transposition to take place via the "key" parameter when including Halftone pages as templates.
Dan Fuhry <dan@enanocms.org>
parents: 5
diff changeset
   493
	return $song;
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   494
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   495
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   496
function page_Special_HalftoneRender()
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   497
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   498
	global $accidentals;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   499
	$text = isset($_POST['src']) ? base64_decode($_POST['src']) : '';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   500
	if ( preg_match('/<halftone(.*?)>(.+?)<\/halftone>/s', $text, $match) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   501
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   502
		require_once(ENANO_ROOT . '/includes/wikiformat.php');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   503
		$carp = new Carpenter();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   504
		$carp->exclusive_rule('heading');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   505
		$tokey = isset($_GET['tokey']) ? intval($_GET['tokey']) : false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   506
		echo $carp->render(halftone_render_body($match[2], $chord_list, $tokey));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   507
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   508
}