Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any language with this PHP script #346

Open
cirolaferrara opened this issue May 31, 2024 · 0 comments
Open

Any language with this PHP script #346

cirolaferrara opened this issue May 31, 2024 · 0 comments

Comments

@cirolaferrara
Copy link

cirolaferrara commented May 31, 2024

Requires mpg123

// Settings
define('LANG_URL', 'https://raw.githubusercontent.com/dgiese/dustcloud/master/devices/xiaomi.vacuum/audio_generator/language');
define('MPG123', 'mpg123.exe');
define('URL', 'https://translate.google.com/translate_tts?ie=UTF-8&q=');
define('LANG', 'it');
define('AGENT', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0');

// Available languages
$availableLanguages = ['ca','cs','de','en','es','fi','fr','it','nl','no','pl','ru','se','tr','uk'];

// Ask for a language
$promptLanguage = readline('Select a language (example it): '); 

// Check if language exists
if(!in_array($promptLanguage, $availableLanguages)) {
    die($promptLanguage. ' not supported!');
}

// Try to download CSV file
$csv = fopen (LANG_URL . '/audio_'. $promptLanguage . '.csv', "r");
if (!$csv) {
    die('Unable to open remote file');
}

// Parse CSV file
while (!feof ($csv)) {
    while (($data = fgetcsv($csv, 1000, ",")) !== FALSE) {
        $fileName = $data[0];
        $text = $data[1];

        // Build Google Translate TTS URL
        $url = URL . urlencode($text) . "&tl=" . LANG . "&client=tw-ob&ttsspeed=1";

        // Try to download mp3 generated file
        $context = stream_context_create(['http' => ['header' => AGENT . "\r\n"]]);
        $response = file_get_contents($url, false, $context);

        if ($response !== false) {
            $tempFile = tempnam(sys_get_temp_dir(), $fileName);
            file_put_contents($tempFile, $response);

            // Convert MP3 to WAV file
            exec(MPG123.' -q -w '.$fileName. ' '. $tempFile);

            // Print successful message
            echo $fileName . ' generated!'.PHP_EOL;
        }
    }

    // Print help message
    echo 'Upload *.wav files into /opt/rockrobo/resources/sounds dir (worked for me)';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant