Thanks NogDog for the class you posted but i will continue to try to fix this cause thats the only way to learn....
@ joshjryan
try this version instead
At the bottom just plug in:
param1 = original key: in the form of a number C = 0, C#=1, D=2 and so on.
param2 = Plug in the desired key also as an interger
param3 = song to transpose
param4 = specify if it's minor or major. Minor being 'true'(boolean)
param5 = weather the results come back as the default flats or sharp. for sharp pass in 'sharp'
I haven't look at this script in a while so im not sure if params 4 and 5 are working...
Im trying to build a mini planningcenteronline.com for my church...
<?php
// this needs the original key, new key and the song. Returns the song with the new chords...
function transpose($key,$newKey,$song,$minor=FALSE,$type='sharp')
{
function shift_values($param,$number)
{
$param = array_merge($param,$param);
$param = array_slice($param,$number,12);
return $param;
}
// preg pettenrs([A-G](#|b)*([a-z]|[0-9])*)(/[A-G](#|b)*([a-z]|[0-9])*)*
// pattern (mine) \b[A-G]{1,1}(#|b)?(m|9|maj|7|sus)?(\/[A-G](#)?(b{1,1})?)?
$search = '`\b[A-G]{1,1}(#|b)?(m|9|maj|7|sus)?(\/[A-G](#)?(b{1,1})?)?`';
preg_match_all($search, $song, $song_chords);
$u = array_unique($song_chords[0]);
print_r($u);
// Majors with sharps
$notes = array('/(?<!\|)C(?!(\#|b))/',
'/(?<!\|)(C#|Db)/',
'/(?<!\|)D(?!(\#|b))/',
'/(?<!\|)(D#|Eb)/',
'/(?<!\|)E(?!(\#|b))/',
'/(?<!\|)F(?!(\#|b))/',
'/(?<!\|)(F#|Gb)/',
'/(?<!\|)G(?!(\#|b))/',
'/(?<!\|)(G#|Ab)/',
'/(?<!\|)A(?!(\#|b))/',
'/(?<!\|)(A#|Bb)/',
'/(?<!\|)B(?!(\#|b))/');
$notes = shift_values($notes,$key);
// Majors replace with sharps
$sharps_replace = array('|C','|C#', '|D','|D#', '|E','|F','|F#', '|G','|G#','|A','|A#', '|B');
$sharps_replace = shift_values($sharps_replace,$newKey);
// Majors replace with flats
$flats_replace = array('|C','|Db', '|D','|Eb', '|E','|F','|Gb', '|G','|Ab','|A','|Bb', '|B');
$flats_replace = shift_values($flats_replace,$newKey);
// Minors
$minors = array('/(?<!\|)C(?!(\#|b))/',
'/(?<!\|)(C#|Db)/',
'/(?<!\|)D(?!(\#|b))/',
'/(?<!\|)(D#|Eb)/',
'/(?<!\|)E(?!(#|b))/',
'/(?<!\|)F(?!(\#|b))/',
'/(?<!\|)(F#|Gb)/',
'/(?<!\|)G(?!(\#|b))/',
'/(?<!\|)(G#|Ab)/',
'/(?<!\|)A(?!(\#|b))/',
'/(?<!\|)(A#|Bb)/',
'/(?<!\|)B(?!(\#|b))/');
$minors = shift_values($minors,$key);
// Minors replace with sharps
$minors_replace = array('|A','|A#', '|B','|C','|C#', '|D','|D#', '|E','|F','|F#', '|G','|G#');
$minors_replace = shift_values($minors_replace,$newKey);
// Minors replace with flats
$minors_replace = array('|A','|Bb', '|B','|C','|Db', '|D','|Eb', '|E','|F','|Gb', '|G','|Ab');
$minors_replace = shift_values($minors_replace,$newKey);
//check if it's minor or major
if ($minor == TRUE) {
$new = preg_replace($minors, $minors_replace, $u);
}
else if ($type == "flat" | $newKey == '3') {
$new = preg_replace($notes, $flats_replace, $u);
}
else {
$new = preg_replace($notes, $sharps_replace, $u);
}
print_r($new);
foreach ($u as $key => $value) {
$u[$key] = '/\b(?<!\|)'. preg_quote($value, '/') .'\b/';
}
print_r($u);
$new_song = preg_replace($u, $new, $song);
$clean = str_replace('|', '', $new_song);
echo $clean;
}
echo transpose(2,4,$song3);
?>