add: formatting, cleaning of notes

Signed-off-by: Tobias Reisinger <tobias@msrg.cc>
This commit is contained in:
Tobias Reisinger 2020-03-07 16:22:08 +01:00
parent ed101c6b4f
commit edbb9d45c6
6 changed files with 102 additions and 19 deletions

View file

@ -1,8 +1,10 @@
<?php <?php
require("./lib//config.php"); require("./lib/config.php");
require("./lib//helpers.php"); require("./lib/helpers.php");
require("./lib//validation.php"); require("./lib/validation.php");
require("./lib/fix_notation.php");
require("./lib/combine_rests.php");
session_start(); session_start();
@ -19,8 +21,8 @@ $shortest_note = intval($_GET["shortest_note"]);
$dynamic_beat = $_GET["dynamic_beat"]; $dynamic_beat = $_GET["dynamic_beat"];
$dynamic_rhythm = $_GET["dynamic_rhythm"]; $dynamic_rhythm = $_GET["dynamic_rhythm"];
$notes_final_mod = [];
$notes_final = []; $notes_final = [];
$notes_modifiers = [];
$bars_count = 0; $bars_count = 0;
$rhythm_length = 0; $rhythm_length = 0;
@ -30,10 +32,11 @@ $rhythm_length_max = $shortest_note * (intval($time_explode[0]) / intval($time_e
$id_hashed = md5($id); $id_hashed = md5($id);
do { do {
$notes_input = []; $notes_input = [];
$notes_input_mod = [];
for($i = 0; $i < strlen($id_hashed); $i+=2) for($i = 0; $i < strlen($id_hashed); $i+=2)
{ {
$notes_input[] = strval(pow(2, (ord(substr($id_hashed, $i)) % intval(log($shortest_note, 2))) + 1)); $notes_input[] = pow(2, (ord(substr($id_hashed, $i)) % intval(log($shortest_note, 2))) + 1);
$notes_modifiers[] = ord(substr($id_hashed, $i + 1)); $notes_mod[] = ord(substr($id_hashed, $i + 1));
} }
for($i = 0; $i < count($notes_input); $i++) for($i = 0; $i < count($notes_input); $i++)
{ {
@ -41,7 +44,7 @@ do {
{ {
continue; continue;
} }
$note_val = intval($notes_input[$i]); $note_val = $notes_input[$i];
if(($note_val == 0) || ($note_val > $shortest_note) || (!is_valid_note($note_val))) if(($note_val == 0) || ($note_val > $shortest_note) || (!is_valid_note($note_val)))
{ {
continue; continue;
@ -52,18 +55,13 @@ do {
} }
$rhythm_length += $shortest_note / $note_val; $rhythm_length += $shortest_note / $note_val;
if($notes_modifiers[$i] & 0b0111) $notes_final[] = $note_val;
{ $notes_final_mod[] = $notes_mod[$i];
$notes_final[] = PERCUSSION_CLAP . $note_val;
}
else
{
$notes_final[] = PERCUSSION_REST . $note_val;
}
if($rhythm_length == $rhythm_length_max) if($rhythm_length == $rhythm_length_max)
{ {
$notes_final[] = "|"; $notes_final[] = "|";
$notes_final_mod[] = $notes_mod[$i];
$rhythm_length = 0; $rhythm_length = 0;
$bars_count++; $bars_count++;
} }
@ -71,6 +69,9 @@ do {
$id_hashed = md5($id_hashed); $id_hashed = md5($id_hashed);
} while($bars_count < $bars); } while($bars_count < $bars);
list($notes_final, $notes_final_mod) = fix_notation($notes_final, $notes_final_mod);
list($notes_final, $notes_final_mod) = combine_rests($notes_final, $notes_final_mod);
$notes_final = implode(" ", $notes_final); $notes_final = implode(" ", $notes_final);
$rest_padding = array_fill(0, $time_explode[0], PERCUSSION_REST . $time_explode[1]); $rest_padding = array_fill(0, $time_explode[0], PERCUSSION_REST . $time_explode[1]);
@ -97,7 +98,6 @@ exec(sprintf("lilypond --png -o %1\$s %1\$s$file_name.ly && convert %1\$s$file_n
echo(render( echo(render(
"./templates/index.tpl.php", "./templates/index.tpl.php",
$locale,
array( array(
"id" => $id, "id" => $id,
"time" => $time, "time" => $time,

26
lib/combine_rests.php Normal file
View file

@ -0,0 +1,26 @@
<?php
function combine_rests($unfixed, $unfixed_mod)
{
$fixed = [];
for($i = 0; $i < count($unfixed); $i++)
{
if($unfixed[$i] == "" || $unfixed[$i] == LY_BAR_SEPARATOR)
{
continue;
}
if(modifier_is_rest($unfixed_mod[$i]) && $unfixed[$i] == $unfixed[$i + 1])
{
$note_val = intval(substr($unfixed[$i], strlen(PERCUSSION_REST)));
$fixed[] = PERCUSSION_REST . strval($note_val / 2);
$fixed_mod[] = $unfixed_mod[$i];
$unfixed[$i + 1] = "";
continue;
}
$fixed[] = $unfixed[$i];
$fixed_mod[] = $unfixed_mod[$i];
}
return array($fixed, $fixed_mod);
}
?>

View file

@ -1,6 +1,7 @@
<?php <?php
const MAX_FRACTION = 8; const LY_BAR_SEPARATOR = "|";
const LY_DOT = ".";
const PERCUSSION_REST = "r"; const PERCUSSION_REST = "r";
const PERCUSSION_CLAP = "hc"; const PERCUSSION_CLAP = "hc";
const PERCUSSION_BEAT = "ss"; const PERCUSSION_BEAT = "ss";

51
lib/fix_notation.php Normal file
View file

@ -0,0 +1,51 @@
<?php
function fix_notation($unfixed, $unfixed_mod)
{
$fixed = [];
$fixed_mod = [];
for($i = 0; $i < count($unfixed); $i++)
{
if(is_string($unfixed[$i]) && $unfixed[$i] == LY_BAR_SEPARATOR)
{
$fixed[] = LY_BAR_SEPARATOR;
$fixed_mod[] = $unfixed_mod[$i];
continue;
}
if($unfixed[$i] == 0)
{
continue;
}
if(modifier_is_rest($unfixed_mod[$i]))
{
$fixed[] = PERCUSSION_REST . strval($unfixed[$i]);
$fixed_mod[] = $unfixed_mod[$i];
continue;
}
if(
($unfixed[$i + 1] != BAR_SEPARATOR) &&
modifier_is_rest($unfixed_mod[$i + 1])
)
{
if($unfixed[$i] * 2 == $unfixed[$i + 1])
{
$fixed[] = PERCUSSION_CLAP . strval($unfixed[$i]) . LY_DOT;
$fixed_mod[] = $unfixed_mod[$i];
$unfixed[$i + 1] = 0;
continue;
}
if($unfixed[$i] == $unfixed[$i + 1])
{
$fixed[] = PERCUSSION_CLAP . strval($unfixed[$i] / 2);
$fixed_mod[] = $unfixed_mod[$i];
$unfixed[$i + 1] = 0;;
continue;
}
}
$fixed[] = PERCUSSION_CLAP . strval($unfixed[$i]);
$fixed_mod[] = $unfixed_mod[$i];
}
return array($fixed, $fixed_mod);
}
?>

View file

@ -5,7 +5,7 @@ function remove_whitespace($target)
return str_replace(" ", "", $target); return str_replace(" ", "", $target);
} }
function render($template, $locale, $param){ function render($template, $param){
ob_start(); ob_start();
extract($param, EXTR_SKIP); extract($param, EXTR_SKIP);
include($template); include($template);
@ -34,4 +34,9 @@ function setup_locale()
textdomain($domain); textdomain($domain);
} }
function modifier_is_rest($modifier)
{
return !($modifier & 0b0111);
}
?> ?>

View file

@ -29,7 +29,7 @@ function is_valid_time($check_time)
return false; return false;
} }
$ratio = $check_time_explode[0] / $check_time_explode[1]; $ratio = $check_time_explode[0] / $check_time_explode[1];
$new_rhythm_length = intval(MAX_FRACTION * $ratio); $new_rhythm_length = intval(16 * $ratio); // 64 is a generic value. insert any valid note (8 and up should be good)
if(!is_int($new_rhythm_length) || $new_rhythm_length == 0) if(!is_int($new_rhythm_length) || $new_rhythm_length == 0)
{ {
return false; return false;