Halftone.php
author Dan Fuhry <dan@enanocms.org>
Tue, 08 May 2012 01:38:29 -0400
changeset 4 bb3789db954a
parent 3 c3150cee8dd9
child 5 c36fbf04faac
permissions -rw-r--r--
Several bugfixes and improvements. - Better key detection by using sus4 as a hint - Recognize major seventh, diminished and augmented chords - Better handling of lines which are a label followed by several chords (i.e. "Intro: (C) (G) (Am) (F)")
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);');
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;
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
    82
	// 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
    83
	// used exclusively on the fifth
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
    84
	$have_sus4 = false;
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    85
	// index which chords are used in the song
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    86
	foreach ( $chord_list as $chord )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    87
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    88
		// discard bass note
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    89
		list($chord) = explode('/', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    90
		$match = array();
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
    91
		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
    92
		if ( !empty($match[1]) )
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
    93
		{
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    94
			$chord = str_replace_once($match[1], '', $chord);
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
    95
			if ( $match[1] === 'sus4' )
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
    96
				$have_sus4 = $chord;
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
    97
		}
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    98
		$sharp_or_flat = get_sharp($chord) == $chord ? ACC_SHARP : ACC_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    99
		$chord = get_sharp($chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   100
		if ( $match[1] == 'm' || $match[1] == 'm7' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   101
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   102
			// minor chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   103
			if ( !isset($minors[$chord]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   104
				$minors[$chord] = 0;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   105
			$minors[$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
		else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   108
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   109
			// major chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   110
			if ( !isset($majors[$chord]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   111
				$majors[$chord] = 0;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   112
			$majors[$chord]++;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   113
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   114
	}
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   115
	/*
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   116
	// remove very low scorers
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   117
	foreach ( $majors as $key => $count )
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   118
	{
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   119
		if ( $count < 1 )
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   120
			unset($majors[$key]);
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   121
	}
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   122
	*/
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   123
	// 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
   124
	$scores = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   125
	foreach ( $majors as $key => $count )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   126
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   127
		$scores[$key] = 0;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   128
		$consonants = get_consonants(name_to_key($key));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   129
		if ( isset($majors[key_to_name($consonants['fourth'])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   130
			$scores[$key] += 2;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   131
		if ( isset($majors[key_to_name($consonants['fifth'])]) )
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   132
			$scores[$key] += $have_sus4 === key_to_name($consonants['fifth']) ? 4 : 2;
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   133
		if ( isset($majors[key_to_name($consonants['minors'][0])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   134
			$scores[$key] += 1;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   135
		if ( isset($majors[key_to_name($consonants['minors'][1])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   136
			$scores[$key] += 2;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   137
		if ( isset($majors[key_to_name($consonants['minors'][2])]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   138
			$scores[$key] += 1;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   139
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   140
	$winner_val = -1;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   141
	$winner_key = '';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   142
	foreach ( $scores as $key => $score )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   143
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   144
		if ( $score > $winner_val )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   145
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   146
			$winner_val = $score;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   147
			$winner_key = $key;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   148
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   149
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   150
	$winner_key = key_to_name(name_to_key($winner_key), $sharp_or_flat);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   151
	return $winner_key;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   152
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   153
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   154
function key_to_name($root_key, $accidental = ACC_SHARP)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   155
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   156
	switch($root_key)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   157
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   158
		case KEY_C:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   159
			return 'C';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   160
		case KEY_D:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   161
			return 'D';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   162
		case KEY_E:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   163
			return 'E';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   164
		case KEY_F:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   165
			return 'F';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   166
		case KEY_G:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   167
			return 'G';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   168
		case KEY_A:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   169
			return 'A';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   170
		case KEY_B:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   171
			return 'B';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   172
		case KEY_C_SHARP:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   173
			return $accidental == ACC_FLAT ? 'Db' : 'C#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   174
		case KEY_E_FLAT:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   175
			return $accidental == ACC_FLAT ? 'Eb' : 'D#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   176
		case KEY_F_SHARP:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   177
			return $accidental == ACC_FLAT ? 'Gb' : 'F#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   178
		case KEY_G_SHARP:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   179
			return $accidental == ACC_FLAT ? 'Ab' : 'G#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   180
		case KEY_B_FLAT:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   181
			return $accidental == ACC_FLAT ? 'Bb' : 'A#';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   182
		default:
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   183
			return false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   184
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   185
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   186
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   187
function name_to_key($name)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   188
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   189
	switch($name)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   190
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   191
		case 'C': return KEY_C;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   192
		case 'D': return KEY_D;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   193
		case 'E': return KEY_E;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   194
		case 'F': return KEY_F;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   195
		case 'G': return KEY_G;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   196
		case 'A': return KEY_A;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   197
		case 'B': return KEY_B;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   198
		case 'C#': case 'Db': return KEY_C_SHARP;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   199
		case 'D#': case 'Eb': return KEY_E_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   200
		case 'F#': case 'Gb': return KEY_F_SHARP;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   201
		case 'G#': case 'Ab': return KEY_G_SHARP;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   202
		case 'A#': case 'Bb': return KEY_B_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   203
		default: return false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   204
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   205
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   206
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   207
function prettify_accidentals($chord)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   208
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   209
	if ( count(explode('/', $chord)) > 1 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   210
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   211
		list($upper, $lower) = explode('/', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   212
		return prettify_accidentals($upper) . '/' . prettify_accidentals($lower);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   213
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   214
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   215
	if ( strlen($chord) < 2 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   216
		return $chord;
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 ( $chord{1} == 'b' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   219
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   220
		$chord = $chord{0} . '&flat;' . substr($chord, 2);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   221
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   222
	else if ( $chord{1} == '#' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   223
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   224
		$chord = $chord{0} . '&sharp;' . substr($chord, 2);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   225
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   226
	return $chord;
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
function transpose_chord($chord, $increment, $accidental = false)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   230
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   231
	global $circle_of_fifths;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   232
	
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   233
	if ( count(explode('/', $chord)) > 1 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   234
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   235
		list($upper, $lower) = explode('/', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   236
		return transpose_chord($upper, $increment, $accidental) . '/' . transpose_chord($lower, $increment, $accidental);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   237
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   238
	// shave off any wacky things we're doing to the chord (minor, seventh, etc.)
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   239
	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
   240
	// find base chord
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   241
	if ( !empty($match[1]) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   242
		$chord = str_replace($match[1], '', $chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   243
	// 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
   244
	if ( !$accidental )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   245
		$accidental = strstr($chord, '#') ? ACC_SHARP : ACC_FLAT;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   246
	// convert to numeric value
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   247
	$key = name_to_key($chord);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   248
	if ( $key === false )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   249
		// should never happen
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   250
		return "[TRANSPOSITION FAILED: " . $chord . $match[1] . "]";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   251
	// transpose
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   252
	$key = (($key + $increment) + count($circle_of_fifths)) % count($circle_of_fifths);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   253
	// return result
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   254
	$kname = key_to_name($key, $accidental);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   255
	if ( !$kname )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   256
		// again, should never happen
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   257
		return "[TRANSPOSITION FAILED: " . $chord . $match[1] . " + $increment (-&gt;$key)]";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   258
	$result = $kname . $match[1];
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   259
	// echo "$chord{$match[1]} + $increment = $result<br />";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   260
	return $result;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   261
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   262
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   263
function halftone_process_tags(&$text)
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   264
{
3
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   265
	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
   266
	
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   267
	static $css_added = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   268
	if ( !$css_added )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   269
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   270
		global $template;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   271
		$template->preload_js(array('jquery', 'jquery-ui'));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   272
		$template->add_header('
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   273
			<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
   274
				div.halftone {
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   275
					page-break-before: always;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   276
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   277
				span.halftone-line {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   278
					display: block;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   279
					padding-top: 10pt;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   280
					position: relative; /* allows the absolute positioning in chords to work */
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   281
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   282
				span.halftone-chord {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   283
					position: absolute;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   284
					top: 0pt;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   285
					color: rgb(27, 104, 184);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   286
				}
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   287
				span.halftone-line.labeled span.halftone-chord {
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   288
					position: static;
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   289
				}
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   290
				span.halftone-chord.sequential {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   291
					padding-left: 20pt;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   292
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   293
				div.halftone-key-select {
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   294
					float: right;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   295
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   296
			</style>
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   297
			<script type="text/javascript">
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   298
				addOnloadHook(function()
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   299
					{
2
2f3b76a405ce Fixed issue where js would break on pages with no halftone blocks
Dan Fuhry <dan@enanocms.org>
parents: 1
diff changeset
   300
						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
   301
						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
   302
							first_ht.style.pageBreakBefore = "auto";
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   303
						$("select.halftone-key").change(function()
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   304
							{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   305
								var me = this;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   306
								var src = $(this.parentNode.parentNode).attr("halftone:src");
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   307
								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
   308
									{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   309
										if ( ajax.readyState == 4 && ajax.status == 200 )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   310
										{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   311
											var $songbody = $("div.halftone-song", me.parentNode.parentNode);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   312
											$songbody.html(ajax.responseText);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   313
										}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   314
									});
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   315
							});
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   316
					});
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   317
			</script>
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   318
			');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   319
		$css_added = true;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   320
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   321
	if ( preg_match_all('/<halftone(.*?)>(.+?)<\/halftone>/s', $text, $matches) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   322
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   323
		foreach ( $matches[0] as $i => $whole_match )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   324
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   325
			$attribs = decodeTagAttributes($matches[1][$i]);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   326
			$song_title = isset($attribs['title']) ? $attribs['title'] : 'Untitled song';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   327
			$chord_list = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   328
			$inner = trim($matches[2][$i]);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   329
			$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
   330
			
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   331
			$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
   332
			$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
   333
			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
   334
			{
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   335
				// 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
   336
				$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
   337
				$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
   338
				$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
   339
			}
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   340
			$select = '<select class="halftone-key">';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   341
			for ( $i = 0; $i < 12; $i++ )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   342
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   343
				$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
   344
				$label = prettify_accidentals($label);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   345
				$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
   346
				$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
   347
			}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   348
			$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
   349
			$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
   350
			$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
   351
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   352
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   353
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   354
3
c3150cee8dd9 Added support for the "transpose" tag which forces a song to be transposed.
Dan Fuhry <dan@enanocms.org>
parents: 2
diff changeset
   355
function halftone_render_body($inner, &$chord_list, $inkey = false, $transpose = 0)
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   356
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   357
	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
   358
	$song = '<div class="section">';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   359
	$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
   360
	$transpose = isset($_GET['transpose']) ? intval($_GET['transpose']) : $transpose;
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   361
	$transpose_accidental = $inkey ? $accidentals[$inkey] : false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   362
	foreach ( explode("\n", $inner) as $line )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   363
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   364
		$chordline = false;
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   365
		$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
   366
		$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
   367
		$line_pattern = '';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   368
		if ( preg_match_all($chords_regex, $line, $chords) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   369
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   370
			// this is a line with lyrics + chords
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   371
			// 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
   372
			// (if requested) and 
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   373
			$line_final = array();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   374
			$last_was_chord = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   375
			foreach ( $line_split as $entry )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   376
			{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   377
				if ( preg_match($chords_regex, $entry) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   378
				{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   379
					if ( $last_was_chord )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   380
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   381
						while ( !($pop = array_pop($line_final)) );
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   382
						$new_entry = preg_replace('#</span>$#', '', $pop);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   383
						$new_entry .= str_repeat('&nbsp;', 4);
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   384
						$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
   385
						$line_final[] = $new_entry;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   386
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   387
					else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   388
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   389
						$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
   390
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   391
					$last_was_chord = true;
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   392
					$line_pattern .= 'c';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   393
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   394
				else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   395
				{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   396
					if ( trim($entry) != "" )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   397
					{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   398
						$last_was_chord = false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   399
						$line_final[] = $entry;
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   400
						$line_pattern .= 'w';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   401
					}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   402
				}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   403
			}
4
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   404
			$class_append = preg_match('/^w?c+$/', $line_pattern) ? ' labeled' : '';
bb3789db954a Several bugfixes and improvements.
Dan Fuhry <dan@enanocms.org>
parents: 3
diff changeset
   405
			$song .= '<span class="halftone-line' . $class_append . '">' . implode("", $line_final) . "</span>\n";
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   406
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   407
		else if ( preg_match('/^=\s*(.+?)\s*=$/', $line, $match) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   408
		{
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
   409
			$song .= "</div>\n<div class=\"section\">\n== {$match[1]} ==\n\n";
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   410
		} 
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   411
		else if ( trim($line) == '' )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   412
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   413
			continue;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   414
		}
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
   415
		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
   416
		{
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
   417
			$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
   418
		}
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   419
		else
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   420
		{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   421
			$song .= "$line<br />\n";
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   422
		}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   423
	}
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
   424
	return $song . '</div>';
0
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   425
}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   426
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   427
function page_Special_HalftoneRender()
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   428
{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   429
	global $accidentals;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   430
	$text = isset($_POST['src']) ? base64_decode($_POST['src']) : '';
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   431
	if ( preg_match('/<halftone(.*?)>(.+?)<\/halftone>/s', $text, $match) )
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   432
	{
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   433
		require_once(ENANO_ROOT . '/includes/wikiformat.php');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   434
		$carp = new Carpenter();
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   435
		$carp->exclusive_rule('heading');
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   436
		$tokey = isset($_GET['tokey']) ? intval($_GET['tokey']) : false;
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   437
		echo $carp->render(halftone_render_body($match[2], $chord_list, $tokey));
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   438
	}
16db14829751 First commit.
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
   439
}