rhythm/index.php

114 lines
3.1 KiB
PHP
Raw Normal View History

2020-03-05 20:46:08 +00:00
<?php
require("./lib/config.php");
require("./lib/helpers.php");
require("./lib/validation.php");
require("./lib/fix_notation.php");
require("./lib/combine_rests.php");
2020-03-05 20:46:08 +00:00
session_start();
setup_locale();
validate_get_parameter();
$id = $_GET["id"];
$tempo = $_GET["tempo"];
$time = $_GET["time"];
$bars = intval($_GET["bars"]);
$shortest_note = intval($_GET["shortest_note"]);
2020-03-05 20:46:08 +00:00
$dynamic_beat = $_GET["dynamic_beat"];
$dynamic_rhythm = $_GET["dynamic_rhythm"];
$notes_final_mod = [];
2020-03-05 20:46:08 +00:00
$notes_final = [];
$bars_count = 0;
$rhythm_length = 0;
$time_explode = explode("/", $time, 2);
$rhythm_length_max = $shortest_note * (intval($time_explode[0]) / intval($time_explode[1]));
2020-03-05 20:46:08 +00:00
$id_hashed = md5($id);
do {
$notes_input = [];
$notes_input_mod = [];
2020-03-05 20:46:08 +00:00
for($i = 0; $i < strlen($id_hashed); $i+=2)
{
$notes_input[] = pow(2, (ord(substr($id_hashed, $i)) % intval(log($shortest_note, 2))) + 1);
$notes_mod[] = ord(substr($id_hashed, $i + 1));
2020-03-05 20:46:08 +00:00
}
for($i = 0; $i < count($notes_input); $i++)
{
if($bars_count == $bars)
{
continue;
}
$note_val = $notes_input[$i];
if(($note_val == 0) || ($note_val > $shortest_note) || (!is_valid_note($note_val)))
2020-03-05 20:46:08 +00:00
{
continue;
}
if($rhythm_length + ($shortest_note / $note_val) > $rhythm_length_max)
2020-03-05 20:46:08 +00:00
{
continue;
}
$rhythm_length += $shortest_note / $note_val;
2020-03-05 20:46:08 +00:00
$notes_final[] = $note_val;
$notes_final_mod[] = $notes_mod[$i];
2020-03-05 20:46:08 +00:00
if($rhythm_length == $rhythm_length_max)
{
$notes_final[] = "|";
$notes_final_mod[] = $notes_mod[$i];
2020-03-05 20:46:08 +00:00
$rhythm_length = 0;
$bars_count++;
}
}
$id_hashed = md5($id_hashed);
} 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);
2020-03-05 20:46:08 +00:00
$notes_final = implode(" ", $notes_final);
$rest_padding = array_fill(0, $time_explode[0], PERCUSSION_REST . $time_explode[1]);
$rest_padding[0] = $rest_padding[0] . "\\$dynamic_rhythm";
$rest_padding = implode(" ", $rest_padding);
$beats_one_bar = array_fill(1, $time_explode[0] - 1, PERCUSSION_BEAT . $time_explode[1]);
array_unshift($beats_one_bar, PERCUSSION_BEAT_ACCENT . $time_explode[1] . "\\$dynamic_beat");
array_push($beats_one_bar, "|");
$beats = implode(" ", array_fill(0, $bars_count + 2, implode(" ", $beats_one_bar)));
$file_content = sprintf(
LILYPOND_FORMAT,
$time,
$tempo,
$notes_final,
$rest_padding,
$beats
);
$file_name = md5($file_content);
file_put_contents( OUTPUT_FOLDER . $file_name . ".ly", $file_content);
exec(sprintf("lilypond --png -o %1\$s %1\$s$file_name.ly && convert %1\$s$file_name.png -trim %1\$s$file_name.png", OUTPUT_FOLDER));
echo(render(
"./templates/index.tpl.php",
array(
"id" => $id,
"time" => $time,
"tempo" => $tempo,
"bars" => $bars,
"shortest_note" => $shortest_note,
2020-03-05 20:46:08 +00:00
"dynamic_beat" => $dynamic_beat,
"dynamic_rhythm" => $dynamic_rhythm,
"file_name" => $file_name,
2020-03-05 20:46:08 +00:00
)
));
?>