├── .require.json ├── CHANGELOG.md ├── Dockerfile ├── LICENSE ├── README.md ├── composer.json ├── phpcs.xml ├── phpstan-tests.neon └── src ├── Exceptions ├── Exception.php ├── InvalidArgumentException.php ├── ProviderException.php └── RuntimeException.php ├── Providers ├── AbstractProvider.php ├── AcapelaProvider.php ├── AmazonPollyProvider.php ├── GoogleProvider.php ├── PicottsProvider.php ├── ProviderInterface.php ├── ResponsiveVoiceProvider.php └── VoiceRssProvider.php ├── TextToSpeech.php └── TextToSpeechInterface.php /.require.json: -------------------------------------------------------------------------------- 1 | { 2 | "symbol-whitelist" : [ 3 | "null", "false", "self", "array", "string", "int", 4 | "Aws\\Polly\\PollyClient" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | ## x.y.z - UNRELEASED 5 | 6 | -------- 7 | 8 | ## 1.5.0 - 2024-01-19 9 | 10 | ### Added 11 | 12 | * [Providers] Added support for a different voice in VoiceRSS. ([#25](https://github.com/duncan3dc/speaker/issues/25)) 13 | 14 | -------- 15 | 16 | ## 1.4.1 - 2024-11-18 17 | 18 | ### Fixed 19 | 20 | * [Providers] Ensure the local filename is unique per text/language. ([#23](https://github.com/duncan3dc/speaker/issues/23)) 21 | 22 | ### Changed 23 | 24 | * [Support] Added support for PHP 8.2, and 8.3. 25 | 26 | -------- 27 | 28 | ## 1.4.0 - 2022-04-14 29 | 30 | ### Changed 31 | 32 | * [Support] Added support for PHP 8.0, and 8.1. 33 | * [Support] Dropped support for PHP 7.2. 34 | 35 | -------- 36 | 37 | ## 1.3.1 - 2020-10-15 38 | 39 | ### Changed 40 | 41 | * [Support] Added support for Guzzle 7. 42 | 43 | -------- 44 | 45 | ## 1.3.0 - 2019-04-06 46 | 47 | ### Changed 48 | 49 | * [Providers] Use duncan3dc/exec in the Picotts provider for shell commands. 50 | * [Support] Dropped support for PHP 7.1. 51 | 52 | -------- 53 | 54 | ## 1.2.0 - 2019-01-13 55 | 56 | ### Changed 57 | 58 | * [Support] Added support for PHP 7.3. 59 | * [Support] Dropped support for PHP 7.0. 60 | * [Support] Added support for Symfony 3. 61 | 62 | -------- 63 | 64 | ## 1.1.0 - 2018-03-09 65 | 66 | ### Added 67 | 68 | * [Providers] AmazonPolly provider. 69 | 70 | -------- 71 | 72 | ## 1.0.0 - 2017-10-14 73 | 74 | ### Added 75 | 76 | * [Providers] ResponsiveVoice provider. 77 | * [TextToSpeech] Added a TextToSpeechInterface. 78 | 79 | ### Changed 80 | 81 | * [Support] Added support for PHP 7. 82 | * [Support] Dropped support for PHP 5. 83 | * [Support] Dropped support for HHVM. 84 | * [Providers] Dropped the Voxygen provider as the service is no longer available. 85 | * [Providers] All providers are now immutable. 86 | * [Exceptions] All exceptions thrown are now library specific. 87 | 88 | ### Fixed 89 | 90 | * [Providers] Improve the error message for a pico unknown language. 91 | 92 | -------- 93 | 94 | ## 0.7.3 - 2017-04-07 95 | 96 | ### Fixed 97 | 98 | * [Providers] Don't use a forward slash in the client name. 99 | 100 | -------- 101 | 102 | ## 0.7.2 - 2017-03-01 103 | 104 | ### Fixed 105 | 106 | * [Providers] Use symfony/process within the Picotts provider for shell commands. 107 | 108 | -------- 109 | 110 | ## 0.7.1 - 2017-02-05 111 | 112 | ### Fixed 113 | 114 | * [Providers] Fixed a bug with setLanguage() for the Picotts provider. 115 | 116 | -------- 117 | 118 | ## 0.7.0 - 2016-09-12 119 | 120 | ### Changed 121 | 122 | * [TextToSpeech] Made generateFilename() public. 123 | * [Providers] Picotts provider. 124 | * [Support] Drop support for php5.5 125 | 126 | -------- 127 | 128 | ## 0.6.0 - 2015-08-29 129 | 130 | ### Changed 131 | 132 | * [Google] Added the new "client" parameter 133 | * [Providers] Acapela provider. 134 | 135 | -------- 136 | 137 | ## 0.5.1 - 2015-08-22 138 | 139 | ### Changed 140 | 141 | * [Dependencies] Added PHPUnit to the dev dependencies. 142 | 143 | -------- 144 | 145 | ## 0.5.0 - 2015-06-13 146 | 147 | ### Changed 148 | 149 | * [Dependencies] Use Guzzle 6. 150 | * [Support] Drop support for php5.4 151 | 152 | -------- 153 | 154 | ## 0.2.0 - 2015-05-20 155 | 156 | ### Added 157 | 158 | * [TextToSpeech] getFile() method to cache webservice calls. 159 | * [TextToSpeech] save() method to store the audio on the local filesystem. 160 | 161 | -------- 162 | 163 | ## 0.1.0 - 2015-05-19 164 | 165 | ### Added 166 | 167 | * [Providers] Google provider. 168 | * [Providers] Voxygen provider. 169 | * [Providers] Voice RSS provider. 170 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | ARG PHP_VERSION=7.3 2 | FROM php:${PHP_VERSION}-cli 3 | 4 | RUN pecl install uopz || pecl install uopz-6.1.2 && docker-php-ext-enable uopz 5 | RUN echo "uopz.exit=1" >> /usr/local/etc/php/conf.d/docker-php-ext-uopz.ini 6 | 7 | ARG COVERAGE 8 | RUN if [ "$COVERAGE" = "pcov" ]; then pecl install pcov && docker-php-ext-enable pcov; fi 9 | 10 | # Install composer to manage PHP dependencies 11 | RUN apt-get update && apt-get install -y git zip 12 | RUN curl https://getcomposer.org/download/2.2.6/composer.phar -o /usr/local/sbin/composer 13 | RUN chmod +x /usr/local/sbin/composer 14 | RUN echo "if [[ $PHP_VERSION == 7.* ]]; then composer self-update --1; else composer self-update; fi" > composer.sh 15 | RUN bash composer.sh 16 | 17 | WORKDIR /app 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # speaker 2 | A PHP library to convert text to speech using various services 3 | 4 | Full documentation is available at http://duncan3dc.github.io/speaker/ 5 | PHPDoc API documentation is also available at [http://duncan3dc.github.io/speaker/api/](http://duncan3dc.github.io/speaker/api/namespaces/duncan3dc.Speaker.html) 6 | 7 | [![release](https://poser.pugx.org/duncan3dc/speaker/version.svg)](https://packagist.org/packages/duncan3dc/speaker) 8 | [![build](https://github.com/duncan3dc/speaker/workflows/.github/workflows/buildcheck.yml/badge.svg?branch=master)](https://github.com/duncan3dc/speaker/actions?query=branch%3Amaster+workflow%3A.github%2Fworkflows%2Fbuildcheck.yml) 9 | [![coverage](https://codecov.io/gh/duncan3dc/speaker/graph/badge.svg)](https://codecov.io/gh/duncan3dc/speaker) 10 | 11 | ## Quick Example 12 | 13 | ```php 14 | $google = new \duncan3dc\Speaker\Providers\GoogleProvider; 15 | $tts = new \duncan3dc\Speaker\TextToSpeech("Hello World", $google); 16 | file_put_contents("/tmp/hello.mp3", $tts->getAudioData()); 17 | ``` 18 | 19 | _Read more at http://duncan3dc.github.io/speaker/_ 20 | 21 | 22 | ## Services 23 | * __Acapela__ - _Paid voice as a service_ 24 | * __AmazonPolly__ - _AWS service with a 12 month free tier_ 25 | * __Google__ - _Unadvertised service with 100 character limit_ 26 | * __Picotts__ - _An offline command line version_ 27 | * __ResponsiveVoice__ - _Unadvertised service running over a javascript engine_ 28 | * __Voice RSS__ - _Free/paid service requires [registration](http://www.voicerss.org/personel/)_ 29 | 30 | 31 | ## Changelog 32 | A [Changelog](CHANGELOG.md) has been available since the beginning of time 33 | 34 | 35 | ## Where to get help 36 | Found a bug? Got a question? Just not sure how something works? 37 | Please [create an issue](//github.com/duncan3dc/speaker/issues) and I'll do my best to help out. 38 | Alternatively you can catch me on [Twitter](https://twitter.com/duncan3dc) 39 | 40 | 41 | ## duncan3dc/speaker for enterprise 42 | 43 | Available as part of the Tidelift Subscription 44 | 45 | The maintainers of duncan3dc/speaker and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/packagist-duncan3dc-speaker?utm_source=packagist-duncan3dc-speaker&utm_medium=referral&utm_campaign=readme) 46 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "duncan3dc/speaker", 3 | "type": "library", 4 | "description": "Convert text to speech using web services", 5 | "keywords": ["text-to-speech", "tts", "speech"], 6 | "homepage": "https://github.com/duncan3dc/speaker", 7 | "license": "Apache-2.0", 8 | "authors": [{ 9 | "name": "Craig Duncan", 10 | "email": "git@duncanc.co.uk", 11 | "homepage": "https://github.com/duncan3dc", 12 | "role": "Developer" 13 | }], 14 | "require": { 15 | "duncan3dc/exec": "^0.4 || ^1.0", 16 | "guzzlehttp/guzzle": "^6.0|^7.0", 17 | "php": "^7.3 || ^8.0" 18 | }, 19 | "require-dev": { 20 | "aws/aws-sdk-php": "^3.219.3", 21 | "duncan3dc/uopz-expectations": "^0.3 || ^1.0", 22 | "maglnet/composer-require-checker": "^2.0 || ^3.0 || ^4.0", 23 | "mockery/mockery": "^1.5", 24 | "nikic/php-parser": "^4.13", 25 | "phpstan/phpstan": "^0.12.99", 26 | "phpstan/phpstan-mockery": "^0.12.14", 27 | "phpstan/phpstan-phpunit": "^0.12.22", 28 | "squizlabs/php_codesniffer": "^3.6", 29 | "phpunit/phpunit": "^9.5" 30 | }, 31 | "suggest": { 32 | "aws/aws-sdk-php": "Allows you to use the AmazonPollyProvider" 33 | }, 34 | "autoload": { 35 | "psr-4": {"duncan3dc\\Speaker\\": "src/"} 36 | }, 37 | "autoload-dev": { 38 | "psr-4": {"duncan3dc\\Speaker\\Test\\": "tests/"} 39 | }, 40 | "scripts": { 41 | "test": [ 42 | "vendor/bin/phpunit", 43 | "vendor/bin/composer-require-checker --config-file=.require.json", 44 | "vendor/bin/phpstan analyse --level=max src", 45 | "vendor/bin/phpstan analyse --level=max --configuration=phpstan-tests.neon tests", 46 | "vendor/bin/phpcs --standard=phpcs.xml src tests", 47 | "@composer validate --strict" 48 | ] 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /phpcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /phpstan-tests.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - vendor/phpstan/phpstan-mockery/extension.neon 3 | - vendor/phpstan/phpstan-phpunit/extension.neon 4 | -------------------------------------------------------------------------------- /src/Exceptions/Exception.php: -------------------------------------------------------------------------------- 1 | client = $client; 31 | 32 | return $this; 33 | } 34 | 35 | 36 | /** 37 | * Get the guzzle client. 38 | * 39 | * @return ClientInterface 40 | */ 41 | public function getClient(): ClientInterface 42 | { 43 | if ($this->client === null) { 44 | $this->client = new Client(); 45 | } 46 | 47 | return $this->client; 48 | } 49 | 50 | 51 | /** 52 | * Get the format of this audio. 53 | * 54 | * @return string 55 | */ 56 | public function getFormat(): string 57 | { 58 | return "mp3"; 59 | } 60 | 61 | 62 | public function getOptions(): array 63 | { 64 | return []; 65 | } 66 | 67 | 68 | /** 69 | * Send a http request. 70 | * 71 | * @param string $hostname The hostname to send the request to 72 | * @param string[] $params The parameters of the request 73 | * 74 | * @return string The response body 75 | */ 76 | protected function sendRequest(string $hostname, array $params): string 77 | { 78 | $url = $hostname . "?" . http_build_query($params); 79 | 80 | $response = $this->getClient()->request("GET", $url); 81 | 82 | if ($response->getStatusCode() != "200") { 83 | throw new ProviderException("Failed to call the external text-to-speech service"); 84 | } 85 | 86 | return $response->getBody(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Providers/AcapelaProvider.php: -------------------------------------------------------------------------------- 1 | login = $login; 54 | $this->application = $application; 55 | $this->password = $password; 56 | 57 | if ($voice !== null) { 58 | $this->voice = $this->getVoice($voice); 59 | } 60 | 61 | if ($speed !== null) { 62 | $this->speed = $this->getSpeed($speed); 63 | } 64 | } 65 | 66 | 67 | /** 68 | * Check if the voce is valid, and convert it to the required format. 69 | * 70 | * @param string $voice The voice to use 71 | * 72 | * @return string 73 | */ 74 | private function getVoice(string $voice): string 75 | { 76 | $voice = trim($voice); 77 | if (strlen($voice) < 3) { 78 | throw new InvalidArgumentException("Unexpected voice name ({$voice}), names should be at least 3 characters long"); 79 | } 80 | 81 | return strtolower($voice); 82 | } 83 | 84 | 85 | /** 86 | * Set the voice to use. 87 | * 88 | * Visit http://www.acapela-vaas.com/ReleasedDocumentation/voices_list.php for available voices 89 | * 90 | * @param string $voice The voice to use (eg 'Graham') 91 | * 92 | * @return self 93 | */ 94 | public function withVoice(string $voice): self 95 | { 96 | $provider = clone $this; 97 | 98 | $provider->voice = $this->getVoice($voice); 99 | 100 | return $provider; 101 | } 102 | 103 | 104 | /** 105 | * Check the speech rate is valid. 106 | * 107 | * @param int $speed The speech rate to use 108 | * 109 | * @return int 110 | */ 111 | private function getSpeed(int $speed): int 112 | { 113 | if ($speed < 60 || $speed > 360) { 114 | throw new InvalidArgumentException("Invalid speed ({$speed}), must be a number between 60 and 360"); 115 | } 116 | 117 | return $speed; 118 | } 119 | 120 | 121 | /** 122 | * Set the speech rate to use. 123 | * 124 | * @param int $speed The speech rate to use (between 60 and 360) 125 | * 126 | * @return self 127 | */ 128 | public function withSpeed(int $speed): self 129 | { 130 | $provider = clone $this; 131 | 132 | $provider->speed = $this->getSpeed($speed); 133 | 134 | return $provider; 135 | } 136 | 137 | 138 | public function getOptions(): array 139 | { 140 | return [ 141 | "voice" => $this->voice, 142 | "speed" => $this->speed, 143 | ]; 144 | } 145 | 146 | 147 | /** 148 | * Convert the specified text to audio. 149 | * 150 | * @param string $text The text to convert 151 | * 152 | * @return string The audio data 153 | */ 154 | public function textToSpeech(string $text): string 155 | { 156 | if (strlen($text) > 300) { 157 | throw new InvalidArgumentException("Only messages under 300 characters are supported"); 158 | } 159 | 160 | return $this->sendRequest("http://vaas.acapela-group.com/Services/FileMaker.mp3", [ 161 | "prot_vers" => "2", 162 | "cl_login" => $this->login, 163 | "cl_app" => $this->application, 164 | "cl_pwd" => $this->password, 165 | "req_voice" => "{$this->voice}22k", 166 | "req_spd" => (string) $this->speed, 167 | "req_text" => $text, 168 | ]); 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /src/Providers/AmazonPollyProvider.php: -------------------------------------------------------------------------------- 1 | client = $client; 33 | if ($voice !== null) { 34 | $this->voice = $voice; 35 | } 36 | } 37 | 38 | 39 | /** 40 | * Set the voice to use. 41 | * 42 | * @param string $voice The voice to use. 43 | * 44 | * @return $this 45 | */ 46 | public function withVoice(string $voice): self 47 | { 48 | $provider = clone $this; 49 | 50 | $provider->voice = $voice; 51 | 52 | return $provider; 53 | } 54 | 55 | 56 | /** 57 | * Get the format of this audio. 58 | * 59 | * @return string 60 | */ 61 | public function getFormat(): string 62 | { 63 | return "mp3"; 64 | } 65 | 66 | 67 | public function getOptions(): array 68 | { 69 | return [ 70 | "voice" => $this->voice, 71 | ]; 72 | } 73 | 74 | 75 | /** 76 | * Convert the specified text to audio. 77 | * 78 | * @param string $text The text to convert 79 | * 80 | * @return string The audio data 81 | */ 82 | public function textToSpeech(string $text): string 83 | { 84 | try { 85 | $result = $this->client->synthesizeSpeech([ 86 | "OutputFormat" => $this->getFormat(), 87 | "Text" => $text, 88 | "VoiceId" => $this->voice, 89 | ]); 90 | } catch (\Throwable $e) { 91 | throw new ProviderException("Failed to call the external text-to-speech service", $e->getCode(), $e); 92 | } 93 | 94 | return (string) $result->get("AudioStream"); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/Providers/GoogleProvider.php: -------------------------------------------------------------------------------- 1 | language = $this->getLanguage($language); 29 | } 30 | } 31 | 32 | 33 | /** 34 | * Check if the language is valid, and convert it to the required format. 35 | * 36 | * @param string $language The language to use 37 | * 38 | * @return string 39 | */ 40 | private function getLanguage(string $language): string 41 | { 42 | $language = trim($language); 43 | 44 | if (strlen($language) !== 2) { 45 | throw new InvalidArgumentException("Unexpected language code ({$language}), codes should be 2 characters"); 46 | } 47 | 48 | return $language; 49 | } 50 | 51 | 52 | /** 53 | * Set the language to use. 54 | * 55 | * @param string $language The language to use (eg 'en') 56 | * 57 | * @return self 58 | */ 59 | public function withLanguage(string $language): self 60 | { 61 | $provider = clone $this; 62 | 63 | $provider->language = $this->getLanguage($language); 64 | 65 | return $provider; 66 | } 67 | 68 | 69 | public function getOptions(): array 70 | { 71 | return [ 72 | "language" => $this->language, 73 | ]; 74 | } 75 | 76 | 77 | /** 78 | * Convert the specified text to audio. 79 | * 80 | * @param string $text The text to convert 81 | * 82 | * @return string The audio data 83 | */ 84 | public function textToSpeech(string $text): string 85 | { 86 | if (strlen($text) > 100) { 87 | throw new InvalidArgumentException("Only messages under 100 characters are supported"); 88 | } 89 | 90 | return $this->sendRequest("http://translate.google.com/translate_tts", [ 91 | "q" => $text, 92 | "tl" => $this->language, 93 | "client" => "duncan3dc-speaker", 94 | ]); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/Providers/PicottsProvider.php: -------------------------------------------------------------------------------- 1 | pico = $pico; 53 | 54 | if ($language !== null) { 55 | $this->language = $this->getLanguage($language); 56 | } 57 | } 58 | 59 | 60 | /** 61 | * Check if the language is valid, and convert it to the required format. 62 | * 63 | * @param string $language The language to use 64 | * 65 | * @return string 66 | */ 67 | private function getLanguage(string $language): string 68 | { 69 | $language = trim($language); 70 | 71 | if (strlen($language) === 2) { 72 | $language = "{$language}-{$language}"; 73 | } 74 | 75 | if (!preg_match("/^[a-z]{2}-[a-z]{2}$/i", $language)) { 76 | throw new InvalidArgumentException("Unexpected language code ({$language}), codes should be 2 characters, a hyphen, and a further 2 characters"); 77 | } 78 | 79 | list($main, $sub) = explode("-", $language); 80 | $language = strtolower($main) . "-" . strtoupper($sub); 81 | 82 | return $language; 83 | } 84 | 85 | 86 | /** 87 | * Set the language to use. 88 | * 89 | * @param string $language The language to use (eg 'en') 90 | * 91 | * @return self 92 | */ 93 | public function withLanguage(string $language): self 94 | { 95 | $provider = clone $this; 96 | 97 | $provider->language = $this->getLanguage($language); 98 | 99 | return $provider; 100 | } 101 | 102 | 103 | /** 104 | * Get the format of this audio. 105 | * 106 | * @return string 107 | */ 108 | public function getFormat(): string 109 | { 110 | return "wav"; 111 | } 112 | 113 | 114 | public function getOptions(): array 115 | { 116 | return [ 117 | "language" => $this->language, 118 | ]; 119 | } 120 | 121 | 122 | /** 123 | * Convert the specified text to audio. 124 | * 125 | * @param string $text The text to convert 126 | * @param FactoryInterface $factory 127 | * 128 | * @return string The audio data 129 | */ 130 | public function textToSpeech(string $text, FactoryInterface $factory = null): string 131 | { 132 | $filename = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . "speaker_picotts_{$this->language}_" . md5($text) . ".wav"; 133 | 134 | if (file_exists($filename)) { 135 | unlink($filename); 136 | } 137 | 138 | if ($factory === null) { 139 | $program = new Program($this->pico, new Silent()); 140 | } else { 141 | $program = $factory->make($this->pico); 142 | } 143 | 144 | $result = $program->getResult("--wave={$filename}", "--lang={$this->language}", $text); 145 | 146 | if ($result->getStatus() !== 0) { 147 | throw new ProviderException($result->getFirstLine()); 148 | } 149 | 150 | if (!file_exists($filename)) { 151 | throw new ProviderException("TextToSpeech unable to create file: {$filename}"); 152 | } 153 | 154 | $result = file_get_contents($filename); 155 | if ($result === false) { 156 | throw new ProviderException("TextToSpeech unable to read file: {$filename}"); 157 | } 158 | 159 | unlink($filename); 160 | 161 | return $result; 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/Providers/ProviderInterface.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | public function getOptions(): array; 26 | 27 | /** 28 | * Convert the specified text to audio. 29 | * 30 | * @param string $text The text to convert 31 | * 32 | * @return string The audio data 33 | */ 34 | public function textToSpeech(string $text): string; 35 | } 36 | -------------------------------------------------------------------------------- /src/Providers/ResponsiveVoiceProvider.php: -------------------------------------------------------------------------------- 1 | language = $this->getLanguage($language); 33 | } 34 | } 35 | 36 | 37 | /** 38 | * Check if the language is valid, and convert it to the required format. 39 | * 40 | * @param string $language The language to use 41 | * 42 | * @return string 43 | */ 44 | private function getLanguage(string $language): string 45 | { 46 | $language = trim($language); 47 | 48 | if (strlen($language) === 2) { 49 | $language = "{$language}-{$language}"; 50 | } 51 | 52 | if (!preg_match("/^[a-z]{2}-[a-z]{2}$/i", $language)) { 53 | throw new InvalidArgumentException("Unexpected language code ({$language}), codes should be 2 characters, a hyphen, and a further 2 characters"); 54 | } 55 | 56 | list($main, $sub) = explode("-", $language); 57 | $language = strtolower($main) . "-" . strtoupper($sub); 58 | 59 | return $language; 60 | } 61 | 62 | 63 | /** 64 | * Set the language to use. 65 | * 66 | * @param string $language The language to use (eg 'en') 67 | * 68 | * @return self 69 | */ 70 | public function withLanguage(string $language): self 71 | { 72 | $provider = clone $this; 73 | 74 | $provider->language = $this->getLanguage($language); 75 | 76 | return $provider; 77 | } 78 | 79 | 80 | public function getOptions(): array 81 | { 82 | return [ 83 | "language" => $this->language, 84 | ]; 85 | } 86 | 87 | 88 | /** 89 | * Convert the specified text to audio. 90 | * 91 | * @param string $text The text to convert 92 | * 93 | * @return string The audio data 94 | */ 95 | public function textToSpeech(string $text): string 96 | { 97 | return $this->sendRequest("https://code.responsivevoice.org/getvoice.php", [ 98 | "tl" => $this->language, 99 | "t" => $text, 100 | ]); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/Providers/VoiceRssProvider.php: -------------------------------------------------------------------------------- 1 | apikey = $apikey; 42 | 43 | if ($language !== null) { 44 | $this->language = $this->getLanguage($language); 45 | } 46 | 47 | if ($speed !== null) { 48 | $this->speed = $this->getSpeed($speed); 49 | } 50 | 51 | if ($voice !== null) { 52 | $this->voice = $voice; 53 | } 54 | } 55 | 56 | 57 | /** 58 | * Check if the language is valid, and convert it to the required format. 59 | * 60 | * @param string $language The language to use 61 | * 62 | * @return string 63 | */ 64 | private function getLanguage(string $language): string 65 | { 66 | $language = strtolower(trim($language)); 67 | 68 | if (strlen($language) === 2) { 69 | $language = "{$language}-{$language}"; 70 | } 71 | 72 | if (!preg_match("/^[a-z]{2}-[a-z]{2}$/", $language)) { 73 | throw new InvalidArgumentException("Unexpected language code ({$language}), codes should be 2 characters, a hyphen, and a further 2 characters"); 74 | } 75 | 76 | return $language; 77 | } 78 | 79 | 80 | /** 81 | * Set the language to use. 82 | * 83 | * @param string $language The language to use (eg 'en') 84 | * 85 | * @return self 86 | */ 87 | public function withLanguage(string $language): self 88 | { 89 | $provider = clone $this; 90 | 91 | $provider->language = $this->getLanguage($language); 92 | 93 | return $provider; 94 | } 95 | 96 | 97 | /** 98 | * Check the speech rate is valid. 99 | * 100 | * @param int $speed The speech rate to use 101 | * 102 | * @return int 103 | */ 104 | private function getSpeed(int $speed) 105 | { 106 | if ($speed < -10 || $speed > 10) { 107 | throw new InvalidArgumentException("Invalid speed ({$speed}), must be a number between -10 and 10"); 108 | } 109 | 110 | return $speed; 111 | } 112 | 113 | 114 | /** 115 | * Set the speech rate to use. 116 | * 117 | * @param int $speed The speech rate to use (between -10 and 10) 118 | * 119 | * @return $this 120 | */ 121 | public function withSpeed(int $speed): self 122 | { 123 | $provider = clone $this; 124 | 125 | $provider->speed = $this->getSpeed($speed); 126 | 127 | return $provider; 128 | } 129 | 130 | 131 | /** 132 | * Set the voice to use. 133 | * 134 | * @param string $voice The voice to use (this must be compatible with the language) 135 | * 136 | * @return $this 137 | */ 138 | public function withVoice(string $voice): self 139 | { 140 | $provider = clone $this; 141 | 142 | $provider->voice = $voice; 143 | 144 | return $provider; 145 | } 146 | 147 | 148 | public function getOptions(): array 149 | { 150 | return [ 151 | "language" => $this->language, 152 | "voice" => $this->voice, 153 | "speed" => $this->speed, 154 | ]; 155 | } 156 | 157 | 158 | /** 159 | * Convert the specified text to audio. 160 | * 161 | * @param string $text The text to convert 162 | * 163 | * @return string The audio data 164 | */ 165 | public function textToSpeech(string $text): string 166 | { 167 | $result = $this->sendRequest("https://api.voicerss.org/", [ 168 | "key" => $this->apikey, 169 | "src" => $text, 170 | "hl" => $this->language, 171 | "v" => $this->voice, 172 | "r" => (string) $this->speed, 173 | "c" => "MP3", 174 | "f" => "16khz_16bit_stereo", 175 | ]); 176 | 177 | if (substr($result, 0, 6) === "ERROR:") { 178 | throw new ProviderException("TextToSpeech {$result}"); 179 | } 180 | 181 | return $result; 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /src/TextToSpeech.php: -------------------------------------------------------------------------------- 1 | text = $text; 43 | $this->provider = $provider; 44 | } 45 | 46 | 47 | /** 48 | * Get the audio for this text. 49 | * 50 | * @return string The audio data 51 | */ 52 | public function getAudioData(): string 53 | { 54 | if ($this->data === null) { 55 | $this->data = $this->provider->textToSpeech($this->text); 56 | } 57 | 58 | return $this->data; 59 | } 60 | 61 | 62 | /** 63 | * Generate the filename to be used for this text. 64 | * 65 | * @return string 66 | */ 67 | public function generateFilename(): string 68 | { 69 | $options = $this->provider->getOptions(); 70 | 71 | $options["text"] = $this->text; 72 | 73 | $data = serialize($options); 74 | 75 | return md5($data) . "." . $this->provider->getFormat(); 76 | } 77 | 78 | 79 | /** 80 | * Create an audio file on the filesystem. 81 | * 82 | * @param string $filename The filename to write to 83 | * 84 | * @return $this 85 | */ 86 | public function save(string $filename): TextToSpeechInterface 87 | { 88 | $result = file_put_contents($filename, $this->getAudioData()); 89 | 90 | if ($result === false) { 91 | throw new RuntimeException("Unable to save the file ({$filename})"); 92 | } 93 | 94 | return $this; 95 | } 96 | 97 | 98 | /** 99 | * Store the audio file on the filesystem. 100 | * 101 | * This function uses caching so if the file already exists 102 | * a call to the text-to-speech service is not made. 103 | * 104 | * @param string $path The path to the directory to store the file in 105 | * 106 | * @return string The full path and filename 107 | */ 108 | public function getFile(string $path = null): string 109 | { 110 | if ($path === null) { 111 | $path = sys_get_temp_dir(); 112 | } 113 | 114 | $filename = $path . "/" . $this->generateFilename(); 115 | 116 | if (!is_file($filename)) { 117 | $this->save($filename); 118 | } 119 | 120 | return $filename; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/TextToSpeechInterface.php: -------------------------------------------------------------------------------- 1 |