定制 adachsoft/text-to-audio-xai 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 TextToSpeechInterface that 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-curl enabled
  • 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 via CurlXaiTtsHttpClient.

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/ and tests/.
  • 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 adachsoft/text-to-audio-xai 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 36
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-19