adachsoft/text-to-audio-xai
Composer 安装命令:
composer require adachsoft/text-to-audio-xai
包简介
Explainable text-to-audio library built on top of adachsoft/text-to-audio-contract.
README 文档
README
PHP wrapper and CLI for the xAI Text-To-Speech (TTS) API, built on top of the adachsoft/text-to-audio-contract.
This package provides:
- An implementation of
TextToSpeechInterfacethat talks to the official xAI TTS HTTP API. - A small CLI tool (
bin/text-to-audio-xai) that turns plain text or text files into audio files. - A thin HTTP client based on PHP's cURL extension.
Important: this project is not an official xAI product. Use it at your own risk and make sure you comply with xAI's terms of service.
Requirements
- PHP 8.3 or higher
ext-curlenabled- Composer
- A valid xAI API key (
XAI_KEY)
Installation
Install via Composer:
composer require adachsoft/text-to-audio-xai
The package depends on adachsoft/text-to-audio-contract, which will be installed automatically.
Configuration
The CLI and service expect the xAI API key to be provided via the XAI_KEY environment variable.
A typical project layout uses a .env file and vlucas/phpdotenv to load environment variables:
# .env
XAI_KEY=sk-...
The provided bin/bootstrap.php script loads vendor/autoload.php and then reads .env using Dotenv::createImmutable().
CLI usage
The main entrypoint is the bin/text-to-audio-xai script:
php bin/text-to-audio-xai [options]
Supported options:
--text "Some text"– text to be synthesized.--file path.txt– path to a text file to be synthesized.--voice name– logical voice identifier (default:"default").--format mp3|wav|opus– output audio format (default:mp3).--output file– optional output file name.-h,--help– show help.
Exactly one of --text or --file must be provided.
Examples
Basic usage with inline text (default format mp3, default output directory var/data):
php bin/text-to-audio-xai --text "Hello from xAI TTS"
Specify voice and output file name:
php bin/text-to-audio-xai \
--text "Cześć, tu Grok po polsku" \
--voice leo \
--output grok-pl.mp3
Use a text file as input and change format:
php bin/text-to-audio-xai \
--file resources/text/introduction.txt \
--format wav
By default, audio files are written into the var/data directory (created on demand). The file extension is derived from the requested audio format.
Library usage
You can also use the services directly from your PHP code, without the CLI.
use AdachSoft\TextToAudioContract\Dto\TextToSpeechRequestDto;
use AdachSoft\TextToAudioXai\CurlXaiTtsHttpClient;
use AdachSoft\TextToAudioXai\TextToSpeechService;
$xaiKey = getenv('XAI_KEY') ?: ($_ENV['XAI_KEY'] ?? '');
$client = new CurlXaiTtsHttpClient();
$textToSpeech = new TextToSpeechService($xaiKey, $client);
$request = new TextToSpeechRequestDto(
text: 'Hello from PHP',
voice: 'default',
format: 'mp3',
);
$response = $textToSpeech->synthesize($request);
file_put_contents('hello.mp3', $response->audioContent);
The low-level HTTP interaction with xAI is encapsulated in CurlXaiTtsHttpClient and the service itself enforces simple validation rules (e.g. non-empty API key, non-empty text).
Testing
The project comes with a test suite based on PHPUnit:
- Unit tests for the core service (
TextToSpeechService) and CLI helpers (e.g.Cli\AudioFileWriter). - Functional tests for the CLI wrapper.
- A production test group (
group=production) that uses the real xAI API viaCurlXaiTtsHttpClient.
To run the default test suite (without production tests):
vendor/bin/phpunit
To run the production tests (will call the real xAI API and requires a valid XAI_KEY):
vendor/bin/phpunit --group production
Quality tools
The repository is configured to use the following tools:
- PHPStan – static analysis for
src/andtests/. - Rector – automated refactorings and code quality improvements.
- PHP-CS-Fixer – coding style enforcement.
- PHPUnit – unit, functional and production tests.
You can run them with the usual vendor binaries, for example:
phpstan analyse src
phpstan analyse tests
vendor/bin/phpunit
vendor/bin/phpunit --group production
PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix src
PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix tests
Changelog and versioning
All notable changes are documented in CHANGELOG.md.
The first public version of this library is 0.1.0.
adachsoft/text-to-audio-xai 适用场景与选型建议
adachsoft/text-to-audio-xai 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「audio」 「text-to-speech」 「tts」 「xai」 「adachsoft」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adachsoft/text-to-audio-xai 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adachsoft/text-to-audio-xai 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adachsoft/text-to-audio-xai 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package creates a shared API to easily use Text to Speech functionalities amongst different TTS providers.
Library for manipulating audio files (validating, transcoding, and tagging)
Audio Manager for text-to-speech cloud services
IVONA SpeechCloud SDK for PHP
ElevenLabs API client
Based on Amazon AWS Polly. Text to Speech serializer
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-19