Skip to content

Instantly share code, notes, and snippets.

@mchorse
Created May 11, 2020 12:31
Show Gist options
  • Save mchorse/40fdd18d21de364e5153404fe6521b21 to your computer and use it in GitHub Desktop.
Save mchorse/40fdd18d21de364e5153404fe6521b21 to your computer and use it in GitHub Desktop.
A simple script to generate audio files using OS X's "say" command from plain text
<?php
/**
* Simple script which allows to generate voice recordings using OS X's say
* command out of Markdown like file.
*
* Syntax of the arguments to this script is following:
*
* $ php say.php <script_file> [prefix] [index]
*
* [prefix] and [index] allow you to specify which section of the script to
* "render," and both are completely optional.
*/
$file = $_SERVER['argv'][1];
$user_prefix = null;
$user_index = null;
$user = false;
$c = count($_SERVER['argv']);
if ($c >= 3)
{
$user_prefix = $_SERVER['argv'][2];
if ($c >= 4) $user_index = (int) $_SERVER['argv'][3];
$user = true;
}
if (!is_file($file))
{
die("Not a file! $file");
}
$file = file_get_contents($file);
$lines = explode("\n", $file);
$prefix = null;
$index = null;
foreach ($lines as $line)
{
$line = trim($line);
if (strpos($line, '#') === 0)
{
if ($user && $user_prefix === $prefix) break;
$prefix = trim(substr($line, 1));
$index = 1;
echo "Detected prefix '$prefix'.\n";
continue;
}
$selected = $prefix === $user_prefix && ($user_index === null ? true : $index === $user_index);
if ($line !== "" && $prefix !== null)
{
if ($user && $selected || !$user)
{
exec('say -v Daniel "' . addcslashes($line, '"') . '" -o ' . $prefix . '_' . $index);
echo "Wrote {$prefix}_{$index} to a file.\n";
if ($user && $user_index !== null) break;
}
$index += 1;
}
}
echo "Done!\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment