Backend for songs.zachdecook.com
* Song: Add method for transposing
Zach DeCook 2020-11-19
parent e04ccd0 · commit 8d16bfb
-rw-r--r--laravel/app/Http/Controllers/SongController.php17
-rw-r--r--laravel/app/Song.php13
2 files changed, 28 insertions, 2 deletions
diff --git a/laravel/app/Http/Controllers/SongController.php b/laravel/app/Http/Controllers/SongController.php
index 83c9601..36fdb3f 100644
--- a/laravel/app/Http/Controllers/SongController.php
+++ b/laravel/app/Http/Controllers/SongController.php
@@ -114,7 +114,20 @@ class SongController extends Controller
}
return ($goodtokens *2)+ $ambtokens >= $badtokens;
}
-
+
+ public function transposeBlock($text, $transp)
+ {
+ $newText = '';
+ foreach(explode("\n", $text) as $line) {
+ if ($this->chordline($line)) {
+ $newText .= $this->z_transpose2($line, $transp) . "\n";
+ } else {
+ $newText .= $line . "\n";
+ }
+ }
+ return $newText;
+ }
+
private function z_transpose2( $line, $transp )
{
$newchords = $this->z_transparray( $transp );
@@ -200,7 +213,7 @@ class SongController extends Controller
return $ochords[($chords[$fromkey] + $integer + 24)%12];
} return false;
}
- private function keydiff($fromkey, $tokey){
+ public function keydiff($fromkey, $tokey){
$chords = array_flip(array( "C","C#","D","D#","E","F","F#","G","G#","A","A#","B" ));
$chords["Db"] = $chords["C#"];
$chords["Eb"] = $chords["D#"];
diff --git a/laravel/app/Song.php b/laravel/app/Song.php
index a6e3a54..4ae3920 100644
--- a/laravel/app/Song.php
+++ b/laravel/app/Song.php
@@ -3,6 +3,7 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
+use App\Http\Controllers\SongController;
class Song extends Model
{
@@ -11,6 +12,18 @@ class Song extends Model
{
return $this->belongsToMany('App\Playlist');
}
+ public function textTranspose($key)
+ {
+ $sc = new SongController();
+ $transp = $key;
+ if ($key && $this->plain_key){
+ $try = $sc->keydiff($this->plain_key, $key);
+ if ($try !== null){
+ $transp = $try;
+ }
+ }
+ return $sc->transposeBlock($this->text, $transp);
+ }
public function getNameAttribute()
{
return $this->title