rosavox/source/helpers/misc.php

86 lines
1.5 KiB
PHP
Raw Normal View History

2025-05-22 06:26:16 +00:00
<?php
namespace rosavox\helpers\misc;
require_once('string.php');
require_once('cache.php');
/**
*/
function render(string $template_name, array $arguments) : string
{
return \rosavox\helpers\string_\coin(
\rosavox\helpers\cache\get(
\rosavox\helpers\string_\coin(
'template.{{name}}',
[
'name' => $template_name,
]
),
fn() => \file_get_contents(
\rosavox\helpers\string_\coin(
'{{directory}}/templates/{{name}}.html.tpl',
[
'directory' => /*__DIR__*/'.',
'name' => $template_name,
]
)
)
),
$arguments
);
}
/**
*/
function navigate(string $target) : void
{
\header('Location: ' . $target);
}
/**
*/
function generate_audio(string $input, string $output_path) : void
{
$key = \hash('sha256', $input);
$path_wav = \rosavox\helpers\string_\coin(
'/tmp/{{key}}.wav',
[
'key' => $key,
]
);
$path_ogg = $output_path;
$command = \rosavox\helpers\string_\coin(
'echo "{{input}}" | piper/piper --model piper/voice.onnx --output_file {{path_wav}} && ffmpeg -y -i {{path_wav}} {{path_ogg}} ; rm -f {{path_wav}}',
[
'input' => $input,
'path_wav' => $path_wav,
'path_ogg' => $path_ogg,
]
);
\exec($command);
}
/**
*/
function translate(string $key, ?array $options = null) : string
{
$strings = \rosavox\helpers\cache\get(
'strings',
fn () => \json_decode(\file_get_contents('strings.json'), true)
);
$options = \array_merge(
[
'language' => 'de',
],
($options ?? [])
);
return ($strings[$options['language']][$key] ?? \sprintf('{%s}', $key));
}
?>