初始化 TTS 引擎工程:Android 系统引擎、Go 在线合成服务与测试客户端

- android-engine:TextToSpeechService 引擎,含设置页、语音包导入(SHA-256 校验)与预热
- server:Go CPU 合成服务与部署文件
- test-client:端到端验证客户端
- 模型与语音包不入库,见 models/README.md
This commit is contained in:
tongtong committed 2026-11-23 14:59:46 +08:00
commit 1856de97bc
62 files changed
+3787

No files matched your search

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
<package android:name="com.ttstd.tts.engine" />
</queries>
<application
android:allowBackup="false"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.DeviceDefault.Light">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
@@ -0,0 +1,221 @@
package com.ttstd.tts.testclient;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.speech.tts.Voice;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Set;
public class MainActivity extends Activity {
private static final String TAG = "TtsTestClient";
private static final String ENGINE = "com.ttstd.tts.engine";
private static final String[] SWEEP_TEXTS = {"一", "二", "三", "四", "五", "六"};
private TextView logView;
private TextView rateLabel;
private TextView pitchLabel;
private EditText textInput;
private SeekBar rateBar;
private SeekBar pitchBar;
private TextToSpeech tts;
private boolean ready;
private int utteranceSeq;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logView = findViewById(R.id.log_view);
logView.setMovementMethod(new ScrollingMovementMethod());
textInput = findViewById(R.id.text_input);
rateBar = findViewById(R.id.rate_bar);
pitchBar = findViewById(R.id.pitch_bar);
rateLabel = findViewById(R.id.rate_label);
pitchLabel = findViewById(R.id.pitch_label);
SeekBar.OnSeekBarChangeListener labels = new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
updateLabels();
}
public void onStartTrackingTouch(SeekBar bar) {
}
public void onStopTrackingTouch(SeekBar bar) {
}
};
rateBar.setOnSeekBarChangeListener(labels);
pitchBar.setOnSeekBarChangeListener(labels);
updateLabels();
findViewById(R.id.btn_speak).setOnClickListener(v -> speak());
findViewById(R.id.btn_stop).setOnClickListener(v -> stop());
findViewById(R.id.btn_sweep).setOnClickListener(v -> sweep());
initTts();
}
private void initTts() {
appendLog("连接引擎 " + ENGINE);
tts = new TextToSpeech(this, status -> {
appendLog("onInit status=" + status);
if (status != TextToSpeech.SUCCESS) {
appendLog("引擎初始化失败");
return;
}
ready = true;
logEngines();
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
appendLog("开始合成 " + utteranceId);
}
@Override
public void onDone(String utteranceId) {
appendLog("播放完成 " + utteranceId);
}
@Override
public void onError(String utteranceId) {
appendLog("合成/播放失败 " + utteranceId);
}
});
appendLog("setLanguage zh-CN -> " + tts.setLanguage(Locale.SIMPLIFIED_CHINESE));
listVoices();
if (getIntent().getBooleanExtra("autosweep", false)) sweep();
else if (getIntent().getBooleanExtra("autospeak", false)) {
long delay = getIntent().getLongExtra("speak_delay_ms", 0);
if (delay > 0) logView.postDelayed(this::speak, delay);
else speak();
}
}, ENGINE);
}
private void logEngines() {
List<TextToSpeech.EngineInfo> engines = tts.getEngines();
appendLog("系统引擎数量 " + engines.size());
for (TextToSpeech.EngineInfo engine : engines) {
appendLog("引擎 " + engine.name + " | " + engine.label);
}
List<ResolveInfo> services = getPackageManager().queryIntentServices(
new Intent("android.intent.action.TTS_SERVICE"), 0);
appendLog("PM TTS 服务数量(flags=0) " + (services == null ? -1 : services.size()));
if (services != null) {
for (ResolveInfo info : services) {
if (info.serviceInfo != null) {
appendLog("PM 服务 " + info.serviceInfo.packageName + "/" + info.serviceInfo.name);
}
}
}
Intent explicit = new Intent("android.intent.action.TTS_SERVICE").setPackage(ENGINE);
List<ResolveInfo> explicitServices = getPackageManager().queryIntentServices(explicit, 0);
appendLog("显式包查询数量 " + (explicitServices == null ? -1 : explicitServices.size()));
try {
getPackageManager().getPackageInfo(ENGINE, 0);
appendLog("getPackageInfo(" + ENGINE + ") 可见");
} catch (Exception e) {
appendLog("getPackageInfo(" + ENGINE + ") 不可见: " + e.getClass().getSimpleName());
}
}
private void listVoices() {
Set<Voice> voices = tts.getVoices();
if (voices == null) {
appendLog("getVoices() = null");
return;
}
appendLog("语音数量 " + voices.size());
for (Voice voice : voices) {
appendLog("语音 " + voice.getName() + " | " + voice.getLocale().toLanguageTag()
+ " | q=" + voice.getQuality());
}
}
private void speak() {
if (!ready) {
appendLog("引擎未就绪");
return;
}
String text = textInput.getText().toString().trim();
if (text.isEmpty()) {
appendLog("文本为空");
return;
}
float rate = rate();
float pitch = pitch();
tts.setSpeechRate(rate);
tts.setPitch(pitch);
String id = "utt-" + (++utteranceSeq);
int result = tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, id);
appendLog(String.format(Locale.ROOT, "speak %s speed=%.2f pitch=%.2f -> %d", id, rate, pitch, result));
}
private void sweep() {
if (!ready) {
appendLog("引擎未就绪");
return;
}
float[] rates = {0.5f, 1.0f, 1.5f, 2.0f, 1.0f, 1.0f};
float[] pitches = {1.0f, 1.0f, 1.0f, 1.0f, 0.5f, 2.0f};
for (int i = 0; i < rates.length; i++) {
tts.setSpeechRate(rates[i]);
tts.setPitch(pitches[i]);
String id = "sweep-" + (++utteranceSeq);
tts.speak(SWEEP_TEXTS[i], TextToSpeech.QUEUE_ADD, null, id);
appendLog(String.format(Locale.ROOT, "%s speed=%.2f pitch=%.2f", id, rates[i], pitches[i]));
}
}
private void stop() {
if (tts != null) {
tts.stop();
appendLog("stop()");
}
}
private float rate() {
return (50 + rateBar.getProgress()) / 100f;
}
private float pitch() {
return (50 + pitchBar.getProgress()) / 100f;
}
private void updateLabels() {
rateLabel.setText(getString(R.string.rate_label, rate()));
pitchLabel.setText(getString(R.string.pitch_label, pitch()));
}
private void appendLog(String message) {
Log.i(TAG, message);
String line = new SimpleDateFormat("HH:mm:ss", Locale.ROOT).format(new Date()) + " " + message;
logView.append(line + "\n");
}
@Override
protected void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
tts = null;
}
super.onDestroy();
}
}
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_text"
android:inputType="text"
android:text="你好,现在是北京时间十二点整" />
<TextView
android:id="@+id/rate_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/rate_label" />
<SeekBar
android:id="@+id/rate_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="150"
android:progress="50" />
<TextView
android:id="@+id/pitch_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pitch_label" />
<SeekBar
android:id="@+id/pitch_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="150"
android:progress="50" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_speak"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/action_speak" />
<Button
android:id="@+id/btn_stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/action_stop" />
<Button
android:id="@+id/btn_sweep"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/action_sweep" />
</LinearLayout>
<TextView
android:id="@+id/log_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:background="#11000000"
android:padding="8dp"
android:scrollbars="vertical"
android:textIsSelectable="true"
android:textSize="12sp" />
</LinearLayout>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TTS 引擎测试</string>
<string name="hint_text">朗读内容</string>
<string name="rate_label">语速 %1$.2f</string>
<string name="pitch_label">音调 %1$.2f</string>
<string name="action_speak">朗读</string>
<string name="action_stop">停止</string>
<string name="action_sweep">参数遍历</string>
</resources>