From 1b1acd754cf9419da46a45a0723a257e1f956d84 Mon Sep 17 00:00:00 2001 From: fusionate Date: Fri, 9 Feb 2024 13:11:40 -0700 Subject: [PATCH] Recordings: Audio waveform generation integration. --- app/recordings/recordings.php | 4 +- app/recordings/waveform.php | 128 ++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 app/recordings/waveform.php diff --git a/app/recordings/recordings.php b/app/recordings/recordings.php index ef047567e5..418ac6b205 100644 --- a/app/recordings/recordings.php +++ b/app/recordings/recordings.php @@ -17,7 +17,7 @@ The Initial Developer of the Original Code is Mark J Crane - Portions created by the Initial Developer are Copyright (C) 2008-2023 + Portions created by the Initial Developer are Copyright (C) 2008-2024 the Initial Developer. All Rights Reserved. Contributor(s): @@ -494,7 +494,7 @@ foreach ($recordings as $row) { //playback progress bar if (permission_exists('recording_play')) { - echo "\n"; + echo "\n"; echo "\n"; // dummy row to maintain alternating background color } if (permission_exists('recording_edit')) { diff --git a/app/recordings/waveform.php b/app/recordings/waveform.php new file mode 100644 index 0000000000..7a32b0c7f1 --- /dev/null +++ b/app/recordings/waveform.php @@ -0,0 +1,128 @@ + + Portions created by the Initial Developer are Copyright (C) 2024 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +//includes files + require_once dirname(__DIR__, 2) . "/resources/require.php"; + require_once "resources/check_auth.php"; + require_once "resources/classes/waveform.php"; + + use maximal\audio\Waveform; + +//check permisions + if (permission_exists('recording_play')) { + //access granted + } + else { + echo "access denied"; + exit; + } + +//create the waveform file + if (is_uuid($_GET['id']) || !empty($_GET['data'])) { + + //set the path for the directory + $path = $_SESSION['switch']['recordings']['dir']."/".$_SESSION['domain_name']; + + //get recording details from db + if (is_uuid($_GET['id'])) { + $sql = "select recording_filename, recording_base64 "; + $sql .= "from v_recordings "; + $sql .= "where domain_uuid = :domain_uuid "; + $sql .= "and recording_uuid = :recording_uuid "; + $parameters['domain_uuid'] = $_SESSION['domain_uuid']; + $parameters['recording_uuid'] = $_GET['id']; + $database = new database; + $row = $database->select($sql, $parameters, 'row'); + if (is_array($row) && @sizeof($row) != 0) { + $recording_filename = $row['recording_filename']; + if (!empty($_SESSION['recordings']['storage_type']['text']) && $_SESSION['recordings']['storage_type']['text'] == 'base64' && !empty($row['recording_base64'])) { + $recording_decoded = base64_decode($row['recording_base64']); + file_put_contents($path.'/'.$recording_filename, $recording_decoded); + } + } + unset($sql, $parameters, $row, $recording_decoded); + } + else if ($_GET['data']) { + $recording_filename = str_replace($path, '', $_GET['data']); + } + + //build full path + $slash = substr($recording_filename,0,1) != '/' ? '/' : null; + $full_file_path = $path.$slash.$recording_filename; + + //stream waveform file + if (file_exists($full_file_path)) { + + //temporary waveform image filename + $temp_filename = 'waveform_'.$_GET['id'].'_'.rand(0000,9999).'.png'; + + //create temporary waveform image, if doesn't exist + if (file_exists($temp_filename)) { + $wf = true; + } + else { + //create temporary waveform image + $waveform = new Waveform($full_file_path); + Waveform::$linesPerPixel = 1; // default: 8 + Waveform::$samplesPerLine = 512; // default: 512 + Waveform::$colorA = !empty($_SESSION['theme']['audio_player_waveform_color_a_leg']['text']) ? color_to_rgba_array($_SESSION['theme']['audio_player_waveform_color_a_leg']['text']) : [32,134,37,0.6]; // array rgba, left (a-leg) wave color + Waveform::$colorB = !empty($_SESSION['theme']['audio_player_waveform_color_b_leg']['text']) ? color_to_rgba_array($_SESSION['theme']['audio_player_waveform_color_b_leg']['text']) : [0,125,232,0.6]; // array rgba, right (b-leg) wave color + Waveform::$backgroundColor = !empty($_SESSION['theme']['audio_player_waveform_color_background']['text']) ? color_to_rgba_array($_SESSION['theme']['audio_player_waveform_color_background']['text']) : [0,0,0,0]; // array rgba, default: transparent + Waveform::$axisColor = !empty($_SESSION['theme']['audio_player_waveform_color_axis']['text']) ? color_to_rgba_array($_SESSION['theme']['audio_player_waveform_color_axis']['text']) : [0,0,0,0.3]; // array rgba + Waveform::$singlePhase = empty($_SESSION['theme']['audio_player_waveform_single_phase']['boolean']) || $_SESSION['theme']['audio_player_waveform_single_phase']['boolean'] !== 'true' ? false : true; // positive phase only - left (a-leg) top, right (b-leg) bottom + Waveform::$singleAxis = empty($_SESSION['theme']['audio_player_waveform_single_axis']['boolean']) || $_SESSION['theme']['audio_player_waveform_single_axis']['boolean'] !== 'false' ? true : false; // combine channels into single axis + $height = !empty($_SESSION['theme']['audio_player_waveform_height']['text']) && is_numeric(str_replace('px','',$_SESSION['theme']['audio_player_waveform_height']['text'])) ? 2.2 * (int) str_replace('px','',$_SESSION['theme']['audio_player_waveform_height']['text']) : null; + $wf = $waveform->getWaveform($temp_filename, 1600, $height ?? 180); // input: png filename returns boolean true/false, or 'base64' returns base64 string + } + + //stream image to browser + if ($wf === true && file_exists($temp_filename)) { + + ob_clean(); + $fd = fopen($temp_filename, 'rb'); + header("Content-Type: application/force-download"); + header("Content-Type: application/octet-stream"); + header("Content-Type: application/download"); + header("Content-Description: File Transfer"); + header("Content-Type: image/png"); + header('Content-Disposition: attachment; filename="'.$temp_filename.'"'); + header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 + header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past + header("Content-Length: ".filesize($temp_filename)); + ob_clean(); + + fpassthru($fd); + + } + + } + + //delete temp waveform image + @unlink($temp_filename); + + } + +?> \ No newline at end of file