Halftone.php
author Dan Fuhry <dan@enanocms.org>
Tue, 27 Mar 2012 11:58:43 -0400
changeset 1 87dfd1a261bd
parent 0 16db14829751
child 2 2f3b76a405ce
permissions -rw-r--r--
Several minor CSS changes to allow better integration with a theme designed for stage use and printing
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);');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    16
$plugins->attachHook('html_attribute_whitelist', '$whitelist["halftone"] = array("title", "key");');
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);');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    18
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    19
define('KEY_C', 0);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    20
define('KEY_D', 2);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    21
define('KEY_E', 4);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    22
define('KEY_F', 5);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    23
define('KEY_G', 7);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    24
define('KEY_A', 9);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    25
define('KEY_B', 11);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    26
define('KEY_C_SHARP', 1);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    27
define('KEY_E_FLAT', 3);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    28
define('KEY_F_SHARP', 6);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    29
define('KEY_G_SHARP', 8);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    30
define('KEY_B_FLAT', 10);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    31
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    32
define('ACC_FLAT', -1);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    33
define('ACC_SHARP', 1);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    34
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    35
$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
    36
$accidentals = array(
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    37
	KEY_C => ACC_FLAT,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    38
	KEY_G => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    39
	KEY_D => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    40
	KEY_A => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    41
	KEY_E => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    42
	KEY_B => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    43
	KEY_F_SHARP => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    44
	KEY_C_SHARP => ACC_SHARP,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    45
	KEY_G_SHARP => ACC_FLAT,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    46
	KEY_E_FLAT => ACC_FLAT,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    47
	KEY_B_FLAT => ACC_FLAT,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    48
	KEY_F => ACC_FLAT
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    49
);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    50
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    51
function get_consonants($root_key)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    52
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    53
	global $circle_of_fifths;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    54
	$first = $root_key;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    55
	$key = array_search($root_key, $circle_of_fifths);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    56
	$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
    57
	$fifth = $circle_of_fifths[($key + 1) % count($circle_of_fifths)];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    58
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    59
	$minor1 = $circle_of_fifths[($key + 2) % count($circle_of_fifths)];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    60
	$minor2 = $circle_of_fifths[($key + 3) % count($circle_of_fifths)];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    61
	$minor3 = $circle_of_fifths[($key + 4) % count($circle_of_fifths)];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    62
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    63
	$result = array(
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    64
			'first' => $first,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    65
			'fourth' => $fourth,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    66
			'fifth' => $fifth,
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    67
			'minors' => array($minor1, $minor2, $minor3)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    68
		);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    69
	return $result;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    70
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    71
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    72
function get_sharp($chord)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    73
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    74
	return key_to_name(name_to_key($chord), ACC_SHARP);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    75
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    76
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    77
function detect_key($chord_list)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    78
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    79
	$majors = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    80
	$minors = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    81
	$sharp_or_flat = ACC_SHARP;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    82
	// index which chords are used in the song
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    83
	foreach ( $chord_list as $chord )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    84
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    85
		// discard bass note
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    86
		list($chord) = explode('/', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    87
		$match = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    88
		preg_match('/((?:m?7?|2|add9|sus4|[Mm]aj[79])?)$/', $chord, $match);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    89
		if ( !empty($match[1]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    90
			$chord = str_replace_once($match[1], '', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    91
		$sharp_or_flat = get_sharp($chord) == $chord ? ACC_SHARP : ACC_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    92
		$chord = get_sharp($chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    93
		if ( $match[1] == 'm' || $match[1] == 'm7' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    94
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    95
			// minor chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    96
			if ( !isset($minors[$chord]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    97
				$minors[$chord] = 0;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    98
			$minors[$chord]++;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    99
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   100
		else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   101
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   102
			// major chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   103
			if ( !isset($majors[$chord]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   104
				$majors[$chord] = 0;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   105
			$majors[$chord]++;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   106
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   107
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   108
	// 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
   109
	$scores = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   110
	foreach ( $majors as $key => $count )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   111
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   112
		$scores[$key] = 0;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   113
		$consonants = get_consonants(name_to_key($key));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   114
		if ( isset($majors[key_to_name($consonants['fourth'])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   115
			$scores[$key] += 2;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   116
		if ( isset($majors[key_to_name($consonants['fifth'])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   117
			$scores[$key] += 2;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   118
		if ( isset($majors[key_to_name($consonants['minors'][0])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   119
			$scores[$key] += 1;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   120
		if ( isset($majors[key_to_name($consonants['minors'][1])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   121
			$scores[$key] += 2;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   122
		if ( isset($majors[key_to_name($consonants['minors'][2])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   123
			$scores[$key] += 1;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   124
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   125
	$winner_val = -1;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   126
	$winner_key = '';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   127
	foreach ( $scores as $key => $score )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   128
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   129
		if ( $score > $winner_val )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   130
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   131
			$winner_val = $score;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   132
			$winner_key = $key;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   133
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   134
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   135
	$winner_key = key_to_name(name_to_key($winner_key), $sharp_or_flat);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   136
	return $winner_key;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   137
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   138
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   139
function key_to_name($root_key, $accidental = ACC_SHARP)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   140
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   141
	switch($root_key)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   142
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   143
		case KEY_C:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   144
			return 'C';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   145
		case KEY_D:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   146
			return 'D';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   147
		case KEY_E:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   148
			return 'E';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   149
		case KEY_F:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   150
			return 'F';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   151
		case KEY_G:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   152
			return 'G';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   153
		case KEY_A:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   154
			return 'A';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   155
		case KEY_B:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   156
			return 'B';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   157
		case KEY_C_SHARP:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   158
			return $accidental == ACC_FLAT ? 'Db' : 'C#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   159
		case KEY_E_FLAT:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   160
			return $accidental == ACC_FLAT ? 'Eb' : 'D#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   161
		case KEY_F_SHARP:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   162
			return $accidental == ACC_FLAT ? 'Gb' : 'F#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   163
		case KEY_G_SHARP:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   164
			return $accidental == ACC_FLAT ? 'Ab' : 'G#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   165
		case KEY_B_FLAT:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   166
			return $accidental == ACC_FLAT ? 'Bb' : 'A#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   167
		default:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   168
			return false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   169
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   170
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   171
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   172
function name_to_key($name)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   173
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   174
	switch($name)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   175
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   176
		case 'C': return KEY_C;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   177
		case 'D': return KEY_D;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   178
		case 'E': return KEY_E;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   179
		case 'F': return KEY_F;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   180
		case 'G': return KEY_G;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   181
		case 'A': return KEY_A;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   182
		case 'B': return KEY_B;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   183
		case 'C#': case 'Db': return KEY_C_SHARP;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   184
		case 'D#': case 'Eb': return KEY_E_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   185
		case 'F#': case 'Gb': return KEY_F_SHARP;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   186
		case 'G#': case 'Ab': return KEY_G_SHARP;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   187
		case 'A#': case 'Bb': return KEY_B_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   188
		default: return false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   189
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   190
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   191
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   192
function prettify_accidentals($chord)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   193
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   194
	if ( count(explode('/', $chord)) > 1 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   195
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   196
		list($upper, $lower) = explode('/', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   197
		return prettify_accidentals($upper) . '/' . prettify_accidentals($lower);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   198
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   199
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   200
	if ( strlen($chord) < 2 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   201
		return $chord;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   202
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   203
	if ( $chord{1} == 'b' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   204
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   205
		$chord = $chord{0} . '&flat;' . substr($chord, 2);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   206
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   207
	else if ( $chord{1} == '#' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   208
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   209
		$chord = $chord{0} . '&sharp;' . substr($chord, 2);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   210
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   211
	return $chord;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   212
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   213
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   214
function transpose_chord($chord, $increment, $accidental = false)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   215
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   216
	global $circle_of_fifths;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   217
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   218
	if ( count(explode('/', $chord)) > 1 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   219
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   220
		list($upper, $lower) = explode('/', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   221
		return transpose_chord($upper, $increment, $accidental) . '/' . transpose_chord($lower, $increment, $accidental);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   222
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   223
	// shave off any wacky things we're doing to the chord (minor, seventh, etc.)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   224
	preg_match('/((?:m?7?|2|add9|sus4|[Mm]aj[79])?)$/', $chord, $match);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   225
	// find base chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   226
	if ( !empty($match[1]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   227
		$chord = str_replace($match[1], '', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   228
	// 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
   229
	if ( !$accidental )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   230
		$accidental = strstr($chord, '#') ? ACC_SHARP : ACC_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   231
	// convert to numeric value
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   232
	$key = name_to_key($chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   233
	if ( $key === false )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   234
		// should never happen
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   235
		return "[TRANSPOSITION FAILED: " . $chord . $match[1] . "]";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   236
	// transpose
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   237
	$key = (($key + $increment) + count($circle_of_fifths)) % count($circle_of_fifths);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   238
	// return result
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   239
	$kname = key_to_name($key, $accidental);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   240
	if ( !$kname )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   241
		// again, should never happen
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   242
		return "[TRANSPOSITION FAILED: " . $chord . $match[1] . " + $increment (-&gt;$key)]";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   243
	$result = $kname . $match[1];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   244
	// echo "$chord{$match[1]} + $increment = $result<br />";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   245
	return $result;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   246
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   247
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   248
function halftone_process_tags(&$text)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   249
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   250
	static $css_added = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   251
	if ( !$css_added )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   252
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   253
		global $template;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   254
		$template->preload_js(array('jquery', 'jquery-ui'));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   255
		$template->add_header('
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   256
			<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
   257
				div.halftone {
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   258
					page-break-before: always;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   259
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   260
				span.halftone-line {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   261
					display: block;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   262
					padding-top: 10pt;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   263
					position: relative; /* allows the absolute positioning in chords to work */
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   264
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   265
				span.halftone-chord {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   266
					position: absolute;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   267
					top: 0pt;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   268
					color: rgb(27, 104, 184);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   269
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   270
				span.halftone-chord.sequential {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   271
					padding-left: 20pt;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   272
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   273
				div.halftone-key-select {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   274
					float: right;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   275
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   276
			</style>
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   277
			<script type="text/javascript">
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   278
				addOnloadHook(function()
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   279
					{
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
   280
						$("div.halftone").get(0).style.pageBreakBefore = "auto";
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   281
						$("select.halftone-key").change(function()
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   282
							{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   283
								var me = this;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   284
								var src = $(this.parentNode.parentNode).attr("halftone:src");
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   285
								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
   286
									{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   287
										if ( ajax.readyState == 4 && ajax.status == 200 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   288
										{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   289
											var $songbody = $("div.halftone-song", me.parentNode.parentNode);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   290
											$songbody.html(ajax.responseText);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   291
										}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   292
									});
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   293
							});
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   294
					});
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   295
			</script>
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   296
			');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   297
		$css_added = true;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   298
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   299
	if ( preg_match_all('/<halftone(.*?)>(.+?)<\/halftone>/s', $text, $matches) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   300
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   301
		foreach ( $matches[0] as $i => $whole_match )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   302
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   303
			$attribs = decodeTagAttributes($matches[1][$i]);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   304
			$song_title = isset($attribs['title']) ? $attribs['title'] : 'Untitled song';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   305
			$chord_list = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   306
			$inner = trim($matches[2][$i]);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   307
			$song = halftone_render_body($inner, $chord_list);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   308
			$src = base64_encode($whole_match);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   309
			$key = name_to_key(detect_key($chord_list));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   310
			$select = '<select class="halftone-key">';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   311
			for ( $i = 0; $i < 12; $i++ )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   312
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   313
				$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
   314
				$label = prettify_accidentals($label);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   315
				$sel = $key == $i ? ' selected="selected"' : '';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   316
				$select .= sprintf("<option%s value=\"%d\" halftone:abs=\"%d\">%s</option>", $sel, $i - $key, $i, $label);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   317
			}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   318
			$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
   319
			$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
   320
			$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
   321
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   322
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   323
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   324
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   325
function halftone_render_body($inner, &$chord_list, $inkey = false)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   326
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   327
	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
   328
	$song = '<div class="section">';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   329
	$chord_list = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   330
	$transpose = isset($_GET['transpose']) ? intval($_GET['transpose']) : 0;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   331
	$transpose_accidental = $inkey ? $accidentals[$inkey] : false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   332
	foreach ( explode("\n", $inner) as $line )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   333
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   334
		$chordline = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   335
		$chords_regex = '/(\((?:[A-G][#b]?(?:m?7?|2|add9|sus4|[Mm]aj[79])?(?:\/[A-G][#b]?)?)\))/';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   336
		$line_split = preg_split($chords_regex, $line, -1, PREG_SPLIT_DELIM_CAPTURE);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   337
		if ( preg_match_all($chords_regex, $line, $chords) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   338
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   339
			// this is a line with lyrics + chords
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   340
			// echo out the line, adding spans around chords. here is where we also do transposition
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   341
			// (if requested) and 
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   342
			$line_final = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   343
			$last_was_chord = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   344
			foreach ( $line_split as $entry )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   345
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   346
				if ( preg_match($chords_regex, $entry) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   347
				{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   348
					if ( $last_was_chord )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   349
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   350
						while ( !($pop = array_pop($line_final)) );
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   351
						$new_entry = preg_replace('#</span>$#', '', $pop);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   352
						$new_entry .= str_repeat('&nbsp;', 4);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   353
						$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
   354
						$line_final[] = $new_entry;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   355
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   356
					else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   357
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   358
						$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
   359
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   360
					$last_was_chord = true;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   361
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   362
				else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   363
				{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   364
					if ( trim($entry) != "" )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   365
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   366
						$last_was_chord = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   367
						$line_final[] = $entry;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   368
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   369
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   370
			}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   371
			$song .= '<span class="halftone-line">' . implode("", $line_final) . "</span>\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   372
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   373
		else if ( preg_match('/^=\s*(.+?)\s*=$/', $line, $match) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   374
		{
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
   375
			$song .= "</div>\n<div class=\"section\">\n== {$match[1]} ==\n\n";
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   376
		} 
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   377
		else if ( trim($line) == '' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   378
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   379
			continue;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   380
		}
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
   381
		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
   382
		{
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
   383
			$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
   384
		}
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   385
		else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   386
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   387
			$song .= "$line<br />\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   388
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   389
	}
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
   390
	return $song . '</div>';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   391
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   392
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   393
function page_Special_HalftoneRender()
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   394
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   395
	global $accidentals;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   396
	$text = isset($_POST['src']) ? base64_decode($_POST['src']) : '';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   397
	if ( preg_match('/<halftone(.*?)>(.+?)<\/halftone>/s', $text, $match) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   398
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   399
		require_once(ENANO_ROOT . '/includes/wikiformat.php');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   400
		$carp = new Carpenter();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   401
		$carp->exclusive_rule('heading');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   402
		$tokey = isset($_GET['tokey']) ? intval($_GET['tokey']) : false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   403
		echo $carp->render(halftone_render_body($match[2], $chord_list, $tokey));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   404
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   405
}