forked from Blendman974/faceclaw
Merge branch 'main' of https://github.com/jimrandomh/faceclaw into android-wear-integration
This commit is contained in:
commit
92a2530e0e
66 files changed
+3731
-1433
No files matched your search
+333
-92
@@ -6,151 +6,392 @@ import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothManager;
|
||||
import android.bluetooth.le.BluetoothLeScanner;
|
||||
import android.bluetooth.le.ScanCallback;
|
||||
import android.bluetooth.le.ScanRecord;
|
||||
import android.bluetooth.le.ScanResult;
|
||||
import android.bluetooth.le.ScanSettings;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Finds Even Realities hardware over BLE and hands every advertisement up to
|
||||
* TypeScript with the raw fields the pairing screen needs: the LIVE local name
|
||||
* from the scan record (BluetoothDevice.getName() is a cache that can hold a
|
||||
* pre-reset value indefinitely), the full manufacturer-specific payload with
|
||||
* its company identifier restored so the "ER"+serial+MAC layout survives
|
||||
* intact, RSSI, advertised TX power, and connectability.
|
||||
*
|
||||
* Admission is deliberately loose — a name containing G2 or R1, or the Even
|
||||
* "ER" manufacturer signature — because renamed custom firmware may drop the
|
||||
* stock name. Side, serial, and pair matching are decided in TypeScript
|
||||
* (app/g2/even-advertisement.ts, app/g2/pairing-candidates.ts) where they can
|
||||
* be unit-tested.
|
||||
*
|
||||
* Ported from the discovery path of SybilSight's Bluetooth SDK (G2.didDiscover
|
||||
* and R1's advertisement snapshot), reduced to what Android exposes.
|
||||
*/
|
||||
@SuppressLint("MissingPermission")
|
||||
public class FaceclawDeviceDiscovery {
|
||||
private static final Pattern RING_NAME = Pattern.compile("^EVEN R1_[0-9A-F]{6}$");
|
||||
private static final Pattern RIGHT_NAME = Pattern.compile("^Even G2_32_R_[0-9A-F]{6}$");
|
||||
private static final Pattern LEFT_NAME = Pattern.compile("^Even G2_32_L_[0-9A-F]{6}$");
|
||||
/** Even Realities' company identifier: the ASCII bytes "ER" read little-endian. */
|
||||
public static final int EVEN_COMPANY_ID = 0x5245;
|
||||
|
||||
private final Context appContext;
|
||||
private final BluetoothAdapter bluetoothAdapter;
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
private final Object lock = new Object();
|
||||
private ScanCallback activeCallback;
|
||||
private volatile FaceclawDeviceDiscoveryListener listener;
|
||||
|
||||
public FaceclawDeviceDiscovery(Context context) {
|
||||
this.appContext = context.getApplicationContext();
|
||||
Context appContext = context.getApplicationContext();
|
||||
BluetoothManager bluetoothManager =
|
||||
(BluetoothManager) this.appContext.getSystemService(Context.BLUETOOTH_SERVICE);
|
||||
(BluetoothManager) appContext.getSystemService(Context.BLUETOOTH_SERVICE);
|
||||
if (bluetoothManager == null || bluetoothManager.getAdapter() == null) {
|
||||
throw new IllegalStateException("Bluetooth adapter unavailable");
|
||||
}
|
||||
this.bluetoothAdapter = bluetoothManager.getAdapter();
|
||||
}
|
||||
|
||||
public String getBondedCandidatesJson() {
|
||||
LinkedHashMap<String, CandidateDevice> matches = new LinkedHashMap<>();
|
||||
Set<BluetoothDevice> bonded = bluetoothAdapter.getBondedDevices();
|
||||
if (bonded != null) {
|
||||
for (BluetoothDevice device : bonded) {
|
||||
maybeAddCandidate(matches, device, "paired");
|
||||
}
|
||||
}
|
||||
return encodeCandidates(matches);
|
||||
public boolean isBluetoothEnabled() {
|
||||
return bluetoothAdapter.isEnabled();
|
||||
}
|
||||
|
||||
public String scanCandidatesJson(int timeoutMs) throws InterruptedException {
|
||||
LinkedHashMap<String, CandidateDevice> matches = new LinkedHashMap<>();
|
||||
BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
|
||||
if (scanner == null) {
|
||||
return encodeCandidates(matches);
|
||||
}
|
||||
final ScanCallback callback = new ScanCallback() {
|
||||
@Override
|
||||
public void onScanResult(int callbackType, ScanResult result) {
|
||||
if (result == null) {
|
||||
return;
|
||||
}
|
||||
maybeAddCandidate(matches, result.getDevice(), "scan");
|
||||
}
|
||||
public void setListener(FaceclawDeviceDiscoveryListener newListener) {
|
||||
this.listener = newListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatchScanResults(java.util.List<ScanResult> results) {
|
||||
if (results == null) {
|
||||
return;
|
||||
}
|
||||
for (ScanResult result : results) {
|
||||
// ------------------------------------------------------------------
|
||||
// Live scan
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Start streaming advertisements to the listener. Returns false when the
|
||||
* radio is off or the scanner is unavailable; a later platform refusal
|
||||
* arrives through onScanFailed. Calling while a scan is running is a no-op.
|
||||
*/
|
||||
public boolean startScan() {
|
||||
synchronized (lock) {
|
||||
if (activeCallback != null) {
|
||||
return true;
|
||||
}
|
||||
if (!bluetoothAdapter.isEnabled()) {
|
||||
emitLog("scan not started: Bluetooth is off");
|
||||
return false;
|
||||
}
|
||||
BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
|
||||
if (scanner == null) {
|
||||
emitLog("scan not started: no BLE scanner");
|
||||
return false;
|
||||
}
|
||||
final ScanCallback callback = new ScanCallback() {
|
||||
@Override
|
||||
public void onScanResult(int callbackType, ScanResult result) {
|
||||
if (result != null) {
|
||||
maybeAddCandidate(matches, result.getDevice(), "scan");
|
||||
emitAdvertisement(result);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatchScanResults(List<ScanResult> results) {
|
||||
if (results == null) {
|
||||
return;
|
||||
}
|
||||
for (ScanResult result : results) {
|
||||
if (result != null) {
|
||||
emitAdvertisement(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanFailed(int errorCode) {
|
||||
synchronized (lock) {
|
||||
if (activeCallback == this) {
|
||||
activeCallback = null;
|
||||
}
|
||||
}
|
||||
final FaceclawDeviceDiscoveryListener l = listener;
|
||||
if (l != null) {
|
||||
final String message = describeScanError(errorCode);
|
||||
mainHandler.post(() -> l.onScanFailed(errorCode, message));
|
||||
}
|
||||
}
|
||||
};
|
||||
activeCallback = callback;
|
||||
try {
|
||||
scanner.startScan(null, buildScanSettings(), callback);
|
||||
} catch (Throwable t) {
|
||||
activeCallback = null;
|
||||
emitLog("scan not started: " + t);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
scanner.startScan(
|
||||
null,
|
||||
new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(),
|
||||
callback
|
||||
);
|
||||
try {
|
||||
Thread.sleep(Math.max(500, timeoutMs));
|
||||
} finally {
|
||||
emitLog("scan started (unfiltered, low latency)");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void stopScan() {
|
||||
ScanCallback callback;
|
||||
synchronized (lock) {
|
||||
callback = activeCallback;
|
||||
activeCallback = null;
|
||||
}
|
||||
if (callback == null) {
|
||||
return;
|
||||
}
|
||||
BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
|
||||
if (scanner != null) {
|
||||
try {
|
||||
scanner.stopScan(callback);
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
}
|
||||
return encodeCandidates(matches);
|
||||
emitLog("scan stopped");
|
||||
}
|
||||
|
||||
private void maybeAddCandidate(Map<String, CandidateDevice> matches, BluetoothDevice device, String source) {
|
||||
if (device == null) {
|
||||
return;
|
||||
public boolean isScanning() {
|
||||
synchronized (lock) {
|
||||
return activeCallback != null;
|
||||
}
|
||||
String name = device.getName();
|
||||
String role = classifyRole(name);
|
||||
if (role == null) {
|
||||
return;
|
||||
}
|
||||
String address = device.getAddress();
|
||||
if (address == null || address.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
matches.put(address, new CandidateDevice(address, name == null ? "" : name, role, source));
|
||||
}
|
||||
|
||||
private String classifyRole(String name) {
|
||||
if (name == null) {
|
||||
return null;
|
||||
/**
|
||||
* Replay bonded Even devices to the listener. A bonded listing has no
|
||||
* signal sample and no manufacturer data, so its serial stays unknown until
|
||||
* the device is also heard advertising.
|
||||
*/
|
||||
public void emitBondedDevices() {
|
||||
final FaceclawDeviceDiscoveryListener l = listener;
|
||||
if (l == null) {
|
||||
return;
|
||||
}
|
||||
if (RIGHT_NAME.matcher(name).matches()) {
|
||||
return "right";
|
||||
for (String json : bondedCandidateJsonList()) {
|
||||
l.onAdvertisement(json);
|
||||
}
|
||||
if (LEFT_NAME.matcher(name).matches()) {
|
||||
return "left";
|
||||
}
|
||||
if (RING_NAME.matcher(name).matches()) {
|
||||
return "ring";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String encodeCandidates(Map<String, CandidateDevice> matches) {
|
||||
/** Bonded Even devices as one JSON array, for callers without a listener. */
|
||||
public String getBondedCandidatesJson() {
|
||||
JSONArray array = new JSONArray();
|
||||
for (CandidateDevice candidate : matches.values()) {
|
||||
JSONObject object = new JSONObject();
|
||||
for (String json : bondedCandidateJsonList()) {
|
||||
try {
|
||||
object.put("address", candidate.address);
|
||||
object.put("name", candidate.name);
|
||||
object.put("role", candidate.role);
|
||||
object.put("source", candidate.source);
|
||||
array.put(object);
|
||||
array.put(new JSONObject(json));
|
||||
} catch (JSONException ignored) {
|
||||
}
|
||||
}
|
||||
return array.toString();
|
||||
}
|
||||
|
||||
private static final class CandidateDevice {
|
||||
final String address;
|
||||
final String name;
|
||||
final String role;
|
||||
final String source;
|
||||
// ------------------------------------------------------------------
|
||||
// Internals
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
CandidateDevice(String address, String name, String role, String source) {
|
||||
this.address = address;
|
||||
this.name = name;
|
||||
this.role = role;
|
||||
this.source = source;
|
||||
private static ScanSettings buildScanSettings() {
|
||||
ScanSettings.Builder builder = new ScanSettings.Builder()
|
||||
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
|
||||
.setReportDelay(0);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
builder.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
|
||||
builder.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE);
|
||||
builder.setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT);
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
// Accept extended advertising too; setLegacy(true) would hide it.
|
||||
builder.setLegacy(false);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private void emitAdvertisement(ScanResult result) {
|
||||
final JSONObject object = toJson(result);
|
||||
if (object == null) {
|
||||
return;
|
||||
}
|
||||
final FaceclawDeviceDiscoveryListener l = listener;
|
||||
if (l == null) {
|
||||
return;
|
||||
}
|
||||
final String json = object.toString();
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
l.onAdvertisement(json);
|
||||
} else {
|
||||
mainHandler.post(() -> {
|
||||
FaceclawDeviceDiscoveryListener current = listener;
|
||||
if (current != null) {
|
||||
current.onAdvertisement(json);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> bondedCandidateJsonList() {
|
||||
List<String> out = new ArrayList<>();
|
||||
Set<BluetoothDevice> bonded = bluetoothAdapter.getBondedDevices();
|
||||
if (bonded == null) {
|
||||
return out;
|
||||
}
|
||||
long now = System.currentTimeMillis();
|
||||
for (BluetoothDevice device : bonded) {
|
||||
if (device == null) {
|
||||
continue;
|
||||
}
|
||||
String name = device.getName();
|
||||
if (!isAdmissible(name, null)) {
|
||||
continue;
|
||||
}
|
||||
JSONObject object = buildJson(
|
||||
device.getAddress(), name, "", null, null, null, true, "paired", now);
|
||||
if (object != null) {
|
||||
out.add(object.toString());
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Null when the result is not something we pair with. */
|
||||
private static JSONObject toJson(ScanResult result) {
|
||||
BluetoothDevice device = result.getDevice();
|
||||
if (device == null) {
|
||||
return null;
|
||||
}
|
||||
ScanRecord record = result.getScanRecord();
|
||||
String liveName = record == null ? null : record.getDeviceName();
|
||||
String name = liveName != null && !liveName.isEmpty() ? liveName : device.getName();
|
||||
byte[] manufacturerData = extractEvenManufacturerData(record);
|
||||
if (!isAdmissible(name, manufacturerData)) {
|
||||
return null;
|
||||
}
|
||||
Integer txPower = null;
|
||||
if (record != null && record.getTxPowerLevel() != Integer.MIN_VALUE) {
|
||||
txPower = record.getTxPowerLevel();
|
||||
}
|
||||
if (txPower == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
|
||||
&& result.getTxPower() != ScanResult.TX_POWER_NOT_PRESENT) {
|
||||
txPower = result.getTxPower();
|
||||
}
|
||||
Boolean connectable = null;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
connectable = result.isConnectable();
|
||||
}
|
||||
int rssi = result.getRssi();
|
||||
// 127 is the "unavailable" sentinel; 0 dBm never happens for a real sample.
|
||||
Integer rssiValue = (rssi == 127 || rssi == 0) ? null : rssi;
|
||||
boolean bonded = device.getBondState() == BluetoothDevice.BOND_BONDED;
|
||||
return buildJson(
|
||||
device.getAddress(), name, toHex(manufacturerData), rssiValue, txPower,
|
||||
connectable, bonded, "scan", System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* Android strips the 2-byte company identifier from each manufacturer
|
||||
* record and keys the SparseArray by it. Put the bytes back (little-endian)
|
||||
* so TypeScript sees the same "ER"+… layout the iOS SDK and HCI captures
|
||||
* document. Prefers Even's own id; otherwise returns the first record so an
|
||||
* unexpected company id still reaches the diagnostics log.
|
||||
*/
|
||||
private static byte[] extractEvenManufacturerData(ScanRecord record) {
|
||||
if (record == null) {
|
||||
return new byte[0];
|
||||
}
|
||||
SparseArray<byte[]> all = record.getManufacturerSpecificData();
|
||||
if (all == null || all.size() == 0) {
|
||||
return new byte[0];
|
||||
}
|
||||
byte[] payload = all.get(EVEN_COMPANY_ID);
|
||||
int companyId = EVEN_COMPANY_ID;
|
||||
if (payload == null) {
|
||||
companyId = all.keyAt(0);
|
||||
payload = all.valueAt(0);
|
||||
}
|
||||
if (payload == null) {
|
||||
return new byte[0];
|
||||
}
|
||||
byte[] out = new byte[payload.length + 2];
|
||||
out[0] = (byte) (companyId & 0xff);
|
||||
out[1] = (byte) ((companyId >> 8) & 0xff);
|
||||
System.arraycopy(payload, 0, out, 2, payload.length);
|
||||
return out;
|
||||
}
|
||||
|
||||
private static boolean isAdmissible(String name, byte[] manufacturerData) {
|
||||
if (manufacturerData != null && manufacturerData.length >= 2
|
||||
&& manufacturerData[0] == 0x45 && manufacturerData[1] == 0x52) {
|
||||
return true;
|
||||
}
|
||||
if (name == null) {
|
||||
return false;
|
||||
}
|
||||
String upper = name.toUpperCase();
|
||||
return upper.contains("G2") || upper.contains("R1");
|
||||
}
|
||||
|
||||
private static JSONObject buildJson(
|
||||
String address, String name, String manufacturerHex, Integer rssi, Integer txPower,
|
||||
Boolean connectable, boolean bonded, String source, long seenAtMs) {
|
||||
if (address == null || address.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("address", address);
|
||||
object.put("name", name == null ? "" : name);
|
||||
object.put("manufacturerData", manufacturerHex == null ? "" : manufacturerHex);
|
||||
object.put("rssi", rssi == null ? JSONObject.NULL : rssi);
|
||||
object.put("txPower", txPower == null ? JSONObject.NULL : txPower);
|
||||
object.put("connectable", connectable == null ? JSONObject.NULL : connectable);
|
||||
object.put("bonded", bonded);
|
||||
object.put("source", source);
|
||||
object.put("seenAtMs", seenAtMs);
|
||||
return object;
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String toHex(byte[] bytes) {
|
||||
if (bytes == null || bytes.length == 0) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02X", b & 0xff));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String describeScanError(int errorCode) {
|
||||
switch (errorCode) {
|
||||
case ScanCallback.SCAN_FAILED_ALREADY_STARTED:
|
||||
return "a scan is already running";
|
||||
case ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED:
|
||||
return "the Bluetooth stack refused to register the scan (try toggling Bluetooth)";
|
||||
case ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED:
|
||||
return "BLE scanning is not supported on this device";
|
||||
case ScanCallback.SCAN_FAILED_INTERNAL_ERROR:
|
||||
return "internal Bluetooth error";
|
||||
case 5: // SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES (API 31)
|
||||
return "out of Bluetooth hardware resources";
|
||||
case 6: // SCAN_FAILED_SCANNING_TOO_FREQUENTLY (API 31)
|
||||
return "scanning too frequently; wait a moment and try again";
|
||||
default:
|
||||
return "scan failed (code " + errorCode + ")";
|
||||
}
|
||||
}
|
||||
|
||||
private void emitLog(String line) {
|
||||
FaceclawDeviceDiscoveryListener l = listener;
|
||||
if (l != null) {
|
||||
l.onLog(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Callbacks from FaceclawDeviceDiscovery's live scan to the TypeScript layer. */
|
||||
public interface FaceclawDeviceDiscoveryListener {
|
||||
/**
|
||||
* One advertisement (or one bonded device) as a JSON object:
|
||||
* {address, name, manufacturerData (hex, company id included), rssi|null,
|
||||
* txPower|null, connectable|null, bonded, source, seenAtMs}.
|
||||
* Protocol decoding happens in TypeScript (see app/g2/even-advertisement.ts).
|
||||
*/
|
||||
void onAdvertisement(String json);
|
||||
|
||||
/** The platform refused to start or continue the scan. */
|
||||
void onScanFailed(int errorCode, String message);
|
||||
|
||||
/** Diagnostic line for the pairing log. */
|
||||
void onLog(String line);
|
||||
}
|
||||
+52
-147
@@ -1,30 +1,20 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import com.k2fsa.sherpa.onnx.FeatureConfig;
|
||||
import com.k2fsa.sherpa.onnx.KeywordSpotter;
|
||||
import com.k2fsa.sherpa.onnx.KeywordSpotterConfig;
|
||||
import com.k2fsa.sherpa.onnx.KeywordSpotterResult;
|
||||
import com.k2fsa.sherpa.onnx.OfflineModelConfig;
|
||||
import com.k2fsa.sherpa.onnx.OfflineMoonshineModelConfig;
|
||||
import com.k2fsa.sherpa.onnx.OfflineRecognizer;
|
||||
import com.k2fsa.sherpa.onnx.OfflineRecognizerConfig;
|
||||
import com.k2fsa.sherpa.onnx.OfflineRecognizerResult;
|
||||
import com.k2fsa.sherpa.onnx.OfflineStream;
|
||||
import com.k2fsa.sherpa.onnx.OnlineModelConfig;
|
||||
import com.k2fsa.sherpa.onnx.OnlineStream;
|
||||
import com.k2fsa.sherpa.onnx.OnlineTransducerModelConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Arrays;
|
||||
@@ -57,17 +47,15 @@ public class FaceclawVoiceController {
|
||||
private static final float TRANSCRIPT_NORMALIZE_TARGET_PEAK = 0.9f;
|
||||
private static final float TRANSCRIPT_NORMALIZE_MAX_GAIN = 30f;
|
||||
private static final int TRANSCRIPT_LOG_PREVIEW_CHARS = 80;
|
||||
private static final String ASSET_ROOT = "faceclaw-voice";
|
||||
private static final String MODEL_DIR = "sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01";
|
||||
private static final String ASR_ASSET_ROOT = "faceclaw-voice-asr";
|
||||
// Model directory shared with the TS-side download flow (asr-model.ts),
|
||||
// which fetches the Moonshine files here on demand; they are no longer
|
||||
// bundled in the APK.
|
||||
private static final String ASR_ROOT = "faceclaw-voice-asr";
|
||||
private static final String ASR_MODEL_DIR = "sherpa-onnx-moonshine-base-en-quantized-2026-02-27";
|
||||
private static final String[] MODEL_FILES = {
|
||||
"encoder-epoch-12-avg-2-chunk-16-left-64.onnx",
|
||||
"decoder-epoch-12-avg-2-chunk-16-left-64.onnx",
|
||||
"joiner-epoch-12-avg-2-chunk-16-left-64.onnx",
|
||||
"tokens.txt",
|
||||
"screen-on-keywords.txt"
|
||||
};
|
||||
// Model files for the retired on-phone wake-word spotter, copied to
|
||||
// filesDir by earlier releases; deleted on sight to reclaim the space.
|
||||
// (The wakeword is now detected by the glasses firmware itself.)
|
||||
private static final String LEGACY_KWS_ROOT = "faceclaw-voice";
|
||||
private static final String[] ASR_MODEL_FILES = {
|
||||
"encoder_model.ort",
|
||||
"decoder_model_merged.ort",
|
||||
@@ -75,7 +63,6 @@ public class FaceclawVoiceController {
|
||||
};
|
||||
|
||||
private enum VoiceInputMode {
|
||||
WAKEWORD, // on-phone keyword spotting (kept for later; not currently wired)
|
||||
ONBOARD, // on-phone Moonshine transcription
|
||||
CLOUD // decode locally, emit PCM for a cloud recognizer on the TS side
|
||||
}
|
||||
@@ -89,10 +76,8 @@ public class FaceclawVoiceController {
|
||||
private volatile FaceclawBleCommunicator communicator;
|
||||
private Thread workerThread;
|
||||
private volatile boolean started;
|
||||
private VoiceInputMode mode = VoiceInputMode.WAKEWORD;
|
||||
private KeywordSpotter keywordSpotter;
|
||||
private VoiceInputMode mode = VoiceInputMode.CLOUD;
|
||||
private OfflineRecognizer recognizer;
|
||||
private OnlineStream stream;
|
||||
private FaceclawLc3Decoder lc3Decoder;
|
||||
private final float[] transcriptSamples = new float[TRANSCRIPT_SEGMENT_MAX_SAMPLES];
|
||||
private int transcriptSampleCount;
|
||||
@@ -140,10 +125,6 @@ public class FaceclawVoiceController {
|
||||
this.endpointing = endpointing;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
start("wakeword");
|
||||
}
|
||||
|
||||
public void start(String requestedMode) {
|
||||
synchronized (lock) {
|
||||
if (started) {
|
||||
@@ -194,26 +175,23 @@ public class FaceclawVoiceController {
|
||||
if ("cloud".equals(requestedMode)) {
|
||||
return VoiceInputMode.CLOUD;
|
||||
}
|
||||
if ("onboard".equals(requestedMode) || "full".equals(requestedMode)) {
|
||||
return VoiceInputMode.ONBOARD;
|
||||
}
|
||||
return VoiceInputMode.WAKEWORD;
|
||||
return VoiceInputMode.ONBOARD;
|
||||
}
|
||||
|
||||
private void runLoop() {
|
||||
try {
|
||||
deleteLegacyKwsFiles();
|
||||
VoiceInputMode currentMode = mode;
|
||||
if (currentMode == VoiceInputMode.ONBOARD) {
|
||||
File modelDir = findAsrModelDir();
|
||||
if (modelDir == null) {
|
||||
emitStatus("Voice model not downloaded (see Settings > Voice).");
|
||||
return;
|
||||
}
|
||||
emitStatus("Loading transcription model...");
|
||||
File modelDir = installAsrModelFiles();
|
||||
recognizer = new OfflineRecognizer(buildRecognizerConfig(modelDir));
|
||||
resetTranscriptState();
|
||||
lastTranscript = "";
|
||||
} else if (currentMode == VoiceInputMode.WAKEWORD) {
|
||||
emitStatus("Loading wake-word model...");
|
||||
File modelDir = installModelFiles();
|
||||
keywordSpotter = new KeywordSpotter(buildConfig(modelDir));
|
||||
stream = keywordSpotter.createStream();
|
||||
}
|
||||
lc3Decoder = new FaceclawLc3Decoder();
|
||||
endpointDetector.reset();
|
||||
@@ -225,9 +203,7 @@ public class FaceclawVoiceController {
|
||||
|
||||
emitStatus(currentMode == VoiceInputMode.CLOUD
|
||||
? "Listening (cloud)..."
|
||||
: currentMode == VoiceInputMode.ONBOARD
|
||||
? "Listening..."
|
||||
: "Listening for \"screen on\"...");
|
||||
: "Listening...");
|
||||
processG2Audio();
|
||||
// Button released / stop requested: emit one final full-utterance
|
||||
// transcript so the UI can freeze it.
|
||||
@@ -312,29 +288,6 @@ public class FaceclawVoiceController {
|
||||
return b.array();
|
||||
}
|
||||
|
||||
private KeywordSpotterConfig buildConfig(File modelDir) {
|
||||
return KeywordSpotterConfig.builder()
|
||||
.setFeatureConfig(FeatureConfig.builder()
|
||||
.setSampleRate(SAMPLE_RATE)
|
||||
.setFeatureDim(FEATURE_DIM)
|
||||
.build())
|
||||
.setOnlineModelConfig(OnlineModelConfig.builder()
|
||||
.setTransducer(OnlineTransducerModelConfig.builder()
|
||||
.setEncoder(new File(modelDir, "encoder-epoch-12-avg-2-chunk-16-left-64.onnx").getAbsolutePath())
|
||||
.setDecoder(new File(modelDir, "decoder-epoch-12-avg-2-chunk-16-left-64.onnx").getAbsolutePath())
|
||||
.setJoiner(new File(modelDir, "joiner-epoch-12-avg-2-chunk-16-left-64.onnx").getAbsolutePath())
|
||||
.build())
|
||||
.setTokens(new File(modelDir, "tokens.txt").getAbsolutePath())
|
||||
.setModelType("zipformer2")
|
||||
.setModelingUnit("")
|
||||
.setNumThreads(1)
|
||||
.build())
|
||||
.setKeywordsFile(new File(modelDir, "screen-on-keywords.txt").getAbsolutePath())
|
||||
.setKeywordsScore(1.5f)
|
||||
.setKeywordsThreshold(0.35f)
|
||||
.build();
|
||||
}
|
||||
|
||||
private OfflineRecognizerConfig buildRecognizerConfig(File modelDir) {
|
||||
return OfflineRecognizerConfig.builder()
|
||||
.setFeatureConfig(FeatureConfig.builder()
|
||||
@@ -352,50 +305,42 @@ public class FaceclawVoiceController {
|
||||
.build();
|
||||
}
|
||||
|
||||
private File installModelFiles() throws IOException {
|
||||
File modelDir = new File(appContext.getFilesDir(), ASSET_ROOT + File.separator + MODEL_DIR);
|
||||
if (!modelDir.exists() && !modelDir.mkdirs()) {
|
||||
throw new IOException("Could not create " + modelDir.getAbsolutePath());
|
||||
}
|
||||
AssetManager assets = appContext.getAssets();
|
||||
for (String fileName : MODEL_FILES) {
|
||||
copyAssetIfNeeded(
|
||||
assets,
|
||||
ASSET_ROOT + "/" + MODEL_DIR + "/" + fileName,
|
||||
new File(modelDir, fileName)
|
||||
);
|
||||
}
|
||||
return modelDir;
|
||||
}
|
||||
|
||||
private File installAsrModelFiles() throws IOException {
|
||||
File modelDir = new File(appContext.getFilesDir(), ASR_ASSET_ROOT + File.separator + ASR_MODEL_DIR);
|
||||
if (!modelDir.exists() && !modelDir.mkdirs()) {
|
||||
throw new IOException("Could not create " + modelDir.getAbsolutePath());
|
||||
}
|
||||
AssetManager assets = appContext.getAssets();
|
||||
/**
|
||||
* The Moonshine model directory, populated by the download flow in
|
||||
* asr-model.ts (releases before 0.5.0 copied the same files out of the
|
||||
* APK, so upgraded installs are already complete). Null when any file is
|
||||
* missing, i.e. the model still needs to be downloaded.
|
||||
*/
|
||||
private File findAsrModelDir() {
|
||||
File modelDir = new File(appContext.getFilesDir(), ASR_ROOT + File.separator + ASR_MODEL_DIR);
|
||||
for (String fileName : ASR_MODEL_FILES) {
|
||||
copyAssetIfNeeded(
|
||||
assets,
|
||||
ASR_ASSET_ROOT + "/" + ASR_MODEL_DIR + "/" + fileName,
|
||||
new File(modelDir, fileName)
|
||||
);
|
||||
}
|
||||
return modelDir;
|
||||
}
|
||||
|
||||
private void copyAssetIfNeeded(AssetManager assets, String assetPath, File destination) throws IOException {
|
||||
if (destination.exists() && destination.length() > 0) {
|
||||
return;
|
||||
}
|
||||
try (InputStream input = assets.open(assetPath);
|
||||
FileOutputStream output = new FileOutputStream(destination)) {
|
||||
byte[] buffer = new byte[64 * 1024];
|
||||
int read;
|
||||
while ((read = input.read(buffer)) >= 0) {
|
||||
output.write(buffer, 0, read);
|
||||
File file = new File(modelDir, fileName);
|
||||
if (!file.exists() || file.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return modelDir;
|
||||
}
|
||||
|
||||
private void deleteLegacyKwsFiles() {
|
||||
try {
|
||||
deleteRecursively(new File(appContext.getFilesDir(), LEGACY_KWS_ROOT));
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "failed to delete legacy wake-word files", t);
|
||||
}
|
||||
}
|
||||
|
||||
private static void deleteRecursively(File file) {
|
||||
if (!file.exists()) {
|
||||
return;
|
||||
}
|
||||
File[] children = file.listFiles();
|
||||
if (children != null) {
|
||||
for (File child : children) {
|
||||
deleteRecursively(child);
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
|
||||
private boolean startG2Audio() {
|
||||
@@ -413,9 +358,8 @@ public class FaceclawVoiceController {
|
||||
private void processG2Audio() {
|
||||
short[] pcm = new short[FaceclawLc3Decoder.SAMPLES_PER_PACKET];
|
||||
while (started && !Thread.currentThread().isInterrupted()) {
|
||||
OnlineStream currentStream = stream;
|
||||
FaceclawLc3Decoder currentDecoder = lc3Decoder;
|
||||
if (currentDecoder == null || (mode == VoiceInputMode.WAKEWORD && currentStream == null)) {
|
||||
if (currentDecoder == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -438,40 +382,17 @@ public class FaceclawVoiceController {
|
||||
}
|
||||
if (mode == VoiceInputMode.CLOUD) {
|
||||
emitPcm(pcm, count);
|
||||
} else if (mode == VoiceInputMode.ONBOARD) {
|
||||
float[] samples = new float[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
samples[i] = pcm[i] / 32768.0f;
|
||||
}
|
||||
processRecognizer(samples);
|
||||
} else {
|
||||
float[] samples = new float[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
samples[i] = pcm[i] / 32768.0f;
|
||||
}
|
||||
currentStream.acceptWaveform(samples, SAMPLE_RATE);
|
||||
processKeywordSpotter(currentStream);
|
||||
processRecognizer(samples);
|
||||
}
|
||||
maybeEmitAudioStats(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void processKeywordSpotter(OnlineStream currentStream) {
|
||||
KeywordSpotter currentSpotter = keywordSpotter;
|
||||
if (currentSpotter == null) {
|
||||
return;
|
||||
}
|
||||
while (currentSpotter.isReady(currentStream)) {
|
||||
currentSpotter.decode(currentStream);
|
||||
KeywordSpotterResult result = currentSpotter.getResult(currentStream);
|
||||
String keyword = result == null ? "" : result.getKeyword();
|
||||
if (keyword != null && keyword.trim().length() > 0) {
|
||||
currentSpotter.reset(currentStream);
|
||||
emitWakeWord(keyword);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processRecognizer(float[] samples) {
|
||||
appendTranscriptSamples(samples);
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
@@ -783,14 +704,6 @@ public class FaceclawVoiceController {
|
||||
}
|
||||
|
||||
private void releaseSherpa() {
|
||||
if (stream != null) {
|
||||
stream.release();
|
||||
stream = null;
|
||||
}
|
||||
if (keywordSpotter != null) {
|
||||
keywordSpotter.release();
|
||||
keywordSpotter = null;
|
||||
}
|
||||
if (recognizer != null) {
|
||||
recognizer.release();
|
||||
recognizer = null;
|
||||
@@ -893,14 +806,6 @@ public class FaceclawVoiceController {
|
||||
mainHandler.post(() -> currentListener.onStatus(status));
|
||||
}
|
||||
|
||||
private void emitWakeWord(String keyword) {
|
||||
FaceclawVoiceControllerListener currentListener = listener;
|
||||
if (currentListener == null) {
|
||||
return;
|
||||
}
|
||||
mainHandler.post(() -> currentListener.onWakeWord(keyword));
|
||||
}
|
||||
|
||||
private void emitTranscript(String text, boolean isFinal) {
|
||||
FaceclawVoiceControllerListener currentListener = listener;
|
||||
if (currentListener == null) {
|
||||
|
||||
-2
@@ -3,8 +3,6 @@ package com.faceclaw.app;
|
||||
public interface FaceclawVoiceControllerListener {
|
||||
void onStatus(String status);
|
||||
|
||||
void onWakeWord(String keyword);
|
||||
|
||||
/**
|
||||
* Best transcript of the current utterance so far. REPLACE semantics: text
|
||||
* is the complete transcript, not a delta — display it verbatim, replacing
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class KeywordSpotter {
|
||||
private long ptr;
|
||||
|
||||
public KeywordSpotter(KeywordSpotterConfig config) {
|
||||
System.loadLibrary("onnxruntime");
|
||||
System.loadLibrary("sherpa-onnx-jni");
|
||||
ptr = newFromFile(config);
|
||||
if (ptr == 0) {
|
||||
throw new IllegalArgumentException("Invalid KeywordSpotterConfig");
|
||||
}
|
||||
}
|
||||
|
||||
public OnlineStream createStream() {
|
||||
return createStream("");
|
||||
}
|
||||
|
||||
public OnlineStream createStream(String keywords) {
|
||||
return new OnlineStream(createStream(ptr, keywords == null ? "" : keywords));
|
||||
}
|
||||
|
||||
public boolean isReady(OnlineStream stream) {
|
||||
return stream != null && stream.getPtr() != 0 && isReady(ptr, stream.getPtr());
|
||||
}
|
||||
|
||||
public void decode(OnlineStream stream) {
|
||||
decode(ptr, stream.getPtr());
|
||||
}
|
||||
|
||||
public void reset(OnlineStream stream) {
|
||||
reset(ptr, stream.getPtr());
|
||||
}
|
||||
|
||||
public KeywordSpotterResult getResult(OnlineStream stream) {
|
||||
return getResult(ptr, stream.getPtr());
|
||||
}
|
||||
|
||||
public void release() {
|
||||
if (ptr == 0) {
|
||||
return;
|
||||
}
|
||||
delete(ptr);
|
||||
ptr = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
release();
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
private native long newFromFile(KeywordSpotterConfig config);
|
||||
|
||||
private native void delete(long ptr);
|
||||
|
||||
private native long createStream(long ptr, String keywords);
|
||||
|
||||
private native boolean isReady(long ptr, long streamPtr);
|
||||
|
||||
private native void decode(long ptr, long streamPtr);
|
||||
|
||||
private native void reset(long ptr, long streamPtr);
|
||||
|
||||
private native KeywordSpotterResult getResult(long ptr, long streamPtr);
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class KeywordSpotterConfig {
|
||||
private final FeatureConfig featConfig;
|
||||
private final OnlineModelConfig modelConfig;
|
||||
private final int maxActivePaths;
|
||||
private final String keywordsFile;
|
||||
private final float keywordsScore;
|
||||
private final float keywordsThreshold;
|
||||
private final int numTrailingBlanks;
|
||||
|
||||
private KeywordSpotterConfig(Builder builder) {
|
||||
this.featConfig = builder.featConfig;
|
||||
this.modelConfig = builder.modelConfig;
|
||||
this.maxActivePaths = builder.maxActivePaths;
|
||||
this.keywordsFile = builder.keywordsFile;
|
||||
this.keywordsScore = builder.keywordsScore;
|
||||
this.keywordsThreshold = builder.keywordsThreshold;
|
||||
this.numTrailingBlanks = builder.numTrailingBlanks;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public FeatureConfig getFeatConfig() {
|
||||
return featConfig;
|
||||
}
|
||||
|
||||
public OnlineModelConfig getModelConfig() {
|
||||
return modelConfig;
|
||||
}
|
||||
|
||||
public int getMaxActivePaths() {
|
||||
return maxActivePaths;
|
||||
}
|
||||
|
||||
public String getKeywordsFile() {
|
||||
return keywordsFile;
|
||||
}
|
||||
|
||||
public float getKeywordsScore() {
|
||||
return keywordsScore;
|
||||
}
|
||||
|
||||
public float getKeywordsThreshold() {
|
||||
return keywordsThreshold;
|
||||
}
|
||||
|
||||
public int getNumTrailingBlanks() {
|
||||
return numTrailingBlanks;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private FeatureConfig featConfig = FeatureConfig.builder().build();
|
||||
private OnlineModelConfig modelConfig = OnlineModelConfig.builder().build();
|
||||
private int maxActivePaths = 4;
|
||||
private String keywordsFile = "keywords.txt";
|
||||
private float keywordsScore = 1.5f;
|
||||
private float keywordsThreshold = 0.25f;
|
||||
private int numTrailingBlanks = 2;
|
||||
|
||||
public Builder setFeatureConfig(FeatureConfig featConfig) {
|
||||
this.featConfig = featConfig;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setOnlineModelConfig(OnlineModelConfig modelConfig) {
|
||||
this.modelConfig = modelConfig;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setKeywordsFile(String keywordsFile) {
|
||||
this.keywordsFile = keywordsFile;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setKeywordsScore(float keywordsScore) {
|
||||
this.keywordsScore = keywordsScore;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setKeywordsThreshold(float keywordsThreshold) {
|
||||
this.keywordsThreshold = keywordsThreshold;
|
||||
return this;
|
||||
}
|
||||
|
||||
public KeywordSpotterConfig build() {
|
||||
return new KeywordSpotterConfig(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class KeywordSpotterResult {
|
||||
private final String keyword;
|
||||
private final String[] tokens;
|
||||
private final float[] timestamps;
|
||||
|
||||
public KeywordSpotterResult(String keyword, String[] tokens, float[] timestamps) {
|
||||
this.keyword = keyword == null ? "" : keyword;
|
||||
this.tokens = tokens == null ? new String[0] : tokens;
|
||||
this.timestamps = timestamps == null ? new float[0] : timestamps;
|
||||
}
|
||||
|
||||
public String getKeyword() {
|
||||
return keyword;
|
||||
}
|
||||
|
||||
public String[] getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
public float[] getTimestamps() {
|
||||
return timestamps;
|
||||
}
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OnlineModelConfig {
|
||||
private final OnlineTransducerModelConfig transducer;
|
||||
private final OnlineParaformerModelConfig paraformer;
|
||||
private final OnlineZipformer2CtcModelConfig zipformer2Ctc;
|
||||
private final OnlineNeMoCtcModelConfig neMoCtc;
|
||||
private final OnlineToneCtcModelConfig toneCtc;
|
||||
private final String tokens;
|
||||
private final int numThreads;
|
||||
private final boolean debug;
|
||||
private final String provider;
|
||||
private final String modelType;
|
||||
private final String modelingUnit;
|
||||
private final String bpeVocab;
|
||||
|
||||
private OnlineModelConfig(Builder builder) {
|
||||
this.transducer = builder.transducer;
|
||||
this.paraformer = builder.paraformer;
|
||||
this.zipformer2Ctc = builder.zipformer2Ctc;
|
||||
this.neMoCtc = builder.neMoCtc;
|
||||
this.toneCtc = builder.toneCtc;
|
||||
this.tokens = builder.tokens;
|
||||
this.numThreads = builder.numThreads;
|
||||
this.debug = builder.debug;
|
||||
this.provider = builder.provider;
|
||||
this.modelType = builder.modelType;
|
||||
this.modelingUnit = builder.modelingUnit;
|
||||
this.bpeVocab = builder.bpeVocab;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public OnlineTransducerModelConfig getTransducer() {
|
||||
return transducer;
|
||||
}
|
||||
|
||||
public OnlineParaformerModelConfig getParaformer() {
|
||||
return paraformer;
|
||||
}
|
||||
|
||||
public OnlineZipformer2CtcModelConfig getZipformer2Ctc() {
|
||||
return zipformer2Ctc;
|
||||
}
|
||||
|
||||
public OnlineNeMoCtcModelConfig getNeMoCtc() {
|
||||
return neMoCtc;
|
||||
}
|
||||
|
||||
public OnlineToneCtcModelConfig getToneCtc() {
|
||||
return toneCtc;
|
||||
}
|
||||
|
||||
public String getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
public int getNumThreads() {
|
||||
return numThreads;
|
||||
}
|
||||
|
||||
public boolean getDebug() {
|
||||
return debug;
|
||||
}
|
||||
|
||||
public String getProvider() {
|
||||
return provider;
|
||||
}
|
||||
|
||||
public String getModelType() {
|
||||
return modelType;
|
||||
}
|
||||
|
||||
public String getModelingUnit() {
|
||||
return modelingUnit;
|
||||
}
|
||||
|
||||
public String getBpeVocab() {
|
||||
return bpeVocab;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private OnlineTransducerModelConfig transducer = OnlineTransducerModelConfig.builder().build();
|
||||
private OnlineParaformerModelConfig paraformer = OnlineParaformerModelConfig.builder().build();
|
||||
private OnlineZipformer2CtcModelConfig zipformer2Ctc = OnlineZipformer2CtcModelConfig.builder().build();
|
||||
private OnlineNeMoCtcModelConfig neMoCtc = OnlineNeMoCtcModelConfig.builder().build();
|
||||
private OnlineToneCtcModelConfig toneCtc = OnlineToneCtcModelConfig.builder().build();
|
||||
private String tokens = "";
|
||||
private int numThreads = 1;
|
||||
private boolean debug = false;
|
||||
private String provider = "cpu";
|
||||
private String modelType = "";
|
||||
private String modelingUnit = "cjkchar";
|
||||
private String bpeVocab = "";
|
||||
|
||||
public Builder setTransducer(OnlineTransducerModelConfig transducer) {
|
||||
this.transducer = transducer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setTokens(String tokens) {
|
||||
this.tokens = tokens;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setModelType(String modelType) {
|
||||
this.modelType = modelType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setNumThreads(int numThreads) {
|
||||
this.numThreads = numThreads;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setProvider(String provider) {
|
||||
this.provider = provider;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setModelingUnit(String modelingUnit) {
|
||||
this.modelingUnit = modelingUnit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setBpeVocab(String bpeVocab) {
|
||||
this.bpeVocab = bpeVocab;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OnlineModelConfig build() {
|
||||
return new OnlineModelConfig(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OnlineNeMoCtcModelConfig {
|
||||
private final String model;
|
||||
|
||||
private OnlineNeMoCtcModelConfig(Builder builder) {
|
||||
this.model = builder.model;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String model = "";
|
||||
|
||||
public Builder setModel(String model) {
|
||||
this.model = model;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OnlineNeMoCtcModelConfig build() {
|
||||
return new OnlineNeMoCtcModelConfig(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OnlineParaformerModelConfig {
|
||||
private final String encoder;
|
||||
private final String decoder;
|
||||
|
||||
private OnlineParaformerModelConfig(Builder builder) {
|
||||
this.encoder = builder.encoder;
|
||||
this.decoder = builder.decoder;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public String getEncoder() {
|
||||
return encoder;
|
||||
}
|
||||
|
||||
public String getDecoder() {
|
||||
return decoder;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String encoder = "";
|
||||
private String decoder = "";
|
||||
|
||||
public Builder setEncoder(String encoder) {
|
||||
this.encoder = encoder;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDecoder(String decoder) {
|
||||
this.decoder = decoder;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OnlineParaformerModelConfig build() {
|
||||
return new OnlineParaformerModelConfig(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OnlineStream {
|
||||
private long ptr;
|
||||
|
||||
public OnlineStream(long ptr) {
|
||||
this.ptr = ptr;
|
||||
}
|
||||
|
||||
public long getPtr() {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
public void acceptWaveform(float[] samples, int sampleRate) {
|
||||
acceptWaveform(ptr, samples, sampleRate);
|
||||
}
|
||||
|
||||
public void release() {
|
||||
if (ptr == 0) {
|
||||
return;
|
||||
}
|
||||
delete(ptr);
|
||||
ptr = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
release();
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
private native void acceptWaveform(long ptr, float[] samples, int sampleRate);
|
||||
|
||||
private native void delete(long ptr);
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OnlineToneCtcModelConfig {
|
||||
private final String model;
|
||||
|
||||
private OnlineToneCtcModelConfig(Builder builder) {
|
||||
this.model = builder.model;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String model = "";
|
||||
|
||||
public Builder setModel(String model) {
|
||||
this.model = model;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OnlineToneCtcModelConfig build() {
|
||||
return new OnlineToneCtcModelConfig(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OnlineTransducerModelConfig {
|
||||
private final String encoder;
|
||||
private final String decoder;
|
||||
private final String joiner;
|
||||
|
||||
private OnlineTransducerModelConfig(Builder builder) {
|
||||
this.encoder = builder.encoder;
|
||||
this.decoder = builder.decoder;
|
||||
this.joiner = builder.joiner;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public String getEncoder() {
|
||||
return encoder;
|
||||
}
|
||||
|
||||
public String getDecoder() {
|
||||
return decoder;
|
||||
}
|
||||
|
||||
public String getJoiner() {
|
||||
return joiner;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String encoder = "";
|
||||
private String decoder = "";
|
||||
private String joiner = "";
|
||||
|
||||
public Builder setEncoder(String encoder) {
|
||||
this.encoder = encoder;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDecoder(String decoder) {
|
||||
this.decoder = decoder;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setJoiner(String joiner) {
|
||||
this.joiner = joiner;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OnlineTransducerModelConfig build() {
|
||||
return new OnlineTransducerModelConfig(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OnlineZipformer2CtcModelConfig {
|
||||
private final String model;
|
||||
|
||||
private OnlineZipformer2CtcModelConfig(Builder builder) {
|
||||
this.model = builder.model;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String model = "";
|
||||
|
||||
public Builder setModel(String model) {
|
||||
this.model = model;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OnlineZipformer2CtcModelConfig build() {
|
||||
return new OnlineZipformer2CtcModelConfig(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user