1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
<?php
namespace App\Http\Controllers;
use App\Playlist;
use App\Song;
use Illuminate\Http\Request;
class SongController extends Controller
{
public function oldShow( $song )
{
$song = Song::where('number', $song );
return $this->show( $song );
}
public function show( Song $song )
{
$lines = explode( "\n", $song['text'] );
$newText = '';
$transp = $_GET['transp'] ?? 0;
foreach( $lines as $line ){
$line = trim(htmlspecialchars( $line ), "\n\t\r");
if ( $this->chordline( $line ) ) {
if ( $transp != 0) {
$line = $this->z_transpose2( $line, $transp );
}
$class = ! isset( $song['key'] ) ?
"tabs chord$transp"
: "tabs chord" . $this->transpadd( $song['key'], $transp );
$line = str_replace(
array('{','}'),
array('</b>{', "}<b class='$class'>" ),
$line );
$newText .= "<b class='$class'>" . $line . "</b>\n";
} else {
$newText .= "$line\n";
}
}
$song['escapedText'] = $newText;
$playlist = NULL;
if ( $_GET['playlist'] ?? FALSE ){
$playlist = Playlist::find($_GET['playlist'] ) ?? NULL;
}
return view('song', ['song' => $song, 'transp' => $transp, 'playlist' => $playlist ] );
}
/**
* @brief Determine whether or not this line contains chords.
*/
private function chordline($line)
{
$chords =
array( "C","C#","D","D#","E","F","F#","G","G#","A#","B",//"A",
"Db", "Eb", "Gb", "Bb",//"Ab",
"Cm", "Dm", "Fm", "Gm", "Bm", //"Em", "Am",
);
$ambiguous = array( "Ab", "Em", "Am", "A" );
$line = str_replace(array('/','7', "\n", '2', '4'), ' ', $line);
$tokens = array_filter(explode(' ', $line));
$badtokens = 0;
$ambtokens = 0;
$goodtokens = 0;
foreach ($tokens as $token) {
if( in_array( substr($token, 0,2), $chords ) ) $goodtokens++;
else if ( in_array( substr( $token, 0,2), $ambiguous) ) $ambtokens++;
else if( $badtokens > 10 ) return FALSE;
else $badtokens++;
}
return ($goodtokens *2)+ $ambtokens >= $badtokens;
}
private function z_transpose2( $line, $transp )
{
$newchords = $this->z_transparray( $transp );
$newline = '';
$space = 0; ///@< Spaces that need to be added or removed.
$inCurly = 0;
for($i = 0; $i < strlen($line); $i++) {
$char = $line[$i];
$nchar = isset($line[$i+1]) ? $line[$i+1] : '';
$upchar = strtoupper($line[$i]);
$cval = ord($upchar);
if ( $char == '}' && $inCurly ){ $inCurly = 0;}
if ( $char == '{' || $inCurly ){ $inCurly = 1; $newline .= $char; continue;}
// A-G
if( $cval <= 71 && $cval >=65 ) {
// Exception for Cmaj7
if( $upchar == 'A' && $nchar == 'j' ) {
$newline .= $char;
} else if( $nchar == 'b' || $nchar =='#') {
$i++; //We have read an extra character now.
$newchord = $newchords[$upchar . $nchar];
if( strlen($newchord) == 1 ) {
// Need to insert an extra space.
$space += 1;
}
$newline .= $newchord;
} else {
$newchord = $newchords[$upchar];
if( strlen($newchord) == 2 ) {
// Need to remove an extra space.
$space -= 1;
}
$newline .= $newchord;
}
} else if ( $char == ' ' ) {
if( $space >= 0) {
for ($j = 0; $j <= $space; $j++) {
$newline .= ' ';
}
$space = 0;
} else {
// Only balance negative spaces if one will be left remaining
if( $nchar == ' ' ) {
$i++;
$space += 1;
}
$newline .= ' ';
}
} else {
$newline .= $char;
}
}
return $newline;
}
private function z_transparray( $transp )
{
$chords =
array( "C","C#","D","D#","E","F","F#","G","G#","A","Bb","B" );
$newchords = array();
// Create array to map old chords to new ones
for ($i=0; $i < 12; $i++) {
$newchords[$chords[$i]] = $chords[($i+$transp+12)%12];
}
$newchords["Db"] = $newchords["C#"];
$newchords["Eb"] = $newchords["D#"];
$newchords["Gb"] = $newchords["F#"];
$newchords["Ab"] = $newchords["G#"];
$newchords["A#"] = $newchords["Bb"];
return $newchords;
}
private function transpadd( $fromkey, $integer )
{
$chords = array_flip(array( "C","C#","D","D#","E","F","F#","G","G#","A","A#","B" ));
$chords["Db"] = $chords["C#"];
$chords["Eb"] = $chords["D#"];
$chords["Gb"] = $chords["F#"];
$chords["Ab"] = $chords["G#"];
$chords["Bb"] = $chords["A#"];
$ochords = array( "C","Db","D","Eb","E","F","Gb","G","Ab","A","Bb","B" );
$fromkey = trim($fromkey, 'm');
return $ochords[($chords[$fromkey] + $integer + 24)%12];
}
public function post()
{
$songD = [];
$songD['title'] = $_POST['title'] ?? '';
$songD['text'] = $_POST['text'] ?? '';
$songD['key'] = $_POST['key'] ?? NULL;
$songD['author'] = $_POST['author'] ?? NULL;
$songD['number'] = substr(md5(microtime()),rand(0,26),5);
if ( $songD['title'] ) {
$song = Song::create( $songD );
if ( $_POST['playlist'] ) {
$song->playlists()->attach( $_POST['playlist'] );
}
return redirect()->route('song.show', [ 'song' => $song, 'playlist' => $_POST['playlist'] ] );
}
if ($_POST['playlist'] ){
return redirect()->route('playlist.show', [ 'playlist' => $_POST['playlist'] ] );
}
return redirect('/');
}
public function edit(Song $song)
{
return view('editsong', ['song' => $song]);
}
public function update(Song $song, Request $request)
{
$song->title = $request->title;
$song->author = $request->author;
$song->key = $request->key;
$song->text = $request->text;
$song->save();
return redirect()->route( 'song.show', ['song' => $song ] );
}
}
|