diff --git a/.github/workflows/secret-scan.yml b/.github/workflows/secret-scan.yml new file mode 100644 index 00000000..e07f47b0 --- /dev/null +++ b/.github/workflows/secret-scan.yml @@ -0,0 +1,22 @@ +name: Secret scan + +# Scans for committed secrets on pushes to main and on pull requests. + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + gitleaks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: gitleaks/gitleaks-action@v2 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 00000000..dce2bfd9 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,28 @@ +name: Tests + +# Runs the test suite on pushes to main and on pull requests. + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: npm ci + - run: npm test + + # The shared Kotlin tests (tests/kotlin, on the JVM host). The runner image + # already carries the JDK 17 and Android SDK the build needs. + test-kotlin: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: npm ci + - run: npm run test:kotlin diff --git a/.gitignore b/.gitignore index 3c6e0d8c..e928afc2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,17 @@ # NativeScript -hooks/ +hooks/* +!hooks/before-prepare/ +hooks/before-prepare/* +!hooks/before-prepare/faceclaw-kotlin.js node_modules/ platforms/ # Release APKs from scripts/release.sh dist/ +# CPU captures and source maps pulled from the installed app +profiles/ + # Logs logs *.log @@ -39,10 +45,27 @@ old # pull_config.sh output: the phone's live settings, including API keys faceclaw_settings.xml +faceclaw_settings.ios.json # Developer configs build_paths.sh +**/xcuserdata/ +# Kotlin build outputs and staged NativeScript artifacts +native/kotlin/.gradle/ +native/kotlin/.kotlin/ +native/kotlin/build/ +native/kotlin/shared/build/ +native/kotlin/local.properties +native/kotlin/plugin/platforms/android/*.aar +native/kotlin/plugin/platforms/ios/ +!native/kotlin/plugin/platforms/ +!native/kotlin/plugin/platforms/android/ +!native/kotlin/plugin/platforms/android/include.gradle + +tests/kotlin/.gradle/ +tests/kotlin/.kotlin/ +tests/kotlin/build/ # tools/health-preview.cjs output (rendered PNGs, regenerate with the tool) preview/ diff --git a/.gitleaks.toml b/.gitleaks.toml new file mode 100644 index 00000000..e87ce09b --- /dev/null +++ b/.gitleaks.toml @@ -0,0 +1,9 @@ +# Secret-scanning config for the workflow in .github/workflows/secret-scan.yml. +[extend] +useDefault = true + +[[rules]] +id = "generic-api-key" + [rules.allowlist] + description = "SIGN_KEY in even-api.ts is a client-side constant the app needs to sign EvenHub requests, not a credential." + paths = ['''app/apps/evenhub/even-api\.ts'''] diff --git a/App_Resources/Android/app.gradle b/App_Resources/Android/app.gradle index 0e24319a..65d42a3c 100644 --- a/App_Resources/Android/app.gradle +++ b/App_Resources/Android/app.gradle @@ -262,6 +262,7 @@ android { sourceSets.main { jniLibs.srcDirs += ["$projectDir/src/main/jniLibs"] + // Shared Kotlin is packaged by the local @faceclaw/kotlin plugin. } defaultConfig { @@ -288,7 +289,25 @@ android { } } +// NativeScript can retain deleted App_Resources sources in its generated +// project. Remove the former copy before Kotlin compilation after this move. +def removeLegacyKotlinBridgeSource = tasks.register("removeLegacyKotlinBridgeSource", Delete) { + delete file("$projectDir/src/main/java/com/faceclaw/shared/KotlinBridge.kt") + // Explicit, reviewed source moves: never infer deletions from class names. + def migrations = new groovy.json.JsonSlurper().parse(file("$rootDir/../../native/kotlin/migrated-java-sources.json")) + migrations.each { migration -> + if (!file("$rootDir/../../${migration.kotlin}").isFile()) { + throw new GradleException("Missing Kotlin replacement: ${migration.kotlin}") + } + delete file("$projectDir/src/main/java/${migration.java}") + } +} +tasks.matching { it.name.startsWith("compile") && it.name.endsWith("Kotlin") }.configureEach { + dependsOn removeLegacyKotlinBridgeSource +} + tasks.matching { it.name == "preBuild" }.configureEach { + dependsOn removeLegacyKotlinBridgeSource dependsOn tasks.named("prepareFaceclawNativeLibs") dependsOn tasks.named("prepareFaceclawLlama") } diff --git a/App_Resources/Android/before-plugins.gradle b/App_Resources/Android/before-plugins.gradle index 9faffb81..da763a5b 100644 --- a/App_Resources/Android/before-plugins.gradle +++ b/App_Resources/Android/before-plugins.gradle @@ -10,6 +10,7 @@ project.ext { // androidXTransition = "1.4.1" // androidXViewPager = "1.0.0" - // useKotlin = true - // kotlinVersion = "1.6.0" + // NativeScript 9 already applies kotlin-android. Match the shared KMP compiler + // for our .kt sources; no separate Kotlin installation is required. + kotlinVersion = "2.4.20" } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.kt new file mode 100644 index 00000000..a8db0cb5 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidNoiseEffects.kt @@ -0,0 +1,67 @@ +package com.faceclaw.app + +import android.media.audiofx.AcousticEchoCanceler +import android.media.audiofx.AudioEffect +import android.media.audiofx.AutomaticGainControl +import android.media.audiofx.NoiseSuppressor +import java.util.ArrayList + +/** Android AudioRecord effect attachment; PCM processing lives in shared Kotlin. */ +class AndroidNoiseEffects { + companion object { + /** Whether this Android build ships the built-in capture noise suppressor. */ + @JvmStatic + fun platformSuppressorAvailable(): Boolean { + return try { + NoiseSuppressor.isAvailable() + } catch (t: Throwable) { + false + } + } + + /** Short engine name for the UI. */ + @JvmStatic + fun engineDescription(): String { + return "spectral (phone DSP)" + } + + /** + * Attach the platform's built-in capture-effect chain (NoiseSuppressor, + * AcousticEchoCanceler, AutomaticGainControl — whichever this Android + * build provides) to a phone AudioRecord session. Only applicable when the + * phone itself captures the audio; the BLE glasses stream must go through + * {@link #process(byte[])} instead. The caller keeps the returned effects + * for the life of the recording and release()s them with it. + */ + @JvmStatic + fun attachPlatformEffects(audioSessionId: Int): Array { + val effects = ArrayList() + try { + if (NoiseSuppressor.isAvailable()) { + val ns = NoiseSuppressor.create(audioSessionId) + if (ns != null) { + ns.setEnabled(true) + effects.add(ns) + } + } + if (AcousticEchoCanceler.isAvailable()) { + val aec = AcousticEchoCanceler.create(audioSessionId) + if (aec != null) { + aec.setEnabled(true) + effects.add(aec) + } + } + if (AutomaticGainControl.isAvailable()) { + val agc = AutomaticGainControl.create(audioSessionId) + if (agc != null) { + agc.setEnabled(true) + effects.add(agc) + } + } + } catch (t: Throwable) { + // A missing effect just means this tier is unavailable. + } + return effects.toTypedArray() + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSessionLink.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSessionLink.kt new file mode 100644 index 00000000..86f45475 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSessionLink.kt @@ -0,0 +1,44 @@ +package com.faceclaw.app + +import android.bluetooth.BluetoothGatt + +/** [SessionLink] over the Android GATT boundary ([FaceclawBleManager]). */ +class AndroidSessionLink(private val bleManager: FaceclawBleManager) : SessionLink { + override fun connect(address: String, timeoutMs: Int): Boolean = bleManager.connect(address, timeoutMs) + + override fun connect(address: String, timeoutMs: Int, autoConnect: Boolean): Boolean = + bleManager.connect(address, timeoutMs, autoConnect) + + override fun requestHighPriority(address: String) { + bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH) + } + + override fun requestMtu(address: String, mtu: Int, timeoutMs: Int): Boolean = + bleManager.requestMtu(address, mtu, timeoutMs) + + override fun negotiatedMtu(address: String): Int = bleManager.getNegotiatedMtu(address) + + override fun discoverServices(address: String, timeoutMs: Int): Boolean = + bleManager.discoverServices(address, timeoutMs) + + override fun enableNotifications(address: String, characteristicUuid: String, enable: Boolean, timeoutMs: Int): Boolean = + bleManager.enableNotifications(address, characteristicUuid, enable, timeoutMs) + + override fun writeFrames( + address: String, + characteristicUuid: String, + frames: List, + mode: GattWriteMode, + timeoutMs: Int, + ): Boolean = bleManager.writeFrames(address, characteristicUuid, frames, AndroidProtocolPlatform.writeType(mode), timeoutMs) + + override fun disconnect(address: String) = bleManager.disconnect(address) + + override fun close() = bleManager.close() + + override fun isBonded(address: String): Boolean = bleManager.isBonded(address) + + override fun prepareBenchmarkLink(address: String, mode: Int) = bleManager.prepareBenchmarkLink(address, mode) + + override fun recordDisplayFrameSent() = FaceclawBleManager.recordDisplayFrameSent() +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSpeechEngines.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSpeechEngines.kt new file mode 100644 index 00000000..2b149f09 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidSpeechEngines.kt @@ -0,0 +1,147 @@ +package com.faceclaw.app + +import android.util.Log + +import com.k2fsa.sherpa.onnx.FeatureConfig +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.OfflineWhisperModelConfig + +import java.io.File + +/** + * Android implementations of the shared voice ports: the sherpa-onnx offline recognizer as an + * [OfflineTranscriber], the phone microphone as a [PcmAudioSource], and the JNI LC3 decoder as + * an [AudioPacketDecoder]. The pipelines themselves live in the shared VoiceCaptureSession and + * CaptionSession. + */ +internal object AndroidSpeechEngines { + private const val TAG = "FaceclawVoice" + private const val SAMPLE_RATE = 16000 + private const val FEATURE_DIM = 80 + + /** Sherpa-onnx configuration for the on-device model files in [modelDir]. */ + fun recognizerConfig(modelDir: File, kind: VoiceModelKind): OfflineRecognizerConfig { + val modelConfig = OfflineModelConfig.builder() + .setNumThreads(1) + if (kind == VoiceModelKind.WHISPER) { + modelConfig + .setWhisper(OfflineWhisperModelConfig.builder() + .setEncoder(File(modelDir, "base.en-encoder.int8.onnx").absolutePath) + .setDecoder(File(modelDir, "base.en-decoder.int8.onnx").absolutePath) + .setLanguage("en") + .setTask("transcribe") + .build()) + .setTokens(File(modelDir, "base.en-tokens.txt").absolutePath) + } else { + modelConfig + .setMoonshine(OfflineMoonshineModelConfig.builder() + .setEncoder(File(modelDir, "encoder_model.ort").absolutePath) + .setMergedDecoder(File(modelDir, "decoder_model_merged.ort").absolutePath) + .build()) + .setTokens(File(modelDir, "tokens.txt").absolutePath) + } + return OfflineRecognizerConfig.builder() + .setFeatureConfig(FeatureConfig.builder() + .setSampleRate(SAMPLE_RATE) + .setFeatureDim(FEATURE_DIM) + .build()) + .setModelConfig(modelConfig.build()) + .build() + } + + class SherpaTranscriber(config: OfflineRecognizerConfig) : OfflineTranscriber { + private var recognizer: OfflineRecognizer? = OfflineRecognizer(config) + + override fun recognize(samples: FloatArray, count: Int): String { + val currentRecognizer = recognizer ?: return "" + val segment = if (count == samples.size) samples else samples.copyOf(count) + val offlineStream: OfflineStream = currentRecognizer.createStream() + try { + offlineStream.acceptWaveform(segment, SAMPLE_RATE) + currentRecognizer.decode(offlineStream) + val result: OfflineRecognizerResult? = currentRecognizer.getResult(offlineStream) + val raw: String? = if (result == null) "" else result.text + return raw?.trim() ?: "" + } finally { + offlineStream.release() + } + } + + override fun release() { + recognizer?.release() + recognizer = null + } + } + + /** The JNI LC3 decoder behind the shared packet-decoder port. */ + class Lc3DecoderAdapter : AudioPacketDecoder { + private val decoder = FaceclawLc3Decoder() + + override fun decodePacket(packet: ByteArray?, pcmOut: ShortArray?): Int = decoder.decodePacket(packet, pcmOut) + + override val lastAngleDegrees: Int get() = decoder.getLastAngleDegrees() + override val lastSsr: Int get() = decoder.getLastSsr() + override val realPackets: Long get() = decoder.getRealPackets() + override val duplicatePackets: Long get() = decoder.getDuplicatePackets() + override val missingPackets: Long get() = decoder.getMissingPackets() + override val decodeErrors: Long get() = decoder.getDecodeErrors() + + override fun close() = decoder.close() + } + + /** + * Open the phone's own microphone at the pipeline's native format (16 kHz + * mono PCM16), or null when it cannot start — the permission is missing + * (SecurityException) or the device refuses the configuration. + */ + fun openPhoneMic(chunkSamples: Int): PcmAudioSource? { + var record: android.media.AudioRecord? = null + try { + val minBytes = android.media.AudioRecord.getMinBufferSize( + SAMPLE_RATE, + android.media.AudioFormat.CHANNEL_IN_MONO, + android.media.AudioFormat.ENCODING_PCM_16BIT) + val bufferBytes = Math.max(minBytes, chunkSamples * 2 * 4) + record = android.media.AudioRecord( + android.media.MediaRecorder.AudioSource.VOICE_RECOGNITION, + SAMPLE_RATE, + android.media.AudioFormat.CHANNEL_IN_MONO, + android.media.AudioFormat.ENCODING_PCM_16BIT, + bufferBytes) + if (record.state != android.media.AudioRecord.STATE_INITIALIZED) { + record.release() + return null + } + record.startRecording() + if (record.recordingState != android.media.AudioRecord.RECORDSTATE_RECORDING) { + record.release() + return null + } + return AudioRecordSource(record) + } catch (t: Throwable) { + Log.w(TAG, "phone mic open failed", t) + if (record != null) { + record.release() + } + return null + } + } + + private class AudioRecordSource(private val record: android.media.AudioRecord) : PcmAudioSource { + override fun read(pcm: ShortArray): Int = record.read(pcm, 0, pcm.size) + + override fun close() { + try { + record.stop() + } catch (ignored: Throwable) { + // Already stopped or never recording; release below either way. + } + record.release() + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidStockLink.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidStockLink.kt new file mode 100644 index 00000000..3e7d264e --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/AndroidStockLink.kt @@ -0,0 +1,64 @@ +package com.faceclaw.app + +import android.bluetooth.BluetoothDevice +import android.bluetooth.BluetoothGatt +import android.content.Context + +/** + * Android [StockLink]: a 1:1 wrapper over FaceclawBleManager for the stock-firmware flows + * (device-info probe, flash prompt, OTA flasher). Owns its own GATT manager, exactly as each + * flow did before the shared cores existed. + */ +class AndroidStockLink(context: Context) : StockLink, FaceclawBleListener { + private val bleManager = FaceclawBleManager(context.applicationContext) + + @Volatile + private var listener: StockLinkListener? = null + + init { + bleManager.setListener(this) + } + + override fun setListener(listener: StockLinkListener?) { + this.listener = listener + } + + override fun connect(address: String, timeoutMs: Int): Boolean = bleManager.connect(address, timeoutMs) + + override fun prepareLink(address: String, desiredMtu: Int, timeoutMs: Int) { + bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH) + bleManager.requestMtu(address, desiredMtu, timeoutMs) + } + + override fun discoverServices(address: String, timeoutMs: Int): Boolean = bleManager.discoverServices(address, timeoutMs) + + override fun enableNotifications(address: String, characteristicUuid: String, timeoutMs: Int): Boolean = + bleManager.enableNotifications(address, characteristicUuid, true, timeoutMs) + + override fun writeFrames(address: String, characteristicUuid: String, frames: List, mode: GattWriteMode, timeoutMs: Int): Boolean = + bleManager.writeFrames(address, characteristicUuid, frames, AndroidProtocolPlatform.writeType(mode), timeoutMs) + + override fun isConnected(address: String): Boolean = bleManager.isConnected(address) + + override fun bondState(address: String): BondState = + when (bleManager.getBondState(address)) { + BluetoothDevice.BOND_NONE -> BondState.NONE + BluetoothDevice.BOND_BONDING -> BondState.BONDING + BluetoothDevice.BOND_BONDED -> BondState.BONDED + else -> BondState.UNKNOWN + } + + override fun disconnect(address: String) = bleManager.disconnect(address) + + override fun close() = bleManager.close() + + override fun onNotification(address: String?, characteristicUuid: String?, data: ByteArray?) { + if (address == null || characteristicUuid == null || data == null) return + listener?.onNotification(address, characteristicUuid, data) + } + + override fun onConnectionStateChange(address: String?, connected: Boolean) { + if (address == null) return + listener?.onConnectionStateChange(address, connected) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.java deleted file mode 100644 index d490ed5d..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.java +++ /dev/null @@ -1,181 +0,0 @@ -package com.faceclaw.app; - -import android.app.Activity; -import android.app.KeyguardManager; -import android.content.Context; -import android.content.Intent; -import android.graphics.Color; -import android.graphics.Typeface; -import android.os.Build; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.util.TypedValue; -import android.view.Gravity; -import android.view.View; -import android.view.WindowManager; -import android.widget.Button; -import android.widget.LinearLayout; -import android.widget.TextView; - -import java.text.DateFormat; -import java.util.Date; -import java.util.List; - -/** - * The phone's ringing screen: shown over the lock screen by the alarm - * notification's full-screen intent (or by tapping the notification). Pure - * Java, so it works with the JavaScript side asleep or gone. Snooze and - * Dismiss go to the service, which journals them for the engine. - */ -public class FaceclawAlarmActivity extends Activity { - private final Handler handler = new Handler(Looper.getMainLooper()); - private TextView titleView; - private TextView textView; - private TextView timeView; - private Button snoozeButton; - private long itemId; - - private final Runnable poll = new Runnable() { - @Override - public void run() { - List items = FaceclawAlarmService.snapshot(); - if (items.isEmpty()) { - finish(); - return; - } - FaceclawAlarmService.Ringing shown = null; - for (FaceclawAlarmService.Ringing item : items) { - if (item.id == itemId) { - shown = item; - } - } - if (shown == null) { - shown = items.get(0); - itemId = shown.id; - } - render(shown, items.size()); - handler.postDelayed(this, 1000); - } - }; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - showOverLockScreen(); - itemId = getIntent() == null ? 0 : getIntent().getLongExtra(FaceclawAlarms.EXTRA_ID, 0); - setContentView(buildLayout()); - } - - @Override - protected void onNewIntent(Intent intent) { - super.onNewIntent(intent); - setIntent(intent); - itemId = intent == null ? 0 : intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0); - } - - @Override - protected void onResume() { - super.onResume(); - handler.post(poll); - } - - @Override - protected void onPause() { - handler.removeCallbacks(poll); - super.onPause(); - } - - private void showOverLockScreen() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { - setShowWhenLocked(true); - setTurnScreenOn(true); - KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); - if (keyguard != null) { - keyguard.requestDismissKeyguard(this, null); - } - } else { - getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED - | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON - | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); - } - getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - } - - private View buildLayout() { - LinearLayout root = new LinearLayout(this); - root.setOrientation(LinearLayout.VERTICAL); - root.setGravity(Gravity.CENTER); - root.setBackgroundColor(Color.BLACK); - int pad = dp(32); - root.setPadding(pad, pad, pad, pad); - - timeView = new TextView(this); - timeView.setTextColor(Color.WHITE); - timeView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 64); - timeView.setTypeface(Typeface.DEFAULT_BOLD); - timeView.setGravity(Gravity.CENTER); - root.addView(timeView, wrap()); - - titleView = new TextView(this); - titleView.setTextColor(Color.WHITE); - titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 28); - titleView.setGravity(Gravity.CENTER); - titleView.setPadding(0, dp(16), 0, 0); - root.addView(titleView, wrap()); - - textView = new TextView(this); - textView.setTextColor(Color.LTGRAY); - textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); - textView.setGravity(Gravity.CENTER); - textView.setPadding(0, dp(8), 0, dp(40)); - root.addView(textView, wrap()); - - snoozeButton = new Button(this); - snoozeButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); - snoozeButton.setOnClickListener(view -> act(FaceclawAlarmService.ACTION_SNOOZE)); - root.addView(snoozeButton, button()); - - Button dismissButton = new Button(this); - dismissButton.setText("Dismiss"); - dismissButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); - dismissButton.setOnClickListener(view -> act(FaceclawAlarmService.ACTION_DISMISS)); - root.addView(dismissButton, button()); - return root; - } - - private void render(FaceclawAlarmService.Ringing item, int count) { - timeView.setText(DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date())); - titleView.setText(count > 1 ? item.title + " (+" + (count - 1) + " more)" : item.title); - String status = item.silenced ? "Not answered" : item.escalated ? "" : "Ringing on the glasses"; - textView.setText(item.text.isEmpty() ? status : status.isEmpty() ? item.text : item.text + "\n" + status); - snoozeButton.setText(FaceclawAlarms.KIND_TIMER.equals(item.kind) - ? "+" + item.snoozeMinutes + " min" - : "Snooze " + item.snoozeMinutes + " min"); - } - - /** Apply the action to every ringing item (one tap silences the phone). */ - private void act(String action) { - for (FaceclawAlarmService.Ringing item : FaceclawAlarmService.snapshot()) { - Intent intent = new Intent(this, FaceclawAlarmService.class) - .setAction(action) - .putExtra(FaceclawAlarms.EXTRA_ID, item.id); - startService(intent); - } - finish(); - } - - private LinearLayout.LayoutParams wrap() { - return new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); - } - - private LinearLayout.LayoutParams button() { - LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp(64)); - params.topMargin = dp(12); - return params; - } - - private int dp(int value) { - return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics())); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.kt new file mode 100644 index 00000000..3aab1c35 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmActivity.kt @@ -0,0 +1,179 @@ +package com.faceclaw.app + +import android.app.Activity +import android.app.KeyguardManager +import android.content.Context +import android.content.Intent +import android.graphics.Color +import android.graphics.Typeface +import android.os.Build +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.util.TypedValue +import android.view.Gravity +import android.view.View +import android.view.WindowManager +import android.widget.Button +import android.widget.LinearLayout +import android.widget.TextView + +import java.text.DateFormat +import java.util.Date + +/** + * The phone's ringing screen: shown over the lock screen by the alarm + * notification's full-screen intent (or by tapping the notification). Pure + * Java, so it works with the JavaScript side asleep or gone. Snooze and + * Dismiss go to the service, which journals them for the engine. + */ +class FaceclawAlarmActivity : Activity() { + private val handler = Handler(Looper.getMainLooper()) + private var titleView: TextView? = null + private var textView: TextView? = null + private var timeView: TextView? = null + private var snoozeButton: Button? = null + private var itemId: Long = 0 + + private val poll: Runnable = object : Runnable { + override fun run() { + val items = FaceclawAlarmService.snapshot() + if (items.isEmpty()) { + finish() + return + } + var shown: FaceclawAlarmService.Ringing? = null + for (item in items) { + if (item.id == itemId) { + shown = item + } + } + if (shown == null) { + shown = items[0] + itemId = shown.id + } + render(shown, items.size) + handler.postDelayed(this, 1000) + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + showOverLockScreen() + itemId = if (intent == null) 0 else intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0) + setContentView(buildLayout()) + } + + override fun onNewIntent(intent: Intent?) { + super.onNewIntent(intent) + setIntent(intent) + itemId = if (intent == null) 0 else intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0) + } + + override fun onResume() { + super.onResume() + handler.post(poll) + } + + override fun onPause() { + handler.removeCallbacks(poll) + super.onPause() + } + + private fun showOverLockScreen() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + setShowWhenLocked(true) + setTurnScreenOn(true) + val keyguard = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager? + keyguard?.requestDismissKeyguard(this, null) + } else { + @Suppress("DEPRECATION") + window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON + or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD) + } + window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + } + + private fun buildLayout(): View { + val root = LinearLayout(this) + root.orientation = LinearLayout.VERTICAL + root.gravity = Gravity.CENTER + root.setBackgroundColor(Color.BLACK) + val pad = dp(32) + root.setPadding(pad, pad, pad, pad) + + val time = TextView(this) + time.setTextColor(Color.WHITE) + time.setTextSize(TypedValue.COMPLEX_UNIT_SP, 64f) + time.typeface = Typeface.DEFAULT_BOLD + time.gravity = Gravity.CENTER + root.addView(time, wrap()) + timeView = time + + val title = TextView(this) + title.setTextColor(Color.WHITE) + title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 28f) + title.gravity = Gravity.CENTER + title.setPadding(0, dp(16), 0, 0) + root.addView(title, wrap()) + titleView = title + + val text = TextView(this) + text.setTextColor(Color.LTGRAY) + text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f) + text.gravity = Gravity.CENTER + text.setPadding(0, dp(8), 0, dp(40)) + root.addView(text, wrap()) + textView = text + + val snooze = Button(this) + snooze.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f) + snooze.setOnClickListener { act(FaceclawAlarmService.ACTION_SNOOZE) } + root.addView(snooze, button()) + snoozeButton = snooze + + val dismissButton = Button(this) + dismissButton.text = "Dismiss" + dismissButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f) + dismissButton.setOnClickListener { act(FaceclawAlarmService.ACTION_DISMISS) } + root.addView(dismissButton, button()) + return root + } + + private fun render(item: FaceclawAlarmService.Ringing, count: Int) { + timeView!!.text = DateFormat.getTimeInstance(DateFormat.SHORT).format(Date()) + titleView!!.text = if (count > 1) item.title + " (+" + (count - 1) + " more)" else item.title + val status = if (item.silenced) "Not answered" else if (item.escalated) "" else "Ringing on the glasses" + textView!!.text = if (item.text.isEmpty()) status else if (status.isEmpty()) item.text else item.text + "\n" + status + snoozeButton!!.text = if (FaceclawAlarms.KIND_TIMER == item.kind) + "+" + item.snoozeMinutes + " min" + else + "Snooze " + item.snoozeMinutes + " min" + } + + /** Apply the action to every ringing item (one tap silences the phone). */ + private fun act(action: String) { + for (item in FaceclawAlarmService.snapshot()) { + val intent = Intent(this, FaceclawAlarmService::class.java) + .setAction(action) + .putExtra(FaceclawAlarms.EXTRA_ID, item.id) + startService(intent) + } + finish() + } + + private fun wrap(): LinearLayout.LayoutParams { + return LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT) + } + + private fun button(): LinearLayout.LayoutParams { + val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp(64)) + params.topMargin = dp(12) + return params + } + + private fun dp(value: Int): Int { + return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value.toFloat(), resources.displayMetrics)) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmListener.java deleted file mode 100644 index 4d9008f0..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmListener.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.faceclaw.app; - -/** Phone-side alarm actions (dismiss / snooze), delivered to the JS engine; implemented in JS. */ -public interface FaceclawAlarmListener { - void onPhoneAction(long id, String action, int minutes); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.java deleted file mode 100644 index b8d44514..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.faceclaw.app; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; - -/** The AlarmManager target: an item came due, start the ringing service. */ -public class FaceclawAlarmReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - if (intent == null || !FaceclawAlarms.ACTION_EXPIRE.equals(intent.getAction())) { - return; - } - long id = intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0); - if (id == 0) { - return; - } - FaceclawAlarmService.ring( - context.getApplicationContext(), - id, - intent.getStringExtra(FaceclawAlarms.EXTRA_TITLE), - intent.getStringExtra(FaceclawAlarms.EXTRA_TEXT), - intent.getStringExtra(FaceclawAlarms.EXTRA_KIND), - intent.getIntExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, 10) - ); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.kt new file mode 100644 index 00000000..c884bbf1 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmReceiver.kt @@ -0,0 +1,26 @@ +package com.faceclaw.app + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent + +/** The AlarmManager target: an item came due, start the ringing service. */ +class FaceclawAlarmReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent?) { + if (intent == null || FaceclawAlarms.ACTION_EXPIRE != intent.action) { + return + } + val id = intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0) + if (id == 0L) { + return + } + FaceclawAlarmService.ring( + context.applicationContext, + id, + intent.getStringExtra(FaceclawAlarms.EXTRA_TITLE), + intent.getStringExtra(FaceclawAlarms.EXTRA_TEXT), + intent.getStringExtra(FaceclawAlarms.EXTRA_KIND), + intent.getIntExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, 10) + ) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.java deleted file mode 100644 index 4d7ed1b9..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.faceclaw.app; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; - -/** - * Re-arms the persisted alarm schedule when the system would otherwise have - * dropped it: after a reboot (AlarmManager alarms do not survive one), a - * package update, or a wall-clock / time-zone change. Runs without the - * JavaScript side; only the device-protected schedule file is needed. - */ -public class FaceclawAlarmRescheduleReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - String action = intent == null ? null : intent.getAction(); - if (action == null) { - return; - } - switch (action) { - case Intent.ACTION_BOOT_COMPLETED: - case Intent.ACTION_LOCKED_BOOT_COMPLETED: - case Intent.ACTION_MY_PACKAGE_REPLACED: - case Intent.ACTION_TIME_CHANGED: - case Intent.ACTION_TIMEZONE_CHANGED: - case "android.intent.action.QUICKBOOT_POWERON": - case "com.htc.intent.action.QUICKBOOT_POWERON": - FaceclawAlarms.rescheduleAll(context, action); - break; - default: - break; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.kt new file mode 100644 index 00000000..d591704a --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmRescheduleReceiver.kt @@ -0,0 +1,28 @@ +package com.faceclaw.app + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent + +/** + * Re-arms the persisted alarm schedule when the system would otherwise have + * dropped it: after a reboot (AlarmManager alarms do not survive one), a + * package update, or a wall-clock / time-zone change. Runs without the + * JavaScript side; only the device-protected schedule file is needed. + */ +class FaceclawAlarmRescheduleReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent?) { + val action = intent?.action ?: return + when (action) { + Intent.ACTION_BOOT_COMPLETED, + Intent.ACTION_LOCKED_BOOT_COMPLETED, + Intent.ACTION_MY_PACKAGE_REPLACED, + Intent.ACTION_TIME_CHANGED, + Intent.ACTION_TIMEZONE_CHANGED, + "android.intent.action.QUICKBOOT_POWERON", + "com.htc.intent.action.QUICKBOOT_POWERON" -> + FaceclawAlarms.rescheduleAll(context, action) + else -> {} + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.java deleted file mode 100644 index a9cac015..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.java +++ /dev/null @@ -1,649 +0,0 @@ -package com.faceclaw.app; - -import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.Service; -import android.content.Context; -import android.content.Intent; -import android.content.pm.ServiceInfo; -import android.media.AudioAttributes; -import android.media.AudioFocusRequest; -import android.media.AudioManager; -import android.media.MediaPlayer; -import android.media.RingtoneManager; -import android.net.Uri; -import android.os.Build; -import android.os.Handler; -import android.os.IBinder; -import android.os.Looper; -import android.os.PowerManager; -import android.os.VibrationEffect; -import android.os.Vibrator; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -/** - * The ringing foreground service. One instance carries every item currently - * ringing. Each item starts silent on the phone (a heads-up / lock-screen - * notification only, the glasses do the ringing) and escalates to phone - * sound and vibration when the glasses cannot carry it: not connected, not - * on a head, charging, no delivery confirmation from the JS side within a - * few seconds, or no acknowledgement within 30 seconds of delivery. Sound - * uses the alarm stream, so media volume, the ringer switch and (unless set - * to total silence) Do Not Disturb do not silence it. - */ -public class FaceclawAlarmService extends Service { - public static final String CHANNEL_ID = "faceclaw-alarms"; - static final String ACTION_RING = "com.faceclaw.app.action.ALARM_RING"; - static final String ACTION_DELIVERED = "com.faceclaw.app.action.ALARM_DELIVERED"; - static final String ACTION_DISMISS = "com.faceclaw.app.action.ALARM_DISMISS"; - static final String ACTION_SNOOZE = "com.faceclaw.app.action.ALARM_SNOOZE"; - /** Stop an item quietly (acknowledged on the glasses / cancelled by the engine). */ - static final String ACTION_STOP_ITEM = "com.faceclaw.app.action.ALARM_STOP_ITEM"; - - /** How long to wait for the JS side to confirm the glasses are showing the item. */ - static final long DELIVERY_WAIT_MS = 5_000L; - /** How long after delivery to the glasses before the phone joins in. */ - static final long ACK_WAIT_MS = 30_000L; - /** Sound and vibration stop after this; the notification stays. */ - static final long AUTO_SILENCE_MS = 10 * 60_000L; - /** Alarm-stream level (fraction of max) used when the wearer has it muted. */ - static final float MUTED_VOLUME_FRACTION = 0.6f; - - static final class Ringing { - final long id; - final String title; - final String text; - final String kind; - final int snoozeMinutes; - final long ringAtMs; - long deliveredAtMs; - boolean escalated; - boolean silenced; - - Ringing(long id, String title, String text, String kind, int snoozeMinutes) { - this.id = id; - this.title = title == null || title.trim().isEmpty() ? "Alarm" : title; - this.text = text == null ? "" : text; - this.kind = kind == null ? FaceclawAlarms.KIND_ALARM : kind; - this.snoozeMinutes = Math.max(1, snoozeMinutes); - this.ringAtMs = System.currentTimeMillis(); - } - } - - /** Ringing items, oldest first; static so the activity can read them. */ - private static final Map ringing = Collections.synchronizedMap(new LinkedHashMap<>()); - /** - * Signals that arrived before the ring intent was processed (the JS - * engine fires, launches its window and reports delivery on the same - * main thread the service's onStartCommand queues behind), keyed by id - * with their arrival time. Consumed by startRinging. - */ - private static final Map deliveredEarly = Collections.synchronizedMap(new LinkedHashMap<>()); - private static final Map stoppedEarly = Collections.synchronizedMap(new LinkedHashMap<>()); - private static final long EARLY_SIGNAL_MAX_AGE_MS = 15_000L; - - private final Handler handler = new Handler(Looper.getMainLooper()); - private MediaPlayer player; - private Vibrator vibrator; - private PowerManager.WakeLock wakeLock; - private AudioManager audioManager; - private AudioFocusRequest focusRequest; - private int restoreAlarmVolume = -1; - private long foregroundId; - - // ------------------------------------------------------------------ - // Static entry points - - public static void ring(Context context, long id, String title, String text, String kind, int snoozeMinutes) { - Context appContext = context.getApplicationContext(); - Intent intent = new Intent(appContext, FaceclawAlarmService.class) - .setAction(ACTION_RING) - .putExtra(FaceclawAlarms.EXTRA_ID, id) - .putExtra(FaceclawAlarms.EXTRA_TITLE, title) - .putExtra(FaceclawAlarms.EXTRA_TEXT, text) - .putExtra(FaceclawAlarms.EXTRA_KIND, kind) - .putExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, snoozeMinutes); - FaceclawAlarms.startServiceCompat(appContext, intent); - } - - public static void delivered(Context context, long id) { - if (!ringing.containsKey(id)) { - deliveredEarly.put(id, System.currentTimeMillis()); - return; - } - Context appContext = context.getApplicationContext(); - appContext.startService(new Intent(appContext, FaceclawAlarmService.class) - .setAction(ACTION_DELIVERED) - .putExtra(FaceclawAlarms.EXTRA_ID, id)); - } - - public static void stopItem(Context context, long id) { - if (!ringing.containsKey(id)) { - stoppedEarly.put(id, System.currentTimeMillis()); - return; - } - Context appContext = context.getApplicationContext(); - appContext.startService(new Intent(appContext, FaceclawAlarmService.class) - .setAction(ACTION_STOP_ITEM) - .putExtra(FaceclawAlarms.EXTRA_ID, id)); - } - - public static boolean isRinging(long id) { - return ringing.containsKey(id); - } - - public static boolean isAnythingRinging() { - return !ringing.isEmpty(); - } - - static List snapshot() { - synchronized (ringing) { - return new ArrayList<>(ringing.values()); - } - } - - static PendingIntent actionIntent(Context appContext, String action, long id) { - Intent intent = new Intent(appContext, FaceclawAlarmService.class) - .setAction(action) - .putExtra(FaceclawAlarms.EXTRA_ID, id); - int flags = PendingIntent.FLAG_UPDATE_CURRENT; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags |= PendingIntent.FLAG_IMMUTABLE; - } - int code = FaceclawAlarms.requestCode(id) ^ action.hashCode(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - return PendingIntent.getForegroundService(appContext, code, intent, flags); - } - return PendingIntent.getService(appContext, code, intent, flags); - } - - // ------------------------------------------------------------------ - // Service lifecycle - - @Override - public IBinder onBind(Intent intent) { - return null; - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - String action = intent == null ? null : intent.getAction(); - long id = intent == null ? 0 : intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0); - if (action == null) { - // Restarted by the system with nothing to do. - finishIfIdle(); - return START_NOT_STICKY; - } - switch (action) { - case ACTION_RING: - startRinging(new Ringing( - id, - intent.getStringExtra(FaceclawAlarms.EXTRA_TITLE), - intent.getStringExtra(FaceclawAlarms.EXTRA_TEXT), - intent.getStringExtra(FaceclawAlarms.EXTRA_KIND), - intent.getIntExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, 10) - )); - break; - case ACTION_DELIVERED: - markDelivered(id); - break; - case ACTION_DISMISS: - dismiss(id); - break; - case ACTION_SNOOZE: - snooze(id); - break; - case ACTION_STOP_ITEM: - stopItemQuietly(id, "acknowledged"); - break; - default: - break; - } - finishIfIdle(); - return START_NOT_STICKY; - } - - @Override - public void onDestroy() { - handler.removeCallbacksAndMessages(null); - stopSound(); - releaseWakeLock(); - super.onDestroy(); - } - - // ------------------------------------------------------------------ - // Ringing - - private void startRinging(Ringing item) { - if (item.id == 0) { - return; - } - ensureChannel(); - Ringing existing = ringing.get(item.id); - if (existing != null) { - // The engine's JS timeout and the AlarmManager alarm both fired: one ring. - showNotification(existing); - return; - } - if (takeEarlySignal(stoppedEarly, item.id)) { - // Acknowledged or cancelled before this intent was processed. - FaceclawAlarms.log(this, "ring " + item.id + " already acknowledged"); - return; - } - ringing.put(item.id, item); - FaceclawAlarms.log(this, "ring " + item.kind + " " + item.id + " (" + FaceclawAlarms.glassesStatusDescription() + ")"); - acquireWakeLock(); - showNotification(item); - if (!FaceclawAlarms.glassesCanCarryAlarm()) { - escalate(item, FaceclawAlarms.glassesStatusDescription()); - return; - } - if (takeEarlySignal(deliveredEarly, item.id)) { - markDelivered(item.id); - return; - } - // The glasses could carry it: give the JS side a moment to confirm - // they are actually showing it, then hold for the acknowledgement. - handler.postDelayed(() -> { - Ringing current = ringing.get(item.id); - if (current != null && !current.escalated && current.deliveredAtMs == 0) { - escalate(current, "not delivered to the glasses"); - } - }, DELIVERY_WAIT_MS); - } - - /** Consume an early signal for the id; stale entries are dropped. */ - private static boolean takeEarlySignal(Map signals, long id) { - Long at = signals.remove(id); - long now = System.currentTimeMillis(); - synchronized (signals) { - signals.values().removeIf(stamp -> now - stamp > EARLY_SIGNAL_MAX_AGE_MS); - } - return at != null && now - at <= EARLY_SIGNAL_MAX_AGE_MS; - } - - private void markDelivered(long id) { - Ringing item = ringing.get(id); - if (item == null || item.deliveredAtMs != 0) { - return; - } - item.deliveredAtMs = System.currentTimeMillis(); - FaceclawAlarms.log(this, "delivered " + id + " to glasses"); - handler.postDelayed(() -> { - Ringing current = ringing.get(id); - if (current != null && !current.escalated) { - escalate(current, "not acknowledged on the glasses"); - } - }, ACK_WAIT_MS); - } - - private void escalate(Ringing item, String reason) { - if (item.escalated) { - return; - } - item.escalated = true; - FaceclawAlarms.log(this, "phone sound for " + item.id + ": " + reason); - showNotification(item); - startSound(); - handler.postDelayed(() -> { - Ringing current = ringing.get(item.id); - if (current != null && current.escalated && !current.silenced) { - current.silenced = true; - FaceclawAlarms.log(this, "auto-silenced " + current.id); - showNotification(current); - syncSound(); - } - }, AUTO_SILENCE_MS); - } - - private void dismiss(long id) { - Ringing item = ringing.get(id); - if (item == null) { - return; - } - FaceclawAlarms.recordPhoneAction(this, id, "dismiss", 0); - removeItem(item); - // Also drop the schedule entry (a one-off is over; the engine re-arms repeats). - FaceclawAlarms.cancel(this, id); - } - - private void snooze(long id) { - Ringing item = ringing.get(id); - if (item == null) { - return; - } - int minutes = item.snoozeMinutes; - FaceclawAlarms.recordPhoneAction(this, id, "snooze", minutes); - removeItem(item); - // Re-arm on the phone directly so the snooze holds even with the JS - // side gone; the engine replays the journal and lands on the same id. - FaceclawAlarms.schedule( - this, - id, - System.currentTimeMillis() + minutes * 60_000L, - item.title, - item.text, - item.kind, - minutes - ); - } - - private void stopItemQuietly(long id, String reason) { - Ringing item = ringing.get(id); - if (item == null) { - return; - } - FaceclawAlarms.log(this, reason + " " + id); - removeItem(item); - } - - private void removeItem(Ringing item) { - ringing.remove(item.id); - NotificationManager manager = getSystemService(NotificationManager.class); - if (manager != null) { - manager.cancel(notificationId(item.id)); - } - syncSound(); - if (item.id == foregroundId) { - promoteAnotherToForeground(); - } - } - - private void finishIfIdle() { - if (!ringing.isEmpty()) { - return; - } - handler.removeCallbacksAndMessages(null); - stopSound(); - releaseWakeLock(); - stopForeground(STOP_FOREGROUND_REMOVE); - stopSelf(); - } - - // ------------------------------------------------------------------ - // Notification (the phone's silent display of the alarm) - - private void showNotification(Ringing item) { - Notification notification = buildNotification(item); - if (foregroundId == 0 || foregroundId == item.id || !ringing.containsKey(foregroundId)) { - foregroundId = item.id; - startForegroundCompat(notificationId(item.id), notification); - } else { - NotificationManager manager = getSystemService(NotificationManager.class); - if (manager != null) { - manager.notify(notificationId(item.id), notification); - } - } - } - - private void promoteAnotherToForeground() { - List items = snapshot(); - if (items.isEmpty()) { - foregroundId = 0; - return; - } - Ringing next = items.get(0); - foregroundId = next.id; - startForegroundCompat(notificationId(next.id), buildNotification(next)); - } - - private void startForegroundCompat(int id, Notification notification) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - startForeground(id, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK); - } else { - startForeground(id, notification); - } - } - - private Notification buildNotification(Ringing item) { - Intent activity = new Intent(this, FaceclawAlarmActivity.class) - .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP) - .putExtra(FaceclawAlarms.EXTRA_ID, item.id); - int flags = PendingIntent.FLAG_UPDATE_CURRENT; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags |= PendingIntent.FLAG_IMMUTABLE; - } - PendingIntent open = PendingIntent.getActivity(this, FaceclawAlarms.requestCode(item.id), activity, flags); - - String status; - if (item.silenced) { - status = "Not answered"; - } else if (item.escalated) { - status = "Ringing"; - } else { - status = "Ringing on the glasses"; - } - String text = item.text.isEmpty() ? status : item.text + " · " + status; - - Notification.Builder builder = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O - ? new Notification.Builder(this, CHANNEL_ID) - : new Notification.Builder(this).setPriority(Notification.PRIORITY_MAX); - builder.setContentTitle(item.title) - .setContentText(text) - .setSmallIcon(getApplicationInfo().icon) - .setContentIntent(open) - .setFullScreenIntent(open, true) - .setCategory(Notification.CATEGORY_ALARM) - .setVisibility(Notification.VISIBILITY_PUBLIC) - .setOngoing(true) - .setAutoCancel(false) - .setOnlyAlertOnce(true) - .setWhen(item.ringAtMs) - .setShowWhen(true) - .addAction(new Notification.Action.Builder( - null, - FaceclawAlarms.KIND_TIMER.equals(item.kind) ? "+" + item.snoozeMinutes + " min" : "Snooze " + item.snoozeMinutes + " min", - actionIntent(this, ACTION_SNOOZE, item.id)).build()) - .addAction(new Notification.Action.Builder( - null, - "Dismiss", - actionIntent(this, ACTION_DISMISS, item.id)).build()); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - // Pre-channel: the builder controls sound, and we want none. - builder.setSound(null).setVibrate(null); - } - return builder.build(); - } - - private void ensureChannel() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - return; - } - NotificationManager manager = getSystemService(NotificationManager.class); - if (manager == null) { - return; - } - NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Alarms", NotificationManager.IMPORTANCE_HIGH); - channel.setDescription("Timers and alarms going off. Silent here: the sound is played by the alarm itself."); - // The service plays the sound itself (on the alarm stream) so the - // notification stays silent and vibration-free on purpose. - channel.setSound(null, null); - channel.enableVibration(false); - channel.setBypassDnd(true); - channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); - manager.createNotificationChannel(channel); - } - - private static int notificationId(long id) { - return 0x41000000 | (FaceclawAlarms.requestCode(id) & 0x00ffffff); - } - - // ------------------------------------------------------------------ - // Sound and vibration (only while some item is escalated and not yet silenced) - - private void syncSound() { - boolean wanted = false; - for (Ringing item : snapshot()) { - if (item.escalated && !item.silenced) { - wanted = true; - break; - } - } - if (wanted) { - startSound(); - } else { - stopSound(); - } - } - - private void startSound() { - if (player != null) { - return; - } - audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); - AudioAttributes attributes = new AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_ALARM) - .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) - .build(); - raiseMutedAlarmVolume(); - requestAudioFocus(attributes); - Uri tone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); - if (tone == null) { - tone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); - } - try { - MediaPlayer created = new MediaPlayer(); - created.setAudioAttributes(attributes); - created.setDataSource(this, tone); - created.setLooping(true); - created.prepare(); - created.start(); - player = created; - } catch (Exception error) { - FaceclawAlarms.log(this, "alarm sound failed: " + error.getMessage()); - player = null; - } - startVibration(); - } - - private void stopSound() { - if (player != null) { - try { - player.stop(); - } catch (RuntimeException ignored) { - // Already stopped. - } - player.release(); - player = null; - } - if (vibrator != null) { - vibrator.cancel(); - vibrator = null; - } - abandonAudioFocus(); - restoreAlarmVolume(); - } - - /** - * A muted alarm stream is the one thing that would keep an alarm clock - * silent; raise it for the duration of the ring and put it back after. - */ - private void raiseMutedAlarmVolume() { - if (audioManager == null) { - return; - } - try { - int current = audioManager.getStreamVolume(AudioManager.STREAM_ALARM); - int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM); - if (current == 0 && max > 0) { - restoreAlarmVolume = current; - audioManager.setStreamVolume(AudioManager.STREAM_ALARM, Math.max(1, Math.round(max * MUTED_VOLUME_FRACTION)), 0); - } - } catch (RuntimeException error) { - FaceclawAlarms.log(this, "alarm volume change failed: " + error.getMessage()); - } - } - - private void restoreAlarmVolume() { - if (audioManager == null || restoreAlarmVolume < 0) { - return; - } - try { - audioManager.setStreamVolume(AudioManager.STREAM_ALARM, restoreAlarmVolume, 0); - } catch (RuntimeException ignored) { - // Best effort. - } - restoreAlarmVolume = -1; - } - - private void requestAudioFocus(AudioAttributes attributes) { - if (audioManager == null) { - return; - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) - .setAudioAttributes(attributes) - .build(); - audioManager.requestAudioFocus(focusRequest); - } else { - audioManager.requestAudioFocus(null, AudioManager.STREAM_ALARM, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); - } - } - - private void abandonAudioFocus() { - if (audioManager == null) { - return; - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - if (focusRequest != null) { - audioManager.abandonAudioFocusRequest(focusRequest); - focusRequest = null; - } - } else { - audioManager.abandonAudioFocus(null); - } - } - - private void startVibration() { - Vibrator device = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); - if (device == null || !device.hasVibrator()) { - return; - } - vibrator = device; - long[] pattern = {0, 600, 400, 600, 1200}; - AudioAttributes attributes = new AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_ALARM) - .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) - .build(); - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - device.vibrate(VibrationEffect.createWaveform(pattern, 0), attributes); - } else { - device.vibrate(pattern, 0, attributes); - } - } catch (RuntimeException error) { - FaceclawAlarms.log(this, "vibration failed: " + error.getMessage()); - } - } - - // ------------------------------------------------------------------ - // Wake lock - - private void acquireWakeLock() { - if (wakeLock != null && wakeLock.isHeld()) { - return; - } - PowerManager power = (PowerManager) getSystemService(Context.POWER_SERVICE); - if (power == null) { - return; - } - wakeLock = power.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "faceclaw:alarm"); - // Bounded: sound auto-silences at AUTO_SILENCE_MS, and a stuck lock - // past that would only drain the battery. - wakeLock.acquire(AUTO_SILENCE_MS + ACK_WAIT_MS + DELIVERY_WAIT_MS + 10_000L); - } - - private void releaseWakeLock() { - if (wakeLock != null && wakeLock.isHeld()) { - wakeLock.release(); - } - wakeLock = null; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.kt new file mode 100644 index 00000000..3bddef2b --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarmService.kt @@ -0,0 +1,532 @@ +package com.faceclaw.app + +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Context +import android.content.Intent +import android.content.pm.ServiceInfo +import android.media.AudioAttributes +import android.media.AudioFocusRequest +import android.media.AudioManager +import android.media.MediaPlayer +import android.media.RingtoneManager +import android.net.Uri +import android.os.Build +import android.os.Handler +import android.os.IBinder +import android.os.Looper +import android.os.PowerManager +import android.os.VibrationEffect +import android.os.Vibrator + + +/** + * The ringing foreground service. One instance carries every item currently + * ringing. Each item starts silent on the phone (a heads-up / lock-screen + * notification only, the glasses do the ringing) and escalates to phone + * sound and vibration when the glasses cannot carry it: not connected, not + * on a head, charging, no delivery confirmation from the JS side within a + * few seconds, or no acknowledgement within 30 seconds of delivery. Sound + * uses the alarm stream, so media volume, the ringer switch and (unless set + * to total silence) Do Not Disturb do not silence it. + */ +class FaceclawAlarmService : Service() { + /** The shared item model; kept as a nested subclass so the lock-screen activity's references hold. */ + internal class Ringing( + id: Long, + title: String?, + text: String?, + kind: String?, + snoozeMinutes: Int + ) : RingingItem(id, title, text, kind, snoozeMinutes, System.currentTimeMillis()) + + companion object { + const val CHANNEL_ID = "faceclaw-alarms" + internal const val ACTION_RING = "com.faceclaw.app.action.ALARM_RING" + internal const val ACTION_DELIVERED = "com.faceclaw.app.action.ALARM_DELIVERED" + internal const val ACTION_DISMISS = "com.faceclaw.app.action.ALARM_DISMISS" + internal const val ACTION_SNOOZE = "com.faceclaw.app.action.ALARM_SNOOZE" + /** Stop an item quietly (acknowledged on the glasses / cancelled by the engine). */ + internal const val ACTION_STOP_ITEM = "com.faceclaw.app.action.ALARM_STOP_ITEM" + + /** How long to wait for the JS side to confirm the glasses are showing the item. */ + internal const val DELIVERY_WAIT_MS = 5_000L + /** How long after delivery to the glasses before the phone joins in. */ + internal const val ACK_WAIT_MS = 30_000L + /** Sound and vibration stop after this; the notification stays. */ + internal const val AUTO_SILENCE_MS = 10 * 60_000L + /** Alarm-stream level (fraction of max) used when the wearer has it muted. */ + internal const val MUTED_VOLUME_FRACTION = 0.6f + + /** + * The escalation policy is process-wide (the activity reads it, the static entry points + * consult it before the service exists); the live service attaches its timers/effects. + */ + private val policy = AlarmRingingPolicy(FaceclawAlarms.glassesGate) + + // ------------------------------------------------------------------ + // Static entry points + + @JvmStatic + fun ring(context: Context, id: Long, title: String?, text: String?, kind: String?, snoozeMinutes: Int) { + val appContext = context.applicationContext + val intent = Intent(appContext, FaceclawAlarmService::class.java) + .setAction(ACTION_RING) + .putExtra(FaceclawAlarms.EXTRA_ID, id) + .putExtra(FaceclawAlarms.EXTRA_TITLE, title) + .putExtra(FaceclawAlarms.EXTRA_TEXT, text) + .putExtra(FaceclawAlarms.EXTRA_KIND, kind) + .putExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, snoozeMinutes) + FaceclawAlarms.startServiceCompat(appContext, intent) + } + + @JvmStatic + fun delivered(context: Context, id: Long) { + if (!policy.deliveredOrDefer(id)) { + return + } + val appContext = context.applicationContext + appContext.startService(Intent(appContext, FaceclawAlarmService::class.java) + .setAction(ACTION_DELIVERED) + .putExtra(FaceclawAlarms.EXTRA_ID, id)) + } + + @JvmStatic + fun stopItem(context: Context, id: Long) { + if (!policy.stopOrDefer(id)) { + return + } + val appContext = context.applicationContext + appContext.startService(Intent(appContext, FaceclawAlarmService::class.java) + .setAction(ACTION_STOP_ITEM) + .putExtra(FaceclawAlarms.EXTRA_ID, id)) + } + + @JvmStatic + fun isRinging(id: Long): Boolean = policy.isRinging(id) + + @JvmStatic + fun isAnythingRinging(): Boolean = policy.isAnythingRinging() + + @JvmStatic + internal fun snapshot(): List = policy.snapshot().map { it as Ringing } + + @JvmStatic + internal fun actionIntent(appContext: Context, action: String, id: Long): PendingIntent { + val intent = Intent(appContext, FaceclawAlarmService::class.java) + .setAction(action) + .putExtra(FaceclawAlarms.EXTRA_ID, id) + var flags = PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or PendingIntent.FLAG_IMMUTABLE + } + val code = FaceclawAlarms.requestCode(id) xor action.hashCode() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + return PendingIntent.getForegroundService(appContext, code, intent, flags) + } + return PendingIntent.getService(appContext, code, intent, flags) + } + + private fun notificationId(id: Long): Int { + return 0x41000000 or (FaceclawAlarms.requestCode(id) and 0x00ffffff) + } + } + + private val handler = Handler(Looper.getMainLooper()) + private var player: MediaPlayer? = null + private var vibrator: Vibrator? = null + private var wakeLock: PowerManager.WakeLock? = null + private var audioManager: AudioManager? = null + private var focusRequest: AudioFocusRequest? = null + private var restoreAlarmVolume = -1 + private var foregroundId: Long = 0 + + // ------------------------------------------------------------------ + // Service lifecycle + + override fun onBind(intent: Intent?): IBinder? { + return null + } + + override fun onCreate() { + super.onCreate() + policy.attach(timers, effects) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + val action = intent?.action + val id = if (intent == null) 0L else intent.getLongExtra(FaceclawAlarms.EXTRA_ID, 0) + if (action == null) { + // Restarted by the system with nothing to do. + finishIfIdle() + return START_NOT_STICKY + } + when (action) { + ACTION_RING -> + startRinging(Ringing( + id, + intent!!.getStringExtra(FaceclawAlarms.EXTRA_TITLE), + intent.getStringExtra(FaceclawAlarms.EXTRA_TEXT), + intent.getStringExtra(FaceclawAlarms.EXTRA_KIND), + intent.getIntExtra(FaceclawAlarms.EXTRA_SNOOZE_MINUTES, 10) + )) + ACTION_DELIVERED -> + policy.markDelivered(id) + ACTION_DISMISS -> + policy.dismiss(id) + ACTION_SNOOZE -> + policy.snooze(id) + ACTION_STOP_ITEM -> + policy.stopQuietly(id, "acknowledged") + else -> {} + } + finishIfIdle() + return START_NOT_STICKY + } + + override fun onDestroy() { + policy.detach() + handler.removeCallbacksAndMessages(null) + stopSound() + releaseWakeLock() + super.onDestroy() + } + + // ------------------------------------------------------------------ + // Ringing + + private fun startRinging(item: Ringing) { + if (item.id == 0L) { + return + } + ensureChannel() + policy.ring(item) + } + + /** Escalation timers on the service's main-thread Handler. */ + private val timers = object : AlarmScheduler { + private val pending = HashMap() + + override fun postDelayed(token: Any, delayMs: Long, action: () -> Unit) { + pending.remove(token)?.let { handler.removeCallbacks(it) } + val runnable = Runnable { + pending.remove(token) + action() + } + pending[token] = runnable + handler.postDelayed(runnable, delayMs) + } + + override fun cancel(token: Any) { + pending.remove(token)?.let { handler.removeCallbacks(it) } + } + + override fun cancelAll() { + pending.clear() + handler.removeCallbacksAndMessages(null) + } + } + + /** The phone-side effects of the shared policy: notifications, sound, schedule, journal. */ + private val effects = object : RingingSink { + override fun showNotification(item: RingingItem) = this@FaceclawAlarmService.showNotification(item as Ringing) + + override fun itemRemoved(item: RingingItem) { + val manager = getSystemService(NotificationManager::class.java) + manager?.cancel(notificationId(item.id)) + if (item.id == foregroundId) { + promoteAnotherToForeground() + } + } + + override fun ringingStarted() = acquireWakeLock() + + override fun startSound() = this@FaceclawAlarmService.startSound() + + override fun stopSound() = this@FaceclawAlarmService.stopSound() + + override fun reschedule(item: RingingItem, atMs: Long, minutes: Int) { + // The engine replays the journal and lands on the same id. + FaceclawAlarms.schedule(this@FaceclawAlarmService, item.id, atMs, item.title, item.text, item.kind, minutes) + } + + override fun cancelSchedule(id: Long) = FaceclawAlarms.cancel(this@FaceclawAlarmService, id) + + override fun recordPhoneAction(id: Long, action: String, minutes: Int) = + FaceclawAlarms.recordPhoneAction(this@FaceclawAlarmService, id, action, minutes) + + override fun log(line: String) = FaceclawAlarms.log(this@FaceclawAlarmService, line) + } + + private fun finishIfIdle() { + if (policy.isAnythingRinging()) { + return + } + policy.cancelTimers() + stopSound() + releaseWakeLock() + stopForeground(STOP_FOREGROUND_REMOVE) + stopSelf() + } + + // ------------------------------------------------------------------ + // Notification (the phone's silent display of the alarm) + + private fun showNotification(item: Ringing) { + val notification = buildNotification(item) + if (foregroundId == 0L || foregroundId == item.id || !policy.isRinging(foregroundId)) { + foregroundId = item.id + startForegroundCompat(notificationId(item.id), notification) + } else { + val manager = getSystemService(NotificationManager::class.java) + manager?.notify(notificationId(item.id), notification) + } + } + + private fun promoteAnotherToForeground() { + val items = snapshot() + if (items.isEmpty()) { + foregroundId = 0 + return + } + val next = items[0] + foregroundId = next.id + startForegroundCompat(notificationId(next.id), buildNotification(next)) + } + + private fun startForegroundCompat(id: Int, notification: Notification) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(id, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK) + } else { + startForeground(id, notification) + } + } + + private fun buildNotification(item: Ringing): Notification { + val activity = Intent(this, FaceclawAlarmActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP) + .putExtra(FaceclawAlarms.EXTRA_ID, item.id) + var flags = PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or PendingIntent.FLAG_IMMUTABLE + } + val open = PendingIntent.getActivity(this, FaceclawAlarms.requestCode(item.id), activity, flags) + + val status: String = if (item.silenced) { + "Not answered" + } else if (item.escalated) { + "Ringing" + } else { + "Ringing on the glasses" + } + val text = if (item.text.isEmpty()) status else item.text + " · " + status + + val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + Notification.Builder(this, CHANNEL_ID) + else + Notification.Builder(this).setPriority(Notification.PRIORITY_MAX) + builder.setContentTitle(item.title) + .setContentText(text) + .setSmallIcon(applicationInfo.icon) + .setContentIntent(open) + .setFullScreenIntent(open, true) + .setCategory(Notification.CATEGORY_ALARM) + .setVisibility(Notification.VISIBILITY_PUBLIC) + .setOngoing(true) + .setAutoCancel(false) + .setOnlyAlertOnce(true) + .setWhen(item.ringAtMs) + .setShowWhen(true) + .addAction(Notification.Action.Builder( + null, + if (FaceclawAlarms.KIND_TIMER == item.kind) "+" + item.snoozeMinutes + " min" else "Snooze " + item.snoozeMinutes + " min", + actionIntent(this, ACTION_SNOOZE, item.id)).build()) + .addAction(Notification.Action.Builder( + null, + "Dismiss", + actionIntent(this, ACTION_DISMISS, item.id)).build()) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + // Pre-channel: the builder controls sound, and we want none. + builder.setSound(null).setVibrate(null) + } + return builder.build() + } + + private fun ensureChannel() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + return + } + val manager = getSystemService(NotificationManager::class.java) ?: return + val channel = NotificationChannel(CHANNEL_ID, "Alarms", NotificationManager.IMPORTANCE_HIGH) + channel.description = "Timers and alarms going off. Silent here: the sound is played by the alarm itself." + // The service plays the sound itself (on the alarm stream) so the + // notification stays silent and vibration-free on purpose. + channel.setSound(null, null) + channel.enableVibration(false) + channel.setBypassDnd(true) + channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC + manager.createNotificationChannel(channel) + } + + // ------------------------------------------------------------------ + // Sound and vibration (only while some item is escalated and not yet silenced) + + private fun startSound() { + if (player != null) { + return + } + audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager? + val attributes = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_ALARM) + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .build() + raiseMutedAlarmVolume() + requestAudioFocus(attributes) + var tone: Uri? = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM) + if (tone == null) { + tone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE) + } + try { + val created = MediaPlayer() + created.setAudioAttributes(attributes) + created.setDataSource(this, tone!!) + created.isLooping = true + created.prepare() + created.start() + player = created + } catch (error: Exception) { + FaceclawAlarms.log(this, "alarm sound failed: " + error.message) + player = null + } + startVibration() + } + + private fun stopSound() { + val current = player + if (current != null) { + try { + current.stop() + } catch (ignored: RuntimeException) { + // Already stopped. + } + current.release() + player = null + } + val device = vibrator + if (device != null) { + device.cancel() + vibrator = null + } + abandonAudioFocus() + restoreAlarmVolume() + } + + /** + * A muted alarm stream is the one thing that would keep an alarm clock + * silent; raise it for the duration of the ring and put it back after. + */ + private fun raiseMutedAlarmVolume() { + val manager = audioManager ?: return + try { + val current = manager.getStreamVolume(AudioManager.STREAM_ALARM) + val max = manager.getStreamMaxVolume(AudioManager.STREAM_ALARM) + if (current == 0 && max > 0) { + restoreAlarmVolume = current + manager.setStreamVolume(AudioManager.STREAM_ALARM, Math.max(1, Math.round(max * MUTED_VOLUME_FRACTION)), 0) + } + } catch (error: RuntimeException) { + FaceclawAlarms.log(this, "alarm volume change failed: " + error.message) + } + } + + private fun restoreAlarmVolume() { + val manager = audioManager + if (manager == null || restoreAlarmVolume < 0) { + return + } + try { + manager.setStreamVolume(AudioManager.STREAM_ALARM, restoreAlarmVolume, 0) + } catch (ignored: RuntimeException) { + // Best effort. + } + restoreAlarmVolume = -1 + } + + private fun requestAudioFocus(attributes: AudioAttributes) { + val manager = audioManager ?: return + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val request = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) + .setAudioAttributes(attributes) + .build() + focusRequest = request + manager.requestAudioFocus(request) + } else { + @Suppress("DEPRECATION") + manager.requestAudioFocus(null, AudioManager.STREAM_ALARM, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) + } + } + + private fun abandonAudioFocus() { + val manager = audioManager ?: return + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val request = focusRequest + if (request != null) { + manager.abandonAudioFocusRequest(request) + focusRequest = null + } + } else { + @Suppress("DEPRECATION") + manager.abandonAudioFocus(null) + } + } + + private fun startVibration() { + val device = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator? + if (device == null || !device.hasVibrator()) { + return + } + vibrator = device + val pattern = longArrayOf(0, 600, 400, 600, 1200) + val attributes = AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_ALARM) + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .build() + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + device.vibrate(VibrationEffect.createWaveform(pattern, 0), attributes) + } else { + @Suppress("DEPRECATION") + device.vibrate(pattern, 0, attributes) + } + } catch (error: RuntimeException) { + FaceclawAlarms.log(this, "vibration failed: " + error.message) + } + } + + // ------------------------------------------------------------------ + // Wake lock + + private fun acquireWakeLock() { + val held = wakeLock + if (held != null && held.isHeld) { + return + } + val power = getSystemService(Context.POWER_SERVICE) as PowerManager? ?: return + val lock = power.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "faceclaw:alarm") + wakeLock = lock + // Bounded: sound auto-silences at AUTO_SILENCE_MS, and a stuck lock + // past that would only drain the battery. + lock.acquire(AUTO_SILENCE_MS + ACK_WAIT_MS + DELIVERY_WAIT_MS + 10_000L) + } + + private fun releaseWakeLock() { + val lock = wakeLock + if (lock != null && lock.isHeld) { + lock.release() + } + wakeLock = null + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.java deleted file mode 100644 index 3dd2b0f1..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.java +++ /dev/null @@ -1,620 +0,0 @@ -package com.faceclaw.app; - -import android.app.ActivityManager; -import android.app.AlarmManager; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.media.AudioManager; -import android.net.Uri; -import android.os.Build; -import android.os.Handler; -import android.os.Looper; -import android.os.PowerManager; -import android.provider.Settings; - -import androidx.core.content.ContextCompat; - -import com.tns.NativeScriptActivity; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.Locale; - -/** - * The phone side of the Timers app's alarms and countdown timers, built to - * ring without the JavaScript side: scheduling through AlarmManager's alarm - * clock API, a persisted schedule (in device-protected storage) that the - * reschedule receiver replays after reboots and time changes, the hand-off - * to the ringing foreground service, and a journal of what the wearer did - * on the phone (dismiss / snooze) for the JS engine to replay when it is - * next awake. Also the reliability self-check the UI surfaces. - * - * Ids are the JS engine's item ids (a millisecond epoch times 100 plus a - * serial), shared between timers and alarms; `kind` tells the two apart for - * wording only. - */ -public final class FaceclawAlarms { - public static final String ACTION_EXPIRE = "com.faceclaw.app.action.ALARM_EXPIRE"; - static final String EXTRA_ID = "id"; - static final String EXTRA_TITLE = "title"; - static final String EXTRA_TEXT = "text"; - static final String EXTRA_KIND = "kind"; - static final String EXTRA_SNOOZE_MINUTES = "snoozeMinutes"; - - public static final String KIND_TIMER = "timer"; - public static final String KIND_ALARM = "alarm"; - - private static final String PREFS_NAME = "faceclaw-alarms"; - private static final String KEY_SCHEDULED = "scheduled"; - private static final String KEY_JOURNAL = "journal"; - private static final String KEY_LOG = "log"; - private static final int LOG_LINES = 60; - - /** - * A schedule entry found already past due (after a reboot, or when the - * process was asleep) rings if it is at most this late; older ones are - * dropped as missed. Matches the JS engine's grace window. - */ - static final long LATE_GRACE_MS = 5 * 60_000L; - /** A glasses status report older than this is not trusted (the JS side is probably gone). */ - static final long GLASSES_STATUS_MAX_AGE_MS = 3 * 60_000L; - - private static volatile boolean glassesConnected; - private static volatile boolean glassesWorn; - private static volatile boolean glassesCharging; - private static volatile long glassesStatusAtMs; - - private static volatile FaceclawAlarmListener listener; - private static final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private FaceclawAlarms() {} - - // ------------------------------------------------------------------ - // Storage - - /** - * Device-protected storage, so the schedule is readable before the first - * unlock after a reboot (the reschedule receiver may run then on some - * devices) and survives credential-encrypted storage being unavailable. - */ - static SharedPreferences prefs(Context context) { - Context base = context.getApplicationContext(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - base = base.createDeviceProtectedStorageContext(); - } - return base.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); - } - - private static synchronized JSONArray readArray(Context context, String key) { - String raw = prefs(context).getString(key, "[]"); - try { - return new JSONArray(raw); - } catch (JSONException error) { - return new JSONArray(); - } - } - - private static synchronized void writeArray(Context context, String key, JSONArray array) { - prefs(context).edit().putString(key, array.toString()).apply(); - } - - static JSONObject findScheduled(Context context, long id) { - JSONArray scheduled = readArray(context, KEY_SCHEDULED); - for (int index = 0; index < scheduled.length(); index++) { - JSONObject entry = scheduled.optJSONObject(index); - if (entry != null && entry.optLong("id") == id) { - return entry; - } - } - return null; - } - - private static synchronized void putScheduled(Context context, JSONObject entry) { - JSONArray scheduled = readArray(context, KEY_SCHEDULED); - JSONArray next = new JSONArray(); - long id = entry.optLong("id"); - for (int index = 0; index < scheduled.length(); index++) { - JSONObject existing = scheduled.optJSONObject(index); - if (existing != null && existing.optLong("id") != id) { - next.put(existing); - } - } - next.put(entry); - writeArray(context, KEY_SCHEDULED, next); - } - - private static synchronized void removeScheduled(Context context, long id) { - JSONArray scheduled = readArray(context, KEY_SCHEDULED); - JSONArray next = new JSONArray(); - for (int index = 0; index < scheduled.length(); index++) { - JSONObject existing = scheduled.optJSONObject(index); - if (existing != null && existing.optLong("id") != id) { - next.put(existing); - } - } - writeArray(context, KEY_SCHEDULED, next); - } - - // ------------------------------------------------------------------ - // Scheduling - - /** - * Schedule (or move) the phone alarm for an item. Idempotent: the same id - * always maps to the same PendingIntent, so a reschedule replaces rather - * than duplicates. setAlarmClock is exact, fires through Doze and battery - * savers, and shows the system's alarm indicator; the fallbacks only run - * where it is unavailable. - */ - public static void schedule( - Context context, - long id, - long triggerAtMs, - String title, - String text, - String kind, - int snoozeMinutes - ) { - Context appContext = context.getApplicationContext(); - JSONObject entry = new JSONObject(); - try { - entry.put("id", id); - entry.put("at", triggerAtMs); - entry.put("title", title == null ? "" : title); - entry.put("text", text == null ? "" : text); - entry.put("kind", kind == null ? KIND_ALARM : kind); - entry.put("snoozeMinutes", snoozeMinutes); - } catch (JSONException ignored) { - return; - } - putScheduled(appContext, entry); - armAlarm(appContext, entry); - } - - private static void armAlarm(Context appContext, JSONObject entry) { - AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE); - if (manager == null) { - return; - } - long id = entry.optLong("id"); - long triggerAt = Math.max(System.currentTimeMillis(), entry.optLong("at")); - PendingIntent fire = expiryPendingIntent(appContext, entry); - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(triggerAt, showIntent(appContext, id)); - manager.setAlarmClock(info, fire); - } else { - manager.setExact(AlarmManager.RTC_WAKEUP, triggerAt, fire); - } - } catch (SecurityException error) { - // Exact-alarm access revoked (Android 12/13 without USE_EXACT_ALARM - // honoured): the inexact alarm is still a wake-up, just a late one. - log(appContext, "exact alarm refused for " + id + ": " + error.getMessage()); - manager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt, fire); - } - } - - /** Cancel the phone alarm for an item, stop it if ringing, and forget it. */ - public static void cancel(Context context, long id) { - Context appContext = context.getApplicationContext(); - AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE); - if (manager != null) { - JSONObject entry = new JSONObject(); - try { - entry.put("id", id); - } catch (JSONException ignored) { - // unreachable for a numeric put - } - manager.cancel(expiryPendingIntent(appContext, entry)); - } - removeScheduled(appContext, id); - FaceclawAlarmService.stopItem(appContext, id); - } - - /** - * Replay the persisted schedule into AlarmManager: after a reboot (alarms - * are wiped), a package update, or a wall-clock / time-zone change. Entries - * already due ring now if inside the grace window, else are dropped. - */ - public static void rescheduleAll(Context context, String reason) { - Context appContext = context.getApplicationContext(); - JSONArray scheduled = readArray(appContext, KEY_SCHEDULED); - long now = System.currentTimeMillis(); - int armed = 0; - for (int index = 0; index < scheduled.length(); index++) { - JSONObject entry = scheduled.optJSONObject(index); - if (entry == null) { - continue; - } - long at = entry.optLong("at"); - if (at <= now) { - if (now - at <= LATE_GRACE_MS) { - ringEntry(appContext, entry); - } else { - log(appContext, "missed " + entry.optString("kind") + " " + entry.optLong("id") + " (" + reason + ")"); - removeScheduled(appContext, entry.optLong("id")); - } - continue; - } - armAlarm(appContext, entry); - armed++; - } - log(appContext, "rescheduled " + armed + " (" + reason + ")"); - } - - /** - * The schedule and AlarmManager can disagree (a force-stop clears alarms - * but not the file, an OEM killer drops them). True when the soonest - * stored entry is not what the system reports as the next alarm clock. - */ - public static boolean scheduleLooksStale(Context context) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - return false; - } - Context appContext = context.getApplicationContext(); - JSONArray scheduled = readArray(appContext, KEY_SCHEDULED); - long now = System.currentTimeMillis(); - long soonest = Long.MAX_VALUE; - for (int index = 0; index < scheduled.length(); index++) { - JSONObject entry = scheduled.optJSONObject(index); - if (entry != null && entry.optLong("at") > now) { - soonest = Math.min(soonest, entry.optLong("at")); - } - } - if (soonest == Long.MAX_VALUE) { - return false; - } - AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE); - if (manager == null) { - return false; - } - AlarmManager.AlarmClockInfo next = manager.getNextAlarmClock(); - // Another app's alarm may legitimately be sooner; only a later (or - // absent) system alarm proves ours is missing. - return next == null || next.getTriggerTime() > soonest + 1000; - } - - // ------------------------------------------------------------------ - // Ringing - - /** An item came due: hand it to the ringing service (idempotent per id). */ - static void ringEntry(Context appContext, JSONObject entry) { - FaceclawAlarmService.ring( - appContext, - entry.optLong("id"), - entry.optString("title"), - entry.optString("text"), - entry.optString("kind", KIND_ALARM), - entry.optInt("snoozeMinutes", 10) - ); - } - - /** The JS engine's own expiry path; the service deduplicates against the alarm's. */ - public static void ring(Context context, long id, String title, String text, String kind, int snoozeMinutes) { - Context appContext = context.getApplicationContext(); - AlarmManager manager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE); - if (manager != null) { - JSONObject probe = new JSONObject(); - try { - probe.put("id", id); - } catch (JSONException ignored) { - // unreachable for a numeric put - } - manager.cancel(expiryPendingIntent(appContext, probe)); - } - FaceclawAlarmService.ring(appContext, id, title, text, kind, snoozeMinutes); - } - - /** The glasses are showing the ringing item; the acknowledgement clock starts now. */ - public static void deliveredToGlasses(Context context, long id) { - FaceclawAlarmService.delivered(context.getApplicationContext(), id); - } - - /** The wearer dismissed or snoozed it on the glasses: the phone side goes quiet. */ - public static void acknowledge(Context context, long id) { - Context appContext = context.getApplicationContext(); - removeScheduled(appContext, id); - FaceclawAlarmService.stopItem(appContext, id); - } - - // ------------------------------------------------------------------ - // Glasses status (pushed by the JS side; in-memory, so a dead process reads as "no glasses") - - public static void setGlassesStatus(boolean connected, boolean worn, boolean charging) { - glassesConnected = connected; - glassesWorn = worn; - glassesCharging = charging; - glassesStatusAtMs = System.currentTimeMillis(); - } - - /** True only when a fresh report says the glasses are connected, on a head, and not charging. */ - static boolean glassesCanCarryAlarm() { - long age = System.currentTimeMillis() - glassesStatusAtMs; - if (glassesStatusAtMs == 0 || age > GLASSES_STATUS_MAX_AGE_MS) { - return false; - } - return glassesConnected && glassesWorn && !glassesCharging; - } - - static String glassesStatusDescription() { - if (glassesStatusAtMs == 0) { - return "no glasses status"; - } - long age = System.currentTimeMillis() - glassesStatusAtMs; - if (age > GLASSES_STATUS_MAX_AGE_MS) { - return "glasses status stale"; - } - if (!glassesConnected) { - return "glasses not connected"; - } - if (glassesCharging) { - return "glasses charging"; - } - if (!glassesWorn) { - return "glasses not worn"; - } - return "glasses worn"; - } - - // ------------------------------------------------------------------ - // Phone actions -> JS - - public static void setListener(FaceclawAlarmListener newListener) { - listener = newListener; - } - - /** - * Record a dismiss / snooze done on the phone. Delivered live to the JS - * listener when one is registered, and journaled regardless so a JS side - * that was asleep or gone can catch up on its next boot. - */ - static void recordPhoneAction(Context context, long id, String action, int minutes) { - Context appContext = context.getApplicationContext(); - JSONObject event = new JSONObject(); - try { - event.put("id", id); - event.put("action", action); - event.put("minutes", minutes); - event.put("at", System.currentTimeMillis()); - } catch (JSONException ignored) { - return; - } - synchronized (FaceclawAlarms.class) { - JSONArray journal = readArray(appContext, KEY_JOURNAL); - journal.put(event); - writeArray(appContext, KEY_JOURNAL, journal); - } - log(appContext, "phone " + action + " " + id + (minutes > 0 ? " (" + minutes + " min)" : "")); - final FaceclawAlarmListener current = listener; - if (current != null) { - mainHandler.post(() -> { - try { - current.onPhoneAction(id, action, minutes); - } catch (RuntimeException error) { - log(appContext, "listener failed: " + error.getMessage()); - } - }); - } - } - - /** Hand the journal to the JS side (as a JSON array) and clear it. */ - public static synchronized String drainJournal(Context context) { - Context appContext = context.getApplicationContext(); - JSONArray journal = readArray(appContext, KEY_JOURNAL); - writeArray(appContext, KEY_JOURNAL, new JSONArray()); - return journal.toString(); - } - - // ------------------------------------------------------------------ - // Reliability self-check - - /** - * Conditions under which the phone may fail to ring, as a JSON array of - * {code, message, fixable}, most serious first. Empty when everything is - * in order. The UI shows these before the wearer relies on an alarm. - */ - public static String checkReliability(Context context) { - Context appContext = context.getApplicationContext(); - JSONArray issues = new JSONArray(); - NotificationManager notifications = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE); - AlarmManager alarms = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && alarms != null && !alarms.canScheduleExactAlarms()) { - addIssue(issues, "exact-alarm", "Exact alarms are not allowed for Faceclaw, so alarms may ring late.", true); - } - if (notifications != null && !notifications.areNotificationsEnabled()) { - addIssue(issues, "notifications", "Notifications are turned off for Faceclaw, so the phone cannot show or sound an alarm.", true); - } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notifications != null) { - NotificationChannel channel = notifications.getNotificationChannel(FaceclawAlarmService.CHANNEL_ID); - if (channel != null && channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { - addIssue(issues, "alarm-channel", "The Alarms notification category is blocked, so the phone cannot show an alarm.", true); - } - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && notifications != null && !notifications.canUseFullScreenIntent()) { - addIssue(issues, "full-screen", "Full-screen alarms are not allowed, so a ringing alarm will not take over the lock screen.", true); - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && notifications != null) { - int filter = notifications.getCurrentInterruptionFilter(); - if (filter == NotificationManager.INTERRUPTION_FILTER_NONE) { - addIssue(issues, "dnd-total", "Do Not Disturb is set to total silence, which silences alarms too.", true); - } else if (filter == NotificationManager.INTERRUPTION_FILTER_PRIORITY) { - try { - NotificationManager.Policy policy = notifications.getNotificationPolicy(); - if (policy != null && (policy.priorityCategories & NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0) { - addIssue(issues, "dnd-alarms", "Do Not Disturb does not allow alarms, so the phone will stay silent.", true); - } - } catch (SecurityException ignored) { - // No notification-policy access: cannot inspect the exceptions. - } - } - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE); - if (activityManager != null && activityManager.isBackgroundRestricted()) { - addIssue(issues, "background-restricted", "Background usage is restricted for Faceclaw, which blocks alarms while the app is not open.", true); - } - } - PowerManager power = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); - if (power != null && !power.isIgnoringBatteryOptimizations(appContext.getPackageName())) { - addIssue(issues, "battery-optimized", "Battery optimization is on for Faceclaw; some phones delay alarms because of it.", true); - } - AudioManager audio = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE); - if (audio != null && audio.getStreamVolume(AudioManager.STREAM_ALARM) == 0) { - addIssue(issues, "alarm-volume", "The phone's alarm volume is muted; Faceclaw raises it while ringing, but check it.", true); - } - String maker = Build.MANUFACTURER == null ? "" : Build.MANUFACTURER.toLowerCase(Locale.ROOT); - if (maker.contains("xiaomi") || maker.contains("huawei") || maker.contains("oppo") || maker.contains("vivo") - || maker.contains("oneplus") || maker.contains("realme") || maker.contains("meizu") || maker.contains("asus")) { - addIssue(issues, "oem-killer", "This phone maker is known to stop background apps; allow Faceclaw to auto-start and run unrestricted (see dontkillmyapp.com).", true); - } - if (scheduleLooksStale(appContext)) { - addIssue(issues, "schedule-stale", "The system lost Faceclaw's next alarm (the app may have been force-stopped); it was re-armed.", false); - rescheduleAll(appContext, "self-check"); - } - return issues.toString(); - } - - private static void addIssue(JSONArray issues, String code, String message, boolean fixable) { - JSONObject issue = new JSONObject(); - try { - issue.put("code", code); - issue.put("message", message); - issue.put("fixable", fixable); - } catch (JSONException ignored) { - return; - } - issues.put(issue); - } - - /** Open the system screen where the issue can be fixed. */ - public static void openReliabilityFix(Context context, String code) { - Context appContext = context.getApplicationContext(); - String pkg = appContext.getPackageName(); - Intent intent; - switch (code == null ? "" : code) { - case "exact-alarm": - intent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S - ? new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, Uri.parse("package:" + pkg)) - : appDetails(pkg); - break; - case "notifications": - case "alarm-channel": - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) - .putExtra(Settings.EXTRA_APP_PACKAGE, pkg); - if ("alarm-channel".equals(code)) { - intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) - .putExtra(Settings.EXTRA_APP_PACKAGE, pkg) - .putExtra(Settings.EXTRA_CHANNEL_ID, FaceclawAlarmService.CHANNEL_ID); - } - } else { - intent = appDetails(pkg); - } - break; - case "full-screen": - intent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE - ? new Intent(Settings.ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT, Uri.parse("package:" + pkg)) - : appDetails(pkg); - break; - case "dnd-total": - case "dnd-alarms": - intent = new Intent(Settings.ACTION_SOUND_SETTINGS); - break; - case "battery-optimized": - intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:" + pkg)); - break; - case "alarm-volume": - intent = new Intent(Settings.ACTION_SOUND_SETTINGS); - break; - default: - intent = appDetails(pkg); - break; - } - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - try { - appContext.startActivity(intent); - } catch (RuntimeException error) { - Intent fallback = appDetails(pkg).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - try { - appContext.startActivity(fallback); - } catch (RuntimeException ignored) { - // No settings activity at all; nothing more to do. - } - } - } - - private static Intent appDetails(String pkg) { - return new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + pkg)); - } - - // ------------------------------------------------------------------ - // Diagnostics log - - static synchronized void log(Context context, String line) { - Context appContext = context.getApplicationContext(); - JSONArray log = readArray(appContext, KEY_LOG); - JSONArray next = new JSONArray(); - int start = Math.max(0, log.length() - (LOG_LINES - 1)); - for (int index = start; index < log.length(); index++) { - next.put(log.optString(index)); - } - next.put(System.currentTimeMillis() + " " + line); - writeArray(appContext, KEY_LOG, next); - } - - /** The ring / miss / action log, newest last, one line per entry. */ - public static String readLog(Context context) { - JSONArray log = readArray(context.getApplicationContext(), KEY_LOG); - StringBuilder out = new StringBuilder(); - for (int index = 0; index < log.length(); index++) { - if (index > 0) { - out.append('\n'); - } - out.append(log.optString(index)); - } - return out.toString(); - } - - // ------------------------------------------------------------------ - // Intents - - private static PendingIntent expiryPendingIntent(Context appContext, JSONObject entry) { - Intent intent = new Intent(appContext, FaceclawAlarmReceiver.class); - intent.setAction(ACTION_EXPIRE); - long id = entry.optLong("id"); - intent.putExtra(EXTRA_ID, id); - intent.putExtra(EXTRA_TITLE, entry.optString("title")); - intent.putExtra(EXTRA_TEXT, entry.optString("text")); - intent.putExtra(EXTRA_KIND, entry.optString("kind", KIND_ALARM)); - intent.putExtra(EXTRA_SNOOZE_MINUTES, entry.optInt("snoozeMinutes", 10)); - int flags = PendingIntent.FLAG_UPDATE_CURRENT; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags |= PendingIntent.FLAG_IMMUTABLE; - } - return PendingIntent.getBroadcast(appContext, requestCode(id), intent, flags); - } - - /** What the system's alarm indicator opens: the app. */ - private static PendingIntent showIntent(Context appContext, long id) { - Intent intent = new Intent(appContext, NativeScriptActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); - int flags = PendingIntent.FLAG_UPDATE_CURRENT; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags |= PendingIntent.FLAG_IMMUTABLE; - } - return PendingIntent.getActivity(appContext, requestCode(id), intent, flags); - } - - static int requestCode(long id) { - return (int) (id ^ (id >>> 32)) & 0x7fffffff; - } - - static void startServiceCompat(Context appContext, Intent intent) { - ContextCompat.startForegroundService(appContext, intent); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.kt new file mode 100644 index 00000000..fa6ad2ed --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAlarms.kt @@ -0,0 +1,470 @@ +package com.faceclaw.app + +import android.app.ActivityManager +import android.app.AlarmManager +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import android.media.AudioManager +import android.net.Uri +import android.os.Build +import android.os.Handler +import android.os.Looper +import android.os.PowerManager +import android.provider.Settings + +import androidx.core.content.ContextCompat + +import com.tns.NativeScriptActivity + +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +import java.util.Locale + +/** + * The phone side of the Timers app's alarms and countdown timers, built to + * ring without the JavaScript side: scheduling through AlarmManager's alarm + * clock API, a persisted schedule (in device-protected storage) that the + * reschedule receiver replays after reboots and time changes, the hand-off + * to the ringing foreground service, and a journal of what the wearer did + * on the phone (dismiss / snooze) for the JS engine to replay when it is + * next awake. Also the reliability self-check the UI surfaces. + * + * Ids are the JS engine's item ids (a millisecond epoch times 100 plus a + * serial), shared between timers and alarms; `kind` tells the two apart for + * wording only. + */ +class FaceclawAlarms private constructor() { + companion object { + const val ACTION_EXPIRE = "com.faceclaw.app.action.ALARM_EXPIRE" + internal const val EXTRA_ID = "id" + internal const val EXTRA_TITLE = "title" + internal const val EXTRA_TEXT = "text" + internal const val EXTRA_KIND = "kind" + internal const val EXTRA_SNOOZE_MINUTES = "snoozeMinutes" + + const val KIND_TIMER = AlarmSchedule.KIND_TIMER + const val KIND_ALARM = AlarmSchedule.KIND_ALARM + + private const val PREFS_NAME = "faceclaw-alarms" + + /** Grace window for past-due entries found on replay (shared policy). */ + internal const val LATE_GRACE_MS = AlarmSchedule.LATE_GRACE_MS + /** A glasses status report older than this is not trusted (shared policy). */ + internal const val GLASSES_STATUS_MAX_AGE_MS = GlassesStatusGate.MAX_AGE_MS + + /** The last glasses status pushed by the JS side; read by the ringing service. */ + @JvmField + internal val glassesGate = GlassesStatusGate() + + @Volatile + private var listener: FaceclawAlarmListener? = null + private val mainHandler = Handler(Looper.getMainLooper()) + + // ------------------------------------------------------------------ + // Storage + + /** + * Device-protected storage, so the schedule is readable before the first + * unlock after a reboot (the reschedule receiver may run then on some + * devices) and survives credential-encrypted storage being unavailable. + */ + @JvmStatic + internal fun prefs(context: Context): SharedPreferences { + var base = context.applicationContext + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + base = base.createDeviceProtectedStorageContext() + } + return base.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + } + + @Volatile + private var scheduleInstance: AlarmSchedule? = null + + /** The shared schedule/journal/log core over the device-protected prefs (one per process). */ + @JvmStatic + internal fun scheduleCore(context: Context): AlarmSchedule { + scheduleInstance?.let { return it } + synchronized(FaceclawAlarms::class.java) { + scheduleInstance?.let { return it } + val prefs = prefs(context) + val created = AlarmSchedule(object : AlarmStore { + override fun read(key: String): String? = prefs.getString(key, null) + + override fun write(key: String, value: String?) { + prefs.edit().putString(key, value).apply() + } + }) + scheduleInstance = created + return created + } + } + + @JvmStatic + internal fun findScheduled(context: Context, id: Long): AlarmEntry? = scheduleCore(context).find(id) + + // ------------------------------------------------------------------ + // Scheduling + + /** + * Schedule (or move) the phone alarm for an item. Idempotent: the same id + * always maps to the same PendingIntent, so a reschedule replaces rather + * than duplicates. setAlarmClock is exact, fires through Doze and battery + * savers, and shows the system's alarm indicator; the fallbacks only run + * where it is unavailable. + */ + @JvmStatic + fun schedule( + context: Context, + id: Long, + triggerAtMs: Long, + title: String?, + text: String?, + kind: String?, + snoozeMinutes: Int + ) { + val appContext = context.applicationContext + val entry = AlarmEntry(id, triggerAtMs, title, text, kind ?: KIND_ALARM, snoozeMinutes) + scheduleCore(appContext).put(entry) + armAlarm(appContext, entry) + } + + private fun armAlarm(appContext: Context, entry: AlarmEntry) { + val manager = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager? + ?: return + val id = entry.id + val triggerAt = Math.max(System.currentTimeMillis(), entry.at) + val fire = expiryPendingIntent(appContext, entry) + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + val info = AlarmManager.AlarmClockInfo(triggerAt, showIntent(appContext, id)) + manager.setAlarmClock(info, fire) + } else { + manager.setExact(AlarmManager.RTC_WAKEUP, triggerAt, fire) + } + } catch (error: SecurityException) { + // Exact-alarm access revoked (Android 12/13 without USE_EXACT_ALARM + // honoured): the inexact alarm is still a wake-up, just a late one. + log(appContext, "exact alarm refused for " + id + ": " + error.message) + manager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt, fire) + } + } + + /** Cancel the phone alarm for an item, stop it if ringing, and forget it. */ + @JvmStatic + fun cancel(context: Context, id: Long) { + val appContext = context.applicationContext + val manager = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager? + if (manager != null) { + manager.cancel(expiryPendingIntent(appContext, AlarmEntry(id, 0L, null, null, null, 10))) + } + scheduleCore(appContext).remove(id) + FaceclawAlarmService.stopItem(appContext, id) + } + + /** + * Replay the persisted schedule into AlarmManager: after a reboot (alarms + * are wiped), a package update, or a wall-clock / time-zone change. Entries + * already due ring now if inside the grace window, else are dropped. + */ + @JvmStatic + fun rescheduleAll(context: Context, reason: String?) { + val appContext = context.applicationContext + scheduleCore(appContext).replay(reason, { ringEntry(appContext, it) }, { armAlarm(appContext, it) }) + } + + /** + * The schedule and AlarmManager can disagree (a force-stop clears alarms + * but not the file, an OEM killer drops them). True when the soonest + * stored entry is not what the system reports as the next alarm clock. + */ + @JvmStatic + fun scheduleLooksStale(context: Context): Boolean { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + return false + } + val appContext = context.applicationContext + val core = scheduleCore(appContext) + if (core.soonestPendingAt() == null) { + return false + } + val manager = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager? + ?: return false + return core.looksStale(manager.nextAlarmClock?.triggerTime) + } + + // ------------------------------------------------------------------ + // Ringing + + /** An item came due: hand it to the ringing service (idempotent per id). */ + @JvmStatic + internal fun ringEntry(appContext: Context, entry: AlarmEntry) { + FaceclawAlarmService.ring(appContext, entry.id, entry.title, entry.text, entry.kind, entry.snoozeMinutes) + } + + /** The JS engine's own expiry path; the service deduplicates against the alarm's. */ + @JvmStatic + fun ring(context: Context, id: Long, title: String?, text: String?, kind: String?, snoozeMinutes: Int) { + val appContext = context.applicationContext + val manager = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager? + if (manager != null) { + manager.cancel(expiryPendingIntent(appContext, AlarmEntry(id, 0L, null, null, null, 10))) + } + FaceclawAlarmService.ring(appContext, id, title, text, kind, snoozeMinutes) + } + + /** The glasses are showing the ringing item; the acknowledgement clock starts now. */ + @JvmStatic + fun deliveredToGlasses(context: Context, id: Long) { + FaceclawAlarmService.delivered(context.applicationContext, id) + } + + /** The wearer dismissed or snoozed it on the glasses: the phone side goes quiet. */ + @JvmStatic + fun acknowledge(context: Context, id: Long) { + val appContext = context.applicationContext + scheduleCore(appContext).remove(id) + FaceclawAlarmService.stopItem(appContext, id) + } + + // ------------------------------------------------------------------ + // Glasses status (pushed by the JS side; in-memory, so a dead process reads as "no glasses") + + @JvmStatic + fun setGlassesStatus(connected: Boolean, worn: Boolean, charging: Boolean) { + glassesGate.set(connected, worn, charging) + } + + /** True only when a fresh report says the glasses are connected, on a head, and not charging. */ + @JvmStatic + internal fun glassesCanCarryAlarm(): Boolean = glassesGate.canCarryAlarm() + + @JvmStatic + internal fun glassesStatusDescription(): String = glassesGate.description() + + // ------------------------------------------------------------------ + // Phone actions -> JS + + @JvmStatic + fun setListener(newListener: FaceclawAlarmListener?) { + listener = newListener + } + + /** + * Record a dismiss / snooze done on the phone. Delivered live to the JS + * listener when one is registered, and journaled regardless so a JS side + * that was asleep or gone can catch up on its next boot. + */ + @JvmStatic + internal fun recordPhoneAction(context: Context, id: Long, action: String, minutes: Int) { + val appContext = context.applicationContext + scheduleCore(appContext).appendJournal(id, action, minutes) + val current = listener + if (current != null) { + mainHandler.post { + try { + current.onPhoneAction(id, action, minutes) + } catch (error: RuntimeException) { + log(appContext, "listener failed: " + error.message) + } + } + } + } + + /** Hand the journal to the JS side (as a JSON array) and clear it. */ + @JvmStatic + fun drainJournal(context: Context): String = scheduleCore(context.applicationContext).drainJournal() + + // ------------------------------------------------------------------ + // Reliability self-check + + /** + * Conditions under which the phone may fail to ring, as a JSON array of + * {code, message, fixable}, most serious first. Empty when everything is + * in order. The UI shows these before the wearer relies on an alarm. + */ + @JvmStatic + fun checkReliability(context: Context): String { + val appContext = context.applicationContext + val issues = JSONArray() + val notifications = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager? + val alarms = appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager? + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && alarms != null && !alarms.canScheduleExactAlarms()) { + addIssue(issues, "exact-alarm", "Exact alarms are not allowed for Faceclaw, so alarms may ring late.", true) + } + if (notifications != null && !notifications.areNotificationsEnabled()) { + addIssue(issues, "notifications", "Notifications are turned off for Faceclaw, so the phone cannot show or sound an alarm.", true) + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notifications != null) { + val channel = notifications.getNotificationChannel(FaceclawAlarmService.CHANNEL_ID) + if (channel != null && channel.importance == NotificationManager.IMPORTANCE_NONE) { + addIssue(issues, "alarm-channel", "The Alarms notification category is blocked, so the phone cannot show an alarm.", true) + } + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && notifications != null && !notifications.canUseFullScreenIntent()) { + addIssue(issues, "full-screen", "Full-screen alarms are not allowed, so a ringing alarm will not take over the lock screen.", true) + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && notifications != null) { + val filter = notifications.currentInterruptionFilter + if (filter == NotificationManager.INTERRUPTION_FILTER_NONE) { + addIssue(issues, "dnd-total", "Do Not Disturb is set to total silence, which silences alarms too.", true) + } else if (filter == NotificationManager.INTERRUPTION_FILTER_PRIORITY) { + try { + val policy = notifications.notificationPolicy + if (policy != null && (policy.priorityCategories and NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) == 0) { + addIssue(issues, "dnd-alarms", "Do Not Disturb does not allow alarms, so the phone will stay silent.", true) + } + } catch (ignored: SecurityException) { + // No notification-policy access: cannot inspect the exceptions. + } + } + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + val activityManager = appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager? + if (activityManager != null && activityManager.isBackgroundRestricted) { + addIssue(issues, "background-restricted", "Background usage is restricted for Faceclaw, which blocks alarms while the app is not open.", true) + } + } + val power = appContext.getSystemService(Context.POWER_SERVICE) as PowerManager? + if (power != null && !power.isIgnoringBatteryOptimizations(appContext.packageName)) { + addIssue(issues, "battery-optimized", "Battery optimization is on for Faceclaw; some phones delay alarms because of it.", true) + } + val audio = appContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager? + if (audio != null && audio.getStreamVolume(AudioManager.STREAM_ALARM) == 0) { + addIssue(issues, "alarm-volume", "The phone's alarm volume is muted; Faceclaw raises it while ringing, but check it.", true) + } + val maker = if (Build.MANUFACTURER == null) "" else Build.MANUFACTURER.lowercase(Locale.ROOT) + if (maker.contains("xiaomi") || maker.contains("huawei") || maker.contains("oppo") || maker.contains("vivo") + || maker.contains("oneplus") || maker.contains("realme") || maker.contains("meizu") || maker.contains("asus")) { + addIssue(issues, "oem-killer", "This phone maker is known to stop background apps; allow Faceclaw to auto-start and run unrestricted (see dontkillmyapp.com).", true) + } + if (scheduleLooksStale(appContext)) { + addIssue(issues, "schedule-stale", "The system lost Faceclaw's next alarm (the app may have been force-stopped); it was re-armed.", false) + rescheduleAll(appContext, "self-check") + } + return issues.toString() + } + + private fun addIssue(issues: JSONArray, code: String, message: String, fixable: Boolean) { + val issue = JSONObject() + try { + issue.put("code", code) + issue.put("message", message) + issue.put("fixable", fixable) + } catch (ignored: JSONException) { + return + } + issues.put(issue) + } + + /** Open the system screen where the issue can be fixed. */ + @JvmStatic + fun openReliabilityFix(context: Context, code: String?) { + val appContext = context.applicationContext + val pkg = appContext.packageName + var intent: Intent + when (code ?: "") { + "exact-alarm" -> + intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, Uri.parse("package:$pkg")) + else + appDetails(pkg) + "notifications", "alarm-channel" -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) + .putExtra(Settings.EXTRA_APP_PACKAGE, pkg) + if ("alarm-channel" == code) { + intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) + .putExtra(Settings.EXTRA_APP_PACKAGE, pkg) + .putExtra(Settings.EXTRA_CHANNEL_ID, FaceclawAlarmService.CHANNEL_ID) + } + } else { + intent = appDetails(pkg) + } + } + "full-screen" -> + intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) + Intent(Settings.ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT, Uri.parse("package:$pkg")) + else + appDetails(pkg) + "dnd-total", "dnd-alarms" -> + intent = Intent(Settings.ACTION_SOUND_SETTINGS) + "battery-optimized" -> + intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:$pkg")) + "alarm-volume" -> + intent = Intent(Settings.ACTION_SOUND_SETTINGS) + else -> + intent = appDetails(pkg) + } + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + try { + appContext.startActivity(intent) + } catch (error: RuntimeException) { + val fallback = appDetails(pkg).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + try { + appContext.startActivity(fallback) + } catch (ignored: RuntimeException) { + // No settings activity at all; nothing more to do. + } + } + } + + private fun appDetails(pkg: String): Intent { + return Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:$pkg")) + } + + // ------------------------------------------------------------------ + // Diagnostics log + + @JvmStatic + internal fun log(context: Context, line: String) { + scheduleCore(context.applicationContext).log(line) + } + + /** The ring / miss / action log, newest last, one line per entry. */ + @JvmStatic + fun readLog(context: Context): String = scheduleCore(context.applicationContext).readLog() + + // ------------------------------------------------------------------ + // Intents + + private fun expiryPendingIntent(appContext: Context, entry: AlarmEntry): PendingIntent { + val intent = Intent(appContext, FaceclawAlarmReceiver::class.java) + intent.action = ACTION_EXPIRE + val id = entry.id + intent.putExtra(EXTRA_ID, id) + intent.putExtra(EXTRA_TITLE, entry.title) + intent.putExtra(EXTRA_TEXT, entry.text) + intent.putExtra(EXTRA_KIND, entry.kind) + intent.putExtra(EXTRA_SNOOZE_MINUTES, entry.snoozeMinutes) + var flags = PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or PendingIntent.FLAG_IMMUTABLE + } + return PendingIntent.getBroadcast(appContext, requestCode(id), intent, flags) + } + + /** What the system's alarm indicator opens: the app. */ + private fun showIntent(appContext: Context, id: Long): PendingIntent { + val intent = Intent(appContext, NativeScriptActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP) + var flags = PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or PendingIntent.FLAG_IMMUTABLE + } + return PendingIntent.getActivity(appContext, requestCode(id), intent, flags) + } + + @JvmStatic + internal fun requestCode(id: Long): Int = AlarmSchedule.requestCode(id) + + @JvmStatic + internal fun startServiceCompat(appContext: Context, intent: Intent) { + ContextCompat.startForegroundService(appContext, intent) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAmbientLightListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAmbientLightListener.java deleted file mode 100644 index bc6bc272..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAmbientLightListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawAmbientLightListener { - /** - * A CFW ambient-light report (settings-channel field 105) from the master - * temple. body is the raw 24-byte ['A','L',ver,reason,...] record; it is - * decoded on the TS side (app/native/ambient-light.ts). - */ - void onAmbientLight(byte[] body); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioPacketListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioPacketListener.java deleted file mode 100644 index 16aa0702..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioPacketListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawAudioPacketListener { - void onAudioPacket(byte[] data, String arm, long arrivalMs); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.java deleted file mode 100644 index 0aab2ff6..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.java +++ /dev/null @@ -1,242 +0,0 @@ -package com.faceclaw.app; - -import android.media.MediaCodec; -import android.media.MediaCodecInfo; -import android.media.MediaFormat; -import android.media.MediaMuxer; -import android.util.Log; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -/** - * Transcodes a 16-bit PCM WAV conversation recording into AAC-LC in an M4A - * container, cutting speech recordings to roughly a tenth of their WAV size. - * Runs on its own thread; the source WAV is deleted on success when requested. - */ -public class FaceclawAudioTranscoder { - private static final String TAG = "FaceclawTranscode"; - private static final long CODEC_TIMEOUT_US = 10_000; - - public interface Listener { - void onDone(String outputPath, long outputBytes); - - void onError(String message); - } - - private final String wavPath; - private final String outputPath; - private final int bitrateBps; - private final boolean deleteSourceOnSuccess; - private final Listener listener; - - public FaceclawAudioTranscoder( - String wavPath, - String outputPath, - int bitrateBps, - boolean deleteSourceOnSuccess, - Listener listener) { - this.wavPath = wavPath; - this.outputPath = outputPath; - this.bitrateBps = bitrateBps > 0 ? bitrateBps : 32_000; - this.deleteSourceOnSuccess = deleteSourceOnSuccess; - this.listener = listener; - } - - public void start() { - Thread thread = new Thread(this::run, "FaceclawTranscode"); - thread.setPriority(Thread.MIN_PRIORITY); - thread.start(); - } - - private void run() { - try { - transcode(); - long size = new File(outputPath).length(); - if (deleteSourceOnSuccess) { - new File(wavPath).delete(); - } - if (listener != null) { - listener.onDone(outputPath, size); - } - } catch (Throwable t) { - Log.w(TAG, "transcode failed for " + wavPath, t); - new File(outputPath).delete(); - if (listener != null) { - listener.onError(t.getMessage() == null ? t.toString() : t.getMessage()); - } - } - } - - private void transcode() throws IOException { - WavInfo wav = readWavHeader(wavPath); - MediaFormat format = MediaFormat.createAudioFormat( - MediaFormat.MIMETYPE_AUDIO_AAC, wav.sampleRate, wav.channels); - format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC); - format.setInteger(MediaFormat.KEY_BIT_RATE, bitrateBps); - format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16 * 1024); - - MediaCodec codec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC); - MediaMuxer muxer = null; - FileInputStream input = null; - try { - codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); - codec.start(); - muxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); - input = new FileInputStream(wavPath); - long skipped = 0; - while (skipped < wav.dataOffset) { - long step = input.skip(wav.dataOffset - skipped); - if (step <= 0) { - throw new IOException("could not seek past WAV header"); - } - skipped += step; - } - - MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); - byte[] chunk = new byte[8192]; - int track = -1; - boolean muxerStarted = false; - boolean inputDone = false; - boolean outputDone = false; - long remaining = wav.dataBytes; - long presentationUs = 0; - long bytesPerSecond = (long) wav.sampleRate * wav.channels * 2; - - while (!outputDone) { - if (!inputDone) { - int inIndex = codec.dequeueInputBuffer(CODEC_TIMEOUT_US); - if (inIndex >= 0) { - ByteBuffer inBuf = codec.getInputBuffer(inIndex); - int wanted = (int) Math.min(chunk.length, Math.min(remaining, inBuf.capacity())); - int read = wanted > 0 ? input.read(chunk, 0, wanted) : -1; - if (read <= 0) { - codec.queueInputBuffer(inIndex, 0, 0, presentationUs, - MediaCodec.BUFFER_FLAG_END_OF_STREAM); - inputDone = true; - } else { - inBuf.clear(); - inBuf.put(chunk, 0, read); - codec.queueInputBuffer(inIndex, 0, read, presentationUs, 0); - remaining -= read; - presentationUs += read * 1_000_000L / bytesPerSecond; - } - } - } - int outIndex = codec.dequeueOutputBuffer(info, CODEC_TIMEOUT_US); - if (outIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { - track = muxer.addTrack(codec.getOutputFormat()); - muxer.start(); - muxerStarted = true; - } else if (outIndex >= 0) { - if (info.size > 0 && (info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == 0) { - if (!muxerStarted) { - throw new IOException("encoder produced data before format"); - } - ByteBuffer outBuf = codec.getOutputBuffer(outIndex); - outBuf.position(info.offset); - outBuf.limit(info.offset + info.size); - muxer.writeSampleData(track, outBuf, info); - } - codec.releaseOutputBuffer(outIndex, false); - if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { - outputDone = true; - } - } - } - } finally { - if (input != null) { - try { - input.close(); - } catch (IOException ignored) { - } - } - try { - codec.stop(); - } catch (Throwable ignored) { - } - codec.release(); - if (muxer != null) { - try { - muxer.stop(); - } catch (Throwable ignored) { - } - muxer.release(); - } - } - } - - private static final class WavInfo { - int sampleRate; - int channels; - long dataOffset; - long dataBytes; - } - - /** Minimal RIFF walk: locates fmt and data chunks, 16-bit PCM only. */ - private static WavInfo readWavHeader(String path) throws IOException { - try (FileInputStream in = new FileInputStream(path)) { - byte[] head = new byte[12]; - if (in.read(head) != 12 || head[0] != 'R' || head[1] != 'I' || head[8] != 'W') { - throw new IOException("not a WAV file"); - } - WavInfo info = new WavInfo(); - long offset = 12; - byte[] chunkHead = new byte[8]; - while (in.read(chunkHead) == 8) { - ByteBuffer b = ByteBuffer.wrap(chunkHead).order(ByteOrder.LITTLE_ENDIAN); - int id = b.getInt(); - long size = b.getInt() & 0xffffffffL; - offset += 8; - if (id == 0x20746d66) { // "fmt " - byte[] fmt = new byte[(int) Math.min(size, 16)]; - if (in.read(fmt) != fmt.length) { - throw new IOException("truncated fmt chunk"); - } - ByteBuffer f = ByteBuffer.wrap(fmt).order(ByteOrder.LITTLE_ENDIAN); - int audioFormat = f.getShort(); - info.channels = f.getShort(); - info.sampleRate = f.getInt(); - f.getInt(); - f.getShort(); - int bits = f.getShort(); - if (audioFormat != 1 || bits != 16) { - throw new IOException("only 16-bit PCM WAV is supported"); - } - long skip = size - fmt.length; - while (skip > 0) { - long step = in.skip(skip); - if (step <= 0) { - break; - } - skip -= step; - } - offset += size; - } else if (id == 0x61746164) { // "data" - info.dataOffset = offset; - // A zero size means the header was never patched (crash - // mid-recording); recover the length from the file size. - info.dataBytes = size > 0 ? size : new File(path).length() - offset; - break; - } else { - long skip = size; - while (skip > 0) { - long step = in.skip(skip); - if (step <= 0) { - break; - } - skip -= step; - } - offset += size; - } - } - if (info.sampleRate == 0 || info.dataOffset == 0) { - throw new IOException("missing fmt or data chunk"); - } - return info; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.kt new file mode 100644 index 00000000..58e016cd --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawAudioTranscoder.kt @@ -0,0 +1,183 @@ +package com.faceclaw.app + +import android.media.MediaCodec +import android.media.MediaCodecInfo +import android.media.MediaFormat +import android.media.MediaMuxer +import android.util.Log + +import java.io.File +import java.io.FileInputStream +import java.io.IOException + +/** + * Transcodes a 16-bit PCM WAV conversation recording into AAC-LC in an M4A + * container, cutting speech recordings to roughly a tenth of their WAV size. + * Runs on its own thread; the source WAV is deleted on success when requested. + */ +class FaceclawAudioTranscoder( + private val wavPath: String?, + private val outputPath: String?, + bitrateBps: Int, + private val deleteSourceOnSuccess: Boolean, + private val listener: Listener? +) { + companion object { + private const val TAG = "FaceclawTranscode" + private const val CODEC_TIMEOUT_US: Long = 10_000 + + /** Shared RIFF walk over the file's first bytes: locates fmt and data chunks, 16-bit PCM only. */ + @JvmStatic + @Throws(IOException::class) + private fun readWavHeader(path: String?): WavPcmReader.WavInfo { + val file = File(path) + val fileLength = file.length() + FileInputStream(file).use { `in` -> + val prefix = ByteArray(Math.min(fileLength, WavPcmReader.HEADER_PREFIX_BYTES.toLong()).toInt()) + var filled = 0 + while (filled < prefix.size) { + val read = `in`.read(prefix, filled, prefix.size - filled) + if (read <= 0) break + filled += read + } + try { + return WavPcmReader.parseHeader(if (filled == prefix.size) prefix else prefix.copyOf(filled), fileLength) + } catch (e: IllegalArgumentException) { + throw IOException(e.message ?: "not a WAV file") + } + } + } + } + + interface Listener { + fun onDone(outputPath: String?, outputBytes: Long) + + fun onError(message: String?) + } + + private val bitrateBps: Int = if (bitrateBps > 0) bitrateBps else 32_000 + + fun start() { + val thread = Thread(Runnable { run() }, "FaceclawTranscode") + thread.priority = Thread.MIN_PRIORITY + thread.start() + } + + private fun run() { + try { + transcode() + val size = File(outputPath).length() + if (deleteSourceOnSuccess) { + File(wavPath).delete() + } + if (listener != null) { + listener.onDone(outputPath, size) + } + } catch (t: Throwable) { + Log.w(TAG, "transcode failed for $wavPath", t) + File(outputPath).delete() + if (listener != null) { + listener.onError(if (t.message == null) t.toString() else t.message) + } + } + } + + @Throws(IOException::class) + private fun transcode() { + val wav = readWavHeader(wavPath) + val format = MediaFormat.createAudioFormat( + MediaFormat.MIMETYPE_AUDIO_AAC, wav.sampleRate, wav.channels) + format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC) + format.setInteger(MediaFormat.KEY_BIT_RATE, bitrateBps) + format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16 * 1024) + + val codec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC) + var muxer: MediaMuxer? = null + var input: FileInputStream? = null + try { + codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE) + codec.start() + muxer = MediaMuxer(outputPath!!, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4) + input = FileInputStream(wavPath) + var skipped: Long = 0 + while (skipped < wav.dataOffset) { + val step = input.skip(wav.dataOffset - skipped) + if (step <= 0) { + throw IOException("could not seek past WAV header") + } + skipped += step + } + + val info = MediaCodec.BufferInfo() + val chunk = ByteArray(8192) + var track = -1 + var muxerStarted = false + var inputDone = false + var outputDone = false + var remaining = wav.dataBytes + var presentationUs: Long = 0 + val bytesPerSecond = wav.sampleRate.toLong() * wav.channels * 2 + + while (!outputDone) { + if (!inputDone) { + val inIndex = codec.dequeueInputBuffer(CODEC_TIMEOUT_US) + if (inIndex >= 0) { + val inBuf = codec.getInputBuffer(inIndex)!! + val wanted = Math.min(chunk.size.toLong(), Math.min(remaining, inBuf.capacity().toLong())).toInt() + val read = if (wanted > 0) input.read(chunk, 0, wanted) else -1 + if (read <= 0) { + codec.queueInputBuffer(inIndex, 0, 0, presentationUs, + MediaCodec.BUFFER_FLAG_END_OF_STREAM) + inputDone = true + } else { + inBuf.clear() + inBuf.put(chunk, 0, read) + codec.queueInputBuffer(inIndex, 0, read, presentationUs, 0) + remaining -= read.toLong() + presentationUs += read * 1_000_000L / bytesPerSecond + } + } + } + val outIndex = codec.dequeueOutputBuffer(info, CODEC_TIMEOUT_US) + if (outIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { + track = muxer.addTrack(codec.outputFormat) + muxer.start() + muxerStarted = true + } else if (outIndex >= 0) { + if (info.size > 0 && (info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == 0) { + if (!muxerStarted) { + throw IOException("encoder produced data before format") + } + val outBuf = codec.getOutputBuffer(outIndex)!! + outBuf.position(info.offset) + outBuf.limit(info.offset + info.size) + muxer.writeSampleData(track, outBuf, info) + } + codec.releaseOutputBuffer(outIndex, false) + if ((info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { + outputDone = true + } + } + } + } finally { + if (input != null) { + try { + input.close() + } catch (ignored: IOException) { + } + } + try { + codec.stop() + } catch (ignored: Throwable) { + } + codec.release() + if (muxer != null) { + try { + muxer.stop() + } catch (ignored: Throwable) { + } + muxer.release() + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java deleted file mode 100644 index a336e9fc..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java +++ /dev/null @@ -1,5164 +0,0 @@ -package com.faceclaw.app; - -import android.annotation.SuppressLint; -import android.bluetooth.BluetoothGatt; -import android.bluetooth.BluetoothGattCharacteristic; -import android.app.KeyguardManager; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.os.Handler; -import android.os.Looper; -import android.os.PowerManager; -import android.os.SystemClock; -import android.util.Log; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Random; -import java.util.TimeZone; - -@SuppressLint("MissingPermission") -public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { - private static final String TAG = "FaceclawComm"; - - // Local metadata for custom-command bookkeeping, not an EvenHub container. - // Submitted frames supply pixel geometry; the stock layout only captures input. - private static final BleProtocol.ImageTileOptions DASHBOARD_TILE = - new BleProtocol.ImageTileOptions("img00", 10, 0, 0, 576, 288); - - private static final String G2_SCREEN_WAKE_LOCK_TAG = "Faceclaw:G2Screen"; - private static final long FACECLAW_WAKE_LEASE_RENEW_MS = 45_000; - private static final long FACECLAW_WAKE_CONTROL_WAIT_MS = 1_500; - private static final long CFW_CLEANUP_WAIT_MS = 4_000; - private static final int COMPASS_REPORT_INTERVAL_MS = 100; - private static final int COMPASS_MIN_CHANGE_DEGREES = 0; - - /** - * Cap on decoded ring health pages held in memory. A full backlog sync is - * ~40 pages, so this holds several syncs; oldest is dropped first. This is a - * holding area for a later health feature, not storage. - */ - private static final int RING_HEALTH_MAX_RECORDS = 256; - - private final Context appContext; - private final PowerManager powerManager; - private final KeyguardManager keyguardManager; - private final FaceclawBleManager bleManager; - private final InterruptibleSleep interruptibleSleep = new InterruptibleSleep(); - private final Object lock = new Object(); - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final String rightAddress; - private final String leftAddress; - private final String ringAddress; - - private volatile FaceclawBleCommunicatorListener listener; - private final java.util.List imuListeners = - new java.util.concurrent.CopyOnWriteArrayList<>(); - /** A compass subscriber plus the Looper it registered from (see addCompassListener). */ - private static final class CompassSubscription { - final FaceclawCompassListener listener; - final Handler handler; - - CompassSubscription(FaceclawCompassListener listener, Handler handler) { - this.listener = listener; - this.handler = handler; - } - } - - private final java.util.List compassSubscriptions = - new java.util.concurrent.CopyOnWriteArrayList<>(); - private final java.util.List ambientLightListeners = - new java.util.concurrent.CopyOnWriteArrayList<>(); - private final java.util.List micStatusListeners = - new java.util.concurrent.CopyOnWriteArrayList<>(); - private volatile Thread workerThread; - // Ring connect/write/health waits must never park the glasses sender. - // FaceclawBleManager serializes GATT operations per device address. - private volatile Thread ringWorkerThread; - private volatile boolean running; - private volatile boolean userDisconnectRequested; - // Set when a connect attempt failed while an arm's Android bond is gone: - // retrying is pointless until the user re-pairs, so the worker loop parks - // instead of redialing. Cleared by start() (a fresh explicit connect). - private volatile boolean reconnectHalted; - - private String phase = "disconnected"; - private String status = "Disconnected."; - - private boolean rightConnected; - private boolean leftConnected; - private boolean ringConnected; - private boolean ringNotificationsReady; - // R1 health-data protocol (RingProtocol). The reassembler and the outbound - // queue are both guarded by `lock`. Page ACKs are queued rather than written - // inline because notifications arrive on the GATT callback thread and - // bleManager.writeFrames() blocks waiting for onCharacteristicWrite, which - // that same thread has to deliver — writing there would deadlock until the - // write timeout. The ring worker drains the queue instead. - private final RingProtocol.Reassembler ringReassembler = new RingProtocol.Reassembler(); - private final ArrayDeque ringOutbound = new ArrayDeque<>(); - private final List ringHealthRecords = new ArrayList<>(); - /** - * Total records ever appended to {@link #ringHealthRecords}, including every - * one already consumed or evicted. Never reset. - * - *

This is the identity behind the ingest watermark. Record N is the Nth - * ever decoded this session, so a consumer that has durably stored - * everything below some N can ask for exactly that much to be dropped - * without racing the pages still arriving above it - see - * {@link #takeRingHealthBatch()} and - * {@link #clearRingHealthRecordsBelow(long)}. A plain "clear the list" - * could not do that: a page landing between the read and the clear would be - * thrown away having never been ingested. Guarded by {@code lock}. - */ - private long ringHealthTotalAdded; - /** - * The phone-side sequence counter. One counter shared by BOTH ring channels - * (measured: 78 phone-to-ring writes in the reference capture step by - * exactly +1 regardless of channel, restarting at 1 on a new connection). - */ - private int ringSeq; - private int ringNonce; - /** - * Response-driven health-pull state (2026-09-11). Set from - * {@link #handleRingHealthNotification} (GATT callback thread), waited on - * from {@link #requestRingHealth} (worker thread), both guarded by - * {@code lock}. Replaces a blind fixed-sleep loop - see that method's - * own doc for why: a real successful Even sync (extracted from - * {@code /tmp/btsnoop_fresh.log} on bazzite-desktop, 2026-09-11) is - * response-paced, not sleep-paced, and ACKs each DATA page immediately - * rather than after the whole run. - */ - private boolean ringHealthRspSeen; - /** Bumped once per decoded health DATA page (any type) so a waiter can - * detect "a page just arrived" without polling ringHealthRecords. */ - private int ringHealthPageCounter; - /** - * elapsedRealtime() of the last time {@link #requestRingHealth()} was - * attempted, or 0 if never. Health data updates hourly at the source - * (heart rate/HRV/SpO2) or slower, but a ring reconnect can happen far - * more often than that (BLE drops, Doze, app restarts - this evening's - * own live testing saw reconnects every few seconds under load). Pulling - * on every reconnect would be pure waste even ignoring the risk below; - * given the race condition documented on requestRingHealth() - a request - * that loses its race may silently discard real backlog rather than just - * delay it - it is actively worse than waste, so this is throttled. - */ - private long ringHealthLastRequestedAtMs; - /** - * Set by {@link #requestRingHealthNow()} from whatever thread the UI is on; - * cleared by the ring worker, which is the only thread allowed to run the - * pull. Guarded by {@code lock}. - */ - private boolean ringHealthPullRequested; - /** - * Aborted-pull retries spent since the last pull that COMPLETED its - * sequence. Guarded by {@code lock}. See - * {@link #RING_HEALTH_ABORTED_RETRY_LIMIT}. - */ - private int ringHealthAbortedRetries; - private boolean sessionReady; - private boolean fixedLayoutCreated; - private boolean shutdownRequested; - // CFW firmware-debug-flags overlay (mode 7). Desired value pushed from TS; the - // sub-op last sent this session (-1 = not yet), reset on (re)connect so the - // overlay state is re-asserted on every reconnect and whenever the value changes. - private volatile boolean firmwareDebugFlagsEnabled; - private int firmwareDebugFlagsLastSent = -1; - // Desired CFW mode-10 compass state. It survives reconnects; lastSent is - // reset with each session so an open Compass window is re-asserted. - /** - * Who currently wants the stock compass running (the Compass window, the - * Navigate worker, ...). The magnetometer is one shared resource, so it - * stays on while any owner holds it and is released when the last lets go; - * this keeps one app's release from silently switching off another's feed. - */ - private final java.util.Set compassOwners = new java.util.HashSet<>(); - private int compassControlLastSent = -1; - // Whether the glasses-side compass may still be running: set when an enable - // is enqueued, cleared only when a disable is acked. Drives the forced - // disable sent ahead of an EvenHub shutdown/suspend, since a pending - // disable can be wiped by the shutdown's queue flush and the retry loop - // does not run while shutdownRequested (magnetometer left on = battery drain). - private boolean compassMaybeOn; - private boolean startupProbePending; - // Desired ownership of CFW's fail-open stock-wake lease (dashboard launch - // and Even AI foreground takeover). This survives a transport reconnect; - // the lease itself is volatile firmware state and is re-acquired once both - // arms are ready. - private boolean faceclawWakeLeaseEnabled; - private long lastFaceclawWakeLeaseQueuedAtMs; - private int faceclawWakeControlGeneration; - private int faceclawWakeControlSentCount; - private long lastFaceclawFramebufferLeaseQueuedAtMs; - private int faceclawFramebufferControlGeneration; - private int faceclawFramebufferControlSentCount; - private int faceclawWakePendingNonce = -1; - /** - * The last firmware-info read said the glasses run Faceclaw's custom - * firmware. Gates the private modes (cleanup, texture cache, ...) so stock - * or third-party firmware never sees them; the TS side checks the actual - * revision and disconnects on a mismatch, so no per-feature gating is - * needed here. - */ - private boolean customFirmwareDetected; - private boolean cfwCleanupDelivered; - private int lastCfwCleanupAckMagic; - - private long reconnectAfterMs; - private long ringReconnectAfterMs; - private long lastAckAtMs; - private long lastIncomingAtMs; - private long lastHeartbeatSentAtMs; - private long lastHeartbeatAckedAtMs; - private long lastConnectionOrInputAtMs; - private long lastBatteryRefreshAtMs; - private long imageRetryAfterMs; - private long lastSessionReadyAtMs; - private long lastEvenAppConflictAtMs; - private int consecutiveAckTimeouts; - private int lastAudioControlAckMagic = 0; - - private ConnectionOptions connectionOptions = new ConnectionOptions(); - private final BleMagicPool magicPool = new BleMagicPool(); - private MessageBuilder messageBuilder = new MessageBuilder(magicPool); - private int nextTransportSeq = 0x40; - private int nextMapSessionId = 0; - private int nextImageUpdateId = 1; - // Wire frame id for mode-3 deltas (CFW reorder/skip/dup diagnostic). uint16, - // advanced by 1 per emitted delta; kept in [1, 0xfffe] to avoid the CFW's - // 0xffff "empty" sentinel. - private int nextImageFrameId = 1; - private int lastShutdownAckMagic = 0; - private long lastShutdownExitAtMs = 0; - private int headsetBattery = -1; - private int headsetCharging = -1; - private int ringBattery = -1; - private int ringCharging = -1; - private int directRingBattery = -1; - private int directRingCharging = -1; - // Silent mode: 1 = on, 0 = off, -1 = not yet known. See updateSilentModeLocked. - private int silentMode = -1; - private int wearState = -1; - private int phoneLockState = -1; - private long lastPhoneLockCheckAtMs; - private boolean phoneLockReceiverRegistered; - private boolean audioCaptureActive; - private boolean firmwareInfoQueried; - // Glasses are in the charging case: nobody is wearing them, so display - // communication pauses and only battery polls flow (see driveSession). - private boolean chargingMode; - private volatile FaceclawAudioPacketListener audioPacketListener; - private PowerManager.WakeLock g2ScreenWakeLock; - - // BLE bandwidth benchmark (Developer app). Streams no-op image payloads - // (CFW mode 7 with an unused sub-op: parsed, acked, and discarded — stock - // firmware likewise ignores unknown image modes) for a fixed duration with - // a selectable message size and pipeline window, then reports throughput. - // While active, desired-frame sends are held back and heartbeats are - // satisfied by the benchmark's own acks, so the stream is the only image - // traffic. All state below is guarded by `lock`; results are read with - // getBandwidthBenchmarkStatus() and survive until the next run starts. - private boolean benchmarkActive; - private boolean benchmarkAborted; - private final Random benchmarkRandom = new Random(); - private int benchmarkMessageSize; - private int benchmarkWindowSize; - private int benchmarkDurationMs; - private int benchmarkLinkMode; - private boolean benchmarkLinkPending; - private long benchmarkReadyAtMs; - private long benchmarkStartAtMs; // first benchmark write; 0 until then - private long benchmarkDeadlineAtMs; // start + duration; MAX_VALUE until first write - private long benchmarkLastAckAtMs; - private long benchmarkEndAtMs; // 0 while running; set when the run drains - private int benchmarkMessagesSent; - private int benchmarkMessagesAcked; - private int benchmarkTimeouts; - private long benchmarkPayloadBytesAcked; - private long benchmarkWireBytesAcked; - // A timed-out benchmark message aborts the run, but its already-in-flight - // peers still time out one by one; keep the window comfortably below - // MAX_CONSECUTIVE_ACK_TIMEOUTS so a dead run can't escalate into a - // transport-failure reconnect all by itself. - private static final int BENCHMARK_MAX_WINDOW = 6; - - private final BroadcastReceiver phoneLockReceiver = new BroadcastReceiver() { - @Override public void onReceive(Context context, Intent intent) { - emitPhoneLockStateIfChanged(true); - interruptibleSleep.interrupt(); - } - }; - - private String displayedFingerprint = ""; - // The frame the firmware shadow will hold once the current image pipeline - // drains: the most recently ENQUEUED image (headerless packed 4bpp, see - // BmpUtil.pack4bppFromGray8), which is the correct base for the next delta - // when frames are pipelined. Set at enqueue; cleared whenever the image - // pipeline is cleared (clearAllMessagesLocked / clearMessagesOfKindLocked - // "image"), so it is only ever read while it holds a valid current-session base. - private byte[] lastEnqueuedPacked = new byte[0]; - private int lastEnqueuedWidth; - private int lastEnqueuedHeight; - private String lastEnqueuedFingerprint = ""; - private final Map imageUpdateStats = new HashMap<>(); - - private final Object desiredTilesLock = new Object(); - private String desiredFingerprint = ""; - // Headerless packed 4bpp frame (see BmpUtil.pack4bppFromGray8) plus its - // pixel dimensions. - private byte[] desiredPacked = new byte[0]; - private int desiredWidth; - private int desiredHeight; - private int desiredPaintMs; - private int desiredFrameId; - // Screen-space deferred draws (glyphs + images) whose pixels are baked - // into desiredPacked; the texture-cache planner may replay them as - // on-glasses cached draws. - private SurfaceCompositor.ScreenDraw[] desiredDraws = new SurfaceCompositor.ScreenDraw[0]; - // (frame, reason) of the last "waiting to send" line, so a frame that - // stalls for seconds records one line per state change (see - // noteImageStallLocked). Send-loop thread only. - private int stallFrameId; - private String stallReason = ""; - // Highest compositor sequence stored as the desired frame; composites that - // lost a store race to a newer one are discarded (their content is already - // included in the newer composite). - private long lastStoredCompositeSeq; - - private final SurfaceCompositor compositor = new SurfaceCompositor(); - - // Phone-side model of the CFW's 64 KiB texture cache (modes 12/13/14). - // Reset whenever the image pipeline / EvenHub session is torn down: the - // firmware frees the cache with the fb lease, and after any resync the - // cheap safe assumption is an empty cache (glyphs re-upload lazily). - private final TextureCacheState textureCache = new TextureCacheState(); - - private final ArrayDeque pendingMessages = new ArrayDeque<>(); - private final CfwTransport[] cfwTransports = { new CfwTransport(), new CfwTransport() }; - private final ArrayDeque inFlightMessages = new ArrayDeque<>(); - private OutboundMessage prewrittenMessage; - private List prewrittenFrames = Collections.emptyList(); - - public FaceclawBleCommunicator(Context context, String rightAddress, String leftAddress, String ringAddress) { - this.appContext = context.getApplicationContext(); - FrameTimings.getInstance().init(appContext); - this.powerManager = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE); - this.keyguardManager = (KeyguardManager) appContext.getSystemService(Context.KEYGUARD_SERVICE); - this.bleManager = new FaceclawBleManager(appContext); - this.bleManager.setListener(this); - this.rightAddress = requireAddress("rightAddress", rightAddress); - this.leftAddress = requireAddress("leftAddress", leftAddress); - this.ringAddress = ringAddress == null ? "" : ringAddress.trim(); - IntentFilter phoneLockFilter = new IntentFilter(); - phoneLockFilter.addAction(Intent.ACTION_SCREEN_ON); - phoneLockFilter.addAction(Intent.ACTION_SCREEN_OFF); - phoneLockFilter.addAction(Intent.ACTION_USER_PRESENT); - appContext.registerReceiver(phoneLockReceiver, phoneLockFilter); - phoneLockReceiverRegistered = true; - } - - - public void setListener(FaceclawBleCommunicatorListener listener) { - this.listener = listener; - emitState(); - emitPhoneLockStateIfChanged(true); - } - - public void start() { - synchronized (lock) { - if (running) { - return; - } - running = true; - userDisconnectRequested = false; - reconnectHalted = false; - shutdownRequested = false; - activeInstance = this; - workerThread = new Thread(this, "FaceclawBleCommunicator"); - workerThread.start(); - if (hasRingAddress()) { - ringWorkerThread = new Thread(this::runRingWorker, "FaceclawRing"); - ringWorkerThread.start(); - } - } - } - - public void disconnect() { - /* On the normal path DashboardController already sent mode 11 after - * quiescing its producers. Also cover direct/early close callers here; - * a successful cleanup must remain the final BLE message. Older CFWs - * fall back to the standalone framebuffer-lease release. */ - if (!cfwCleanupDelivered && !sendCfwCleanup()) { - releaseFaceclawFramebufferLease(); - } - Thread threadToJoin; - Thread ringThreadToJoin; - synchronized (lock) { - userDisconnectRequested = true; - running = false; - audioCaptureActive = false; - audioPacketListener = null; - threadToJoin = workerThread; - ringThreadToJoin = ringWorkerThread; - lock.notifyAll(); - } - setStateDisplay("disconnecting", "Disconnecting..."); - interruptibleSleep.interrupt(); - if (ringThreadToJoin != null) ringThreadToJoin.interrupt(); - if (threadToJoin != null) { - threadToJoin.interrupt(); - try { - threadToJoin.join(5_000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - if (ringThreadToJoin != null) { - try { - ringThreadToJoin.join(5_000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - synchronized (lock) { - workerThread = null; - ringWorkerThread = null; - resetSessionStateLocked(); - clearAllMessagesLocked("disconnect"); - // Unknown until the next connection's first push or settings poll. - silentMode = -1; - } - bleManager.disconnect(rightAddress); - bleManager.disconnect(leftAddress); - if (hasRingAddress()) { - bleManager.disconnect(ringAddress); - } - bleManager.close(); - releaseG2ScreenWakeLock(); - setStateDisplay("disconnected", "Disconnected."); - } - - public void close() { - if (activeInstance == this) { - activeInstance = null; - } - disconnect(); - if (phoneLockReceiverRegistered) { - phoneLockReceiverRegistered = false; - appContext.unregisterReceiver(phoneLockReceiver); - } - } - - public void setG2ScreenOn(boolean screenOn) { - mainHandler.post(() -> updateG2ScreenWakeLock(screenOn)); - } - - public void setFirmwareDebugFlags(boolean enabled) { - // Just record it; the drive loop emits the mode-7 control message when the - // display path is ready and idle, and re-emits when this value changes. - firmwareDebugFlagsEnabled = enabled; - } - - public boolean startG2AudioCapture(FaceclawAudioPacketListener listener) { - if (listener == null) { - throw new IllegalArgumentException("listener is required"); - } - int magic; - synchronized (lock) { - if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated) { - logLine("skip G2 mic enable; EvenHub display path not ready"); - return false; - } - audioPacketListener = listener; - OutboundMessage message = createAudioControlMessageLocked(true); - magic = message.magic; - pendingMessages.addFirst(message); - logLine("queue G2 mic enable"); - } - interruptibleSleep.interrupt(); - return waitForAudioControlAck(magic, "enable"); - } - - public void stopG2AudioCapture() { - int magic = 0; - synchronized (lock) { - audioPacketListener = null; - audioCaptureActive = false; - clearMessagesOfKindLocked("audio-control"); - if (running && sessionReady) { - OutboundMessage message = createAudioControlMessageLocked(false); - magic = message.magic; - pendingMessages.addFirst(message); - logLine("queue G2 mic disable"); - } - } - interruptibleSleep.interrupt(); - if (magic != 0) { - waitForAudioControlAck(magic, "disable"); - } - } - - public boolean isSessionReady() { - synchronized (lock) { - return running && sessionReady; - } - } - - /** - * Whether the glasses mic is enabled right now. The enable lives in the - * current EvenHub session, so it dies with a transport drop, the charging - * case, or a suspend — silently, from the phone's point of view. Callers - * that track a capture across those events must check this rather than - * assume their earlier enable still holds. - */ - public boolean isAudioCaptureActive() { - synchronized (lock) { - return running && sessionReady && !shutdownRequested && audioCaptureActive; - } - } - - /** - * Acquire/renew or release CFW's volatile wake-takeover lease on both - * arms. Delivery (not a protocol ACK) is awaited so a caller can ensure - * the fail-open firmware policy is installed before relying on wakeword - * interception or suspending EvenHub. - */ - public boolean setFaceclawWakeLeaseEnabled(boolean enabled) { - int generation; - synchronized (lock) { - faceclawWakeLeaseEnabled = enabled; - if (!running || !sessionReady) { - return !enabled; - } - generation = enqueueFaceclawWakeControlLocked( - enabled ? BleProtocol.FACECLAW_WAKE_OP_ACQUIRE : BleProtocol.FACECLAW_WAKE_OP_RELEASE, - 0, - true - ); - if (!enabled) { - faceclawWakePendingNonce = -1; - } - } - interruptibleSleep.interrupt(); - return waitForFaceclawWakeControlDelivery(generation, FACECLAW_WAKE_CONTROL_WAIT_MS); - } - - /** - * Wait until the recreated layout and retained compositor frame have both - * landed. If this wake came from CFW's deferred double tap, READY - * is then sent to both arms to cancel their stock-dashboard fallback. - */ - public boolean awaitEvenHubSessionReady(int timeoutMs) { - long deadline = SystemClock.elapsedRealtime() + Math.max(0, timeoutMs); - int readyGeneration = 0; - synchronized (lock) { - while (running && sessionReady) { - boolean frameReady = false; - synchronized (desiredTilesLock) { - frameReady = !desiredFingerprint.isEmpty() - && desiredFingerprint.equals(displayedFingerprint); - } - if (!shutdownRequested && fixedLayoutCreated && frameReady) { - if (faceclawWakePendingNonce >= 0) { - readyGeneration = enqueueFaceclawWakeControlLocked( - BleProtocol.FACECLAW_WAKE_OP_READY, - faceclawWakePendingNonce, - true - ); - faceclawWakePendingNonce = -1; - } - break; - } - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - return false; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - } - if (!running || !sessionReady) { - return false; - } - } - if (readyGeneration != 0) { - interruptibleSleep.interrupt(); - if (!waitForFaceclawWakeControlDelivery(readyGeneration, FACECLAW_WAKE_CONTROL_WAIT_MS)) { - logLine("wake READY delivery not confirmed before fallback deadline"); - } - } - return true; - } - - /** - * Enable or disable the IMU (accelerometer) report stream. Fire-and-forget: - * the control message is queued ahead of other traffic; readings arrive via - * registered FaceclawImuListeners. reportFrq is the requested sample rate - * (ignored on disable). - */ - public void setImuReportEnabled(boolean enable, int reportFrq) { - synchronized (lock) { - if (!running || !sessionReady) { - logLine("skip IMU " + (enable ? "enable" : "disable") + "; session not ready"); - return; - } - clearMessagesOfKindLocked("imu-control"); - OutboundMessage message = messageBuilder.enableOrDisableImu(enable, reportFrq); - message.onTimeout = () -> logLine("IMU control ack timeout"); - pendingMessages.addFirst(message); - logLine("queue IMU " + (enable ? "enable freq=" + reportFrq : "disable")); - } - interruptibleSleep.interrupt(); - } - - /** - * Enable/disable the stock compass through CFW image-handler mode 10. The - * desired state is retained across reconnects; headings arrive through - * stock sid-0x08 navigation notifications and FaceclawCompassListeners. - */ - public void setCompassEnabled(boolean enable) { - setCompassEnabled("compass", enable); - } - - /** - * As above, on behalf of a named owner. The compass runs while at least - * one owner has enabled it; an owner disabling it only takes effect once - * no other owner still wants it. - */ - public void setCompassEnabled(String owner, boolean enable) { - synchronized (lock) { - boolean before = !compassOwners.isEmpty(); - if (enable) { - compassOwners.add(owner); - } else { - compassOwners.remove(owner); - } - boolean wanted = !compassOwners.isEmpty(); - if (wanted == before && compassControlLastSent == (wanted ? 1 : 0)) { - logLine("compass " + (enable ? "enable" : "disable") + " by " + owner - + "; state unchanged (owners=" + compassOwners + ")"); - return; - } - compassControlLastSent = -1; - clearMessagesOfKindLocked("compass-control"); - if (running && sessionReady && !shutdownRequested && fixedLayoutCreated) { - enqueueCompassControlLocked(true, wanted); - } else { - logLine("defer compass " + (enable ? "enable" : "disable") + "; display path not ready"); - } - } - interruptibleSleep.interrupt(); - } - - /** - * Set the lens brightness. Fire-and-forget, like the IMU control: the - * message is queued ahead of other traffic and any not-yet-sent brightness - * message is superseded. When autoAdjust is true the ambient-light sensor - * drives brightness and brightnessLevel is ignored; otherwise - * brightnessLevel (0-100) is applied directly. - */ - public void setBrightness(boolean autoAdjust, int brightnessLevel) { - synchronized (lock) { - if (!running || !sessionReady) { - logLine("skip brightness set; session not ready"); - return; - } - clearMessagesOfKindLocked("brightness-control"); - OutboundMessage message = messageBuilder.setBrightness(autoAdjust, brightnessLevel); - message.onTimeout = () -> logLine("brightness control ack timeout"); - pendingMessages.addFirst(message); - logLine("queue brightness " + (autoAdjust ? "auto" : "level=" + brightnessLevel)); - } - interruptibleSleep.interrupt(); - } - - /** - * Enable the stock wear detector, then ask CFW to emit its current cached - * state. The queue order matters: the query must run after the setting is - * applied, including on a fresh install where wear detection was disabled. - */ - public void enableWearDetectionAndRequestState() { - synchronized (lock) { - if (!running || !sessionReady) { - logLine("skip wear detector setup; session not ready"); - return; - } - clearMessagesOfKindLocked("wear-detection-control"); - clearMessagesOfKindLocked("wear-query-control"); - OutboundMessage queryLeft = messageBuilder.faceclawWearQuery(true); - OutboundMessage queryRight = messageBuilder.faceclawWearQuery(false); - OutboundMessage enable = messageBuilder.setWearDetection(true); - enable.onTimeout = () -> logLine("wear detection enable ack timeout"); - pendingMessages.addFirst(queryLeft); - pendingMessages.addFirst(queryRight); - pendingMessages.addFirst(enable); - logLine("queue wear detection enable + current-state query"); - } - interruptibleSleep.interrupt(); - } - - /** - * Start the BLE bandwidth benchmark: stream messageSize-byte no-op image - * payloads for durationMs, keeping up to windowSize messages awaiting ack - * at once, then leave the results for getBandwidthBenchmarkStatus(). - * Returns false when a run is already active or the image path is not - * ready. The duration clock starts at the first benchmark write, so - * traffic already queued ahead of the run doesn't count against it. - */ - public boolean startBandwidthBenchmark(int messageSize, int windowSize, int durationMs) { - return startBandwidthBenchmarkWithLinkMode(messageSize, windowSize, durationMs, 0); - } - - // 0: current link; 1: re-request HIGH; 2: request 2M; 3: both. - public boolean startBandwidthBenchmarkWithLinkMode(int messageSize, int windowSize, - int durationMs, int linkMode) { - synchronized (lock) { - if (benchmarkActive || !running || !sessionReady || !fixedLayoutCreated - || shutdownRequested || chargingMode) { - logLine("skip bandwidth benchmark; already running or image path not ready"); - return false; - } - benchmarkMessageSize = Math.max(2, Math.min(messageSize, ConnectionOptions.IMAGE_FRAGMENT_SIZE)); - benchmarkWindowSize = Math.max(1, Math.min(windowSize, BENCHMARK_MAX_WINDOW)); - benchmarkDurationMs = Math.max(1_000, durationMs); - benchmarkLinkMode = linkMode & 3; - benchmarkLinkPending = true; - benchmarkReadyAtMs = Long.MAX_VALUE; - benchmarkStartAtMs = 0; - benchmarkDeadlineAtMs = Long.MAX_VALUE; - benchmarkLastAckAtMs = 0; - benchmarkEndAtMs = 0; - benchmarkMessagesSent = 0; - benchmarkMessagesAcked = 0; - benchmarkTimeouts = 0; - benchmarkPayloadBytesAcked = 0; - benchmarkWireBytesAcked = 0; - benchmarkAborted = false; - benchmarkActive = true; - logLine("bandwidth benchmark start: size=" + benchmarkMessageSize - + "B window=" + benchmarkWindowSize + " duration=" + benchmarkDurationMs - + "ms linkMode=" + benchmarkLinkMode); - } - interruptibleSleep.interrupt(); - return true; - } - - /** - * Cancel an in-progress benchmark (benchmark page closed). Queued no-op - * messages are dropped; in-flight ones drain through their normal acks. - */ - public void cancelBandwidthBenchmark() { - synchronized (lock) { - if (!benchmarkActive) { - return; - } - clearMessagesOfKindLocked("bandwidth"); - finishBenchmarkLocked(true, "cancelled"); - } - interruptibleSleep.interrupt(); - } - - /** Status/results of the current or most recent benchmark run, as JSON. */ - public String getBandwidthBenchmarkStatus() { - synchronized (lock) { - String state = benchmarkActive - ? (benchmarkStartAtMs == 0 ? "starting" : "running") - : (benchmarkEndAtMs != 0 ? "done" : "idle"); - long end = benchmarkActive ? SystemClock.elapsedRealtime() : benchmarkEndAtMs; - long elapsed = benchmarkStartAtMs == 0 ? 0 : Math.max(0, end - benchmarkStartAtMs); - try { - org.json.JSONObject status = new org.json.JSONObject(); - status.put("state", state); - status.put("messageSize", benchmarkMessageSize); - status.put("windowSize", benchmarkWindowSize); - status.put("linkMode", benchmarkLinkMode); - status.put("elapsedMs", elapsed); - status.put("messagesSent", benchmarkMessagesSent); - status.put("messagesAcked", benchmarkMessagesAcked); - status.put("timeouts", benchmarkTimeouts); - status.put("payloadBytesAcked", benchmarkPayloadBytesAcked); - status.put("wireBytesAcked", benchmarkWireBytesAcked); - status.put("aborted", benchmarkAborted); - return status.toString(); - } catch (org.json.JSONException e) { - return "{\"state\":\"idle\"}"; - } - } - } - - /** Pending + in-flight benchmark no-op messages. */ - private int benchmarkOutstandingLocked() { - int count = 0; - for (OutboundMessage message : pendingMessages) { - if ("bandwidth".equals(message.kind)) count++; - } - for (OutboundMessage message : inFlightMessages) { - if ("bandwidth".equals(message.kind)) count++; - } - return count; - } - - /** - * Keep the benchmark stream fed: top the pending queue up so the send - * window never starves, stop enqueueing once the run expires (or a message - * times out), and finish the run when the last outstanding message drains. - */ - private void maintainBenchmarkLocked(long now) { - if (!sessionReady || !fixedLayoutCreated || shutdownRequested) { - finishBenchmarkLocked(true, "session no longer ready"); - return; - } - if (benchmarkLinkPending || now < benchmarkReadyAtMs) { - return; - } - if (benchmarkAborted || now >= benchmarkDeadlineAtMs) { - // The run is over: drop queued-but-unsent no-ops (sending them - // would stretch the run past its deadline) and finish once the - // in-flight tail has acked or timed out. - Iterator pendingIterator = pendingMessages.iterator(); - while (pendingIterator.hasNext()) { - OutboundMessage queued = pendingIterator.next(); - if ("bandwidth".equals(queued.kind)) { - pendingIterator.remove(); - magicPool.release(queued.sid, queued.magic, queued.label, "benchmark over"); - } - } - if (benchmarkOutstandingLocked() == 0) { - finishBenchmarkLocked(false, "complete"); - } - return; - } - // One more than the window so a fresh message is always ready to write - // the moment an ack frees a slot. - for (int outstanding = benchmarkOutstandingLocked(); outstanding <= benchmarkWindowSize; outstanding++) { - enqueueBenchmarkMessageLocked(); - } - } - - private void finishBenchmarkLocked(boolean aborted, String reason) { - if (!benchmarkActive) { - return; - } - benchmarkActive = false; - benchmarkAborted |= aborted; - // Prefer the last ack as the end time so drain lag after the deadline - // doesn't dilute the throughput figure. - benchmarkEndAtMs = benchmarkLastAckAtMs != 0 ? benchmarkLastAckAtMs : SystemClock.elapsedRealtime(); - logLine("bandwidth benchmark " + (benchmarkAborted ? "aborted" : "finished") + " (" + reason + "): " - + benchmarkMessagesAcked + "/" + benchmarkMessagesSent + " acked, " - + benchmarkPayloadBytesAcked + "B payload, " + benchmarkTimeouts + " timeouts"); - } - - private void enqueueBenchmarkMessageLocked() { - // Fresh bytes per message: the transport's persistent compression history - // would compress even a random payload if we reused it across the run. - byte[] payload = new byte[benchmarkMessageSize]; - benchmarkRandom.nextBytes(payload); - payload[0] = 7; // CFW diagnostic-control mode... - payload[1] = (byte) 0x7f; // ...with an unused sub-op: acked, no effect - OutboundMessage message = messageBuilder.imagePayload( - "bandwidth", - DASHBOARD_TILE, - nextMapSessionId(), - payload, - "bandwidth no-op " + payload.length + "B", - connectionOptions.sendImagesToLeft); - final int payloadBytes = payload.length; - // Logical custom-message bytes; excludes length tag and packet framing. - final int wireBytes = message.message.length; - message.onSent = () -> { - benchmarkMessagesSent++; - if (benchmarkStartAtMs == 0) { - benchmarkStartAtMs = SystemClock.elapsedRealtime(); - benchmarkDeadlineAtMs = benchmarkStartAtMs + benchmarkDurationMs; - } - }; - message.onAck = () -> { - long ackedAtMs = SystemClock.elapsedRealtime(); - benchmarkMessagesAcked++; - benchmarkPayloadBytesAcked += payloadBytes; - benchmarkWireBytesAcked += wireBytes; - benchmarkLastAckAtMs = ackedAtMs; - // No-op payloads ride the image path, so the firmware resets its - // heartbeat timer on them just like real image messages. - lastHeartbeatAckedAtMs = ackedAtMs; - }; - message.onTimeout = () -> { - benchmarkTimeouts++; - benchmarkAborted = true; - logLine("bandwidth benchmark ack timeout"); - }; - pendingMessages.addLast(message); - } - - public void addImuListener(FaceclawImuListener listener) { - if (listener != null) { - imuListeners.add(listener); - } - } - - public void removeImuListener(FaceclawImuListener listener) { - if (listener != null) { - imuListeners.remove(listener); - } - } - - public void addAmbientLightListener(FaceclawAmbientLightListener listener) { - if (listener != null) { - ambientLightListeners.add(listener); - } - } - - public void removeAmbientLightListener(FaceclawAmbientLightListener listener) { - if (listener != null) { - ambientLightListeners.remove(listener); - } - } - - private void emitAmbientLight(byte[] body) { - if (ambientLightListeners.isEmpty()) { - return; - } - byte[] copy = java.util.Arrays.copyOf(body, body.length); - mainHandler.post(() -> { - for (FaceclawAmbientLightListener alsListener : ambientLightListeners) { - try { - alsListener.onAmbientLight(copy); - } catch (Throwable t) { - Log.w(TAG, "ambient light listener failed", t); - } - } - }); - } - - /** Request one CFW ambient-light report (image-handler mode 16 op 0). */ - public void queryAmbientLight() { - synchronized (lock) { - enqueueAmbientLightControlLocked(new byte[] { (byte) 16, (byte) 0 }, "als query", false); - } - interruptibleSleep.interrupt(); - } - - /** - * Start or stop CFW passive light-sensor polling (image-handler mode 16 - * ops 1/2). While polling, the firmware reads its OPT3001 every intervalMs - * (clamped 100..5000 by the firmware), never steps the panel brightness - * itself, and pushes a field-105 report when the reading moved by at least - * minDelta or heartbeatMs elapsed. bindToLease stops polling automatically - * when the Faceclaw framebuffer lease is released or lapses. Starting is - * idempotent and re-opens the sensor if the stock firmware closed it. - */ - public void setAmbientLightPolling(boolean enable, int intervalMs, int minDelta, - int heartbeatMs, boolean bindToLease) { - byte[] payload = enable - ? new byte[] { - (byte) 16, - (byte) 1, - (byte) (bindToLease ? 1 : 0), - (byte) (intervalMs & 0xff), - (byte) ((intervalMs >> 8) & 0xff), - (byte) (minDelta & 0xff), - (byte) ((minDelta >> 8) & 0xff), - (byte) (heartbeatMs & 0xff), - (byte) ((heartbeatMs >> 8) & 0xff), - } - : new byte[] { (byte) 16, (byte) 2 }; - synchronized (lock) { - clearMessagesOfKindLocked("als-control"); - enqueueAmbientLightControlLocked(payload, "als polling " + (enable ? "start" : "stop"), true); - } - interruptibleSleep.interrupt(); - } - - private void enqueueAmbientLightControlLocked(byte[] payload, String label, boolean priority) { - if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated) { - logLine("skip " + label + "; display path not ready"); - return; - } - OutboundMessage message = messageBuilder.imagePayload( - "als-control", - DASHBOARD_TILE, - nextMapSessionId(), - payload, - label, - connectionOptions.sendImagesToLeft); - message.onTimeout = () -> logLine(label + " ack timeout"); - if (priority) pendingMessages.addFirst(message); - else pendingMessages.addLast(message); - logLine("queue " + label); - } - - public void addMicStatusListener(FaceclawMicStatusListener listener) { - if (listener != null) { - micStatusListeners.add(listener); - } - } - - public void removeMicStatusListener(FaceclawMicStatusListener listener) { - if (listener != null) { - micStatusListeners.remove(listener); - } - } - - private void emitMicStatus(byte[] body, String address) { - if (micStatusListeners.isEmpty()) { - return; - } - String arm = address.equalsIgnoreCase(leftAddress) ? "L" - : address.equalsIgnoreCase(rightAddress) ? "R" : "?"; - byte[] copy = java.util.Arrays.copyOf(body, body.length); - mainHandler.post(() -> { - for (FaceclawMicStatusListener micListener : micStatusListeners) { - try { - micListener.onMicStatus(copy, arm); - } catch (Throwable t) { - Log.w(TAG, "mic status listener failed", t); - } - } - }); - } - - /** - * Queue a CFW mic_control record (['M','C',ver,op,...]) as a settings - * field-103 write to both temples, or to a single one. Fire-and-forget: - * the firmware answers with a field-104 status notify per temple, which - * arrives through addMicStatusListener. - */ - public void sendFaceclawMicControl(byte[] record, String label, boolean rightTemple, boolean leftTemple) { - if (record == null || record.length < 4) { - return; - } - synchronized (lock) { - if (!running || !sessionReady) { - logLine("skip mic control (" + label + "); session not ready"); - return; - } - if (rightTemple) { - pendingMessages.addLast(messageBuilder.faceclawMicControl(record, label, false)); - } - if (leftTemple) { - pendingMessages.addLast(messageBuilder.faceclawMicControl(record, label, true)); - } - logLine("queue mic control " + label); - } - interruptibleSleep.interrupt(); - } - - /** - * Forward render-characteristic audio packets to the listener WITHOUT - * sending the stock EvenHub audio-control enable. Used for the CFW - * mic_control streaming path, where capture is armed through settings - * field 103 and the temples emit 'SM' frames on the same characteristic - * that stock mono LC3 uses. Returns false when no session is up. - */ - public boolean startG2AudioForwarding(FaceclawAudioPacketListener listener) { - if (listener == null) { - throw new IllegalArgumentException("listener is required"); - } - synchronized (lock) { - if (!running || !sessionReady || shutdownRequested) { - logLine("skip G2 audio forwarding; session not ready"); - return false; - } - audioPacketListener = listener; - audioCaptureActive = true; - logLine("G2 audio forwarding enabled"); - } - return true; - } - - public void stopG2AudioForwarding() { - synchronized (lock) { - audioPacketListener = null; - audioCaptureActive = false; - logLine("G2 audio forwarding disabled"); - } - } - - /** - * Subscribe to compass events. Callbacks are delivered on the Looper of - * the thread that registered (falling back to the main thread), so app - * worker isolates can listen without a cross-thread hop into their JS. - */ - public void addCompassListener(FaceclawCompassListener listener) { - if (listener == null) { - return; - } - Looper looper = Looper.myLooper(); - Handler handler = looper != null ? new Handler(looper) : mainHandler; - compassSubscriptions.add(new CompassSubscription(listener, handler)); - } - - public void removeCompassListener(FaceclawCompassListener listener) { - if (listener == null) { - return; - } - for (CompassSubscription subscription : compassSubscriptions) { - if (subscription.listener == listener) { - compassSubscriptions.remove(subscription); - } - } - } - - - // The most recently started communicator; lets app worker threads submit - // surface frames without holding a cross-isolate reference to the bridge - // object (JS wrappers do not cross isolates, but the Java instance does). - private static volatile FaceclawBleCommunicator activeInstance; - - public static FaceclawBleCommunicator getActive() { - return activeInstance; - } - - /** Set the compositor's output frame size. Call before configuring surfaces. */ - public void configureCompositorScreen(int width, int height) { - compositor.configureScreen(width, height); - } - - /** - * The current composited screen as a phone-UI preview bitmap, or null - * before any surface has been configured. Built from the compositor so - * the preview reflects every surface (chrome + whichever app is - * foreground), including worker-app frames the TS side never sees. - */ - public android.graphics.Bitmap getCompositePreviewBitmap(double brightenGamma) { - return getCompositePreviewBitmap(brightenGamma, false); - } - - /** As above; `green` renders the preview green-on-black (Settings > Phone display > Preview color). */ - public android.graphics.Bitmap getCompositePreviewBitmap(double brightenGamma, boolean green) { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return null; - } - return PreviewBitmapUtil.fromGray( - java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green); - } - - /** Save the current composite as a 4-bit grayscale PNG; returns the path or "". */ - public String saveCompositePngScreenshot() throws java.io.IOException { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return ""; - } - return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height); - } - - /** - * Save the current composite cropped to the given screen rect (the region - * the shell says is actually occupied). The rect is clamped to the screen; - * a degenerate rect falls back to the full screen. - */ - public String saveCompositePngScreenshot(int cropX, int cropY, int cropWidth, int cropHeight) - throws java.io.IOException { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return ""; - } - int x = Math.max(0, cropX); - int y = Math.max(0, cropY); - int width = Math.min(composite.width - x, cropWidth - (x - cropX)); - int height = Math.min(composite.height - y, cropHeight - (y - cropY)); - if (width <= 0 || height <= 0 || (x == 0 && y == 0 && width == composite.width && height == composite.height)) { - return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height); - } - byte[] cropped = new byte[width * height]; - for (int row = 0; row < height; row++) { - System.arraycopy(composite.gray, (y + row) * composite.width + x, cropped, row * width, width); - } - return ScreenshotUtil.savePngScreenshot(appContext, cropped, width, height); - } - - // Active animated-GIF screen recording, or null when idle. Frames are - // pushed by recordScreenFrame(), which the TS side calls at each - // phone-preview flush. - private volatile GifScreenRecorder screenRecorder; - - /** Begin collecting composite frames for an animated-GIF screen recording. */ - public void startScreenRecording() { - screenRecorder = new GifScreenRecorder(); - } - - /** Capture the current composite into the active recording; no-op when idle. */ - public void recordScreenFrame() { - GifScreenRecorder recorder = screenRecorder; - if (recorder == null) { - return; - } - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return; - } - recorder.addFrame(composite.gray, composite.width, composite.height, System.currentTimeMillis()); - } - - /** Finish the recording and save it as an animated GIF; returns the path or "". */ - public String stopScreenRecording() throws java.io.IOException { - GifScreenRecorder recorder = screenRecorder; - screenRecorder = null; - if (recorder == null) { - return ""; - } - if (recorder.isOverflowed()) { - logLine("screen recording hit its frame cap; the tail was dropped"); - } - return recorder.save(appContext); - } - - /** - * Show or hide a compositor surface, immediately submitting the resulting - * frame. Recompositing here (rather than waiting for the next surface - * update) is what makes a just-foregrounded window's retained frame - * actually appear — otherwise a static window (e.g. the terminal hub) whose - * frame landed while briefly hidden would stay blank until its next repaint. - */ - public void setSurfaceVisible(String id, boolean visible) { - // Its own frame: this recomposite is a real screen update with real - // latency, and without one it would show up in other frames' logs only - // as an anonymous "superseded by frame#0". - int frameId = FrameTimings.getInstance().startFrame( - "compositor:visible " + id + "=" + visible); - compositor.setSurfaceVisible(id, visible); - SurfaceCompositor.Composite composite = compositor.composite(); - byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height); - storeDesiredComposite(composite, packed, 0, frameId); - } - - /** - * Blank (screen off) or unblank the composited output, immediately - * submitting the resulting frame. Retained surface state is untouched, so - * unblanking restores the previous screen content without repaints. - */ - public void setScreenBlanked(boolean blanked) { - int frameId = FrameTimings.getInstance().startFrame( - "compositor:" + (blanked ? "blank" : "unblank")); - compositor.setBlanked(blanked); - SurfaceCompositor.Composite composite = compositor.composite(); - byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height); - storeDesiredComposite(composite, packed, 0, frameId); - } - - /** - * Create or reconfigure a compositor surface. transparency is one of the - * SurfaceCompositor.TRANSPARENCY_* constants. Geometry changes take effect - * when the next frame is submitted. - */ - public void configureSurface(String id, int x, int y, int width, int height, int zOrder, int transparency) { - compositor.configureSurface(id, x, y, width, height, zOrder, transparency); - } - - public void removeSurface(String id) { - compositor.removeSurface(id); - } - - /** - * Dim every surface below zOrder belowZOrder to factor256/256 (see - * SurfaceCompositor.setUnderlayDim). Takes effect with the next submitted - * frame: the shell always submits its own surface right after changing - * this, so no recomposite happens here. - */ - public void setUnderlayDim(int belowZOrder, int factor256) { - compositor.setUnderlayDim(belowZOrder, factor256); - } - - /** - * Apply an update to one compositor surface and submit the recomposited - * screen as the desired frame. The update covers the rect (rectX, rectY, - * rectWidth, rectHeight) in surface-local coordinates; contentFingerprint - * identifies the surface's full content after the update. - * - * pixels8bpp arrives as a ByteBuffer because NativeScript marshals a JS - * ArrayBuffer to one without the per-element bridge copy that a byte[] - * parameter would need (~150ms for a full frame). - */ - public void submitSurfaceFrame( - java.nio.ByteBuffer pixels8bpp, - String surfaceId, - int rectX, - int rectY, - int rectWidth, - int rectHeight, - String contentFingerprint, - int paintMs, - int frameId - ) { - submitSurfaceFrame(pixels8bpp, surfaceId, rectX, rectY, rectWidth, rectHeight, - contentFingerprint, paintMs, frameId, null); - } - - /** - * As above, with the frame's glyph draws (see SurfaceCompositor's glyph - * overload for the buffer format): the surface's full text content as - * structured draws, letting the texture-cache planner ship glyphs as - * on-glasses cached draws instead of pixels. Null when the submitter has - * no glyph metadata; the pixels alone remain fully correct. - */ - public void submitSurfaceFrame( - java.nio.ByteBuffer pixels8bpp, - String surfaceId, - int rectX, - int rectY, - int rectWidth, - int rectHeight, - String contentFingerprint, - int paintMs, - int frameId, - java.nio.ByteBuffer glyphs - ) { - Log.i(TAG, "Received an updated frame for surface " + surfaceId); - FrameTimings.getInstance().log(frameId, "surface " + surfaceId + " updated rect=" - + rectWidth + "x" + rectHeight + "+" + rectX + "+" + rectY - + (glyphs == null ? " (no glyph draws)" : "")); - FrameTimings.getInstance().spanStart(frameId, "composite"); - SurfaceCompositor.Composite composite = compositor.applyAndComposite( - surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, glyphs); - FrameTimings.getInstance().spanEnd(frameId, "composite"); - // Pack the composited 8bpp buffer down to the headerless 4bpp frame - // format the wire planners consume; BMP framing is added later only for - // the uncompressed fallback. - FrameTimings.getInstance().spanStart(frameId, "pack-4bpp"); - byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height); - FrameTimings.getInstance().spanEnd(frameId, "pack-4bpp"); - storeDesiredComposite(composite, packed, paintMs, frameId); - } - - /** Store a composite as the desired frame unless a newer one won the race. */ - private void storeDesiredComposite(SurfaceCompositor.Composite composite, byte[] packed, int paintMs, int frameId) { - int supersededFrameId = 0; - boolean stale = false; - synchronized (desiredTilesLock) { - if (composite.seq <= lastStoredCompositeSeq) { - // A concurrent submission composited after us and stored first; - // its composite already includes this surface update. - stale = true; - } else { - lastStoredCompositeSeq = composite.seq; - supersededFrameId = desiredFrameId; - desiredPacked = packed; - desiredWidth = composite.width; - desiredHeight = composite.height; - desiredFingerprint = composite.fingerprint; - desiredPaintMs = paintMs; - desiredFrameId = frameId; - desiredDraws = composite.draws; - } - } - if (stale) { - finishFrame(frameId, "discarded: composite superseded before store"); - return; - } - if (supersededFrameId != 0 && supersededFrameId != frameId) { - finishFrame(supersededFrameId, "discarded: superseded by frame#" + frameId + " before send"); - } - FrameTimings.getInstance().log(frameId, "image submitted as desired frame"); - interruptibleSleep.interrupt(); - } - - /** - * Play a tone sequence via CFW load_image_z mode 5 kind 4. The payload is - * the complete wire buffer ([5][4][nSteps][freqLo,freqHi,duty,msLo,msHi]*n, - * up to 48 steps), built on the TS side; it rides the arbitrary-payload - * image path like the other mode-5 controls. - */ - public void playBuzzerSequence(java.nio.ByteBuffer payload) { - synchronized (lock) { - if (!running || !sessionReady || !fixedLayoutCreated) { - logLine("skip buzzer sequence; session not ready"); - return; - } - byte[] bytes = new byte[payload == null ? 0 : payload.remaining()]; - if (payload != null) { - payload.get(bytes); - } - if (bytes.length < 3) { - logLine("skip buzzer sequence; empty payload"); - return; - } - OutboundMessage message = messageBuilder.imagePayload( - DASHBOARD_TILE, - nextMapSessionId(), - bytes, - "buzzer sequence " + bytes.length + "B", - connectionOptions.sendImagesToLeft - ); - message.onTimeout = () -> { - handleTransportFailure("buzzer sequence ack timeout"); - }; - pendingMessages.addLast(message); - logLine("queue " + message.label); - } - interruptibleSleep.interrupt(); - } - - public boolean sendShutdown(int exitMode) { - return sendShutdownInternal(exitMode, true); - } - - /** - * Send CFW image-handler mode 11 after quiescing normal traffic. A successful - * return means the cleanup was ACKed and no later Faceclaw message should be - * emitted before closing BLE. Unsupported/older CFWs return false so callers - * can use the legacy shutdown-and-lease-release path. - */ - public boolean sendCfwCleanup() { - int magic; - synchronized (lock) { - if (cfwCleanupDelivered) { - return true; - } - if (!customFirmwareDetected || !running || !sessionReady - || shutdownRequested || !fixedLayoutCreated) { - logLine("skip CFW cleanup; mode 11 unavailable or image path not ready"); - return false; - } - - /* Stop auto-renewals, heartbeats, image generation, and control - * retries, then discard everything that has not reached BLE yet. */ - shutdownRequested = true; - lastCfwCleanupAckMagic = 0; - clearPendingMessagesLocked("CFW cleanup requested"); - logLine("quiescing transport for CFW cleanup"); - } - interruptibleSleep.interrupt(); - - /* WINDOW_SIZE can exceed one, so merely appending cleanup would allow it - * to overlap previously-written image fragments. Wait until all of those - * ACK or time out; shutdownRequested prevents the drive loop from adding - * any fresh automatic traffic meanwhile. */ - long drainDeadline = SystemClock.elapsedRealtime() + CFW_CLEANUP_WAIT_MS; - synchronized (lock) { - while (running && sessionReady && !inFlightMessages.isEmpty()) { - long remaining = drainDeadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) break; - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - if (!running || !sessionReady || !inFlightMessages.isEmpty()) { - shutdownRequested = false; - logLine("CFW cleanup could not drain prior traffic"); - return false; - } - - /* External producers are expected to be stopped by the caller, but - * clear once more at the barrier so cleanup is definitely last. */ - clearPendingMessagesLocked("CFW cleanup barrier"); - OutboundMessage message = messageBuilder.cfwCleanup( - DASHBOARD_TILE, - nextMapSessionId(), - connectionOptions.sendImagesToLeft - ); - magic = message.magic; - message.onAck = () -> { - lastCfwCleanupAckMagic = message.magic; - cfwCleanupDelivered = true; - compassMaybeOn = false; - faceclawWakeLeaseEnabled = false; - logLine("CFW cleanup completed"); - }; - message.onTimeout = () -> logLine("CFW cleanup ack timeout"); - pendingMessages.addLast(message); - logLine("queue CFW cleanup"); - } - interruptibleSleep.interrupt(); - - long deadline = SystemClock.elapsedRealtime() + CFW_CLEANUP_WAIT_MS; - synchronized (lock) { - while (running - && sessionReady - && lastCfwCleanupAckMagic != magic - && hasPendingOrInflightMagicLocked(magic)) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) break; - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - boolean acked = lastCfwCleanupAckMagic == magic; - if (!acked) shutdownRequested = false; - return acked; - } - } - - /** - * End the EvenHub page while retaining both arm GATT connections and all - * notification subscriptions. A missed ACK is intentionally non-fatal: - * reconnecting Bluetooth here would defeat the power-saving mode. - */ - public boolean suspendEvenHubSession() { - // Ending the plugin task releases the fb lease, which frees the - // on-glasses texture cache; forget it phone-side either way (a lost - // ack may still have taken effect). - synchronized (lock) { - textureCache.reset(); - } - if (sendShutdownInternal(0, false)) { - return true; - } - synchronized (lock) { - // A shutdown ACK can be lost even though the command took effect. - // If the transport remains intentionally quiesced, callers still - // need to remember to run the resume path on the next wake. - return running && sessionReady && shutdownRequested; - } - } - - /** - * Start a fresh EvenHub plugin task on the existing BLE transport, then let - * the session driver create the layout, warm up the image path, and send - * the desired frame. - */ - public boolean resumeEvenHubSession() { - int claimGeneration = 0; - synchronized (lock) { - if (!running || !sessionReady || chargingMode) { - logLine("skip EvenHub resume; transport not ready"); - return false; - } - if (!shutdownRequested) { - return true; - } - if (faceclawWakePendingNonce >= 0 - && hasPendingOrInflightKindLocked("wake-lease-control")) { - claimGeneration = faceclawWakeControlGeneration; - } - logLine("replaying session prelude for EvenHub resume"); - } - - // A custom double-tap wake has only a short unclaimed fail-open - // deadline. Let the worker put CLAIM on both arms before the direct - // prelude write begins. - if (claimGeneration != 0) { - waitForFaceclawWakeControlDelivery(claimGeneration, 500); - } - - try { - // Empty-name Cmd=9 tears down the whole plugin task, not just its - // image container. Re-run the launch prelude before Cmd=0 CREATE. - sendPrelude(true); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - handleTransportFailure("EvenHub resume prelude interrupted"); - return false; - } catch (Throwable t) { - logLine("EvenHub resume prelude failed: " + safeMessage(t)); - handleTransportFailure("EvenHub resume prelude failed"); - return false; - } - - synchronized (lock) { - if (!running || !sessionReady || chargingMode) { - return false; - } - shutdownRequested = false; - fixedLayoutCreated = false; - startupProbePending = false; - audioCaptureActive = false; - clearAllMessagesPreservingWakeLeaseLocked("EvenHub resume"); - displayedFingerprint = ""; - imageRetryAfterMs = 0; - lastHeartbeatSentAtMs = 0; - lastHeartbeatAckedAtMs = 0; - logLine("EvenHub session resume requested"); - } - interruptibleSleep.interrupt(); - return true; - } - - private boolean sendShutdownInternal(int exitMode, boolean reconnectOnTimeout) { - int magic; - long startedAtMs = SystemClock.elapsedRealtime(); - synchronized (lock) { - if (!running || !sessionReady) { - logLine("skip shutdown; session not ready"); - return false; - } - if (shutdownRequested) { - return true; - } - shutdownRequested = true; - // Magic values wrap, so an ACK from a much older suspend must not - // satisfy this request after enough sleep/wake cycles. - lastShutdownAckMagic = 0; - clearPendingMessagesLocked("shutdown requested"); - OutboundMessage message = messageBuilder.shutdown(exitMode); - magic = message.magic; - message.onAck = () -> { - lastShutdownAckMagic = message.magic; - fixedLayoutCreated = false; - displayedFingerprint = ""; - }; - message.onTimeout = () -> { - if (reconnectOnTimeout) { - handleTransportFailure("shutdown ack timeout"); - } else { - logLine("EvenHub shutdown ack timeout; keeping BLE connected"); - } - }; - pendingMessages.addFirst(message); - logLine("queue shutdown"); - // The stock compass keeps the magnetometer sampling independently of - // the plugin task, so ending the page does not stop it. Force a - // disable ahead of the shutdown command whenever it may be running: - // this also covers a disable that was wiped by the queue flush above - // or whose ack was lost, and the charging-mode/exit paths where the - // Compass window never got a chance to release it. - if (compassMaybeOn && fixedLayoutCreated) { - enqueueCompassControlLocked(true, false); - } - } - interruptibleSleep.interrupt(); - - long ackDeadline = SystemClock.elapsedRealtime() + ConnectionOptions.ACK_TIMEOUT_MS + 500; - synchronized (lock) { - while (running - && sessionReady - && lastShutdownAckMagic != magic - && lastShutdownExitAtMs < startedAtMs - && hasPendingOrInflightMagicLocked(magic)) { - long remaining = ackDeadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - boolean acked = lastShutdownAckMagic == magic; - if (acked) { - long exitDeadline = SystemClock.elapsedRealtime() + ConnectionOptions.ACK_TIMEOUT_MS + 500; - while (running && sessionReady && lastShutdownExitAtMs < startedAtMs) { - long remaining = exitDeadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - } - return acked || lastShutdownExitAtMs >= startedAtMs; - } - } - - private boolean waitForAudioControlAck(int magic, String operation) { - long deadline = SystemClock.elapsedRealtime() + ConnectionOptions.ACK_TIMEOUT_MS + ConnectionOptions.WRITE_TIMEOUT_MS + 500; - synchronized (lock) { - while (running && sessionReady && lastAudioControlAckMagic != magic && hasPendingOrInflightMagicLocked(magic)) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - boolean acked = lastAudioControlAckMagic == magic; - if (!acked) { - if ("enable".equals(operation)) { - audioPacketListener = null; - audioCaptureActive = false; - } - logLine("G2 mic " + operation + " ack timeout"); - } - return acked; - } - } - - - @Override public void run() { - logLine(String.format(Locale.US, "communicator start R=%s L=%s ring=%s", rightAddress, leftAddress, ringAddress)); - while (true) { - try { - if (!running) { - Log.w(TAG, "Exiting event looop"); - break; - } - emitPhoneLockStateIfChanged(false); - if (!sessionReady) { - if (reconnectHalted) { - interruptibleSleep.sleep(ConnectionOptions.IDLE_SLEEP_MS); - continue; - } - long now = SystemClock.elapsedRealtime(); - if (now < reconnectAfterMs) { - interruptibleSleep.sleep(Math.min(ConnectionOptions.IDLE_SLEEP_MS, reconnectAfterMs - now)); - continue; - } - Log.w(TAG, "Attempting to connect"); - connectLoopOnce(); - continue; - } - - long sleepMs = driveSession(); - if (sleepMs > 0) { - interruptibleSleep.sleep(sleepMs); - } - } catch (Throwable t) { - logLine("communicator loop error: " + safeMessage(t)); - handleTransportFailure("loop error"); - } - } - logLine("communicator stop"); - } - - /** Sole owner of blocking ring operations; callbacks only enqueue ACKs. */ - private void runRingWorker() { - while (running && !Thread.currentThread().isInterrupted()) { - try { - if (shouldAttemptRingConnect()) tryConnectRing("ring worker"); - if (!running) break; - flushRingOutbound(); - runRequestedRingHealthPull(); - resumeAbortedRingHealthPull(); - synchronized (lock) { - if (running) lock.wait(100); - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } catch (Throwable t) { - logLine("ring worker error: " + safeMessage(t)); - // A ring failure must not tear down the glasses session. - synchronized (lock) { - ringConnected = false; - ringNotificationsReady = false; - ringReconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RING_RECONNECT_DELAY_MS; - } - bleManager.disconnect(ringAddress); - } - } - logLine("ring worker stop"); - } - - @Override public void onNotification(String address, String characteristicUuid, byte[] data) { - if (address == null || characteristicUuid == null || data == null) { - return; - } - String uuid = characteristicUuid.toLowerCase(Locale.US); - if (isDirectRingNotification(address, uuid)) { - handleDirectRingNotification(uuid, data); - return; - } - if (BleProtocol.RENDER_NOTIFY_UUID.equals(uuid)) { - handleRenderNotification(address, data); - return; - } - if (!BleProtocol.NOTIFY_CHAR_UUID.equals(uuid)) { - return; - } - if (data.length >= 7 && (data[6] & 255) == CfwTransport.SID) { - CfwTransport.Ack[] acks = CfwTransport.parseAcks(data); - if (acks == null) return; - synchronized (lock) { - lastIncomingAtMs = SystemClock.elapsedRealtime(); - for (CfwTransport.Ack ack : acks) { - for (OutboundMessage message : inFlightMessages) { - String ingress = message.isLeftArmMessage ? leftAddress : rightAddress; - if (message.sid == CfwTransport.SID && address.equalsIgnoreCase(ingress) && message.magic == ack.streamId) { - message.acceptCfwAck(ack); - Log.i(TAG, "CFW " + (ack.nack ? "NACK" : "ACK") + " id=" + ack.streamId - + " ordinal=" + ack.messageId + " lens=" + ack.lens + " txseq=" + (data[2] & 255) - + " redundant=" + (ack != acks[0]) - + " size=" + ack.size + " crc=" + ack.checksum - + " expected=" + message.message.length + "/" + message.cfwChecksum - + " ackedLenses=" + message.cfwAckLenses - + " ageMs=" + (lastIncomingAtMs - message.sentAtMs)); - message.ackPayload = Arrays.copyOf(data, data.length); - break; - } - } - } - drainCfwAcknowledgementsLocked(); - } - interruptibleSleep.interrupt(); - return; - } - Log.d(TAG, "onNotification: address=" + address + " characteristicUuid=" + characteristicUuid + " data.length=" + data.length); - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(data); - int decodedWearState = BleProtocol.parseWearState(frame); - BleProtocol.CompassEvent compassEvent = address.equalsIgnoreCase(rightAddress) - ? BleProtocol.parseCompassEvent(frame) - : null; - boolean emitWearState = false; - G2Event event = null; - synchronized (lock) { - lastIncomingAtMs = SystemClock.elapsedRealtime(); - if (decodedWearState >= 0 && decodedWearState != wearState) { - wearState = decodedWearState; - emitWearState = true; - } - boolean faceclawWakeNotification = false; - if (shutdownRequested - && frame.ok - && frame.sid == BleProtocol.SID_UI_SETTING - && address.equalsIgnoreCase(rightAddress)) { - int wakeNonce = BleProtocol.parseFaceclawWakeEvent(frame.pb); - if (wakeNonce >= 0) { - faceclawWakePendingNonce = wakeNonce; - enqueueFaceclawWakeControlLocked( - BleProtocol.FACECLAW_WAKE_OP_CLAIM, - wakeNonce, - true - ); - faceclawWakeNotification = true; - lastConnectionOrInputAtMs = lastIncomingAtMs; - // The event type says which gesture woke the glasses: TS - // gives a head-up the Glanceboard and a double tap the - // regular UI. The claim handshake is the same for both. - boolean headUp = BleProtocol.parseFaceclawWakeEventCode(frame.pb) - == BleProtocol.FACECLAW_WAKE_EVENT_HEAD_UP; - event = new G2Event( - "display-wake", - "", - headUp ? BleProtocol.EVENT_HEAD_UP : BleProtocol.EVENT_DOUBLE_CLICK, - 0, - 0 - ); - logLine("claimed deferred dashboard wake nonce=" + wakeNonce - + (headUp ? " (head-up)" : "")); - } - } - if (!faceclawWakeNotification - && shutdownRequested - && frame.ok - && frame.sid == BleProtocol.SID_UI_SETTING - && address.equalsIgnoreCase(rightAddress)) { - // CFW idle-gesture forwarding (firmware revision 2+): with no - // EvenHub page the stock display thread drops taps, long - // presses and releases; the CFW reports them on the settings - // sid while our wake lease is held. Deliver them as the - // sys-events a live page would have produced, so TS treats a - // sleep-time tap or hold exactly like one during soft sleep - // (the Glanceboard). - BleProtocol.FaceclawGestureEvent gesture = BleProtocol.parseFaceclawGestureEvent(frame.pb); - if (gesture != null) { - lastConnectionOrInputAtMs = lastIncomingAtMs; - event = new G2Event("sys-event", "", gesture.eventType, gesture.eventSource, 0); - logLine("idle gesture forwarded by CFW: type=" + gesture.eventType - + " source=" + gesture.eventSource); - } - } - if (!faceclawWakeNotification - && event == null - && shutdownRequested - && address.equalsIgnoreCase(rightAddress) - && BleProtocol.isDisplayWakeStateChange(frame)) { - // With no EvenHub page, ring/arm double-taps are handled by the - // stock display lifecycle and surface only as this state ping. - // Translate it back into an input event so TS can wake the shell - // and recreate the page. - event = new G2Event( - "display-wake", - "", - BleProtocol.EVENT_DOUBLE_CLICK, - 0, - 0 - ); - } - if (!faceclawWakeNotification - && frame.ok - && frame.sid == BleProtocol.SID_UI_SETTING) { - // Mode-17 query notifications; settings READs are handled by - // createBatteryQueryMessageLocked so headset/ring update together. - if (address.equalsIgnoreCase(rightAddress) - && (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT)) { - BleProtocol.RingBatterySnapshot ring = BleProtocol.parseRingBattery(frame.pb); - if (ring != null) { - ringBattery = ring.battery; - ringCharging = ring.charging; - emitBatteryState(headsetBattery, headsetCharging); - } - } - // CFW mic status (field 104) rides both standalone pushes and - // settings read acks, from each temple on its own link. - byte[] micStatus = BleProtocol.parseFaceclawMicStatus(frame.pb); - if (micStatus != null) { - emitMicStatus(micStatus, address); - } - } - if (!faceclawWakeNotification - && frame.ok - && frame.sid == BleProtocol.SID_UI_SETTING) { - // CFW ambient-light report (field 105) from the master temple. - byte[] alsReport = BleProtocol.parseFaceclawAlsReport(frame.pb); - if (alsReport != null) { - emitAmbientLight(alsReport); - } - } - if (!faceclawWakeNotification - && decodedWearState < 0 - && frame.ok - && frame.sid == BleProtocol.SID_UI_SETTING) { - // Device-initiated settings push. It carries a magic the glasses - // chose, so it would otherwise fall through to resolveAckLocked, - // match nothing, and be logged as an unexpected ack. - int pushedSilentMode = BleProtocol.parseSilentModePush(frame.pb); - if (pushedSilentMode >= 0) { - updateSilentModeLocked(pushedSilentMode > 0); - return; - } - } - if (!faceclawWakeNotification - && decodedWearState < 0 - && frame.ok - && frame.msgSeq >= 0 - && frame.flag != BleProtocol.FLAG_NOTIFY - && frame.flag != BleProtocol.FLAG_NOTIFY_ALT) { - lastAckAtMs = lastIncomingAtMs; - resolveAckLocked(frame.sid, frame.msgSeq, frame.pb); - } - if (!faceclawWakeNotification - && event == null - && frame.ok - && address.equalsIgnoreCase(rightAddress) - && (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT)) { - event = G2Event.decode(frame); - if (event != null - && "sys-event".equals(event.kind) - && event.eventType == BleProtocol.EVENT_HEAD_UP) { - // CFW forwards the IMU head-up while our page is on screen - // (soft sleep). Surface it as the same wake-only input the - // deferred head-up wake produces from a dark display. - event = new G2Event("display-wake", "", BleProtocol.EVENT_HEAD_UP, event.eventSource, 0); - logLine("head-up forwarded by CFW while page on screen"); - } - if (event != null) { - // Pure IMU samples arrive continuously; don't let them count - // as user input (which would starve battery polling). - boolean pureImuSample = "sys-event".equals(event.kind) - && event.eventType == BleProtocol.EVENT_IMU_DATA_REPORT; - if (!pureImuSample) { - lastConnectionOrInputAtMs = lastIncomingAtMs; - } - if ("list-click".equals(event.kind) || "text-click".equals(event.kind)) { - // Container-routed touchpad input reached us, so the - // firmware is dispatching input: silent mode is off, - // whether or not its end-of-silent push arrived. - updateSilentModeLocked(false); - } - if ("sys-event".equals(event.kind)) { - if (event.eventType == BleProtocol.EVENT_FOREGROUND_EXIT || event.eventType == BleProtocol.EVENT_ABNORMAL_EXIT || event.eventType == BleProtocol.EVENT_SYSTEM_EXIT) { - if (shutdownRequested) { - lastShutdownExitAtMs = SystemClock.elapsedRealtime(); - } - fixedLayoutCreated = false; - displayedFingerprint = ""; - clearAllMessagesLocked("firmware exit event"); - } - } - } - } - } - interruptibleSleep.interrupt(); - if (emitWearState) { - logLine(decodedWearState > 0 ? "wear state ON_HEAD" : "wear state OFF_HEAD"); - emitWearState(decodedWearState > 0); - } - if (compassEvent != null) { - emitCompassEvent(compassEvent); - } - if (event != null) { - if (event.hasImu) { - emitImuData(event.imuX, event.imuY, event.imuZ, event.eventSource); - } - // A standalone IMU_DATA_REPORT is a sensor sample, not a gesture: - // deliver it only to IMU listeners, skipping the input pipeline (and - // its per-frame latency bookkeeping) to avoid flooding it. - boolean pureImuSample = "sys-event".equals(event.kind) - && event.eventType == BleProtocol.EVENT_IMU_DATA_REPORT; - if (!pureImuSample) { - int frameId = FrameTimings.getInstance().startFrame( - "input:" + event.kind + " type=" + event.eventType + " src=" + event.eventSource); - FrameTimings.getInstance().log(frameId, "input event decoded from BLE notification"); - emitRingEvent(event.kind, event.containerName, event.eventType, event.eventSource, event.systemExitReasonCode, frameId); - } - } - } - - private void handleDirectRingNotification(String characteristicUuid, byte[] data) { - if (handleRingHealthNotification(characteristicUuid, data)) { - return; - } - FaceclawRingEventDecoder.DirectRingEvent decoded = FaceclawRingEventDecoder.decode(data); - if (decoded == null) { - Log.d(TAG, "direct ring notify ignored: characteristicUuid=" + characteristicUuid + " raw=" + hex(data)); - return; - } - - G2Event event = decoded.event; - long arrivalMs = SystemClock.elapsedRealtime(); - synchronized (lock) { - lastIncomingAtMs = arrivalMs; - lastConnectionOrInputAtMs = arrivalMs; - } - logLine("direct ring " + decoded.label + " " + decoded.detail + " raw=" + hex(data)); - int frameId = FrameTimings.getInstance().startFrame("input:ring:" + decoded.label); - FrameTimings.getInstance().log(frameId, "input event decoded from direct ring notification"); - emitRingEvent(event.kind, event.containerName, event.eventType, event.eventSource, event.systemExitReasonCode, frameId); - interruptibleSleep.interrupt(); - } - - /** - * The R1 health-data protocol branch. Returns true when the value belonged - * to that protocol and must not fall through to the gesture decoder. - * - *

Runs on the GATT callback thread, so it only ever queues writes. - * - *

Restricted to R1_NOTIFY_CHAR_UUID because that is the notify - * characteristic (ATT handle 0x0017) the reference capture shows this - * protocol on; gesture traffic on the other notify characteristic is left - * strictly alone rather than being offered to the reassembler. - */ - private boolean handleRingHealthNotification(String characteristicUuid, byte[] data) { - if (!BleProtocol.R1_NOTIFY_CHAR_UUID.equals(characteristicUuid)) { - return false; - } - - RingProtocol.Intake intake; - synchronized (lock) { - intake = ringReassembler.accept(data); - } - if (!intake.consumed) { - return false; - } - - long arrivalMs = SystemClock.elapsedRealtime(); - synchronized (lock) { - lastIncomingAtMs = arrivalMs; - } - if (intake.note != null) { - logLine("ring frame: " + intake.note); - } - RingProtocol.Frame frame = intake.frame; - if (frame == null) { - // Genuinely silent by default: the reassembler consumed these - // bytes (e.g. as a fragment awaiting its continuation) without - // yet producing a complete frame, and intake.note was null too. - // This exact blind spot cost real debugging time on 2026-09-10 - - // DATA appeared to simply never arrive, when the actual cause - // (once found, by temporarily logging every raw notification - // unconditionally) was a real handshake gap, not a dropped - // frame here. If DATA ever looks like it's going missing again - // with nothing logged at all, re-add that raw hex dump here - // rather than assuming nothing arrived on the wire. - logLine("ring frame: consumed, no complete frame yet, len=" + data.length); - return true; - } - if (!frame.crcOk) { - // Never act on a frame whose CRC failed: the payload offsets below - // are only meaningful for an intact frame. - logLine("ring frame CRC mismatch " + frame.describe()); - return true; - } - - // Keep the original bytes before acknowledging them. Ring firmware - // variants can have layouts we cannot decode yet, and the ring may - // never deliver an acknowledged page again. This private journal also - // lets us diagnose a new device without repeatedly draining its data. - if (!journalRingFrame(frame)) return true; - - // Route on the CHAN byte, not on the shape of the payload. - if (frame.chan != RingProtocol.CHAN_HEALTH) { - RingProtocol.DeviceStatus status = RingProtocol.decodeDeviceStatus(frame); - if (status != null) { - synchronized (lock) { - directRingBattery = status.battery; - directRingCharging = status.charging; - emitBatteryState(headsetBattery, headsetCharging); - } - logLine("direct ring battery " + status.battery + "% charging=" + status.charging); - } - Log.d(TAG, "ring device frame " + frame.describe()); - return true; - } - - if (frame.kind == RingProtocol.KIND_RSP) { - Log.d(TAG, "ring health rsp " + frame.describe()); - synchronized (lock) { - ringHealthRspSeen = true; - lock.notifyAll(); - } - return true; - } - if (frame.kind != RingProtocol.KIND_DATA) { - Log.d(TAG, "ring health frame " + frame.describe()); - return true; - } - - RingProtocol.HealthRecord record = null; - try { - record = RingProtocol.decode(frame, System.currentTimeMillis()); - } catch (Throwable t) { - logLine("ring health decode error " + frame.describe() + ": " + safeMessage(t)); - } - if (record == null) { - logLine("ring health page undecoded " + frame.describe()); - } else { - synchronized (lock) { - if (ringHealthRecords.size() >= RING_HEALTH_MAX_RECORDS) { - // This was silent. An eviction here drops a record that - // nothing has ingested yet - real data lost inside the - // phone, with no trace anywhere. Say so. With the consume - // path in place the buffer should never reach this size; - // if this line ever appears, the consumer has stopped. - logLine("ring health: buffer FULL at " + RING_HEALTH_MAX_RECORDS - + " - dropping the oldest UNINGESTED record"); - ringHealthRecords.remove(0); - } - ringHealthRecords.add(record); - ringHealthTotalAdded++; - } - logLine("ring health " + record.summary()); - } - - // Acknowledge the page regardless of whether we could decode it: the - // ACK is what keeps the ring sending, and a decode gap must not stall - // the rest of the transfer. - queueRingPageAck(frame); - return true; - } - - private void queueRingPageAck(RingProtocol.Frame page) { - byte[] ack; - synchronized (lock) { - if (!ringNotificationsReady) { - return; - } - ack = RingProtocol.buildPageAck( - nextRingSeqLocked(), - nextRingNonceLocked(), - page.cmdHi, - page.cmdLo, - page.seq - ); - ringOutbound.add(ack); - ringHealthPageCounter++; - lock.notifyAll(); - } - // lock.notifyAll() wakes the ring worker, independently of the glasses. - } - - private boolean journalRingFrame(RingProtocol.Frame frame) { - File folder = new File(appContext.getFilesDir(), "health"); - try { - if (!folder.isDirectory() && !folder.mkdirs()) { - throw new java.io.IOException("cannot create health folder"); - } - String line = "\n{\"receivedAtMs\":" + System.currentTimeMillis() - + ",\"raw\":\"" + hex(frame.raw) + "\"}\n"; - try (FileOutputStream out = new FileOutputStream(new File(folder, "ring-frames.jsonl"), true)) { - out.write(line.getBytes(StandardCharsets.UTF_8)); - out.getFD().sync(); - } - return true; - } catch (Exception error) { - logLine("ring frame persistence failed; page not acknowledged: " + safeMessage(error)); - return false; - } - } - - private void handleRenderNotification(String address, byte[] data) { - FaceclawAudioPacketListener listenerToCall; - long arrivalMs = SystemClock.elapsedRealtime(); - synchronized (lock) { - lastIncomingAtMs = arrivalMs; - listenerToCall = audioCaptureActive ? audioPacketListener : null; - } - if (listenerToCall == null) { - return; - } - String arm = address.equalsIgnoreCase(leftAddress) ? "L" : address.equalsIgnoreCase(rightAddress) ? "R" : "?"; - try { - listenerToCall.onAudioPacket(Arrays.copyOf(data, data.length), arm, arrivalMs); - } catch (Throwable t) { - logLine("G2 mic packet listener failed: " + safeMessage(t)); - } - } - - @Override public void onConnectionStateChange(String address, boolean connected) { - synchronized (lock) { - if (address == null) { - return; - } - if (isConfiguredRingAddress(address)) { - ringConnected = connected; - ringNotificationsReady = false; - if (!connected) { - ringReconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RING_RECONNECT_DELAY_MS; - // Half-received fragments and unsent ACKs do not survive the - // link; the sequence counter restarts on the next connect. - ringReassembler.reset(); - ringOutbound.clear(); - directRingBattery = -1; - directRingCharging = -1; - emitBatteryState(headsetBattery, headsetCharging); - } - logLine(connected ? "direct ring BLE connected" : "direct ring BLE disconnected"); - lock.notifyAll(); - return; - } - if (address.equalsIgnoreCase(rightAddress)) { - rightConnected = connected; - } else if (address.equalsIgnoreCase(leftAddress)) { - leftConnected = connected; - } else { - return; - } - if (!connected) { - sessionReady = false; - fixedLayoutCreated = false; - startupProbePending = false; - chargingMode = false; - audioCaptureActive = false; - audioPacketListener = null; - clearAllMessagesLocked("connection lost"); - displayedFingerprint = ""; - if (!reconnectHalted) { - reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS; - } - } - } - interruptibleSleep.interrupt(); - if (connected) { - setStateDisplay("connected", "Connected."); - } else if (!reconnectHalted) { - // While parked on a missing bond, keep the "unpaired" display: this - // callback is just the teardown of the arm that did connect. - setStateDisplay("connecting", "Connecting to the glasses..."); - } - } - - private void connectLoopOnce() throws InterruptedException { - setStateDisplay("connecting", "Connecting to the glasses..."); - try { - connectArm(rightAddress, true); - connectArm(leftAddress, true); - if (!sleepDuringConnectSettling(800)) { - return; - } - authenticateArms(); - sendPrelude(); - - synchronized (lock) { - sessionReady = true; - // A fresh transport prelude always starts an active EvenHub - // lifecycle, even if the previous connection dropped while - // its page was intentionally suspended. - shutdownRequested = false; - fixedLayoutCreated = false; - clearAllMessagesLocked("session ready"); - displayedFingerprint = ""; - lastAckAtMs = SystemClock.elapsedRealtime(); - lastIncomingAtMs = lastAckAtMs; - lastConnectionOrInputAtMs = lastAckAtMs; - lastSessionReadyAtMs = lastAckAtMs; - lastBatteryRefreshAtMs = 0; - imageRetryAfterMs = 0; - lastHeartbeatSentAtMs = 0; - lastHeartbeatAckedAtMs = 0; - consecutiveAckTimeouts = 0; - lastAudioControlAckMagic = 0; - audioCaptureActive = false; - faceclawWakePendingNonce = -1; - cfwCleanupDelivered = false; - for (CfwTransport transport : cfwTransports) transport.reset(); - lastCfwCleanupAckMagic = 0; - lastFaceclawWakeLeaseQueuedAtMs = 0; - lastFaceclawFramebufferLeaseQueuedAtMs = 0; - enqueueFaceclawFramebufferControlLocked( - BleProtocol.FACECLAW_FB_OP_ACQUIRE, - true - ); - if (faceclawWakeLeaseEnabled) { - enqueueFaceclawWakeControlLocked( - BleProtocol.FACECLAW_WAKE_OP_ACQUIRE, - 0, - true - ); - } - } - setStateDisplay("connected", "Connected."); - logLine("session ready"); - synchronized (lock) { - // Query settings promptly on the first session so firmware - // version/extension (and battery) arrive without waiting for - // the input-quiet battery poll. The settings response doubles as - // the firmware-compatibility check surfaced during onboarding. - if (!firmwareInfoQueried) { - firmwareInfoQueried = true; - lastBatteryRefreshAtMs = SystemClock.elapsedRealtime(); - pendingMessages.addLast(createBatteryQueryMessageLocked()); - logLine("queue settings query for firmware info"); - } - } - synchronized (lock) { lock.notifyAll(); } - } catch (Throwable t) { - logLine("connect failed: " + safeMessage(t)); - String unpairedArm = firstUnpairedArm(); - if (unpairedArm != null) { - handleUnpairedFailure(unpairedArm); - } else { - handleTransportFailure("connect failed"); - } - } - } - - private boolean sleepDuringConnectSettling(long delayMs) throws InterruptedException { - long deadline = SystemClock.elapsedRealtime() + delayMs; - synchronized (lock) { - while (running && !userDisconnectRequested) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - return true; - } - lock.wait(Math.min(remaining, 100)); - } - return false; - } - } - - private void connectArm(String address, boolean enableRenderNotify) { - if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { - throw new IllegalStateException("connect failed: " + address); - } - // requestConnectionPriority has no callback in this Android compile target, so there is - // no reliable completion point to keep it in the global GATT operation pipeline. But it's - // important enough for performance that we call it anyways. - bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH); - - bleManager.requestMtu(address, ConnectionOptions.DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); - - if (!bleManager.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) { - throw new IllegalStateException("discoverServices failed: " + address); - } - if (!bleManager.enableNotifications(address, BleProtocol.NOTIFY_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { - throw new IllegalStateException("enableNotifications failed: " + address + " " + BleProtocol.NOTIFY_CHAR_UUID); - } - if (enableRenderNotify) { - bleManager.enableNotifications(address, BleProtocol.RENDER_NOTIFY_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS); - } - synchronized (lock) { - if (address.equalsIgnoreCase(rightAddress)) { - rightConnected = true; - } else if (address.equalsIgnoreCase(leftAddress)) { - leftConnected = true; - } - } - } - - private boolean shouldAttemptRingConnect() { - if (!hasRingAddress()) { - return false; - } - long now = SystemClock.elapsedRealtime(); - synchronized (lock) { - return running - && sessionReady - && !ringNotificationsReady - && now >= ringReconnectAfterMs; - } - } - - private void tryConnectRing(String reason) { - if (!hasRingAddress()) { - return; - } - try { - connectRing(); - } catch (Throwable t) { - synchronized (lock) { - ringConnected = false; - ringNotificationsReady = false; - ringReconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RING_RECONNECT_DELAY_MS; - } - logLine("direct ring connect failed (" + reason + "): " + safeMessage(t)); - } - } - - private void connectRing() { - logLine("connecting direct ring " + ringAddress); - // autoConnect=true (see FaceclawBleManager.connect's 3-arg overload) - - // matches what Even's own app does for this device specifically. - // Direct connect (false, the default used for the glasses) was - // measured tonight dying ~5s into an otherwise-idle ring connection, - // repeatedly, with nothing else competing for it. - if (!bleManager.connect(ringAddress, ConnectionOptions.RING_CONNECT_TIMEOUT_MS, true)) { - throw new IllegalStateException("connect failed: " + ringAddress); - } - - // Deliberately NOT requesting CONNECTION_PRIORITY_HIGH here (2026-09-11). - // Even's own app never requests any priority for the ring at all - it - // logs no requestConnectionPriority/onConnectionUpdated call anywhere - - // and just runs on whatever Android's default (BALANCED) parameters are. - // HIGH negotiated interval=12 (15ms) / timeout=500 (5000ms) here, and a - // live test tonight found the ring-only connection reliably dying at - // 5.5-5.7s, matching that 5000ms supervision timeout almost exactly, - // on every attempt, with nothing else competing for the ring. If the - // ring's firmware occasionally needs more than 5s of headroom after a - // burst of writes, HIGH priority's short timeout would kill the link - // while Even's default (longer) one wouldn't - untested but the timing - // match is exact, not approximate. Leaving default priority for the - // ring only; the glasses' own HIGH-priority request above is untouched. - bleManager.requestMtu(ringAddress, ConnectionOptions.RING_DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); - - if (!bleManager.discoverServices(ringAddress, ConnectionOptions.SERVICES_TIMEOUT_MS)) { - throw new IllegalStateException("discoverServices failed: " + ringAddress); - } - - // Even's app subscribes only to the health-data notify CCCD (ATT - // handle 24) and never touches the phone-notify one (handle 19). That - // difference was briefly suspected on 2026-09-11 of causing the ring's - // total silence and this call was dropped to match - it changed - // nothing, and the real cause turned out to be the malformed 00:08 - // frame (see sendRingHandshake). Restored, because the phone-notify - // channel is plausibly what carries ring-as-remote gesture events, - // which are a daily-driver feature; matching Even byte-for-byte here - // buys nothing and risks breaking them. - boolean phoneNotify = enableRingNotification(BleProtocol.R1_PHONE_NOTIFY_CHAR_UUID); - boolean dataNotify = enableRingNotification(BleProtocol.R1_NOTIFY_CHAR_UUID); - if (!phoneNotify && !dataNotify) { - throw new IllegalStateException("no R1 notify characteristic subscribed"); - } - - synchronized (lock) { - ringConnected = true; - ringNotificationsReady = true; - ringReconnectAfterMs = 0; - // The ring's sequence counter restarts with the connection. - ringSeq = 0; - ringNonce = 0; - ringReassembler.reset(); - ringOutbound.clear(); - } - logLine("direct ring ready phoneNotify=" + phoneNotify + " dataNotify=" + dataNotify); - - if (dataNotify) { - // The handshake mirrors what Even's own app repeats on every one - // of its sync bursts (confirmed in the reference capture), so - // sending it on every reconnect matches known-working behavior. - // The actual health pull is throttled separately below - see - // ringHealthLastRequestedAtMs and requestRingHealth()'s own - // race-condition warning for why. - sendRingHandshake(); - long now = SystemClock.elapsedRealtime(); - boolean dueForHealthPull; - synchronized (lock) { - dueForHealthPull = ringHealthPullDueLocked(now); - if (dueForHealthPull) { - ringHealthLastRequestedAtMs = now; - } - } - if (dueForHealthPull) { - runRingHealthPull(); - } else { - logLine("ring health: pull skipped, last one was " - + ((now - ringHealthLastRequestedAtMs) / 1000) + "s ago"); - } - } - } - - /** - * Floor between health pulls, independent of how often the ring - * reconnects. Heart rate/HRV/SpO2 update hourly at the source and steps - * every 10 minutes (confirmed from the real export, - * {@code knowledge/inbox/Even_health_data/}); 30 minutes is comfortably - * inside that cadence while keeping reconnect churn from turning into - * repeated pull attempts against the race condition documented on - * {@link #requestRingHealth()}. Not tuned against any real constraint - * from the ring itself - just a sane default. - * - *

Reduced 30min -> 5min on 2026-09-12, and its JOB CHANGED. The - * 30-minute figure was always meant as a CADENCE - how often to collect - - * but it was implemented here as a floor, which is a different thing. The - * result was that a connected, stable ring pulled never: nothing - * drives a pull on a timer, only {@code onRingReady()} on reconnect, so the - * floor was the only clock in the system and it gated a pull that had - * nothing to trigger it. - * - *

The cadence now lives where it belongs, on a wall-clock-aligned tick - * (:01 and :31) in {@code health-live.ts}. What remains here is purely an - * ANTI-SPAM floor: stop a burst of reconnects turning into a burst of - * requests against the race documented on {@link #requestRingHealth()}. - * That job needs 5 minutes, not 30. - */ - private static final long RING_HEALTH_MIN_PULL_INTERVAL_MS = 5L * 60L * 1000L; - - /** - * Floor for a pull the user actually asked for by opening the health app, - * as opposed to the automatic one above. - * - *

Deliberately much shorter than the automatic interval but not - * zero. The 30-minute figure is conservative because an automatic pull - * is speculative — nobody is waiting for it, so there is no reason to spend - * a request. Opening the health app is the opposite: it is an explicit ask, - * and the response-driven {@link #requestRingHealth()} now waits for each - * type's DATA and ACKs its pages before advancing, which is what made the - * backlog-discard race survivable in the first place. A floor still has to - * exist, because the discard risk documented on {@code requestRingHealth()} - * is real and open/close/open would otherwise hammer the ring. - */ - private static final long RING_HEALTH_ON_DEMAND_MIN_INTERVAL_MS = 60L * 1000L; - - /** - * How many times a pull that ABORTED may be resumed inside the anti-spam - * floor before it goes back to waiting that floor out. - * - *

The floor exists to stop SPECULATIVE repeat pulls, because a request - * that loses its race can permanently consume backlog - see - * {@link #requestRingHealth()}. Resuming a pull that never got a single - * answer is a different thing: nothing was ever in flight to be lost. - * - *

Measured 2026-09-12: the 09:33 pull got {@code no RSP} for 0x1, 0x4 - * and 0x2 and then died on {@code write error: IllegalStateException: Not - * connected}. Every attempt after it was refused by the 30-minute floor, so - * the rest of that night's data was simply never requested again - the - * night's second sleep block went with it. The floor was protecting against - * the wrong thing. - * - *

Bounded and non-looping on purpose: the budget is spent whether or not - * the retries help, and a pull that completes resets it. Worst case is two - * extra attempts, then silence until the floor expires. - * - *

⚠ UNTESTED, and worth knowing before trusting this: whether an aborted - * pull's data is still on the ring at all, or was discarded when the ring - * RSP'd (it did not RSP here, which is the reason to think it survives). - * Nobody knows. The retry costs little if the data is gone. - */ - private static final int RING_HEALTH_ABORTED_RETRY_LIMIT = 2; - - /** - * Gap before an aborted pull may be resumed. Short, but not zero - a link - * that just failed a write needs time to come back, and a tight loop - * against a dead connection helps nobody. - */ - private static final long RING_HEALTH_ABORTED_RETRY_GAP_MS = 60L * 1000L; - - /** - * Ask for a health pull as soon as the worker thread can run one. Safe to - * call from any thread; returns immediately without blocking. - * - *

Called when the health app opens, so a glance shows something current - * rather than whatever the last automatic pull happened to catch. Subject - * to {@link #RING_HEALTH_ON_DEMAND_MIN_INTERVAL_MS}; a request inside that - * window is dropped rather than queued, because a stale duplicate pull has - * no value and every pull carries the discard risk. - */ - public void requestRingHealthNow() { - synchronized (lock) { - ringHealthPullRequested = true; - lock.notifyAll(); - } - interruptibleSleep.interrupt(); - } - - /** - * Whether an automatic pull may run now. Caller must hold {@code lock}. - * - *

Two ways to be due: the ordinary 30-minute floor has expired, or the - * last pull ABORTED and still has retry budget. The second is not a - * speculative repeat - see {@link #RING_HEALTH_ABORTED_RETRY_LIMIT}. - */ - private boolean ringHealthPullDueLocked(long now) { - if (ringHealthLastRequestedAtMs == 0) { - return true; - } - long since = now - ringHealthLastRequestedAtMs; - if (since >= RING_HEALTH_MIN_PULL_INTERVAL_MS) { - return true; - } - return ringHealthAbortedRetries > 0 && since >= RING_HEALTH_ABORTED_RETRY_GAP_MS; - } - - /** - * Run a pull and account for whether it finished. The ONLY place - * {@link #requestRingHealth()} may be called from, so that every path - * shares one definition of "aborted". - */ - private void runRingHealthPull() { - boolean completed = requestRingHealth(); - synchronized (lock) { - if (completed) { - ringHealthAbortedRetries = 0; - } else if (ringHealthAbortedRetries < RING_HEALTH_ABORTED_RETRY_LIMIT) { - ringHealthAbortedRetries++; - logLine("ring health: aborted pull may resume in " - + (RING_HEALTH_ABORTED_RETRY_GAP_MS / 1000L) + "s (retry " - + ringHealthAbortedRetries + "/" + RING_HEALTH_ABORTED_RETRY_LIMIT + ")"); - } else { - ringHealthAbortedRetries = 0; - logLine("ring health: aborted pull retry budget spent, back to the " - + (RING_HEALTH_MIN_PULL_INTERVAL_MS / 60000L) + "-minute anti-spam floor"); - } - } - } - - /** - * Worker-thread tick that resumes a pull which ABORTED. - * - *

Distinct from {@link #runRequestedRingHealthPull()}: nobody asked for - * this one. It exists because the automatic pull otherwise only fires from - * the ring-ready path, so an abort that is not followed by a reconnect - * would never be retried at all. - */ - private void resumeAbortedRingHealthPull() { - long now = SystemClock.elapsedRealtime(); - synchronized (lock) { - if (ringHealthAbortedRetries == 0) { - return; - } - if (!ringConnected || !ringNotificationsReady) { - return; - } - if (now - ringHealthLastRequestedAtMs < RING_HEALTH_ABORTED_RETRY_GAP_MS) { - return; - } - ringHealthLastRequestedAtMs = now; - } - logLine("ring health: resuming an aborted pull"); - runRingHealthPull(); - } - - /** Worker-thread side of {@link #requestRingHealthNow()}. */ - private void runRequestedRingHealthPull() { - long now = SystemClock.elapsedRealtime(); - synchronized (lock) { - if (!ringHealthPullRequested) { - return; - } - ringHealthPullRequested = false; - if (!ringConnected || !ringNotificationsReady) { - logLine("ring health: on-demand pull skipped, ring not ready"); - return; - } - if (ringHealthLastRequestedAtMs != 0 - && now - ringHealthLastRequestedAtMs < RING_HEALTH_ON_DEMAND_MIN_INTERVAL_MS) { - logLine("ring health: on-demand pull skipped, last one was " - + ((now - ringHealthLastRequestedAtMs) / 1000) + "s ago"); - return; - } - ringHealthLastRequestedAtMs = now; - } - logLine("ring health: on-demand pull requested"); - runRingHealthPull(); - } - - /** - * Device-channel initialization. 00:0e enables health recording; 00:05 sets - * the clock (signed timezone minutes plus Unix seconds). - * - * The inherited extra timezone shift is WRONG: on this PDT ring, fresh HR - * timestamps track receipt time + 7h. Firmware expects UTC seconds and - * applies the timezone separately. Do not simply deploy a seven-hour - * rewind: stock firmware can format its health database on a backward - * jump >= 1h. Existing pages also contain mixed clock domains and invalid - * daily anchors, so their dates cannot be repaired by one subtraction. - * - * TODO: controlled UTC migration after preserving ring history. Retain the - * existing wire behavior until that migration is authorized and validated. - * See notes/ring-health-live-validation.md for evidence and the next test. - */ - private void sendRingHandshake() { - long nowMs = System.currentTimeMillis(); - long clockOffsetSeconds = -TimeZone.getDefault().getOffset(nowMs) / 1000L; - long liveClockSeconds = (nowMs / 1000L) + clockOffsetSeconds; - Log.i(TAG, "ring handshake legacy clock offset seconds = " + clockOffsetSeconds - + " (tz " + TimeZone.getDefault().getID() + ")"); - byte[] clock = le32(liveClockSeconds); - - int seq1, nonce1, seq2, nonce2, seq3, nonce3, seq4, nonce4, seq5, nonce5, seq6, nonce6, seq7, nonce7; - synchronized (lock) { - seq1 = nextRingSeqLocked(); - nonce1 = nextRingNonceLocked(); - seq2 = nextRingSeqLocked(); - nonce2 = nextRingNonceLocked(); - seq3 = nextRingSeqLocked(); - nonce3 = nextRingNonceLocked(); - seq4 = nextRingSeqLocked(); - nonce4 = nextRingNonceLocked(); - seq5 = nextRingSeqLocked(); - nonce5 = nextRingNonceLocked(); - seq6 = nextRingSeqLocked(); - nonce6 = nextRingNonceLocked(); - seq7 = nextRingSeqLocked(); - nonce7 = nextRingNonceLocked(); - } - - byte[][] frames = { - RingProtocol.buildFrame(RingProtocol.CHAN_DEVICE, RingProtocol.KIND_REQ, 0x00, 0x08, seq1, - // NO nonce prefix on this one frame - payload is exactly the - // three bytes 3f 01 01. Every other request on both channels - // starts with the 2-byte nonce; 00:08 does not, and the ring - // silently drops the whole frame if you add one, then ignores - // everything that follows on the connection. - // - // Measured 2026-09-11 across all four btsnoop captures, 46 - // real 00:08 writes, zero exceptions: - // payload 3f0101 (3 bytes) -> 3 writes, ALL answered (12-21 notifications each) - // payload 01003f0101 (5 bytes) -> 43 writes, ALL silent (zero notifications) - // The 5-byte form is the more common one in the captures only - // because it is ours, failing and retrying all night. Do not - // "fix" this back to the nonce form by pattern-matching the - // other frames or by counting which variant appears more. - // - // This also explains 2026-09-10: the handshake worked when it - // was raw bytes replayed from the capture, and stopped working - // the moment it was rebuilt "properly" through buildFrame(), - // which is what introduced the nonce prefix. - new byte[] {0x3f, 0x01, 0x01}), - RingProtocol.buildFrame(RingProtocol.CHAN_DEVICE, RingProtocol.KIND_DATA, 0x00, 0x0e, seq2, - concatBytes(le16(nonce2), clock, new byte[] {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), - RingProtocol.buildFrame(RingProtocol.CHAN_DEVICE, RingProtocol.KIND_DATA, 0x00, 0x05, seq3, - concatBytes(le16(nonce3), - le16(TimeZone.getDefault().getOffset(nowMs) / 60000), clock)), - // Battery, firmware version, device id - all bare nonce-only REQs, - // same shape as the health requests. New this round. - RingProtocol.buildFrame(RingProtocol.CHAN_DEVICE, RingProtocol.KIND_REQ, 0x00, 0x01, seq4, - le16(nonce4)), - RingProtocol.buildFrame(RingProtocol.CHAN_DEVICE, RingProtocol.KIND_REQ, 0x00, 0x02, seq5, - le16(nonce5)), - RingProtocol.buildFrame(RingProtocol.CHAN_DEVICE, RingProtocol.KIND_REQ, 0x00, 0x0b, seq6, - le16(nonce6)), - RingProtocol.buildFrame(RingProtocol.CHAN_DEVICE, RingProtocol.KIND_REQ, 0x00, 0x0a, seq7, - concatBytes(le16(nonce7), parseHex("f8f53d235ac4ceaa073885cc"))), - }; - for (byte[] frame : frames) { - if (!writeRingFrame(frame, "handshake")) { - return; - } - } - logLine("ring health: sent device-channel handshake (" + frames.length + " frames)"); - } - - private static byte[] parseHex(String hex) { - byte[] out = new byte[hex.length() / 2]; - for (int i = 0; i < out.length; i++) { - out[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16); - } - return out; - } - - private static byte[] le16(int value) { - return new byte[] {(byte) value, (byte) (value >>> 8)}; - } - - private static byte[] le32(long value) { - return new byte[] {(byte) value, (byte) (value >>> 8), (byte) (value >>> 16), (byte) (value >>> 24)}; - } - - private static byte[] concatBytes(byte[]... parts) { - int len = 0; - for (byte[] part : parts) { - len += part.length; - } - byte[] out = new byte[len]; - int offset = 0; - for (byte[] part : parts) { - System.arraycopy(part, 0, out, offset, part.length); - offset += part.length; - } - return out; - } - - /** - * Timeout waiting for a health REQ's own RSP. In the reference capture - * (a real successful Even sync, extracted from - * {@code /tmp/btsnoop_fresh.log} on bazzite-desktop, 2026-09-11) RSPs - * land within ~150ms; this only needs to bound the pathological "ring - * stopped answering entirely" case. - */ - private static final long RING_HEALTH_RSP_TIMEOUT_MS = 3000L; - - /** - * How long to keep waiting after the most recent DATA page for a health - * type before deciding its transfer is over (or that there was never - * going to be one). Chosen from the same reference capture: consecutive - * pages for one type land well under a second apart, so 1.5s covers - * that with real margin while still moving on quickly when a type - * genuinely has nothing new. - */ - private static final long RING_HEALTH_DATA_IDLE_MS = 1500L; - - /** - * Device-channel commands Even's real app fires in the gaps between - * health requests - identity (0x0a), battery (0x01), firmware version - * (0x02), device id (0x0b) - found by extracting the literal wire order - * of a real successful sync (same capture as above; two independent - * syncs in it agree). Rotated one-per-health-type here rather than - * replicated exactly (Even's real cadence isn't perfectly regular - * either - compare the two syncs in the capture), since which of these, - * if any, is actually load-bearing for the ring answering has never - * been isolated. This is matching cadence, not a confirmed protocol - * requirement. - */ - private static final int[] RING_DEVICE_PING_CMD_LO = {0x0a, 0x01, 0x02, 0x0b}; - - /** - * Pull the five health record types. Each request is a bare 17-byte frame - * carrying only a nonce; the ring answers with an RSP on the same - * sequence number, then - sometimes - pushes one or more DATA pages, - * which arrive through {@link #handleRingHealthNotification}. - * - *

Response-driven, not sleep-driven (rewritten 2026-09-11). - * The original version fired all five REQs on a blind fixed sleep and - * only flushed queued page ACKs after the whole loop finished - a real - * successful Even sync (see the constants above) does the opposite: it - * waits for each REQ's own RSP, waits for DATA to go idle, ACKs - * immediately, then interleaves one device-channel command before the - * next health type. This version does the same, using the existing - * lock.wait()/notifyAll() idiom already used elsewhere in this class - * (e.g. the shutdown-ack wait above) rather than a new mechanism. - * - *

Race condition, confirmed live 2026-09-10 - this is what the - * rewrite above addresses, not a reason to remove this warning. The - * ring appears to hold only one health type's response in flight at a - * time. Sending the next health REQ before the previous type's DATA - * page(s) have actually arrived silently drops the earlier type's DATA: - * you still get its RSP, you never get its DATA, and nothing in the - * protocol signals an error. Evidence: five REQs fired back-to-back (the - * old blind-sleep version, badly timed) got five RSPs and zero DATA - * pages over more than a minute of waiting. - * - *

Worse: a lost race may PERMANENTLY discard that backlog, not - * just delay it. On 2026-09-10, real HRV/SpO2/sleep backlog that - * should have existed (unreported since that morning, confirmed against - * the hourly export cadence in {@code knowledge/inbox/Even_health_data}) - * did not come back on a later, more carefully spaced retry. The working - * theory - not proven, but treat it as true until disproven, because the - * downside of being wrong is silent data loss with nothing to catch it - * - is that the ring advances its own "last delivered" watermark for a - * type as soon as it RSPs a REQ for it, whether or not the DATA page - * actually made it out. Do not send a speculative or test health REQ - * for a type unless you intend to actually receive and persist its - * DATA - re-requesting will not recover what an earlier, raced - * request already consumed. The rewrite below waiting for DATA to go - * idle before advancing reduces how often this can happen but does not - * prove it can no longer happen (e.g. if the ring's real per-type - * timeout is shorter than RING_HEALTH_DATA_IDLE_MS for some type). - * - *

A separate, completely ordinary case looks identical from the wire - * alone: an RSP with no DATA can also just mean the ring genuinely has - * nothing new for that metric since the last successful pull. There is - * currently no way to tell "raced and silently discarded" apart from - * "genuinely nothing new" other than knowing independently whether fresh - * data should exist (e.g. from the export's own sampling cadence). - * - *

Runs on the ring worker (from connectRing), which is the only - * thread allowed to call the blocking write path - the wait loops below - * block that same thread, which is why {@link #flushRingOutbound} is - * called inside the response and DATA waits rather than relying on the - * ring loop's own call to it. - */ - private boolean requestRingHealth() { - // A previous attempt may have left an ACK queued after a failed write. - // Never start another type's request ahead of that acknowledgement. - if (flushRingOutbound() < 0) return false; - int[] commands = RingProtocol.HEALTH_COMMANDS; - int rspCount = 0; - for (int i = 0; i < commands.length; i++) { - int command = commands[i]; - byte[] frame; - synchronized (lock) { - ringHealthRspSeen = false; - frame = RingProtocol.buildHealthRequest(command, nextRingSeqLocked(), nextRingNonceLocked()); - } - if (!writeRingFrame(frame, "health request 0x" + Integer.toHexString(command))) { - // ABORTED: the sequence stopped partway. Distinct from "the - // ring had nothing for a type", which is an ordinary outcome - // and leaves the loop running - see the return below. - logLine("ring health: pull ABORTED on write at 0x" + Integer.toHexString(command) - + " (" + i + " of " + commands.length + " types attempted, " - + rspCount + " answered)"); - return false; - } - - if (awaitRingHealthRsp(RING_HEALTH_RSP_TIMEOUT_MS)) { - rspCount++; - if (!awaitRingHealthDataIdle(RING_HEALTH_DATA_IDLE_MS)) { - logLine("ring health: pull ABORTED while acknowledging DATA"); - return false; - } - } else { - logLine("ring health: pull ABORTED waiting for RSP at 0x" + Integer.toHexString(command)); - return false; - } - - if (i < commands.length - 1) { - sendRingDevicePing(RING_DEVICE_PING_CMD_LO[i % RING_DEVICE_PING_CMD_LO.length]); - } - } - - // Two more ways to have not really completed, both distinguishable from - // "completed but empty": - // - // - the link dropped while the loop was running, so the later types - // were written into nothing; - // - the ring answered NONE of the five. A type with no backlog still - // RSPs (measured: five REQs, five RSPs, zero DATA), so zero RSPs - // across the whole sequence means the ring was not answering at - // all, not that there was nothing to send. - // - // An RSP with no DATA remains ambiguous per type and is NOT treated as - // a failure here - that is the ordinary "nothing new" case. - boolean stillConnected; - synchronized (lock) { - stillConnected = ringConnected; - } - if (!stillConnected || rspCount == 0) { - logLine("ring health: pull ABORTED - ran all " + commands.length + " types but " - + (stillConnected ? "the ring answered none of them" : "the link dropped")); - return false; - } - logLine("ring health: requested " + commands.length + " record types, " - + rspCount + " answered"); - return true; - } - - /** Block (worker thread) until the health RSP flag is set or the timeout - * elapses. Not correlated to a specific command/seq - like the rest of - * this protocol's request path, it trusts the ring answers in order. */ - private boolean awaitRingHealthRsp(long timeoutMs) { - long deadline = SystemClock.elapsedRealtime() + timeoutMs; - while (true) { - // A DATA notification may precede its RSP. Writes must happen - // outside lock so GATT callbacks can continue delivering pages. - if (flushRingOutbound() < 0) return false; - synchronized (lock) { - if (!running || !ringConnected) return false; - if (ringHealthRspSeen) return true; - if (!ringOutbound.isEmpty()) continue; - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - return false; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - } - } - } - - /** Block (worker thread) until DATA pages go idle for {@code idleMs}, - * extending the window on every new page so a real multi-page backlog - * transfer isn't cut short. Not filtered by health type - correct given - * the ring only has one type in flight at a time (see the class doc - * above), so any page arriving here belongs to the type just requested. */ - private boolean awaitRingHealthDataIdle(long idleMs) { - long idleDeadline = SystemClock.elapsedRealtime() + idleMs; - int lastSeenCounter = -1; - while (true) { - // The next page can depend on this ACK. Waiting for idle first - // mistakes an ACK-paced transfer for a completed transfer. - int acknowledged = flushRingOutbound(); - if (acknowledged < 0) return false; - synchronized (lock) { - if (!running || !ringConnected) return false; - if (acknowledged > 0 || ringHealthPageCounter != lastSeenCounter) { - lastSeenCounter = ringHealthPageCounter; - // Start the next-page window AFTER the blocking ACK write. - idleDeadline = SystemClock.elapsedRealtime() + idleMs; - } - // A callback may have queued a page between flush and lock. - if (!ringOutbound.isEmpty()) continue; - long remaining = idleDeadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) return true; - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - } - } - } - - /** - * Fire one bare device-channel REQ in the gap between health types, - * matching (without waiting on a response) the cadence a real Even sync - * uses. See {@link #RING_DEVICE_PING_CMD_LO} for provenance and caveats. - */ - private void sendRingDevicePing(int cmdLo) { - byte[] frame; - synchronized (lock) { - int seq = nextRingSeqLocked(); - int nonce = nextRingNonceLocked(); - if (cmdLo == 0x0a) { - // The only one of these four that isn't a bare nonce - it - // carries the same opaque constant blob as the handshake's - // own 0x0a frame (confirmed byte-identical across two - // different nights, see sendRingHandshake()'s comments). - frame = RingProtocol.buildFrame(RingProtocol.CHAN_DEVICE, RingProtocol.KIND_REQ, 0x00, 0x0a, seq, - concatBytes(le16(nonce), parseHex("f8f53d235ac4ceaa073885cc"))); - } else { - frame = RingProtocol.buildRequest(RingProtocol.CHAN_DEVICE, 0x00, cmdLo, seq, nonce); - } - } - writeRingFrame(frame, "device ping 0x" + Integer.toHexString(cmdLo)); - } - - /** Write one already-built ring frame. Worker-thread only (blocking). */ - private boolean writeRingFrame(byte[] frame, String what) { - try { - boolean ok = bleManager.writeFrames( - ringAddress, - BleProtocol.R1_WRITE_CHAR_UUID, - Collections.singletonList(frame), - ConnectionOptions.WRITE_TYPE, - ConnectionOptions.WRITE_TIMEOUT_MS - ); - if (!ok) { - logLine("ring " + what + " write failed"); - } - return ok; - } catch (Throwable t) { - logLine("ring " + what + " write error: " + safeMessage(t)); - return false; - } - } - - /** - * Drain queued ring frames (page ACKs). Worker-thread only. Returns the - * number written, or -1 if the link is unavailable or a write failed. - */ - private int flushRingOutbound() { - int acknowledged = 0; - while (true) { - byte[] frame; - synchronized (lock) { - if (!ringNotificationsReady) return -1; - if (ringOutbound.isEmpty()) return acknowledged; - frame = ringOutbound.peek(); - } - if (!writeRingFrame(frame, "page ack")) return -1; - synchronized (lock) { - // Keep failed writes queued for retry. A disconnect can clear - // the queue during the write, so remove this exact frame only. - ringOutbound.remove(frame); - acknowledged++; - } - } - } - - private int nextRingSeqLocked() { - ringSeq = (ringSeq + 1) & 0xff; - if (ringSeq == 0) { - ringSeq = 1; - } - return ringSeq; - } - - private int nextRingNonceLocked() { - ringNonce = (ringNonce + 1) & 0xffff; - return ringNonce; - } - - /** - * A snapshot of the health buffer together with the watermark identifying - * it. - * - *

The two have to come out under ONE lock. Reading the list and the - * count separately would let a page land in between, and the consumer would - * then clear a record nothing had ingested - the exact failure the - * watermark exists to prevent. - */ - public static final class RingHealthBatch { - private final List records; - private final long watermark; - - RingHealthBatch(List records, long watermark) { - this.records = records; - this.watermark = watermark; - } - - public List getRecords() { - return records; - } - - /** Total-ever-added at the instant of the snapshot. */ - public long getWatermark() { - return watermark; - } - } - - /** - * Snapshot of everything the ring has pushed and not yet been consumed, - * with the watermark needed to hand it back as consumed. - */ - public RingHealthBatch takeRingHealthBatch() { - synchronized (lock) { - return new RingHealthBatch(new ArrayList<>(ringHealthRecords), ringHealthTotalAdded); - } - } - - /** - * Drop every held record whose identity is below {@code watermark} - that - * is, everything the caller has durably stored. - * - *

"Cut and paste instead of copy paste. If you didn't receive the pull - * then it should not delete yet." Call this ONLY after the store write has - * succeeded; a caller that dies before calling it simply sees the same - * records again, which is a no-op at the store. - * - *

Records that arrived after the snapshot sit above the watermark and - * are left alone, so a page landing during an ingest is never lost. If the - * buffer evicted its oldest in the meantime (see - * {@link #RING_HEALTH_MAX_RECORDS}) the arithmetic accounts for it via - * {@code ringHealthTotalAdded} rather than clearing the wrong end. - * - * @return how many records were actually removed. - */ - public int clearRingHealthRecordsBelow(long watermark) { - synchronized (lock) { - long firstHeldIndex = ringHealthTotalAdded - ringHealthRecords.size(); - long toRemove = watermark - firstHeldIndex; - if (toRemove <= 0) { - return 0; - } - if (toRemove > ringHealthRecords.size()) { - toRemove = ringHealthRecords.size(); - } - ringHealthRecords.subList(0, (int) toRemove).clear(); - return (int) toRemove; - } - } - - /** - * Snapshot of everything the ring has pushed this session, WITHOUT - * consuming it. - * - *

Kept for callers that only want to look. The ingest path must use - * {@link #takeRingHealthBatch()} instead - a pure copy is what let the - * buffer grow unbounded and every tick re-process the whole session. - */ - public List getRingHealthRecords() { - synchronized (lock) { - return new ArrayList<>(ringHealthRecords); - } - } - - private boolean enableRingNotification(String characteristicUuid) { - try { - return bleManager.enableNotifications( - ringAddress, - characteristicUuid, - true, - ConnectionOptions.DESCRIPTOR_TIMEOUT_MS - ); - } catch (Throwable t) { - Log.d(TAG, "direct ring notify subscribe skipped: " + characteristicUuid + " " + safeMessage(t)); - return false; - } - } - - /** - * Complete the sid-0x80 security-auth exchange on both freshly opened arm - * connections. Firmware 2.2.9 answers no queries until it completes over an - * encrypted link and closes unauthenticated links after ~30 s (see - * ../notes/ble-connections-2.2.9.md); on an unbonded phone the exchange is - * also what triggers SMP pairing. Deliberately soft: on timeout we log and - * continue rather than fail the connect — the custom firmware's response - * behavior is not yet hardware-verified, and on stock firmware an - * unanswered auth just means the prelude fails exactly as it did before. - * A pairing prompt accepted after our window still bonds at the OS level, - * so the next reconnect attempt authenticates promptly. - */ - private void authenticateArms() throws InterruptedException { - OutboundMessage right = messageBuilder.securityAuth(false); - OutboundMessage left = messageBuilder.securityAuth(true); - long now = SystemClock.elapsedRealtime(); - for (OutboundMessage message : new OutboundMessage[] {right, left}) { - message.onAck = () -> { - }; - message.onTimeout = () -> { - }; - message.sentAtMs = now; - writeMessage(message); - } - long deadline = SystemClock.elapsedRealtime() + ConnectionOptions.SECURITY_AUTH_SOFT_TIMEOUT_MS; - while (running && !userDisconnectRequested && !inFlightMessages.isEmpty()) { - synchronized (lock) { - if (!running || userDisconnectRequested || inFlightMessages.isEmpty()) { - break; - } - } - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - interruptibleSleep.sleep(Math.min(remaining, 100)); - } - synchronized (lock) { - if (!inFlightMessages.isEmpty()) { - clearInFlightMessagesLocked("security auth timeout"); - logLine("security auth not acknowledged; continuing (2.2.9 stock requires it; older/custom firmware may not answer)"); - return; - } - } - boolean rightOk = BleProtocol.isAuthenticationSuccess(right.ackPayload, right.magic); - boolean leftOk = BleProtocol.isAuthenticationSuccess(left.ackPayload, left.magic); - logLine("security auth R=" + (rightOk ? "ok" : "unconfirmed") + " L=" + (leftOk ? "ok" : "unconfirmed")); - } - - private void sendPrelude() throws InterruptedException { - sendPrelude(false); - } - - private void sendPrelude(boolean preserveWakeLeaseControls) throws InterruptedException { - synchronized (lock) { - if (preserveWakeLeaseControls) { - clearAllMessagesPreservingWakeLeaseLocked("prelude"); - } else { - clearAllMessagesLocked("prelude"); - } - } - long now = SystemClock.elapsedRealtime(); - OutboundMessage prelude = messageBuilder.prelude(); - prelude.onAck = () -> { - }; - prelude.onTimeout = () -> { - handleTransportFailure("ack timeout"); - }; - prelude.sentAtMs = now; - writeMessage(prelude); - - long deadline = SystemClock.elapsedRealtime() + ConnectionOptions.PRELUDE_TIMEOUT_MS; - while (running && !userDisconnectRequested && !inFlightMessages.isEmpty()) { - synchronized (lock) { - if (!running || userDisconnectRequested || inFlightMessages.isEmpty()) { - break; - } - } - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - interruptibleSleep.sleep(Math.min(remaining, 100)); - } - synchronized (lock) { - if (!inFlightMessages.isEmpty()) { - clearInFlightMessagesLocked("prelude timeout"); - throw new IllegalStateException("prelude ack timeout"); - } - } - } - - private long driveSession() { - //Log.d(TAG, "driveSession called (pendingMessages.size=" + pendingMessages.size() + " inFlightMessages.size=" + inFlightMessages.size() + ")"); - while (true) { - // Run on the sender thread, outside `lock`: Bluetooth API calls can - // wait on the global GATT lock, and callbacks need the session lock. - // Keep negotiation outside the timed stream. A fixed settling period - // is only an experiment boundary; HCI must confirm actual parameters. - int linkMode = -1; - String[] benchmarkAddresses = null; - synchronized (lock) { - if (benchmarkActive && benchmarkLinkPending) { - benchmarkLinkPending = false; - linkMode = benchmarkLinkMode; - benchmarkAddresses = new String[] {leftAddress, rightAddress}; - } - } - if (benchmarkAddresses != null) { - for (String address : benchmarkAddresses) { - try { - bleManager.prepareBenchmarkLink(address, linkMode); - } catch (RuntimeException error) { - logLine("benchmark link request failed: " + safeMessage(error)); - } - } - synchronized (lock) { - // Cancellation/new-run races leave the new run pending; its - // own preparation will replace this deadline before sending. - benchmarkReadyAtMs = SystemClock.elapsedRealtime() + 1_000; - } - } - OutboundMessage messageToWrite = null; - OutboundMessage messageToPrewrite = null; - long now = SystemClock.elapsedRealtime(); - - maybeFinishNoChangeDesiredFrame(); - - synchronized (lock) { - drainCfwAcknowledgementsLocked(); - if (cfwCleanupDelivered) { - /* Successful mode 11 must be the last Faceclaw write. Drop - * anything a late external producer attempted to enqueue - * while DashboardController was closing the transport. */ - clearPendingMessagesLocked("after CFW cleanup"); - return 250; - } - if (sessionReady - && now - lastFaceclawFramebufferLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS - && !hasPendingOrInflightKindLocked("framebuffer-lease-control")) { - enqueueFaceclawFramebufferControlLocked( - BleProtocol.FACECLAW_FB_OP_ACQUIRE, - false - ); - } - if (faceclawWakeLeaseEnabled - && sessionReady - && now - lastFaceclawWakeLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS - && !hasPendingOrInflightKindLocked("wake-lease-control")) { - enqueueFaceclawWakeControlLocked( - BleProtocol.FACECLAW_WAKE_OP_ACQUIRE, - 0, - false - ); - } - if (!inFlightMessages.isEmpty()) { - OutboundMessage oldest = inFlightMessages.peekFirst(); - List replay = CfwMessageWindow.replayWindow(inFlightMessages, now); - if (!replay.isEmpty()) { - logLine("CFW recovery: replay " + replay.size() - + " unresolved message(s) from id=" + replay.get(0).magic); - for (OutboundMessage candidate : replay) { - if (candidate.cfwRetries >= CfwMessageWindow.MAX_RETRIES) { - handleTransportFailure("CFW recovery retry limit"); - return 0; - } - logLine("CFW replay id=" + candidate.magic + " label=" + candidate.label - + " cause=" + (candidate.cfwRetryPending ? "NACK" - : candidate.cfwAckLenses == CfwTransport.BOTH ? "ordered tail" : "missing ACK") - + " ackedLenses=" + candidate.cfwAckLenses - + " ageMs=" + (now - candidate.sentAtMs)); - inFlightMessages.remove(candidate); - magicPool.release(candidate.sid, candidate.magic, candidate.label, "CFW replay"); - candidate.prepareCfwReplay(magicPool.allocate()); - } - for (int i = replay.size() - 1; i >= 0; --i) pendingMessages.addFirst(replay.get(i)); - return 0; - } - if (oldest != null && oldest.ackDeadlineAtMs <= now) { - Log.i(TAG, "message timed out: " + oldest.label); - inFlightMessages.removeFirst(); - logLine("message timed out: " + oldest.label + " sid=" + oldest.sid - + " id=" + oldest.magic + " ackedLenses=" + oldest.cfwAckLenses - + " ageMs=" + (now - oldest.sentAtMs)); - magicPool.release(oldest.sid, oldest.magic, oldest.label, "timeout"); - if (oldest.sid == CfwTransport.SID) - cfwTransports[oldest.isLeftArmMessage ? 0 : 1].reset(); - handleAckTimeoutLocked(oldest); - return 0; - } - if (connectionOptions.WINDOW_SIZE <= 1 - && sessionReady - && prewrittenMessage == null - && !pendingMessages.isEmpty() - && canPrewriteCandidate(pendingMessages.peekFirst()) - && !shouldBlockPrewriteForHeartbeatLocked(now)) { - // Only the serial (window==1) path pre-sends the all-but-last - // packet; with a real window we just send the next message fully. - messageToPrewrite = pendingMessages.peekFirst(); - } - } - - if (chargingMode) { - // Glasses are in the case: no display traffic, only battery - // polls (which also detect the end of charging). - finishDesiredFrameLocked("discarded: glasses charging"); - if (sessionReady && inFlightMessages.isEmpty() && !pendingMessages.isEmpty()) { - messageToWrite = pendingMessages.removeFirst(); - Log.i(TAG, "sending pending message (charging): " + messageToWrite.label); - } else if (sessionReady && pendingMessages.isEmpty() && inFlightMessages.isEmpty() - && now - lastBatteryRefreshAtMs >= ConnectionOptions.CHARGING_BATTERY_POLL_MS) { - Log.i(TAG, "Writing charging-mode battery poll"); - messageToWrite = createBatteryQueryMessageLocked(); - lastBatteryRefreshAtMs = now; - } else { - return 1_000; - } - } else { - if (messageToPrewrite == null - && !shutdownRequested - && !fixedLayoutCreated - && pendingMessages.isEmpty() - && inFlightMessages.isEmpty()) { - Log.i(TAG, "enqueueing create layout"); - enqueueCreateLayoutLocked(); - } else if (messageToPrewrite != null) { - // Prewrite outside the lock; the logical message remains pending until - // its final BLE frame is sent after the current protocol ACK. - } - if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated - && (firmwareDebugFlagsEnabled ? 2 : 1) != firmwareDebugFlagsLastSent - && pendingMessages.isEmpty() && inFlightMessages.isEmpty()) { - Log.i(TAG, "enqueueing firmware debug flags " + (firmwareDebugFlagsEnabled ? "show" : "hide")); - enqueueFirmwareDebugFlagsLocked(); - } - - if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated - && (compassOwners.isEmpty() ? 0 : 1) != compassControlLastSent - && pendingMessages.isEmpty() && inFlightMessages.isEmpty()) { - boolean wanted = !compassOwners.isEmpty(); - Log.i(TAG, "enqueueing compass " + (wanted ? "enable" : "disable")); - enqueueCompassControlLocked(false, wanted); - } - - if (benchmarkActive) { - maintainBenchmarkLocked(now); - } - - // Up to WINDOW_SIZE messages may be in flight at once (full - // pipelining); a slot frees when an ack arrives. An active - // bandwidth benchmark measures its own selected window instead. - boolean windowHasRoom = inFlightMessages.size() - < Math.max(1, benchmarkActive ? benchmarkWindowSize : connectionOptions.WINDOW_SIZE); - // A frame ready to send right now: don't inject a fresh - // heartbeat in front of it (the image's own ack resets the - // firmware heartbeat timer, so the heartbeat is redundant). - // "Ready" covers both a fresh composite still to be planned - // and a planned image already queued but not yet written -- - // enqueueing sets lastEnqueuedFingerprint, so testing only - // the desired fingerprint left the queued-but-unwritten - // window unprotected and a heartbeat that came due there - // cost the frame a full ack round trip (measured 124ms on - // frame#232 of the 2026-08-20 02:27 capture). - // A benchmark run counts as image traffic here for the same - // reason: its acks reset the firmware heartbeat timer, so a - // fresh heartbeat in front of it is redundant. - boolean imageWaiting = !shutdownRequested && fixedLayoutCreated - && !hasPendingOrInflightKindLocked("heartbeat") - && (benchmarkActive - || (now >= imageRetryAfterMs - && (hasPendingImageLocked() - || !getDesiredFingerprint().equals(lastEnqueuedFingerprint)))); - noteImageStallLocked(now, windowHasRoom); - if (messageToPrewrite == null && handleHeartbeat(imageWaiting)) { - return ConnectionOptions.IDLE_SLEEP_MS; - } - - if (messageToPrewrite == null && sessionReady && windowHasRoom && !pendingMessages.isEmpty()) { - messageToWrite = pendingMessages.removeFirst(); - Log.i(TAG, "sending pending message: " + messageToWrite.label); - } else if (messageToPrewrite == null && !shutdownRequested && !benchmarkActive - && fixedLayoutCreated - && windowHasRoom && !hasPendingImageLocked() - && now >= imageRetryAfterMs - && !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) { - // Enqueue the next frame's delta against lastEnqueuedPacked - // (what the shadow will be), so it can pipeline behind an - // image still awaiting its ack. - Log.i(TAG, "Enqueued image update"); - enqueueDesiredImageLocked(); - return 0; - } else if (messageToPrewrite == null && shouldPollBatteryLocked(now)) { - Log.i(TAG, "Writing battery query"); - messageToWrite = createBatteryQueryMessageLocked(); - lastBatteryRefreshAtMs = now; - } else if (messageToPrewrite == null && (!pendingMessages.isEmpty() || !inFlightMessages.isEmpty())) { - return ConnectionOptions.IDLE_SLEEP_MS; - } else if (messageToPrewrite == null) { - return 250; - } - } - } - - if (messageToPrewrite != null) { - if (prewriteMessage(messageToPrewrite)) { - return 0; - } - return ConnectionOptions.IDLE_SLEEP_MS; - } - - if (!writeMessage(messageToWrite)) { - synchronized (lock) { - if (removePreparedMessageLocked(messageToWrite) || messageToWrite.magic == 0) { - handleTransportFailure("write failed"); - } - } - return 0; - } - } - } - - /** - * If the desired image already matches what the glasses display, nothing will - * ever be enqueued for it, so finish its frame now (otherwise the TS side - * would block on it until its backpressure timeout). - */ - private void maybeFinishNoChangeDesiredFrame() { - int frameIdToFinish = 0; - synchronized (lock) { - synchronized (desiredTilesLock) { - if (desiredFrameId != 0 && !lastEnqueuedFingerprint.isEmpty() && desiredFingerprint.equals(lastEnqueuedFingerprint)) { - frameIdToFinish = desiredFrameId; - desiredFrameId = 0; - } - } - } - if (frameIdToFinish != 0) { - finishFrame(frameIdToFinish, "discarded: no change from displayed image"); - } - } - - private boolean shouldBlockPrewriteForHeartbeatLocked(long now) { - if (shutdownRequested || !fixedLayoutCreated) { - return false; - } - return hasPendingOrInflightKindLocked("heartbeat") - || now - lastHeartbeatAckedAtMs >= ConnectionOptions.HEARTBEAT_READY_MS; - } - - private boolean canPrewriteCandidate(OutboundMessage message) { - if (message == null || message.sid == CfwTransport.SID || !message.isLeftArmMessage) { - return false; - } - if (!"image".equals(message.kind)) { - return false; - } - return message.message.length + 2 > 232; - } - - private boolean handleHeartbeat(boolean imageWaiting) { - long now = SystemClock.elapsedRealtime(); - boolean heartbeatEligible = !shutdownRequested && fixedLayoutCreated; - boolean heartbeatPending = heartbeatEligible && hasPendingOrInflightKindLocked("heartbeat"); - long heartbeatElapsedMs = now - lastHeartbeatAckedAtMs; - boolean heartbeatReady = heartbeatEligible && heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_READY_MS; - boolean heartbeatUrgent = heartbeatEligible && heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_URGENT_MS; - boolean heartbeatBlocksLeftWrites = heartbeatReady || heartbeatPending; - - if (heartbeatReady && !heartbeatPending && inFlightMessages.isEmpty()) { - if (imageWaiting && !heartbeatUrgent) { - // Defer to the waiting frame: sending it now satisfies the - // firmware heartbeat deadline (its ack resets the timer), and - // the heartbeat still fires once we reach the URGENT threshold - // if rendering goes quiet again. Preserves the pending-heartbeat - // inter-lens-sync invariant below (that path is untouched). - return false; - } - Log.i(TAG, "Writing heartbeat"); - OutboundMessage heartbeatMessage = createHeartbeatMessage(); - lastHeartbeatSentAtMs = now; - writeMessage(heartbeatMessage); - return true; - } else if (heartbeatUrgent) { - return true; - } else if (heartbeatPending) { - // Don't send other message types while a heartbeat is pending because that - // can lead to inter-lens sync issues - return true; - } - - return false; - } - - private OutboundMessage createHeartbeatMessage() { - OutboundMessage message = messageBuilder.heartbeat(); - message.onAck = () -> { - synchronized (lock) { - lastHeartbeatAckedAtMs = SystemClock.elapsedRealtime(); - } - }; - message.onTimeout = () -> { - // If a heartbeat fails to ack and we're over the heartbeat deadline, assume the connection is failed and reconnect. - // Otherwise ignore it, which will cause a retransmission attempt. - boolean isPastDeadline; - synchronized (lock) { - isPastDeadline = SystemClock.elapsedRealtime() - lastHeartbeatSentAtMs >= ConnectionOptions.HEARTBEAT_FAILURE_DEADLINE_MS; - } - if (isPastDeadline) { - handleTransportFailure("heartbeat ack timeout"); - } - }; - return message; - } - - - private boolean writeMessage(OutboundMessage message) { - long now = SystemClock.elapsedRealtime(); - message.writeStartedAtMs = now; - message.sentAtMs = now; - message.ackDeadlineAtMs = now + message.ackTimeoutMs + ConnectionOptions.WRITE_TIMEOUT_MS; - if (message.magic != 0) { - synchronized (lock) { - inFlightMessages.addLast(message); - } - } - if (message.imageUpdateId > 0 && message.imageMessageNumber == 1) { - synchronized(lock) { - BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId); - if (stats != null && stats.firstWriteStartedAtMs <= 0) { - stats.firstWriteStartedAtMs = now; - } - } - } - - String writeAddress = message.isLeftArmMessage ? leftAddress : rightAddress; - List frames; - if (prewrittenMessage != null && prewrittenMessage != message) { - if (!spoilPrewrittenMessage("before " + message.label)) { - return false; - } - } - if (prewrittenMessage == message) { - frames = Collections.singletonList(prewrittenFrames.get(prewrittenFrames.size() - 1)); - prewrittenMessage = null; - prewrittenFrames = Collections.emptyList(); - } else if (message.sid == CfwTransport.SID) { - if (message.cfwRetries > 0) cfwTransports[message.isLeftArmMessage ? 0 : 1].reset(); - frames = cfwTransports[message.isLeftArmMessage ? 0 : 1].encode( - message.message, message.magic, CfwTransport.BOTH, - bleManager.getNegotiatedMtu(writeAddress)); - } else { - frames = BleProtocol.framePb( - message.message, - message.sid, - message.flag, - nextTransportSeq++ - ); - } - boolean result = bleManager.writeFrames( - writeAddress, - BleProtocol.WRITE_CHAR_UUID, - frames, - ConnectionOptions.WRITE_TYPE, - ConnectionOptions.WRITE_TIMEOUT_MS - ); - - synchronized (lock) { - long sentAtMs = SystemClock.elapsedRealtime(); - message.sentAtMs = sentAtMs; - message.ackDeadlineAtMs = sentAtMs + message.ackTimeoutMs; - logImageUpdateSendLandmarkLocked(message); - if (result && message.onSent != null) { - message.onSent.run(); - lock.notifyAll(); - } - } - - if (!result && message.sid == CfwTransport.SID) - cfwTransports[message.isLeftArmMessage ? 0 : 1].reset(); - return result; - } - - private boolean prewriteMessage(OutboundMessage message) { - if (prewrittenMessage == message) { - return true; - } - if (prewrittenMessage != null && !spoilPrewrittenMessage("before prewrite " + message.label)) { - return false; - } - if (!canPrewriteCandidate(message)) { - return false; - } - - List frames = BleProtocol.framePb( - message.message, - message.sid, - message.flag, - nextTransportSeq++ - ); - if (frames.size() <= 1) { - return false; - } - - String writeAddress = message.isLeftArmMessage ? leftAddress : rightAddress; - List prefixFrames = frames.subList(0, frames.size() - 1); - boolean result = bleManager.writeFrames( - writeAddress, - BleProtocol.WRITE_CHAR_UUID, - prefixFrames, - ConnectionOptions.WRITE_TYPE, - ConnectionOptions.WRITE_TIMEOUT_MS - ); - if (!result) { - return false; - } - - prewrittenMessage = message; - prewrittenFrames = new ArrayList<>(frames); - logLine("prewrote " + message.label + " frames=" + prefixFrames.size() + "/" + frames.size()); - return true; - } - - private boolean spoilPrewrittenMessage(String reason) { - if (prewrittenMessage == null || prewrittenFrames.isEmpty()) { - prewrittenMessage = null; - prewrittenFrames = Collections.emptyList(); - return true; - } - OutboundMessage message = prewrittenMessage; - byte[] finalFrame = Arrays.copyOf( - prewrittenFrames.get(prewrittenFrames.size() - 1), - prewrittenFrames.get(prewrittenFrames.size() - 1).length - ); - if (finalFrame.length > 8) { - finalFrame[finalFrame.length - 1] ^= (byte) 0xff; - } - prewrittenMessage = null; - prewrittenFrames = Collections.emptyList(); - - String writeAddress = message.isLeftArmMessage ? leftAddress : rightAddress; - logLine("spoiling prewritten " + message.label + ": " + reason); - return bleManager.writeFrames( - writeAddress, - BleProtocol.WRITE_CHAR_UUID, - Collections.singletonList(finalFrame), - ConnectionOptions.WRITE_TYPE, - ConnectionOptions.WRITE_TIMEOUT_MS - ); - } - - private boolean removePreparedMessageLocked(OutboundMessage message) { - if (message == null || message.magic == 0) { - return false; - } - Iterator iterator = inFlightMessages.iterator(); - while (iterator.hasNext()) { - if (iterator.next() == message) { - iterator.remove(); - magicPool.release(message.sid, message.magic, message.label, "write failed"); - return true; - } - } - return false; - } - - - /** Also drain on the worker: a clear/timeout may have removed a blocking - * head since the last notification. Fully ACKed controls must never reach - * the generic timeout path just because no further BLE reply arrived. */ - private void drainCfwAcknowledgementsLocked() { - while (true) { - OutboundMessage first = CfwMessageWindow.acknowledgedHead(inFlightMessages); - if (first == null) return; - lastAckAtMs = SystemClock.elapsedRealtime(); - resolveAckLocked(first, first.ackPayload); - } - } - - private void resolveAckLocked(int sid, int magic, byte[] pb) { - Iterator iterator = inFlightMessages.iterator(); - while (iterator.hasNext()) { - OutboundMessage message = iterator.next(); - if (message.sid == sid && message.magic == magic) { - resolveAckLocked(message, pb); - return; - } - } - recordUnexpectedAckLocked(sid, magic); - } - - private void resolveAckLocked(OutboundMessage message, byte[] pb) { - Log.i(TAG, "Got ACK for " + message.label + "(sid=" + message.sid + ", id=" + message.magic + ")"); - inFlightMessages.remove(message); - message.ackPayload = pb == null ? new byte[0] : Arrays.copyOf(pb, pb.length); - magicPool.release(message.sid, message.magic, message.label, "ack"); - if (message.onAck != null) { - message.onAck.run(); - } - consecutiveAckTimeouts = 0; - // onAck may have just satisfied a waiter blocked on lock.wait() (e.g. - // awaitEvenHubSessionReady polling fixedLayoutCreated/displayedFingerprint - // after a create-layout or image ack). Without this, that waiter only - // notices on its own up-to-100ms poll tick, adding avoidable latency to - // every EvenHub wake. Always called with lock held (see call site). - lock.notifyAll(); - } - - private void logImageUpdateSendLandmarkLocked(OutboundMessage message) { - if (message.imageUpdateId <= 0) { - return; - } - BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId); - int frameId = stats == null ? 0 : stats.frameId; - if (message.imageMessageNumber == 1) { - if (stats != null && stats.firstWriteStartedAtMs <= 0) { - stats.firstWriteStartedAtMs = message.writeStartedAtMs > 0 ? message.writeStartedAtMs : message.sentAtMs; - } - FrameTimings.getInstance().log(frameId, "first bluetooth packet sent"); - logImageUpdateLandmarkLocked("first bluetooth message sent", message, message.sentAtMs); - } - if (message.imageMessageNumber == message.imageMessageCount) { - FrameTimings.getInstance().log(frameId, - "last bluetooth packet sent (message " + message.imageMessageNumber + "/" + message.imageMessageCount + ")"); - logImageUpdateLandmarkLocked("last bluetooth message sent", message, message.sentAtMs); - } - } - - private void logImageUpdateAckLandmarkLocked(OutboundMessage message) { - if (message.imageUpdateId <= 0 || message.imageMessageNumber != message.imageMessageCount) { - return; - } - long ackedAtMs = SystemClock.elapsedRealtime(); - FaceclawBleManager.recordDisplayFrameSent(); - BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.remove(message.imageUpdateId); - if (stats != null && stats.firstWriteStartedAtMs > 0) { - emitFrameMetrics(stats.paintMs, (int) Math.max(0, ackedAtMs - stats.firstWriteStartedAtMs), stats.tileCount); - } - if (stats != null) { - finishFrame(stats.frameId, "sent"); - } - logImageUpdateLandmarkLocked("last bluetooth message acked", message, ackedAtMs); - } - - /** Remove the stats entry for an image update that will not complete, finishing its frame. */ - private void discardImageUpdateStatsLocked(int imageUpdateId, String reason) { - if (imageUpdateId <= 0) { - return; - } - BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.remove(imageUpdateId); - if (stats != null) { - finishFrame(stats.frameId, "discarded: " + reason); - } - } - - private void logImageUpdateLandmarkLocked(String event, OutboundMessage message, long elapsedMs) { - logLine("image update#" + message.imageUpdateId + " " + event - + " at " + timestamp(elapsedMs) - + " message=" + message.imageMessageNumber + "/" + message.imageMessageCount - + " label=" + message.label); - } - - private void enqueueCreateLayoutLocked() { - // New session: re-assert the firmware-debug-flags overlay once - // the layout is ready (the mode-7 send is gated on this having reset). - firmwareDebugFlagsLastSent = -1; - OutboundMessage message = messageBuilder.createLayout(); - message.onAck = () -> { - startupProbePending = false; - clearMessagesOfKindLocked("startup-text-probe"); - fixedLayoutCreated = true; - displayedFingerprint = ""; - }; - message.onTimeout = () -> { - if (startupProbePending) { - logLine("create layout timed out while startup text probe is pending"); - if (hasPendingOrInflightKindLocked("startup-text-probe")) { - return; - } - startupProbePending = false; - } - handleTransportFailure("ack timeout"); - }; - pendingMessages.addLast(message); - logLine("queue create layout"); - } - - private void enqueueStartupProbeLocked() { - enqueueCreateLayoutLocked(); - - OutboundMessage message = messageBuilder.startupTextProbe(); - message.onAck = () -> { - startupProbePending = false; - clearMessagesOfKindLocked("create-layout"); - fixedLayoutCreated = true; - displayedFingerprint = ""; - logLine("existing dashboard layout accepted text probe"); - }; - message.onTimeout = () -> { - startupProbePending = false; - if (hasPendingOrInflightKindLocked("create-layout")) { - return; - } - handleTransportFailure("ack timeout"); - }; - pendingMessages.addLast(message); - startupProbePending = true; - logLine("queue startup text probe"); - } - - /** - * Send the CFW mode-7 diagnostic-flag control op through the private stream: - * [7][2] to show the on-glasses debug-flag overlay, [7][1] to hide it. Uses the - * arbitrary-payload custom path (no bmp/dedup/frame-timing interaction). - */ - private void enqueueFirmwareDebugFlagsLocked() { - boolean show = firmwareDebugFlagsEnabled; - int sub = show ? 2 : 1; - byte[] payload = new byte[] { (byte) 7, (byte) sub }; - OutboundMessage message = messageBuilder.imagePayload( - DASHBOARD_TILE, nextMapSessionId(), payload, - "fw-debug-flags " + (show ? "show" : "hide"), - connectionOptions.sendImagesToLeft); - pendingMessages.addLast(message); - firmwareDebugFlagsLastSent = sub; - logLine("queue firmware debug flags " + (show ? "show" : "hide")); - } - - /** - * Send CFW image-handler mode 10. Enable uses the configurable form - * [10][2][interval-ms LE16][minimum-change-degrees LE16]; disable remains [10][0]. - */ - private void enqueueCompassControlLocked(boolean priority, boolean enable) { - int sentState = enable ? 1 : 0; - byte[] payload = enable - ? new byte[] { - (byte) 10, - (byte) 2, - (byte) (COMPASS_REPORT_INTERVAL_MS & 0xff), - (byte) ((COMPASS_REPORT_INTERVAL_MS >> 8) & 0xff), - (byte) (COMPASS_MIN_CHANGE_DEGREES & 0xff), - (byte) ((COMPASS_MIN_CHANGE_DEGREES >> 8) & 0xff), - } - : new byte[] { (byte) 10, (byte) 0 }; - OutboundMessage message = messageBuilder.imagePayload( - "compass-control", - DASHBOARD_TILE, - nextMapSessionId(), - payload, - "compass " + (enable ? "enable" : "disable"), - connectionOptions.sendImagesToLeft); - message.onTimeout = () -> { - compassControlLastSent = -1; - logLine("compass control ack timeout"); - }; - if (enable) { - compassMaybeOn = true; - } else { - message.onAck = () -> compassMaybeOn = false; - } - if (priority) pendingMessages.addFirst(message); - else pendingMessages.addLast(message); - compassControlLastSent = sentState; - logLine("queue " + message.label); - } - - private void enqueueDesiredImageLocked() { - String fingerprint = getDesiredFingerprint(); - byte[] packed; - int width; - int height; - int paintMs; - int frameId; - SurfaceCompositor.ScreenDraw[] draws; - synchronized (desiredTilesLock) { - packed = desiredPacked; - width = desiredWidth; - height = desiredHeight; - paintMs = desiredPaintMs; - frameId = desiredFrameId; - draws = desiredDraws; - desiredFrameId = 0; - } - if (packed == null) { - packed = new byte[0]; - } - if (lastEnqueuedWidth == width && lastEnqueuedHeight == height - && Arrays.equals(packed, lastEnqueuedPacked)) { - lastEnqueuedFingerprint = fingerprint; - finishFrame(frameId, "discarded: image content identical to displayed"); - return; - } - - // Texture-cache path: ship text as cached-glyph draws, punching their - // ink out of the baked deltas. Uploads (mode 12) ride ahead of the - // image message on the ordered transport. Falls through to the plain - // paths whenever the planner has nothing to draw. - if (customFirmwareDetected && connectionOptions.TEXTURE_CACHE_FRAMES - && draws != null && draws.length > 0 && packed.length > 0) { - byte[] deltaBase = (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedPacked.length > 0 - && lastEnqueuedWidth == width && lastEnqueuedHeight == height) - ? lastEnqueuedPacked : null; - FrameTimings.getInstance().spanStart(frameId, "texture-plan"); - TexturePlanner.Result tex = TexturePlanner.plan( - deltaBase, packed, width, height, draws, textureCache, - nextImageFrameId, - connectionOptions.MULTI_RECT_FRAMES, ConnectionOptions.MULTI_RECT_MAX_RECTS); - if (tex != null) { - nextImageFrameId = tex.nextFid; - for (byte[] upload : tex.uploads) { - enqueueTextureUploadLocked(upload); - } - BleImageOptimizer.TileImagePlan plan = new BleImageOptimizer.TileImagePlan( - 0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), tex.payload); - FrameTimings.getInstance().spanEnd(frameId, "texture-plan"); - String texLog = "texture update " + (tex.fullFrame ? "full" : ("rects=" + tex.rectCount)) - + " glyphs=" + tex.drawnGlyphs + " runs=" + tex.runCount - + " images=" + tex.drawnImages - + " fw=" + tex.fwGlyphs + "/" + tex.fwRuns - + " baked=" + tex.bakedCandidates - + (tex.uploadBytes > 0 ? " upload=" + tex.uploadBytes + "B" : "") - + " cache=" + textureCache.usedBytes() + "B" - + " payload=" + tex.payload.length + "B" - + " (rects " + tex.rectsMs + "ms, match " + tex.matchMs - + "ms, cache " + tex.cacheMs + "ms, punch " + tex.punchMs - + "ms, encode " + tex.encodeMs + "ms)"; - FrameTimings.getInstance().log(frameId, texLog); - finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId); - return; - } - FrameTimings.getInstance().spanEnd(frameId, "texture-plan"); - } - - FrameTimings.getInstance().spanStart(frameId, "compress-and-plan"); - // Incremental (mode 3 bounding box) update against the last ENQUEUED frame - // (the base the firmware shadow will hold when this update is applied). - // lastEnqueuedPacked is cleared whenever the image pipeline is cleared, so - // a non-empty value means the display base is trusted. - byte[] incrementalPayload = null; - String incrementalLog = null; - if (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedPacked.length > 0 - && lastEnqueuedWidth == width && lastEnqueuedHeight == height) { - int baseFid = nextImageFrameId; - BleImageOptimizer.IncrementalPlan single = - BleImageOptimizer.buildIncrementalImagePayload(lastEnqueuedPacked, packed, width, height, baseFid); - if (single != null) { - incrementalPayload = single.payload; - // advance only when a delta is actually emitted, so consecutive - // deltas carry consecutive ids (CFW skip/reorder detection) - nextImageFrameId = nextImageFrameId >= 0xfffe ? 1 : nextImageFrameId + 1; - incrementalLog = "incremental update bbox=" - + ((single.payload[3] & 0xff) * 4) + "x" + ((single.payload[4] & 0xff) * 2) - + "+" + ((single.payload[1] & 0xff) * 4) + "+" + ((single.payload[2] & 0xff) * 2) - + " changed=" + single.changedBytes + "/" + single.boxBytes + "B" - + " clusters=" + single.clusterCount; - - // When the bounding box spans multiple clusters or is sizeable, try - // splitting into tight rects (CFW mode-8). Only replace the single - // box if the multi-rect message is actually smaller on the wire. - if (connectionOptions.MULTI_RECT_FRAMES - && (single.clusterCount > 1 || single.payload.length > ConnectionOptions.MULTI_RECT_MIN_PAYLOAD)) { - BleImageOptimizer.MultiRectPlan multi = BleImageOptimizer.buildMultiRectImagePayload( - lastEnqueuedPacked, packed, width, height, baseFid, ConnectionOptions.MULTI_RECT_MAX_RECTS); - if (multi != null && multi.payload.length < single.payload.length) { - incrementalPayload = multi.payload; - nextImageFrameId = multi.nextFid; // rectCount fids consumed - incrementalLog = "multi-rect update n=" + multi.rectCount - + " covered=" + multi.coveredBytes + "B" - + " payload=" + multi.payload.length + "B (vs bbox " + single.payload.length + "B)"; - } - } - } - } - BleImageOptimizer.TileImagePlan plan = incrementalPayload != null - ? new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), incrementalPayload) - : new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, packed, width, height, nextMapSessionId()); - FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan"); - if (incrementalLog != null) { - FrameTimings.getInstance().log(frameId, incrementalLog); - } - finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId); - } - - /** Queue complete private commands, advance the delta base, and retain frame/ACK bookkeeping. */ - private void finishEnqueueDesiredImageLocked( - BleImageOptimizer.TileImagePlan plan, String fingerprint, int paintMs, int frameId) { - int updateId = nextImageUpdateId++; - List commands = new ArrayList<>(); - if (plan.payload.length <= CfwTransport.MAX_MESSAGE) { - commands.add(plan.payload); - } else { - // A noisy full frame can exceed the stream record limit. Repaint it - // with independently decodable RLE bands below the decoded-message limit. - commands.addAll(BleImageOptimizer.encodeFullFrameBands( - plan.packed, plan.width, plan.height, nextImageFrameId)); - for (int i = 0; i < commands.size(); i++) { - nextImageFrameId = nextImageFrameId >= 0xfffe ? 1 : nextImageFrameId + 1; - } - } - int messageCount = commands.size(); - imageUpdateStats.put(updateId, new BleImageOptimizer.ImageUpdateStats(paintMs, 1, frameId)); - for (int i = 0; i < commands.size(); i++) { - enqueueCustomImageLocked(plan, commands.get(i), fingerprint, updateId, i + 1, messageCount); - } - // This frame is now the base for the next delta (it will be the firmware - // shadow once applied), even though it hasn't been acked yet — that is what - // lets the next frame pipeline behind it. plan.packed is the full frame; - // frames are immutable by convention, so referencing it is safe. - lastEnqueuedPacked = plan.packed; - lastEnqueuedWidth = plan.width; - lastEnqueuedHeight = plan.height; - lastEnqueuedFingerprint = fingerprint; - - FrameTimings.getInstance().log(frameId, "queued image update#" + updateId - + " messages=" + messageCount + " payload=" + plan.payload.length + "B"); - logLine("queue image update#" + updateId + " fingerprint=" + fingerprint - + " messages=" + messageCount); - } - - /** - * Enqueue one mode-12 texture-cache upload ahead of the image message that - * references its glyphs (the transport is FIFO, so no ack round trip is - * needed before use). A timeout means the on-glasses cache state is - * unknown; forget everything phone-side (glyphs re-upload lazily) and let - * the accompanying image update's own timeout drive the frame resync. - */ - private void enqueueTextureUploadLocked(byte[] payload) { - OutboundMessage message = messageBuilder.imagePayload( - "texcache", - DASHBOARD_TILE, - nextMapSessionId(), - payload, - "texture upload " + payload.length + "B", - connectionOptions.sendImagesToLeft); - message.onTimeout = () -> { - textureCache.reset(); - logLine("texture upload ack timeout; texture cache state reset"); - }; - pendingMessages.addLast(message); - logLine("queue " + message.label); - } - - private void enqueueCustomImageLocked( - BleImageOptimizer.TileImagePlan plan, - byte[] payload, - String fingerprint, - int updateId, - int messageNumber, - int messageCount - ) { - OutboundMessage message = messageBuilder.customMessage("image", payload, - "image " + plan.tile.name + "#" + messageNumber, plan.tileIndex, connectionOptions.sendImagesToLeft); - message.setImageUpdatePosition(updateId, messageNumber, messageCount); - message.onAck = () -> { - imageRetryAfterMs = 0; - // Firmware >= 2.2.4.34 resets its heartbeat timer when it receives - // image messages (not just heartbeats), so an acked image fragment - // satisfies the heartbeat deadline and heartbeats stop contending - // with active rendering. - lastHeartbeatAckedAtMs = SystemClock.elapsedRealtime(); - logImageUpdateAckLandmarkLocked(message); - boolean imageStillInFlight = false; - for (OutboundMessage inFlight : inFlightMessages) { - if ("image".equals(inFlight.kind)) { - imageStillInFlight = true; - break; - } - } - if (!imageStillInFlight) { - boolean imageStillQueued = false; - for (OutboundMessage queued : pendingMessages) { - if ("image".equals(queued.kind)) { - imageStillQueued = true; - break; - } - } - if (!imageStillQueued) { - displayedFingerprint = fingerprint; - } - } - }; - message.onTimeout = () -> { - discardImageUpdateStatsLocked(message.imageUpdateId, "image ack timeout (will retry)"); - clearMessagesOfKindLocked("image"); - displayedFingerprint = ""; - imageRetryAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.IMAGE_RETRY_DELAY_MS; - }; - pendingMessages.addLast(message); - } - - private OutboundMessage createAudioControlMessageLocked(boolean enable) { - OutboundMessage message = messageBuilder.enableOrDisableMic(enable); - message.onAck = () -> { - lastAudioControlAckMagic = message.magic; - audioCaptureActive = message.label != null && message.label.contains("enable"); - logLine(audioCaptureActive ? "G2 mic enabled" : "G2 mic disabled"); - }; - message.onTimeout = () -> { - handleTransportFailure("audio control ack timeout"); - }; - return message; - } - - private boolean shouldPollBatteryLocked(long now) { - return !shutdownRequested - && sessionReady - && pendingMessages.isEmpty() - && inFlightMessages.isEmpty() - && now - lastConnectionOrInputAtMs >= ConnectionOptions.BATTERY_INPUT_QUIET_MS - && (lastBatteryRefreshAtMs == 0 || now - lastBatteryRefreshAtMs >= ConnectionOptions.BATTERY_REFRESH_INTERVAL_MS); - } - - private OutboundMessage createBatteryQueryMessageLocked() { - OutboundMessage message = messageBuilder.batteryQuery(); - message.onAck = () -> { - BleProtocol.RingBatterySnapshot ring = BleProtocol.parseRingBattery(message.ackPayload); - // Old firmware and missing/malformed extensions must clear any prior reading. - ringBattery = ring == null ? -1 : ring.battery; - ringCharging = ring == null ? -1 : ring.charging; - BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(message.ackPayload); - if (snapshot != null) { - headsetBattery = snapshot.battery; - headsetCharging = snapshot.charging; - emitBatteryState(headsetBattery, headsetCharging); - if (snapshot.silentMode >= 0) { - // Backstop for the push in onNotification: the firmware is - // confirmed to push silent-mode-on, but the off transition is - // not, so re-read the authoritative value on every poll. - updateSilentModeLocked(snapshot.silentMode > 0); - } - updateChargingModeLocked(snapshot.charging > 0, snapshot.battery); - } - BleProtocol.FirmwareInfo firmwareInfo = BleProtocol.parseSettingsFirmwareInfo(message.ackPayload); - if (firmwareInfo != null) { - customFirmwareDetected = firmwareInfo.isFaceclawFirmware(); - emitFirmwareInfo(firmwareInfo); - } - }; - message.onTimeout = () -> { - logLine("Battery query timed out"); - }; - return message; - } - - /** - * Track silent mode, which the wearer toggles by long-pressing both - * touchpads at once. While it is on the firmware refuses input events and - * app launches and powers the display down, so the glasses look dead even - * though the BLE session is healthy; the phone UI says so explicitly. - */ - private void updateSilentModeLocked(boolean silent) { - int next = silent ? 1 : 0; - if (silentMode == next) { - return; - } - silentMode = next; - logLine(silent ? "glasses entered silent mode" : "glasses left silent mode"); - emitSilentMode(silent); - } - - /** - * Track whether the glasses are in the charging case. Charging means nobody - * is wearing them: display communication pauses (no heartbeats, so the - * firmware tears down its EvenHub context on its own) and only battery polls - * continue. When charging stops, tear the transport down and let the normal - * reconnect loop rebuild the session, layout, and first frame. - */ - private void updateChargingModeLocked(boolean charging, int battery) { - if (charging == chargingMode) { - if (chargingMode) { - setStateDisplay("charging", chargingStatusText(battery)); - } - return; - } - if (charging) { - chargingMode = true; - clearAllMessagesLocked("glasses charging"); - fixedLayoutCreated = false; - startupProbePending = false; - displayedFingerprint = ""; - finishDesiredFrameLocked("discarded: glasses charging"); - logLine("glasses are charging; pausing display communication"); - setStateDisplay("charging", chargingStatusText(battery)); - } else { - chargingMode = false; - logLine("glasses removed from charger; reconnecting"); - handleTransportFailure("charging ended"); - } - } - - private static String chargingStatusText(int battery) { - return battery >= 0 - ? "Glasses charging. Battery " + battery + "%." - : "Glasses charging."; - } - - /** - * Record, into the frame that is waiting, why it did not go out on this - * pass of the send loop. Without this the export shows a bare multi-second - * jump between "image submitted as desired frame" and the first BLE - * packet, with no hint whether we were blocked on a heartbeat, the - * BLE window, or another message queued ahead. Deduped on (frame, reason), - * so a frame stalled for seconds gets one line per state change rather - * than one per loop pass. - */ - private void noteImageStallLocked(long now, boolean windowHasRoom) { - int frameId; - synchronized (desiredTilesLock) { - frameId = desiredFrameId; - } - String reason; - if (frameId != 0 && !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) { - reason = describeEnqueueBlockerLocked(now, windowHasRoom); - } else { - // Nothing waiting to be planned; an already-planned image may still - // be queued behind other traffic. - OutboundMessage queuedImage = firstPendingImageLocked(); - frameId = queuedImage == null ? 0 : imageUpdateFrameIdLocked(queuedImage.imageUpdateId); - reason = queuedImage == null ? null : describeWriteBlockerLocked(now, windowHasRoom, queuedImage); - } - if (frameId == 0 || reason == null) { - stallFrameId = 0; - stallReason = ""; - return; - } - if (frameId == stallFrameId && reason.equals(stallReason)) { - return; - } - stallFrameId = frameId; - stallReason = reason; - FrameTimings.getInstance().log(frameId, "waiting to send: " + reason); - } - - /** Why the desired composite has not been turned into wire messages yet, or null. */ - private String describeEnqueueBlockerLocked(long now, boolean windowHasRoom) { - if (shutdownRequested) { - return "shutdown requested"; - } - if (!sessionReady) { - return "BLE session not ready"; - } - if (!fixedLayoutCreated) { - return "display layout not created yet"; - } - if (now < imageRetryAfterMs) { - return "image retry backoff (" + (imageRetryAfterMs - now) + "ms left)"; - } - if (hasPendingImageLocked()) { - return "an earlier image is still queued"; - } - if (!windowHasRoom) { - return "BLE window full (" + inFlightMessages.size() + " message(s) in flight)"; - } - if (hasPendingOrInflightKindLocked("heartbeat")) { - return "heartbeat in flight"; - } - if (!pendingMessages.isEmpty()) { - return pendingMessages.size() + " message(s) queued ahead, next " - + pendingMessages.peekFirst().label; - } - return null; - } - - /** Why a planned image message has not been written to BLE yet, or null. */ - private String describeWriteBlockerLocked(long now, boolean windowHasRoom, OutboundMessage queuedImage) { - if (!sessionReady) { - return "BLE session not ready"; - } - if (!windowHasRoom) { - return "BLE window full (" + inFlightMessages.size() + " message(s) in flight)"; - } - if (hasPendingOrInflightKindLocked("heartbeat")) { - return "heartbeat in flight"; - } - // handleHeartbeat is a barrier: while one is due it holds back every - // other write, so a frame queued at the wrong moment waits a heartbeat - // round trip. Reported explicitly because it is otherwise invisible -- - // heartbeats belong to no frame. - long heartbeatElapsedMs = now - lastHeartbeatAckedAtMs; - if (fixedLayoutCreated && !shutdownRequested - && heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_READY_MS) { - return "heartbeat due (" + heartbeatElapsedMs + "ms since the last one acked)"; - } - OutboundMessage head = pendingMessages.peekFirst(); - if (head != null && head != queuedImage) { - return "queued behind " + head.label; - } - return null; - } - - private OutboundMessage firstPendingImageLocked() { - for (OutboundMessage message : pendingMessages) { - if (message.imageUpdateId > 0) { - return message; - } - } - return null; - } - - private int imageUpdateFrameIdLocked(int imageUpdateId) { - BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(imageUpdateId); - return stats == null ? 0 : stats.frameId; - } - - private void finishDesiredFrameLocked(String outcome) { - int frameIdToFinish; - synchronized (desiredTilesLock) { - frameIdToFinish = desiredFrameId; - desiredFrameId = 0; - } - finishFrame(frameIdToFinish, outcome); - } - - private boolean hasPendingOrInflightKindLocked(String kind) { - for (OutboundMessage queued : pendingMessages) { - if (kind.equals(queued.kind)) { - return true; - } - } - for (OutboundMessage inFlight : inFlightMessages) { - if (kind.equals(inFlight.kind)) { - return true; - } - } - return false; - } - - private boolean hasPendingOrInflightMagicLocked(int magic) { - for (OutboundMessage queued : pendingMessages) { - if (queued.magic == magic) { - return true; - } - } - for (OutboundMessage inFlight : inFlightMessages) { - if (inFlight.magic == magic) { - return true; - } - } - return false; - } - - private boolean hasPendingMagicLocked(int sid, int magic) { - for (OutboundMessage queued : pendingMessages) { - if (queued.sid == sid && queued.magic == magic) { - return true; - } - } - return false; - } - - private void handleAckTimeoutLocked(OutboundMessage message) { - consecutiveAckTimeouts += 1; - - if (message.onTimeout != null) { - message.onTimeout.run(); - } - - if (consecutiveAckTimeouts > ConnectionOptions.MAX_CONSECUTIVE_ACK_TIMEOUTS) { - handleTransportFailure("too many ack timeouts"); - } - } - - /** - * Replace any stale lease control with one right-arm and one left-arm - * fire-and-forget write. With priority=true the right arm is sent first so - * CLAIM reaches the lens that originated the deferred wake immediately. - */ - private int enqueueFaceclawWakeControlLocked(int operation, int nonce, boolean priority) { - clearMessagesOfKindLocked("wake-lease-control"); - final int generation = ++faceclawWakeControlGeneration; - faceclawWakeControlSentCount = 0; - if (operation == BleProtocol.FACECLAW_WAKE_OP_ACQUIRE) { - lastFaceclawWakeLeaseQueuedAtMs = SystemClock.elapsedRealtime(); - } - Runnable onSent = () -> { - if (faceclawWakeControlGeneration == generation) { - faceclawWakeControlSentCount += 1; - } - }; - OutboundMessage right = messageBuilder.faceclawWakeControl(operation, nonce, false); - OutboundMessage left = messageBuilder.faceclawWakeControl(operation, nonce, true); - right.onSent = onSent; - left.onSent = onSent; - if (priority) { - pendingMessages.addFirst(left); - pendingMessages.addFirst(right); - } else { - pendingMessages.addLast(right); - pendingMessages.addLast(left); - } - logLine("queue " + right.label + " + L"); - return generation; - } - - private boolean waitForFaceclawWakeControlDelivery(int generation, long timeoutMs) { - long deadline = SystemClock.elapsedRealtime() + Math.max(0, timeoutMs); - synchronized (lock) { - while (running - && sessionReady - && faceclawWakeControlGeneration == generation - && faceclawWakeControlSentCount < 2) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - return faceclawWakeControlGeneration == generation - && faceclawWakeControlSentCount >= 2; - } - } - - /** - * Acquire/renew or release CFW's independent direct-framebuffer repaint - * guard on both arms. It is separate from the optional idle-wake lease: - * every Faceclaw display session needs this guard while its EvenHub layout - * contains swipe-capturing stock widgets. - */ - private int enqueueFaceclawFramebufferControlLocked(int operation, boolean priority) { - clearMessagesOfKindLocked("framebuffer-lease-control"); - final int generation = ++faceclawFramebufferControlGeneration; - faceclawFramebufferControlSentCount = 0; - if (operation == BleProtocol.FACECLAW_FB_OP_ACQUIRE) { - lastFaceclawFramebufferLeaseQueuedAtMs = SystemClock.elapsedRealtime(); - } - Runnable onSent = () -> { - if (faceclawFramebufferControlGeneration == generation) { - faceclawFramebufferControlSentCount += 1; - } - }; - OutboundMessage right = messageBuilder.faceclawFramebufferControl(operation, false); - OutboundMessage left = messageBuilder.faceclawFramebufferControl(operation, true); - right.onSent = onSent; - left.onSent = onSent; - if (priority) { - pendingMessages.addFirst(left); - pendingMessages.addFirst(right); - } else { - pendingMessages.addLast(right); - pendingMessages.addLast(left); - } - logLine("queue " + right.label + " + L"); - return generation; - } - - private boolean waitForFaceclawFramebufferControlDelivery(int generation, long timeoutMs) { - long deadline = SystemClock.elapsedRealtime() + Math.max(0, timeoutMs); - synchronized (lock) { - while (running - && sessionReady - && faceclawFramebufferControlGeneration == generation - && faceclawFramebufferControlSentCount < 2) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - try { - lock.wait(Math.min(remaining, 100)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - return faceclawFramebufferControlGeneration == generation - && faceclawFramebufferControlSentCount >= 2; - } - } - - private boolean releaseFaceclawFramebufferLease() { - int generation; - synchronized (lock) { - if (!running || !sessionReady) { - return true; - } - generation = enqueueFaceclawFramebufferControlLocked( - BleProtocol.FACECLAW_FB_OP_RELEASE, - true - ); - } - interruptibleSleep.interrupt(); - return waitForFaceclawFramebufferControlDelivery( - generation, - FACECLAW_WAKE_CONTROL_WAIT_MS - ); - } - - private void clearMessagesOfKindLocked(String kind) { - Iterator pendingIterator = pendingMessages.iterator(); - while (pendingIterator.hasNext()) { - OutboundMessage message = pendingIterator.next(); - if (kind.equals(message.kind)) { - pendingIterator.remove(); - discardPrewriteIfMatchesLocked(message); - if ("image".equals(kind)) { - discardImageUpdateStatsLocked(message.imageUpdateId, "pending messages cleared (" + kind + ")"); - } - magicPool.release(message.sid, message.magic, message.label, "cleared pending " + kind); - } - } - Iterator inFlightIterator = inFlightMessages.iterator(); - while (inFlightIterator.hasNext()) { - OutboundMessage message = inFlightIterator.next(); - if (kind.equals(message.kind)) { - inFlightIterator.remove(); - if ("image".equals(kind)) { - discardImageUpdateStatsLocked(message.imageUpdateId, "inflight messages cleared (" + kind + ")"); - } - magicPool.release(message.sid, message.magic, message.label, "cleared inflight " + kind); - } - } - if ("image".equals(kind)) { - // The image pipeline was flushed (e.g. ack timeout -> keyframe resync): - // drop the pipelined delta base so the next image is a full keyframe. - lastEnqueuedPacked = new byte[0]; - lastEnqueuedFingerprint = ""; - } - } - - private void clearAllMessagesLocked(String reason) { - clearPendingMessagesLocked(reason); - clearInFlightMessagesLocked(reason); - // The image pipeline is gone: drop the pipelined delta base so the next - // image is a full keyframe rather than a delta onto a stale base. - lastEnqueuedPacked = new byte[0]; - lastEnqueuedFingerprint = ""; - // Queued texture uploads (if any) were dropped with the rest, and the - // session churn behind a full clear may have freed the on-glasses - // cache; forget it phone-side so glyphs re-upload lazily. - textureCache.reset(); - } - - /** - * A custom wake queues CLAIM before NativeScript asks for a resume. Keep - * that private control while flushing stale EvenHub traffic around the - * direct prelude write. - */ - private void clearAllMessagesPreservingWakeLeaseLocked(String reason) { - Iterator pendingIterator = pendingMessages.iterator(); - while (pendingIterator.hasNext()) { - OutboundMessage message = pendingIterator.next(); - if ("wake-lease-control".equals(message.kind)) { - continue; - } - pendingIterator.remove(); - discardPrewriteIfMatchesLocked(message); - discardImageUpdateStatsLocked( - message.imageUpdateId, - "pending messages cleared: " + reason - ); - magicPool.release( - message.sid, - message.magic, - message.label, - "cleared pending: " + reason - ); - } - clearInFlightMessagesLocked(reason); - lastEnqueuedPacked = new byte[0]; - lastEnqueuedFingerprint = ""; - textureCache.reset(); - } - - /** Any image update whose fragments are still queued (not yet sent). */ - private boolean hasPendingImageLocked() { - for (OutboundMessage message : pendingMessages) { - if ("image".equals(message.kind)) { - return true; - } - } - return false; - } - - private void clearPendingMessagesLocked(String reason) { - while (!pendingMessages.isEmpty()) { - var message = pendingMessages.removeFirst(); - discardPrewriteIfMatchesLocked(message); - discardImageUpdateStatsLocked(message.imageUpdateId, "pending messages cleared: " + reason); - magicPool.release(message.sid, message.magic, message.label, "cleared pending: " + reason); - } - } - - private void clearInFlightMessagesLocked(String reason) { - while (!inFlightMessages.isEmpty()) { - var message = inFlightMessages.removeFirst(); - discardImageUpdateStatsLocked(message.imageUpdateId, "inflight messages cleared: " + reason); - magicPool.release(message.sid, message.magic, message.label, "cleared inflight: " + reason); - } - } - - private void discardPrewriteIfMatchesLocked(OutboundMessage message) { - if (message != null && message == prewrittenMessage) { - prewrittenMessage = null; - prewrittenFrames = Collections.emptyList(); - } - } - - private void recordUnexpectedAckLocked(int sid, int magic) { - if (magic < BleMagicPool.MIN_MAGIC || magic > BleMagicPool.MAX_MAGIC) { - return; - } - BleMagicPool.ReleaseRecord previous = magicPool.getReleaseRecord(sid, magic); - if (previous == null) { - String pendingNote = hasPendingMagicLocked(sid, magic) ? " while that magic is only pending locally" : ""; - logLine("unexpected ACK sid=" + sid + " magic=" + magic + pendingNote - + "; possible Even app BLE contention"); - return; - } - if ("timeout".equals(previous.reason)) { - logLine("late ACK after timeout sid=" + sid + " magic=" + magic - + " label=" + previous.label - + "; ACK timeout may be too short"); - return; - } - if ("ack".equals(previous.reason)) { - logLine("duplicate ACK for already-acked message sid=" + sid + " magic=" + magic - + " label=" + previous.label - + "; possible Even app BLE contention"); - return; - } - logLine("late ACK for released message sid=" + sid + " magic=" + magic - + " label=" + previous.label - + " release=" + previous.reason); - } - - /** The address of a configured arm Android no longer holds a bond for, or null. */ - private String firstUnpairedArm() { - if (!bleManager.isBonded(rightAddress)) return rightAddress; - if (!bleManager.isBonded(leftAddress)) return leftAddress; - return null; - } - - /** - * A connect failure while an arm's bond is missing (the pairing was - * forgotten in Android settings, or the address was typed in by hand and - * never paired) will repeat forever, so instead of scheduling a retry, - * park the worker loop and tell the user to re-pair. Only an explicit - * connect (which builds a fresh communicator) starts a new attempt. - */ - private void handleUnpairedFailure(String address) { - Log.e(TAG, "Connect failed and " + address + " is not paired; suspending reconnect"); - synchronized (lock) { - reconnectHalted = true; - sessionReady = false; - fixedLayoutCreated = false; - startupProbePending = false; - shutdownRequested = false; - chargingMode = false; - imageRetryAfterMs = 0; - displayedFingerprint = ""; - faceclawWakePendingNonce = -1; - lastFaceclawWakeLeaseQueuedAtMs = 0; - faceclawWakeControlSentCount = 0; - clearAllMessagesLocked("arm not paired: " + address); - reconnectAfterMs = Long.MAX_VALUE; - bleManager.disconnect(rightAddress); - bleManager.disconnect(leftAddress); - } - if (!userDisconnectRequested) { - setStateDisplay( - "unpaired", - "The glasses (" + address + ") are not paired with this phone." - + " Use \"Pair glasses\" to pair them again, then connect." - ); - } - interruptibleSleep.interrupt(); - } - - private void handleTransportFailure(String reason) { - Log.e(TAG, "Transport failure: "+reason); - synchronized (lock) { - maybeEmitEvenAppConflictLocked(reason); - finishBenchmarkLocked(true, "transport failure"); - sessionReady = false; - fixedLayoutCreated = false; - startupProbePending = false; - shutdownRequested = false; - chargingMode = false; - imageRetryAfterMs = 0; - displayedFingerprint = ""; - faceclawWakePendingNonce = -1; - lastFaceclawWakeLeaseQueuedAtMs = 0; - faceclawWakeControlSentCount = 0; - clearAllMessagesLocked("transport failure: " + reason); - reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS; - bleManager.disconnect(rightAddress); - bleManager.disconnect(leftAddress); - } - if (!userDisconnectRequested) { - setStateDisplay("retrying", reason == null || reason.isEmpty() ? "Reconnecting..." : "Reconnecting after " + reason); - } - interruptibleSleep.interrupt(); - } - - private void resetSessionStateLocked() { - ringBattery = -1; - ringCharging = -1; - directRingBattery = -1; - directRingCharging = -1; - sessionReady = false; - shutdownRequested = false; - fixedLayoutCreated = false; - chargingMode = false; - rightConnected = false; - leftConnected = false; - ringConnected = false; - ringNotificationsReady = false; - reconnectAfterMs = 0; - reconnectHalted = false; - ringReconnectAfterMs = 0; - lastAckAtMs = 0; - lastIncomingAtMs = 0; - lastHeartbeatSentAtMs = 0; - lastSessionReadyAtMs = 0; - consecutiveAckTimeouts = 0; - lastAudioControlAckMagic = 0; - audioCaptureActive = false; - audioPacketListener = null; - compassControlLastSent = -1; - // A dead transport orphans any glasses-side compass state; the fresh - // session re-asserts the desired state once its layout is ready. - compassMaybeOn = false; - faceclawWakePendingNonce = -1; - lastFaceclawWakeLeaseQueuedAtMs = 0; - faceclawWakeControlSentCount = 0; - cfwCleanupDelivered = false; - for (CfwTransport transport : cfwTransports) transport.reset(); - lastCfwCleanupAckMagic = 0; - wearState = -1; - displayedFingerprint = ""; - // Deliberately not clearing silentMode: it is a property of the glasses, - // not of our session, and silent mode blocks app launches, so it can be - // the very cause of the session teardown that got us here. - } - - private void emitRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - FrameTimings.getInstance().finishFrame(frameId, "discarded: no listener attached"); - return; - } - final String containerNameSnapshot = containerName == null ? "" : containerName; - mainHandler.post(() -> { - FrameTimings.getInstance().log(frameId, "dispatching input event on main thread"); - try { - current.onRingEvent(kind, containerNameSnapshot, eventType, eventSource, systemExitReasonCode, frameId); - } catch (Throwable t) { - Log.w(TAG, "listener onRingEvent failed", t); - FrameTimings.getInstance().finishFrame(frameId, "discarded: listener onRingEvent failed"); - } - }); - } - - /** Finish a frame owned by the communicator and tell the TS side, which may be awaiting it. */ - private void finishFrame(int frameId, String outcome) { - if (frameId <= 0) { - return; - } - FrameTimings.getInstance().finishFrame(frameId, outcome); - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onFrameFinished(frameId, outcome); - } catch (Throwable t) { - Log.w(TAG, "listener onFrameFinished failed", t); - } - }); - } - - private void emitImuData(double x, double y, double z, int eventSource) { - if (imuListeners.isEmpty()) { - return; - } - mainHandler.post(() -> { - for (FaceclawImuListener imuListener : imuListeners) { - try { - imuListener.onImuData(x, y, z, eventSource); - } catch (Throwable t) { - Log.w(TAG, "listener onImuData failed", t); - } - } - }); - } - - private void emitCompassEvent(BleProtocol.CompassEvent event) { - if (event.diagnosticFlags >= 0) { - String[] sources = { "unknown", "GRV", "GMRV", "RV" }; - Log.i("FaceclawCompass", "heading=" + event.headingDegrees - + " magneticAccuracy=" + event.magneticAccuracy - + " magneticAnomalies=" + event.magneticAnomalies - + " orientationSource=" + sources[event.orientationSource] - + " flags=0x" + Integer.toHexString(event.diagnosticFlags) - + " sampleTimeMs=" + event.sampleTimeMs); - } - for (CompassSubscription subscription : compassSubscriptions) { - subscription.handler.post(() -> { - try { - subscription.listener.onCompassEvent(event.command, event.headingDegrees, - event.magneticAccuracy, event.magneticAnomalies, event.orientationSource, - event.diagnosticFlags, event.sampleTimeMs); - } catch (Throwable t) { - Log.w(TAG, "listener onCompassEvent failed", t); - } - }); - } - } - - private void emitSilentMode(boolean silent) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onSilentMode(silent); - } catch (Throwable t) { - Log.w(TAG, "listener onSilentMode failed", t); - } - }); - } - - private void emitWearState(boolean wearing) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) return; - mainHandler.post(() -> { - try { - current.onWearState(wearing); - } catch (Throwable t) { - Log.w(TAG, "listener onWearState failed", t); - } - }); - } - - private void emitPhoneLockStateIfChanged(boolean force) { - final boolean locked; - synchronized (lock) { - long now = SystemClock.elapsedRealtime(); - if (!force && now - lastPhoneLockCheckAtMs < 1_000) return; - lastPhoneLockCheckAtMs = now; - locked = keyguardManager != null && keyguardManager.isDeviceLocked(); - int value = locked ? 1 : 0; - if (!force && value == phoneLockState) return; - phoneLockState = value; - } - final FaceclawBleCommunicatorListener current = listener; - if (current == null) return; - mainHandler.post(() -> { - try { - current.onPhoneLockState(locked); - } catch (Throwable t) { - Log.w(TAG, "listener onPhoneLockState failed", t); - } - }); - } - - private void emitBatteryState(int headsetBattery, int headsetCharging) { - // A missing glasses-side report must not erase a verified direct - // reading. Fall back to the glasses cache when the direct link drops. - final boolean useDirect = ringConnected && directRingBattery >= 0; - final int reportedRingBattery = useDirect ? directRingBattery : ringBattery; - final int reportedRingCharging = useDirect ? directRingCharging : ringCharging; - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onBatteryState(headsetBattery, headsetCharging, reportedRingBattery, reportedRingCharging); - } catch (Throwable t) { - Log.w(TAG, "listener onBatteryState failed", t); - } - }); - } - - private void emitFirmwareInfo(BleProtocol.FirmwareInfo info) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onFirmwareInfo(info.leftVersion, info.rightVersion, info.extension); - } catch (Throwable t) { - Log.w(TAG, "listener onFirmwareInfo failed", t); - } - }); - } - - private void emitFrameMetrics(int paintMs, int transmitMs, int tileCount) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onFrameMetrics(paintMs, transmitMs, tileCount); - } catch (Throwable t) { - Log.w(TAG, "listener onFrameMetrics failed", t); - } - }); - } - - private void maybeEmitEvenAppConflictLocked(String reason) { - if (!"write failed".equals(reason)) { - return; - } - long now = SystemClock.elapsedRealtime(); - if (lastSessionReadyAtMs <= 0 || now - lastSessionReadyAtMs > ConnectionOptions.EVEN_APP_WRITE_FAILURE_WINDOW_MS) { - return; - } - if (lastEvenAppConflictAtMs > 0 && now - lastEvenAppConflictAtMs < 60_000) { - return; - } - if (!FaceclawEvenAppDetector.isEvenNotificationActive(appContext)) { - return; - } - lastEvenAppConflictAtMs = now; - emitEvenAppConflict("The Even Realities app still appears to be running. It can hold the glasses BLE link and cause Faceclaw write failures. Open its app settings and force stop it, then reconnect Faceclaw."); - } - - private void emitEvenAppConflict(String message) { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - final String messageSnapshot = message == null ? "" : message; - mainHandler.post(() -> { - try { - current.onEvenAppConflict(messageSnapshot); - } catch (Throwable t) { - Log.w(TAG, "listener onEvenAppConflict failed", t); - } - }); - } - - private void setStateDisplay(String nextPhase, String nextStatus) { - synchronized (lock) { - phase = nextPhase; - status = nextStatus; - } - emitState(); - } - - private void emitState() { - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - final String phaseSnapshot; - final String statusSnapshot; - synchronized (lock) { - phaseSnapshot = phase; - statusSnapshot = status; - } - mainHandler.post(() -> { - try { - current.onStateChange(phaseSnapshot, statusSnapshot); - } catch (Throwable t) { - Log.w(TAG, "listener onStateChange failed", t); - } - }); - } - - private void updateG2ScreenWakeLock(boolean screenOn) { - if (screenOn) { - if (g2ScreenWakeLock == null) { - g2ScreenWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, G2_SCREEN_WAKE_LOCK_TAG); - g2ScreenWakeLock.setReferenceCounted(false); - } - if (!g2ScreenWakeLock.isHeld()) { - g2ScreenWakeLock.acquire(); - logLine("G2 screen wake lock acquired"); - } - return; - } - releaseG2ScreenWakeLock(); - } - - private void releaseG2ScreenWakeLock() { - if (g2ScreenWakeLock != null && g2ScreenWakeLock.isHeld()) { - g2ScreenWakeLock.release(); - logLine("G2 screen wake lock released"); - } - } - - private void logLine(String line) { - Log.i(TAG, line); - final FaceclawBleCommunicatorListener current = listener; - if (current == null) { - return; - } - mainHandler.post(() -> { - try { - current.onLog(line); - } catch (Throwable t) { - Log.w(TAG, "listener onLog failed", t); - } - }); - } - - private static String timestamp(long elapsedMs) { - long wallMs = System.currentTimeMillis(); - return String.format(Locale.US, "%tF %tT.%tL elapsed=%dms", wallMs, wallMs, wallMs, elapsedMs); - } - - private int nextMapSessionId() { - int id = nextMapSessionId; - int increment = connectionOptions.skipSessionIds ? 2 : 1; - nextMapSessionId = (nextMapSessionId + increment) & 0xff; - return id; - } - - private static String requireAddress(String name, String address) { - if (address == null || address.trim().isEmpty()) { - throw new IllegalArgumentException(name + " is required"); - } - return address.trim(); - } - - private boolean hasRingAddress() { - return ringAddress != null && !ringAddress.trim().isEmpty(); - } - - private boolean isConfiguredRingAddress(String address) { - return hasRingAddress() && address != null && address.equalsIgnoreCase(ringAddress); - } - - private boolean isDirectRingNotification(String address, String characteristicUuid) { - if (!isConfiguredRingAddress(address) || characteristicUuid == null) { - return false; - } - return BleProtocol.R1_PHONE_NOTIFY_CHAR_UUID.equals(characteristicUuid) - || BleProtocol.R1_NOTIFY_CHAR_UUID.equals(characteristicUuid); - } - - private static String hex(byte[] data) { - if (data == null || data.length == 0) { - return ""; - } - char[] out = new char[data.length * 2]; - char[] digits = "0123456789abcdef".toCharArray(); - for (int i = 0; i < data.length; i++) { - int value = data[i] & 0xff; - out[i * 2] = digits[value >>> 4]; - out[i * 2 + 1] = digits[value & 0x0f]; - } - return new String(out); - } - - private static String safeMessage(Throwable t) { - if (t == null) { - return "unknown"; - } - StringWriter writer = new StringWriter(); - t.printStackTrace(new PrintWriter(writer)); - String trace = writer.toString(); - if (!trace.trim().isEmpty()) { - return trace; - } - String message = t.getMessage(); - return message == null || message.trim().isEmpty() ? String.valueOf(t) : message; - } - - private String getDesiredFingerprint() { - synchronized (desiredTilesLock) { - return desiredFingerprint; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt new file mode 100644 index 00000000..6d774926 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt @@ -0,0 +1,427 @@ +package com.faceclaw.app + +import android.annotation.SuppressLint +import android.app.KeyguardManager +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.os.Handler +import android.os.Looper +import android.os.PowerManager +import android.util.Log + +/** + * Android facade over the shared [GlassesSessionCore]: the JVM API TypeScript + * and the other Android classes call by name, plus the Android-only pieces: + * the GATT link ([AndroidSessionLink] over [FaceclawBleManager]), main-thread + * marshalling, the worker thread, the screen wake lock, keyguard/screen + * broadcast state, the Even-app conflict check, and the Bitmap/ByteBuffer/ + * Context entry points for previews, screenshots and recordings. + */ +@SuppressLint("MissingPermission") +class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddress: String?, ringAddress: String?) { + companion object { + private const val TAG = GlassesSessionCore.TAG + private const val G2_SCREEN_WAKE_LOCK_TAG = "Faceclaw:G2Screen" + + // The most recently started communicator; lets app worker threads submit + // surface frames without holding a cross-isolate reference to the bridge + // object (JS wrappers do not cross isolates, but the Java instance does). + @Volatile private var activeInstance: FaceclawBleCommunicator? = null + + @JvmStatic + fun getActive(): FaceclawBleCommunicator? { + return activeInstance + } + } + + private val appContext: Context = context.applicationContext + init { + FrameTimings.getInstance().init(appContext) + } + private val powerManager: PowerManager = appContext.getSystemService(Context.POWER_SERVICE) as PowerManager + private val keyguardManager: KeyguardManager? = appContext.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager? + private val bleManager: FaceclawBleManager = FaceclawBleManager(appContext) + private val mainHandler = Handler(Looper.getMainLooper()) + private val mainDispatcher = SessionDispatcher { action -> mainHandler.post { action() } } + @Volatile private var workerThread: Thread? = null + @Volatile private var ringWorkerThread: Thread? = null + private var g2ScreenWakeLock: PowerManager.WakeLock? = null + private var phoneLockReceiverRegistered = false + + private val host = object : SessionHost { + override fun postToMain(action: () -> Unit) { + mainHandler.post { action() } + } + + override fun currentThreadDispatcher(): SessionDispatcher { + val looper = Looper.myLooper() ?: return mainDispatcher + val handler = Handler(looper) + return SessionDispatcher { action -> handler.post { action() } } + } + + override fun isPhoneLocked(): Boolean = keyguardManager != null && keyguardManager.isDeviceLocked + + override fun setScreenWakeLock(on: Boolean) = updateG2ScreenWakeLock(on) + + override fun isEvenAppActive(): Boolean = FaceclawEvenAppDetector.isEvenNotificationActive(appContext) + + override fun startWorker(body: () -> Unit) { + val thread = Thread(body, "FaceclawBleCommunicator") + workerThread = thread + thread.start() + } + + override fun joinWorker(timeoutMs: Long) { + val threadToJoin = workerThread ?: return + threadToJoin.interrupt() + try { + threadToJoin.join(timeoutMs) + } catch (e: InterruptedException) { + Thread.currentThread().interrupt() + } + workerThread = null + } + + override fun startRingWorker(body: () -> Unit) { + val thread = Thread(body, "FaceclawRing") + ringWorkerThread = thread + thread.start() + } + + override fun interruptRingWorker() { + ringWorkerThread?.interrupt() + } + + override fun supportsRingHealth(): Boolean = true + + override fun appendRingFrameJournal(line: String) { + val folder = java.io.File(appContext.filesDir, "health") + if (!folder.isDirectory && !folder.mkdirs()) { + throw java.io.IOException("cannot create health folder") + } + java.io.FileOutputStream(java.io.File(folder, "ring-frames.jsonl"), true).use { out -> + out.write(line.toByteArray(Charsets.UTF_8)) + out.fd.sync() + } + } + + override fun log(level: SessionLogLevel, tag: String, message: String, error: Throwable?) { + when (level) { + SessionLogLevel.DEBUG -> Log.d(tag, message) + SessionLogLevel.INFO -> Log.i(tag, message) + SessionLogLevel.WARN -> if (error != null) Log.w(tag, message, error) else Log.w(tag, message) + SessionLogLevel.ERROR -> if (error != null) Log.e(tag, message, error) else Log.e(tag, message) + } + } + } + + private val core = GlassesSessionCore( + AndroidSessionLink(bleManager), + host, + FrameTimings.getInstance().core, + AndroidProtocolPlatform, + rightAddress, + leftAddress, + ringAddress, + ) + + private val phoneLockReceiver: BroadcastReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + core.onPhoneLockSignal() + } + } + + // Active animated-GIF screen recording, or null when idle. Frames are + // pushed by recordScreenFrame(), which the TS side calls at each + // phone-preview flush. + @Volatile private var screenRecorder: GifScreenRecorder? = null + + init { + bleManager.setListener(core) + val phoneLockFilter = IntentFilter() + phoneLockFilter.addAction(Intent.ACTION_SCREEN_ON) + phoneLockFilter.addAction(Intent.ACTION_SCREEN_OFF) + phoneLockFilter.addAction(Intent.ACTION_USER_PRESENT) + appContext.registerReceiver(phoneLockReceiver, phoneLockFilter) + phoneLockReceiverRegistered = true + } + + fun setListener(listener: FaceclawBleCommunicatorListener?) = core.setListener(listener) + + fun start() { + if (core.start()) { + activeInstance = this + } + } + + fun disconnect() = core.disconnect() + + fun close() { + if (activeInstance === this) { + activeInstance = null + } + core.close() + if (phoneLockReceiverRegistered) { + phoneLockReceiverRegistered = false + appContext.unregisterReceiver(phoneLockReceiver) + } + } + + fun setG2ScreenOn(screenOn: Boolean) = core.setG2ScreenOn(screenOn) + + fun setFirmwareDebugFlags(enabled: Boolean) = core.setFirmwareDebugFlags(enabled) + + fun startG2AudioCapture(listener: FaceclawAudioPacketListener?): Boolean = core.startG2AudioCapture(listener) + + fun stopG2AudioCapture() = core.stopG2AudioCapture() + + fun isSessionReady(): Boolean = core.isSessionReady() + + /** See [GlassesSessionCore.requestRingHealthNow]. */ + fun requestRingHealthNow() = core.requestRingHealthNow() + + /** See [GlassesSessionCore.takeRingHealthBatch]. */ + fun takeRingHealthBatch(): RingHealthBatch = core.takeRingHealthBatch() + + /** See [GlassesSessionCore.clearRingHealthRecordsBelow]. */ + fun clearRingHealthRecordsBelow(watermark: Long): Int = core.clearRingHealthRecordsBelow(watermark) + + /** See [GlassesSessionCore.getRingHealthRecords]. */ + fun getRingHealthRecords(): List = core.getRingHealthRecords() + + /** See [GlassesSessionCore.isAudioCaptureActive]. */ + fun isAudioCaptureActive(): Boolean = core.isAudioCaptureActive() + + fun setFaceclawWakeLeaseEnabled(enabled: Boolean): Boolean = core.setFaceclawWakeLeaseEnabled(enabled) + + fun awaitEvenHubSessionReady(timeoutMs: Int): Boolean = core.awaitEvenHubSessionReady(timeoutMs) + + fun setImuReportEnabled(enable: Boolean, reportFrq: Int) = core.setImuReportEnabled(enable, reportFrq) + + fun setCompassEnabled(enable: Boolean) = core.setCompassEnabled(enable) + + fun setCompassEnabled(owner: String?, enable: Boolean) = core.setCompassEnabled(owner, enable) + + fun setBrightness(autoAdjust: Boolean, brightnessLevel: Int) = core.setBrightness(autoAdjust, brightnessLevel) + fun configureBrightness(auto: Boolean, level: Int, minimum: Int, maximum: Int, curve: String, fadeMs: Int) = + core.configureBrightness(auto, level, minimum, maximum, curve, fadeMs) + + fun enableWearDetectionAndRequestState() = core.enableWearDetectionAndRequestState() + + fun startBandwidthBenchmark(messageSize: Int, windowSize: Int, durationMs: Int): Boolean = + core.startBandwidthBenchmark(messageSize, windowSize, durationMs) + + // 0: current link; 1: re-request HIGH; 2: request 2M; 3: both. + fun startBandwidthBenchmarkWithLinkMode(messageSize: Int, windowSize: Int, + durationMs: Int, linkMode: Int): Boolean = + core.startBandwidthBenchmarkWithLinkMode(messageSize, windowSize, durationMs, linkMode) + + fun cancelBandwidthBenchmark() = core.cancelBandwidthBenchmark() + + /** Status/results of the current or most recent benchmark run, as JSON. */ + fun getBandwidthBenchmarkStatus(): String = core.getBandwidthBenchmarkStatus() + + fun addImuListener(listener: FaceclawImuListener?) = core.addImuListener(listener) + + fun removeImuListener(listener: FaceclawImuListener?) = core.removeImuListener(listener) + + fun addAmbientLightListener(listener: FaceclawAmbientLightListener?) = core.addAmbientLightListener(listener) + + fun removeAmbientLightListener(listener: FaceclawAmbientLightListener?) = core.removeAmbientLightListener(listener) + + fun queryAmbientLight() = core.queryAmbientLight() + + fun setAmbientLightPolling(enable: Boolean, intervalMs: Int, minDelta: Int, + heartbeatMs: Int, bindToLease: Boolean) = + core.setAmbientLightPolling(enable, intervalMs, minDelta, heartbeatMs, bindToLease) + + fun addMicStatusListener(listener: FaceclawMicStatusListener?) = core.addMicStatusListener(listener) + + fun removeMicStatusListener(listener: FaceclawMicStatusListener?) = core.removeMicStatusListener(listener) + + fun sendFaceclawMicControl(record: ByteArray?, label: String, rightTemple: Boolean, leftTemple: Boolean) = + core.sendFaceclawMicControl(record, label, rightTemple, leftTemple) + + fun startG2AudioForwarding(listener: FaceclawAudioPacketListener?): Boolean = core.startG2AudioForwarding(listener) + + fun stopG2AudioForwarding() = core.stopG2AudioForwarding() + + fun addCompassListener(listener: FaceclawCompassListener?) = core.addCompassListener(listener) + + fun removeCompassListener(listener: FaceclawCompassListener?) = core.removeCompassListener(listener) + + /** Set the compositor's output frame size. Call before configuring surfaces. */ + fun configureCompositorScreen(width: Int, height: Int) = core.configureCompositorScreen(width, height) + + /** + * The current composited screen as a phone-UI preview bitmap, or null + * before any surface has been configured. Built from the compositor so + * the preview reflects every surface (chrome + whichever app is + * foreground), including worker-app frames the TS side never sees. + */ + fun getCompositePreviewBitmap(brightenGamma: Double): android.graphics.Bitmap? { + return getCompositePreviewBitmap(brightenGamma, false) + } + + /** As above; `green` renders the preview green-on-black (Settings > Phone display > Preview color). */ + fun getCompositePreviewBitmap(brightenGamma: Double, green: Boolean): android.graphics.Bitmap? { + val composite = core.previewComposite() ?: return null + return PreviewBitmapUtil.fromGray( + java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green) + } + + /** Save the current composite as a 4-bit grayscale PNG; returns the path or "". */ + @Throws(java.io.IOException::class) + fun saveCompositePngScreenshot(): String { + val composite = core.previewComposite() ?: return "" + return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height) + } + + /** + * Save the current composite cropped to the given screen rect (the region + * the shell says is actually occupied). The rect is clamped to the screen; + * a degenerate rect falls back to the full screen. + */ + @Throws(java.io.IOException::class) + fun saveCompositePngScreenshot(cropX: Int, cropY: Int, cropWidth: Int, cropHeight: Int): String { + val composite = core.previewComposite() ?: return "" + val x = Math.max(0, cropX) + val y = Math.max(0, cropY) + val width = Math.min(composite.width - x, cropWidth - (x - cropX)) + val height = Math.min(composite.height - y, cropHeight - (y - cropY)) + if (width <= 0 || height <= 0 || (x == 0 && y == 0 && width == composite.width && height == composite.height)) { + return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height) + } + val cropped = ByteArray(width * height) + for (row in 0 until height) { + System.arraycopy(composite.gray, (y + row) * composite.width + x, cropped, row * width, width) + } + return ScreenshotUtil.savePngScreenshot(appContext, cropped, width, height) + } + + /** Begin collecting composite frames for an animated-GIF screen recording. */ + fun startScreenRecording() { + screenRecorder = GifScreenRecorder() + } + + /** Capture the current composite into the active recording; no-op when idle. */ + fun recordScreenFrame() { + val recorder = screenRecorder ?: return + val composite = core.previewComposite() ?: return + recorder.addFrame(composite.gray, composite.width, composite.height, System.currentTimeMillis()) + } + + /** Finish the recording and save it as an animated GIF; returns the path or "". */ + @Throws(java.io.IOException::class) + fun stopScreenRecording(): String { + val recorder = screenRecorder + screenRecorder = null + if (recorder == null) { + return "" + } + if (recorder.isOverflowed()) { + Log.i(TAG, "screen recording hit its frame cap; the tail was dropped") + } + return recorder.save(appContext) + } + + fun setSurfaceVisible(id: String, visible: Boolean) = core.setSurfaceVisible(id, visible) + + fun setSurfaceDepth(id: String, depth: Int) = core.setSurfaceDepth(id, depth) + + fun setScreenBlanked(blanked: Boolean) = core.setScreenBlanked(blanked) + + fun configureSurface(id: String?, x: Int, y: Int, width: Int, height: Int, zOrder: Int, transparency: Int) = + core.configureSurface(id, x, y, width, height, zOrder, transparency) + + fun removeSurface(id: String) = core.removeSurface(id) + + /** Stage an immutable shell snapshot alongside the retained app screen. */ + fun submitShellScene(bytes: java.nio.ByteBuffer, paintMs: Int, frameId: Int) = + core.submitShellScene(AndroidByteReader(bytes), paintMs, frameId) + + /** Legacy compositor dimming for callers without a shell scene. */ + fun setUnderlayDim(belowZOrder: Int, factor256: Int) = core.setUnderlayDim(belowZOrder, factor256) + + /** + * Apply an update to one compositor surface and submit the recomposited + * screen as the desired frame (see [GlassesSessionCore.submitSurfaceFrame]). + * + * pixels8bpp arrives as a ByteBuffer because NativeScript marshals a JS + * ArrayBuffer to one without the per-element bridge copy that a byte[] + * parameter would need (~150ms for a full frame). + */ + fun submitSurfaceFrame( + pixels8bpp: java.nio.ByteBuffer, + surfaceId: String, + rectX: Int, + rectY: Int, + rectWidth: Int, + rectHeight: Int, + contentFingerprint: String?, + paintMs: Int, + frameId: Int + ) { + submitSurfaceFrame(pixels8bpp, surfaceId, rectX, rectY, rectWidth, rectHeight, + contentFingerprint, paintMs, frameId, null) + } + + /** As above, with the frame's glyph draws (null when the submitter has no glyph metadata). */ + fun submitSurfaceFrame( + pixels8bpp: java.nio.ByteBuffer, + surfaceId: String, + rectX: Int, + rectY: Int, + rectWidth: Int, + rectHeight: Int, + contentFingerprint: String?, + paintMs: Int, + frameId: Int, + glyphs: java.nio.ByteBuffer? + ) { + core.submitSurfaceFrame(AndroidByteReader(pixels8bpp), surfaceId, rectX, rectY, rectWidth, rectHeight, + contentFingerprint, paintMs, frameId, if (glyphs == null) null else AndroidByteReader(glyphs)) + } + + /** Play a tone sequence via CFW load_image_z mode 5 kind 4 (payload built on the TS side). */ + fun playBuzzerSequence(payload: java.nio.ByteBuffer?) { + val bytes = ByteArray(if (payload == null) 0 else payload.remaining()) + if (payload != null) { + payload.get(bytes) + } + core.playBuzzerSequence(bytes) + } + + fun sendShutdown(exitMode: Int): Boolean = core.sendShutdown(exitMode) + + fun sendCfwCleanup(): Boolean = core.sendCfwCleanup() + + fun suspendEvenHubSession(): Boolean = core.suspendEvenHubSession() + + fun resumeEvenHubSession(): Boolean = core.resumeEvenHubSession() + + // --------------------------------------------------------------------- + + private fun updateG2ScreenWakeLock(screenOn: Boolean) { + if (screenOn) { + var wakeLock = g2ScreenWakeLock + if (wakeLock == null) { + wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, G2_SCREEN_WAKE_LOCK_TAG) + wakeLock.setReferenceCounted(false) + g2ScreenWakeLock = wakeLock + } + if (!wakeLock.isHeld) { + wakeLock.acquire() + Log.i(TAG, "G2 screen wake lock acquired") + } + return + } + val wakeLock = g2ScreenWakeLock + if (wakeLock != null && wakeLock.isHeld) { + wakeLock.release() + Log.i(TAG, "G2 screen wake lock released") + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java deleted file mode 100644 index 8a6ed3d0..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicatorListener.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawBleCommunicatorListener { - void onLog(String line); - void onStateChange(String phase, String status); - void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId); - void onBatteryState(int headsetBattery, int headsetCharging, int ringBattery, int ringCharging); - void onSilentMode(boolean silent); - void onWearState(boolean wearing); - void onPhoneLockState(boolean locked); - void onEvenAppConflict(String message); - void onFrameMetrics(int paintMs, int transmitMs, int tileCount); - void onFrameFinished(int frameId, String outcome); - void onFirmwareInfo(String leftVersion, String rightVersion, String extension); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleListener.java deleted file mode 100644 index 2a64c2fd..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleListener.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawBleListener { - void onNotification(String address, String characteristicUuid, byte[] data); - void onConnectionStateChange(String address, boolean connected); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java deleted file mode 100644 index 14a6e52a..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java +++ /dev/null @@ -1,578 +0,0 @@ -package com.faceclaw.app; - -import android.annotation.SuppressLint; -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothGatt; -import android.bluetooth.BluetoothGattCallback; -import android.bluetooth.BluetoothGattCharacteristic; -import android.bluetooth.BluetoothGattDescriptor; -import android.bluetooth.BluetoothGattService; -import android.bluetooth.BluetoothManager; -import android.bluetooth.BluetoothProfile; -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; -import android.util.Log; - -import java.util.List; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; - -@SuppressLint("MissingPermission") -public class FaceclawBleManager { - private static final String TAG = "FaceclawBle"; - private static final int[] WRITE_RETRY_DELAYS_MS = new int[] {1, 1, 1, 2, 4, 8, 12, 20, 35, 100, 200}; - - private final Context context; - private final BluetoothAdapter bluetoothAdapter; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private final ConcurrentHashMap gattClients = new ConcurrentHashMap<>(); - private final Object bluetoothApiLock = new Object(); - - private final ConcurrentHashMap connectLatches = new ConcurrentHashMap<>(); - private final ConcurrentHashMap connectResults = new ConcurrentHashMap<>(); - - private final ConcurrentHashMap servicesLatches = new ConcurrentHashMap<>(); - private final ConcurrentHashMap servicesStatuses = new ConcurrentHashMap<>(); - - private final ConcurrentHashMap mtuLatches = new ConcurrentHashMap<>(); - private final ConcurrentHashMap negotiatedMtus = new ConcurrentHashMap<>(); - private final ConcurrentHashMap mtuStatuses = new ConcurrentHashMap<>(); - - private final ConcurrentHashMap descriptorLatches = new ConcurrentHashMap<>(); - private final ConcurrentHashMap descriptorStatuses = new ConcurrentHashMap<>(); - - private final ConcurrentHashMap writeLatches = new ConcurrentHashMap<>(); - private final ConcurrentHashMap writeStatuses = new ConcurrentHashMap<>(); - - private volatile FaceclawBleListener listener; - - // Process-wide outbound-traffic totals, sampled by the phone UI's BLE - // bandwidth indicator. Static so counts from every manager instance and - // isolate land in the same totals. - private static final AtomicLong outboundMessages = new AtomicLong(); - private static final AtomicLong outboundBytes = new AtomicLong(); - private static final AtomicLong displayFramesSent = new AtomicLong(); - - /** Called by the communicator when a display frame's last message is acked. */ - public static void recordDisplayFrameSent() { - displayFramesSent.incrementAndGet(); - } - - /** Running totals of outbound BLE traffic since process start: [messages, bytes, display frames]. */ - public static long[] sampleOutboundTraffic() { - return new long[] { outboundMessages.get(), outboundBytes.get(), displayFramesSent.get() }; - } - - public FaceclawBleManager(Context context) { - this.context = context.getApplicationContext(); - BluetoothManager bluetoothManager = (BluetoothManager) this.context.getSystemService(Context.BLUETOOTH_SERVICE); - if (bluetoothManager == null || bluetoothManager.getAdapter() == null) { - throw new IllegalStateException("Bluetooth adapter unavailable"); - } - this.bluetoothAdapter = bluetoothManager.getAdapter(); - } - - public void setListener(FaceclawBleListener listener) { - this.listener = listener; - } - - /** - * Whether Android still holds a bond (pairing) for this address. Returns - * true when the state cannot be determined, so callers only act on a - * definite BOND_NONE. - */ - public boolean isBonded(String address) { - if (address == null || address.trim().isEmpty()) { - return true; - } - try { - BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); - return device == null || device.getBondState() != BluetoothDevice.BOND_NONE; - } catch (Throwable t) { - return true; - } - } - - /** - * Android's bond state for this address (BluetoothDevice.BOND_NONE / - * BOND_BONDING / BOND_BONDED), or -1 when it cannot be determined. - * BOND_BONDING covers the whole OS pairing flow, including a pairing - * dialog that is still waiting for the user. - */ - public int getBondState(String address) { - if (address == null || address.trim().isEmpty()) { - return -1; - } - try { - BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); - return device == null ? -1 : device.getBondState(); - } catch (Throwable t) { - return -1; - } - } - - /** Whether a GATT client for this address is open, i.e. no disconnect callback has arrived for it. */ - public boolean isConnected(String address) { - return address != null && gattClients.containsKey(address); - } - - public boolean connect(String address, int timeoutMs) { - return connect(address, timeoutMs, false); - } - - /** - * EXPERIMENTAL (2026-09-11): the ring specifically needs autoConnect=true. - * Even's own vendor app connects the ring with Android's autoConnect flag - * set (confirmed via its own BLE debug log, "connect() - device: ..., - * auto: true"), while every other faceclaw connection (both glasses arms) - * uses autoConnect=false. Direct connect (false) is faster to establish - * but Android supervises it less patiently; a live test tonight showed - * the ring-only direct connection dying almost exactly 5 seconds in, - * repeatedly, with nothing else competing for it - consistent with a - * companion device Android's stack expects to be babied via autoConnect, - * not direct-connected like the glasses. autoConnect=true can take much - * longer to actually complete (Android manages it as a background - * reconnect, not an immediate attempt), so this needs its own longer - * timeout - do not reuse CONNECT_TIMEOUT_MS/awaitLatch's short window for - * it, that would just trade one spurious failure for another. - */ - public boolean connect(String address, int timeoutMs, boolean autoConnect) { - if (address == null || address.trim().isEmpty()) { - throw new IllegalArgumentException("address is required"); - } - CountDownLatch latch = new CountDownLatch(1); - connectLatches.put(address, latch); - connectResults.remove(address); - - Object gattLock = gattLock(address); - BluetoothGatt gatt; - synchronized (gattLock) { - BluetoothGatt existing = gattClients.get(address); - if (existing != null) { - connectLatches.remove(address); - return true; - } - - final BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); - if (device == null) { - connectLatches.remove(address); - throw new IllegalArgumentException("remote device not found: " + address); - } - - gatt = device.connectGatt( - context, - autoConnect, - gattCallback, - BluetoothDevice.TRANSPORT_LE, - BluetoothDevice.PHY_LE_2M|BluetoothDevice.PHY_LE_1M - ); - if (gatt != null) { - gattClients.put(address, gatt); - } - - if (gatt == null) { - connectLatches.remove(address); - return false; - } - - if (!awaitLatch(latch, timeoutMs)) { - connectLatches.remove(address); - connectResults.remove(address); - gattClients.remove(address, gatt); - negotiatedMtus.remove(address); - gatt.disconnect(); - gatt.close(); - return false; - } - - Boolean connected = connectResults.remove(address); - connectLatches.remove(address); - return Boolean.TRUE.equals(connected); - } - } - - public boolean requestConnectionPriority(String address, int priority) { - synchronized (gattLock(address)) { - BluetoothGatt gatt = requireGatt(address); - return gatt.requestConnectionPriority(priority); - } - } - - /** One-shot benchmark preferences; success is established by callbacks/HCI, not submission. */ - public void prepareBenchmarkLink(String address, int mode) { - synchronized (gattLock(address)) { - BluetoothGatt gatt = requireGatt(address); - if ((mode & 1) != 0) { - boolean accepted = gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); - Log.i(TAG, "benchmark HIGH address=" + address + " submitted=" + accepted); - } - if ((mode & 2) != 0) { - Log.i(TAG, "benchmark request 2M address=" + address); - gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK, - BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED); - } - gatt.readPhy(); - } - } - - public int getNegotiatedMtu(String address) { - Integer mtu = negotiatedMtus.get(address); - return mtu == null ? 23 : mtu; - } - - public boolean requestMtu(String address, int mtu, int timeoutMs) { - synchronized (gattLock(address)) { - CountDownLatch latch = new CountDownLatch(1); - mtuLatches.put(address, latch); - mtuStatuses.remove(address); - - BluetoothGatt gatt = requireGatt(address); - boolean started = gatt.requestMtu(mtu); - if (!started) { - mtuLatches.remove(address); - return false; - } - if (!awaitLatch(latch, timeoutMs)) { - mtuLatches.remove(address); - mtuStatuses.remove(address); - return false; - } - Integer status = mtuStatuses.remove(address); - mtuLatches.remove(address); - return status != null && status == BluetoothGatt.GATT_SUCCESS; - } - } - - public boolean discoverServices(String address, int timeoutMs) { - synchronized (gattLock(address)) { - CountDownLatch latch = new CountDownLatch(1); - servicesLatches.put(address, latch); - servicesStatuses.remove(address); - - BluetoothGatt gatt = requireGatt(address); - boolean started = gatt.discoverServices(); - if (!started) { - servicesLatches.remove(address); - return false; - } - if (!awaitLatch(latch, timeoutMs)) { - servicesLatches.remove(address); - servicesStatuses.remove(address); - return false; - } - Integer status = servicesStatuses.remove(address); - servicesLatches.remove(address); - return status != null && status == BluetoothGatt.GATT_SUCCESS; - } - } - - public boolean enableNotifications(String address, String characteristicUuid, boolean enable, int timeoutMs) { - synchronized (gattLock(address)) { - CountDownLatch latch = new CountDownLatch(1); - descriptorLatches.put(address, latch); - descriptorStatuses.remove(address); - - BluetoothGatt gatt = requireGatt(address); - BluetoothGattCharacteristic characteristic = requireCharacteristic(gatt, characteristicUuid); - - boolean notificationSet = gatt.setCharacteristicNotification(characteristic, enable); - if (!notificationSet) { - descriptorLatches.remove(address); - return false; - } - - BluetoothGattDescriptor descriptor = characteristic.getDescriptor(BleProtocol.CCCD_UUID); - if (descriptor == null) { - descriptorLatches.remove(address); - return true; - } - - descriptor.setValue(enable - ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE - : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); - boolean started = gatt.writeDescriptor(descriptor); - if (!started) { - descriptorLatches.remove(address); - descriptorStatuses.remove(address); - return false; - } - if (!awaitLatch(latch, timeoutMs)) { - descriptorLatches.remove(address); - descriptorStatuses.remove(address); - return false; - } - Integer status = descriptorStatuses.remove(address); - descriptorLatches.remove(address); - return status != null && status == BluetoothGatt.GATT_SUCCESS; - } - } - - public boolean writeFrames( - String address, - String characteristicUuid, - List frames, - int writeType, - int timeoutMs - ) { - if (frames == null || frames.isEmpty()) { - return true; - } - - long startMs = System.currentTimeMillis(); - synchronized (gattLock(address)) { - BluetoothGatt gatt = requireGatt(address); - BluetoothGattCharacteristic characteristic = requireCharacteristic(gatt, characteristicUuid); - - for (int i = 0; i < frames.size(); i++) { - byte[] frame = frames.get(i); - if (!startWrite(gatt, characteristic, address, frame, writeType, timeoutMs)) { - return false; - } - outboundBytes.addAndGet(frame != null ? frame.length : 0); - } - } - outboundMessages.incrementAndGet(); - int totalSize = frames.stream().mapToInt(frame -> frame != null ? frame.length : 0).sum(); - Log.i(TAG, "writeFrames wrote " + frames.size() + " frames totaling " + totalSize + " bytes in " + (System.currentTimeMillis() - startMs) + "ms"); - return true; - } - - private boolean startWrite( - BluetoothGatt gatt, - BluetoothGattCharacteristic characteristic, - String address, - byte[] data, - int writeType, - int timeoutMs - ) { - int retryCount = 0; - while (true) { - CountDownLatch latch = new CountDownLatch(1); - writeLatches.put(address, latch); - writeStatuses.remove(address); - - long currentTime = System.currentTimeMillis(); - int result = gatt.writeCharacteristic(characteristic, data, writeType); - long timeAsleep = System.currentTimeMillis() - currentTime; - //Log.i(TAG, "writeCharacteristic: spent " + timeAsleep + "ms"); - - if (result == android.bluetooth.BluetoothStatusCodes.ERROR_GATT_WRITE_REQUEST_BUSY) { - writeLatches.remove(address, latch); - writeStatuses.remove(address); - if (!sleepBeforeWriteRetry(address, "busy", retryCount++)) { - return false; - } - continue; - } else { - //Log.i(TAG, "writeCharacteristic with writeType=" + writeType + " result=" + result + " retryCount=" + retryCount); - if (result != android.bluetooth.BluetoothStatusCodes.SUCCESS) { - writeLatches.remove(address, latch); - writeStatuses.remove(address); - if (!sleepBeforeWriteRetry(address, "start result=" + result, retryCount++)) { - return false; - } - continue; - } - if (!awaitLatch(latch, timeoutMs)) { - writeLatches.remove(address, latch); - writeStatuses.remove(address); - return false; - } - Integer status = writeStatuses.remove(address); - writeLatches.remove(address); - if (status != null && status == BluetoothGatt.GATT_SUCCESS) { - return true; - } - if (!sleepBeforeWriteRetry(address, "callback status=" + status, retryCount++)) { - return false; - } - } - } - } - - private boolean sleepBeforeWriteRetry(String address, String reason, int retryIndex) { - if (retryIndex >= WRITE_RETRY_DELAYS_MS.length) { - Log.w(TAG, "writeCharacteristic retry exhausted: address=" + address + " reason=" + reason); - return false; - } - int delayMs = WRITE_RETRY_DELAYS_MS[retryIndex]; - Log.w(TAG, "writeCharacteristic retry: address=" + address + " reason=" + reason + " delayMs=" + delayMs); - try { - Thread.sleep(delayMs); - return true; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - } - - public void disconnect(String address) { - Object gattLock = gattLock(address); - synchronized (gattLock) { - BluetoothGatt gatt = gattClients.remove(address); - negotiatedMtus.remove(address); - if (gatt == null) { - return; - } - gatt.disconnect(); - gatt.close(); - } - } - - public void close() { - for (String address : gattClients.keySet()) { - disconnect(address); - } - } - - private BluetoothGatt requireGatt(String address) { - BluetoothGatt gatt = gattClients.get(address); - if (gatt == null) { - throw new IllegalStateException("Not connected: " + address); - } - return gatt; - } - - private BluetoothGattCharacteristic requireCharacteristic(BluetoothGatt gatt, String characteristicUuid) { - UUID target = UUID.fromString(characteristicUuid); - for (BluetoothGattService service : gatt.getServices()) { - BluetoothGattCharacteristic characteristic = service.getCharacteristic(target); - if (characteristic != null) { - return characteristic; - } - } - throw new IllegalStateException("Characteristic not found: " + characteristicUuid); - } - - private boolean awaitLatch(CountDownLatch latch, int timeoutMs) { - try { - return latch.await(timeoutMs, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - } - - private Object gattLock(String address) { - // Android's Bluetooth stack has process-wide command-pipeline constraints on some devices. - // Keep every BluetoothGatt API call serialized globally, even for different MAC addresses. - return bluetoothApiLock; - } - - private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { - @Override - public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { - Log.i(TAG, "onConnectionStateChange: status=" + status + " newState=" + newState); - String address = gatt.getDevice().getAddress(); - - if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { - connectResults.put(address, true); - CountDownLatch latch = connectLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - dispatchConnectionState(address, true); - return; - } - - if (newState == BluetoothProfile.STATE_DISCONNECTED) { - connectResults.put(address, false); - CountDownLatch latch = connectLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - Object gattLock = gattLock(address); - synchronized (gattLock) { - gattClients.remove(address, gatt); - negotiatedMtus.remove(address); - gatt.close(); - } - dispatchConnectionState(address, false); - } - } - - @Override - public void onServicesDiscovered(BluetoothGatt gatt, int status) { - String address = gatt.getDevice().getAddress(); - servicesStatuses.put(address, status); - CountDownLatch latch = servicesLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - } - - @Override - public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { - String address = gatt.getDevice().getAddress(); - if (status == BluetoothGatt.GATT_SUCCESS) negotiatedMtus.put(address, mtu); - mtuStatuses.put(address, status); - CountDownLatch latch = mtuLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - } - - @Override - public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) { - Log.i(TAG, "onPhyRead: address=" + gatt.getDevice().getAddress() - + " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status); - } - - @Override - public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) { - Log.i(TAG, "onPhyUpdate: address=" + gatt.getDevice().getAddress() - + " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status); - } - - @Override - public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { - String address = gatt.getDevice().getAddress(); - descriptorStatuses.put(address, status); - CountDownLatch latch = descriptorLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - } - - @Override - public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { - String address = gatt.getDevice().getAddress(); - writeStatuses.put(address, status); - CountDownLatch latch = writeLatches.remove(address); - if (latch != null) { - latch.countDown(); - } - } - - @Override - public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] value) { - dispatchNotification(gatt.getDevice().getAddress(), characteristic.getUuid().toString(), value); - } - - @Deprecated - @Override - public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - dispatchNotification(gatt.getDevice().getAddress(), characteristic.getUuid().toString(), characteristic.getValue()); - } - }; - - private void dispatchConnectionState(String address, boolean connected) { - FaceclawBleListener current = listener; - if (current == null) return; - current.onConnectionStateChange(address, connected); - } - - private void dispatchNotification(String address, String characteristicUuid, byte[] data) { - FaceclawBleListener current = listener; - if (current == null) return; - byte[] copy = data != null ? data.clone() : new byte[0]; - current.onNotification(address, characteristicUuid, copy); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.kt new file mode 100644 index 00000000..a2d7f718 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.kt @@ -0,0 +1,557 @@ +package com.faceclaw.app + +import android.annotation.SuppressLint +import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothDevice +import android.bluetooth.BluetoothGatt +import android.bluetooth.BluetoothGattCallback +import android.bluetooth.BluetoothGattCharacteristic +import android.bluetooth.BluetoothGattDescriptor +import android.bluetooth.BluetoothGattService +import android.bluetooth.BluetoothManager +import android.bluetooth.BluetoothProfile +import android.content.Context +import android.os.Handler +import android.os.Looper +import android.os.SystemClock +import android.util.Log + +import java.util.UUID +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicLong + +@SuppressLint("MissingPermission") +class FaceclawBleManager(context: Context) { + companion object { + private const val TAG = "FaceclawBle" + private val WRITE_RETRY_DELAYS_MS = intArrayOf(1, 1, 1, 2, 4, 8, 12, 20, 35, 100, 200) + + // Process-wide outbound-traffic totals, sampled by the phone UI's BLE + // bandwidth indicator. Static so counts from every manager instance and + // isolate land in the same totals. + private val outboundMessages = AtomicLong() + private val outboundBytes = AtomicLong() + private val displayFramesSent = AtomicLong() + + /** Called by the communicator when a display frame's last message is acked. */ + @JvmStatic + fun recordDisplayFrameSent() { + displayFramesSent.incrementAndGet() + } + + /** Running totals of outbound BLE traffic since process start: [messages, bytes, display frames]. */ + @JvmStatic + fun sampleOutboundTraffic(): LongArray { + return longArrayOf(outboundMessages.get(), outboundBytes.get(), displayFramesSent.get()) + } + } + + private val context: Context + private val bluetoothAdapter: BluetoothAdapter + private val mainHandler = Handler(Looper.getMainLooper()) + + private val gattClients = ConcurrentHashMap() + private val bluetoothApiLock = Any() + + private val connectLatches = ConcurrentHashMap() + private val connectResults = ConcurrentHashMap() + + private val servicesLatches = ConcurrentHashMap() + private val servicesStatuses = ConcurrentHashMap() + + private val mtuLatches = ConcurrentHashMap() + private val negotiatedMtus = ConcurrentHashMap() + private val mtuStatuses = ConcurrentHashMap() + + private val descriptorLatches = ConcurrentHashMap() + private val descriptorStatuses = ConcurrentHashMap() + + private val writeLatches = ConcurrentHashMap() + private val writeStatuses = ConcurrentHashMap() + + @Volatile + private var listener: FaceclawBleListener? = null + + init { + this.context = context.applicationContext + val bluetoothManager = this.context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager? + if (bluetoothManager == null || bluetoothManager.adapter == null) { + throw IllegalStateException("Bluetooth adapter unavailable") + } + this.bluetoothAdapter = bluetoothManager.adapter + } + + fun setListener(listener: FaceclawBleListener?) { + this.listener = listener + } + + /** + * Whether Android still holds a bond (pairing) for this address. Returns + * true when the state cannot be determined, so callers only act on a + * definite BOND_NONE. + */ + fun isBonded(address: String?): Boolean { + if (address == null || address.trim().isEmpty()) { + return true + } + try { + val device = bluetoothAdapter.getRemoteDevice(address) + return device == null || device.bondState != BluetoothDevice.BOND_NONE + } catch (t: Throwable) { + return true + } + } + + /** + * Android's bond state for this address (BluetoothDevice.BOND_NONE / + * BOND_BONDING / BOND_BONDED), or -1 when it cannot be determined. + * BOND_BONDING covers the whole OS pairing flow, including a pairing + * dialog that is still waiting for the user. + */ + fun getBondState(address: String?): Int { + if (address == null || address.trim().isEmpty()) { + return -1 + } + try { + val device = bluetoothAdapter.getRemoteDevice(address) + return if (device == null) -1 else device.bondState + } catch (t: Throwable) { + return -1 + } + } + + /** Whether a GATT client for this address is open, i.e. no disconnect callback has arrived for it. */ + fun isConnected(address: String?): Boolean { + return address != null && gattClients.containsKey(address) + } + + fun connect(address: String?, timeoutMs: Int): Boolean = connect(address, timeoutMs, false) + + /** + * EXPERIMENTAL (2026-09-11): the ring specifically needs autoConnect=true. + * Even's own vendor app connects the ring with Android's autoConnect flag + * set (confirmed via its own BLE debug log, "connect() - device: ..., + * auto: true"), while every other faceclaw connection (both glasses arms) + * uses autoConnect=false. Direct connect (false) is faster to establish + * but Android supervises it less patiently; a live test tonight showed + * the ring-only direct connection dying almost exactly 5 seconds in, + * repeatedly, with nothing else competing for it - consistent with a + * companion device Android's stack expects to be babied via autoConnect, + * not direct-connected like the glasses. autoConnect=true can take much + * longer to actually complete (Android manages it as a background + * reconnect, not an immediate attempt), so this needs its own longer + * timeout - do not reuse CONNECT_TIMEOUT_MS/awaitLatch's short window for + * it, that would just trade one spurious failure for another. + */ + fun connect(address: String?, timeoutMs: Int, autoConnect: Boolean): Boolean { + if (address == null || address.trim().isEmpty()) { + throw IllegalArgumentException("address is required") + } + val latch = CountDownLatch(1) + connectLatches[address] = latch + connectResults.remove(address) + + val gattLock = gattLock(address) + synchronized(gattLock) { + val existing = gattClients[address] + if (existing != null) { + connectLatches.remove(address) + return true + } + + val device = bluetoothAdapter.getRemoteDevice(address) + if (device == null) { + connectLatches.remove(address) + throw IllegalArgumentException("remote device not found: $address") + } + + val gatt: BluetoothGatt? = device.connectGatt( + context, + autoConnect, + gattCallback, + BluetoothDevice.TRANSPORT_LE, + BluetoothDevice.PHY_LE_2M or BluetoothDevice.PHY_LE_1M + ) + if (gatt != null) { + gattClients[address] = gatt + } + + if (gatt == null) { + connectLatches.remove(address) + return false + } + + if (!awaitLatch(latch, timeoutMs)) { + connectLatches.remove(address) + connectResults.remove(address) + gattClients.remove(address, gatt) + negotiatedMtus.remove(address) + gatt.disconnect() + gatt.close() + return false + } + + val connected = connectResults.remove(address) + connectLatches.remove(address) + return connected == true + } + } + + fun requestConnectionPriority(address: String, priority: Int): Boolean { + synchronized(gattLock(address)) { + val gatt = requireGatt(address) + return gatt.requestConnectionPriority(priority) + } + } + + /** One-shot benchmark preferences; success is established by callbacks/HCI, not submission. */ + fun prepareBenchmarkLink(address: String, mode: Int) { + synchronized(gattLock(address)) { + val gatt = requireGatt(address) + if ((mode and 1) != 0) { + val accepted = gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH) + Log.i(TAG, "benchmark HIGH address=$address submitted=$accepted") + } + if ((mode and 2) != 0) { + Log.i(TAG, "benchmark request 2M address=$address") + gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK, + BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED) + } + gatt.readPhy() + } + } + + fun getNegotiatedMtu(address: String): Int { + val mtu = negotiatedMtus[address] + return mtu ?: 23 + } + + fun requestMtu(address: String, mtu: Int, timeoutMs: Int): Boolean { + synchronized(gattLock(address)) { + val latch = CountDownLatch(1) + mtuLatches[address] = latch + mtuStatuses.remove(address) + + val gatt = requireGatt(address) + val started = gatt.requestMtu(mtu) + if (!started) { + mtuLatches.remove(address) + return false + } + if (!awaitLatch(latch, timeoutMs)) { + mtuLatches.remove(address) + mtuStatuses.remove(address) + return false + } + val status = mtuStatuses.remove(address) + mtuLatches.remove(address) + return status != null && status == BluetoothGatt.GATT_SUCCESS + } + } + + fun discoverServices(address: String, timeoutMs: Int): Boolean { + synchronized(gattLock(address)) { + val latch = CountDownLatch(1) + servicesLatches[address] = latch + servicesStatuses.remove(address) + + val gatt = requireGatt(address) + val started = gatt.discoverServices() + if (!started) { + servicesLatches.remove(address) + return false + } + if (!awaitLatch(latch, timeoutMs)) { + servicesLatches.remove(address) + servicesStatuses.remove(address) + return false + } + val status = servicesStatuses.remove(address) + servicesLatches.remove(address) + return status != null && status == BluetoothGatt.GATT_SUCCESS + } + } + + fun enableNotifications(address: String, characteristicUuid: String, enable: Boolean, timeoutMs: Int): Boolean { + synchronized(gattLock(address)) { + val latch = CountDownLatch(1) + descriptorLatches[address] = latch + descriptorStatuses.remove(address) + + val gatt = requireGatt(address) + val characteristic = requireCharacteristic(gatt, characteristicUuid) + + val notificationSet = gatt.setCharacteristicNotification(characteristic, enable) + if (!notificationSet) { + descriptorLatches.remove(address) + return false + } + + val descriptor = characteristic.getDescriptor(java.util.UUID.fromString(BleProtocol.CCCD_UUID)) + if (descriptor == null) { + descriptorLatches.remove(address) + return true + } + + descriptor.value = if (enable) + BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE + else + BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE + val started = gatt.writeDescriptor(descriptor) + if (!started) { + descriptorLatches.remove(address) + descriptorStatuses.remove(address) + return false + } + if (!awaitLatch(latch, timeoutMs)) { + descriptorLatches.remove(address) + descriptorStatuses.remove(address) + return false + } + val status = descriptorStatuses.remove(address) + descriptorLatches.remove(address) + return status != null && status == BluetoothGatt.GATT_SUCCESS + } + } + + fun writeFrames( + address: String, + characteristicUuid: String, + frames: List?, + writeType: Int, + timeoutMs: Int + ): Boolean { + if (frames == null || frames.isEmpty()) { + return true + } + + val startMs = System.currentTimeMillis() + synchronized(gattLock(address)) { + val gatt = requireGatt(address) + val characteristic = requireCharacteristic(gatt, characteristicUuid) + + for (i in frames.indices) { + val frame = frames[i] + if (!startWrite(gatt, characteristic, address, frame, writeType, timeoutMs)) { + return false + } + outboundBytes.addAndGet((frame?.size ?: 0).toLong()) + } + } + outboundMessages.incrementAndGet() + val totalSize = frames.sumOf { frame -> frame?.size ?: 0 } + Log.i(TAG, "writeFrames wrote " + frames.size + " frames totaling " + totalSize + " bytes in " + (System.currentTimeMillis() - startMs) + "ms") + return true + } + + private fun startWrite( + gatt: BluetoothGatt, + characteristic: BluetoothGattCharacteristic, + address: String, + data: ByteArray?, + writeType: Int, + timeoutMs: Int + ): Boolean { + var retryCount = 0 + while (true) { + val latch = CountDownLatch(1) + writeLatches[address] = latch + writeStatuses.remove(address) + + val currentTime = System.currentTimeMillis() + val result = gatt.writeCharacteristic(characteristic, data!!, writeType) + val timeAsleep = System.currentTimeMillis() - currentTime + //Log.i(TAG, "writeCharacteristic: spent " + timeAsleep + "ms"); + + if (result == android.bluetooth.BluetoothStatusCodes.ERROR_GATT_WRITE_REQUEST_BUSY) { + writeLatches.remove(address, latch) + writeStatuses.remove(address) + if (!sleepBeforeWriteRetry(address, "busy", retryCount++)) { + return false + } + continue + } else { + //Log.i(TAG, "writeCharacteristic with writeType=" + writeType + " result=" + result + " retryCount=" + retryCount); + if (result != android.bluetooth.BluetoothStatusCodes.SUCCESS) { + writeLatches.remove(address, latch) + writeStatuses.remove(address) + if (!sleepBeforeWriteRetry(address, "start result=$result", retryCount++)) { + return false + } + continue + } + if (!awaitLatch(latch, timeoutMs)) { + writeLatches.remove(address, latch) + writeStatuses.remove(address) + return false + } + val status = writeStatuses.remove(address) + writeLatches.remove(address) + if (status != null && status == BluetoothGatt.GATT_SUCCESS) { + return true + } + if (!sleepBeforeWriteRetry(address, "callback status=$status", retryCount++)) { + return false + } + } + } + } + + private fun sleepBeforeWriteRetry(address: String, reason: String, retryIndex: Int): Boolean { + if (retryIndex >= WRITE_RETRY_DELAYS_MS.size) { + Log.w(TAG, "writeCharacteristic retry exhausted: address=$address reason=$reason") + return false + } + val delayMs = WRITE_RETRY_DELAYS_MS[retryIndex] + Log.w(TAG, "writeCharacteristic retry: address=$address reason=$reason delayMs=$delayMs") + try { + Thread.sleep(delayMs.toLong()) + return true + } catch (e: InterruptedException) { + Thread.currentThread().interrupt() + return false + } + } + + fun disconnect(address: String?) { + val gattLock = gattLock(address) + synchronized(gattLock) { + val gatt = if (address != null) gattClients.remove(address) else null + if (address != null) negotiatedMtus.remove(address) + if (gatt == null) { + return + } + gatt.disconnect() + gatt.close() + } + } + + fun close() { + for (address in gattClients.keys) { + disconnect(address) + } + } + + private fun requireGatt(address: String): BluetoothGatt { + val gatt = gattClients[address] + if (gatt == null) { + throw IllegalStateException("Not connected: $address") + } + return gatt + } + + private fun requireCharacteristic(gatt: BluetoothGatt, characteristicUuid: String): BluetoothGattCharacteristic { + val target = UUID.fromString(characteristicUuid) + for (service: BluetoothGattService in gatt.services) { + val characteristic = service.getCharacteristic(target) + if (characteristic != null) { + return characteristic + } + } + throw IllegalStateException("Characteristic not found: $characteristicUuid") + } + + private fun awaitLatch(latch: CountDownLatch, timeoutMs: Int): Boolean { + try { + return latch.await(timeoutMs.toLong(), TimeUnit.MILLISECONDS) + } catch (e: InterruptedException) { + Thread.currentThread().interrupt() + return false + } + } + + private fun gattLock(address: String?): Any { + // Android's Bluetooth stack has process-wide command-pipeline constraints on some devices. + // Keep every BluetoothGatt API call serialized globally, even for different MAC addresses. + return bluetoothApiLock + } + + private val gattCallback: BluetoothGattCallback = object : BluetoothGattCallback() { + override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) { + Log.i(TAG, "onConnectionStateChange: status=$status newState=$newState") + val address = gatt.device.address + + if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { + connectResults[address] = true + val latch = connectLatches.remove(address) + latch?.countDown() + dispatchConnectionState(address, true) + return + } + + if (newState == BluetoothProfile.STATE_DISCONNECTED) { + connectResults[address] = false + val latch = connectLatches.remove(address) + latch?.countDown() + val gattLock = gattLock(address) + synchronized(gattLock) { + gattClients.remove(address, gatt) + negotiatedMtus.remove(address) + gatt.close() + } + dispatchConnectionState(address, false) + } + } + + override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) { + val address = gatt.device.address + servicesStatuses[address] = status + val latch = servicesLatches.remove(address) + latch?.countDown() + } + + override fun onMtuChanged(gatt: BluetoothGatt, mtu: Int, status: Int) { + val address = gatt.device.address + if (status == BluetoothGatt.GATT_SUCCESS) negotiatedMtus[address] = mtu + mtuStatuses[address] = status + val latch = mtuLatches.remove(address) + latch?.countDown() + } + + override fun onPhyRead(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int) { + Log.i(TAG, "onPhyRead: address=" + gatt.device.address + + " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status) + } + + override fun onPhyUpdate(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int) { + Log.i(TAG, "onPhyUpdate: address=" + gatt.device.address + + " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status) + } + + override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) { + val address = gatt.device.address + descriptorStatuses[address] = status + val latch = descriptorLatches.remove(address) + latch?.countDown() + } + + override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) { + val address = gatt.device.address + writeStatuses[address] = status + val latch = writeLatches.remove(address) + latch?.countDown() + } + + override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, value: ByteArray) { + dispatchNotification(gatt.device.address, characteristic.uuid.toString(), value) + } + + @Deprecated("Deprecated in Java") + override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) { + dispatchNotification(gatt.device.address, characteristic.uuid.toString(), characteristic.value) + } + } + + private fun dispatchConnectionState(address: String, connected: Boolean) { + val current = listener ?: return + current.onConnectionStateChange(address, connected) + } + + private fun dispatchNotification(address: String, characteristicUuid: String, data: ByteArray?) { + val current = listener ?: return + val copy = data?.clone() ?: ByteArray(0) + current.onNotification(address, characteristicUuid, copy) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.java deleted file mode 100644 index fbdb25af..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.faceclaw.app; - -import android.content.ContentUris; -import android.content.Context; -import android.database.Cursor; -import android.net.Uri; -import android.provider.CalendarContract; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -/** - * Reads upcoming events from the Android Calendar provider for the Calendar - * app. Queries the Instances table (rather than Events) so that recurring - * events are expanded into concrete occurrences within the requested window. - * Requires the READ_CALENDAR runtime permission; without it the content - * resolver throws SecurityException and this returns an empty array. - */ -public final class FaceclawCalendarProvider { - private static final String TAG = "FaceclawCalendar"; - - private static final String[] PROJECTION = { - CalendarContract.Instances.EVENT_ID, - CalendarContract.Instances.TITLE, - CalendarContract.Instances.BEGIN, - CalendarContract.Instances.END, - CalendarContract.Instances.ALL_DAY, - CalendarContract.Instances.EVENT_LOCATION, - CalendarContract.Instances.CALENDAR_DISPLAY_NAME, - }; - - private FaceclawCalendarProvider() { - } - - /** - * JSON array of upcoming events starting from now through now+windowMs, - * ordered by start time, capped at maxEvents. Each element carries id, - * title, startMs, endMs, allDay, location, and calendarName. - */ - public static String getUpcomingEventsJson(Context context, int maxEvents, long windowMs) { - if (context == null || maxEvents <= 0) { - return "[]"; - } - long now = System.currentTimeMillis(); - long end = now + Math.max(0L, windowMs); - int limit = Math.min(200, maxEvents); - - Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon(); - ContentUris.appendId(builder, now); - ContentUris.appendId(builder, end); - Uri uri = builder.build(); - - JSONArray out = new JSONArray(); - Cursor cursor = null; - try { - cursor = context.getContentResolver().query( - uri, - PROJECTION, - null, - null, - CalendarContract.Instances.BEGIN + " ASC"); - if (cursor != null) { - while (cursor.moveToNext() && out.length() < limit) { - try { - out.put(buildEventJson(cursor)); - } catch (JSONException e) { - Log.w(TAG, "failed to serialize calendar event", e); - } - } - } - } catch (SecurityException e) { - Log.w(TAG, "calendar access denied while reading events", e); - return "[]"; - } catch (Throwable t) { - Log.w(TAG, "failed to read calendar events", t); - return "[]"; - } finally { - if (cursor != null) { - cursor.close(); - } - } - return out.toString(); - } - - private static JSONObject buildEventJson(Cursor cursor) throws JSONException { - JSONObject event = new JSONObject(); - event.put("id", cursor.getLong(0)); - event.put("title", cursor.isNull(1) ? "" : cursor.getString(1)); - event.put("startMs", cursor.getLong(2)); - event.put("endMs", cursor.getLong(3)); - event.put("allDay", cursor.getInt(4) != 0); - event.put("location", cursor.isNull(5) ? "" : cursor.getString(5)); - event.put("calendarName", cursor.isNull(6) ? "" : cursor.getString(6)); - return event; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.kt new file mode 100644 index 00000000..ed7cec22 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.kt @@ -0,0 +1,166 @@ +package com.faceclaw.app + +import android.content.ContentUris +import android.content.Context +import android.database.Cursor +import android.provider.CalendarContract +import android.util.Log + +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +import java.util.Calendar +import java.util.TimeZone + +/** + * Reads upcoming events from the Android Calendar provider for the Calendar + * app. Queries the Instances table (rather than Events) so that recurring + * events are expanded into concrete occurrences within the requested window. + * Requires the READ_CALENDAR runtime permission; without it the content + * resolver throws SecurityException and this returns an empty array. + */ +class FaceclawCalendarProvider private constructor() { + companion object { + private const val TAG = "FaceclawCalendar" + private const val DAY_MS = 24L * 60 * 60 * 1000 + + private val PROJECTION = arrayOf( + CalendarContract.Instances.EVENT_ID, + CalendarContract.Instances.TITLE, + CalendarContract.Instances.BEGIN, + CalendarContract.Instances.END, + CalendarContract.Instances.ALL_DAY, + CalendarContract.Instances.EVENT_LOCATION, + CalendarContract.Instances.CALENDAR_DISPLAY_NAME, + ) + + private class Event( + val id: Long, + val title: String, + val startMs: Long, + val endMs: Long, + val allDay: Boolean, + val location: String, + val calendarName: String, + ) + + /** + * JSON array of events that haven't ended yet and start before + * now+windowMs, ordered by start time, capped at maxEvents. Each + * element carries id, title, startMs, endMs, allDay, location, and + * calendarName. All-day events are reported as local-midnight + * boundaries (matching EventKit on iOS), not the UTC-midnight + * boundaries the provider stores them as. + */ + @JvmStatic + fun getUpcomingEventsJson(context: Context?, maxEvents: Int, windowMs: Long): String { + if (context == null || maxEvents <= 0) { + return "[]" + } + val now = System.currentTimeMillis() + val end = now + Math.max(0L, windowMs) + val limit = Math.min(200, maxEvents) + + // The provider matches all-day instances by their UTC-midnight + // bounds, which can be up to a day away from the local day they + // represent; widen the query and filter after converting. + val builder = CalendarContract.Instances.CONTENT_URI.buildUpon() + ContentUris.appendId(builder, now - DAY_MS) + ContentUris.appendId(builder, end + DAY_MS) + val uri = builder.build() + + val events = ArrayList() + var cursor: Cursor? = null + try { + cursor = context.contentResolver.query( + uri, + PROJECTION, + null, + null, + CalendarContract.Instances.BEGIN + " ASC") + if (cursor != null) { + val utc = Calendar.getInstance(TimeZone.getTimeZone("UTC")) + val local = Calendar.getInstance() + // Rows arrive in raw-BEGIN order, but converting all-day + // events moves them by less than a day. Once `limit` + // events are kept, nothing starting after the latest of + // them can make the cut, and no row whose raw BEGIN is a + // day past that can start before it. + var cutoff = Long.MAX_VALUE + while (cursor.moveToNext()) { + if (cutoff != Long.MAX_VALUE && cursor.getLong(2) - DAY_MS > cutoff) break + val event = readEvent(cursor, utc, local) + if (event.endMs <= now || event.startMs > end || event.startMs > cutoff) continue + events.add(event) + if (events.size == limit) cutoff = events.maxOf { it.startMs } + } + } + } catch (e: SecurityException) { + Log.w(TAG, "calendar access denied while reading events", e) + return "[]" + } catch (t: Throwable) { + Log.w(TAG, "failed to read calendar events", t) + return "[]" + } finally { + cursor?.close() + } + + events.sortBy { it.startMs } + val out = JSONArray() + for (event in events.take(limit)) { + try { + out.put(buildEventJson(event)) + } catch (e: JSONException) { + Log.w(TAG, "failed to serialize calendar event", e) + } + } + return out.toString() + } + + private fun readEvent(cursor: Cursor, utc: Calendar, local: Calendar): Event { + val allDay = cursor.getInt(4) != 0 + var startMs = cursor.getLong(2) + var endMs = cursor.getLong(3) + if (allDay) { + startMs = utcMidnightToLocal(startMs, utc, local) + endMs = utcMidnightToLocal(endMs, utc, local) + } + return Event( + id = cursor.getLong(0), + title = if (cursor.isNull(1)) "" else cursor.getString(1), + startMs = startMs, + endMs = endMs, + allDay = allDay, + location = if (cursor.isNull(5)) "" else cursor.getString(5), + calendarName = if (cursor.isNull(6)) "" else cursor.getString(6), + ) + } + + /** + * All-day events are stored as UTC midnights (CalendarContract + * requires EVENT_TIMEZONE=UTC for them); re-anchor that calendar + * date to midnight in the device's timezone. + */ + private fun utcMidnightToLocal(utcMs: Long, utc: Calendar, local: Calendar): Long { + utc.timeInMillis = utcMs + local.timeZone = TimeZone.getDefault() + local.clear() + local.set(utc.get(Calendar.YEAR), utc.get(Calendar.MONTH), utc.get(Calendar.DAY_OF_MONTH)) + return local.timeInMillis + } + + @Throws(JSONException::class) + private fun buildEventJson(event: Event): JSONObject { + val json = JSONObject() + json.put("id", event.id) + json.put("title", event.title) + json.put("startMs", event.startMs) + json.put("endMs", event.endMs) + json.put("allDay", event.allDay) + json.put("location", event.location) + json.put("calendarName", event.calendarName) + return json + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.java deleted file mode 100644 index 488d2bca..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.java +++ /dev/null @@ -1,446 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import com.k2fsa.sherpa.onnx.FeatureConfig; -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 java.io.File; -import java.util.ArrayDeque; -import java.util.Arrays; - -/** - * Continuous captioning over the decoded mic PCM: segments speech into - * utterances with an adaptive energy gate, transcribes each utterance with - * the on-device Moonshine model, and attaches a speaker voice-print per - * utterance. PCM is pushed in from the TS side (which owns mic arbitration - * and any beam-direction gating), so this engine has no BLE dependencies. - * - * Utterance boundaries are measured on the sample clock, not the wall clock: - * BLE delivers mic packets in bursts, so elapsed real time overestimates the - * audio heard. The TS side converts startMs/endMs to wall-clock times using - * the engine start timestamp. - */ -public class FaceclawCaptionEngine { - private static final String TAG = "FaceclawCaptions"; - private static final int SAMPLE_RATE = 16000; - private static final int FEATURE_DIM = 80; - private static final int MAX_QUEUE_PACKETS = 200; - - // Segmentation: RMS thresholds relative to a rolling noise floor, in the - // same spirit as FaceclawVoiceController.EndpointDetector but recurring. - private static final double ONSET_FACTOR = 3.0; - private static final double RELEASE_FACTOR = 1.8; - private static final double MIN_RMS = 220.0; - private static final double NOISE_EMA_ALPHA = 0.05; - private static final int PRE_ROLL_MS = 400; - private static final int DEFAULT_SILENCE_MS = 800; - private static final int MIN_UTTERANCE_MS = 350; - private static final int MAX_UTTERANCE_MS = 15000; - // Moonshine v2 fails past ~9.1 s of input; decode long utterances in - // segments cut at the quietest window (mirrors FaceclawVoiceController). - private static final int DECODE_SEGMENT_MAX_SAMPLES = SAMPLE_RATE * 8; - private static final int CUT_SEARCH_SAMPLES = SAMPLE_RATE * 2; - private static final int CUT_WINDOW_SAMPLES = SAMPLE_RATE * 30 / 1000; - private static final float NORMALIZE_TARGET_PEAK = 0.9f; - private static final float NORMALIZE_MAX_GAIN = 30f; - // Voice-prints degrade on very long inputs; embed at most the first 10 s. - private static final int EMBED_MAX_SAMPLES = SAMPLE_RATE * 10; - - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final Object lock = new Object(); - private final Object queueLock = new Object(); - private final ArrayDeque queue = new ArrayDeque<>(); - - private volatile FaceclawCaptionEngineListener listener; - private volatile String asrModelDir; - private volatile String speakerModelPath; - private volatile int silenceMs = DEFAULT_SILENCE_MS; - private Thread workerThread; - private volatile boolean started; - - private OfflineRecognizer recognizer; - private FaceclawSpeakerId speakerId; - - // Segmentation state (worker thread only). - private long totalSamples; - private double noiseFloor; - private boolean inUtterance; - private long utteranceStartSample; - private long silenceRunSamples; - private double utterancePeakRms; - private short[] utterance = new short[SAMPLE_RATE * (MAX_UTTERANCE_MS / 1000)]; - private int utteranceLength; - private final short[] preRoll = new short[SAMPLE_RATE * PRE_ROLL_MS / 1000]; - private int preRollLength; - - public void setListener(FaceclawCaptionEngineListener listener) { - this.listener = listener; - } - - /** Directory holding the Moonshine model files, or null to disable ASR. */ - public void setAsrModelDir(String dir) { - this.asrModelDir = dir; - } - - /** Speaker-embedding ONNX model path, or null to disable voice-prints. */ - public void setSpeakerModelPath(String path) { - this.speakerModelPath = path; - } - - public void setSilenceMs(int ms) { - this.silenceMs = Math.max(200, Math.min(3000, ms)); - } - - public void start() { - synchronized (lock) { - if (started) { - return; - } - started = true; - workerThread = new Thread(this::runLoop, "FaceclawCaptionEngine"); - workerThread.start(); - } - } - - public void stop() { - Thread threadToJoin; - synchronized (lock) { - if (!started) { - return; - } - started = false; - threadToJoin = workerThread; - workerThread = null; - } - synchronized (queueLock) { - queueLock.notifyAll(); - } - if (threadToJoin != null) { - threadToJoin.interrupt(); - if (Thread.currentThread() != threadToJoin) { - try { - threadToJoin.join(2000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - } - } - - /** Push decoded 16 kHz mono S16LE PCM (any chunking). */ - public void acceptPcm(byte[] pcm16le) { - if (!started || pcm16le == null || pcm16le.length < 2) { - return; - } - synchronized (queueLock) { - if (queue.size() >= MAX_QUEUE_PACKETS) { - queue.removeFirst(); - } - queue.addLast(pcm16le); - queueLock.notifyAll(); - } - } - - private void runLoop() { - try { - loadModels(); - resetSegmentation(); - while (started && !Thread.currentThread().isInterrupted()) { - byte[] chunk = takeChunk(); - if (chunk == null) { - continue; - } - processChunk(chunk); - } - // Flush a trailing utterance so its text isn't lost on stop. - if (inUtterance && utteranceLength > 0) { - finalizeUtterance(); - } - } catch (Throwable t) { - Log.e(TAG, "caption engine failed", t); - emitStatus("Captions failed: " + t.getMessage()); - } finally { - releaseModels(); - } - } - - private void loadModels() { - String modelDir = asrModelDir; - if (modelDir != null && new File(modelDir, "tokens.txt").exists()) { - emitStatus("Loading caption model..."); - recognizer = new OfflineRecognizer(OfflineRecognizerConfig.builder() - .setFeatureConfig(FeatureConfig.builder() - .setSampleRate(SAMPLE_RATE) - .setFeatureDim(FEATURE_DIM) - .build()) - .setModelConfig(OfflineModelConfig.builder() - .setMoonshine(OfflineMoonshineModelConfig.builder() - .setEncoder(new File(modelDir, "encoder_model.ort").getAbsolutePath()) - .setMergedDecoder(new File(modelDir, "decoder_model_merged.ort").getAbsolutePath()) - .build()) - .setTokens(new File(modelDir, "tokens.txt").getAbsolutePath()) - .setNumThreads(1) - .build()) - .build()); - } else { - recognizer = null; - } - String embedModel = speakerModelPath; - if (embedModel != null && new File(embedModel).exists()) { - speakerId = new FaceclawSpeakerId(embedModel); - speakerId.ensureLoaded(); - } else { - speakerId = null; - } - emitStatus(recognizer != null ? "Captions listening..." : "Captions listening (no ASR model)..."); - } - - private void releaseModels() { - if (recognizer != null) { - recognizer.release(); - recognizer = null; - } - if (speakerId != null) { - speakerId.close(); - speakerId = null; - } - } - - private byte[] takeChunk() { - synchronized (queueLock) { - while (started && queue.isEmpty()) { - try { - queueLock.wait(250); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return null; - } - } - return queue.pollFirst(); - } - } - - private void resetSegmentation() { - totalSamples = 0; - noiseFloor = 0; - inUtterance = false; - utteranceLength = 0; - preRollLength = 0; - silenceRunSamples = 0; - utterancePeakRms = 0; - } - - private void processChunk(byte[] chunk) { - int count = chunk.length / 2; - short[] pcm = new short[count]; - double sumSquares = 0; - for (int i = 0; i < count; i++) { - short s = (short) ((chunk[i * 2] & 0xff) | (chunk[i * 2 + 1] << 8)); - pcm[i] = s; - sumSquares += (double) s * s; - } - double rms = Math.sqrt(sumSquares / Math.max(1, count)); - totalSamples += count; - - if (!inUtterance) { - // Track the noise floor only while idle so speech doesn't raise it. - noiseFloor = noiseFloor == 0 ? rms : noiseFloor * (1 - NOISE_EMA_ALPHA) + rms * NOISE_EMA_ALPHA; - double threshold = Math.max(noiseFloor, MIN_RMS); - if (rms >= threshold * ONSET_FACTOR) { - inUtterance = true; - utteranceLength = 0; - silenceRunSamples = 0; - utterancePeakRms = rms; - appendUtterance(preRoll, preRollLength); - utteranceStartSample = Math.max(0, totalSamples - count - preRollLength); - appendUtterance(pcm, count); - emitSpeechStart(utteranceStartSample * 1000 / SAMPLE_RATE); - } else { - appendPreRoll(pcm, count); - } - return; - } - - appendUtterance(pcm, count); - utterancePeakRms = Math.max(utterancePeakRms, rms); - double threshold = Math.max(noiseFloor, MIN_RMS); - if (rms < threshold * RELEASE_FACTOR) { - silenceRunSamples += count; - } else { - silenceRunSamples = 0; - } - long utteranceMs = (long) utteranceLength * 1000 / SAMPLE_RATE; - boolean silenceEnded = silenceRunSamples * 1000L / SAMPLE_RATE >= silenceMs; - if (silenceEnded || utteranceMs >= MAX_UTTERANCE_MS) { - finalizeUtterance(); - inUtterance = false; - preRollLength = 0; - } - } - - private void appendPreRoll(short[] pcm, int count) { - // Keep the last PRE_ROLL_MS of idle audio so onset consonants survive. - if (count >= preRoll.length) { - System.arraycopy(pcm, count - preRoll.length, preRoll, 0, preRoll.length); - preRollLength = preRoll.length; - return; - } - int keep = Math.min(preRollLength, preRoll.length - count); - System.arraycopy(preRoll, preRollLength - keep, preRoll, 0, keep); - System.arraycopy(pcm, 0, preRoll, keep, count); - preRollLength = keep + count; - } - - private void appendUtterance(short[] pcm, int count) { - int room = utterance.length - utteranceLength; - int copied = Math.min(room, count); - if (copied > 0) { - System.arraycopy(pcm, 0, utterance, utteranceLength, copied); - utteranceLength += copied; - } - } - - private void finalizeUtterance() { - int length = utteranceLength; - long startMs = utteranceStartSample * 1000 / SAMPLE_RATE; - long endMs = (utteranceStartSample + length) * 1000 / SAMPLE_RATE; - if (endMs - startMs < MIN_UTTERANCE_MS) { - return; - } - String text = recognizeUtterance(length); - float[] embedding = embedUtterance(length); - emitUtterance(text, embedding, startMs, endMs, utterancePeakRms); - } - - private String recognizeUtterance(int length) { - if (recognizer == null || length <= 0) { - return ""; - } - StringBuilder joined = new StringBuilder(); - int offset = 0; - while (offset < length) { - int remaining = length - offset; - int segment = Math.min(remaining, DECODE_SEGMENT_MAX_SAMPLES); - if (remaining > DECODE_SEGMENT_MAX_SAMPLES) { - segment = findQuietCut(offset, segment); - } - String part = recognizeRange(offset, segment); - if (part.length() > 0) { - if (joined.length() > 0 && ".,!?;:%)]}".indexOf(part.charAt(0)) < 0) { - joined.append(' '); - } - joined.append(part); - } - offset += segment; - } - return joined.toString().trim(); - } - - /** - * End a decode segment at the center of the quietest window near its end - * so the cut lands between words (same approach as the PTT controller). - */ - private int findQuietCut(int offset, int segment) { - int searchStart = Math.max(0, segment - CUT_SEARCH_SAMPLES); - int win = CUT_WINDOW_SAMPLES; - if (segment - searchStart <= win) { - return segment; - } - double sum = 0; - for (int i = searchStart; i < searchStart + win; i++) { - double s = utterance[offset + i]; - sum += s * s; - } - double best = sum; - int bestStart = searchStart; - for (int start = searchStart + 1; start + win <= segment; start++) { - double dropped = utterance[offset + start - 1]; - double added = utterance[offset + start + win - 1]; - sum += added * added - dropped * dropped; - if (sum < best) { - best = sum; - bestStart = start; - } - } - return bestStart + win / 2; - } - - private String recognizeRange(int offset, int count) { - float[] samples = new float[count]; - float peak = 0f; - for (int i = 0; i < count; i++) { - float v = utterance[offset + i] / 32768.0f; - samples[i] = v; - float a = Math.abs(v); - if (a > peak) { - peak = a; - } - } - if (peak > 0f) { - float gain = Math.min(NORMALIZE_TARGET_PEAK / peak, NORMALIZE_MAX_GAIN); - if (gain > 1f) { - for (int i = 0; i < count; i++) { - samples[i] *= gain; - } - } - } - OfflineStream stream = recognizer.createStream(); - try { - stream.acceptWaveform(samples, SAMPLE_RATE); - recognizer.decode(stream); - OfflineRecognizerResult result = recognizer.getResult(stream); - String raw = result == null ? "" : result.getText(); - return raw == null ? "" : raw.trim(); - } finally { - stream.release(); - } - } - - private float[] embedUtterance(int length) { - FaceclawSpeakerId currentSpeakerId = speakerId; - if (currentSpeakerId == null || length <= 0) { - return null; - } - int count = Math.min(length, EMBED_MAX_SAMPLES); - byte[] le = new byte[count * 2]; - for (int i = 0; i < count; i++) { - short s = utterance[i]; - le[i * 2] = (byte) (s & 0xff); - le[i * 2 + 1] = (byte) ((s >> 8) & 0xff); - } - return currentSpeakerId.embed(le, SAMPLE_RATE); - } - - private void emitUtterance(String text, float[] embedding, long startMs, long endMs, double peakRms) { - FaceclawCaptionEngineListener currentListener = listener; - if (currentListener == null) { - return; - } - float[] embeddingCopy = embedding == null ? null : Arrays.copyOf(embedding, embedding.length); - mainHandler.post(() -> currentListener.onUtterance(text, embeddingCopy, startMs, endMs, peakRms)); - } - - private void emitSpeechStart(long startMs) { - FaceclawCaptionEngineListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(() -> currentListener.onSpeechStart(startMs)); - } - - private void emitStatus(String status) { - FaceclawCaptionEngineListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(() -> currentListener.onStatus(status)); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.kt new file mode 100644 index 00000000..d985766e --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngine.kt @@ -0,0 +1,75 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper + +import java.io.File + +/** + * Continuous captioning over the decoded mic PCM: the shared [CaptionSession] segments speech + * into utterances with an adaptive energy gate, transcribes each with the on-device Moonshine + * model and attaches a speaker voice-print. This Android class only supplies the sherpa-onnx + * recognizer and speaker model and delivers callbacks on the main thread. PCM is pushed in + * from the TS side (which owns mic arbitration and any beam-direction gating), so the engine + * has no BLE dependencies. + * + * Utterance boundaries are measured on the sample clock, not the wall clock; the TS side + * converts startMs/endMs to wall-clock times using the engine start timestamp. + */ +class FaceclawCaptionEngine { + private val mainHandler = Handler(Looper.getMainLooper()) + + private val host = object : CaptionHost { + override val dispatcher = CallbackDispatcher { action -> mainHandler.post { action() } } + + override fun loadTranscriber(modelDir: String): OfflineTranscriber? { + if (!File(modelDir, "tokens.txt").exists()) { + return null + } + return AndroidSpeechEngines.SherpaTranscriber( + AndroidSpeechEngines.recognizerConfig(File(modelDir), VoiceModelKind.MOONSHINE)) + } + + override fun loadEmbedder(modelPath: String): SpeakerEmbedder? { + if (!File(modelPath).exists()) { + return null + } + val speakerId = FaceclawSpeakerId(modelPath) + speakerId.ensureLoaded() + return speakerId + } + } + + private val session = CaptionSession(host) + + fun setListener(listener: FaceclawCaptionEngineListener?) { + session.setListener(listener) + } + + /** Directory holding the Moonshine model files, or null to disable ASR. */ + fun setAsrModelDir(dir: String?) { + session.setAsrModelDir(dir) + } + + /** Speaker-embedding ONNX model path, or null to disable voice-prints. */ + fun setSpeakerModelPath(path: String?) { + session.setSpeakerModelPath(path) + } + + fun setSilenceMs(ms: Int) { + session.setSilenceMs(ms) + } + + fun start() { + session.start() + } + + fun stop() { + session.stop() + } + + /** Push decoded 16 kHz mono S16LE PCM (any chunking). */ + fun acceptPcm(pcm16le: ByteArray?) { + session.acceptPcm(pcm16le) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngineListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngineListener.java deleted file mode 100644 index 24b58a85..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCaptionEngineListener.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawCaptionEngineListener { - /** - * One finished utterance. text may be empty when no ASR model is loaded - * or recognition produced nothing; embedding is the L2-normalized - * voice-print or null when the speaker model is unavailable or the - * utterance was too short. startMs/endMs are on the engine's sample - * clock (milliseconds of audio since start()). - */ - void onUtterance(String text, float[] embedding, long startMs, long endMs, double peakRms); - - /** A speech onset was detected; the utterance is now being collected. */ - void onSpeechStart(long startMs); - - void onStatus(String status); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCompassListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCompassListener.java deleted file mode 100644 index 1d6bb06e..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCompassListener.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.faceclaw.app; - -/** Receives stock compass heading and calibration notifications on the thread that subscribed. */ -public interface FaceclawCompassListener { - void onCompassEvent(int command, int headingDegrees, int magneticAccuracy, int magneticAnomalies, - int orientationSource, int diagnosticFlags, long sampleTimeMs); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.java deleted file mode 100644 index 38f723fe..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.java +++ /dev/null @@ -1,778 +0,0 @@ -package com.faceclaw.app; - -import android.content.ContentValues; -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; -import android.util.Base64; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.File; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.ArrayList; -import java.util.List; - -/** - * Persistent store for caption sessions, transcript segments, and speaker - * voice profiles, backing the Microphones app and its phone-side review UI. - * All complex values cross the JS bridge as JSON strings; voice-print - * embeddings are float32 LE blobs, exposed as base64. - */ -public class FaceclawConversationStore extends SQLiteOpenHelper { - private static final String TAG = "FaceclawConvStore"; - private static final String DB_NAME = "faceclaw-conversations.db"; - private static final int DB_VERSION = 2; - - private static FaceclawConversationStore instance; - - public static synchronized FaceclawConversationStore getInstance(Context context) { - if (instance == null) { - instance = new FaceclawConversationStore(context.getApplicationContext()); - } - return instance; - } - - private FaceclawConversationStore(Context context) { - super(context, DB_NAME, null, DB_VERSION); - } - - @Override - public void onCreate(SQLiteDatabase db) { - db.execSQL("CREATE TABLE speakers (" - + "id INTEGER PRIMARY KEY AUTOINCREMENT," - + "name TEXT NOT NULL," - + "color TEXT NOT NULL DEFAULT '#4FC3F7'," - + "is_wearer INTEGER NOT NULL DEFAULT 0," - + "embedding BLOB," - + "embedding_count INTEGER NOT NULL DEFAULT 0," - + "created_at INTEGER NOT NULL," - + "last_heard_at INTEGER," - + "tag TEXT," - + "last_recap TEXT," - + "action_items TEXT," - + "facts TEXT," - + "insights_updated_at INTEGER," - + "insights_session_id INTEGER)"); - db.execSQL("CREATE TABLE sessions (" - + "id INTEGER PRIMARY KEY AUTOINCREMENT," - + "started_at INTEGER NOT NULL," - + "ended_at INTEGER," - + "title TEXT," - + "audio_path TEXT," - + "audio_codec TEXT," - + "avg_sentiment REAL," - + "segment_count INTEGER NOT NULL DEFAULT 0)"); - db.execSQL("CREATE TABLE segments (" - + "id INTEGER PRIMARY KEY AUTOINCREMENT," - + "session_id INTEGER NOT NULL," - + "speaker_id INTEGER," - + "started_at INTEGER NOT NULL," - + "ended_at INTEGER NOT NULL," - + "audio_offset_ms INTEGER," - + "text TEXT NOT NULL," - + "lang TEXT," - + "translation TEXT," - + "translation_lang TEXT," - + "sentiment REAL," - + "emotion TEXT," - + "search_meta TEXT," - + "angle INTEGER," - + "embedding BLOB)"); - db.execSQL("CREATE INDEX idx_segments_session ON segments(session_id)"); - db.execSQL("CREATE INDEX idx_segments_speaker ON segments(speaker_id)"); - db.execSQL("CREATE INDEX idx_segments_time ON segments(started_at)"); - db.execSQL("CREATE INDEX idx_sessions_time ON sessions(started_at)"); - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - if (oldVersion < 2) { - // v2: per-speaker conversation insights (recap of the most recent - // conversation, its action items, and accumulated inferred facts). - db.execSQL("ALTER TABLE speakers ADD COLUMN last_recap TEXT"); - db.execSQL("ALTER TABLE speakers ADD COLUMN action_items TEXT"); - db.execSQL("ALTER TABLE speakers ADD COLUMN facts TEXT"); - db.execSQL("ALTER TABLE speakers ADD COLUMN insights_updated_at INTEGER"); - db.execSQL("ALTER TABLE speakers ADD COLUMN insights_session_id INTEGER"); - } - } - - // ---- sessions ---- - - public long startSession(long startedAtMs, String title) { - ContentValues values = new ContentValues(); - values.put("started_at", startedAtMs); - values.put("title", title == null || title.isEmpty() ? null : title); - return getWritableDatabase().insert("sessions", null, values); - } - - public void endSession(long sessionId, long endedAtMs, String audioPath, String audioCodec, double avgSentiment) { - ContentValues values = new ContentValues(); - values.put("ended_at", endedAtMs); - if (audioPath != null && !audioPath.isEmpty()) { - values.put("audio_path", audioPath); - values.put("audio_codec", audioCodec); - } - values.put("avg_sentiment", avgSentiment); - getWritableDatabase().update("sessions", values, "id=?", new String[]{String.valueOf(sessionId)}); - } - - public void setSessionAudio(long sessionId, String audioPath, String audioCodec) { - ContentValues values = new ContentValues(); - values.put("audio_path", audioPath); - values.put("audio_codec", audioCodec); - getWritableDatabase().update("sessions", values, "id=?", new String[]{String.valueOf(sessionId)}); - } - - public String getSessionAudioPath(long sessionId) { - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT audio_path FROM sessions WHERE id=?", new String[]{String.valueOf(sessionId)})) { - if (c.moveToFirst()) { - return c.isNull(0) ? "" : c.getString(0); - } - } - return ""; - } - - /** - * Sessions, newest first, as JSON. filterJson supports: sinceMs, untilMs, - * speakerId, emotion (matches any segment emotion in the session), query - * (LIKE over title and segment text/search metadata), limit. - */ - public String querySessions(String filterJson) { - try { - JSONObject filter = filterJson == null || filterJson.isEmpty() - ? new JSONObject() : new JSONObject(filterJson); - StringBuilder sql = new StringBuilder( - "SELECT DISTINCT s.id, s.started_at, s.ended_at, s.title, s.audio_path, s.audio_codec," - + " s.avg_sentiment, s.segment_count FROM sessions s"); - List args = new ArrayList<>(); - List where = new ArrayList<>(); - boolean joinSegments = filter.has("speakerId") || filter.has("emotion") || filter.has("query"); - if (joinSegments) { - sql.append(" LEFT JOIN segments g ON g.session_id = s.id"); - } - if (filter.has("sinceMs")) { - where.add("s.started_at >= ?"); - args.add(String.valueOf(filter.getLong("sinceMs"))); - } - if (filter.has("untilMs")) { - where.add("s.started_at <= ?"); - args.add(String.valueOf(filter.getLong("untilMs"))); - } - if (filter.has("speakerId")) { - where.add("g.speaker_id = ?"); - args.add(String.valueOf(filter.getLong("speakerId"))); - } - if (filter.has("emotion")) { - where.add("g.emotion = ?"); - args.add(filter.getString("emotion")); - } - if (filter.has("query")) { - String like = "%" + filter.getString("query") + "%"; - where.add("(s.title LIKE ? OR g.text LIKE ? OR g.search_meta LIKE ?)"); - args.add(like); - args.add(like); - args.add(like); - } - if (!where.isEmpty()) { - sql.append(" WHERE ").append(String.join(" AND ", where)); - } - sql.append(" ORDER BY s.started_at DESC"); - int limit = filter.optInt("limit", 200); - sql.append(" LIMIT ").append(Math.max(1, Math.min(limit, 1000))); - - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[0]))) { - while (c.moveToNext()) { - JSONObject row = new JSONObject(); - long id = c.getLong(0); - row.put("id", id); - row.put("startedAt", c.getLong(1)); - row.put("endedAt", c.isNull(2) ? JSONObject.NULL : c.getLong(2)); - row.put("title", c.isNull(3) ? "" : c.getString(3)); - row.put("audioPath", c.isNull(4) ? "" : c.getString(4)); - row.put("audioCodec", c.isNull(5) ? "" : c.getString(5)); - row.put("avgSentiment", c.isNull(6) ? 0 : c.getDouble(6)); - row.put("segmentCount", c.getLong(7)); - row.put("speakers", sessionSpeakersJson(id)); - out.put(row); - } - } - return out.toString(); - } catch (JSONException e) { - Log.w(TAG, "querySessions failed", e); - return "[]"; - } - } - - private JSONArray sessionSpeakersJson(long sessionId) throws JSONException { - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT DISTINCT p.id, p.name, p.color FROM segments g" - + " JOIN speakers p ON p.id = g.speaker_id WHERE g.session_id=?", - new String[]{String.valueOf(sessionId)})) { - while (c.moveToNext()) { - JSONObject row = new JSONObject(); - row.put("id", c.getLong(0)); - row.put("name", c.getString(1)); - row.put("color", c.getString(2)); - out.put(row); - } - } - return out; - } - - // ---- segments ---- - - /** - * Insert one transcript segment. json fields: sessionId, speakerId?, - * startedAt, endedAt, audioOffsetMs?, text, lang?, translation?, - * translationLang?, sentiment?, emotion?, searchMeta?, angle?, - * embeddingBase64?. Returns the new row id, or -1. - */ - public long insertSegment(String json) { - try { - JSONObject s = new JSONObject(json); - ContentValues values = new ContentValues(); - long sessionId = s.getLong("sessionId"); - values.put("session_id", sessionId); - if (s.has("speakerId")) { - values.put("speaker_id", s.getLong("speakerId")); - } - values.put("started_at", s.getLong("startedAt")); - values.put("ended_at", s.getLong("endedAt")); - if (s.has("audioOffsetMs")) { - values.put("audio_offset_ms", s.getLong("audioOffsetMs")); - } - values.put("text", s.getString("text")); - putOptString(values, s, "lang", "lang"); - putOptString(values, s, "translation", "translation"); - putOptString(values, s, "translationLang", "translation_lang"); - if (s.has("sentiment")) { - values.put("sentiment", s.getDouble("sentiment")); - } - putOptString(values, s, "emotion", "emotion"); - putOptString(values, s, "searchMeta", "search_meta"); - if (s.has("angle")) { - values.put("angle", s.getInt("angle")); - } - if (s.has("embeddingBase64")) { - values.put("embedding", Base64.decode(s.getString("embeddingBase64"), Base64.NO_WRAP)); - } - SQLiteDatabase db = getWritableDatabase(); - long id = db.insert("segments", null, values); - db.execSQL("UPDATE sessions SET segment_count = segment_count + 1 WHERE id=?", - new Object[]{sessionId}); - return id; - } catch (Throwable t) { - Log.w(TAG, "insertSegment failed", t); - return -1; - } - } - - private static void putOptString(ContentValues values, JSONObject s, String jsonKey, String column) - throws JSONException { - if (s.has(jsonKey) && !s.isNull(jsonKey)) { - values.put(column, s.getString(jsonKey)); - } - } - - /** Update mutable fields of a segment (same keys as insertSegment). */ - public void updateSegment(long segmentId, String json) { - try { - JSONObject s = new JSONObject(json); - ContentValues values = new ContentValues(); - if (s.has("speakerId")) { - if (s.isNull("speakerId")) { - values.putNull("speaker_id"); - } else { - values.put("speaker_id", s.getLong("speakerId")); - } - } - if (s.has("text")) { - values.put("text", s.getString("text")); - } - putOptString(values, s, "lang", "lang"); - putOptString(values, s, "translation", "translation"); - putOptString(values, s, "translationLang", "translation_lang"); - if (s.has("sentiment")) { - values.put("sentiment", s.getDouble("sentiment")); - } - putOptString(values, s, "emotion", "emotion"); - putOptString(values, s, "searchMeta", "search_meta"); - if (values.size() > 0) { - getWritableDatabase().update("segments", values, "id=?", - new String[]{String.valueOf(segmentId)}); - } - } catch (Throwable t) { - Log.w(TAG, "updateSegment failed", t); - } - } - - public String querySegments(long sessionId) { - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT id, speaker_id, started_at, ended_at, audio_offset_ms, text, lang," - + " translation, translation_lang, sentiment, emotion, angle" - + " FROM segments WHERE session_id=? ORDER BY started_at ASC", - new String[]{String.valueOf(sessionId)})) { - while (c.moveToNext()) { - out.put(segmentRowJson(c)); - } - } catch (JSONException e) { - Log.w(TAG, "querySegments failed", e); - } - return out.toString(); - } - - private JSONObject segmentRowJson(Cursor c) throws JSONException { - JSONObject row = new JSONObject(); - row.put("id", c.getLong(0)); - row.put("speakerId", c.isNull(1) ? JSONObject.NULL : c.getLong(1)); - row.put("startedAt", c.getLong(2)); - row.put("endedAt", c.getLong(3)); - row.put("audioOffsetMs", c.isNull(4) ? JSONObject.NULL : c.getLong(4)); - row.put("text", c.getString(5)); - row.put("lang", c.isNull(6) ? "" : c.getString(6)); - row.put("translation", c.isNull(7) ? "" : c.getString(7)); - row.put("translationLang", c.isNull(8) ? "" : c.getString(8)); - row.put("sentiment", c.isNull(9) ? 0 : c.getDouble(9)); - row.put("emotion", c.isNull(10) ? "" : c.getString(10)); - row.put("angle", c.isNull(11) ? JSONObject.NULL : c.getInt(11)); - return row; - } - - /** - * Search segments across sessions. filterJson: query?, emotion?, - * speakerId?, sinceMs?, untilMs?, limit?. Matches text, translation, and - * the sentiment search metadata. - */ - public String searchSegments(String filterJson) { - try { - JSONObject filter = filterJson == null || filterJson.isEmpty() - ? new JSONObject() : new JSONObject(filterJson); - StringBuilder sql = new StringBuilder( - "SELECT id, speaker_id, started_at, ended_at, audio_offset_ms, text, lang," - + " translation, translation_lang, sentiment, emotion, angle, session_id" - + " FROM segments"); - List args = new ArrayList<>(); - List where = new ArrayList<>(); - if (filter.has("query") && !filter.getString("query").isEmpty()) { - String like = "%" + filter.getString("query") + "%"; - where.add("(text LIKE ? OR translation LIKE ? OR search_meta LIKE ?)"); - args.add(like); - args.add(like); - args.add(like); - } - if (filter.has("emotion")) { - where.add("emotion = ?"); - args.add(filter.getString("emotion")); - } - if (filter.has("speakerId")) { - where.add("speaker_id = ?"); - args.add(String.valueOf(filter.getLong("speakerId"))); - } - if (filter.has("sinceMs")) { - where.add("started_at >= ?"); - args.add(String.valueOf(filter.getLong("sinceMs"))); - } - if (filter.has("untilMs")) { - where.add("started_at <= ?"); - args.add(String.valueOf(filter.getLong("untilMs"))); - } - if (!where.isEmpty()) { - sql.append(" WHERE ").append(String.join(" AND ", where)); - } - sql.append(" ORDER BY started_at DESC LIMIT ") - .append(Math.max(1, Math.min(filter.optInt("limit", 300), 1000))); - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[0]))) { - while (c.moveToNext()) { - JSONObject row = segmentRowJson(c); - row.put("sessionId", c.getLong(12)); - out.put(row); - } - } - return out.toString(); - } catch (JSONException e) { - Log.w(TAG, "searchSegments failed", e); - return "[]"; - } - } - - /** Per-segment voice-print embeddings of a session (for re-diarization). */ - public String querySegmentEmbeddings(long sessionId) { - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT id, embedding FROM segments WHERE session_id=? AND embedding IS NOT NULL", - new String[]{String.valueOf(sessionId)})) { - while (c.moveToNext()) { - JSONObject row = new JSONObject(); - row.put("id", c.getLong(0)); - row.put("embeddingBase64", Base64.encodeToString(c.getBlob(1), Base64.NO_WRAP)); - out.put(row); - } - } catch (JSONException e) { - Log.w(TAG, "querySegmentEmbeddings failed", e); - } - return out.toString(); - } - - // ---- speakers ---- - - public long createSpeaker(String name, String color, boolean isWearer, String embeddingBase64) { - ContentValues values = new ContentValues(); - values.put("name", name); - values.put("color", color == null || color.isEmpty() ? "#4FC3F7" : color); - values.put("is_wearer", isWearer ? 1 : 0); - values.put("created_at", System.currentTimeMillis()); - values.put("last_heard_at", System.currentTimeMillis()); - if (embeddingBase64 != null && !embeddingBase64.isEmpty()) { - values.put("embedding", Base64.decode(embeddingBase64, Base64.NO_WRAP)); - values.put("embedding_count", 1); - } - return getWritableDatabase().insert("speakers", null, values); - } - - public String querySpeakers() { - JSONArray out = new JSONArray(); - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT p.id, p.name, p.color, p.is_wearer, p.embedding, p.embedding_count," - + " p.last_heard_at, p.tag," - + " p.last_recap, p.action_items, p.facts, p.insights_updated_at, p.insights_session_id," - + " (SELECT COUNT(*) FROM segments g WHERE g.speaker_id = p.id) AS segments" - + " FROM speakers p ORDER BY p.last_heard_at DESC", null)) { - while (c.moveToNext()) { - JSONObject row = new JSONObject(); - row.put("id", c.getLong(0)); - row.put("name", c.getString(1)); - row.put("color", c.getString(2)); - row.put("isWearer", c.getInt(3) != 0); - byte[] blob = c.isNull(4) ? null : c.getBlob(4); - row.put("embeddingBase64", blob == null ? "" : Base64.encodeToString(blob, Base64.NO_WRAP)); - row.put("embeddingCount", c.getInt(5)); - row.put("lastHeardAt", c.isNull(6) ? 0 : c.getLong(6)); - row.put("tag", c.isNull(7) ? "" : c.getString(7)); - row.put("lastRecap", c.isNull(8) ? "" : c.getString(8)); - row.put("actionItemsJson", c.isNull(9) ? "" : c.getString(9)); - row.put("factsJson", c.isNull(10) ? "" : c.getString(10)); - row.put("insightsUpdatedAt", c.isNull(11) ? 0 : c.getLong(11)); - row.put("insightsSessionId", c.isNull(12) ? 0 : c.getLong(12)); - row.put("segmentCount", c.getLong(13)); - out.put(row); - } - } catch (JSONException e) { - Log.w(TAG, "querySpeakers failed", e); - } - return out.toString(); - } - - public void renameSpeaker(long speakerId, String name) { - ContentValues values = new ContentValues(); - values.put("name", name); - getWritableDatabase().update("speakers", values, "id=?", new String[]{String.valueOf(speakerId)}); - } - - public void setSpeakerColor(long speakerId, String color) { - ContentValues values = new ContentValues(); - values.put("color", color); - getWritableDatabase().update("speakers", values, "id=?", new String[]{String.valueOf(speakerId)}); - } - - public void setSpeakerTag(long speakerId, String tag) { - ContentValues values = new ContentValues(); - values.put("tag", tag); - getWritableDatabase().update("speakers", values, "id=?", new String[]{String.valueOf(speakerId)}); - } - - /** - * Store LLM-derived conversation insights for a speaker: a recap of their - * most recent conversation, its open action items (JSON string array), and - * the accumulated inferred facts about them (JSON string array). - */ - public void setSpeakerInsights(long speakerId, String recap, String actionItemsJson, - String factsJson, long sessionId) { - ContentValues values = new ContentValues(); - values.put("last_recap", recap == null ? "" : recap); - values.put("action_items", actionItemsJson == null ? "" : actionItemsJson); - values.put("facts", factsJson == null ? "" : factsJson); - values.put("insights_updated_at", System.currentTimeMillis()); - values.put("insights_session_id", sessionId); - getWritableDatabase().update("speakers", values, "id=?", new String[]{String.valueOf(speakerId)}); - } - - public void setSpeakerWearer(long speakerId, boolean isWearer) { - SQLiteDatabase db = getWritableDatabase(); - if (isWearer) { - // Exactly one wearer profile at a time. - db.execSQL("UPDATE speakers SET is_wearer = 0"); - } - ContentValues values = new ContentValues(); - values.put("is_wearer", isWearer ? 1 : 0); - db.update("speakers", values, "id=?", new String[]{String.valueOf(speakerId)}); - } - - /** - * Running-mean centroid update for a speaker's voice-print, capped so a - * long session cannot drown the enrolled voice. - */ - public void updateSpeakerEmbedding(long speakerId, String embeddingBase64, int maxCount) { - try { - byte[] incoming = Base64.decode(embeddingBase64, Base64.NO_WRAP); - float[] update = blobToFloats(incoming); - if (update == null) { - return; - } - float[] current = null; - int count = 0; - try (Cursor c = getReadableDatabase().rawQuery( - "SELECT embedding, embedding_count FROM speakers WHERE id=?", - new String[]{String.valueOf(speakerId)})) { - if (c.moveToFirst()) { - current = c.isNull(0) ? null : blobToFloats(c.getBlob(0)); - count = c.getInt(1); - } - } - float[] merged; - if (current == null || current.length != update.length) { - merged = update; - count = 1; - } else { - int effective = Math.min(count, Math.max(1, maxCount)); - merged = new float[current.length]; - for (int i = 0; i < merged.length; i++) { - merged[i] = (current[i] * effective + update[i]) / (effective + 1); - } - l2NormalizeInPlace(merged); - count = count + 1; - } - ContentValues values = new ContentValues(); - values.put("embedding", floatsToBlob(merged)); - values.put("embedding_count", count); - values.put("last_heard_at", System.currentTimeMillis()); - getWritableDatabase().update("speakers", values, "id=?", - new String[]{String.valueOf(speakerId)}); - } catch (Throwable t) { - Log.w(TAG, "updateSpeakerEmbedding failed", t); - } - } - - /** Move every segment from one speaker onto another and drop the source. */ - public void mergeSpeakers(long fromId, long intoId) { - if (fromId == intoId) { - return; - } - SQLiteDatabase db = getWritableDatabase(); - db.beginTransaction(); - try { - db.execSQL("UPDATE segments SET speaker_id=? WHERE speaker_id=?", - new Object[]{intoId, fromId}); - // Blend the two centroids weighted by their sample counts. - float[] from = null; - float[] into = null; - int fromCount = 0; - int intoCount = 0; - boolean fromWearer = false; - try (Cursor c = db.rawQuery( - "SELECT id, embedding, embedding_count, is_wearer FROM speakers WHERE id IN (?,?)", - new String[]{String.valueOf(fromId), String.valueOf(intoId)})) { - while (c.moveToNext()) { - long id = c.getLong(0); - float[] embedding = c.isNull(1) ? null : blobToFloats(c.getBlob(1)); - int count = c.getInt(2); - if (id == fromId) { - from = embedding; - fromCount = count; - fromWearer = c.getInt(3) != 0; - } else { - into = embedding; - intoCount = count; - } - } - } - float[] merged = into; - if (from != null && into != null && from.length == into.length) { - merged = new float[into.length]; - int total = Math.max(1, fromCount + intoCount); - for (int i = 0; i < merged.length; i++) { - merged[i] = (into[i] * intoCount + from[i] * fromCount) / total; - } - l2NormalizeInPlace(merged); - } else if (into == null) { - merged = from; - } - ContentValues values = new ContentValues(); - if (merged != null) { - values.put("embedding", floatsToBlob(merged)); - values.put("embedding_count", fromCount + intoCount); - } - if (fromWearer) { - values.put("is_wearer", 1); - } - values.put("last_heard_at", System.currentTimeMillis()); - // Keep the target's conversation insights; adopt the source's only - // where the target has none (e.g. merging a rich profile into a - // freshly auto-created one). - try (Cursor c = db.rawQuery( - "SELECT last_recap, action_items, facts, insights_updated_at, insights_session_id" - + " FROM speakers WHERE id IN (?,?) ORDER BY CASE id WHEN ? THEN 0 ELSE 1 END", - new String[]{String.valueOf(fromId), String.valueOf(intoId), String.valueOf(intoId)})) { - boolean targetHasInsights = false; - while (c.moveToNext()) { - boolean hasInsights = !c.isNull(0) && !c.getString(0).isEmpty(); - if (c.getPosition() == 0) { - targetHasInsights = hasInsights; - } else if (!targetHasInsights && hasInsights) { - values.put("last_recap", c.getString(0)); - values.put("action_items", c.isNull(1) ? "" : c.getString(1)); - values.put("facts", c.isNull(2) ? "" : c.getString(2)); - values.put("insights_updated_at", c.isNull(3) ? 0 : c.getLong(3)); - values.put("insights_session_id", c.isNull(4) ? 0 : c.getLong(4)); - } - } - } - db.update("speakers", values, "id=?", new String[]{String.valueOf(intoId)}); - db.delete("speakers", "id=?", new String[]{String.valueOf(fromId)}); - db.setTransactionSuccessful(); - } finally { - db.endTransaction(); - } - } - - public void reassignSegmentSpeaker(long segmentId, long speakerId) { - ContentValues values = new ContentValues(); - values.put("speaker_id", speakerId); - getWritableDatabase().update("segments", values, "id=?", new String[]{String.valueOf(segmentId)}); - } - - /** Reassign every segment of a speaker within one session (split support). */ - public void reassignSessionSpeaker(long sessionId, long fromSpeakerId, long toSpeakerId) { - getWritableDatabase().execSQL( - "UPDATE segments SET speaker_id=? WHERE session_id=? AND speaker_id=?", - new Object[]{toSpeakerId, sessionId, fromSpeakerId}); - } - - public void deleteSpeaker(long speakerId) { - SQLiteDatabase db = getWritableDatabase(); - db.execSQL("UPDATE segments SET speaker_id=NULL WHERE speaker_id=?", new Object[]{speakerId}); - db.delete("speakers", "id=?", new String[]{String.valueOf(speakerId)}); - } - - // ---- retention ---- - - /** - * Delete caption sessions older than the cutoff (0 disables), and strip - * recordings older than their own cutoff while keeping the transcript. - * Returns a JSON summary of what was removed. - */ - public String applyRetention(long captionCutoffMs, long recordingCutoffMs) { - SQLiteDatabase db = getWritableDatabase(); - int sessionsDeleted = 0; - int recordingsDeleted = 0; - if (recordingCutoffMs > 0) { - List paths = new ArrayList<>(); - try (Cursor c = db.rawQuery( - "SELECT id, audio_path FROM sessions WHERE audio_path IS NOT NULL AND started_at < ?", - new String[]{String.valueOf(recordingCutoffMs)})) { - while (c.moveToNext()) { - if (!c.isNull(1)) { - paths.add(c.getString(1)); - } - } - } - for (String path : paths) { - if (new File(path).delete()) { - recordingsDeleted++; - } - } - db.execSQL("UPDATE sessions SET audio_path=NULL, audio_codec=NULL WHERE started_at < ?", - new Object[]{recordingCutoffMs}); - } - if (captionCutoffMs > 0) { - List paths = new ArrayList<>(); - try (Cursor c = db.rawQuery( - "SELECT audio_path FROM sessions WHERE audio_path IS NOT NULL AND started_at < ?", - new String[]{String.valueOf(captionCutoffMs)})) { - while (c.moveToNext()) { - paths.add(c.getString(0)); - } - } - for (String path : paths) { - if (path != null && new File(path).delete()) { - recordingsDeleted++; - } - } - db.execSQL("DELETE FROM segments WHERE session_id IN (SELECT id FROM sessions WHERE started_at < ?)", - new Object[]{captionCutoffMs}); - Cursor count = db.rawQuery("SELECT changes()", null); - count.close(); - db.execSQL("DELETE FROM sessions WHERE started_at < ?", new Object[]{captionCutoffMs}); - try (Cursor c = db.rawQuery("SELECT changes()", null)) { - if (c.moveToFirst()) { - sessionsDeleted = c.getInt(0); - } - } - } - try { - JSONObject summary = new JSONObject(); - summary.put("sessionsDeleted", sessionsDeleted); - summary.put("recordingsDeleted", recordingsDeleted); - return summary.toString(); - } catch (JSONException e) { - return "{}"; - } - } - - public void deleteSession(long sessionId) { - SQLiteDatabase db = getWritableDatabase(); - String path = getSessionAudioPath(sessionId); - if (!path.isEmpty()) { - new File(path).delete(); - } - db.delete("segments", "session_id=?", new String[]{String.valueOf(sessionId)}); - db.delete("sessions", "id=?", new String[]{String.valueOf(sessionId)}); - } - - // ---- blob helpers ---- - - private static float[] blobToFloats(byte[] blob) { - if (blob == null || blob.length < 4 || blob.length % 4 != 0) { - return null; - } - ByteBuffer buffer = ByteBuffer.wrap(blob).order(ByteOrder.LITTLE_ENDIAN); - float[] out = new float[blob.length / 4]; - for (int i = 0; i < out.length; i++) { - out[i] = buffer.getFloat(); - } - return out; - } - - private static byte[] floatsToBlob(float[] values) { - ByteBuffer buffer = ByteBuffer.allocate(values.length * 4).order(ByteOrder.LITTLE_ENDIAN); - for (float v : values) { - buffer.putFloat(v); - } - return buffer.array(); - } - - private static void l2NormalizeInPlace(float[] v) { - double sum = 0; - for (float x : v) { - sum += (double) x * x; - } - double norm = Math.sqrt(sum); - if (norm <= 0) { - return; - } - for (int i = 0; i < v.length; i++) { - v[i] = (float) (v[i] / norm); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.kt new file mode 100644 index 00000000..2d9f316f --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawConversationStore.kt @@ -0,0 +1,730 @@ +package com.faceclaw.app + +import android.content.ContentValues +import android.content.Context +import android.database.Cursor +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper +import android.util.Base64 +import android.util.Log + +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +import java.io.File +import java.util.ArrayList + +/** + * Persistent store for caption sessions, transcript segments, and speaker + * voice profiles, backing the Microphones app and its phone-side review UI. + * All complex values cross the JS bridge as JSON strings; voice-print + * embeddings are float32 LE blobs, exposed as base64. + */ +class FaceclawConversationStore private constructor(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) { + companion object { + private const val TAG = "FaceclawConvStore" + private const val DB_NAME = "faceclaw-conversations.db" + private const val DB_VERSION = 2 + + private var instance: FaceclawConversationStore? = null + + @JvmStatic + @Synchronized + fun getInstance(context: Context): FaceclawConversationStore { + if (instance == null) { + instance = FaceclawConversationStore(context.applicationContext) + } + return instance!! + } + + @Throws(JSONException::class) + private fun putOptString(values: ContentValues, s: JSONObject, jsonKey: String, column: String) { + if (s.has(jsonKey) && !s.isNull(jsonKey)) { + values.put(column, s.getString(jsonKey)) + } + } + + } + + override fun onCreate(db: SQLiteDatabase) { + db.execSQL("CREATE TABLE speakers (" + + "id INTEGER PRIMARY KEY AUTOINCREMENT," + + "name TEXT NOT NULL," + + "color TEXT NOT NULL DEFAULT '#4FC3F7'," + + "is_wearer INTEGER NOT NULL DEFAULT 0," + + "embedding BLOB," + + "embedding_count INTEGER NOT NULL DEFAULT 0," + + "created_at INTEGER NOT NULL," + + "last_heard_at INTEGER," + + "tag TEXT," + + "last_recap TEXT," + + "action_items TEXT," + + "facts TEXT," + + "insights_updated_at INTEGER," + + "insights_session_id INTEGER)") + db.execSQL("CREATE TABLE sessions (" + + "id INTEGER PRIMARY KEY AUTOINCREMENT," + + "started_at INTEGER NOT NULL," + + "ended_at INTEGER," + + "title TEXT," + + "audio_path TEXT," + + "audio_codec TEXT," + + "avg_sentiment REAL," + + "segment_count INTEGER NOT NULL DEFAULT 0)") + db.execSQL("CREATE TABLE segments (" + + "id INTEGER PRIMARY KEY AUTOINCREMENT," + + "session_id INTEGER NOT NULL," + + "speaker_id INTEGER," + + "started_at INTEGER NOT NULL," + + "ended_at INTEGER NOT NULL," + + "audio_offset_ms INTEGER," + + "text TEXT NOT NULL," + + "lang TEXT," + + "translation TEXT," + + "translation_lang TEXT," + + "sentiment REAL," + + "emotion TEXT," + + "search_meta TEXT," + + "angle INTEGER," + + "embedding BLOB)") + db.execSQL("CREATE INDEX idx_segments_session ON segments(session_id)") + db.execSQL("CREATE INDEX idx_segments_speaker ON segments(speaker_id)") + db.execSQL("CREATE INDEX idx_segments_time ON segments(started_at)") + db.execSQL("CREATE INDEX idx_sessions_time ON sessions(started_at)") + } + + override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + if (oldVersion < 2) { + // v2: per-speaker conversation insights (recap of the most recent + // conversation, its action items, and accumulated inferred facts). + db.execSQL("ALTER TABLE speakers ADD COLUMN last_recap TEXT") + db.execSQL("ALTER TABLE speakers ADD COLUMN action_items TEXT") + db.execSQL("ALTER TABLE speakers ADD COLUMN facts TEXT") + db.execSQL("ALTER TABLE speakers ADD COLUMN insights_updated_at INTEGER") + db.execSQL("ALTER TABLE speakers ADD COLUMN insights_session_id INTEGER") + } + } + + // ---- sessions ---- + + fun startSession(startedAtMs: Long, title: String?): Long { + val values = ContentValues() + values.put("started_at", startedAtMs) + values.put("title", if (title == null || title.isEmpty()) null else title) + return writableDatabase.insert("sessions", null, values) + } + + fun endSession(sessionId: Long, endedAtMs: Long, audioPath: String?, audioCodec: String?, avgSentiment: Double) { + val values = ContentValues() + values.put("ended_at", endedAtMs) + if (audioPath != null && !audioPath.isEmpty()) { + values.put("audio_path", audioPath) + values.put("audio_codec", audioCodec) + } + values.put("avg_sentiment", avgSentiment) + writableDatabase.update("sessions", values, "id=?", arrayOf(sessionId.toString())) + } + + fun setSessionAudio(sessionId: Long, audioPath: String?, audioCodec: String?) { + val values = ContentValues() + values.put("audio_path", audioPath) + values.put("audio_codec", audioCodec) + writableDatabase.update("sessions", values, "id=?", arrayOf(sessionId.toString())) + } + + fun getSessionAudioPath(sessionId: Long): String { + readableDatabase.rawQuery( + "SELECT audio_path FROM sessions WHERE id=?", arrayOf(sessionId.toString())).use { c -> + if (c.moveToFirst()) { + return if (c.isNull(0)) "" else c.getString(0) + } + } + return "" + } + + /** + * Sessions, newest first, as JSON. filterJson supports: sinceMs, untilMs, + * speakerId, emotion (matches any segment emotion in the session), query + * (LIKE over title and segment text/search metadata), limit. + */ + fun querySessions(filterJson: String?): String { + try { + val filter = if (filterJson == null || filterJson.isEmpty()) + JSONObject() else JSONObject(filterJson) + val sql = StringBuilder( + "SELECT DISTINCT s.id, s.started_at, s.ended_at, s.title, s.audio_path, s.audio_codec," + + " s.avg_sentiment, s.segment_count FROM sessions s") + val args: MutableList = ArrayList() + val where: MutableList = ArrayList() + val joinSegments = filter.has("speakerId") || filter.has("emotion") || filter.has("query") + if (joinSegments) { + sql.append(" LEFT JOIN segments g ON g.session_id = s.id") + } + if (filter.has("sinceMs")) { + where.add("s.started_at >= ?") + args.add(filter.getLong("sinceMs").toString()) + } + if (filter.has("untilMs")) { + where.add("s.started_at <= ?") + args.add(filter.getLong("untilMs").toString()) + } + if (filter.has("speakerId")) { + where.add("g.speaker_id = ?") + args.add(filter.getLong("speakerId").toString()) + } + if (filter.has("emotion")) { + where.add("g.emotion = ?") + args.add(filter.getString("emotion")) + } + if (filter.has("query")) { + val like = "%" + filter.getString("query") + "%" + where.add("(s.title LIKE ? OR g.text LIKE ? OR g.search_meta LIKE ?)") + args.add(like) + args.add(like) + args.add(like) + } + if (!where.isEmpty()) { + sql.append(" WHERE ").append(where.joinToString(" AND ")) + } + sql.append(" ORDER BY s.started_at DESC") + val limit = filter.optInt("limit", 200) + sql.append(" LIMIT ").append(Math.max(1, Math.min(limit, 1000))) + + val out = JSONArray() + readableDatabase.rawQuery(sql.toString(), args.toTypedArray()).use { c -> + while (c.moveToNext()) { + val row = JSONObject() + val id = c.getLong(0) + row.put("id", id) + row.put("startedAt", c.getLong(1)) + row.put("endedAt", if (c.isNull(2)) JSONObject.NULL else c.getLong(2)) + row.put("title", if (c.isNull(3)) "" else c.getString(3)) + row.put("audioPath", if (c.isNull(4)) "" else c.getString(4)) + row.put("audioCodec", if (c.isNull(5)) "" else c.getString(5)) + row.put("avgSentiment", if (c.isNull(6)) 0.0 else c.getDouble(6)) + row.put("segmentCount", c.getLong(7)) + row.put("speakers", sessionSpeakersJson(id)) + out.put(row) + } + } + return out.toString() + } catch (e: JSONException) { + Log.w(TAG, "querySessions failed", e) + return "[]" + } + } + + @Throws(JSONException::class) + private fun sessionSpeakersJson(sessionId: Long): JSONArray { + val out = JSONArray() + readableDatabase.rawQuery( + "SELECT DISTINCT p.id, p.name, p.color FROM segments g" + + " JOIN speakers p ON p.id = g.speaker_id WHERE g.session_id=?", + arrayOf(sessionId.toString())).use { c -> + while (c.moveToNext()) { + val row = JSONObject() + row.put("id", c.getLong(0)) + row.put("name", c.getString(1)) + row.put("color", c.getString(2)) + out.put(row) + } + } + return out + } + + // ---- segments ---- + + /** + * Insert one transcript segment. json fields: sessionId, speakerId?, + * startedAt, endedAt, audioOffsetMs?, text, lang?, translation?, + * translationLang?, sentiment?, emotion?, searchMeta?, angle?, + * embeddingBase64?. Returns the new row id, or -1. + */ + fun insertSegment(json: String?): Long { + try { + val s = JSONObject(json) + val values = ContentValues() + val sessionId = s.getLong("sessionId") + values.put("session_id", sessionId) + if (s.has("speakerId")) { + values.put("speaker_id", s.getLong("speakerId")) + } + values.put("started_at", s.getLong("startedAt")) + values.put("ended_at", s.getLong("endedAt")) + if (s.has("audioOffsetMs")) { + values.put("audio_offset_ms", s.getLong("audioOffsetMs")) + } + values.put("text", s.getString("text")) + putOptString(values, s, "lang", "lang") + putOptString(values, s, "translation", "translation") + putOptString(values, s, "translationLang", "translation_lang") + if (s.has("sentiment")) { + values.put("sentiment", s.getDouble("sentiment")) + } + putOptString(values, s, "emotion", "emotion") + putOptString(values, s, "searchMeta", "search_meta") + if (s.has("angle")) { + values.put("angle", s.getInt("angle")) + } + if (s.has("embeddingBase64")) { + values.put("embedding", Base64.decode(s.getString("embeddingBase64"), Base64.NO_WRAP)) + } + val db = writableDatabase + val id = db.insert("segments", null, values) + db.execSQL("UPDATE sessions SET segment_count = segment_count + 1 WHERE id=?", + arrayOf(sessionId)) + return id + } catch (t: Throwable) { + Log.w(TAG, "insertSegment failed", t) + return -1 + } + } + + /** Update mutable fields of a segment (same keys as insertSegment). */ + fun updateSegment(segmentId: Long, json: String?) { + try { + val s = JSONObject(json) + val values = ContentValues() + if (s.has("speakerId")) { + if (s.isNull("speakerId")) { + values.putNull("speaker_id") + } else { + values.put("speaker_id", s.getLong("speakerId")) + } + } + if (s.has("text")) { + values.put("text", s.getString("text")) + } + putOptString(values, s, "lang", "lang") + putOptString(values, s, "translation", "translation") + putOptString(values, s, "translationLang", "translation_lang") + if (s.has("sentiment")) { + values.put("sentiment", s.getDouble("sentiment")) + } + putOptString(values, s, "emotion", "emotion") + putOptString(values, s, "searchMeta", "search_meta") + if (values.size() > 0) { + writableDatabase.update("segments", values, "id=?", + arrayOf(segmentId.toString())) + } + } catch (t: Throwable) { + Log.w(TAG, "updateSegment failed", t) + } + } + + fun querySegments(sessionId: Long): String { + val out = JSONArray() + try { + readableDatabase.rawQuery( + "SELECT id, speaker_id, started_at, ended_at, audio_offset_ms, text, lang," + + " translation, translation_lang, sentiment, emotion, angle" + + " FROM segments WHERE session_id=? ORDER BY started_at ASC", + arrayOf(sessionId.toString())).use { c -> + while (c.moveToNext()) { + out.put(segmentRowJson(c)) + } + } + } catch (e: JSONException) { + Log.w(TAG, "querySegments failed", e) + } + return out.toString() + } + + @Throws(JSONException::class) + private fun segmentRowJson(c: Cursor): JSONObject { + val row = JSONObject() + row.put("id", c.getLong(0)) + row.put("speakerId", if (c.isNull(1)) JSONObject.NULL else c.getLong(1)) + row.put("startedAt", c.getLong(2)) + row.put("endedAt", c.getLong(3)) + row.put("audioOffsetMs", if (c.isNull(4)) JSONObject.NULL else c.getLong(4)) + row.put("text", c.getString(5)) + row.put("lang", if (c.isNull(6)) "" else c.getString(6)) + row.put("translation", if (c.isNull(7)) "" else c.getString(7)) + row.put("translationLang", if (c.isNull(8)) "" else c.getString(8)) + row.put("sentiment", if (c.isNull(9)) 0.0 else c.getDouble(9)) + row.put("emotion", if (c.isNull(10)) "" else c.getString(10)) + row.put("angle", if (c.isNull(11)) JSONObject.NULL else c.getInt(11)) + return row + } + + /** + * Search segments across sessions. filterJson: query?, emotion?, + * speakerId?, sinceMs?, untilMs?, limit?. Matches text, translation, and + * the sentiment search metadata. + */ + fun searchSegments(filterJson: String?): String { + try { + val filter = if (filterJson == null || filterJson.isEmpty()) + JSONObject() else JSONObject(filterJson) + val sql = StringBuilder( + "SELECT id, speaker_id, started_at, ended_at, audio_offset_ms, text, lang," + + " translation, translation_lang, sentiment, emotion, angle, session_id" + + " FROM segments") + val args: MutableList = ArrayList() + val where: MutableList = ArrayList() + if (filter.has("query") && !filter.getString("query").isEmpty()) { + val like = "%" + filter.getString("query") + "%" + where.add("(text LIKE ? OR translation LIKE ? OR search_meta LIKE ?)") + args.add(like) + args.add(like) + args.add(like) + } + if (filter.has("emotion")) { + where.add("emotion = ?") + args.add(filter.getString("emotion")) + } + if (filter.has("speakerId")) { + where.add("speaker_id = ?") + args.add(filter.getLong("speakerId").toString()) + } + if (filter.has("sinceMs")) { + where.add("started_at >= ?") + args.add(filter.getLong("sinceMs").toString()) + } + if (filter.has("untilMs")) { + where.add("started_at <= ?") + args.add(filter.getLong("untilMs").toString()) + } + if (!where.isEmpty()) { + sql.append(" WHERE ").append(where.joinToString(" AND ")) + } + sql.append(" ORDER BY started_at DESC LIMIT ") + .append(Math.max(1, Math.min(filter.optInt("limit", 300), 1000))) + val out = JSONArray() + readableDatabase.rawQuery(sql.toString(), args.toTypedArray()).use { c -> + while (c.moveToNext()) { + val row = segmentRowJson(c) + row.put("sessionId", c.getLong(12)) + out.put(row) + } + } + return out.toString() + } catch (e: JSONException) { + Log.w(TAG, "searchSegments failed", e) + return "[]" + } + } + + /** Per-segment voice-print embeddings of a session (for re-diarization). */ + fun querySegmentEmbeddings(sessionId: Long): String { + val out = JSONArray() + try { + readableDatabase.rawQuery( + "SELECT id, embedding FROM segments WHERE session_id=? AND embedding IS NOT NULL", + arrayOf(sessionId.toString())).use { c -> + while (c.moveToNext()) { + val row = JSONObject() + row.put("id", c.getLong(0)) + row.put("embeddingBase64", Base64.encodeToString(c.getBlob(1), Base64.NO_WRAP)) + out.put(row) + } + } + } catch (e: JSONException) { + Log.w(TAG, "querySegmentEmbeddings failed", e) + } + return out.toString() + } + + // ---- speakers ---- + + fun createSpeaker(name: String?, color: String?, isWearer: Boolean, embeddingBase64: String?): Long { + val values = ContentValues() + values.put("name", name) + values.put("color", if (color == null || color.isEmpty()) "#4FC3F7" else color) + values.put("is_wearer", if (isWearer) 1 else 0) + values.put("created_at", System.currentTimeMillis()) + values.put("last_heard_at", System.currentTimeMillis()) + if (embeddingBase64 != null && !embeddingBase64.isEmpty()) { + values.put("embedding", Base64.decode(embeddingBase64, Base64.NO_WRAP)) + values.put("embedding_count", 1) + } + return writableDatabase.insert("speakers", null, values) + } + + fun querySpeakers(): String { + val out = JSONArray() + try { + readableDatabase.rawQuery( + "SELECT p.id, p.name, p.color, p.is_wearer, p.embedding, p.embedding_count," + + " p.last_heard_at, p.tag," + + " p.last_recap, p.action_items, p.facts, p.insights_updated_at, p.insights_session_id," + + " (SELECT COUNT(*) FROM segments g WHERE g.speaker_id = p.id) AS segments" + + " FROM speakers p ORDER BY p.last_heard_at DESC", null).use { c -> + while (c.moveToNext()) { + val row = JSONObject() + row.put("id", c.getLong(0)) + row.put("name", c.getString(1)) + row.put("color", c.getString(2)) + row.put("isWearer", c.getInt(3) != 0) + val blob = if (c.isNull(4)) null else c.getBlob(4) + row.put("embeddingBase64", if (blob == null) "" else Base64.encodeToString(blob, Base64.NO_WRAP)) + row.put("embeddingCount", c.getInt(5)) + row.put("lastHeardAt", if (c.isNull(6)) 0L else c.getLong(6)) + row.put("tag", if (c.isNull(7)) "" else c.getString(7)) + row.put("lastRecap", if (c.isNull(8)) "" else c.getString(8)) + row.put("actionItemsJson", if (c.isNull(9)) "" else c.getString(9)) + row.put("factsJson", if (c.isNull(10)) "" else c.getString(10)) + row.put("insightsUpdatedAt", if (c.isNull(11)) 0L else c.getLong(11)) + row.put("insightsSessionId", if (c.isNull(12)) 0L else c.getLong(12)) + row.put("segmentCount", c.getLong(13)) + out.put(row) + } + } + } catch (e: JSONException) { + Log.w(TAG, "querySpeakers failed", e) + } + return out.toString() + } + + fun renameSpeaker(speakerId: Long, name: String?) { + val values = ContentValues() + values.put("name", name) + writableDatabase.update("speakers", values, "id=?", arrayOf(speakerId.toString())) + } + + fun setSpeakerColor(speakerId: Long, color: String?) { + val values = ContentValues() + values.put("color", color) + writableDatabase.update("speakers", values, "id=?", arrayOf(speakerId.toString())) + } + + fun setSpeakerTag(speakerId: Long, tag: String?) { + val values = ContentValues() + values.put("tag", tag) + writableDatabase.update("speakers", values, "id=?", arrayOf(speakerId.toString())) + } + + /** + * Store LLM-derived conversation insights for a speaker: a recap of their + * most recent conversation, its open action items (JSON string array), and + * the accumulated inferred facts about them (JSON string array). + */ + fun setSpeakerInsights(speakerId: Long, recap: String?, actionItemsJson: String?, + factsJson: String?, sessionId: Long) { + val values = ContentValues() + values.put("last_recap", recap ?: "") + values.put("action_items", actionItemsJson ?: "") + values.put("facts", factsJson ?: "") + values.put("insights_updated_at", System.currentTimeMillis()) + values.put("insights_session_id", sessionId) + writableDatabase.update("speakers", values, "id=?", arrayOf(speakerId.toString())) + } + + fun setSpeakerWearer(speakerId: Long, isWearer: Boolean) { + val db = writableDatabase + if (isWearer) { + // Exactly one wearer profile at a time. + db.execSQL("UPDATE speakers SET is_wearer = 0") + } + val values = ContentValues() + values.put("is_wearer", if (isWearer) 1 else 0) + db.update("speakers", values, "id=?", arrayOf(speakerId.toString())) + } + + /** + * Running-mean centroid update for a speaker's voice-print, capped so a + * long session cannot drown the enrolled voice. + */ + fun updateSpeakerEmbedding(speakerId: Long, embeddingBase64: String?, maxCount: Int) { + try { + val incoming = Base64.decode(embeddingBase64, Base64.NO_WRAP) + val update = SpeakerEmbeddings.blobToFloats(incoming) + if (update == null) { + return + } + var current: FloatArray? = null + var count = 0 + val c = readableDatabase.rawQuery( + "SELECT embedding, embedding_count FROM speakers WHERE id=?", + arrayOf(speakerId.toString())) + try { + if (c.moveToFirst()) { + current = if (c.isNull(0)) null else SpeakerEmbeddings.blobToFloats(c.getBlob(0)) + count = c.getInt(1) + } + } finally { + c.close() + } + val centroid = SpeakerEmbeddings.runningMean(current, count, update, maxCount) + val values = ContentValues() + values.put("embedding", SpeakerEmbeddings.floatsToBlob(centroid.embedding)) + values.put("embedding_count", centroid.count) + values.put("last_heard_at", System.currentTimeMillis()) + writableDatabase.update("speakers", values, "id=?", + arrayOf(speakerId.toString())) + } catch (t: Throwable) { + Log.w(TAG, "updateSpeakerEmbedding failed", t) + } + } + + /** Move every segment from one speaker onto another and drop the source. */ + fun mergeSpeakers(fromId: Long, intoId: Long) { + if (fromId == intoId) { + return + } + val db = writableDatabase + db.beginTransaction() + try { + db.execSQL("UPDATE segments SET speaker_id=? WHERE speaker_id=?", + arrayOf(intoId, fromId)) + // Blend the two centroids weighted by their sample counts. + var from: FloatArray? = null + var into: FloatArray? = null + var fromCount = 0 + var intoCount = 0 + var fromWearer = false + val c = db.rawQuery( + "SELECT id, embedding, embedding_count, is_wearer FROM speakers WHERE id IN (?,?)", + arrayOf(fromId.toString(), intoId.toString())) + try { + while (c.moveToNext()) { + val id = c.getLong(0) + val embedding = if (c.isNull(1)) null else SpeakerEmbeddings.blobToFloats(c.getBlob(1)) + val count = c.getInt(2) + if (id == fromId) { + from = embedding + fromCount = count + fromWearer = c.getInt(3) != 0 + } else { + into = embedding + intoCount = count + } + } + } finally { + c.close() + } + val merged = SpeakerEmbeddings.blend(from, fromCount, into, intoCount) + val values = ContentValues() + if (merged != null) { + values.put("embedding", SpeakerEmbeddings.floatsToBlob(merged)) + values.put("embedding_count", fromCount + intoCount) + } + if (fromWearer) { + values.put("is_wearer", 1) + } + values.put("last_heard_at", System.currentTimeMillis()) + // Keep the target's conversation insights; adopt the source's only + // where the target has none (e.g. merging a rich profile into a + // freshly auto-created one). + db.rawQuery( + "SELECT last_recap, action_items, facts, insights_updated_at, insights_session_id" + + " FROM speakers WHERE id IN (?,?) ORDER BY CASE id WHEN ? THEN 0 ELSE 1 END", + arrayOf(fromId.toString(), intoId.toString(), intoId.toString())).use { c2 -> + var targetHasInsights = false + while (c2.moveToNext()) { + val hasInsights = !c2.isNull(0) && !c2.getString(0).isEmpty() + if (c2.position == 0) { + targetHasInsights = hasInsights + } else if (SpeakerEmbeddings.shouldAdoptInsights(targetHasInsights, hasInsights)) { + values.put("last_recap", c2.getString(0)) + values.put("action_items", if (c2.isNull(1)) "" else c2.getString(1)) + values.put("facts", if (c2.isNull(2)) "" else c2.getString(2)) + values.put("insights_updated_at", if (c2.isNull(3)) 0L else c2.getLong(3)) + values.put("insights_session_id", if (c2.isNull(4)) 0L else c2.getLong(4)) + } + } + } + db.update("speakers", values, "id=?", arrayOf(intoId.toString())) + db.delete("speakers", "id=?", arrayOf(fromId.toString())) + db.setTransactionSuccessful() + } finally { + db.endTransaction() + } + } + + fun reassignSegmentSpeaker(segmentId: Long, speakerId: Long) { + val values = ContentValues() + values.put("speaker_id", speakerId) + writableDatabase.update("segments", values, "id=?", arrayOf(segmentId.toString())) + } + + /** Reassign every segment of a speaker within one session (split support). */ + fun reassignSessionSpeaker(sessionId: Long, fromSpeakerId: Long, toSpeakerId: Long) { + writableDatabase.execSQL( + "UPDATE segments SET speaker_id=? WHERE session_id=? AND speaker_id=?", + arrayOf(toSpeakerId, sessionId, fromSpeakerId)) + } + + fun deleteSpeaker(speakerId: Long) { + val db = writableDatabase + db.execSQL("UPDATE segments SET speaker_id=NULL WHERE speaker_id=?", arrayOf(speakerId)) + db.delete("speakers", "id=?", arrayOf(speakerId.toString())) + } + + // ---- retention ---- + + /** + * Delete caption sessions older than the cutoff (0 disables), and strip + * recordings older than their own cutoff while keeping the transcript. + * Returns a JSON summary of what was removed. + */ + fun applyRetention(captionCutoffMs: Long, recordingCutoffMs: Long): String { + val db = writableDatabase + var sessionsDeleted = 0 + var recordingsDeleted = 0 + if (recordingCutoffMs > 0) { + val paths: MutableList = ArrayList() + db.rawQuery( + "SELECT id, audio_path FROM sessions WHERE audio_path IS NOT NULL AND started_at < ?", + arrayOf(recordingCutoffMs.toString())).use { c -> + while (c.moveToNext()) { + if (!c.isNull(1)) { + paths.add(c.getString(1)) + } + } + } + for (path in paths) { + if (File(path).delete()) { + recordingsDeleted++ + } + } + db.execSQL("UPDATE sessions SET audio_path=NULL, audio_codec=NULL WHERE started_at < ?", + arrayOf(recordingCutoffMs)) + } + if (captionCutoffMs > 0) { + val paths: MutableList = ArrayList() + db.rawQuery( + "SELECT audio_path FROM sessions WHERE audio_path IS NOT NULL AND started_at < ?", + arrayOf(captionCutoffMs.toString())).use { c -> + while (c.moveToNext()) { + paths.add(c.getString(0)) + } + } + for (path in paths) { + if (path != null && File(path).delete()) { + recordingsDeleted++ + } + } + db.execSQL("DELETE FROM segments WHERE session_id IN (SELECT id FROM sessions WHERE started_at < ?)", + arrayOf(captionCutoffMs)) + val count = db.rawQuery("SELECT changes()", null) + count.close() + db.execSQL("DELETE FROM sessions WHERE started_at < ?", arrayOf(captionCutoffMs)) + db.rawQuery("SELECT changes()", null).use { c -> + if (c.moveToFirst()) { + sessionsDeleted = c.getInt(0) + } + } + } + try { + val summary = JSONObject() + summary.put("sessionsDeleted", sessionsDeleted) + summary.put("recordingsDeleted", recordingsDeleted) + return summary.toString() + } catch (e: JSONException) { + return "{}" + } + } + + fun deleteSession(sessionId: Long) { + val db = writableDatabase + val path = getSessionAudioPath(sessionId) + if (!path.isEmpty()) { + File(path).delete() + } + db.delete("segments", "session_id=?", arrayOf(sessionId.toString())) + db.delete("sessions", "id=?", arrayOf(sessionId.toString())) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.java deleted file mode 100644 index 53645f27..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.java +++ /dev/null @@ -1,386 +0,0 @@ -package com.faceclaw.app; - -import android.annotation.SuppressLint; -import android.bluetooth.BluetoothAdapter; -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.ArrayList; -import java.util.List; -import java.util.Set; - -/** - * 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, the stock - * "EVEN R1…" ring prefix, 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; this filter - * must stay a superset of the TS classifier's admission. - */ -@SuppressLint("MissingPermission") -public class FaceclawDeviceDiscovery { - /** Even Realities' company identifier: the ASCII bytes "ER" read little-endian. */ - public static final int EVEN_COMPANY_ID = 0x5245; - - 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) { - Context appContext = context.getApplicationContext(); - BluetoothManager bluetoothManager = - (BluetoothManager) appContext.getSystemService(Context.BLUETOOTH_SERVICE); - if (bluetoothManager == null || bluetoothManager.getAdapter() == null) { - throw new IllegalStateException("Bluetooth adapter unavailable"); - } - this.bluetoothAdapter = bluetoothManager.getAdapter(); - } - - public boolean isBluetoothEnabled() { - return bluetoothAdapter.isEnabled(); - } - - public void setListener(FaceclawDeviceDiscoveryListener newListener) { - this.listener = newListener; - } - - // ------------------------------------------------------------------ - // 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) { - emitAdvertisement(result); - } - } - - @Override - public void onBatchScanResults(List 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; - } - 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) { - } - } - emitLog("scan stopped"); - } - - public boolean isScanning() { - synchronized (lock) { - return activeCallback != 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; - } - for (String json : bondedCandidateJsonList()) { - l.onAdvertisement(json); - } - } - - /** Bonded Even devices as one JSON array, for callers without a listener. */ - public String getBondedCandidatesJson() { - JSONArray array = new JSONArray(); - for (String json : bondedCandidateJsonList()) { - try { - array.put(new JSONObject(json)); - } catch (JSONException ignored) { - } - } - return array.toString(); - } - - // ------------------------------------------------------------------ - // Internals - // ------------------------------------------------------------------ - - 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 bondedCandidateJsonList() { - List out = new ArrayList<>(); - Set 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, FaceclawFirmwareUtil.bytesToHex(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 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 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(); - // A bare "R1" substring also matches unrelated hardware ("Oppo Enco - // R1"), and TS only classifies rings from the stock name prefix. - return upper.contains("G2") || upper.startsWith("EVEN 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 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); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.kt new file mode 100644 index 00000000..b43bcaae --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.kt @@ -0,0 +1,372 @@ +package com.faceclaw.app + +import android.annotation.SuppressLint +import android.bluetooth.BluetoothAdapter +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.ArrayList + +/** + * 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, the stock + * "EVEN R1…" ring prefix, 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; this filter + * must stay a superset of the TS classifier's admission. + */ +@SuppressLint("MissingPermission") +class FaceclawDeviceDiscovery(context: Context) { + companion object { + /** Even Realities' company identifier: the ASCII bytes "ER" read little-endian. */ + const val EVEN_COMPANY_ID = 0x5245 + + // ------------------------------------------------------------------ + // Internals + // ------------------------------------------------------------------ + + private fun buildScanSettings(): ScanSettings { + val builder = 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() + } + + /** Null when the result is not something we pair with. */ + private fun toJson(result: ScanResult): JSONObject? { + val device = result.device ?: return null + val record = result.scanRecord + val liveName = record?.deviceName + val name = if (liveName != null && !liveName.isEmpty()) liveName else device.name + val manufacturerData = extractEvenManufacturerData(record) + if (!isAdmissible(name, manufacturerData)) { + return null + } + var txPower: Int? = null + if (record != null && record.txPowerLevel != Int.MIN_VALUE) { + txPower = record.txPowerLevel + } + if (txPower == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O + && result.txPower != ScanResult.TX_POWER_NOT_PRESENT) { + txPower = result.txPower + } + var connectable: Boolean? = null + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + connectable = result.isConnectable + } + val rssi = result.rssi + // 127 is the "unavailable" sentinel; 0 dBm never happens for a real sample. + val rssiValue: Int? = if (rssi == 127 || rssi == 0) null else rssi + val bonded = device.bondState == BluetoothDevice.BOND_BONDED + return buildJson( + device.address, name, FaceclawFirmwareUtil.bytesToHex(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 HCI captures + * document. Prefers Even's own id; otherwise returns the first record so an + * unexpected company id still reaches the diagnostics log. + */ + private fun extractEvenManufacturerData(record: ScanRecord?): ByteArray { + if (record == null) { + return ByteArray(0) + } + val all: SparseArray? = record.manufacturerSpecificData + if (all == null || all.size() == 0) { + return ByteArray(0) + } + var payload: ByteArray? = all.get(EVEN_COMPANY_ID) + var companyId = EVEN_COMPANY_ID + if (payload == null) { + companyId = all.keyAt(0) + payload = all.valueAt(0) + } + if (payload == null) { + return ByteArray(0) + } + val out = ByteArray(payload.size + 2) + out[0] = (companyId and 0xff).toByte() + out[1] = ((companyId shr 8) and 0xff).toByte() + System.arraycopy(payload, 0, out, 2, payload.size) + return out + } + + private fun isAdmissible(name: String?, manufacturerData: ByteArray?): Boolean { + if (manufacturerData != null && manufacturerData.size >= 2 + && manufacturerData[0] == 0x45.toByte() && manufacturerData[1] == 0x52.toByte()) { + return true + } + if (name == null) { + return false + } + val upper = name.uppercase() + // A bare "R1" substring also matches unrelated hardware ("Oppo Enco + // R1"), and TS only classifies rings from the stock name prefix. + return upper.contains("G2") || upper.startsWith("EVEN R1") + } + + private fun buildJson( + address: String?, name: String?, manufacturerHex: String?, rssi: Int?, txPower: Int?, + connectable: Boolean?, bonded: Boolean, source: String, seenAtMs: Long): JSONObject? { + if (address == null || address.isEmpty()) { + return null + } + try { + val obj = JSONObject() + obj.put("address", address) + obj.put("name", name ?: "") + obj.put("manufacturerData", manufacturerHex ?: "") + obj.put("rssi", rssi ?: JSONObject.NULL) + obj.put("txPower", txPower ?: JSONObject.NULL) + obj.put("connectable", connectable ?: JSONObject.NULL) + obj.put("bonded", bonded) + obj.put("source", source) + obj.put("seenAtMs", seenAtMs) + return obj + } catch (e: JSONException) { + return null + } + } + + private fun describeScanError(errorCode: Int): String { + return when (errorCode) { + ScanCallback.SCAN_FAILED_ALREADY_STARTED -> + "a scan is already running" + ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED -> + "the Bluetooth stack refused to register the scan (try toggling Bluetooth)" + ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED -> + "BLE scanning is not supported on this device" + ScanCallback.SCAN_FAILED_INTERNAL_ERROR -> + "internal Bluetooth error" + 5 -> // SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES (API 31) + "out of Bluetooth hardware resources" + 6 -> // SCAN_FAILED_SCANNING_TOO_FREQUENTLY (API 31) + "scanning too frequently; wait a moment and try again" + else -> + "scan failed (code $errorCode)" + } + } + } + + private val bluetoothAdapter: BluetoothAdapter + private val mainHandler = Handler(Looper.getMainLooper()) + + private val lock = Any() + private var activeCallback: ScanCallback? = null + @Volatile + private var listener: FaceclawDeviceDiscoveryListener? = null + + init { + val appContext = context.applicationContext + val bluetoothManager = + appContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager? + if (bluetoothManager == null || bluetoothManager.adapter == null) { + throw IllegalStateException("Bluetooth adapter unavailable") + } + this.bluetoothAdapter = bluetoothManager.adapter + } + + fun isBluetoothEnabled(): Boolean { + return bluetoothAdapter.isEnabled + } + + fun setListener(newListener: FaceclawDeviceDiscoveryListener?) { + this.listener = newListener + } + + // ------------------------------------------------------------------ + // 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. + */ + fun startScan(): Boolean { + synchronized(lock) { + if (activeCallback != null) { + return true + } + if (!bluetoothAdapter.isEnabled) { + emitLog("scan not started: Bluetooth is off") + return false + } + val scanner: BluetoothLeScanner? = bluetoothAdapter.bluetoothLeScanner + if (scanner == null) { + emitLog("scan not started: no BLE scanner") + return false + } + val callback = object : ScanCallback() { + override fun onScanResult(callbackType: Int, result: ScanResult?) { + if (result != null) { + emitAdvertisement(result) + } + } + + override fun onBatchScanResults(results: List?) { + if (results == null) { + return + } + for (result in results) { + if (result != null) { + emitAdvertisement(result) + } + } + } + + override fun onScanFailed(errorCode: Int) { + synchronized(lock) { + if (activeCallback === this) { + activeCallback = null + } + } + val l = listener + if (l != null) { + val message = describeScanError(errorCode) + mainHandler.post { l.onScanFailed(errorCode, message) } + } + } + } + activeCallback = callback + try { + scanner.startScan(null, buildScanSettings(), callback) + } catch (t: Throwable) { + activeCallback = null + emitLog("scan not started: $t") + return false + } + emitLog("scan started (unfiltered, low latency)") + return true + } + } + + fun stopScan() { + val callback: ScanCallback? + synchronized(lock) { + callback = activeCallback + activeCallback = null + } + if (callback == null) { + return + } + val scanner = bluetoothAdapter.bluetoothLeScanner + if (scanner != null) { + try { + scanner.stopScan(callback) + } catch (ignored: Throwable) { + } + } + emitLog("scan stopped") + } + + fun isScanning(): Boolean { + synchronized(lock) { + return activeCallback != 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. + */ + fun emitBondedDevices() { + val l = listener ?: return + for (json in bondedCandidateJsonList()) { + l.onAdvertisement(json) + } + } + + /** Bonded Even devices as one JSON array, for callers without a listener. */ + fun getBondedCandidatesJson(): String { + val array = JSONArray() + for (json in bondedCandidateJsonList()) { + try { + array.put(JSONObject(json)) + } catch (ignored: JSONException) { + } + } + return array.toString() + } + + private fun emitAdvertisement(result: ScanResult) { + val obj = toJson(result) ?: return + val l = listener ?: return + val json = obj.toString() + if (Looper.myLooper() == Looper.getMainLooper()) { + l.onAdvertisement(json) + } else { + mainHandler.post { + val current = listener + if (current != null) { + current.onAdvertisement(json) + } + } + } + } + + private fun bondedCandidateJsonList(): List { + val out = ArrayList() + val bonded: Set? = bluetoothAdapter.bondedDevices + if (bonded == null) { + return out + } + val now = System.currentTimeMillis() + for (device in bonded) { + if (device == null) { + continue + } + val name = device.name + if (!isAdmissible(name, null)) { + continue + } + val obj = buildJson( + device.address, name, "", null, null, null, true, "paired", now) + if (obj != null) { + out.add(obj.toString()) + } + } + return out + } + + private fun emitLog(line: String) { + val l = listener + if (l != null) { + l.onLog(line) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscoveryListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscoveryListener.java deleted file mode 100644 index 922e7adb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscoveryListener.java +++ /dev/null @@ -1,18 +0,0 @@ -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); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java deleted file mode 100644 index c4ad3efe..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java +++ /dev/null @@ -1,619 +0,0 @@ -package com.faceclaw.app; - -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothGatt; -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; -import android.util.Log; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -/** - * Connects to the glasses (stock-firmware compatible), reads the device-info / - * settings response, and reports the firmware versions plus the firmware- - * extension string (empty on stock firmware). Used by onboarding to decide whether to - * flash. Reuses FaceclawBleManager + BleProtocol; owns its own connection and - * runs on a single worker thread. Shows nothing on the lens. - */ -public class FaceclawDeviceInfoProbe implements FaceclawBleListener { - private static final String TAG = "FaceclawDeviceInfo"; - private static final int QUERY_TIMEOUT_MS = 4_000; - /** Connect + auth attempts per arm; a link that drops mid-pairing is retried once the bond settles. */ - private static final int ARM_ATTEMPTS = 3; - private static final int ARM_RETRY_DELAY_MS = 1_000; - /** - * Upper bound on waiting for the auth success while Android reports the arm - * as BOND_BONDING — i.e. an OS pairing dialog may be sitting there waiting - * for the user, which can take a lot longer than the normal 30 s window. - */ - private static final int PAIRING_WAIT_CAP_MS = 90_000; - /** After a fresh bond, how long the firmware gets to notify success on its own before the request is re-sent. */ - private static final int POST_BOND_GRACE_MS = 2_000; - private static final int POLL_MS = 250; - - private final Context context; - private final String rightAddress; - private final String leftAddress; - private final FaceclawBleManager bleManager; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private final Object lock = new Object(); - private int nextSeq = 0x40; - private int nextMagic = 100; - - private int awaitSid = -1; - private int awaitMagic = -1; - private byte[] awaitPb = null; - private CountDownLatch awaitLatch = null; - - /** - * Security-auth wait, kept separate from the generic ack wait: only the - * firmware's SUCCESS result (empty result message) may satisfy it. The - * firmware answers the request as soon as it arrives, but with a - * non-success result until the link is encrypted; accepting that first - * reply as "the ack" is what let the probe move on to the other arm while - * Android's pairing dialog for this one was still up. - */ - private final Set authMagics = new HashSet<>(); - private CountDownLatch authLatch = null; - - /** - * A sid-0x09 frame that carried firmware versions without matching the - * awaited magic — some firmware pushes the settings snapshot on a magic it - * picked itself instead of (or in addition to) acking the read. - */ - private volatile byte[] unsolicitedSettingsPb; - - private volatile FaceclawDeviceInfoProbeListener listener; - private volatile Thread worker; - private volatile boolean cancelled = false; - - public FaceclawDeviceInfoProbe(Context context, String rightAddress, String leftAddress) { - this.context = context.getApplicationContext(); - this.rightAddress = rightAddress == null ? "" : rightAddress; - this.leftAddress = leftAddress == null ? "" : leftAddress; - this.bleManager = new FaceclawBleManager(this.context); - this.bleManager.setListener(this); - } - - public void setListener(FaceclawDeviceInfoProbeListener listener) { - this.listener = listener; - } - - public void start() { - synchronized (lock) { - if (worker != null) { - return; - } - worker = new Thread(this::run, "faceclaw-device-info"); - worker.start(); - } - } - - public void cancel() { - cancelled = true; - Thread w = worker; - if (w != null) { - w.interrupt(); - } - } - - public void close() { - cancel(); - try { - bleManager.close(); - } catch (Exception ignored) { - } - } - - private void run() { - try { - if (rightAddress.trim().isEmpty()) { - emitError("No right-arm address configured."); - return; - } - - boolean haveLeft = !leftAddress.trim().isEmpty() && !leftAddress.equalsIgnoreCase(rightAddress); - - // Bring up and authenticate BOTH arms before asking for versions. - // Each arm is its own peripheral with its own bond, so on a fresh - // phone each raises its own pairing prompt; completing both here - // means the flash step (and the app proper) find both bonds in - // place. Strictly one arm at a time: Android pairs with one device - // at a time, and a second arm connected while the first was still - // pairing has been seen to drop within a second and never pair - // (2026-09-11 logcat). - boolean rightAuthenticated = bringUpArm(rightAddress, "right"); - boolean leftAuthenticated = false; - if (haveLeft && !cancelled) { - leftAuthenticated = bringUpArm(leftAddress, "left"); - } - if (cancelled) { - emitError("Cancelled."); - return; - } - if (!bleManager.isConnected(rightAddress)) { - // The right lens may have dropped while the left one paired; - // it is bonded by now, so this comes back quickly. - emitLog("right lens disconnected while the left lens was set up; reconnecting"); - rightAuthenticated = bringUpArm(rightAddress, "right"); - if (cancelled) { - emitError("Cancelled."); - return; - } - } - - byte[] ack = null; - String rightFailure = null; - try { - ack = queryArm(rightAddress, "right", rightAuthenticated); - } catch (IllegalStateException e) { - rightFailure = e.getMessage() == null ? e.toString() : e.getMessage(); - emitLog("right-lens query failed: " + rightFailure); - } - // The right lens is the documented control endpoint, but a silent - // right lens has been observed on stock 2.2.9 even after a - // successful security auth — run the same probe against the left - // lens rather than giving up, and log which lens answered. - if (ack == null && !cancelled && haveLeft) { - emitLog("right lens did not answer the settings query; probing the left lens"); - try { - ack = queryArm(leftAddress, "left", leftAuthenticated); - } catch (Exception e) { - emitLog("left-lens probe failed: " + (e.getMessage() == null ? e.toString() : e.getMessage())); - } - } - if (cancelled) { - emitError("Cancelled."); - return; - } - if (ack == null) { - throw new IllegalStateException(rightFailure != null - ? rightFailure - : "no response to the device-info query on either lens — check `adb logcat -s " - + TAG + "` for the frame trace"); - } - - BleProtocol.FirmwareInfo info = BleProtocol.parseSettingsFirmwareInfo(ack); - String left = info == null ? "" : info.leftVersion; - String right = info == null ? "" : info.rightVersion; - String extension = info == null ? "" : info.extension; - emitLog("device-info: L=" + left + " R=" + right + " ext=[" + extension + "]"); - emitResult(left, right, extension); - } catch (Exception e) { - String message = cancelled ? "Cancelled." : (e.getMessage() == null ? e.toString() : e.getMessage()); - emitError(message); - } finally { - try { - bleManager.close(); - } catch (Exception ignored) { - } - } - } - - /** - * Connect to one arm and complete its security-auth exchange, retrying - * when the link drops mid-pairing. Returns whether the auth success was - * seen (false = connected but unconfirmed, tolerated because the custom - * firmware's response to the exchange is not yet hardware-verified); - * throws when the arm cannot be brought up at all. - */ - private boolean bringUpArm(String address, String label) throws InterruptedException { - String lastFailure = null; - for (int attempt = 1; attempt <= ARM_ATTEMPTS && !cancelled; attempt++) { - if (attempt > 1) { - // A link Android dropped while it was still pairing comes back - // once the bond settles; reconnecting sooner just fails again. - waitForBondToSettle(address, label); - if (cancelled) { - break; - } - emitLog("retrying the " + label + " lens (attempt " + attempt + "/" + ARM_ATTEMPTS + "): " + lastFailure); - Thread.sleep(ARM_RETRY_DELAY_MS); - } - emitState("connecting", label); - try { - connectArm(address); - } catch (IllegalStateException e) { - lastFailure = e.getMessage() == null ? e.toString() : e.getMessage(); - emitLog(label + " lens: " + lastFailure); - continue; - } - if (cancelled) { - break; - } - - // Firmware 2.2.9 answers no queries until the security-auth - // exchange completes over an encrypted link; on a phone with no - // existing bond this is also what triggers SMP pairing (and its OS - // prompt), so it must come before the prelude and query. - emitState("authenticating", label); - AuthResult result = authenticate(address, label); - if (result == AuthResult.SUCCESS) { - return true; - } - if (result == AuthResult.UNCONFIRMED) { - return false; - } - lastFailure = "link dropped during authentication"; - } - if (cancelled) { - return false; - } - throw new IllegalStateException("could not connect to the " + label + " lens (" + address + "): " - + lastFailure + " — if Android showed a Bluetooth pairing request, accept it and try again"); - } - - private enum AuthResult { SUCCESS, UNCONFIRMED, LINK_DROPPED } - - /** - * Send the sid-0x80 authentication request and wait for the firmware's - * SUCCESS notification, which only arrives once the link is encrypted. - * On an unbonded phone that means waiting through Android's pairing flow - * (BOND_BONDING, possibly with a dialog up), so the wait is extended while - * the OS reports pairing in progress and the request is re-sent once - * after a fresh bond in case the pre-pairing write was dropped. Bond - * state is polled rather than awaited via broadcast; 250 ms granularity - * is plenty here. - */ - private AuthResult authenticate(String address, String label) throws InterruptedException { - long start = SystemClock.elapsedRealtime(); - long deadline = start + ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS; - long hardDeadline = start + PAIRING_WAIT_CAP_MS; - CountDownLatch latch = new CountDownLatch(1); - synchronized (lock) { - authMagics.clear(); - authLatch = latch; - } - int initialBond = bleManager.getBondState(address); - emitLog("security auth: " + label + " lens, Android bond state " + bondStateName(initialBond)); - try { - int sends = 0; - boolean lastSendFailed = false; - long nextSendAt = start; - long bondedAt = -1; - boolean resentAfterBond = false; - boolean loggedBonding = false; - while (!cancelled) { - long now = SystemClock.elapsedRealtime(); - int bond = bleManager.getBondState(address); - - if (bond == BluetoothDevice.BOND_BONDING) { - // OS pairing in progress — the firmware will answer once the - // link is encrypted. Don't write, don't give up, don't move - // on to the other arm. - if (!loggedBonding) { - emitLog("Android is pairing with the " + label + " lens; waiting for the user to accept"); - loggedBonding = true; - } - if (now >= hardDeadline) { - break; - } - } else { - if (bond == BluetoothDevice.BOND_BONDED && initialBond != BluetoothDevice.BOND_BONDED - && bondedAt < 0) { - bondedAt = now; - emitLog("Android bond established with the " + label + " lens; waiting for the auth success"); - deadline = Math.max(deadline, now + ConnectionOptions.SECURITY_AUTH_SOFT_TIMEOUT_MS); - } - boolean wantSend = sends == 0 - || (lastSendFailed && sends < 3 && now >= nextSendAt) - || (bondedAt >= 0 && !resentAfterBond && now - bondedAt >= POST_BOND_GRACE_MS); - if (wantSend) { - if (bondedAt >= 0) { - resentAfterBond = true; - if (sends > 0) { - emitLog("re-sending the auth request after pairing (" + label + " lens)"); - } - } - lastSendFailed = !sendAuthRequest(address); - sends++; - nextSendAt = SystemClock.elapsedRealtime() + ARM_RETRY_DELAY_MS; - if (lastSendFailed && !bleManager.isConnected(address)) { - return AuthResult.LINK_DROPPED; - } - } - if (now >= deadline) { - break; - } - } - - if (latch.await(POLL_MS, TimeUnit.MILLISECONDS)) { - emitLog("security auth complete: " + label + " lens (" + address + ")"); - return AuthResult.SUCCESS; - } - if (!bleManager.isConnected(address)) { - emitLog("security auth: " + label + " lens disconnected while waiting" - + (bond == BluetoothDevice.BOND_BONDING ? " (Android was still pairing)" : "")); - return AuthResult.LINK_DROPPED; - } - } - if (cancelled) { - return AuthResult.UNCONFIRMED; - } - emitLog("security auth unconfirmed: " + label + " lens (" + address + "), Android bond state " - + bondStateName(bleManager.getBondState(address))); - return AuthResult.UNCONFIRMED; - } finally { - synchronized (lock) { - authLatch = null; - authMagics.clear(); - } - } - } - - private boolean sendAuthRequest(String address) { - int magic = allocMagic(); - int seq; - synchronized (lock) { - authMagics.add(magic); - seq = nextSeq++ & 0xff; - } - byte[] payload = BleProtocol.buildAuthenticationRequest(magic); - emitLog(String.format("tx %s sid=0x%02x flag=0x%02x magic=%d seq=0x%02x len=%d", - address, BleProtocol.SID_SECURITY_AUTH, BleProtocol.FLAG_SECURITY_AUTH, magic, seq, payload.length)); - List frames = BleProtocol.framePb(payload, BleProtocol.SID_SECURITY_AUTH, BleProtocol.FLAG_SECURITY_AUTH, seq); - boolean written; - try { - written = bleManager.writeFrames( - address, BleProtocol.WRITE_CHAR_UUID, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS); - } catch (IllegalStateException e) { - // "Not connected" — the link went away underneath us. - written = false; - } - if (!written) { - emitLog("tx write FAILED sid=0x80 (" + address + ")"); - } - return written; - } - - /** Block while Android reports this arm as BOND_BONDING (pairing dialog up / SMP in flight). */ - private void waitForBondToSettle(String address, String label) throws InterruptedException { - long cap = SystemClock.elapsedRealtime() + PAIRING_WAIT_CAP_MS; - boolean logged = false; - while (!cancelled && bleManager.getBondState(address) == BluetoothDevice.BOND_BONDING - && SystemClock.elapsedRealtime() < cap) { - if (!logged) { - emitLog("waiting for Android to finish pairing with the " + label + " lens before reconnecting"); - logged = true; - } - Thread.sleep(POLL_MS); - } - } - - private static String bondStateName(int state) { - switch (state) { - case BluetoothDevice.BOND_NONE: - return "none"; - case BluetoothDevice.BOND_BONDING: - return "bonding"; - case BluetoothDevice.BOND_BONDED: - return "bonded"; - default: - return "unknown"; - } - } - - /** - * Prelude + settings read on an already connected/authenticated lens. - * Returns the settings ack protobuf, an unsolicited settings push that - * carried firmware versions, or null when the lens never answered the read. - * Throws on prelude failure; the caller wraps the fallback lens's attempt - * so its failure cannot mask the primary lens's outcome. - */ - private byte[] queryArm(String address, String label, boolean authenticated) throws InterruptedException { - if (cancelled) { - return null; - } - - emitState("querying", label); - // Session prelude, then a settings/device-info read (both arms' - // versions and the firmware-extension string ride back in one response). - if (writeAndAwaitAck(address, BleProtocol.PRELUDE_ACK_SID, BleProtocol.FLAG_REQUEST, - BleProtocol.PRELUDE_ACK_MAGIC, BleProtocol.PRELUDE_F5872_PAYLOAD, - ConnectionOptions.PRELUDE_TIMEOUT_MS) == null) { - throw new IllegalStateException("session prelude not acked (" + label + " lens)" - + (authenticated ? "" : "; authentication did not complete —" - + " if Android shows a Bluetooth pairing request, accept it and try again")); - } - - // Two attempts: the first read straight after a fresh pairing has been - // seen to go unanswered while a later one succeeds. - for (int attempt = 0; attempt < 2 && !cancelled; attempt++) { - int magic = allocMagic(); - byte[] ack = writeAndAwaitAck(address, BleProtocol.SID_UI_SETTING, BleProtocol.FLAG_REQUEST, - magic, BleProtocol.buildSettingsQuery(magic), QUERY_TIMEOUT_MS); - if (ack != null) { - return ack; - } - // A push with the firmware versions on the device's own magic is - // as good as the ack we asked for. - byte[] pushed = unsolicitedSettingsPb; - if (pushed != null) { - emitLog("using unsolicited settings push instead of the read ack (" + label + " lens)"); - return pushed; - } - emitLog("settings query attempt " + (attempt + 1) + " unanswered (" + label + " lens)"); - } - return null; - } - - private void connectArm(String address) { - if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { - throw new IllegalStateException("connect failed"); - } - bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH); - bleManager.requestMtu(address, ConnectionOptions.DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); - if (!bleManager.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) { - throw new IllegalStateException("service discovery failed"); - } - if (!bleManager.enableNotifications(address, BleProtocol.NOTIFY_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { - throw new IllegalStateException("could not subscribe to notifications"); - } - } - - private byte[] writeAndAwaitAck(String address, int sid, int flag, int magic, byte[] payload, int timeoutMs) - throws InterruptedException { - CountDownLatch latch = new CountDownLatch(1); - synchronized (lock) { - awaitSid = sid; - awaitMagic = magic; - awaitPb = null; - awaitLatch = latch; - } - int seq; - synchronized (lock) { - seq = nextSeq++ & 0xff; - } - emitLog(String.format("tx %s sid=0x%02x flag=0x%02x magic=%d seq=0x%02x len=%d", - address, sid, flag, magic, seq, payload.length)); - List frames = BleProtocol.framePb(payload, sid, flag, seq); - boolean written = bleManager.writeFrames( - address, BleProtocol.WRITE_CHAR_UUID, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS); - if (!written) { - emitLog("tx write FAILED sid=0x" + Integer.toHexString(sid)); - synchronized (lock) { - awaitLatch = null; - } - return null; - } - boolean acked = latch.await(timeoutMs, TimeUnit.MILLISECONDS); - synchronized (lock) { - awaitLatch = null; - return acked ? awaitPb : null; - } - } - - @Override - public void onNotification(String address, String characteristicUuid, byte[] data) { - if (!BleProtocol.NOTIFY_CHAR_UUID.equalsIgnoreCase(characteristicUuid)) { - return; - } - // One notification value can carry several envelope frames back to - // back; reading only the first would silently drop the rest. - List frames = BleProtocol.splitFrames(data); - if (frames.size() > 1) { - emitLog("rx " + address + " value carries " + frames.size() + " frames (raw " + data.length + " bytes)"); - } - for (byte[] buf : frames) { - handleFrame(address, buf, data.length); - } - } - - private void handleFrame(String address, byte[] buf, int rawValueLength) { - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(buf); - if (!frame.ok) { - emitLog("rx " + address + " unparseable frame len=" + buf.length + " (raw value " + rawValueLength + ")" - + " head=" + FaceclawFirmwareUtil.bytesToHex(java.util.Arrays.copyOf(buf, Math.min(16, buf.length)))); - return; - } - // Log every control frame while diagnosing 2.2.9: sid/flag/type/magic - // plus a payload prefix is enough to reconstruct what the lens said. - int declared = buf.length > 3 ? buf[3] & 0xff : 0; - String truncated = buf.length < 8 + declared ? " TRUNCATED(declared=" + declared + ")" : ""; - emitLog(String.format("rx %s sid=0x%02x flag=0x%02x type=%d magic=%d frag=%d/%d len=%d%s pb=%s", - address, frame.sid, frame.flag, frame.msgType, frame.msgSeq, - buf.length > 5 ? buf[5] & 0xff : 0, buf.length > 4 ? buf[4] & 0xff : 0, frame.pb.length, truncated, - FaceclawFirmwareUtil.bytesToHex(java.util.Arrays.copyOf(frame.pb, Math.min(48, frame.pb.length))))); - if (frame.sid == BleProtocol.SID_UI_SETTING - && BleProtocol.parseSettingsFirmwareInfo(frame.pb) != null) { - // Any settings frame carrying firmware versions answers the probe's - // question, whether or not it matches the magic we asked with. - unsolicitedSettingsPb = frame.pb; - } - if (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT) { - // Async event, not an ack. This includes the lens's own periodic - // sid-0x80 notifies (device-chosen magic, non-empty result). - return; - } - if (frame.sid == BleProtocol.SID_SECURITY_AUTH) { - boolean success = false; - boolean ours = false; - synchronized (lock) { - if (authLatch != null) { - ours = authMagics.contains(frame.msgSeq); - for (int magic : authMagics) { - if (BleProtocol.isAuthenticationSuccess(frame.pb, magic)) { - success = true; - authLatch.countDown(); - break; - } - } - } - } - if (ours && !success) { - // Expected before the link is encrypted: the firmware answers - // the request straight away with a non-success result and - // sends the real success once pairing/encryption completes. - emitLog("auth reply for magic " + frame.msgSeq + " is not the success result; still waiting"); - } - return; - } - synchronized (lock) { - if (awaitLatch != null && frame.sid == awaitSid && frame.msgSeq == awaitMagic) { - awaitPb = frame.pb; - awaitLatch.countDown(); - } - } - } - - @Override - public void onConnectionStateChange(String address, boolean connected) { - if (!connected) { - Log.i(TAG, "disconnected: " + address); - } - } - - private int allocMagic() { - synchronized (lock) { - int magic = nextMagic; - nextMagic = nextMagic >= 255 ? 100 : nextMagic + 1; - return magic; - } - } - - private void emitLog(String line) { - Log.i(TAG, line); - mainHandler.post(() -> { - FaceclawDeviceInfoProbeListener current = listener; - if (current != null) { - current.onLog(line); - } - }); - } - - private void emitState(String state, String detail) { - final String safeDetail = detail == null ? "" : detail; - mainHandler.post(() -> { - FaceclawDeviceInfoProbeListener current = listener; - if (current != null) { - current.onState(state, safeDetail); - } - }); - } - - private void emitResult(String left, String right, String extension) { - mainHandler.post(() -> { - FaceclawDeviceInfoProbeListener current = listener; - if (current != null) { - current.onResult(left, right, extension); - } - }); - } - - private void emitError(String message) { - final String safeMessage = message == null ? "" : message; - mainHandler.post(() -> { - FaceclawDeviceInfoProbeListener current = listener; - if (current != null) { - current.onError(safeMessage); - } - }); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.kt new file mode 100644 index 00000000..5568a6ce --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.kt @@ -0,0 +1,78 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.Handler +import android.os.Looper + +/** + * Android host for the shared [DeviceInfoProbeFlow]: owns the GATT link (AndroidStockLink), + * the worker thread the flow runs on, and delivers listener callbacks on the main thread. + * Public API unchanged for the TypeScript wrapper (app/native/device-info-probe.ts). + */ +class FaceclawDeviceInfoProbe(context: Context, rightAddress: String?, leftAddress: String?) { + private val link = AndroidStockLink(context.applicationContext) + private val mainHandler = Handler(Looper.getMainLooper()) + private val lock = Any() + + @Volatile + private var listener: FaceclawDeviceInfoProbeListener? = null + + @Volatile + private var worker: Thread? = null + + private val flow = + DeviceInfoProbeFlow( + link, + rightAddress ?: "", + leftAddress ?: "", + object : FaceclawDeviceInfoProbeListener { + override fun onLog(line: String?) { + android.util.Log.i(DeviceInfoProbeFlow.TAG, line ?: "") + post { it.onLog(line) } + } + + override fun onState(state: String?, detail: String?) = post { it.onState(state, detail ?: "") } + + override fun onResult(leftVersion: String?, rightVersion: String?, extension: String?) = + post { it.onResult(leftVersion, rightVersion, extension) } + + override fun onError(message: String?) = post { it.onError(message ?: "") } + }, + ) + + fun setListener(listener: FaceclawDeviceInfoProbeListener?) { + this.listener = listener + } + + fun start() { + synchronized(lock) { + if (worker != null) { + return + } + val w = Thread({ flow.run() }, "faceclaw-device-info") + worker = w + w.start() + } + } + + fun cancel() { + flow.cancel() + } + + fun close() { + cancel() + try { + link.close() + } catch (ignored: Exception) { + } + } + + private fun post(action: (FaceclawDeviceInfoProbeListener) -> Unit) { + mainHandler.post { + val current = listener + if (current != null) { + action(current) + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.java deleted file mode 100644 index ea9352aa..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.java +++ /dev/null @@ -1,262 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import com.k2fsa.sherpa.onnx.FastClusteringConfig; -import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarization; -import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarizationConfig; -import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarizationSegment; -import com.k2fsa.sherpa.onnx.OfflineSpeakerSegmentationModelConfig; -import com.k2fsa.sherpa.onnx.OfflineSpeakerSegmentationPyannoteModelConfig; -import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractorConfig; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.io.DataInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.ByteBuffer; - -/** - * Offline re-diarization of a stored conversation recording (16-bit mono - * WAV): pyannote segmentation plus the shared speaker-embedding model with - * clustering, via sherpa-onnx. Runs on its own thread; results are handed to - * TS as JSON turns for reconciliation against the transcript segments. - */ -public class FaceclawDiarizer { - private static final String TAG = "FaceclawDiarizer"; - - public interface Listener { - /** progress in [0, 1]. */ - void onProgress(double progress); - - /** turns: [{startMs, endMs, cluster}], sorted by start time. */ - void onDone(String turnsJson); - - void onError(String message); - } - - private final String segmentationModelPath; - private final String embeddingModelPath; - private final String wavPath; - private final int numClusters; - private final float threshold; - private final Listener listener; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private volatile boolean cancelled; - - public FaceclawDiarizer( - String segmentationModelPath, - String embeddingModelPath, - String wavPath, - int numClusters, - float threshold, - Listener listener) { - this.segmentationModelPath = segmentationModelPath; - this.embeddingModelPath = embeddingModelPath; - this.wavPath = wavPath; - this.numClusters = numClusters; - this.threshold = threshold <= 0 ? 0.5f : threshold; - this.listener = listener; - } - - public void start() { - Thread thread = new Thread(this::run, "FaceclawDiarizer"); - thread.setPriority(Thread.MIN_PRIORITY); - thread.start(); - } - - public void cancel() { - cancelled = true; - } - - private void run() { - OfflineSpeakerDiarization diarization = null; - try { - float[] samples = wavPath.endsWith(".wav") - ? readWavMono16(wavPath) - : decodeCompressedMono(wavPath); - OfflineSpeakerDiarizationConfig config = OfflineSpeakerDiarizationConfig.builder() - .setSegmentation(OfflineSpeakerSegmentationModelConfig.builder() - .setPyannote(OfflineSpeakerSegmentationPyannoteModelConfig.builder() - .setModel(segmentationModelPath) - .build()) - .setNumThreads(1) - .build()) - .setEmbedding(SpeakerEmbeddingExtractorConfig.builder() - .setModel(embeddingModelPath) - .setNumThreads(1) - .build()) - .setClustering(FastClusteringConfig.builder() - .setNumClusters(numClusters > 0 ? numClusters : -1) - .setThreshold(threshold) - .build()) - .setMinDurationOn(0.3f) - .setMinDurationOff(0.5f) - .build(); - diarization = new OfflineSpeakerDiarization(config); - OfflineSpeakerDiarizationSegment[] segments = diarization.processWithCallback( - samples, - (numProcessedChunks, numTotalChunks, arg) -> { - emitProgress(numTotalChunks <= 0 ? 0 - : (double) numProcessedChunks / numTotalChunks); - return cancelled ? 1 : 0; - }); - JSONArray turns = new JSONArray(); - if (segments != null) { - for (OfflineSpeakerDiarizationSegment segment : segments) { - JSONObject turn = new JSONObject(); - turn.put("startMs", Math.round(segment.getStart() * 1000)); - turn.put("endMs", Math.round(segment.getEnd() * 1000)); - turn.put("cluster", segment.getSpeaker()); - turns.put(turn); - } - } - String result = turns.toString(); - mainHandler.post(() -> listener.onDone(result)); - } catch (Throwable t) { - Log.w(TAG, "diarization failed for " + wavPath, t); - String message = t.getMessage() == null ? t.toString() : t.getMessage(); - mainHandler.post(() -> listener.onError(message)); - } finally { - if (diarization != null) { - diarization.release(); - } - } - } - - private void emitProgress(double progress) { - mainHandler.post(() -> listener.onProgress(Math.max(0, Math.min(1, progress)))); - } - - /** - * Decode a compressed recording (M4A/AAC from the transcode sweep) into - * [-1, 1] floats via MediaExtractor + MediaCodec. Recordings are written - * as 16 kHz mono, so no resampling is needed. - */ - private static float[] decodeCompressedMono(String path) throws IOException { - android.media.MediaExtractor extractor = new android.media.MediaExtractor(); - android.media.MediaCodec codec = null; - java.io.ByteArrayOutputStream pcm = new java.io.ByteArrayOutputStream(); - try { - extractor.setDataSource(path); - int trackIndex = -1; - android.media.MediaFormat format = null; - for (int i = 0; i < extractor.getTrackCount(); i++) { - android.media.MediaFormat candidate = extractor.getTrackFormat(i); - String mime = candidate.getString(android.media.MediaFormat.KEY_MIME); - if (mime != null && mime.startsWith("audio/")) { - trackIndex = i; - format = candidate; - break; - } - } - if (trackIndex < 0 || format == null) { - throw new IOException("no audio track in " + path); - } - extractor.selectTrack(trackIndex); - codec = android.media.MediaCodec.createDecoderByType( - format.getString(android.media.MediaFormat.KEY_MIME)); - codec.configure(format, null, null, 0); - codec.start(); - android.media.MediaCodec.BufferInfo info = new android.media.MediaCodec.BufferInfo(); - boolean inputDone = false; - boolean outputDone = false; - while (!outputDone) { - if (!inputDone) { - int inIndex = codec.dequeueInputBuffer(10_000); - if (inIndex >= 0) { - ByteBuffer inBuf = codec.getInputBuffer(inIndex); - int size = extractor.readSampleData(inBuf, 0); - if (size < 0) { - codec.queueInputBuffer(inIndex, 0, 0, 0, - android.media.MediaCodec.BUFFER_FLAG_END_OF_STREAM); - inputDone = true; - } else { - codec.queueInputBuffer(inIndex, 0, size, extractor.getSampleTime(), 0); - extractor.advance(); - } - } - } - int outIndex = codec.dequeueOutputBuffer(info, 10_000); - if (outIndex >= 0) { - ByteBuffer outBuf = codec.getOutputBuffer(outIndex); - byte[] chunk = new byte[info.size]; - outBuf.position(info.offset); - outBuf.get(chunk); - pcm.write(chunk); - codec.releaseOutputBuffer(outIndex, false); - if ((info.flags & android.media.MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { - outputDone = true; - } - } - } - } finally { - extractor.release(); - if (codec != null) { - try { - codec.stop(); - } catch (Throwable ignored) { - } - codec.release(); - } - } - byte[] bytes = pcm.toByteArray(); - int count = bytes.length / 2; - float[] samples = new float[count]; - for (int i = 0; i < count; i++) { - samples[i] = ((short) ((bytes[i * 2] & 0xff) | (bytes[i * 2 + 1] << 8))) / 32768.0f; - } - return samples; - } - - /** Load a 16-bit mono PCM WAV into [-1, 1] floats. */ - private static float[] readWavMono16(String path) throws IOException { - File file = new File(path); - long fileLength = file.length(); - try (DataInputStream in = new DataInputStream(new FileInputStream(file))) { - byte[] header = new byte[44]; - in.readFully(header); - if (header[0] != 'R' || header[1] != 'I' || header[8] != 'W') { - throw new IOException("not a WAV file"); - } - int channels = (header[22] & 0xff) | (header[23] << 8); - int bits = (header[34] & 0xff) | (header[35] << 8); - if (channels != 1 || bits != 16) { - throw new IOException("only 16-bit mono WAV is supported"); - } - long dataBytes = fileLength - 44; - int count = (int) Math.min(dataBytes / 2, Integer.MAX_VALUE); - float[] samples = new float[count]; - byte[] chunk = new byte[65536]; - int sampleIndex = 0; - int carried = 0; - byte carriedByte = 0; - int read; - while (sampleIndex < count && (read = in.read(chunk)) > 0) { - int offset = 0; - if (carried == 1) { - samples[sampleIndex++] = - ((short) ((carriedByte & 0xff) | (chunk[0] << 8))) / 32768.0f; - offset = 1; - carried = 0; - } - int pairs = (read - offset) / 2; - for (int i = 0; i < pairs && sampleIndex < count; i++) { - int base = offset + i * 2; - samples[sampleIndex++] = - ((short) ((chunk[base] & 0xff) | (chunk[base + 1] << 8))) / 32768.0f; - } - if (((read - offset) & 1) == 1) { - carried = 1; - carriedByte = chunk[read - 1]; - } - } - return samples; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.kt new file mode 100644 index 00000000..86c911d6 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDiarizer.kt @@ -0,0 +1,277 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.util.Log + +import com.k2fsa.sherpa.onnx.FastClusteringConfig +import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarization +import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarizationCallback +import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarizationConfig +import com.k2fsa.sherpa.onnx.OfflineSpeakerDiarizationSegment +import com.k2fsa.sherpa.onnx.OfflineSpeakerSegmentationModelConfig +import com.k2fsa.sherpa.onnx.OfflineSpeakerSegmentationPyannoteModelConfig +import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractorConfig + +import org.json.JSONArray +import org.json.JSONObject + +import java.io.DataInputStream +import java.io.File +import java.io.FileInputStream +import java.io.IOException +import java.nio.ByteBuffer + +/** + * Offline re-diarization of a stored conversation recording (16-bit mono + * WAV): pyannote segmentation plus the shared speaker-embedding model with + * clustering, via sherpa-onnx. Runs on its own thread; results are handed to + * TS as JSON turns for reconciliation against the transcript segments. + */ +class FaceclawDiarizer( + private val segmentationModelPath: String?, + private val embeddingModelPath: String?, + private val wavPath: String?, + private val numClusters: Int, + threshold: Float, + private val listener: Listener +) { + companion object { + private const val TAG = "FaceclawDiarizer" + + /** + * Decode a compressed recording (M4A/AAC from the transcode sweep) into + * [-1, 1] floats via MediaExtractor + MediaCodec. Recordings are written + * as 16 kHz mono, so no resampling is needed. + */ + @JvmStatic + @Throws(IOException::class) + private fun decodeCompressedMono(path: String?): FloatArray { + val extractor = android.media.MediaExtractor() + var codec: android.media.MediaCodec? = null + val pcm = java.io.ByteArrayOutputStream() + try { + extractor.setDataSource(path!!) + var trackIndex = -1 + var format: android.media.MediaFormat? = null + for (i in 0 until extractor.trackCount) { + val candidate = extractor.getTrackFormat(i) + val mime = candidate.getString(android.media.MediaFormat.KEY_MIME) + if (mime != null && mime.startsWith("audio/")) { + trackIndex = i + format = candidate + break + } + } + if (trackIndex < 0 || format == null) { + throw IOException("no audio track in $path") + } + extractor.selectTrack(trackIndex) + codec = android.media.MediaCodec.createDecoderByType( + format.getString(android.media.MediaFormat.KEY_MIME)!!) + codec.configure(format, null, null, 0) + codec.start() + val info = android.media.MediaCodec.BufferInfo() + var inputDone = false + var outputDone = false + while (!outputDone) { + if (!inputDone) { + val inIndex = codec.dequeueInputBuffer(10_000) + if (inIndex >= 0) { + val inBuf = codec.getInputBuffer(inIndex)!! + val size = extractor.readSampleData(inBuf, 0) + if (size < 0) { + codec.queueInputBuffer(inIndex, 0, 0, 0, + android.media.MediaCodec.BUFFER_FLAG_END_OF_STREAM) + inputDone = true + } else { + codec.queueInputBuffer(inIndex, 0, size, extractor.sampleTime, 0) + extractor.advance() + } + } + } + val outIndex = codec.dequeueOutputBuffer(info, 10_000) + if (outIndex >= 0) { + val outBuf = codec.getOutputBuffer(outIndex)!! + val chunk = ByteArray(info.size) + outBuf.position(info.offset) + outBuf.get(chunk) + pcm.write(chunk) + codec.releaseOutputBuffer(outIndex, false) + if ((info.flags and android.media.MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { + outputDone = true + } + } + } + } finally { + extractor.release() + if (codec != null) { + try { + codec.stop() + } catch (ignored: Throwable) { + } + codec.release() + } + } + val bytes = pcm.toByteArray() + val count = bytes.size / 2 + val samples = FloatArray(count) + for (i in 0 until count) { + samples[i] = ((bytes[i * 2].toInt() and 0xff) or (bytes[i * 2 + 1].toInt() shl 8)).toShort() / 32768.0f + } + return samples + } + + /** Load a 16-bit mono PCM WAV into [-1, 1] floats. */ + @JvmStatic + @Throws(IOException::class) + private fun readWavMono16(path: String?): FloatArray { + val file = File(path) + val fileLength = file.length() + DataInputStream(FileInputStream(file)).use { `in` -> + // Shared RIFF walk (handles extra chunks and unpatched data sizes); the + // stream is then positioned at the first PCM sample. + val prefix = ByteArray(Math.min(fileLength, WavPcmReader.HEADER_PREFIX_BYTES.toLong()).toInt()) + `in`.readFully(prefix) + val info = try { + WavPcmReader.parseHeader(prefix, fileLength) + } catch (e: IllegalArgumentException) { + throw IOException(e.message ?: "not a WAV file") + } + if (info.channels != 1) { + throw IOException("only 16-bit mono WAV is supported") + } + val dataBytes = info.dataBytes + val count = Math.min(dataBytes / 2, Int.MAX_VALUE.toLong()).toInt() + val samples = FloatArray(count) + val chunk = ByteArray(65536) + var sampleIndex = 0 + var carried = 0 + var carriedByte: Byte = 0 + var read = 0 + // The prefix already holds the start of the PCM data; consume it first. + val prefixData = prefix.size - info.dataOffset.toInt() + if (prefixData > 0) { + val pairs = prefixData / 2 + var i = 0 + while (i < pairs && sampleIndex < count) { + val base = info.dataOffset.toInt() + i * 2 + samples[sampleIndex++] = AudioSegmentation.pcm16le(prefix[base], prefix[base + 1]) / 32768.0f + i++ + } + if ((prefixData and 1) == 1) { + carried = 1 + carriedByte = prefix[prefix.size - 1] + } + } + while (sampleIndex < count && `in`.read(chunk).also { read = it } > 0) { + var offset = 0 + if (carried == 1) { + samples[sampleIndex++] = + ((carriedByte.toInt() and 0xff) or (chunk[0].toInt() shl 8)).toShort() / 32768.0f + offset = 1 + carried = 0 + } + val pairs = (read - offset) / 2 + var i = 0 + while (i < pairs && sampleIndex < count) { + val base = offset + i * 2 + samples[sampleIndex++] = + ((chunk[base].toInt() and 0xff) or (chunk[base + 1].toInt() shl 8)).toShort() / 32768.0f + i++ + } + if (((read - offset) and 1) == 1) { + carried = 1 + carriedByte = chunk[read - 1] + } + } + return samples + } + } + } + + interface Listener { + /** progress in [0, 1]. */ + fun onProgress(progress: Double) + + /** turns: [{startMs, endMs, cluster}], sorted by start time. */ + fun onDone(turnsJson: String?) + + fun onError(message: String?) + } + + private val threshold: Float = if (threshold <= 0) 0.5f else threshold + private val mainHandler = Handler(Looper.getMainLooper()) + @Volatile + private var cancelled = false + + fun start() { + val thread = Thread(Runnable { run() }, "FaceclawDiarizer") + thread.priority = Thread.MIN_PRIORITY + thread.start() + } + + fun cancel() { + cancelled = true + } + + private fun run() { + var diarization: OfflineSpeakerDiarization? = null + try { + val samples = if (wavPath!!.endsWith(".wav")) + readWavMono16(wavPath) + else + decodeCompressedMono(wavPath) + val config = OfflineSpeakerDiarizationConfig.builder() + .setSegmentation(OfflineSpeakerSegmentationModelConfig.builder() + .setPyannote(OfflineSpeakerSegmentationPyannoteModelConfig.builder() + .setModel(segmentationModelPath) + .build()) + .setNumThreads(1) + .build()) + .setEmbedding(SpeakerEmbeddingExtractorConfig.builder() + .setModel(embeddingModelPath) + .setNumThreads(1) + .build()) + .setClustering(FastClusteringConfig.builder() + .setNumClusters(if (numClusters > 0) numClusters else -1) + .setThreshold(threshold) + .build()) + .setMinDurationOn(0.3f) + .setMinDurationOff(0.5f) + .build() + diarization = OfflineSpeakerDiarization(config) + val segments: Array? = diarization.processWithCallback( + samples, + OfflineSpeakerDiarizationCallback { numProcessedChunks, numTotalChunks, _ -> + emitProgress(if (numTotalChunks <= 0) 0.0 + else numProcessedChunks.toDouble() / numTotalChunks) + if (cancelled) 1 else 0 + }) + val turns = JSONArray() + if (segments != null) { + for (segment in segments) { + val turn = JSONObject() + turn.put("startMs", Math.round(segment.start * 1000)) + turn.put("endMs", Math.round(segment.end * 1000)) + turn.put("cluster", segment.speaker) + turns.put(turn) + } + } + val result = turns.toString() + mainHandler.post { listener.onDone(result) } + } catch (t: Throwable) { + Log.w(TAG, "diarization failed for $wavPath", t) + val message = if (t.message == null) t.toString() else t.message + mainHandler.post { listener.onError(message) } + } finally { + if (diarization != null) { + diarization.release() + } + } + } + + private fun emitProgress(progress: Double) { + mainHandler.post { listener.onProgress(Math.max(0.0, Math.min(1.0, progress))) } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.java deleted file mode 100644 index bb3e952d..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.faceclaw.app; - -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.net.Uri; -import android.provider.Settings; -import android.util.Log; - -public final class FaceclawEvenAppDetector { - private static final String TAG = "FaceclawEvenApp"; - - public static final String EVEN_PACKAGE_NAME = "com.even.sg"; - public static final String EVEN_NOTIFICATION_TITLE = "Even Notification Service"; - - private FaceclawEvenAppDetector() { - } - - public static boolean isNotificationAccessEnabled(Context context) { - if (context == null) { - return false; - } - Context appContext = context.getApplicationContext(); - ComponentName listenerComponent = new ComponentName(appContext, FaceclawMediaNotificationListenerService.class); - String enabledListeners = Settings.Secure.getString( - appContext.getContentResolver(), - "enabled_notification_listeners" - ); - if (enabledListeners == null || enabledListeners.isEmpty()) { - return false; - } - String fullName = listenerComponent.flattenToString(); - String shortName = listenerComponent.flattenToShortString(); - return enabledListeners.contains(fullName) - || enabledListeners.contains(shortName) - || enabledListeners.contains(appContext.getPackageName()); - } - - public static boolean isEvenNotificationActive(Context context) { - boolean hasAccess = isNotificationAccessEnabled(context); - if (!hasAccess) { - Log.i(TAG, "Can't check for Even Realities app notification, permission is not enabled"); - return false; - } - boolean hasNotfication = FaceclawMediaNotificationListenerService.hasActiveNotificationTitle(EVEN_NOTIFICATION_TITLE); - Log.i(TAG, "isEvenNotificationActive: " + hasNotfication); - return hasNotfication; - } - - /** Returns "opened", "not-installed", or "failed" for the phone UI. */ - public static String openEvenAppSettings(Context context) { - if (context == null) { - return "failed"; - } - Context appContext = context.getApplicationContext(); - try { - // Settings may launch successfully and then immediately close for a - // missing package, so check before starting the activity. - appContext.getPackageManager().getApplicationInfo(EVEN_PACKAGE_NAME, 0); - Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); - intent.setData(Uri.parse("package:" + EVEN_PACKAGE_NAME)); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - appContext.startActivity(intent); - return "opened"; - } catch (PackageManager.NameNotFoundException e) { - return "not-installed"; - } catch (Exception t) { - Log.w(TAG, "failed to open Even app settings", t); - return "failed"; - } - } - - public static void openNotificationAccessSettings(Context context) { - if (context == null) { - return; - } - Context appContext = context.getApplicationContext(); - Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - try { - appContext.startActivity(intent); - } catch (Throwable t) { - Log.w(TAG, "failed to open notification access settings", t); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.kt new file mode 100644 index 00000000..4a0b64be --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenAppDetector.kt @@ -0,0 +1,90 @@ +package com.faceclaw.app + +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.provider.Settings +import android.util.Log + +class FaceclawEvenAppDetector private constructor() { + companion object { + private const val TAG = "FaceclawEvenApp" + + const val EVEN_PACKAGE_NAME = "com.even.sg" + const val EVEN_NOTIFICATION_TITLE = "Even Notification Service" + + @JvmStatic + fun isNotificationAccessEnabled(context: Context?): Boolean { + if (context == null) { + return false + } + val appContext = context.applicationContext + val listenerComponent = ComponentName(appContext, FaceclawMediaNotificationListenerService::class.java) + val enabledListeners = Settings.Secure.getString( + appContext.contentResolver, + "enabled_notification_listeners" + ) + if (enabledListeners == null || enabledListeners.isEmpty()) { + return false + } + val fullName = listenerComponent.flattenToString() + val shortName = listenerComponent.flattenToShortString() + return enabledListeners.contains(fullName) + || enabledListeners.contains(shortName) + || enabledListeners.contains(appContext.packageName) + } + + @JvmStatic + fun isEvenNotificationActive(context: Context?): Boolean { + val hasAccess = isNotificationAccessEnabled(context) + if (!hasAccess) { + Log.i(TAG, "Can't check for Even Realities app notification, permission is not enabled") + return false + } + val hasNotfication = FaceclawMediaNotificationListenerService.hasActiveNotificationTitle(EVEN_NOTIFICATION_TITLE) + Log.i(TAG, "isEvenNotificationActive: $hasNotfication") + return hasNotfication + } + + /** Returns "opened", "not-installed", or "failed" for the phone UI. */ + @JvmStatic + fun openEvenAppSettings(context: Context?): String { + if (context == null) { + return "failed" + } + val appContext = context.applicationContext + try { + // Settings may launch successfully and then immediately close for a + // missing package, so check before starting the activity. + appContext.packageManager.getApplicationInfo(EVEN_PACKAGE_NAME, 0) + val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) + intent.data = Uri.parse("package:$EVEN_PACKAGE_NAME") + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + appContext.startActivity(intent) + return "opened" + } catch (e: PackageManager.NameNotFoundException) { + return "not-installed" + } catch (t: Exception) { + Log.w(TAG, "failed to open Even app settings", t) + return "failed" + } + } + + @JvmStatic + fun openNotificationAccessSettings(context: Context?) { + if (context == null) { + return + } + val appContext = context.applicationContext + val intent = Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + try { + appContext.startActivity(intent) + } catch (t: Throwable) { + Log.w(TAG, "failed to open notification access settings", t) + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.java deleted file mode 100644 index 2f9c0e90..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; -import android.webkit.WebView; - -import androidx.webkit.WebViewCompat; -import androidx.webkit.WebViewFeature; - -import java.util.Collections; -import java.util.Set; - -/** - * Injects the EvenHub bridge shim at document start into a WebView that loads - * a page over the real network (the developer "Load app from URL" flow). - * - * Packaged apps get the shim spliced into the HTML they are served - * (FaceclawEvenHubWebViewClient), which is not an option for a page fetched - * from a live server. WebViewCompat.addDocumentStartJavaScript is the - * supported way to run script before any page script, and unlike an - * onPageStarted evaluateJavascript it is guaranteed to win the race — apps - * poll for window.flutter_inappwebview with a few seconds' patience, but the - * timer shim must be in place before the app captures setTimeout. - * - * Requires WebView 83+; the caller falls back to onPageStarted injection when - * this returns false. - */ -public final class FaceclawEvenHubDocumentStart { - private static final String TAG = "FaceclawEvenHub"; - - private FaceclawEvenHubDocumentStart() {} - - /** - * @param originRule an allowed-origin rule, e.g. "https://example.com" or "*". - * @return whether the script was registered. - */ - public static boolean install(WebView webView, String script, String originRule) { - if (!WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) { - Log.w(TAG, "document-start scripts unsupported; falling back to onPageStarted"); - return false; - } - try { - Set rules = Collections.singleton(originRule); - WebViewCompat.addDocumentStartJavaScript(webView, script, rules); - return true; - } catch (Throwable error) { - Log.w(TAG, "addDocumentStartJavaScript failed for " + originRule, error); - return false; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.kt new file mode 100644 index 00000000..3f50eaec --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubDocumentStart.kt @@ -0,0 +1,50 @@ +package com.faceclaw.app + +import android.util.Log +import android.webkit.WebView + +import androidx.webkit.WebViewCompat +import androidx.webkit.WebViewFeature + +import java.util.Collections + +/** + * Injects the EvenHub bridge shim at document start into a WebView that loads + * a page over the real network (the developer "Load app from URL" flow). + * + * Packaged apps get the shim spliced into the HTML they are served + * (FaceclawEvenHubWebViewClient), which is not an option for a page fetched + * from a live server. WebViewCompat.addDocumentStartJavaScript is the + * supported way to run script before any page script, and unlike an + * onPageStarted evaluateJavascript it is guaranteed to win the race — apps + * poll for window.flutter_inappwebview with a few seconds' patience, but the + * timer shim must be in place before the app captures setTimeout. + * + * Requires WebView 83+; the caller falls back to onPageStarted injection when + * this returns false. + */ +class FaceclawEvenHubDocumentStart private constructor() { + companion object { + private const val TAG = "FaceclawEvenHub" + + /** + * @param originRule an allowed-origin rule, e.g. "https://example.com" or "*". + * @return whether the script was registered. + */ + @JvmStatic + fun install(webView: WebView, script: String, originRule: String): Boolean { + if (!WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) { + Log.w(TAG, "document-start scripts unsupported; falling back to onPageStarted") + return false + } + try { + val rules: Set = Collections.singleton(originRule) + WebViewCompat.addDocumentStartJavaScript(webView, script, rules) + return true + } catch (error: Throwable) { + Log.w(TAG, "addDocumentStartJavaScript failed for $originRule", error) + return false + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.java deleted file mode 100644 index 6e40bbef..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.webkit.JavascriptInterface; - -/** - * The object injected into a hosted EvenHub app's WebView as - * window.__faceclawEvenHub. The document-start shim (served inline by - * FaceclawEvenHubWebViewClient) wraps it in a promise-returning - * window.flutter_inappwebview.callHandler, which is what the EvenHub SDK - * actually talks to. - * - * JavascriptInterface methods run on a WebView-internal thread; everything - * is bounced to the main thread before touching the NativeScript listener. - */ -public class FaceclawEvenHubJsBridge { - private final FaceclawEvenHubListener listener; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - public FaceclawEvenHubJsBridge(FaceclawEvenHubListener listener) { - this.listener = listener; - } - - @JavascriptInterface - public void postMessage(final String handlerName, final String argsJson, final int callId) { - mainHandler.post(new Runnable() { - @Override - public void run() { - listener.onEvenAppMessage(handlerName, argsJson, callId); - } - }); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.kt new file mode 100644 index 00000000..90c8c1e7 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubJsBridge.kt @@ -0,0 +1,26 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.webkit.JavascriptInterface + +/** + * The object injected into a hosted EvenHub app's WebView as + * window.__faceclawEvenHub. The document-start shim (served inline by + * FaceclawEvenHubWebViewClient) wraps it in a promise-returning + * window.flutter_inappwebview.callHandler, which is what the EvenHub SDK + * actually talks to. + * + * JavascriptInterface methods run on a WebView-internal thread; everything + * is bounced to the main thread before touching the NativeScript listener. + */ +class FaceclawEvenHubJsBridge(private val listener: FaceclawEvenHubListener) { + private val mainHandler = Handler(Looper.getMainLooper()) + + @JavascriptInterface + fun postMessage(handlerName: String?, argsJson: String?, callId: Int) { + mainHandler.post { + listener.onEvenAppMessage(handlerName, argsJson, callId) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubListener.java deleted file mode 100644 index 56ff2d61..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubListener.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.faceclaw.app; - -/** - * Callbacks from a hosted EvenHub app's WebView to the TypeScript host. - * All methods are delivered on the Android main thread (NativeScript's JS - * thread), regardless of which WebView-internal thread produced them. - */ -public interface FaceclawEvenHubListener { - /** - * The app invoked window.flutter_inappwebview.callHandler(handlerName, ...args). - * argsJson is the JSON-encoded argument array; callId identifies the - * promise to resolve via window.__fcResolve(callId, ok, value). - */ - void onEvenAppMessage(String handlerName, String argsJson, int callId); - - /** The WebView finished loading the app's page. */ - void onPageFinished(String url); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.kt similarity index 78% rename from App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java rename to App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.kt index 5a590902..4f5024a6 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebView.kt @@ -1,8 +1,8 @@ -package com.faceclaw.app; +package com.faceclaw.app -import android.content.Context; -import android.view.View; -import android.webkit.WebView; +import android.content.Context +import android.view.View +import android.webkit.WebView /** * A WebView for hosting an EvenHub app that keeps rendering and running JS even @@ -25,16 +25,11 @@ import android.webkit.WebView; * service regardless of the phone's screen, so "always rendering" is what we * actually want; there is no on-phone cost while the app is unseen. */ -public class FaceclawEvenHubWebView extends WebView { +class FaceclawEvenHubWebView(context: Context) : WebView(context) { - public FaceclawEvenHubWebView(Context context) { - super(context); - } - - @Override - protected void onWindowVisibilityChanged(int visibility) { + override fun onWindowVisibilityChanged(visibility: Int) { // Never propagate a hidden window: keep Chromium's page-visibility // "visible" so the renderer isn't frozen when Faceclaw backgrounds. - super.onWindowVisibilityChanged(View.VISIBLE); + super.onWindowVisibilityChanged(View.VISIBLE) } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java deleted file mode 100644 index f8c21348..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.webkit.WebResourceRequest; -import android.webkit.WebResourceResponse; -import android.webkit.WebView; -import android.webkit.WebViewClient; -import android.net.Uri; -import android.util.Log; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.Locale; - -/** - * Serves an unpacked EvenHub app's files to its WebView from a fake per-app - * https origin, entirely offline. HTML responses get the host bridge shim - * injected at the top of the document, guaranteeing - * window.flutter_inappwebview exists before any app JS runs (apps race - * waitForEvenAppBridge against 4-6s timeouts and silently fall back to demo - * modes if the handler appears late). - * - * shouldInterceptRequest runs on a WebView-internal thread; it only touches - * files, never the NativeScript runtime. Requests to other hosts fall - * through to the network (the manifest whitelist is not yet enforced). - * - * A developer-loaded app ("Load app from URL") has no local root: host is - * empty, nothing is intercepted, and the page loads normally from its server. - * The shim is registered as a document-start script instead - * (FaceclawEvenHubDocumentStart); injectOnPageStarted is the fallback for - * WebView versions that lack that API. - */ -public class FaceclawEvenHubWebViewClient extends WebViewClient { - private static final String TAG = "FaceclawEvenHub"; - - private final String rootDir; - private final String host; - private final String injectScript; - private final boolean injectOnPageStarted; - private final FaceclawEvenHubListener listener; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - public FaceclawEvenHubWebViewClient( - String rootDir, - String host, - String injectScript, - boolean injectOnPageStarted, - FaceclawEvenHubListener listener) { - this.rootDir = rootDir; - this.host = host; - this.injectScript = injectScript; - this.injectOnPageStarted = injectOnPageStarted; - this.listener = listener; - } - - @Override - public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { - if (host == null || host.isEmpty()) { - return null; // Remote app: everything comes from the network. - } - Uri url = request.getUrl(); - if (url == null || !host.equals(url.getHost())) { - return null; // External hosts: normal network handling. - } - String path = url.getPath(); - if (path == null || path.isEmpty() || path.equals("/")) { - path = "/index.html"; - } - try { - File root = new File(rootDir).getCanonicalFile(); - File target = new File(root, path.substring(1)).getCanonicalFile(); - if (!target.getPath().startsWith(root.getPath()) || !target.isFile()) { - Log.w(TAG, "404 " + path); - return new WebResourceResponse( - "text/plain", "utf-8", - new ByteArrayInputStream("not found".getBytes(StandardCharsets.UTF_8))); - } - String mime = mimeTypeFor(target.getName()); - if (mime.equals("text/html")) { - return new WebResourceResponse(mime, "utf-8", new ByteArrayInputStream(injectIntoHtml(target))); - } - InputStream stream = new FileInputStream(target); - return new WebResourceResponse(mime, null, stream); - } catch (IOException e) { - Log.e(TAG, "serve failed for " + path, e); - return new WebResourceResponse( - "text/plain", "utf-8", - new ByteArrayInputStream("error".getBytes(StandardCharsets.UTF_8))); - } - } - - @Override - public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) { - super.onPageStarted(view, url, favicon); - if (!injectOnPageStarted) return; - // Best-effort: the document has committed but the parser has not run - // the page's own scripts yet, so this usually lands first. Only used - // when document-start scripts are unavailable. - view.evaluateJavascript(injectScript, null); - } - - @Override - public void onPageFinished(WebView view, final String url) { - mainHandler.post(new Runnable() { - @Override - public void run() { - listener.onPageFinished(url); - } - }); - } - - private byte[] injectIntoHtml(File file) throws IOException { - byte[] raw = readAll(file); - String html = new String(raw, StandardCharsets.UTF_8); - String tag = ""; - // After if present, else before everything. - int headIndex = indexOfIgnoreCase(html, "= 0) { - int close = html.indexOf('>', headIndex); - if (close >= 0) { - return (html.substring(0, close + 1) + tag + html.substring(close + 1)) - .getBytes(StandardCharsets.UTF_8); - } - } - return (tag + html).getBytes(StandardCharsets.UTF_8); - } - - private static int indexOfIgnoreCase(String haystack, String needle) { - return haystack.toLowerCase(Locale.ROOT).indexOf(needle); - } - - private static byte[] readAll(File file) throws IOException { - FileInputStream in = new FileInputStream(file); - try { - byte[] data = new byte[(int) file.length()]; - int off = 0; - while (off < data.length) { - int n = in.read(data, off, data.length - off); - if (n < 0) throw new IOException("short read: " + file); - off += n; - } - return data; - } finally { - in.close(); - } - } - - private static String mimeTypeFor(String name) { - String lower = name.toLowerCase(Locale.ROOT); - if (lower.endsWith(".html") || lower.endsWith(".htm")) return "text/html"; - if (lower.endsWith(".js") || lower.endsWith(".mjs")) return "application/javascript"; - if (lower.endsWith(".css")) return "text/css"; - if (lower.endsWith(".json")) return "application/json"; - if (lower.endsWith(".png")) return "image/png"; - if (lower.endsWith(".jpg") || lower.endsWith(".jpeg")) return "image/jpeg"; - if (lower.endsWith(".gif")) return "image/gif"; - if (lower.endsWith(".svg")) return "image/svg+xml"; - if (lower.endsWith(".webp")) return "image/webp"; - if (lower.endsWith(".ico")) return "image/x-icon"; - if (lower.endsWith(".wasm")) return "application/wasm"; - if (lower.endsWith(".woff")) return "font/woff"; - if (lower.endsWith(".woff2")) return "font/woff2"; - if (lower.endsWith(".ttf")) return "font/ttf"; - if (lower.endsWith(".txt") || lower.endsWith(".map")) return "text/plain"; - return "application/octet-stream"; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.kt new file mode 100644 index 00000000..af821b0b --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewClient.kt @@ -0,0 +1,116 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.webkit.WebResourceRequest +import android.webkit.WebResourceResponse +import android.webkit.WebView +import android.webkit.WebViewClient +import android.net.Uri +import android.util.Log + +import java.io.ByteArrayInputStream +import java.io.File +import java.io.FileInputStream +import java.io.IOException +import java.io.InputStream +import java.nio.charset.StandardCharsets + +/** + * Serves an unpacked EvenHub app's files to its WebView from a fake per-app + * https origin, entirely offline. HTML responses get the host bridge shim + * injected at the top of the document, guaranteeing + * window.flutter_inappwebview exists before any app JS runs (apps race + * waitForEvenAppBridge against 4-6s timeouts and silently fall back to demo + * modes if the handler appears late). + * + * shouldInterceptRequest runs on a WebView-internal thread; it only touches + * files, never the NativeScript runtime. Requests to other hosts fall + * through to the network (the manifest whitelist is not yet enforced). + * + * A developer-loaded app ("Load app from URL") has no local root: host is + * empty, nothing is intercepted, and the page loads normally from its server. + * The shim is registered as a document-start script instead + * (FaceclawEvenHubDocumentStart); injectOnPageStarted is the fallback for + * WebView versions that lack that API. + */ +class FaceclawEvenHubWebViewClient( + private val rootDir: String?, + private val host: String?, + private val injectScript: String, + private val injectOnPageStarted: Boolean, + private val listener: FaceclawEvenHubListener +) : WebViewClient() { + private val mainHandler = Handler(Looper.getMainLooper()) + + override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? { + if (host == null || host.isEmpty()) { + return null // Remote app: everything comes from the network. + } + val url: Uri? = request.url + if (url == null || host != url.host) { + return null // External hosts: normal network handling. + } + val path = url.path + try { + // Path rules, MIME table and shim injection are shared (EvenHubAssetServer); + // the canonical-file check below additionally defeats symlinks. + val asset = EvenHubAssetServer.resolve(path) + val root = File(rootDir).canonicalFile + val target = asset?.let { File(root, it.relativePath).canonicalFile } + if (asset == null || target == null || !EvenHubAssetServer.isInsideRoot(root.path, target.path) || !target.isFile) { + Log.w(TAG, "404 $path") + return WebResourceResponse( + "text/plain", "utf-8", + ByteArrayInputStream("not found".toByteArray(StandardCharsets.UTF_8))) + } + if (asset.isHtml) { + val html = EvenHubAssetServer.injectIntoHtml(String(readAll(target), StandardCharsets.UTF_8), injectScript) + return WebResourceResponse(asset.mime, "utf-8", ByteArrayInputStream(html.toByteArray(StandardCharsets.UTF_8))) + } + val stream: InputStream = FileInputStream(target) + return WebResourceResponse(asset.mime, null, stream) + } catch (e: IOException) { + Log.e(TAG, "serve failed for $path", e) + return WebResourceResponse( + "text/plain", "utf-8", + ByteArrayInputStream("error".toByteArray(StandardCharsets.UTF_8))) + } + } + + override fun onPageStarted(view: WebView, url: String?, favicon: android.graphics.Bitmap?) { + super.onPageStarted(view, url, favicon) + if (!injectOnPageStarted) return + // Best-effort: the document has committed but the parser has not run + // the page's own scripts yet, so this usually lands first. Only used + // when document-start scripts are unavailable. + view.evaluateJavascript(injectScript, null) + } + + override fun onPageFinished(view: WebView, url: String?) { + mainHandler.post { + listener.onPageFinished(url) + } + } + + companion object { + private const val TAG = "FaceclawEvenHub" + + @Throws(IOException::class) + private fun readAll(file: File): ByteArray { + val input = FileInputStream(file) + try { + val data = ByteArray(file.length().toInt()) + var off = 0 + while (off < data.size) { + val n = input.read(data, off, data.size - off) + if (n < 0) throw IOException("short read: $file") + off += n + } + return data + } finally { + input.close() + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.kt similarity index 56% rename from App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java rename to App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.kt index d50d00ae..7e563d0e 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawEvenHubWebViewHost.kt @@ -1,16 +1,15 @@ -package com.faceclaw.app; +package com.faceclaw.app -import android.app.Activity; -import android.os.Build; -import android.os.Handler; -import android.os.Looper; -import android.view.View; -import android.view.ViewGroup; -import android.webkit.WebView; -import android.widget.FrameLayout; +import android.app.Activity +import android.os.Build +import android.os.Handler +import android.os.Looper +import android.view.View +import android.view.ViewGroup +import android.webkit.WebView +import android.widget.FrameLayout -import java.util.ArrayList; -import java.util.List; +import java.util.ArrayList /** * Keeps EvenHub app WebViews alive and rendering while the phone shows the @@ -49,109 +48,113 @@ import java.util.List; * activity is foreground. When Faceclaw itself is backgrounded, the window goes * invisible and Chromium freezes the renderer — the timer/rAF ticks and pushed * input events then queue up and only run once foregrounded again. Two things - * prevent that: the WebViews are {@link FaceclawEvenHubWebView}, which lies to + * prevent that: the WebViews are [FaceclawEvenHubWebView], which lies to * Chromium about window visibility so the page never goes hidden; and each is * pinned to IMPORTANT renderer priority (not waived when not visible) with * timers/onResume asserted on attach. The main Looper keeps ticking under the * foreground service, so apps keep driving their glasses windows in the * background. */ -public class FaceclawEvenHubWebViewHost { - private static FaceclawEvenHubWebViewHost instance; +class FaceclawEvenHubWebViewHost { + private var overlay: FrameLayout? = null + private var shown = false - /** How often the host fires the JS timer queue. 60Hz keeps games smooth. */ - private static final long TICK_MS = 16; - - private FrameLayout overlay; - private boolean shown = false; - - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final List webViews = new ArrayList<>(); - private boolean ticking = false; - private final Runnable ticker = new Runnable() { - @Override - public void run() { - for (int i = 0; i < webViews.size(); i++) { - webViews.get(i).evaluateJavascript( - "window.__fcTimerTick&&__fcTimerTick();window.__fcRafTick&&__fcRafTick()", null); + private val mainHandler = Handler(Looper.getMainLooper()) + private val webViews: MutableList = ArrayList() + private var ticking = false + private val ticker: Runnable = object : Runnable { + override fun run() { + for (i in 0 until webViews.size) { + webViews[i].evaluateJavascript( + "window.__fcTimerTick&&__fcTimerTick();window.__fcRafTick&&__fcRafTick()", null) } - if (ticking) mainHandler.postDelayed(this, TICK_MS); + if (ticking) mainHandler.postDelayed(this, TICK_MS) } - }; - - public static synchronized FaceclawEvenHubWebViewHost getInstance() { - if (instance == null) instance = new FaceclawEvenHubWebViewHost(); - return instance; } - private FrameLayout ensureOverlay(Activity activity) { - if (overlay != null) return overlay; - ViewGroup content = activity.findViewById(android.R.id.content); - overlay = new FrameLayout(activity); + companion object { + private var instance: FaceclawEvenHubWebViewHost? = null + + /** How often the host fires the JS timer queue. 60Hz keeps games smooth. */ + private const val TICK_MS = 16L + + @JvmStatic + @Synchronized + fun getInstance(): FaceclawEvenHubWebViewHost { + if (instance == null) instance = FaceclawEvenHubWebViewHost() + return instance!! + } + } + + private fun ensureOverlay(activity: Activity): FrameLayout { + overlay?.let { return it } + val content = activity.findViewById(android.R.id.content) + val created = FrameLayout(activity) + overlay = created // index 0 = behind the NativeScript content view. - content.addView(overlay, 0, new FrameLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); - return overlay; + content.addView(created, 0, FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + return created } /** Add a WebView to the host, full-size and rendering, hidden behind the UI. */ - public void attach(Activity activity, WebView web) { - FrameLayout o = ensureOverlay(activity); - web.setVisibility(View.VISIBLE); - if (web.getParent() == null) { - o.addView(web, new FrameLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + fun attach(activity: Activity, web: WebView) { + val o = ensureOverlay(activity) + web.visibility = View.VISIBLE + if (web.parent == null) { + o.addView(web, FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) } // resumeTimers() is process-global; make sure nothing left timers paused. // onResume() undoes any per-WebView pause. Together with // FaceclawEvenHubWebView faking window visibility, this keeps the page // running JS while Faceclaw is backgrounded. - web.resumeTimers(); - web.onResume(); + web.resumeTimers() + web.onResume() // Keep the renderer process at IMPORTANT priority even when the WebView // isn't visible (waivedWhenNotVisible=false), so Android doesn't // deprioritize/kill it in the background. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - web.setRendererPriorityPolicy(WebView.RENDERER_PRIORITY_IMPORTANT, false); + web.setRendererPriorityPolicy(WebView.RENDERER_PRIORITY_IMPORTANT, false) } - if (!webViews.contains(web)) webViews.add(web); + if (!webViews.contains(web)) webViews.add(web) if (!ticking) { - ticking = true; - mainHandler.postDelayed(ticker, TICK_MS); + ticking = true + mainHandler.postDelayed(ticker, TICK_MS) } } /** Bring an app's WebView to the front so it is visible on the phone. */ - public void showOnPhone(WebView web) { - if (overlay == null) return; - web.bringToFront(); - overlay.bringToFront(); - shown = true; + fun showOnPhone(web: WebView) { + val o = overlay ?: return + web.bringToFront() + o.bringToFront() + shown = true } /** Send the overlay back behind the NativeScript UI. */ - public void hideOnPhone() { - shown = false; - if (overlay == null) return; - ViewGroup content = (ViewGroup) overlay.getParent(); + fun hideOnPhone() { + shown = false + val o = overlay ?: return + val content = o.parent as ViewGroup? if (content != null) { - content.removeView(overlay); - content.addView(overlay, 0); + content.removeView(o) + content.addView(o, 0) } } - public boolean isShown() { - return shown; + fun isShown(): Boolean { + return shown } /** Remove and destroy a WebView (its app is closing). */ - public void detach(WebView web) { - webViews.remove(web); + fun detach(web: WebView) { + webViews.remove(web) if (webViews.isEmpty() && ticking) { - ticking = false; - mainHandler.removeCallbacks(ticker); + ticking = false + mainHandler.removeCallbacks(ticker) } - if (overlay != null) overlay.removeView(web); - web.destroy(); + overlay?.removeView(web) + web.destroy() } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java deleted file mode 100644 index 84d66a4f..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java +++ /dev/null @@ -1,695 +0,0 @@ -package com.faceclaw.app; - -import android.bluetooth.BluetoothGatt; -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; -import android.util.Log; - -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -/** - * Ports g2flash.py's OTA flash procedure to Android. Streams the EVENOTA - * firmware container to each lens over the firmware-data service using the - * aa21 envelope (BleProtocol.framePb) and the c0/c1 control/data protocol. - * - * Each lens connection completes the sid-0x80 security-auth exchange before - * OTA BEGIN — firmware 2.2.9 closes unauthenticated links ~30 s in, killing - * the transfer mid-component. No control-channel traffic is injected during - * the transfer itself: official OTA captures show none between BEGIN and the - * final END, heartbeats demonstrably do not satisfy the 2.2.9 deadline, and a - * concurrent control writer would interleave with the marker/data - * transactions. (See ../notes/ble-connections-2.2.9.md.) - * - * Lenses are flashed one at a time (left, then right). Finishing either lens - * reboots BOTH lenses, so the second lens is briefly unreachable — the reconnect - * for it uses a generous window rather than failing fast. - * - * Separate from FaceclawBleCommunicator on purpose: this speaks the stock OTA - * protocol and must run before the custom firmware exists. It reuses the GATT - * wrapper (FaceclawBleManager) and framing (BleProtocol). - */ -public class FaceclawFirmwareFlasher implements FaceclawBleListener { - private static final String TAG = "FaceclawFlasher"; - - // OTA message types (envelope sid byte) and control opcodes. - private static final int SID_CTRL = 0xc0; - private static final int SID_DATA = 0xc1; - private static final int FLAG_OTA = 0x00; - private static final int OP_BEGIN = 0x00; - private static final int OP_FILE_CHECK = 0x01; - private static final int OP_BLOCK = 0x02; - private static final int OP_END = 0x03; - - private static final int BLOCK_SIZE = 4096; - private static final int BLOCK_ACK_TIMEOUT_MS = 4_000; - private static final int CTRL_ACK_TIMEOUT_MS = 8_000; - private static final int BLOCK_NAK_RETRIES = 3; - private static final int COMPONENT_RETRIES = 3; - - // Reconnect windows. The first lens connects from an idle device; the second - // must ride out the post-first-lens reboot of both lenses. - private static final int FIRST_LENS_CONNECT_WINDOW_MS = 30_000; - private static final int SECOND_LENS_CONNECT_WINDOW_MS = 120_000; - private static final int REBOOT_SETTLE_MS = 5_000; - private static final int NOTIFY_SETTLE_MS = 2_500; - private static final int RETRY_DELAY_MS = 2_500; - - // END ack statuses that mean "component accepted": SUCCESS, UPDATING, SYS_RESTART. - private static final int[] END_OK = new int[] {0, 8, 9}; - - // Firmware layout expectations (mirrors g2flash.py). Firmware 2.2.4 has 5 - // components; 2.2.6 has 6. - private static final int MIN_SEGMENTS = 5; - private static final int MAX_SEGMENTS = 6; - private static final String REQUIRED_SEGMENT = "ota/s200_firmware_ota.bin"; - private static final long APP_LOAD_ADDR = 0x00438000L; - private static final long APP_MAX_END = 0x007F0000L; - private static final int APP_PREAMBLE = 0x20; - - private final Context context; - private final String leftAddress; - private final String rightAddress; - private final String firmwarePath; - private final FaceclawBleManager bleManager; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private final Object lock = new Object(); - private final LinkedBlockingQueue dataAcks = new LinkedBlockingQueue<>(); - - private volatile FaceclawFirmwareFlasherListener listener; - private volatile Thread worker; - private volatile boolean cancelled = false; - - private int nextSeq = 0; - private int nextAuthMagic = 0x60; - private int pendingAuthMagic = -1; - private CountDownLatch authLatch; - private byte[] authAckPb; - - public FaceclawFirmwareFlasher(Context context, String rightAddress, String leftAddress, String firmwarePath) { - this.context = context.getApplicationContext(); - this.rightAddress = rightAddress == null ? "" : rightAddress; - this.leftAddress = leftAddress == null ? "" : leftAddress; - this.firmwarePath = firmwarePath == null ? "" : firmwarePath; - this.bleManager = new FaceclawBleManager(this.context); - this.bleManager.setListener(this); - } - - public void setListener(FaceclawFirmwareFlasherListener listener) { - this.listener = listener; - } - - public void start() { - synchronized (lock) { - if (worker != null) { - return; - } - worker = new Thread(this::run, "faceclaw-flasher"); - worker.start(); - } - } - - public void cancel() { - cancelled = true; - Thread w = worker; - if (w != null) { - w.interrupt(); - } - } - - public void close() { - cancel(); - try { - bleManager.close(); - } catch (Exception ignored) { - } - } - - private void run() { - try { - if (leftAddress.trim().isEmpty() || rightAddress.trim().isEmpty()) { - throw new IllegalStateException("Both lens addresses are required to flash."); - } - - emitState("validating", ""); - byte[] img = readFile(firmwarePath); - List segs = validate(img); - emitLog("firmware validated: " + segs.size() + " components, " + img.length + " bytes"); - - flashLens("left", leftAddress, img, segs, FIRST_LENS_CONNECT_WINDOW_MS); - if (cancelled) { - emitState("error", "Cancelled."); - emitComplete(false, "Cancelled after the left lens."); - return; - } - - emitState("rebooting", "Left lens done. Both lenses reboot briefly; reconnecting for the right lens."); - sleepInterruptibly(REBOOT_SETTLE_MS); - flashLens("right", rightAddress, img, segs, SECOND_LENS_CONNECT_WINDOW_MS); - - emitState("done", ""); - emitComplete(true, "Both lenses flashed. The glasses are rebooting into the custom firmware."); - } catch (Exception e) { - String message = e.getMessage() == null ? e.toString() : e.getMessage(); - if (cancelled) { - message = "Cancelled."; - } - emitState("error", message); - emitComplete(false, message); - } finally { - teardown(); - } - } - - private void flashLens(String lens, String address, byte[] img, List segs, int connectWindowMs) - throws TimeoutException { - emitState("connecting", lens); - connectLensResilient(lens, address, connectWindowMs); - sleepInterruptibly(NOTIFY_SETTLE_MS); - drainAcks(); - // Fresh envelope counter for the OTA stream (the auth exchange during - // bring-up used its own); mirrors the stock sequence. - resetSeq(); - - emitState("flashing", lens); - int beginStatus = sendCtrlAndWait(address, OP_BEGIN, EMPTY, CTRL_ACK_TIMEOUT_MS); - if (!isEndOk(beginStatus)) { - emitLog("warning: unexpected begin status " + beginStatus + "; continuing"); - } - // Progress is reported in bytes across the whole image: the segments - // are one large firmware blob plus several small ones, so a bar - // stepped per segment would sit still through the big one and then - // sprint through the rest. - long totalBytes = 0; - for (Segment seg : segs) { - totalBytes += seg.ps; - } - long bytesBefore = 0; - for (int i = 0; i < segs.size(); i++) { - if (cancelled) { - throw new IllegalStateException("Cancelled."); - } - flashComponentWithRetry(lens, address, i, segs.size(), segs.get(i), img, bytesBefore, totalBytes); - bytesBefore += segs.get(i).ps; - } - emitLog(lens + " lens: all components verified"); - try { - bleManager.disconnect(address); - } catch (Exception ignored) { - } - } - - private void flashComponentWithRetry(String lens, String address, int index, int count, Segment seg, byte[] img, - long bytesBefore, long totalBytes) { - for (int attempt = 0; attempt < COMPONENT_RETRIES; attempt++) { - if (attempt > 0) { - emitLog(seg.name + ": re-flash attempt " + (attempt + 1) + "/" + COMPONENT_RETRIES); - } - int endStatus; - try { - endStatus = flashComponent(lens, address, index, count, seg, img, bytesBefore, totalBytes); - } catch (TimeoutException | RuntimeException e) { - emitLog(seg.name + ": block phase failed: " + e.getMessage()); - endStatus = -1; - } - if (isEndOk(endStatus)) { - emitLog(seg.name + ": END verify OK (status " + endStatus + ")"); - return; - } - if (endStatus >= 0) { - emitLog(seg.name + ": END verify FAILED (status " + endStatus + ")"); - } - drainAcks(); - sleepInterruptibly(1_500); - } - throw new IllegalStateException("component " + seg.name + " failed after " + COMPONENT_RETRIES + " attempts"); - } - - private int flashComponent(String lens, String address, int index, int count, Segment seg, byte[] img, - long bytesBefore, long totalBytes) throws TimeoutException { - byte[] sub = Arrays.copyOfRange(img, seg.off, seg.off + 128); - int ps = seg.ps; - int payloadStart = seg.off + 128; - - int checkStatus = sendCtrlAndWait(address, OP_FILE_CHECK, sub, CTRL_ACK_TIMEOUT_MS); - if (checkStatus != 0) { - throw new IllegalStateException("FILE_CHECK rejected status=" + checkStatus); - } - - int blockCount = (ps + BLOCK_SIZE - 1) / BLOCK_SIZE; - for (int b = 0; b < blockCount; b++) { - if (cancelled) { - throw new IllegalStateException("Cancelled."); - } - int start = payloadStart + b * BLOCK_SIZE; - int end = Math.min(start + BLOCK_SIZE, payloadStart + ps); - byte[] block = Arrays.copyOfRange(img, start, end); - - boolean accepted = false; - for (int tries = 0; tries < BLOCK_NAK_RETRIES; tries++) { - int status = sendBlock(address, block); // throws TimeoutException -> component re-flash - if (status == 0) { - accepted = true; - break; - } - emitLog(seg.name + ": block " + b + "/" + blockCount + " NAK=" + status - + " resend " + (tries + 1) + "/" + BLOCK_NAK_RETRIES); - } - if (!accepted) { - throw new IllegalStateException("block " + b + " NAK'd " + BLOCK_NAK_RETRIES + " times"); - } - if (b % 20 == 0 || b == blockCount - 1) { - long bytesSent = bytesBefore + Math.min((long) (b + 1) * BLOCK_SIZE, (long) ps); - emitProgress(lens, index + 1, count, b + 1, blockCount, bytesSent, totalBytes); - } - } - emitLog(seg.name + ": data phase done; sending END"); - return sendCtrlAndWait(address, OP_END, EMPTY, CTRL_ACK_TIMEOUT_MS); - } - - /** Send one 4 KB block as a marker + data pair sharing one envelope seq. */ - private int sendBlock(String address, byte[] block) throws TimeoutException { - int seq = nextSeq(); - drainAcks(); - writeOta(address, BleProtocol.OTA_DATA_WRITE_UUID, SID_CTRL, new byte[] {(byte) OP_BLOCK}, seq); - writeOta(address, BleProtocol.OTA_DATA_WRITE_UUID, SID_DATA, block, seq); - return waitAck(OP_BLOCK, BLOCK_ACK_TIMEOUT_MS); - } - - private int sendCtrlAndWait(String address, int op, byte[] data, int timeoutMs) throws TimeoutException { - int seq = nextSeq(); - drainAcks(); - byte[] payload = new byte[1 + data.length]; - payload[0] = (byte) op; - System.arraycopy(data, 0, payload, 1, data.length); - writeOta(address, BleProtocol.OTA_DATA_WRITE_UUID, SID_CTRL, payload, seq); - return waitAck(op, timeoutMs); - } - - private boolean writeOta(String address, String writeChar, int sid, byte[] payload, int seq) { - List frames = BleProtocol.framePb(payload, sid, FLAG_OTA, seq); - return bleManager.writeFrames( - address, writeChar, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS); - } - - private int waitAck(int wantOp, int timeoutMs) throws TimeoutException { - long deadline = SystemClock.elapsedRealtime() + timeoutMs; - while (true) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - break; - } - byte[] pb; - try { - pb = dataAcks.poll(remaining, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new TimeoutException("interrupted waiting for ack op=" + wantOp); - } - if (pb == null) { - break; - } - if (pb.length >= 2 && (pb[0] & 0xff) == wantOp) { - return pb[1] & 0xff; - } - // Different opcode (e.g. a late ack from a prior stage) — ignore, keep waiting. - } - throw new TimeoutException("no ack op=0x" + Integer.toHexString(wantOp) + " within " + timeoutMs + "ms"); - } - - // ---- connection ---------------------------------------------------------- - - private void connectLensResilient(String lens, String address, int windowMs) { - long deadline = SystemClock.elapsedRealtime() + windowMs; - String lastError = "unknown"; - int attempt = 0; - while (SystemClock.elapsedRealtime() < deadline && !cancelled) { - attempt++; - try { - if (bringUpLens(address)) { - emitLog("connected " + lens + " lens (attempt " + attempt + ")"); - return; - } - lastError = "connect/discover incomplete"; - } catch (Exception e) { - lastError = e.getMessage() == null ? e.toString() : e.getMessage(); - } - emitLog(lens + " connect attempt " + attempt + " failed (" + lastError + "); retrying..."); - try { - bleManager.disconnect(address); - } catch (Exception ignored) { - } - sleepInterruptibly(RETRY_DELAY_MS); - } - if (cancelled) { - throw new IllegalStateException("Cancelled."); - } - throw new IllegalStateException("could not reach " + lens + " lens: " + lastError); - } - - private boolean bringUpLens(String address) { - if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { - return false; - } - bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH); - bleManager.requestMtu(address, ConnectionOptions.DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); - if (!bleManager.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) { - return false; - } - // Control-channel notify carries the auth response; mandatory. - if (!bleManager.enableNotifications(address, BleProtocol.NOTIFY_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { - return false; - } - if (!bleManager.enableNotifications(address, BleProtocol.OTA_DATA_NOTIFY_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { - return false; - } - if (!authenticate(address)) { - throw new IllegalStateException( - "security auth not acknowledged — if Android shows a Bluetooth pairing request, accept it"); - } - return true; - } - - /** - * Complete the sid-0x80 security-auth exchange on this connection. On an - * unbonded phone this triggers SMP pairing (possibly with an OS prompt), - * so the wait is generous. Must succeed before any OTA traffic. - */ - private boolean authenticate(String address) { - int magic; - CountDownLatch latch = new CountDownLatch(1); - synchronized (lock) { - magic = nextAuthMagic; - nextAuthMagic = nextAuthMagic >= 0x7f ? 0x60 : nextAuthMagic + 1; - pendingAuthMagic = magic; - authAckPb = null; - authLatch = latch; - } - try { - if (!writeOta(address, BleProtocol.WRITE_CHAR_UUID, BleProtocol.SID_SECURITY_AUTH, - BleProtocol.buildAuthenticationRequest(magic), nextSeq())) { - emitLog("auth write failed: " + address); - return false; - } - boolean signalled; - try { - signalled = latch.await(ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - byte[] pb; - synchronized (lock) { - pb = authAckPb; - } - if (!signalled || !BleProtocol.isAuthenticationSuccess(pb, magic)) { - emitLog("security auth not acknowledged (" + address - + ") — if Android shows a Bluetooth pairing request, accept it"); - return false; - } - emitLog("security auth complete: " + address); - return true; - } finally { - synchronized (lock) { - pendingAuthMagic = -1; - authLatch = null; - } - } - } - - // ---- firmware container parsing / validation ----------------------------- - - private static final byte[] EMPTY = new byte[0]; - - private static final class Segment { - final int off; - final int ps; - final long crc; - final String name; - - Segment(int off, int ps, long crc, String name) { - this.off = off; - this.ps = ps; - this.crc = crc; - this.name = name; - } - } - - private List parseSegments(byte[] img) { - if (img.length < 0x40) { - throw new IllegalStateException("file is too small to be a firmware image"); - } - long n = readU32(img, 8); - if (n <= 0 || n > 64) { - throw new IllegalStateException("implausible component count " + n + " (corrupt image?)"); - } - List segs = new ArrayList<>(); - for (int i = 0; i < n; i++) { - int base = 0x40 + i * 16; - long crc = readU32(img, base + 12); - int off = (int) readU32(img, base + 4); - if (off + 128 > img.length) { - throw new IllegalStateException("segment " + i + " subheader runs past end of file"); - } - int ps = (int) readU32(img, off + 8); - String name = readCString(img, off + 48, 80); - segs.add(new Segment(off, ps, crc, name)); - } - return segs; - } - - private List validate(byte[] img) { - List segs = parseSegments(img); - if (segs.size() < MIN_SEGMENTS || segs.size() > MAX_SEGMENTS) { - throw new IllegalStateException( - "expected " + MIN_SEGMENTS + "-" + MAX_SEGMENTS + " components, found " + segs.size()); - } - Segment main = null; - for (Segment s : segs) { - byte[] payload = Arrays.copyOfRange(img, s.off + 128, s.off + 128 + s.ps); - long calc = crc32cMsb(payload) & 0xffffffffL; - long subCrc = readU32(img, s.off + 12); - if (calc != s.crc || calc != subCrc) { - throw new IllegalStateException("component " + s.name + " CRC32C is stale (image not checksum-fixed)"); - } - if (REQUIRED_SEGMENT.equals(s.name)) { - main = s; - } - } - if (main == null) { - throw new IllegalStateException("required component " + REQUIRED_SEGMENT + " not found"); - } - checkMainAppFitsMram(img, main); - return segs; - } - - private void checkMainAppFitsMram(byte[] img, Segment main) { - if (main.ps < APP_PREAMBLE) { - throw new IllegalStateException("main-app payload is smaller than its preamble"); - } - long loadAddr = readU32(img, main.off + 128 + 0x14); - long preLen = readU32(img, main.off + 128) & 0xFFFFFFL; - if (loadAddr != APP_LOAD_ADDR) { - throw new IllegalStateException( - "main-app preamble load address is 0x" + Long.toHexString(loadAddr) - + ", expected 0x" + Long.toHexString(APP_LOAD_ADDR)); - } - if (preLen != main.ps) { - throw new IllegalStateException( - "main-app preamble length (" + preLen + ") != staged payload size (" + main.ps + ")"); - } - long progEnd = APP_LOAD_ADDR + main.ps - APP_PREAMBLE; - if (progEnd > APP_MAX_END) { - long over = progEnd - APP_MAX_END; - throw new IllegalStateException( - "main-app is too large: programmed region ends at 0x" + Long.toHexString(progEnd) - + ", " + over + " bytes past the safe MRAM ceiling — refusing to flash (brick risk)"); - } - } - - // ---- listener callbacks -------------------------------------------------- - - @Override - public void onNotification(String address, String characteristicUuid, byte[] data) { - if (BleProtocol.NOTIFY_CHAR_UUID.equalsIgnoreCase(characteristicUuid)) { - // Control channel: only the security-auth response is awaited here. - // A value can carry several envelope frames back to back (e.g. the - // response packed with one of the lens's own sid-0x80 notifies). - for (byte[] buf : BleProtocol.splitFrames(data)) { - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(buf); - if (frame.ok && frame.sid == BleProtocol.SID_SECURITY_AUTH) { - synchronized (lock) { - if (authLatch != null && frame.msgSeq == pendingAuthMagic) { - authAckPb = frame.pb; - authLatch.countDown(); - } - } - } - } - return; - } - if (!BleProtocol.OTA_DATA_NOTIFY_UUID.equalsIgnoreCase(characteristicUuid)) { - return; // OTA acks arrive on the data-notify char - } - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(data); - if (!frame.ok || frame.pb.length < 2) { - return; - } - dataAcks.add(Arrays.copyOf(frame.pb, Math.min(frame.pb.length, 2))); - } - - @Override - public void onConnectionStateChange(String address, boolean connected) { - if (!connected) { - Log.i(TAG, "disconnected: " + address); - } - } - - // ---- helpers ------------------------------------------------------------- - - private void teardown() { - try { - bleManager.close(); - } catch (Exception ignored) { - } - } - - private void resetSeq() { - synchronized (lock) { - nextSeq = 0; - } - } - - private int nextSeq() { - synchronized (lock) { - nextSeq = (nextSeq + 1) & 0xff; - return nextSeq; - } - } - - private void drainAcks() { - dataAcks.clear(); - } - - private static boolean isEndOk(int status) { - for (int ok : END_OK) { - if (ok == status) { - return true; - } - } - return false; - } - - private void sleepInterruptibly(int ms) { - try { - Thread.sleep(ms); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - - private static byte[] readFile(String path) { - try (FileInputStream fis = new FileInputStream(path)) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buf = new byte[65536]; - int read; - while ((read = fis.read(buf)) >= 0) { - out.write(buf, 0, read); - } - return out.toByteArray(); - } catch (IOException e) { - throw new IllegalStateException("could not read firmware: " + e.getMessage(), e); - } - } - - private static long readU32(byte[] buf, int offset) { - return (buf[offset] & 0xffL) - | ((buf[offset + 1] & 0xffL) << 8) - | ((buf[offset + 2] & 0xffL) << 16) - | ((buf[offset + 3] & 0xffL) << 24); - } - - private static String readCString(byte[] buf, int offset, int maxLen) { - int end = offset; - int limit = Math.min(buf.length, offset + maxLen); - while (end < limit && buf[end] != 0) { - end++; - } - return new String(buf, offset, end - offset, StandardCharsets.ISO_8859_1); - } - - private static final int[] CRC32C_TABLE = buildCrc32cTable(); - - private static int[] buildCrc32cTable() { - int[] table = new int[256]; - for (int b = 0; b < 256; b++) { - int c = b << 24; - for (int i = 0; i < 8; i++) { - c = (c & 0x80000000) != 0 ? (c << 1) ^ 0x1edc6f41 : c << 1; - } - table[b] = c; - } - return table; - } - - /** CRC-32C, MSB-first, init 0, no final xor (matches g2flash.py crc32c_msb). */ - private static int crc32cMsb(byte[] data) { - int crc = 0; - for (byte value : data) { - crc = (crc << 8) ^ CRC32C_TABLE[((crc >>> 24) ^ (value & 0xff)) & 0xff]; - } - return crc; - } - - private void emitLog(String line) { - Log.i(TAG, line); - mainHandler.post(() -> { - FaceclawFirmwareFlasherListener current = listener; - if (current != null) { - current.onLog(line); - } - }); - } - - private void emitProgress(String lens, int componentIndex, int componentCount, int blockIndex, int blockCount, - long bytesSent, long bytesTotal) { - mainHandler.post(() -> { - FaceclawFirmwareFlasherListener current = listener; - if (current != null) { - current.onProgress(lens, componentIndex, componentCount, blockIndex, blockCount, bytesSent, bytesTotal); - } - }); - } - - private void emitState(String state, String detail) { - final String safeDetail = detail == null ? "" : detail; - mainHandler.post(() -> { - FaceclawFirmwareFlasherListener current = listener; - if (current != null) { - current.onState(state, safeDetail); - } - }); - } - - private void emitComplete(boolean success, String detail) { - final String safeDetail = detail == null ? "" : detail; - mainHandler.post(() -> { - FaceclawFirmwareFlasherListener current = listener; - if (current != null) { - current.onComplete(success, safeDetail); - } - }); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.kt new file mode 100644 index 00000000..6169c965 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.kt @@ -0,0 +1,88 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.Handler +import android.os.Looper + +/** + * Android host for the shared [OtaFlashFlow] (g2flash.py's OTA procedure over the stock + * firmware-data service): owns the GATT link (AndroidStockLink) and the worker thread, and + * delivers listener callbacks on the main thread. Separate from FaceclawBleCommunicator on + * purpose: this speaks the stock OTA protocol and must run before the custom firmware exists. + * Public API unchanged for app/native/firmware-flasher.ts. + */ +class FaceclawFirmwareFlasher(context: Context, rightAddress: String?, leftAddress: String?, firmwarePath: String?) { + private val link = AndroidStockLink(context.applicationContext) + private val mainHandler = Handler(Looper.getMainLooper()) + private val lock = Any() + + @Volatile + private var listener: FaceclawFirmwareFlasherListener? = null + + @Volatile + private var worker: Thread? = null + + private val flow = + OtaFlashFlow( + link, + rightAddress ?: "", + leftAddress ?: "", + firmwarePath ?: "", + object : FaceclawFirmwareFlasherListener { + override fun onLog(line: String?) { + android.util.Log.i(OtaFlashFlow.TAG, line ?: "") + post { it.onLog(line) } + } + + override fun onProgress( + lens: String?, + componentIndex: Int, + componentCount: Int, + blockIndex: Int, + blockCount: Int, + bytesSent: Long, + bytesTotal: Long, + ) = post { it.onProgress(lens, componentIndex, componentCount, blockIndex, blockCount, bytesSent, bytesTotal) } + + override fun onState(state: String?, detail: String?) = post { it.onState(state, detail ?: "") } + + override fun onComplete(success: Boolean, detail: String?) = post { it.onComplete(success, detail ?: "") } + }, + ) + + fun setListener(listener: FaceclawFirmwareFlasherListener?) { + this.listener = listener + } + + fun start() { + synchronized(lock) { + if (worker != null) { + return + } + val w = Thread({ flow.run() }, "faceclaw-flasher") + worker = w + w.start() + } + } + + fun cancel() { + flow.cancel() + } + + fun close() { + cancel() + try { + link.close() + } catch (ignored: Exception) { + } + } + + private fun post(action: (FaceclawFirmwareFlasherListener) -> Unit) { + mainHandler.post { + val current = listener + if (current != null) { + action(current) + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasherListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasherListener.java deleted file mode 100644 index 2cd24e20..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasherListener.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.faceclaw.app; - -/** Callbacks from FaceclawFirmwareFlasher to the TypeScript layer. */ -public interface FaceclawFirmwareFlasherListener { - void onLog(String line); - - /** - * Fine-grained progress for the UI bar. `lens` is "left"/"right"; - * component/block indices are 1-based against their counts. `bytesSent` / - * `bytesTotal` cover the whole lens image, so a bar driven by them moves - * evenly even though the components differ wildly in size. - */ - void onProgress(String lens, int componentIndex, int componentCount, int blockIndex, int blockCount, - long bytesSent, long bytesTotal); - - /** - * Coarse lifecycle. `state` is one of: validating, connecting, flashing, - * rebooting, done, error. `detail` carries the lens name or a message. - */ - void onState(String state, String detail); - - /** Terminal callback: success or failure, with an explanatory message. */ - void onComplete(boolean success, String detail); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareUtil.java deleted file mode 100644 index 32ce5dc1..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareUtil.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.faceclaw.app; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * Native helpers for the on-device custom-firmware build: SHA-256 hashing and - * binary file writing. Both take a java.nio.ByteBuffer so NativeScript can - * marshal a JS ArrayBuffer straight across without a per-element copy of the - * ~4 MB firmware image (and with no overload ambiguity). Also home of the one - * shared byte→hex encoder for com.faceclaw.app. - */ -public final class FaceclawFirmwareUtil { - private FaceclawFirmwareUtil() {} - - /** Lowercase hex of the bytes. The TS-side decoder is case-insensitive. */ - public static String bytesToHex(byte[] bytes) { - if (bytes == null || bytes.length == 0) { - return ""; - } - StringBuilder sb = new StringBuilder(bytes.length * 2); - for (byte b : bytes) { - int v = b & 0xff; - sb.append(Character.forDigit(v >>> 4, 16)); - sb.append(Character.forDigit(v & 0x0f, 16)); - } - return sb.toString(); - } - - /** Lowercase hex SHA-256 of the buffer's remaining bytes. */ - public static String sha256Hex(ByteBuffer data) { - try { - MessageDigest md = MessageDigest.getInstance("SHA-256"); - md.update(data.duplicate()); - return bytesToHex(md.digest()); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException("SHA-256 unavailable", e); - } - } - - /** Write the buffer's remaining bytes to `path`, overwriting any existing file. */ - public static void writeFile(String path, ByteBuffer data) { - try (FileOutputStream fos = new FileOutputStream(path); - FileChannel channel = fos.getChannel()) { - ByteBuffer view = data.duplicate(); - while (view.hasRemaining()) { - channel.write(view); - } - } catch (IOException e) { - throw new RuntimeException("firmware write failed: " + e.getMessage(), e); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java deleted file mode 100644 index 239e290f..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java +++ /dev/null @@ -1,546 +0,0 @@ -package com.faceclaw.app; - -import android.bluetooth.BluetoothGatt; -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -/** - * A minimal, stock-firmware-compatible BLE communicator used only to show the - * pre-flash confirmation prompt on the glasses and read back the user's Yes/No - * choice. It is deliberately separate from FaceclawBleCommunicator: the main - * app assumes the custom firmware (image containers, compositor, etc.), whereas - * this must work on unmodified glasses, so it speaks only the stock subset — - * session prelude, a text container, a list container, and heartbeats. No - * images. - * - * It reuses the low-level GATT wrapper (FaceclawBleManager) and the framing / - * protobuf helpers (BleProtocol), but owns its own GATT connection and runs its - * whole lifecycle on a single worker thread. It expects the main app to be - * disconnected while it runs (onboarding, before flashing). - * - * Both arms are connected and security-authenticated up front, even though the - * prompt itself only needs the right arm (the lenses relay messages to each - * other, and acks/events only ever come from the right arm): on an unbonded - * phone the auth exchange is what raises the OS pairing prompt, and doing both - * here means both prompts appear at the start rather than one popping up half - * way through flashing the second lens. While connected, each arm's battery - * level is read (they are independent batteries) so the caller can refuse to - * flash on a low charge. - * - * With `skipPrompt` the on-glasses confirmation is not shown: the flow is just - * connect + auth + battery read + result(approved). Used to re-check the - * battery after the user has already confirmed once. - */ -public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { - private static final String TAG = "FaceclawFlashPrompt"; - - private static final String TEXT_NAME = "flashwarn"; - private static final String LIST_NAME = "flashmenu"; - private static final int TEXT_CONTAINER_ID = 1; - private static final int LIST_CONTAINER_ID = 2; - // Index 0 = decline, index 1 = approve. Kept short for the ~50-col grid. - private static final String[] ITEMS = new String[] {"No, cancel", "Yes, flash"}; - - private static final int HEARTBEAT_INTERVAL_MS = 4_000; - private static final int SELECTION_TIMEOUT_MS = 120_000; - private static final int CREATE_ACK_TIMEOUT_MS = 3_000; - private static final int BATTERY_ACK_TIMEOUT_MS = 3_000; - - private final Context context; - private final String rightAddress; - private final String leftAddress; - private final String warningText; - private final boolean skipPrompt; - private final FaceclawBleManager bleManager; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - - private final Object lock = new Object(); - private final ConcurrentHashMap pendingAcks = new ConcurrentHashMap<>(); - private final ConcurrentHashMap ackPayloads = new ConcurrentHashMap<>(); - private final CountDownLatch selectionLatch = new CountDownLatch(1); - - private volatile FaceclawFlashPromptListener listener; - private volatile Thread worker; - private volatile boolean cancelled = false; - private volatile boolean finished = false; - private volatile boolean rightConnected = false; - private volatile boolean leftConnected = false; - private volatile boolean rightLost = false; - private volatile Boolean approved = null; - private volatile ScheduledExecutorService heartbeatExecutor; - - private int nextMagic = 100; - private int nextSeq = 0x40; - - public FaceclawFlashPromptCommunicator( - Context context, String rightAddress, String leftAddress, String warningText, boolean skipPrompt) { - this.context = context.getApplicationContext(); - this.rightAddress = rightAddress == null ? "" : rightAddress; - this.leftAddress = leftAddress == null ? "" : leftAddress; - this.warningText = warningText == null ? "" : warningText; - this.skipPrompt = skipPrompt; - this.bleManager = new FaceclawBleManager(this.context); - this.bleManager.setListener(this); - } - - public void setListener(FaceclawFlashPromptListener listener) { - this.listener = listener; - } - - public void start() { - synchronized (lock) { - if (worker != null) { - return; - } - worker = new Thread(this::run, "faceclaw-flash-prompt"); - worker.start(); - } - } - - /** Abort the prompt (e.g. the user backed out on the phone). */ - public void cancel() { - cancelled = true; - selectionLatch.countDown(); - Thread w = worker; - if (w != null) { - w.interrupt(); - } - } - - public void close() { - cancel(); - stopHeartbeat(); - try { - bleManager.close(); - } catch (Exception ignored) { - } - } - - private void run() { - try { - if (rightAddress.trim().isEmpty()) { - emitState("error", "No right-arm address configured."); - return; - } - - emitState("connecting", ""); - connectArm(rightAddress, "right"); - rightConnected = true; - if (cancelled) { - teardown(); - return; - } - if (!leftAddress.trim().isEmpty()) { - connectArm(leftAddress, "left"); - leftConnected = true; - if (cancelled) { - teardown(); - return; - } - } - - emitState("connected", ""); - // Auth both arms now (pairing prompts, if any, happen here) before - // anything else, so both bonds exist by the time flashing starts. - authenticateArm(rightAddress, "right"); - if (leftConnected) { - authenticateArm(leftAddress, "left"); - } - if (cancelled) { - teardown(); - return; - } - sendPrelude(rightAddress); - - if (!skipPrompt) { - showPrompt(rightAddress); - startHeartbeat(); - emitState("prompting", ""); - - selectionLatch.await(SELECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS); - stopHeartbeat(); - - if (cancelled) { - emitState("cancelled", ""); - teardown(); - return; - } - if (approved == null) { - if (rightLost) { - emitState("disconnected", "Lost connection to the glasses."); - } else { - emitState("timeout", "No response from the glasses."); - } - teardown(); - return; - } - try { - sendShutdown(); - } catch (Exception ignored) { - } - if (!Boolean.TRUE.equals(approved)) { - emitResult(false); - emitState("result", "declined"); - teardown(); - return; - } - } - - // Approved (or prompt skipped): read each arm's battery while the - // links are still up, then report the result. - emitState("battery", ""); - readBatteries(); - if (cancelled) { - emitState("cancelled", ""); - teardown(); - return; - } - if (rightLost) { - emitState("disconnected", "Lost connection to the glasses."); - teardown(); - return; - } - finished = true; - emitResult(true); - emitState("result", "approved"); - teardown(); - } catch (Exception e) { - stopHeartbeat(); - if (!cancelled) { - String message = e.getMessage() == null ? e.toString() : e.getMessage(); - emitState("error", message); - } - teardown(); - } - } - - private void connectArm(String address, String arm) { - if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { - throw new IllegalStateException("could not connect to the " + arm + " arm (" + address + ")"); - } - bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH); - bleManager.requestMtu(address, ConnectionOptions.DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); - if (!bleManager.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) { - throw new IllegalStateException("discoverServices failed: " + arm + " arm (" + address + ")"); - } - if (!bleManager.enableNotifications(address, BleProtocol.NOTIFY_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { - throw new IllegalStateException("enableNotifications failed: " + arm + " arm (" + address + ")"); - } - emitLog("connected " + arm + " arm"); - } - - /** - * Security-auth exchange on sid=0x80: firmware 2.2.9 will not run a - * session (and closes the link after ~30 s) without it, and on an - * unbonded phone this is what triggers SMP pairing, so it may sit - * waiting on an OS pairing prompt. - */ - private void authenticateArm(String address, String arm) throws InterruptedException { - int authMagic = allocMagic(); - byte[] authAck = writeAndAwaitAck( - address, - BleProtocol.SID_SECURITY_AUTH, - BleProtocol.FLAG_SECURITY_AUTH, - authMagic, - BleProtocol.buildAuthenticationRequest(authMagic), - ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS); - if (authAck == null || !BleProtocol.isAuthenticationSuccess(authAck, authMagic)) { - throw new IllegalStateException( - "could not authenticate with the " + arm + " arm (" + address - + ") — if Android shows a Bluetooth pairing request, accept it and try again"); - } - emitLog("security auth complete: " + arm + " arm"); - } - - /** Mandatory session prelude on sid=0x01 (app-launch). Right arm only; it relays. */ - private void sendPrelude(String address) throws InterruptedException { - if (writeAndAwaitAck( - address, - BleProtocol.PRELUDE_ACK_SID, - BleProtocol.FLAG_REQUEST, - BleProtocol.PRELUDE_ACK_MAGIC, - BleProtocol.PRELUDE_F5872_PAYLOAD, - ConnectionOptions.PRELUDE_TIMEOUT_MS) == null) { - throw new IllegalStateException("session prelude not acked: " + address); - } - } - - private void showPrompt(String address) throws InterruptedException { - // Create the prompt page (warning text + No/Yes list) on sid=0xe0 Cmd=0. - // Two attempts: on 2.2.9 the first request sent right after the - // prelude can be dropped while the lens emits its own sid-0x80 - // notifications (observed with the device-info settings read). - byte[] pageAck = null; - for (int attempt = 0; attempt < 2 && pageAck == null && !cancelled; attempt++) { - int magic = allocMagic(); - byte[] page = BleProtocol.buildCreatePromptPage( - magic, TEXT_NAME, TEXT_CONTAINER_ID, warningText, LIST_NAME, LIST_CONTAINER_ID, ITEMS); - pageAck = writeAndAwaitAck(address, BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST, magic, page, CREATE_ACK_TIMEOUT_MS); - if (pageAck == null) { - emitLog("prompt page attempt " + (attempt + 1) + " unacked: " + address); - } - } - if (pageAck == null) { - throw new IllegalStateException("prompt page not acked: " + address); - } - emitLog("prompt page shown on " + address); - } - - /** - * Read each arm's battery via a sid-0x09 settings read on that arm's own - * link (the arms have independent batteries and each answers with its - * own). An arm that does not answer reports -1; the caller decides what - * to do with a partial reading. - */ - private void readBatteries() throws InterruptedException { - int right = rightConnected ? readBattery(rightAddress, "right") : -1; - int left = leftConnected ? readBattery(leftAddress, "left") : -1; - emitLog("battery R=" + (right < 0 ? "?" : right + "%") + " L=" + (left < 0 ? "?" : left + "%")); - emitBattery(right, left); - } - - private int readBattery(String address, String arm) throws InterruptedException { - for (int attempt = 0; attempt < 2 && !cancelled; attempt++) { - int magic = allocMagic(); - byte[] ack = writeAndAwaitAck( - address, - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildSettingsQuery(magic), - BATTERY_ACK_TIMEOUT_MS); - if (ack == null) { - emitLog("battery read attempt " + (attempt + 1) + " unacked: " + arm + " arm"); - continue; - } - BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(ack); - if (snapshot != null) { - return snapshot.battery; - } - emitLog("battery read ack had no battery field: " + arm + " arm"); - } - return -1; - } - - /** Write and wait for the matching ack; returns the ack's protobuf (may be empty), or null on timeout/write failure. */ - private byte[] writeAndAwaitAck(String address, int sid, int flag, int magic, byte[] payload, int timeoutMs) - throws InterruptedException { - CountDownLatch latch = new CountDownLatch(1); - String key = ackKey(sid, magic); - pendingAcks.put(key, latch); - try { - if (!writeFrame(address, sid, flag, payload)) { - return null; - } - if (!latch.await(timeoutMs, TimeUnit.MILLISECONDS)) { - return null; - } - byte[] pb = ackPayloads.get(key); - return pb == null ? new byte[0] : pb; - } finally { - pendingAcks.remove(key, latch); - ackPayloads.remove(key); - } - } - - private boolean writeFrame(String address, int sid, int flag, byte[] payload) { - int seq; - synchronized (lock) { - seq = nextSeq++ & 0xff; - } - List frames = BleProtocol.framePb(payload, sid, flag, seq); - return bleManager.writeFrames( - address, - BleProtocol.WRITE_CHAR_UUID, - frames, - ConnectionOptions.WRITE_TYPE, - ConnectionOptions.WRITE_TIMEOUT_MS); - } - - private void startHeartbeat() { - stopHeartbeat(); - ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(runnable -> { - Thread t = new Thread(runnable, "faceclaw-flash-hb"); - t.setDaemon(true); - return t; - }); - heartbeatExecutor = executor; - executor.scheduleWithFixedDelay( - this::sendHeartbeat, HEARTBEAT_INTERVAL_MS, HEARTBEAT_INTERVAL_MS, TimeUnit.MILLISECONDS); - } - - private void sendHeartbeat() { - if (finished || cancelled) { - return; - } - try { - byte[] heartbeat = BleProtocol.buildHeartbeat(allocMagic()); - if (rightConnected) { - writeFrame(rightAddress, BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST, heartbeat); - } - } catch (Exception e) { - Log.w(TAG, "heartbeat failed: " + e.getMessage()); - } - } - - private void stopHeartbeat() { - ScheduledExecutorService executor = heartbeatExecutor; - heartbeatExecutor = null; - if (executor != null) { - executor.shutdownNow(); - } - } - - private void sendShutdown() { - byte[] shutdown = BleProtocol.buildShutdown(allocMagic(), 0); - if (rightConnected) { - writeFrame(rightAddress, BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST, shutdown); - } - } - - private void teardown() { - finished = true; - stopHeartbeat(); - try { - bleManager.close(); - } catch (Exception ignored) { - } - rightConnected = false; - leftConnected = false; - } - - @Override - public void onNotification(String address, String characteristicUuid, byte[] data) { - if (!BleProtocol.NOTIFY_CHAR_UUID.equalsIgnoreCase(characteristicUuid)) { - return; - } - // A value can carry several envelope frames back to back. - for (byte[] buf : BleProtocol.splitFrames(data)) { - handleFrame(address, buf); - } - } - - private void handleFrame(String address, byte[] buf) { - BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(buf); - if (!frame.ok) { - return; - } - if (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT) { - handleEvent(frame); - return; - } - if (frame.msgSeq >= 0) { - String key = ackKey(frame.sid, frame.msgSeq); - CountDownLatch latch = pendingAcks.get(key); - if (latch != null) { - ackPayloads.put(key, frame.pb); - latch.countDown(); - } - } - } - - private void handleEvent(BleProtocol.ParsedFrame frame) { - BleProtocol.ListSelection selection = BleProtocol.parseListSelection(frame); - if (selection == null || !LIST_NAME.equals(selection.containerName)) { - return; - } - // Only a confirmed click is a decision; scroll/highlight changes are ignored. - if (selection.eventType != BleProtocol.EVENT_CLICK) { - return; - } - boolean yes = selection.itemIndex == 1 - || (selection.itemName != null && selection.itemName.toLowerCase().startsWith("yes")); - synchronized (lock) { - if (finished) { - return; - } - finished = true; - approved = yes; - } - emitLog("selection: " + (yes ? "flash" : "cancel") + " (index " + selection.itemIndex + ")"); - selectionLatch.countDown(); - } - - @Override - public void onConnectionStateChange(String address, boolean connected) { - if (connected) { - return; - } - if (address.equalsIgnoreCase(leftAddress)) { - // The prompt itself only needs the right arm; a dropped left link - // just means no left battery reading. - leftConnected = false; - emitLog("left arm disconnected"); - return; - } - if (address.equalsIgnoreCase(rightAddress)) { - rightConnected = false; - synchronized (lock) { - if (!finished && !cancelled) { - rightLost = true; - finished = true; - selectionLatch.countDown(); - } - } - } - } - - private int allocMagic() { - synchronized (lock) { - int magic = nextMagic; - nextMagic = nextMagic >= 255 ? 100 : nextMagic + 1; - return magic; - } - } - - private static String ackKey(int sid, int magic) { - return sid + ":" + magic; - } - - private void emitLog(String line) { - Log.i(TAG, line); - mainHandler.post(() -> { - FaceclawFlashPromptListener current = listener; - if (current != null) { - current.onLog(line); - } - }); - } - - private void emitState(String state, String detail) { - final String safeDetail = detail == null ? "" : detail; - mainHandler.post(() -> { - FaceclawFlashPromptListener current = listener; - if (current != null) { - current.onState(state, safeDetail); - } - }); - } - - private void emitBattery(int rightPercent, int leftPercent) { - mainHandler.post(() -> { - FaceclawFlashPromptListener current = listener; - if (current != null) { - current.onBattery(rightPercent, leftPercent); - } - }); - } - - private void emitResult(boolean approvedResult) { - mainHandler.post(() -> { - FaceclawFlashPromptListener current = listener; - if (current != null) { - current.onResult(approvedResult); - } - }); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.kt new file mode 100644 index 00000000..37ad4593 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.kt @@ -0,0 +1,88 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.Handler +import android.os.Looper + +/** + * Android host for the shared [FlashPromptFlow] (the stock-firmware pre-flash Yes/No prompt + * plus battery read): owns the GATT link (AndroidStockLink) and the worker thread, and + * delivers listener callbacks on the main thread. It expects the main app to be disconnected + * while it runs (onboarding, before flashing). Public API unchanged for + * app/native/flash-prompt-communicator.ts. + */ +class FaceclawFlashPromptCommunicator( + context: Context, + rightAddress: String?, + leftAddress: String?, + warningText: String?, + skipPrompt: Boolean, +) { + private val link = AndroidStockLink(context.applicationContext) + private val mainHandler = Handler(Looper.getMainLooper()) + private val lock = Any() + + @Volatile + private var listener: FaceclawFlashPromptListener? = null + + @Volatile + private var worker: Thread? = null + + private val flow = + FlashPromptFlow( + link, + rightAddress ?: "", + leftAddress ?: "", + warningText ?: "", + skipPrompt, + object : FaceclawFlashPromptListener { + override fun onLog(line: String?) { + android.util.Log.i(FlashPromptFlow.TAG, line ?: "") + post { it.onLog(line) } + } + + override fun onState(state: String?, detail: String?) = post { it.onState(state, detail ?: "") } + + override fun onBattery(rightPercent: Int, leftPercent: Int) = post { it.onBattery(rightPercent, leftPercent) } + + override fun onResult(approved: Boolean) = post { it.onResult(approved) } + }, + ) + + fun setListener(listener: FaceclawFlashPromptListener?) { + this.listener = listener + } + + fun start() { + synchronized(lock) { + if (worker != null) { + return + } + val w = Thread({ flow.run() }, "faceclaw-flash-prompt") + worker = w + w.start() + } + } + + /** Abort the prompt (e.g. the user backed out on the phone). */ + fun cancel() { + flow.cancel() + } + + fun close() { + cancel() + try { + link.close() + } catch (ignored: Exception) { + } + } + + private fun post(action: (FaceclawFlashPromptListener) -> Unit) { + mainHandler.post { + val current = listener + if (current != null) { + action(current) + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptListener.java deleted file mode 100644 index a7ac2da3..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptListener.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.faceclaw.app; - -/** Callbacks from FaceclawFlashPromptCommunicator to the TypeScript layer. */ -public interface FaceclawFlashPromptListener { - void onLog(String line); - - /** - * Lifecycle updates. `state` is one of: connecting, connected, prompting, - * battery, result, cancelled, timeout, disconnected, error. `detail` carries the - * "approved"/"declined" text for result, or an error/explanation message. - */ - void onState(String state, String detail); - - /** - * Battery percent of each arm, read after the user approved (or right - * after auth when the prompt is skipped); -1 when an arm didn't answer. - * Always fires before onResult(true). - */ - void onBattery(int rightPercent, int leftPercent); - - /** Fires once the user picks a menu row (or, with skipPrompt, once the battery is read): true = flash, false = cancel. */ - void onResult(boolean approved); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.java deleted file mode 100644 index 44e38a0b..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.java +++ /dev/null @@ -1,142 +0,0 @@ -package com.faceclaw.app; - -import android.Manifest; -import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.Service; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ServiceInfo; -import android.os.Build; -import android.os.IBinder; - -import androidx.core.content.ContextCompat; - -import com.tns.NativeScriptActivity; - -public class FaceclawForegroundService extends Service { - public static final String ACTION_START = "com.faceclaw.app.action.START"; - public static final String ACTION_UPDATE = "com.faceclaw.app.action.UPDATE"; - public static final String ACTION_STOP = "com.faceclaw.app.action.STOP"; - public static final String EXTRA_TEXT = "text"; - - // Channel id was bumped from "faceclaw-dashboard" when the badge setting - // changed: Android freezes a channel's showBadge flag at creation, so the - // old channel (which let Samsung's launcher count the pinned notification - // as a red "1" badge) is deleted on upgrade rather than reused. - private static final String LEGACY_CHANNEL_ID = "faceclaw-dashboard"; - private static final String CHANNEL_ID = "faceclaw-connection"; - private static final int NOTIFICATION_ID = 4201; - - @Override - public IBinder onBind(Intent intent) { - return null; - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - String action = intent != null ? intent.getAction() : ACTION_START; - String text = intent != null ? intent.getStringExtra(EXTRA_TEXT) : null; - - if (ACTION_STOP.equals(action)) { - stopForeground(STOP_FOREGROUND_REMOVE); - stopSelf(); - return START_NOT_STICKY; - } - - ensureNotificationChannel(); - Notification notification = buildNotification( - text != null && !text.trim().isEmpty() ? text : "Connected to glasses" - ); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - startForeground(NOTIFICATION_ID, notification, foregroundServiceType()); - } else { - startForeground(NOTIFICATION_ID, notification); - } - - if (ACTION_UPDATE.equals(action)) { - NotificationManager manager = getSystemService(NotificationManager.class); - if (manager != null) { - manager.notify(NOTIFICATION_ID, notification); - } - } - - return START_STICKY; - } - - private void ensureNotificationChannel() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - return; - } - - NotificationChannel channel = new NotificationChannel( - CHANNEL_ID, - "Glasses connection", - NotificationManager.IMPORTANCE_LOW - ); - channel.setDescription("Keeps Faceclaw connected to the glasses."); - // The pinned status notification must not count toward the launcher - // icon's notification badge. - channel.setShowBadge(false); - - NotificationManager manager = getSystemService(NotificationManager.class); - if (manager != null) { - manager.createNotificationChannel(channel); - manager.deleteNotificationChannel(LEGACY_CHANNEL_ID); - } - } - - private Notification buildNotification(String text) { - Intent launchIntent = new Intent(this, NativeScriptActivity.class); - launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); - - int flags = PendingIntent.FLAG_UPDATE_CURRENT; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - flags |= PendingIntent.FLAG_IMMUTABLE; - } - - PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent, flags); - - Notification.Builder builder = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O - ? new Notification.Builder(this, CHANNEL_ID) - : new Notification.Builder(this); - - return builder - .setContentTitle("Faceclaw") - .setContentText(text) - .setSmallIcon(getApplicationInfo().icon) - .setContentIntent(contentIntent) - .setOngoing(true) - .setOnlyAlertOnce(true) - .build(); - } - - private int foregroundServiceType() { - int type = ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE; - // TODO: Make this depend on which audio path (G2 vs phone) is selected - if (hasRecordAudioPermission()) { - type |= ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE; - } - // The location type keeps while-in-use location flowing to the - // Navigate app when the phone screen locks. Only claimed once the - // permission exists: on API 34+ claiming it without the permission - // makes startForeground throw. - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && hasFineLocationPermission()) { - type |= ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION; - } - return type; - } - - private boolean hasRecordAudioPermission() { - return ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) - == PackageManager.PERMISSION_GRANTED; - } - - private boolean hasFineLocationPermission() { - return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) - == PackageManager.PERMISSION_GRANTED; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.kt new file mode 100644 index 00000000..9b50bd3e --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawForegroundService.kt @@ -0,0 +1,141 @@ +package com.faceclaw.app + +import android.Manifest +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Intent +import android.content.pm.PackageManager +import android.content.pm.ServiceInfo +import android.os.Build +import android.os.IBinder + +import androidx.core.content.ContextCompat + +import com.tns.NativeScriptActivity + +class FaceclawForegroundService : Service() { + companion object { + const val ACTION_START = "com.faceclaw.app.action.START" + const val ACTION_UPDATE = "com.faceclaw.app.action.UPDATE" + const val ACTION_STOP = "com.faceclaw.app.action.STOP" + const val EXTRA_TEXT = "text" + + // Channel id was bumped from "faceclaw-dashboard" when the badge setting + // changed: Android freezes a channel's showBadge flag at creation, so the + // old channel (which let Samsung's launcher count the pinned notification + // as a red "1" badge) is deleted on upgrade rather than reused. + private const val LEGACY_CHANNEL_ID = "faceclaw-dashboard" + private const val CHANNEL_ID = "faceclaw-connection" + private const val NOTIFICATION_ID = 4201 + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + val action = if (intent != null) intent.action else ACTION_START + val text = intent?.getStringExtra(EXTRA_TEXT) + + if (ACTION_STOP == action) { + stopForeground(STOP_FOREGROUND_REMOVE) + stopSelf() + return START_NOT_STICKY + } + + ensureNotificationChannel() + val notification = buildNotification( + if (text != null && !text.trim().isEmpty()) text else "Connected to glasses" + ) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(NOTIFICATION_ID, notification, foregroundServiceType()) + } else { + startForeground(NOTIFICATION_ID, notification) + } + + if (ACTION_UPDATE == action) { + val manager = getSystemService(NotificationManager::class.java) + manager?.notify(NOTIFICATION_ID, notification) + } + + return START_STICKY + } + + private fun ensureNotificationChannel() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + return + } + + val channel = NotificationChannel( + CHANNEL_ID, + "Glasses connection", + NotificationManager.IMPORTANCE_LOW + ) + channel.description = "Keeps Faceclaw connected to the glasses." + // The pinned status notification must not count toward the launcher + // icon's notification badge. + channel.setShowBadge(false) + + val manager = getSystemService(NotificationManager::class.java) + if (manager != null) { + manager.createNotificationChannel(channel) + manager.deleteNotificationChannel(LEGACY_CHANNEL_ID) + } + } + + private fun buildNotification(text: String): Notification { + val launchIntent = Intent(this, NativeScriptActivity::class.java) + launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP) + + var flags = PendingIntent.FLAG_UPDATE_CURRENT + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + flags = flags or PendingIntent.FLAG_IMMUTABLE + } + + val contentIntent = PendingIntent.getActivity(this, 0, launchIntent, flags) + + val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + Notification.Builder(this, CHANNEL_ID) + else + Notification.Builder(this) + + return builder + .setContentTitle("Faceclaw") + .setContentText(text) + .setSmallIcon(applicationInfo.icon) + .setContentIntent(contentIntent) + .setOngoing(true) + .setOnlyAlertOnce(true) + .build() + } + + private fun foregroundServiceType(): Int { + var type = ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE + // TODO: Make this depend on which audio path (G2 vs phone) is selected + if (hasRecordAudioPermission()) { + type = type or ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE + } + // The location type keeps while-in-use location flowing to the + // Navigate app when the phone screen locks. Only claimed once the + // permission exists: on API 34+ claiming it without the permission + // makes startForeground throw. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && hasFineLocationPermission()) { + type = type or ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION + } + return type + } + + private fun hasRecordAudioPermission(): Boolean { + return ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == + PackageManager.PERMISSION_GRANTED + } + + private fun hasFineLocationPermission(): Boolean { + return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == + PackageManager.PERMISSION_GRANTED + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.java deleted file mode 100644 index 9f9287a9..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.faceclaw.app; - -import java.io.IOException; - -import okhttp3.Interceptor; -import okhttp3.Request; -import okhttp3.Response; - -/** - * Shared HTTP identity for the Java networking helpers. The version lives in - * app/version.ts, so the TypeScript side pushes the real string in at startup - * (see installNativeUserAgent in app/util/http.ts); the default below is only - * a fallback for requests that somehow beat that call. - */ -public final class FaceclawHttp { - private static volatile String userAgent = "Faceclaw"; - - private FaceclawHttp() {} - - public static void setUserAgent(String value) { - if (value != null && !value.trim().isEmpty()) { - userAgent = value.trim(); - } - } - - public static String getUserAgent() { - return userAgent; - } - - /** Stamps our User-Agent on any request that doesn't already carry one. */ - public static Interceptor userAgentInterceptor() { - return new Interceptor() { - @Override public Response intercept(Chain chain) throws IOException { - Request request = chain.request(); - if (request.header("User-Agent") != null) { - return chain.proceed(request); - } - return chain.proceed(request.newBuilder().header("User-Agent", userAgent).build()); - } - }; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.kt new file mode 100644 index 00000000..9f26efab --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawHttp.kt @@ -0,0 +1,38 @@ +package com.faceclaw.app + +import java.io.IOException + +import okhttp3.Interceptor +import okhttp3.Request +import okhttp3.Response + +/** + * Android okhttp face of the shared HTTP identity. The version lives in + * app/version.ts, so the TypeScript side pushes the real string in at startup + * (see installNativeUserAgent in app/util/http.ts); the value itself is held + * by the shared HttpIdentity so every platform's networking reads one string. + */ +class FaceclawHttp private constructor() { + companion object { + @JvmStatic + fun setUserAgent(value: String?) = HttpIdentity.setUserAgent(value) + + @JvmStatic + fun getUserAgent(): String = HttpIdentity.getUserAgent() + + /** Stamps our User-Agent on any request that doesn't already carry one. */ + @JvmStatic + fun userAgentInterceptor(): Interceptor { + return object : Interceptor { + @Throws(IOException::class) + override fun intercept(chain: Interceptor.Chain): Response { + val request: Request = chain.request() + if (request.header("User-Agent") != null) { + return chain.proceed(request) + } + return chain.proceed(request.newBuilder().header("User-Agent", HttpIdentity.getUserAgent()).build()) + } + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawImuListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawImuListener.java deleted file mode 100644 index 2be8bb81..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawImuListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.faceclaw.app; - -/** - * Receives IMU (accelerometer) readings decoded from the glasses' sys-event - * stream. Registered on FaceclawBleCommunicator via addImuListener; callbacks - * are delivered on the Android main thread. - */ -public interface FaceclawImuListener { - void onImuData(double x, double y, double z, int eventSource); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.java deleted file mode 100644 index 1ed6b950..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; - -public class FaceclawLc3Decoder { - private static final String TAG = "FaceclawLc3"; - public static final int SAMPLE_RATE = 16000; - public static final int FRAME_US = 10000; - public static final int FRAME_BYTES = 40; - public static final int FRAMES_PER_PACKET = 5; - public static final int PACKET_BYTES = 205; - public static final int COUNTER_OFFSET = 204; - public static final int SAMPLES_PER_FRAME = 160; - public static final int SAMPLES_PER_PACKET = FRAMES_PER_PACKET * SAMPLES_PER_FRAME; - // Trailer layout (firmware service_audio.c): after the 200 LC3 bytes the - // glasses DSP appends two signed 16-bit LE values computed on the raw - // stereo capture before the mono downmix — a signal-strength ratio and the - // asin-derived direction-of-arrival in degrees — then the packet counter. - public static final int SSR_OFFSET = 200; - public static final int ANGLE_OFFSET = 202; - - static { - System.loadLibrary("faceclaw_lc3"); - } - - private long nativeHandle; - private int lastCounter = -1; - private long realPackets; - private long duplicatePackets; - private long missingPackets; - private long decodeErrors; - private int lastSsr; - private int lastAngleDegrees; - - public FaceclawLc3Decoder() { - nativeHandle = nativeCreate(FRAME_US, SAMPLE_RATE); - if (nativeHandle == 0) { - throw new IllegalStateException("Could not create LC3 decoder"); - } - } - - public synchronized int decodePacket(byte[] packet, short[] pcmOut) { - if (nativeHandle == 0) { - throw new IllegalStateException("LC3 decoder is closed"); - } - if (packet == null || packet.length != PACKET_BYTES) { - decodeErrors++; - Log.w(TAG, "unexpected G2 audio packet length=" + (packet == null ? -1 : packet.length)); - return 0; - } - if (pcmOut == null || pcmOut.length < SAMPLES_PER_PACKET) { - throw new IllegalArgumentException("pcmOut must hold " + SAMPLES_PER_PACKET + " samples"); - } - - int counter = packet[COUNTER_OFFSET] & 0xff; - if (lastCounter >= 0) { - int gap = (counter - lastCounter) & 0xff; - if (gap == 0) { - duplicatePackets++; - return 0; - } - int missing = gap - 1; - if (missing > 0) { - missingPackets += missing; - } - } - - int decoded = nativeDecodePacket(nativeHandle, packet, pcmOut); - if (decoded <= 0) { - decodeErrors++; - return 0; - } - realPackets++; - lastCounter = counter; - lastSsr = (short) ((packet[SSR_OFFSET] & 0xff) | (packet[SSR_OFFSET + 1] << 8)); - lastAngleDegrees = (short) ((packet[ANGLE_OFFSET] & 0xff) | (packet[ANGLE_OFFSET + 1] << 8)); - return decoded; - } - - /** Firmware signal-strength ratio from the most recent decoded packet. */ - public synchronized int getLastSsr() { - return lastSsr; - } - - /** Firmware direction-of-arrival (signed degrees) from the most recent decoded packet. */ - public synchronized int getLastAngleDegrees() { - return lastAngleDegrees; - } - - public synchronized long getRealPackets() { - return realPackets; - } - - public synchronized long getDuplicatePackets() { - return duplicatePackets; - } - - public synchronized long getMissingPackets() { - return missingPackets; - } - - public synchronized long getDecodeErrors() { - return decodeErrors; - } - - public synchronized void close() { - long handle = nativeHandle; - nativeHandle = 0; - if (handle != 0) { - nativeDestroy(handle); - } - } - - private static native long nativeCreate(int frameUs, int sampleRate); - private static native int nativeDecodePacket(long handle, byte[] packet, short[] pcmOut); - private static native void nativeDestroy(long handle); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.kt new file mode 100644 index 00000000..1028f3bf --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLc3Decoder.kt @@ -0,0 +1,88 @@ +package com.faceclaw.app + +import android.util.Log + +/** + * Android LC3 decoder for G2 microphone packets: the liblc3 JNI codec behind the shared + * [Lc3PacketFramer], which owns the counter/duplicate/trailer logic and statistics. + */ +class FaceclawLc3Decoder { + companion object { + private const val TAG = "FaceclawLc3" + const val SAMPLE_RATE = Lc3PacketFramer.SAMPLE_RATE + const val FRAME_US = Lc3PacketFramer.FRAME_US + const val FRAME_BYTES = Lc3PacketFramer.FRAME_BYTES + const val FRAMES_PER_PACKET = Lc3PacketFramer.FRAMES_PER_PACKET + const val PACKET_BYTES = Lc3PacketFramer.PACKET_BYTES + const val COUNTER_OFFSET = Lc3PacketFramer.COUNTER_OFFSET + const val SAMPLES_PER_FRAME = Lc3PacketFramer.SAMPLES_PER_FRAME + const val SAMPLES_PER_PACKET = Lc3PacketFramer.SAMPLES_PER_PACKET + const val SSR_OFFSET = Lc3PacketFramer.SSR_OFFSET + const val ANGLE_OFFSET = Lc3PacketFramer.ANGLE_OFFSET + + init { + System.loadLibrary("faceclaw_lc3") + } + + @JvmStatic + private external fun nativeCreate(frameUs: Int, sampleRate: Int): Long + @JvmStatic + private external fun nativeDecodePacket(handle: Long, packet: ByteArray, pcmOut: ShortArray): Int + @JvmStatic + private external fun nativeDestroy(handle: Long) + } + + private var nativeHandle: Long = nativeCreate(FRAME_US, SAMPLE_RATE) + private val framer = Lc3PacketFramer(object : Lc3Codec { + override fun decodeFrames(packet: ByteArray, offset: Int, length: Int, pcmOut: ShortArray): Int { + // The JNI entry decodes the five 40-byte frames at the start of the packet. + return nativeDecodePacket(nativeHandle, packet, pcmOut) + } + }) + + init { + if (nativeHandle == 0L) { + throw IllegalStateException("Could not create LC3 decoder") + } + } + + @Synchronized + fun decodePacket(packet: ByteArray?, pcmOut: ShortArray?): Int { + if (nativeHandle == 0L) { + throw IllegalStateException("LC3 decoder is closed") + } + if (packet == null || packet.size != PACKET_BYTES) { + Log.w(TAG, "unexpected G2 audio packet length=" + (packet?.size ?: -1)) + } + return framer.decodePacket(packet, pcmOut) + } + + /** Firmware signal-strength ratio from the most recent decoded packet. */ + @Synchronized + fun getLastSsr(): Int = framer.lastSsr + + /** Firmware direction-of-arrival (signed degrees) from the most recent decoded packet. */ + @Synchronized + fun getLastAngleDegrees(): Int = framer.lastAngleDegrees + + @Synchronized + fun getRealPackets(): Long = framer.realPackets + + @Synchronized + fun getDuplicatePackets(): Long = framer.duplicatePackets + + @Synchronized + fun getMissingPackets(): Long = framer.missingPackets + + @Synchronized + fun getDecodeErrors(): Long = framer.decodeErrors + + @Synchronized + fun close() { + val handle = nativeHandle + nativeHandle = 0 + if (handle != 0L) { + nativeDestroy(handle) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaListener.java deleted file mode 100644 index f5419b84..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaListener.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.faceclaw.app; - -/** - * Callbacks for one on-phone LLM generation. Like the other bridge listeners, - * implementations are typically created on the NativeScript JS thread and - * FaceclawLlamaRunner posts these callbacks back to that thread's Looper. - */ -public interface FaceclawLlamaListener { - /** A piece of generated text (may be a partial word). */ - void onToken(String piece); - - /** Generation finished: "stop" (EOG), "length", or "cancelled". */ - void onDone(String stopReason); - - void onError(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.java deleted file mode 100644 index 17e98834..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -/** - * On-phone LLM inference for the voice assistant (llama.cpp over JNI). - * - * All model work (load, generate, free) runs on a single background executor - * thread so calls are naturally serialized; listener callbacks are posted to - * the Looper of the thread that constructed this object (the JS thread), the - * same contract as FaceclawSseRequest. cancel() may be called from any thread - * and interrupts the in-flight generation at the next token boundary. - * - * The model stays loaded between generations (turn iterations reuse the KV - * cache prefix); the TypeScript side calls unload() on an idle timer to give - * the ~3GB back. - */ -public class FaceclawLlamaRunner { - private static final String TAG = "FaceclawLlamaRunner"; - - static { - System.loadLibrary("faceclaw_llama"); - } - - private final Handler callbackHandler; - private final ExecutorService executor = Executors.newSingleThreadExecutor(runnable -> { - Thread thread = new Thread(runnable, "FaceclawLlama"); - thread.setPriority(Thread.NORM_PRIORITY); - return thread; - }); - - // Mutated only on the executor thread; volatile so cancel() can read it. - private volatile long handle; - private String loadedPath; - - public FaceclawLlamaRunner() { - Looper looper = Looper.myLooper(); - callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); - } - - public boolean isModelLoaded() { - return handle != 0; - } - - /** - * Generate a completion, loading (or swapping) the model first if needed. - * grammar may be null/empty for unconstrained generation. - */ - public void generate(final String modelPath, final int nCtx, final int nThreads, - final String prompt, final String grammar, final int maxTokens, - final float temperature, final float topP, final int topK, - final FaceclawLlamaListener listener) { - executor.execute(() -> { - try { - if (handle != 0 && !modelPath.equals(loadedPath)) { - nativeFree(handle); - handle = 0; - loadedPath = null; - } - if (handle == 0) { - long loaded = nativeLoadModel(modelPath, nCtx, nThreads); - if (loaded == 0) { - post(() -> listener.onError("Could not load the on-phone model")); - return; - } - handle = loaded; - loadedPath = modelPath; - } - nativeGenerate(handle, prompt, grammar == null ? "" : grammar, - maxTokens, temperature, topP, topK, new FaceclawLlamaListener() { - @Override public void onToken(final String piece) { - post(() -> listener.onToken(piece)); - } - @Override public void onDone(final String stopReason) { - post(() -> listener.onDone(stopReason)); - } - @Override public void onError(final String message) { - post(() -> listener.onError(message)); - } - }); - } catch (Throwable t) { - Log.e(TAG, "generation failed", t); - final String message = String.valueOf(t); - post(() -> listener.onError("On-phone model failed: " + message)); - } - }); - } - - /** Interrupt the current generation (its listener gets onDone("cancelled")). */ - public void cancel() { - long current = handle; - if (current != 0) { - nativeCancel(current); - } - } - - /** Free the model and its KV cache. Safe to call when nothing is loaded. */ - public void unload() { - executor.execute(() -> { - if (handle != 0) { - nativeFree(handle); - handle = 0; - loadedPath = null; - } - }); - } - - private void post(Runnable runnable) { - callbackHandler.post(runnable); - } - - private static native long nativeLoadModel(String path, int nCtx, int nThreads); - private static native void nativeGenerate(long handle, String prompt, String grammar, - int maxTokens, float temperature, float topP, int topK, FaceclawLlamaListener listener); - private static native void nativeCancel(long handle); - private static native void nativeFree(long handle); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.kt new file mode 100644 index 00000000..9cc3fa66 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLlamaRunner.kt @@ -0,0 +1,133 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.util.Log + +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors + +/** + * On-phone LLM inference for the voice assistant (llama.cpp over JNI). + * + * All model work (load, generate, free) runs on a single background executor + * thread so calls are naturally serialized; listener callbacks are posted to + * the Looper of the thread that constructed this object (the JS thread), the + * same contract as FaceclawSseRequest. cancel() may be called from any thread + * and interrupts the in-flight generation at the next token boundary. + * + * The model stays loaded between generations (turn iterations reuse the KV + * cache prefix); the TypeScript side calls unload() on an idle timer to give + * the ~3GB back. + */ +class FaceclawLlamaRunner { + companion object { + private const val TAG = "FaceclawLlamaRunner" + + init { + System.loadLibrary("faceclaw_llama") + } + + @JvmStatic + private external fun nativeLoadModel(path: String?, nCtx: Int, nThreads: Int): Long + @JvmStatic + private external fun nativeGenerate( + handle: Long, prompt: String?, grammar: String, + maxTokens: Int, temperature: Float, topP: Float, topK: Int, listener: FaceclawLlamaListener + ) + @JvmStatic + private external fun nativeCancel(handle: Long) + @JvmStatic + private external fun nativeFree(handle: Long) + } + + private val callbackHandler: Handler + private val executor: ExecutorService = Executors.newSingleThreadExecutor { runnable -> + val thread = Thread(runnable, "FaceclawLlama") + thread.priority = Thread.NORM_PRIORITY + thread + } + + // Mutated only on the executor thread; volatile so cancel() can read it. + @Volatile + private var handle: Long = 0 + private var loadedPath: String? = null + + init { + val looper = Looper.myLooper() + callbackHandler = Handler(looper ?: Looper.getMainLooper()) + } + + fun isModelLoaded(): Boolean { + return handle != 0L + } + + /** + * Generate a completion, loading (or swapping) the model first if needed. + * grammar may be null/empty for unconstrained generation. + */ + fun generate( + modelPath: String?, nCtx: Int, nThreads: Int, + prompt: String?, grammar: String?, maxTokens: Int, + temperature: Float, topP: Float, topK: Int, + listener: FaceclawLlamaListener + ) { + executor.execute { + try { + if (handle != 0L && modelPath != loadedPath) { + nativeFree(handle) + handle = 0 + loadedPath = null + } + if (handle == 0L) { + val loaded = nativeLoadModel(modelPath, nCtx, nThreads) + if (loaded == 0L) { + post { listener.onError("Could not load the on-phone model") } + return@execute + } + handle = loaded + loadedPath = modelPath + } + nativeGenerate(handle, prompt, grammar ?: "", + maxTokens, temperature, topP, topK, object : FaceclawLlamaListener { + override fun onToken(piece: String?) { + post { listener.onToken(piece) } + } + override fun onDone(stopReason: String?) { + post { listener.onDone(stopReason) } + } + override fun onError(message: String?) { + post { listener.onError(message) } + } + }) + } catch (t: Throwable) { + Log.e(TAG, "generation failed", t) + val message = t.toString() + post { listener.onError("On-phone model failed: $message") } + } + } + } + + /** Interrupt the current generation (its listener gets onDone("cancelled")). */ + fun cancel() { + val current = handle + if (current != 0L) { + nativeCancel(current) + } + } + + /** Free the model and its KV cache. Safe to call when nothing is loaded. */ + fun unload() { + executor.execute { + if (handle != 0L) { + nativeFree(handle) + handle = 0 + loadedPath = null + } + } + } + + private fun post(runnable: Runnable) { + callbackHandler.post(runnable) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationListener.java deleted file mode 100644 index 84aead80..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationListener.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.faceclaw.app; - -/** One-shot Android location callbacks for the TypeScript Weather bridge. */ -public interface FaceclawLocationListener { - void onLocation(double latitude, double longitude, float accuracyMeters, long timestampMs); - - void onError(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.java deleted file mode 100644 index 90e610c9..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.java +++ /dev/null @@ -1,196 +0,0 @@ -package com.faceclaw.app; - -import android.Manifest; -import android.content.Context; -import android.content.pm.PackageManager; -import android.location.Criteria; -import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; - -import androidx.core.content.ContextCompat; - -import java.util.List; - -/** - * One-shot, foreground-only location lookup for Weather. It prefers a fresh - * coarse network fix, but can fall back to the newest cached provider fix if - * Android cannot produce a new location before the timeout. - */ -public final class FaceclawLocationProvider implements LocationListener { - private static final long FRESH_CACHE_MS = 10L * 60L * 1000L; - private static final long MAX_CACHE_MS = 24L * 60L * 60L * 1000L; - private static final long TIMEOUT_MS = 15L * 1000L; - - private final Context context; - private final LocationManager locationManager; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final Runnable timeout = this::onTimeout; - - private FaceclawLocationListener listener; - private Location cachedLocation; - private boolean running; - - public FaceclawLocationProvider(Context context) { - this.context = context.getApplicationContext(); - this.locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE); - } - - public void setListener(FaceclawLocationListener listener) { - this.listener = listener; - } - - public void start() { - mainHandler.post(this::startOnMainThread); - } - - public void cancel() { - mainHandler.post(() -> finish(null, null)); - } - - private void startOnMainThread() { - if (running) { - return; - } - if (locationManager == null) { - deliverError("Android location service is unavailable."); - return; - } - if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) - != PackageManager.PERMISSION_GRANTED - && ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) - != PackageManager.PERMISSION_GRANTED) { - deliverError("Location permission is required."); - return; - } - - cachedLocation = newestCachedLocation(); - if (isRecent(cachedLocation, FRESH_CACHE_MS)) { - deliverLocation(cachedLocation); - return; - } - - String provider = chooseProvider(); - if (provider == null) { - if (isRecent(cachedLocation, MAX_CACHE_MS)) { - deliverLocation(cachedLocation); - } else { - deliverError("Turn on Location on your phone, then retry."); - } - return; - } - - try { - running = true; - locationManager.requestSingleUpdate(provider, this, Looper.getMainLooper()); - mainHandler.postDelayed(timeout, TIMEOUT_MS); - } catch (SecurityException error) { - finish(null, "Location permission is required."); - } catch (Throwable error) { - finish(null, "Unable to request the current location."); - } - } - - private String chooseProvider() { - try { - if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { - return LocationManager.NETWORK_PROVIDER; - } - Criteria criteria = new Criteria(); - criteria.setAccuracy(Criteria.ACCURACY_COARSE); - criteria.setPowerRequirement(Criteria.POWER_LOW); - return locationManager.getBestProvider(criteria, true); - } catch (Throwable ignored) { - return null; - } - } - - private Location newestCachedLocation() { - Location newest = null; - try { - List providers = locationManager.getProviders(true); - for (String provider : providers) { - Location candidate = locationManager.getLastKnownLocation(provider); - if (candidate != null && (newest == null || candidate.getTime() > newest.getTime())) { - newest = candidate; - } - } - } catch (SecurityException ignored) { - // The explicit permission check above owns the user-facing error. - } catch (Throwable ignored) { - // A fresh request may still succeed even if cached providers fail. - } - return newest; - } - - private static boolean isRecent(Location location, long maximumAgeMs) { - return location != null - && location.getTime() > 0L - && System.currentTimeMillis() - location.getTime() <= maximumAgeMs; - } - - private void onTimeout() { - if (isRecent(cachedLocation, MAX_CACHE_MS)) { - finish(cachedLocation, null); - } else { - finish(null, "Couldn't get your current location. Tap to retry."); - } - } - - @Override - public void onLocationChanged(Location location) { - finish(location, null); - } - - @Override - public void onProviderDisabled(String provider) { - // Keep waiting: Android may still deliver a queued fix or the timeout - // can fall back to another provider's cached location. - } - - @Override - public void onProviderEnabled(String provider) { - } - - @Override - @SuppressWarnings("deprecation") - public void onStatusChanged(String provider, int status, Bundle extras) { - } - - private void finish(Location location, String error) { - if (running) { - try { - locationManager.removeUpdates(this); - } catch (Throwable ignored) { - } - } - running = false; - mainHandler.removeCallbacks(timeout); - if (location != null) { - deliverLocation(location); - } else if (error != null) { - deliverError(error); - } - } - - private void deliverLocation(Location location) { - FaceclawLocationListener current = listener; - if (current != null && location != null) { - current.onLocation( - location.getLatitude(), - location.getLongitude(), - location.hasAccuracy() ? location.getAccuracy() : -1f, - location.getTime()); - } - } - - private void deliverError(String message) { - FaceclawLocationListener current = listener; - if (current != null) { - current.onError(message); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.kt new file mode 100644 index 00000000..ce3a7568 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationProvider.kt @@ -0,0 +1,188 @@ +package com.faceclaw.app + +import android.Manifest +import android.content.Context +import android.content.pm.PackageManager +import android.location.Criteria +import android.location.Location +import android.location.LocationListener +import android.location.LocationManager +import android.os.Bundle +import android.os.Handler +import android.os.Looper + +import androidx.core.content.ContextCompat + +/** + * One-shot, foreground-only location lookup for Weather. It prefers a fresh + * coarse network fix, but can fall back to the newest cached provider fix if + * Android cannot produce a new location before the timeout. + */ +class FaceclawLocationProvider(context: Context) : LocationListener { + companion object { + private const val FRESH_CACHE_MS = 10L * 60L * 1000L + private const val MAX_CACHE_MS = 24L * 60L * 60L * 1000L + private const val TIMEOUT_MS = 15L * 1000L + + private fun isRecent(location: Location?, maximumAgeMs: Long): Boolean { + return location != null && + location.time > 0L && + System.currentTimeMillis() - location.time <= maximumAgeMs + } + } + + private val context: Context = context.applicationContext + private val locationManager: LocationManager? = + this.context.getSystemService(Context.LOCATION_SERVICE) as LocationManager? + private val mainHandler = Handler(Looper.getMainLooper()) + private val timeout = Runnable { onTimeout() } + + private var listener: FaceclawLocationListener? = null + private var cachedLocation: Location? = null + private var running = false + + fun setListener(listener: FaceclawLocationListener?) { + this.listener = listener + } + + fun start() { + mainHandler.post { startOnMainThread() } + } + + fun cancel() { + mainHandler.post { finish(null, null) } + } + + private fun startOnMainThread() { + if (running) { + return + } + val manager = locationManager + if (manager == null) { + deliverError("Android location service is unavailable.") + return + } + if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != + PackageManager.PERMISSION_GRANTED && + ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != + PackageManager.PERMISSION_GRANTED) { + deliverError("Location permission is required.") + return + } + + cachedLocation = newestCachedLocation() + if (isRecent(cachedLocation, FRESH_CACHE_MS)) { + deliverLocation(cachedLocation) + return + } + + val provider = chooseProvider() + if (provider == null) { + if (isRecent(cachedLocation, MAX_CACHE_MS)) { + deliverLocation(cachedLocation) + } else { + deliverError("Turn on Location on your phone, then retry.") + } + return + } + + try { + running = true + @Suppress("DEPRECATION") + manager.requestSingleUpdate(provider, this, Looper.getMainLooper()) + mainHandler.postDelayed(timeout, TIMEOUT_MS) + } catch (error: SecurityException) { + finish(null, "Location permission is required.") + } catch (error: Throwable) { + finish(null, "Unable to request the current location.") + } + } + + private fun chooseProvider(): String? { + try { + if (locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { + return LocationManager.NETWORK_PROVIDER + } + val criteria = Criteria() + criteria.accuracy = Criteria.ACCURACY_COARSE + criteria.powerRequirement = Criteria.POWER_LOW + return locationManager.getBestProvider(criteria, true) + } catch (ignored: Throwable) { + return null + } + } + + private fun newestCachedLocation(): Location? { + var newest: Location? = null + try { + val providers = locationManager!!.getProviders(true) + for (provider in providers) { + val candidate = locationManager.getLastKnownLocation(provider) + if (candidate != null && (newest == null || candidate.time > newest.time)) { + newest = candidate + } + } + } catch (ignored: SecurityException) { + // The explicit permission check above owns the user-facing error. + } catch (ignored: Throwable) { + // A fresh request may still succeed even if cached providers fail. + } + return newest + } + + private fun onTimeout() { + if (isRecent(cachedLocation, MAX_CACHE_MS)) { + finish(cachedLocation, null) + } else { + finish(null, "Couldn't get your current location. Tap to retry.") + } + } + + override fun onLocationChanged(location: Location) { + finish(location, null) + } + + override fun onProviderDisabled(provider: String) { + // Keep waiting: Android may still deliver a queued fix or the timeout + // can fall back to another provider's cached location. + } + + override fun onProviderEnabled(provider: String) { + } + + @Deprecated("Deprecated in Java") + override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) { + } + + private fun finish(location: Location?, error: String?) { + if (running) { + try { + locationManager!!.removeUpdates(this) + } catch (ignored: Throwable) { + } + } + running = false + mainHandler.removeCallbacks(timeout) + if (location != null) { + deliverLocation(location) + } else if (error != null) { + deliverError(error) + } + } + + private fun deliverLocation(location: Location?) { + val current = listener + if (current != null && location != null) { + current.onLocation( + location.latitude, + location.longitude, + if (location.hasAccuracy()) location.accuracy else -1f, + location.time) + } + } + + private fun deliverError(message: String) { + val current = listener + current?.onError(message) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.java deleted file mode 100644 index c39f3fa6..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.java +++ /dev/null @@ -1,188 +0,0 @@ -package com.faceclaw.app; - -import android.Manifest; -import android.content.Context; -import android.content.pm.PackageManager; -import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; - -import androidx.core.content.ContextCompat; - -/** - * Continuous location updates for turn-by-turn navigation. Unlike - * FaceclawLocationProvider (one-shot, coarse, for Weather), this streams GPS - * fixes with bearing and speed until stopped. - * - * Callbacks are delivered on the Looper of the thread that constructed the - * tracker, so a worker isolate receives them on its own thread (the same - * convention as FaceclawSseRequest). - */ -public final class FaceclawLocationTracker implements LocationListener { - private final Context context; - private final LocationManager locationManager; - private final Handler callbackHandler; - - /** - * Only seed from the platform's cached fix when it is this fresh. An older - * one is likely wherever the phone was last used (for example the previous - * navigation's destination), and guidance built on it is wrong from the - * first frame: the route starts there and arrival fires immediately. - */ - private static final long MAX_SEED_AGE_MS = 60L * 1000L; - - private FaceclawLocationTrackerListener listener; - private boolean running; - - public FaceclawLocationTracker(Context context) { - this.context = context.getApplicationContext(); - this.locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE); - Looper looper = Looper.myLooper(); - this.callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); - } - - public void setListener(FaceclawLocationTrackerListener listener) { - this.listener = listener; - } - - /** Begin streaming fixes at roughly the requested interval. */ - public void start(long intervalMs) { - callbackHandler.post(() -> startOnCallbackThread(intervalMs)); - } - - public void stop() { - callbackHandler.post(() -> { - if (!running) { - return; - } - running = false; - try { - locationManager.removeUpdates(this); - } catch (Throwable ignored) { - } - }); - } - - private void startOnCallbackThread(long intervalMs) { - if (running) { - return; - } - if (locationManager == null) { - deliverError("Android location service is unavailable."); - return; - } - if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) - != PackageManager.PERMISSION_GRANTED) { - deliverError("Precise location permission is required for navigation."); - return; - } - - String provider = chooseProvider(); - if (provider == null) { - deliverError("Turn on Location on your phone, then retry."); - return; - } - - try { - running = true; - locationManager.requestLocationUpdates(provider, Math.max(500L, intervalMs), 0f, this, - callbackHandler.getLooper()); - // Seed with the freshest cached fix so the UI has a position - // before the first live fix (GPS cold starts can take a while), - // but only if it's recent enough to still be where the user is. - Location cached = freshestCachedLocation(provider); - if (cached != null) { - deliverLocation(cached); - } - } catch (SecurityException error) { - running = false; - deliverError("Precise location permission is required for navigation."); - } catch (Throwable error) { - running = false; - deliverError("Unable to start location updates."); - } - } - - private Location freshestCachedLocation(String primaryProvider) { - Location best = null; - for (String provider : new String[] {primaryProvider, LocationManager.NETWORK_PROVIDER, - LocationManager.GPS_PROVIDER}) { - Location candidate; - try { - candidate = locationManager.getLastKnownLocation(provider); - } catch (Throwable ignored) { - continue; - } - if (candidate == null || ageMs(candidate) > MAX_SEED_AGE_MS) { - continue; - } - if (best == null || candidate.getElapsedRealtimeNanos() > best.getElapsedRealtimeNanos()) { - best = candidate; - } - } - return best; - } - - /** Age via the monotonic clock, so a wall-clock change can't make an old fix look fresh. */ - private static long ageMs(Location location) { - long ageNanos = SystemClock.elapsedRealtimeNanos() - location.getElapsedRealtimeNanos(); - return ageNanos / 1_000_000L; - } - - private String chooseProvider() { - try { - if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { - return LocationManager.GPS_PROVIDER; - } - if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { - return LocationManager.NETWORK_PROVIDER; - } - } catch (Throwable ignored) { - } - return null; - } - - @Override - public void onLocationChanged(Location location) { - deliverLocation(location); - } - - @Override - public void onProviderDisabled(String provider) { - deliverError("Location was turned off on the phone."); - } - - @Override - public void onProviderEnabled(String provider) { - } - - @Override - @SuppressWarnings("deprecation") - public void onStatusChanged(String provider, int status, Bundle extras) { - } - - private void deliverLocation(Location location) { - FaceclawLocationTrackerListener current = listener; - if (current == null || location == null) { - return; - } - current.onLocation( - location.getLatitude(), - location.getLongitude(), - location.hasAccuracy() ? location.getAccuracy() : -1f, - location.hasBearing() ? location.getBearing() : -1f, - location.hasSpeed() ? location.getSpeed() : -1f, - location.getTime()); - } - - private void deliverError(String message) { - FaceclawLocationTrackerListener current = listener; - if (current != null) { - current.onError(message); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.kt new file mode 100644 index 00000000..9eb4e255 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTracker.kt @@ -0,0 +1,184 @@ +package com.faceclaw.app + +import android.Manifest +import android.content.Context +import android.content.pm.PackageManager +import android.location.Location +import android.location.LocationListener +import android.location.LocationManager +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.os.SystemClock + +import androidx.core.content.ContextCompat + +/** + * Continuous location updates for turn-by-turn navigation. Unlike + * FaceclawLocationProvider (one-shot, coarse, for Weather), this streams GPS + * fixes with bearing and speed until stopped. + * + * Callbacks are delivered on the Looper of the thread that constructed the + * tracker, so a worker isolate receives them on its own thread (the same + * convention as FaceclawSseRequest). + */ +class FaceclawLocationTracker(context: Context) : LocationListener { + companion object { + /** + * Only seed from the platform's cached fix when it is this fresh. An older + * one is likely wherever the phone was last used (for example the previous + * navigation's destination), and guidance built on it is wrong from the + * first frame: the route starts there and arrival fires immediately. + */ + private const val MAX_SEED_AGE_MS = 60L * 1000L + + /** Age via the monotonic clock, so a wall-clock change can't make an old fix look fresh. */ + private fun ageMs(location: Location): Long { + val ageNanos = SystemClock.elapsedRealtimeNanos() - location.elapsedRealtimeNanos + return ageNanos / 1_000_000L + } + } + + private val context: Context = context.applicationContext + private val locationManager: LocationManager? = + this.context.getSystemService(Context.LOCATION_SERVICE) as LocationManager? + private val callbackHandler: Handler + + private var listener: FaceclawLocationTrackerListener? = null + private var running = false + + init { + val looper = Looper.myLooper() + this.callbackHandler = Handler(looper ?: Looper.getMainLooper()) + } + + fun setListener(listener: FaceclawLocationTrackerListener?) { + this.listener = listener + } + + /** Begin streaming fixes at roughly the requested interval. */ + fun start(intervalMs: Long) { + callbackHandler.post { startOnCallbackThread(intervalMs) } + } + + fun stop() { + callbackHandler.post { + if (!running) { + return@post + } + running = false + try { + locationManager!!.removeUpdates(this) + } catch (ignored: Throwable) { + } + } + } + + private fun startOnCallbackThread(intervalMs: Long) { + if (running) { + return + } + val manager = locationManager + if (manager == null) { + deliverError("Android location service is unavailable.") + return + } + if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != + PackageManager.PERMISSION_GRANTED) { + deliverError("Precise location permission is required for navigation.") + return + } + + val provider = chooseProvider() + if (provider == null) { + deliverError("Turn on Location on your phone, then retry.") + return + } + + try { + running = true + manager.requestLocationUpdates(provider, Math.max(500L, intervalMs), 0f, this, + callbackHandler.looper) + // Seed with the freshest cached fix so the UI has a position + // before the first live fix (GPS cold starts can take a while), + // but only if it's recent enough to still be where the user is. + val cached = freshestCachedLocation(provider) + if (cached != null) { + deliverLocation(cached) + } + } catch (error: SecurityException) { + running = false + deliverError("Precise location permission is required for navigation.") + } catch (error: Throwable) { + running = false + deliverError("Unable to start location updates.") + } + } + + private fun freshestCachedLocation(primaryProvider: String): Location? { + var best: Location? = null + for (provider in arrayOf(primaryProvider, LocationManager.NETWORK_PROVIDER, + LocationManager.GPS_PROVIDER)) { + val candidate: Location? + try { + candidate = locationManager!!.getLastKnownLocation(provider) + } catch (ignored: Throwable) { + continue + } + if (candidate == null || ageMs(candidate) > MAX_SEED_AGE_MS) { + continue + } + if (best == null || candidate.elapsedRealtimeNanos > best.elapsedRealtimeNanos) { + best = candidate + } + } + return best + } + + private fun chooseProvider(): String? { + try { + if (locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + return LocationManager.GPS_PROVIDER + } + if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { + return LocationManager.NETWORK_PROVIDER + } + } catch (ignored: Throwable) { + } + return null + } + + override fun onLocationChanged(location: Location) { + deliverLocation(location) + } + + override fun onProviderDisabled(provider: String) { + deliverError("Location was turned off on the phone.") + } + + override fun onProviderEnabled(provider: String) { + } + + @Deprecated("Deprecated in Java") + override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) { + } + + private fun deliverLocation(location: Location?) { + val current = listener + if (current == null || location == null) { + return + } + current.onLocation( + location.latitude, + location.longitude, + if (location.hasAccuracy()) location.accuracy else -1f, + if (location.hasBearing()) location.bearing else -1f, + if (location.hasSpeed()) location.speed else -1f, + location.time) + } + + private fun deliverError(message: String) { + val current = listener + current?.onError(message) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTrackerListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTrackerListener.java deleted file mode 100644 index ff0fd1aa..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawLocationTrackerListener.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.faceclaw.app; - -/** Continuous location callbacks for the TypeScript navigation bridge. */ -public interface FaceclawLocationTrackerListener { - /** - * A new fix. bearingDeg/speedMps are -1 when the fix doesn't carry them - * (common when stationary); accuracyMeters is -1 when unknown. - */ - void onLocation(double latitude, double longitude, float accuracyMeters, - float bearingDeg, float speedMps, long timestampMs); - - void onError(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.java deleted file mode 100644 index 07bef961..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.java +++ /dev/null @@ -1,321 +0,0 @@ -package com.faceclaw.app; - -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.media.browse.MediaBrowser; -import android.media.session.MediaController; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * Client for other apps' MediaBrowserService implementations (the mechanism - * Android Auto uses). Binding starts the target player's process even when it - * is not running, so this can list a player's library (playlists, albums, ...) - * and start playback without the phone being unlocked. - * - * Holds at most one connection at a time. All MediaBrowser interaction happens - * on the main thread; results are reported through - * FaceclawMediaBrowserListener, also on the main thread. - */ -public class FaceclawMediaBrowser { - private static final String TAG = "FaceclawMediaBrowser"; - private static final String BROWSER_SERVICE_ACTION = "android.media.browse.MediaBrowserService"; - // Generous: connecting may cold-start the player's process. - private static final long CONNECT_TIMEOUT_MS = 15_000; - private static final long BROWSE_TIMEOUT_MS = 15_000; - // Unbinding immediately after a play command can race the player promoting - // itself to a foreground service, letting the system kill its process - // before playback starts; keep the binding alive briefly instead. - private static final long PLAY_DISCONNECT_GRACE_MS = 5_000; - - private final Context appContext; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private volatile FaceclawMediaBrowserListener listener; - - // Main-thread only. - private MediaBrowser browser; - private int generation; - private long lastPlayCommandAtMs; - - public FaceclawMediaBrowser(Context context) { - this.appContext = context.getApplicationContext(); - } - - public void setListener(FaceclawMediaBrowserListener listener) { - this.listener = listener; - } - - /** - * Installed apps exposing a media browser service, as JSON sorted by app - * name: [{"packageName": ..., "serviceClass": ..., "appName": ...}, ...]. - * Synchronous (PackageManager lookup only). - */ - public String listBrowsableAppsJson() { - PackageManager packageManager = appContext.getPackageManager(); - List services; - try { - services = packageManager.queryIntentServices(new Intent(BROWSER_SERVICE_ACTION), 0); - } catch (Exception e) { - Log.w(TAG, "media browser service query failed", e); - return "[]"; - } - List apps = new ArrayList<>(); - for (ResolveInfo service : services) { - if (service.serviceInfo == null) { - continue; - } - try { - CharSequence label = service.serviceInfo.applicationInfo.loadLabel(packageManager); - JSONObject app = new JSONObject(); - app.put("packageName", service.serviceInfo.packageName); - app.put("serviceClass", service.serviceInfo.name); - app.put("appName", label == null ? service.serviceInfo.packageName : label.toString()); - apps.add(app); - } catch (Exception e) { - Log.w(TAG, "media browser service entry failed", e); - } - } - Collections.sort(apps, (a, b) -> a.optString("appName").compareToIgnoreCase(b.optString("appName"))); - return new JSONArray(apps).toString(); - } - - /** - * Connect to an app's media browser service, starting its process if - * needed; replaces any existing connection. Async; reports through - * onConnectResult with the browse-tree root id on success. - */ - public void connect(int requestId, String packageName, String serviceClass) { - mainHandler.post(() -> { - closeBrowser(); - final int myGeneration = generation; - final boolean[] settled = {false}; - final MediaBrowser[] browserRef = new MediaBrowser[1]; - MediaBrowser newBrowser = new MediaBrowser( - appContext, - new ComponentName(packageName, serviceClass), - new MediaBrowser.ConnectionCallback() { - @Override - public void onConnected() { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - String rootId = ""; - try { - rootId = browserRef[0].getRoot(); - } catch (Exception e) { - Log.w(TAG, "getRoot failed", e); - } - emitConnectResult(requestId, true, rootId, ""); - } - - @Override - public void onConnectionFailed() { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - closeBrowser(); - emitConnectResult(requestId, false, "", "Connection refused by " + packageName); - } - - @Override - public void onConnectionSuspended() { - if (generation != myGeneration) { - return; - } - FaceclawMediaBrowserListener currentListener = listener; - if (currentListener != null) { - currentListener.onDisconnected(); - } - } - }, - null - ); - browserRef[0] = newBrowser; - browser = newBrowser; - mainHandler.postDelayed(() -> { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - closeBrowser(); - emitConnectResult(requestId, false, "", "Connection to " + packageName + " timed out"); - }, CONNECT_TIMEOUT_MS); - try { - newBrowser.connect(); - } catch (Exception e) { - if (!settled[0]) { - settled[0] = true; - closeBrowser(); - emitConnectResult(requestId, false, "", "Connection failed: " + e.getMessage()); - } - } - }); - } - - /** - * Fetch one level of the connected service's content tree. Async; reports - * through onBrowseResult with children as JSON: - * [{"mediaId": ..., "title": ..., "subtitle": ..., "browsable": bool, "playable": bool}, ...]. - */ - public void browse(int requestId, String parentId) { - mainHandler.post(() -> { - final MediaBrowser currentBrowser = browser; - if (currentBrowser == null || !currentBrowser.isConnected()) { - emitBrowseResult(requestId, "", "Not connected"); - return; - } - final int myGeneration = generation; - final boolean[] settled = {false}; - mainHandler.postDelayed(() -> { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - try { - currentBrowser.unsubscribe(parentId); - } catch (Exception ignored) { - } - emitBrowseResult(requestId, "", "Browse timed out"); - }, BROWSE_TIMEOUT_MS); - try { - currentBrowser.subscribe(parentId, new MediaBrowser.SubscriptionCallback() { - @Override - public void onChildrenLoaded(String loadedParentId, List children) { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - // One snapshot is all we want; unsubscribe outside the callback. - mainHandler.post(() -> { - try { - currentBrowser.unsubscribe(loadedParentId); - } catch (Exception ignored) { - } - }); - emitBrowseResult(requestId, serializeChildren(children), ""); - } - - @Override - public void onError(String erroredParentId) { - if (generation != myGeneration || settled[0]) { - return; - } - settled[0] = true; - mainHandler.post(() -> { - try { - currentBrowser.unsubscribe(erroredParentId); - } catch (Exception ignored) { - } - }); - emitBrowseResult(requestId, "", "Browse failed"); - } - }); - } catch (Exception e) { - if (!settled[0]) { - settled[0] = true; - emitBrowseResult(requestId, "", "Browse failed: " + e.getMessage()); - } - } - }); - } - - /** Start playback of a browsed item through the connected service's session. */ - public void playFromMediaId(String mediaId) { - mainHandler.post(() -> { - if (browser == null || !browser.isConnected()) { - return; - } - try { - MediaController controller = new MediaController(appContext, browser.getSessionToken()); - controller.getTransportControls().playFromMediaId(mediaId, null); - lastPlayCommandAtMs = SystemClock.elapsedRealtime(); - } catch (Exception e) { - Log.w(TAG, "playFromMediaId failed", e); - } - }); - } - - public void disconnect() { - mainHandler.post(() -> { - long sincePlayMs = SystemClock.elapsedRealtime() - lastPlayCommandAtMs; - long graceMs = lastPlayCommandAtMs == 0 ? 0 : PLAY_DISCONNECT_GRACE_MS - sincePlayMs; - if (graceMs <= 0) { - closeBrowser(); - return; - } - final MediaBrowser lingering = browser; - browser = null; - generation++; - if (lingering != null) { - mainHandler.postDelayed(() -> { - try { - lingering.disconnect(); - } catch (Exception ignored) { - } - }, graceMs); - } - }); - } - - private void closeBrowser() { - generation++; - if (browser != null) { - try { - browser.disconnect(); - } catch (Exception ignored) { - } - browser = null; - } - } - - private String serializeChildren(List children) { - JSONArray out = new JSONArray(); - if (children == null) { - return out.toString(); - } - for (MediaBrowser.MediaItem item : children) { - try { - CharSequence title = item.getDescription() == null ? null : item.getDescription().getTitle(); - CharSequence subtitle = item.getDescription() == null ? null : item.getDescription().getSubtitle(); - JSONObject entry = new JSONObject(); - entry.put("mediaId", item.getMediaId() == null ? "" : item.getMediaId()); - entry.put("title", title == null ? "" : title.toString()); - entry.put("subtitle", subtitle == null ? "" : subtitle.toString()); - entry.put("browsable", item.isBrowsable()); - entry.put("playable", item.isPlayable()); - out.put(entry); - } catch (Exception e) { - Log.w(TAG, "media item serialization failed", e); - } - } - return out.toString(); - } - - private void emitConnectResult(int requestId, boolean connected, String rootId, String error) { - FaceclawMediaBrowserListener currentListener = listener; - if (currentListener != null) { - currentListener.onConnectResult(requestId, connected, rootId, error); - } - } - - private void emitBrowseResult(int requestId, String childrenJson, String error) { - FaceclawMediaBrowserListener currentListener = listener; - if (currentListener != null) { - currentListener.onBrowseResult(requestId, childrenJson, error); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.kt new file mode 100644 index 00000000..fb716c84 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowser.kt @@ -0,0 +1,316 @@ +package com.faceclaw.app + +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.content.pm.ResolveInfo +import android.media.browse.MediaBrowser +import android.media.session.MediaController +import android.os.Handler +import android.os.Looper +import android.os.SystemClock +import android.util.Log + +import org.json.JSONArray +import org.json.JSONObject + +import java.util.ArrayList +import java.util.Collections + +/** + * Client for other apps' MediaBrowserService implementations (the mechanism + * Android Auto uses). Binding starts the target player's process even when it + * is not running, so this can list a player's library (playlists, albums, ...) + * and start playback without the phone being unlocked. + * + * Holds at most one connection at a time. All MediaBrowser interaction happens + * on the main thread; results are reported through + * FaceclawMediaBrowserListener, also on the main thread. + */ +class FaceclawMediaBrowser(context: Context) { + companion object { + private const val TAG = "FaceclawMediaBrowser" + private const val BROWSER_SERVICE_ACTION = "android.media.browse.MediaBrowserService" + // Generous: connecting may cold-start the player's process. + private const val CONNECT_TIMEOUT_MS = 15_000L + private const val BROWSE_TIMEOUT_MS = 15_000L + // Unbinding immediately after a play command can race the player promoting + // itself to a foreground service, letting the system kill its process + // before playback starts; keep the binding alive briefly instead. + private const val PLAY_DISCONNECT_GRACE_MS = 5_000L + } + + private val appContext: Context = context.applicationContext + private val mainHandler = Handler(Looper.getMainLooper()) + @Volatile + private var listener: FaceclawMediaBrowserListener? = null + + // Main-thread only. + private var browser: MediaBrowser? = null + private var generation = 0 + private var lastPlayCommandAtMs = 0L + + fun setListener(listener: FaceclawMediaBrowserListener?) { + this.listener = listener + } + + /** + * Installed apps exposing a media browser service, as JSON sorted by app + * name: [{"packageName": ..., "serviceClass": ..., "appName": ...}, ...]. + * Synchronous (PackageManager lookup only). + */ + fun listBrowsableAppsJson(): String { + val packageManager = appContext.packageManager + val services: List + try { + services = packageManager.queryIntentServices(Intent(BROWSER_SERVICE_ACTION), 0) + } catch (e: Exception) { + Log.w(TAG, "media browser service query failed", e) + return "[]" + } + val apps: MutableList = ArrayList() + for (service in services) { + if (service.serviceInfo == null) { + continue + } + try { + val label: CharSequence? = service.serviceInfo.applicationInfo.loadLabel(packageManager) + val app = JSONObject() + app.put("packageName", service.serviceInfo.packageName) + app.put("serviceClass", service.serviceInfo.name) + app.put("appName", if (label == null) service.serviceInfo.packageName else label.toString()) + apps.add(app) + } catch (e: Exception) { + Log.w(TAG, "media browser service entry failed", e) + } + } + Collections.sort(apps) { a, b -> a.optString("appName").compareTo(b.optString("appName"), ignoreCase = true) } + return JSONArray(apps).toString() + } + + /** + * Connect to an app's media browser service, starting its process if + * needed; replaces any existing connection. Async; reports through + * onConnectResult with the browse-tree root id on success. + */ + fun connect(requestId: Int, packageName: String?, serviceClass: String?) { + mainHandler.post { + closeBrowser() + val myGeneration = generation + val settled = booleanArrayOf(false) + val browserRef = arrayOfNulls(1) + val newBrowser = MediaBrowser( + appContext, + ComponentName(packageName!!, serviceClass!!), + object : MediaBrowser.ConnectionCallback() { + override fun onConnected() { + if (generation != myGeneration || settled[0]) { + return + } + settled[0] = true + var rootId = "" + try { + rootId = browserRef[0]!!.root + } catch (e: Exception) { + Log.w(TAG, "getRoot failed", e) + } + emitConnectResult(requestId, true, rootId, "") + } + + override fun onConnectionFailed() { + if (generation != myGeneration || settled[0]) { + return + } + settled[0] = true + closeBrowser() + emitConnectResult(requestId, false, "", "Connection refused by $packageName") + } + + override fun onConnectionSuspended() { + if (generation != myGeneration) { + return + } + val currentListener = listener + if (currentListener != null) { + currentListener.onDisconnected() + } + } + }, + null + ) + browserRef[0] = newBrowser + browser = newBrowser + mainHandler.postDelayed({ + if (generation != myGeneration || settled[0]) { + return@postDelayed + } + settled[0] = true + closeBrowser() + emitConnectResult(requestId, false, "", "Connection to $packageName timed out") + }, CONNECT_TIMEOUT_MS) + try { + newBrowser.connect() + } catch (e: Exception) { + if (!settled[0]) { + settled[0] = true + closeBrowser() + emitConnectResult(requestId, false, "", "Connection failed: " + e.message) + } + } + } + } + + /** + * Fetch one level of the connected service's content tree. Async; reports + * through onBrowseResult with children as JSON: + * [{"mediaId": ..., "title": ..., "subtitle": ..., "browsable": bool, "playable": bool}, ...]. + */ + fun browse(requestId: Int, parentId: String?) { + mainHandler.post { + val currentBrowser = browser + if (currentBrowser == null || !currentBrowser.isConnected) { + emitBrowseResult(requestId, "", "Not connected") + return@post + } + val myGeneration = generation + val settled = booleanArrayOf(false) + mainHandler.postDelayed({ + if (generation != myGeneration || settled[0]) { + return@postDelayed + } + settled[0] = true + try { + currentBrowser.unsubscribe(parentId!!) + } catch (ignored: Exception) { + } + emitBrowseResult(requestId, "", "Browse timed out") + }, BROWSE_TIMEOUT_MS) + try { + currentBrowser.subscribe(parentId!!, object : MediaBrowser.SubscriptionCallback() { + override fun onChildrenLoaded(loadedParentId: String, children: List) { + if (generation != myGeneration || settled[0]) { + return + } + settled[0] = true + // One snapshot is all we want; unsubscribe outside the callback. + mainHandler.post { + try { + currentBrowser.unsubscribe(loadedParentId) + } catch (ignored: Exception) { + } + } + emitBrowseResult(requestId, serializeChildren(children), "") + } + + override fun onError(erroredParentId: String) { + if (generation != myGeneration || settled[0]) { + return + } + settled[0] = true + mainHandler.post { + try { + currentBrowser.unsubscribe(erroredParentId) + } catch (ignored: Exception) { + } + } + emitBrowseResult(requestId, "", "Browse failed") + } + }) + } catch (e: Exception) { + if (!settled[0]) { + settled[0] = true + emitBrowseResult(requestId, "", "Browse failed: " + e.message) + } + } + } + } + + /** Start playback of a browsed item through the connected service's session. */ + fun playFromMediaId(mediaId: String?) { + mainHandler.post { + val currentBrowser = browser + if (currentBrowser == null || !currentBrowser.isConnected) { + return@post + } + try { + val controller = MediaController(appContext, currentBrowser.sessionToken) + controller.transportControls.playFromMediaId(mediaId, null) + lastPlayCommandAtMs = SystemClock.elapsedRealtime() + } catch (e: Exception) { + Log.w(TAG, "playFromMediaId failed", e) + } + } + } + + fun disconnect() { + mainHandler.post { + val sincePlayMs = SystemClock.elapsedRealtime() - lastPlayCommandAtMs + val graceMs = if (lastPlayCommandAtMs == 0L) 0 else PLAY_DISCONNECT_GRACE_MS - sincePlayMs + if (graceMs <= 0) { + closeBrowser() + return@post + } + val lingering = browser + browser = null + generation++ + if (lingering != null) { + mainHandler.postDelayed({ + try { + lingering.disconnect() + } catch (ignored: Exception) { + } + }, graceMs) + } + } + } + + private fun closeBrowser() { + generation++ + val currentBrowser = browser + if (currentBrowser != null) { + try { + currentBrowser.disconnect() + } catch (ignored: Exception) { + } + browser = null + } + } + + private fun serializeChildren(children: List?): String { + val out = JSONArray() + if (children == null) { + return out.toString() + } + for (item in children) { + try { + val title: CharSequence? = if (item.description == null) null else item.description.title + val subtitle: CharSequence? = if (item.description == null) null else item.description.subtitle + val entry = JSONObject() + entry.put("mediaId", if (item.mediaId == null) "" else item.mediaId) + entry.put("title", if (title == null) "" else title.toString()) + entry.put("subtitle", if (subtitle == null) "" else subtitle.toString()) + entry.put("browsable", item.isBrowsable) + entry.put("playable", item.isPlayable) + out.put(entry) + } catch (e: Exception) { + Log.w(TAG, "media item serialization failed", e) + } + } + return out.toString() + } + + private fun emitConnectResult(requestId: Int, connected: Boolean, rootId: String?, error: String?) { + val currentListener = listener + if (currentListener != null) { + currentListener.onConnectResult(requestId, connected, rootId, error) + } + } + + private fun emitBrowseResult(requestId: Int, childrenJson: String?, error: String?) { + val currentListener = listener + if (currentListener != null) { + currentListener.onBrowseResult(requestId, childrenJson, error) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowserListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowserListener.java deleted file mode 100644 index d25c6758..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaBrowserListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawMediaBrowserListener { - void onConnectResult(int requestId, boolean connected, String rootId, String error); - - void onBrowseResult(int requestId, String childrenJson, String error); - - /** The service connection dropped after connecting (player crashed or was killed). */ - void onDisconnected(); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java deleted file mode 100644 index c8775f40..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java +++ /dev/null @@ -1,576 +0,0 @@ -package com.faceclaw.app; - -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.database.ContentObserver; -import android.graphics.Bitmap; -import android.media.AudioManager; -import android.media.MediaDescription; -import android.media.MediaMetadata; -import android.media.session.MediaController; -import android.media.session.MediaSession; -import android.media.session.MediaSessionManager; -import android.media.session.PlaybackState; -import android.os.Handler; -import android.os.Looper; -import android.os.SystemClock; -import android.provider.Settings; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.util.List; -import java.util.HashSet; -import java.util.Set; - -public class FaceclawMediaController { - private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners"; - - private final Context appContext; - private final Object lock = new Object(); - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final ComponentName listenerComponent; - private final MediaSessionManager sessionManager; - private final AudioManager audioManager; - - private final MediaController.Callback controllerCallback = new MediaController.Callback() { - @Override - public void onPlaybackStateChanged(PlaybackState state) { - synchronized (lock) { - emitStateLocked(); - } - } - - @Override - public void onMetadataChanged(MediaMetadata metadata) { - synchronized (lock) { - emitStateLocked(); - } - } - - @Override - public void onQueueChanged(List queue) { - synchronized (lock) { - emitStateLocked(); - } - } - - @Override - public void onSessionDestroyed() { - synchronized (lock) { - refreshActiveControllerLocked(null); - } - } - }; - - private final MediaSessionManager.OnActiveSessionsChangedListener sessionsChangedListener = controllers -> { - synchronized (lock) { - refreshActiveControllerLocked(controllers); - } - }; - - /** - * Fires when the user grants or revokes notification-listener access in - * system settings. Session access depends on that grant, so the sessions - * listener is (re)registered and the state re-emitted whenever it changes; - * otherwise a grant made after start() would never be noticed. - */ - private final ContentObserver notificationAccessObserver = new ContentObserver(mainHandler) { - @Override - public void onChange(boolean selfChange) { - synchronized (lock) { - if (!started) return; - syncSessionsListenerLocked(); - refreshActiveControllerLocked(null); - } - } - }; - - private volatile FaceclawMediaControllerListener listener; - private MediaController activeController; - private boolean started; - private boolean sessionsListenerRegistered; - private Set ignoredPackages = new HashSet<>(); - - public FaceclawMediaController(Context context) { - this.appContext = context.getApplicationContext(); - this.listenerComponent = new ComponentName(appContext, FaceclawMediaNotificationListenerService.class); - this.sessionManager = (MediaSessionManager) appContext.getSystemService(Context.MEDIA_SESSION_SERVICE); - this.audioManager = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE); - } - - public void setListener(FaceclawMediaControllerListener listener) { - synchronized (lock) { - this.listener = listener; - emitStateLocked(); - } - } - - public void start() { - synchronized (lock) { - if (started) { - emitStateLocked(); - return; - } - started = true; - try { - appContext.getContentResolver().registerContentObserver( - Settings.Secure.getUriFor(ENABLED_NOTIFICATION_LISTENERS), - false, - notificationAccessObserver - ); - } catch (Exception e) { - Log.w("FaceclawMedia", "notification access observer registration failed", e); - } - syncSessionsListenerLocked(); - refreshActiveControllerLocked(null); - } - } - - /** - * Keep the active-sessions listener registered exactly while notification - * access is granted. Registration throws SecurityException without the - * grant, so this is retried from the settings observer rather than only - * attempted once at start(). - */ - private void syncSessionsListenerLocked() { - if (sessionManager == null) return; - boolean accessEnabled = started && isNotificationAccessEnabled(); - if (accessEnabled && !sessionsListenerRegistered) { - try { - sessionManager.addOnActiveSessionsChangedListener( - sessionsChangedListener, - listenerComponent, - mainHandler - ); - sessionsListenerRegistered = true; - } catch (SecurityException ignored) { - } - } else if (!accessEnabled && sessionsListenerRegistered) { - try { - sessionManager.removeOnActiveSessionsChangedListener(sessionsChangedListener); - } catch (SecurityException ignored) { - } - sessionsListenerRegistered = false; - } - } - - /** Apply the complete preference set and immediately choose an allowed session. */ - public void setIgnoredPackagesJson(String packagesJson) { - Set packages = new HashSet<>(); - try { - JSONArray array = new JSONArray(packagesJson); - for (int i = 0; i < array.length(); i++) packages.add(array.getString(i)); - } catch (Exception e) { - Log.w("FaceclawMedia", "invalid ignored packages", e); - return; - } - synchronized (lock) { - if (ignoredPackages.equals(packages)) return; - ignoredPackages = packages; - refreshActiveControllerLocked(null); - } - } - - public void stop() { - synchronized (lock) { - if (!started) { - return; - } - started = false; - try { - appContext.getContentResolver().unregisterContentObserver(notificationAccessObserver); - } catch (Exception ignored) { - } - syncSessionsListenerLocked(); - setActiveControllerLocked(null); - emitStateLocked(); - } - } - - public void playPause() { - synchronized (lock) { - if (activeController == null) { - return; - } - PlaybackState playbackState = activeController.getPlaybackState(); - MediaController.TransportControls controls = activeController.getTransportControls(); - if (playbackState == null) { - controls.play(); - return; - } - switch (playbackState.getState()) { - case PlaybackState.STATE_PLAYING: - case PlaybackState.STATE_BUFFERING: - case PlaybackState.STATE_CONNECTING: - controls.pause(); - return; - default: - controls.play(); - } - } - } - - public void skipNext() { - synchronized (lock) { - if (activeController != null) { - activeController.getTransportControls().skipToNext(); - } - } - } - - public void skipPrevious() { - synchronized (lock) { - if (activeController != null) { - activeController.getTransportControls().skipToPrevious(); - } - } - } - - public void skipToQueueItem(long queueId) { - synchronized (lock) { - if (activeController != null) { - activeController.getTransportControls().skipToQueueItem(queueId); - } - } - } - - /** Current phone media-stream volume normalized to the range 0..100. */ - public int getMediaVolumePercent() { - if (audioManager == null) { - return -1; - } - int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); - if (maxVolume <= 0) { - return 0; - } - int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); - return Math.max(0, Math.min(100, Math.round(currentVolume * 100f / maxVolume))); - } - - /** Set the phone media-stream volume from a normalized 0..100 value. */ - public void setMediaVolumePercent(int volumePercent) { - if (audioManager == null) { - return; - } - int clampedPercent = Math.max(0, Math.min(100, volumePercent)); - int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); - int streamVolume = Math.round(clampedPercent * maxVolume / 100f); - audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume, 0); - } - - /** - * Album art for the active session's current item, grayscale, scaled to - * fit within maxSize x maxSize preserving aspect. Returns a gray packet - * (see ImageFileLoader.bitmapToGrayPacket) or an empty array when no art - * is available. gamma and dither are the photographic tone handling - * described on ImageFileLoader.bitmapToGrayPacket; the TS side passes - * its shared photo preset. - */ - public byte[] getAlbumArtGray(int maxSize, float gamma, boolean dither) { - Bitmap art; - synchronized (lock) { - if (activeController == null) { - return new byte[0]; - } - MediaMetadata metadata = activeController.getMetadata(); - if (metadata == null) { - return new byte[0]; - } - art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART); - if (art == null) { - art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART); - } - if (art == null) { - art = metadata.getBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON); - } - } - return ImageFileLoader.bitmapToGrayPacket(art, maxSize, maxSize, gamma, dither); - } - - /** - * The active session's queue (playlist) as JSON: - * [{"id": long, "title": string, "active": bool}, ...]. Empty string when - * the player exposes no queue. - */ - public String getQueueJson() { - synchronized (lock) { - if (activeController == null) { - return ""; - } - List queue = activeController.getQueue(); - if (queue == null || queue.isEmpty()) { - return ""; - } - PlaybackState state = activeController.getPlaybackState(); - long activeId = state == null ? -1 : state.getActiveQueueItemId(); - try { - JSONArray out = new JSONArray(); - for (MediaSession.QueueItem item : queue) { - MediaDescription description = item.getDescription(); - CharSequence title = description == null ? null : description.getTitle(); - JSONObject entry = new JSONObject(); - entry.put("id", item.getQueueId()); - entry.put("title", title == null ? "" : title.toString()); - entry.put("active", item.getQueueId() == activeId); - out.put(entry); - } - return out.toString(); - } catch (Exception e) { - Log.w("FaceclawMedia", "queue serialization failed", e); - return ""; - } - } - } - - public void openNotificationAccessSettings() { - Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - appContext.startActivity(intent); - } - - private void refreshActiveControllerLocked(List controllers) { - if (!started) { - setActiveControllerLocked(null); - emitStateLocked(); - return; - } - if (!isNotificationAccessEnabled()) { - setActiveControllerLocked(null); - emitStateLocked(); - return; - } - if (controllers == null && sessionManager != null) { - try { - controllers = sessionManager.getActiveSessions(listenerComponent); - } catch (SecurityException ignored) { - controllers = null; - } - } - emitSessionAppsLocked(controllers); - setActiveControllerLocked(chooseController(controllers)); - emitStateLocked(); - } - - private MediaController chooseController(List controllers) { - if (controllers == null || controllers.isEmpty()) { - return null; - } - MediaController first = null; - for (MediaController controller : controllers) { - if (ignoredPackages.contains(controller.getPackageName())) continue; - if (first == null) first = controller; - PlaybackState playbackState = controller.getPlaybackState(); - if (playbackState != null && playbackState.getState() == PlaybackState.STATE_PLAYING) { - return controller; - } - } - return first; - } - - private void emitSessionAppsLocked(List controllers) { - FaceclawMediaControllerListener currentListener = listener; - if (currentListener == null || controllers == null) return; - JSONArray apps = new JSONArray(); - Set seen = new HashSet<>(); - for (MediaController controller : controllers) { - String packageName = safe(controller.getPackageName()); - if (packageName.isEmpty() || !seen.add(packageName)) continue; - try { - JSONObject app = new JSONObject(); - app.put("packageName", packageName); - app.put("appName", getApplicationLabel(packageName)); - apps.put(app); - } catch (Exception e) { - Log.w("FaceclawMedia", "session app serialization failed", e); - } - } - String json = apps.toString(); - mainHandler.post(() -> currentListener.onSessionAppsChanged(json)); - } - - private void setActiveControllerLocked(MediaController controller) { - if (sameController(activeController, controller)) { - return; - } - if (activeController != null) { - activeController.unregisterCallback(controllerCallback); - } - activeController = controller; - if (activeController != null) { - activeController.registerCallback(controllerCallback, mainHandler); - } - } - - private boolean sameController(MediaController a, MediaController b) { - if (a == b) { - return true; - } - if (a == null || b == null) { - return false; - } - return a.getSessionToken().equals(b.getSessionToken()); - } - - private boolean isNotificationAccessEnabled() { - String enabledListeners = Settings.Secure.getString( - appContext.getContentResolver(), - ENABLED_NOTIFICATION_LISTENERS - ); - if (enabledListeners == null || enabledListeners.isEmpty()) { - return false; - } - String fullName = listenerComponent.flattenToString(); - String shortName = listenerComponent.flattenToShortString(); - return enabledListeners.contains(fullName) - || enabledListeners.contains(shortName) - || enabledListeners.contains(appContext.getPackageName()); - } - - private void emitStateLocked() { - FaceclawMediaControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - - String playbackState = started ? "idle" : "stopped"; - String packageName = ""; - String appName = ""; - String title = ""; - String artist = ""; - String album = ""; - long positionMs = -1L; - long durationMs = -1L; - float playbackSpeed = 0f; - boolean canPlayPause = false; - boolean canSkipNext = false; - boolean canSkipPrevious = false; - boolean accessEnabled = isNotificationAccessEnabled(); - String status; - - if (!accessEnabled) { - playbackState = "notification-access-required"; - status = "Notification access required."; - } else if (activeController == null) { - status = "No active media session."; - } else { - packageName = safe(activeController.getPackageName()); - appName = getApplicationLabel(packageName); - PlaybackState state = activeController.getPlaybackState(); - MediaMetadata metadata = activeController.getMetadata(); - playbackState = playbackStateName(state); - if (metadata != null) { - title = safe(metadata.getString(MediaMetadata.METADATA_KEY_TITLE)); - artist = safe(metadata.getString(MediaMetadata.METADATA_KEY_ARTIST)); - album = safe(metadata.getString(MediaMetadata.METADATA_KEY_ALBUM)); - durationMs = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION); - if (durationMs <= 0L) { - durationMs = -1L; - } - } - if (state != null) { - positionMs = estimatedPositionMs(state, durationMs); - playbackSpeed = state.getPlaybackSpeed(); - } - long actions = state == null ? 0L : state.getActions(); - canPlayPause = (actions & PlaybackState.ACTION_PLAY_PAUSE) != 0 - || (actions & PlaybackState.ACTION_PLAY) != 0 - || (actions & PlaybackState.ACTION_PAUSE) != 0; - canSkipNext = (actions & PlaybackState.ACTION_SKIP_TO_NEXT) != 0; - canSkipPrevious = (actions & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0; - status = packageName.isEmpty() ? "Active media session." : "Active session: " + packageName; - } - - final String finalPlaybackState = playbackState; - final String finalPackageName = packageName; - final String finalAppName = appName; - final String finalTitle = title; - final String finalArtist = artist; - final String finalAlbum = album; - final long finalPositionMs = positionMs; - final long finalDurationMs = durationMs; - final float finalPlaybackSpeed = playbackSpeed; - final boolean finalCanPlayPause = canPlayPause; - final boolean finalCanSkipNext = canSkipNext; - final boolean finalCanSkipPrevious = canSkipPrevious; - final boolean finalAccessEnabled = accessEnabled; - final String finalStatus = status; - - mainHandler.post(() -> currentListener.onStateChange( - finalPlaybackState, - finalPackageName, - finalAppName, - finalTitle, - finalArtist, - finalAlbum, - finalPositionMs, - finalDurationMs, - finalPlaybackSpeed, - finalCanPlayPause, - finalCanSkipNext, - finalCanSkipPrevious, - finalAccessEnabled, - finalStatus - )); - } - - private String playbackStateName(PlaybackState state) { - if (state == null) { - return "idle"; - } - switch (state.getState()) { - case PlaybackState.STATE_PLAYING: - return "playing"; - case PlaybackState.STATE_PAUSED: - return "paused"; - case PlaybackState.STATE_BUFFERING: - case PlaybackState.STATE_CONNECTING: - return "buffering"; - case PlaybackState.STATE_STOPPED: - return "stopped"; - default: - return "idle"; - } - } - - /** PlaybackState positions are snapshots; advance a playing snapshot to now. */ - private long estimatedPositionMs(PlaybackState state, long durationMs) { - long positionMs = state.getPosition(); - if (positionMs == PlaybackState.PLAYBACK_POSITION_UNKNOWN) { - return -1L; - } - if (state.getState() == PlaybackState.STATE_PLAYING) { - long updatedAtMs = state.getLastPositionUpdateTime(); - if (updatedAtMs > 0L) { - positionMs += (long) ((SystemClock.elapsedRealtime() - updatedAtMs) * state.getPlaybackSpeed()); - } - } - positionMs = Math.max(0L, positionMs); - return durationMs > 0L ? Math.min(positionMs, durationMs) : positionMs; - } - - /** Resolve a media session's package id to the user-facing installed app name. */ - private String getApplicationLabel(String packageName) { - if (packageName == null || packageName.isEmpty()) { - return ""; - } - PackageManager packageManager = appContext.getPackageManager(); - try { - ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0); - CharSequence label = packageManager.getApplicationLabel(applicationInfo); - if (label != null && label.length() > 0) { - return label.toString(); - } - } catch (PackageManager.NameNotFoundException ignored) { - } - return packageName; - } - - private String safe(String value) { - return value == null ? "" : value; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.kt new file mode 100644 index 00000000..b7f8ca5a --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.kt @@ -0,0 +1,577 @@ +package com.faceclaw.app + +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.pm.ApplicationInfo +import android.content.pm.PackageManager +import android.database.ContentObserver +import android.graphics.Bitmap +import android.media.AudioManager +import android.media.MediaDescription +import android.media.MediaMetadata +import android.media.session.MediaController +import android.media.session.MediaSession +import android.media.session.MediaSessionManager +import android.media.session.PlaybackState +import android.os.Handler +import android.os.Looper +import android.os.SystemClock +import android.provider.Settings +import android.util.Log + +import org.json.JSONArray +import org.json.JSONObject + +import java.util.HashSet + +class FaceclawMediaController(context: Context) { + companion object { + private const val ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners" + } + + private val appContext: Context = context.applicationContext + private val lock = Any() + private val mainHandler = Handler(Looper.getMainLooper()) + private val listenerComponent: ComponentName + private val sessionManager: MediaSessionManager? + private val audioManager: AudioManager? + + private val controllerCallback: MediaController.Callback = object : MediaController.Callback() { + override fun onPlaybackStateChanged(state: PlaybackState?) { + synchronized(lock) { + emitStateLocked() + } + } + + override fun onMetadataChanged(metadata: MediaMetadata?) { + synchronized(lock) { + emitStateLocked() + } + } + + override fun onQueueChanged(queue: MutableList?) { + synchronized(lock) { + emitStateLocked() + } + } + + override fun onSessionDestroyed() { + synchronized(lock) { + refreshActiveControllerLocked(null) + } + } + } + + private val sessionsChangedListener = MediaSessionManager.OnActiveSessionsChangedListener { controllers -> + synchronized(lock) { + refreshActiveControllerLocked(controllers) + } + } + + /** + * Fires when the user grants or revokes notification-listener access in + * system settings. Session access depends on that grant, so the sessions + * listener is (re)registered and the state re-emitted whenever it changes; + * otherwise a grant made after start() would never be noticed. + */ + private val notificationAccessObserver: ContentObserver = object : ContentObserver(mainHandler) { + override fun onChange(selfChange: Boolean) { + synchronized(lock) { + if (!started) return + syncSessionsListenerLocked() + refreshActiveControllerLocked(null) + } + } + } + + @Volatile + private var listener: FaceclawMediaControllerListener? = null + private var activeController: MediaController? = null + private var started = false + private var sessionsListenerRegistered = false + private var ignoredPackages: Set = HashSet() + + init { + this.listenerComponent = ComponentName(appContext, FaceclawMediaNotificationListenerService::class.java) + this.sessionManager = appContext.getSystemService(Context.MEDIA_SESSION_SERVICE) as MediaSessionManager? + this.audioManager = appContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager? + } + + fun setListener(listener: FaceclawMediaControllerListener?) { + synchronized(lock) { + this.listener = listener + emitStateLocked() + } + } + + fun start() { + synchronized(lock) { + if (started) { + emitStateLocked() + return + } + started = true + try { + appContext.contentResolver.registerContentObserver( + Settings.Secure.getUriFor(ENABLED_NOTIFICATION_LISTENERS), + false, + notificationAccessObserver + ) + } catch (e: Exception) { + Log.w("FaceclawMedia", "notification access observer registration failed", e) + } + syncSessionsListenerLocked() + refreshActiveControllerLocked(null) + } + } + + /** + * Keep the active-sessions listener registered exactly while notification + * access is granted. Registration throws SecurityException without the + * grant, so this is retried from the settings observer rather than only + * attempted once at start(). + */ + private fun syncSessionsListenerLocked() { + if (sessionManager == null) return + val accessEnabled = started && isNotificationAccessEnabled() + if (accessEnabled && !sessionsListenerRegistered) { + try { + sessionManager.addOnActiveSessionsChangedListener( + sessionsChangedListener, + listenerComponent, + mainHandler + ) + sessionsListenerRegistered = true + } catch (ignored: SecurityException) { + } + } else if (!accessEnabled && sessionsListenerRegistered) { + try { + sessionManager.removeOnActiveSessionsChangedListener(sessionsChangedListener) + } catch (ignored: SecurityException) { + } + sessionsListenerRegistered = false + } + } + + /** Apply the complete preference set and immediately choose an allowed session. */ + fun setIgnoredPackagesJson(packagesJson: String?) { + val packages: MutableSet = HashSet() + try { + val array = JSONArray(packagesJson) + for (i in 0 until array.length()) packages.add(array.getString(i)) + } catch (e: Exception) { + Log.w("FaceclawMedia", "invalid ignored packages", e) + return + } + synchronized(lock) { + if (ignoredPackages == packages) return + ignoredPackages = packages + refreshActiveControllerLocked(null) + } + } + + fun stop() { + synchronized(lock) { + if (!started) { + return + } + started = false + try { + appContext.contentResolver.unregisterContentObserver(notificationAccessObserver) + } catch (ignored: Exception) { + } + syncSessionsListenerLocked() + setActiveControllerLocked(null) + emitStateLocked() + } + } + + fun playPause() { + synchronized(lock) { + val controller = activeController + if (controller == null) { + return + } + val playbackState: PlaybackState? = controller.playbackState + val controls: MediaController.TransportControls = controller.transportControls + if (playbackState == null) { + controls.play() + return + } + when (playbackState.state) { + PlaybackState.STATE_PLAYING, + PlaybackState.STATE_BUFFERING, + PlaybackState.STATE_CONNECTING -> { + controls.pause() + return + } + else -> controls.play() + } + } + } + + fun skipNext() { + synchronized(lock) { + val controller = activeController + if (controller != null) { + controller.transportControls.skipToNext() + } + } + } + + fun skipPrevious() { + synchronized(lock) { + val controller = activeController + if (controller != null) { + controller.transportControls.skipToPrevious() + } + } + } + + fun skipToQueueItem(queueId: Long) { + synchronized(lock) { + val controller = activeController + if (controller != null) { + controller.transportControls.skipToQueueItem(queueId) + } + } + } + + /** Current phone media-stream volume normalized to the range 0..100. */ + fun getMediaVolumePercent(): Int { + if (audioManager == null) { + return -1 + } + val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) + if (maxVolume <= 0) { + return 0 + } + val currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) + return Math.max(0, Math.min(100, Math.round(currentVolume * 100f / maxVolume))) + } + + /** Set the phone media-stream volume from a normalized 0..100 value. */ + fun setMediaVolumePercent(volumePercent: Int) { + if (audioManager == null) { + return + } + val clampedPercent = Math.max(0, Math.min(100, volumePercent)) + val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) + val streamVolume = Math.round(clampedPercent * maxVolume / 100f) + audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume, 0) + } + + /** + * Album art for the active session's current item, grayscale, scaled to + * fit within maxSize x maxSize preserving aspect. Returns a gray packet + * (see ImageFileLoader.bitmapToGrayPacket) or an empty array when no art + * is available. gamma and dither are the photographic tone handling + * described on ImageFileLoader.bitmapToGrayPacket; the TS side passes + * its shared photo preset. + */ + fun getAlbumArtGray(maxSize: Int, gamma: Float, dither: Boolean): ByteArray { + var art: Bitmap? + synchronized(lock) { + val controller = activeController + if (controller == null) { + return ByteArray(0) + } + val metadata: MediaMetadata? = controller.metadata + if (metadata == null) { + return ByteArray(0) + } + art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART) + if (art == null) { + art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART) + } + if (art == null) { + art = metadata.getBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON) + } + } + return ImageFileLoader.bitmapToGrayPacket(art, maxSize, maxSize, gamma, dither) + } + + /** + * The active session's queue (playlist) as JSON: + * [{"id": long, "title": string, "active": bool}, ...]. Empty string when + * the player exposes no queue. + */ + fun getQueueJson(): String { + synchronized(lock) { + val controller = activeController + if (controller == null) { + return "" + } + val queue: List? = controller.queue + if (queue == null || queue.isEmpty()) { + return "" + } + val state: PlaybackState? = controller.playbackState + val activeId = if (state == null) -1L else state.activeQueueItemId + try { + val out = JSONArray() + for (item in queue) { + val description: MediaDescription? = item.description + val title: CharSequence? = if (description == null) null else description.title + val entry = JSONObject() + entry.put("id", item.queueId) + entry.put("title", if (title == null) "" else title.toString()) + entry.put("active", item.queueId == activeId) + out.put(entry) + } + return out.toString() + } catch (e: Exception) { + Log.w("FaceclawMedia", "queue serialization failed", e) + return "" + } + } + } + + fun openNotificationAccessSettings() { + val intent = Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + appContext.startActivity(intent) + } + + private fun refreshActiveControllerLocked(controllersIn: List?) { + var controllers = controllersIn + if (!started) { + setActiveControllerLocked(null) + emitStateLocked() + return + } + if (!isNotificationAccessEnabled()) { + setActiveControllerLocked(null) + emitStateLocked() + return + } + if (controllers == null && sessionManager != null) { + try { + controllers = sessionManager.getActiveSessions(listenerComponent) + } catch (ignored: SecurityException) { + controllers = null + } + } + emitSessionAppsLocked(controllers) + setActiveControllerLocked(chooseController(controllers)) + emitStateLocked() + } + + private fun chooseController(controllers: List?): MediaController? { + if (controllers == null || controllers.isEmpty()) { + return null + } + var first: MediaController? = null + for (controller in controllers) { + if (ignoredPackages.contains(controller.packageName)) continue + if (first == null) first = controller + val playbackState: PlaybackState? = controller.playbackState + if (playbackState != null && playbackState.state == PlaybackState.STATE_PLAYING) { + return controller + } + } + return first + } + + private fun emitSessionAppsLocked(controllers: List?) { + val currentListener = listener + if (currentListener == null || controllers == null) return + val apps = JSONArray() + val seen: MutableSet = HashSet() + for (controller in controllers) { + val packageName = safe(controller.packageName) + if (packageName.isEmpty() || !seen.add(packageName)) continue + try { + val app = JSONObject() + app.put("packageName", packageName) + app.put("appName", getApplicationLabel(packageName)) + apps.put(app) + } catch (e: Exception) { + Log.w("FaceclawMedia", "session app serialization failed", e) + } + } + val json = apps.toString() + mainHandler.post { currentListener.onSessionAppsChanged(json) } + } + + private fun setActiveControllerLocked(controller: MediaController?) { + if (sameController(activeController, controller)) { + return + } + val previous = activeController + if (previous != null) { + previous.unregisterCallback(controllerCallback) + } + activeController = controller + if (controller != null) { + controller.registerCallback(controllerCallback, mainHandler) + } + } + + private fun sameController(a: MediaController?, b: MediaController?): Boolean { + if (a === b) { + return true + } + if (a == null || b == null) { + return false + } + return a.sessionToken == b.sessionToken + } + + private fun isNotificationAccessEnabled(): Boolean { + val enabledListeners: String? = Settings.Secure.getString( + appContext.contentResolver, + ENABLED_NOTIFICATION_LISTENERS + ) + if (enabledListeners == null || enabledListeners.isEmpty()) { + return false + } + val fullName = listenerComponent.flattenToString() + val shortName = listenerComponent.flattenToShortString() + return enabledListeners.contains(fullName) + || enabledListeners.contains(shortName) + || enabledListeners.contains(appContext.packageName) + } + + private fun emitStateLocked() { + val currentListener = listener + if (currentListener == null) { + return + } + + var playbackState = if (started) "idle" else "stopped" + var packageName = "" + var appName = "" + var title = "" + var artist = "" + var album = "" + var positionMs = -1L + var durationMs = -1L + var playbackSpeed = 0f + var canPlayPause = false + var canSkipNext = false + var canSkipPrevious = false + val accessEnabled = isNotificationAccessEnabled() + val status: String + + val controller = activeController + if (!accessEnabled) { + playbackState = "notification-access-required" + status = "Notification access required." + } else if (controller == null) { + status = "No active media session." + } else { + packageName = safe(controller.packageName) + appName = getApplicationLabel(packageName) + val state: PlaybackState? = controller.playbackState + val metadata: MediaMetadata? = controller.metadata + playbackState = playbackStateName(state) + if (metadata != null) { + title = safe(metadata.getString(MediaMetadata.METADATA_KEY_TITLE)) + artist = safe(metadata.getString(MediaMetadata.METADATA_KEY_ARTIST)) + album = safe(metadata.getString(MediaMetadata.METADATA_KEY_ALBUM)) + durationMs = metadata.getLong(MediaMetadata.METADATA_KEY_DURATION) + if (durationMs <= 0L) { + durationMs = -1L + } + } + if (state != null) { + positionMs = estimatedPositionMs(state, durationMs) + playbackSpeed = state.playbackSpeed + } + val actions = if (state == null) 0L else state.actions + canPlayPause = (actions and PlaybackState.ACTION_PLAY_PAUSE) != 0L + || (actions and PlaybackState.ACTION_PLAY) != 0L + || (actions and PlaybackState.ACTION_PAUSE) != 0L + canSkipNext = (actions and PlaybackState.ACTION_SKIP_TO_NEXT) != 0L + canSkipPrevious = (actions and PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0L + status = if (packageName.isEmpty()) "Active media session." else "Active session: $packageName" + } + + val finalPlaybackState = playbackState + val finalPackageName = packageName + val finalAppName = appName + val finalTitle = title + val finalArtist = artist + val finalAlbum = album + val finalPositionMs = positionMs + val finalDurationMs = durationMs + val finalPlaybackSpeed = playbackSpeed + val finalCanPlayPause = canPlayPause + val finalCanSkipNext = canSkipNext + val finalCanSkipPrevious = canSkipPrevious + val finalAccessEnabled = accessEnabled + val finalStatus = status + + mainHandler.post { + currentListener.onStateChange( + finalPlaybackState, + finalPackageName, + finalAppName, + finalTitle, + finalArtist, + finalAlbum, + finalPositionMs, + finalDurationMs, + finalPlaybackSpeed, + finalCanPlayPause, + finalCanSkipNext, + finalCanSkipPrevious, + finalAccessEnabled, + finalStatus + ) + } + } + + private fun playbackStateName(state: PlaybackState?): String { + if (state == null) { + return "idle" + } + return when (state.state) { + PlaybackState.STATE_PLAYING -> "playing" + PlaybackState.STATE_PAUSED -> "paused" + PlaybackState.STATE_BUFFERING, + PlaybackState.STATE_CONNECTING -> "buffering" + PlaybackState.STATE_STOPPED -> "stopped" + else -> "idle" + } + } + + /** PlaybackState positions are snapshots; advance a playing snapshot to now. */ + private fun estimatedPositionMs(state: PlaybackState, durationMs: Long): Long { + var positionMs = state.position + if (positionMs == PlaybackState.PLAYBACK_POSITION_UNKNOWN) { + return -1L + } + if (state.state == PlaybackState.STATE_PLAYING) { + val updatedAtMs = state.lastPositionUpdateTime + if (updatedAtMs > 0L) { + positionMs += ((SystemClock.elapsedRealtime() - updatedAtMs) * state.playbackSpeed).toLong() + } + } + positionMs = Math.max(0L, positionMs) + return if (durationMs > 0L) Math.min(positionMs, durationMs) else positionMs + } + + /** Resolve a media session's package id to the user-facing installed app name. */ + private fun getApplicationLabel(packageName: String?): String { + if (packageName == null || packageName.isEmpty()) { + return "" + } + val packageManager = appContext.packageManager + try { + val applicationInfo: ApplicationInfo = packageManager.getApplicationInfo(packageName, 0) + val label: CharSequence? = packageManager.getApplicationLabel(applicationInfo) + if (label != null && label.length > 0) { + return label.toString() + } + } catch (ignored: PackageManager.NameNotFoundException) { + } + return packageName + } + + private fun safe(value: String?): String { + return value ?: "" + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaControllerListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaControllerListener.java deleted file mode 100644 index 75909f0e..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaControllerListener.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawMediaControllerListener { - /** All observed session owners, before filtering or choosing the active player. */ - void onSessionAppsChanged(String appsJson); - - void onStateChange( - String playbackState, - String packageName, - String appName, - String title, - String artist, - String album, - long positionMs, - long durationMs, - float playbackSpeed, - boolean canPlayPause, - boolean canSkipNext, - boolean canSkipPrevious, - boolean accessEnabled, - String status - ); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java deleted file mode 100644 index 8ca97f00..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.java +++ /dev/null @@ -1,657 +0,0 @@ -package com.faceclaw.app; - -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.graphics.drawable.Icon; -import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.service.notification.NotificationListenerService; -import android.service.notification.StatusBarNotification; -import android.util.Log; - -import java.io.ByteArrayOutputStream; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.CopyOnWriteArraySet; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -public class FaceclawMediaNotificationListenerService extends NotificationListenerService { - private static final String TAG = "FaceclawNotify"; - private static final double NOTIFICATION_ICON_GAMMA = 1.6; - private static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName"; - - private static volatile FaceclawMediaNotificationListenerService activeService; - private static final Handler mainHandler = new Handler(Looper.getMainLooper()); - private static final Set notificationListeners = new CopyOnWriteArraySet<>(); - private static final Set activeNotificationWakeKeys = new HashSet<>(); - - @Override - public void onCreate() { - super.onCreate(); - activeService = this; - } - - @Override - public void onDestroy() { - if (activeService == this) { - activeService = null; - } - super.onDestroy(); - } - - @Override - public void onListenerConnected() { - activeService = this; - super.onListenerConnected(); - refreshActiveNotificationWakeKeys(this); - } - - @Override - public void onListenerDisconnected() { - if (activeService == this) { - activeService = null; - } - super.onListenerDisconnected(); - } - - @Override - public void onNotificationPosted(StatusBarNotification statusBarNotification) { - super.onNotificationPosted(statusBarNotification); - if (!shouldShowNotificationInList(this, statusBarNotification)) { - forgetActiveNotificationWakeKey(statusBarNotification); - return; - } - if (shouldEmitNotificationPosted(statusBarNotification)) { - emitNotificationPosted(statusBarNotification.getKey()); - } - } - - @Override - public void onNotificationRemoved(StatusBarNotification statusBarNotification) { - forgetActiveNotificationWakeKey(statusBarNotification); - super.onNotificationRemoved(statusBarNotification); - } - - public static void addNotificationListener(FaceclawNotificationListener listener) { - if (listener != null) { - notificationListeners.add(listener); - } - } - - public static void removeNotificationListener(FaceclawNotificationListener listener) { - if (listener != null) { - notificationListeners.remove(listener); - } - } - - public static boolean hasActiveNotificationTitle(String expectedTitle) { - FaceclawMediaNotificationListenerService service = activeService; - if (service == null || expectedTitle == null || expectedTitle.isEmpty()) { - return false; - } - StatusBarNotification[] notifications; - try { - notifications = service.getActiveNotifications(); - } catch (SecurityException e) { - Log.w(TAG, "notification access denied while checking active notifications", e); - return false; - } catch (Throwable t) { - Log.w(TAG, "failed to check active notifications", t); - return false; - } - if (notifications == null || notifications.length == 0) { - return false; - } - for (StatusBarNotification notification : notifications) { - if (notification == null || notification.getNotification() == null) { - continue; - } - Bundle extras = notification.getNotification().extras; - if (extras == null) { - continue; - } - CharSequence title = extras.getCharSequence(Notification.EXTRA_TITLE); - if (title != null && expectedTitle.contentEquals(title)) { - return true; - } - } - return false; - } - - public static byte[] getActiveNotificationIconGrays(int iconSize, int maxIcons) { - FaceclawMediaNotificationListenerService service = activeService; - int size = Math.max(1, Math.min(96, iconSize)); - int limit = Math.max(0, maxIcons); - if (service == null || limit == 0) { - return new byte[0]; - } - - StatusBarNotification[] notifications; - try { - notifications = service.getActiveNotifications(); - } catch (SecurityException e) { - Log.w(TAG, "notification access denied while reading icons", e); - return new byte[0]; - } catch (Throwable t) { - Log.w(TAG, "failed to read notification icons", t); - return new byte[0]; - } - if (notifications == null || notifications.length == 0) { - return new byte[0]; - } - - ByteArrayOutputStream out = new ByteArrayOutputStream(size * size * Math.min(limit, notifications.length)); - Set emittedGroupKeys = new HashSet<>(); - int emitted = 0; - for (StatusBarNotification statusBarNotification : notifications) { - if (!shouldShowNotificationIcon(service, statusBarNotification)) { - continue; - } - String dedupeGroupKey = getNotificationDedupeGroupKey(statusBarNotification); - if (dedupeGroupKey != null && emittedGroupKeys.contains(dedupeGroupKey)) { - continue; - } - Drawable drawable = loadNotificationIcon(service, statusBarNotification.getNotification()); - if (drawable == null) { - Log.i(TAG, "icon skipped (no drawable): " + statusBarNotification.getPackageName()); - continue; - } - Log.i(TAG, "icon[" + emitted + "] pkg=" + statusBarNotification.getPackageName() - + " drawable=" + drawable.getClass().getSimpleName() - + " intrinsic=" + drawable.getIntrinsicWidth() + "x" + drawable.getIntrinsicHeight()); - appendIconGrayBytes(drawable, size, out, service, emitted, statusBarNotification.getPackageName()); - if (dedupeGroupKey != null) { - emittedGroupKeys.add(dedupeGroupKey); - } - emitted += 1; - if (emitted >= limit) { - break; - } - } - return out.toByteArray(); - } - - /** - * Grayscale icon (iconSize*iconSize bytes) for one active notification, - * identified by its key; empty array when the notification or its icon is - * unavailable. Shares the extraction/scaling pipeline with the tray icon - * strip above. - */ - public static byte[] getNotificationIconGrayForKey(String key, int iconSize) { - FaceclawMediaNotificationListenerService service = activeService; - int size = Math.max(1, Math.min(96, iconSize)); - if (service == null) { - return new byte[0]; - } - StatusBarNotification statusBarNotification = findActiveNotificationByKey(service, key); - if (statusBarNotification == null || statusBarNotification.getNotification() == null) { - return new byte[0]; - } - Drawable drawable = loadNotificationIcon(service, statusBarNotification.getNotification()); - if (drawable == null) { - return new byte[0]; - } - ByteArrayOutputStream out = new ByteArrayOutputStream(size * size); - appendIconGrayBytes(drawable, size, out, service, -1, statusBarNotification.getPackageName()); - return out.toByteArray(); - } - - public static String getActiveNotificationsJson(int maxNotifications) { - FaceclawMediaNotificationListenerService service = activeService; - int limit = Math.max(0, maxNotifications); - if (service == null || limit == 0) { - return "[]"; - } - - StatusBarNotification[] notifications; - try { - notifications = service.getActiveNotifications(); - } catch (SecurityException e) { - Log.w(TAG, "notification access denied while reading notifications", e); - return "[]"; - } catch (Throwable t) { - Log.w(TAG, "failed to read notifications", t); - return "[]"; - } - if (notifications == null || notifications.length == 0) { - return "[]"; - } - - Arrays.sort(notifications, new Comparator() { - @Override - public int compare(StatusBarNotification a, StatusBarNotification b) { - long left = a == null ? 0 : a.getPostTime(); - long right = b == null ? 0 : b.getPostTime(); - return Long.compare(right, left); - } - }); - - JSONArray out = new JSONArray(); - for (StatusBarNotification statusBarNotification : notifications) { - if (out.length() >= limit) { - break; - } - if (!shouldShowNotificationInList(service, statusBarNotification)) { - continue; - } - try { - out.put(buildNotificationJson(service, statusBarNotification)); - } catch (Throwable t) { - Log.w(TAG, "failed to serialize notification", t); - } - } - return out.toString(); - } - - public static boolean invokeNotificationAction(String key, int actionIndex) { - FaceclawMediaNotificationListenerService service = activeService; - StatusBarNotification statusBarNotification = findActiveNotificationByKey(service, key); - if (statusBarNotification == null || statusBarNotification.getNotification() == null) { - return false; - } - Notification.Action[] actions = statusBarNotification.getNotification().actions; - if (actions == null || actionIndex < 0 || actionIndex >= actions.length) { - return false; - } - PendingIntent intent = actions[actionIndex].actionIntent; - if (intent == null) { - return false; - } - try { - intent.send(); - return true; - } catch (PendingIntent.CanceledException e) { - Log.w(TAG, "notification action pending intent was canceled", e); - return false; - } catch (Throwable t) { - Log.w(TAG, "failed to invoke notification action", t); - return false; - } - } - - public static boolean dismissNotification(String key) { - FaceclawMediaNotificationListenerService service = activeService; - if (service == null || key == null || key.isEmpty()) { - return false; - } - try { - service.cancelNotification(key); - return true; - } catch (SecurityException e) { - Log.w(TAG, "notification access denied while dismissing notification", e); - return false; - } catch (Throwable t) { - Log.w(TAG, "failed to dismiss notification", t); - return false; - } - } - - private static void emitNotificationPosted(String key) { - if (key == null || key.isEmpty() || notificationListeners.isEmpty()) { - return; - } - for (FaceclawNotificationListener listener : notificationListeners) { - mainHandler.post(() -> { - try { - listener.onNotificationPosted(key); - } catch (Throwable t) { - Log.w(TAG, "notification listener failed", t); - } - }); - } - } - - private static void refreshActiveNotificationWakeKeys(FaceclawMediaNotificationListenerService service) { - StatusBarNotification[] notifications; - try { - notifications = service.getActiveNotifications(); - } catch (Throwable t) { - Log.w(TAG, "failed to refresh active notification wake keys", t); - return; - } - synchronized (activeNotificationWakeKeys) { - activeNotificationWakeKeys.clear(); - if (notifications == null) { - return; - } - for (StatusBarNotification statusBarNotification : notifications) { - if (shouldShowNotificationInList(service, statusBarNotification)) { - String key = statusBarNotification.getKey(); - if (key != null && !key.isEmpty()) { - activeNotificationWakeKeys.add(key); - } - } - } - } - } - - private static boolean shouldEmitNotificationPosted(StatusBarNotification statusBarNotification) { - String key = statusBarNotification.getKey(); - if (key == null || key.isEmpty()) { - return true; - } - - boolean alreadyActive; - synchronized (activeNotificationWakeKeys) { - alreadyActive = activeNotificationWakeKeys.contains(key); - activeNotificationWakeKeys.add(key); - } - return !alreadyActive || !isPersistentNotification(statusBarNotification); - } - - private static void forgetActiveNotificationWakeKey(StatusBarNotification statusBarNotification) { - if (statusBarNotification == null) { - return; - } - String key = statusBarNotification.getKey(); - if (key == null || key.isEmpty()) { - return; - } - synchronized (activeNotificationWakeKeys) { - activeNotificationWakeKeys.remove(key); - } - } - - private static boolean isPersistentNotification(StatusBarNotification statusBarNotification) { - Notification notification = statusBarNotification.getNotification(); - if (notification == null) { - return false; - } - int persistentFlags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; - return (notification.flags & persistentFlags) != 0; - } - - private static boolean shouldShowNotificationIcon(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) { - if (!shouldShowNotificationInList(service, statusBarNotification)) { - return false; - } - Notification notification = statusBarNotification.getNotification(); - if ((notification.flags & Notification.FLAG_GROUP_SUMMARY) != 0) { - return false; - } - return true; - } - - private static boolean shouldShowNotificationInList(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) { - if (statusBarNotification == null || statusBarNotification.getNotification() == null) { - return false; - } - // Our own notifications stay out of the mirror: the foreground-service - // one is noise, and the Timers app rings on the glasses itself (its - // phone notification is for the phone), so mirroring it would stack a - // notification modal over the ringing screen. - if (service.getPackageName().equals(statusBarNotification.getPackageName())) { - return false; - } - Notification notification = statusBarNotification.getNotification(); - if (Notification.CATEGORY_TRANSPORT.equals(notification.category)) { - return false; - } - Bundle extras = notification.extras; - if (extras != null && extras.containsKey("android.mediaSession")) { - return false; - } - - NotificationListenerService.RankingMap rankingMap = service.getCurrentRanking(); - if (rankingMap == null) { - return true; - } - NotificationListenerService.Ranking ranking = new NotificationListenerService.Ranking(); - if (!rankingMap.getRanking(statusBarNotification.getKey(), ranking)) { - return true; - } - int importance = ranking.getImportance(); - return importance > NotificationManager.IMPORTANCE_MIN; - } - - private static String getNotificationDedupeGroupKey(StatusBarNotification statusBarNotification) { - Notification notification = statusBarNotification.getNotification(); - if (notification.getGroup() == null && statusBarNotification.getOverrideGroupKey() == null) { - return null; - } - String groupKey = statusBarNotification.getGroupKey(); - if (groupKey == null || groupKey.isEmpty()) { - return null; - } - // Group children often share the same small icon. Emit only one icon for the group. - return groupKey; - } - - private static Drawable loadNotificationIcon(FaceclawMediaNotificationListenerService service, Notification notification) { - try { - Icon smallIcon = notification.getSmallIcon(); - if (smallIcon != null) { - Drawable drawable = smallIcon.loadDrawable(service); - if (drawable != null) { - // Status-bar small icons are alpha templates: the platform - // draws them tinted and ignores their color channels, which - // apps may fill with garbage (Discord ships noise there). - // Tint white so the shape comes from alpha alone, matching - // how the status bar renders them. - drawable.mutate(); - drawable.setTint(Color.WHITE); - if (drawable instanceof BitmapDrawable) { - ((BitmapDrawable) drawable).setFilterBitmap(true); - } - return drawable; - } - } - } catch (Throwable t) { - Log.w(TAG, "failed to load small notification icon", t); - } - try { - // Large icons are real color images (avatars, album art); keep color. - Icon largeIcon = notification.getLargeIcon(); - if (largeIcon != null) { - return largeIcon.loadDrawable(service); - } - } catch (Throwable t) { - Log.w(TAG, "failed to load large notification icon", t); - } - return null; - } - - private static void appendIconGrayBytes(Drawable drawable, int size, ByteArrayOutputStream out, - FaceclawMediaNotificationListenerService service, int index, String packageName) { - Bitmap bitmap = renderIconScaled(drawable, size); - dumpIconDebugPng(service, index, packageName, bitmap); - for (int y = 0; y < size; y++) { - for (int x = 0; x < size; x++) { - int color = bitmap.getPixel(x, y); - int alpha = Color.alpha(color); - double grayLinear = (0.2126 * Color.red(color) + 0.7152 * Color.green(color) + 0.0722 * Color.blue(color)) * alpha / (255.0 * 255.0); - int gray = (int) Math.round(255.0 * Math.pow(Math.max(0.0, Math.min(1.0, grayLinear)), NOTIFICATION_ICON_GAMMA)); - out.write(gray & 0xff); - } - } - bitmap.recycle(); - } - - /** - * Render a drawable at the target size with proper downscaling. Detailed - * sources (e.g. avatar bitmaps used as notification icons) are rendered at - * native resolution and reduced by repeated halving: a single filtered pass - * from, say, 126px to 24px samples too sparsely and turns fine detail into - * speckle that reads as a garbled icon. - */ - private static Bitmap renderIconScaled(Drawable drawable, int size) { - int renderW = Math.max(size, drawable.getIntrinsicWidth()); - int renderH = Math.max(size, drawable.getIntrinsicHeight()); - Bitmap bitmap = Bitmap.createBitmap(renderW, renderH, Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(bitmap); - drawable.setBounds(0, 0, renderW, renderH); - drawable.draw(canvas); - while (bitmap.getWidth() >= size * 2 && bitmap.getHeight() >= size * 2) { - Bitmap halved = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / 2, bitmap.getHeight() / 2, true); - bitmap.recycle(); - bitmap = halved; - } - if (bitmap.getWidth() != size || bitmap.getHeight() != size) { - Bitmap scaled = Bitmap.createScaledBitmap(bitmap, size, size, true); - bitmap.recycle(); - bitmap = scaled; - } - return bitmap; - } - - /** - * Debug aid for garbled-icon reports: saves each rendered icon to - * /debug-icons/ (adb-pullable) so extraction problems can - * be told apart from downstream compositing/transmission problems. Cheap: - * runs at most once per icon-cache refresh on tiny bitmaps. - */ - private static void dumpIconDebugPng(FaceclawMediaNotificationListenerService service, int index, String packageName, Bitmap bitmap) { - if (index < 0) { - return; - } - try { - java.io.File dir = new java.io.File(service.getExternalFilesDir(null), "debug-icons"); - if (!dir.exists() && !dir.mkdirs()) { - return; - } - String safeName = packageName == null ? "unknown" : packageName.replaceAll("[^A-Za-z0-9._-]", "_"); - java.io.File file = new java.io.File(dir, "icon-" + index + "-" + safeName + ".png"); - try (java.io.FileOutputStream stream = new java.io.FileOutputStream(file)) { - bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); - } - } catch (Throwable t) { - Log.w(TAG, "failed to dump debug icon", t); - } - } - - private static StatusBarNotification findActiveNotificationByKey(FaceclawMediaNotificationListenerService service, String key) { - if (service == null || key == null || key.isEmpty()) { - return null; - } - StatusBarNotification[] notifications; - try { - notifications = service.getActiveNotifications(); - } catch (Throwable t) { - Log.w(TAG, "failed to find active notification", t); - return null; - } - if (notifications == null) { - return null; - } - for (StatusBarNotification statusBarNotification : notifications) { - if (statusBarNotification != null && key.equals(statusBarNotification.getKey())) { - return statusBarNotification; - } - } - return null; - } - - private static JSONObject buildNotificationJson(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) - throws JSONException { - Notification notification = statusBarNotification.getNotification(); - Bundle extras = notification.extras; - JSONObject out = new JSONObject(); - out.put("key", statusBarNotification.getKey()); - out.put("packageName", statusBarNotification.getPackageName()); - out.put("appName", getNotificationAppName(service, statusBarNotification)); - out.put("postTime", statusBarNotification.getPostTime()); - out.put("when", notification.when); - putString(out, "category", notification.category); - if (extras != null) { - putCharSequence(out, "title", firstNonEmpty( - extras.getCharSequence(Notification.EXTRA_TITLE_BIG), - extras.getCharSequence(Notification.EXTRA_TITLE) - )); - putCharSequence(out, "text", extras.getCharSequence(Notification.EXTRA_TEXT)); - putCharSequence(out, "bigText", extras.getCharSequence(Notification.EXTRA_BIG_TEXT)); - putCharSequence(out, "subText", extras.getCharSequence(Notification.EXTRA_SUB_TEXT)); - putCharSequence(out, "infoText", extras.getCharSequence(Notification.EXTRA_INFO_TEXT)); - putCharSequence(out, "summaryText", extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT)); - CharSequence[] textLines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES); - JSONArray lines = new JSONArray(); - if (textLines != null) { - for (CharSequence line : textLines) { - String text = charSequenceToString(line); - if (!text.isEmpty()) { - lines.put(text); - } - } - } - out.put("lines", lines); - } else { - out.put("lines", new JSONArray()); - } - - JSONArray actionsJson = new JSONArray(); - Notification.Action[] actions = notification.actions; - if (actions != null) { - for (int index = 0; index < actions.length; index++) { - Notification.Action action = actions[index]; - if (action == null) { - continue; - } - String title = charSequenceToString(action.title); - if (title.isEmpty()) { - continue; - } - JSONObject actionJson = new JSONObject(); - actionJson.put("index", index); - actionJson.put("title", title); - actionJson.put("enabled", action.actionIntent != null); - actionsJson.put(actionJson); - } - } - out.put("actions", actionsJson); - return out; - } - - private static String getNotificationAppName(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) { - Notification notification = statusBarNotification.getNotification(); - Bundle extras = notification.extras; - if (extras != null) { - String substituteName = charSequenceToString(extras.getCharSequence(EXTRA_SUBSTITUTE_APP_NAME)); - if (!substituteName.isEmpty()) { - return substituteName; - } - } - return getAppLabel(service, statusBarNotification.getPackageName()); - } - - private static String getAppLabel(FaceclawMediaNotificationListenerService service, String packageName) { - if (packageName == null || packageName.isEmpty()) { - return ""; - } - try { - CharSequence label = service - .getPackageManager() - .getApplicationLabel(service.getPackageManager().getApplicationInfo(packageName, 0)); - String text = charSequenceToString(label); - return text.isEmpty() ? packageName : text; - } catch (Throwable t) { - return packageName; - } - } - - private static void putString(JSONObject out, String key, String value) throws JSONException { - out.put(key, value == null ? "" : value); - } - - private static void putCharSequence(JSONObject out, String key, CharSequence value) throws JSONException { - out.put(key, charSequenceToString(value)); - } - - private static CharSequence firstNonEmpty(CharSequence first, CharSequence second) { - return charSequenceToString(first).isEmpty() ? second : first; - } - - private static String charSequenceToString(CharSequence value) { - return value == null ? "" : value.toString(); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt new file mode 100644 index 00000000..7c40de56 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt @@ -0,0 +1,662 @@ +package com.faceclaw.app + +import android.app.Notification +import android.app.NotificationManager +import android.app.PendingIntent +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.drawable.BitmapDrawable +import android.graphics.drawable.Drawable +import android.graphics.drawable.Icon +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.service.notification.NotificationListenerService +import android.service.notification.StatusBarNotification +import android.util.Log + +import java.io.ByteArrayOutputStream +import java.util.Arrays +import java.util.Comparator +import java.util.HashSet +import java.util.concurrent.CopyOnWriteArraySet + +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +class FaceclawMediaNotificationListenerService : NotificationListenerService() { + companion object { + private const val TAG = "FaceclawNotify" + private const val NOTIFICATION_ICON_GAMMA = 1.6 + private const val EXTRA_SUBSTITUTE_APP_NAME = "android.substName" + + @Volatile + private var activeService: FaceclawMediaNotificationListenerService? = null + private val mainHandler = Handler(Looper.getMainLooper()) + private val notificationListeners: MutableSet = CopyOnWriteArraySet() + private val activeNotificationWakeKeys: MutableSet = HashSet() + + @JvmStatic + fun addNotificationListener(listener: FaceclawNotificationListener?) { + if (listener != null) { + notificationListeners.add(listener) + } + } + + @JvmStatic + fun removeNotificationListener(listener: FaceclawNotificationListener?) { + if (listener != null) { + notificationListeners.remove(listener) + } + } + + @JvmStatic + fun hasActiveNotificationTitle(expectedTitle: String?): Boolean { + val service = activeService + if (service == null || expectedTitle == null || expectedTitle.isEmpty()) { + return false + } + val notifications: Array? + try { + notifications = service.activeNotifications + } catch (e: SecurityException) { + Log.w(TAG, "notification access denied while checking active notifications", e) + return false + } catch (t: Throwable) { + Log.w(TAG, "failed to check active notifications", t) + return false + } + if (notifications == null || notifications.size == 0) { + return false + } + for (notification in notifications) { + if (notification == null || notification.notification == null) { + continue + } + val extras: Bundle? = notification.notification.extras + if (extras == null) { + continue + } + val title: CharSequence? = extras.getCharSequence(Notification.EXTRA_TITLE) + if (title != null && expectedTitle.contentEquals(title)) { + return true + } + } + return false + } + + @JvmStatic + fun getActiveNotificationIconGrays(iconSize: Int, maxIcons: Int): ByteArray { + val service = activeService + val size = Math.max(1, Math.min(96, iconSize)) + val limit = Math.max(0, maxIcons) + if (service == null || limit == 0) { + return ByteArray(0) + } + + val notifications: Array? + try { + notifications = service.activeNotifications + } catch (e: SecurityException) { + Log.w(TAG, "notification access denied while reading icons", e) + return ByteArray(0) + } catch (t: Throwable) { + Log.w(TAG, "failed to read notification icons", t) + return ByteArray(0) + } + if (notifications == null || notifications.size == 0) { + return ByteArray(0) + } + + val out = ByteArrayOutputStream(size * size * Math.min(limit, notifications.size)) + val emittedGroupKeys: MutableSet = HashSet() + var emitted = 0 + for (statusBarNotification in notifications) { + if (!shouldShowNotificationIcon(service, statusBarNotification)) { + continue + } + val dedupeGroupKey = getNotificationDedupeGroupKey(statusBarNotification!!) + if (dedupeGroupKey != null && emittedGroupKeys.contains(dedupeGroupKey)) { + continue + } + val drawable = loadNotificationIcon(service, statusBarNotification.notification) + if (drawable == null) { + Log.i(TAG, "icon skipped (no drawable): " + statusBarNotification.packageName) + continue + } + Log.i(TAG, "icon[" + emitted + "] pkg=" + statusBarNotification.packageName + + " drawable=" + drawable.javaClass.simpleName + + " intrinsic=" + drawable.intrinsicWidth + "x" + drawable.intrinsicHeight) + appendIconGrayBytes(drawable, size, out, service, emitted, statusBarNotification.packageName) + if (dedupeGroupKey != null) { + emittedGroupKeys.add(dedupeGroupKey) + } + emitted += 1 + if (emitted >= limit) { + break + } + } + return out.toByteArray() + } + + /** + * Grayscale icon (iconSize*iconSize bytes) for one active notification, + * identified by its key; empty array when the notification or its icon is + * unavailable. Shares the extraction/scaling pipeline with the tray icon + * strip above. + */ + @JvmStatic + fun getNotificationIconGrayForKey(key: String?, iconSize: Int): ByteArray { + val service = activeService + val size = Math.max(1, Math.min(96, iconSize)) + if (service == null) { + return ByteArray(0) + } + val statusBarNotification = findActiveNotificationByKey(service, key) + if (statusBarNotification == null || statusBarNotification.notification == null) { + return ByteArray(0) + } + val drawable = loadNotificationIcon(service, statusBarNotification.notification) + if (drawable == null) { + return ByteArray(0) + } + val out = ByteArrayOutputStream(size * size) + appendIconGrayBytes(drawable, size, out, service, -1, statusBarNotification.packageName) + return out.toByteArray() + } + + @JvmStatic + fun getActiveNotificationsJson(maxNotifications: Int): String { + val service = activeService + val limit = Math.max(0, maxNotifications) + if (service == null || limit == 0) { + return "[]" + } + + val notifications: Array? + try { + notifications = service.activeNotifications + } catch (e: SecurityException) { + Log.w(TAG, "notification access denied while reading notifications", e) + return "[]" + } catch (t: Throwable) { + Log.w(TAG, "failed to read notifications", t) + return "[]" + } + if (notifications == null || notifications.size == 0) { + return "[]" + } + + Arrays.sort(notifications, object : Comparator { + override fun compare(a: StatusBarNotification?, b: StatusBarNotification?): Int { + val left = if (a == null) 0L else a.postTime + val right = if (b == null) 0L else b.postTime + return java.lang.Long.compare(right, left) + } + }) + + val out = JSONArray() + for (statusBarNotification in notifications) { + if (out.length() >= limit) { + break + } + if (!shouldShowNotificationInList(service, statusBarNotification)) { + continue + } + try { + out.put(buildNotificationJson(service, statusBarNotification!!)) + } catch (t: Throwable) { + Log.w(TAG, "failed to serialize notification", t) + } + } + return out.toString() + } + + @JvmStatic + fun invokeNotificationAction(key: String?, actionIndex: Int): Boolean { + val service = activeService + val statusBarNotification = findActiveNotificationByKey(service, key) + if (statusBarNotification == null || statusBarNotification.notification == null) { + return false + } + val actions: Array? = statusBarNotification.notification.actions + if (actions == null || actionIndex < 0 || actionIndex >= actions.size) { + return false + } + val intent: PendingIntent? = actions[actionIndex].actionIntent + if (intent == null) { + return false + } + try { + intent.send() + return true + } catch (e: PendingIntent.CanceledException) { + Log.w(TAG, "notification action pending intent was canceled", e) + return false + } catch (t: Throwable) { + Log.w(TAG, "failed to invoke notification action", t) + return false + } + } + + @JvmStatic + fun dismissNotification(key: String?): Boolean { + val service = activeService + if (service == null || key == null || key.isEmpty()) { + return false + } + try { + service.cancelNotification(key) + return true + } catch (e: SecurityException) { + Log.w(TAG, "notification access denied while dismissing notification", e) + return false + } catch (t: Throwable) { + Log.w(TAG, "failed to dismiss notification", t) + return false + } + } + + private fun emitNotificationPosted(key: String?) { + if (key == null || key.isEmpty() || notificationListeners.isEmpty()) { + return + } + for (listener in notificationListeners) { + mainHandler.post { + try { + listener.onNotificationPosted(key) + } catch (t: Throwable) { + Log.w(TAG, "notification listener failed", t) + } + } + } + } + + private fun refreshActiveNotificationWakeKeys(service: FaceclawMediaNotificationListenerService) { + val notifications: Array? + try { + notifications = service.activeNotifications + } catch (t: Throwable) { + Log.w(TAG, "failed to refresh active notification wake keys", t) + return + } + synchronized(activeNotificationWakeKeys) { + activeNotificationWakeKeys.clear() + if (notifications == null) { + return + } + for (statusBarNotification in notifications) { + if (shouldShowNotificationInList(service, statusBarNotification)) { + val key: String? = statusBarNotification!!.key + if (key != null && !key.isEmpty()) { + activeNotificationWakeKeys.add(key) + } + } + } + } + } + + private fun shouldEmitNotificationPosted(statusBarNotification: StatusBarNotification): Boolean { + val key: String? = statusBarNotification.key + if (key == null || key.isEmpty()) { + return true + } + + val alreadyActive: Boolean + synchronized(activeNotificationWakeKeys) { + alreadyActive = activeNotificationWakeKeys.contains(key) + activeNotificationWakeKeys.add(key) + } + return !alreadyActive || !isPersistentNotification(statusBarNotification) + } + + private fun forgetActiveNotificationWakeKey(statusBarNotification: StatusBarNotification?) { + if (statusBarNotification == null) { + return + } + val key: String? = statusBarNotification.key + if (key == null || key.isEmpty()) { + return + } + synchronized(activeNotificationWakeKeys) { + activeNotificationWakeKeys.remove(key) + } + } + + private fun isPersistentNotification(statusBarNotification: StatusBarNotification): Boolean { + val notification: Notification? = statusBarNotification.notification + if (notification == null) { + return false + } + val persistentFlags = Notification.FLAG_ONGOING_EVENT or Notification.FLAG_NO_CLEAR + return (notification.flags and persistentFlags) != 0 + } + + private fun shouldShowNotificationIcon(service: FaceclawMediaNotificationListenerService, statusBarNotification: StatusBarNotification?): Boolean { + if (!shouldShowNotificationInList(service, statusBarNotification)) { + return false + } + val notification = statusBarNotification!!.notification + if ((notification.flags and Notification.FLAG_GROUP_SUMMARY) != 0) { + return false + } + return true + } + + private fun shouldShowNotificationInList(service: FaceclawMediaNotificationListenerService, statusBarNotification: StatusBarNotification?): Boolean { + if (statusBarNotification == null || statusBarNotification.notification == null) { + return false + } + // Our own notifications stay out of the mirror: the foreground-service + // one is noise, and the Timers app rings on the glasses itself (its + // phone notification is for the phone), so mirroring it would stack a + // notification modal over the ringing screen. + if (service.packageName == statusBarNotification.packageName) { + return false + } + val notification = statusBarNotification.notification + if (Notification.CATEGORY_TRANSPORT == notification.category) { + return false + } + val extras: Bundle? = notification.extras + if (extras != null && extras.containsKey("android.mediaSession")) { + return false + } + + val rankingMap: NotificationListenerService.RankingMap? = service.currentRanking + if (rankingMap == null) { + return true + } + val ranking = NotificationListenerService.Ranking() + if (!rankingMap.getRanking(statusBarNotification.key, ranking)) { + return true + } + val importance = ranking.importance + return importance > NotificationManager.IMPORTANCE_MIN + } + + private fun getNotificationDedupeGroupKey(statusBarNotification: StatusBarNotification): String? { + val notification = statusBarNotification.notification + if (notification.group == null && statusBarNotification.overrideGroupKey == null) { + return null + } + val groupKey: String? = statusBarNotification.groupKey + if (groupKey == null || groupKey.isEmpty()) { + return null + } + // Group children often share the same small icon. Emit only one icon for the group. + return groupKey + } + + private fun loadNotificationIcon(service: FaceclawMediaNotificationListenerService, notification: Notification): Drawable? { + try { + val smallIcon: Icon? = notification.smallIcon + if (smallIcon != null) { + val drawable: Drawable? = smallIcon.loadDrawable(service) + if (drawable != null) { + // Status-bar small icons are alpha templates: the platform + // draws them tinted and ignores their color channels, which + // apps may fill with garbage (Discord ships noise there). + // Tint white so the shape comes from alpha alone, matching + // how the status bar renders them. + drawable.mutate() + drawable.setTint(Color.WHITE) + if (drawable is BitmapDrawable) { + drawable.setFilterBitmap(true) + } + return drawable + } + } + } catch (t: Throwable) { + Log.w(TAG, "failed to load small notification icon", t) + } + try { + // Large icons are real color images (avatars, album art); keep color. + val largeIcon: Icon? = notification.getLargeIcon() + if (largeIcon != null) { + return largeIcon.loadDrawable(service) + } + } catch (t: Throwable) { + Log.w(TAG, "failed to load large notification icon", t) + } + return null + } + + private fun appendIconGrayBytes(drawable: Drawable, size: Int, out: ByteArrayOutputStream, + service: FaceclawMediaNotificationListenerService, index: Int, packageName: String?) { + val bitmap = renderIconScaled(drawable, size) + dumpIconDebugPng(service, index, packageName, bitmap) + for (y in 0 until size) { + for (x in 0 until size) { + val color = bitmap.getPixel(x, y) + val alpha = Color.alpha(color) + val grayLinear = (0.2126 * Color.red(color) + 0.7152 * Color.green(color) + 0.0722 * Color.blue(color)) * alpha / (255.0 * 255.0) + val gray = Math.round(255.0 * Math.pow(Math.max(0.0, Math.min(1.0, grayLinear)), NOTIFICATION_ICON_GAMMA)).toInt() + out.write(gray and 0xff) + } + } + bitmap.recycle() + } + + /** + * Render a drawable at the target size with proper downscaling. Detailed + * sources (e.g. avatar bitmaps used as notification icons) are rendered at + * native resolution and reduced by repeated halving: a single filtered pass + * from, say, 126px to 24px samples too sparsely and turns fine detail into + * speckle that reads as a garbled icon. + */ + private fun renderIconScaled(drawable: Drawable, size: Int): Bitmap { + val renderW = Math.max(size, drawable.intrinsicWidth) + val renderH = Math.max(size, drawable.intrinsicHeight) + var bitmap = Bitmap.createBitmap(renderW, renderH, Bitmap.Config.ARGB_8888) + val canvas = Canvas(bitmap) + drawable.setBounds(0, 0, renderW, renderH) + drawable.draw(canvas) + while (bitmap.width >= size * 2 && bitmap.height >= size * 2) { + val halved = Bitmap.createScaledBitmap(bitmap, bitmap.width / 2, bitmap.height / 2, true) + bitmap.recycle() + bitmap = halved + } + if (bitmap.width != size || bitmap.height != size) { + val scaled = Bitmap.createScaledBitmap(bitmap, size, size, true) + bitmap.recycle() + bitmap = scaled + } + return bitmap + } + + /** + * Debug aid for garbled-icon reports: saves each rendered icon to + * /debug-icons/ (adb-pullable) so extraction problems can + * be told apart from downstream compositing/transmission problems. Cheap: + * runs at most once per icon-cache refresh on tiny bitmaps. + */ + private fun dumpIconDebugPng(service: FaceclawMediaNotificationListenerService, index: Int, packageName: String?, bitmap: Bitmap) { + if (index < 0) { + return + } + try { + val dir = java.io.File(service.getExternalFilesDir(null), "debug-icons") + if (!dir.exists() && !dir.mkdirs()) { + return + } + val safeName = if (packageName == null) "unknown" else packageName.replace(Regex("[^A-Za-z0-9._-]"), "_") + val file = java.io.File(dir, "icon-$index-$safeName.png") + java.io.FileOutputStream(file).use { stream -> + bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream) + } + } catch (t: Throwable) { + Log.w(TAG, "failed to dump debug icon", t) + } + } + + private fun findActiveNotificationByKey(service: FaceclawMediaNotificationListenerService?, key: String?): StatusBarNotification? { + if (service == null || key == null || key.isEmpty()) { + return null + } + val notifications: Array? + try { + notifications = service.activeNotifications + } catch (t: Throwable) { + Log.w(TAG, "failed to find active notification", t) + return null + } + if (notifications == null) { + return null + } + for (statusBarNotification in notifications) { + if (statusBarNotification != null && key == statusBarNotification.key) { + return statusBarNotification + } + } + return null + } + + @Throws(JSONException::class) + private fun buildNotificationJson(service: FaceclawMediaNotificationListenerService, statusBarNotification: StatusBarNotification): JSONObject { + val notification = statusBarNotification.notification + val extras: Bundle? = notification.extras + val out = JSONObject() + out.put("key", statusBarNotification.key) + out.put("packageName", statusBarNotification.packageName) + out.put("appName", getNotificationAppName(service, statusBarNotification)) + out.put("postTime", statusBarNotification.postTime) + out.put("when", notification.`when`) + putString(out, "category", notification.category) + if (extras != null) { + putCharSequence(out, "title", firstNonEmpty( + extras.getCharSequence(Notification.EXTRA_TITLE_BIG), + extras.getCharSequence(Notification.EXTRA_TITLE) + )) + putCharSequence(out, "text", extras.getCharSequence(Notification.EXTRA_TEXT)) + putCharSequence(out, "bigText", extras.getCharSequence(Notification.EXTRA_BIG_TEXT)) + putCharSequence(out, "subText", extras.getCharSequence(Notification.EXTRA_SUB_TEXT)) + putCharSequence(out, "infoText", extras.getCharSequence(Notification.EXTRA_INFO_TEXT)) + putCharSequence(out, "summaryText", extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT)) + val textLines: Array? = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES) + val lines = JSONArray() + if (textLines != null) { + for (line in textLines) { + val text = charSequenceToString(line) + if (!text.isEmpty()) { + lines.put(text) + } + } + } + out.put("lines", lines) + } else { + out.put("lines", JSONArray()) + } + + val actionsJson = JSONArray() + val actions: Array? = notification.actions + if (actions != null) { + for (index in actions.indices) { + val action = actions[index] + if (action == null) { + continue + } + val title = charSequenceToString(action.title) + if (title.isEmpty()) { + continue + } + val actionJson = JSONObject() + actionJson.put("index", index) + actionJson.put("title", title) + actionJson.put("enabled", action.actionIntent != null) + actionsJson.put(actionJson) + } + } + out.put("actions", actionsJson) + return out + } + + private fun getNotificationAppName(service: FaceclawMediaNotificationListenerService, statusBarNotification: StatusBarNotification): String { + val notification = statusBarNotification.notification + val extras: Bundle? = notification.extras + if (extras != null) { + val substituteName = charSequenceToString(extras.getCharSequence(EXTRA_SUBSTITUTE_APP_NAME)) + if (!substituteName.isEmpty()) { + return substituteName + } + } + return getAppLabel(service, statusBarNotification.packageName) + } + + private fun getAppLabel(service: FaceclawMediaNotificationListenerService, packageName: String?): String { + if (packageName == null || packageName.isEmpty()) { + return "" + } + try { + val label: CharSequence? = service + .packageManager + .getApplicationLabel(service.packageManager.getApplicationInfo(packageName, 0)) + val text = charSequenceToString(label) + return if (text.isEmpty()) packageName else text + } catch (t: Throwable) { + return packageName + } + } + + @Throws(JSONException::class) + private fun putString(out: JSONObject, key: String, value: String?) { + out.put(key, value ?: "") + } + + @Throws(JSONException::class) + private fun putCharSequence(out: JSONObject, key: String, value: CharSequence?) { + out.put(key, charSequenceToString(value)) + } + + private fun firstNonEmpty(first: CharSequence?, second: CharSequence?): CharSequence? { + return if (charSequenceToString(first).isEmpty()) second else first + } + + private fun charSequenceToString(value: CharSequence?): String { + return value?.toString() ?: "" + } + } + + override fun onCreate() { + super.onCreate() + activeService = this + } + + override fun onDestroy() { + if (activeService === this) { + activeService = null + } + super.onDestroy() + } + + override fun onListenerConnected() { + activeService = this + super.onListenerConnected() + refreshActiveNotificationWakeKeys(this) + } + + override fun onListenerDisconnected() { + if (activeService === this) { + activeService = null + } + super.onListenerDisconnected() + } + + override fun onNotificationPosted(statusBarNotification: StatusBarNotification?) { + super.onNotificationPosted(statusBarNotification) + if (!shouldShowNotificationInList(this, statusBarNotification)) { + forgetActiveNotificationWakeKey(statusBarNotification) + return + } + if (shouldEmitNotificationPosted(statusBarNotification!!)) { + emitNotificationPosted(statusBarNotification.key) + } + } + + override fun onNotificationRemoved(statusBarNotification: StatusBarNotification?) { + forgetActiveNotificationWakeKey(statusBarNotification) + super.onNotificationRemoved(statusBarNotification) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMicStatusListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMicStatusListener.java deleted file mode 100644 index e739ea34..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMicStatusListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawMicStatusListener { - /** - * A CFW mic_control field-104 status record from one temple. body is the - * raw 21-byte ['M','C',ver,...] record (decoded on the TS side); arm is - * "L" or "R" for the temple's own BLE link, "?" if unmatched. - */ - void onMicStatus(byte[] body, String arm); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.java deleted file mode 100644 index c1764d6e..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.java +++ /dev/null @@ -1,187 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.security.MessageDigest; -import java.util.concurrent.TimeUnit; - -import okhttp3.Call; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; -import okhttp3.ResponseBody; - -/** - * Large-file downloader for on-phone model weights (multi-GB, from Hugging - * Face). Downloads to ".part" with HTTP Range resume across app - * restarts, verifies a pinned sha256 (hashed incrementally, including the - * resumed prefix), then renames into place. Listener callbacks are posted to - * the constructing thread's Looper, like the other Faceclaw bridges. - */ -public class FaceclawModelDownloader { - private static final String TAG = "FaceclawModelDl"; - private static final long PROGRESS_INTERVAL_MS = 500; - - private static volatile OkHttpClient sharedClient; - - private final Handler callbackHandler; - private final String url; - private final File destFile; - private final File partFile; - private final String expectedSha256; - private final long expectedTotalBytes; - private final FaceclawModelDownloaderListener listener; - - private volatile boolean cancelled; - private volatile Call call; - private Thread worker; - - public FaceclawModelDownloader(String url, String destPath, String expectedSha256, - long expectedTotalBytes, FaceclawModelDownloaderListener listener) { - if (listener == null) throw new IllegalArgumentException("listener is required"); - Looper looper = Looper.myLooper(); - this.callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); - this.url = url; - this.destFile = new File(destPath); - this.partFile = new File(destPath + ".part"); - this.expectedSha256 = expectedSha256 == null ? "" : expectedSha256.toLowerCase(); - this.expectedTotalBytes = expectedTotalBytes; - this.listener = listener; - } - - private static OkHttpClient getClient() { - if (sharedClient == null) { - synchronized (FaceclawModelDownloader.class) { - if (sharedClient == null) { - sharedClient = new OkHttpClient.Builder() - .connectTimeout(30, TimeUnit.SECONDS) - .readTimeout(60, TimeUnit.SECONDS) - .addInterceptor(FaceclawHttp.userAgentInterceptor()) - .build(); - } - } - } - return sharedClient; - } - - public void start() { - worker = new Thread(this::run, "FaceclawModelDl"); - worker.start(); - } - - /** Stops the download but keeps the .part file so a later start() resumes. */ - public void cancel() { - cancelled = true; - Call current = call; - if (current != null) current.cancel(); - } - - private void run() { - try { - if (destFile.exists() && destFile.length() > 0) { - post(() -> listener.onDone(destFile.getAbsolutePath())); - return; - } - destFile.getParentFile().mkdirs(); - - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - long offset = 0; - if (partFile.exists()) { - offset = hashExistingPrefix(digest); - if (cancelled) return; - } - - Request.Builder builder = new Request.Builder().url(url); - if (offset > 0) builder.header("Range", "bytes=" + offset + "-"); - call = getClient().newCall(builder.build()); - - try (Response response = call.execute()) { - ResponseBody body = response.body(); - if (!response.isSuccessful() || body == null) { - postError("Model download failed: HTTP " + response.code()); - return; - } - boolean resumed = response.code() == 206; - if (offset > 0 && !resumed) { - // Server ignored the Range header; start over. - offset = 0; - digest = MessageDigest.getInstance("SHA-256"); - } - long total = expectedTotalBytes > 0 ? expectedTotalBytes - : (body.contentLength() > 0 ? offset + body.contentLength() : -1); - - try (FileOutputStream out = new FileOutputStream(partFile, resumed); - InputStream in = body.byteStream()) { - byte[] buffer = new byte[1 << 16]; - long done = offset; - long lastProgressAt = 0; - int read; - while ((read = in.read(buffer)) != -1) { - if (cancelled) return; - out.write(buffer, 0, read); - digest.update(buffer, 0, read); - done += read; - long now = System.currentTimeMillis(); - if (now - lastProgressAt >= PROGRESS_INTERVAL_MS) { - lastProgressAt = now; - final long doneFinal = done; - final long totalFinal = total; - post(() -> listener.onProgress(doneFinal, totalFinal)); - } - } - } - } - - if (expectedTotalBytes > 0 && partFile.length() != expectedTotalBytes) { - postError("Model download ended early (" + partFile.length() + " of " - + expectedTotalBytes + " bytes); try again to resume"); - return; - } - String actualSha = FaceclawFirmwareUtil.bytesToHex(digest.digest()); - if (!expectedSha256.isEmpty() && !actualSha.equals(expectedSha256)) { - partFile.delete(); - postError("Model download was corrupted (checksum mismatch); download it again"); - return; - } - if (!partFile.renameTo(destFile)) { - postError("Could not move the downloaded model into place"); - return; - } - post(() -> listener.onDone(destFile.getAbsolutePath())); - } catch (Exception e) { - if (cancelled) return; - Log.w(TAG, "download failed", e); - postError("Model download failed: " + e); - } - } - - /** Hash the already-downloaded prefix so the final digest covers the whole file. */ - private long hashExistingPrefix(MessageDigest digest) throws IOException { - long hashed = 0; - try (FileInputStream in = new FileInputStream(partFile)) { - byte[] buffer = new byte[1 << 16]; - int read; - while ((read = in.read(buffer)) != -1) { - if (cancelled) return hashed; - digest.update(buffer, 0, read); - hashed += read; - } - } - return hashed; - } - - private void post(Runnable runnable) { - callbackHandler.post(runnable); - } - - private void postError(final String message) { - post(() -> listener.onError(message)); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.kt new file mode 100644 index 00000000..96cb52ff --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloader.kt @@ -0,0 +1,148 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper + +import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream +import java.io.InputStream +import java.util.concurrent.TimeUnit + +import okhttp3.Call +import okhttp3.OkHttpClient +import okhttp3.Request + +/** + * Large-file downloader for on-phone model weights (multi-GB, from Hugging + * Face). The resume/hash/verify/rename policy is the shared ResumableDownload; + * this class supplies okhttp, java.io storage, the worker thread, and posts + * listener callbacks to the constructing thread's Looper, like the other + * Faceclaw bridges. + */ +class FaceclawModelDownloader( + url: String, + destPath: String, + expectedSha256: String?, + expectedTotalBytes: Long, + listener: FaceclawModelDownloaderListener? +) { + private val callbackHandler: Handler + private val transport = OkHttpTransport() + private val download: ResumableDownload + private var worker: Thread? = null + + init { + if (listener == null) throw IllegalArgumentException("listener is required") + val looper = Looper.myLooper() + this.callbackHandler = Handler(looper ?: Looper.getMainLooper()) + val posted = object : FaceclawModelDownloaderListener { + override fun onProgress(bytesDownloaded: Long, totalBytes: Long) { + callbackHandler.post { listener.onProgress(bytesDownloaded, totalBytes) } + } + + override fun onDone(path: String?) { + callbackHandler.post { listener.onDone(path) } + } + + override fun onError(message: String?) { + callbackHandler.post { listener.onError(message) } + } + } + download = ResumableDownload(url, File(destPath).absolutePath, expectedSha256, expectedTotalBytes, transport, FileStorage, posted) + } + + companion object { + @Volatile + private var sharedClient: OkHttpClient? = null + + private fun getClient(): OkHttpClient { + if (sharedClient == null) { + synchronized(FaceclawModelDownloader::class.java) { + if (sharedClient == null) { + sharedClient = OkHttpClient.Builder() + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(60, TimeUnit.SECONDS) + .addInterceptor(FaceclawHttp.userAgentInterceptor()) + .build() + } + } + } + return sharedClient!! + } + } + + fun start() { + val thread = Thread({ download.run() }, "FaceclawModelDl") + worker = thread + thread.start() + } + + /** Stops the download but keeps the .part file so a later start() resumes. */ + fun cancel() { + download.cancel() + transport.call?.cancel() + } + + /** One call at a time per downloader, so cancel() can abort a connect in progress. */ + private class OkHttpTransport : DownloadTransport { + @Volatile + var call: Call? = null + + override fun open(url: String, rangeFrom: Long): DownloadResponse { + val builder = Request.Builder().url(url) + if (rangeFrom > 0) builder.header("Range", "bytes=$rangeFrom-") + val call: Call = getClient().newCall(builder.build()) + this.call = call + val response = call.execute() + val body = response.body + val input: InputStream? = body?.byteStream() + return object : DownloadResponse { + override val code: Int = response.code + override val contentLength: Long = body?.contentLength() ?: -1L + + override fun read(buffer: ByteArray): Int = input?.read(buffer) ?: -1 + + override fun close() { + call.cancel() + response.close() + } + } + } + } + + private object FileStorage : DownloadStorage { + override fun length(path: String): Long = File(path).let { if (it.exists()) it.length() else -1L } + + override fun prepareParent(path: String) { + File(path).parentFile?.mkdirs() + } + + override fun openWrite(path: String, append: Boolean): FileSink { + val out = FileOutputStream(path, append) + return object : FileSink { + override fun write(bytes: ByteArray, offset: Int, length: Int) = out.write(bytes, offset, length) + + override fun close() = out.close() + } + } + + override fun readAll(path: String, consumer: (ByteArray, Int) -> Boolean): Long { + var consumed = 0L + FileInputStream(path).use { input -> + val buffer = ByteArray(1 shl 16) + while (true) { + val read = input.read(buffer) + if (read < 0) break + if (!consumer(buffer, read)) break + consumed += read + } + } + return consumed + } + + override fun rename(from: String, to: String): Boolean = File(from).renameTo(File(to)) + + override fun delete(path: String): Boolean = File(path).delete() + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloaderListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloaderListener.java deleted file mode 100644 index 90bf9380..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawModelDownloaderListener.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.faceclaw.app; - -/** Progress callbacks for FaceclawModelDownloader, posted to the creating thread's Looper. */ -public interface FaceclawModelDownloaderListener { - void onProgress(long bytesDownloaded, long totalBytes); - - /** The file is fully downloaded, verified, and moved to its final path. */ - void onDone(String path); - - void onError(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNoiseSuppressor.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNoiseSuppressor.java deleted file mode 100644 index 5d9514dc..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNoiseSuppressor.java +++ /dev/null @@ -1,266 +0,0 @@ -package com.faceclaw.app; - -import android.media.audiofx.AcousticEchoCanceler; -import android.media.audiofx.AudioEffect; -import android.media.audiofx.AutomaticGainControl; -import android.media.audiofx.NoiseSuppressor; - -import java.util.ArrayList; -import java.util.Arrays; - -/** - * Phone-side noise suppression for the microphone stream. - * - * Android's built-in noise cancellation (the {@link NoiseSuppressor} capture - * effect) only attaches to AudioRecord sessions on the phone's own microphones. Faceclaw's audio - * arrives as raw PCM over BLE from the glasses, which that effect chain never - * sees, so this class picks the best engine that can actually reach the - * samples: - * - * - Audio captured by the phone itself: {@link #attachPlatformEffects(int)} - * enables the device's built-in NS/AEC/AGC chain (hardware or OEM software, - * whatever the Android build ships) on the AudioRecord session. - * - Audio from the glasses (the normal case): {@link #process(byte[])} runs a - * WebRTC-NS-family spectral suppressor here on the phone — 16 ms sqrt-Hann - * frames at 50% overlap, per-bin asymmetric noise-floor tracking, and an - * over-subtracted Wiener gain with a residual floor so speech never pumps - * in and out of silence. - */ -public final class FaceclawNoiseSuppressor { - - private static final int FFT_SIZE = 256; - private static final int HOP = FFT_SIZE / 2; - private static final float OVERSUBTRACTION = 1.6f; - /** About -16 dB of residual: keeps the noise bed natural instead of gated. */ - private static final float GAIN_FLOOR = 0.15f; - private static final float GAIN_SMOOTHING = 0.6f; - /** Frames of pure noise-floor learning after start/reset (~100 ms). */ - private static final int NOISE_WARMUP_FRAMES = 12; - - private final int sampleRate; - private final float[] window = new float[FFT_SIZE]; - private final float[] history = new float[FFT_SIZE]; - private final float[] overlap = new float[HOP]; - private final float[] noise = new float[FFT_SIZE / 2 + 1]; - private final float[] gain = new float[FFT_SIZE / 2 + 1]; - private final float[] re = new float[FFT_SIZE]; - private final float[] im = new float[FFT_SIZE]; - private final short[] pending = new short[HOP]; - private int pendingCount = 0; - private int warmupFrames = 0; - - public FaceclawNoiseSuppressor(int sampleRate) { - this.sampleRate = sampleRate; - // sqrt-Hann (sine) analysis+synthesis windows: exact COLA at 50% overlap. - for (int i = 0; i < FFT_SIZE; i++) { - window[i] = (float) Math.sin(Math.PI * (i + 0.5) / FFT_SIZE); - } - reset(); - } - - public int getSampleRate() { - return sampleRate; - } - - /** Forget the learned noise floor (e.g. after the suppressor was toggled off). */ - public void reset() { - Arrays.fill(history, 0f); - Arrays.fill(overlap, 0f); - Arrays.fill(noise, 0f); - Arrays.fill(gain, 1f); - pendingCount = 0; - warmupFrames = 0; - } - - /** - * Suppress noise in 16-bit little-endian mono PCM. Streaming: input not - * filling a whole 8 ms hop is carried into the next call, so the output - * may be up to one hop shorter than the input (and longer on a later - * call); total latency is one hop. - */ - public byte[] process(byte[] pcm16le) { - if (pcm16le == null || pcm16le.length < 2) { - return pcm16le; - } - int sampleCount = pcm16le.length / 2; - int hops = (pendingCount + sampleCount) / HOP; - byte[] out = new byte[hops * HOP * 2]; - int outPos = 0; - int inPos = 0; - while (inPos < sampleCount) { - int take = Math.min(HOP - pendingCount, sampleCount - inPos); - for (int i = 0; i < take; i++) { - int lo = pcm16le[(inPos + i) * 2] & 0xff; - int hi = pcm16le[(inPos + i) * 2 + 1]; - pending[pendingCount + i] = (short) ((hi << 8) | lo); - } - pendingCount += take; - inPos += take; - if (pendingCount == HOP) { - outPos = processHop(out, outPos); - pendingCount = 0; - } - } - return out; - } - - private int processHop(byte[] out, int outPos) { - System.arraycopy(history, HOP, history, 0, FFT_SIZE - HOP); - for (int i = 0; i < HOP; i++) { - history[FFT_SIZE - HOP + i] = pending[i] / 32768f; - } - for (int i = 0; i < FFT_SIZE; i++) { - re[i] = history[i] * window[i]; - im[i] = 0f; - } - fft(re, im, false); - - boolean warming = warmupFrames < NOISE_WARMUP_FRAMES; - if (warming) { - warmupFrames++; - } - for (int b = 0; b <= FFT_SIZE / 2; b++) { - float power = re[b] * re[b] + im[b] * im[b]; - if (warming) { - noise[b] = warmupFrames == 1 ? power : noise[b] + (power - noise[b]) / warmupFrames; - } else if (power < noise[b]) { - // Track the floor down quickly; drift up slowly so speech - // energy never gets learned as noise. - noise[b] += (power - noise[b]) * 0.2f; - } else { - noise[b] = Math.min(power, noise[b] * 1.006f + 1e-10f); - } - float clean = power - OVERSUBTRACTION * noise[b]; - float g = clean > 0f ? clean / power : 0f; - if (g < GAIN_FLOOR) { - g = GAIN_FLOOR; - } - g = GAIN_SMOOTHING * gain[b] + (1f - GAIN_SMOOTHING) * g; - gain[b] = g; - re[b] *= g; - im[b] *= g; - if (b > 0 && b < FFT_SIZE / 2) { - re[FFT_SIZE - b] *= g; - im[FFT_SIZE - b] *= g; - } - } - - fft(re, im, true); - for (int i = 0; i < HOP; i++) { - float value = (overlap[i] + re[i] * window[i]) * 32767f; - int sample = Math.round(value); - if (sample > 32767) { - sample = 32767; - } - if (sample < -32768) { - sample = -32768; - } - out[outPos + i * 2] = (byte) (sample & 0xff); - out[outPos + i * 2 + 1] = (byte) ((sample >> 8) & 0xff); - } - for (int i = 0; i < HOP; i++) { - overlap[i] = re[HOP + i] * window[HOP + i]; - } - return outPos + HOP * 2; - } - - /** In-place iterative radix-2 FFT (inverse includes the 1/N scale). */ - private static void fft(float[] re, float[] im, boolean inverse) { - int n = re.length; - for (int i = 1, j = 0; i < n; i++) { - int bit = n >> 1; - for (; (j & bit) != 0; bit >>= 1) { - j ^= bit; - } - j ^= bit; - if (i < j) { - float t = re[i]; - re[i] = re[j]; - re[j] = t; - t = im[i]; - im[i] = im[j]; - im[j] = t; - } - } - for (int len = 2; len <= n; len <<= 1) { - double ang = 2 * Math.PI / len * (inverse ? 1 : -1); - float wRe = (float) Math.cos(ang); - float wIm = (float) Math.sin(ang); - for (int i = 0; i < n; i += len) { - float curRe = 1f; - float curIm = 0f; - for (int k = 0; k < len / 2; k++) { - int a = i + k; - int b = a + len / 2; - float xr = re[b] * curRe - im[b] * curIm; - float xi = re[b] * curIm + im[b] * curRe; - re[b] = re[a] - xr; - im[b] = im[a] - xi; - re[a] += xr; - im[a] += xi; - float nextRe = curRe * wRe - curIm * wIm; - curIm = curRe * wIm + curIm * wRe; - curRe = nextRe; - } - } - } - if (inverse) { - for (int i = 0; i < n; i++) { - re[i] /= n; - im[i] /= n; - } - } - } - - /** Whether this Android build ships the built-in capture noise suppressor. */ - public static boolean platformSuppressorAvailable() { - try { - return NoiseSuppressor.isAvailable(); - } catch (Throwable t) { - return false; - } - } - - /** Short engine name for the UI. */ - public static String engineDescription() { - return "spectral (phone DSP)"; - } - - /** - * Attach the platform's built-in capture-effect chain (NoiseSuppressor, - * AcousticEchoCanceler, AutomaticGainControl — whichever this Android - * build provides) to a phone AudioRecord session. Only applicable when the - * phone itself captures the audio; the BLE glasses stream must go through - * {@link #process(byte[])} instead. The caller keeps the returned effects - * for the life of the recording and release()s them with it. - */ - public static AudioEffect[] attachPlatformEffects(int audioSessionId) { - ArrayList effects = new ArrayList<>(); - try { - if (NoiseSuppressor.isAvailable()) { - NoiseSuppressor ns = NoiseSuppressor.create(audioSessionId); - if (ns != null) { - ns.setEnabled(true); - effects.add(ns); - } - } - if (AcousticEchoCanceler.isAvailable()) { - AcousticEchoCanceler aec = AcousticEchoCanceler.create(audioSessionId); - if (aec != null) { - aec.setEnabled(true); - effects.add(aec); - } - } - if (AutomaticGainControl.isAvailable()) { - AutomaticGainControl agc = AutomaticGainControl.create(audioSessionId); - if (agc != null) { - agc.setEnabled(true); - effects.add(agc); - } - } - } catch (Throwable t) { - // A missing effect just means this tier is unavailable. - } - return effects.toArray(new AudioEffect[0]); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNotificationListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNotificationListener.java deleted file mode 100644 index 5dd6687a..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawNotificationListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawNotificationListener { - void onNotificationPosted(String key); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.java deleted file mode 100644 index e0aaae58..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.java +++ /dev/null @@ -1,190 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; - -/** - * Headless display target for preview-only mode (no glasses paired). Hosts - * the same SurfaceCompositor the BLE communicator uses, so the TS side can - * run its full surface/render pipeline unchanged; composited frames simply - * have nowhere to go, and the phone-mirror preview, screenshots, and GIF - * recordings read the retained composite back exactly as they do on a live - * connection. - * - * The method signatures mirror the display subset of FaceclawBleCommunicator - * (the TS DisplayTarget interface is typed against that subset). - */ -public final class FaceclawPreviewCompositor { - private final Context appContext; - private final SurfaceCompositor compositor = new SurfaceCompositor(); - private final android.os.Handler mainHandler = new android.os.Handler(android.os.Looper.getMainLooper()); - - // Notified (on the main looper, where the phone UI's JS runs) after each - // applied surface frame, standing in for the BLE path's per-frame metrics - // callback so the phone mirror refreshes promptly instead of waiting for - // its safety-net poll. - private volatile Runnable frameListener; - - // Active animated-GIF screen recording, or null when idle. Frames are - // pushed by recordScreenFrame(), which the TS side calls at each - // phone-preview flush. - private volatile GifScreenRecorder screenRecorder; - - // Worker-app isolates locate their frame target through a static, like - // FaceclawBleCommunicator.getActive(); Java statics are shared across - // isolates, unlike anything on the JS side. - private static volatile FaceclawPreviewCompositor activeInstance; - - public static FaceclawPreviewCompositor getActive() { - return activeInstance; - } - - public FaceclawPreviewCompositor(Context context) { - this.appContext = context.getApplicationContext(); - } - - /** Publish this compositor as the workers' frame target. */ - public void makeActive() { - activeInstance = this; - } - - /** Withdraw from workers (a real connection is taking over). */ - public void release() { - if (activeInstance == this) { - activeInstance = null; - } - frameListener = null; - } - - public void setFrameListener(Runnable listener) { - this.frameListener = listener; - } - - /** Set the compositor's output frame size. Call before configuring surfaces. */ - public void configureCompositorScreen(int width, int height) { - compositor.configureScreen(width, height); - } - - public void configureSurface(String id, int x, int y, int width, int height, int zOrder, int transparency) { - compositor.configureSurface(id, x, y, width, height, zOrder, transparency); - } - - public void removeSurface(String id) { - compositor.removeSurface(id); - } - - /** Dim every surface below zOrder belowZOrder to factor256/256 (see SurfaceCompositor). */ - public void setUnderlayDim(int belowZOrder, int factor256) { - compositor.setUnderlayDim(belowZOrder, factor256); - } - - public void setSurfaceVisible(String id, boolean visible) { - compositor.setSurfaceVisible(id, visible); - } - - /** Blank (screen off) or unblank the output; retained surface state survives. */ - public void setScreenBlanked(boolean blanked) { - compositor.setBlanked(blanked); - } - - /** - * Apply an update to one compositor surface. Unlike the connected path - * there is no desired-frame store or transmit pipeline downstream; the - * preview/screenshot calls recomposite the retained state on demand, so - * applying the update is the whole job and the frame finishes here. - */ - public void submitSurfaceFrame( - java.nio.ByteBuffer pixels8bpp, - String surfaceId, - int rectX, - int rectY, - int rectWidth, - int rectHeight, - String contentFingerprint, - int paintMs, - int frameId, - java.nio.ByteBuffer glyphs - ) { - compositor.applyAndComposite( - surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, glyphs); - FrameTimings.getInstance().finishFrame(frameId, "composited (preview-only, no glasses)"); - Runnable listener = frameListener; - if (listener != null) { - mainHandler.post(listener); - } - } - - /** - * The current composited screen as a phone-UI preview bitmap, or null - * before any surface has been configured. - */ - public android.graphics.Bitmap getCompositePreviewBitmap(double brightenGamma, boolean green) { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return null; - } - return PreviewBitmapUtil.fromGray( - java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green); - } - - /** Save the current composite as a 4-bit grayscale PNG; returns the path or "". */ - public String saveCompositePngScreenshot() throws java.io.IOException { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return ""; - } - return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height); - } - - /** - * Save the current composite cropped to the given screen rect (the region - * the shell says is actually occupied). The rect is clamped to the screen; - * a degenerate rect falls back to the full screen. - */ - public String saveCompositePngScreenshot(int cropX, int cropY, int cropWidth, int cropHeight) - throws java.io.IOException { - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return ""; - } - int x = Math.max(0, cropX); - int y = Math.max(0, cropY); - int width = Math.min(composite.width - x, cropWidth - (x - cropX)); - int height = Math.min(composite.height - y, cropHeight - (y - cropY)); - if (width <= 0 || height <= 0 || (x == 0 && y == 0 && width == composite.width && height == composite.height)) { - return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height); - } - byte[] cropped = new byte[width * height]; - for (int row = 0; row < height; row++) { - System.arraycopy(composite.gray, (y + row) * composite.width + x, cropped, row * width, width); - } - return ScreenshotUtil.savePngScreenshot(appContext, cropped, width, height); - } - - /** Begin collecting composite frames for an animated-GIF screen recording. */ - public void startScreenRecording() { - screenRecorder = new GifScreenRecorder(); - } - - /** Capture the current composite into the active recording; no-op when idle. */ - public void recordScreenFrame() { - GifScreenRecorder recorder = screenRecorder; - if (recorder == null) { - return; - } - SurfaceCompositor.Composite composite = compositor.previewComposite(); - if (composite == null) { - return; - } - recorder.addFrame(composite.gray, composite.width, composite.height, System.currentTimeMillis()); - } - - /** Finish the recording and save it as an animated GIF; returns the path or "". */ - public String stopScreenRecording() throws java.io.IOException { - GifScreenRecorder recorder = screenRecorder; - screenRecorder = null; - if (recorder == null) { - return ""; - } - return recorder.save(appContext); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.kt new file mode 100644 index 00000000..4f4c65fa --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.kt @@ -0,0 +1,203 @@ +package com.faceclaw.app + +import android.content.Context + +/** + * Headless display target for preview-only mode (no glasses paired). Hosts + * the same SurfaceCompositor the BLE communicator uses, so the TS side can + * run its full surface/render pipeline unchanged; composited frames simply + * have nowhere to go, and the phone-mirror preview, screenshots, and GIF + * recordings read the retained composite back exactly as they do on a live + * connection. + * + * The method signatures mirror the display subset of FaceclawBleCommunicator + * (the TS DisplayTarget interface is typed against that subset). + */ +class FaceclawPreviewCompositor(context: Context) { + private val appContext: Context = context.applicationContext + private val compositor = SurfaceCompositor() + private val mainHandler = android.os.Handler(android.os.Looper.getMainLooper()) + + // Notified (on the main looper, where the phone UI's JS runs) after each + // applied surface frame, standing in for the BLE path's per-frame metrics + // callback so the phone mirror refreshes promptly instead of waiting for + // its safety-net poll. + @Volatile private var frameListener: Runnable? = null + + // Active animated-GIF screen recording, or null when idle. Frames are + // pushed by recordScreenFrame(), which the TS side calls at each + // phone-preview flush. + @Volatile private var screenRecorder: GifScreenRecorder? = null + + companion object { + // Worker-app isolates locate their frame target through a static, like + // FaceclawBleCommunicator.getActive(); Java statics are shared across + // isolates, unlike anything on the JS side. + @Volatile private var activeInstance: FaceclawPreviewCompositor? = null + + @JvmStatic + fun getActive(): FaceclawPreviewCompositor? { + return activeInstance + } + } + + /** Publish this compositor as the workers' frame target. */ + fun makeActive() { + activeInstance = this + } + + /** Withdraw from workers (a real connection is taking over). */ + fun release() { + if (activeInstance === this) { + activeInstance = null + } + frameListener = null + } + + fun setFrameListener(listener: Runnable?) { + this.frameListener = listener + } + + /** Set the compositor's output frame size. Call before configuring surfaces. */ + fun configureCompositorScreen(width: Int, height: Int) { + compositor.configureScreen(width, height) + } + + fun configureSurface(id: String?, x: Int, y: Int, width: Int, height: Int, zOrder: Int, transparency: Int) { + compositor.configureSurface(id, x, y, width, height, zOrder, transparency) + } + + fun removeSurface(id: String) { + compositor.removeSurface(id) + } + + /** Dim every surface below zOrder belowZOrder to factor256/256 (see SurfaceCompositor). */ + fun submitShellScene(bytes: java.nio.ByteBuffer, paintMs: Int, frameId: Int) { + compositor.setShellScene(AndroidByteReader(bytes)) + FrameTimings.getInstance().finishFrame(frameId, "composited (preview-only, no glasses)") + val listener = frameListener + if (listener != null) mainHandler.post(listener) + } + + fun setUnderlayDim(belowZOrder: Int, factor256: Int) { + compositor.setUnderlayDim(belowZOrder, factor256) + } + + fun setSurfaceVisible(id: String, visible: Boolean) { + compositor.setSurfaceVisible(id, visible) + } + + fun setSurfaceDepth(id: String, depth: Int) { + compositor.setSurfaceDepth(id, depth) + } + + /** Blank (screen off) or unblank the output; retained surface state survives. */ + fun setScreenBlanked(blanked: Boolean) { + compositor.setBlanked(blanked) + } + + /** + * Apply an update to one compositor surface. Unlike the connected path + * there is no desired-frame store or transmit pipeline downstream; the + * preview/screenshot calls recomposite the retained state on demand, so + * applying the update is the whole job and the frame finishes here. + */ + fun submitSurfaceFrame( + pixels8bpp: java.nio.ByteBuffer, + surfaceId: String, + rectX: Int, + rectY: Int, + rectWidth: Int, + rectHeight: Int, + contentFingerprint: String, + paintMs: Int, + frameId: Int, + glyphs: java.nio.ByteBuffer? + ) { + compositor.submitSurface( + surfaceId, AndroidByteReader(pixels8bpp), rectX, rectY, rectWidth, rectHeight, contentFingerprint, + if (glyphs == null) null else AndroidByteReader(glyphs)) + FrameTimings.getInstance().finishFrame(frameId, "composited (preview-only, no glasses)") + val listener = frameListener + if (listener != null) { + mainHandler.post(listener) + } + } + + /** + * The current composited screen as a phone-UI preview bitmap, or null + * before any surface has been configured. + */ + fun getCompositePreviewBitmap(brightenGamma: Double, green: Boolean): android.graphics.Bitmap? { + val composite = compositor.previewComposite() + if (composite == null) { + return null + } + return PreviewBitmapUtil.fromGray( + java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green) + } + + /** Save the current composite as a 4-bit grayscale PNG; returns the path or "". */ + @Throws(java.io.IOException::class) + fun saveCompositePngScreenshot(): String { + val composite = compositor.previewComposite() + if (composite == null) { + return "" + } + return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height) + } + + /** + * Save the current composite cropped to the given screen rect (the region + * the shell says is actually occupied). The rect is clamped to the screen; + * a degenerate rect falls back to the full screen. + */ + @Throws(java.io.IOException::class) + fun saveCompositePngScreenshot(cropX: Int, cropY: Int, cropWidth: Int, cropHeight: Int): String { + val composite = compositor.previewComposite() + if (composite == null) { + return "" + } + val x = Math.max(0, cropX) + val y = Math.max(0, cropY) + val width = Math.min(composite.width - x, cropWidth - (x - cropX)) + val height = Math.min(composite.height - y, cropHeight - (y - cropY)) + if (width <= 0 || height <= 0 || (x == 0 && y == 0 && width == composite.width && height == composite.height)) { + return ScreenshotUtil.savePngScreenshot(appContext, composite.gray, composite.width, composite.height) + } + val cropped = ByteArray(width * height) + for (row in 0 until height) { + System.arraycopy(composite.gray, (y + row) * composite.width + x, cropped, row * width, width) + } + return ScreenshotUtil.savePngScreenshot(appContext, cropped, width, height) + } + + /** Begin collecting composite frames for an animated-GIF screen recording. */ + fun startScreenRecording() { + screenRecorder = GifScreenRecorder() + } + + /** Capture the current composite into the active recording; no-op when idle. */ + fun recordScreenFrame() { + val recorder = screenRecorder + if (recorder == null) { + return + } + val composite = compositor.previewComposite() + if (composite == null) { + return + } + recorder.addFrame(composite.gray, composite.width, composite.height, System.currentTimeMillis()) + } + + /** Finish the recording and save it as an animated GIF; returns the path or "". */ + @Throws(java.io.IOException::class) + fun stopScreenRecording(): String { + val recorder = screenRecorder + screenRecorder = null + if (recorder == null) { + return "" + } + return recorder.save(appContext) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.java deleted file mode 100644 index 4b7cd5c4..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.faceclaw.app; - -import android.app.Activity; -import android.content.Context; -import android.util.Log; - -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.GoogleApiAvailability; -import com.google.android.gms.tasks.OnCanceledListener; -import com.google.android.gms.tasks.OnFailureListener; -import com.google.android.gms.tasks.OnSuccessListener; -import com.google.mlkit.vision.barcode.common.Barcode; -import com.google.mlkit.vision.codescanner.GmsBarcodeScanner; -import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions; -import com.google.mlkit.vision.codescanner.GmsBarcodeScanning; - -/** - * QR scanning through Play Services' ML Kit code scanner. The scanner UI and - * the camera both live inside Play Services, so Faceclaw needs no camera - * permission of its own and no third-party scanner app — which is what the - * earlier ZXing SCAN intent needed, and no app on a stock Pixel registers for - * it. - * - * The scanner module is downloaded on demand the first time it is used (the - * manifest's com.google.mlkit.vision.DEPENDENCIES meta-data asks Play Services - * to fetch it at install time instead); a scan attempted before that finishes - * fails with a module-unavailable error rather than blocking. - * - * Callbacks fire on the main thread. - */ -public final class FaceclawQrScanner { - private static final String TAG = "FaceclawQrScanner"; - - private FaceclawQrScanner() {} - - /** Whether Play Services is present and current enough to scan. */ - public static boolean isAvailable(Context context) { - try { - int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context); - return status == ConnectionResult.SUCCESS; - } catch (Throwable error) { - Log.w(TAG, "Play Services availability check failed", error); - return false; - } - } - - /** Open the scanner UI; the listener gets the decoded text, or a cancel/error. */ - public static void scan(Activity activity, final FaceclawQrScannerListener listener) { - GmsBarcodeScannerOptions options = new GmsBarcodeScannerOptions.Builder() - .setBarcodeFormats(Barcode.FORMAT_QR_CODE) - .enableAutoZoom() - .build(); - GmsBarcodeScanner scanner = GmsBarcodeScanning.getClient(activity, options); - scanner.startScan() - .addOnSuccessListener(new OnSuccessListener() { - @Override - public void onSuccess(Barcode barcode) { - String value = barcode == null ? null : barcode.getRawValue(); - if (value == null) { - listener.onError("The code held no text."); - } else { - listener.onResult(value); - } - } - }) - .addOnCanceledListener(new OnCanceledListener() { - @Override - public void onCanceled() { - listener.onCancelled(); - } - }) - .addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(Exception error) { - Log.w(TAG, "scan failed", error); - String message = error.getMessage(); - listener.onError(message == null ? error.toString() : message); - } - }); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.kt new file mode 100644 index 00000000..a3857f44 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScanner.kt @@ -0,0 +1,74 @@ +package com.faceclaw.app + +import android.app.Activity +import android.content.Context +import android.util.Log + +import com.google.android.gms.common.ConnectionResult +import com.google.android.gms.common.GoogleApiAvailability +import com.google.android.gms.tasks.OnCanceledListener +import com.google.android.gms.tasks.OnFailureListener +import com.google.android.gms.tasks.OnSuccessListener +import com.google.mlkit.vision.barcode.common.Barcode +import com.google.mlkit.vision.codescanner.GmsBarcodeScanner +import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions +import com.google.mlkit.vision.codescanner.GmsBarcodeScanning + +/** + * QR scanning through Play Services' ML Kit code scanner. The scanner UI and + * the camera both live inside Play Services, so Faceclaw needs no camera + * permission of its own and no third-party scanner app — which is what the + * earlier ZXing SCAN intent needed, and no app on a stock Pixel registers for + * it. + * + * The scanner module is downloaded on demand the first time it is used (the + * manifest's com.google.mlkit.vision.DEPENDENCIES meta-data asks Play Services + * to fetch it at install time instead); a scan attempted before that finishes + * fails with a module-unavailable error rather than blocking. + * + * Callbacks fire on the main thread. + */ +class FaceclawQrScanner private constructor() { + companion object { + private const val TAG = "FaceclawQrScanner" + + /** Whether Play Services is present and current enough to scan. */ + @JvmStatic + fun isAvailable(context: Context): Boolean { + try { + val status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) + return status == ConnectionResult.SUCCESS + } catch (error: Throwable) { + Log.w(TAG, "Play Services availability check failed", error) + return false + } + } + + /** Open the scanner UI; the listener gets the decoded text, or a cancel/error. */ + @JvmStatic + fun scan(activity: Activity, listener: FaceclawQrScannerListener) { + val options = GmsBarcodeScannerOptions.Builder() + .setBarcodeFormats(Barcode.FORMAT_QR_CODE) + .enableAutoZoom() + .build() + val scanner: GmsBarcodeScanner = GmsBarcodeScanning.getClient(activity, options) + scanner.startScan() + .addOnSuccessListener(OnSuccessListener { barcode -> + val value = barcode?.rawValue + if (value == null) { + listener.onError("The code held no text.") + } else { + listener.onResult(value) + } + }) + .addOnCanceledListener(OnCanceledListener { + listener.onCancelled() + }) + .addOnFailureListener(OnFailureListener { error -> + Log.w(TAG, "scan failed", error) + val message = error.message + listener.onError(message ?: error.toString()) + }) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScannerListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScannerListener.java deleted file mode 100644 index b255991e..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScannerListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.faceclaw.app; - -/** Callbacks for one FaceclawQrScanner.scan; exactly one method fires. */ -public interface FaceclawQrScannerListener { - void onResult(String text); - - void onCancelled(); - - void onError(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRemoteInput.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRemoteInput.kt new file mode 100644 index 00000000..10203b7c --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRemoteInput.kt @@ -0,0 +1,112 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import java.io.IOException +import java.net.InetAddress +import java.net.NetworkInterface +import java.net.ServerSocket +import java.net.Socket +import java.security.SecureRandom +import java.util.Collections + +/** + * Explicit-address, bounded input port. The request state machine lives in the shared + * [RemoteInputSession]; this adapter supplies java.net sockets, SecureRandom and main-thread + * delivery, and keeps the JVM API TypeScript calls (app/native/remote-input.ts). + */ +class FaceclawRemoteInput { + companion object { + private val INSTANCE = FaceclawRemoteInput() + @JvmStatic fun getInstance(): FaceclawRemoteInput { return INSTANCE } + + @JvmStatic + fun interfaces(): String { + val all = ArrayList() + try { + for (device in Collections.list(NetworkInterface.getNetworkInterfaces())) { + for (address in Collections.list(device.inetAddresses)) { + all.add(InterfaceAddress( + interfaceName = device.name, + address = address.hostAddress ?: continue, + up = device.isUp, + pointToPoint = device.isPointToPoint, + loopback = address.isLoopbackAddress, + linkLocal = address.isLinkLocalAddress, + anyLocal = address.isAnyLocalAddress, + )) + } + } + } catch (ignored: Exception) {} + return RemoteInputSession.interfacesJson(all) + } + } + + private val mainHandler = Handler(Looper.getMainLooper()) + private val session = RemoteInputSession( + port = AndroidLocalServerPort, + random = object : RandomSource { + private val secureRandom = SecureRandom() + override fun nextBytes(count: Int): ByteArray = ByteArray(count).also { secureRandom.nextBytes(it) } + }, + dispatch = { action -> mainHandler.post(action) }, + platform = AndroidProtocolPlatform, + ) + + fun setRequestListener(listener: Runnable?) { session.setRequestListener(listener?.let { { it.run() } }) } + + fun start(port: Int): String { return startAddress(port, "127.0.0.1") } + fun startAddress(port: Int, address: String): String = session.start(port, address) + fun stop() = session.stop() + fun nextRequest(): String? = session.nextRequest() + fun complete(id: Long, value: String?) = session.complete(id, value) + fun randomSecret(): String = session.randomSecret() + fun hash(value: String): String = RemoteInputSession.tokenHash(value) +} + +private object AndroidLocalServerPort : LocalServerPort { + override fun isLocalUnicastAddress(address: String): Boolean { + return try { + // Numeric literals only reach here, so getByName never resolves a hostname. + val local = InetAddress.getByName(address) + !(local.isAnyLocalAddress || local.isMulticastAddress || NetworkInterface.getByInetAddress(local) == null) + } catch (e: Exception) { + false + } + } + + override fun bind(address: String, port: Int): RemoteInputListener { + val server = ServerSocket(port, 8, InetAddress.getByName(address)) + return object : RemoteInputListener { + override fun accept(): RemoteInputConnection? { + val socket = try { server.accept() } catch (e: IOException) { return null } + return AndroidConnection(socket) + } + override val isClosed: Boolean get() = server.isClosed + override fun close() = server.close() + } + } +} + +private class AndroidConnection(private val socket: Socket) : RemoteInputConnection { + init { + socket.soTimeout = 5000 + socket.tcpNoDelay = true + } + + override fun readByte(timeoutMs: Long): Int { + return try { + socket.soTimeout = timeoutMs.coerceIn(1L, Int.MAX_VALUE.toLong()).toInt() + socket.getInputStream().read() + } catch (e: IOException) { + -1 + } + } + + override fun write(bytes: ByteArray) { + socket.getOutputStream().write(bytes) + socket.getOutputStream().flush() + } + + override fun close() = socket.close() +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.java deleted file mode 100644 index d2358878..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.faceclaw.app; - -import android.os.Debug; -import android.os.Process; -import android.os.SystemClock; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; - -/** Lightweight process counters for the on-glasses resource-usage monitor. */ -public final class FaceclawResourceUsage { - private FaceclawResourceUsage() {} - - /** - * Returns elapsedMs, processCpuMs, rssKb, nativeHeapKb, javaHeapKb, - * javaHeapCommittedKb, and threadCount. Reading /proc/self/status avoids - * the considerably heavier full Debug.MemoryInfo/PSS collection once per - * second while still reporting the RSS that Android records at exit. - */ - public static long[] sample() { - long[] status = readProcessStatus(); - Runtime runtime = Runtime.getRuntime(); - long javaUsedBytes = runtime.totalMemory() - runtime.freeMemory(); - return new long[] { - SystemClock.elapsedRealtime(), - Process.getElapsedCpuTime(), - status[0], - Debug.getNativeHeapAllocatedSize() / 1024L, - javaUsedBytes / 1024L, - runtime.totalMemory() / 1024L, - status[1], - }; - } - - /** Returns {VmRSS in KiB, thread count}; unavailable values are zero. */ - private static long[] readProcessStatus() { - long rssKb = 0; - long threadCount = 0; - try (BufferedReader reader = new BufferedReader(new FileReader("/proc/self/status"))) { - String line; - while ((line = reader.readLine()) != null) { - if (line.startsWith("VmRSS:")) { - rssKb = firstNumber(line); - } else if (line.startsWith("Threads:")) { - threadCount = firstNumber(line); - } - if (rssKb > 0 && threadCount > 0) break; - } - } catch (IOException ignored) { - // The graph remains useful with the runtime heap counters alone. - } - return new long[] { rssKb, threadCount }; - } - - private static long firstNumber(String line) { - int start = 0; - while (start < line.length() && !Character.isDigit(line.charAt(start))) start++; - int end = start; - while (end < line.length() && Character.isDigit(line.charAt(end))) end++; - if (start == end) return 0; - try { - return Long.parseLong(line.substring(start, end)); - } catch (NumberFormatException ignored) { - return 0; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.kt new file mode 100644 index 00000000..c5a94b4c --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawResourceUsage.kt @@ -0,0 +1,72 @@ +package com.faceclaw.app + +import android.os.Debug +import android.os.Process +import android.os.SystemClock + +import java.io.BufferedReader +import java.io.FileReader +import java.io.IOException + +/** Lightweight process counters for the on-glasses resource-usage monitor. */ +class FaceclawResourceUsage private constructor() { + companion object { + /** + * Returns elapsedMs, processCpuMs, rssKb, nativeHeapKb, javaHeapKb, + * javaHeapCommittedKb, and threadCount. Reading /proc/self/status avoids + * the considerably heavier full Debug.MemoryInfo/PSS collection once per + * second while still reporting the RSS that Android records at exit. + */ + @JvmStatic + fun sample(): LongArray { + val status = readProcessStatus() + val runtime = Runtime.getRuntime() + val javaUsedBytes = runtime.totalMemory() - runtime.freeMemory() + return longArrayOf( + SystemClock.elapsedRealtime(), + Process.getElapsedCpuTime(), + status[0], + Debug.getNativeHeapAllocatedSize() / 1024L, + javaUsedBytes / 1024L, + runtime.totalMemory() / 1024L, + status[1], + ) + } + + /** Returns {VmRSS in KiB, thread count}; unavailable values are zero. */ + private fun readProcessStatus(): LongArray { + var rssKb = 0L + var threadCount = 0L + try { + BufferedReader(FileReader("/proc/self/status")).use { reader -> + var line: String? + while (reader.readLine().also { line = it } != null) { + val current = line!! + if (current.startsWith("VmRSS:")) { + rssKb = firstNumber(current) + } else if (current.startsWith("Threads:")) { + threadCount = firstNumber(current) + } + if (rssKb > 0 && threadCount > 0) break + } + } + } catch (ignored: IOException) { + // The graph remains useful with the runtime heap counters alone. + } + return longArrayOf(rssKb, threadCount) + } + + private fun firstNumber(line: String): Long { + var start = 0 + while (start < line.length && !Character.isDigit(line[start])) start++ + var end = start + while (end < line.length && Character.isDigit(line[end])) end++ + if (start == end) return 0 + return try { + line.substring(start, end).toLong() + } catch (ignored: NumberFormatException) { + 0 + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRingEventDecoder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRingEventDecoder.java deleted file mode 100644 index 9cdc44f0..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawRingEventDecoder.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.faceclaw.app; - -import java.util.Locale; - -final class FaceclawRingEventDecoder { - private FaceclawRingEventDecoder() { - } - - static DirectRingEvent decode(byte[] raw) { - if (raw == null || raw.length == 0) { - return null; - } - - DirectRingEvent chargerGesture = decodeChargerGesture(raw); - if (chargerGesture != null) { - return chargerGesture; - } - - return decodeDirectGesture(raw); - } - - private static DirectRingEvent decodeChargerGesture(byte[] raw) { - if (raw.length != 11) { - return null; - } - if (u8(raw[0]) != 0x00 || u8(raw[1]) != 0x09 || u8(raw[2]) != 0x61 || u8(raw[3]) != 0x00) { - return null; - } - - int code = u8(raw[4]); - int param = u8(raw[5]) | (u8(raw[6]) << 8); - long tick = (u8(raw[7]) | (u8(raw[8]) << 8) | (u8(raw[9]) << 16) | (u8(raw[10]) << 24)) & 0xffffffffL; - String detail = String.format(Locale.US, "code=0x%02x(%s) param=0x%04x tick=%d", code, chargerCodeName(code), param, tick); - - switch (code) { - case 0x00: - return event(BleProtocol.EVENT_RING_LONG_PRESS, "LONG_PRESS", detail); - case 0x01: - return event(BleProtocol.EVENT_CLICK, "TAP", detail); - case 0x02: - return event(BleProtocol.EVENT_DOUBLE_CLICK, "DOUBLE_TAP", detail); - case 0x04: - return event(BleProtocol.EVENT_SCROLL_TOP, "SWIPE_UP", detail); - case 0x05: - return event(BleProtocol.EVENT_SCROLL_BOTTOM, "SWIPE_DOWN", detail); - case 0x08: - return event(BleProtocol.EVENT_RING_LONG_PRESS_RELEASE, "LONG_PRESS_RELEASE", detail); - default: - return null; - } - } - - private static DirectRingEvent decodeDirectGesture(byte[] raw) { - if (raw.length != 3 || u8(raw[0]) != 0xff) { - return null; - } - - int type = u8(raw[1]); - int param = u8(raw[2]); - String detail = String.format(Locale.US, "type=0x%02x param=0x%02x", type, param); - - if (type == 0x03 && param == 0x20) { - return event(BleProtocol.EVENT_RING_LONG_PRESS, "HOLD", detail); - } - if (type == 0x04 && param == 0x01) { - return event(BleProtocol.EVENT_CLICK, "TAP_SINGLE", detail); - } - if (type == 0x04 && param == 0x02) { - return event(BleProtocol.EVENT_DOUBLE_CLICK, "TAP_DOUBLE", detail); - } - if (type == 0x05) { - if (param <= 0x01) { - return event(BleProtocol.EVENT_SCROLL_BOTTOM, "SWIPE_FORWARD", detail); - } - return event(BleProtocol.EVENT_SCROLL_TOP, "SWIPE_BACKWARD", detail); - } - - return null; - } - - private static DirectRingEvent event(int eventType, String label, String detail) { - G2Event event = new G2Event( - "sys-event", - "", - eventType, - BleProtocol.EVENT_SOURCE_RING, - 0 - ); - return new DirectRingEvent(event, label, detail); - } - - private static String chargerCodeName(int code) { - switch (code) { - case 0x00: - return "LONG_PRESS"; - case 0x01: - return "TAP"; - case 0x02: - return "DOUBLE_TAP"; - case 0x04: - return "SWIPE_UP"; - case 0x05: - return "SWIPE_DOWN"; - case 0x08: - return "LONG_PRESS_RELEASE"; - default: - return "unknown"; - } - } - - private static int u8(byte value) { - return value & 0xff; - } - - static final class DirectRingEvent { - final G2Event event; - final String label; - final String detail; - - DirectRingEvent(G2Event event, String label, String detail) { - this.event = event; - this.label = label; - this.detail = detail; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.java deleted file mode 100644 index bec8c494..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.java +++ /dev/null @@ -1,121 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; -import android.content.SharedPreferences; -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.util.concurrent.CopyOnWriteArrayList; - -/** - * App settings store shared by every JS isolate (main thread and app - * workers). Values live in a dedicated SharedPreferences file, distinct from - * NativeScript's ApplicationSettings file, so this store owns its keys - * outright (the old TS-side settings were deliberately abandoned, not - * migrated). - * - * Change notifications: each isolate registers one listener from its own - * thread. The registering thread's Looper is captured, and notifications are - * posted through it so the JS callback always runs on the isolate's own - * thread (calling into an isolate from a foreign thread is not allowed). - * NativeScript worker threads run a message loop, so both the main thread - * and workers have a Looper; a listener registered from a Looper-less thread - * is accepted but never notified (it can still read fresh values on demand). - */ -public final class FaceclawSettings { - private static final String TAG = "FaceclawSettings"; - private static final String PREFS_NAME = "faceclaw_settings"; - private static volatile FaceclawSettings instance; - - private final SharedPreferences prefs; - private final CopyOnWriteArrayList listeners = new CopyOnWriteArrayList<>(); - - private static final class ListenerEntry { - final FaceclawSettingsListener listener; - final Handler handler; - - ListenerEntry(FaceclawSettingsListener listener, Handler handler) { - this.listener = listener; - this.handler = handler; - } - } - - private FaceclawSettings(Context context) { - this.prefs = context.getApplicationContext() - .getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); - } - - /** Initialize (idempotent) and return the singleton. */ - public static FaceclawSettings getInstance(Context context) { - if (instance == null) { - synchronized (FaceclawSettings.class) { - if (instance == null) { - instance = new FaceclawSettings(context); - } - } - } - return instance; - } - - /** Return the singleton; the main isolate must have initialized it first. */ - public static FaceclawSettings getInstance() { - FaceclawSettings result = instance; - if (result == null) { - throw new IllegalStateException("FaceclawSettings not initialized; call getInstance(context) first"); - } - return result; - } - - public String getString(String key, String defaultValue) { - return prefs.getString(key, defaultValue); - } - - public void setString(String key, String value) { - prefs.edit().putString(key, value).apply(); - notifyChanged(key); - } - - public boolean getBoolean(String key, boolean defaultValue) { - return prefs.getBoolean(key, defaultValue); - } - - public void setBoolean(String key, boolean value) { - prefs.edit().putBoolean(key, value).apply(); - notifyChanged(key); - } - - /** - * Register a change listener. Must be called from the thread whose - * isolate owns the listener; that thread's Looper is captured for - * dispatch. - */ - public void registerListener(FaceclawSettingsListener listener) { - Looper looper = Looper.myLooper(); - if (looper == null) { - Log.w(TAG, "settings listener registered from a Looper-less thread; it will never be notified"); - } - listeners.add(new ListenerEntry(listener, looper != null ? new Handler(looper) : null)); - } - - public void unregisterListener(FaceclawSettingsListener listener) { - for (ListenerEntry entry : listeners) { - if (entry.listener == listener) { - listeners.remove(entry); - } - } - } - - private void notifyChanged(String key) { - for (ListenerEntry entry : listeners) { - if (entry.handler == null) continue; - entry.handler.post(() -> { - try { - entry.listener.onSettingChanged(key); - } catch (Exception e) { - Log.w(TAG, "settings listener failed for key " + key, e); - } - }); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.kt new file mode 100644 index 00000000..6a6b1450 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettings.kt @@ -0,0 +1,88 @@ +package com.faceclaw.app + +import android.content.Context +import android.content.SharedPreferences +import android.os.Handler +import android.os.Looper + +/** + * App settings store shared by every JS isolate (main thread and app + * workers). Values live in a dedicated SharedPreferences file, distinct from + * NativeScript's ApplicationSettings file, so this store owns its keys + * outright (the old TS-side settings were deliberately abandoned, not + * migrated). + * + * Change notifications: each isolate registers one listener from its own + * thread. The registering thread's Looper is captured, and notifications are + * posted through it so the JS callback always runs on the isolate's own + * thread (calling into an isolate from a foreign thread is not allowed). + * NativeScript worker threads run a message loop, so both the main thread + * and workers have a Looper; a listener registered from a Looper-less thread + * is accepted but never notified (it can still read fresh values on demand). + * The reads/writes and the fan-out live in the shared SettingsChangeHub. + */ +class FaceclawSettings private constructor(context: Context) { + private val prefs: SharedPreferences = context.applicationContext + .getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + private val hub = SettingsChangeHub(object : SettingsStorage { + override fun getString(key: String?, defaultValue: String?): String? = prefs.getString(key, defaultValue) + + override fun putString(key: String?, value: String?) { + prefs.edit().putString(key, value).apply() + } + + override fun getBoolean(key: String?, defaultValue: Boolean): Boolean = prefs.getBoolean(key, defaultValue) + + override fun putBoolean(key: String?, value: Boolean) { + prefs.edit().putBoolean(key, value).apply() + } + }) + + companion object { + private const val PREFS_NAME = "faceclaw_settings" + @Volatile + private var instance: FaceclawSettings? = null + + /** Initialize (idempotent) and return the singleton. */ + @JvmStatic + fun getInstance(context: Context): FaceclawSettings { + if (instance == null) { + synchronized(FaceclawSettings::class.java) { + if (instance == null) { + instance = FaceclawSettings(context) + } + } + } + return instance!! + } + + /** Return the singleton; the main isolate must have initialized it first. */ + @JvmStatic + fun getInstance(): FaceclawSettings { + val result = instance + ?: throw IllegalStateException("FaceclawSettings not initialized; call getInstance(context) first") + return result + } + } + + fun getString(key: String?, defaultValue: String?): String? = hub.getString(key, defaultValue) + + fun setString(key: String?, value: String?) = hub.setString(key, value) + + fun getBoolean(key: String?, defaultValue: Boolean): Boolean = hub.getBoolean(key, defaultValue) + + fun setBoolean(key: String?, value: Boolean) = hub.setBoolean(key, value) + + /** + * Register a change listener. Must be called from the thread whose + * isolate owns the listener; that thread's Looper is captured for + * dispatch. + */ + fun registerListener(listener: FaceclawSettingsListener) { + val looper = Looper.myLooper() + val handler = if (looper != null) Handler(looper) else null + hub.register(listener, if (handler != null) SettingsDispatcher { action -> handler.post(action) } else null) + } + + fun unregisterListener(listener: FaceclawSettingsListener?) = hub.unregister(listener) +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsListener.java deleted file mode 100644 index a34bd7ab..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsListener.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.faceclaw.app; - -/** Change-notification callback for FaceclawSettings; implemented in JS. */ -public interface FaceclawSettingsListener { - void onSettingChanged(String key); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.java deleted file mode 100644 index 90becbef..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.java +++ /dev/null @@ -1,141 +0,0 @@ -package com.faceclaw.app; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.os.Handler; -import android.os.Looper; -import android.util.Log; -import android.util.Xml; - -import org.xmlpull.v1.XmlPullParser; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -/** - * adb-triggered export/import of the faceclaw_settings SharedPreferences - * file, so configuration (API keys, device addresses, ...) can be moved off a - * release build, where run-as is unavailable. See scripts/pull_config.sh and - * scripts/push_config.sh, which use run-as on debuggable builds and fall back - * to this receiver otherwise. - * - * adb shell am broadcast -n com.faceclaw.app/.FaceclawSettingsPortReceiver \ - * -a com.faceclaw.app.SETTINGS_EXPORT - * adb pull /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-export.xml - * - * adb push settings.xml /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-import.xml - * adb shell am broadcast -n com.faceclaw.app/.FaceclawSettingsPortReceiver \ - * -a com.faceclaw.app.SETTINGS_IMPORT - * - * Security: the settings file contains API tokens, so the manifest guards - * this receiver with android.permission.DUMP — a development permission the - * adb shell holds but ordinary apps cannot obtain without adb's help, keeping - * the trigger reachable from adb only. The transfer file lives in - * getExternalFilesDir(), which other apps cannot read on Android 11+ (and - * which the scripts delete as soon as the transfer completes); on the - * Android 7-10 devices minSdk still admits, apps holding READ_EXTERNAL_STORAGE - * could read it during that window. - */ -public class FaceclawSettingsPortReceiver extends BroadcastReceiver { - private static final String TAG = "FaceclawSettingsPort"; - public static final String ACTION_EXPORT = "com.faceclaw.app.SETTINGS_EXPORT"; - public static final String ACTION_IMPORT = "com.faceclaw.app.SETTINGS_IMPORT"; - private static final String PREFS_FILE = "faceclaw_settings.xml"; - private static final String EXPORT_NAME = "faceclaw-settings-export.xml"; - private static final String IMPORT_NAME = "faceclaw-settings-import.xml"; - - @Override - public void onReceive(Context context, Intent intent) { - String action = intent == null ? null : intent.getAction(); - try { - if (ACTION_EXPORT.equals(action)) { - setResultData(exportSettings(context)); - } else if (ACTION_IMPORT.equals(action)) { - setResultData(importSettings(context)); - // Exit once the broadcast result has been delivered: this - // process may hold stale in-memory SharedPreferences that a - // later apply() would flush over the imported file, and the - // next launch must re-read from disk. - new Handler(Looper.getMainLooper()).postDelayed(() -> { - Log.i(TAG, "Exiting after settings import"); - System.exit(0); - }, 500); - } - } catch (Exception e) { - Log.e(TAG, "Settings " + action + " failed", e); - setResultData("error: " + e); - } - } - - private File prefsFile(Context context) { - return new File(context.getApplicationInfo().dataDir, "shared_prefs/" + PREFS_FILE); - } - - private File externalFile(Context context, String name) throws IOException { - File dir = context.getExternalFilesDir(null); - if (dir == null) { - throw new IOException("external files dir unavailable"); - } - return new File(dir, name); - } - - private String exportSettings(Context context) throws IOException { - File prefs = prefsFile(context); - if (!prefs.exists()) { - return "error: no settings file yet (" + prefs + ")"; - } - File out = externalFile(context, EXPORT_NAME); - copy(prefs, out); - return "exported: " + out; - } - - private String importSettings(Context context) throws Exception { - File staged = externalFile(context, IMPORT_NAME); - if (!staged.exists()) { - return "error: nothing staged at " + staged; - } - validatePrefsXml(staged); - File prefs = prefsFile(context); - prefs.getParentFile().mkdirs(); - if (prefs.exists()) { - copy(prefs, new File(prefs.getPath() + ".bak")); - } - copy(staged, prefs); - staged.delete(); - return "imported: " + prefs + " (previous settings in " + PREFS_FILE + ".bak)"; - } - - /** Reject files that are not a SharedPreferences document. */ - private void validatePrefsXml(File file) throws Exception { - try (InputStream in = new FileInputStream(file)) { - XmlPullParser parser = Xml.newPullParser(); - parser.setInput(in, null); - int event = parser.next(); - while (event != XmlPullParser.START_TAG && event != XmlPullParser.END_DOCUMENT) { - event = parser.next(); - } - if (event != XmlPullParser.START_TAG || !"map".equals(parser.getName())) { - throw new IOException("not a SharedPreferences document"); - } - // Walk the rest so malformed XML is caught before it replaces - // the live settings file. - while (parser.next() != XmlPullParser.END_DOCUMENT) { } - } - } - - private void copy(File from, File to) throws IOException { - try (InputStream in = new FileInputStream(from); - OutputStream out = new FileOutputStream(to)) { - byte[] buffer = new byte[8192]; - int n; - while ((n = in.read(buffer)) > 0) { - out.write(buffer, 0, n); - } - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.kt new file mode 100644 index 00000000..788c93a1 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSettingsPortReceiver.kt @@ -0,0 +1,146 @@ +package com.faceclaw.app + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.os.Handler +import android.os.Looper +import android.util.Log +import android.util.Xml + +import org.xmlpull.v1.XmlPullParser + +import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream +import java.io.IOException +import java.io.InputStream +import java.io.OutputStream + +/** + * adb-triggered export/import of the faceclaw_settings SharedPreferences + * file, so configuration (API keys, device addresses, ...) can be moved off a + * release build, where run-as is unavailable. See scripts/pull_config.sh and + * scripts/push_config.sh, which use run-as on debuggable builds and fall back + * to this receiver otherwise. + * + * adb shell am broadcast -n com.faceclaw.app/.FaceclawSettingsPortReceiver \ + * -a com.faceclaw.app.SETTINGS_EXPORT + * adb pull /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-export.xml + * + * adb push settings.xml /sdcard/Android/data/com.faceclaw.app/files/faceclaw-settings-import.xml + * adb shell am broadcast -n com.faceclaw.app/.FaceclawSettingsPortReceiver \ + * -a com.faceclaw.app.SETTINGS_IMPORT + * + * Security: the settings file contains API tokens, so the manifest guards + * this receiver with android.permission.DUMP — a development permission the + * adb shell holds but ordinary apps cannot obtain without adb's help, keeping + * the trigger reachable from adb only. The transfer file lives in + * getExternalFilesDir(), which other apps cannot read on Android 11+ (and + * which the scripts delete as soon as the transfer completes); on the + * Android 7-10 devices minSdk still admits, apps holding READ_EXTERNAL_STORAGE + * could read it during that window. + */ +class FaceclawSettingsPortReceiver : BroadcastReceiver() { + companion object { + private const val TAG = "FaceclawSettingsPort" + const val ACTION_EXPORT = "com.faceclaw.app.SETTINGS_EXPORT" + const val ACTION_IMPORT = "com.faceclaw.app.SETTINGS_IMPORT" + private const val PREFS_FILE = "faceclaw_settings.xml" + private const val EXPORT_NAME = "faceclaw-settings-export.xml" + private const val IMPORT_NAME = "faceclaw-settings-import.xml" + } + + override fun onReceive(context: Context, intent: Intent?) { + val action = intent?.action + try { + if (ACTION_EXPORT == action) { + resultData = exportSettings(context) + } else if (ACTION_IMPORT == action) { + resultData = importSettings(context) + // Exit once the broadcast result has been delivered: this + // process may hold stale in-memory SharedPreferences that a + // later apply() would flush over the imported file, and the + // next launch must re-read from disk. + Handler(Looper.getMainLooper()).postDelayed({ + Log.i(TAG, "Exiting after settings import") + System.exit(0) + }, 500) + } + } catch (e: Exception) { + Log.e(TAG, "Settings $action failed", e) + resultData = "error: $e" + } + } + + private fun prefsFile(context: Context): File { + return File(context.applicationInfo.dataDir, "shared_prefs/$PREFS_FILE") + } + + @Throws(IOException::class) + private fun externalFile(context: Context, name: String): File { + val dir = context.getExternalFilesDir(null) + ?: throw IOException("external files dir unavailable") + return File(dir, name) + } + + @Throws(IOException::class) + private fun exportSettings(context: Context): String { + val prefs = prefsFile(context) + if (!prefs.exists()) { + return "error: no settings file yet ($prefs)" + } + val out = externalFile(context, EXPORT_NAME) + copy(prefs, out) + return "exported: $out" + } + + @Throws(Exception::class) + private fun importSettings(context: Context): String { + val staged = externalFile(context, IMPORT_NAME) + if (!staged.exists()) { + return "error: nothing staged at $staged" + } + validatePrefsXml(staged) + val prefs = prefsFile(context) + prefs.parentFile?.mkdirs() + if (prefs.exists()) { + copy(prefs, File(prefs.path + ".bak")) + } + copy(staged, prefs) + staged.delete() + return "imported: $prefs (previous settings in $PREFS_FILE.bak)" + } + + /** Reject files that are not a SharedPreferences document. */ + @Throws(Exception::class) + private fun validatePrefsXml(file: File) { + FileInputStream(file).use { input -> + val parser = Xml.newPullParser() + parser.setInput(input, null) + var event = parser.next() + while (event != XmlPullParser.START_TAG && event != XmlPullParser.END_DOCUMENT) { + event = parser.next() + } + if (event != XmlPullParser.START_TAG || "map" != parser.name) { + throw IOException("not a SharedPreferences document") + } + // Walk the rest so malformed XML is caught before it replaces + // the live settings file. + while (parser.next() != XmlPullParser.END_DOCUMENT) { } + } + } + + @Throws(IOException::class) + private fun copy(from: File, to: File) { + FileInputStream(from).use { input: InputStream -> + FileOutputStream(to).use { out: OutputStream -> + val buffer = ByteArray(8192) + var n: Int + while (input.read(buffer).also { n = it } > 0) { + out.write(buffer, 0, n) + } + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.java deleted file mode 100644 index c258ad91..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; - -import com.k2fsa.sherpa.onnx.OnlineStream; -import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractor; -import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractorConfig; - -/** - * Speaker voice-print extraction over sherpa-onnx. One embedding per - * utterance; matching/enrollment happens on the TS side (cosine similarity - * against the stored speaker profiles). - */ -public class FaceclawSpeakerId { - private static final String TAG = "FaceclawSpeakerId"; - - private SpeakerEmbeddingExtractor extractor; - private final String modelPath; - - public FaceclawSpeakerId(String modelPath) { - this.modelPath = modelPath; - } - - /** Lazily loads the model; returns false when it cannot be loaded. */ - public synchronized boolean ensureLoaded() { - if (extractor != null) { - return true; - } - try { - SpeakerEmbeddingExtractorConfig config = SpeakerEmbeddingExtractorConfig.builder() - .setModel(modelPath) - .setNumThreads(1) - .setDebug(false) - .setProvider("cpu") - .build(); - extractor = new SpeakerEmbeddingExtractor(config); - return true; - } catch (Throwable t) { - Log.w(TAG, "could not load speaker model at " + modelPath, t); - extractor = null; - return false; - } - } - - public synchronized int embeddingDim() { - return extractor == null ? 0 : extractor.getDim(); - } - - /** - * Compute an L2-normalized voice-print for one utterance of 16 kHz mono - * S16LE PCM. Returns null when the model is unavailable or the utterance - * is too short to embed (under ~0.5 s). - */ - public synchronized float[] embed(byte[] pcm16le, int sampleRate) { - if (pcm16le == null || pcm16le.length < sampleRate) { - return null; - } - if (!ensureLoaded()) { - return null; - } - int count = pcm16le.length / 2; - float[] samples = new float[count]; - for (int i = 0; i < count; i++) { - short s = (short) ((pcm16le[i * 2] & 0xff) | (pcm16le[i * 2 + 1] << 8)); - samples[i] = s / 32768.0f; - } - OnlineStream stream = null; - try { - stream = extractor.createStream(); - stream.acceptWaveform(samples, sampleRate); - stream.inputFinished(); - if (!extractor.isReady(stream)) { - return null; - } - float[] embedding = extractor.compute(stream); - return l2Normalize(embedding); - } catch (Throwable t) { - Log.w(TAG, "embedding failed", t); - return null; - } finally { - if (stream != null) { - stream.release(); - } - } - } - - private static float[] l2Normalize(float[] v) { - if (v == null) { - return null; - } - double sum = 0; - for (float x : v) { - sum += (double) x * x; - } - double norm = Math.sqrt(sum); - if (norm <= 0) { - return v; - } - for (int i = 0; i < v.length; i++) { - v[i] = (float) (v[i] / norm); - } - return v; - } - - public synchronized void close() { - if (extractor != null) { - extractor.release(); - extractor = null; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.kt new file mode 100644 index 00000000..87dd6c22 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSpeakerId.kt @@ -0,0 +1,97 @@ +package com.faceclaw.app + +import android.util.Log + +import com.k2fsa.sherpa.onnx.OnlineStream +import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractor +import com.k2fsa.sherpa.onnx.SpeakerEmbeddingExtractorConfig + +/** + * Speaker voice-print extraction over sherpa-onnx. One embedding per + * utterance; matching/enrollment happens on the TS side (cosine similarity + * against the stored speaker profiles). + */ +class FaceclawSpeakerId(private val modelPath: String?) : SpeakerEmbedder { + companion object { + private const val TAG = "FaceclawSpeakerId" + } + + private var extractor: SpeakerEmbeddingExtractor? = null + + /** Lazily loads the model; returns false when it cannot be loaded. */ + @Synchronized + fun ensureLoaded(): Boolean { + if (extractor != null) { + return true + } + try { + val config = SpeakerEmbeddingExtractorConfig.builder() + .setModel(modelPath) + .setNumThreads(1) + .setDebug(false) + .setProvider("cpu") + .build() + extractor = SpeakerEmbeddingExtractor(config) + return true + } catch (t: Throwable) { + Log.w(TAG, "could not load speaker model at $modelPath", t) + extractor = null + return false + } + } + + @Synchronized + fun embeddingDim(): Int { + val extractor = this.extractor + return if (extractor == null) 0 else extractor.dim + } + + /** + * Compute an L2-normalized voice-print for one utterance of 16 kHz mono + * S16LE PCM. Returns null when the model is unavailable or the utterance + * is too short to embed (under ~0.5 s). + */ + @Synchronized + override fun embed(pcm16le: ByteArray, sampleRate: Int): FloatArray? = embedNullable(pcm16le, sampleRate) + + @Synchronized + fun embedNullable(pcm16le: ByteArray?, sampleRate: Int): FloatArray? { + if (pcm16le == null || !VoicePrint.isLongEnoughToEmbed(pcm16le.size, sampleRate)) { + return null + } + if (!ensureLoaded()) { + return null + } + val extractor = this.extractor!! + val samples = AudioSegmentation.pcm16leToFloat(pcm16le) + var stream: OnlineStream? = null + try { + stream = extractor.createStream() + stream.acceptWaveform(samples, sampleRate) + stream.inputFinished() + if (!extractor.isReady(stream)) { + return null + } + val embedding = extractor.compute(stream) + return VoicePrint.l2Normalize(embedding) + } catch (t: Throwable) { + Log.w(TAG, "embedding failed", t) + return null + } finally { + if (stream != null) { + stream.release() + } + } + } + + override fun release() = close() + + @Synchronized + fun close() { + val extractor = this.extractor + if (extractor != null) { + extractor.release() + this.extractor = null + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.java deleted file mode 100644 index 5ec20fe9..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.io.BufferedReader; -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import okhttp3.Call; -import okhttp3.Callback; -import okhttp3.MediaType; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; -import okhttp3.ResponseBody; - -/** - * Streaming HTTP POST for the TypeScript side, used for server-sent-events - * APIs (e.g. the Anthropic Messages API with stream=true). The response body - * is delivered line by line as it arrives, so the JS side can parse SSE - * events incrementally. Like FaceclawWebSocket, listener callbacks are posted - * to the Looper of the thread that constructed this object. - */ -public class FaceclawSseRequest { - private static final String TAG = "FaceclawSseRequest"; - private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); - private static volatile OkHttpClient sharedClient; - - private final Handler callbackHandler; - private final Call call; - private volatile boolean cancelled; - - /** - * Starts the request immediately. headers is a flat alternating - * [name, value, name, value, ...] array (okhttp Headers can't cross the - * JS bridge, and parallel arrays are easy to build with Array.create). - */ - public FaceclawSseRequest(String url, String jsonBody, String[] headers, final FaceclawSseListener listener) { - if (url == null || url.trim().isEmpty()) { - throw new IllegalArgumentException("url is required"); - } - if (listener == null) { - throw new IllegalArgumentException("listener is required"); - } - Looper looper = Looper.myLooper(); - callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); - Request.Builder builder = new Request.Builder() - .url(url.trim()) - .post(RequestBody.create(JSON, jsonBody == null ? "" : jsonBody)); - if (headers != null) { - for (int i = 0; i + 1 < headers.length; i += 2) { - if (headers[i] != null && !headers[i].isEmpty() && headers[i + 1] != null) { - builder.addHeader(headers[i], headers[i + 1]); - } - } - } - call = getClient().newCall(builder.build()); - call.enqueue(new Callback() { - @Override public void onFailure(Call call, IOException e) { - postFailure(listener, String.valueOf(e)); - } - - @Override public void onResponse(Call call, Response response) { - try (ResponseBody body = response.body()) { - if (!response.isSuccessful()) { - final int code = response.code(); - String errorBody = ""; - try { - errorBody = body == null ? "" : body.string(); - } catch (IOException e) { - Log.w(TAG, "error body read failed", e); - } - final String finalBody = errorBody; - post(() -> listener.onHttpError(code, finalBody)); - return; - } - if (body == null) { - post(listener::onComplete); - return; - } - BufferedReader reader = new BufferedReader(body.charStream()); - String line; - while ((line = reader.readLine()) != null) { - final String finalLine = line; - post(() -> listener.onLine(finalLine)); - } - post(listener::onComplete); - } catch (IOException e) { - postFailure(listener, String.valueOf(e)); - } - } - }); - } - - public void cancel() { - cancelled = true; - call.cancel(); - } - - private void post(Runnable runnable) { - if (cancelled) { - return; - } - callbackHandler.post(() -> { - if (cancelled) { - return; - } - try { - runnable.run(); - } catch (Throwable t) { - Log.w(TAG, "listener callback failed", t); - } - }); - } - - private void postFailure(FaceclawSseListener listener, String message) { - if (cancelled) { - return; - } - post(() -> listener.onFailure(message)); - } - - private static OkHttpClient getClient() { - OkHttpClient client = sharedClient; - if (client == null) { - synchronized (FaceclawSseRequest.class) { - if (sharedClient == null) { - // Streaming responses can pause between events (e.g. while - // the model thinks), so the read timeout is generous. The - // Anthropic API sends periodic ping events well within it. - sharedClient = new OkHttpClient.Builder() - .connectTimeout(15, TimeUnit.SECONDS) - .readTimeout(180, TimeUnit.SECONDS) - .addInterceptor(FaceclawHttp.userAgentInterceptor()) - .build(); - } - client = sharedClient; - } - } - return client; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.kt new file mode 100644 index 00000000..245ebc95 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSseRequest.kt @@ -0,0 +1,179 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.util.Log + +import java.io.IOException +import java.util.concurrent.TimeUnit + +import okhttp3.Call +import okhttp3.Callback +import okhttp3.MediaType +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.Response + +/** + * Streaming HTTP POST for the TypeScript side, used for server-sent-events + * APIs (e.g. the Anthropic Messages API with stream=true). The response body + * is delivered line by line as it arrives, so the JS side can parse SSE + * events incrementally. Like FaceclawWebSocket, listener callbacks are posted + * to the Looper of the thread that constructed this object. + * + * The line splitting, error-body and overflow policy live in the shared + * SseStream; this class only supplies okhttp as the transport and the Handler + * delivery. + */ +class FaceclawSseRequest +/** + * Starts the request immediately. headers is a flat alternating + * [name, value, name, value, ...] array (okhttp Headers can't cross the + * JS bridge, and parallel arrays are easy to build with Array.create). + */ +constructor(url: String?, jsonBody: String?, headers: Array?, listener: FaceclawSseListener?) { + private val callbackHandler: Handler + private val stream = SseStream() + private val transfer: Cancellable + @Volatile + private var cancelled = false + + init { + if (url == null || url.trim().isEmpty()) { + throw IllegalArgumentException("url is required") + } + if (listener == null) { + throw IllegalArgumentException("listener is required") + } + val looper = Looper.myLooper() + callbackHandler = Handler(looper ?: Looper.getMainLooper()) + val sink = object : ChunkSink { + override fun onStatus(code: Int) { + stream.onStatus(code) + } + + override fun onChunk(bytes: ByteArray, offset: Int, length: Int) { + stream.onChunk(bytes, offset, length) + drain(listener) + } + + override fun onComplete() { + stream.onComplete() + drain(listener) + } + + override fun onFailure(message: String) { + stream.onFailure(message) + drain(listener) + } + } + transfer = OkHttpStreaming.post(url.trim(), jsonBody ?: "", SseStream.unpackHeaders(headers), sink) + } + + fun cancel() { + cancelled = true + stream.cancel() + transfer.cancel() + } + + /** Hands every queued event to the listener on the constructing thread, in order. */ + private fun drain(listener: FaceclawSseListener) { + val events = stream.takeEvents() + if (events.isEmpty()) return + post { + for (event in events) { + if (cancelled) return@post + try { + SseStream.deliver(event, listener) + } catch (t: Throwable) { + Log.w(TAG, "listener callback failed", t) + } + } + } + } + + private fun post(runnable: Runnable) { + if (cancelled) { + return + } + callbackHandler.post { + if (cancelled) { + return@post + } + runnable.run() + } + } + + /** okhttp transport: enqueues the call and feeds the body to the sink in chunks. */ + private object OkHttpStreaming : StreamingHttp { + override fun post(url: String, body: String, headers: List>, sink: ChunkSink): Cancellable { + val builder = Request.Builder() + .url(url) + .post(body.toRequestBody(JSON)) + for ((name, value) in headers) builder.addHeader(name, value) + val call = getClient().newCall(builder.build()) + call.enqueue(object : Callback { + override fun onFailure(call: Call, e: IOException) { + sink.onFailure(e.toString()) + } + + override fun onResponse(call: Call, response: Response) { + try { + response.use { + sink.onStatus(response.code) + val input = response.body?.byteStream() + if (input != null) { + val buffer = ByteArray(8192) + while (true) { + val read = input.read(buffer) + if (read < 0) break + if (read > 0) sink.onChunk(buffer, 0, read) + } + } + sink.onComplete() + } + } catch (e: IOException) { + sink.onFailure(e.toString()) + } + } + }) + return object : Cancellable { + override fun cancel() = call.cancel() + } + } + } + + companion object { + private const val TAG = "FaceclawSseRequest" + private val JSON: MediaType = "application/json; charset=utf-8".toMediaType() + @Volatile + private var sharedClient: OkHttpClient? = null + + private fun getClient(): OkHttpClient { + var client = sharedClient + if (client == null) { + synchronized(FaceclawSseRequest::class.java) { + if (sharedClient == null) { + // Streaming responses can pause between events (e.g. while + // the model thinks), so the read timeout is generous. The + // Anthropic API sends periodic ping events well within it. + // Redirects are not followed: provider credentials in the + // headers must never be forwarded to another endpoint (a + // 3xx surfaces to the listener as an HTTP error instead). + sharedClient = OkHttpClient.Builder() + .connectTimeout(15, TimeUnit.SECONDS) + .readTimeout(180, TimeUnit.SECONDS) + .followRedirects(false) + .followSslRedirects(false) + .addInterceptor(FaceclawHttp.userAgentInterceptor()) + .build() + } + client = sharedClient + } + } + return client!! + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.java deleted file mode 100644 index 34209e16..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.java +++ /dev/null @@ -1,193 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; -import android.net.ConnectivityManager; -import android.net.Network; -import android.net.NetworkCapabilities; -import android.net.wifi.WifiInfo; -import android.net.wifi.WifiManager; -import android.os.Build; -import android.telephony.SignalStrength; -import android.telephony.TelephonyManager; -import android.util.Log; - -import java.io.ByteArrayOutputStream; -import java.lang.reflect.Method; - -public final class FaceclawSystemStatusIconProvider { - private static final String TAG = "FaceclawStatusIcons"; - private static final int ICON_SIZE = 24; - - private FaceclawSystemStatusIconProvider() { - } - - public static byte[] getSystemStatusIconGrays(Context context, int iconSize) { - if (context == null) { - return new byte[0]; - } - Context appContext = context.getApplicationContext(); - int size = Math.max(1, Math.min(96, iconSize)); - ByteArrayOutputStream out = new ByteArrayOutputStream(size * size * 3); - - int wifiLevel = getWifiLevel(appContext); - if (wifiLevel >= 0) { - append(out, scaleIcon(drawWifiIcon(wifiLevel), size)); - } - - int cellLevel = getCellLevel(appContext); - if (cellLevel >= 0) { - append(out, scaleIcon(drawCellIcon(cellLevel), size)); - } - - if (isHotspotEnabled(appContext)) { - append(out, scaleIcon(drawHotspotIcon(), size)); - } - - return out.toByteArray(); - } - - private static int getWifiLevel(Context context) { - try { - ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - Network activeNetwork = connectivityManager == null ? null : connectivityManager.getActiveNetwork(); - NetworkCapabilities capabilities = activeNetwork == null ? null : connectivityManager.getNetworkCapabilities(activeNetwork); - if (capabilities == null || !capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { - return -1; - } - WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); - WifiInfo info = wifiManager == null ? null : wifiManager.getConnectionInfo(); - if (info == null || !isValidRssi(info.getRssi())) { - return -1; - } - return Math.max(0, Math.min(4, WifiManager.calculateSignalLevel(info.getRssi(), 5))); - } catch (Throwable t) { - Log.w(TAG, "failed to read Wi-Fi status", t); - return -1; - } - } - - private static int getCellLevel(Context context) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { - return -1; - } - try { - TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); - SignalStrength signalStrength = telephonyManager == null ? null : telephonyManager.getSignalStrength(); - if (signalStrength == null) { - return -1; - } - return Math.max(0, Math.min(4, signalStrength.getLevel())); - } catch (SecurityException e) { - // Most Android versions require READ_PHONE_STATE for this. We skip it - // rather than prompting for a broad phone permission just for an icon. - return -1; - } catch (Throwable t) { - Log.w(TAG, "failed to read cellular status", t); - return -1; - } - } - - private static boolean isHotspotEnabled(Context context) { - try { - WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); - if (wifiManager == null) { - return false; - } - Method method = wifiManager.getClass().getDeclaredMethod("getWifiApState"); - method.setAccessible(true); - Object result = method.invoke(wifiManager); - int state = result instanceof Integer ? (Integer) result : -1; - return state == 13 || state == 12; // WIFI_AP_STATE_ENABLED / ENABLING - } catch (Throwable ignored) { - return false; - } - } - - private static boolean isValidRssi(int rssi) { - return rssi > -127 && rssi < 0; - } - - private static byte[] drawWifiIcon(int level) { - byte[] icon = new byte[ICON_SIZE * ICON_SIZE]; - int cx = 12; - int cy = 18; - fillRect(icon, cx - 1, cy - 1, 3, 3, 230); - int[] radii = new int[] {5, 9, 13, 17}; - for (int index = 0; index < radii.length; index++) { - if (level <= index) { - continue; - } - drawWifiArc(icon, cx, cy, radii[index], 210); - } - return icon; - } - - private static void drawWifiArc(byte[] icon, int cx, int cy, int radius, int value) { - int inner = (radius - 1) * (radius - 1); - int outer = (radius + 1) * (radius + 1); - for (int y = 0; y < ICON_SIZE; y++) { - for (int x = 0; x < ICON_SIZE; x++) { - int dx = x - cx; - int dy = y - cy; - int dist = dx * dx + dy * dy; - if (dy <= 0 && dist >= inner && dist <= outer && Math.abs(dx) <= radius) { - setPixel(icon, x, y, value); - } - } - } - } - - private static byte[] drawCellIcon(int level) { - byte[] icon = new byte[ICON_SIZE * ICON_SIZE]; - for (int bar = 0; bar < 4; bar++) { - int x = 5 + bar * 4; - int height = 5 + bar * 4; - int value = level > bar ? 220 : 55; - fillRect(icon, x, 20 - height, 3, height, value); - } - return icon; - } - - private static byte[] drawHotspotIcon() { - byte[] icon = new byte[ICON_SIZE * ICON_SIZE]; - fillRect(icon, 11, 17, 3, 3, 230); - drawWifiArc(icon, 12, 19, 7, 210); - drawWifiArc(icon, 12, 19, 12, 210); - fillRect(icon, 6, 5, 12, 2, 170); - return icon; - } - - private static byte[] scaleIcon(byte[] source, int size) { - if (size == ICON_SIZE) { - return source; - } - byte[] scaled = new byte[size * size]; - for (int y = 0; y < size; y++) { - int sy = Math.min(ICON_SIZE - 1, (y * ICON_SIZE) / size); - for (int x = 0; x < size; x++) { - int sx = Math.min(ICON_SIZE - 1, (x * ICON_SIZE) / size); - scaled[y * size + x] = source[sy * ICON_SIZE + sx]; - } - } - return scaled; - } - - private static void fillRect(byte[] icon, int x, int y, int width, int height, int value) { - for (int row = y; row < y + height; row++) { - for (int col = x; col < x + width; col++) { - setPixel(icon, col, row, value); - } - } - } - - private static void setPixel(byte[] icon, int x, int y, int value) { - if (x < 0 || y < 0 || x >= ICON_SIZE || y >= ICON_SIZE) { - return; - } - icon[y * ICON_SIZE + x] = (byte) Math.max(0, Math.min(255, value)); - } - - private static void append(ByteArrayOutputStream out, byte[] bytes) { - out.write(bytes, 0, bytes.length); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.kt new file mode 100644 index 00000000..b5108f48 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawSystemStatusIconProvider.kt @@ -0,0 +1,113 @@ +package com.faceclaw.app + +import android.content.Context +import android.net.ConnectivityManager +import android.net.Network +import android.net.NetworkCapabilities +import android.net.wifi.WifiInfo +import android.net.wifi.WifiManager +import android.os.Build +import android.telephony.SignalStrength +import android.telephony.TelephonyManager +import android.util.Log + +import java.io.ByteArrayOutputStream +import java.lang.reflect.Method + +/** Reads Wi-Fi/cell/hotspot state; the icon artwork is the shared StatusIconArt. */ +class FaceclawSystemStatusIconProvider private constructor() { + companion object { + private const val TAG = "FaceclawStatusIcons" + + @JvmStatic + fun getSystemStatusIconGrays(context: Context?, iconSize: Int): ByteArray { + if (context == null) { + return ByteArray(0) + } + val appContext = context.applicationContext + val size = Math.max(1, Math.min(96, iconSize)) + val out = ByteArrayOutputStream(size * size * 3) + + val wifiLevel = getWifiLevel(appContext) + if (wifiLevel >= 0) { + append(out, StatusIconArt.scale(StatusIconArt.wifi(wifiLevel), size)) + } + + val cellLevel = getCellLevel(appContext) + if (cellLevel >= 0) { + append(out, StatusIconArt.scale(StatusIconArt.cell(cellLevel), size)) + } + + if (isHotspotEnabled(appContext)) { + append(out, StatusIconArt.scale(StatusIconArt.hotspot(), size)) + } + + return out.toByteArray() + } + + private fun getWifiLevel(context: Context): Int { + try { + val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager? + val activeNetwork: Network? = if (connectivityManager == null) null else connectivityManager.activeNetwork + val capabilities: NetworkCapabilities? = if (activeNetwork == null) null else connectivityManager!!.getNetworkCapabilities(activeNetwork) + if (capabilities == null || !capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { + return -1 + } + val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager? + val info: WifiInfo? = if (wifiManager == null) null else wifiManager.connectionInfo + if (info == null || !isValidRssi(info.rssi)) { + return -1 + } + return Math.max(0, Math.min(4, WifiManager.calculateSignalLevel(info.rssi, 5))) + } catch (t: Throwable) { + Log.w(TAG, "failed to read Wi-Fi status", t) + return -1 + } + } + + private fun getCellLevel(context: Context): Int { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { + return -1 + } + try { + val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager? + val signalStrength: SignalStrength? = if (telephonyManager == null) null else telephonyManager.signalStrength + if (signalStrength == null) { + return -1 + } + return Math.max(0, Math.min(4, signalStrength.level)) + } catch (e: SecurityException) { + // Most Android versions require READ_PHONE_STATE for this. We skip it + // rather than prompting for a broad phone permission just for an icon. + return -1 + } catch (t: Throwable) { + Log.w(TAG, "failed to read cellular status", t) + return -1 + } + } + + private fun isHotspotEnabled(context: Context): Boolean { + try { + val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager? + if (wifiManager == null) { + return false + } + val method: Method = wifiManager.javaClass.getDeclaredMethod("getWifiApState") + method.isAccessible = true + val result: Any? = method.invoke(wifiManager) + val state = if (result is Int) result else -1 + return state == 13 || state == 12 // WIFI_AP_STATE_ENABLED / ENABLING + } catch (ignored: Throwable) { + return false + } + } + + private fun isValidRssi(rssi: Int): Boolean { + return rssi > -127 && rssi < 0 + } + + private fun append(out: ByteArrayOutputStream, bytes: ByteArray) { + out.write(bytes, 0, bytes.size) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.java deleted file mode 100644 index 37a53d58..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.java +++ /dev/null @@ -1,1267 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; -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.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.OfflineWhisperModelConfig; - -import java.io.File; -import java.nio.charset.StandardCharsets; -import java.util.ArrayDeque; -import java.util.Arrays; - -public class FaceclawVoiceController { - private static final String TAG = "FaceclawVoice"; - private static final int SAMPLE_RATE = 16000; - private static final int FEATURE_DIM = 80; - private static final int MAX_AUDIO_QUEUE_PACKETS = 80; - private static final int EXPECTED_PACKET_INTERVAL_MS = 50; - private static final int LATE_PACKET_INTERVAL_MS = 90; - private static final int STATS_INTERVAL_MS = 5_000; - // Push-to-talk utterance boundaries come from the button. We re-decode the - // current audio segment in full for each live partial and emit the complete - // utterance text (REPLACE, never a delta). The sherpa Moonshine v2 decoder - // used here fails once a single input grows past roughly 9.1 seconds, so - // longer utterances are committed in model-safe segments. - private static final int TRANSCRIPT_DECODE_INTERVAL_MS = 700; - private static final int TRANSCRIPT_MIN_SAMPLES = SAMPLE_RATE / 3; - private static final int TRANSCRIPT_SEGMENT_MAX_SAMPLES = SAMPLE_RATE * 8; - // When a segment fills, cut at the quietest window within the last - // TRANSCRIPT_CUT_SEARCH_SAMPLES rather than mid-word at the 8s mark; the - // audio after the cut carries over into the next segment. - private static final int TRANSCRIPT_CUT_SEARCH_SAMPLES = SAMPLE_RATE * 2; - private static final int TRANSCRIPT_CUT_WINDOW_SAMPLES = SAMPLE_RATE * 30 / 1000; - // Glasses-mic PCM peaks around 0.1 full scale, and at that level the - // quantized Moonshine model often returns empty or garbled text. Boost - // each decode window toward this peak, with a gain cap so near-silent - // buffers aren't amplified into pure noise. - 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; - // 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"; - // 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", - "tokens.txt" - }; - // Second on-device model: sherpa-onnx's offline Whisper backend (base.en, - // int8-quantized -- see the model-choice note in asr-model.ts). Directory - // shared with the TS-side download flow, same convention as ASR_MODEL_DIR. - // - // On every call, sherpa-onnx (offline-recognizer-whisper-impl.h, v1.13.0) - // runs Whisper's encoder over the whole buffer plus 1000 frames (~10s) of - // zero tail padding, capped at 30s. Re-decoding on Moonshine's - // TRANSCRIPT_DECODE_INTERVAL_MS live-partial cadence would repeat that - // encode, growing with the utterance, roughly 1.4x/second while the user is - // still speaking, so onboardModelKind gates that loop off for Whisper -- - // see processRecognizer(). Whisper is also known to hallucinate text on - // near-silent input; recognizeTranscriptSegment() gates that too. - private static final String ASR_WHISPER_MODEL_DIR = "sherpa-onnx-whisper-base-en-int8"; - private static final String[] ASR_WHISPER_MODEL_FILES = { - "base.en-encoder.int8.onnx", - "base.en-decoder.int8.onnx", - "base.en-tokens.txt" - }; - // Below this peak amplitude (pre-normalization, of a full-scale +/-1.0f - // buffer) a segment is treated as silence and never reaches the Whisper - // recognizer at all, rather than risking a hallucinated non-answer. Picked - // conservatively low (well under typical mic noise floor already seen in - // this pipeline's normalization target) -- UNTESTED on real hardware, tune - // against real glasses captures rather than trusting this number. - private static final float WHISPER_SILENCE_PEAK_THRESHOLD = 0.01f; - - private enum VoiceInputMode { - ONBOARD, // on-phone transcription (Moonshine or Whisper; see onboardModelKind) - CLOUD // decode locally, emit PCM for a cloud recognizer on the TS side - } - - /** Which on-device model ONBOARD mode uses. Set via setOnboardModelKind() before start(). */ - private enum OnboardModelKind { - MOONSHINE, - WHISPER - } - - private final Context appContext; - private final Handler mainHandler = new Handler(Looper.getMainLooper()); - private final Object lock = new Object(); - private final Object audioQueueLock = new Object(); - private final ArrayDeque audioQueue = new ArrayDeque<>(); - private volatile FaceclawVoiceControllerListener listener; - private volatile FaceclawBleCommunicator communicator; - private Thread workerThread; - private volatile boolean started; - // Set once the worker has the glasses mic enabled for this session. - // Read and written under `lock`, so it flips with `started` atomically. - private boolean audioStarted; - // Capture from the phone's own microphone instead of the G2 over BLE - // (preview-only mode, where no glasses are connected). Latched into - // activePhoneMic at start() (under `lock`) so a mid-session setter call - // can't switch pipelines underneath the worker. - private volatile boolean usePhoneMic; - private boolean activePhoneMic; - private VoiceInputMode mode = VoiceInputMode.CLOUD; - private volatile OnboardModelKind onboardModelKind = OnboardModelKind.MOONSHINE; - private OfflineRecognizer recognizer; - private FaceclawLc3Decoder lc3Decoder; - private final float[] transcriptSamples = new float[TRANSCRIPT_SEGMENT_MAX_SAMPLES]; - private int transcriptSampleCount; - private long committedTranscriptSampleCount; - private String committedTranscript = ""; - private String currentSegmentTranscript = ""; - private long lastTranscriptDecodeAtMs; - private String lastTranscript = ""; - private volatile boolean saveRecordings; - private volatile boolean endpointing; - private final EndpointDetector endpointDetector = new EndpointDetector(); - private java.io.ByteArrayOutputStream recordingPcm; - // Speaker verification against the enrolled wearer voice-print ("my voice - // only" command gating). Configured before start(); the utterance PCM is - // buffered (capped) and verified once at session end. - private static final int VERIFY_MAX_SAMPLES = SAMPLE_RATE * 10; - private static final int VERIFY_MIN_SAMPLES = SAMPLE_RATE; - private volatile String verifySpeakerModelPath; - private volatile float[] verifyWearerEmbedding; - private volatile float verifyThreshold = 0.8f; - private short[] verifyBuffer; - private int verifyCount; - // Global mic processing (Microphones app config): spectral noise - // suppression and firmware-DoA beam gating, applied to every capture - // session that opts in (assistant push-to-talk, Transcribe, hands-free). - // The raw tap opts out — raw means raw, and the Microphones session does - // its own beam-compensated processing on that path. - private volatile boolean suppressionEnabled; - private volatile boolean beamFilterEnabled; - private volatile int beamCenterDeg; - private volatile int beamHalfWidthDeg = 180; - private FaceclawNoiseSuppressor suppressor; - private long queuedPackets; - private long queueDroppedPackets; - private long decodedSamples; - private long latePackets; - private long wrongArmPackets; - private long lastPacketArrivalMs; - private long maxInterPacketMs; - private long lastStatsAtMs; - - public FaceclawVoiceController(Context context) { - this.appContext = context.getApplicationContext(); - } - - public void setListener(FaceclawVoiceControllerListener listener) { - this.listener = listener; - } - - public void setCommunicator(FaceclawBleCommunicator communicator) { - this.communicator = communicator; - } - - /** Source the next capture from the phone microphone (no glasses paired). */ - public void setUsePhoneMic(boolean usePhoneMic) { - this.usePhoneMic = usePhoneMic; - } - - /** When true, the decoded mic PCM for each session is saved as a WAV. */ - public void setSaveRecordings(boolean saveRecordings) { - this.saveRecordings = saveRecordings; - } - - /** - * Which on-device model {@link #start}("onboard") should load: "whisper" - * selects the second on-device model (sherpa-onnx offline Whisper); any - * other value (including null/absent) keeps the existing Moonshine model, - * so callers that never call this see unchanged behavior. Must be set - * before {@link #start}; has no effect in CLOUD mode. - */ - public void setOnboardModelKind(String kind) { - this.onboardModelKind = "whisper".equals(kind) ? OnboardModelKind.WHISPER : OnboardModelKind.MOONSHINE; - } - - /** - * When true, watch the decoded PCM and fire {@code onSpeechEnd} once the - * speaker stops. Used by hands-free ("Hey Even") capture, which has no - * button release to end the utterance. Must be set before {@link #start}. - */ - public void setEndpointing(boolean endpointing) { - this.endpointing = endpointing; - } - - /** - * Verify this session's speaker against the enrolled wearer voice-print - * and report the result via onSpeakerVerified just before the final - * transcript. Must be set before {@link #start}; pass a null model path - * to disable. - */ - public void setSpeakerVerification(String speakerModelPath, float[] wearerEmbedding, float threshold) { - this.verifySpeakerModelPath = speakerModelPath; - this.verifyWearerEmbedding = wearerEmbedding; - this.verifyThreshold = threshold > 0 ? threshold : 0.8f; - } - - public void clearSpeakerVerification() { - this.verifySpeakerModelPath = null; - this.verifyWearerEmbedding = null; - } - - /** Spectral noise suppression on the decoded stream. Safe to flip mid-run. */ - public void setNoiseSuppression(boolean enabled) { - this.suppressionEnabled = enabled; - } - - /** - * Direction gating from the Sonic Radar beam: packets whose firmware - * direction-of-arrival falls outside centerDeg ± halfWidthDeg (device - * frame, 0 = straight ahead, positive right) are dropped before any - * consumer sees them. Safe to update mid-run. - */ - public void setBeamFilter(boolean enabled, int centerDeg, int halfWidthDeg) { - this.beamFilterEnabled = enabled; - this.beamCenterDeg = centerDeg; - this.beamHalfWidthDeg = Math.max(5, Math.min(180, halfWidthDeg)); - } - - private boolean withinBeam(int angleDegrees) { - int delta = angleDegrees - beamCenterDeg; - while (delta > 180) delta -= 360; - while (delta < -180) delta += 360; - return Math.abs(delta) <= beamHalfWidthDeg; - } - - private void applySuppression(short[] pcm, int count) { - try { - if (suppressor == null) { - suppressor = new FaceclawNoiseSuppressor(SAMPLE_RATE); - } - byte[] le = new byte[count * 2]; - for (int i = 0; i < count; i++) { - le[i * 2] = (byte) (pcm[i] & 0xff); - le[i * 2 + 1] = (byte) ((pcm[i] >> 8) & 0xff); - } - byte[] cleaned = suppressor.process(le); - int cleanedCount = Math.min(count, cleaned.length / 2); - for (int i = 0; i < cleanedCount; i++) { - pcm[i] = (short) ((cleaned[i * 2] & 0xff) | (cleaned[i * 2 + 1] << 8)); - } - } catch (Throwable t) { - Log.w(TAG, "noise suppression failed; passing audio through", t); - suppressionEnabled = false; - } - } - - public void start(String requestedMode) { - start(requestedMode, 0); - } - - public void start(String requestedMode, int captureId) { - synchronized (lock) { - if (started) { - emitStatus("Voice control is already listening."); - emitStopped(captureId); - return; - } - if (!usePhoneMic && (communicator == null || !communicator.isSessionReady())) { - emitStatus("Voice control needs an active G2 connection."); - emitStopped(captureId); - return; - } - mode = parseMode(requestedMode); - activePhoneMic = usePhoneMic; - started = true; - audioStarted = false; - workerThread = new Thread(() -> runLoop(captureId), "FaceclawVoiceController"); - workerThread.start(); - } - } - - /** - * Whether mic audio is actually flowing. {@link #start} only records - * intent: the enable lives in the glasses' EvenHub session, so a transport - * drop or a session suspend can leave this controller started with a - * worker that will never see another packet. Anything deciding whether to - * (re)start capture must ask this rather than assume its own bookkeeping. - */ - public boolean isCapturing() { - boolean audioUp; - boolean phoneMic; - synchronized (lock) { - if (!started) { - return false; - } - audioUp = audioStarted; - phoneMic = activePhoneMic; - } - if (!audioUp) { - // The worker is still bringing the mic up; report it as running so - // a concurrent request shares it instead of restarting it. - return true; - } - if (phoneMic) { - // AudioRecord has no session to lose the enable to; it runs until stop(). - return true; - } - FaceclawBleCommunicator currentCommunicator = communicator; - return currentCommunicator != null && currentCommunicator.isAudioCaptureActive(); - } - - public void stop() { - Thread threadToJoin; - synchronized (lock) { - if (!started) { - return; - } - started = false; - threadToJoin = workerThread; - } - stopG2Audio(); - synchronized (audioQueueLock) { - audioQueueLock.notifyAll(); - } - if (threadToJoin != null) { - threadToJoin.interrupt(); - if (Thread.currentThread() != threadToJoin) { - try { - threadToJoin.join(1500); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - } - } - - public void close() { - stop(); - } - - private VoiceInputMode parseMode(String requestedMode) { - if ("cloud".equals(requestedMode)) { - return VoiceInputMode.CLOUD; - } - return VoiceInputMode.ONBOARD; - } - - private void runLoop(int captureId) { - try { - deleteLegacyKwsFiles(); - VoiceInputMode currentMode = mode; - OnboardModelKind currentOnboardKind = onboardModelKind; - if (currentMode == VoiceInputMode.ONBOARD) { - File modelDir = findAsrModelDir(currentOnboardKind); - if (modelDir == null) { - emitStatus("Voice model not downloaded (see Settings > Voice)."); - return; - } - emitStatus("Loading transcription model..."); - recognizer = new OfflineRecognizer(buildRecognizerConfig(modelDir, currentOnboardKind)); - resetTranscriptState(); - lastTranscript = ""; - } - endpointDetector.reset(); - if (suppressor != null) { - suppressor.reset(); - } - recordingPcm = saveRecordings ? new java.io.ByteArrayOutputStream(SAMPLE_RATE * 2 * 4) : null; - boolean verifying = verifySpeakerModelPath != null && verifyWearerEmbedding != null; - verifyBuffer = verifying ? new short[VERIFY_MAX_SAMPLES] : null; - verifyCount = 0; - if (activePhoneMic) { - android.media.AudioRecord record = openPhoneMic(); - if (record == null) { - emitStatus("Could not start the phone microphone."); - return; - } - synchronized (lock) { - audioStarted = true; - } - emitStatus(currentMode == VoiceInputMode.CLOUD - ? "Listening (cloud)..." - : "Listening..."); - try { - processPhoneAudio(record); - } finally { - try { - record.stop(); - } catch (Throwable ignored) { - // Already stopped or never recording; release below either way. - } - record.release(); - } - } else { - lc3Decoder = new FaceclawLc3Decoder(); - if (!startG2Audio()) { - emitStatus("Could not start G2 microphone input."); - return; - } - synchronized (lock) { - audioStarted = true; - } - emitStatus(currentMode == VoiceInputMode.CLOUD - ? "Listening (cloud)..." - : "Listening..."); - processG2Audio(); - } - // Verification result must precede the final transcript so the - // TS bridge can suppress a non-wearer command before it is acted - // on (the callbacks are posted in order to the main handler). - runSpeakerVerification(); - // Button released / stop requested: emit one final full-utterance - // transcript so the UI can freeze it. - if (currentMode == VoiceInputMode.ONBOARD) { - decodeTranscript(true); - } - } catch (Throwable error) { - Log.e(TAG, "Voice control failed", error); - emitStatus("Voice control failed: " + error.getMessage()); - } finally { - stopG2Audio(); - writeRecordingIfAny(); - releaseSherpa(); - releaseLc3(); - synchronized (lock) { - started = false; - audioStarted = false; - workerThread = null; - } - emitStopped(captureId); - } - } - - private void appendRecording(short[] pcm, int count) { - java.io.ByteArrayOutputStream out = recordingPcm; - if (out == null) { - return; - } - for (int i = 0; i < count; i++) { - short s = pcm[i]; - out.write(s & 0xff); - out.write((s >> 8) & 0xff); - } - } - - /** Save the session's decoded mic PCM as a 16 kHz mono 16-bit WAV. */ - private void writeRecordingIfAny() { - java.io.ByteArrayOutputStream out = recordingPcm; - recordingPcm = null; - if (out == null || out.size() == 0) { - return; - } - try { - byte[] pcmBytes = out.toByteArray(); - java.io.File dir = new java.io.File(appContext.getExternalFilesDir(null), "voice-recordings"); - if (!dir.exists() && !dir.mkdirs()) { - Log.w(TAG, "could not create voice-recordings dir"); - return; - } - String stamp = new java.text.SimpleDateFormat("yyyyMMdd-HHmmss-SSS", java.util.Locale.US) - .format(new java.util.Date()); - java.io.File file = new java.io.File(dir, "voice-" + stamp + ".wav"); - try (java.io.FileOutputStream fos = new java.io.FileOutputStream(file)) { - fos.write(buildWavHeader(pcmBytes.length, SAMPLE_RATE, 1, 16)); - fos.write(pcmBytes); - } - Log.i(TAG, "saved voice recording " + file.getAbsolutePath() - + " samples=" + (pcmBytes.length / 2) - + " sec=" + String.format(java.util.Locale.US, "%.2f", pcmBytes.length / 2.0 / SAMPLE_RATE)); - } catch (Throwable t) { - Log.w(TAG, "failed to save voice recording", t); - } - } - - private static byte[] buildWavHeader(int pcmBytes, int sampleRate, int channels, int bitsPerSample) { - int byteRate = sampleRate * channels * bitsPerSample / 8; - int blockAlign = channels * bitsPerSample / 8; - int dataSize = pcmBytes; - int riffSize = 36 + dataSize; - java.nio.ByteBuffer b = java.nio.ByteBuffer.allocate(44).order(java.nio.ByteOrder.LITTLE_ENDIAN); - b.put("RIFF".getBytes(StandardCharsets.US_ASCII)); - b.putInt(riffSize); - b.put("WAVE".getBytes(StandardCharsets.US_ASCII)); - b.put("fmt ".getBytes(StandardCharsets.US_ASCII)); - b.putInt(16); // PCM fmt chunk size - b.putShort((short) 1); // PCM - b.putShort((short) channels); - b.putInt(sampleRate); - b.putInt(byteRate); - b.putShort((short) blockAlign); - b.putShort((short) bitsPerSample); - b.put("data".getBytes(StandardCharsets.US_ASCII)); - b.putInt(dataSize); - return b.array(); - } - - private OfflineRecognizerConfig buildRecognizerConfig(File modelDir, OnboardModelKind kind) { - OfflineModelConfig.Builder modelConfig = OfflineModelConfig.builder() - .setNumThreads(1); - if (kind == OnboardModelKind.WHISPER) { - modelConfig - .setWhisper(OfflineWhisperModelConfig.builder() - .setEncoder(new File(modelDir, "base.en-encoder.int8.onnx").getAbsolutePath()) - .setDecoder(new File(modelDir, "base.en-decoder.int8.onnx").getAbsolutePath()) - .setLanguage("en") - .setTask("transcribe") - .build()) - .setTokens(new File(modelDir, "base.en-tokens.txt").getAbsolutePath()); - } else { - modelConfig - .setMoonshine(OfflineMoonshineModelConfig.builder() - .setEncoder(new File(modelDir, "encoder_model.ort").getAbsolutePath()) - .setMergedDecoder(new File(modelDir, "decoder_model_merged.ort").getAbsolutePath()) - .build()) - .setTokens(new File(modelDir, "tokens.txt").getAbsolutePath()); - } - return OfflineRecognizerConfig.builder() - .setFeatureConfig(FeatureConfig.builder() - .setSampleRate(SAMPLE_RATE) - .setFeatureDim(FEATURE_DIM) - .build()) - .setModelConfig(modelConfig.build()) - .build(); - } - - /** - * The on-device model directory for the given kind, populated by the - * download flow in asr-model.ts (releases before 0.5.0 copied the - * Moonshine files out of the APK directly, so upgraded installs are - * already complete for that model). Null when any expected file is - * missing, i.e. the model still needs to be downloaded. - */ - private File findAsrModelDir(OnboardModelKind kind) { - boolean whisper = kind == OnboardModelKind.WHISPER; - String dirName = whisper ? ASR_WHISPER_MODEL_DIR : ASR_MODEL_DIR; - String[] fileNames = whisper ? ASR_WHISPER_MODEL_FILES : ASR_MODEL_FILES; - File modelDir = new File(appContext.getFilesDir(), ASR_ROOT + File.separator + dirName); - for (String fileName : fileNames) { - 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() { - FaceclawBleCommunicator currentCommunicator = communicator; - if (currentCommunicator == null) { - return false; - } - resetAudioStats(); - synchronized (audioQueueLock) { - audioQueue.clear(); - } - return currentCommunicator.startG2AudioCapture(this::queueAudioPacket); - } - - private void processG2Audio() { - short[] pcm = new short[FaceclawLc3Decoder.SAMPLES_PER_PACKET]; - while (started && !Thread.currentThread().isInterrupted()) { - FaceclawLc3Decoder currentDecoder = lc3Decoder; - if (currentDecoder == null) { - return; - } - - AudioPacket packet = takeAudioPacket(); - if (packet == null) { - continue; - } - - int count = currentDecoder.decodePacket(packet.data, pcm); - if (count <= 0) { - maybeEmitAudioStats(false); - continue; - } - decodedSamples += count; - // Global mic processing from the Microphones app config: the - // beam filter drops packets whose firmware direction-of-arrival - // falls outside the listening wedge (isolating the aimed talker - // for every consumer, recognition included), and the spectral - // noise suppressor cleans what remains before it reaches the - // recognizer, cloud PCM, endpointing, or speaker verification. - int angleDegrees = currentDecoder.getLastAngleDegrees(); - int ssr = currentDecoder.getLastSsr(); - if (beamFilterEnabled && ssr > 0 && !withinBeam(angleDegrees)) { - emitFrameMeta(angleDegrees, ssr); - maybeEmitAudioStats(false); - continue; - } - processPcmChunk(pcm, count, angleDegrees, ssr, true); - maybeEmitAudioStats(false); - } - } - - /** - * Per-chunk processing shared by the G2 and phone-mic paths, downstream of - * decode and the beam filter. hasFrameMeta is false for the phone mic, - * which has no firmware DSP metadata to report. - */ - private void processPcmChunk(short[] pcm, int count, int angleDegrees, int ssr, boolean hasFrameMeta) { - if (suppressionEnabled) { - applySuppression(pcm, count); - } - if (recordingPcm != null) { - appendRecording(pcm, count); - } - if (verifyBuffer != null && verifyCount < VERIFY_MAX_SAMPLES) { - int copied = Math.min(count, VERIFY_MAX_SAMPLES - verifyCount); - System.arraycopy(pcm, 0, verifyBuffer, verifyCount, copied); - verifyCount += copied; - } - if (endpointing && endpointDetector.accept(pcm, count)) { - emitSpeechEnd(); - } - // PCM and frame metadata flow in every mode so levels, recording, - // and the Microphones radar keep working alongside onboard ASR. - emitPcm(pcm, count); - if (hasFrameMeta) { - emitFrameMeta(angleDegrees, ssr); - } - if (mode != VoiceInputMode.CLOUD) { - float[] samples = new float[count]; - for (int i = 0; i < count; i++) { - samples[i] = pcm[i] / 32768.0f; - } - processRecognizer(samples); - } - } - - // 50 ms chunks match the G2 packet cadence the rest of the pipeline - // (endpointing, transcript pacing) is tuned for. - private static final int PHONE_MIC_CHUNK_SAMPLES = SAMPLE_RATE / 20; - - /** - * Open the phone's own microphone at the pipeline's native format - * (16 kHz mono PCM16), or null when it cannot start — the permission is - * missing (SecurityException) or the device refuses the configuration. - */ - private android.media.AudioRecord openPhoneMic() { - android.media.AudioRecord record = null; - try { - int minBytes = android.media.AudioRecord.getMinBufferSize( - SAMPLE_RATE, - android.media.AudioFormat.CHANNEL_IN_MONO, - android.media.AudioFormat.ENCODING_PCM_16BIT); - int bufferBytes = Math.max(minBytes, PHONE_MIC_CHUNK_SAMPLES * 2 * 4); - record = new android.media.AudioRecord( - android.media.MediaRecorder.AudioSource.VOICE_RECOGNITION, - SAMPLE_RATE, - android.media.AudioFormat.CHANNEL_IN_MONO, - android.media.AudioFormat.ENCODING_PCM_16BIT, - bufferBytes); - if (record.getState() != android.media.AudioRecord.STATE_INITIALIZED) { - record.release(); - return null; - } - record.startRecording(); - if (record.getRecordingState() != android.media.AudioRecord.RECORDSTATE_RECORDING) { - record.release(); - return null; - } - return record; - } catch (Throwable t) { - Log.w(TAG, "phone mic open failed", t); - if (record != null) { - record.release(); - } - return null; - } - } - - /** - * Phone-mic capture loop: no LC3 decode, no arm bookkeeping, no frame - * metadata — AudioRecord already delivers the pipeline's PCM format. The - * blocking read returns every chunk (50 ms), which bounds how long a - * stop() waits for the loop to notice `started` dropped. - */ - private void processPhoneAudio(android.media.AudioRecord record) { - short[] pcm = new short[PHONE_MIC_CHUNK_SAMPLES]; - while (started && !Thread.currentThread().isInterrupted()) { - int read = record.read(pcm, 0, pcm.length); - if (read < 0) { - Log.w(TAG, "phone mic read failed: " + read); - return; - } - if (read == 0) { - continue; - } - decodedSamples += read; - processPcmChunk(pcm, read, 0, 0, false); - } - } - - private void processRecognizer(float[] samples) { - appendTranscriptSamples(samples); - // Each Whisper call re-encodes the whole buffer plus ~10s of tail - // padding (see the ASR_WHISPER_* comment above), - // so it skips the live-partial redecode Moonshine does on this - // interval and only decodes when a segment commits (8s buffer fill) - // or the utterance ends (decodeTranscript(true) in runLoop()). This - // means no live preview text while speaking in Whisper mode -- status - // stays "Listening..." until release. This is a deliberate tradeoff. - if (onboardModelKind == OnboardModelKind.WHISPER) { - return; - } - long now = SystemClock.elapsedRealtime(); - if (transcriptSampleCount >= TRANSCRIPT_MIN_SAMPLES - && now - lastTranscriptDecodeAtMs >= TRANSCRIPT_DECODE_INTERVAL_MS) { - decodeTranscript(false); - lastTranscriptDecodeAtMs = now; - } - } - - private void appendTranscriptSamples(float[] samples) { - int sourceOffset = 0; - while (sourceOffset < samples.length) { - int available = TRANSCRIPT_SEGMENT_MAX_SAMPLES - transcriptSampleCount; - int count = Math.min(available, samples.length - sourceOffset); - System.arraycopy(samples, sourceOffset, transcriptSamples, transcriptSampleCount, count); - transcriptSampleCount += count; - sourceOffset += count; - - if (transcriptSampleCount == TRANSCRIPT_SEGMENT_MAX_SAMPLES) { - commitTranscriptSegment(); - } - } - } - - /** - * Decode the current model-safe segment and emit the best transcript of the - * complete utterance (REPLACE semantics — the caller displays it as-is). - */ - private void decodeTranscript(boolean isFinal) { - if (recognizer == null || transcriptSampleCount <= 0) { - if (isFinal) { - emitTranscript(lastTranscript, true); - } - return; - } - int segmentSampleCount = transcriptSampleCount; - String segmentText = recognizeTranscriptSegment(segmentSampleCount); - if (segmentText.length() > 0) { - currentSegmentTranscript = segmentText; - } else { - segmentText = currentSegmentTranscript; - } - String text = joinTranscript(committedTranscript, segmentText); - lastTranscript = text; - logTranscriptDecode(isFinal, segmentSampleCount, text); - emitTranscript(text, isFinal); - } - - /** - * Finalize a full segment before accepting more audio. This keeps every - * Moonshine invocation below its failing sequence length while retaining - * all earlier text in the replace-semantics preview. - */ - private void commitTranscriptSegment() { - int cut = findSegmentCutPoint(); - String segmentText = recognizeTranscriptSegment(cut); - if (segmentText.length() == 0) { - // Fallback text came from partial decodes of the full buffer, so it - // may include words from the carried-over tail; rare now that decode - // windows are peak-normalized. - segmentText = currentSegmentTranscript; - } - committedTranscript = joinTranscript(committedTranscript, segmentText); - currentSegmentTranscript = ""; - committedTranscriptSampleCount += cut; - int tail = transcriptSampleCount - cut; - System.arraycopy(transcriptSamples, cut, transcriptSamples, 0, tail); - transcriptSampleCount = tail; - lastTranscript = committedTranscript; - lastTranscriptDecodeAtMs = SystemClock.elapsedRealtime(); - logTranscriptDecode(false, cut, committedTranscript); - emitTranscript(committedTranscript, false); - } - - /** - * Pick where to end the committed segment: the center of the quietest - * window within the search region at the end of the buffer, so the cut - * lands between words instead of splitting one. - */ - private int findSegmentCutPoint() { - int count = transcriptSampleCount; - int searchStart = Math.max(0, count - TRANSCRIPT_CUT_SEARCH_SAMPLES); - int win = TRANSCRIPT_CUT_WINDOW_SAMPLES; - if (count - searchStart <= win) { - return count; - } - double sum = 0; - for (int i = searchStart; i < searchStart + win; i++) { - sum += (double) transcriptSamples[i] * transcriptSamples[i]; - } - double best = sum; - int bestStart = searchStart; - for (int start = searchStart + 1; start + win <= count; start++) { - float dropped = transcriptSamples[start - 1]; - float added = transcriptSamples[start + win - 1]; - sum += (double) added * added - (double) dropped * dropped; - if (sum < best) { - best = sum; - bestStart = start; - } - } - return bestStart + win / 2; - } - - private String recognizeTranscriptSegment(int sampleCount) { - OfflineRecognizer currentRecognizer = recognizer; - if (currentRecognizer == null || sampleCount <= 0) { - return ""; - } - float[] segment = Arrays.copyOf(transcriptSamples, sampleCount); - // Whisper hallucinates text on near-silent input (a known quirk of the - // model, not this pipeline); gate it on the PRE-normalization peak, since - // normalizePeak() below would otherwise amplify true silence right up to - // the target level and hide the very thing being checked for. Moonshine - // does not share this failure mode in practice, so it is left unchanged. - if (onboardModelKind == OnboardModelKind.WHISPER && peakAmplitude(segment) < WHISPER_SILENCE_PEAK_THRESHOLD) { - return ""; - } - normalizePeak(segment); - OfflineStream offlineStream = currentRecognizer.createStream(); - try { - offlineStream.acceptWaveform(segment, SAMPLE_RATE); - currentRecognizer.decode(offlineStream); - OfflineRecognizerResult result = currentRecognizer.getResult(offlineStream); - String raw = result == null ? "" : result.getText(); - return raw == null ? "" : raw.trim(); - } finally { - offlineStream.release(); - } - } - - private static float peakAmplitude(float[] samples) { - float peak = 0f; - for (float s : samples) { - float a = Math.abs(s); - if (a > peak) { - peak = a; - } - } - return peak; - } - - private static void normalizePeak(float[] samples) { - float peak = peakAmplitude(samples); - if (peak <= 0f) { - return; - } - float gain = Math.min(TRANSCRIPT_NORMALIZE_TARGET_PEAK / peak, TRANSCRIPT_NORMALIZE_MAX_GAIN); - if (gain <= 1f) { - return; - } - for (int i = 0; i < samples.length; i++) { - samples[i] *= gain; - } - } - - private void logTranscriptDecode(boolean isFinal, int segmentSampleCount, String text) { - double totalAudioSec = - (committedTranscriptSampleCount + transcriptSampleCount) / (double) SAMPLE_RATE; - String preview = text.length() <= TRANSCRIPT_LOG_PREVIEW_CHARS - ? text : text.substring(0, TRANSCRIPT_LOG_PREVIEW_CHARS) + "..."; - Log.i(TAG, (onboardModelKind == OnboardModelKind.WHISPER ? "Whisper" : "Moonshine") + " decode final=" + isFinal - + " audioSec=" + String.format(java.util.Locale.US, "%.2f", totalAudioSec) - + " segmentAudioSec=" + String.format(java.util.Locale.US, "%.2f", segmentSampleCount / (double) SAMPLE_RATE) - + " textLen=" + text.length() + " text=\"" + preview + "\""); - } - - private static String joinTranscript(String prefix, String suffix) { - if (prefix == null || prefix.length() == 0) { - return suffix == null ? "" : suffix; - } - if (suffix == null || suffix.length() == 0) { - return prefix; - } - char first = suffix.charAt(0); - boolean attachesToPrevious = ".,!?;:%)]}".indexOf(first) >= 0; - return prefix + (attachesToPrevious ? "" : " ") + suffix; - } - - private void resetTranscriptState() { - transcriptSampleCount = 0; - committedTranscriptSampleCount = 0; - committedTranscript = ""; - currentSegmentTranscript = ""; - lastTranscriptDecodeAtMs = 0; - } - - private void emitPcm(short[] pcm, int count) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null || count <= 0) { - return; - } - byte[] le = new byte[count * 2]; - for (int i = 0; i < count; i++) { - short s = pcm[i]; - le[i * 2] = (byte) (s & 0xff); - le[i * 2 + 1] = (byte) ((s >> 8) & 0xff); - } - mainHandler.post(() -> currentListener.onPcm(le)); - } - - /** - * Embed the session's buffered utterance and compare it to the enrolled - * wearer voice-print. Fails open: a session too short to verify, or a - * model that will not load, counts as the wearer rather than silencing - * every command. - */ - private void runSpeakerVerification() { - short[] buffer = verifyBuffer; - float[] wearer = verifyWearerEmbedding; - String modelPath = verifySpeakerModelPath; - verifyBuffer = null; - if (buffer == null || wearer == null || modelPath == null) { - return; - } - if (verifyCount < VERIFY_MIN_SAMPLES) { - emitSpeakerVerified(true, 0f); - return; - } - FaceclawSpeakerId speakerId = cachedSpeakerId(modelPath); - try { - byte[] le = new byte[verifyCount * 2]; - for (int i = 0; i < verifyCount; i++) { - short s = buffer[i]; - le[i * 2] = (byte) (s & 0xff); - le[i * 2 + 1] = (byte) ((s >> 8) & 0xff); - } - float[] embedding = speakerId.embed(le, SAMPLE_RATE); - if (embedding == null || embedding.length != wearer.length) { - emitSpeakerVerified(true, 0f); - return; - } - double dot = 0; - for (int i = 0; i < embedding.length; i++) { - dot += (double) embedding[i] * wearer[i]; - } - boolean isWearer = dot >= verifyThreshold; - Log.i(TAG, "speaker verification similarity=" + String.format(java.util.Locale.US, "%.3f", dot) - + " threshold=" + verifyThreshold + " isWearer=" + isWearer); - emitSpeakerVerified(isWearer, (float) dot); - } catch (Throwable t) { - Log.w(TAG, "speaker verification failed", t); - emitSpeakerVerified(true, 0f); - } - } - - // The 28 MB embedding model takes seconds to load; keep one instance - // across capture sessions so verification adds only the embed time. - private static FaceclawSpeakerId sharedSpeakerId; - private static String sharedSpeakerIdPath; - - private static synchronized FaceclawSpeakerId cachedSpeakerId(String modelPath) { - if (sharedSpeakerId == null || !modelPath.equals(sharedSpeakerIdPath)) { - if (sharedSpeakerId != null) { - sharedSpeakerId.close(); - } - sharedSpeakerId = new FaceclawSpeakerId(modelPath); - sharedSpeakerIdPath = modelPath; - } - return sharedSpeakerId; - } - - private void emitSpeakerVerified(boolean isWearer, float similarity) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(() -> currentListener.onSpeakerVerified(isWearer, similarity)); - } - - private void emitFrameMeta(int angleDegrees, int ssr) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(() -> currentListener.onFrameMeta(angleDegrees, ssr)); - } - - private void emitSpeechEnd() { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(currentListener::onSpeechEnd); - } - - private void emitStopped(int captureId) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener != null) { - mainHandler.post(() -> currentListener.onStopped(captureId)); - } - } - - /** - * Decides when a hands-free utterance is over, so "Hey Even" capture can - * stop without a button release. - * - * Runs on the decoded 16 kHz PCM, so it works the same in every input mode - * (the cloud path never sees the samples on this side, and the onboard - * recognizer's own endpointing only covers ONBOARD). - * - * Timing is measured on the sample clock rather than the wall clock: BLE - * delivers mic packets in bursts, so elapsed real time badly overestimates - * how much audio has actually been heard. - * - * The threshold is relative to a noise floor measured over the first - * {@link #CALIBRATE_MS} of the session, which is roughly the interval where - * the user is reacting to the dialog appearing and not yet speaking. - */ - private static final class EndpointDetector { - /** Audio used to estimate the room's noise floor. */ - private static final int CALIBRATE_MS = 300; - /** Speech must exceed this multiple of the noise floor to count as onset. */ - private static final double ONSET_FACTOR = 3.0; - /** Below this multiple of the noise floor counts as silence again. */ - private static final double RELEASE_FACTOR = 1.8; - /** Absolute floor, so a silent room can't make the threshold ~0. */ - private static final double MIN_RMS = 220.0; - /** Trailing silence that ends an utterance. */ - private static final int SILENCE_MS = 900; - /** If the user never speaks, give up rather than record forever. */ - private static final int LEAD_IN_MS = 6000; - /** Hard cap on a single utterance. */ - private static final int MAX_UTTERANCE_MS = 30000; - - private long totalSamples; - private double noiseAccum; - private int noisePackets; - private double threshold; - private boolean speechStarted; - private long silenceSamples; - private boolean fired; - - void reset() { - totalSamples = 0; - noiseAccum = 0; - noisePackets = 0; - threshold = 0; - speechStarted = false; - silenceSamples = 0; - fired = false; - } - - /** Returns true exactly once, on the packet that ends the utterance. */ - boolean accept(short[] pcm, int count) { - if (fired || count <= 0) { - return false; - } - totalSamples += count; - long elapsedMs = totalSamples * 1000L / SAMPLE_RATE; - - double sumSquares = 0; - for (int i = 0; i < count; i++) { - double s = pcm[i]; - sumSquares += s * s; - } - double rms = Math.sqrt(sumSquares / count); - - if (elapsedMs <= CALIBRATE_MS) { - noiseAccum += rms; - noisePackets++; - return false; - } - if (threshold == 0) { - double noiseFloor = noisePackets > 0 ? noiseAccum / noisePackets : 0; - threshold = Math.max(noiseFloor, MIN_RMS); - } - - if (!speechStarted) { - if (rms >= threshold * ONSET_FACTOR) { - speechStarted = true; - silenceSamples = 0; - } else if (elapsedMs >= LEAD_IN_MS) { - // Never heard anything; close the dialog rather than hang. - fired = true; - return true; - } - return false; - } - - if (rms < threshold * RELEASE_FACTOR) { - silenceSamples += count; - if (silenceSamples * 1000L / SAMPLE_RATE >= SILENCE_MS) { - fired = true; - return true; - } - } else { - silenceSamples = 0; - } - - if (elapsedMs >= MAX_UTTERANCE_MS) { - fired = true; - return true; - } - return false; - } - } - - private void stopG2Audio() { - FaceclawBleCommunicator currentCommunicator = communicator; - if (currentCommunicator != null) { - currentCommunicator.stopG2AudioCapture(); - } - maybeEmitAudioStats(true); - } - - private void releaseSherpa() { - if (recognizer != null) { - recognizer.release(); - recognizer = null; - } - } - - private void releaseLc3() { - if (lc3Decoder != null) { - lc3Decoder.close(); - lc3Decoder = null; - } - } - - private void queueAudioPacket(byte[] data, String arm, long arrivalMs) { - if (!started || data == null) { - return; - } - if (!"L".equals(arm)) { - wrongArmPackets++; - } - synchronized (audioQueueLock) { - if (audioQueue.size() >= MAX_AUDIO_QUEUE_PACKETS) { - audioQueue.removeFirst(); - queueDroppedPackets++; - } - audioQueue.addLast(new AudioPacket(data, arm, arrivalMs)); - queuedPackets++; - if (lastPacketArrivalMs > 0) { - long delta = arrivalMs - lastPacketArrivalMs; - if (delta > maxInterPacketMs) { - maxInterPacketMs = delta; - } - if (delta > LATE_PACKET_INTERVAL_MS) { - latePackets++; - } - } - lastPacketArrivalMs = arrivalMs; - audioQueueLock.notifyAll(); - } - } - - private AudioPacket takeAudioPacket() { - synchronized (audioQueueLock) { - while (started && audioQueue.isEmpty()) { - try { - audioQueueLock.wait(250); - maybeEmitAudioStats(false); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return null; - } - } - return audioQueue.pollFirst(); - } - } - - private void resetAudioStats() { - queuedPackets = 0; - queueDroppedPackets = 0; - decodedSamples = 0; - latePackets = 0; - wrongArmPackets = 0; - lastPacketArrivalMs = 0; - maxInterPacketMs = 0; - lastStatsAtMs = SystemClock.elapsedRealtime(); - } - - private void maybeEmitAudioStats(boolean force) { - long now = SystemClock.elapsedRealtime(); - if (!force && now - lastStatsAtMs < STATS_INTERVAL_MS) { - return; - } - lastStatsAtMs = now; - FaceclawLc3Decoder currentDecoder = lc3Decoder; - long real = currentDecoder == null ? 0 : currentDecoder.getRealPackets(); - long duplicate = currentDecoder == null ? 0 : currentDecoder.getDuplicatePackets(); - long missing = currentDecoder == null ? 0 : currentDecoder.getMissingPackets(); - long decodeErrors = currentDecoder == null ? 0 : currentDecoder.getDecodeErrors(); - String status = "G2 mic packets=" + queuedPackets - + " decoded=" + real - + " missing=" + missing - + " duplicate=" + duplicate - + " late=" + latePackets - + " maxGapMs=" + maxInterPacketMs - + "\n" - + " queueDrop=" + queueDroppedPackets - + " decodeErrors=" + decodeErrors - + " wrongArm=" + wrongArmPackets - + " audioSec=" + String.format(java.util.Locale.US, "%.1f", decodedSamples / (double) SAMPLE_RATE); - // Audio-pipeline stats are diagnostic; keep them in logcat only, out of - // the on-glasses voice UI. - Log.i(TAG, status.replace('\n', ' ') + " expectedIntervalMs=" + EXPECTED_PACKET_INTERVAL_MS); - } - - private void emitStatus(String status) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - mainHandler.post(() -> currentListener.onStatus(status)); - } - - private void emitTranscript(String text, boolean isFinal) { - FaceclawVoiceControllerListener currentListener = listener; - if (currentListener == null) { - return; - } - Log.i(TAG, "Emit transcript final=" + isFinal + " textLen=" + (text == null ? 0 : text.trim().length())); - mainHandler.post(() -> currentListener.onTranscript(text, isFinal)); - } - - private static final class AudioPacket { - final byte[] data; - final String arm; - final long arrivalMs; - - AudioPacket(byte[] data, String arm, long arrivalMs) { - this.data = data; - this.arm = arm == null ? "?" : arm; - this.arrivalMs = arrivalMs; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.kt new file mode 100644 index 00000000..ad0531bb --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceController.kt @@ -0,0 +1,285 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.Handler +import android.os.Looper +import android.util.Log + +import java.io.File + +/** + * Android host for the shared [VoiceCaptureSession]: supplies the glasses packet source (the + * BLE communicator), the phone microphone, the sherpa-onnx recognizer and speaker model, model + * directory lookup, recording storage, and main-thread callback delivery. The capture pipeline + * itself (packet queue, beam gate, endpointing, segmented transcript, verification) is shared + * with iOS. + */ +class FaceclawVoiceController(context: Context) { + companion object { + private const val TAG = "FaceclawVoice" + private const val SAMPLE_RATE = 16000 + // 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 const val ASR_ROOT = "faceclaw-voice-asr" + private const val ASR_MODEL_DIR = "sherpa-onnx-moonshine-base-en-quantized-2026-02-27" + // 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 const val LEGACY_KWS_ROOT = "faceclaw-voice" + private val ASR_MODEL_FILES = arrayOf( + "encoder_model.ort", + "decoder_model_merged.ort", + "tokens.txt" + ) + // Second on-device model: sherpa-onnx's offline Whisper backend (base.en, + // int8-quantized -- see the model-choice note in asr-model.ts). Directory + // shared with the TS-side download flow, same convention as ASR_MODEL_DIR. + // Whisper re-encodes the whole buffer on every call, so the shared session + // skips live partials for it; see VoiceCaptureSession.processRecognizer. + private const val ASR_WHISPER_MODEL_DIR = "sherpa-onnx-whisper-base-en-int8" + private val ASR_WHISPER_MODEL_FILES = arrayOf( + "base.en-encoder.int8.onnx", + "base.en-decoder.int8.onnx", + "base.en-tokens.txt" + ) + // 50 ms chunks match the G2 packet cadence the rest of the pipeline + // (endpointing, transcript pacing) is tuned for. + private const val PHONE_MIC_CHUNK_SAMPLES = SAMPLE_RATE / 20 + + @JvmStatic + private fun deleteRecursively(file: File) { + if (!file.exists()) { + return + } + val children = file.listFiles() + if (children != null) { + for (child in children) { + deleteRecursively(child) + } + } + file.delete() + } + + // The 28 MB embedding model takes seconds to load; keep one instance + // across capture sessions so verification adds only the embed time. + private var sharedSpeakerId: FaceclawSpeakerId? = null + private var sharedSpeakerIdPath: String? = null + + @JvmStatic + @Synchronized + private fun cachedSpeakerId(modelPath: String): FaceclawSpeakerId { + if (sharedSpeakerId == null || modelPath != sharedSpeakerIdPath) { + if (sharedSpeakerId != null) { + sharedSpeakerId!!.close() + } + sharedSpeakerId = FaceclawSpeakerId(modelPath) + sharedSpeakerIdPath = modelPath + } + return sharedSpeakerId!! + } + } + + private val appContext: Context = context.applicationContext + private val mainHandler = Handler(Looper.getMainLooper()) + @Volatile + private var communicator: FaceclawBleCommunicator? = null + + private val host = object : VoiceCaptureHost { + override val dispatcher = CallbackDispatcher { action -> mainHandler.post { action() } } + + override fun onSessionStarting() { + deleteLegacyKwsFiles() + } + + override fun isG2SessionReady(): Boolean { + val currentCommunicator = communicator + return currentCommunicator != null && currentCommunicator.isSessionReady() + } + + override fun isG2AudioCaptureActive(): Boolean { + val currentCommunicator = communicator + return currentCommunicator != null && currentCommunicator.isAudioCaptureActive() + } + + override fun g2AudioSource(): PacketAudioSource? { + val currentCommunicator = communicator ?: return null + return object : PacketAudioSource { + override fun start(listener: FaceclawAudioPacketListener): Boolean = + currentCommunicator.startG2AudioCapture(listener) + + override fun stop() = currentCommunicator.stopG2AudioCapture() + + override fun isActive(): Boolean = currentCommunicator.isAudioCaptureActive() + } + } + + override fun createPacketDecoder(): AudioPacketDecoder = AndroidSpeechEngines.Lc3DecoderAdapter() + + override fun openPhoneMic(): PcmAudioSource? = AndroidSpeechEngines.openPhoneMic(PHONE_MIC_CHUNK_SAMPLES) + + override fun hasTranscriberModel(kind: VoiceModelKind): Boolean = findAsrModelDir(kind) != null + + override fun loadTranscriber(kind: VoiceModelKind): OfflineTranscriber { + val modelDir = findAsrModelDir(kind) ?: throw IllegalStateException("voice model missing") + return AndroidSpeechEngines.SherpaTranscriber(AndroidSpeechEngines.recognizerConfig(modelDir, kind)) + } + + override fun speakerEmbedder(modelPath: String): SpeakerEmbedder { + val cached = cachedSpeakerId(modelPath) + // The cached model outlives the session; never release it here. + return object : SpeakerEmbedder { + override fun embed(pcm16le: ByteArray, sampleRate: Int): FloatArray? = cached.embed(pcm16le, sampleRate) + + override fun release() {} + } + } + + override fun recordingSink(): RecordingSink = RecordingSink { pcmBytes, sampleRate -> saveRecording(pcmBytes, sampleRate) } + } + + private val session = VoiceCaptureSession(host) + + fun setListener(listener: FaceclawVoiceControllerListener?) { + session.setListener(listener) + } + + fun setCommunicator(communicator: FaceclawBleCommunicator?) { + this.communicator = communicator + } + + /** Source the next capture from the phone microphone (no glasses paired). */ + fun setUsePhoneMic(usePhoneMic: Boolean) { + session.setUsePhoneMic(usePhoneMic) + } + + /** When true, the decoded mic PCM for each session is saved as a WAV. */ + fun setSaveRecordings(saveRecordings: Boolean) { + session.setSaveRecordings(saveRecordings) + } + + /** + * Which on-device model {@link #start}("onboard") should load: "whisper" + * selects the second on-device model (sherpa-onnx offline Whisper); any + * other value (including null/absent) keeps the existing Moonshine model. + * Must be set before {@link #start}; has no effect in CLOUD mode. + */ + fun setOnboardModelKind(kind: String?) { + session.setOnboardModelKind(kind) + } + + /** + * When true, watch the decoded PCM and fire {@code onSpeechEnd} once the + * speaker stops. Used by hands-free ("Hey Even") capture, which has no + * button release to end the utterance. Must be set before {@link #start}. + */ + fun setEndpointing(endpointing: Boolean) { + session.setEndpointing(endpointing) + } + + /** + * Verify this session's speaker against the enrolled wearer voice-print + * and report the result via onSpeakerVerified just before the final + * transcript. Must be set before {@link #start}; pass a null model path + * to disable. + */ + fun setSpeakerVerification(speakerModelPath: String?, wearerEmbedding: FloatArray?, threshold: Float) { + session.setSpeakerVerification(speakerModelPath, wearerEmbedding, threshold) + } + + fun clearSpeakerVerification() { + session.clearSpeakerVerification() + } + + /** Spectral noise suppression on the decoded stream. Safe to flip mid-run. */ + fun setNoiseSuppression(enabled: Boolean) { + session.setNoiseSuppression(enabled) + } + + /** + * Direction gating from the Sonic Radar beam: packets whose firmware + * direction-of-arrival falls outside centerDeg ± halfWidthDeg (device + * frame, 0 = straight ahead, positive right) are dropped before any + * consumer sees them. Safe to update mid-run. + */ + fun setBeamFilter(enabled: Boolean, centerDeg: Int, halfWidthDeg: Int) { + session.setBeamFilter(enabled, centerDeg, halfWidthDeg) + } + + fun start(requestedMode: String?) { + session.start(requestedMode, 0) + } + + fun start(requestedMode: String?, captureId: Int) { + session.start(requestedMode, captureId) + } + + /** + * Whether mic audio is actually flowing. {@link #start} only records + * intent: the enable lives in the glasses' EvenHub session, so a transport + * drop or a session suspend can leave this controller started with a + * worker that will never see another packet. Anything deciding whether to + * (re)start capture must ask this rather than assume its own bookkeeping. + */ + fun isCapturing(): Boolean = session.isCapturing() + + fun stop() { + session.stop() + } + + fun close() { + stop() + } + + /** Save the session's decoded mic PCM as a 16 kHz mono 16-bit WAV. */ + private fun saveRecording(pcmBytes: ByteArray, sampleRate: Int) { + try { + val dir = File(appContext.getExternalFilesDir(null), "voice-recordings") + if (!dir.exists() && !dir.mkdirs()) { + Log.w(TAG, "could not create voice-recordings dir") + return + } + val stamp = java.text.SimpleDateFormat("yyyyMMdd-HHmmss-SSS", java.util.Locale.US) + .format(java.util.Date()) + val file = File(dir, "voice-$stamp.wav") + java.io.FileOutputStream(file).use { fos -> + fos.write(BinaryEncoding.wavHeader(pcmBytes.size, sampleRate, 1)) + fos.write(pcmBytes) + } + Log.i(TAG, "saved voice recording " + file.absolutePath + + " samples=" + (pcmBytes.size / 2) + + " sec=" + String.format(java.util.Locale.US, "%.2f", pcmBytes.size / 2.0 / sampleRate)) + } catch (t: Throwable) { + Log.w(TAG, "failed to save voice recording", t) + } + } + + /** + * The on-device model directory for the given kind, populated by the + * download flow in asr-model.ts (releases before 0.5.0 copied the + * Moonshine files out of the APK directly, so upgraded installs are + * already complete for that model). Null when any expected file is + * missing, i.e. the model still needs to be downloaded. + */ + private fun findAsrModelDir(kind: VoiceModelKind): File? { + val whisper = kind == VoiceModelKind.WHISPER + val dirName = if (whisper) ASR_WHISPER_MODEL_DIR else ASR_MODEL_DIR + val fileNames = if (whisper) ASR_WHISPER_MODEL_FILES else ASR_MODEL_FILES + val modelDir = File(appContext.filesDir, ASR_ROOT + File.separator + dirName) + for (fileName in fileNames) { + val file = File(modelDir, fileName) + if (!file.exists() || file.length() == 0L) { + return null + } + } + return modelDir + } + + private fun deleteLegacyKwsFiles() { + try { + deleteRecursively(File(appContext.filesDir, LEGACY_KWS_ROOT)) + } catch (t: Throwable) { + Log.w(TAG, "failed to delete legacy wake-word files", t) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceControllerListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceControllerListener.java deleted file mode 100644 index 95c5826f..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawVoiceControllerListener.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.faceclaw.app; - -public interface FaceclawVoiceControllerListener { - void onStatus(String status); - - /** - * Best transcript of the current utterance so far. REPLACE semantics: text - * is the complete transcript, not a delta — display it verbatim, replacing - * any previous partial. isFinal marks the end of the utterance. - */ - void onTranscript(String text, boolean isFinal); - - /** Capture has finished, after the final transcript or a startup/recognition error. */ - void onStopped(int captureId); - - /** - * Decoded microphone audio: 16 kHz mono signed 16-bit little-endian PCM. - * Emitted in every mode so raw-PCM consumers (levels, recording, captions) - * keep working while the onboard recognizer owns the transcript. - */ - void onPcm(byte[] pcm16le); - - /** - * Per-packet DSP metadata the glasses firmware appends to each 50 ms audio - * packet, computed on the raw stereo capture before its mono downmix: the - * direction-of-arrival in signed degrees (0 = straight ahead, positive to - * the wearer's right) and a signal-strength ratio that serves as a - * confidence/energy proxy for the angle. - */ - void onFrameMeta(int angleDegrees, int ssr); - - /** - * The speaker stopped, in a hands-free session that has no button release - * to end it. Only fires when endpointing was enabled for the session; see - * FaceclawVoiceController.setEndpointing. - */ - void onSpeechEnd(); - - /** - * Result of speaker verification against the enrolled wearer voice-print, - * emitted once per capture session just before the final transcript. - * Only fires when verification was configured for the session; fails - * open (isWearer=true) when the utterance was too short to verify or the - * speaker model could not be loaded. - */ - void onSpeakerVerified(boolean isWearer, float similarity); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWavRecorder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWavRecorder.java deleted file mode 100644 index ed08d388..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWavRecorder.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.charset.StandardCharsets; - -/** - * Streaming WAV writer for conversation recordings: the header is written up - * front with zero sizes and patched on finish, so a crash mid-session leaves - * a recoverable file (data length can be derived from the file size). - */ -public class FaceclawWavRecorder { - private static final String TAG = "FaceclawWavRecorder"; - - private final String path; - private final int sampleRate; - private final int channels; - private RandomAccessFile file; - private long dataBytes; - - public FaceclawWavRecorder(String path, int sampleRate, int channels) throws IOException { - this.path = path; - this.sampleRate = sampleRate; - this.channels = channels; - File parent = new File(path).getParentFile(); - if (parent != null && !parent.exists() && !parent.mkdirs()) { - throw new IOException("could not create " + parent); - } - file = new RandomAccessFile(path, "rw"); - file.setLength(0); - file.write(buildHeader(0)); - } - - public synchronized void append(byte[] pcm16le) { - if (file == null || pcm16le == null || pcm16le.length == 0) { - return; - } - try { - file.write(pcm16le); - dataBytes += pcm16le.length; - } catch (IOException e) { - Log.w(TAG, "append failed", e); - } - } - - public synchronized long getDurationMs() { - return dataBytes * 1000L / (sampleRate * channels * 2L); - } - - public String getPath() { - return path; - } - - /** Patch the header and close. Returns the PCM byte count (0 = empty). */ - public synchronized long finish() { - RandomAccessFile current = file; - file = null; - if (current == null) { - return dataBytes; - } - try { - current.seek(0); - current.write(buildHeader((int) Math.min(dataBytes, Integer.MAX_VALUE))); - current.close(); - } catch (IOException e) { - Log.w(TAG, "finish failed", e); - } - return dataBytes; - } - - /** Delete the (finished or abandoned) recording. */ - public synchronized void discard() { - finish(); - new File(path).delete(); - } - - private byte[] buildHeader(int pcmBytes) { - int byteRate = sampleRate * channels * 2; - ByteBuffer b = ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN); - b.put("RIFF".getBytes(StandardCharsets.US_ASCII)); - b.putInt(36 + pcmBytes); - b.put("WAVE".getBytes(StandardCharsets.US_ASCII)); - b.put("fmt ".getBytes(StandardCharsets.US_ASCII)); - b.putInt(16); - b.putShort((short) 1); - b.putShort((short) channels); - b.putInt(sampleRate); - b.putInt(byteRate); - b.putShort((short) (channels * 2)); - b.putShort((short) 16); - b.put("data".getBytes(StandardCharsets.US_ASCII)); - b.putInt(pcmBytes); - return b.array(); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java deleted file mode 100644 index cb0da66f..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.java +++ /dev/null @@ -1,391 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.GoogleApiAvailability; -import com.google.android.gms.wearable.CapabilityClient; -import com.google.android.gms.wearable.CapabilityInfo; -import com.google.android.gms.wearable.DataMap; -import com.google.android.gms.wearable.MessageEvent; -import com.google.android.gms.wearable.Node; -import com.google.android.gms.wearable.PutDataMapRequest; -import com.google.android.gms.wearable.PutDataRequest; -import com.google.android.gms.wearable.Wearable; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.nio.charset.StandardCharsets; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -/** - * Phone side of the Wear OS integration: the single place that talks to the - * Wearable Data Layer (Google Play services), so the JS side only ever sees - * JSON strings and never touches Play services types. - * - * Inbound: FaceclawWearListenerService hands every watch message to - * {@link #handleMessage}; it is forwarded to the JS listener on the thread - * that registered it (the main isolate), mirroring FaceclawSettings. Messages - * that arrive before a listener exists (the watch poked a phone whose JS - * dashboard is not up yet) are acked with jsReady=false so the watch can say - * "open Faceclaw on your phone"; only idempotent state requests are kept for - * replay on registration — the watch already reported gestures and commands - * as failed, and executing them tens of seconds later would act on whatever - * the glasses happen to show by then. - * - * Outbound: {@link #publishState} mirrors the dashboard state into a Data - * Layer item (delivered even if the watch is out of range right now, and - * available to the watch app the moment it opens), while {@link #sendToWatch} - * sends fire-and-forget messages (acks, assistant text) to every reachable - * watch that advertises the Faceclaw watch-app capability. - * - * Everything is a no-op on phones without Google Play services. - */ -public final class FaceclawWearBridge { - private static final String TAG = "FaceclawWear"; - - /** Advertised by the watch app (wear/app/src/main/res/values/wear.xml). */ - public static final String CAPABILITY_WATCH = "faceclaw_watch"; - /** Advertised by this app (res/values/wear.xml) so the watch can find the phone. */ - public static final String CAPABILITY_PHONE = "faceclaw_phone"; - - public static final String PATH_PREFIX = "/faceclaw"; - public static final String PATH_STATE = "/faceclaw/state"; - public static final String PATH_STATE_REQUEST = "/faceclaw/state/request"; - /** Watch -> phone: the watch's own battery; carries no seq and is never acked. */ - public static final String PATH_WATCH_BATTERY = "/faceclaw/battery"; - /** Phone -> watch: answered with PATH_WATCH_BATTERY even while the watch app is closed. */ - public static final String PATH_WATCH_BATTERY_REQUEST = "/faceclaw/battery/request"; - public static final String PATH_ACK = "/faceclaw/ack"; - public static final String PATH_EVENT = "/faceclaw/event"; - - private static final String STATE_KEY_JSON = "json"; - private static final String STATE_KEY_UPDATED_AT = "updatedAt"; - - /** Messages kept for a listener that is not registered yet. */ - private static final int PENDING_LIMIT = 16; - private static final long PENDING_MAX_AGE_MS = 30_000; - - private static volatile FaceclawWearBridge instance; - - private final Context context; - private final boolean available; - private final Object lock = new Object(); - private FaceclawWearListener listener; - private Handler listenerHandler; - private final ArrayDeque pending = new ArrayDeque<>(); - private final List watchNodes = new ArrayList<>(); - private String lastPublishedState = null; - - private static final class PendingMessage { - final String path; - final String json; - final String nodeId; - final long receivedAt; - - PendingMessage(String path, String json, String nodeId, long receivedAt) { - this.path = path; - this.json = json; - this.nodeId = nodeId; - this.receivedAt = receivedAt; - } - } - - private FaceclawWearBridge(Context context) { - this.context = context.getApplicationContext(); - boolean playServices; - try { - playServices = GoogleApiAvailability.getInstance() - .isGooglePlayServicesAvailable(this.context) == ConnectionResult.SUCCESS; - } catch (Throwable error) { - Log.w(TAG, "Play services availability check failed", error); - playServices = false; - } - this.available = playServices; - if (!available) { - Log.i(TAG, "Google Play services unavailable; watch integration disabled"); - } - } - - /** Initialize (idempotent) and return the singleton. */ - public static FaceclawWearBridge getInstance(Context context) { - if (instance == null) { - synchronized (FaceclawWearBridge.class) { - if (instance == null) { - instance = new FaceclawWearBridge(context); - } - } - } - return instance; - } - - /** Whether the Wearable Data Layer can be used on this phone at all. */ - public boolean isAvailable() { - return available; - } - - /** - * Register the JS listener. Must be called from the thread whose isolate - * owns it; that thread's Looper is captured for dispatch. Replays any - * recently queued watch messages and refreshes the reachable-watch set. - */ - public void setListener(FaceclawWearListener newListener) { - Looper looper = Looper.myLooper(); - if (looper == null) { - Log.w(TAG, "wear listener registered from a Looper-less thread; it will never be notified"); - } - List replay; - synchronized (lock) { - listener = newListener; - listenerHandler = looper != null ? new Handler(looper) : null; - replay = new ArrayList<>(pending); - pending.clear(); - } - long now = System.currentTimeMillis(); - for (PendingMessage message : replay) { - if (now - message.receivedAt <= PENDING_MAX_AGE_MS) { - dispatchMessage(message.path, message.json, message.nodeId); - } - } - refreshWatchNodes(); - } - - public void clearListener() { - synchronized (lock) { - listener = null; - listenerHandler = null; - } - } - - public boolean isWatchReachable() { - synchronized (lock) { - return !watchNodes.isEmpty(); - } - } - - /** Display name of a reachable watch ("" if none). */ - public String getWatchName() { - synchronized (lock) { - return watchNodes.isEmpty() ? "" : watchNodes.get(0).getDisplayName(); - } - } - - /** Re-query which watches advertise the Faceclaw watch-app capability. */ - public void refreshWatchNodes() { - if (!available) return; - try { - Wearable.getCapabilityClient(context) - .getCapability(CAPABILITY_WATCH, CapabilityClient.FILTER_REACHABLE) - .addOnSuccessListener(this::updateWatchNodes) - .addOnFailureListener(error -> Log.w(TAG, "capability query failed", error)); - } catch (Throwable error) { - Log.w(TAG, "capability query failed", error); - } - } - - /** - * Mirror the dashboard state to the watch. Identical consecutive states - * are not re-sent (the Data Layer would drop them anyway). - */ - public void publishState(String json) { - publishState(json, false); - } - - /** As {@link #publishState(String)}; `force` re-sends an unchanged state. */ - public void publishState(String json, boolean force) { - if (!available || json == null) return; - synchronized (lock) { - if (!force && json.equals(lastPublishedState)) return; - lastPublishedState = json; - } - try { - PutDataMapRequest request = PutDataMapRequest.create(PATH_STATE); - DataMap map = request.getDataMap(); - map.putString(STATE_KEY_JSON, json); - map.putLong(STATE_KEY_UPDATED_AT, System.currentTimeMillis()); - PutDataRequest put = request.asPutDataRequest().setUrgent(); - Wearable.getDataClient(context) - .putDataItem(put) - .addOnFailureListener(error -> Log.w(TAG, "state publish failed", error)); - } catch (Throwable error) { - Log.w(TAG, "state publish failed", error); - } - } - - /** Send a message to every reachable watch running the Faceclaw watch app. */ - public void sendToWatch(String path, String json) { - if (!available) return; - List targets; - synchronized (lock) { - targets = new ArrayList<>(watchNodes); - } - if (targets.isEmpty()) { - // Nothing known yet (first message after boot): look the watch up, then send. - try { - Wearable.getCapabilityClient(context) - .getCapability(CAPABILITY_WATCH, CapabilityClient.FILTER_REACHABLE) - .addOnSuccessListener(info -> { - updateWatchNodes(info); - for (Node node : info.getNodes()) { - sendToNode(node.getId(), path, json); - } - }) - .addOnFailureListener(error -> Log.w(TAG, "capability query failed", error)); - } catch (Throwable error) { - Log.w(TAG, "capability query failed", error); - } - return; - } - for (Node node : targets) { - sendToNode(node.getId(), path, json); - } - } - - /** Send a message to one specific node (e.g. an ack back to the sender). */ - public void sendToNode(String nodeId, String path, String json) { - if (!available || nodeId == null || nodeId.isEmpty()) return; - try { - byte[] payload = (json != null ? json : "{}").getBytes(StandardCharsets.UTF_8); - Wearable.getMessageClient(context) - .sendMessage(nodeId, path, payload) - .addOnFailureListener(error -> Log.w(TAG, "send " + path + " to " + nodeId + " failed", error)); - } catch (Throwable error) { - Log.w(TAG, "send " + path + " failed", error); - } - } - - /** Reply to a watch message; `seq` echoes the watch's sequence number. */ - public void sendAck(String nodeId, long seq, boolean ok, boolean jsReady, String message) { - JSONObject ack = new JSONObject(); - try { - ack.put("seq", seq); - ack.put("ok", ok); - ack.put("jsReady", jsReady); - ack.put("message", message != null ? message : ""); - } catch (JSONException ignored) { - // The keys are constants; this cannot happen. - } - sendToNode(nodeId, PATH_ACK, ack.toString()); - } - - /** Entry point for FaceclawWearListenerService; may run on any thread. */ - void handleMessage(MessageEvent event) { - if (event == null) return; - String path = event.getPath(); - if (path == null || !path.startsWith(PATH_PREFIX)) return; - byte[] data = event.getData(); - String json = data != null && data.length > 0 ? new String(data, StandardCharsets.UTF_8) : "{}"; - String nodeId = event.getSourceNodeId(); - Log.d(TAG, "recv " + path + " seq=" + readSeq(json)); - - boolean delivered = dispatchMessage(path, json, nodeId); - if (delivered) return; - // A battery report answers a poll the dashboard sent; with no - // dashboard to receive it, it is simply dropped (the dashboard polls - // again when it starts), and the watch does not expect an ack. - if (PATH_WATCH_BATTERY.equals(path)) return; - - // No JS listener yet: tell the watch so it can explain why nothing is - // happening. Only state requests are kept for replay (see class doc); - // everything else is dropped after the failure ack. - if (PATH_STATE_REQUEST.equals(path)) { - synchronized (lock) { - long now = System.currentTimeMillis(); - while (!pending.isEmpty() && now - pending.peekFirst().receivedAt > PENDING_MAX_AGE_MS) { - pending.pollFirst(); - } - if (pending.size() >= PENDING_LIMIT) { - pending.pollFirst(); - } - pending.addLast(new PendingMessage(path, json, nodeId, now)); - } - } - sendAck(nodeId, readSeq(json), false, false, "Faceclaw is not running on the phone. Open it to connect the glasses."); - } - - /** Entry point for FaceclawWearListenerService's capability callback. */ - void handleCapabilityChanged(CapabilityInfo info) { - if (info == null || !CAPABILITY_WATCH.equals(info.getName())) return; - updateWatchNodes(info); - } - - private void updateWatchNodes(CapabilityInfo info) { - Set nodes = info != null ? info.getNodes() : Collections.emptySet(); - // Every reachable watch gets messages; a nearby (Bluetooth-linked) - // one is listed first so it is the name the phone UI shows. - List reachable = new ArrayList<>(); - for (Node node : nodes) { - if (node.isNearby()) reachable.add(0, node); else reachable.add(node); - } - boolean changed; - String name; - synchronized (lock) { - changed = !sameNodeIds(watchNodes, reachable); - watchNodes.clear(); - watchNodes.addAll(reachable); - name = watchNodes.isEmpty() ? "" : watchNodes.get(0).getDisplayName(); - } - if (changed) { - Log.i(TAG, reachable.isEmpty() ? "no Faceclaw watch reachable" : "watch reachable: " + name); - boolean reachableNow = !reachable.isEmpty(); - FaceclawWearListener target; - Handler handler; - synchronized (lock) { - target = listener; - handler = listenerHandler; - } - if (target != null && handler != null) { - handler.post(() -> { - try { - target.onWatchConnection(reachableNow, name); - } catch (Exception error) { - Log.w(TAG, "watch connection listener failed", error); - } - }); - } - } - } - - private static boolean sameNodeIds(List a, List b) { - if (a.size() != b.size()) return false; - for (int i = 0; i < a.size(); i++) { - if (!a.get(i).getId().equals(b.get(i).getId())) return false; - } - return true; - } - - /** Post to the JS listener; false when there is none to post to. */ - private boolean dispatchMessage(String path, String json, String nodeId) { - FaceclawWearListener target; - Handler handler; - synchronized (lock) { - target = listener; - handler = listenerHandler; - } - if (target == null || handler == null) return false; - handler.post(() -> { - try { - target.onMessage(path, json, nodeId); - } catch (Exception error) { - Log.w(TAG, "wear message listener failed for " + path, error); - } - }); - return true; - } - - private static long readSeq(String json) { - try { - return new JSONObject(json).optLong("seq", 0); - } catch (JSONException error) { - return 0; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.kt new file mode 100644 index 00000000..5be4a511 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearBridge.kt @@ -0,0 +1,386 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.Handler +import android.os.Looper +import android.util.Log + +import com.google.android.gms.common.ConnectionResult +import com.google.android.gms.common.GoogleApiAvailability +import com.google.android.gms.wearable.CapabilityClient +import com.google.android.gms.wearable.CapabilityInfo +import com.google.android.gms.wearable.DataMap +import com.google.android.gms.wearable.MessageEvent +import com.google.android.gms.wearable.Node +import com.google.android.gms.wearable.PutDataMapRequest +import com.google.android.gms.wearable.PutDataRequest +import com.google.android.gms.wearable.Wearable + +import org.json.JSONException +import org.json.JSONObject + +import java.nio.charset.StandardCharsets +import java.util.ArrayDeque +import java.util.ArrayList +import java.util.Collections + +/** + * Phone side of the Wear OS integration: the single place that talks to the + * Wearable Data Layer (Google Play services), so the JS side only ever sees + * JSON strings and never touches Play services types. + * + * Inbound: FaceclawWearListenerService hands every watch message to + * [handleMessage]; it is forwarded to the JS listener on the thread + * that registered it (the main isolate), mirroring FaceclawSettings. Messages + * that arrive before a listener exists (the watch poked a phone whose JS + * dashboard is not up yet) are acked with jsReady=false so the watch can say + * "open Faceclaw on your phone"; only idempotent state requests are kept for + * replay on registration — the watch already reported gestures and commands + * as failed, and executing them tens of seconds later would act on whatever + * the glasses happen to show by then. + * + * Outbound: [publishState] mirrors the dashboard state into a Data + * Layer item (delivered even if the watch is out of range right now, and + * available to the watch app the moment it opens), while [sendToWatch] + * sends fire-and-forget messages (acks, assistant text) to every reachable + * watch that advertises the Faceclaw watch-app capability. + * + * Everything is a no-op on phones without Google Play services. + */ +class FaceclawWearBridge private constructor(context: Context) { + companion object { + private const val TAG = "FaceclawWear" + + /** Advertised by the watch app (wear/app/src/main/res/values/wear.xml). */ + const val CAPABILITY_WATCH = "faceclaw_watch" + /** Advertised by this app (res/values/wear.xml) so the watch can find the phone. */ + const val CAPABILITY_PHONE = "faceclaw_phone" + + const val PATH_PREFIX = "/faceclaw" + const val PATH_STATE = "/faceclaw/state" + const val PATH_STATE_REQUEST = "/faceclaw/state/request" + /** Watch -> phone: the watch's own battery; carries no seq and is never acked. */ + const val PATH_WATCH_BATTERY = "/faceclaw/battery" + /** Phone -> watch: answered with PATH_WATCH_BATTERY even while the watch app is closed. */ + const val PATH_WATCH_BATTERY_REQUEST = "/faceclaw/battery/request" + const val PATH_ACK = "/faceclaw/ack" + const val PATH_EVENT = "/faceclaw/event" + + private const val STATE_KEY_JSON = "json" + private const val STATE_KEY_UPDATED_AT = "updatedAt" + + /** Messages kept for a listener that is not registered yet. */ + private const val PENDING_LIMIT = 16 + private const val PENDING_MAX_AGE_MS = 30_000L + + @Volatile + private var instance: FaceclawWearBridge? = null + + /** Initialize (idempotent) and return the singleton. */ + @JvmStatic + fun getInstance(context: Context): FaceclawWearBridge { + if (instance == null) { + synchronized(FaceclawWearBridge::class.java) { + if (instance == null) { + instance = FaceclawWearBridge(context) + } + } + } + return instance!! + } + + private fun sameNodeIds(a: List, b: List): Boolean { + if (a.size != b.size) return false + for (i in a.indices) { + if (a[i].id != b[i].id) return false + } + return true + } + + private fun readSeq(json: String): Long { + try { + return JSONObject(json).optLong("seq", 0) + } catch (error: JSONException) { + return 0 + } + } + } + + private val context: Context + private val available: Boolean + private val lock = Any() + private var listener: FaceclawWearListener? = null + private var listenerHandler: Handler? = null + private val pending = ArrayDeque() + private val watchNodes: MutableList = ArrayList() + private var lastPublishedState: String? = null + + private class PendingMessage( + val path: String, + val json: String, + val nodeId: String?, + val receivedAt: Long, + ) + + init { + this.context = context.applicationContext + var playServices: Boolean + try { + playServices = GoogleApiAvailability.getInstance() + .isGooglePlayServicesAvailable(this.context) == ConnectionResult.SUCCESS + } catch (error: Throwable) { + Log.w(TAG, "Play services availability check failed", error) + playServices = false + } + this.available = playServices + if (!available) { + Log.i(TAG, "Google Play services unavailable; watch integration disabled") + } + } + + /** Whether the Wearable Data Layer can be used on this phone at all. */ + fun isAvailable(): Boolean { + return available + } + + /** + * Register the JS listener. Must be called from the thread whose isolate + * owns it; that thread's Looper is captured for dispatch. Replays any + * recently queued watch messages and refreshes the reachable-watch set. + */ + fun setListener(newListener: FaceclawWearListener?) { + val looper = Looper.myLooper() + if (looper == null) { + Log.w(TAG, "wear listener registered from a Looper-less thread; it will never be notified") + } + val replay: List + synchronized(lock) { + listener = newListener + listenerHandler = if (looper != null) Handler(looper) else null + replay = ArrayList(pending) + pending.clear() + } + val now = System.currentTimeMillis() + for (message in replay) { + if (now - message.receivedAt <= PENDING_MAX_AGE_MS) { + dispatchMessage(message.path, message.json, message.nodeId) + } + } + refreshWatchNodes() + } + + fun clearListener() { + synchronized(lock) { + listener = null + listenerHandler = null + } + } + + fun isWatchReachable(): Boolean { + synchronized(lock) { + return !watchNodes.isEmpty() + } + } + + /** Display name of a reachable watch ("" if none). */ + fun getWatchName(): String { + synchronized(lock) { + return if (watchNodes.isEmpty()) "" else watchNodes[0].displayName + } + } + + /** Re-query which watches advertise the Faceclaw watch-app capability. */ + fun refreshWatchNodes() { + if (!available) return + try { + Wearable.getCapabilityClient(context) + .getCapability(CAPABILITY_WATCH, CapabilityClient.FILTER_REACHABLE) + .addOnSuccessListener { info -> updateWatchNodes(info) } + .addOnFailureListener { error -> Log.w(TAG, "capability query failed", error) } + } catch (error: Throwable) { + Log.w(TAG, "capability query failed", error) + } + } + + /** + * Mirror the dashboard state to the watch. Identical consecutive states + * are not re-sent (the Data Layer would drop them anyway). + */ + fun publishState(json: String?) { + publishState(json, false) + } + + /** As [publishState]; `force` re-sends an unchanged state. */ + fun publishState(json: String?, force: Boolean) { + if (!available || json == null) return + synchronized(lock) { + if (!force && json == lastPublishedState) return + lastPublishedState = json + } + try { + val request = PutDataMapRequest.create(PATH_STATE) + val map: DataMap = request.dataMap + map.putString(STATE_KEY_JSON, json) + map.putLong(STATE_KEY_UPDATED_AT, System.currentTimeMillis()) + val put: PutDataRequest = request.asPutDataRequest().setUrgent() + Wearable.getDataClient(context) + .putDataItem(put) + .addOnFailureListener { error -> Log.w(TAG, "state publish failed", error) } + } catch (error: Throwable) { + Log.w(TAG, "state publish failed", error) + } + } + + /** Send a message to every reachable watch running the Faceclaw watch app. */ + fun sendToWatch(path: String, json: String?) { + if (!available) return + val targets: List + synchronized(lock) { + targets = ArrayList(watchNodes) + } + if (targets.isEmpty()) { + // Nothing known yet (first message after boot): look the watch up, then send. + try { + Wearable.getCapabilityClient(context) + .getCapability(CAPABILITY_WATCH, CapabilityClient.FILTER_REACHABLE) + .addOnSuccessListener { info -> + updateWatchNodes(info) + for (node in info.nodes) { + sendToNode(node.id, path, json) + } + } + .addOnFailureListener { error -> Log.w(TAG, "capability query failed", error) } + } catch (error: Throwable) { + Log.w(TAG, "capability query failed", error) + } + return + } + for (node in targets) { + sendToNode(node.id, path, json) + } + } + + /** Send a message to one specific node (e.g. an ack back to the sender). */ + fun sendToNode(nodeId: String?, path: String, json: String?) { + if (!available || nodeId == null || nodeId.isEmpty()) return + try { + val payload = (json ?: "{}").toByteArray(StandardCharsets.UTF_8) + Wearable.getMessageClient(context) + .sendMessage(nodeId, path, payload) + .addOnFailureListener { error -> Log.w(TAG, "send $path to $nodeId failed", error) } + } catch (error: Throwable) { + Log.w(TAG, "send $path failed", error) + } + } + + /** Reply to a watch message; `seq` echoes the watch's sequence number. */ + fun sendAck(nodeId: String?, seq: Long, ok: Boolean, jsReady: Boolean, message: String?) { + val ack = JSONObject() + try { + ack.put("seq", seq) + ack.put("ok", ok) + ack.put("jsReady", jsReady) + ack.put("message", message ?: "") + } catch (ignored: JSONException) { + // The keys are constants; this cannot happen. + } + sendToNode(nodeId, PATH_ACK, ack.toString()) + } + + /** Entry point for FaceclawWearListenerService; may run on any thread. */ + internal fun handleMessage(event: MessageEvent?) { + if (event == null) return + val path: String? = event.path + if (path == null || !path.startsWith(PATH_PREFIX)) return + val data: ByteArray? = event.data + val json = if (data != null && data.size > 0) String(data, StandardCharsets.UTF_8) else "{}" + val nodeId: String? = event.sourceNodeId + Log.d(TAG, "recv " + path + " seq=" + readSeq(json)) + + val delivered = dispatchMessage(path, json, nodeId) + if (delivered) return + // A battery report answers a poll the dashboard sent; with no + // dashboard to receive it, it is simply dropped (the dashboard polls + // again when it starts), and the watch does not expect an ack. + if (PATH_WATCH_BATTERY == path) return + + // No JS listener yet: tell the watch so it can explain why nothing is + // happening. Only state requests are kept for replay (see class doc); + // everything else is dropped after the failure ack. + if (PATH_STATE_REQUEST == path) { + synchronized(lock) { + val now = System.currentTimeMillis() + while (!pending.isEmpty() && now - pending.peekFirst().receivedAt > PENDING_MAX_AGE_MS) { + pending.pollFirst() + } + if (pending.size >= PENDING_LIMIT) { + pending.pollFirst() + } + pending.addLast(PendingMessage(path, json, nodeId, now)) + } + } + sendAck(nodeId, readSeq(json), false, false, "Faceclaw is not running on the phone. Open it to connect the glasses.") + } + + /** Entry point for FaceclawWearListenerService's capability callback. */ + internal fun handleCapabilityChanged(info: CapabilityInfo?) { + if (info == null || CAPABILITY_WATCH != info.name) return + updateWatchNodes(info) + } + + private fun updateWatchNodes(info: CapabilityInfo?) { + val nodes: Set = if (info != null) info.nodes else Collections.emptySet() + // Every reachable watch gets messages; a nearby (Bluetooth-linked) + // one is listed first so it is the name the phone UI shows. + val reachable: MutableList = ArrayList() + for (node in nodes) { + if (node.isNearby) reachable.add(0, node) else reachable.add(node) + } + val changed: Boolean + val name: String + synchronized(lock) { + changed = !sameNodeIds(watchNodes, reachable) + watchNodes.clear() + watchNodes.addAll(reachable) + name = if (watchNodes.isEmpty()) "" else watchNodes[0].displayName + } + if (changed) { + Log.i(TAG, if (reachable.isEmpty()) "no Faceclaw watch reachable" else "watch reachable: $name") + val reachableNow = !reachable.isEmpty() + val target: FaceclawWearListener? + val handler: Handler? + synchronized(lock) { + target = listener + handler = listenerHandler + } + if (target != null && handler != null) { + handler.post { + try { + target.onWatchConnection(reachableNow, name) + } catch (error: Exception) { + Log.w(TAG, "watch connection listener failed", error) + } + } + } + } + } + + /** Post to the JS listener; false when there is none to post to. */ + private fun dispatchMessage(path: String, json: String, nodeId: String?): Boolean { + val target: FaceclawWearListener? + val handler: Handler? + synchronized(lock) { + target = listener + handler = listenerHandler + } + if (target == null || handler == null) return false + handler.post { + try { + target.onMessage(path, json, nodeId) + } catch (error: Exception) { + Log.w(TAG, "wear message listener failed for $path", error) + } + } + return true + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListener.java deleted file mode 100644 index 590c87d6..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListener.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.faceclaw.app; - -/** - * Callbacks from the Android Wear (Wear OS) bridge; implemented in JS. Every - * call is delivered on the thread that registered the listener (see - * FaceclawWearBridge.setListener). - */ -public interface FaceclawWearListener { - /** - * A message from the watch app. `path` is the Data Layer message path - * (e.g. "/faceclaw/input"); `json` is the UTF-8 payload (always a JSON - * object, "{}" when the watch sent none); `nodeId` identifies the watch - * so a reply can be addressed to it. - */ - void onMessage(String path, String json, String nodeId); - - /** The set of reachable watches running the Faceclaw watch app changed. */ - void onWatchConnection(boolean reachable, String watchName); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.java deleted file mode 100644 index 7520d780..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; - -import com.google.android.gms.wearable.CapabilityInfo; -import com.google.android.gms.wearable.MessageEvent; -import com.google.android.gms.wearable.WearableListenerService; - -/** - * Play services starts this service (in the app process) for every Data - * Layer message under /faceclaw and for capability changes, whether or not - * the app is running. It only forwards to FaceclawWearBridge, which owns the - * hand-off to the JS side. - */ -public class FaceclawWearListenerService extends WearableListenerService { - @Override - public void onMessageReceived(MessageEvent event) { - Log.d("FaceclawWear", "service onMessageReceived " + event.getPath() + " from " + event.getSourceNodeId()); - FaceclawWearBridge.getInstance(this).handleMessage(event); - } - - @Override - public void onCapabilityChanged(CapabilityInfo info) { - FaceclawWearBridge.getInstance(this).handleCapabilityChanged(info); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.kt new file mode 100644 index 00000000..ed0ce360 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWearListenerService.kt @@ -0,0 +1,24 @@ +package com.faceclaw.app + +import android.util.Log + +import com.google.android.gms.wearable.CapabilityInfo +import com.google.android.gms.wearable.MessageEvent +import com.google.android.gms.wearable.WearableListenerService + +/** + * Play services starts this service (in the app process) for every Data + * Layer message under /faceclaw and for capability changes, whether or not + * the app is running. It only forwards to FaceclawWearBridge, which owns the + * hand-off to the JS side. + */ +class FaceclawWearListenerService : WearableListenerService() { + override fun onMessageReceived(event: MessageEvent) { + Log.d("FaceclawWear", "service onMessageReceived " + event.path + " from " + event.sourceNodeId) + FaceclawWearBridge.getInstance(this).handleMessage(event) + } + + override fun onCapabilityChanged(info: CapabilityInfo) { + FaceclawWearBridge.getInstance(this).handleCapabilityChanged(info) + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.java deleted file mode 100644 index f228ed7a..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.java +++ /dev/null @@ -1,151 +0,0 @@ -package com.faceclaw.app; - -import android.os.Handler; -import android.os.Looper; -import android.util.Log; - -import java.util.concurrent.TimeUnit; - -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; -import okhttp3.WebSocket; -import okhttp3.WebSocketListener; -import okio.ByteString; - -/** - * Thin okhttp WebSocket wrapper for the TypeScript side. Sends text frames - * (JSON protocols) and binary frames (raw audio for Soniox). Listener callbacks are - * posted to the Looper of the thread that constructed this object, so a JS - * isolate (main thread or app worker) always receives them on its own - * thread; a Looper-less constructing thread falls back to the main thread. - */ -public class FaceclawWebSocket { - private static final String TAG = "FaceclawWebSocket"; - private static volatile OkHttpClient sharedClient; - - private final Handler callbackHandler; - private final WebSocket socket; - private volatile boolean closeRequested; - - // Single constructor (no overloads) so NativeScript constructor resolution - // can never pick a variant that drops the auth header; callers with no - // header pass null/null. - public FaceclawWebSocket(String url, final FaceclawWebSocketListener listener, String headerName, String headerValue) { - if (url == null || url.trim().isEmpty()) { - throw new IllegalArgumentException("url is required"); - } - if (listener == null) { - throw new IllegalArgumentException("listener is required"); - } - Looper looper = Looper.myLooper(); - callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper()); - Request.Builder builder = new Request.Builder().url(url.trim()); - if (headerName != null && !headerName.isEmpty() && headerValue != null) { - builder.addHeader(headerName, headerValue); - } - Request request = builder.build(); - // Redacted diagnostics: confirm the auth header is actually present on - // the handshake and carries a plausible value (not empty/truncated). - StringBuilder headerLog = new StringBuilder(); - for (String name : request.headers().names()) { - String value = request.header(name); - headerLog.append(name).append('=').append(redact(value)).append(' '); - } - Log.i(TAG, "ws connect host=" + request.url().host() + request.url().encodedPath() - + " headers=[" + headerLog.toString().trim() + "]"); - socket = getClient().newWebSocket(request, new WebSocketListener() { - @Override public void onOpen(WebSocket webSocket, Response response) { - callbackHandler.post(() -> { - try { - listener.onOpen(); - } catch (Throwable t) { - Log.w(TAG, "listener onOpen failed", t); - } - }); - } - - @Override public void onMessage(WebSocket webSocket, String text) { - callbackHandler.post(() -> { - try { - listener.onTextMessage(text); - } catch (Throwable t) { - Log.w(TAG, "listener onTextMessage failed", t); - } - }); - } - - @Override public void onClosing(WebSocket webSocket, int code, String reason) { - webSocket.close(code, reason); - } - - @Override public void onClosed(WebSocket webSocket, int code, String reason) { - callbackHandler.post(() -> { - try { - listener.onClosed(code, reason == null ? "" : reason); - } catch (Throwable t) { - Log.w(TAG, "listener onClosed failed", t); - } - }); - } - - @Override public void onFailure(WebSocket webSocket, Throwable t, Response response) { - if (closeRequested) { - return; - } - final String message = t == null ? "unknown websocket failure" : String.valueOf(t); - callbackHandler.post(() -> { - try { - listener.onFailure(message); - } catch (Throwable inner) { - Log.w(TAG, "listener onFailure failed", inner); - } - }); - } - }); - } - - private static OkHttpClient getClient() { - OkHttpClient client = sharedClient; - if (client == null) { - synchronized (FaceclawWebSocket.class) { - if (sharedClient == null) { - sharedClient = new OkHttpClient.Builder() - .pingInterval(20, TimeUnit.SECONDS) - .addInterceptor(FaceclawHttp.userAgentInterceptor()) - .build(); - } - client = sharedClient; - } - } - return client; - } - - private static String redact(String value) { - if (value == null) { - return "null"; - } - int len = value.length(); - String prefix = value.substring(0, Math.min(4, len)); - return "len" + len + ":" + prefix + "..."; - } - - public boolean sendText(String message) { - return socket.send(message == null ? "" : message); - } - - public boolean sendBinary(byte[] bytes) { - return socket.send(ByteString.of(bytes == null ? new byte[0] : bytes)); - } - - public void close(int code, String reason) { - closeRequested = true; - try { - if (!socket.close(code, reason)) { - socket.cancel(); - } - } catch (Throwable t) { - socket.cancel(); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.kt new file mode 100644 index 00000000..f4a793bd --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocket.kt @@ -0,0 +1,156 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper +import android.util.Log + +import java.util.concurrent.TimeUnit + +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response +import okhttp3.WebSocket +import okhttp3.WebSocketListener +import okio.ByteString.Companion.toByteString + +/** + * Thin okhttp WebSocket wrapper for the TypeScript side. Sends text frames + * (JSON protocols) and binary frames (raw audio for Soniox). Listener callbacks are + * posted to the Looper of the thread that constructed this object, so a JS + * isolate (main thread or app worker) always receives them on its own + * thread; a Looper-less constructing thread falls back to the main thread. + */ +class FaceclawWebSocket +// Single constructor (no overloads) so NativeScript constructor resolution +// can never pick a variant that drops the auth header; callers with no +// header pass null/null. +constructor(url: String?, listener: FaceclawWebSocketListener?, headerName: String?, headerValue: String?) { + private val callbackHandler: Handler + private val socket: WebSocket + @Volatile + private var closeRequested = false + + init { + if (url == null || url.trim().isEmpty()) { + throw IllegalArgumentException("url is required") + } + if (listener == null) { + throw IllegalArgumentException("listener is required") + } + val looper = Looper.myLooper() + callbackHandler = Handler(looper ?: Looper.getMainLooper()) + val builder = Request.Builder().url(url.trim()) + if (headerName != null && !headerName.isEmpty() && headerValue != null) { + builder.addHeader(headerName, headerValue) + } + val request = builder.build() + // Redacted diagnostics: confirm the auth header is actually present on + // the handshake and carries a plausible value (not empty/truncated). + val headerLog = StringBuilder() + for (name in request.headers.names()) { + val value = request.header(name) + headerLog.append(name).append('=').append(redact(value)).append(' ') + } + Log.i(TAG, "ws connect host=" + request.url.host + request.url.encodedPath + + " headers=[" + headerLog.toString().trim() + "]") + socket = getClient().newWebSocket(request, object : WebSocketListener() { + override fun onOpen(webSocket: WebSocket, response: Response) { + callbackHandler.post { + try { + listener.onOpen() + } catch (t: Throwable) { + Log.w(TAG, "listener onOpen failed", t) + } + } + } + + override fun onMessage(webSocket: WebSocket, text: String) { + callbackHandler.post { + try { + listener.onTextMessage(text) + } catch (t: Throwable) { + Log.w(TAG, "listener onTextMessage failed", t) + } + } + } + + override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { + webSocket.close(code, reason) + } + + override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { + callbackHandler.post { + try { + listener.onClosed(code, reason) + } catch (t: Throwable) { + Log.w(TAG, "listener onClosed failed", t) + } + } + } + + override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { + if (closeRequested) { + return + } + val message = t.toString() + callbackHandler.post { + try { + listener.onFailure(message) + } catch (inner: Throwable) { + Log.w(TAG, "listener onFailure failed", inner) + } + } + } + }) + } + + companion object { + private const val TAG = "FaceclawWebSocket" + @Volatile + private var sharedClient: OkHttpClient? = null + + private fun getClient(): OkHttpClient { + var client = sharedClient + if (client == null) { + synchronized(FaceclawWebSocket::class.java) { + if (sharedClient == null) { + sharedClient = OkHttpClient.Builder() + .pingInterval(20, TimeUnit.SECONDS) + .addInterceptor(FaceclawHttp.userAgentInterceptor()) + .build() + } + client = sharedClient + } + } + return client!! + } + + private fun redact(value: String?): String { + if (value == null) { + return "null" + } + val len = value.length + val prefix = value.substring(0, Math.min(4, len)) + return "len$len:$prefix..." + } + } + + fun sendText(message: String?): Boolean { + return socket.send(message ?: "") + } + + fun sendBinary(bytes: ByteArray?): Boolean { + return socket.send((bytes ?: ByteArray(0)).toByteString()) + } + + fun close(code: Int, reason: String?) { + closeRequested = true + try { + if (!socket.close(code, reason)) { + socket.cancel() + } + } catch (t: Throwable) { + socket.cancel() + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocketListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocketListener.java deleted file mode 100644 index f8e8733f..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawWebSocketListener.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.faceclaw.app; - -/** Callbacks for FaceclawWebSocket, delivered on the Android main thread. */ -public interface FaceclawWebSocketListener { - void onOpen(); - void onTextMessage(String message); - void onClosed(int code, String reason); - void onFailure(String message); -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java deleted file mode 100644 index 0eca05e5..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java +++ /dev/null @@ -1,537 +0,0 @@ -package com.faceclaw.app; - -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Typeface; -import android.os.Build; -import android.util.Log; - -import java.io.File; -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * Rasterizes text using font files (TTF/OTF/TTC) via the Android text stack, - * so shaping, kerning, and antialiasing are all delegated to - * minikin/HarfBuzz. Output uses the same grayscale packet format as - * ImageFileLoader ([widthLo, widthHi, heightLo, heightHi, pixels...], one - * byte per pixel, row-major), which the TS side turns into a GrayImage and - * the compositor later quantizes to the 4bpp the firmware wants. - * - * The gamma parameter maps antialiased coverage to output shade: - * out = 255 * (coverage/255)^gamma. The G2 display response looks roughly - * linear, so 1.0 is the expected default; the font previewer exposes it for - * on-hardware comparison. - */ -public final class FontFileRenderer { - private static final String TAG = "FontFileRenderer"; - private static final int TYPEFACE_CACHE_SIZE = 4; - - /** Small LRU of loaded typefaces keyed by file path. */ - private static final Map typefaceCache = - new LinkedHashMap(TYPEFACE_CACHE_SIZE, 0.75f, true) { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > TYPEFACE_CACHE_SIZE; - } - }; - - private FontFileRenderer() {} - - /** - * Render one line of text with the given font file at the given pixel - * size. The returned image is the font's full vertical extent (top to - * bottom, taller than ascent+descent), plus any horizontal glyph - * overhang beyond the advance width. - * - * Returns an empty array if the font cannot be loaded or the text - * renders to nothing. - */ - public static byte[] renderText(String path, String text, float sizePx, double gamma) { - if (path == null || text == null || sizePx <= 0) { - return new byte[0]; - } - Typeface typeface = loadTypeface(path); - if (typeface == null) { - return new byte[0]; - } - try { - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); - paint.setTypeface(typeface); - paint.setTextSize(sizePx); - paint.setColor(Color.WHITE); - - // top/bottom (the font's maximal extent) rather than - // ascent/descent, so diacritics and swashes don't clip. - Paint.FontMetricsInt fm = paint.getFontMetricsInt(); - int ascent = -fm.top; - int height = -fm.top + fm.bottom; - // measureText gives advance width; italic/swash glyphs can paint - // outside it, so pad by half an em on each side and trim after. - int pad = (int) Math.ceil(sizePx / 2); - int advance = (int) Math.ceil(paint.measureText(text)); - int width = advance + 2 * pad; - if (width <= 0 || height <= 0 || width * height > 4_000_000) { - return new byte[0]; - } - - Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8); - Canvas canvas = new Canvas(bitmap); - canvas.drawText(text, pad, ascent, paint); - byte[] pixels = alpha8Pixels(bitmap, width, height); - bitmap.recycle(); - - // Trim the horizontal padding down to the painted extent, but - // keep at least the advance width so spacing stays truthful. - int left = pad; - int right = pad + advance; - int[] painted = paintedColumnRange(pixels, width, height); - if (painted != null) { - left = Math.min(left, painted[0]); - right = Math.max(right, painted[1] + 1); - } - int outWidth = Math.max(1, right - left); - - byte[] lut = gammaLut(gamma); - byte[] out = new byte[4 + outWidth * height]; - out[0] = (byte) (outWidth & 0xff); - out[1] = (byte) ((outWidth >> 8) & 0xff); - out[2] = (byte) (height & 0xff); - out[3] = (byte) ((height >> 8) & 0xff); - for (int y = 0; y < height; y++) { - int srcRow = y * width; - int dstRow = 4 + y * outWidth; - for (int x = 0; x < outWidth; x++) { - out[dstRow + x] = lut[pixels[srcRow + left + x] & 0xff]; - } - } - return out; - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "text render failed: " + path, e); - return new byte[0]; - } - } - - /** - * Render a multi-line block of text wrapped to maxWidth, with line - * breaking delegated to StaticLayout (so it matches Android text - * behavior, including breaking inside long words). Truncated with an - * ellipsis past maxLines. Same packet format as renderText. - */ - public static byte[] renderWrapped( - String path, String text, float sizePx, int maxWidth, double gamma, int maxLines) { - if (path == null || text == null || sizePx <= 0 || maxWidth <= 0 || maxLines <= 0) { - return new byte[0]; - } - Typeface typeface = loadTypeface(path); - if (typeface == null) { - return new byte[0]; - } - try { - android.text.TextPaint paint = - new android.text.TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); - paint.setTypeface(typeface); - paint.setTextSize(sizePx); - paint.setColor(Color.WHITE); - paint.setHinting(Paint.HINTING_ON); - - android.text.StaticLayout layout = android.text.StaticLayout.Builder - .obtain(text, 0, text.length(), paint, maxWidth) - .setAlignment(android.text.Layout.Alignment.ALIGN_NORMAL) - .setIncludePad(false) - .setMaxLines(maxLines) - .setEllipsize(android.text.TextUtils.TruncateAt.END) - .build(); - int height = Math.max(1, layout.getHeight()); - if ((long) maxWidth * height > 4_000_000L) { - return new byte[0]; - } - - Bitmap bitmap = Bitmap.createBitmap(maxWidth, height, Bitmap.Config.ALPHA_8); - Canvas canvas = new Canvas(bitmap); - layout.draw(canvas); - byte[] pixels = alpha8Pixels(bitmap, maxWidth, height); - bitmap.recycle(); - - byte[] lut = gammaLut(gamma); - byte[] out = new byte[4 + pixels.length]; - out[0] = (byte) (maxWidth & 0xff); - out[1] = (byte) ((maxWidth >> 8) & 0xff); - out[2] = (byte) (height & 0xff); - out[3] = (byte) ((height >> 8) & 0xff); - for (int i = 0; i < pixels.length; i++) { - out[4 + i] = lut[pixels[i] & 0xff]; - } - return out; - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "wrapped render failed: " + path, e); - return new byte[0]; - } - } - - /** - * Render one codepoint as a tight-ink-box antialiased glyph cell for the - * texture-cache text pipeline (see app/graphics/ttf-font.ts). Ligatures - * are disabled so a per-codepoint raster plus pairwise kerning (measured - * via measureTextExact) fully describes a line. Little-endian packet: - * [advance u16, 26.6 fixed point] - * [bearingX s16] ink left relative to the pen position - * [inkTop s16] ink top relative to the line top (ascent above baseline) - * [width u16][height u16] - * [width*height coverage bytes, gamma-mapped] - * An ink-free glyph (space) has width = height = 0 but a valid advance. - * Empty array when the font cannot be loaded or the render fails. - */ - public static byte[] renderGlyphCell(String path, float sizePx, int codePoint, double gamma) { - Typeface typeface = loadTypeface(path); - if (typeface == null || sizePx <= 0 || codePoint < 0 || codePoint > 0x10ffff) { - return new byte[0]; - } - try { - Paint paint = glyphPaint(typeface, sizePx); - String text = new String(Character.toChars(codePoint)); - float advance = paint.measureText(text); - int advanceFixed = Math.max(0, Math.min(0xffff, Math.round(advance * 64f))); - - android.graphics.Rect bounds = new android.graphics.Rect(); - paint.getTextBounds(text, 0, text.length(), bounds); - if (bounds.isEmpty()) { - return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null); - } - // getTextBounds can be off by a hair on AA edges; render with - // padding, then trim to the actually painted box. - int pad = 2; - int bw = bounds.width() + 2 * pad; - int bh = bounds.height() + 2 * pad; - if (bw <= 0 || bh <= 0 || bw * bh > 1_000_000) { - return new byte[0]; - } - int penX = pad - bounds.left; - int baselineY = pad - bounds.top; - Bitmap bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ALPHA_8); - Canvas canvas = new Canvas(bitmap); - canvas.drawText(text, penX, baselineY, paint); - byte[] pixels = alpha8Pixels(bitmap, bw, bh); - bitmap.recycle(); - - int x0 = bw, x1 = -1, y0 = bh, y1 = -1; - for (int y = 0; y < bh; y++) { - int row = y * bw; - for (int x = 0; x < bw; x++) { - if (pixels[row + x] != 0) { - if (x < x0) x0 = x; - if (x > x1) x1 = x; - if (y < y0) y0 = y; - if (y > y1) y1 = y; - } - } - } - if (x1 < x0) { - return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null); - } - int width = x1 - x0 + 1; - int height = y1 - y0 + 1; - Paint.FontMetricsInt fm = paint.getFontMetricsInt(); - int bearingX = x0 - penX; - int inkTop = (-fm.ascent) + (y0 - baselineY); - - byte[] lut = gammaLut(gamma); - byte[] coverage = new byte[width * height]; - for (int y = 0; y < height; y++) { - int src = (y0 + y) * bw + x0; - int dst = y * width; - for (int x = 0; x < width; x++) { - coverage[dst + x] = lut[pixels[src + x] & 0xff]; - } - } - return glyphCellPacket(advanceFixed, bearingX, inkTop, width, height, coverage); - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "glyph render failed: " + path + " U+" + Integer.toHexString(codePoint), e); - return new byte[0]; - } - } - - /** - * Exact (float) advance width of a text run, with the same paint setup as - * renderGlyphCell (subpixel advances, ligatures off), so pair kerning can - * be derived as measure(ab) - measure(a) - measure(b). - */ - public static double measureTextExact(String path, String text, float sizePx) { - Typeface typeface = loadTypeface(path); - if (typeface == null || text == null || sizePx <= 0) { - return 0; - } - return glyphPaint(typeface, sizePx).measureText(text); - } - - /** Paint used by both the glyph-cell renderer and its measurements. */ - private static Paint glyphPaint(Typeface typeface, float sizePx) { - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); - paint.setTypeface(typeface); - paint.setTextSize(sizePx); - paint.setColor(Color.WHITE); - // Ligatures merge codepoints into one glyph, which the per-codepoint - // cache model cannot represent; kerning still applies. - paint.setFontFeatureSettings("'liga' off, 'clig' off"); - return paint; - } - - private static byte[] glyphCellPacket( - int advanceFixed, int bearingX, int inkTop, int width, int height, byte[] coverage) { - byte[] out = new byte[10 + (coverage == null ? 0 : coverage.length)]; - out[0] = (byte) (advanceFixed & 0xff); - out[1] = (byte) ((advanceFixed >> 8) & 0xff); - out[2] = (byte) (bearingX & 0xff); - out[3] = (byte) ((bearingX >> 8) & 0xff); - out[4] = (byte) (inkTop & 0xff); - out[5] = (byte) ((inkTop >> 8) & 0xff); - out[6] = (byte) (width & 0xff); - out[7] = (byte) ((width >> 8) & 0xff); - out[8] = (byte) (height & 0xff); - out[9] = (byte) ((height >> 8) & 0xff); - if (coverage != null) { - System.arraycopy(coverage, 0, out, 10, coverage.length); - } - return out; - } - - /** - * Line metrics for a font at a pixel size, as "ascent descent lineGap" - * (integers, pixels). Empty string if the font cannot be loaded. - */ - public static String getFontMetrics(String path, float sizePx) { - Typeface typeface = loadTypeface(path); - if (typeface == null || sizePx <= 0) { - return ""; - } - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); - paint.setTypeface(typeface); - paint.setTextSize(sizePx); - Paint.FontMetricsInt fm = paint.getFontMetricsInt(); - int lineGap = fm.leading; - return (-fm.ascent) + " " + fm.descent + " " + lineGap; - } - - /** Advance width in pixels of a line of text (rounded up). */ - public static int measureText(String path, String text, float sizePx) { - Typeface typeface = loadTypeface(path); - if (typeface == null || text == null || sizePx <= 0) { - return 0; - } - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); - paint.setTypeface(typeface); - paint.setTextSize(sizePx); - return (int) Math.ceil(paint.measureText(text)); - } - - /** - * Whether the font file loads at all (strictly validated on API 29+; - * best-effort below that). - */ - public static boolean canLoadFont(String path) { - return loadTypeface(path) != null; - } - - /** - * The font's family and style names from its 'name' table, as - * "Family\nStyle" ("Style" may be empty). Falls back to "" when the - * table cannot be parsed; callers should then use the filename. - */ - public static String getFontName(String path) { - try { - return parseNameTable(path); - } catch (Exception e) { - Log.w(TAG, "name table parse failed: " + path, e); - return ""; - } - } - - private static synchronized Typeface loadTypeface(String path) { - if (path == null) { - return null; - } - Typeface cached = typefaceCache.get(path); - if (cached != null) { - return cached; - } - File file = new File(path); - if (!file.isFile() || !file.canRead()) { - return null; - } - Typeface typeface = null; - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - // Strict path: Font.Builder throws on files that are not - // valid fonts, unlike createFromFile which silently falls - // back to the default typeface. - android.graphics.fonts.Font font = - new android.graphics.fonts.Font.Builder(file).build(); - typeface = new Typeface.CustomFallbackBuilder( - new android.graphics.fonts.FontFamily.Builder(font).build()) - .build(); - } else { - typeface = Typeface.createFromFile(file); - if (typeface == null || typeface.equals(Typeface.DEFAULT)) { - typeface = null; - } - } - } catch (Exception | LinkageError e) { - Log.w(TAG, "font load failed: " + path + ": " + e); - typeface = null; - } - if (typeface != null) { - typefaceCache.put(path, typeface); - } - return typeface; - } - - /** - * The coverage bytes of an ALPHA_8 bitmap as a tightly-packed - * width*height array (copyPixelsToBuffer copies rowBytes*height, and the - * row stride is not guaranteed to equal the width). - */ - private static byte[] alpha8Pixels(Bitmap bitmap, int width, int height) { - int rowBytes = bitmap.getRowBytes(); - byte[] raw = new byte[rowBytes * height]; - bitmap.copyPixelsToBuffer(ByteBuffer.wrap(raw)); - if (rowBytes == width) { - return raw; - } - byte[] pixels = new byte[width * height]; - for (int y = 0; y < height; y++) { - System.arraycopy(raw, y * rowBytes, pixels, y * width, width); - } - return pixels; - } - - /** [first, last] painted (nonzero) column indexes, or null if blank. */ - private static int[] paintedColumnRange(byte[] pixels, int width, int height) { - int first = width; - int last = -1; - for (int y = 0; y < height; y++) { - int row = y * width; - for (int x = 0; x < first; x++) { - if (pixels[row + x] != 0) { - first = x; - break; - } - } - for (int x = width - 1; x > last; x--) { - if (pixels[row + x] != 0) { - last = x; - break; - } - } - } - return last >= first ? new int[] { first, last } : null; - } - - private static byte[] gammaLut(double gamma) { - byte[] lut = new byte[256]; - if (gamma <= 0) { - gamma = 1.0; - } - for (int i = 0; i < 256; i++) { - lut[i] = (byte) Math.max(0, Math.min(255, - (int) Math.round(255.0 * Math.pow(i / 255.0, gamma)))); - } - return lut; - } - - // --- Minimal 'name' table reader (TTF/OTF/TTC) for preview titles --- - - private static String parseNameTable(String path) throws Exception { - try (RandomAccessFile raf = new RandomAccessFile(path, "r")) { - long fileSize = raf.length(); - if (fileSize < 12) { - return ""; - } - long offset = 0; - int tag = readU32(raf, 0); - if (tag == 0x74746366) { // 'ttcf': use the first face - if (readU32(raf, 8) < 1) { - return ""; - } - offset = readU32(raf, 12) & 0xffffffffL; - } - int numTables = readU16(raf, offset + 4); - long nameOffset = -1; - for (int i = 0; i < numTables; i++) { - long rec = offset + 12 + i * 16L; - if (rec + 16 > fileSize) { - return ""; - } - if (readU32(raf, rec) == 0x6e616d65) { // 'name' - nameOffset = readU32(raf, rec + 8) & 0xffffffffL; - break; - } - } - if (nameOffset < 0 || nameOffset + 6 > fileSize) { - return ""; - } - int count = readU16(raf, nameOffset + 2); - long stringStorage = nameOffset + readU16(raf, nameOffset + 4); - String family = null; - String style = null; - String preferredFamily = null; - String preferredStyle = null; - for (int i = 0; i < count; i++) { - long rec = nameOffset + 6 + i * 12L; - if (rec + 12 > fileSize) { - break; - } - int platform = readU16(raf, rec); - int nameId = readU16(raf, rec + 6); - if (nameId != 1 && nameId != 2 && nameId != 16 && nameId != 17) { - continue; - } - int length = readU16(raf, rec + 8); - long strOffset = stringStorage + readU16(raf, rec + 10); - if (strOffset + length > fileSize || length <= 0 || length > 512) { - continue; - } - byte[] data = new byte[length]; - raf.seek(strOffset); - raf.readFully(data); - // Platform 0 (Unicode) and 3 (Windows) store UTF-16BE; - // platform 1 (Mac) is close enough to Latin-1 for names. - String value = platform == 1 - ? new String(data, java.nio.charset.StandardCharsets.ISO_8859_1) - : new String(data, java.nio.charset.StandardCharsets.UTF_16BE); - value = value.trim(); - if (value.isEmpty()) { - continue; - } - if (nameId == 1 && family == null) family = value; - if (nameId == 2 && style == null) style = value; - if (nameId == 16 && preferredFamily == null) preferredFamily = value; - if (nameId == 17 && preferredStyle == null) preferredStyle = value; - } - String outFamily = preferredFamily != null ? preferredFamily : family; - String outStyle = preferredStyle != null ? preferredStyle : style; - if (outFamily == null) { - return ""; - } - return outFamily + "\n" + (outStyle == null ? "" : outStyle); - } - } - - private static int readU16(RandomAccessFile raf, long offset) throws Exception { - raf.seek(offset); - byte[] b = new byte[2]; - raf.readFully(b); - return ByteBuffer.wrap(b).order(ByteOrder.BIG_ENDIAN).getShort() & 0xffff; - } - - private static int readU32(RandomAccessFile raf, long offset) throws Exception { - raf.seek(offset); - byte[] b = new byte[4]; - raf.readFully(b); - return ByteBuffer.wrap(b).order(ByteOrder.BIG_ENDIAN).getInt(); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.kt new file mode 100644 index 00000000..fa33d1c3 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.kt @@ -0,0 +1,377 @@ +package com.faceclaw.app + +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.graphics.Typeface +import android.os.Build +import android.util.Log + +import java.io.File +import java.io.RandomAccessFile +import java.nio.ByteBuffer +import java.util.LinkedHashMap + +/** + * Rasterizes text using font files (TTF/OTF/TTC) via the Android text stack, + * so shaping, kerning, and antialiasing are all delegated to + * minikin/HarfBuzz. Output uses the same grayscale packet format as + * ImageFileLoader ([widthLo, widthHi, heightLo, heightHi, pixels...], one + * byte per pixel, row-major), which the TS side turns into a GrayImage and + * the compositor later quantizes to the 4bpp the firmware wants. The packet + * layouts, gamma mapping and ink cropping are shared with iOS in GlyphPacket; + * only rasterization lives here. + * + * The gamma parameter maps antialiased coverage to output shade: + * out = 255 * (coverage/255)^gamma. The G2 display response looks roughly + * linear, so 1.0 is the expected default; the font previewer exposes it for + * on-hardware comparison. + */ +class FontFileRenderer private constructor() { + companion object { + private const val TAG = "FontFileRenderer" + private const val TYPEFACE_CACHE_SIZE = 4 + + /** Small LRU of loaded typefaces keyed by file path. */ + private val typefaceCache: MutableMap = + object : LinkedHashMap(TYPEFACE_CACHE_SIZE, 0.75f, true) { + override fun removeEldestEntry(eldest: MutableMap.MutableEntry): Boolean { + return size > TYPEFACE_CACHE_SIZE + } + } + + /** + * Render one line of text with the given font file at the given pixel + * size. The returned image is the font's full vertical extent (top to + * bottom, taller than ascent+descent), plus any horizontal glyph + * overhang beyond the advance width. + * + * Returns an empty array if the font cannot be loaded or the text + * renders to nothing. + */ + @JvmStatic + fun renderText(path: String?, text: String?, sizePx: Float, gamma: Double): ByteArray { + if (path == null || text == null || sizePx <= 0) { + return ByteArray(0) + } + val typeface = loadTypeface(path) + if (typeface == null) { + return ByteArray(0) + } + try { + val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG) + paint.typeface = typeface + paint.textSize = sizePx + paint.color = Color.WHITE + + // top/bottom (the font's maximal extent) rather than + // ascent/descent, so diacritics and swashes don't clip. + val fm = paint.fontMetricsInt + val ascent = -fm.top + val height = -fm.top + fm.bottom + // measureText gives advance width; italic/swash glyphs can paint + // outside it, so pad by half an em on each side and trim after. + val pad = Math.ceil((sizePx / 2).toDouble()).toInt() + val advance = Math.ceil(paint.measureText(text).toDouble()).toInt() + val width = advance + 2 * pad + if (width <= 0 || height <= 0 || width * height > 4_000_000) { + return ByteArray(0) + } + + val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8) + val canvas = Canvas(bitmap) + canvas.drawText(text, pad.toFloat(), ascent.toFloat(), paint) + val pixels = alpha8Pixels(bitmap, width, height) + bitmap.recycle() + + // Trim the horizontal padding down to the painted extent, but + // keep at least the advance width so spacing stays truthful. + var left = pad + var right = pad + advance + val painted = GlyphPacket.paintedColumnRange(pixels, width, height) + if (painted != null) { + left = Math.min(left, painted[0]) + right = Math.max(right, painted[1] + 1) + } + val outWidth = Math.max(1, right - left) + return GlyphPacket.packImageRegion(pixels, width, left, 0, outWidth, height, gamma) + } catch (e: Exception) { + Log.w(TAG, "text render failed: $path", e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "text render failed: $path", e) + return ByteArray(0) + } + } + + /** + * Render a multi-line block of text wrapped to maxWidth, with line + * breaking delegated to StaticLayout (so it matches Android text + * behavior, including breaking inside long words). Truncated with an + * ellipsis past maxLines. Same packet format as renderText. + */ + @JvmStatic + fun renderWrapped( + path: String?, text: String?, sizePx: Float, maxWidth: Int, gamma: Double, maxLines: Int): ByteArray { + if (path == null || text == null || sizePx <= 0 || maxWidth <= 0 || maxLines <= 0) { + return ByteArray(0) + } + val typeface = loadTypeface(path) + if (typeface == null) { + return ByteArray(0) + } + try { + val paint = android.text.TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG) + paint.typeface = typeface + paint.textSize = sizePx + paint.color = Color.WHITE + paint.hinting = Paint.HINTING_ON + + val layout = android.text.StaticLayout.Builder + .obtain(text, 0, text.length, paint, maxWidth) + .setAlignment(android.text.Layout.Alignment.ALIGN_NORMAL) + .setIncludePad(false) + .setMaxLines(maxLines) + .setEllipsize(android.text.TextUtils.TruncateAt.END) + .build() + val height = Math.max(1, layout.height) + if (maxWidth.toLong() * height > 4_000_000L) { + return ByteArray(0) + } + + val bitmap = Bitmap.createBitmap(maxWidth, height, Bitmap.Config.ALPHA_8) + val canvas = Canvas(bitmap) + layout.draw(canvas) + val pixels = alpha8Pixels(bitmap, maxWidth, height) + bitmap.recycle() + + return GlyphPacket.packImage(pixels, maxWidth, height, gamma) + } catch (e: Exception) { + Log.w(TAG, "wrapped render failed: $path", e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "wrapped render failed: $path", e) + return ByteArray(0) + } + } + + /** + * Render one codepoint as a tight-ink-box antialiased glyph cell for the + * texture-cache text pipeline (see app/graphics/ttf-font.ts). Ligatures + * are disabled so a per-codepoint raster plus pairwise kerning (measured + * via measureTextExact) fully describes a line. Little-endian packet: + * [advance u16, 26.6 fixed point] + * [bearingX s16] ink left relative to the pen position + * [inkTop s16] ink top relative to the line top (ascent above baseline) + * [width u16][height u16] + * [width*height coverage bytes, gamma-mapped] + * An ink-free glyph (space) has width = height = 0 but a valid advance. + * Empty array when the font cannot be loaded or the render fails. + */ + @JvmStatic + fun renderGlyphCell(path: String?, sizePx: Float, codePoint: Int, gamma: Double): ByteArray { + val typeface = loadTypeface(path) + if (typeface == null || sizePx <= 0 || codePoint < 0 || codePoint > 0x10ffff) { + return ByteArray(0) + } + try { + val paint = glyphPaint(typeface, sizePx) + val text = String(Character.toChars(codePoint)) + val advance = paint.measureText(text) + val advanceFixed = Math.max(0, Math.min(0xffff, Math.round(advance * 64f))) + + val bounds = android.graphics.Rect() + paint.getTextBounds(text, 0, text.length, bounds) + if (bounds.isEmpty) { + return GlyphPacket.emptyGlyphCell(advanceFixed) + } + // getTextBounds can be off by a hair on AA edges; render with + // padding, then trim to the actually painted box. + val pad = 2 + val bw = bounds.width() + 2 * pad + val bh = bounds.height() + 2 * pad + if (bw <= 0 || bh <= 0 || bw * bh > 1_000_000) { + return ByteArray(0) + } + val penX = pad - bounds.left + val baselineY = pad - bounds.top + val bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ALPHA_8) + val canvas = Canvas(bitmap) + canvas.drawText(text, penX.toFloat(), baselineY.toFloat(), paint) + val pixels = alpha8Pixels(bitmap, bw, bh) + bitmap.recycle() + + // Trim to the tight ink box; bearing/inkTop become pen- and + // line-relative inside the shared packer. + val fm = paint.fontMetricsInt + return GlyphPacket.packGlyphCell( + advanceFixed, pixels, bw, bh, penX, baselineY, -fm.ascent, gamma) + } catch (e: Exception) { + Log.w(TAG, "glyph render failed: " + path + " U+" + Integer.toHexString(codePoint), e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "glyph render failed: " + path + " U+" + Integer.toHexString(codePoint), e) + return ByteArray(0) + } + } + + /** + * Exact (float) advance width of a text run, with the same paint setup as + * renderGlyphCell (subpixel advances, ligatures off), so pair kerning can + * be derived as measure(ab) - measure(a) - measure(b). + */ + @JvmStatic + fun measureTextExact(path: String?, text: String?, sizePx: Float): Double { + val typeface = loadTypeface(path) + if (typeface == null || text == null || sizePx <= 0) { + return 0.0 + } + return glyphPaint(typeface, sizePx).measureText(text).toDouble() + } + + /** Paint used by both the glyph-cell renderer and its measurements. */ + @JvmStatic + private fun glyphPaint(typeface: Typeface, sizePx: Float): Paint { + val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG) + paint.typeface = typeface + paint.textSize = sizePx + paint.color = Color.WHITE + // Ligatures merge codepoints into one glyph, which the per-codepoint + // cache model cannot represent; kerning still applies. + paint.fontFeatureSettings = "'liga' off, 'clig' off" + return paint + } + + /** + * Line metrics for a font at a pixel size, as "ascent descent lineGap" + * (integers, pixels). Empty string if the font cannot be loaded. + */ + @JvmStatic + fun getFontMetrics(path: String?, sizePx: Float): String { + val typeface = loadTypeface(path) + if (typeface == null || sizePx <= 0) { + return "" + } + val paint = Paint(Paint.ANTI_ALIAS_FLAG) + paint.typeface = typeface + paint.textSize = sizePx + val fm = paint.fontMetricsInt + val lineGap = fm.leading + return (-fm.ascent).toString() + " " + fm.descent + " " + lineGap + } + + /** Advance width in pixels of a line of text (rounded up). */ + @JvmStatic + fun measureText(path: String?, text: String?, sizePx: Float): Int { + val typeface = loadTypeface(path) + if (typeface == null || text == null || sizePx <= 0) { + return 0 + } + val paint = Paint(Paint.ANTI_ALIAS_FLAG) + paint.typeface = typeface + paint.textSize = sizePx + return Math.ceil(paint.measureText(text).toDouble()).toInt() + } + + /** + * Whether the font file loads at all (strictly validated on API 29+; + * best-effort below that). + */ + @JvmStatic + fun canLoadFont(path: String?): Boolean { + return loadTypeface(path) != null + } + + /** + * The font's family and style names from its 'name' table, as + * "Family\nStyle" ("Style" may be empty). Falls back to "" when the + * table cannot be parsed; callers should then use the filename. + */ + @JvmStatic + fun getFontName(path: String?): String { + if (path == null) { + return "" + } + try { + RandomAccessFile(path, "r").use { raf -> + return OpenTypeNames.parse(object : RandomAccessBytes { + override val length: Long + get() = raf.length() + + override fun read(offset: Long, into: ByteArray, count: Int) { + raf.seek(offset) + raf.readFully(into, 0, count) + } + }) + } + } catch (e: Exception) { + Log.w(TAG, "name table parse failed: $path", e) + return "" + } + } + + @JvmStatic + @Synchronized + private fun loadTypeface(path: String?): Typeface? { + if (path == null) { + return null + } + val cached = typefaceCache[path] + if (cached != null) { + return cached + } + val file = File(path) + if (!file.isFile || !file.canRead()) { + return null + } + var typeface: Typeface? = null + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + // Strict path: Font.Builder throws on files that are not + // valid fonts, unlike createFromFile which silently falls + // back to the default typeface. + val font = android.graphics.fonts.Font.Builder(file).build() + typeface = Typeface.CustomFallbackBuilder( + android.graphics.fonts.FontFamily.Builder(font).build()) + .build() + } else { + typeface = Typeface.createFromFile(file) + if (typeface == null || typeface == Typeface.DEFAULT) { + typeface = null + } + } + } catch (e: Exception) { + Log.w(TAG, "font load failed: $path: $e") + typeface = null + } catch (e: LinkageError) { + Log.w(TAG, "font load failed: $path: $e") + typeface = null + } + if (typeface != null) { + typefaceCache[path] = typeface + } + return typeface + } + + /** + * The coverage bytes of an ALPHA_8 bitmap as a tightly-packed + * width*height array (copyPixelsToBuffer copies rowBytes*height, and the + * row stride is not guaranteed to equal the width). + */ + @JvmStatic + private fun alpha8Pixels(bitmap: Bitmap, width: Int, height: Int): ByteArray { + val rowBytes = bitmap.rowBytes + val raw = ByteArray(rowBytes * height) + bitmap.copyPixelsToBuffer(ByteBuffer.wrap(raw)) + if (rowBytes == width) { + return raw + } + val pixels = ByteArray(width * height) + for (y in 0 until height) { + System.arraycopy(raw, y * rowBytes, pixels, y * width, width) + } + return pixels + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.java deleted file mode 100644 index 4dba5ac0..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.java +++ /dev/null @@ -1,567 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; -import android.os.SystemClock; -import android.util.Log; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -/** - * Per-frame timing instrumentation, shared between the Java BLE layer and the - * Typescript UI layer. A "frame" starts when we receive an input event (or a - * timer fires and we decide to redraw), and finishes when the resulting screen - * update has been fully transmitted to the glasses, is discarded unsent, or - * times out because nobody reported finishing it. - * - * Frames form a tree. One input event can fan out into several renders (the - * focused app's window plus the shell chrome, say), and each of those is its - * own frame linked back to the input frame that caused it via - * {@link #startFrame(String, int)}. The export nests descendants under their - * root and renders every line against the root's clock, so a single block - * shows the whole input-to-pixels story. Latency percentiles are measured over - * roots: root start to the first descendant that actually reached the glasses. - * - * All public methods are thread-safe and cheap enough to call from the BLE - * worker thread, the Android main thread, the JS thread, and app worker - * threads. Frame IDs are positive ints; 0 means "no frame" and is silently - * ignored everywhere, so callers do not need to null-check. - * - * Statistics and full log lines for recent and slowest frames are periodically - * exported to getExternalFilesDir()/frame-timings.txt, retrievable via - * adb pull /sdcard/Android/data//files/frame-timings.txt - */ -public final class FrameTimings { - private static final String TAG = "FrameTimings"; - private static final FrameTimings INSTANCE = new FrameTimings(); - - /** Frames still open after this long are finished as "timeout". */ - private static final long FRAME_TIMEOUT_MS = 30_000; - private static final long EXPORT_INTERVAL_MS = 15_000; - private static final long SWEEP_INTERVAL_MS = 5_000; - private static final int RECENT_ROOTS_KEPT = 40; - private static final int SLOWEST_ROOTS_KEPT = 20; - private static final int SENT_DURATIONS_WINDOW = 512; - private static final int MAX_LINES_PER_FRAME = 200; - /** - * Frames retained for ID lookup. Well above the number any export can - * reference, so a late log/span/finish call always finds its frame; frames - * older than this are unreachable and get collected. - */ - private static final int FRAMES_RETAINED = 1024; - private static final String EXPORT_FILE_NAME = "frame-timings.txt"; - - public static FrameTimings getInstance() { - return INSTANCE; - } - - private static final class Line { - final long atMs; // SystemClock.elapsedRealtime() - final String thread; - final String message; - - Line(long atMs, String thread, String message) { - this.atMs = atMs; - this.thread = thread; - this.message = message; - } - } - - private static final class Frame { - final int id; - final long startedAtMs; // SystemClock.elapsedRealtime() - final long startedWallClockMs; - final Frame parent; - final List children = new ArrayList<>(0); - final List lines = new ArrayList<>(); - final Map openSpans = new LinkedHashMap<>(); - String reason; - long finishedAtMs; - String outcome; // null while open; "sent", "discarded: ...", "timeout: ..." - /** Set on the root once some frame in its subtree reached the glasses. */ - boolean subtreeSentRecorded; - - Frame(int id, String reason, long startedAtMs, long startedWallClockMs, Frame parent) { - this.id = id; - this.reason = reason; - this.startedAtMs = startedAtMs; - this.startedWallClockMs = startedWallClockMs; - this.parent = parent; - } - - Frame root() { - Frame frame = this; - while (frame.parent != null) { - frame = frame.parent; - } - return frame; - } - - boolean isOpen() { - return outcome == null; - } - - long durationMs() { - return finishedAtMs - startedAtMs; - } - - boolean wasSent() { - return outcome != null && outcome.startsWith("sent"); - } - - /** Last moment anything in this subtree happened, for whole-tree duration. */ - long subtreeEndAtMs(long nowMs) { - long end = isOpen() ? nowMs : finishedAtMs; - for (Frame child : children) { - end = Math.max(end, child.subtreeEndAtMs(nowMs)); - } - return end; - } - - boolean subtreeHasOpenFrame() { - if (isOpen()) { - return true; - } - for (Frame child : children) { - if (child.subtreeHasOpenFrame()) { - return true; - } - } - return false; - } - } - - private final Object lock = new Object(); - /** - * Every frame we still retain, keyed by ID: open frames plus enough - * history that late calls and the export can still resolve one. - */ - private final LinkedHashMap framesById = - new LinkedHashMap(64, 0.75f, false) { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > FRAMES_RETAINED && !eldest.getValue().isOpen(); - } - }; - private final ArrayDeque recentRoots = new ArrayDeque<>(); - private final List slowestRoots = new ArrayList<>(); - private final ArrayDeque inputLatencies = new ArrayDeque<>(); - private final ArrayDeque renderLatencies = new ArrayDeque<>(); - private int nextFrameId = 1; - private long framesStarted; - private long framesSent; - private long framesDiscarded; - private long framesTimedOut; - private boolean exportDirty; - private File exportDir; - private Thread exportThread; - - private FrameTimings() { - } - - /** Idempotent; enables filesystem export. Safe to call from any constructor path. */ - public void init(Context context) { - File dir = context.getApplicationContext().getExternalFilesDir(null); - if (dir == null) { - dir = context.getApplicationContext().getFilesDir(); - } - synchronized (lock) { - exportDir = dir; - if (exportThread == null) { - exportThread = new Thread(this::exportLoop, "FrameTimingsExport"); - exportThread.setDaemon(true); - exportThread.start(); - } - } - } - - /** Begin a root frame; reason is a short label like "input:sys-event type=0". */ - public int startFrame(String reason) { - return startFrame(reason, 0); - } - - /** - * Begin a frame caused by an existing one (parentFrameId; 0 for a root). - * The child is nested under its root in the export and its latency counts - * against the root, so an input event that fans out into an app render and - * a shell-chrome render reads as one timeline instead of three. - */ - public int startFrame(String reason, int parentFrameId) { - long now = SystemClock.elapsedRealtime(); - Frame frame; - synchronized (lock) { - Frame parent = framesById.get(parentFrameId); - frame = new Frame(nextFrameId++, reason == null ? "" : reason, now, - System.currentTimeMillis(), parent); - framesById.put(frame.id, frame); - framesStarted++; - if (parent != null) { - parent.children.add(frame); - addLineLocked(parent, now, "spawned frame#" + frame.id + " (" + frame.reason + ")"); - } - } - return frame.id; - } - - /** - * Append to a frame's label, for facts only known after it started (which - * app is in the foreground, which window a render is for). Shows up in the - * frame's header line, so the export is scannable without reading bodies. - */ - public void annotate(int frameId, String text) { - if (text == null || text.isEmpty()) { - return; - } - synchronized (lock) { - Frame frame = framesById.get(frameId); - if (frame == null) { - return; - } - frame.reason = frame.reason.isEmpty() ? text : frame.reason + " " + text; - } - } - - public void log(int frameId, String message) { - long now = SystemClock.elapsedRealtime(); - synchronized (lock) { - Frame frame = openFrameLocked(frameId); - if (frame == null) { - return; - } - addLineLocked(frame, now, message); - } - } - - public void spanStart(int frameId, String name) { - long now = SystemClock.elapsedRealtime(); - synchronized (lock) { - Frame frame = openFrameLocked(frameId); - if (frame == null) { - return; - } - frame.openSpans.put(name, now); - addLineLocked(frame, now, "span " + name + " start"); - } - } - - public void spanEnd(int frameId, String name) { - long now = SystemClock.elapsedRealtime(); - synchronized (lock) { - Frame frame = openFrameLocked(frameId); - if (frame == null) { - return; - } - Long startedAt = frame.openSpans.remove(name); - if (startedAt == null) { - addLineLocked(frame, now, "span " + name + " end (start not recorded)"); - } else { - addLineLocked(frame, now, "span " + name + " end (" + (now - startedAt) + "ms)"); - } - } - } - - /** - * Finish a frame. Outcomes: "sent" (counted in latency percentiles), anything - * starting with "discarded", or "timeout". First finish wins; later calls for - * the same frame are ignored, so racing completion paths are safe. - */ - public void finishFrame(int frameId, String outcome) { - long now = SystemClock.elapsedRealtime(); - Frame finished; - synchronized (lock) { - Frame frame = openFrameLocked(frameId); - if (frame == null) { - return; - } - finishFrameLocked(frame, now, outcome == null ? "discarded: no outcome given" : outcome); - finished = frame; - } - Log.i(TAG, "frame#" + finished.id + " [" + finished.reason + "] -> " + finished.outcome - + " in " + finished.durationMs() + "ms"); - } - - /** One-line stats summary, e.g. for showing in the phone UI. */ - public String statsSummary() { - synchronized (lock) { - long[] input = percentilesLocked(inputLatencies); - return "frames started=" + framesStarted + " sent=" + framesSent - + " discarded=" + framesDiscarded + " timeout=" + framesTimedOut - + (input == null - ? "" - : " | input-to-display p50=" + input[0] + "ms p90=" + input[1] - + "ms p99=" + input[2] + "ms max=" + input[3] + "ms"); - } - } - - // --------------------------------------------------------------------- - - /** The frame with this ID if it exists and is still open, else null. */ - private Frame openFrameLocked(int frameId) { - if (frameId <= 0) { - return null; - } - Frame frame = framesById.get(frameId); - return frame != null && frame.isOpen() ? frame : null; - } - - private void addLineLocked(Frame frame, long now, String message) { - if (frame.lines.size() >= MAX_LINES_PER_FRAME) { - if (frame.lines.size() == MAX_LINES_PER_FRAME) { - frame.lines.add(new Line(now, Thread.currentThread().getName(), "... line cap reached")); - } - return; - } - frame.lines.add(new Line(now, Thread.currentThread().getName(), message)); - } - - private void finishFrameLocked(Frame frame, long now, String outcome) { - frame.finishedAtMs = now; - frame.outcome = outcome; - for (Map.Entry open : frame.openSpans.entrySet()) { - frame.lines.add(new Line(now, Thread.currentThread().getName(), - "span " + open.getKey() + " never ended (started at +" - + (open.getValue() - frame.startedAtMs) + "ms)")); - } - frame.openSpans.clear(); - addLineLocked(frame, now, "finished: " + outcome); - - Frame root = frame.root(); - if (frame.wasSent()) { - framesSent++; - // The user-visible latency is input (or timer) to the first pixels - // that actually reached the glasses, wherever in the tree that was. - if (!root.subtreeSentRecorded) { - root.subtreeSentRecorded = true; - recordLatencyLocked(root, now - root.startedAtMs); - insertSlowestLocked(root); - } - } else if (outcome.startsWith("timeout")) { - framesTimedOut++; - } else { - framesDiscarded++; - } - - if (root == frame) { - recentRoots.addLast(frame); - while (recentRoots.size() > RECENT_ROOTS_KEPT) { - recentRoots.removeFirst(); - } - } - exportDirty = true; - } - - private void recordLatencyLocked(Frame root, long latencyMs) { - ArrayDeque bucket = root.reason.startsWith("input:") ? inputLatencies : renderLatencies; - bucket.addLast(latencyMs); - while (bucket.size() > SENT_DURATIONS_WINDOW) { - bucket.removeFirst(); - } - } - - private void insertSlowestLocked(Frame root) { - if (!slowestRoots.contains(root)) { - slowestRoots.add(root); - } - // Sorted and trimmed at export time, when every subtree duration is final. - } - - /** {p50, p90, p99, max} over a rolling latency window, or null if empty. */ - private long[] percentilesLocked(ArrayDeque window) { - if (window.isEmpty()) { - return null; - } - List sorted = new ArrayList<>(window); - Collections.sort(sorted); - return new long[] { - percentileOfSorted(sorted, 50), - percentileOfSorted(sorted, 90), - percentileOfSorted(sorted, 99), - sorted.get(sorted.size() - 1), - }; - } - - private static long percentileOfSorted(List sorted, int percentile) { - int index = (int) Math.ceil(percentile / 100.0 * sorted.size()) - 1; - return sorted.get(Math.max(0, Math.min(sorted.size() - 1, index))); - } - - private void sweepTimedOutFrames() { - long now = SystemClock.elapsedRealtime(); - List timedOut = new ArrayList<>(); - synchronized (lock) { - for (Frame frame : new ArrayList<>(framesById.values())) { - if (frame.isOpen() && now - frame.startedAtMs >= FRAME_TIMEOUT_MS) { - finishFrameLocked(frame, now, "timeout: never reported finished"); - timedOut.add(frame); - } - } - } - for (Frame frame : timedOut) { - Log.w(TAG, "frame#" + frame.id + " [" + frame.reason + "] timed out after " - + frame.durationMs() + "ms without being finished"); - } - } - - private void exportLoop() { - long lastExportAtMs = 0; - while (true) { - try { - Thread.sleep(SWEEP_INTERVAL_MS); - } catch (InterruptedException e) { - return; - } - try { - sweepTimedOutFrames(); - long now = SystemClock.elapsedRealtime(); - boolean shouldExport; - synchronized (lock) { - shouldExport = exportDirty && exportDir != null && now - lastExportAtMs >= EXPORT_INTERVAL_MS; - } - if (shouldExport) { - lastExportAtMs = now; - exportToFile(); - } - } catch (Throwable t) { - Log.w(TAG, "export loop error", t); - } - } - } - - private void exportToFile() { - String content; - File dir; - synchronized (lock) { - dir = exportDir; - if (dir == null) { - return; - } - content = buildExportLocked(); - exportDirty = false; - } - File target = new File(dir, EXPORT_FILE_NAME); - File temp = new File(dir, EXPORT_FILE_NAME + ".tmp"); - try { - try (Writer writer = new OutputStreamWriter(new FileOutputStream(temp), StandardCharsets.UTF_8)) { - writer.write(content); - } - if (!temp.renameTo(target)) { - Log.w(TAG, "failed to rename " + temp + " to " + target); - } - } catch (Throwable t) { - Log.w(TAG, "failed to export frame timings", t); - } - } - - private String buildExportLocked() { - long now = SystemClock.elapsedRealtime(); - SimpleDateFormat wallClockFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US); - StringBuilder out = new StringBuilder(64 * 1024); - out.append("FrameTimings export at ").append(wallClockFormat.format(new Date())).append('\n'); - out.append(statsSummaryLocked()).append('\n'); - out.append('\n'); - out.append("Frames are trees: an input event's own frame is the root, and the renders it\n"); - out.append("caused are indented under it with offsets measured from the root's start.\n"); - out.append('\n'); - - Collections.sort(slowestRoots, (a, b) -> - Long.compare(b.subtreeEndAtMs(now) - b.startedAtMs, a.subtreeEndAtMs(now) - a.startedAtMs)); - while (slowestRoots.size() > SLOWEST_ROOTS_KEPT) { - slowestRoots.remove(slowestRoots.size() - 1); - } - - out.append("=== slowest frames that reached the glasses ===\n"); - for (Frame frame : slowestRoots) { - appendTreeLocked(out, frame, frame, wallClockFormat, now); - } - - out.append("=== recent frames (oldest first) ===\n"); - for (Frame frame : recentRoots) { - appendTreeLocked(out, frame, frame, wallClockFormat, now); - } - return out.toString(); - } - - private String statsSummaryLocked() { - long[] input = percentilesLocked(inputLatencies); - long[] render = percentilesLocked(renderLatencies); - StringBuilder out = new StringBuilder(); - out.append("frames started=").append(framesStarted).append(" sent=").append(framesSent) - .append(" discarded=").append(framesDiscarded).append(" timeout=").append(framesTimedOut) - .append(" open=").append(openFrameCountLocked()); - appendPercentilesLocked(out, "input-to-display latency", inputLatencies.size(), input); - appendPercentilesLocked(out, "render-to-display latency", renderLatencies.size(), render); - return out.toString(); - } - - private void appendPercentilesLocked(StringBuilder out, String label, int count, long[] percentiles) { - if (percentiles == null) { - return; - } - out.append('\n').append(label).append(" (last ").append(count).append("): p50=") - .append(percentiles[0]).append("ms p90=").append(percentiles[1]) - .append("ms p99=").append(percentiles[2]).append("ms max=").append(percentiles[3]).append("ms"); - } - - private int openFrameCountLocked() { - int open = 0; - for (Frame frame : framesById.values()) { - if (frame.isOpen()) { - open++; - } - } - return open; - } - - /** Print a frame and its descendants, all timed against root's start. */ - private void appendTreeLocked( - StringBuilder out, Frame frame, Frame root, SimpleDateFormat wallClockFormat, long now) { - int depth = 0; - for (Frame walk = frame; walk != root; walk = walk.parent) { - depth++; - } - String indent = repeat(" ", depth); - long startOffsetMs = frame.startedAtMs - root.startedAtMs; - out.append(indent).append("frame#").append(frame.id) - .append(" [").append(frame.reason).append(']'); - if (frame == root) { - out.append(" started ").append(wallClockFormat.format(new Date(frame.startedWallClockMs))); - long totalMs = frame.subtreeEndAtMs(now) - frame.startedAtMs; - out.append(" duration ").append(frame.durationMs()).append("ms"); - if (!frame.children.isEmpty()) { - out.append(" (tree ").append(totalMs).append("ms") - .append(frame.subtreeHasOpenFrame() ? ", still open" : "").append(')'); - } - } else { - out.append(" started +").append(startOffsetMs).append("ms") - .append(" duration ").append(frame.isOpen() ? "open" : (frame.durationMs() + "ms")); - } - out.append(" outcome ").append(frame.outcome == null ? "(open)" : frame.outcome).append('\n'); - for (Line line : frame.lines) { - out.append(indent).append(String.format(Locale.US, " %+7dms [%s] %s", - line.atMs - root.startedAtMs, line.thread, line.message)).append('\n'); - } - for (Frame child : frame.children) { - appendTreeLocked(out, child, root, wallClockFormat, now); - } - } - - private static String repeat(String unit, int times) { - StringBuilder out = new StringBuilder(unit.length() * times); - for (int i = 0; i < times; i++) { - out.append(unit); - } - return out.toString(); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.kt new file mode 100644 index 00000000..f59a9d56 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FrameTimings.kt @@ -0,0 +1,123 @@ +package com.faceclaw.app + +import android.content.Context +import android.os.SystemClock +import android.util.Log + +import java.io.File +import java.io.FileOutputStream +import java.io.OutputStreamWriter +import java.io.Writer +import java.nio.charset.StandardCharsets + +/** + * Android facade over the shared [FrameTimingsCore] (per-frame latency + * instrumentation; see that class for the frame-tree model). This class keeps + * the process-wide singleton the BLE layer and TypeScript call by name, and owns + * the Android-only parts: the export directory derived from the Context and the + * daemon thread that sweeps timed-out frames and writes the export file. + * + * Statistics and full log lines for recent and slowest frames are periodically + * exported to getExternalFilesDir()/frame-timings.txt, retrievable via + * adb pull /sdcard/Android/data//files/frame-timings.txt + */ +class FrameTimings private constructor() { + companion object { + private const val TAG = FrameTimingsCore.TAG + private val INSTANCE = FrameTimings() + private const val EXPORT_FILE_NAME = "frame-timings.txt" + + @JvmStatic + fun getInstance(): FrameTimings { + return INSTANCE + } + } + + /** The shared core; the glasses session core records frames into it directly. */ + val core = FrameTimingsCore(AndroidProtocolPlatform) + private val lock = Any() + private var exportDir: File? = null + private var exportThread: Thread? = null + + /** Idempotent; enables filesystem export. Safe to call from any constructor path. */ + fun init(context: Context) { + var dir = context.applicationContext.getExternalFilesDir(null) + if (dir == null) { + dir = context.applicationContext.filesDir + } + synchronized(lock) { + exportDir = dir + if (exportThread == null) { + val thread = Thread(Runnable { exportLoop() }, "FrameTimingsExport") + thread.isDaemon = true + thread.start() + exportThread = thread + } + } + } + + /** Begin a root frame; reason is a short label like "input:sys-event type=0". */ + fun startFrame(reason: String?): Int = core.startFrame(reason) + + /** Begin a frame caused by an existing one (parentFrameId; 0 for a root). */ + fun startFrame(reason: String?, parentFrameId: Int): Int = core.startFrame(reason, parentFrameId) + + /** Append to a frame's label, for facts only known after it started. */ + fun annotate(frameId: Int, text: String?) = core.annotate(frameId, text) + + fun log(frameId: Int, message: String?) = core.log(frameId, message) + + fun spanStart(frameId: Int, name: String) = core.spanStart(frameId, name) + + fun spanEnd(frameId: Int, name: String) = core.spanEnd(frameId, name) + + /** + * Finish a frame. Outcomes: "sent" (counted in latency percentiles), anything + * starting with "discarded", or "timeout". First finish wins. + */ + fun finishFrame(frameId: Int, outcome: String?) = core.finishFrame(frameId, outcome) + + /** One-line stats summary, e.g. for showing in the phone UI. */ + fun statsSummary(): String = core.statsSummary() + + // --------------------------------------------------------------------- + + private fun exportLoop() { + var lastExportAtMs = 0L + while (true) { + try { + Thread.sleep(FrameTimingsCore.SWEEP_INTERVAL_MS) + } catch (e: InterruptedException) { + return + } + try { + core.sweepTimedOut() + val now = SystemClock.elapsedRealtime() + val dir: File? + synchronized(lock) { dir = exportDir } + if (dir != null && core.isExportDirty && now - lastExportAtMs >= FrameTimingsCore.EXPORT_INTERVAL_MS) { + lastExportAtMs = now + exportToFile(dir) + } + } catch (t: Throwable) { + Log.w(TAG, "export loop error", t) + } + } + } + + private fun exportToFile(dir: File) { + val content = core.takeExport() ?: return + val target = File(dir, EXPORT_FILE_NAME) + val temp = File(dir, "$EXPORT_FILE_NAME.tmp") + try { + (OutputStreamWriter(FileOutputStream(temp), StandardCharsets.UTF_8) as Writer).use { writer -> + writer.write(content) + } + if (!temp.renameTo(target)) { + Log.w(TAG, "failed to rename $temp to $target") + } + } catch (t: Throwable) { + Log.w(TAG, "failed to export frame timings", t) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FwGlyphAtlas.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FwGlyphAtlas.java deleted file mode 100644 index c6535c0c..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FwGlyphAtlas.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.faceclaw.app; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.HashMap; -import java.util.Map; - -/** - * Process-wide registry of the glasses' builtin 20 px font's glyph rasters, - * fed from the TS side (EvenHubFont's extracted firmware font data). Unlike - * GlyphAtlas/ImageAtlas entries, these are never uploaded: the firmware - * already owns the font, and mode 15 draws it directly. The planner needs - * the rasters purely phone-side — for the "would this draw land correctly" - * check against the composite and for punching drawn ink out of delta rects. - * - * Placement model matches the firmware's mode-15 math: a glyph draws at - * (penX + ofsX, lineY + inkTop) where inkTop already folds in the root - * font's line height and baseline. The TS side only registers glyphs whose - * on-glasses rendering it predicts exactly (native-table glyphs; baseline- - * shifted fallback symbols stay baked). - */ -public final class FwGlyphAtlas { - private FwGlyphAtlas() {} - - public static final class Entry { - /** Draw x = pen x + ofsX. */ - public final int ofsX; - /** First raster row relative to the run's line-top y. */ - public final int inkTop; - public final int boxW; - public final int boxH; - /** 4bpp rows, stride ceil(boxW/2), high nibble = left pixel. */ - final byte[] nibbles; - - Entry(int ofsX, int inkTop, int boxW, int boxH, byte[] nibbles) { - this.ofsX = ofsX; - this.inkTop = inkTop; - this.boxW = boxW; - this.boxH = boxH; - this.nibbles = nibbles; - } - - /** The 4bpp source value at (col, row); the draw skips 0 (transparent). */ - public int nibbleAt(int col, int row) { - int b = nibbles[row * ((boxW + 1) >> 1) + (col >> 1)] & 0xff; - return (col & 1) != 0 ? (b & 0x0f) : (b >> 4); - } - } - - private static final Object lock = new Object(); - private static final Map entries = new HashMap<>(); - - /** - * Register glyph rasters. Little-endian buffer of records: - * [cp u32][ofsX s16][inkTop s16][boxW u8][boxH u8] - * [boxH x ceil(boxW/2) nibble bytes] - * Re-registration of a codepoint is ignored (the builtin font is fixed). - */ - public static void register(ByteBuffer buffer) { - if (buffer == null) return; - ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN); - synchronized (lock) { - while (in.remaining() >= 10) { - int cp = in.getInt(); - int ofsX = in.getShort(); - int inkTop = in.getShort(); - int boxW = in.get() & 0xff; - int boxH = in.get() & 0xff; - byte[] nibbles = new byte[((boxW + 1) >> 1) * boxH]; - in.get(nibbles); - if (boxW > 0 && boxH > 0 && !entries.containsKey(cp)) { - entries.put(cp, new Entry(ofsX, inkTop, boxW, boxH, nibbles)); - } - } - } - } - - public static Entry get(int cp) { - synchronized (lock) { - return entries.get(cp); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.java deleted file mode 100644 index cfba7631..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.java +++ /dev/null @@ -1,273 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; - -import java.io.BufferedOutputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.zip.DataFormatException; -import java.util.zip.Inflater; - -/** - * Accumulates composited screen frames and saves them as a looping animated - * GIF with a 16-entry grayscale palette. Stored frames are deflate-compressed - * (a typical UI frame shrinks from 166KB of indices to a few KB) so long - * recordings stay affordable in memory; consecutive identical frames are - * deduplicated, with the elapsed time folded into the next frame's delay. - */ -public final class GifScreenRecorder { - /** Backstop against unbounded growth if a recording is left running. */ - private static final int MAX_FRAMES = 3600; - /** Displayed duration of the final frame, which has no successor. */ - private static final int LAST_FRAME_DELAY_CS = 100; - /** Below this, GIF renderers historically clamp or ignore the delay. */ - private static final int MIN_FRAME_DELAY_CS = 2; - - private static final class Frame { - final byte[] deflatedIndices; - final long timestampMs; - - Frame(byte[] deflatedIndices, long timestampMs) { - this.deflatedIndices = deflatedIndices; - this.timestampMs = timestampMs; - } - } - - private final List frames = new ArrayList<>(); - /** Undeflated copy of the newest frame, kept for duplicate detection. */ - private byte[] lastIndices; - private int width; - private int height; - private boolean overflowed; - - /** - * Append the current screen. gray is 8bpp; only the top nibble (the - * display's native depth, and the palette index) is kept. - */ - public synchronized void addFrame(byte[] gray, int width, int height, long timestampMs) { - if (gray == null || width <= 0 || height <= 0 || gray.length < width * height) { - return; - } - if (frames.isEmpty()) { - this.width = width; - this.height = height; - } else if (width != this.width || height != this.height) { - return; - } - byte[] indices = new byte[width * height]; - for (int i = 0; i < indices.length; i++) { - indices[i] = (byte) ((gray[i] & 0xff) >>> 4); - } - if (lastIndices != null && Arrays.equals(indices, lastIndices)) { - return; - } - if (frames.size() >= MAX_FRAMES) { - overflowed = true; - return; - } - frames.add(new Frame(ScreenshotUtil.zlibDeflate(indices), timestampMs)); - lastIndices = indices; - } - - /** True if frames were dropped because the recording hit MAX_FRAMES. */ - public synchronized boolean isOverflowed() { - return overflowed; - } - - /** - * Encode the recording as an animated GIF in the screenshots directory. - * Returns the absolute path, or "" if no frames were captured. - */ - public synchronized String save(Context context) throws IOException { - if (frames.isEmpty()) { - return ""; - } - File file = new File(ScreenshotUtil.ensureScreenshotsDir(context), "recording-" + ScreenshotUtil.timestamp() + ".gif"); - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { - writeGif(out); - } - return file.getAbsolutePath(); - } - - private void writeGif(OutputStream out) throws IOException { - out.write('G'); - out.write('I'); - out.write('F'); - out.write('8'); - out.write('9'); - out.write('a'); - // Logical screen descriptor: global color table present, 8 bits of - // color resolution, 2^(3+1)=16 palette entries. - writeShortLE(out, width); - writeShortLE(out, height); - out.write(0x80 | 0x70 | 0x03); - out.write(0); // background color index - out.write(0); // pixel aspect ratio - for (int i = 0; i < 16; i++) { - int v = i * 17; - out.write(v); - out.write(v); - out.write(v); - } - // Netscape application extension: loop forever. - out.write(0x21); - out.write(0xff); - out.write(11); - out.write("NETSCAPE2.0".getBytes("US-ASCII")); - out.write(3); - out.write(1); - writeShortLE(out, 0); - out.write(0); - - for (int i = 0; i < frames.size(); i++) { - Frame frame = frames.get(i); - // Each frame displays until the next one was captured; the last - // frame has no successor, so give it a nominal one-second hold. - int delayCs = LAST_FRAME_DELAY_CS; - if (i + 1 < frames.size()) { - long deltaMs = frames.get(i + 1).timestampMs - frame.timestampMs; - delayCs = (int) Math.max(MIN_FRAME_DELAY_CS, Math.min(0xffff, Math.round(deltaMs / 10.0))); - } - // Graphic control extension: no disposal, no transparency. - out.write(0x21); - out.write(0xf9); - out.write(4); - out.write(0x04); // disposal method 1 (do not dispose) - writeShortLE(out, delayCs); - out.write(0); // transparent color index (unused) - out.write(0); - // Image descriptor: full frame, no local color table. - out.write(0x2c); - writeShortLE(out, 0); - writeShortLE(out, 0); - writeShortLE(out, width); - writeShortLE(out, height); - out.write(0); - writeLzwImageData(out, inflate(frame.deflatedIndices, width * height)); - } - out.write(0x3b); // trailer - } - - private static byte[] inflate(byte[] deflated, int size) throws IOException { - Inflater inflater = new Inflater(); - inflater.setInput(deflated); - byte[] data = new byte[size]; - try { - int offset = 0; - while (offset < size && !inflater.finished()) { - int n = inflater.inflate(data, offset, size - offset); - if (n == 0) break; - offset += n; - } - if (offset != size) { - throw new IOException("recorded frame truncated: " + offset + " of " + size); - } - } catch (DataFormatException e) { - throw new IOException("recorded frame corrupt", e); - } finally { - inflater.end(); - } - return data; - } - - /** GIF LZW compression of a 4-bit index stream (minimum code size 4). */ - private static void writeLzwImageData(OutputStream out, byte[] indices) throws IOException { - final int minCodeSize = 4; - out.write(minCodeSize); - SubBlockBitWriter writer = new SubBlockBitWriter(out); - final int clearCode = 1 << minCodeSize; - final int eoiCode = clearCode + 1; - int codeSize = minCodeSize + 1; - int nextCode = eoiCode + 1; - HashMap table = new HashMap<>(); - writer.writeCode(clearCode, codeSize); - int prefix = indices[0] & 0xff; - for (int i = 1; i < indices.length; i++) { - int k = indices[i] & 0xff; - int key = (prefix << 8) | k; - Integer code = table.get(key); - if (code != null) { - prefix = code; - continue; - } - writer.writeCode(prefix, codeSize); - if (nextCode == 0x1000) { - writer.writeCode(clearCode, codeSize); - table.clear(); - codeSize = minCodeSize + 1; - nextCode = eoiCode + 1; - } else { - if (nextCode >= (1 << codeSize)) { - codeSize++; - } - table.put(key, nextCode++); - } - prefix = k; - } - writer.writeCode(prefix, codeSize); - writer.writeCode(eoiCode, codeSize); - writer.finish(); - } - - /** - * Packs LZW codes LSB-first into bytes and the bytes into the 255-byte - * data sub-blocks GIF image data is carried in. - */ - private static final class SubBlockBitWriter { - private final OutputStream out; - private final byte[] block = new byte[255]; - private int blockLength; - private int bitBuffer; - private int bitCount; - - SubBlockBitWriter(OutputStream out) { - this.out = out; - } - - void writeCode(int code, int codeSize) throws IOException { - bitBuffer |= code << bitCount; - bitCount += codeSize; - while (bitCount >= 8) { - writeByte(bitBuffer & 0xff); - bitBuffer >>>= 8; - bitCount -= 8; - } - } - - void finish() throws IOException { - if (bitCount > 0) { - writeByte(bitBuffer & 0xff); - bitBuffer = 0; - bitCount = 0; - } - flushBlock(); - out.write(0); // block terminator - } - - private void writeByte(int b) throws IOException { - block[blockLength++] = (byte) b; - if (blockLength == 255) { - flushBlock(); - } - } - - private void flushBlock() throws IOException { - if (blockLength == 0) return; - out.write(blockLength); - out.write(block, 0, blockLength); - blockLength = 0; - } - } - - private static void writeShortLE(OutputStream out, int value) throws IOException { - out.write(value & 0xff); - out.write((value >>> 8) & 0xff); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.kt new file mode 100644 index 00000000..e2fde296 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/GifScreenRecorder.kt @@ -0,0 +1,27 @@ +package com.faceclaw.app + +import android.content.Context +import java.io.File +import java.io.FileOutputStream +import java.io.IOException + +/** Android storage adapter for the shared GIF recorder. */ +class GifScreenRecorder { + private val recorder = SharedGifScreenRecorder() + + fun addFrame(gray: ByteArray?, width: Int, height: Int, timestampMs: Long) { + recorder.addFrame(gray, width, height, timestampMs) + } + + fun isOverflowed(): Boolean = recorder.isOverflowed() + + @Throws(IOException::class) + fun save(context: Context): String { + val gif = recorder.encode() + if (gif.isEmpty()) return "" + val file = File(ScreenshotUtil.ensureScreenshotsDir(context), + "recording-" + ScreenshotUtil.timestamp() + ".gif") + FileOutputStream(file).use { out -> out.write(gif) } + return file.absolutePath + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java b/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java deleted file mode 100644 index c075c230..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java +++ /dev/null @@ -1,267 +0,0 @@ -package com.faceclaw.app; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; - -/** - * Process-wide registry of glyph rasters, fed from the TS side so glyph - * identity survives to the BLE encoder (see notes/texture-cache-display-list- - * design.md). A frame's glyph list references entries here by (fontId, - * encoding); the texture-cache planner uses the raster three ways: - * - * - as the ink mask for the "would this draw land correctly" check against - * the composited frame, - * - to punch the ink pixels out of the baked delta rect it replaces, - * - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE]) uploaded - * via mode 12 and drawn via mode 14 with a top-color LUT. 1bpp (BDF) - * glyphs store ink at 15 so the LUT maps them to exactly the requested - * level; AA (TTF) glyphs store true coverage nibbles the LUT scales. - * - * Fonts are identified by a stable string key (the embedded font name), NOT a - * per-JS-context counter: worker threads register independently and must agree - * on ids. Glyph rasters are immutable once registered; re-registration of the - * same (fontId, encoding) is ignored. - * - * Cell model: a cached glyph image is bbxWidth x cellHeight (the font's line - * height), with the ink rows at inkTop — so every glyph of a line is drawn at - * the same y (the line top), which is what lets one mode-14 string carry a - * whole run. The horizontal bearing (bbxX) is applied phone-side when - * computing the draw x. Vertical padding is nearly free: it RLE-encodes to a - * couple of tokens. - */ -public final class GlyphAtlas { - private GlyphAtlas() {} - - public static final class Glyph { - /** Cached image width in pixels (the glyph's tight bbox width). */ - public final int width; - /** Cached image height in pixels (the font's line height). */ - public final int cellHeight; - /** First row of ink within the cell. */ - public final int inkTop; - /** Number of ink rows. */ - public final int inkHeight; - /** Horizontal bearing: draw x = pen x + bbxX. */ - public final int bbxX; - /** 1bpp ink rows (inkHeight entries), bit (rowBitWidth-1-col) = ink; null for AA glyphs. */ - final int[] rows; - final int rowBitWidth; - /** AA coverage, width*inkHeight nibble values 0..15; null for 1bpp glyphs. */ - final byte[] coverage; - /** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px)]. */ - public final byte[] cachedBytes; - - Glyph(int width, int cellHeight, int inkTop, int inkHeight, int bbxX, - int[] rows, byte[] coverage) { - this.width = width; - this.cellHeight = cellHeight; - this.inkTop = inkTop; - this.inkHeight = inkHeight; - this.bbxX = bbxX; - this.rows = rows; - this.coverage = coverage; - this.rowBitWidth = ((width + 7) >> 3) << 3; - this.cachedBytes = encodeCachedImage(); - } - - /** - * The cell pixel's 4bpp source value at (col, row), row cell-relative. - * 1bpp glyphs are stored at 15 so the draw-time LUT (source*top/15) - * maps them to exactly the requested top color; AA glyphs carry their - * true coverage level. - */ - public int nibbleAt(int col, int row) { - if (col < 0 || col >= width) return 0; - int inkRow = row - inkTop; - if (inkRow < 0 || inkRow >= inkHeight) return 0; - if (coverage != null) return coverage[inkRow * width + col]; - return ((rows[inkRow] >>> (rowBitWidth - 1 - col)) & 1) != 0 ? 15 : 0; - } - - /** Whether the cell pixel at (col, row) has any ink (row is cell-relative). */ - public boolean inkAt(int col, int row) { - return nibbleAt(col, row) != 0; - } - - /** - * The firmware cached-image encoding: [width][height][RLE tokens] - * covering exactly width*cellHeight pixels (no row padding), each - * pixel's stored color being its nibbleAt value. - */ - private byte[] encodeCachedImage() { - int total = width * cellHeight; - // Worst case one token per pixel (1 byte each) + 2 header bytes. - byte[] out = new byte[2 + total]; - out[0] = (byte) width; - out[1] = (byte) cellHeight; - int o = 2; - int i = 0; - while (i < total) { - int color = nibbleAt(i % width, i / width); - int j = i + 1; - while (j < total && nibbleAt(j % width, j / width) == color) j++; - int run = j - i; - while (run > 0) { - int c = Math.min(run, 0xffff); - if (c <= 15) { - out[o++] = (byte) ((c << 4) | color); - } else if (c <= 255) { - // Worst case can exceed the sizing above only via escape - // forms, which occur for runs >= 16 (net win); safe. - out[o++] = (byte) color; - out[o++] = (byte) c; - } else { - out[o++] = (byte) color; - out[o++] = 0; - out[o++] = (byte) (c & 0xff); - out[o++] = (byte) (c >> 8); - } - run -= c; - } - i = j; - } - byte[] trimmed = new byte[o]; - System.arraycopy(out, 0, trimmed, 0, o); - return trimmed; - } - } - - private static final Object lock = new Object(); - private static final Map fontIds = new HashMap<>(); - private static final Map glyphs = new HashMap<>(); - private static int nextFontId = 1; - - /** Stable id for a font key; assigns one on first use. */ - public static int fontId(String key) { - synchronized (lock) { - Integer id = fontIds.get(key); - if (id == null) { - id = nextFontId++; - fontIds.put(key, id); - } - return id; - } - } - - public static Glyph get(int fontId, int encoding) { - synchronized (lock) { - return glyphs.get(key(fontId, encoding)); - } - } - - private static long key(int fontId, int encoding) { - return ((long) fontId << 32) | (encoding & 0xffffffffL); - } - - /** - * Register a batch of glyph rasters. Little-endian buffer, a sequence of - * font groups: - * [keyLen u8][key utf8][cellHeight u8][count u16] - * each followed by count glyph records: - * [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8] - * [inkHeight x rows u32] (bit (ceil(width/8)*8 - 1 - col) = ink) - * Records for an already-registered (font, encoding) are skipped (glyph - * rasters are immutable for a given font key). Malformed buffers throw: - * they indicate a phone-side marshalling bug, never device state. - */ - public static void register(ByteBuffer buffer) { - if (buffer == null) return; - ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN); - synchronized (lock) { - while (in.remaining() > 0) { - int keyLen = in.get() & 0xff; - byte[] keyBytes = new byte[keyLen]; - in.get(keyBytes); - String fontKey = new String(keyBytes, StandardCharsets.UTF_8); - int cellHeight = in.get() & 0xff; - int count = in.getShort() & 0xffff; - Integer idBoxed = fontIds.get(fontKey); - int id; - if (idBoxed == null) { - id = nextFontId++; - fontIds.put(fontKey, id); - } else { - id = idBoxed; - } - for (int g = 0; g < count; g++) { - int encoding = in.getInt(); - int bbxX = in.get(); - int inkTop = in.get() & 0xff; - int width = in.get() & 0xff; - int inkHeight = in.get() & 0xff; - int[] rows = new int[inkHeight]; - for (int r = 0; r < inkHeight; r++) { - rows[r] = in.getInt(); - } - long k = key(id, encoding); - if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0 - && inkTop + inkHeight <= cellHeight) { - glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX, - rows, null)); - } - } - } - } - } - - /** - * Register a batch of antialiased (4bpp) glyph rasters, e.g. TTF renders. - * Same font-group framing as register(), but each glyph record carries - * packed coverage instead of 1bpp rows: - * [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8] - * [inkHeight x ceil(width/2) packed bytes, high nibble = left pixel] - * Nibble values are the final 4bpp source levels (the draw-time LUT - * scales them by the top color). Already-registered (font, encoding) - * records are skipped, as in register(). - */ - public static void registerAa(ByteBuffer buffer) { - if (buffer == null) return; - ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN); - synchronized (lock) { - while (in.remaining() > 0) { - int keyLen = in.get() & 0xff; - byte[] keyBytes = new byte[keyLen]; - in.get(keyBytes); - String fontKey = new String(keyBytes, StandardCharsets.UTF_8); - int cellHeight = in.get() & 0xff; - int count = in.getShort() & 0xffff; - Integer idBoxed = fontIds.get(fontKey); - int id; - if (idBoxed == null) { - id = nextFontId++; - fontIds.put(fontKey, id); - } else { - id = idBoxed; - } - for (int g = 0; g < count; g++) { - int encoding = in.getInt(); - int bbxX = in.get(); - int inkTop = in.get() & 0xff; - int width = in.get() & 0xff; - int inkHeight = in.get() & 0xff; - int stride = (width + 1) >> 1; - byte[] coverage = new byte[width * inkHeight]; - for (int r = 0; r < inkHeight; r++) { - for (int b = 0; b < stride; b++) { - int packed = in.get() & 0xff; - int col = b * 2; - coverage[r * width + col] = (byte) (packed >> 4); - if (col + 1 < width) { - coverage[r * width + col + 1] = (byte) (packed & 0x0f); - } - } - } - long k = key(id, encoding); - if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0 - && inkTop + inkHeight <= cellHeight) { - glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX, - null, coverage)); - } - } - } - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.java deleted file mode 100644 index 80589f47..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.java +++ /dev/null @@ -1,185 +0,0 @@ -package com.faceclaw.app; - -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.Path; -import android.util.Log; - -import androidx.core.graphics.PathParser; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Renders a small subset of SVG (the shapes used by Lucide / simple Noun - * Project icons: path, circle, ellipse, rect, line, polyline, polygon) to a - * grayscale coverage buffer for use as an on-glasses icon. Icons are stroked - * (Lucide style: fill="none", 2px stroke, round caps/joins); the returned - * bytes are one coverage value per pixel (0=transparent .. 255=opaque white), - * row-major, size*size. Called once per icon and cached on the TS side. - */ -public final class IconRenderer { - private static final String TAG = "IconRenderer"; - private static final Pattern TAG_RE = Pattern.compile("<(path|circle|ellipse|rect|line|polyline|polygon)\\b([^>]*)>"); - private static final Pattern VIEWBOX_RE = Pattern.compile("viewBox\\s*=\\s*\"([^\"]*)\""); - - private IconRenderer() {} - - public static byte[] renderSvgGray(String svg, int size, float strokeWidth) { - if (svg == null || size <= 0) { - return new byte[0]; - } - try { - float minX = 0f, minY = 0f, viewW = 24f, viewH = 24f; - Matcher vb = VIEWBOX_RE.matcher(svg); - if (vb.find()) { - String[] parts = vb.group(1).trim().split("[\\s,]+"); - if (parts.length == 4) { - minX = Float.parseFloat(parts[0]); - minY = Float.parseFloat(parts[1]); - viewW = Float.parseFloat(parts[2]); - viewH = Float.parseFloat(parts[3]); - } - } - // Lucide-style icons are stroked outlines (fill="none"); everything - // else (Noun Project glyphs, brand logos) is a filled shape. - boolean stroked = svg.contains("fill=\"none\""); - Path path = new Path(); - Matcher m = TAG_RE.matcher(svg); - while (m.find()) { - appendElement(path, m.group(1), m.group(2)); - } - if (path.isEmpty()) { - return new byte[0]; - } - - Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(bitmap); - float scale = Math.min(size / viewW, size / viewH); - // Center the (square) viewBox in the bitmap, then map its origin. - canvas.translate((size - viewW * scale) / 2f, (size - viewH * scale) / 2f); - canvas.scale(scale, scale); - canvas.translate(-minX, -minY); - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); - paint.setColor(0xffffffff); - if (stroked) { - paint.setStyle(Paint.Style.STROKE); - paint.setStrokeWidth(strokeWidth); - paint.setStrokeCap(Paint.Cap.ROUND); - paint.setStrokeJoin(Paint.Join.ROUND); - } else { - paint.setStyle(Paint.Style.FILL); - } - canvas.drawPath(path, paint); - - int[] pixels = new int[size * size]; - bitmap.getPixels(pixels, 0, size, 0, 0, size, size); - byte[] gray = new byte[size * size]; - for (int i = 0; i < pixels.length; i++) { - gray[i] = (byte) quantizeCoverage((pixels[i] >>> 24) & 0xff); // alpha = coverage - } - return gray; - } catch (Exception e) { - Log.w(TAG, "icon render failed", e); - return new byte[0]; - } - } - - /** - * Snap antialiased coverage toward hard edges so the 4bpp frames sent - * over BLE deflate better: mostly-transparent and mostly-opaque pixels - * become exactly 0/255 (long runs, and 0 stays color-key transparent for - * bitBlt), and only genuinely half-covered edge pixels keep one mid gray. - * The packed frame then uses a 3-symbol alphabet instead of all 16 - * levels, at icon sizes this is visually near-identical. - */ - private static int quantizeCoverage(int alpha) { - if (alpha < 64) return 0; - if (alpha >= 192) return 255; - return 128; - } - - private static void appendElement(Path path, String tag, String attrs) { - switch (tag) { - case "path": { - String d = attr(attrs, "d"); - if (d != null) { - Path sub = PathParser.createPathFromPathData(d); - if (sub != null) { - path.addPath(sub); - } - } - break; - } - case "circle": { - float cx = num(attrs, "cx", 0), cy = num(attrs, "cy", 0), r = num(attrs, "r", 0); - if (r > 0) path.addCircle(cx, cy, r, Path.Direction.CW); - break; - } - case "ellipse": { - float cx = num(attrs, "cx", 0), cy = num(attrs, "cy", 0); - float rx = num(attrs, "rx", 0), ry = num(attrs, "ry", 0); - if (rx > 0 && ry > 0) path.addOval(cx - rx, cy - ry, cx + rx, cy + ry, Path.Direction.CW); - break; - } - case "rect": { - float x = num(attrs, "x", 0), y = num(attrs, "y", 0); - float w = num(attrs, "width", 0), h = num(attrs, "height", 0); - float rx = num(attrs, "rx", 0), ry = num(attrs, "ry", rx > 0 ? rx : 0); - if (rx <= 0) rx = ry; - if (ry <= 0) ry = rx; - if (w > 0 && h > 0) { - if (rx > 0 || ry > 0) { - path.addRoundRect(x, y, x + w, y + h, rx, ry, Path.Direction.CW); - } else { - path.addRect(x, y, x + w, y + h, Path.Direction.CW); - } - } - break; - } - case "line": { - path.moveTo(num(attrs, "x1", 0), num(attrs, "y1", 0)); - path.lineTo(num(attrs, "x2", 0), num(attrs, "y2", 0)); - break; - } - case "polyline": - case "polygon": { - String points = attr(attrs, "points"); - if (points != null) { - String[] nums = points.trim().split("[\\s,]+"); - boolean started = false; - for (int i = 0; i + 1 < nums.length; i += 2) { - float px = Float.parseFloat(nums[i]); - float py = Float.parseFloat(nums[i + 1]); - if (!started) { - path.moveTo(px, py); - started = true; - } else { - path.lineTo(px, py); - } - } - if (tag.equals("polygon")) path.close(); - } - break; - } - default: - break; - } - } - - private static String attr(String attrs, String name) { - Matcher m = Pattern.compile("\\b" + name + "\\s*=\\s*\"([^\"]*)\"").matcher(attrs); - return m.find() ? m.group(1) : null; - } - - private static float num(String attrs, String name, float fallback) { - String value = attr(attrs, name); - if (value == null) return fallback; - try { - return Float.parseFloat(value.trim()); - } catch (NumberFormatException e) { - return fallback; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.kt new file mode 100644 index 00000000..efaf4ac4 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/IconRenderer.kt @@ -0,0 +1,107 @@ +package com.faceclaw.app + +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Path +import android.util.Log + +import androidx.core.graphics.PathParser + +/** + * Renders a small subset of SVG (the shapes used by Lucide / simple Noun + * Project icons: path, circle, ellipse, rect, line, polyline, polygon) to a + * grayscale coverage buffer for use as an on-glasses icon. Icons are stroked + * (Lucide style: fill="none", 2px stroke, round caps/joins); the returned + * bytes are one coverage value per pixel (0=transparent .. 255=opaque white), + * row-major, size*size. Called once per icon and cached on the TS side. + * + * Parsing, placement math and the coverage quantization are shared with iOS + * (SvgIconSource); this class only maps shapes onto android.graphics.Path and + * rasterizes them. + */ +class IconRenderer private constructor() { + companion object { + private const val TAG = "IconRenderer" + + @JvmStatic + fun renderSvgGray(svg: String?, size: Int, strokeWidth: Float): ByteArray { + if (svg == null || size <= 0) { + return ByteArray(0) + } + try { + val icon = SvgIconSource.parse(svg) ?: return ByteArray(0) + val path = Path() + for (shape in icon.shapes) { + appendShape(path, shape) + } + if (path.isEmpty) { + return ByteArray(0) + } + + val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888) + val canvas = Canvas(bitmap) + // Center the (square) viewBox in the bitmap, then map its origin. + val placement = icon.placement(size) + canvas.translate(placement[0], placement[1]) + canvas.scale(placement[2], placement[2]) + canvas.translate(-icon.minX, -icon.minY) + val paint = Paint(Paint.ANTI_ALIAS_FLAG) + paint.color = 0xffffffff.toInt() + if (icon.stroked) { + paint.style = Paint.Style.STROKE + paint.strokeWidth = strokeWidth + paint.strokeCap = Paint.Cap.ROUND + paint.strokeJoin = Paint.Join.ROUND + } else { + paint.style = Paint.Style.FILL + } + canvas.drawPath(path, paint) + + val pixels = IntArray(size * size) + bitmap.getPixels(pixels, 0, size, 0, 0, size, size) + return SvgIconSource.quantizeArgb(pixels) // alpha = coverage + } catch (e: Exception) { + Log.w(TAG, "icon render failed", e) + return ByteArray(0) + } + } + + @JvmStatic + private fun appendShape(path: Path, shape: SvgShape) { + when (shape) { + is SvgShape.PathData -> { + val sub = PathParser.createPathFromPathData(shape.d) + if (sub != null) { + path.addPath(sub) + } + } + is SvgShape.Circle -> path.addCircle(shape.cx, shape.cy, shape.r, Path.Direction.CW) + is SvgShape.Ellipse -> path.addOval( + shape.cx - shape.rx, shape.cy - shape.ry, shape.cx + shape.rx, shape.cy + shape.ry, Path.Direction.CW) + is SvgShape.Rect -> { + if (shape.rx > 0 || shape.ry > 0) { + path.addRoundRect(shape.x, shape.y, shape.x + shape.width, shape.y + shape.height, + shape.rx, shape.ry, Path.Direction.CW) + } else { + path.addRect(shape.x, shape.y, shape.x + shape.width, shape.y + shape.height, Path.Direction.CW) + } + } + is SvgShape.Line -> { + path.moveTo(shape.x1, shape.y1) + path.lineTo(shape.x2, shape.y2) + } + is SvgShape.Polyline -> { + val p = shape.points + path.moveTo(p[0], p[1]) + var i = 2 + while (i + 1 < p.size) { + path.lineTo(p[i], p[i + 1]) + i += 2 + } + if (shape.closed) path.close() + } + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageAtlas.java b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageAtlas.java deleted file mode 100644 index 206bae6b..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageAtlas.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.faceclaw.app; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Process-wide registry of icon/image rasters for the texture-cache pipeline, - * the image counterpart of GlyphAtlas. Entries are content-addressed: the TS - * side keys each image by a hash of its dimensions and pixels, so the same - * icon registered from any thread or rendered at any time dedupes to one - * entry, and an icon whose content changes is simply a new entry. - * - * Unlike glyphs (1-bit ink recolored at draw time), images keep their exact - * 4bpp values: the cached bytes quantize the registered 8bpp pixels with the - * same GRAY_TO_NIBBLE table the composite is packed with, and the planner - * draws them via mode 13 with an identity LUT (top color 15) and the - * transparent bit — the draw writes exactly the nonzero-nibble pixels, which - * is what the eligibility check and hole punching are defined over. - */ -public final class ImageAtlas { - private ImageAtlas() {} - - public static final class Entry { - public final int width; - public final int height; - /** One 4bpp value per pixel, row-major. */ - final byte[] nibbles; - /** CFW cached-image bytes: [w][h][RLE(w*h pixels)]. */ - public final byte[] cachedBytes; - - Entry(int width, int height, byte[] nibbles) { - this.width = width; - this.height = height; - this.nibbles = nibbles; - this.cachedBytes = encodeCachedImage(); - } - - /** The 4bpp value the mode-13 draw would write at (col, row); 0 = skipped. */ - public int nibbleAt(int col, int row) { - return nibbles[row * width + col] & 0xff; - } - - private byte[] encodeCachedImage() { - int total = width * height; - byte[] out = new byte[2 + total]; - out[0] = (byte) width; - out[1] = (byte) height; - int o = 2; - int i = 0; - while (i < total) { - int color = nibbles[i] & 0xff; - int j = i + 1; - while (j < total && (nibbles[j] & 0xff) == color) j++; - int run = j - i; - while (run > 0) { - int c = Math.min(run, 0xffff); - if (c <= 15) { - out[o++] = (byte) ((c << 4) | color); - } else if (c <= 255) { - out[o++] = (byte) color; - out[o++] = (byte) c; - } else { - out[o++] = (byte) color; - out[o++] = 0; - out[o++] = (byte) (c & 0xff); - out[o++] = (byte) (c >> 8); - } - run -= c; - } - i = j; - } - byte[] trimmed = new byte[o]; - System.arraycopy(out, 0, trimmed, 0, o); - return trimmed; - } - } - - private static final Object lock = new Object(); - private static final Map ids = new HashMap<>(); - private static final List entries = new ArrayList<>(); - - /** - * Register an image (8bpp grayscale pixels, width*height bytes, row-major) - * under a content key, returning its id — or the existing id when the key - * is already registered (the pixels are then ignored: same key means same - * content). Ids are positive and stable for the process lifetime. - */ - public static int ensure(String key, int width, int height, ByteBuffer pixels8bpp) { - if (key == null || width <= 0 || width > 255 || height <= 0 || height > 255 - || pixels8bpp == null || pixels8bpp.remaining() < width * height) { - throw new IllegalArgumentException("bad image registration " + width + "x" + height); - } - synchronized (lock) { - Integer existing = ids.get(key); - if (existing != null) return existing; - byte[] nibbles = new byte[width * height]; - for (int i = 0; i < nibbles.length; i++) { - nibbles[i] = (byte) BmpUtil.nibbleForGray(pixels8bpp.get(i) & 0xff); - } - entries.add(new Entry(width, height, nibbles)); - int id = entries.size(); // ids start at 1 - ids.put(key, id); - return id; - } - } - - public static Entry get(int id) { - synchronized (lock) { - return id >= 1 && id <= entries.size() ? entries.get(id - 1) : null; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.java b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.java deleted file mode 100644 index ef6debbb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.java +++ /dev/null @@ -1,229 +0,0 @@ -package com.faceclaw.app; - -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.util.Log; - -/** - * Decodes image files (and bitmaps generally) to the grayscale packet format - * used across the TS bridge: [widthLo, widthHi, heightLo, heightHi, - * pixels...] with one byte per pixel, row-major, or an empty array on - * failure. FaceclawMediaController's album art shares bitmapToGrayPacket; - * the TS side (app/native/image-files.ts) owns the shared photo-tone preset - * passed as (gamma, dither). - */ -public final class ImageFileLoader { - private static final String TAG = "ImageFileLoader"; - - private ImageFileLoader() {} - - /** - * Decode an image file and downscale it to fit within maxWidth x - * maxHeight, preserving aspect ratio and never upscaling. Flat - * conversion (no tone curve, per-pixel rounding): right for UI raster - * such as icons. - */ - public static byte[] loadGray(String path, int maxWidth, int maxHeight) { - return loadGray(path, maxWidth, maxHeight, 1f, false); - } - - /** - * As above, with the photographic tone handling described on - * bitmapToGrayPacket(Bitmap, int, int, float, boolean): use for - * continuous-tone images (photos) viewed on the glasses. - */ - public static byte[] loadGray( - String path, int maxWidth, int maxHeight, float gamma, boolean dither) { - if (path == null || maxWidth <= 0 || maxHeight <= 0) { - return new byte[0]; - } - try { - BitmapFactory.Options bounds = new BitmapFactory.Options(); - bounds.inJustDecodeBounds = true; - BitmapFactory.decodeFile(path, bounds); - if (bounds.outWidth <= 0 || bounds.outHeight <= 0) { - return new byte[0]; - } - // Power-of-two subsampling during decode keeps large photos from - // allocating full-size ARGB buffers; exact fitting happens in - // bitmapToGrayPacket. - BitmapFactory.Options opts = new BitmapFactory.Options(); - opts.inSampleSize = 1; - while (bounds.outWidth / (opts.inSampleSize * 2) >= maxWidth - && bounds.outHeight / (opts.inSampleSize * 2) >= maxHeight) { - opts.inSampleSize *= 2; - } - Bitmap bitmap = BitmapFactory.decodeFile(path, opts); - if (bitmap == null) { - return new byte[0]; - } - return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither); - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "image decode failed: " + path, e); - return new byte[0]; - } - } - - /** - * Decode an in-memory image file (PNG/JPEG/WebP bytes, e.g. a fetched - * map tile) and downscale to fit within maxWidth x maxHeight. Takes a - * ByteBuffer because NativeScript marshals a JS ArrayBuffer to one. - */ - public static byte[] loadGrayFromBytes(java.nio.ByteBuffer buffer, int maxWidth, int maxHeight) { - return loadGrayFromBytes(buffer, maxWidth, maxHeight, 1f, false); - } - - /** As above, with photographic tone handling (see bitmapToGrayPacket). */ - public static byte[] loadGrayFromBytes( - java.nio.ByteBuffer buffer, int maxWidth, int maxHeight, float gamma, boolean dither) { - if (buffer == null || maxWidth <= 0 || maxHeight <= 0) { - return new byte[0]; - } - try { - java.nio.ByteBuffer source = buffer.duplicate(); - source.rewind(); - byte[] data = new byte[source.remaining()]; - source.get(data); - if (data.length == 0) { - return new byte[0]; - } - Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); - if (bitmap == null) { - return new byte[0]; - } - return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither); - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "byte-array image decode failed", e); - return new byte[0]; - } - } - - /** - * Scale a bitmap to fit within maxWidth x maxHeight (preserving aspect, - * never upscaling) and convert it to the grayscale packet format. - * Transparent pixels darken toward black (the on-glasses background). - */ - public static byte[] bitmapToGrayPacket(Bitmap source, int maxWidth, int maxHeight) { - return bitmapToGrayPacket(source, maxWidth, maxHeight, 1f, false); - } - - /** - * As above, with photographic tone handling for continuous-tone images - * (album art, photos) rather than UI raster: - * - * gamma > 1 darkens midtones (out = 255 * (in/255)^gamma). Source pixels - * are sRGB-encoded but the G2 drives its 16 levels roughly linearly, so - * mid-gray otherwise displays far brighter than intended; 2.2 undoes the - * sRGB encoding outright. - * - * dither error-diffuses against the 16 levels the frame is eventually - * packed to (BmpUtil.GRAY_TO_NIBBLE) instead of leaving each pixel to - * round on its own, which turns smooth gradients into visible bands. - * Dithered output is already quantized: every byte is a level times 16, - * which BmpUtil re-quantizes back to exactly that level. - */ - public static byte[] bitmapToGrayPacket( - Bitmap source, int maxWidth, int maxHeight, float gamma, boolean dither) { - if (source == null || source.getWidth() <= 0 || source.getHeight() <= 0 - || maxWidth <= 0 || maxHeight <= 0) { - return new byte[0]; - } - try { - float scale = Math.min(1f, Math.min( - (float) maxWidth / source.getWidth(), - (float) maxHeight / source.getHeight())); - int width = Math.max(1, Math.round(source.getWidth() * scale)); - int height = Math.max(1, Math.round(source.getHeight() * scale)); - Bitmap scaled = Bitmap.createScaledBitmap(source, width, height, true); - if (scaled.getConfig() == Bitmap.Config.HARDWARE) { - scaled = scaled.copy(Bitmap.Config.ARGB_8888, false); - } - int[] pixels = new int[width * height]; - scaled.getPixels(pixels, 0, width, 0, 0, width, height); - byte[] out = new byte[4 + width * height]; - out[0] = (byte) (width & 0xff); - out[1] = (byte) ((width >> 8) & 0xff); - out[2] = (byte) (height & 0xff); - out[3] = (byte) ((height >> 8) & 0xff); - int[] tone = toneCurve(gamma); - for (int i = 0; i < pixels.length; i++) { - int p = pixels[i]; - int a = (p >>> 24) & 0xff; - int r = (p >> 16) & 0xff; - int g = (p >> 8) & 0xff; - int b = p & 0xff; - out[4 + i] = (byte) tone[((r * 299 + g * 587 + b * 114) / 1000) * a / 255]; - } - if (dither) { - ditherToDisplayLevels(out, 4, width, height); - } - return out; - } catch (Exception | OutOfMemoryError e) { - Log.w(TAG, "bitmap conversion failed", e); - return new byte[0]; - } - } - - /** 256-entry map of source gray to displayed gray for out = in^gamma. */ - private static int[] toneCurve(float gamma) { - int[] curve = new int[256]; - for (int v = 0; v < 256; v++) { - curve[v] = gamma == 1f - ? v - : Math.round(255f * (float) Math.pow(v / 255f, gamma)); - } - return curve; - } - - /** - * Floyd-Steinberg error diffusion onto the display's 16 gray levels, in - * place over an 8bpp plane. Serpentine scanning keeps the diffusion from - * building up a directional texture across wide flat areas. - * - * Level n is written back as n * 16 rather than the n * 17 it stands for - * so that BmpUtil's (v + 8) >> 4 reproduces n exactly; level 0 is written - * as 1, not 0, because 0 is the shell's color-key for transparent. - */ - private static void ditherToDisplayLevels(byte[] plane, int offset, int width, int height) { - float[] curr = new float[width]; - float[] next = new float[width]; - for (int x = 0; x < width; x++) { - curr[x] = plane[offset + x] & 0xff; - } - for (int y = 0; y < height; y++) { - int rowStart = offset + y * width; - int nextStart = rowStart + width; - boolean hasNext = y + 1 < height; - for (int x = 0; x < width; x++) { - next[x] = hasNext ? (plane[nextStart + x] & 0xff) : 0f; - } - boolean leftToRight = (y & 1) == 0; - int start = leftToRight ? 0 : width - 1; - int step = leftToRight ? 1 : -1; - for (int i = 0; i < width; i++) { - int x = start + i * step; - float wanted = curr[x]; - int level = Math.round(Math.min(255f, Math.max(0f, wanted)) / 17f); - float error = wanted - level * 17f; - plane[rowStart + x] = (byte) (level == 0 ? 1 : level * 16); - int ahead = x + step; - if (ahead >= 0 && ahead < width) { - curr[ahead] += error * (7f / 16f); - } - if (hasNext) { - if (ahead >= 0 && ahead < width) { - next[ahead] += error * (1f / 16f); - } - next[x] += error * (5f / 16f); - int behind = x - step; - if (behind >= 0 && behind < width) { - next[behind] += error * (3f / 16f); - } - } - } - float[] swap = curr; - curr = next; - next = swap; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.kt new file mode 100644 index 00000000..b73baee1 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/ImageFileLoader.kt @@ -0,0 +1,165 @@ +package com.faceclaw.app + +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.util.Log + +/** + * Decodes image files (and bitmaps generally) to the grayscale packet format + * used across the TS bridge: [widthLo, widthHi, heightLo, heightHi, + * pixels...] with one byte per pixel, row-major, or an empty array on + * failure. FaceclawMediaController's album art shares bitmapToGrayPacket; + * the TS side (app/native/image-files.ts) owns the shared photo-tone preset + * passed as (gamma, dither). The pixel math (tone curve, alpha darkening, + * dither) lives in the shared GrayPacket so iOS produces identical bytes. + */ +class ImageFileLoader private constructor() { + companion object { + private const val TAG = "ImageFileLoader" + + /** + * Decode an image file and downscale it to fit within maxWidth x + * maxHeight, preserving aspect ratio and never upscaling. Flat + * conversion (no tone curve, per-pixel rounding): right for UI raster + * such as icons. + */ + @JvmStatic + fun loadGray(path: String?, maxWidth: Int, maxHeight: Int): ByteArray { + return loadGray(path, maxWidth, maxHeight, 1f, false) + } + + /** + * As above, with the photographic tone handling described on + * bitmapToGrayPacket(Bitmap, int, int, float, boolean): use for + * continuous-tone images (photos) viewed on the glasses. + */ + @JvmStatic + fun loadGray( + path: String?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean): ByteArray { + if (path == null || maxWidth <= 0 || maxHeight <= 0) { + return ByteArray(0) + } + try { + val bounds = BitmapFactory.Options() + bounds.inJustDecodeBounds = true + BitmapFactory.decodeFile(path, bounds) + if (bounds.outWidth <= 0 || bounds.outHeight <= 0) { + return ByteArray(0) + } + // Power-of-two subsampling during decode keeps large photos from + // allocating full-size ARGB buffers; exact fitting happens in + // bitmapToGrayPacket. + val opts = BitmapFactory.Options() + opts.inSampleSize = 1 + while (bounds.outWidth / (opts.inSampleSize * 2) >= maxWidth + && bounds.outHeight / (opts.inSampleSize * 2) >= maxHeight) { + opts.inSampleSize *= 2 + } + val bitmap = BitmapFactory.decodeFile(path, opts) + if (bitmap == null) { + return ByteArray(0) + } + return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither) + } catch (e: Exception) { + Log.w(TAG, "image decode failed: $path", e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "image decode failed: $path", e) + return ByteArray(0) + } + } + + /** + * Decode an in-memory image file (PNG/JPEG/WebP bytes, e.g. a fetched + * map tile) and downscale to fit within maxWidth x maxHeight. Takes a + * ByteBuffer because NativeScript marshals a JS ArrayBuffer to one. + */ + @JvmStatic + fun loadGrayFromBytes(buffer: java.nio.ByteBuffer?, maxWidth: Int, maxHeight: Int): ByteArray { + return loadGrayFromBytes(buffer, maxWidth, maxHeight, 1f, false) + } + + /** As above, with photographic tone handling (see bitmapToGrayPacket). */ + @JvmStatic + fun loadGrayFromBytes( + buffer: java.nio.ByteBuffer?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean): ByteArray { + if (buffer == null || maxWidth <= 0 || maxHeight <= 0) { + return ByteArray(0) + } + try { + val source = buffer.duplicate() + source.rewind() + val data = ByteArray(source.remaining()) + source.get(data) + if (data.isEmpty()) { + return ByteArray(0) + } + val bitmap = BitmapFactory.decodeByteArray(data, 0, data.size) + if (bitmap == null) { + return ByteArray(0) + } + return bitmapToGrayPacket(bitmap, maxWidth, maxHeight, gamma, dither) + } catch (e: Exception) { + Log.w(TAG, "byte-array image decode failed", e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "byte-array image decode failed", e) + return ByteArray(0) + } + } + + /** + * Scale a bitmap to fit within maxWidth x maxHeight (preserving aspect, + * never upscaling) and convert it to the grayscale packet format. + * Transparent pixels darken toward black (the on-glasses background). + */ + @JvmStatic + fun bitmapToGrayPacket(source: Bitmap?, maxWidth: Int, maxHeight: Int): ByteArray { + return bitmapToGrayPacket(source, maxWidth, maxHeight, 1f, false) + } + + /** + * As above, with photographic tone handling for continuous-tone images + * (album art, photos) rather than UI raster: + * + * gamma > 1 darkens midtones (out = 255 * (in/255)^gamma). Source pixels + * are sRGB-encoded but the G2 drives its 16 levels roughly linearly, so + * mid-gray otherwise displays far brighter than intended; 2.2 undoes the + * sRGB encoding outright. + * + * dither error-diffuses against the 16 levels the frame is eventually + * packed to (BmpUtil.GRAY_TO_NIBBLE) instead of leaving each pixel to + * round on its own, which turns smooth gradients into visible bands. + * Dithered output is already quantized: every byte is a level times 16, + * which BmpUtil re-quantizes back to exactly that level. + */ + @JvmStatic + fun bitmapToGrayPacket( + source: Bitmap?, maxWidth: Int, maxHeight: Int, gamma: Float, dither: Boolean): ByteArray { + if (source == null || source.width <= 0 || source.height <= 0 + || maxWidth <= 0 || maxHeight <= 0) { + return ByteArray(0) + } + try { + val scale = Math.min(1f, Math.min( + maxWidth.toFloat() / source.width, + maxHeight.toFloat() / source.height)) + val width = Math.max(1, Math.round(source.width * scale)) + val height = Math.max(1, Math.round(source.height * scale)) + var scaled = Bitmap.createScaledBitmap(source, width, height, true) + if (scaled.config == Bitmap.Config.HARDWARE) { + scaled = scaled.copy(Bitmap.Config.ARGB_8888, false) + } + val pixels = IntArray(width * height) + scaled.getPixels(pixels, 0, width, 0, 0, width, height) + return GrayPacket.fromArgb(pixels, width, height, gamma, dither) + } catch (e: Exception) { + Log.w(TAG, "bitmap conversion failed", e) + return ByteArray(0) + } catch (e: OutOfMemoryError) { + Log.w(TAG, "bitmap conversion failed", e) + return ByteArray(0) + } + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/LvglFontFile.java b/App_Resources/Android/src/main/java/com/faceclaw/app/LvglFontFile.java deleted file mode 100644 index d728cc6b..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/LvglFontFile.java +++ /dev/null @@ -1,229 +0,0 @@ -package com.faceclaw.app; - -import android.util.Log; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; - -/** - * Reader for the serialized LVGL font image in the G2's CJK flash partition. - * Faceclaw bundles that font because it is Source Han Sans SC Light under the - * SIL OFL. Proprietary fonts embedded in the main firmware never pass through - * this class and are instead extracted on the user's phone during CFW install. - */ -public final class LvglFontFile { - private static final String TAG = "LvglFontFile"; - private static final int MAPPED_BASE = 0x80100000; - private static final int FONT_CACHE_SIZE = 2; - - private static final Map fontCache = - new LinkedHashMap(FONT_CACHE_SIZE, 0.75f, true) { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > FONT_CACHE_SIZE; - } - }; - - private LvglFontFile() {} - - /** - * Returns [lineHeight, baseline] or an empty array when the font is invalid. - * The baseline is measured down from the top of the line. - */ - public static byte[] getMetrics(String path) { - FontData font = load(path); - if (font == null) return new byte[0]; - return new byte[] { (byte) font.lineHeight, (byte) font.baseline }; - } - - /** - * Returns one packed glyph: - * u16 boxW, u16 boxH, i16 ofsX, i16 ofsY, then aligned 4bpp bitmap bytes. - * Returns an empty array when the code point is not mapped. - */ - public static byte[] getGlyph(String path, int codePoint) { - FontData font = load(path); - if (font == null) return new byte[0]; - Integer glyphId = font.codePointToGlyph.get(codePoint); - if (glyphId == null || glyphId <= 0) return new byte[0]; - try { - int descriptor = font.glyphDescriptors + glyphId * 16; - int bitmapIndex = font.u32(descriptor); - int boxWidth = font.u16(descriptor + 8); - int boxHeight = font.u16(descriptor + 10); - int offsetX = font.i16(descriptor + 12); - int offsetY = font.i16(descriptor + 14); - int stride = boxWidth / 2 + 1; - int bitmapLength = boxWidth > 0 && boxHeight > 0 ? stride * boxHeight : 0; - font.check(font.glyphBitmaps + bitmapIndex, bitmapLength); - - byte[] result = new byte[8 + bitmapLength]; - putU16(result, 0, boxWidth); - putU16(result, 2, boxHeight); - putU16(result, 4, offsetX); - putU16(result, 6, offsetY); - System.arraycopy(font.bytes, font.glyphBitmaps + bitmapIndex, result, 8, bitmapLength); - return result; - } catch (RuntimeException e) { - Log.w(TAG, "invalid glyph U+" + Integer.toHexString(codePoint) + " in " + path, e); - return new byte[0]; - } - } - - private static synchronized FontData load(String path) { - if (path == null || path.isEmpty()) return null; - FontData cached = fontCache.get(path); - if (cached != null) return cached; - try { - FontData font = FontData.read(path); - fontCache.put(path, font); - return font; - } catch (IOException | RuntimeException e) { - Log.w(TAG, "could not load LVGL font " + path, e); - return null; - } - } - - private static void putU16(byte[] destination, int offset, int value) { - destination[offset] = (byte) (value & 0xff); - destination[offset + 1] = (byte) ((value >>> 8) & 0xff); - } - - private static final class FontData { - final byte[] bytes; - final ByteBuffer view; - final int lineHeight; - final int baseline; - final int glyphBitmaps; - final int glyphDescriptors; - final Map codePointToGlyph; - - private FontData(byte[] bytes) { - this.bytes = bytes; - this.view = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); - if (bytes.length < 0x40 || bytes[0] != 'Z' || bytes[1] != 'Z' || bytes[2] != 'Z' || bytes[3] != 'Z') { - throw new IllegalArgumentException("missing ZZZZ font header"); - } - - lineHeight = u16(0x38); - int baseLine = u16(0x3a); - baseline = lineHeight - baseLine; - - int descriptor = pointerOffset(u32(0x34)); - glyphBitmaps = pointerOffset(u32(descriptor)); - glyphDescriptors = pointerOffset(u32(descriptor + 4)); - int cmaps = pointerOffset(u32(descriptor + 8)); - int packed = u16(descriptor + 18); - int cmapCount = packed & 0x1ff; - int bitsPerPixel = (packed >>> 9) & 0xf; - int bitmapFormat = (packed >>> 14) & 0x3; - if (lineHeight <= 0 || baseline < 0 || cmapCount <= 0 || - bitsPerPixel != 4 || bitmapFormat != 3) { - throw new IllegalArgumentException("unsupported LVGL font metadata"); - } - - codePointToGlyph = new HashMap<>(); - for (int index = 0; index < cmapCount; index++) { - readCmap(cmaps + index * 20); - } - } - - static FontData read(String path) throws IOException { - File file = new File(path); - if (!file.isFile() || !file.canRead() || file.length() > Integer.MAX_VALUE) { - throw new IOException("font is not a readable file"); - } - byte[] bytes = new byte[(int) file.length()]; - try (RandomAccessFile input = new RandomAccessFile(file, "r")) { - input.readFully(bytes); - } - return new FontData(bytes); - } - - void readCmap(int address) { - int rangeStart = u32(address); - int rangeLength = u16(address + 4); - int glyphIdStart = u16(address + 6); - int unicodeListPointer = u32(address + 8); - int glyphIdOffsetsPointer = u32(address + 12); - int listLength = u16(address + 16); - int type = u8(address + 18); - - if (type == 2) { - for (int relative = 0; relative < rangeLength; relative++) { - codePointToGlyph.put(rangeStart + relative, glyphIdStart + relative); - } - return; - } - - if (type == 0) { - int offsets = pointerOffset(glyphIdOffsetsPointer); - for (int relative = 0; relative < rangeLength; relative++) { - int offset = u8(offsets + relative); - if (relative != 0 && offset == 0) continue; - codePointToGlyph.put(rangeStart + relative, glyphIdStart + offset); - } - return; - } - - if (type == 3 || type == 1) { - int unicodeList = pointerOffset(unicodeListPointer); - int offsets = glyphIdOffsetsPointer == 0 ? -1 : pointerOffset(glyphIdOffsetsPointer); - for (int index = 0; index < listLength; index++) { - int relative = u16(unicodeList + index * 2); - int offset = index; - if (offsets >= 0) { - offset = type == 1 ? u16(offsets + index * 2) : u8(offsets + index); - if (index != 0 && offset == 0) continue; - } - codePointToGlyph.put(rangeStart + relative, glyphIdStart + offset); - } - return; - } - - throw new IllegalArgumentException("unknown cmap type " + type); - } - - int u8(int offset) { - check(offset, 1); - return bytes[offset] & 0xff; - } - - int u16(int offset) { - check(offset, 2); - return view.getShort(offset) & 0xffff; - } - - int i16(int offset) { - check(offset, 2); - return view.getShort(offset); - } - - int u32(int offset) { - check(offset, 4); - return view.getInt(offset); - } - - int pointerOffset(int pointer) { - long unsignedPointer = pointer & 0xffffffffL; - long offset = unsignedPointer - (MAPPED_BASE & 0xffffffffL); - if (offset < 0 || offset > Integer.MAX_VALUE) { - throw new IllegalArgumentException("font pointer is outside the mapped partition"); - } - check((int) offset, 1); - return (int) offset; - } - - void check(int offset, int length) { - if (offset < 0 || length < 0 || (long) offset + length > bytes.length) { - throw new IllegalArgumentException("font range is out of bounds"); - } - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.java deleted file mode 100644 index 4b599d67..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.faceclaw.app; - -import android.graphics.Bitmap; - -import java.nio.ByteBuffer; - -/** - * Builds the phone-UI preview bitmap from an 8bpp grayscale frame. The - * grayscale buffer arrives as a ByteBuffer (NativeScript marshals a JS - * ArrayBuffer to one without copying element-by-element); doing the - * gray-to-ARGB expansion here keeps the 165KB-per-frame loop out of the - * JS/Java bridge, where it used to cost ~150ms per preview. - */ -public final class PreviewBitmapUtil { - private PreviewBitmapUtil() {} - - public static Bitmap fromGray(ByteBuffer gray, int width, int height, double brightenGamma) { - return fromGray(gray, width, height, brightenGamma, false); - } - - /** As above; `green` renders green-on-black (matching the physical glasses) instead of grayscale. */ - public static Bitmap fromGray(ByteBuffer gray, int width, int height, double brightenGamma, boolean green) { - if (gray == null || width <= 0 || height <= 0 || gray.remaining() < width * height) { - throw new IllegalArgumentException("invalid gray preview buffer"); - } - int[] lut = new int[256]; - for (int g = 0; g < 256; g++) { - int v = (int) Math.max(0, Math.min(255, Math.round(255 * Math.pow(g / 255.0, brightenGamma)))); - lut[g] = green ? (0xff000000 | (v << 8)) : (0xff000000 | (v << 16) | (v << 8) | v); - } - byte[] bytes = new byte[width * height]; - gray.get(bytes); - int[] colors = new int[width * height]; - for (int i = 0; i < colors.length; i++) { - colors[i] = lut[bytes[i] & 0xff]; - } - return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt new file mode 100644 index 00000000..54f31c42 --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt @@ -0,0 +1,34 @@ +package com.faceclaw.app + +import android.graphics.Bitmap + +import java.nio.ByteBuffer + +/** + * Builds the phone-UI preview bitmap from an 8bpp grayscale frame. The + * grayscale buffer arrives as a ByteBuffer (NativeScript marshals a JS + * ArrayBuffer to one without copying element-by-element); doing the + * gray-to-ARGB expansion here keeps the 165KB-per-frame loop out of the + * JS/Java bridge, where it used to cost ~150ms per preview. The palette + * itself is the shared PreviewPalette. + */ +class PreviewBitmapUtil private constructor() { + companion object { + @JvmStatic + fun fromGray(gray: ByteBuffer?, width: Int, height: Int, brightenGamma: Double): Bitmap { + return fromGray(gray, width, height, brightenGamma, false) + } + + /** As above; `green` renders green-on-black (matching the physical glasses) instead of grayscale. */ + @JvmStatic + fun fromGray(gray: ByteBuffer?, width: Int, height: Int, brightenGamma: Double, green: Boolean): Bitmap { + if (gray == null || width <= 0 || height <= 0 || gray.remaining() < width * height) { + throw IllegalArgumentException("invalid gray preview buffer") + } + val bytes = ByteArray(width * height) + gray.get(bytes) + val colors = PreviewPalette.expand(bytes, 0, width * height, brightenGamma, green) + return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.java deleted file mode 100644 index 9ffdcd1b..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.faceclaw.app; - -import android.content.Context; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; -import java.util.zip.CRC32; -import java.util.zip.Deflater; - -/** - * Saves screenshots of the composited screen as 4-bit grayscale PNGs (the - * display's native depth). Retrieve with - * adb pull /sdcard/Android/data/com.faceclaw.app/files/screenshots/ - */ -public final class ScreenshotUtil { - private ScreenshotUtil() {} - - /** Returns the absolute path of the written file. */ - public static String savePngScreenshot(Context context, byte[] gray, int width, int height) throws IOException { - if (gray == null || width <= 0 || height <= 0 || gray.length < width * height) { - throw new IllegalArgumentException("invalid screenshot buffer"); - } - byte[] png = encode4BitGrayPng(gray, width, height); - File file = new File(ensureScreenshotsDir(context), "screen-" + timestamp() + ".png"); - try (FileOutputStream out = new FileOutputStream(file)) { - out.write(png); - } - return file.getAbsolutePath(); - } - - static File ensureScreenshotsDir(Context context) throws IOException { - File dir = new File(context.getExternalFilesDir(null), "screenshots"); - if (!dir.exists() && !dir.mkdirs()) { - throw new IOException("failed to create " + dir); - } - return dir; - } - - static String timestamp() { - return new SimpleDateFormat("yyyyMMdd-HHmmss-SSS", Locale.US).format(new Date()); - } - - /** - * Encode an 8bpp grayscale buffer as a bit-depth-4 grayscale PNG, keeping - * the top nibble of each pixel (the same quantization the wire format uses). - */ - static byte[] encode4BitGrayPng(byte[] gray, int width, int height) { - // Raw scanlines: one filter byte (0 = None) then two pixels per byte, - // leftmost pixel in the high nibble. - int rowBytes = (width + 1) / 2; - byte[] raw = new byte[height * (1 + rowBytes)]; - int p = 0; - for (int y = 0; y < height; y++) { - raw[p++] = 0; - int row = y * width; - for (int x = 0; x < width; x += 2) { - int hi = (gray[row + x] & 0xff) >>> 4; - int lo = (x + 1 < width) ? (gray[row + x + 1] & 0xff) >>> 4 : 0; - raw[p++] = (byte) ((hi << 4) | lo); - } - } - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - out.write(0x89); - out.write('P'); - out.write('N'); - out.write('G'); - out.write(0x0d); - out.write(0x0a); - out.write(0x1a); - out.write(0x0a); - - ByteArrayOutputStream ihdr = new ByteArrayOutputStream(); - writeIntBE(ihdr, width); - writeIntBE(ihdr, height); - ihdr.write(4); // bit depth - ihdr.write(0); // color type: grayscale - ihdr.write(0); // compression - ihdr.write(0); // filter - ihdr.write(0); // interlace - writeChunk(out, "IHDR", ihdr.toByteArray()); - writeChunk(out, "IDAT", zlibDeflate(raw)); - writeChunk(out, "IEND", new byte[0]); - return out.toByteArray(); - } - - static byte[] zlibDeflate(byte[] data) { - Deflater deflater = new Deflater(); - deflater.setInput(data); - deflater.finish(); - ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(64, data.length / 8)); - byte[] buffer = new byte[8192]; - while (!deflater.finished()) { - out.write(buffer, 0, deflater.deflate(buffer)); - } - deflater.end(); - return out.toByteArray(); - } - - private static void writeChunk(ByteArrayOutputStream out, String type, byte[] data) { - writeIntBE(out, data.length); - CRC32 crc = new CRC32(); - for (int i = 0; i < 4; i++) { - int b = type.charAt(i); - out.write(b); - crc.update(b); - } - out.write(data, 0, data.length); - crc.update(data); - writeIntBE(out, (int) crc.getValue()); - } - - private static void writeIntBE(ByteArrayOutputStream out, int value) { - out.write((value >>> 24) & 0xff); - out.write((value >>> 16) & 0xff); - out.write((value >>> 8) & 0xff); - out.write(value & 0xff); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.kt new file mode 100644 index 00000000..c0d33c9b --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/ScreenshotUtil.kt @@ -0,0 +1,49 @@ +package com.faceclaw.app + +import android.content.Context + +import java.io.File +import java.io.FileOutputStream +import java.io.IOException +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale + +/** + * Saves screenshots of the composited screen as 4-bit grayscale PNGs (the + * display's native depth). Retrieve with + * adb pull /sdcard/Android/data/com.faceclaw.app/files/screenshots/ + */ +class ScreenshotUtil private constructor() { + companion object { + /** Returns the absolute path of the written file. */ + @JvmStatic + @Throws(IOException::class) + fun savePngScreenshot(context: Context, gray: ByteArray?, width: Int, height: Int): String { + if (gray == null || width <= 0 || height <= 0 || gray.size < width * height) { + throw IllegalArgumentException("invalid screenshot buffer") + } + val png = SharedScreenshots.encode4BitGrayPng(gray, width, height) + val file = File(ensureScreenshotsDir(context), "screen-" + timestamp() + ".png") + FileOutputStream(file).use { out -> + out.write(png) + } + return file.absolutePath + } + + @JvmStatic + @Throws(IOException::class) + fun ensureScreenshotsDir(context: Context): File { + val dir = File(context.getExternalFilesDir(null), "screenshots") + if (!dir.exists() && !dir.mkdirs()) { + throw IOException("failed to create $dir") + } + return dir + } + + @JvmStatic + fun timestamp(): String { + return SimpleDateFormat("yyyyMMdd-HHmmss-SSS", Locale.US).format(Date()) + } + } +} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java b/App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java deleted file mode 100644 index 1a86e8cb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/SurfaceCompositor.java +++ /dev/null @@ -1,509 +0,0 @@ -package com.faceclaw.app; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Retained-surface compositor: the phone-side model of what is on the glasses - * screen. - * - * Each surface is an 8bpp grayscale buffer retained at its last-submitted - * contents, positioned on the screen with a z-order and a transparency mode. - * Sources on the TS side (today the dashboard; later the shell and per-app - * worker threads) submit updates to their surface, and the compositor - * recombines the retained surfaces into a full-screen frame that feeds the - * existing dedupe/compress/transmit pipeline. Because every surface is - * retained, a full screen frame can be regenerated at any time (frame drops, - * reconnects, previews, screenshots) without asking sources to repaint. - * - * Surface format contract (mirrored by the TS side): - * - Pixels are 8bpp grayscale, row-major, one byte per pixel. The wire - * pipeline quantizes to 4bpp downstream, so the low nibble is not visible. - * - TRANSPARENCY_COLOR_KEY surfaces treat pixel value 0 as fully transparent - * and value 1 as black. Quantization collapses 1 into the same 4bpp level - * as 0, so reserving 0 costs no visible shade; painters of color-key - * surfaces must clamp intentional black to 1. - * - An update may cover any rect of its surface; the rest is retained. The - * update buffer holds exactly rectWidth*rectHeight bytes, row-major. - * - Surfaces composite in ascending z-order onto a black (0) background. - * - Surface geometry changes take effect when the next frame composites; - * they do not trigger a recomposite by themselves. - * - * Thread safety: all methods are safe to call from any thread. Apply and - * composite happen atomically under an internal lock, and each composite - * carries a monotonic sequence number so callers can detect when a composite - * was superseded by a concurrent one before being acted on. - */ -public final class SurfaceCompositor { - public static final int TRANSPARENCY_OPAQUE = 0; - public static final int TRANSPARENCY_COLOR_KEY = 1; - - /** - * One deferred draw (a text glyph or an icon image) within a frame, in - * screen coordinates. The draw's pixels are already baked into the - * composited gray buffer (the TS side bakes before submitting); this - * record preserves the draw's identity so the texture-cache planner can - * replay it as an on-glasses cached draw instead of image bytes. - * - * Glyphs: x/y are the pen position and line top; the raster (and its - * bearing/cell placement) comes from GlyphAtlas under (fontId, encoding). - * Images: x/y are the blit's top-left; the raster comes from ImageAtlas - * under imageId. - */ - public static final class ScreenDraw { - public static final int KIND_GLYPH = 0; - public static final int KIND_IMAGE = 1; - /** A firmware-builtin-font text run (CFW mode 15). */ - public static final int KIND_FWTEXT = 2; - - public final int kind; - /** Glyph draws only. */ - public final int fontId; - /** Glyph draws only. */ - public final int encoding; - /** Image draws only. */ - public final int imageId; - public final int x; - public final int y; - /** Glyph and fw-text draws: 8-bit brightness. */ - public final int value; - /** Fw-text runs only: member codepoints, in text order. */ - public final int[] fwCps; - /** Fw-text runs only: member pen offsets relative to x. */ - public final int[] fwDx; - /** Fw-text runs only: whether each member has visible pixels. */ - public final boolean[] fwInk; - - ScreenDraw(int kind, int fontId, int encoding, int imageId, int x, int y, int value, - int[] fwCps, int[] fwDx, boolean[] fwInk) { - this.kind = kind; - this.fontId = fontId; - this.encoding = encoding; - this.imageId = imageId; - this.x = x; - this.y = y; - this.value = value; - this.fwCps = fwCps; - this.fwDx = fwDx; - this.fwInk = fwInk; - } - - static ScreenDraw glyph(int fontId, int encoding, int penX, int lineY, int value) { - return new ScreenDraw(KIND_GLYPH, fontId, encoding, 0, penX, lineY, value, null, null, null); - } - - static ScreenDraw image(int imageId, int x, int y) { - return new ScreenDraw(KIND_IMAGE, 0, 0, imageId, x, y, 0, null, null, null); - } - - static ScreenDraw fwText(int x, int y, int value, int[] cps, int[] dx, boolean[] ink) { - return new ScreenDraw(KIND_FWTEXT, 0, 0, 0, x, y, value, cps, dx, ink); - } - } - - private static final ScreenDraw[] NO_DRAWS = new ScreenDraw[0]; - - /** One composited full-screen frame plus the metadata the pipeline needs. */ - public static final class Composite { - /** Full-screen 8bpp grayscale pixels, screenWidth*screenHeight bytes. */ - public final byte[] gray; - public final int width; - public final int height; - /** - * Stable identifier of screen content, combining every surface's - * geometry and content fingerprint; equal fingerprints mean equal - * composited pixels. - */ - public final String fingerprint; - /** Monotonic: a Composite with a higher seq contains strictly newer state. */ - public final long seq; - /** - * Screen-space deferred draws of every visible surface, in composite - * order (surface z ascending, then each surface's draw order). Their - * pixels are already baked into gray. - */ - public final ScreenDraw[] draws; - - Composite(byte[] gray, int width, int height, String fingerprint, long seq, ScreenDraw[] draws) { - this.gray = gray; - this.width = width; - this.height = height; - this.fingerprint = fingerprint; - this.seq = seq; - this.draws = draws == null ? NO_DRAWS : draws; - } - } - - private static final class Surface { - final String id; - int x; - int y; - int width; - int height; - int zOrder; - int transparency; - boolean visible = true; - byte[] pixels; - String fingerprint = ""; - /** Surface-local deferred draws of the retained content (already baked into pixels). */ - ScreenDraw[] draws = NO_DRAWS; - - Surface(String id) { - this.id = id; - } - } - - private final Object lock = new Object(); - private int screenWidth; - private int screenHeight; - private boolean blanked; - /** See setUnderlayDim: surfaces with zOrder below this are dimmed by underlayDim/256. */ - private int underlayDimBelowZOrder = Integer.MIN_VALUE; - private int underlayDim = 256; - private final Map surfaces = new HashMap<>(); - private long nextCompositeSeq = 1; - - /** Set the output frame size. Must be called before any surface work. */ - public void configureScreen(int width, int height) { - if (width <= 0 || height <= 0) { - throw new IllegalArgumentException("bad screen size " + width + "x" + height); - } - synchronized (lock) { - this.screenWidth = width; - this.screenHeight = height; - } - } - - /** - * Create a surface or update an existing one's geometry. A resize discards - * the surface's retained pixels (reset to 0). - */ - public void configureSurface(String id, int x, int y, int width, int height, int zOrder, int transparency) { - if (id == null || id.isEmpty()) { - throw new IllegalArgumentException("surface id must be non-empty"); - } - if (width <= 0 || height <= 0) { - throw new IllegalArgumentException("bad surface size " + width + "x" + height + " for " + id); - } - if (transparency != TRANSPARENCY_OPAQUE && transparency != TRANSPARENCY_COLOR_KEY) { - throw new IllegalArgumentException("bad transparency mode " + transparency + " for " + id); - } - synchronized (lock) { - requireScreenConfiguredLocked(); - Surface surface = surfaces.get(id); - if (surface == null) { - surface = new Surface(id); - surfaces.put(id, surface); - } - if (surface.pixels == null || surface.width != width || surface.height != height) { - surface.pixels = new byte[width * height]; - surface.fingerprint = ""; - surface.draws = NO_DRAWS; - } - surface.x = x; - surface.y = y; - surface.width = width; - surface.height = height; - surface.zOrder = zOrder; - surface.transparency = transparency; - } - } - - public void removeSurface(String id) { - synchronized (lock) { - surfaces.remove(id); - } - } - - /** - * Dim every surface whose zOrder is below belowZOrder to factor256/256 of - * its brightness (256 = no dimming): how a shell overlay that dims what it - * covers (the TS Layer.dimUnderneath) reaches the window surfaces beneath - * the shell surface, which the shell's own layer stack cannot paint. - * Visible pixels stay at least 1 (the color-key black); glyph and - * firmware-text draws keep their cached-draw form with a dimmed value; - * image draws leave the composite's draw list (their pixels, already - * baked into the surface, dim as raster). Takes effect when the next - * frame composites. - */ - public void setUnderlayDim(int belowZOrder, int factor256) { - synchronized (lock) { - underlayDimBelowZOrder = belowZOrder; - underlayDim = Math.max(0, Math.min(256, factor256)); - } - } - - /** The dim factor (256 = none) that applies to a surface. */ - private int dimForLocked(Surface surface) { - return surface.zOrder < underlayDimBelowZOrder ? underlayDim : 256; - } - - private static int dimValue(int value, int dim) { - return Math.max(1, (value * dim + 128) >> 8); - } - - /** - * Hidden surfaces keep their retained pixels and accept updates, but are - * excluded from the composite and its fingerprint (used for background - * windows). Takes effect when the next frame composites. - */ - public void setSurfaceVisible(String id, boolean visible) { - synchronized (lock) { - Surface surface = surfaces.get(id); - if (surface == null) { - throw new IllegalArgumentException("unknown surface " + id); - } - surface.visible = visible; - } - } - - /** - * While blanked (screen off), composites are all-zero regardless of - * surface content; retained state is untouched, so unblanking restores - * the screen without asking sources to repaint. - */ - public void setBlanked(boolean blanked) { - synchronized (lock) { - this.blanked = blanked; - } - } - - /** - * Apply an update to one surface and composite the whole screen, as one - * atomic step. The update covers the rect (rectX, rectY, rectWidth, - * rectHeight) in surface-local coordinates; pixels must hold exactly - * rectWidth*rectHeight bytes. contentFingerprint identifies the surface's - * full content after this update. - */ - public Composite applyAndComposite( - String surfaceId, - ByteBuffer pixels, - int rectX, - int rectY, - int rectWidth, - int rectHeight, - String contentFingerprint - ) { - return applyAndComposite(surfaceId, pixels, rectX, rectY, rectWidth, rectHeight, - contentFingerprint, null); - } - - /** - * As above, with the frame's deferred draws: a little-endian buffer of - * tagged records - * [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] (glyph) - * [1][imageId u32][x s16][y s16] (image) - * in surface-local coordinates and draw order. The list describes the - * surface's FULL retained content and replaces the previous list, so it is - * only meaningful for full-surface updates (which is what every submitter - * sends); pass null to clear. Draw pixels must already be baked into pixels. - */ - public Composite applyAndComposite( - String surfaceId, - ByteBuffer pixels, - int rectX, - int rectY, - int rectWidth, - int rectHeight, - String contentFingerprint, - ByteBuffer glyphs - ) { - ScreenDraw[] parsed = parseDraws(glyphs); - synchronized (lock) { - requireScreenConfiguredLocked(); - Surface surface = surfaces.get(surfaceId); - if (surface == null) { - throw new IllegalArgumentException("unknown surface " + surfaceId); - } - if (rectX < 0 || rectY < 0 || rectWidth <= 0 || rectHeight <= 0 - || rectX + rectWidth > surface.width || rectY + rectHeight > surface.height) { - throw new IllegalArgumentException("update rect " + rectWidth + "x" + rectHeight - + "+" + rectX + "+" + rectY + " outside surface " + surfaceId - + " (" + surface.width + "x" + surface.height + ")"); - } - int expectedBytes = rectWidth * rectHeight; - if (pixels == null || pixels.remaining() != expectedBytes) { - throw new IllegalArgumentException("update buffer for " + surfaceId + " has " - + (pixels == null ? 0 : pixels.remaining()) + " bytes, expected " + expectedBytes); - } - for (int row = 0; row < rectHeight; row++) { - int dstOffset = (rectY + row) * surface.width + rectX; - pixels.get(surface.pixels, dstOffset, rectWidth); - } - surface.fingerprint = contentFingerprint == null ? "" : contentFingerprint; - surface.draws = parsed; - return compositeLocked(); - } - } - - private static ScreenDraw[] parseDraws(ByteBuffer draws) { - if (draws == null || draws.remaining() < 1) { - return NO_DRAWS; - } - ByteBuffer in = draws.order(java.nio.ByteOrder.LITTLE_ENDIAN); - List out = new ArrayList<>(); - while (in.remaining() >= 1) { - int kind = in.get() & 0xff; - if (kind == ScreenDraw.KIND_GLYPH && in.remaining() >= 11) { - int fontId = in.getShort() & 0xffff; - int encoding = in.getInt(); - int penX = in.getShort(); - int lineY = in.getShort(); - int value = in.get() & 0xff; - out.add(ScreenDraw.glyph(fontId, encoding, penX, lineY, value)); - } else if (kind == ScreenDraw.KIND_IMAGE && in.remaining() >= 8) { - int imageId = in.getInt(); - int x = in.getShort(); - int y = in.getShort(); - out.add(ScreenDraw.image(imageId, x, y)); - } else if (kind == ScreenDraw.KIND_FWTEXT && in.remaining() >= 6) { - int x = in.getShort(); - int y = in.getShort(); - int value = in.get() & 0xff; - int count = in.get() & 0xff; - if (in.remaining() < count * 7) { - break; // malformed tail: keep what parsed cleanly - } - int[] cps = new int[count]; - int[] dx = new int[count]; - boolean[] ink = new boolean[count]; - for (int i = 0; i < count; i++) { - cps[i] = in.getInt(); - dx[i] = in.getShort(); - ink[i] = in.get() != 0; - } - out.add(ScreenDraw.fwText(x, y, value, cps, dx, ink)); - } else { - break; // malformed tail: keep what parsed cleanly - } - } - return out.toArray(new ScreenDraw[0]); - } - - /** Composite the current retained state without applying an update. */ - public Composite composite() { - synchronized (lock) { - requireScreenConfiguredLocked(); - return compositeLocked(); - } - } - - /** - * Current composited pixels for the phone-side preview / screenshot, or - * null before the screen is configured. Does not consume a sequence - * number (it is never stored as the desired frame). - */ - public Composite previewComposite() { - synchronized (lock) { - if (screenWidth <= 0 || screenHeight <= 0) { - return null; - } - byte[] gray = buildGrayLocked(); - return new Composite(gray, screenWidth, screenHeight, "preview", 0, NO_DRAWS); - } - } - - private byte[] buildGrayLocked() { - byte[] gray = new byte[screenWidth * screenHeight]; - if (blanked) { - return gray; - } - List ordered = new ArrayList<>(surfaces.values()); - ordered.sort(Comparator - .comparingInt((Surface s) -> s.zOrder) - .thenComparing(s -> s.id)); - for (Surface surface : ordered) { - if (surface.visible) { - blendLocked(gray, surface); - } - } - return gray; - } - - private Composite compositeLocked() { - byte[] gray = new byte[screenWidth * screenHeight]; - if (blanked) { - return new Composite(gray, screenWidth, screenHeight, - "blanked:" + screenWidth + "x" + screenHeight, nextCompositeSeq++, NO_DRAWS); - } - List ordered = new ArrayList<>(surfaces.values()); - ordered.sort(Comparator - .comparingInt((Surface s) -> s.zOrder) - .thenComparing(s -> s.id)); - StringBuilder fingerprint = new StringBuilder(); - fingerprint.append(screenWidth).append('x').append(screenHeight); - List draws = new ArrayList<>(); - for (Surface surface : ordered) { - if (!surface.visible) continue; - blendLocked(gray, surface); - int dim = dimForLocked(surface); - for (ScreenDraw draw : surface.draws) { - if (dim < 256 && draw.kind == ScreenDraw.KIND_IMAGE) continue; - int value = dim < 256 ? dimValue(draw.value, dim) : draw.value; - draws.add(new ScreenDraw(draw.kind, draw.fontId, draw.encoding, draw.imageId, - draw.x + surface.x, draw.y + surface.y, value, - draw.fwCps, draw.fwDx, draw.fwInk)); - } - fingerprint.append('|').append(surface.id) - .append('@').append(surface.x).append(',').append(surface.y) - .append('+').append(surface.width).append('x').append(surface.height) - .append('#').append(surface.zOrder) - .append(':').append(surface.transparency) - .append(':').append(surface.fingerprint); - if (dim < 256) { - fingerprint.append(":dim").append(dim); - } - } - return new Composite(gray, screenWidth, screenHeight, fingerprint.toString(), nextCompositeSeq++, - draws.toArray(new ScreenDraw[0])); - } - - private void blendLocked(byte[] gray, Surface surface) { - // Clip the surface rect to the screen. - int srcX = Math.max(0, -surface.x); - int srcY = Math.max(0, -surface.y); - int dstX = Math.max(0, surface.x); - int dstY = Math.max(0, surface.y); - int copyWidth = Math.min(surface.width - srcX, screenWidth - dstX); - int copyHeight = Math.min(surface.height - srcY, screenHeight - dstY); - if (copyWidth <= 0 || copyHeight <= 0) { - return; - } - int dim = dimForLocked(surface); - for (int row = 0; row < copyHeight; row++) { - int srcOffset = (srcY + row) * surface.width + srcX; - int dstOffset = (dstY + row) * screenWidth + dstX; - if (dim < 256) { - // Dimmed: opaque or color-keyed, 0 stays 0 (black / transparent) - // and everything visible scales, never below 1. - for (int col = 0; col < copyWidth; col++) { - int value = surface.pixels[srcOffset + col] & 0xff; - if (value != 0) { - gray[dstOffset + col] = (byte) dimValue(value, dim); - } else if (surface.transparency == TRANSPARENCY_OPAQUE) { - gray[dstOffset + col] = 0; - } - } - } else if (surface.transparency == TRANSPARENCY_OPAQUE) { - System.arraycopy(surface.pixels, srcOffset, gray, dstOffset, copyWidth); - } else { - for (int col = 0; col < copyWidth; col++) { - byte value = surface.pixels[srcOffset + col]; - if (value != 0) { - gray[dstOffset + col] = value; - } - } - } - } - } - - private void requireScreenConfiguredLocked() { - if (screenWidth <= 0 || screenHeight <= 0) { - throw new IllegalStateException("configureScreen must be called first"); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java deleted file mode 100644 index b9336cee..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java +++ /dev/null @@ -1,555 +0,0 @@ -package com.faceclaw.app; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import android.util.Log; - -public final class BleImageOptimizer { - private BleImageOptimizer() {} - private static final String TAG = "BleImageOptimizer"; - - /** - * Split an image into fragments (packets), taking advantage of the fact that each packet can declare - * a logical size longer than its byte payload to get free trailing zero-padding. - * - * Each fragment's logical size stays within maxFragmentSize, while trailing zeros inside that span can - * be omitted from the payload. - */ - public static List planImageFragments(byte[] bmp, int maxFragmentSize) { - return planImageFragments(bmp, maxFragmentSize, false); - } - - public static List planImageFragments( - byte[] bmp, - int maxFragmentSize, - boolean reserveFinalByte - ) { - if (bmp == null || bmp.length == 0) { - return Collections.singletonList(new BleProtocol.ImageFragment(0, new byte[0], 0)); - } - if (maxFragmentSize <= 0) { - throw new IllegalArgumentException("maxFragmentSize must be positive"); - } - - final int bulkLength = reserveFinalByte ? bmp.length - 1 : bmp.length; - final int fragmentCount = ((bulkLength + maxFragmentSize - 1) / maxFragmentSize) - + (bulkLength < bmp.length ? 1 : 0); - final List fragments = new ArrayList<>(fragmentCount); - for (int index = 0; index * maxFragmentSize < bulkLength; index++) { - int start = index * maxFragmentSize; - int end = Math.min(start + maxFragmentSize, bulkLength); - fragments.add(buildFragment(index, bmp, start, end)); - } - if (bulkLength < bmp.length) { - fragments.add(new BleProtocol.ImageFragment( - fragments.size(), - Arrays.copyOfRange(bmp, bulkLength, bmp.length), - bmp.length - bulkLength - )); - } - Log.i(TAG, "planImageFragments: fragments=" + fragments.size() + ", bmp.length=" + bmp.length + ", maxFragmentSize=" + maxFragmentSize); - return fragments; - } - - private static BleProtocol.ImageFragment buildFragment(int index, byte[] bmp, int start, int end) { - int dataEnd = end; - while (dataEnd > start && bmp[dataEnd - 1] == 0) { - dataEnd -= 1; - } - if (dataEnd == start) { - return new BleProtocol.ImageFragment(index, new byte[0], end - start); - } - return new BleProtocol.ImageFragment( - index, - Arrays.copyOfRange(bmp, start, dataEnd), - end - start - ); - } - - public static final class TileImagePlan { - final int tileIndex; - final BleProtocol.ImageTileOptions tile; - // Headerless packed 4bpp frame (top-down, stride ceil(width/2)), kept for - // the displayed-tile dedup cache. Frames are immutable by convention: - // producers always allocate a fresh buffer, so no defensive copy here. - public final byte[] packed; - public final int width; - public final int height; - final byte[] payload; // bytes actually streamed: mode-6 rle(4bpp) - final int sessionId; - List fragments = Collections.emptyList(); - - TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] packed, int width, int height, int sessionId) { - this.tileIndex = tileIndex; - this.tile = tile; - this.packed = packed == null ? new byte[0] : packed; - this.width = width; - this.height = height; - this.payload = maybeCompress(this.packed, width, height); - this.sessionId = sessionId; - } - - /** - * Plan with an explicit wire payload (e.g. a mode-3 incremental update). - * packed must still be the FULL new frame: it feeds the displayed-frame - * dedup cache, which after an applied incremental equals the full frame. - */ - TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] packed, int width, int height, int sessionId, byte[] payloadOverride) { - this.tileIndex = tileIndex; - this.tile = tile; - this.packed = packed == null ? new byte[0] : packed; - this.width = width; - this.height = height; - this.payload = payloadOverride; - this.sessionId = sessionId; - } - } - - /** - * Build a mode-3 bounding-box incremental payload, or null when a full - * update should be sent instead (frames not comparable, or the changed - * region spans the whole screen). Wire format: - * [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][rle(headerless 4bpp region)] - * The region is top-down rows of the NEW frame, stride width/2 bytes. The - * box is aligned so left/width are multiples of 4 pixels and top/height - * multiples of 2 rows, letting each coordinate fit one byte and avoiding - * 4bpp byte-boundary corner cases. Identical frames yield null too; the - * caller's dedup check should normally catch that first. - * - * frameId is a little-endian uint16 the CFW uses to detect reordered, - * skipped or duplicated deltas (its diagnostic overlay); the caller advances - * it by 1 per emitted delta so consecutive deltas are consecutive. - */ - /** - * A mode-3 incremental payload plus diagnostics about how tightly the - * bounding box fit the actual change. {@code changedBytes} is how many - * packed bytes actually differ; {@code boxBytes} is how many the box sends. - * A large box with few changed bytes / multiple clusters means distant - * small edits were merged into one oversized box (which can spill past the - * fragment size into an extra serialized BLE message). - */ - public static final class IncrementalPlan { - public final byte[] payload; - public final int changedBytes; - public final int boxBytes; - public final int clusterCount; - - IncrementalPlan(byte[] payload, int changedBytes, int boxBytes, int clusterCount) { - this.payload = payload; - this.changedBytes = changedBytes; - this.boxBytes = boxBytes; - this.clusterCount = clusterCount; - } - } - - public static IncrementalPlan buildIncrementalImagePayload(byte[] previous, byte[] next, int width, int height, int frameId) { - int stride = (width + 1) >> 1; - if (width <= 0 || height <= 0 || previous == null || next == null - || previous.length != stride * height || next.length != stride * height) { - return null; - } - - // Full diff scan in packed-byte coordinates (1 byte = 2 pixels): the - // changed bounding box plus diagnostics (total changed bytes, and how - // many disjoint horizontal column-clusters the change splits into). - int minByteX = stride; - int maxByteX = -1; - int minY = height; - int maxY = -1; - int changedBytes = 0; - boolean[] columnChanged = new boolean[stride]; - for (int y = 0; y < height; y++) { - int rowOffset = y * stride; - for (int x = 0; x < stride; x++) { - if (previous[rowOffset + x] != next[rowOffset + x]) { - changedBytes++; - columnChanged[x] = true; - if (x < minByteX) minByteX = x; - if (x > maxByteX) maxByteX = x; - if (y < minY) minY = y; - maxY = y; - } - } - } - if (maxY < 0) { - return null; // identical - } - int clusterCount = 0; - boolean inCluster = false; - for (int x = minByteX; x <= maxByteX; x++) { - if (columnChanged[x]) { - if (!inCluster) clusterCount++; - inCluster = true; - } else { - inCluster = false; - } - } - - // Pixel-space box, aligned: left/width to multiples of 4, top/height to - // multiples of 2, clamped to the frame. - int left = (minByteX * 2) & ~3; - int rightExclusive = Math.min(width, (((maxByteX + 1) * 2) + 3) & ~3); - int top = minY & ~1; - int bottomExclusive = Math.min(height, (maxY + 2) & ~1); - int boxWidth = rightExclusive - left; - int boxHeight = bottomExclusive - top; - if (boxWidth >= width && boxHeight >= height) { - return null; // whole screen changed; full update is strictly better - } - - byte[] out = encodeMode3Rect(next, stride, left, top, boxWidth, boxHeight, frameId); - return new IncrementalPlan(out, changedBytes, (boxWidth >> 1) * boxHeight, clusterCount); - } - - /** - * The aligned changed bounding box between two same-size packed frames, or - * null when they are identical: pixel-space {left, top, width, height} with - * left/width multiples of 4 and top/height multiples of 2 (mode-3 header - * quantization). Exposed so the texture-cache planner can reuse the exact - * rect geometry the single-bbox path would send. - */ - public static int[] computeChangedBox(byte[] previous, byte[] next, int width, int height) { - int stride = (width + 1) >> 1; - if (width <= 0 || height <= 0 || previous == null || next == null - || previous.length != stride * height || next.length != stride * height) { - return null; - } - int minByteX = stride; - int maxByteX = -1; - int minY = height; - int maxY = -1; - for (int y = 0; y < height; y++) { - int rowOffset = y * stride; - for (int x = 0; x < stride; x++) { - if (previous[rowOffset + x] != next[rowOffset + x]) { - if (x < minByteX) minByteX = x; - if (x > maxByteX) maxByteX = x; - if (y < minY) minY = y; - maxY = y; - } - } - } - if (maxY < 0) { - return null; - } - int left = (minByteX * 2) & ~3; - int rightExclusive = Math.min(width, (((maxByteX + 1) * 2) + 3) & ~3); - int top = minY & ~1; - int bottomExclusive = Math.min(height, (maxY + 2) & ~1); - return new int[]{left, top, rightExclusive - left, bottomExclusive - top}; - } - - /** - * The multi-rect split of the changed region (see computeSplitRects), or null - * when it is one region / too fragmented / the frames aren't comparable. - * Exposed for the texture-cache planner. - */ - public static List computeChangedRects(byte[] previous, byte[] next, int width, int height, int maxRects) { - int stride = (width + 1) >> 1; - if (width <= 0 || height <= 0 || previous == null || next == null - || previous.length != stride * height || next.length != stride * height) { - return null; - } - List rects = computeSplitRects(previous, next, stride, width, height, maxRects); - return (rects == null || rects.size() < 2) ? null : rects; - } - - /** - * Encode one CFW mode-3 rectangle delta: - * [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][rle(box pixels)] - * left/width must be multiples of 4 (=> left>>1, width>>1 are whole bytes), top/ - * height multiples of 2. The box pixels are top-down rows of `next` (4bpp packed, - * width>>1 bytes/row), run-length encoded before deflate. Shared by the single-bbox - * path, each rect of a mode-8 multi-rect batch, and the texture-cache planner. - */ - static byte[] encodeMode3Rect(byte[] next, int stride, int left, int top, - int boxWidth, int boxHeight, int fid) { - int regionStride = boxWidth >> 1; - byte[] region = new byte[regionStride * boxHeight]; - for (int y = 0; y < boxHeight; y++) { - System.arraycopy(next, (top + y) * stride + (left >> 1), region, y * regionStride, regionStride); - } - byte[] compressed = rleEncode(region); - byte[] out = new byte[7 + compressed.length]; - out[0] = 3; - out[1] = (byte) (left / 4); - out[2] = (byte) (top / 2); - out[3] = (byte) (boxWidth / 4); - out[4] = (byte) (boxHeight / 2); - out[5] = (byte) (fid & 0xff); // fid_lo - out[6] = (byte) ((fid >> 8) & 0xff); // fid_hi - System.arraycopy(compressed, 0, out, 7, compressed.length); - return out; - } - - /** A full repaint in independently decodable commands below the uint16 record limit. */ - static List encodeFullFrameBands(byte[] packed, int width, int height, int firstFid) { - if (width <= 0 || width > 640 || (width & 3) != 0 || height <= 0 || height > 480 - || (height & 1) != 0 || packed.length != width * height / 2) { - throw new IllegalArgumentException("unsupported custom framebuffer dimensions"); - } - List commands = new ArrayList<>(); - int fid = firstFid; - for (int top = 0; top < height; top += 64) { - commands.add(encodeMode3Rect(packed, width / 2, 0, top, width, - Math.min(64, height - top), fid)); - fid = fid >= 0xfffe ? 1 : fid + 1; - } - return commands; - } - - /** - * A CFW mode-8 multi-segment payload (`[8][count]` then per-rect - * `[len16][mode-3 submsg]`) carrying several tight rectangle deltas instead of - * one bounding box, plus diagnostics. Each rect is applied to the firmware - * shadow with the panel push deferred, then presented once. Used when the change - * splits into regions whose bounding box wastes bytes (distant edits, or a sparse - * box); the caller falls back to the single-rect path when this returns null or - * isn't smaller. - */ - public static final class MultiRectPlan { - public final byte[] payload; - public final int rectCount; - public final int coveredBytes; // sum of rect region bytes, pre-compression - public final int nextFid; // fid to resume from (rectCount consumed) - - MultiRectPlan(byte[] payload, int rectCount, int coveredBytes, int nextFid) { - this.payload = payload; - this.rectCount = rectCount; - this.coveredBytes = coveredBytes; - this.nextFid = nextFid; - } - } - - // Break-even tuning for the rect splitter. Splitting adds ~15 fixed bytes per - // rect (seglen + mode-3 header + fid), so only split across gaps big enough to pay for that. - // Gaps are in the changed-mask's native units: whole 4bpp bytes (2px) for - // columns, rows for the vertical bands. Kept above the 4px/2px box alignment so - // aligned rects never overlap. - private static final int MULTI_RECT_V_GAP_ROWS = 4; - private static final int MULTI_RECT_H_GAP_BYTES = 6; - - /** - * Build a mode-8 multi-rect payload for the change between two same-size 4bpp - * frames, assigning consecutive frame ids starting at fidStart (each rect needs a - * distinct fid — the CFW skips duplicate fids). Returns null when the change is a - * single region (use the single-rect path), when it fragments past maxRects, or - * when the frames aren't comparable. - */ - public static MultiRectPlan buildMultiRectImagePayload(byte[] previous, byte[] next, int width, int height, int fidStart, int maxRects) { - int stride = (width + 1) >> 1; - if (width <= 0 || height <= 0 || previous == null || next == null - || previous.length != stride * height || next.length != stride * height) { - return null; - } - - List rects = computeSplitRects(previous, next, stride, width, height, maxRects); - if (rects == null || rects.size() < 2) { - return null; // single region (or too fragmented): caller uses the bbox path - } - - List subs = new ArrayList<>(rects.size()); - int coveredBytes = 0; - int fid = fidStart; - int total = 2; // [8][count] - for (int[] r : rects) { - byte[] sub = encodeMode3Rect(next, stride, r[0], r[1], r[2], r[3], fid); - subs.add(sub); - coveredBytes += (r[2] >> 1) * r[3]; - total += 2 + sub.length; // len16 + submsg - fid = (fid >= 0xfffe) ? 1 : fid + 1; - } - - byte[] out = new byte[total]; - out[0] = 8; - out[1] = (byte) rects.size(); - int pos = 2; - for (byte[] sub : subs) { - out[pos] = (byte) (sub.length & 0xff); - out[pos + 1] = (byte) ((sub.length >> 8) & 0xff); - pos += 2; - System.arraycopy(sub, 0, out, pos, sub.length); - pos += sub.length; - } - return new MultiRectPlan(out, rects.size(), coveredBytes, fid); - } - - /** - * Split the changed region between two packed 4bpp frames into a small set of - * aligned rectangles that together cover every changed pixel: maximal vertical - * bands of changed rows (merging gaps < V_GAP), each split into horizontal column - * clusters (merging gaps < H_GAP), with each cluster's rows tightened to where it - * actually changes. Rects are pixel-space {left,top,width,height}, left/width - * aligned to 4px and top/height to 2px. Returns null if there's no change or the - * split exceeds maxRects (caller falls back to a single bounding box). - */ - private static List computeSplitRects(byte[] previous, byte[] next, int stride, int width, int height, int maxRects) { - boolean[] rowChanged = new boolean[height]; - boolean anyChange = false; - for (int y = 0; y < height; y++) { - int off = y * stride; - for (int x = 0; x < stride; x++) { - if (previous[off + x] != next[off + x]) { - rowChanged[y] = true; - anyChange = true; - break; - } - } - } - if (!anyChange) { - return null; - } - - List rects = new ArrayList<>(); - boolean[] col = new boolean[stride]; - int y = 0; - while (y < height) { - if (!rowChanged[y]) { y++; continue; } - // Extend a vertical band across changed rows, hopping unchanged gaps < V_GAP. - int bandTop = y; - int bandBot = y; - int cur = y + 1; - while (cur < height) { - if (rowChanged[cur]) { bandBot = cur; cur++; continue; } - int g = cur; - while (g < height && !rowChanged[g]) g++; - if (g < height && (g - cur) < MULTI_RECT_V_GAP_ROWS) { cur = g; } else break; - } - - // Columns changed anywhere in the band. - Arrays.fill(col, false); - for (int yy = bandTop; yy <= bandBot; yy++) { - if (!rowChanged[yy]) continue; - int off = yy * stride; - for (int x = 0; x < stride; x++) { - if (previous[off + x] != next[off + x]) col[x] = true; - } - } - - // Split the band into horizontal clusters, hopping column gaps < H_GAP. - int x = 0; - while (x < stride) { - if (!col[x]) { x++; continue; } - int cL = x; - int cR = x; - int cx = x + 1; - while (cx < stride) { - if (col[cx]) { cR = cx; cx++; continue; } - int g = cx; - while (g < stride && !col[g]) g++; - if (g < stride && (g - cx) < MULTI_RECT_H_GAP_BYTES) { cx = g; } else break; - } - - // Tighten this cluster's rows to those that change within [cL,cR]. - int tTop = height; - int tBot = -1; - for (int yy = bandTop; yy <= bandBot; yy++) { - if (!rowChanged[yy]) continue; - int off = yy * stride; - for (int xx = cL; xx <= cR; xx++) { - if (previous[off + xx] != next[off + xx]) { - if (yy < tTop) tTop = yy; - tBot = yy; - break; - } - } - } - if (tBot >= 0) { - int left = (cL * 2) & ~3; - int rightExclusive = Math.min(width, (((cR + 1) * 2) + 3) & ~3); - int top = tTop & ~1; - int bottomExclusive = Math.min(height, (tBot + 2) & ~1); - rects.add(new int[]{left, top, rightExclusive - left, bottomExclusive - top}); - if (rects.size() > maxRects) { - return null; // too fragmented: a single bounding box is simpler - } - } - x = cR + 1; - } - y = bandBot + 1; - } - return rects; - } - - /** - * Run-length encode headerless 4bpp pixels for CFW load_image_z mode 6. - * Wire format: [6][rle(4bpp pixels)]. Always use mode 6: the logical - * image can be larger than its EvenHub carrier, so a raw BMP fallback would - * carry dimensions the legacy container loader cannot accept. - */ - static byte[] maybeCompress(byte[] packed, int width, int height) { - if (packed == null || packed.length == 0) { - return packed; - } - byte[] z = rleEncode(packed); - byte[] out = new byte[z.length + 1]; - out[0] = 6; - System.arraycopy(z, 0, out, 1, z.length); - return out; - } - - /** - * Run-length encode packed 4bpp pixels for CFW modes 3 and 6, whose payload is - * RLE tokens; deflate belongs to the transport. Runs are over the pixel NIBBLES of - * {@code pix} in wire order (high nibble = left pixel), including the pad nibble - * that ends each row at odd widths — i.e. {@code pix} read as 2*length nibbles. - * One token is: - *

-     *   [cnt4|color4]                 cnt 1..15
-     *   [0|color4][cnt8]              cnt 1..255
-     *   [0|color4][0][cntLo][cntHi]   cnt 1..65535, little-endian
-     * 
- * with the low nibble always the color; runs longer than 65535 split across - * tokens. Worst case (every nibble its own run) is exactly one byte per nibble, - * which is what the buffer is sized for. - */ - static byte[] rleEncode(byte[] pix) { - int n = pix.length * 2; - byte[] out = new byte[n]; - int o = 0; - int i = 0; - while (i < n) { - int v = nibbleAt(pix, i); - int j = i + 1; - while (j < n && nibbleAt(pix, j) == v) j++; - int run = j - i; - while (run > 0) { - int c = Math.min(run, 0xffff); - if (c <= 15) { - out[o++] = (byte) ((c << 4) | v); - } else if (c <= 255) { - out[o++] = (byte) v; // high nibble 0 = escape to an 8-bit count - out[o++] = (byte) c; - } else { - out[o++] = (byte) v; - out[o++] = 0; // 8-bit count 0 = escape to a 16-bit count - out[o++] = (byte) (c & 0xff); - out[o++] = (byte) (c >> 8); - } - run -= c; - } - i = j; - } - return Arrays.copyOf(out, o); - } - - private static int nibbleAt(byte[] pix, int i) { - int b = pix[i >> 1] & 0xff; - return (i & 1) != 0 ? (b & 0x0f) : (b >> 4); - } - - public static final class ImageUpdateStats { - final int paintMs; - final int tileCount; - final int frameId; - long firstWriteStartedAtMs; - - ImageUpdateStats(int paintMs, int tileCount, int frameId) { - this.paintMs = Math.max(0, paintMs); - this.tileCount = tileCount; - this.frameId = frameId; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleMagicPool.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleMagicPool.java deleted file mode 100644 index bd6afd79..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleMagicPool.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.faceclaw.app; -import java.util.ArrayDeque; -import java.util.HashMap; -import java.util.Map; -import android.os.SystemClock; - -/** - * Protobuf messages contain a 1-byte identifier that is used to identify - * which message is referred to in ACK messages. We keep track of which - * identifiers are in use for inflight messages, with some metadata about - * what the message was. We allocate identifiers with an LRU policy so that - * if we release a message's identifier because it timed out, we can still - * detect late ACKs if it hasn't been reused yet. - */ -public final class BleMagicPool { - static final int MIN_MAGIC = 100; - static final int MAX_MAGIC = 255; - - private final ArrayDeque available = new ArrayDeque<>(); - private final boolean[] allocated = new boolean[256]; - private final Map releaseRecords = new HashMap<>(); - - BleMagicPool() { - for (int magic = MIN_MAGIC; magic <= MAX_MAGIC; magic++) { - available.addLast(magic); - } - } - - synchronized int allocate() { - Integer magic = available.pollFirst(); - if (magic == null) { - throw new IllegalStateException("no BLE magic values available"); - } - allocated[magic] = true; - return magic; - } - - synchronized void release(int sid, int magic, String label, String reason) { - if (magic < MIN_MAGIC || magic > MAX_MAGIC) { - return; - } - releaseRecords.put(key(sid, magic), new ReleaseRecord(label, reason, SystemClock.elapsedRealtime())); - if (!allocated[magic]) { - return; - } - allocated[magic] = false; - available.addLast(magic); - } - - synchronized ReleaseRecord getReleaseRecord(int sid, int magic) { - return releaseRecords.get(key(sid, magic)); - } - - private static String key(int sid, int magic) { - return sid + ":" + magic; - } - - public static final class ReleaseRecord { - final String label; - final String reason; - final long releasedAtMs; - - ReleaseRecord(String label, String reason, long releasedAtMs) { - this.label = label == null ? "" : label; - this.reason = reason == null ? "" : reason; - this.releasedAtMs = releasedAtMs; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java deleted file mode 100644 index 991c6b84..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java +++ /dev/null @@ -1,1259 +0,0 @@ -package com.faceclaw.app; - -import java.io.ByteArrayOutputStream; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.UUID; - -/** - * BleProtocol: Functions for generating and parsing messages when speaking to - * the Even Realities G2. Does _not_ include code for actually sending anything; - * for that, see FaceclawBleCommunicator. - * - * Most message formats are protobufs. For schemas, check g2-kit-unofficial. - */ -public class BleProtocol { - public static final String WRITE_CHAR_UUID = "00002760-08c2-11e1-9073-0e8ac72e5401"; - public static final String NOTIFY_CHAR_UUID = "00002760-08c2-11e1-9073-0e8ac72e5402"; - // Firmware-data service (svc ...e1001): the OTA transfer channel, distinct - // from the EvenHub write/notify chars above (which the flasher reuses only - // for the keepalive heartbeat). - public static final String OTA_DATA_WRITE_UUID = "00002760-08c2-11e1-9073-0e8ac72e0001"; - public static final String OTA_DATA_NOTIFY_UUID = "00002760-08c2-11e1-9073-0e8ac72e0002"; - public static final String RENDER_NOTIFY_UUID = "00002760-08c2-11e1-9073-0e8ac72e6402"; - // R1 ring, vendor service bae80001-4f05-4503-8e65-3af1f7329d1f. The service - // holds two write/notify pairs: bae80010 (write) + bae80011 (notify), and - // bae80012 (write) + bae80013 (notify). The health-data protocol - // (RingProtocol) rides the SECOND pair. - public static final String R1_PHONE_NOTIFY_CHAR_UUID = "bae80011-4f05-4503-8e65-3af1f7329d1f"; - public static final String R1_NOTIFY_CHAR_UUID = "bae80013-4f05-4503-8e65-3af1f7329d1f"; - /** - * Where health-data requests and page ACKs are written. Properties are - * 0x04 (write without response), so writes use ConnectionOptions.WRITE_TYPE - * (WRITE_TYPE_NO_RESPONSE) — which is also what the reference capture shows - * (ATT opcode 0x52 Write Command, never 0x12 Write Request). - * - * Resolved from the phone's own cached GATT database, not guessed: the - * bugreport taken alongside the capture contains a [gatt_cache_c73eb8f9c7c5] - * block (keyed by the ring's MAC C7:3E:B8:F9:C7:C5) listing - * Characteristic: declaration_handle=0x0014, value_handle=0x0015, - * uuid=bae80012-4f05-4503-8e65-3af1f7329d1f, prop=0x04 - * and 0x0015 is exactly the ATT handle every protocol write in the capture - * targets. See knowledge/staging/faceclaw-ring-protocol-implement-return.md. - */ - public static final String R1_WRITE_CHAR_UUID = "bae80012-4f05-4503-8e65-3af1f7329d1f"; - - public static final int PRELUDE_ACK_SID = 0x01; - public static final int PRELUDE_ACK_MAGIC = 156; - /** - * DEVICE_SETTINGS security-auth channel. Firmware 2.2.9 enforces completing - * this exchange shortly after a GATT connection opens (a ~30 s deadline - * closes unauthenticated links) and gates query responses on it; the - * message itself exists back to the 2.2.4 era, so sending it is safe on all - * stock versions. See ../notes/ble-connections-2.2.9.md. - */ - public static final int SID_SECURITY_AUTH = 0x80; - public static final int FLAG_SECURITY_AUTH = 0x00; - private static final int SECURITY_AUTH_CMD = 4; - public static final int SID_APP_LAUNCH = 0x01; - public static final int SID_EVENHUB = 0xe0; - public static final int SID_UI_SETTING = 0x09; - public static final int SID_STATE_CHANGE = 0x0d; - public static final int SID_ONBOARDING = 0x10; - /** G2SettingPackage.commandId for device-initiated pushes (vs. 2 = app read). */ - public static final int SETTINGS_CMD_DEVICE_SEND_TO_APP = 3; - // Private Faceclaw/CFW wake-takeover protocol. Both payloads are unknown - // protobuf fields to stock implementations, so they are safely ignored. - public static final int FACECLAW_WAKE_CONTROL_FIELD = 101; - public static final int FACECLAW_WAKE_EVENT_FIELD = 102; - /** - * CFW mic_control: host writes an ['M','C',ver,op,...] record as settings - * field 103; each temple - * reports a 21-byte ['M','C',ver,...] status as field 104, both appended - * to settings read responses and as standalone commandId=3 pushes. - */ - public static final int FACECLAW_MIC_CONTROL_FIELD = 103; - public static final int FACECLAW_MIC_STATUS_FIELD = 104; - /** - * CFW als_sensor: ambient-light reports on settings-channel field 105, 24-byte ['A','L',ver,reason,...] records from - * the master temple (decoded in app/native/ambient-light.ts). - */ - public static final int FACECLAW_ALS_REPORT_FIELD = 105; - public static final int FACECLAW_WAKE_OP_ACQUIRE = 1; - public static final int FACECLAW_WAKE_OP_RELEASE = 2; - public static final int FACECLAW_WAKE_OP_CLAIM = 3; - public static final int FACECLAW_WAKE_OP_READY = 4; - public static final int FACECLAW_FB_OP_ACQUIRE = 5; - public static final int FACECLAW_FB_OP_RELEASE = 6; - public static final int FACECLAW_WEAR_OP_QUERY = 7; - public static final int CFW_IMAGE_MODE_CLEANUP = 11; - private static final int FACECLAW_WAKE_EVENT = 1; - // Field-102 deferred wake from the IMU head-up (Faceclaw firmware revision - // 3+); same nonce handshake as the double-tap wake event 1. - public static final int FACECLAW_WAKE_EVENT_HEAD_UP = 5; - // Field-102 idle-gesture events (Faceclaw firmware revision 2+): gestures the - // stock display thread drops while no app is on screen, forwarded while the - // wake lease is held. Their last two bytes are the raw touch source and 0. - public static final int FACECLAW_GESTURE_EVENT_TAP = 2; - public static final int FACECLAW_GESTURE_EVENT_LONG_PRESS = 3; - public static final int FACECLAW_GESTURE_EVENT_LONG_PRESS_RELEASE = 4; - // The display thread's raw touch sources, as the firmware reports them. - private static final int FACECLAW_RAW_SOURCE_LEFT_TEMPLE = 0; - private static final int FACECLAW_RAW_SOURCE_RIGHT_TEMPLE = 1; - private static final int FACECLAW_RAW_SOURCE_RING = 4; - private static final int FACECLAW_WAKE_PROTOCOL_VERSION = 1; - // UI_FOREGROUND_EVEN_AI_ID: the stock "Even AI" assistant app. It is a - // FOREGROUND app, whereas EvenHub (and therefore faceclaw) is the - // BACKGROUND app -- which is why an Even AI launch draws over us. - // Payload is g2.even_ai.EvenAIDataPackage (see - // g2-kit-unofficial/ble/gen/even_ai_pb.ts). - public static final int SID_EVEN_AI = 0x07; - public static final int SID_NAVIGATION = 0x08; - public static final int NAV_CMD_COMPASS_CHANGED = 15; - public static final int NAV_CMD_COMPASS_CALIBRATION_STARTED = 16; - public static final int NAV_CMD_COMPASS_CALIBRATION_COMPLETE = 17; - // eEvenAICommandId - public static final int EVEN_AI_CMD_CTRL = 1; - // eEvenAIStatus, carried in EvenAIDataPackage.ctrl.status (field 3, sub 1). - // WAKE_UP is the on-glasses "Hey Even" wakeword firing; ENTER is a manual - // entry (touch/double-tap), so the two are distinguishable. - public static final int EVEN_AI_STATUS_WAKE_UP = 1; - public static final int EVEN_AI_STATUS_ENTER = 2; - public static final int EVEN_AI_STATUS_EXIT = 3; - public static final int FLAG_REQUEST = 0x20; - public static final int FLAG_NOTIFY = 0x01; - public static final int FLAG_NOTIFY_ALT = 0x06; - public static final int CMD_AUDIO_CONTROL = 15; - // EvenHub_Cmd_List.APP_REQUEST_OPEN_IMU_PACKET; the accelerometer stream is - // gated behind this control message (ImuCtrl, field 22 of the main ctx). - public static final int CMD_OPEN_IMU = 19; - - public static final int EVENT_CLICK = 0; - public static final int EVENT_SCROLL_TOP = 1; - public static final int EVENT_SCROLL_BOTTOM = 2; - public static final int EVENT_DOUBLE_CLICK = 3; - public static final int EVENT_FOREGROUND_ENTER = 4; - public static final int EVENT_FOREGROUND_EXIT = 5; - public static final int EVENT_ABNORMAL_EXIT = 6; - public static final int EVENT_SYSTEM_EXIT = 7; - public static final int EVENT_IMU_DATA_REPORT = 8; - public static final int EVENT_RING_LONG_PRESS = 9; - public static final int EVENT_RING_LONG_PRESS_RELEASE = 10; - // CFW extension: G2 2.2.9 tap-then-hold gesture forwarded to the phone. - public static final int EVENT_SHORT_THEN_LONG_PRESS = 11; - // CFW extension (firmware revision 3+): the IMU head-up, forwarded as a - // sys-event while an EvenHub page is on screen (soft sleep). - public static final int EVENT_HEAD_UP = 12; - - public static final int EVENT_SOURCE_GLASSES_R = 1; - public static final int EVENT_SOURCE_RING = 2; - public static final int EVENT_SOURCE_GLASSES_L = 3; - - public static final UUID CCCD_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); - - // App-launch message. Protobuf contents come from a capture (done in g2-kit-unofficial) - // so the actual meaning of most fields isn't known. The standard protobuf - // wire format still lets us identify field numbers, wire types, and lengths. - public static final byte[] PRELUDE_F5872_PAYLOAD = buildPreludeF5872Payload(); - public static final byte[] PRELUDE_F5872 = - framePb(PRELUDE_F5872_PAYLOAD, SID_APP_LAUNCH, FLAG_REQUEST, 0x92).getFirst(); - - private static byte[] buildPreludeF5872Payload() { - byte[] field4 = encodeMessageField(3, - encodeMessageField(2, - encodeMessageField(2, concat(CollectionUtils.listOf( - encodeVarintField(1, 0), - encodeVarintField(2, 0) - ))) - ) - ); - return concat(CollectionUtils.listOf( - encodeVarintField(1, 2), // known app-launch type/Cmd - encodeVarintField(2, PRELUDE_ACK_MAGIC), - encodeMessageField(4, field4) - )); - } - - public static List framePb(byte[] pb, int sid, int flag, int seq) { - int chunkSize = 232; - byte[] crc = crcBytesLe(pb); - int totalWithCrc = pb.length + 2; - int totalFrags = Math.max(1, (int) Math.ceil(totalWithCrc / (double) chunkSize)); - List frames = new ArrayList<>(totalFrags); - byte[] payload = Arrays.copyOf(pb, totalWithCrc); - payload[pb.length] = crc[0]; - payload[pb.length + 1] = crc[1]; - for (int i = 0; i < totalFrags; i++) { - int offset = i * chunkSize; - byte[] chunk = Arrays.copyOfRange(payload, offset, Math.min(offset + chunkSize, payload.length)); - byte[] frame = new byte[8 + chunk.length]; - frame[0] = (byte) 0xaa; - frame[1] = 0x21; - frame[2] = (byte) (seq & 0xff); - frame[3] = (byte) (chunk.length & 0xff); - frame[4] = (byte) (totalFrags & 0xff); - frame[5] = (byte) ((i + 1) & 0xff); - frame[6] = (byte) (sid & 0xff); - frame[7] = (byte) (flag & 0xff); - System.arraycopy(chunk, 0, frame, 8, chunk.length); - frames.add(frame); - } - return frames; - } - - - /** Retain the text container for forwarded inputs, without an image container. */ - public static byte[] buildCreateInputPage(int magic) { - List innerParts = new ArrayList<>(); - innerParts.add(encodeVarintField(1, 1)); - innerParts.add(encodeMessageField(3, encodeTextObject("dashboard", 1, 0, 0, 576, 288, " ", true))); - innerParts.add(encodeVarintField(5, 10000)); - byte[] inner = concat(innerParts); - return wrapEvenHub(0, magic, 3, inner); - } - - public static byte[] buildDashboardTextUpgrade(int magic) { - List inner = new ArrayList<>(); - inner.add(encodeVarintField(1, 1)); - inner.add(encodeStringField(2, "dashboard")); - inner.add(encodeVarintField(3, 0)); - inner.add(encodeVarintField(4, 1)); - inner.add(encodeStringField(5, " ")); - return wrapEvenHub(5, magic, 9, concat(inner)); - } - - public static byte[] buildImageRawData(ImageTileOptions tile, int sessionId, int totalSize, ImageFragment fragment, int magic) { - List inner = new ArrayList<>(); - inner.add(encodeVarintField(1, tile.containerId)); - inner.add(encodeStringField(2, tile.name)); - //inner.add(encodeVarintField(3, sessionId)); - inner.add(encodeVarintField(3, tile.containerId)); - inner.add(encodeVarintField(4, totalSize)); - // CompressMode stays 0: the CFW's zlib path is detected from the buffer's - // zlib header at BMP-load time, and frag_write must copy our deflated bytes - // VERBATIM (CompressMode!=0 would route them through the 1bpp expander). - inner.add(encodeVarintField(5, 0)); //compression - inner.add(encodeVarintField(6, fragment.index)); - inner.add(encodeVarintField(7, fragment.size)); - inner.add(encodeBytesField(8, fragment.data)); - return wrapEvenHub(3, magic, 5, concat(inner)); - } - - public static byte[] buildHeartbeat(int magic) { - return wrapEvenHub(12, magic, 14, encodeVarintField(1, 0)); - } - - /** - * Cmd=0 CreateStartUpPage carrying a text container (the message) plus a - * list container (the selectable rows) in one page. Uses stock-firmware-safe - * geometry (each container within the stock ~280x130 container-size cap; the - * CFW is what lifts that to 576x288), so this works before flashing. The - * selected row comes back as an async list event — see parseListSelection. - */ - public static byte[] buildCreatePromptPage( - int magic, - String textName, - int textContainerId, - String warningText, - String listName, - int listContainerId, - String[] items - ) { - List inner = new ArrayList<>(); - inner.add(encodeVarintField(1, 2)); // ContainerTotalNum: 1 text + 1 list - inner.add(encodeMessageField(2, encodeListObject(listName, listContainerId, 0, 150, 280, 120, items, true))); - inner.add(encodeMessageField(3, encodeTextObject(textName, textContainerId, 0, 0, 280, 130, warningText, false))); - inner.add(encodeVarintField(5, 10000)); // widgetId - return wrapEvenHub(0, magic, 3, concat(inner)); - } - - public static byte[] encodeListObject( - String name, - int containerId, - int x, - int y, - int width, - int height, - String[] items, - boolean captureEvents - ) { - List parts = new ArrayList<>(); - parts.add(encodeVarintField(1, x)); - parts.add(encodeVarintField(2, y)); - parts.add(encodeVarintField(3, width)); - parts.add(encodeVarintField(4, height)); - parts.add(encodeVarintField(9, containerId)); - parts.add(encodeStringField(10, name)); - parts.add(encodeMessageField(11, encodeListItemContainer(items))); - if (captureEvents) { - parts.add(encodeVarintField(12, 1)); - } - return concat(parts); - } - - private static byte[] encodeListItemContainer(String[] items) { - List parts = new ArrayList<>(); - parts.add(encodeVarintField(1, items.length)); // ItemCount - parts.add(encodeVarintField(3, 1)); // IsItemSelectBorderEn - for (String item : items) { - parts.add(encodeStringField(4, item)); // repeated ItemName - } - return concat(parts); - } - - /** - * Decode a list-selection async event (sid=0xe0, flag=0x01/0x06). Returns - * null if the frame is not a list event. `itemIndex` is 0-based into the - * ItemName array the list was built with; `eventType` is EVENT_CLICK for a - * confirmed selection (scroll/highlight changes arrive with other types). - */ - public static ListSelection parseListSelection(ParsedFrame frame) { - byte[] pb = stripTrailingCrc(frame.pb); - byte[] deviceEvent = readFieldBytes(pb, 13); - if (deviceEvent == null) { - return null; - } - byte[] listEvent = readFieldBytes(deviceEvent, 1); - if (listEvent == null) { - return null; - } - return new ListSelection( - readStringFieldValue(listEvent, 2), - readStringFieldValue(listEvent, 3), - readVarintFieldValue(listEvent, 4, -1), - readVarintFieldValue(listEvent, 5, EVENT_CLICK) - ); - } - - public static byte[] buildAudioControl(int magic, boolean enable) { - return wrapEvenHub(CMD_AUDIO_CONTROL, magic, 18, encodeVarintField(1, enable ? 1 : 0)); - } - - public static byte[] buildShutdown(int magic, int exitMode) { - return wrapEvenHub(9, magic, 11, encodeVarintField(1, exitMode)); - } - - /** - * IMU control (ImuCtrl = field 22 of the main ctx). IMU_CtrlCmd carries - * IMUReportEn (field 1) and reportFrq (field 2). reportFrq is only - * meaningful when enabling; on disable we send just the enable flag. - */ - public static byte[] buildImuControl(int magic, boolean enable, int reportFrq) { - List inner = new ArrayList<>(); - inner.add(encodeVarintField(1, enable ? 1 : 0)); - if (enable && reportFrq > 0) { - inner.add(encodeVarintField(2, reportFrq)); - } - return wrapEvenHub(CMD_OPEN_IMU, magic, 22, concat(inner)); - } - - /** - * Set the lens brightness: G2SettingPackage{commandId=1 (DeviceReceiveInfo), - * deviceReceiveInfoFromApp(3){deviceReceiveBrightness(1){autoAdjust(1), - * brightnessLevel(2)}}} on sid 0x09. brightnessLevel is 0-100 (nonlinear; - * 0 is dim-but-visible, not off). When autoAdjust is set the ambient-light - * sensor drives brightness, so the level field is omitted; otherwise the - * level is always encoded (explicit zero included) so brightnessLevel=0 - * reaches the wire. - */ - public static byte[] buildSetBrightness(int magic, boolean autoAdjust, int brightnessLevel) { - List brightness = new ArrayList<>(); - brightness.add(encodeVarintField(1, autoAdjust ? 1 : 0)); - if (!autoAdjust) { - brightness.add(encodeVarintField(2, brightnessLevel)); - } - return concat(CollectionUtils.listOf( - encodeVarintField(1, 1), - encodeVarintField(2, magic), - encodeMessageField(3, encodeMessageField(1, concat(brightness))) - )); - } - - /** Enable the firmware's wear detector (DeviceReceiveInfoFromAPP field 5). */ - public static byte[] buildSetWearDetection(int magic, boolean enabled) { - byte[] wear = encodeVarintField(1, enabled ? 1 : 0); - return concat(CollectionUtils.listOf( - encodeVarintField(1, 1), - encodeVarintField(2, magic), - encodeMessageField(3, encodeMessageField(5, wear)) - )); - } - - /** - * The application authentication request: DEVICE_SETTINGS/AUTHENTICATION(4) - * on sid 0x80, flag 0x00. Carries no key material — it arms the firmware's - * "security auth" flag; the firmware sends the success notification once - * the flag is set AND the BLE link is encrypted (SMP pairing/bond - * restoration, handled by the OS below GATT, possibly after a pairing - * prompt). Protobuf: f1=4 (command), f2=magic, f3={f1=1, f2=4}. - */ - public static byte[] buildAuthenticationRequest(int magic) { - byte[] auth = concat(CollectionUtils.listOf( - encodeVarintField(1, 1), - encodeVarintField(2, SECURITY_AUTH_CMD) - )); - return concat(CollectionUtils.listOf( - encodeVarintField(1, SECURITY_AUTH_CMD), - encodeVarintField(2, magic), - encodeMessageField(3, auth) - )); - } - - /** - * True when `pb` (trailing CRC still attached) is the success notification - * for the authentication request sent with `magic`: command 4, the same - * magic, and an EMPTY field-3 result message (`1a 00` — the omitted result - * code defaults to zero). A GATT write completing is NOT success; only this - * notification is. - */ - public static boolean isAuthenticationSuccess(byte[] pb, int magic) { - if (pb == null) { - return false; - } - byte[] root = stripTrailingCrc(pb); - if (readVarintFieldValue(root, 1, -1) != SECURITY_AUTH_CMD) { - return false; - } - if (readVarintFieldValue(root, 2, -1) != magic) { - return false; - } - byte[] result = readFieldBytes(root, 3); - return result != null && result.length == 0; - } - - public static byte[] buildSettingsQuery(int magic) { - byte[] request = encodeVarintField(1, 1); - return concat(CollectionUtils.listOf( - encodeVarintField(1, 2), - encodeVarintField(2, magic), - encodeMessageField(4, request) - )); - } - - /** - * Build the CFW-only wake lease control package. Magic zero deliberately - * makes this fire-and-forget: the firmware consumes field 101 before its - * stock nanopb decoder discards the unknown field. - */ - public static byte[] buildFaceclawWakeControl(int operation, int nonce) { - byte[] control = new byte[] { - 'F', - 'C', - (byte) FACECLAW_WAKE_PROTOCOL_VERSION, - (byte) operation, - (byte) (nonce & 0xff), - (byte) ((nonce >>> 8) & 0xff) - }; - return concat(CollectionUtils.listOf( - encodeVarintField(1, 1), - encodeVarintField(2, 0), - encodeBytesField(FACECLAW_WAKE_CONTROL_FIELD, control) - )); - } - - /** - * Wrap an already-encoded ['M','C',ver,op,...] mic-control record as a - * settings write carrying CFW field 103. Magic zero makes it - * fire-and-forget like the wake lease: the firmware consumes the field - * before its stock nanopb decoder discards the unknown tag, and the - * "ack" is the field-104 status notify that follows. - */ - public static byte[] buildFaceclawMicControl(byte[] record) { - return concat(CollectionUtils.listOf( - encodeVarintField(1, 1), - encodeVarintField(2, 0), - encodeBytesField(FACECLAW_MIC_CONTROL_FIELD, record) - )); - } - - /** - * Extract the CFW mic status record (field 104) from a settings frame, or - * null when absent. The 21-byte body layout is decoded on the TS side; - * here we only validate the 'M','C',version prelude. - */ - public static byte[] parseFaceclawMicStatus(byte[] pb) { - if (pb == null) { - return null; - } - byte[] body = readFieldBytes(stripTrailingCrc(pb), FACECLAW_MIC_STATUS_FIELD); - if (body == null - || body.length < 3 - || body[0] != 'M' - || body[1] != 'C') { - return null; - } - return body; - } - - /** - * Return the raw CFW ambient-light report from a sid-0x09 frame, or null - * when this is an ordinary settings frame. Only the 'A','L',version prelude - * is validated here; the fields are decoded on the TS side. - */ - public static byte[] parseFaceclawAlsReport(byte[] pb) { - if (pb == null) { - return null; - } - byte[] body = readFieldBytes(stripTrailingCrc(pb), FACECLAW_ALS_REPORT_FIELD); - if (body == null - || body.length < 24 - || body[0] != 'A' - || body[1] != 'L' - || (body[2] & 0xff) != 1) { - return null; - } - return body; - } - - /** Field 106: RB/version 1/flags/percentage, from the CFW ring-battery cache. */ - public static RingBatterySnapshot parseRingBattery(byte[] pb) { - if (pb == null) return null; - byte[] body = readFieldBytes(stripTrailingCrc(pb), 106); - if (body == null || body.length != 5 || body[0] != 'R' || body[1] != 'B' - || body[2] != 1) return null; - int flags = body[3] & 0xff; - int level = body[4] & 0xff; - if ((flags & ~7) != 0) return null; - if ((flags & 2) != 0) { - if ((flags & 1) == 0 || level > 100) return null; - return new RingBatterySnapshot(level, (flags & 4) != 0 ? 1 : 0); - } - if (level != 255 || (flags & 4) != 0) return null; - return new RingBatterySnapshot(-1, -1); - } - - public static final class RingBatterySnapshot { - final int battery; - final int charging; - RingBatterySnapshot(int battery, int charging) { - this.battery = battery; - this.charging = charging; - } - } - - /** - * Return the uint16 wake nonce from a CFW field-102 deferred-wake - * notification (double tap or head-up), or -1 when this is an ordinary - * settings frame (idle-gesture events included). - */ - public static int parseFaceclawWakeEvent(byte[] pb) { - byte[] event = faceclawWakeEventBytes(pb); - return event == null ? -1 : (event[4] & 0xff) | ((event[5] & 0xff) << 8); - } - - /** - * Which gesture a CFW deferred-wake notification reports: 1 (double tap) - * or FACECLAW_WAKE_EVENT_HEAD_UP; -1 when the frame is not a wake event. - */ - public static int parseFaceclawWakeEventCode(byte[] pb) { - byte[] event = faceclawWakeEventBytes(pb); - return event == null ? -1 : (event[3] & 0xff); - } - - private static byte[] faceclawWakeEventBytes(byte[] pb) { - if (pb == null) { - return null; - } - byte[] event = readFieldBytes(stripTrailingCrc(pb), FACECLAW_WAKE_EVENT_FIELD); - if (event == null - || event.length != 6 - || event[0] != 'F' - || event[1] != 'C' - || (event[2] & 0xff) != FACECLAW_WAKE_PROTOCOL_VERSION) { - return null; - } - int code = event[3] & 0xff; - return code == FACECLAW_WAKE_EVENT || code == FACECLAW_WAKE_EVENT_HEAD_UP ? event : null; - } - - /** An idle gesture from a CFW field-102 notification, in EvenHub sys-event terms. */ - public static final class FaceclawGestureEvent { - public final int eventType; - public final int eventSource; - - FaceclawGestureEvent(int eventType, int eventSource) { - this.eventType = eventType; - this.eventSource = eventSource; - } - } - - /** - * Decode a CFW field-102 idle-gesture event (tap, long press, release - * while no app is on screen) into the EvenHub sys-event type and source - * the same gesture would carry from a live page, or null when the frame - * is not one (ordinary settings frames and the double-tap wake event - * included). - */ - public static FaceclawGestureEvent parseFaceclawGestureEvent(byte[] pb) { - if (pb == null) { - return null; - } - byte[] event = readFieldBytes(stripTrailingCrc(pb), FACECLAW_WAKE_EVENT_FIELD); - if (event == null - || event.length != 6 - || event[0] != 'F' - || event[1] != 'C' - || (event[2] & 0xff) != FACECLAW_WAKE_PROTOCOL_VERSION) { - return null; - } - int eventType; - switch (event[3] & 0xff) { - case FACECLAW_GESTURE_EVENT_TAP: - eventType = EVENT_CLICK; - break; - case FACECLAW_GESTURE_EVENT_LONG_PRESS: - eventType = EVENT_RING_LONG_PRESS; - break; - case FACECLAW_GESTURE_EVENT_LONG_PRESS_RELEASE: - eventType = EVENT_RING_LONG_PRESS_RELEASE; - break; - default: - return null; - } - int eventSource; - switch (event[4] & 0xff) { - case FACECLAW_RAW_SOURCE_LEFT_TEMPLE: - eventSource = EVENT_SOURCE_GLASSES_L; - break; - case FACECLAW_RAW_SOURCE_RIGHT_TEMPLE: - eventSource = EVENT_SOURCE_GLASSES_R; - break; - case FACECLAW_RAW_SOURCE_RING: - eventSource = EVENT_SOURCE_RING; - break; - default: - eventSource = 0; - break; - } - return new FaceclawGestureEvent(eventType, eventSource); - } - - /** - * Decode OnboardingDataPackage EVENT/GLS_WEAR_STATUS. Unlike most async - * G2 traffic, stock firmware sends this event with flag 0x00, so callers - * must recognize it by sid and protobuf shape rather than notify flag. - * Returns 0/1, or -1 when the frame is not a wear event. - */ - public static int parseWearState(ParsedFrame frame) { - if (frame == null || !frame.ok || frame.sid != SID_ONBOARDING) { - return -1; - } - byte[] root = stripTrailingCrc(frame.pb); - if (readVarintFieldValue(root, 1, -1) != 3) { - return -1; - } - byte[] event = readFieldBytes(root, 5); - if (event == null || readVarintFieldValue(event, 1, -1) != 1) { - return -1; - } - int wearStatus = readVarintFieldValue(event, 2, -1); - return wearStatus == 0 || wearStatus == 1 ? wearStatus : -1; - } - - public static BatterySnapshot parseSettingsBattery(byte[] pb) { - byte[] root = stripTrailingCrc(pb); - byte[] request = readFieldBytes(root, 4); - if (request == null) { - return null; - } - int battery = readVarintFieldValue(request, 12, -1); - int charging = readVarintFieldValue(request, 13, -1); - int silentMode = readVarintFieldValue(request, 14, -1); - if (battery < 0) { - return null; - } - return new BatterySnapshot(battery, charging, silentMode); - } - - /** - * Silent mode from an unsolicited settings push. The wearer toggles silent - * mode by long-pressing both touchpads at once; the firmware then sends - * G2SettingPackage{commandId=3 (DeviceSendToAPP), deviceSendInfoToApp(5){ - * silentModeSwitch(2)}} on sid 0x09, with a magic it picked itself, so this - * arrives outside the request/ack flow. - * - * Returns -1 when the frame is not such a push. Ordinary settings-read acks - * (the battery poll) carry commandId=2 and deviceReceiveRequestFromApp in - * field 4, so they never match and stay on the normal ack path. - */ - public static int parseSilentModePush(byte[] pb) { - if (pb == null) { - return -1; - } - byte[] root = stripTrailingCrc(pb); - if (readVarintFieldValue(root, 1, -1) != SETTINGS_CMD_DEVICE_SEND_TO_APP) { - return -1; - } - byte[] info = readFieldBytes(root, 5); - if (info == null) { - return -1; - } - return readVarintFieldValue(info, 2, -1); - } - - /** - * Firmware versions and the firmware-extension string from a settings - * READ ack. Versions are fields 5/6 of the deviceReceiveRequestFromApp - * submessage (field 4); Faceclaw's custom firmware additionally appends - * top-level field 100 with its revision ("Faceclaw/"; older builds sent - * "EVENCFW/ "), which stock firmware never sends. Returns null - * when the ack carries none of it. Compatibility is judged on the TS side - * (app/g2/firmware-compat.ts); Java only needs to know whether the firmware - * is ours at all. - */ - public static FirmwareInfo parseSettingsFirmwareInfo(byte[] pb) { - byte[] root = stripTrailingCrc(pb); - byte[] request = readFieldBytes(root, 4); - if (request == null) { - return null; - } - String leftVersion = readStringFieldValue(request, 5); - String rightVersion = readStringFieldValue(request, 6); - String extension = readStringFieldValue(root, 100); - if (leftVersion.isEmpty() && rightVersion.isEmpty() && extension.isEmpty()) { - return null; - } - return new FirmwareInfo(leftVersion, rightVersion, extension); - } - - public static byte[] wrapEvenHub(int cmd, int magic, int innerFieldNumber, byte[] inner) { - List parts = new ArrayList<>(); - parts.add(encodeVarintField(1, cmd)); - parts.add(encodeVarintField(2, magic)); - parts.add(encodeMessageField(innerFieldNumber, inner)); - return concat(parts); - } - - public static byte[] encodeTextObject(String name, int containerId, int x, int y, int width, int height, String text, boolean captureEvents) { - List parts = new ArrayList<>(); - parts.add(encodeVarintField(1, x)); - parts.add(encodeVarintField(2, y)); - parts.add(encodeVarintField(3, width)); - parts.add(encodeVarintField(4, height)); - parts.add(encodeVarintField(9, containerId)); - parts.add(encodeStringField(10, name)); - if (captureEvents) { - parts.add(encodeVarintField(11, 1)); - } - parts.add(encodeStringField(12, text)); - return concat(parts); - } - - public static byte[] encodeImageObject(ImageTileOptions tile) { - List parts = new ArrayList<>(); - parts.add(encodeVarintField(1, tile.x)); - parts.add(encodeVarintField(2, tile.y)); - parts.add(encodeVarintField(3, tile.width)); - parts.add(encodeVarintField(4, tile.height)); - parts.add(encodeVarintField(5, tile.containerId)); - parts.add(encodeStringField(6, tile.name)); - return concat(parts); - } - - private static byte[] concat(List parts) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - for (byte[] part : parts) { - if (part != null && part.length > 0) { - out.write(part, 0, part.length); - } - } - return out.toByteArray(); - } - - private static byte[] encodeVarintField(int fieldNumber, int value) { - return concat(CollectionUtils.listOf(encodeKey(fieldNumber, 0), encodeVarint(value))); - } - - private static byte[] encodeStringField(int fieldNumber, String value) { - byte[] bytes = value == null ? new byte[0] : value.getBytes(StandardCharsets.UTF_8); - return concat(CollectionUtils.listOf(encodeKey(fieldNumber, 2), encodeVarint(bytes.length), bytes)); - } - - private static byte[] encodeBytesField(int fieldNumber, byte[] value) { - byte[] bytes = value == null ? new byte[0] : value; - return concat(CollectionUtils.listOf(encodeKey(fieldNumber, 2), encodeVarint(bytes.length), bytes)); - } - - private static byte[] encodeMessageField(int fieldNumber, byte[] value) { - byte[] bytes = value == null ? new byte[0] : value; - return concat(CollectionUtils.listOf(encodeKey(fieldNumber, 2), encodeVarint(bytes.length), bytes)); - } - - private static byte[] encodeKey(int fieldNumber, int wireType) { - return encodeVarint((fieldNumber << 3) | wireType); - } - - private static byte[] encodeVarint(int value) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - int v = value >>> 0; - while (v >= 0x80) { - out.write((v & 0x7f) | 0x80); - v >>>= 7; - } - out.write(v); - return out.toByteArray(); - } - - private static byte[] crcBytesLe(byte[] data) { - int crc = 0xffff; - for (byte b : data) { - crc ^= (b & 0xff) << 8; - for (int i = 0; i < 8; i++) { - if ((crc & 0x8000) != 0) { - crc = ((crc << 1) ^ 0x1021) & 0xffff; - } else { - crc = (crc << 1) & 0xffff; - } - } - } - return new byte[] {(byte) (crc & 0xff), (byte) ((crc >>> 8) & 0xff)}; - } - - - /** - * Split one notification value into individual `aa21`/`aa12` envelope - * frames. A value normally holds exactly one frame, but the firmware can - * pack several (each self-describing via the length byte at offset 3), and - * a frame that only reads the first would silently drop the rest. A value - * that does not start with an envelope, or a trailing partial frame - * (truncated by the stack — observed on a Samsung tablet for values over - * 64 bytes), is returned as-is so the caller can log it. - */ - public static List splitFrames(byte[] value) { - List out = new ArrayList<>(); - if (value == null || value.length == 0) { - return out; - } - int offset = 0; - while (offset + 8 <= value.length - && value[offset] == (byte) 0xaa - && (value[offset + 1] == 0x21 || value[offset + 1] == 0x12)) { - int len = value[offset + 3] & 0xff; - int end = Math.min(value.length, offset + 8 + len); - out.add(Arrays.copyOfRange(value, offset, end)); - offset = end; - } - if (offset < value.length) { - out.add(Arrays.copyOfRange(value, offset, value.length)); - } - return out; - } - - public static ParsedFrame parseFrame(byte[] buf) { - if (buf == null || buf.length < 10 || buf[0] != (byte) 0xaa || (buf[1] != 0x21 && buf[1] != 0x12)) { - return new ParsedFrame(false, 0, 0, new byte[0], -1, -1); - } - int sid = buf[6] & 0xff; - int flag = buf[7] & 0xff; - int len = buf[3] & 0xff; - int end = Math.min(buf.length, 8 + len); - byte[] pb = Arrays.copyOfRange(buf, 8, end); - - int msgType = -1; - int msgSeq = -1; - int offset = 0; - for (int i = 0; i < 8 && offset < pb.length; i++) { - VarintResult key = readVarint(pb, offset); - if (key == null) { - break; - } - offset = key.next; - int tag = key.value >> 3; - int wire = key.value & 7; - if (wire == 0) { - VarintResult value = readVarint(pb, offset); - if (value == null) { - break; - } - if (tag == 1) { - msgType = value.value; - } else if (tag == 2) { - msgSeq = value.value; - } - offset = value.next; - } else if (wire == 2) { - VarintResult length = readVarint(pb, offset); - if (length == null) { - break; - } - offset = length.next + length.value; - } else { - break; - } - if (msgType >= 0 && msgSeq >= 0) { - break; - } - } - return new ParsedFrame(true, sid, flag, pb, msgType, msgSeq); - } - - public static byte[] stripTrailingCrc(byte[] pb) { - if (pb == null || pb.length < 2) { - return pb == null ? new byte[0] : pb; - } - return Arrays.copyOf(pb, pb.length - 2); - } - - /** Decode the stock firmware's compass navigation notifications (sid 0x08). */ - public static CompassEvent parseCompassEvent(ParsedFrame frame) { - if (frame == null - || !frame.ok - || frame.sid != SID_NAVIGATION - || (frame.flag != FLAG_NOTIFY && frame.flag != FLAG_NOTIFY_ALT)) { - return null; - } - byte[] root = stripTrailingCrc(frame.pb); - int command = readVarintFieldValue(root, 1, -1); - if (command == NAV_CMD_COMPASS_CHANGED) { - byte[] compass = readFieldBytes(root, 10); - if (compass == null) return null; - int heading = readVarintFieldValue(compass, 1, -1); - if (heading < 0 || heading >= 360) return null; - byte[] diagnostic = readFieldBytes(root, 100); - if (diagnostic != null && diagnostic.length == 12 - && diagnostic[0] == 'C' && diagnostic[1] == 'M' && diagnostic[2] == 1 - && diagnostic[7] == 0 - && ((diagnostic[3] & 0xff) <= 3 || diagnostic[3] == (byte) 255) - && ((diagnostic[4] & 0xff) <= 2 || diagnostic[4] == (byte) 255) - && (diagnostic[5] & 0xff) <= 3) { - long sampleTimeMs = 0; - for (int i = 0; i < 4; i++) sampleTimeMs |= (long) (diagnostic[8 + i] & 0xff) << (8 * i); - return new CompassEvent(command, heading, - diagnostic[3] == (byte) 255 ? -1 : diagnostic[3] & 0xff, - diagnostic[4] == (byte) 255 ? -1 : diagnostic[4] & 0xff, - diagnostic[5] & 0xff, diagnostic[6] & 0xff, sampleTimeMs); - } - // Stock/older CFW and unknown extensions still supply usable headings. - return new CompassEvent(command, heading); - } - if (command == NAV_CMD_COMPASS_CALIBRATION_STARTED - || command == NAV_CMD_COMPASS_CALIBRATION_COMPLETE) { - return new CompassEvent(command, -1); - } - return null; - } - - /** - * Display wake notification observed after a ring or right-arm double tap - * while no EvenHub page is running: - * - * sid=0x0d, flag=notify, payload f1=1, f3={f1=1} - * - * Other state-change shapes (wear, charging, head-up, etc.) are deliberately - * excluded. The caller additionally gates this on an intentional EvenHub - * suspension, so normal state traffic cannot become application input. - */ - public static boolean isDisplayWakeStateChange(ParsedFrame frame) { - if (frame == null - || !frame.ok - || frame.sid != SID_STATE_CHANGE - || (frame.flag != FLAG_NOTIFY && frame.flag != FLAG_NOTIFY_ALT)) { - return false; - } - byte[] root = stripTrailingCrc(frame.pb); - if (readVarintFieldValue(root, 1, -1) != 1) { - return false; - } - byte[] state = readFieldBytes(root, 3); - return state != null - && state.length == 2 - && readVarintFieldValue(state, 1, -1) == 1; - } - - public static byte[] readFieldBytes(byte[] pb, int fieldNumber) { - int offset = 0; - while (offset < pb.length) { - VarintResult key = readVarint(pb, offset); - if (key == null) { - return null; - } - offset = key.next; - int field = key.value >> 3; - int wire = key.value & 0x07; - if (wire == 0) { - VarintResult value = readVarint(pb, offset); - if (value == null) { - return null; - } - offset = value.next; - continue; - } - if (wire == 1) { - offset += 8; - continue; - } - if (wire == 5) { - offset += 4; - continue; - } - if (wire != 2) { - return null; - } - VarintResult length = readVarint(pb, offset); - if (length == null) { - return null; - } - offset = length.next; - int end = offset + length.value; - if (end > pb.length) { - return null; - } - byte[] bytes = Arrays.copyOfRange(pb, offset, end); - offset = end; - if (field == fieldNumber) { - return bytes; - } - } - return null; - } - - public static int readVarintFieldValue(byte[] pb, int fieldNumber, int defaultValue) { - int offset = 0; - while (offset < pb.length) { - VarintResult key = readVarint(pb, offset); - if (key == null) { - return defaultValue; - } - offset = key.next; - int field = key.value >> 3; - int wire = key.value & 0x07; - if (wire == 0) { - VarintResult value = readVarint(pb, offset); - if (value == null) { - return defaultValue; - } - offset = value.next; - if (field == fieldNumber) { - return value.value; - } - continue; - } - if (wire == 1) { - offset += 8; - continue; - } - if (wire == 5) { - offset += 4; - continue; - } - if (wire != 2) { - return defaultValue; - } - VarintResult length = readVarint(pb, offset); - if (length == null) { - return defaultValue; - } - offset = length.next + length.value; - } - return defaultValue; - } - - /** - * Read a `float` (protobuf wire type 5, little-endian fixed32) field. The - * g2-kit proto declares IMU_Report_Data's x/y/z as `double`, but the - * firmware actually sends them as fixed32 floats (confirmed from captures). - * Returns defaultValue if absent. - */ - public static float readFloatFieldValue(byte[] pb, int fieldNumber, float defaultValue) { - int offset = 0; - while (offset < pb.length) { - VarintResult key = readVarint(pb, offset); - if (key == null) { - return defaultValue; - } - offset = key.next; - int field = key.value >> 3; - int wire = key.value & 0x07; - if (wire == 0) { - VarintResult value = readVarint(pb, offset); - if (value == null) { - return defaultValue; - } - offset = value.next; - } else if (wire == 1) { - offset += 8; - } else if (wire == 5) { - if (offset + 4 > pb.length) { - return defaultValue; - } - if (field == fieldNumber) { - int bits = 0; - for (int i = 0; i < 4; i++) { - bits |= (pb[offset + i] & 0xff) << (8 * i); - } - return Float.intBitsToFloat(bits); - } - offset += 4; - } else if (wire == 2) { - VarintResult length = readVarint(pb, offset); - if (length == null) { - return defaultValue; - } - offset = length.next + length.value; - } else { - return defaultValue; - } - } - return defaultValue; - } - - public static String readStringFieldValue(byte[] pb, int fieldNumber) { - byte[] bytes = readFieldBytes(pb, fieldNumber); - if (bytes == null) { - return ""; - } - return new String(bytes, StandardCharsets.UTF_8); - } - - public static VarintResult readVarint(byte[] pb, int offset) { - int value = 0; - int shift = 0; - int cursor = offset; - while (cursor < pb.length) { - int b = pb[cursor++] & 0xff; - value |= (b & 0x7f) << shift; - if ((b & 0x80) == 0) { - return new VarintResult(value, cursor); - } - shift += 7; - } - return null; - } - - - public static final class ParsedFrame { - final boolean ok; - final int sid; - final int flag; - final byte[] pb; - final int msgType; - final int msgSeq; - - ParsedFrame(boolean ok, int sid, int flag, byte[] pb, int msgType, int msgSeq) { - this.ok = ok; - this.sid = sid; - this.flag = flag; - this.pb = pb; - this.msgType = msgType; - this.msgSeq = msgSeq; - } - } - - private static final class VarintResult { - final int value; - final int next; - - VarintResult(int value, int next) { - this.value = value; - this.next = next; - } - } - - public static final class ListSelection { - public final String containerName; - public final String itemName; - public final int itemIndex; - public final int eventType; - - ListSelection(String containerName, String itemName, int itemIndex, int eventType) { - this.containerName = containerName == null ? "" : containerName; - this.itemName = itemName == null ? "" : itemName; - this.itemIndex = itemIndex; - this.eventType = eventType; - } - } - - public static final class CompassEvent { - public final int command; - public final int headingDegrees; - /** -1 means unavailable; source: 0 unknown, 1 GRV, 2 GMRV, 3 RV. */ - public final int magneticAccuracy, magneticAnomalies, orientationSource, diagnosticFlags; - public final long sampleTimeMs; - - CompassEvent(int command, int headingDegrees) { - this(command, headingDegrees, -1, -1, -1, -1, -1); - } - - CompassEvent(int command, int headingDegrees, int magneticAccuracy, int magneticAnomalies, - int orientationSource, int diagnosticFlags, long sampleTimeMs) { - this.command = command; - this.headingDegrees = headingDegrees; - this.magneticAccuracy = magneticAccuracy; - this.magneticAnomalies = magneticAnomalies; - this.orientationSource = orientationSource; - this.diagnosticFlags = diagnosticFlags; - this.sampleTimeMs = sampleTimeMs; - } - } - - public static final class ImageTileOptions { - final String name; - final int containerId; - final int x; - final int y; - final int width; - final int height; - - ImageTileOptions(String name, int containerId, int x, int y, int width, int height) { - this.name = name; - this.containerId = containerId; - this.x = x; - this.y = y; - this.width = width; - this.height = height; - } - } - - public static final class ImageFragment { - final int index; - final byte[] data; - final int size; - - ImageFragment(int index, byte[] data, int size) { - this.index = index; - this.data = data; - this.size = size; - } - } - - public static final class BatterySnapshot { - final int battery; - final int charging; - /** 1 = silent mode on, 0 = off, -1 = the ack didn't carry the field. */ - final int silentMode; - - BatterySnapshot(int battery, int charging, int silentMode) { - this.battery = battery; - this.charging = charging; - this.silentMode = silentMode; - } - } - - public static final class FirmwareInfo { - /** Prefix of the firmware-extension string on Faceclaw's custom firmware. */ - public static final String FACECLAW_EXTENSION_PREFIX = "Faceclaw/"; - - final String leftVersion; - final String rightVersion; - /** Raw field-100 string ("" on stock firmware). */ - final String extension; - - FirmwareInfo(String leftVersion, String rightVersion, String extension) { - this.leftVersion = leftVersion; - this.rightVersion = rightVersion; - this.extension = extension == null ? "" : extension; - } - - /** - * True when the glasses run Faceclaw's custom firmware (any revision). - * Whether the revision is the one this app needs is decided on the TS - * side, which disconnects on a mismatch; this only guards the private - * modes against stock or third-party firmware in the meantime. - */ - public boolean isFaceclawFirmware() { - return extension.trim().startsWith(FACECLAW_EXTENSION_PREFIX); - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwMessageWindow.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwMessageWindow.java deleted file mode 100644 index c342b066..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwMessageWindow.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.faceclaw.app; - -import java.util.ArrayList; -import java.util.List; - -/** Ordered completion and go-back-N recovery for the custom-message window. */ -final class CfwMessageWindow { - // Flappy capture: p99 ACK 63ms, max 77ms. Leave room for full-frame - // processing while recovering a lost reply well before the stock timeout. - static final int ACK_TIMEOUT_MS = 500; - static final int MAX_RETRIES = 3; - - private CfwMessageWindow() {} - - static OutboundMessage acknowledgedHead(Iterable messages) { - for (OutboundMessage message : messages) { - if (message.sid != CfwTransport.SID) continue; - return !message.cfwRetryPending && message.cfwAckLenses == CfwTransport.BOTH ? message : null; - } - return null; - } - - static List replayWindow(Iterable messages, long nowMs) { - List replay = new ArrayList<>(); - boolean failed = false; - for (OutboundMessage message : messages) { - if (message.sid != CfwTransport.SID) continue; - failed |= message.cfwRetryPending || (message.cfwAckLenses != CfwTransport.BOTH - && message.ackDeadlineAtMs > 0 && message.ackDeadlineAtMs <= nowMs); - replay.add(message); - } - // A lost ACK, first fragment, or NACK can leave an older attempt - // unresolved. Replaying only the named failure strands that older - // attempt at the head until its timeout. Rewind the unresolved window. - if (!failed) replay.clear(); - return replay; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwTransport.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwTransport.java deleted file mode 100644 index e921b3fb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/CfwTransport.java +++ /dev/null @@ -1,141 +0,0 @@ -package com.faceclaw.app; - -import java.io.ByteArrayOutputStream; -import java.util.zip.Deflater; -import java.util.ArrayList; -import java.util.List; - -/** Faceclaw/10 records: clear flags, wire length and decoded CRC; persistent zlib body. */ -public final class CfwTransport { - public static final int SID = 0xf0; - public static final int BOTH = 3; - public static final int MAX_MESSAGE = 65535; - public static final int COMPRESSED = 4, RESET_CONTEXT = 8; - private final Deflater deflater = new Deflater(); - private boolean resetPending = true; - private int previousLenses; - - public CfwTransport() {} - - public synchronized void reset() { - deflater.reset(); - resetPending = true; - previousLenses = 0; - } - - /** Compress only when writing, in exactly the order seen by the receiver. */ - public synchronized List encode(byte[] message, int streamId, int lenses, int mtu) { - validate(message, streamId, lenses, mtu); - if (previousLenses != lenses) reset(); - boolean reset = resetPending; - deflater.setInput(message); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] chunk = new byte[4096]; - int count; - do { - count = deflater.deflate(chunk, 0, chunk.length, Deflater.SYNC_FLUSH); - out.write(chunk, 0, count); - } while (count == chunk.length); - byte[] body = out.toByteArray(); - int flags = lenses | COMPRESSED | (reset ? RESET_CONTEXT : 0); - if (body.length == 0) flags &= ~COMPRESSED; // repeated empty sync flush - if (body.length > MAX_MESSAGE) { - // Incompressible maximum-size records still fit as raw records. - // The attempted deflate advanced history: reset both ends before reuse. - reset(); - body = message; - flags = lenses | RESET_CONTEXT; - } else { - resetPending = false; - previousLenses = lenses; - } - return frameRecord(body, crc(message), streamId, flags, mtu); - } - - private static void validate(byte[] message, int streamId, int lenses, int mtu) { - if (message == null || message.length > MAX_MESSAGE || streamId < 0 || streamId > 255 - || lenses < 1 || lenses > BOTH || mtu < 23 || mtu > 517) - throw new IllegalArgumentException("invalid CFW stream parameters"); - } - - /** One logical command per stream for now. Packets are bounded by actual ATT MTU. */ - public static List frame(byte[] message, int streamId, int lenses, int mtu) { - validate(message, streamId, lenses, mtu); - return frameRecord(message, crc(message), streamId, lenses | RESET_CONTEXT, mtu); - } - - private static List frameRecord(byte[] body, int checksum, int streamId, int flags, int mtu) { - int lenses = flags & BOTH; - int capacity = Math.min(252, mtu - 14); - byte[] stream = new byte[body.length + 5]; - stream[0] = (byte) flags; - stream[1] = (byte) body.length; - stream[2] = (byte) (body.length >>> 8); - stream[3] = (byte) checksum; - stream[4] = (byte) (checksum >>> 8); - System.arraycopy(body, 0, stream, 5, body.length); - List packets = new ArrayList<>(); - for (int offset = 0, index = 0; offset < stream.length; offset += capacity, index++) { - int count = Math.min(capacity, stream.length - offset); - byte[] packet = new byte[count + 11]; - packet[0] = (byte) 0xaa; - packet[1] = 0x21; - packet[2] = (byte) (streamId + index); - packet[3] = (byte) (count + 3); - packet[4] = packet[5] = 1; - packet[6] = (byte) SID; - packet[8] = (byte) (lenses | (offset == 0 ? 0x80 : 0) - | (offset + count == stream.length ? 0x40 : 0)); - System.arraycopy(stream, offset, packet, 9, count); - int crc = crc(packet, 8, count + 1); - packet[packet.length - 2] = (byte) crc; - packet[packet.length - 1] = (byte) (crc >>> 8); - packets.add(packet); - } - return packets; - } - - public static int crc(byte[] data) { return crc(data, 0, data.length); } - private static int crc(byte[] data, int offset, int count) { - int crc = 0xffff; - for (int i = offset; i < offset + count; i++) { - crc ^= (data[i] & 255) << 8; - for (int bit = 0; bit < 8; bit++) - crc = ((crc << 1) ^ ((crc & 0x8000) != 0 ? 0x1021 : 0)) & 65535; - } - return crc; - } - private static int u16(byte[] data, int offset) { - return (data[offset] & 255) | ((data[offset + 1] & 255) << 8); - } - public static Ack parseAck(byte[] packet) { - Ack[] acks = parseAcks(packet); - return acks == null ? null : acks[0]; - } - /** Primary ACK followed by up to three explicit preceding successes from - * the same processing lens. Validate the entire packet before applying any. */ - public static Ack[] parseAcks(byte[] packet) { - if (packet == null || packet.length < 19 || packet.length > 40 - || (packet.length - 19) % 7 != 0 || (packet[0] & 255) != 0xaa - || packet[1] != 0x12 || (packet[3] & 255) != packet.length - 8 || packet[4] != 1 || packet[5] != 1 - || (packet[6] & 255) != SID || packet[7] != 0 || (packet[8] != 1 && packet[8] != 3) - || (packet[12] != 1 && packet[12] != 2) - || (packet[8] == 3 && packet.length != 19) - || crc(packet, 8, packet.length - 10) != u16(packet, packet.length - 2)) return null; - Ack[] acks = new Ack[1 + (packet.length - 19) / 7]; - acks[0] = new Ack(packet[8] == 3, packet[9] & 255, u16(packet, 10), packet[12], u16(packet, 13), u16(packet, 15)); - for (int i = 1, offset = 17; i < acks.length; i++, offset += 7) - acks[i] = new Ack(false, packet[offset] & 255, u16(packet, offset + 1), - packet[12], u16(packet, offset + 3), u16(packet, offset + 5)); - return acks; - } - public static final class Ack { - public final boolean nack; - public final int streamId, messageId, lens, size, checksum; - Ack(boolean nack, int streamId, int messageId, int lens, int size, int checksum) { - this.nack = nack; - this.streamId = streamId; this.messageId = messageId; this.lens = lens; - this.size = size; this.checksum = checksum; - } - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java deleted file mode 100644 index 331090e3..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/ConnectionOptions.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.faceclaw.app; -import android.bluetooth.BluetoothGattCharacteristic; - -public class ConnectionOptions { - public static final int WRITE_TYPE = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE; - public static final int DESIRED_MTU = 512; - - public static final int CONNECT_TIMEOUT_MS = 5_000; - public static final int SERVICES_TIMEOUT_MS = 5_000; - public static final int DESCRIPTOR_TIMEOUT_MS = 5_000; - public static final int RING_DESIRED_MTU = 247; - public static final int RING_RECONNECT_DELAY_MS = 2_000; - // The ring connects with autoConnect=true (see FaceclawBleManager.connect's - // 3-arg overload) - Android manages that as a background reconnect rather - // than an immediate attempt, so it needs far more room than the glasses' - // direct-connect CONNECT_TIMEOUT_MS. Untuned guess, not measured. - public static final int RING_CONNECT_TIMEOUT_MS = 20_000; - public static final int WRITE_TIMEOUT_MS = 2_000; - public static final int PRELUDE_TIMEOUT_MS = 2_000; - // Application security-auth exchange (sid 0x80). The success notification - // only arrives once the BLE link is encrypted, which on a first-time - // connection means SMP pairing — possibly with an OS dialog the user has - // to accept — so the stock-firmware paths wait generously. - public static final int SECURITY_AUTH_TIMEOUT_MS = 30_000; - // The main communicator's wait is soft (it proceeds on timeout so firmware - // that never answers can't stall every reconnect), so it is kept short. - public static final int SECURITY_AUTH_SOFT_TIMEOUT_MS = 6_000; - public static final int ACK_TIMEOUT_MS = 3_500; - public static final int HEARTBEAT_FAILURE_DEADLINE_MS = 10_000; - public static final int HEARTBEAT_READY_MS = 4_000; - public static final int HEARTBEAT_URGENT_MS = 6_000; - public static final int BATTERY_REFRESH_INTERVAL_MS = 5 * 60_000; - public static final int BATTERY_INPUT_QUIET_MS = 5_000; - // Poll cadence while in charging mode; also bounds how quickly we notice - // the glasses coming back out of the case. - public static final int CHARGING_BATTERY_POLL_MS = 30_000; - public static final int IMAGE_FRAGMENT_SIZE = 3800; - public static final int IMAGE_RETRY_DELAY_MS = 2_000; - public static final int MAX_CONSECUTIVE_ACK_TIMEOUTS = 8; - public static final int EVEN_APP_WRITE_FAILURE_WINDOW_MS = 15_000; - public static final int IDLE_SLEEP_MS = 100; - public static final int RECONNECT_DELAY_MS = 2_000; - - final boolean sendImagesToLeft = true; - final boolean skipSessionIds = true; - // Max messages in flight (awaiting ack) at once — full pipelining. >1 lets a - // frame be sent before the previous frame's ack returns, hiding the BLE ack - // round-trip that otherwise serialized every update. Requires firmware that - // accepts pipelined image messages (the CFW snapshot/deferred FIFO). At 1 the - // communicator behaves exactly as before (serial sends + last-packet prewrite). - final int WINDOW_SIZE = 3; - // Send changed-region (mode 3 bounding box) updates instead of full frames - // when the previous frame is known to be displayed. Requires firmware with - // the experimental bbox-incremental mode. Disabled: the firmware-side - // display buffer is not always the previous frame (occasionally two frames - // back, apparently display-driver buffer swapping), so partial updates - // composite onto stale content per-lens. Re-enable once the firmware - // guarantees the compositing base. - final boolean INCREMENTAL_FRAMES = true; - - // Send several tight rectangle deltas as one CFW mode-8 multi-segment message - // instead of a single bounding box, when the change splits into regions whose - // bounding box wastes bytes (distant edits, or a sparse box). Requires firmware - // with mode-8 multi-rect support; needs INCREMENTAL_FRAMES (same seeded shadow). - // Falls back to the single bounding box whenever multi-rect isn't smaller. - final boolean MULTI_RECT_FRAMES = true; - // Cap on rects per batch. Each rect consumes a distinct CFW frame id, and the - // firmware's duplicate-fid ring holds 16, so keep several batches of history - // within it. Above this the split is abandoned for a single bounding box. - public static final int MULTI_RECT_MAX_RECTS = 6; - // Don't bother splitting when the single bounding box already compresses this - // small (one tight rect); only attempt multi-rect above it or when the box - // spans multiple horizontal clusters. - public static final int MULTI_RECT_MIN_PAYLOAD = 900; - - // Ship text as on-glasses cached-glyph draws (CFW modes 12/14) instead of - // pixels, punching glyph ink out of the baked deltas (see TexturePlanner). - // Also gated at runtime on the firmware advertising texcache12 + texstr14. - final boolean TEXTURE_CACHE_FRAMES = true; -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/G2Event.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/G2Event.java deleted file mode 100644 index 7d5c57bb..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/G2Event.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.faceclaw.app; - -public final class G2Event { - final String kind; - final String containerName; - final int eventType; - final int eventSource; - final int systemExitReasonCode; - // IMU_Report_Data (Sys_ItemEvent.IMUData), populated when the firmware - // attaches an accelerometer reading to a sys-event. - final boolean hasImu; - final double imuX; - final double imuY; - final double imuZ; - - public G2Event(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode) { - this(kind, containerName, eventType, eventSource, systemExitReasonCode, false, 0, 0, 0); - } - - public G2Event(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, - boolean hasImu, double imuX, double imuY, double imuZ) { - this.kind = kind; - this.containerName = containerName; - this.eventType = eventType; - this.eventSource = eventSource; - this.systemExitReasonCode = systemExitReasonCode; - this.hasImu = hasImu; - this.imuX = imuX; - this.imuY = imuY; - this.imuZ = imuZ; - } - - public static G2Event decode(BleProtocol.ParsedFrame frame) { - byte[] pb = BleProtocol.stripTrailingCrc(frame.pb); - - // sid 0x07 is the stock Even AI app, not EvenHub, so it carries an - // EvenAIDataPackage rather than the device-event wrapper below. - // EvenAIDataPackage { command_id = 1, magic_random = 2, ctrl = 3 }, - // EvenAIControl { status = 1 }. The event we care about is - // command_id=CTRL with status=WAKE_UP, which the firmware emits when the - // on-glasses "Hey Even" wakeword fires. - if (frame.sid == BleProtocol.SID_EVEN_AI) { - int commandId = BleProtocol.readVarintFieldValue(pb, 1, -1); - if (commandId != BleProtocol.EVEN_AI_CMD_CTRL) { - return null; - } - byte[] ctrl = BleProtocol.readFieldBytes(pb, 3); - if (ctrl == null) { - return null; - } - int status = BleProtocol.readVarintFieldValue(ctrl, 1, -1); - if (status < 0) { - return null; - } - return new G2Event("even-ai", "", status, 0, 0); - } - - byte[] deviceEvent = BleProtocol.readFieldBytes(pb, 13); - if (deviceEvent == null) { - return null; - } - - byte[] listEvent = BleProtocol.readFieldBytes(deviceEvent, 1); - if (listEvent != null) { - return new G2Event( - "list-click", - BleProtocol.readStringFieldValue(listEvent, 2), - BleProtocol.readVarintFieldValue(listEvent, 5, BleProtocol.EVENT_CLICK), - 0, - 0 - ); - } - - byte[] textEvent = BleProtocol.readFieldBytes(deviceEvent, 2); - if (textEvent != null) { - return new G2Event( - "text-click", - BleProtocol.readStringFieldValue(textEvent, 2), - BleProtocol.readVarintFieldValue(textEvent, 3, BleProtocol.EVENT_CLICK), - 0, - 0 - ); - } - - byte[] sysEvent = BleProtocol.readFieldBytes(deviceEvent, 3); - if (sysEvent != null) { - int eventType = BleProtocol.readVarintFieldValue(sysEvent, 1, BleProtocol.EVENT_CLICK); - int eventSource = BleProtocol.readVarintFieldValue(sysEvent, 2, 0); - int exitReason = BleProtocol.readVarintFieldValue(sysEvent, 4, 0); - // IMUData (field 3) is IMU_Report_Data { double x=1, y=2, z=3 }. It - // may accompany any sys-event, or arrive as a standalone - // IMU_DATA_REPORT sample. - byte[] imu = BleProtocol.readFieldBytes(sysEvent, 3); - if (imu != null) { - double x = BleProtocol.readFloatFieldValue(imu, 1, 0); - double y = BleProtocol.readFloatFieldValue(imu, 2, 0); - double z = BleProtocol.readFloatFieldValue(imu, 3, 0); - return new G2Event("sys-event", "", eventType, eventSource, exitReason, true, x, y, z); - } - return new G2Event("sys-event", "", eventType, eventSource, exitReason); - } - return null; - } - - private static String hex(byte[] data) { - if (data == null) { - return "null"; - } - StringBuilder sb = new StringBuilder(data.length * 2); - for (byte b : data) { - sb.append(String.format("%02x", b & 0xff)); - } - return sb.toString(); - } - -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java deleted file mode 100644 index 8377dcc4..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java +++ /dev/null @@ -1,281 +0,0 @@ -package com.faceclaw.app; - -public class MessageBuilder { - private static final int ACK_TIMEOUT_MS = 3_500; - private static final int HEARTBEAT_TIMEOUT_MS = 1_500; - - private BleMagicPool magicPool; - - public MessageBuilder(BleMagicPool magicPool) { - this.magicPool = magicPool; - } - - public OutboundMessage prelude() { - return new OutboundMessage( - "prelude", "prelude", - BleProtocol.PRELUDE_ACK_SID, - BleProtocol.FLAG_REQUEST, - BleProtocol.PRELUDE_ACK_MAGIC, - BleProtocol.PRELUDE_F5872_PAYLOAD, - ACK_TIMEOUT_MS, - -1, - false - ); - } - - /** The per-connection sid-0x80 security-auth request (one per arm). */ - public OutboundMessage securityAuth(boolean leftArm) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "security-auth", - "security auth " + (leftArm ? "L" : "R"), - BleProtocol.SID_SECURITY_AUTH, - BleProtocol.FLAG_SECURITY_AUTH, - magic, - BleProtocol.buildAuthenticationRequest(magic), - ConnectionOptions.SECURITY_AUTH_SOFT_TIMEOUT_MS, - -1, - leftArm - ); - } - - public OutboundMessage shutdown(int exitMode) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "shutdown", - "shutdown mode=" + exitMode, - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildShutdown(magic, exitMode), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage imagePayload( - BleProtocol.ImageTileOptions tile, - int sessionId, - byte[] payload, - String label, - boolean leftArm - ) { - return imagePayload("sound", tile, sessionId, payload, label, leftArm); - } - - public OutboundMessage imagePayload( - String kind, - BleProtocol.ImageTileOptions tile, - int sessionId, - byte[] payload, - String label, - boolean leftArm - ) { - // Keep these arguments for source compatibility with existing callers; - // the private command no longer carries a tile, session, or image header. - return customMessage(kind, payload, label, -1, leftArm); - } - - public OutboundMessage customMessage(String kind, byte[] payload, String label, - int tileIndex, boolean leftArm) { - if (payload == null || payload.length > CfwTransport.MAX_MESSAGE) - throw new IllegalArgumentException("CFW message exceeds uint16 length"); - int streamId = magicPool.allocate(); - return new OutboundMessage(kind, label, CfwTransport.SID, 0, streamId, payload, - CfwMessageWindow.ACK_TIMEOUT_MS, tileIndex, leftArm); - } - - public OutboundMessage cfwCleanup( - BleProtocol.ImageTileOptions tile, - int sessionId, - boolean leftArm - ) { - byte[] payload = new byte[] { (byte) BleProtocol.CFW_IMAGE_MODE_CLEANUP }; - return imagePayload( - "cfw-cleanup", - tile, - sessionId, - payload, - "CFW cleanup", - leftArm - ); - } - - public OutboundMessage enableOrDisableMic(boolean enable) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "audio-control", - enable ? "G2 mic enable" : "G2 mic disable", - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildAudioControl(magic, enable), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage enableOrDisableImu(boolean enable, int reportFrq) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "imu-control", - enable ? "IMU enable" : "IMU disable", - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildImuControl(magic, enable, reportFrq), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage setBrightness(boolean autoAdjust, int brightnessLevel) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "brightness-control", - autoAdjust ? "brightness auto" : "brightness level=" + brightnessLevel, - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildSetBrightness(magic, autoAdjust, brightnessLevel), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage setWearDetection(boolean enabled) { - int magic = magicPool.allocate(); - return new OutboundMessage( - "wear-detection-control", - "wear detection " + (enabled ? "enable" : "disable"), - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildSetWearDetection(magic, enabled), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage createLayout() { - int magic = magicPool.allocate(); - return new OutboundMessage( - "create-layout", - "create-layout", - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildCreateInputPage(magic), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage startupTextProbe() { - int magic = magicPool.allocate(); - return new OutboundMessage( - "startup-text-probe", - "startup text probe", - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildDashboardTextUpgrade(magic), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage heartbeat() { - int magic = magicPool.allocate(); - return new OutboundMessage( - "heartbeat", - "heartbeat", - BleProtocol.SID_EVENHUB, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildHeartbeat(magic), - HEARTBEAT_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage batteryQuery() { - int magic = magicPool.allocate(); - return new OutboundMessage( - "battery", - "battery", - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - magic, - BleProtocol.buildSettingsQuery(magic), - ACK_TIMEOUT_MS, - -1, - false - ); - } - - public OutboundMessage faceclawWakeControl(int operation, int nonce, boolean leftArm) { - return new OutboundMessage( - "wake-lease-control", - "wake lease op=" + operation + " nonce=" + nonce + (leftArm ? " L" : " R"), - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - 0, - BleProtocol.buildFaceclawWakeControl(operation, nonce), - ACK_TIMEOUT_MS, - -1, - leftArm - ); - } - - public OutboundMessage faceclawFramebufferControl(int operation, boolean leftArm) { - return new OutboundMessage( - "framebuffer-lease-control", - "framebuffer lease op=" + operation + (leftArm ? " L" : " R"), - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - 0, - BleProtocol.buildFaceclawWakeControl(operation, 0), - ACK_TIMEOUT_MS, - -1, - leftArm - ); - } - - /** CFW mic_control write (settings field 103) targeted at one temple. */ - public OutboundMessage faceclawMicControl(byte[] record, String label, boolean leftArm) { - return new OutboundMessage( - "mic-control", - "mic control " + label + (leftArm ? " L" : " R"), - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - 0, - BleProtocol.buildFaceclawMicControl(record), - ACK_TIMEOUT_MS, - -1, - leftArm - ); - } - - public OutboundMessage faceclawWearQuery(boolean leftArm) { - return new OutboundMessage( - "wear-query-control", - "wear state query" + (leftArm ? " L" : " R"), - BleProtocol.SID_UI_SETTING, - BleProtocol.FLAG_REQUEST, - 0, - BleProtocol.buildFaceclawWakeControl(BleProtocol.FACECLAW_WEAR_OP_QUERY, 0), - ACK_TIMEOUT_MS, - -1, - leftArm - ); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/OutboundMessage.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/OutboundMessage.java deleted file mode 100644 index bfdcd5cf..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/OutboundMessage.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.faceclaw.app; -import java.util.Arrays; - -public final class OutboundMessage { - final String kind; - final String label; - final int sid; - final int flag; - int magic; - final byte[] message; - final int ackTimeoutMs; - final int tileIndex; - final boolean isLeftArmMessage; - - int cfwAckLenses, cfwRetries; - boolean cfwRetryPending; - final int cfwChecksum; - - int imageUpdateId; - int imageMessageNumber; - int imageMessageCount; - long sentAtMs; - long writeStartedAtMs; - long ackDeadlineAtMs; - byte[] ackPayload = new byte[0]; - Runnable onSent; - Runnable onAck; - Runnable onTimeout; - - OutboundMessage(String kind, String label, int sid, int flag, int magic, byte[] message, int ackTimeoutMs, int tileIndex, boolean isLeftArmMessage) { - this.kind = kind; - this.label = label; - this.sid = sid; - this.flag = flag; - this.magic = magic; - this.message = message == null ? new byte[0] : Arrays.copyOf(message, message.length); - this.cfwChecksum = sid == CfwTransport.SID ? CfwTransport.crc(this.message) : 0; - this.ackTimeoutMs = ackTimeoutMs; - this.tileIndex = tileIndex; - this.isLeftArmMessage = isLeftArmMessage; - } - - /** NACK names this attempt's stream/message sequence, even without a decoded size. */ - boolean acceptCfwAck(CfwTransport.Ack ack) { - if (ack == null || sid != CfwTransport.SID || magic != ack.streamId - || ack.messageId != 0 || (ack.lens != 1 && ack.lens != 2)) return false; - if (ack.nack) { - cfwRetryPending = true; - return false; - } - if (message.length != ack.size || cfwChecksum != ack.checksum) return false; - cfwAckLenses |= ack.lens; - return !cfwRetryPending && cfwAckLenses == CfwTransport.BOTH; - } - - /** Replace an attempt atomically under the communicator lock. */ - void prepareCfwReplay(int streamId) { - magic = streamId; - ++cfwRetries; - cfwAckLenses = 0; - cfwRetryPending = false; - ackDeadlineAtMs = 0; - ackPayload = new byte[0]; - } - - void setImageUpdatePosition(int updateId, int messageNumber, int messageCount) { - this.imageUpdateId = updateId; - this.imageMessageNumber = messageNumber; - this.imageMessageCount = messageCount; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/RingProtocol.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/RingProtocol.java deleted file mode 100644 index 04b04cfd..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/RingProtocol.java +++ /dev/null @@ -1,965 +0,0 @@ -package com.faceclaw.app; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; - -/** - * RingProtocol: frame build/parse plus per-record decoding for the Even R1 - * ring's health-data channel. Does _not_ include code for actually sending - * anything; for that, see FaceclawBleCommunicator (same split as BleProtocol). - * - *

Deliberately free of Android imports so the whole thing can be exercised - * with plain javac/java — see notes/ring-protocol-selftest/. - * - *

This is a different protocol from BleProtocol's. The glasses speak - * an {@code aa21} envelope with a CRC-16/CCITT; the ring speaks a 15-byte - * {@code 64 .. 64} header with a CRC-32C. Nothing is shared between them, and - * in particular {@link #crc32} must never be pointed at a glasses frame. - * - *

Wire format (offsets from the start of the ATT value): - *

- *  off size field
- *   0   1   FLAG      0x00 normally; 0x01 = fragmented, a continuation follows
- *   1   4   CRC32     little-endian, over bytes [5:] of the *reassembled* frame
- *   5   1   0x64      constant magic
- *   6   1   CHAN      0x01 device/system, 0x02 health data
- *   7   1   0x64      constant magic
- *   8   1   SEQ       phone-side: one counter shared by both channels
- *   9   1   0x00      constant
- *  10   1   KIND      0x00 REQ, 0x01 ACK, 0x02 DATA, 0x03 RSP
- *  11   1   CMD_HI
- *  12   1   CMD_LO
- *  13   2   PLEN      little-endian; equals (total frame length - 5)
- *  15  ..   PAYLOAD   PLEN - 10 bytes; PAYLOAD[0:2] is a free per-message nonce
- * 
- * - *

Protocol spec and its evidence: - * knowledge/staging/faceclaw-ring-protocol-decode-return.md in the TLC - * knowledge base. Everything below is implemented from that document; the - * places where it is explicitly *not* solved are marked UNSOLVED and are - * surfaced raw rather than guessed at. - */ -public final class RingProtocol { - private RingProtocol() { - } - - /** CRC-32C polynomial, MSB-first / non-reflected form. Not CRC-32 (0x04C11DB7). */ - private static final int CRC32C_POLY = 0x1EDC6F41; - - public static final int MAGIC = 0x64; - public static final int HEADER_LEN = 15; - /** A continuation frame is FLAG(1) + CRC32(4) and then raw remainder bytes. */ - public static final int CONTINUATION_PREFIX_LEN = 5; - - public static final int FLAG_PLAIN = 0x00; - public static final int FLAG_FRAGMENTED = 0x01; - - public static final int CHAN_DEVICE = 0x01; - public static final int CHAN_HEALTH = 0x02; - - public static final int KIND_REQ = 0x00; - public static final int KIND_ACK = 0x01; - public static final int KIND_DATA = 0x02; - public static final int KIND_RSP = 0x03; - - /** Every health record type uses CMD_LO = 0x01; the type lives in CMD_HI. */ - public static final int CMD_LO_HEALTH = 0x01; - public static final int CMD_HI_HEART_RATE = 0x01; - public static final int CMD_HI_SPO2 = 0x02; - public static final int CMD_HI_HRV = 0x04; - public static final int CMD_HI_STEPS = 0x05; - public static final int CMD_HI_SLEEP = 0x06; - - /** Device/system channel commands all have CMD_HI = 0x00. */ - public static final int CMD_HI_DEVICE = 0x00; - public static final int CMD_LO_PAGE_ACK = 0x7E; - - /** - * The five health pulls, in the order Even's own app issues them: - * heart rate, HRV, SpO2, sleep, steps. That order is identical in all four - * sync bursts of the reference capture. (It is NOT the order the spec - * document happens to list them in — that is just its presentation order.) - * Whether the order matters to the ring is untested; matching the app is - * the cheapest way not to find out the hard way. - */ - public static final int[] HEALTH_COMMANDS = { - CMD_HI_HEART_RATE, - CMD_HI_HRV, - CMD_HI_SPO2, - CMD_HI_SLEEP, - CMD_HI_STEPS, - }; - - /** Returned by anchor/timestamp accessors when the value is not known. */ - public static final long UNKNOWN_TIME = -1L; - - private static final byte[] EMPTY = new byte[0]; - - // ------------------------------------------------------------------ - // CRC - // ------------------------------------------------------------------ - - /** - * CRC-32C over {@code data[from, to)}: polynomial 0x1EDC6F41, MSB-first, - * init 0, no reflection, no final xor. Verified against 188/188 frames in - * the reference capture. - */ - public static int crc32(byte[] data, int from, int to) { - int crc = 0; - for (int i = from; i < to; i++) { - crc ^= (data[i] & 0xff) << 24; - for (int bit = 0; bit < 8; bit++) { - if ((crc & 0x80000000) != 0) { - crc = (crc << 1) ^ CRC32C_POLY; - } else { - crc = crc << 1; - } - } - } - return crc; - } - - public static int crc32(byte[] data) { - return crc32(data, 0, data == null ? 0 : data.length); - } - - // ------------------------------------------------------------------ - // Frame building - // ------------------------------------------------------------------ - - /** - * Build one complete, CRC-correct frame. Fragmentation is a receive-side - * concern only: nothing we send approaches the 244-byte ATT ceiling. - */ - public static byte[] buildFrame(int chan, int kind, int cmdHi, int cmdLo, int seq, byte[] payload) { - byte[] body = payload == null ? EMPTY : payload; - int total = HEADER_LEN + body.length; - byte[] frame = new byte[total]; - frame[0] = (byte) FLAG_PLAIN; - frame[5] = (byte) MAGIC; - frame[6] = (byte) (chan & 0xff); - frame[7] = (byte) MAGIC; - frame[8] = (byte) (seq & 0xff); - frame[9] = 0x00; - frame[10] = (byte) (kind & 0xff); - frame[11] = (byte) (cmdHi & 0xff); - frame[12] = (byte) (cmdLo & 0xff); - int plen = total - 5; - frame[13] = (byte) (plen & 0xff); - frame[14] = (byte) ((plen >>> 8) & 0xff); - System.arraycopy(body, 0, frame, HEADER_LEN, body.length); - writeIntLe(frame, 1, crc32(frame, 5, total)); - return frame; - } - - /** - * A request frame. Health requests carry no arguments at all: the payload - * is the 2-byte nonce and nothing else. - * - *

The nonce is a free per-message id — it is covered by the frame CRC but - * is not content-derived, so any value works. It is encoded little-endian. - */ - public static byte[] buildRequest(int chan, int cmdHi, int cmdLo, int seq, int nonce) { - return buildFrame(chan, KIND_REQ, cmdHi, cmdLo, seq, nonceBytes(nonce)); - } - - /** One of the five health pulls; see {@link #HEALTH_COMMANDS}. */ - public static byte[] buildHealthRequest(int cmdHi, int seq, int nonce) { - return buildRequest(CHAN_HEALTH, cmdHi, CMD_LO_HEALTH, seq, nonce); - } - - /** - * The per-page acknowledgement: command 00:7E on the DEVICE channel, - * echoing the sequence number of the DATA page being acknowledged. - * - *

Payload is 12 bytes: the usual 2-byte nonce, then - * {@code 02 00 }, then five zero bytes. (Note - * the echoed page seq is at payload offset 6, after the nonce — a spec doc - * that describes the payload as starting at {@code 02} is off by the nonce.) - * - *

The frame's own SEQ is the phone's shared counter, NOT the echoed page - * seq: across 20 ACKs in the reference capture the two are always different, - * and the header SEQ continues the same +1 sequence used by every other - * phone-to-ring write on both channels. - * - *

Whether the ring actually *requires* this is untested — Even's app - * sends one for every page without exception (20/20), but no page has ever - * been deliberately left unacknowledged to see what happens. - */ - public static byte[] buildPageAck(int seq, int nonce, int ackedCmdHi, int ackedCmdLo, int pageSeq) { - byte[] payload = new byte[12]; - payload[0] = (byte) (nonce & 0xff); - payload[1] = (byte) ((nonce >>> 8) & 0xff); - payload[2] = 0x02; - payload[3] = (byte) (ackedCmdHi & 0xff); - payload[4] = (byte) (ackedCmdLo & 0xff); - payload[5] = 0x00; - payload[6] = (byte) (pageSeq & 0xff); - // payload[7..11] stay zero. - return buildFrame(CHAN_DEVICE, KIND_ACK, CMD_HI_DEVICE, CMD_LO_PAGE_ACK, seq, payload); - } - - private static byte[] nonceBytes(int nonce) { - return new byte[] {(byte) (nonce & 0xff), (byte) ((nonce >>> 8) & 0xff)}; - } - - // ------------------------------------------------------------------ - // Frame parsing and fragment reassembly - // ------------------------------------------------------------------ - - /** True when a value could be the start of a frame (magic bytes in place). */ - public static boolean looksLikeHeader(byte[] value) { - return value != null - && value.length >= HEADER_LEN - && (value[5] & 0xff) == MAGIC - && (value[7] & 0xff) == MAGIC; - } - - /** Total frame length declared by PLEN, or -1 when this is not a header. */ - public static int declaredLength(byte[] value) { - if (!looksLikeHeader(value)) { - return -1; - } - int plen = (value[13] & 0xff) | ((value[14] & 0xff) << 8); - int total = plen + 5; - return total < HEADER_LEN ? -1 : total; - } - - /** Parse a complete (already reassembled) frame. Returns null if malformed. */ - public static Frame parse(byte[] frame) { - if (frame == null || frame.length < HEADER_LEN) { - return null; - } - if ((frame[5] & 0xff) != MAGIC || (frame[7] & 0xff) != MAGIC) { - return null; - } - int plen = (frame[13] & 0xff) | ((frame[14] & 0xff) << 8); - if (plen + 5 != frame.length) { - return null; - } - int storedCrc = readIntLe(frame, 1); - int actualCrc = crc32(frame, 5, frame.length); - byte[] payload = Arrays.copyOfRange(frame, HEADER_LEN, frame.length); - return new Frame( - frame[0] & 0xff, - frame[6] & 0xff, - frame[8] & 0xff, - frame[10] & 0xff, - frame[11] & 0xff, - frame[12] & 0xff, - payload, - frame, - storedCrc == actualCrc - ); - } - - /** - * Feeds raw ATT notification values in and produces complete frames, - * rejoining fragmented ones. - * - *

Fragmentation (spec §2.3): when a frame exceeds the 244-byte ATT - * ceiling the ring sets FLAG=0x01, sends the first 244 bytes, and later - * sends a continuation frame in a different, headerless shape — FLAG(1), - * CRC32(4), then the remaining bytes verbatim. - * - *

Two things the spec document does not say, both measured from the - * reference capture and both load-bearing here: - *

    - *
  • The continuation is not adjacent. In one of the six captured - * fragmentations three unrelated complete frames arrive between the - * first fragment and its continuation, so a "next value is the rest" - * rule mis-joins. Complete frames are therefore still parsed normally - * while a fragment is outstanding.
  • - *
  • The continuation's own CRC field is not reliably the reassembled - * CRC. It matched in five of six cases and differed in the sixth - * (whose join is nonetheless byte-exact and CRC-valid), so the - * continuation must not be matched by comparing CRC fields.
  • - *
- * - *

What is reliable, in all six cases, is length: a continuation carries - * exactly {@code CONTINUATION_PREFIX_LEN + bytesStillNeeded} bytes and does - * not itself look like a header. The reassembled frame's CRC is the real - * validator and is always checked. - */ - public static final class Reassembler { - /** - * Give up on an outstanding fragment after this many intervening - * complete frames. The observed worst case is 3; this is slack, not a - * measured bound. - */ - private static final int MAX_INTERLEAVED_FRAMES = 16; - - private byte[] pending; - private int pendingTotal; - private int interleaved; - - public boolean hasPending() { - return pending != null; - } - - public void reset() { - pending = null; - pendingTotal = 0; - interleaved = 0; - } - - /** - * Offer one ATT notification value. The result says whether the value - * belonged to this protocol at all (so the caller can fall through to - * the gesture decoder) and carries a frame once one is complete. - */ - public Intake accept(byte[] value) { - if (value == null || value.length == 0) { - return Intake.ignored(); - } - - if (pending != null) { - int needed = pendingTotal - pending.length; - if (value.length == CONTINUATION_PREFIX_LEN + needed && !looksLikeHeader(value)) { - byte[] joined = new byte[pendingTotal]; - System.arraycopy(pending, 0, joined, 0, pending.length); - System.arraycopy(value, CONTINUATION_PREFIX_LEN, joined, pending.length, needed); - reset(); - return complete(joined); - } - if (!looksLikeHeader(value)) { - int dropped = pendingTotal; - reset(); - return Intake.consumed(String.format( - Locale.US, - "dropped %d-byte fragment: continuation was %d bytes, expected %d", - dropped, value.length, CONTINUATION_PREFIX_LEN + needed)); - } - if (++interleaved > MAX_INTERLEAVED_FRAMES) { - int dropped = pendingTotal; - reset(); - // Fall through and parse this value as a fresh frame. - Intake result = acceptFresh(value); - return result.withNote(String.format( - Locale.US, "dropped %d-byte fragment: continuation never arrived", dropped)); - } - } - - return acceptFresh(value); - } - - private Intake acceptFresh(byte[] value) { - int total = declaredLength(value); - if (total < 0) { - return Intake.ignored(); - } - if (value.length >= total) { - return complete(Arrays.copyOf(value, total)); - } - // Only one fragment is ever outstanding in the reference capture, - // but never silently clobber one if that assumption breaks. - String clobbered = pending == null ? null : String.format( - Locale.US, "dropped %d-byte fragment: a new fragment started", pendingTotal); - pending = Arrays.copyOf(value, value.length); - pendingTotal = total; - interleaved = 0; - return Intake.consumed(String.format( - Locale.US, "fragment start, %d of %d bytes", value.length, total)).withNote(clobbered); - } - - private Intake complete(byte[] raw) { - Frame frame = parse(raw); - if (frame == null) { - return Intake.consumed("malformed frame, " + raw.length + " bytes"); - } - return Intake.frame(frame); - } - } - - /** The outcome of offering one ATT value to a {@link Reassembler}. */ - public static final class Intake { - /** True when the value belonged to this protocol and must not be re-handled. */ - public final boolean consumed; - /** Non-null once a complete frame is available. Check {@link Frame#crcOk}. */ - public final Frame frame; - /** Human-readable detail for logging, or null. */ - public final String note; - - private Intake(boolean consumed, Frame frame, String note) { - this.consumed = consumed; - this.frame = frame; - this.note = note; - } - - static Intake ignored() { - return new Intake(false, null, null); - } - - static Intake consumed(String note) { - return new Intake(true, null, note); - } - - static Intake frame(Frame frame) { - return new Intake(true, frame, null); - } - - Intake withNote(String extra) { - if (extra == null) { - return this; - } - return new Intake(consumed, frame, note == null ? extra : note + "; " + extra); - } - } - - /** One complete frame. */ - public static final class Frame { - public final int flag; - public final int chan; - public final int seq; - public final int kind; - public final int cmdHi; - public final int cmdLo; - /** PLEN - 10 bytes. payload[0:2] is the sender's nonce. */ - public final byte[] payload; - public final byte[] raw; - public final boolean crcOk; - - Frame(int flag, int chan, int seq, int kind, int cmdHi, int cmdLo, - byte[] payload, byte[] raw, boolean crcOk) { - this.flag = flag; - this.chan = chan; - this.seq = seq; - this.kind = kind; - this.cmdHi = cmdHi; - this.cmdLo = cmdLo; - this.payload = payload; - this.raw = raw; - this.crcOk = crcOk; - } - - public boolean isHealth() { - return chan == CHAN_HEALTH; - } - - public String commandLabel() { - return String.format(Locale.US, "%02x:%02x", cmdHi, cmdLo); - } - - public String describe() { - return String.format( - Locale.US, - "chan=%02x kind=%02x cmd=%s seq=%02x len=%d", - chan, kind, commandLabel(), seq, raw.length); - } - } - - // ------------------------------------------------------------------ - // Record decoding - // ------------------------------------------------------------------ - - /** - * Decode a health DATA page. Returns null when the frame is not a health - * DATA page or its body does not match the layout for its type. - */ - public static HealthRecord decode(Frame frame, long receivedAtMs) { - if (frame == null || !frame.crcOk || !frame.isHealth() || frame.kind != KIND_DATA) { - return null; - } - if (frame.cmdLo != CMD_LO_HEALTH) { - return null; - } - switch (frame.cmdHi) { - case CMD_HI_HEART_RATE: - case CMD_HI_SPO2: - case CMD_HI_HRV: - return decodeHourly(frame, receivedAtMs); - case CMD_HI_STEPS: - return decodeSteps(frame, receivedAtMs); - case CMD_HI_SLEEP: - return decodeSleep(frame, receivedAtMs); - default: - return null; - } - } - - /** - * Heart rate / SpO2 / HRV: an hourly {@code [index][avg][max][min]} series. - * - *

Layout follows openR1 r1_health_encode_daily_u8/u16 in r1_health.c: - * count, signed timezone minutes, day start, optional latest timestamp/value, - * then count groups. There is no footer. HR/SpO2 values are one byte; HRV - * values are two. Subtracting a supposed four-byte footer misdecoded a - * one-group HRV page as one-byte values and rejected real HR/SpO2 pages. - */ - public static HourlyRecord decodeHourly(Frame frame, long receivedAtMs) { - byte[] pay = frame.payload; - if (pay.length < 9) { - return null; - } - int count = pay[2] & 0xff; - long anchor = anchorUnixSeconds(pay); - int width = frame.cmdHi == CMD_HI_HRV ? 2 : 1; - int groupBytes = count * (1 + 3 * width); - int prefixLength = pay.length - groupBytes; - boolean hasLatest = prefixLength == 13 + width; - if (count > 24 || (!hasLatest && prefixLength != 9)) return null; - long tag = hasLatest ? readUInt32Le(pay, 9) : UNKNOWN_TIME; - int current = hasLatest ? readUIntLe(pay, 13, width) : -1; - int offset = prefixLength; - HourlyGroup[] groups = new HourlyGroup[count]; - for (int i = 0; i < count; i++) { - int hourIndex = pay[offset] & 0xff; - if (hourIndex >= 24) return null; - offset++; - int avg = readUIntLe(pay, offset, width); - offset += width; - int max = readUIntLe(pay, offset, width); - offset += width; - int min = readUIntLe(pay, offset, width); - offset += width; - long unix = anchor == UNKNOWN_TIME ? UNKNOWN_TIME : anchor + hourIndex * 3600L; - groups[i] = new HourlyGroup(hourIndex, avg, max, min, unix); - } - return new HourlyRecord(frame.cmdHi, receivedAtMs, anchor, tag, current, width, groups); - } - - /** - * Steps / activity buckets: {@code [index][v1][v2][v3]} after the record - * prefix. v1 (steps) is proven against a full-day total; v2 and v3 are - * calorie-shaped but unconfirmed, and the bucket index does NOT map to a - * known wall-clock time (see {@link StepsBucket}). - */ - public static StepsRecord decodeSteps(Frame frame, long receivedAtMs) { - byte[] pay = frame.payload; - if (pay.length < 9) { - return null; - } - int count = pay[2] & 0xff; - if (count > 144 || pay.length != 9 + count * 7) { - return null; - } - long anchor = anchorUnixSeconds(pay); - int offset = 9; - StepsBucket[] buckets = new StepsBucket[count]; - for (int i = 0; i < count; i++) { - int index = pay[offset] & 0xff; - if (index >= 144) return null; - int steps = readUInt16Le(pay, offset + 1); - int v2 = readUInt16Le(pay, offset + 3); - int v3 = readUInt16Le(pay, offset + 5); - offset += 7; - buckets[i] = new StepsBucket(index, steps, v2, v3); - } - return new StepsRecord(receivedAtMs, anchor, buckets); - } - - /** - * A sleep session. The ring transmits pre-classified stages, per-stage - * totals and the session boundaries; nothing is computed phone-side. - * - *

{@code startTs}/{@code endTs} are ring-relative seconds, NOT Unix - * timestamps — see {@link SleepRecord}. - */ - public static SleepRecord decodeSleep(Frame frame, long receivedAtMs) { - byte[] pay = frame.payload; - if (pay.length < 3) { - return null; - } - int recordState = pay[2] & 0xff; - byte[] unknownA = safeRange(pay, 3, 9); - long unknownTag = pay.length >= 13 ? readUInt32Le(pay, 9) : UNKNOWN_TIME; - if (recordState != 1) { - // RECSTATE 2 is the empty / end-of-list marker. - return new SleepRecord( - receivedAtMs, recordState, unknownA, unknownTag, - 0, 0, 0, 0, 0, 0, 0, new SleepSegment[0]); - } - if (pay.length < 34) return null; - - long startTs = readUInt32Le(pay, 14); - long endTs = readUInt32Le(pay, 18); - int totalTime = readUInt16Le(pay, 22); - int wakeTime = readUInt16Le(pay, 24); - int remTime = readUInt16Le(pay, 26); - int lightTime = readUInt16Le(pay, 28); - int deepTime = readUInt16Le(pay, 30); - int segmentCount = readUInt16Le(pay, 32); - if (pay.length != 34 + segmentCount * 3) { - return null; - } - SleepSegment[] segments = new SleepSegment[segmentCount]; - int offset = 34; - for (int i = 0; i < segmentCount; i++) { - int stage = pay[offset] & 0xff; - int halfMinutes = readUInt16Le(pay, offset + 1); - offset += 3; - segments[i] = new SleepSegment(stage, halfMinutes); - } - return new SleepRecord( - receivedAtMs, recordState, unknownA, unknownTag, - startTs, endTs, totalTime, wakeTime, remTime, lightTime, deepTime, segments); - } - - /** - * The record's day anchor as Unix epoch seconds, or {@link #UNKNOWN_TIME} - * when the day timestamp is zero or the timezone offset is invalid. - * - *

UNSOLVED: a backlog page's true base is not understood as a - * general rule. One capture's backlog base landed at 2026-09-08 23:48:49 - * local, which is the moment that sync ran plus a per-type offset — it is - * not a fixed offset from midnight and must not be hardcoded. Callers get - * {@link #UNKNOWN_TIME} and the raw hour index instead of an invented - * absolute time. - */ - public static long anchorUnixSeconds(byte[] payload) { - if (payload == null || payload.length < 9) { - return UNKNOWN_TIME; - } - // These two bytes are an int16 timezone offset, not magic. 10 ff is - // -240 minutes (EDT); Pacific time, UTC and eastern zones differ. - int timezoneMinutes = (short) readUInt16Le(payload, 3); - long anchor = readUInt32Le(payload, 5); - return timezoneMinutes >= -840 && timezoneMinutes <= 840 && anchor != 0 - ? anchor : UNKNOWN_TIME; - } - - /** Device-channel 00:01 response, matching openR1 get_device_status(). */ - public static DeviceStatus decodeDeviceStatus(Frame frame) { - if (frame == null || !frame.crcOk || frame.chan != CHAN_DEVICE - || frame.kind != KIND_RSP || frame.cmdHi != 0 || frame.cmdLo != 1 - || frame.payload.length != 9) return null; - int battery = frame.payload[2] & 0xff; - int charge = frame.payload[3] & 0xff; - if (battery > 100 || charge > 3) return null; - return new DeviceStatus(battery, charge == 0 ? -1 : charge == 2 ? 0 : 1); - } - - public static final class DeviceStatus { - public final int battery; - public final int charging; - DeviceStatus(int battery, int charging) { - this.battery = battery; - this.charging = charging; - } - } - - // ------------------------------------------------------------------ - // Decoded record types - // ------------------------------------------------------------------ - - /** Common supertype so one store can hold every decoded page. */ - public abstract static class HealthRecord { - public final int cmdHi; - public final long receivedAtMs; - - HealthRecord(int cmdHi, long receivedAtMs) { - this.cmdHi = cmdHi; - this.receivedAtMs = receivedAtMs; - } - - /** The metric name, for logs. */ - public String metric() { - switch (cmdHi) { - case CMD_HI_HEART_RATE: return "heart_rate"; - case CMD_HI_SPO2: return "spo2"; - case CMD_HI_HRV: return "hrv"; - case CMD_HI_STEPS: return "steps"; - case CMD_HI_SLEEP: return "sleep"; - default: return String.format(Locale.US, "cmd%02x", cmdHi); - } - } - - /** A one-line summary safe to put in a log. */ - public abstract String summary(); - } - - /** Heart rate, SpO2 or HRV: one page of hourly buckets. */ - public static final class HourlyRecord extends HealthRecord { - /** Day anchor in Unix seconds, or {@link #UNKNOWN_TIME} on a backlog page. */ - public final long anchorUnixSeconds; - /** Per-metric "last measured at". See the 4-hour skew note in the spec. */ - public final long tagRaw; - /** The metric's latest value. */ - public final int current; - /** 1 byte for HR/SpO2, 2 for HRV, as defined by the metric serializer. */ - public final int valueWidth; - public final HourlyGroup[] groups; - - HourlyRecord(int cmdHi, long receivedAtMs, long anchorUnixSeconds, long tagRaw, - int current, int valueWidth, HourlyGroup[] groups) { - super(cmdHi, receivedAtMs); - this.anchorUnixSeconds = anchorUnixSeconds; - this.tagRaw = tagRaw; - this.current = current; - this.valueWidth = valueWidth; - this.groups = groups; - } - - /** True when this page has no anchor, i.e. it is a backlog page. */ - public boolean isBacklog() { - return anchorUnixSeconds == UNKNOWN_TIME; - } - - @Override public String summary() { - return String.format( - Locale.US, - "%s groups=%d width=%d current=%d anchor=%s tag=%d", - metric(), groups.length, valueWidth, current, - isBacklog() ? "backlog(unanchored)" : Long.toString(anchorUnixSeconds), - tagRaw); - } - } - - /** One hourly bucket. */ - public static final class HourlyGroup { - /** Raw index as sent. On an anchored page this is hours since the anchor. */ - public final int hourIndex; - public final int avg; - public final int max; - public final int min; - /** - * Absolute Unix seconds for this bucket, or {@link #UNKNOWN_TIME} on a - * backlog page — where the base is UNSOLVED and deliberately not guessed. - */ - public final long unixSeconds; - - HourlyGroup(int hourIndex, int avg, int max, int min, long unixSeconds) { - this.hourIndex = hourIndex; - this.avg = avg; - this.max = max; - this.min = min; - this.unixSeconds = unixSeconds; - } - } - - /** One page of activity buckets. */ - public static final class StepsRecord extends HealthRecord { - public final long anchorUnixSeconds; - public final StepsBucket[] buckets; - - StepsRecord(long receivedAtMs, long anchorUnixSeconds, StepsBucket[] buckets) { - super(CMD_HI_STEPS, receivedAtMs); - this.anchorUnixSeconds = anchorUnixSeconds; - this.buckets = buckets; - } - - public int totalSteps() { - int total = 0; - for (StepsBucket bucket : buckets) { - total += bucket.steps; - } - return total; - } - - @Override public String summary() { - return String.format( - Locale.US, - "steps buckets=%d total=%d anchor=%s", - buckets.length, totalSteps(), - anchorUnixSeconds == UNKNOWN_TIME ? "none" : Long.toString(anchorUnixSeconds)); - } - } - - /** - * One activity bucket. - * - *

UNSOLVED: {@link #index} does not map to a known wall-clock - * time. It runs 0-7, 10, 11, 13-34 and then jumps to 130+, and no bucket - * width at any base reproduces the app's own per-bucket distribution — even - * though the steps total across all buckets is exactly right. The raw index - * is stored as sent; no time-of-day mapping is invented. - */ - public static final class StepsBucket { - public final int index; - /** Confirmed: summed over a page this equals the app's daily step total. */ - public final int steps; - /** UNCONFIRMED: calorie-shaped (v3 - v2 tracks per-bucket resting kcal). */ - public final int calorieLike2; - /** UNCONFIRMED: calorie-shaped. */ - public final int calorieLike3; - - StepsBucket(int index, int steps, int calorieLike2, int calorieLike3) { - this.index = index; - this.steps = steps; - this.calorieLike2 = calorieLike2; - this.calorieLike3 = calorieLike3; - } - } - - /** - * One sleep session. - * - *

{@link #startTs} and {@link #endTs} are ring-relative seconds, not - * Unix time. Even's own app gets this wrong and stamps live-pushed - * sessions with timestamps in 1979. Do not feed them to a date formatter. - * - *

UNSOLVED: {@link #unknownPrefix} (6 bytes) and - * {@link #unknownTag} (u32) are not decoded; one of them probably carries - * the session date, which would remove the anchoring problem. They are - * stored raw rather than interpreted. - */ - public static final class SleepRecord extends HealthRecord { - /** 1 = a real record, 2 = the empty / end-of-list marker. */ - public final int recordState; - /** UNSOLVED per-record 6-byte field (payload[3:9]). Stored raw. */ - public final byte[] unknownPrefix; - /** UNSOLVED u32 tag (payload[9:13]). Stored raw. */ - public final long unknownTag; - /** Ring-relative seconds, NOT Unix. */ - public final long startTs; - /** Ring-relative seconds, NOT Unix. */ - public final long endTs; - public final int totalTime; - public final int wakeTime; - public final int remTime; - public final int lightTime; - public final int deepTime; - /** - * The stage series. Stage ids are the same 0-3 encoding the app's own - * export uses, but which id means wake/rem/light/deep is NOT stated by - * the spec and is deliberately not guessed here — see - * {@link #halfMinutesForStage}. - */ - public final SleepSegment[] segments; - - SleepRecord(long receivedAtMs, int recordState, byte[] unknownPrefix, long unknownTag, - long startTs, long endTs, int totalTime, int wakeTime, int remTime, - int lightTime, int deepTime, SleepSegment[] segments) { - super(CMD_HI_SLEEP, receivedAtMs); - this.recordState = recordState; - this.unknownPrefix = unknownPrefix; - this.unknownTag = unknownTag; - this.startTs = startTs; - this.endTs = endTs; - this.totalTime = totalTime; - this.wakeTime = wakeTime; - this.remTime = remTime; - this.lightTime = lightTime; - this.deepTime = deepTime; - this.segments = segments; - } - - public boolean isRealRecord() { - return recordState == 1; - } - - public int totalHalfMinutes() { - int total = 0; - for (SleepSegment segment : segments) { - total += segment.halfMinutes; - } - return total; - } - - public int halfMinutesForStage(int stage) { - int total = 0; - for (SleepSegment segment : segments) { - if (segment.stage == stage) { - total += segment.halfMinutes; - } - } - return total; - } - - /** - * The two whole-record arithmetic identities from the spec, which held - * exactly for all ten real records in the reference capture. A false - * result means the body was mis-parsed. - */ - public boolean identitiesHold() { - if (!isRealRecord()) { - return true; - } - int seconds = totalHalfMinutes() * 30; - return seconds == totalTime + wakeTime && seconds == (int) (endTs - startTs); - } - - @Override public String summary() { - if (!isRealRecord()) { - return String.format(Locale.US, "sleep marker state=%d", recordState); - } - return String.format( - Locale.US, - "sleep segments=%d total=%ds wake=%d rem=%d light=%d deep=%d relStart=%d relEnd=%d identities=%s", - segments.length, totalTime, wakeTime, remTime, lightTime, deepTime, - startTs, endTs, identitiesHold()); - } - } - - /** One stage run: a stage id and a duration in half-minutes (30 s units). */ - public static final class SleepSegment { - public final int stage; - public final int halfMinutes; - - SleepSegment(int stage, int halfMinutes) { - this.stage = stage; - this.halfMinutes = halfMinutes; - } - } - - // ------------------------------------------------------------------ - // Little-endian helpers - // ------------------------------------------------------------------ - - private static void writeIntLe(byte[] out, int offset, int value) { - out[offset] = (byte) (value & 0xff); - out[offset + 1] = (byte) ((value >>> 8) & 0xff); - out[offset + 2] = (byte) ((value >>> 16) & 0xff); - out[offset + 3] = (byte) ((value >>> 24) & 0xff); - } - - private static int readIntLe(byte[] data, int offset) { - return (data[offset] & 0xff) - | ((data[offset + 1] & 0xff) << 8) - | ((data[offset + 2] & 0xff) << 16) - | ((data[offset + 3] & 0xff) << 24); - } - - private static long readUInt32Le(byte[] data, int offset) { - return readIntLe(data, offset) & 0xffffffffL; - } - - private static int readUInt16Le(byte[] data, int offset) { - return (data[offset] & 0xff) | ((data[offset + 1] & 0xff) << 8); - } - - private static int readUIntLe(byte[] data, int offset, int width) { - int value = 0; - for (int i = 0; i < width; i++) { - value |= (data[offset + i] & 0xff) << (8 * i); - } - return value; - } - - private static byte[] safeRange(byte[] data, int from, int to) { - if (data == null || from >= data.length) { - return EMPTY; - } - return Arrays.copyOfRange(data, from, Math.min(to, data.length)); - } - - /** Hex, for logs. Mirrors FaceclawBleCommunicator.hex(). */ - public static String hex(byte[] data) { - if (data == null || data.length == 0) { - return ""; - } - char[] digits = "0123456789abcdef".toCharArray(); - char[] out = new char[data.length * 2]; - for (int i = 0; i < data.length; i++) { - int value = data[i] & 0xff; - out[i * 2] = digits[value >>> 4]; - out[i * 2 + 1] = digits[value & 0x0f]; - } - return new String(out); - } - - /** The five health requests, ready to write, sharing one ascending seq run. */ - public static List buildHealthRequestBurst(int firstSeq, int firstNonce) { - List frames = new ArrayList<>(HEALTH_COMMANDS.length); - int seq = firstSeq; - int nonce = firstNonce; - for (int cmdHi : HEALTH_COMMANDS) { - frames.add(buildHealthRequest(cmdHi, seq, nonce)); - seq = (seq + 1) & 0xff; - nonce = (nonce + 1) & 0xffff; - } - return frames; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java deleted file mode 100644 index 23cebc1d..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java +++ /dev/null @@ -1,179 +0,0 @@ -package com.faceclaw.app; - -import java.io.ByteArrayOutputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Phone-side model of the CFW's flat 256 KiB texture cache (zlib_glue mode 18). - * The firmware holds only bytes; every structural decision — where font - * tables and glyph images live, what is resident, when to start over — is - * made here. Layout per font: a 96-entry uint32 offset table (chars 32..127) - * whose entries are written lazily as each glyph is uploaded; glyph images - * are the GlyphAtlas cached bytes ([w][h][4bpp RLE]). - * - * Allocation is a bump pointer with reset-on-full: the realistic working set - * (a few fonts' tables plus ~hundreds of glyphs at tens of bytes each) is far - * below 256 KiB, so eviction sophistication buys nothing. After a reset the - * firmware cache still holds stale bytes; that is safe because a draw only - * ever references (font table entry, glyph image) pairs uploaded in the - * current generation — stale table entries are never named in a mode-20 - * string. - * - * Upload entries accumulate as complete [offset u32][len u16][data] records - * and drain into one or more standalone mode-18 payloads, each independently - * valid, enqueued ahead of the image message that references them (the - * transport is FIFO, so no ack round-trip is needed before use). - * - * Not thread-safe by itself: the communicator calls it under its own lock. - */ -public final class TextureCacheState { - public static final int CACHE_SIZE = 262144; - private static final int FONT_TABLE_BYTES = 96 * 4; - - private int allocEnd = 0; - private final Map fontTableOffsets = new HashMap<>(); - private final Map glyphOffsets = new HashMap<>(); - private final Map imageOffsets = new HashMap<>(); - private final List pendingEntries = new ArrayList<>(); - private int pendingBytes = 0; - private int generation = 0; - - /** - * Forget everything: the on-glasses cache is gone (session teardown, lease - * loss) or no longer trustworthy (upload timeout), or we ran out of space. - * Glyphs and images re-upload lazily on next use. - */ - public void reset() { - allocEnd = 0; - fontTableOffsets.clear(); - glyphOffsets.clear(); - imageOffsets.clear(); - pendingEntries.clear(); - pendingBytes = 0; - generation++; - } - - /** Bumped by every reset; lets a planner detect a mid-plan reset. */ - public int generation() { - return generation; - } - - /** - * Bytes currently allocated out of CACHE_SIZE, for logging/tuning (a - * sudden drop in the frame log means a reset-on-full re-upload cycle — - * the signal that the 256 KiB budget is being outgrown). - */ - public int usedBytes() { - return allocEnd; - } - - /** The font's 96-entry table offset in the cache, or -1 when it exists but this glyph can't fit. */ - public int fontTableOffset(int fontId) { - Integer existing = fontTableOffsets.get(fontId); - return existing == null ? -1 : existing; - } - - /** - * Ensure a glyph image (and its font table slot) is resident, queuing the - * mode-18 upload entries for anything newly placed. Returns the glyph's - * image offset, or -1 when the cache is full (caller resets and retries) - * or the encoding is outside the mode-20 table (32..127). - */ - public int ensureGlyph(int fontId, int encoding, GlyphAtlas.Glyph glyph) { - if (glyph == null || encoding < 32 || encoding > 127) return -1; - long key = ((long) fontId << 32) | (encoding & 0xffffffffL); - Integer existing = glyphOffsets.get(key); - if (existing != null) return existing; - - Integer table = fontTableOffsets.get(fontId); - int need = glyph.cachedBytes.length + (table == null ? FONT_TABLE_BYTES : 0); - if (allocEnd + need > CACHE_SIZE) return -1; - - if (table == null) { - table = allocEnd; - allocEnd += FONT_TABLE_BYTES; - fontTableOffsets.put(fontId, table); - // No upload for the table itself: entries are written per glyph, - // and only uploaded entries are ever referenced by a draw. - } - int offset = allocEnd; - allocEnd += glyph.cachedBytes.length; - glyphOffsets.put(key, offset); - - addEntry(offset, glyph.cachedBytes); - addEntry(table + 4 * (encoding - 32), - new byte[]{(byte) offset, (byte) (offset >> 8), - (byte) (offset >> 16), (byte) (offset >> 24)}); - return offset; - } - - /** - * Ensure an ImageAtlas entry is resident (mode-19 draws reference it by - * raw cache offset; no table involved). Returns the image offset, or -1 - * when the cache is full (caller resets and retries). - */ - public int ensureImage(int imageId, ImageAtlas.Entry image) { - if (image == null) return -1; - Integer existing = imageOffsets.get(imageId); - if (existing != null) return existing; - if (allocEnd + image.cachedBytes.length > CACHE_SIZE) return -1; - int offset = allocEnd; - allocEnd += image.cachedBytes.length; - imageOffsets.put(imageId, offset); - addEntry(offset, image.cachedBytes); - return offset; - } - - private void addEntry(int offset, byte[] data) { - byte[] entry = new byte[6 + data.length]; - entry[0] = (byte) (offset & 0xff); - entry[1] = (byte) ((offset >> 8) & 0xff); - entry[2] = (byte) ((offset >> 16) & 0xff); - entry[3] = (byte) ((offset >> 24) & 0xff); - entry[4] = (byte) (data.length & 0xff); - entry[5] = (byte) ((data.length >> 8) & 0xff); - System.arraycopy(data, 0, entry, 6, data.length); - pendingEntries.add(entry); - pendingBytes += entry.length; - } - - public boolean hasPendingUploads() { - return !pendingEntries.isEmpty(); - } - - /** Total queued upload bytes (entry framing included), for logging. */ - public int pendingUploadBytes() { - return pendingBytes; - } - - /** - * Drain the queued entries into complete mode-18 payloads ([18][entries]), - * each at most maxPayloadBytes so a payload fits one BLE image message - * without fragmentation. A single entry larger than the cap still gets its - * own payload (the transport can fragment it). - */ - public List drainUploadPayloads(int maxPayloadBytes) { - List payloads = new ArrayList<>(); - ByteArrayOutputStream current = null; - for (byte[] entry : pendingEntries) { - if (current != null && current.size() + entry.length > maxPayloadBytes) { - payloads.add(current.toByteArray()); - current = null; - } - if (current == null) { - current = new ByteArrayOutputStream(Math.min(maxPayloadBytes, 4 + entry.length)); - current.write(18); - } - current.write(entry, 0, entry.length); - } - if (current != null) { - payloads.add(current.toByteArray()); - } - pendingEntries.clear(); - pendingBytes = 0; - return payloads; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java deleted file mode 100644 index ac700cc2..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java +++ /dev/null @@ -1,721 +0,0 @@ -package com.faceclaw.app; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * Plans a screen update that ships text and icons as on-glasses cached draws - * instead of pixels (CFW modes 18/19/20; see g2flash/patches/zlib_glue.c and - * texture_cache.c). - * - * Approach: dirty rects come from comparing the fully composited old and new - * frames, exactly as the plain incremental path does. For each deferred draw - * (glyph or image) whose pixels intersect a sent rect, check that every pixel - * the on-glasses draw would write lands correct — i.e. the new composite's - * 4bpp value at each in-panel nonzero-source pixel equals what the draw - * produces (this one check subsumes plane occlusion, surface - * occlusion/clipping, and draw-on-draw overlap). A draw that passes and is - * (or can be made) resident in the texture cache is replayed on-glasses: its - * written pixels are punched to 0 in the delta rect content — which is what - * makes text and icons nearly free to compress — and re-emitted as a mode-20 - * string or mode-19 image sub-message in the same atomic mode-8 batch, so - * the shadow after apply equals the full composite exactly. Everything else - * stays baked. - * - * Fallback shape: when nothing qualifies, plan() returns null and the caller - * uses the plain single-bbox/multi-rect/full-frame paths unchanged. - */ -public final class TexturePlanner { - private TexturePlanner() {} - - /** Cap on mode-20 string sub-messages (mode-8 count is a u8). */ - private static final int MAX_GLYPH_RUNS = 180; - /** - * Soft cap on cached-glyph selection per update, before run grouping. - * Sized above a full-screen 6x12 terminal repaint (~4,240 cells) so dense - * screens draw everything; the run budget is the real bound. - */ - private static final int MAX_SELECTED_GLYPHS = 4600; - /** Cap on mode-19 image sub-messages per update. */ - private static final int MAX_IMAGE_DRAWS = 60; - /** Cap on mode-15 builtin-font string sub-messages per update. */ - private static final int MAX_FWTEXT_RUNS = 80; - /** Mode-18 payload size that fits one BLE image message comfortably. */ - private static final int UPLOAD_PAYLOAD_MAX = 3600; - /** Max x-adjust control bytes between two glyphs before starting a new run. */ - private static final int MAX_ADJUST_BYTES = 4; - /** Options: identity LUT (top 15) + transparent, for mode-19 image draws. */ - private static final int IMAGE_DRAW_OPTIONS = 0x1f; - - public static final class Result { - /** Complete image payload: a mode-8 batch of rect deltas + cached draws. */ - public final byte[] payload; - /** Mode-18 upload payloads to enqueue BEFORE the image message. */ - public final List uploads; - /** Next mode-3 frame id (deltas consumed some). */ - public final int nextFid; - public final int rectCount; - public final int drawnGlyphs; - public final int runCount; - public final int drawnImages; - public final int fwGlyphs; - public final int fwRuns; - public final int bakedCandidates; - public final int uploadBytes; - public final boolean fullFrame; - /** - * Where the planning time went, in ms. This runs on the send thread, - * directly in the input-to-display path, so the split is reported in - * the frame log: rects = finding the changed region, match = checking - * each draw against the composite, cache = residency/uploads plus run - * building, punch = clearing replayed draws out of a copy of the - * frame, encode = compressing the leftover pixels. - */ - public int rectsMs; - public int matchMs; - public int cacheMs; - public int punchMs; - public int encodeMs; - - Result(byte[] payload, List uploads, int nextFid, int rectCount, - int drawnGlyphs, int runCount, int drawnImages, int fwGlyphs, int fwRuns, - int bakedCandidates, int uploadBytes, boolean fullFrame) { - this.payload = payload; - this.uploads = uploads; - this.nextFid = nextFid; - this.rectCount = rectCount; - this.drawnGlyphs = drawnGlyphs; - this.runCount = runCount; - this.drawnImages = drawnImages; - this.fwGlyphs = fwGlyphs; - this.fwRuns = fwRuns; - this.bakedCandidates = bakedCandidates; - this.uploadBytes = uploadBytes; - this.fullFrame = fullFrame; - } - } - - /** One draw selected for on-glasses replay. */ - private static final class Selected { - final SurfaceCompositor.ScreenDraw draw; - final GlyphAtlas.Glyph glyphAtlas; // glyph draws - final ImageAtlas.Entry imageAtlas; // image draws - final int gx; // screen x of the cached image (pen + bearing / blit left) - final int top; // glyph draws: 4bpp top color - - Selected(SurfaceCompositor.ScreenDraw draw, GlyphAtlas.Glyph glyphAtlas, - ImageAtlas.Entry imageAtlas, int gx, int top) { - this.draw = draw; - this.glyphAtlas = glyphAtlas; - this.imageAtlas = imageAtlas; - this.gx = gx; - this.top = top; - } - } - - /** - * Plan a cached-draw update. previous is the delta base (the frame the - * shadow currently holds), or null/mismatched for a full-frame keyframe. - * Returns null when the plain paths should run instead (no replayable - * draws, or identical frames). - */ - public static Result plan( - byte[] previous, byte[] next, int width, int height, - SurfaceCompositor.ScreenDraw[] draws, - TextureCacheState cache, int fidStart, - boolean allowMultiRect, int maxRects) { - if (next == null || draws == null || draws.length == 0 || width <= 0 || height <= 0) { - return null; - } - int stride = (width + 1) >> 1; - if (next.length != stride * height) { - return null; - } - long planStartedAtMs = android.os.SystemClock.elapsedRealtime(); - - // Rect geometry, matching what the plain paths would send. - boolean fullFrame = previous == null || previous.length != next.length; - List rects; - if (!fullFrame) { - int[] box = BleImageOptimizer.computeChangedBox(previous, next, width, height); - if (box == null) { - return null; // identical frames; caller's dedup normally catches this - } - if (box[2] >= width && box[3] >= height) { - fullFrame = true; - rects = Collections.singletonList(new int[]{0, 0, width, height}); - } else { - List split = allowMultiRect - ? BleImageOptimizer.computeChangedRects(previous, next, width, height, maxRects) - : null; - rects = split != null ? split : Collections.singletonList(box); - } - } else { - rects = Collections.singletonList(new int[]{0, 0, width, height}); - } - - long matchStartedAtMs = android.os.SystemClock.elapsedRealtime(); - - // Candidates: draws whose written pixels intersect a sent rect, are - // representable on the wire, and land correct against the composite. - List selected = new ArrayList<>(); - int bakedCandidates = 0; - int selectedImages = 0; - // Builtin-font (mode 15) runs: no cache residency, no uploads — the - // firmware owns the font. Processed first because it has no cache - // side effects; the counters feed the shared stats. - List fwSubs = new ArrayList<>(); - List fwPunches = new ArrayList<>(); - int fwGlyphCount = 0; - int fwBaked = 0; - for (SurfaceCompositor.ScreenDraw draw : draws) { - if (draw.kind != SurfaceCompositor.ScreenDraw.KIND_FWTEXT) continue; - fwBaked += planFwRun(draw, rects, next, stride, width, height, fwSubs, fwPunches); - } - fwGlyphCount = fwPunches.size(); - bakedCandidates += fwBaked; - for (SurfaceCompositor.ScreenDraw draw : draws) { - if (draw.kind == SurfaceCompositor.ScreenDraw.KIND_GLYPH) { - GlyphAtlas.Glyph atlas = GlyphAtlas.get(draw.fontId, draw.encoding); - if (atlas == null) continue; // unregistered: stays baked, not a "candidate" - int gx = draw.x + atlas.bbxX; - int inkTop = draw.y + atlas.inkTop; - if (!intersectsAny(rects, gx, inkTop, atlas.width, atlas.inkHeight)) { - continue; - } - if (draw.encoding < 32 || draw.encoding > 127 - || gx < 0 || draw.y < 0 || gx > 0xffff || draw.y > 0xffff) { - bakedCandidates++; - continue; - } - int top = BmpUtil.nibbleForGray(draw.value); - if (!glyphMatchesComposite(next, stride, width, height, atlas, gx, draw.y, top)) { - bakedCandidates++; - continue; - } - if (selected.size() >= MAX_SELECTED_GLYPHS) { - bakedCandidates++; - continue; - } - selected.add(new Selected(draw, atlas, null, gx, top)); - } else { - ImageAtlas.Entry atlas = ImageAtlas.get(draw.imageId); - if (atlas == null) continue; - if (!intersectsAny(rects, draw.x, draw.y, atlas.width, atlas.height)) { - continue; - } - if (draw.x < 0 || draw.y < 0 || draw.x > 0xffff || draw.y > 0xffff - || selectedImages >= MAX_IMAGE_DRAWS) { - bakedCandidates++; - continue; - } - if (!imageMatchesComposite(next, stride, width, height, atlas, draw.x, draw.y)) { - bakedCandidates++; - continue; - } - selected.add(new Selected(draw, null, atlas, draw.x, 0)); - selectedImages++; - } - } - if (selected.isEmpty() && fwSubs.isEmpty()) { - return null; - } - long cacheStartedAtMs = android.os.SystemClock.elapsedRealtime(); - - // Residency: ensure every selected draw is in the cache, retrying once - // after a reset when the bump allocator fills. A draw that still fails - // falls back to baked. IMPORTANT: once ensure* has run, every bail-out - // to the plain path must cache.reset() — ensure* marked entries - // resident and queued uploads that only a returned Result would - // enqueue. (Fw-text needs no residency, so a fw-only plan skips this.) - List drawable = selected.isEmpty() - ? Collections.emptyList() - : ensureResident(cache, selected); - bakedCandidates += selected.size() - drawable.size(); - - // Group glyphs into mode-20 runs; leftover-budget glyphs bake. - List glyphDrawable = new ArrayList<>(); - List imageDrawable = new ArrayList<>(); - for (Selected sel : drawable) { - if (sel.glyphAtlas != null) glyphDrawable.add(sel); - else imageDrawable.add(sel); - } - List runs = new ArrayList<>(); - List drawnGlyphs = buildRuns(cache, glyphDrawable, runs); - bakedCandidates += glyphDrawable.size() - drawnGlyphs.size(); - List drawn = new ArrayList<>(drawnGlyphs); - drawn.addAll(imageDrawable); - if ((drawn.isEmpty() && fwSubs.isEmpty()) - || rects.size() + runs.size() + imageDrawable.size() + fwSubs.size() > 255) { - if (!selected.isEmpty()) { - cache.reset(); - } - return null; - } - - // Punch the drawn pixels out of a copy of the composite, then encode - // the rect deltas from the punched content. - long punchStartedAtMs = android.os.SystemClock.elapsedRealtime(); - byte[] punched = next.clone(); - for (Selected sel : drawn) { - punch(punched, stride, width, height, sel); - } - for (FwPunch fw : fwPunches) { - punchFw(punched, stride, width, height, fw); - } - - long encodeStartedAtMs = android.os.SystemClock.elapsedRealtime(); - List subs = new ArrayList<>(); - int fid = fidStart; - if (fullFrame) { - subs.add(BleImageOptimizer.maybeCompress(punched, width, height)); - } else { - for (int[] r : rects) { - subs.add(BleImageOptimizer.encodeMode3Rect(punched, stride, r[0], r[1], r[2], r[3], fid)); - fid = (fid >= 0xfffe) ? 1 : fid + 1; - } - } - for (Selected sel : imageDrawable) { - subs.add(encodeImageDraw(cache, sel)); - } - subs.addAll(runs); - subs.addAll(fwSubs); - byte[] payload = assembleMode8(subs); - - int uploadBytes = cache.pendingUploadBytes(); - List uploads = cache.drainUploadPayloads(UPLOAD_PAYLOAD_MAX); - Result result = new Result(payload, uploads, fid, rects.size(), drawnGlyphs.size(), runs.size(), - imageDrawable.size(), fwGlyphCount, fwSubs.size(), - bakedCandidates, uploadBytes, fullFrame); - long doneAtMs = android.os.SystemClock.elapsedRealtime(); - result.rectsMs = (int) (matchStartedAtMs - planStartedAtMs); - result.matchMs = (int) (cacheStartedAtMs - matchStartedAtMs); - result.cacheMs = (int) (punchStartedAtMs - cacheStartedAtMs); - result.punchMs = (int) (encodeStartedAtMs - punchStartedAtMs); - result.encodeMs = (int) (doneAtMs - encodeStartedAtMs); - return result; - } - - /** One punch task for an emitted firmware-font glyph. */ - private static final class FwPunch { - final FwGlyphAtlas.Entry entry; - final int gx; - final int gy; - - FwPunch(FwGlyphAtlas.Entry entry, int gx, int gy) { - this.entry = entry; - this.gx = gx; - this.gy = gy; - } - } - - /** - * Plan one firmware-font run (mode 15): classify every member, and when - * any correct ink member intersects a sent rect, emit sub-messages over - * the maximal correct stretches. A stretch never spans an incorrect - * member — the sub-message boundary restarts the firmware's advance walk - * at an explicit pen x, so occluded or unknown glyphs simply stay baked - * without disturbing their neighbors' positions. Emitted ink members are - * queued for punching. Returns how many ink members stay baked. - */ - private static int planFwRun( - SurfaceCompositor.ScreenDraw draw, List rects, - byte[] next, int stride, int width, int height, - List outSubs, List outPunches) { - int n = draw.fwCps.length; - if (n == 0 || draw.y < 0 || draw.y > 0xffff) { - return countInk(draw); - } - int top = BmpUtil.nibbleForGray(draw.value); - boolean[] ok = new boolean[n]; - FwGlyphAtlas.Entry[] entries = new FwGlyphAtlas.Entry[n]; - boolean relevant = false; - int baked = 0; - for (int i = 0; i < n; i++) { - if (!draw.fwInk[i]) { - ok[i] = true; // inkless connector (space): draws nothing - continue; - } - FwGlyphAtlas.Entry entry = FwGlyphAtlas.get(draw.fwCps[i]); - if (entry == null) { - baked++; - continue; - } - int gx = draw.x + draw.fwDx[i] + entry.ofsX; - int gy = draw.y + entry.inkTop; - if (!fwGlyphMatchesComposite(next, stride, width, height, entry, gx, gy, top)) { - baked++; - continue; - } - ok[i] = true; - entries[i] = entry; - if (intersectsAny(rects, gx, gy, entry.boxW, entry.boxH)) { - relevant = true; - } - } - if (!relevant) { - return 0; // nothing in a sent rect: leave the run's pixels alone - } - - int i = 0; - while (i < n && outSubs.size() < MAX_FWTEXT_RUNS) { - while (i < n && !(ok[i] && draw.fwInk[i])) i++; - if (i >= n) break; - int startX = draw.x + draw.fwDx[i]; - if (startX < 0 || startX > 0xffff) { - baked++; - i++; - continue; - } - // Extend across correct members while the UTF-8 payload fits, - // tracking the last ink member so trailing connectors trim away. - int j = i; - int lastInk = -1; - int bytesToLastInk = 0; - int bytesSoFar = 0; - while (j < n && ok[j]) { - int encodedLength = utf8Length(draw.fwCps[j]); - if (bytesSoFar + encodedLength > 255) break; - bytesSoFar += encodedLength; - if (draw.fwInk[j]) { - lastInk = j; - bytesToLastInk = bytesSoFar; - } - j++; - } - if (lastInk < i) { // a single over-long codepoint; not realistic - baked++; - i++; - continue; - } - byte[] sub = new byte[7 + bytesToLastInk]; - sub[0] = 15; - sub[1] = (byte) (startX & 0xff); - sub[2] = (byte) ((startX >> 8) & 0xff); - sub[3] = (byte) (draw.y & 0xff); - sub[4] = (byte) ((draw.y >> 8) & 0xff); - sub[5] = (byte) (top | 0x10); // top color + transparent - sub[6] = (byte) bytesToLastInk; - int pos = 7; - for (int k = i; k <= lastInk; k++) { - byte[] encoded = new StringBuilder().appendCodePoint(draw.fwCps[k]) - .toString().getBytes(java.nio.charset.StandardCharsets.UTF_8); - System.arraycopy(encoded, 0, sub, pos, encoded.length); - pos += encoded.length; - } - outSubs.add(sub); - for (int k = i; k <= lastInk; k++) { - if (draw.fwInk[k]) { - outPunches.add(new FwPunch(entries[k], - draw.x + draw.fwDx[k] + entries[k].ofsX, - draw.y + entries[k].inkTop)); - } - } - i = lastInk + 1; - } - return baked; - } - - private static int utf8Length(int cp) { - if (cp < 0x80) return 1; - if (cp < 0x800) return 2; - if (cp < 0x10000) return 3; - return 4; - } - - private static int countInk(SurfaceCompositor.ScreenDraw draw) { - int count = 0; - for (boolean ink : draw.fwInk) { - if (ink) count++; - } - return count; - } - - /** - * Whether every in-panel nonzero pixel of the builtin-font glyph lands - * correct: the composite's 4bpp value must equal the firmware's LUT - * output src*top/15 (integer division — a general LUT check, since these - * glyphs are anti-aliased). - */ - private static boolean fwGlyphMatchesComposite(byte[] packed, int stride, int width, int height, - FwGlyphAtlas.Entry entry, int gx, int gy, int top) { - for (int row = 0; row < entry.boxH; row++) { - int y = gy + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < entry.boxW; col++) { - int source = entry.nibbleAt(col, row); - if (source == 0) continue; - int x = gx + col; - if (x < 0 || x >= width) continue; - if (nibbleAt(packed, stride, x, y) != (source * top) / 15) { - return false; - } - } - } - return true; - } - - private static void punchFw(byte[] packed, int stride, int width, int height, FwPunch fw) { - for (int row = 0; row < fw.entry.boxH; row++) { - int y = fw.gy + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < fw.entry.boxW; col++) { - if (fw.entry.nibbleAt(col, row) == 0) continue; - punchPixel(packed, stride, width, fw.gx + col, y); - } - } - } - - private static boolean intersectsAny(List rects, int x, int y, int w, int h) { - if (w <= 0 || h <= 0) return false; - for (int[] r : rects) { - if (x < r[0] + r[2] && x + w > r[0] && y < r[1] + r[3] && y + h > r[1]) { - return true; - } - } - return false; - } - - /** - * Whether every in-panel ink pixel of the glyph lands correct in the - * packed new frame: the composite's 4bpp value must equal the firmware's - * LUT output source*top/15 (integer). For 1bpp glyphs (source 15) this is - * exactly `== top`; AA glyphs get the general check. Ink outside the - * panel is clipped by the firmware and irrelevant. - */ - private static boolean glyphMatchesComposite(byte[] packed, int stride, int width, int height, - GlyphAtlas.Glyph atlas, int gx, int lineY, int top) { - for (int row = atlas.inkTop; row < atlas.inkTop + atlas.inkHeight; row++) { - int y = lineY + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < atlas.width; col++) { - int source = atlas.nibbleAt(col, row); - if (source == 0) continue; - int x = gx + col; - if (x < 0 || x >= width) continue; - if (nibbleAt(packed, stride, x, y) != (source * top) / 15) { - return false; - } - } - } - return true; - } - - /** - * Whether every in-panel nonzero pixel of the cached image equals the - * packed new frame — the mode-19 transparent draw writes exactly those. - */ - private static boolean imageMatchesComposite(byte[] packed, int stride, int width, int height, - ImageAtlas.Entry atlas, int left, int top) { - for (int row = 0; row < atlas.height; row++) { - int y = top + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < atlas.width; col++) { - int source = atlas.nibbleAt(col, row); - if (source == 0) continue; - int x = left + col; - if (x < 0 || x >= width) continue; - if (nibbleAt(packed, stride, x, y) != source) { - return false; - } - } - } - return true; - } - - private static int nibbleAt(byte[] packed, int stride, int x, int y) { - int b = packed[y * stride + (x >> 1)] & 0xff; - return (x & 1) != 0 ? (b & 0x0f) : (b >> 4); - } - - private static void punch(byte[] packed, int stride, int width, int height, Selected sel) { - if (sel.glyphAtlas != null) { - GlyphAtlas.Glyph atlas = sel.glyphAtlas; - for (int row = atlas.inkTop; row < atlas.inkTop + atlas.inkHeight; row++) { - int y = sel.draw.y + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < atlas.width; col++) { - if (!atlas.inkAt(col, row)) continue; - punchPixel(packed, stride, width, sel.gx + col, y); - } - } - } else { - ImageAtlas.Entry atlas = sel.imageAtlas; - for (int row = 0; row < atlas.height; row++) { - int y = sel.draw.y + row; - if (y < 0 || y >= height) continue; - for (int col = 0; col < atlas.width; col++) { - if (atlas.nibbleAt(col, row) == 0) continue; - punchPixel(packed, stride, width, sel.gx + col, y); - } - } - } - } - - private static void punchPixel(byte[] packed, int stride, int width, int x, int y) { - if (x < 0 || x >= width) return; - int index = y * stride + (x >> 1); - if ((x & 1) != 0) { - packed[index] = (byte) (packed[index] & 0xf0); - } else { - packed[index] = (byte) (packed[index] & 0x0f); - } - } - - /** - * Ensure residency for all of `selected`, resetting the cache once if the - * allocator fills mid-plan (a reset invalidates offsets handed out earlier - * in the same pass, so the whole pass reruns). Returns the draws that are - * resident afterwards. - */ - private static List ensureResident(TextureCacheState cache, List selected) { - for (int attempt = 0; attempt < 2; attempt++) { - List resident = new ArrayList<>(selected.size()); - boolean full = false; - for (Selected sel : selected) { - int offset = sel.glyphAtlas != null - ? cache.ensureGlyph(sel.draw.fontId, sel.draw.encoding, sel.glyphAtlas) - : cache.ensureImage(sel.draw.imageId, sel.imageAtlas); - if (offset >= 0) { - resident.add(sel); - } else { - full = true; - } - } - if (!full || attempt == 1) { - return resident; - } - cache.reset(); - } - return Collections.emptyList(); // unreachable - } - - /** Mode-19 cached-image draw: [19][offset u32][x u16][y u16][options u8]. */ - private static byte[] encodeImageDraw(TextureCacheState cache, Selected sel) { - int offset = cache.ensureImage(sel.draw.imageId, sel.imageAtlas); // resident: returns the offset - byte[] sub = new byte[10]; - sub[0] = 19; - sub[1] = (byte) (offset & 0xff); - sub[2] = (byte) ((offset >> 8) & 0xff); - sub[3] = (byte) ((offset >> 16) & 0xff); - sub[4] = (byte) ((offset >> 24) & 0xff); - sub[5] = (byte) (sel.draw.x & 0xff); - sub[6] = (byte) ((sel.draw.x >> 8) & 0xff); - sub[7] = (byte) (sel.draw.y & 0xff); - sub[8] = (byte) ((sel.draw.y >> 8) & 0xff); - sub[9] = (byte) IMAGE_DRAW_OPTIONS; - return sub; - } - - /** - * Group selected glyphs into mode-20 string sub-messages: - * [20][fontTable u32][x u16][y u16][options u8][strlen u8][string] - * One run per (font, line y, top color) span; within a run, control bytes - * 1..31 adjust x by -10..+20 to hit each glyph's exact position, and each - * glyph advances x by its cached width. Order across runs is free: every - * drawn glyph's ink equals the composite, so overlaps write equal values. - * Returns the glyphs actually emitted (run budget can drop stragglers). - */ - private static List buildRuns(TextureCacheState cache, List drawable, List outRuns) { - List sorted = new ArrayList<>(drawable); - sorted.sort(Comparator - .comparingInt((Selected s) -> s.draw.fontId) - .thenComparingInt(s -> s.draw.y) - .thenComparingInt(s -> s.top) - .thenComparingInt(s -> s.gx)); - - List drawn = new ArrayList<>(sorted.size()); - int i = 0; - while (i < sorted.size() && outRuns.size() < MAX_GLYPH_RUNS) { - Selected first = sorted.get(i); - int fontTable = cache.fontTableOffset(first.draw.fontId); - if (fontTable < 0) { // shouldn't happen: residency ensured above - i++; - continue; - } - java.io.ByteArrayOutputStream string = new java.io.ByteArrayOutputStream(); - List runGlyphs = new ArrayList<>(); - int cursor = first.gx; // the run's wire x starts exactly at the first glyph - int j = i; - while (j < sorted.size()) { - Selected sel = sorted.get(j); - if (sel.draw.fontId != first.draw.fontId - || sel.draw.y != first.draw.y - || sel.top != first.top) { - break; - } - int delta = sel.gx - cursor; - int adjustBytes = adjustByteCount(delta); - if (adjustBytes > MAX_ADJUST_BYTES && j > i) { - break; // far gap: cheaper to start a fresh run - } - if (string.size() + adjustBytes + 1 > 255) { - break; // strlen is a u8 - } - emitAdjust(string, delta); - string.write(sel.draw.encoding); - runGlyphs.add(sel); - cursor = sel.gx + sel.glyphAtlas.width; - j++; - } - if (!runGlyphs.isEmpty()) { - byte[] stringBytes = string.toByteArray(); - byte[] run = new byte[11 + stringBytes.length]; - run[0] = 20; - run[1] = (byte) (fontTable & 0xff); - run[2] = (byte) ((fontTable >> 8) & 0xff); - run[3] = (byte) ((fontTable >> 16) & 0xff); - run[4] = (byte) ((fontTable >> 24) & 0xff); - run[5] = (byte) (first.gx & 0xff); - run[6] = (byte) ((first.gx >> 8) & 0xff); - run[7] = (byte) (first.draw.y & 0xff); - run[8] = (byte) ((first.draw.y >> 8) & 0xff); - run[9] = (byte) (first.top | 0x10); // top color + transparent - run[10] = (byte) stringBytes.length; - System.arraycopy(stringBytes, 0, run, 11, stringBytes.length); - outRuns.add(run); - drawn.addAll(runGlyphs); - i = j; - } else { - i++; - } - } - return drawn; - } - - /** How many 1..31 control bytes are needed to move the cursor by delta. */ - private static int adjustByteCount(int delta) { - if (delta == 0) return 0; - return delta > 0 ? (delta + 19) / 20 : (-delta + 9) / 10; - } - - private static void emitAdjust(java.io.ByteArrayOutputStream out, int delta) { - while (delta != 0) { - int step = delta > 0 ? Math.min(delta, 20) : Math.max(delta, -10); - out.write(step + 11); // byte 1..31 adjusts x by (byte - 11) - delta -= step; - } - } - - /** [8][count]([len u16][sub])* — one atomic present for deltas + draws. */ - private static byte[] assembleMode8(List subs) { - int total = 2; - for (byte[] sub : subs) { - total += 2 + sub.length; - } - byte[] out = new byte[total]; - out[0] = 8; - out[1] = (byte) subs.size(); - int pos = 2; - for (byte[] sub : subs) { - out[pos] = (byte) (sub.length & 0xff); - out[pos + 1] = (byte) ((sub.length >> 8) & 0xff); - pos += 2; - System.arraycopy(sub, 0, out, pos, sub.length); - pos += sub.length; - } - return out; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java deleted file mode 100644 index 23898347..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.faceclaw.app; -import java.util.Arrays; - -public class BmpUtil { - public static byte[] copyTileBmp(byte[] bmp) { - if (bmp == null || bmp.length == 0) { - return new byte[0]; - } - return Arrays.copyOf(bmp, bmp.length); - } - - // Quantize an 8-bit gray value to a 4-bit nibble. Truncate rather than round - // up at the bottom: the shell's color-key convention paints intentional - // black as 1 (0 being transparent), which must land on nibble 0, not a - // faintly-visible level 1. - private static final byte[] GRAY_TO_NIBBLE = new byte[256]; - static { - for (int v = 1; v < 256; v++) { - GRAY_TO_NIBBLE[v] = (byte) Math.min(15, (v + 8) >> 4); - } - } - - /** - * The 4bpp level an 8-bit gray value packs to. Public so the texture-cache - * planner computes a glyph draw's top color with the exact quantization - * the composited frame was packed with. - */ - public static int nibbleForGray(int gray) { - return GRAY_TO_NIBBLE[gray & 0xff] & 0xff; - } - - /** - * Pack a full-resolution 8bpp grayscale buffer (row-major, top-to-bottom, - * one byte per pixel) into the headerless 4bpp wire format used by CFW - * load_image_z modes 3/6/8: top-down rows, stride ceil(width/2), high - * nibble = left pixel. This is the canonical in-memory frame format; BMP - * framing (needed only when an uncompressed full frame goes on the wire) - * is added on demand by build4bppBmpFromPacked. - */ - public static byte[] pack4bppFromGray8(byte[] gray8, int width, int height) { - if (width <= 0 || height <= 0) { - return new byte[0]; - } - int stride = (width + 1) >> 1; - byte[] out = new byte[stride * height]; - if (gray8 == null || gray8.length < width * height) { - return out; // short buffer: treat missing pixels as black - } - int pairs = width >> 1; - int src = 0; - int dst = 0; - for (int y = 0; y < height; y++) { - for (int i = 0; i < pairs; i++) { - out[dst++] = (byte) ((GRAY_TO_NIBBLE[gray8[src] & 0xff] << 4) - | GRAY_TO_NIBBLE[gray8[src + 1] & 0xff]); - src += 2; - } - if ((width & 1) != 0) { - out[dst++] = (byte) (GRAY_TO_NIBBLE[gray8[src++] & 0xff] << 4); - } - } - return out; - } - - /** - * Wrap a packed 4bpp frame in BMP framing (BITMAPINFOHEADER + 16-entry gray - * palette, bottom-up rows padded to a 4-byte stride). Only the full-frame - * fallback used when mode-6 compression does not shrink the frame still - * speaks BMP on the wire; everything else uses the headerless packed format - * directly. - */ - public static byte[] build4bppBmpFromPacked(byte[] packed, int width, int height) { - int packedStride = (width + 1) >> 1; - if (width <= 0 || height <= 0 - || packed == null || packed.length < packedStride * height) { - return new byte[0]; - } - int rowStride = (packedStride + 3) & ~3; - int pixelDataSize = rowStride * height; - - int fileHeaderSize = 14; - int dibHeaderSize = 40; - int paletteSize = 16 * 4; - int pixelOffset = fileHeaderSize + dibHeaderSize + paletteSize; - int fileSize = pixelOffset + pixelDataSize; - - byte[] buf = new byte[fileSize]; - buf[0] = 0x42; // 'B' - buf[1] = 0x4d; // 'M' - putUint32Le(buf, 2, fileSize); - putUint32Le(buf, 10, pixelOffset); - putUint32Le(buf, 14, dibHeaderSize); - putInt32Le(buf, 18, width); - putInt32Le(buf, 22, height); - putUint16Le(buf, 26, 1); // planes - putUint16Le(buf, 28, 4); // bits per pixel - putUint32Le(buf, 30, 0); // compression (BI_RGB) - putUint32Le(buf, 34, pixelDataSize); - putUint32Le(buf, 46, 16); // palette entry count - - for (int i = 0; i < 16; i++) { - int v = i * 17; - int base = fileHeaderSize + dibHeaderSize + i * 4; - buf[base] = (byte) v; - buf[base + 1] = (byte) v; - buf[base + 2] = (byte) v; - buf[base + 3] = 0; - } - - for (int bmpRow = 0; bmpRow < height; bmpRow++) { - int srcY = height - 1 - bmpRow; // BMP rows run bottom-up - System.arraycopy(packed, srcY * packedStride, - buf, pixelOffset + bmpRow * rowStride, packedStride); - } - return buf; - } - - private static void putUint16Le(byte[] buf, int offset, int value) { - buf[offset] = (byte) (value & 0xff); - buf[offset + 1] = (byte) ((value >>> 8) & 0xff); - } - - private static void putUint32Le(byte[] buf, int offset, int value) { - buf[offset] = (byte) (value & 0xff); - buf[offset + 1] = (byte) ((value >>> 8) & 0xff); - buf[offset + 2] = (byte) ((value >>> 16) & 0xff); - buf[offset + 3] = (byte) ((value >>> 24) & 0xff); - } - - private static void putInt32Le(byte[] buf, int offset, int value) { - putUint32Le(buf, offset, value); - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java b/App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java deleted file mode 100644 index c99e29ea..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.faceclaw.app; -import java.util.ArrayList; -import java.util.List; - -public class CollectionUtils { - public static List singletonList(byte[] value) { - List list = new ArrayList<>(1); - list.add(value); - return list; - } - - public static List listOf(byte[] a, byte[] b) { - List list = new ArrayList<>(2); - list.add(a); - list.add(b); - return list; - } - - public static List listOf(byte[] a, byte[] b, byte[] c) { - List list = new ArrayList<>(3); - list.add(a); - list.add(b); - list.add(c); - return list; - } -} diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/util/InterruptibleSleep.java b/App_Resources/Android/src/main/java/com/faceclaw/app/util/InterruptibleSleep.java deleted file mode 100644 index a77454a6..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/util/InterruptibleSleep.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.faceclaw.app; - -import android.os.SystemClock; - -final class InterruptibleSleep { - private final Object lock = new Object(); - private boolean interrupted; - - boolean sleep(long ms) { - long delayMs = Math.max(1, ms); - long deadline = SystemClock.elapsedRealtime() + delayMs; - synchronized (lock) { - // Consume (do not clear-and-ignore) an interrupt delivered before this - // sleep began. interrupt() runs on other threads with no lock held by - // the sleeper between driveSession() returning and sleep() starting, so - // clearing the flag at entry lost that wake and forced the worker to - // sleep the full interval — up to 250ms per idle frame. Honoring it - // costs at most one extra (cheap) driveSession pass. - if (interrupted) { - interrupted = false; - return false; - } - while (true) { - long remaining = deadline - SystemClock.elapsedRealtime(); - if (remaining <= 0) { - return true; - } - try { - lock.wait(remaining); - } catch (InterruptedException ignored) { - interrupted = false; - return false; - } - if (interrupted) { - interrupted = false; - return false; - } - } - } - } - - void interrupt() { - synchronized (lock) { - interrupted = true; - lock.notifyAll(); - } - } -} diff --git a/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/AppIcon.png b/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/AppIcon.png new file mode 100644 index 00000000..42d0c7b7 Binary files /dev/null and b/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/AppIcon.png differ diff --git a/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/Contents.json b/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 00000000..0a42994e --- /dev/null +++ b/App_Resources/iOS/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1 @@ +{"images":[{"filename":"AppIcon.png","idiom":"universal","platform":"ios","size":"1024x1024"}],"info":{"author":"xcode","version":1}} diff --git a/App_Resources/iOS/Assets.xcassets/Contents.json b/App_Resources/iOS/Assets.xcassets/Contents.json new file mode 100644 index 00000000..f223229d --- /dev/null +++ b/App_Resources/iOS/Assets.xcassets/Contents.json @@ -0,0 +1 @@ +{"info":{"author":"xcode","version":1}} diff --git a/App_Resources/iOS/Assets.xcassets/ic_tab_keyboard.imageset/Contents.json b/App_Resources/iOS/Assets.xcassets/ic_tab_keyboard.imageset/Contents.json new file mode 100644 index 00000000..aae26278 --- /dev/null +++ b/App_Resources/iOS/Assets.xcassets/ic_tab_keyboard.imageset/Contents.json @@ -0,0 +1,13 @@ +{ + "images": [ + { + "idiom": "universal", + "filename": "ic_tab_keyboard@3x.png", + "scale": "3x" + } + ], + "info": { + "author": "xcode", + "version": 1 + } +} diff --git a/App_Resources/iOS/Assets.xcassets/ic_tab_keyboard.imageset/ic_tab_keyboard@3x.png b/App_Resources/iOS/Assets.xcassets/ic_tab_keyboard.imageset/ic_tab_keyboard@3x.png new file mode 100644 index 00000000..f580af0e Binary files /dev/null and b/App_Resources/iOS/Assets.xcassets/ic_tab_keyboard.imageset/ic_tab_keyboard@3x.png differ diff --git a/App_Resources/iOS/Assets.xcassets/ic_tab_mic.imageset/Contents.json b/App_Resources/iOS/Assets.xcassets/ic_tab_mic.imageset/Contents.json new file mode 100644 index 00000000..b5dfb395 --- /dev/null +++ b/App_Resources/iOS/Assets.xcassets/ic_tab_mic.imageset/Contents.json @@ -0,0 +1,13 @@ +{ + "images": [ + { + "idiom": "universal", + "filename": "ic_tab_mic@3x.png", + "scale": "3x" + } + ], + "info": { + "author": "xcode", + "version": 1 + } +} diff --git a/App_Resources/iOS/Assets.xcassets/ic_tab_mic.imageset/ic_tab_mic@3x.png b/App_Resources/iOS/Assets.xcassets/ic_tab_mic.imageset/ic_tab_mic@3x.png new file mode 100644 index 00000000..6840cb66 Binary files /dev/null and b/App_Resources/iOS/Assets.xcassets/ic_tab_mic.imageset/ic_tab_mic@3x.png differ diff --git a/App_Resources/iOS/Info.plist b/App_Resources/iOS/Info.plist new file mode 100644 index 00000000..0f165231 --- /dev/null +++ b/App_Resources/iOS/Info.plist @@ -0,0 +1,69 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleDisplayName + Faceclaw + CFBundleShortVersionString + 1.0.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + NSBluetoothAlwaysUsageDescription + Faceclaw uses Bluetooth to connect to your glasses and ring, display your apps, and receive gestures. + NSMicrophoneUsageDescription + Faceclaw uses your phone microphone for voice input when your glasses are disconnected. + NSSpeechRecognitionUsageDescription + Faceclaw turns audio from your glasses or phone microphone into text for the app you choose. Dictation uses on-device recognition. + NSCameraUsageDescription + Faceclaw uses the camera to scan QR codes for app URLs you open in Developer. + NSCalendarsUsageDescription + Faceclaw reads your upcoming calendar events to display them in Calendar and Glanceboard on your glasses. + NSCalendarsFullAccessUsageDescription + Faceclaw reads your upcoming calendar events to display them in Calendar and Glanceboard on your glasses. + NSLocationWhenInUseUsageDescription + Faceclaw uses your location for local weather, turn-by-turn navigation, and true north on the glasses compass. + NSLocationTemporaryUsageDescriptionDictionary + NavigationPrecise location lets Faceclaw follow your route and show upcoming turns on your glasses. + UIBackgroundModes + + bluetooth-central + location + + NSAppTransportSecurity + + NSAllowsLocalNetworking + + + NSExceptionDomains + + 100.68.94.67 + NSExceptionAllowsInsecureHTTPLoads + + + NSLocalNetworkUsageDescription + Faceclaw connects to the terminal servers and developer app URLs you configure on your local network. + UILaunchScreen + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UIRequiresFullScreen + + UIFileSharingEnabled + LSSupportsOpeningDocumentsInPlace + + diff --git a/App_Resources/iOS/Podfile b/App_Resources/iOS/Podfile new file mode 100644 index 00000000..c643ad84 --- /dev/null +++ b/App_Resources/iOS/Podfile @@ -0,0 +1,13 @@ +platform :ios, '15.1' +pod 'SVGKit', '3.0.0' + +# SVGKit 3's physical-DPI lookup asserts on arm64 simulators. Our icons use +# only viewBox/pixel units, so that lookup is irrelevant to their rendering. +post_install do |installer| + installer.pods_project.targets.each do |target| + next unless target.name == 'SVGKit' + target.build_configurations.each do |config| + config.build_settings['ENABLE_NS_ASSERTIONS'] = 'NO' + end + end +end diff --git a/App_Resources/iOS/build.xcconfig b/App_Resources/iOS/build.xcconfig new file mode 100644 index 00000000..f657ddce --- /dev/null +++ b/App_Resources/iOS/build.xcconfig @@ -0,0 +1,17 @@ +IPHONEOS_DEPLOYMENT_TARGET = 15.1; +TARGETED_DEVICE_FAMILY = 1; +CODE_SIGN_STYLE = Automatic; + +// Select the local signing team in Xcode or pass --teamId to the iOS scripts. +OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework UniformTypeIdentifiers -framework ImageIO -framework EventKit + +ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + +HEADER_SEARCH_PATHS = $(inherited) "$(SRCROOT)/../../App_Resources/iOS/src/lc3" +// NativeScript's xcconfig merge keeps only the CocoaPods HEADER_SEARCH_PATHS when the +// generated project is rebuilt, so the vendored liblc3 include dir is also passed as a flag +// (OTHER_CFLAGS merges like OTHER_LDFLAGS). +OTHER_CFLAGS = $(inherited) -I"$(SRCROOT)/../../App_Resources/iOS/src/lc3" + +// FaceclawKit ships arm64 device and Apple-silicon simulator slices. +EXCLUDED_ARCHS[sdk=iphonesimulator*] = x86_64; diff --git a/App_Resources/iOS/src/FaceclawCalendar.h b/App_Resources/iOS/src/FaceclawCalendar.h new file mode 100644 index 00000000..d76ed117 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawCalendar.h @@ -0,0 +1,13 @@ +#import + +/** Read-only EventKit bridge. Callbacks are delivered on the main queue. */ +@interface FaceclawCalendar : NSObject ++ (instancetype)shared; +@property(nonatomic, copy) void (^changeHandler)(void); +- (BOOL)hasPermission; +- (NSString *)permissionStatus; +- (void)requestPermission:(void (^)(BOOL granted, NSString *error))completion; +- (void)readUpcomingEvents:(NSInteger)maxEvents + windowMs:(double)windowMs + completion:(void (^)(NSString *json, NSString *error))completion; +@end diff --git a/App_Resources/iOS/src/FaceclawCalendar.m b/App_Resources/iOS/src/FaceclawCalendar.m new file mode 100644 index 00000000..78c247f9 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawCalendar.m @@ -0,0 +1,115 @@ +#import "FaceclawCalendar.h" +#import +#import +#import + +@interface FaceclawCalendar () +@property(nonatomic, strong) EKEventStore *store; +@property(nonatomic, strong) dispatch_queue_t queue; +@end + +@implementation FaceclawCalendar ++ (instancetype)shared { + static FaceclawCalendar *instance; + static dispatch_once_t once; + dispatch_once(&once, ^{ instance = [FaceclawCalendar new]; }); + return instance; +} +- (instancetype)init { + if ((self = [super init])) { + _queue = dispatch_queue_create("com.faceclaw.calendar", DISPATCH_QUEUE_SERIAL); + _store = [EKEventStore new]; + NSNotificationCenter *center = NSNotificationCenter.defaultCenter; + [center addObserver:self selector:@selector(calendarChanged:) name:EKEventStoreChangedNotification object:_store]; + // Re-query after Settings changes, calendar sync, or a timezone change. + [center addObserver:self selector:@selector(calendarChanged:) name:UIApplicationDidBecomeActiveNotification object:nil]; + [center addObserver:self selector:@selector(calendarChanged:) name:UIApplicationSignificantTimeChangeNotification object:nil]; + } + return self; +} +- (BOOL)hasPermission { + EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]; + if (@available(iOS 17.0, *)) return status == EKAuthorizationStatusFullAccess; + return status == EKAuthorizationStatusAuthorized; +} +- (NSString *)permissionStatus { + if ([self hasPermission]) return @"granted"; + EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]; + if (status == EKAuthorizationStatusDenied) return @"denied"; + if (status == EKAuthorizationStatusRestricted) return @"restricted"; + if (@available(iOS 17.0, *)) { + if (status == EKAuthorizationStatusWriteOnly) return @"write-only"; + } + return @"not-determined"; +} +- (void)calendarChanged:(NSNotification *)notification { + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.changeHandler) self.changeHandler(); + }); +} +- (void)requestPermission:(void (^)(BOOL, NSString *))completion { + dispatch_async(self.queue, ^{ + if ([self hasPermission]) { + dispatch_async(dispatch_get_main_queue(), ^{ completion(YES, nil); }); + return; + } + void (^done)(BOOL, NSError *) = ^(BOOL granted, NSError *error) { + dispatch_async(self.queue, ^{ + // Discard objects fetched under the previous authorization. + [self.store reset]; + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.changeHandler) self.changeHandler(); + completion(granted && [self hasPermission], error.localizedDescription); + }); + }); + }; + if (@available(iOS 17.0, *)) { + [self.store requestFullAccessToEventsWithCompletion:done]; + } else { + [self.store requestAccessToEntityType:EKEntityTypeEvent completion:done]; + } + }); +} +- (void)readUpcomingEvents:(NSInteger)maxEvents windowMs:(double)windowMs completion:(void (^)(NSString *, NSString *))completion { + // EventKit queries may block. Never query on the glasses render thread. + dispatch_async(self.queue, ^{ + if (![self hasPermission] || maxEvents <= 0 || !isfinite(windowMs) || windowMs <= 0) { + dispatch_async(dispatch_get_main_queue(), ^{ completion(@"[]", nil); }); + return; + } + @try { + NSDate *now = [NSDate date]; + // Bound the query well below EventKit's four-year predicate limit. + NSDate *end = [now dateByAddingTimeInterval:MIN(windowMs / 1000.0, 366 * 24 * 60 * 60)]; + NSPredicate *predicate = [self.store predicateForEventsWithStartDate:now endDate:end calendars:nil]; + // EventKit expands recurring events and includes overlapping events. + NSArray *events = [[self.store eventsMatchingPredicate:predicate] + sortedArrayUsingComparator:^NSComparisonResult(EKEvent *a, EKEvent *b) { + NSComparisonResult order = [a.startDate compare:b.startDate]; + return order == NSOrderedSame ? [(a.eventIdentifier ?: @"") compare:(b.eventIdentifier ?: @"")] : order; + }]; + NSMutableArray *rows = [NSMutableArray new]; + for (EKEvent *event in events) { + if (!event.startDate || !event.endDate || event.status == EKEventStatusCanceled || + [event.endDate compare:now] != NSOrderedDescending || [event.startDate compare:end] != NSOrderedAscending) continue; + [rows addObject:@{ + @"id": event.eventIdentifier ?: @"", + @"title": event.title ?: @"", + @"startMs": @(event.startDate.timeIntervalSince1970 * 1000.0), + @"endMs": @(event.endDate.timeIntervalSince1970 * 1000.0), + @"allDay": @(event.allDay), + @"location": event.location ?: @"", + @"calendarName": event.calendar.title ?: @"" + }]; + if (rows.count >= MIN(200, maxEvents)) break; + } + NSError *error = nil; + NSData *data = [NSJSONSerialization dataWithJSONObject:rows options:0 error:&error]; + NSString *json = data ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : @"[]"; + dispatch_async(dispatch_get_main_queue(), ^{ completion(json, error.localizedDescription); }); + } @catch (NSException *exception) { + dispatch_async(dispatch_get_main_queue(), ^{ completion(@"[]", exception.reason ?: @"Calendar unavailable"); }); + } + }); +} +@end diff --git a/App_Resources/iOS/src/FaceclawConfigPort.h b/App_Resources/iOS/src/FaceclawConfigPort.h new file mode 100644 index 00000000..768f5cd5 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawConfigPort.h @@ -0,0 +1,6 @@ +#import +/** Developer config transfer through the app container, before workers start. */ +@interface FaceclawConfigPort : NSObject ++ (void)processPendingRequest; ++ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain; +@end diff --git a/App_Resources/iOS/src/FaceclawConfigPort.m b/App_Resources/iOS/src/FaceclawConfigPort.m new file mode 100644 index 00000000..7da8efa2 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawConfigPort.m @@ -0,0 +1,59 @@ +#import "FaceclawConfigPort.h" +@implementation FaceclawConfigPort ++ (void)processPendingRequest { + NSString *library = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject; + [self processDirectory:[library stringByAppendingPathComponent:@"FaceclawConfigPort"] + defaults:NSUserDefaults.standardUserDefaults domain:NSBundle.mainBundle.bundleIdentifier]; +} ++ (BOOL)validSettings:(id)settings { + if (![settings isKindOfClass:NSDictionary.class] || [settings count] > 4096) return NO; + for (id key in settings) { + id value = settings[key]; + if (![key isKindOfClass:NSString.class] || ![key length] || [key length] > 512 || + !([value isKindOfClass:NSString.class] || [value isKindOfClass:NSNumber.class])) return NO; + } + return YES; +} ++ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain { + NSString *requestPath = [directory stringByAppendingPathComponent:@"request.json"]; + NSData *data = [NSData dataWithContentsOfFile:requestPath]; + if (!data) return; + NSFileManager *fm = NSFileManager.defaultManager; + // Consume once. A failed import must never be replayed at a later launch. + [fm removeItemAtPath:requestPath error:nil]; + id request = data.length <= 2 * 1024 * 1024 ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil; + BOOL valid = [request isKindOfClass:NSDictionary.class]; + NSString *identifier = valid && [request[@"id"] isKindOfClass:NSString.class] ? request[@"id"] : @""; + NSMutableDictionary *reply = [@{@"id":identifier, @"ok":@NO, @"message":@"Invalid config request"} mutableCopy]; + [defaults synchronize]; + NSDictionary *before = [defaults persistentDomainForName:domain] ?: @{}; + if (valid && [request[@"operation"] isEqual:@"pull"]) { + NSMutableDictionary *settings = [NSMutableDictionary new]; + for (NSString *key in before) { + id value = before[key]; + if ([value isKindOfClass:NSString.class] || [value isKindOfClass:NSNumber.class]) settings[key] = value; + } + [settings removeObjectForKey:@"ios.settings.changeToken"]; + reply[@"config"] = @{@"schema":@1, @"settings":settings}; reply[@"ok"] = @YES; + reply[@"message"] = @"Config exported"; + } else if (valid && [request[@"operation"] isEqual:@"push"] && [self validSettings:request[@"settings"]]) { + NSError *error = nil; + NSData *backup = [NSPropertyListSerialization dataWithPropertyList:before format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error]; + if (backup && [backup writeToFile:[directory stringByAppendingPathComponent:@"previous.plist"] options:NSDataWritingAtomic error:&error]) { + NSMutableDictionary *next = [before mutableCopy]; + [next addEntriesFromDictionary:request[@"settings"]]; + next[@"ios.settings.changeToken"] = NSUUID.UUID.UUIDString; + [defaults setPersistentDomain:next forName:domain]; + if ([defaults synchronize]) { + reply[@"ok"] = @YES; reply[@"message"] = @"Config imported"; + reply[@"count"] = @([request[@"settings"] count]); + } else { + [defaults setPersistentDomain:before forName:domain]; [defaults synchronize]; + reply[@"message"] = @"Could not persist config; restored previous settings"; + } + } else reply[@"message"] = @"Could not back up existing settings; import skipped"; + } + NSData *response = [NSJSONSerialization dataWithJSONObject:reply options:NSJSONWritingPrettyPrinted error:nil]; + [response writeToFile:[directory stringByAppendingPathComponent:@"response.json"] options:NSDataWritingAtomic error:nil]; +} +@end diff --git a/App_Resources/iOS/src/FaceclawCrypto.h b/App_Resources/iOS/src/FaceclawCrypto.h new file mode 100644 index 00000000..c0eb02ba --- /dev/null +++ b/App_Resources/iOS/src/FaceclawCrypto.h @@ -0,0 +1,5 @@ +#import +@interface FaceclawCrypto : NSObject ++ (NSString *)hmacSha256:(NSString *)secret message:(NSString *)message; ++ (NSString *)sha256:(NSData *)data; +@end diff --git a/App_Resources/iOS/src/FaceclawCrypto.m b/App_Resources/iOS/src/FaceclawCrypto.m new file mode 100644 index 00000000..6814fb3c --- /dev/null +++ b/App_Resources/iOS/src/FaceclawCrypto.m @@ -0,0 +1,19 @@ +#import "FaceclawCrypto.h" +#import +#import +@implementation FaceclawCrypto ++ (NSString *)hmacSha256:(NSString *)secret message:(NSString *)message { + NSData *key = [secret dataUsingEncoding:NSUTF8StringEncoding]; + NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding]; + unsigned char digest[CC_SHA256_DIGEST_LENGTH]; + CCHmac(kCCHmacAlgSHA256, key.bytes, key.length, data.bytes, data.length, digest); + return [[NSData dataWithBytes:digest length:sizeof(digest)] base64EncodedStringWithOptions:0]; +} ++ (NSString *)sha256:(NSData *)data { + unsigned char digest[CC_SHA256_DIGEST_LENGTH]; + CC_SHA256(data.bytes, (CC_LONG)data.length, digest); + NSMutableString *hex = [NSMutableString stringWithCapacity:64]; + for (NSUInteger i = 0; i < sizeof(digest); i++) [hex appendFormat:@"%02x", digest[i]]; + return hex; +} +@end diff --git a/App_Resources/iOS/src/FaceclawEvenHubWebView.h b/App_Resources/iOS/src/FaceclawEvenHubWebView.h new file mode 100644 index 00000000..0634e501 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawEvenHubWebView.h @@ -0,0 +1,14 @@ +#import +#import + +/** One local or URL EvenHub runtime, retained by its TS session until its window closes. */ +@interface FaceclawEvenHubWebView : NSObject +@property(nonatomic, copy) void (^eventHandler)(NSString *json); +@property(nonatomic, copy) NSString *packageIdentifier; +- (void)start:(NSString *)directory entrypoint:(NSString *)entrypoint script:(NSString *)script; +- (void)startURL:(NSString *)url script:(NSString *)script; +- (void)evaluate:(NSString *)script; +- (void)showOnPhone; +- (void)hideOnPhone; +- (void)destroy; +@end diff --git a/App_Resources/iOS/src/FaceclawEvenHubWebView.m b/App_Resources/iOS/src/FaceclawEvenHubWebView.m new file mode 100644 index 00000000..448a8da2 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawEvenHubWebView.m @@ -0,0 +1,173 @@ +#import "FaceclawEvenHubWebView.h" +#import +#import +#import + +@interface FaceclawEvenHubWebView () +@property(nonatomic) WKWebView *webView; +@property(nonatomic) UIView *host; +@property(nonatomic) UIButton *back; +@property(nonatomic) NSTimer *timer; +@property(nonatomic) NSString *directory; +@property(nonatomic) NSURL *remoteURL; +@property(nonatomic) BOOL ticking; +@property(nonatomic) BOOL destroyed; +@end + +@implementation FaceclawEvenHubWebView +- (void)emit:(NSDictionary *)event { + if (!_eventHandler || _destroyed) return; + NSData *data = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil]; + if (data) _eventHandler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); +} +- (void)start:(NSString *)directory entrypoint:(NSString *)entrypoint script:(NSString *)script { + _directory = directory.stringByStandardizingPath.stringByResolvingSymlinksInPath; + NSURLComponents *url = [NSURLComponents new]; + url.scheme = @"faceclaw-ehpk"; url.host = @"app"; url.path = [@"/" stringByAppendingString:entrypoint]; + [self startRequest:url.URL script:script]; +} +- (void)startURL:(NSString *)url script:(NSString *)script { + NSURL *target = [NSURL URLWithString:url]; + if (!target.host.length || ![@[@"http", @"https"] containsObject:target.scheme.lowercaseString] || target.user || target.password) { + [self emit:@{@"kind": @"error", @"message": @"Invalid app URL."}]; return; + } + _remoteURL = target; + [self startRequest:target script:script]; +} +- (void)startRequest:(NSURL *)url script:(NSString *)script { + if (_destroyed) return; + WKWebViewConfiguration *config = [WKWebViewConfiguration new]; + // Isolate browser storage by package, retaining it across closing/reopening. + // SDK storage also persists independently through the shared TS session. + config.websiteDataStore = WKWebsiteDataStore.nonPersistentDataStore; + if (@available(iOS 17.0, *)) { + if (_packageIdentifier.length) { + NSData *key = [[@"com.faceclaw.ehpk:" stringByAppendingString:_packageIdentifier] dataUsingEncoding:NSUTF8StringEncoding]; + unsigned char digest[CC_SHA256_DIGEST_LENGTH]; CC_SHA256(key.bytes, (CC_LONG)key.length, digest); + config.websiteDataStore = [WKWebsiteDataStore dataStoreForIdentifier:[[NSUUID alloc] initWithUUIDBytes:digest]]; + } + } + [config setURLSchemeHandler:self forURLScheme:@"faceclaw-ehpk"]; + [config.userContentController addScriptMessageHandler:self name:@"faceclaw"]; + [config.userContentController addUserScript:[[WKUserScript alloc] initWithSource:script + injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES]]; + _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config]; + _webView.navigationDelegate = self; + _webView.accessibilityIdentifier = @"evenhub-webview"; + UIWindow *window = nil; + for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) { + if (![scene isKindOfClass:UIWindowScene.class]) continue; + for (UIWindow *candidate in ((UIWindowScene *)scene).windows) if (candidate.isKeyWindow) window = candidate; + } + if (!window) window = UIApplication.sharedApplication.keyWindow; + if (!window) { [self emit:@{@"kind": @"error", @"message": @"No phone window is available."}]; return; } + _host = [[UIView alloc] initWithFrame:window.bounds]; + _host.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + _host.backgroundColor = UIColor.systemBackgroundColor; + _host.userInteractionEnabled = NO; + _host.accessibilityElementsHidden = YES; + [window insertSubview:_host atIndex:0]; + [_host addSubview:_webView]; + _webView.translatesAutoresizingMaskIntoConstraints = NO; + _back = [UIButton buttonWithType:UIButtonTypeSystem]; + [_back setTitle:@"Back to Faceclaw" forState:UIControlStateNormal]; + _back.accessibilityIdentifier = @"evenhub-back"; + [_back addTarget:self action:@selector(hideOnPhone) forControlEvents:UIControlEventTouchUpInside]; + [_host addSubview:_back]; _back.translatesAutoresizingMaskIntoConstraints = NO; + [NSLayoutConstraint activateConstraints:@[ + [_back.topAnchor constraintEqualToAnchor:_host.safeAreaLayoutGuide.topAnchor], + [_back.leadingAnchor constraintEqualToAnchor:_host.leadingAnchor], + [_back.trailingAnchor constraintEqualToAnchor:_host.trailingAnchor], + [_back.heightAnchor constraintEqualToConstant:44], + [_webView.topAnchor constraintEqualToAnchor:_back.bottomAnchor], + [_webView.leadingAnchor constraintEqualToAnchor:_host.leadingAnchor], + [_webView.trailingAnchor constraintEqualToAnchor:_host.trailingAnchor], + [_webView.bottomAnchor constraintEqualToAnchor:_host.safeAreaLayoutGuide.bottomAnchor] + ]]; + // Keep a nonzero attached view behind the dashboard; do not hide/detach it. + // Tick whenever iOS gives us execution time, including in the background or + // with the phone locked. Gating on UIApplicationStateActive stalls the + // injected timers/rAF even while BLE input can still evaluate JavaScript. + // Bound host ticks to one evaluation in flight, including across suspension. + __weak typeof(self) weakSelf = self; + _timer = [NSTimer timerWithTimeInterval:1.0 / 60 repeats:YES block:^(NSTimer *timer) { + typeof(self) strongSelf = weakSelf; + if (!strongSelf || strongSelf.destroyed || strongSelf.ticking) return; + strongSelf.ticking = YES; + [strongSelf.webView evaluateJavaScript:@"window.__fcTimerTick && window.__fcTimerTick(); window.__fcRafTick && window.__fcRafTick();" completionHandler:^(id result, NSError *error) { + weakSelf.ticking = NO; + }]; + }]; + [NSRunLoop.mainRunLoop addTimer:_timer forMode:NSRunLoopCommonModes]; + [_webView loadRequest:[NSURLRequest requestWithURL:url]]; +} +- (void)evaluate:(NSString *)script { + if (!_destroyed) [_webView evaluateJavaScript:script completionHandler:nil]; +} +- (void)showOnPhone { + if (_destroyed) return; + [_host.superview bringSubviewToFront:_host]; _host.userInteractionEnabled = YES; _host.accessibilityElementsHidden = NO; +} +- (void)hideOnPhone { + [_host endEditing:YES]; [_host.superview sendSubviewToBack:_host]; _host.userInteractionEnabled = NO; _host.accessibilityElementsHidden = YES; + [self emit:@{@"kind": @"hidden"}]; +} +- (void)destroy { + if (_destroyed) return; + _destroyed = YES; [_timer invalidate]; _timer = nil; + [_webView stopLoading]; _webView.navigationDelegate = nil; + [_webView.configuration.userContentController removeScriptMessageHandlerForName:@"faceclaw"]; + [_webView.configuration.userContentController removeAllUserScripts]; + [_host removeFromSuperview]; _host = nil; _back = nil; _webView = nil; _eventHandler = nil; +} +- (BOOL)allowsURL:(NSURL *)url { + if (!_remoteURL) return [url.scheme isEqual:@"faceclaw-ehpk"] && [url.host isEqual:@"app"]; + NSNumber *port = url.port ?: @([url.scheme.lowercaseString isEqual:@"https"] ? 443 : 80); + NSNumber *remotePort = _remoteURL.port ?: @([_remoteURL.scheme.lowercaseString isEqual:@"https"] ? 443 : 80); + return [url.scheme.lowercaseString isEqual:_remoteURL.scheme.lowercaseString] && + [url.host.lowercaseString isEqual:_remoteURL.host.lowercaseString] && [port isEqual:remotePort]; +} +- (void)userContentController:(WKUserContentController *)controller didReceiveScriptMessage:(WKScriptMessage *)message { + // Subframes and navigated third-party documents never receive host access. + NSURL *url = message.frameInfo.request.URL; + if (!message.frameInfo.isMainFrame || ![self allowsURL:url]) return; + if ([message.body isKindOfClass:NSDictionary.class]) [self emit:message.body]; +} +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)action decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { + NSURL *url = action.request.URL; + BOOL allowed = [self allowsURL:url] && action.targetFrame.isMainFrame; + if (!allowed && action.targetFrame.isMainFrame) + [self emit:@{@"kind": @"error", @"message": @"Navigation outside the app origin is blocked. Open the destination as a separate app URL."}]; + decisionHandler(allowed ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel); +} +- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { + [self emit:@{@"kind": @"loaded"}]; +} +- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error { + [self emit:@{@"kind": @"error", @"message": error.localizedDescription}]; +} +- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error { + [self emit:@{@"kind": @"error", @"message": error.localizedDescription}]; +} +- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView { + [self emit:@{@"kind": @"error", @"message": @"The app's web process ended. Close and reopen the app."}]; +} +- (void)webView:(WKWebView *)webView startURLSchemeTask:(id)task { + NSURL *url = task.request.URL; + NSString *relative = [url.path stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]]; + NSString *file = [[_directory stringByAppendingPathComponent:relative] stringByStandardizingPath].stringByResolvingSymlinksInPath; + BOOL allowed = !_destroyed && _directory.length && !_remoteURL && [url.host isEqual:@"app"] && + [file hasPrefix:[_directory stringByAppendingString:@"/"]]; + NSData *data = allowed ? [NSData dataWithContentsOfFile:file options:NSDataReadingMappedIfSafe error:nil] : nil; + NSString *ext = file.pathExtension.lowercaseString; + NSString *mime = [@{@"js": @"text/javascript", @"mjs": @"text/javascript", @"html": @"text/html", + @"css": @"text/css", @"json": @"application/json", @"wasm": @"application/wasm", @"svg": @"image/svg+xml"} objectForKey:ext]; + if (!mime) mime = [UTType typeWithFilenameExtension:ext].preferredMIMEType ?: @"application/octet-stream"; + NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:url statusCode:data ? 200 : 404 + HTTPVersion:@"HTTP/1.1" headerFields:@{@"Content-Type": mime, @"Access-Control-Allow-Origin": @"*", @"Cache-Control": @"no-store"}]; + // Synchronous completion on WebKit's main-queue callback means there is no + // outstanding task to finish after stopURLSchemeTask cancels it. + [task didReceiveResponse:response]; [task didReceiveData:data ?: NSData.data]; [task didFinish]; +} +- (void)webView:(WKWebView *)webView stopURLSchemeTask:(id)task {} +@end diff --git a/App_Resources/iOS/src/FaceclawFontRenderer.h b/App_Resources/iOS/src/FaceclawFontRenderer.h new file mode 100644 index 00000000..63a3890c --- /dev/null +++ b/App_Resources/iOS/src/FaceclawFontRenderer.h @@ -0,0 +1,15 @@ +#import + +/** Core Text rasterizer. All sizes are glasses pixels, independent of screen scale. + * Each operation owns its layout/context, so main and worker isolates may call it. + * Packets match Android FontFileRenderer: grayscale image or 10-byte glyph header. + */ +@interface FaceclawFontRenderer : NSObject ++ (BOOL)canLoadFont:(NSString *)path; ++ (NSString *)getFontName:(NSString *)path; ++ (NSString *)getFontMetrics:(NSString *)path size:(double)size; ++ (double)measureTextExact:(NSString *)path text:(NSString *)text size:(double)size; ++ (NSData *)renderGlyphCell:(NSString *)path size:(double)size codePoint:(NSInteger)codePoint gamma:(double)gamma; ++ (NSData *)renderText:(NSString *)path text:(NSString *)text size:(double)size gamma:(double)gamma; ++ (NSData *)renderWrapped:(NSString *)path text:(NSString *)text size:(double)size width:(NSInteger)width gamma:(double)gamma maxLines:(NSInteger)maxLines; +@end diff --git a/App_Resources/iOS/src/FaceclawFontRenderer.m b/App_Resources/iOS/src/FaceclawFontRenderer.m new file mode 100644 index 00000000..012f36d4 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawFontRenderer.m @@ -0,0 +1,189 @@ +#import "FaceclawFontRenderer.h" +#import +#import +#import + +// Immutable font descriptors may be shared; layout objects never cross threads. +static CTFontRef CreateFont(NSString *path, double size) CF_RETURNS_RETAINED { + if (!path.length || !isfinite(size) || size <= 0 || size > 512) return NULL; + static NSCache *cache; + static dispatch_once_t once; + dispatch_once(&once, ^{ cache = [NSCache new]; cache.countLimit = 32; }); + NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil]; + if (![attrs[NSFileType] isEqual:NSFileTypeRegular]) return NULL; + NSString *key = [NSString stringWithFormat:@"%@:%@:%@", path, attrs[NSFileSize], attrs[NSFileModificationDate]]; + id descriptor = [cache objectForKey:key]; + if (!descriptor) { + NSArray *descriptors = CFBridgingRelease(CTFontManagerCreateFontDescriptorsFromURL((__bridge CFURLRef)[NSURL fileURLWithPath:path])); + descriptor = descriptors.firstObject; // TTC: first face, matching Android. + if (!descriptor) return NULL; // Never silently substitute a system font for an invalid file. + [cache setObject:descriptor forKey:key]; + } + return CTFontCreateWithFontDescriptor((__bridge CTFontDescriptorRef)descriptor, size, NULL); +} +static NSAttributedString *Attributed(CTFontRef font, NSString *text, BOOL glyphLayout) { + NSMutableDictionary *attrs = [@{(__bridge id)kCTFontAttributeName: (__bridge id)font, + (__bridge id)kCTForegroundColorFromContextAttributeName: @YES} mutableCopy]; + // Shared UI lays out codepoints with pair kerning. Whole preview paragraphs + // can use normal ligatures/shaping; UI measurements must match glyph cells. + if (glyphLayout) attrs[(__bridge id)kCTLigatureAttributeName] = @0; + return [[NSAttributedString alloc] initWithString:text attributes:attrs]; +} +static BOOL ValidText(NSString *text) { return text != nil && text.length <= 100000; } +static BOOL ValidRaster(NSInteger width, NSInteger height) { + return width > 0 && height > 0 && width <= 65535 && height <= 65535 && width * height <= 4000000; +} +static CGContextRef CreateContext(NSMutableData *pixels, NSInteger width, NSInteger height) CF_RETURNS_RETAINED { + CGContextRef ctx = CGBitmapContextCreate(pixels.mutableBytes, width, height, 8, width, NULL, (CGBitmapInfo)kCGImageAlphaOnly); + if (ctx) { + CGContextSetShouldAntialias(ctx, true); + CGContextSetShouldSmoothFonts(ctx, false); // Grayscale coverage, never LCD subpixel color. + CGContextSetShouldSubpixelPositionFonts(ctx, true); + CGContextSetShouldSubpixelQuantizeFonts(ctx, false); + CGContextSetTextMatrix(ctx, CGAffineTransformIdentity); + CGContextSetGrayFillColor(ctx, 1, 1); + } + return ctx; +} +static void U16(uint8_t *out, NSInteger value) { out[0] = value & 255; out[1] = (value >> 8) & 255; } +static void GammaCopy(uint8_t *dst, const uint8_t *src, NSInteger count, double gamma) { + if (!isfinite(gamma) || gamma <= 0) gamma = 1; + uint8_t lut[256]; + for (int i = 0; i < 256; i++) lut[i] = (uint8_t)lround(255 * pow(i / 255.0, gamma)); + for (NSInteger i = 0; i < count; i++) dst[i] = lut[src[i]]; +} +static NSData *ImagePacket(NSData *pixels, NSInteger width, NSInteger height, double gamma) { + NSMutableData *out = [NSMutableData dataWithLength:4 + pixels.length]; + uint8_t *dst = out.mutableBytes; + U16(dst, width); U16(dst + 2, height); + GammaCopy(dst + 4, pixels.bytes, pixels.length, gamma); + return out; +} + +@implementation FaceclawFontRenderer ++ (BOOL)canLoadFont:(NSString *)path { + CTFontRef font = CreateFont(path, 16); + if (!font) return NO; + CFRelease(font); return YES; +} ++ (NSString *)getFontName:(NSString *)path { + CTFontRef font = CreateFont(path, 16); + if (!font) return @""; + NSString *family = CFBridgingRelease(CTFontCopyFamilyName(font)); + NSString *style = CFBridgingRelease(CTFontCopyName(font, kCTFontStyleNameKey)); + CFRelease(font); + return [NSString stringWithFormat:@"%@\n%@", family ?: @"", style ?: @""]; +} ++ (NSString *)getFontMetrics:(NSString *)path size:(double)size { + CTFontRef font = CreateFont(path, size); + if (!font) return @""; + NSString *metrics = [NSString stringWithFormat:@"%ld %ld %ld", (long)ceil(CTFontGetAscent(font)), + (long)ceil(CTFontGetDescent(font)), (long)ceil(MAX(0, CTFontGetLeading(font)))]; + CFRelease(font); return metrics; +} ++ (double)measureTextExact:(NSString *)path text:(NSString *)text size:(double)size { + if (!ValidText(text)) return 0; + CTFontRef font = CreateFont(path, size); + if (!font) return 0; + CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)Attributed(font, text, YES)); + double width = CTLineGetTypographicBounds(line, NULL, NULL, NULL); + CFRelease(line); CFRelease(font); return width; +} ++ (NSData *)renderGlyphCell:(NSString *)path size:(double)size codePoint:(NSInteger)cp gamma:(double)gamma { + if (cp < 0 || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff)) return [NSData data]; + CTFontRef font = CreateFont(path, size); + if (!font) return [NSData data]; + uint32_t scalar = (uint32_t)cp; + NSString *text = [[NSString alloc] initWithBytes:&scalar length:4 encoding:NSUTF32LittleEndianStringEncoding]; + CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)Attributed(font, text, YES)); + double advance = CTLineGetTypographicBounds(line, NULL, NULL, NULL); + NSMutableData *out = [NSMutableData dataWithLength:10]; + U16(out.mutableBytes, MAX(0, MIN(65535, lround(advance * 64)))); + CGRect bounds = CTLineGetImageBounds(line, NULL); + if (!CGRectIsEmpty(bounds) && !CGRectIsNull(bounds)) { + NSInteger left = floor(CGRectGetMinX(bounds)) - 2, top = ceil(CGRectGetMaxY(bounds)) + 2; + NSInteger bottom = floor(CGRectGetMinY(bounds)) - 2; + NSInteger width = ceil(CGRectGetMaxX(bounds)) + 2 - left, height = top - bottom; + if (!ValidRaster(width, height)) { CFRelease(line); CFRelease(font); return [NSData data]; } + NSMutableData *pixels = [NSMutableData dataWithLength:width * height]; + CGContextRef ctx = CreateContext(pixels, width, height); + if (!ctx) { CFRelease(line); CFRelease(font); return [NSData data]; } + CGContextSetTextPosition(ctx, -left, -bottom); CTLineDraw(line, ctx); CGContextRelease(ctx); + const uint8_t *src = pixels.bytes; + NSInteger x0 = width, y0 = height, x1 = -1, y1 = -1; + for (NSInteger y = 0; y < height; y++) for (NSInteger x = 0; x < width; x++) if (src[y * width + x]) { + x0 = MIN(x0, x); x1 = MAX(x1, x); y0 = MIN(y0, y); y1 = MAX(y1, y); + } + if (x1 >= x0) { + NSInteger w = x1 - x0 + 1, h = y1 - y0 + 1; + [out setLength:10 + w * h]; uint8_t *dst = out.mutableBytes; + U16(dst + 2, left + x0); U16(dst + 4, ceil(CTFontGetAscent(font)) - top + y0); + U16(dst + 6, w); U16(dst + 8, h); + for (NSInteger y = 0; y < h; y++) GammaCopy(dst + 10 + y * w, src + (y0 + y) * width + x0, w, gamma); + } + } + CFRelease(line); CFRelease(font); return out; +} ++ (NSData *)renderText:(NSString *)path text:(NSString *)text size:(double)size gamma:(double)gamma { + if (!ValidText(text)) return [NSData data]; + CTFontRef font = CreateFont(path, size); + if (!font) return [NSData data]; + CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)Attributed(font, text, NO)); + CGRect bounds = CTLineGetImageBounds(line, NULL); + BOOL ink = !CGRectIsNull(bounds) && !CGRectIsEmpty(bounds); + NSInteger left = ink ? MIN(0, floor(CGRectGetMinX(bounds))) : 0; + NSInteger right = MAX(1, ceil(CTLineGetTypographicBounds(line, NULL, NULL, NULL))); + NSInteger top = ceil(CTFontGetAscent(font)), bottom = -ceil(CTFontGetDescent(font)); + if (ink) { right = MAX(right, ceil(CGRectGetMaxX(bounds))); top = MAX(top, ceil(CGRectGetMaxY(bounds))); bottom = MIN(bottom, floor(CGRectGetMinY(bounds))); } + NSInteger width = right - left, height = top - bottom; + NSData *result = [NSData data]; + if (ValidRaster(width, height)) { + NSMutableData *pixels = [NSMutableData dataWithLength:width * height]; + CGContextRef ctx = CreateContext(pixels, width, height); + if (ctx) { + CGContextSetTextPosition(ctx, -left, -bottom); CTLineDraw(line, ctx); CGContextRelease(ctx); + result = ImagePacket(pixels, width, height, gamma); + } + } + CFRelease(line); CFRelease(font); return result; +} ++ (NSData *)renderWrapped:(NSString *)path text:(NSString *)text size:(double)size width:(NSInteger)width gamma:(double)gamma maxLines:(NSInteger)maxLines { + if (!ValidText(text) || width <= 0 || width > 65535 || maxLines <= 0) return [NSData data]; + CTFontRef font = CreateFont(path, size); + if (!font) return [NSData data]; + NSInteger ascent = ceil(CTFontGetAscent(font)), descent = ceil(CTFontGetDescent(font)); + NSInteger step = MAX(1, ascent + descent + (NSInteger)ceil(MAX(0, CTFontGetLeading(font)))); + maxLines = MIN(maxLines, MIN(65535 / step, 4000000 / width / step)); + if (maxLines <= 0) { CFRelease(font); return [NSData data]; } + NSAttributedString *attributed = Attributed(font, text, NO); + CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributed); + NSMutableArray *lines = [NSMutableArray new]; + CFIndex start = 0; + while (start < (CFIndex)text.length && lines.count < (NSUInteger)maxLines) { + CFIndex count = CTTypesetterSuggestLineBreak(typesetter, start, width); + if (count <= 0) count = [text rangeOfComposedCharacterSequenceAtIndex:start].length; + CTLineRef line; + if (lines.count == (NSUInteger)maxLines - 1 && start + count < (CFIndex)text.length) { + NSString *rest = [[text substringFromIndex:start] stringByReplacingOccurrencesOfString:@"\n" withString:@" "]; + CTLineRef full = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)Attributed(font, rest, NO)); + CTLineRef token = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)Attributed(font, @"…", NO)); + line = CTLineCreateTruncatedLine(full, width, kCTLineTruncationEnd, token); + if (!line) line = CFRetain(token); + CFRelease(full); CFRelease(token); + } else line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, count)); + [lines addObject:CFBridgingRelease(line)]; start += count; + } + NSInteger height = MAX(1, lines.count) * step; + NSMutableData *pixels = [NSMutableData dataWithLength:width * height]; + CGContextRef ctx = CreateContext(pixels, width, height); + NSData *result = [NSData data]; + if (ctx) { + for (NSUInteger i = 0; i < lines.count; i++) { + CGContextSetTextPosition(ctx, 0, height - ascent - i * step); + CTLineDraw((__bridge CTLineRef)lines[i], ctx); + } + CGContextRelease(ctx); result = ImagePacket(pixels, width, height, gamma); + } + CFRelease(typesetter); CFRelease(font); return result; +} +@end diff --git a/App_Resources/iOS/src/FaceclawGraphics.h b/App_Resources/iOS/src/FaceclawGraphics.h new file mode 100644 index 00000000..1a8eacf7 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawGraphics.h @@ -0,0 +1,8 @@ +#import + +@interface FaceclawGraphics : NSObject ++ (UIImage *)preview:(NSData *)gray width:(NSInteger)width height:(NSInteger)height green:(BOOL)green; ++ (NSData *)renderSVG:(NSString *)svg size:(NSInteger)size; ++ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height; ++ (NSData *)decodeImageFile:(NSString *)path maxWidth:(NSInteger)maxWidth maxHeight:(NSInteger)maxHeight; +@end diff --git a/App_Resources/iOS/src/FaceclawGraphics.m b/App_Resources/iOS/src/FaceclawGraphics.m new file mode 100644 index 00000000..0c9e746c --- /dev/null +++ b/App_Resources/iOS/src/FaceclawGraphics.m @@ -0,0 +1,106 @@ +#import "FaceclawGraphics.h" +#import +#import + +@implementation FaceclawGraphics ++ (NSData *)decodeImageFile:(NSString *)path maxWidth:(NSInteger)maxWidth maxHeight:(NSInteger)maxHeight { + if (maxWidth <= 0 || maxHeight <= 0 || maxWidth > 2048 || maxHeight > 2048) return nil; + NSURL *url = [NSURL fileURLWithPath:path]; + CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL); + if (!source) return nil; + NSDictionary *properties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, NULL)); + CFRelease(source); + double width = [properties[(id)kCGImagePropertyPixelWidth] doubleValue]; + double height = [properties[(id)kCGImagePropertyPixelHeight] doubleValue]; + if (width <= 0 || height <= 0) return nil; + NSInteger orientation = [properties[(id)kCGImagePropertyOrientation] integerValue]; + if (orientation >= 5 && orientation <= 8) { double swap = width; width = height; height = swap; } + double scale = MIN(1.0, MIN(maxWidth / width, maxHeight / height)); + NSInteger w = MAX(1, lround(width * scale)), h = MAX(1, lround(height * scale)); + NSData *bytes = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:nil]; + NSData *gray = [self decodeImage:bytes width:w height:h]; + if (!gray) return nil; + // Same dimensions + pixels packet used by the shared image adapter. + uint8_t dimensions[] = { w & 255, (w >> 8) & 255, h & 255, (h >> 8) & 255 }; + NSMutableData *packet = [NSMutableData dataWithBytes:dimensions length:4]; + [packet appendData:gray]; + return packet; +} ++ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height { + if (!data.length || width <= 0 || height <= 0 || width > 2048 || height > 2048) return nil; + CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); + if (!source) return nil; + NSDictionary *options = @{(id)kCGImageSourceCreateThumbnailFromImageAlways: @YES, + (id)kCGImageSourceCreateThumbnailWithTransform: @YES, + (id)kCGImageSourceThumbnailMaxPixelSize: @(MAX(width, height))}; + CGImageRef image = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef)options); + CFRelease(source); + if (!image) return nil; + NSMutableData *rgba = [NSMutableData dataWithLength:width * height * 4]; + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + CGContextRef context = CGBitmapContextCreate(rgba.mutableBytes, width, height, 8, width * 4, space, + kCGBitmapByteOrder32Big | (CGBitmapInfo)kCGImageAlphaPremultipliedLast); + CGColorSpaceRelease(space); + if (!context) { CGImageRelease(image); return nil; } + // ImageIO applies image orientation; the bitmap's row order is top-to-bottom. + CGContextDrawImage(context, CGRectMake(0, 0, width, height), image); + NSMutableData *gray = [NSMutableData dataWithLength:width * height]; + const uint8_t *src = rgba.bytes; + uint8_t *dst = gray.mutableBytes; + for (NSInteger i = 0; i < width * height; i++) + dst[i] = (uint8_t)round(0.2126 * src[i * 4] + 0.7152 * src[i * 4 + 1] + 0.0722 * src[i * 4 + 2]); + CGContextRelease(context); CGImageRelease(image); + return gray; +} ++ (UIImage *)preview:(NSData *)gray width:(NSInteger)width height:(NSInteger)height green:(BOOL)green { + if (width <= 0 || height <= 0 || gray.length != width * height) return nil; + NSMutableData *rgba = [NSMutableData dataWithLength:width * height * 4]; + const uint8_t *src = gray.bytes; + uint8_t *dst = rgba.mutableBytes; + uint8_t lut[256]; + for (int i = 0; i < 256; i++) lut[i] = (uint8_t)round(pow(i / 255.0, 0.7) * 255.0); + for (NSInteger i = 0; i < width * height; i++) { + uint8_t value = lut[src[i]]; + dst[i * 4] = green ? 0 : value; + dst[i * 4 + 1] = value; + dst[i * 4 + 2] = green ? 0 : value; + dst[i * 4 + 3] = 255; + } + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)rgba); + CGImageRef image = CGImageCreate(width, height, 8, 32, width * 4, space, + kCGBitmapByteOrder32Big | (CGBitmapInfo)kCGImageAlphaPremultipliedLast, provider, NULL, false, kCGRenderingIntentDefault); + UIImage *result = image ? [UIImage imageWithCGImage:image scale:1 orientation:UIImageOrientationUp] : nil; + if (image) CGImageRelease(image); + CGDataProviderRelease(provider); + CGColorSpaceRelease(space); + return result; +} ++ (NSData *)renderSVG:(NSString *)svg size:(NSInteger)size { + if (size <= 0 || size > 1024) return nil; + SVGKImage *image = [SVGKImage imageWithData:[svg dataUsingEncoding:NSUTF8StringEncoding]]; + if (!image) return nil; + image.size = CGSizeMake(size, size); + UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat]; + format.scale = 1; + format.opaque = NO; + UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:image.size format:format]; + UIImage *raster = [renderer imageWithActions:^(UIGraphicsImageRendererContext *context) { + [image.CALayerTree renderInContext:context.CGContext]; + }]; + NSMutableData *rgba = [NSMutableData dataWithLength:size * size * 4]; + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + CGContextRef context = CGBitmapContextCreate(rgba.mutableBytes, size, size, 8, size * 4, space, + kCGBitmapByteOrder32Big | (CGBitmapInfo)kCGImageAlphaPremultipliedLast); + CGContextDrawImage(context, CGRectMake(0, 0, size, size), raster.CGImage); + NSMutableData *gray = [NSMutableData dataWithLength:size * size]; + const uint8_t *src = rgba.bytes; + uint8_t *dst = gray.mutableBytes; + // Icons are monochrome coverage masks, matching Android IconRenderer. + // Source black/currentColor shapes must remain visible on black lenses. + for (NSInteger i = 0; i < size * size; i++) dst[i] = src[i * 4 + 3]; + CGContextRelease(context); + CGColorSpaceRelease(space); + return gray; +} +@end diff --git a/App_Resources/iOS/src/FaceclawLc3Decoder.h b/App_Resources/iOS/src/FaceclawLc3Decoder.h new file mode 100644 index 00000000..e9af329d --- /dev/null +++ b/App_Resources/iOS/src/FaceclawLc3Decoder.h @@ -0,0 +1,11 @@ +#import +/** One decoder per capture. Stock G2 packets: 5x40 LC3 bytes, DSP trailer, + * counter. Output: 800 mono PCM16LE samples at 16 kHz (50 ms). + */ +@interface FaceclawLc3Decoder : NSObject +- (NSData *)decodePacket:(NSData *)packet; +@property(nonatomic, readonly) NSUInteger packets; +@property(nonatomic, readonly) NSUInteger duplicates; +@property(nonatomic, readonly) NSUInteger missing; +@property(nonatomic, readonly) NSUInteger errors; +@end diff --git a/App_Resources/iOS/src/FaceclawLc3Decoder.m b/App_Resources/iOS/src/FaceclawLc3Decoder.m new file mode 100644 index 00000000..e0ed1bd3 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawLc3Decoder.m @@ -0,0 +1,42 @@ +#import "FaceclawLc3Decoder.h" +#include "lc3/lc3.h" +#include +@interface FaceclawLc3Decoder () { + void *_memory; + lc3_decoder_t _decoder; + int _lastCounter; +} +@property(nonatomic) NSUInteger packets, duplicates, missing, errors; +@end +@implementation FaceclawLc3Decoder +- (instancetype)init { + if ((self = [super init])) { + _lastCounter = -1; + _memory = calloc(1, lc3_decoder_size(10000, 16000)); + if (!_memory) return nil; + _decoder = lc3_setup_decoder(10000, 16000, 0, _memory); + if (!_decoder) return nil; + } + return self; +} +- (void)dealloc { free(_memory); } +- (NSData *)decodePacket:(NSData *)packet { + @synchronized(self) { + if (packet.length != 205) { _errors++; return nil; } + const uint8_t *bytes = packet.bytes; + int counter = bytes[204], gap = _lastCounter < 0 ? 1 : (counter - _lastCounter) & 255; + // Both arms may relay the same packet. Drop duplicates/late copies, + // including across the 255 -> 0 wrap, before touching decoder history. + if (gap == 0 || gap >= 128) { _duplicates++; return nil; } + _missing += gap - 1; + _lastCounter = counter; + NSMutableData *out = [NSMutableData dataWithLength:800 * sizeof(int16_t)]; + int16_t *pcm = out.mutableBytes; + for (int frame = 0; frame < 5; frame++) { + int status = lc3_decode(_decoder, bytes + frame * 40, 40, LC3_PCM_FORMAT_S16, pcm + frame * 160, 1); + if (status < 0) { _errors++; return nil; } + } + _packets++; return out; + } +} +@end diff --git a/App_Resources/iOS/src/FaceclawLocation.h b/App_Resources/iOS/src/FaceclawLocation.h new file mode 100644 index 00000000..9045fbf3 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawLocation.h @@ -0,0 +1,18 @@ +#import + +/** Short, foreground Core Location lookups for the glasses' true-north correction. */ +@interface FaceclawLocation : NSObject ++ (instancetype)shared; +- (BOOL)hasPermission; +- (void)requestPermission:(void (^)(BOOL granted))completion; +- (void)requestPrecisePermission:(void (^)(BOOL granted))completion; +- (void)requestDeclination:(void (^)(double degrees, double latitude, double longitude, NSString *error))completion; +@end + +/** Each lookup/stream owns a separate manager, so stopping one never stops another. Main thread only. */ +@interface FaceclawLocationUpdates : NSObject +@property(nonatomic, copy) void (^eventHandler)(NSString *json); +- (void)startOnce; +- (void)startTracking:(NSInteger)intervalMs; +- (void)stop; +@end diff --git a/App_Resources/iOS/src/FaceclawLocation.m b/App_Resources/iOS/src/FaceclawLocation.m new file mode 100644 index 00000000..0e9b175f --- /dev/null +++ b/App_Resources/iOS/src/FaceclawLocation.m @@ -0,0 +1,197 @@ +#import "FaceclawLocation.h" +#import +#import + +@interface FaceclawLocation () +@property(nonatomic, strong) CLLocationManager *manager; +@property(nonatomic, strong) NSMutableArray *permissionCallbacks; +@property(nonatomic, strong) NSMutableArray *declinationCallbacks; +@property(nonatomic, strong) NSTimer *timer; +@property(nonatomic, strong) CLLocation *location; +@property(nonatomic, strong) CLHeading *heading; +@end + +@implementation FaceclawLocation ++ (instancetype)shared { + static FaceclawLocation *instance; + static dispatch_once_t once; + dispatch_once(&once, ^{ instance = [FaceclawLocation new]; }); + return instance; +} +- (instancetype)init { + if ((self = [super init])) { + _permissionCallbacks = [NSMutableArray new]; + _declinationCallbacks = [NSMutableArray new]; + _manager = [CLLocationManager new]; + _manager.delegate = self; + _manager.desiredAccuracy = kCLLocationAccuracyKilometer; + } + return self; +} +- (BOOL)hasPermission { + CLAuthorizationStatus status = self.manager.authorizationStatus; + return status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse; +} +- (void)requestPermission:(void (^)(BOOL))completion { + if (self.manager.authorizationStatus != kCLAuthorizationStatusNotDetermined) { + completion([self hasPermission]); + return; + } + [self.permissionCallbacks addObject:[completion copy]]; + [self.manager requestWhenInUseAuthorization]; +} +- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager { + if (manager.authorizationStatus == kCLAuthorizationStatusNotDetermined) return; + NSArray *callbacks = [self.permissionCallbacks copy]; + [self.permissionCallbacks removeAllObjects]; + for (void (^callback)(BOOL) in callbacks) callback([self hasPermission]); + if (![self hasPermission]) [self finishWithError:@"Location permission is required for true north"]; +} +- (void)requestPrecisePermission:(void (^)(BOOL))completion { + [self requestPermission:^(BOOL granted) { + if (!granted || self.manager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy) { + completion(granted); + return; + } + [self.manager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"Navigation" completion:^(NSError *error) { + completion([self hasPermission] && self.manager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy); + }]; + }]; +} +- (void)requestDeclination:(void (^)(double, double, double, NSString *))completion { + if (![self hasPermission] || ![CLLocationManager headingAvailable]) { + completion(0, 0, 0, @"Location or heading unavailable for true north"); + return; + } + [self.declinationCallbacks addObject:[completion copy]]; + if (self.timer) return; + self.location = nil; + self.heading = nil; + // trueHeading is valid only while the same manager receives location updates. + // Subtracting magneticHeading cancels the phone's orientation: the result is + // a local correction to the GLASSES heading, never the phone's heading. + [self.manager startUpdatingLocation]; + [self.manager startUpdatingHeading]; + self.timer = [NSTimer scheduledTimerWithTimeInterval:20 repeats:NO block:^(NSTimer *timer) { + [self finishWithError:@"Timed out obtaining the true-north correction"]; + }]; +} +- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { + CLLocation *location = locations.lastObject; + if (!location || location.horizontalAccuracy < 0 || fabs(location.timestamp.timeIntervalSinceNow) > 60) return; + self.location = location; + [self finishIfReady]; +} +- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading { + if (heading.headingAccuracy < 0 || heading.trueHeading < 0 || heading.magneticHeading < 0 || + fabs(heading.timestamp.timeIntervalSinceNow) > 60) return; + self.heading = heading; + [self finishIfReady]; +} +- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager { return NO; } +- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { + if (error.code != kCLErrorLocationUnknown) [self finishWithError:error.localizedDescription]; +} +- (void)finishIfReady { + if (self.location && self.heading) [self finishWithError:@""]; +} +- (void)finishWithError:(NSString *)error { + double degrees = self.heading.trueHeading - self.heading.magneticHeading; + if (degrees > 180) degrees -= 360; + if (degrees < -180) degrees += 360; + CLLocationCoordinate2D coordinate = self.location.coordinate; + NSArray *callbacks = [self.declinationCallbacks copy]; + [self.declinationCallbacks removeAllObjects]; + [self.timer invalidate]; self.timer = nil; + [self.manager stopUpdatingLocation]; + [self.manager stopUpdatingHeading]; + self.location = nil; self.heading = nil; + for (void (^callback)(double, double, double, NSString *) in callbacks) + callback(degrees, coordinate.latitude, coordinate.longitude, error); +} +@end + +@interface FaceclawLocationUpdates () +@property(nonatomic, strong) CLLocationManager *manager; +@property(nonatomic, strong) NSTimer *timeout; +@property(nonatomic) BOOL running; +@property(nonatomic) BOOL once; +@property(nonatomic) NSTimeInterval interval; +@property(nonatomic) NSTimeInterval lastTimestamp; +@end + +@implementation FaceclawLocationUpdates +- (void)startOnce { [self start:YES interval:0]; } +- (void)startTracking:(NSInteger)intervalMs { [self start:NO interval:MAX(500, intervalMs) / 1000.0]; } +- (void)start:(BOOL)once interval:(NSTimeInterval)interval { + if (self.running) return; + self.once = once; self.interval = interval; self.lastTimestamp = 0; + self.manager = [CLLocationManager new]; + self.manager.delegate = self; + CLAuthorizationStatus status = self.manager.authorizationStatus; + if (status != kCLAuthorizationStatusAuthorizedAlways && status != kCLAuthorizationStatusAuthorizedWhenInUse) { + [self fail:@"Location permission is required. Allow location in the phone's Settings, then retry."]; return; + } + if (!once && self.manager.accuracyAuthorization != CLAccuracyAuthorizationFullAccuracy) { + [self fail:@"Precise location is required for navigation. Enable Precise Location in the phone's Settings."]; return; + } + self.running = YES; + self.manager.desiredAccuracy = once ? kCLLocationAccuracyKilometer : kCLLocationAccuracyBestForNavigation; + self.manager.distanceFilter = kCLDistanceFilterNone; + if (!once) { + self.manager.activityType = CLActivityTypeOtherNavigation; + self.manager.pausesLocationUpdatesAutomatically = NO; + self.manager.allowsBackgroundLocationUpdates = YES; + self.manager.showsBackgroundLocationIndicator = YES; + } + [self.manager startUpdatingLocation]; + if (once) self.timeout = [NSTimer scheduledTimerWithTimeInterval:20 repeats:NO block:^(NSTimer *timer) { + [self fail:@"Couldn't get a current location. Check Location Services and retry."]; + }]; +} +- (void)stop { + self.running = NO; + [self.timeout invalidate]; self.timeout = nil; + [self.manager stopUpdatingLocation]; + self.manager.delegate = nil; self.manager = nil; + self.eventHandler = nil; +} +- (void)emit:(NSDictionary *)event { + NSData *data = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil]; + if (data && self.eventHandler) self.eventHandler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); +} +- (void)fail:(NSString *)message { + void (^handler)(NSString *) = self.eventHandler; + [self stop]; + NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"error": message} options:0 error:nil]; + if (handler) handler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); +} +- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager { + if (!self.running) return; + CLAuthorizationStatus status = manager.authorizationStatus; + if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted || + (!self.once && manager.accuracyAuthorization != CLAccuracyAuthorizationFullAccuracy)) + [self fail:@"Location permission changed. Allow precise location in the phone's Settings, then retry."]; +} +- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { + if (!self.running) return; + CLLocation *fix = locations.lastObject; + if (!fix || fix.horizontalAccuracy < 0 || fabs(fix.timestamp.timeIntervalSinceNow) > 60) return; + NSTimeInterval timestamp = fix.timestamp.timeIntervalSince1970; + if (self.lastTimestamp && timestamp - self.lastTimestamp < self.interval) return; + self.lastTimestamp = timestamp; + NSDictionary *event = @{@"latitude": @(fix.coordinate.latitude), @"longitude": @(fix.coordinate.longitude), + @"accuracyMeters": @(fix.horizontalAccuracy), @"timestampMs": @(timestamp * 1000), + @"bearingDeg": fix.course >= 0 ? @(fix.course) : NSNull.null, + @"speedMps": fix.speed >= 0 ? @(fix.speed) : NSNull.null}; + if (self.once) { + void (^handler)(NSString *) = self.eventHandler; + [self stop]; + NSData *data = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil]; + if (handler) handler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); + } else [self emit:event]; +} +- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { + if (self.running && error.code != kCLErrorLocationUnknown) [self fail:error.localizedDescription]; +} +@end diff --git a/App_Resources/iOS/src/FaceclawQrScanner.h b/App_Resources/iOS/src/FaceclawQrScanner.h new file mode 100644 index 00000000..fd6f0c6a --- /dev/null +++ b/App_Resources/iOS/src/FaceclawQrScanner.h @@ -0,0 +1,8 @@ +#import + +/** A single foreground QR scan. Completion runs once, after the camera closes. */ +@interface FaceclawQrScanner : NSObject ++ (BOOL)isAvailable; +- (void)startWithCompletion:(void (^)(NSString *text, NSString *error))completion; +- (void)cancel; +@end diff --git a/App_Resources/iOS/src/FaceclawQrScanner.m b/App_Resources/iOS/src/FaceclawQrScanner.m new file mode 100644 index 00000000..5762f6af --- /dev/null +++ b/App_Resources/iOS/src/FaceclawQrScanner.m @@ -0,0 +1,186 @@ +#import "FaceclawQrScanner.h" +#import +#import + +@interface FaceclawQrScreen : UIViewController +@property(nonatomic) AVCaptureVideoPreviewLayer *preview; +@property(nonatomic, copy) void (^cancelHandler)(void); +@end + +@implementation FaceclawQrScreen +- (void)viewDidLoad { + [super viewDidLoad]; + self.view.backgroundColor = UIColor.blackColor; + self.title = @"Scan app QR code"; + self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] + initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelScan)]; + UILabel *instructions = [UILabel new]; + instructions.text = @"Point the camera at the app's QR code."; + instructions.textColor = UIColor.whiteColor; + instructions.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.7]; + instructions.textAlignment = NSTextAlignmentCenter; + instructions.numberOfLines = 0; + instructions.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; + instructions.adjustsFontForContentSizeCategory = YES; + instructions.translatesAutoresizingMaskIntoConstraints = NO; + [self.view addSubview:instructions]; + [NSLayoutConstraint activateConstraints:@[ + [instructions.leadingAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.leadingAnchor constant:20], + [instructions.trailingAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.trailingAnchor constant:-20], + [instructions.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor constant:-24], + ]]; +} +- (void)cancelScan { if (_cancelHandler) _cancelHandler(); } +- (void)viewDidLayoutSubviews { + [super viewDidLayoutSubviews]; + _preview.frame = self.view.bounds; + UIInterfaceOrientation orientation = self.view.window.windowScene.interfaceOrientation; + if (_preview.connection.isVideoOrientationSupported && orientation != UIInterfaceOrientationUnknown) + _preview.connection.videoOrientation = (AVCaptureVideoOrientation)orientation; +} +@end + +@interface FaceclawQrScanner () +@property(nonatomic) AVCaptureSession *session; +@property(nonatomic) AVCaptureMetadataOutput *output; +@property(nonatomic) dispatch_queue_t cameraQueue; +@property(nonatomic) FaceclawQrScreen *screen; +@property(nonatomic) UINavigationController *navigation; +@property(nonatomic, copy) void (^completion)(NSString *, NSString *); +@property(atomic) BOOL finished; +@property(nonatomic) BOOL presenting; +@property(nonatomic) BOOL stopped; +@property(nonatomic) BOOL closing; +@property(nonatomic) NSString *resultText; +@property(nonatomic) NSString *resultError; +@end + +@implementation FaceclawQrScanner ++ (BOOL)isAvailable { return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] != nil; } +- (void)startWithCompletion:(void (^)(NSString *, NSString *))completion { + NSAssert(NSThread.isMainThread, @"Start QR scanner on the main thread"); + if (_completion || _finished) { completion(nil, @"This QR scanner has already been used."); return; } + _completion = [completion copy]; + _cameraQueue = dispatch_queue_create("com.faceclaw.qr-camera", DISPATCH_QUEUE_SERIAL); + if (UIApplication.sharedApplication.applicationState != UIApplicationStateActive) { + [self finish:nil error:@"Open Faceclaw on your iPhone to scan a QR code."]; return; + } + if (![FaceclawQrScanner isAvailable]) { + [self finish:nil error:@"No camera is available on this device."]; return; + } + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(backgrounded:) + name:UIApplicationDidEnterBackgroundNotification object:nil]; + AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; + if (status == AVAuthorizationStatusNotDetermined) { + [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.finished) return; + if (granted) [self presentScanner]; + else [self finish:nil error:@"Camera access denied. Allow Camera for Faceclaw in iPhone Settings, then scan again."]; + }); + }]; + } else if (status == AVAuthorizationStatusAuthorized) [self presentScanner]; + else [self finish:nil error:@"Camera access is unavailable. Check Camera permission for Faceclaw in iPhone Settings."]; +} +- (void)presentScanner { + if (_finished) return; + UIViewController *presenter = nil; + for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) { + if (![scene isKindOfClass:UIWindowScene.class] || scene.activationState != UISceneActivationStateForegroundActive) continue; + for (UIWindow *window in ((UIWindowScene *)scene).windows) + if (window.isKeyWindow) presenter = window.rootViewController; + } + // NativeScript also supports the legacy, non-scene app lifecycle. + if (!presenter) presenter = UIApplication.sharedApplication.keyWindow.rootViewController; + while (presenter.presentedViewController) presenter = presenter.presentedViewController; + if (!presenter.view.window || presenter.isBeingDismissed || presenter.isBeingPresented || [presenter isKindOfClass:UIAlertController.class]) { + [self finish:nil error:@"Close the phone dialog and open Faceclaw to scan again."]; return; + } + _screen = [FaceclawQrScreen new]; + __weak FaceclawQrScanner *weakSelf = self; + _screen.cancelHandler = ^{ [weakSelf cancel]; }; + _navigation = [[UINavigationController alloc] initWithRootViewController:_screen]; + _navigation.modalPresentationStyle = UIModalPresentationFullScreen; + _navigation.overrideUserInterfaceStyle = UIUserInterfaceStyleDark; + _presenting = YES; + [presenter presentViewController:_navigation animated:YES completion:^{ + self.presenting = NO; + if (self.finished) [self closeScanner]; + else [self configureCamera]; + }]; +} +- (void)configureCamera { + _session = [AVCaptureSession new]; + _screen.preview = [AVCaptureVideoPreviewLayer layerWithSession:_session]; + _screen.preview.videoGravity = AVLayerVideoGravityResizeAspectFill; + [_screen.view.layer insertSublayer:_screen.preview atIndex:0]; + [_screen.view setNeedsLayout]; + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(cameraFailed:) + name:AVCaptureSessionRuntimeErrorNotification object:_session]; + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(cameraInterrupted:) + name:AVCaptureSessionWasInterruptedNotification object:_session]; + dispatch_async(_cameraQueue, ^{ + if (self.finished) return; + NSError *error = nil; + AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; + AVCaptureDeviceInput *input = camera ? [AVCaptureDeviceInput deviceInputWithDevice:camera error:&error] : nil; + AVCaptureMetadataOutput *output = [AVCaptureMetadataOutput new]; + [self.session beginConfiguration]; + if (input && [self.session canAddInput:input]) [self.session addInput:input]; + if (self.session.inputs.count && [self.session canAddOutput:output]) [self.session addOutput:output]; + BOOL ready = [output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]; + if (ready) { + self.output = output; + [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; + output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode]; + } + [self.session commitConfiguration]; + if (!ready) { + dispatch_async(dispatch_get_main_queue(), ^{ [self finish:nil error:error.localizedDescription ?: @"Could not start the QR camera. Try scanning again."]; }); + return; + } + dispatch_async(dispatch_get_main_queue(), ^{ [self.screen.view setNeedsLayout]; }); + if (!self.finished) [self.session startRunning]; + }); +} +- (void)metadataOutput:(AVCaptureMetadataOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)objects fromConnection:(AVCaptureConnection *)connection { + for (AVMetadataObject *object in objects) { + if (![object.type isEqual:AVMetadataObjectTypeQRCode] || ![object isKindOfClass:AVMetadataMachineReadableCodeObject.class]) continue; + NSString *text = ((AVMetadataMachineReadableCodeObject *)object).stringValue; + if (text.length) { [self finish:text error:nil]; return; } + } +} +- (void)cameraFailed:(NSNotification *)notification { + dispatch_async(dispatch_get_main_queue(), ^{ [self finish:nil error:@"The camera stopped. Try scanning again."]; }); +} +- (void)cameraInterrupted:(NSNotification *)notification { + dispatch_async(dispatch_get_main_queue(), ^{ [self finish:nil error:@"Camera interrupted. Return to Faceclaw and scan again."]; }); +} +- (void)backgrounded:(NSNotification *)notification { [self cancel]; } +- (void)cancel { [self finish:nil error:nil]; } +- (void)finish:(NSString *)text error:(NSString *)error { + if (_finished) return; + self.finished = YES; + _resultText = text; _resultError = error; + [NSNotificationCenter.defaultCenter removeObserver:self]; + // Serialize teardown after any pending setup/start. Never block the UI. + dispatch_async(_cameraQueue, ^{ + [self.output setMetadataObjectsDelegate:nil queue:NULL]; + [self.session stopRunning]; + dispatch_async(dispatch_get_main_queue(), ^{ self.stopped = YES; [self closeScanner]; }); + }); +} +- (void)closeScanner { + if (!_stopped || _presenting || _closing) return; + _closing = YES; + void (^done)(void) = ^{ + void (^completion)(NSString *, NSString *) = self.completion; + self.completion = nil; + self.screen.preview.session = nil; + self.screen = nil; self.navigation = nil; self.session = nil; self.output = nil; + if (completion) completion(self.resultText, self.resultError); + }; + if (_navigation.presentingViewController) [_navigation dismissViewControllerAnimated:YES completion:done]; + else done(); +} +@end diff --git a/App_Resources/iOS/src/FaceclawSocket.h b/App_Resources/iOS/src/FaceclawSocket.h new file mode 100644 index 00000000..84a058c5 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawSocket.h @@ -0,0 +1,8 @@ +#import +/** Thread-safe event queue; each NativeScript worker drains it on its own thread. */ +@interface FaceclawSocket : NSObject +- (instancetype)initWithURL:(NSString *)url; +- (NSString *)takeEvents; +- (void)sendText:(NSString *)text; +- (void)close; +@end diff --git a/App_Resources/iOS/src/FaceclawSocket.m b/App_Resources/iOS/src/FaceclawSocket.m new file mode 100644 index 00000000..a53e6e6b --- /dev/null +++ b/App_Resources/iOS/src/FaceclawSocket.m @@ -0,0 +1,75 @@ +#import "FaceclawSocket.h" +@interface FaceclawSocket () +@property(nonatomic, strong) NSURLSession *session; +@property(nonatomic, strong) NSURLSessionWebSocketTask *task; +@property(nonatomic, strong) NSMutableArray *events; +@property(nonatomic) BOOL ended; +@property(nonatomic) NSUInteger queuedBytes; +@end +@implementation FaceclawSocket +- (instancetype)initWithURL:(NSString *)url { + if ((self = [super init])) { + _events = [NSMutableArray array]; + NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration]; + config.timeoutIntervalForRequest = 20; + _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; + _task = [_session webSocketTaskWithURL:[NSURL URLWithString:url]]; + _task.maximumMessageSize = 8 * 1024 * 1024; + [_task resume]; + } + return self; +} +- (void)enqueue:(NSDictionary *)event { + @synchronized(self) { + if (_ended) return; + _queuedBytes += [event[@"text"] lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; + if (_events.count >= 256 || _queuedBytes > 16 * 1024 * 1024) { + [_events removeAllObjects]; _queuedBytes = 0; + [_events addObject:@{@"kind": @"error", @"text": @"Terminal input queue overflow"}]; + [self close]; + } else [_events addObject:event]; + } +} +- (NSString *)takeEvents { + @synchronized(self) { + NSData *data = [NSJSONSerialization dataWithJSONObject:_events options:0 error:nil]; + [_events removeAllObjects]; _queuedBytes = 0; + return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"[]"; + } +} +- (void)receiveNext { + __weak FaceclawSocket *weakSelf = self; + [_task receiveMessageWithCompletionHandler:^(NSURLSessionWebSocketMessage *message, NSError *error) { + FaceclawSocket *owner = weakSelf; if (!owner) return; + @synchronized(owner) { if (owner.ended) return; } + if (error) { [owner enqueue:@{@"kind": @"error", @"text": error.localizedDescription}]; [owner close]; return; } + if (message.type == NSURLSessionWebSocketMessageTypeString) + [owner enqueue:@{@"kind": @"text", @"text": message.string}]; + [owner receiveNext]; + }]; +} +- (void)URLSession:(NSURLSession *)session webSocketTask:(NSURLSessionWebSocketTask *)task didOpenWithProtocol:(NSString *)protocol { + [self enqueue:@{@"kind": @"open"}]; [self receiveNext]; +} +- (void)URLSession:(NSURLSession *)session webSocketTask:(NSURLSessionWebSocketTask *)task didCloseWithCode:(NSURLSessionWebSocketCloseCode)code reason:(NSData *)reason { + [self enqueue:@{@"kind": @"closed", @"code": @(code), @"text": reason ? ([[NSString alloc] initWithData:reason encoding:NSUTF8StringEncoding] ?: @"") : @""}]; + [self close]; +} +- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { + if (error) [self enqueue:@{@"kind": @"error", @"text": error.localizedDescription}]; + [self close]; +} +- (void)sendText:(NSString *)text { + __weak FaceclawSocket *weakSelf = self; + [_task sendMessage:[[NSURLSessionWebSocketMessage alloc] initWithString:text] completionHandler:^(NSError *error) { + if (error) { [weakSelf enqueue:@{@"kind": @"error", @"text": error.localizedDescription}]; [weakSelf close]; } + }]; +} +- (void)close { + @synchronized(self) { + if (_ended) return; _ended = YES; + [_task cancelWithCloseCode:NSURLSessionWebSocketCloseCodeNormalClosure reason:nil]; + [_session invalidateAndCancel]; _session = nil; + } +} +@end diff --git a/App_Resources/iOS/src/FaceclawSpeech.h b/App_Resources/iOS/src/FaceclawSpeech.h new file mode 100644 index 00000000..8f647292 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawSpeech.h @@ -0,0 +1,15 @@ +#import +/** On-device speech recognition fed by G2 BLE audio or the default phone microphone. */ +@interface FaceclawSpeech : NSObject +@property(nonatomic, copy) void (^eventHandler)(NSString *json); ++ (NSInteger)authorizationStatus; ++ (NSInteger)microphoneAuthorizationStatus; ++ (void)requestMicrophoneAuthorization:(void (^)(NSInteger status))completion; ++ (void)requestAuthorization:(void (^)(NSInteger status))completion; +/** Returns an actionable error, or an empty string when capture starts. */ +- (NSString *)startWithEndpointing:(BOOL)endpointing; +- (NSString *)startPhoneWithEndpointing:(BOOL)endpointing; +- (void)acceptPacket:(NSData *)packet; +- (void)finish; +- (void)cancel; +@end diff --git a/App_Resources/iOS/src/FaceclawSpeech.m b/App_Resources/iOS/src/FaceclawSpeech.m new file mode 100644 index 00000000..5487011e --- /dev/null +++ b/App_Resources/iOS/src/FaceclawSpeech.m @@ -0,0 +1,249 @@ +#import "FaceclawSpeech.h" +#import "FaceclawLc3Decoder.h" +#import "FaceclawSpeechTranscript.h" +#import +#import +#import +#import + +@interface FaceclawSpeech () +@property(nonatomic, strong) SFSpeechRecognizer *recognizer; +@property(nonatomic, strong) SFSpeechAudioBufferRecognitionRequest *request; +@property(nonatomic, strong) SFSpeechRecognitionTask *task; +@property(nonatomic, strong) FaceclawLc3Decoder *decoder; +@property(nonatomic, strong) dispatch_queue_t audioQueue; +@property(atomic) NSUInteger generation; +@property(nonatomic) NSUInteger pendingPackets; +@property(atomic) BOOL accepting; +@property(nonatomic, strong) AVAudioEngine *phoneEngine; +@property(nonatomic) BOOL phoneTapInstalled; +@property(nonatomic) BOOL phoneSessionActive; +@property(nonatomic, strong) id interruptionObserver; +@property(nonatomic, copy) NSString *bestText; +@property(nonatomic, strong) FaceclawSpeechTranscript *transcript; +@property(nonatomic, strong) FaceclawKitVoiceEndpointDetector *endpointDetector; +@end +@implementation FaceclawSpeech +- (instancetype)init { + if ((self = [super init])) _audioQueue = dispatch_queue_create("com.faceclaw.glasses-speech", DISPATCH_QUEUE_SERIAL); + return self; +} ++ (NSInteger)microphoneAuthorizationStatus { return [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]; } ++ (void)requestMicrophoneAuthorization:(void (^)(NSInteger))completion { + [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) { + dispatch_async(dispatch_get_main_queue(), ^{ completion(granted ? 3 : 2); }); + }]; +} ++ (NSInteger)authorizationStatus { return SFSpeechRecognizer.authorizationStatus; } ++ (void)requestAuthorization:(void (^)(NSInteger))completion { + [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) { + dispatch_async(dispatch_get_main_queue(), ^{ completion(status); }); + }]; +} +- (void)emit:(NSDictionary *)event { + if (!self.eventHandler) return; + NSData *json = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil]; + if (json) self.eventHandler([[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]); +} +- (NSString *)startWithEndpointing:(BOOL)endpointing { + return [self startWithEndpointing:endpointing phone:NO]; +} +- (NSString *)startPhoneWithEndpointing:(BOOL)endpointing { + return [self startWithEndpointing:endpointing phone:YES]; +} +- (NSString *)startWithEndpointing:(BOOL)endpointing phone:(BOOL)phone { + [self cancel]; + if (phone && [FaceclawSpeech microphoneAuthorizationStatus] != AVAuthorizationStatusAuthorized) + return @"Allow Microphone access for Faceclaw in iPhone Settings."; + if (SFSpeechRecognizer.authorizationStatus != SFSpeechRecognizerAuthorizationStatusAuthorized) + return @"Allow Speech Recognition for Faceclaw in iPhone Settings."; + NSString *language = NSLocale.preferredLanguages.firstObject ?: @"en-US"; + self.recognizer = [[SFSpeechRecognizer alloc] initWithLocale:[NSLocale localeWithLocaleIdentifier:language]]; + if (!self.recognizer.supportsOnDeviceRecognition) + return [NSString stringWithFormat:@"On-device speech is unavailable for %@. Enable Dictation for that language in iPhone Settings → General → Keyboard, then retry.", language]; + if (!self.recognizer.available) return @"Speech recognition is temporarily unavailable. Try again shortly."; + if (!phone) { + self.decoder = [FaceclawLc3Decoder new]; + if (!self.decoder) return @"Could not initialize the glasses audio decoder."; + } + self.recognizer.queue = NSOperationQueue.mainQueue; + self.request = [SFSpeechAudioBufferRecognitionRequest new]; + self.request.requiresOnDeviceRecognition = YES; + self.request.shouldReportPartialResults = YES; + self.request.taskHint = SFSpeechRecognitionTaskHintDictation; + if (@available(iOS 16.0, *)) self.request.addsPunctuation = YES; + self.bestText = @""; self.accepting = YES; + self.transcript = [FaceclawSpeechTranscript new]; + self.endpointDetector = endpointing ? [FaceclawKitVoiceEndpointDetector new] : nil; + NSUInteger generation = self.generation; + __weak FaceclawSpeech *weakSelf = self; + self.task = [self.recognizer recognitionTaskWithRequest:self.request resultHandler:^(SFSpeechRecognitionResult *result, NSError *error) { + dispatch_async(dispatch_get_main_queue(), ^{ + FaceclawSpeech *owner = weakSelf; + if (!owner || owner.generation != generation) return; + if (result) { + SFSpeechRecognitionMetadata *metadata = result.speechRecognitionMetadata; + SFTranscriptionSegment *first = result.bestTranscription.segments.firstObject; + SFTranscriptionSegment *last = result.bestTranscription.segments.lastObject; + NSTimeInterval start = metadata ? metadata.speechStartTimestamp : first.timestamp; + NSTimeInterval duration = metadata ? metadata.speechDuration : last.timestamp + last.duration - start; + owner.bestText = [owner.transcript updateText:result.bestTranscription.formattedString ?: @"" + start:start duration:duration settled:metadata != nil || result.final final:result.final]; + if (!result.final) [owner emit:@{@"kind":@"transcript", @"text":owner.bestText, @"final":@NO}]; + } + if (result.final || error) { + NSString *message = error ? [NSString stringWithFormat:@"Speech recognition: %@", error.localizedDescription] : @"Ready to send"; + [owner complete:message]; + } + }); + }]; + if (phone) { + NSString *error = [self startPhoneAudio]; + if (error.length) { [self cancel]; return error; } + } + [self emit:@{@"kind":@"status", @"message":[NSString stringWithFormat:@"Listening on %@ (%@, on-device)…", phone ? @"phone" : @"glasses", language]}]; + // Bound a dictation to the recognizer's supported short-session workflow. + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 55 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ + FaceclawSpeech *owner = weakSelf; + if (owner && owner.generation == generation && owner.accepting) [owner finish]; + }); + return @""; +} +- (NSString *)startPhoneAudio { + AVAudioSession *session = AVAudioSession.sharedInstance; + NSError *error = nil; + // Leave preferredInput unset so iOS selects its current default route, + // including a headset microphone when one is attached. + if (![session setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeMeasurement + options:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetoothHFP error:&error]) + return [NSString stringWithFormat:@"Could not configure phone microphone: %@", error.localizedDescription]; + if (![session setActive:YES error:&error]) + return [NSString stringWithFormat:@"Could not activate phone microphone: %@", error.localizedDescription]; + self.phoneSessionActive = YES; + self.phoneEngine = [AVAudioEngine new]; + AVAudioInputNode *input = self.phoneEngine.inputNode; + AVAudioFormat *format = [input outputFormatForBus:0]; + if (format.sampleRate <= 0 || !format.channelCount) return @"No phone microphone is available. Check your audio input and try again."; + NSUInteger generation = self.generation; + SFSpeechAudioBufferRecognitionRequest *request = self.request; + FaceclawKitVoiceEndpointDetector *detector = self.endpointDetector; + __weak FaceclawSpeech *weakSelf = self; + [input installTapOnBus:0 bufferSize:1024 format:format block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) { + FaceclawSpeech *owner = weakSelf; + if (!owner || owner.generation != generation || !owner.accepting || !buffer.frameLength) return; + // The tap owns buffer only for this callback. Copy before handing it + // to the serial recognition queue, which finish drains before endAudio. + AVAudioPCMBuffer *copy = [[AVAudioPCMBuffer alloc] initWithPCMFormat:buffer.format frameCapacity:buffer.frameLength]; + copy.frameLength = buffer.frameLength; + const AudioBufferList *source = buffer.audioBufferList; + AudioBufferList *destination = copy.mutableAudioBufferList; + for (UInt32 i = 0; i < source->mNumberBuffers; i++) + memcpy(destination->mBuffers[i].mData, source->mBuffers[i].mData, source->mBuffers[i].mDataByteSize); + dispatch_async(owner.audioQueue, ^{ @autoreleasepool { + if (owner.generation != generation) return; + [request appendAudioPCMBuffer:copy]; + double sum = 0; + if (copy.floatChannelData) { + const float *samples = copy.floatChannelData[0]; + NSUInteger stride = copy.format.interleaved ? copy.format.channelCount : 1; + for (NSUInteger i = 0; i < copy.frameLength; i++) sum += samples[i * stride] * samples[i * stride]; + } + double rms = sqrt(sum / copy.frameLength); + // The endpoint detector uses a 16 kHz sample clock; device input + // commonly arrives at 48 kHz. Recognition accepts its native rate. + int32_t samples = (int32_t)llround(copy.frameLength * 16000.0 / copy.format.sampleRate); + BOOL ended = [detector acceptLevelRms:rms * 32768.0 sampleCount:samples]; + if (ended) dispatch_async(dispatch_get_main_queue(), ^{ + if (owner.generation == generation && owner.accepting) [owner finish]; + }); + }}); + }]; + self.phoneTapInstalled = YES; + [self.phoneEngine prepare]; + if (![self.phoneEngine startAndReturnError:&error]) + return [NSString stringWithFormat:@"Could not start phone microphone: %@", error.localizedDescription]; + self.interruptionObserver = [NSNotificationCenter.defaultCenter addObserverForName:AVAudioSessionInterruptionNotification + object:session queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *notification) { + FaceclawSpeech *owner = weakSelf; + if (owner.generation == generation && [notification.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue] == AVAudioSessionInterruptionTypeBegan) + [owner complete:@"Phone microphone interrupted. Start voice input again to continue."]; + }]; + return @""; +} +- (void)stopPhoneAudio { + if (self.interruptionObserver) [NSNotificationCenter.defaultCenter removeObserver:self.interruptionObserver]; + self.interruptionObserver = nil; + [self.phoneEngine stop]; + if (self.phoneTapInstalled) [self.phoneEngine.inputNode removeTapOnBus:0]; + self.phoneTapInstalled = NO; + self.phoneEngine = nil; + if (self.phoneSessionActive) [AVAudioSession.sharedInstance setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; + self.phoneSessionActive = NO; +} +- (void)acceptPacket:(NSData *)packet { + if (!self.accepting) return; + if (self.pendingPackets >= 40) { [self complete:@"Audio processing fell behind. Please try again."]; return; } + self.pendingPackets++; + NSUInteger generation = self.generation; + FaceclawLc3Decoder *decoder = self.decoder; + SFSpeechAudioBufferRecognitionRequest *request = self.request; + FaceclawKitVoiceEndpointDetector *endpointDetector = self.endpointDetector; + NSData *copy = [packet copy]; + dispatch_async(self.audioQueue, ^{ @autoreleasepool { + if (self.generation != generation) return; + NSData *pcm = [decoder decodePacket:copy]; + double rms = 0; + BOOL speechEnded = NO; + if (pcm.length) { + AVAudioFormat *format = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 sampleRate:16000 channels:1 interleaved:NO]; + AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:format frameCapacity:(AVAudioFrameCount)(pcm.length / 2)]; + buffer.frameLength = buffer.frameCapacity; + const int16_t *samples = pcm.bytes; + for (NSUInteger i = 0; i < buffer.frameLength; i++) { + float value = samples[i] / 32768.0f; + buffer.floatChannelData[0][i] = value; rms += value * value; + } + rms = sqrt(rms / buffer.frameLength); + if (self.generation == generation) [request appendAudioPCMBuffer:buffer]; + speechEnded = [endpointDetector acceptLevelRms:rms * 32768.0 sampleCount:(int32_t)buffer.frameLength]; + } + NSUInteger packets = decoder.packets, errors = decoder.errors, missing = decoder.missing; + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.generation != generation) return; + self.pendingPackets--; + if (speechEnded && self.accepting) [self finish]; + if (pcm.length && packets % 20 == 0) [self emit:@{@"kind":@"audio", @"packets":@(packets), @"seconds":@(packets * 0.05), @"rms":@(rms), @"missing":@(missing), @"errors":@(errors)}]; + if (errors > 10 && !packets) [self complete:@"Could not decode glasses audio. Reconnect the glasses and retry."]; + }); + }}); +} +- (void)finish { + if (!self.accepting) return; + self.accepting = NO; + [self stopPhoneAudio]; + NSUInteger generation = self.generation; + SFSpeechAudioBufferRecognitionRequest *request = self.request; + [self emit:@{@"kind":@"finishing"}]; + // Drain already received audio before marking the utterance complete. + dispatch_async(self.audioQueue, ^{ if (self.generation == generation) [request endAudio]; }); + __weak FaceclawSpeech *weakSelf = self; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 8 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ + FaceclawSpeech *owner = weakSelf; + if (owner && owner.generation == generation) [owner complete:@"Ready to send"]; + }); +} +- (void)complete:(NSString *)status { + NSString *text = self.bestText ?: @""; + [self cancel]; + NSUInteger generation = self.generation; + if (text.length) [self emit:@{@"kind":@"transcript", @"text":text, @"final":@YES}]; + if (self.generation == generation) [self emit:@{@"kind":@"ended", @"message":status}]; +} +- (void)cancel { + self.generation++; + self.accepting = NO; self.pendingPackets = 0; + [self stopPhoneAudio]; + [self.task cancel]; self.task = nil; self.request = nil; self.decoder = nil; self.recognizer = nil; + self.bestText = @""; self.transcript = nil; self.endpointDetector = nil; +} +@end diff --git a/App_Resources/iOS/src/FaceclawSpeechTranscript.h b/App_Resources/iOS/src/FaceclawSpeechTranscript.h new file mode 100644 index 00000000..1b72a8dc --- /dev/null +++ b/App_Resources/iOS/src/FaceclawSpeechTranscript.h @@ -0,0 +1,6 @@ +#import +/** Accumulates on-device utterances while allowing revisions within each one. */ +@interface FaceclawSpeechTranscript : NSObject +@property(nonatomic, readonly) NSString *text; +- (NSString *)updateText:(NSString *)text start:(NSTimeInterval)start duration:(NSTimeInterval)duration settled:(BOOL)settled final:(BOOL)final; +@end diff --git a/App_Resources/iOS/src/FaceclawSpeechTranscript.m b/App_Resources/iOS/src/FaceclawSpeechTranscript.m new file mode 100644 index 00000000..934b5806 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawSpeechTranscript.m @@ -0,0 +1,60 @@ +#import "FaceclawSpeechTranscript.h" +#import +@interface FaceclawSpeechTranscript () +@property(nonatomic, strong) NSMutableArray *utterances; +@end +@implementation FaceclawSpeechTranscript +- (instancetype)init { + if ((self = [super init])) _utterances = [NSMutableArray new]; + return self; +} +// Only infer a sentence boundary between separate recognizer utterances. +// Keep the raw hypotheses unchanged so later revisions remain authoritative. +static NSString *sentenceJoin(NSString *previous, NSString *next) { + NSCharacterSet *openingQuotes = [NSCharacterSet characterSetWithCharactersInString:@"\"'“‘«"]; + NSUInteger first = 0; + while (first < next.length && [openingQuotes characterIsMember:[next characterAtIndex:first]]) first++; + if (first == next.length || ![NSCharacterSet.uppercaseLetterCharacterSet characterIsMember:[next characterAtIndex:first]]) return previous; + NSCharacterSet *closingQuotes = [NSCharacterSet characterSetWithCharactersInString:@"\"'”’»"]; + NSUInteger end = previous.length; + while (end && [closingQuotes characterIsMember:[previous characterAtIndex:end - 1]]) end--; + if (!end || [NSCharacterSet.punctuationCharacterSet characterIsMember:[previous characterAtIndex:end - 1]]) return previous; + return [NSString stringWithFormat:@"%@.%@", [previous substringToIndex:end], [previous substringFromIndex:end]]; +} +- (NSString *)rawText { + NSMutableArray *parts = [NSMutableArray new]; + for (NSDictionary *utterance in self.utterances) [parts addObject:utterance[@"text"]]; + return [parts componentsJoinedByString:@" "]; +} +- (NSString *)text { + NSMutableArray *parts = [NSMutableArray new]; + for (NSUInteger i = 0; i < self.utterances.count; i++) { + NSString *part = self.utterances[i][@"text"]; + if (i + 1 < self.utterances.count) part = sentenceJoin(part, self.utterances[i + 1][@"text"]); + [parts addObject:part]; + } + return [parts componentsJoinedByString:@" "]; +} +- (NSString *)updateText:(NSString *)text start:(NSTimeInterval)start duration:(NSTimeInterval)duration settled:(BOOL)settled final:(BOOL)final { + text = [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]; + // Apple's final callback can be empty after a pause. Keep the best result. + if (!text.length || (final && ([text isEqual:self.text] || [text isEqual:self.rawText]))) return self.text; + BOOL timed = isfinite(start) && start >= 0 && isfinite(duration) && duration > 0; + NSUInteger index = self.utterances.count; + if (timed) { + // Audio timestamps identify revisions, repeated callbacks and cumulative + // results. Replace the covered suffix, preserving earlier utterances. + for (NSUInteger i = 0; i < self.utterances.count; i++) { + NSDictionary *item = self.utterances[i]; + if (![item[@"settled"] boolValue] || start < [item[@"end"] doubleValue] - 0.01) { index = i; break; } + } + } else if (index && (final || ![self.utterances.lastObject[@"settled"] boolValue])) { + index--; // An ordinary partial corrects the active utterance. + } + // Untimed partials following a metadata-bearing result begin a new + // utterance even when the words are identical ("yes" ... "yes"). + if (index < self.utterances.count) [self.utterances removeObjectsInRange:NSMakeRange(index, self.utterances.count - index)]; + [self.utterances addObject:@{@"text":text, @"end":@(timed ? start + duration : 0), @"settled":@(settled)}]; + return self.text; +} +@end diff --git a/App_Resources/iOS/src/lc3/LICENSE b/App_Resources/iOS/src/lc3/LICENSE new file mode 100644 index 00000000..d6456956 --- /dev/null +++ b/App_Resources/iOS/src/lc3/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/App_Resources/iOS/src/lc3/README.faceclaw.md b/App_Resources/iOS/src/lc3/README.faceclaw.md new file mode 100644 index 00000000..a69c34f5 --- /dev/null +++ b/App_Resources/iOS/src/lc3/README.faceclaw.md @@ -0,0 +1,7 @@ +Google liblc3 v1.1.3 (same release as Android app.gradle). +Source: https://github.com/google/liblc3/releases/tag/v1.1.3 +Archive SHA-256: 276752ff54ce6a77d54ec133397b9d7e71f90caf3d9afa32d8b0e891b8ecb8af + +Unmodified include/*.h, src/*.h and the 12 translation units compiled by +Android. Licensed under Apache-2.0; see LICENSE. NativeScript compiles the +C files into the iOS app. No generated binaries are checked in. diff --git a/App_Resources/iOS/src/lc3/attdet.c b/App_Resources/iOS/src/lc3/attdet.c new file mode 100644 index 00000000..b498ef9b --- /dev/null +++ b/App_Resources/iOS/src/lc3/attdet.c @@ -0,0 +1,92 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "attdet.h" + + +/** + * Time domain attack detector + */ +bool lc3_attdet_run(enum lc3_dt dt, enum lc3_srate sr, + int nbytes, struct lc3_attdet_analysis *attdet, const int16_t *x) +{ + /* --- Check enabling --- */ + + const int nbytes_ranges[][LC3_NUM_SRATE - LC3_SRATE_32K][2] = { + [LC3_DT_7M5 - LC3_DT_7M5] = { { 61, 149 }, { 75, 149 } }, + [LC3_DT_10M - LC3_DT_7M5] = { { 81, INT_MAX }, { 100, INT_MAX } }, + }; + + if (dt < LC3_DT_7M5 || sr < LC3_SRATE_32K || lc3_hr(sr) || + nbytes < nbytes_ranges[dt - LC3_DT_7M5][sr - LC3_SRATE_32K][0] || + nbytes > nbytes_ranges[dt - LC3_DT_7M5][sr - LC3_SRATE_32K][1] ) + return 0; + + /* --- Filtering & Energy calculation --- */ + + int nblk = 4 - (dt == LC3_DT_7M5); + int32_t e[4]; + + for (int i = 0; i < nblk; i++) { + e[i] = 0; + + if (sr == LC3_SRATE_32K) { + int16_t xn2 = (x[-4] + x[-3]) >> 1; + int16_t xn1 = (x[-2] + x[-1]) >> 1; + int16_t xn, xf; + + for (int j = 0; j < 40; j++, x += 2, xn2 = xn1, xn1 = xn) { + xn = (x[0] + x[1]) >> 1; + xf = (3 * xn - 4 * xn1 + 1 * xn2) >> 3; + e[i] += (xf * xf) >> 5; + } + } + + else { + int16_t xn2 = (x[-6] + x[-5] + x[-4]) >> 2; + int16_t xn1 = (x[-3] + x[-2] + x[-1]) >> 2; + int16_t xn, xf; + + for (int j = 0; j < 40; j++, x += 3, xn2 = xn1, xn1 = xn) { + xn = (x[0] + x[1] + x[2]) >> 2; + xf = (3 * xn - 4 * xn1 + 1 * xn2) >> 3; + e[i] += (xf * xf) >> 5; + } + } + } + + /* --- Attack detection --- + * The attack block `p_att` is defined as the normative value + 1, + * in such way, it will be initialized to 0 */ + + int p_att = 0; + int32_t a[4]; + + for (int i = 0; i < nblk; i++) { + a[i] = LC3_MAX(attdet->an1 >> 2, attdet->en1); + attdet->en1 = e[i], attdet->an1 = a[i]; + + if ((e[i] >> 3) > a[i] + (a[i] >> 4)) + p_att = i + 1; + } + + int att = attdet->p_att >= 1 + (nblk >> 1) || p_att > 0; + attdet->p_att = p_att; + + return att; +} diff --git a/App_Resources/iOS/src/lc3/attdet.h b/App_Resources/iOS/src/lc3/attdet.h new file mode 100644 index 00000000..4d4ee170 --- /dev/null +++ b/App_Resources/iOS/src/lc3/attdet.h @@ -0,0 +1,37 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_ATTDET_H +#define __LC3_ATTDET_H + +#include "common.h" + + +/** + * Time domain attack detector + * dt, sr Duration and samplerate of the frame + * nbytes Size in bytes of the frame + * attdet Context of the Attack Detector + * x [-6..-1] Previous, [0..ns-1] Current samples + * return 1: Attack detected 0: Otherwise + */ +bool lc3_attdet_run(enum lc3_dt dt, enum lc3_srate sr, + int nbytes, lc3_attdet_analysis_t *attdet, const int16_t *x); + + +#endif /* __LC3_ATTDET_H */ diff --git a/App_Resources/iOS/src/lc3/bits.c b/App_Resources/iOS/src/lc3/bits.c new file mode 100644 index 00000000..881258bf --- /dev/null +++ b/App_Resources/iOS/src/lc3/bits.c @@ -0,0 +1,375 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "bits.h" +#include "common.h" + + +/* ---------------------------------------------------------------------------- + * Common + * -------------------------------------------------------------------------- */ + +static inline int ac_get(struct lc3_bits_buffer *); +static inline void accu_load(struct lc3_bits_accu *, struct lc3_bits_buffer *); + +/** + * Arithmetic coder return range bits + * ac Arithmetic coder + * return 1 + log2(ac->range) + */ +static int ac_get_range_bits(const struct lc3_bits_ac *ac) +{ + int nbits = 0; + + for (unsigned r = ac->range; r; r >>= 1, nbits++); + + return nbits; +} + +/** + * Arithmetic coder return pending bits + * ac Arithmetic coder + * return Pending bits + */ +static int ac_get_pending_bits(const struct lc3_bits_ac *ac) +{ + return 26 - ac_get_range_bits(ac) + + ((ac->cache >= 0) + ac->carry_count) * 8; +} + +/** + * Return number of bits left in the bitstream + * bits Bitstream context + * return >= 0: Number of bits left < 0: Overflow + */ +static int get_bits_left(const struct lc3_bits *bits) +{ + const struct lc3_bits_buffer *buffer = &bits->buffer; + const struct lc3_bits_accu *accu = &bits->accu; + const struct lc3_bits_ac *ac = &bits->ac; + + uintptr_t end = (uintptr_t)buffer->p_bw + + (bits->mode == LC3_BITS_MODE_READ ? LC3_ACCU_BITS/8 : 0); + + uintptr_t start = (uintptr_t)buffer->p_fw - + (bits->mode == LC3_BITS_MODE_READ ? LC3_AC_BITS/8 : 0); + + int n = end > start ? (int)(end - start) : -(int)(start - end); + + return 8 * n - (accu->n + accu->nover + ac_get_pending_bits(ac)); +} + +/** + * Setup bitstream writing + */ +void lc3_setup_bits(struct lc3_bits *bits, + enum lc3_bits_mode mode, void *buffer, int len) +{ + *bits = (struct lc3_bits){ + .mode = mode, + .accu = { + .n = mode == LC3_BITS_MODE_READ ? LC3_ACCU_BITS : 0, + }, + .ac = { + .range = 0xffffff, + .cache = -1 + }, + .buffer = { + .start = (uint8_t *)buffer, .end = (uint8_t *)buffer + len, + .p_fw = (uint8_t *)buffer, .p_bw = (uint8_t *)buffer + len, + } + }; + + if (mode == LC3_BITS_MODE_READ) { + struct lc3_bits_ac *ac = &bits->ac; + struct lc3_bits_accu *accu = &bits->accu; + struct lc3_bits_buffer *buffer = &bits->buffer; + + ac->low = ac_get(buffer) << 16; + ac->low |= ac_get(buffer) << 8; + ac->low |= ac_get(buffer); + + accu_load(accu, buffer); + } +} + +/** + * Return number of bits left in the bitstream + */ +int lc3_get_bits_left(const struct lc3_bits *bits) +{ + return LC3_MAX(get_bits_left(bits), 0); +} + +/** + * Return number of bits left in the bitstream + */ +int lc3_check_bits(const struct lc3_bits *bits) +{ + const struct lc3_bits_ac *ac = &bits->ac; + + return -(get_bits_left(bits) < 0 || ac->error); +} + + +/* ---------------------------------------------------------------------------- + * Writing + * -------------------------------------------------------------------------- */ + +/** + * Flush the bits accumulator + * accu Bitstream accumulator + * buffer Bitstream buffer + */ +static inline void accu_flush( + struct lc3_bits_accu *accu, struct lc3_bits_buffer *buffer) +{ + int nbytes = LC3_MIN(accu->n >> 3, + LC3_MAX(buffer->p_bw - buffer->p_fw, 0)); + + accu->n -= 8 * nbytes; + + for ( ; nbytes; accu->v >>= 8, nbytes--) + *(--buffer->p_bw) = accu->v & 0xff; + + if (accu->n >= 8) + accu->n = 0; +} + +/** + * Arithmetic coder put byte + * buffer Bitstream buffer + * byte Byte to output + */ +static inline void ac_put(struct lc3_bits_buffer *buffer, int byte) +{ + if (buffer->p_fw < buffer->end) + *(buffer->p_fw++) = byte; +} + +/** + * Arithmetic coder range shift + * ac Arithmetic coder + * buffer Bitstream buffer + */ +LC3_HOT static inline void ac_shift( + struct lc3_bits_ac *ac, struct lc3_bits_buffer *buffer) +{ + if (ac->low < 0xff0000 || ac->carry) + { + if (ac->cache >= 0) + ac_put(buffer, ac->cache + ac->carry); + + for ( ; ac->carry_count > 0; ac->carry_count--) + ac_put(buffer, ac->carry ? 0x00 : 0xff); + + ac->cache = ac->low >> 16; + ac->carry = 0; + } + else + ac->carry_count++; + + ac->low = (ac->low << 8) & 0xffffff; +} + +/** + * Arithmetic coder termination + * ac Arithmetic coder + * buffer Bitstream buffer + * end_val/nbits End value and count of bits to terminate (1 to 8) + */ +static void ac_terminate(struct lc3_bits_ac *ac, + struct lc3_bits_buffer *buffer) +{ + int nbits = 25 - ac_get_range_bits(ac); + unsigned mask = 0xffffff >> nbits; + unsigned val = ac->low + mask; + unsigned high = ac->low + ac->range; + + bool over_val = val >> 24; + bool over_high = high >> 24; + + val = (val & 0xffffff) & ~mask; + high = (high & 0xffffff); + + if (over_val == over_high) { + + if (val + mask >= high) { + nbits++; + mask >>= 1; + val = ((ac->low + mask) & 0xffffff) & ~mask; + } + + ac->carry |= val < ac->low; + } + + ac->low = val; + + for (; nbits > 8; nbits -= 8) + ac_shift(ac, buffer); + ac_shift(ac, buffer); + + int end_val = ac->cache >> (8 - nbits); + + if (ac->carry_count) { + ac_put(buffer, ac->cache); + for ( ; ac->carry_count > 1; ac->carry_count--) + ac_put(buffer, 0xff); + + end_val = nbits < 8 ? 0 : 0xff; + } + + if (buffer->p_fw < buffer->end) { + *buffer->p_fw &= 0xff >> nbits; + *buffer->p_fw |= end_val << (8 - nbits); + } +} + +/** + * Flush and terminate bitstream + */ +void lc3_flush_bits(struct lc3_bits *bits) +{ + struct lc3_bits_ac *ac = &bits->ac; + struct lc3_bits_accu *accu = &bits->accu; + struct lc3_bits_buffer *buffer = &bits->buffer; + + int nleft = buffer->p_bw - buffer->p_fw; + for (int n = 8 * nleft - accu->n; n > 0; n -= 32) + lc3_put_bits(bits, 0, LC3_MIN(n, 32)); + + accu_flush(accu, buffer); + + ac_terminate(ac, buffer); +} + +/** + * Write from 1 to 32 bits, + * exceeding the capacity of the accumulator + */ +LC3_HOT void lc3_put_bits_generic(struct lc3_bits *bits, unsigned v, int n) +{ + struct lc3_bits_accu *accu = &bits->accu; + + /* --- Fulfill accumulator and flush -- */ + + int n1 = LC3_MIN(LC3_ACCU_BITS - accu->n, n); + if (n1) { + accu->v |= v << accu->n; + accu->n = LC3_ACCU_BITS; + } + + accu_flush(accu, &bits->buffer); + + /* --- Accumulate remaining bits -- */ + + accu->v = v >> n1; + accu->n = n - n1; +} + +/** + * Arithmetic coder renormalization + */ +LC3_HOT void lc3_ac_write_renorm(struct lc3_bits *bits) +{ + struct lc3_bits_ac *ac = &bits->ac; + + for ( ; ac->range < 0x10000; ac->range <<= 8) + ac_shift(ac, &bits->buffer); +} + + +/* ---------------------------------------------------------------------------- + * Reading + * -------------------------------------------------------------------------- */ + +/** + * Arithmetic coder get byte + * buffer Bitstream buffer + * return Byte read, 0 on overflow + */ +static inline int ac_get(struct lc3_bits_buffer *buffer) +{ + return buffer->p_fw < buffer->end ? *(buffer->p_fw++) : 0; +} + +/** + * Load the accumulator + * accu Bitstream accumulator + * buffer Bitstream buffer + */ +static inline void accu_load(struct lc3_bits_accu *accu, + struct lc3_bits_buffer *buffer) +{ + int nbytes = LC3_MIN(accu->n >> 3, buffer->p_bw - buffer->start); + + accu->n -= 8 * nbytes; + + for ( ; nbytes; nbytes--) { + accu->v >>= 8; + accu->v |= (unsigned)*(--buffer->p_bw) << (LC3_ACCU_BITS - 8); + } + + if (accu->n >= 8) { + accu->nover = LC3_MIN(accu->nover + accu->n, LC3_ACCU_BITS); + accu->v >>= accu->n; + accu->n = 0; + } +} + +/** + * Read from 1 to 32 bits, + * exceeding the capacity of the accumulator + */ +LC3_HOT unsigned lc3_get_bits_generic(struct lc3_bits *bits, int n) +{ + struct lc3_bits_accu *accu = &bits->accu; + struct lc3_bits_buffer *buffer = &bits->buffer; + + /* --- Fulfill accumulator and read -- */ + + accu_load(accu, buffer); + + int n1 = LC3_MIN(LC3_ACCU_BITS - accu->n, n); + unsigned v = (accu->v >> accu->n) & ((1u << n1) - 1); + accu->n += n1; + + /* --- Second round --- */ + + int n2 = n - n1; + + if (n2) { + accu_load(accu, buffer); + + v |= ((accu->v >> accu->n) & ((1u << n2) - 1)) << n1; + accu->n += n2; + } + + return v; +} + +/** + * Arithmetic coder renormalization + */ +LC3_HOT void lc3_ac_read_renorm(struct lc3_bits *bits) +{ + struct lc3_bits_ac *ac = &bits->ac; + + for ( ; ac->range < 0x10000; ac->range <<= 8) + ac->low = ((ac->low << 8) | ac_get(&bits->buffer)) & 0xffffff; +} diff --git a/App_Resources/iOS/src/lc3/bits.h b/App_Resources/iOS/src/lc3/bits.h new file mode 100644 index 00000000..de65c931 --- /dev/null +++ b/App_Resources/iOS/src/lc3/bits.h @@ -0,0 +1,309 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/** + * The bitstream is written by the 2 ends of the buffer : + * + * - Arthmetic coder put bits while increasing memory addresses + * in the buffer (forward) + * + * - Plain bits are puts starting the end of the buffer, with memeory + * addresses decreasing (backward) + * + * .---------------------------------------------------. + * | > > > > > > > > > > : : < < < < < < < < < | + * '---------------------------------------------------' + * |---------------------> - - - - - - - - - - - - - ->| + * |< - - - <-------------------| + * Arithmetic coding Plain bits + * `lc3_put_symbol()` `lc3_put_bits()` + * + * - The forward writing is protected against buffer overflow, it cannot + * write after the buffer, but can overwrite plain bits previously + * written in the buffer. + * + * - The backward writing is protected against overwrite of the arithmetic + * coder bitstream. In such way, the backward bitstream is always limited + * by the aritmetic coder bitstream, and can be overwritten by him. + * + * .---------------------------------------------------. + * | > > > > > > > > > > : : < < < < < < < < < | + * '---------------------------------------------------' + * |---------------------> - - - - - - - - - - - - - ->| + * |< - - - - - - - - - - - - - - <-------------------| + * Arithmetic coding Plain bits + * `lc3_get_symbol()` `lc3_get_bits()` + * + * - Reading is limited to read of the complementary end of the buffer. + * + * - The procedure `lc3_check_bits()` returns indication that read has been + * made crossing the other bit plane. + * + */ + +#ifndef __LC3_BITS_H +#define __LC3_BITS_H + +#include "common.h" + + +/** + * Bitstream mode + */ + +enum lc3_bits_mode { + LC3_BITS_MODE_READ, + LC3_BITS_MODE_WRITE, +}; + +/** + * Arithmetic coder symbol interval + * The model split the interval in 17 symbols + */ + +struct lc3_ac_symbol { + uint16_t low : 16; + uint16_t range : 16; +}; + +struct lc3_ac_model { + struct lc3_ac_symbol s[17]; +}; + +/** + * Bitstream context + */ + +#define LC3_ACCU_BITS (int)(8 * sizeof(unsigned)) + +struct lc3_bits_accu { + unsigned v; + int n, nover; +}; + +#define LC3_AC_BITS (int)(24) + +struct lc3_bits_ac { + unsigned low, range; + int cache, carry, carry_count; + bool error; +}; + +struct lc3_bits_buffer { + const uint8_t *start, *end; + uint8_t *p_fw, *p_bw; +}; + +typedef struct lc3_bits { + enum lc3_bits_mode mode; + struct lc3_bits_ac ac; + struct lc3_bits_accu accu; + struct lc3_bits_buffer buffer; +} lc3_bits_t; + + +/** + * Setup bitstream reading/writing + * bits Bitstream context + * mode Either READ or WRITE mode + * buffer, len Output buffer and length (in bytes) + */ +void lc3_setup_bits(lc3_bits_t *bits, + enum lc3_bits_mode mode, void *buffer, int len); + +/** + * Return number of bits left in the bitstream + * bits Bitstream context + * return Number of bits left + */ +int lc3_get_bits_left(const lc3_bits_t *bits); + +/** + * Check if error occured on bitstream reading/writing + * bits Bitstream context + * return 0: Ok -1: Bitstream overflow or AC reading error + */ +int lc3_check_bits(const lc3_bits_t *bits); + +/** + * Put a bit + * bits Bitstream context + * v Bit value, 0 or 1 + */ +static inline void lc3_put_bit(lc3_bits_t *bits, int v); + +/** + * Put from 1 to 32 bits + * bits Bitstream context + * v, n Value, in range 0 to 2^n - 1, and bits count (1 to 32) + */ +static inline void lc3_put_bits(lc3_bits_t *bits, unsigned v, int n); + +/** + * Put arithmetic coder symbol + * bits Bitstream context + * model, s Model distribution and symbol value + */ +static inline void lc3_put_symbol(lc3_bits_t *bits, + const struct lc3_ac_model *model, unsigned s); + +/** + * Flush and terminate bitstream writing + * bits Bitstream context + */ +void lc3_flush_bits(lc3_bits_t *bits); + +/** + * Get a bit + * bits Bitstream context + */ +static inline int lc3_get_bit(lc3_bits_t *bits); + +/** + * Get from 1 to 32 bits + * bits Bitstream context + * n Number of bits to read (1 to 32) + * return The value read + */ +static inline unsigned lc3_get_bits(lc3_bits_t *bits, int n); + +/** + * Get arithmetic coder symbol + * bits Bitstream context + * model Model distribution + * return The value read + */ +static inline unsigned lc3_get_symbol(lc3_bits_t *bits, + const struct lc3_ac_model *model); + + + +/* ---------------------------------------------------------------------------- + * Inline implementations + * -------------------------------------------------------------------------- */ + +void lc3_put_bits_generic(lc3_bits_t *bits, unsigned v, int n); +unsigned lc3_get_bits_generic(struct lc3_bits *bits, int n); + +void lc3_ac_read_renorm(lc3_bits_t *bits); +void lc3_ac_write_renorm(lc3_bits_t *bits); + + +/** + * Put a bit + */ +LC3_HOT static inline void lc3_put_bit(lc3_bits_t *bits, int v) +{ + lc3_put_bits(bits, v, 1); +} + +/** + * Put from 1 to 32 bits + */ +LC3_HOT static inline void lc3_put_bits( + struct lc3_bits *bits, unsigned v, int n) +{ + struct lc3_bits_accu *accu = &bits->accu; + + if (accu->n + n <= LC3_ACCU_BITS) { + accu->v |= v << accu->n; + accu->n += n; + } else { + lc3_put_bits_generic(bits, v, n); + } +} + +/** + * Get a bit + */ +LC3_HOT static inline int lc3_get_bit(lc3_bits_t *bits) +{ + return lc3_get_bits(bits, 1); +} + +/** + * Get from 1 to 32 bits + */ +LC3_HOT static inline unsigned lc3_get_bits(struct lc3_bits *bits, int n) +{ + struct lc3_bits_accu *accu = &bits->accu; + + if (accu->n + n <= LC3_ACCU_BITS) { + int v = (accu->v >> accu->n) & ((1u << n) - 1); + return (accu->n += n), v; + } + else { + return lc3_get_bits_generic(bits, n); + } +} + +/** + * Put arithmetic coder symbol + */ +LC3_HOT static inline void lc3_put_symbol( + struct lc3_bits *bits, const struct lc3_ac_model *model, unsigned s) +{ + const struct lc3_ac_symbol *symbols = model->s; + struct lc3_bits_ac *ac = &bits->ac; + unsigned range = ac->range >> 10; + + ac->low += range * symbols[s].low; + ac->range = range * symbols[s].range; + + ac->carry |= ac->low >> 24; + ac->low &= 0xffffff; + + if (ac->range < 0x10000) + lc3_ac_write_renorm(bits); +} + +/** + * Get arithmetic coder symbol + */ +LC3_HOT static inline unsigned lc3_get_symbol( + lc3_bits_t *bits, const struct lc3_ac_model *model) +{ + const struct lc3_ac_symbol *symbols = model->s; + struct lc3_bits_ac *ac = &bits->ac; + + unsigned range = (ac->range >> 10) & 0xffff; + + ac->error |= (ac->low >= (range << 10)); + if (ac->error) + ac->low = 0; + + int s = 16; + + if (ac->low < range * symbols[s].low) { + s >>= 1; + s -= ac->low < range * symbols[s].low ? 4 : -4; + s -= ac->low < range * symbols[s].low ? 2 : -2; + s -= ac->low < range * symbols[s].low ? 1 : -1; + s -= ac->low < range * symbols[s].low; + } + + ac->low -= range * symbols[s].low; + ac->range = range * symbols[s].range; + + if (ac->range < 0x10000) + lc3_ac_read_renorm(bits); + + return s; +} + +#endif /* __LC3_BITS_H */ diff --git a/App_Resources/iOS/src/lc3/bwdet.c b/App_Resources/iOS/src/lc3/bwdet.c new file mode 100644 index 00000000..0c489397 --- /dev/null +++ b/App_Resources/iOS/src/lc3/bwdet.c @@ -0,0 +1,154 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "bwdet.h" + + +/** + * Bandwidth detector + */ +enum lc3_bandwidth lc3_bwdet_run( + enum lc3_dt dt, enum lc3_srate sr, const float *e) +{ + /* Bandwidth regions */ + + struct region { int is : 8; int ie : 8; }; + +#if LC3_PLUS + + static const struct region bws_table_2m5[][LC3_BANDWIDTH_SWB+1] = { + { { 24, 34+1 } }, + { { 24, 32+1 }, { 35, 39+1 } }, + { { 24, 31+1 }, { 33, 38+1 }, { 39, 42+1 } }, + { { 22, 29+1 }, { 31, 35+1 }, { 37, 40+1 }, { 41, 43+1 } }, + }; + + static const struct region bws_table_5m[][LC3_BANDWIDTH_SWB+1] = { + { { 39, 49+1 } }, + { { 35, 44+1 }, { 47, 51+1 } }, + { { 34, 42+1 }, { 44, 49+1 }, { 50, 53+1 } }, + { { 32, 40+1 }, { 42, 46+1 }, { 48, 51+1 }, { 52, 54+1 } }, + }; + +#endif /* LC3_PLUS */ + + static const struct region bws_table_7m5[][LC3_BANDWIDTH_SWB+1] = { + { { 51, 63+1 } }, + { { 45, 55+1 }, { 58, 63+1 } }, + { { 42, 51+1 }, { 53, 58+1 }, { 60, 63+1 } }, + { { 40, 48+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } }, + }; + + static const struct region bws_table_10m[][LC3_BANDWIDTH_SWB+1] = { + { { 53, 63+1 } }, + { { 47, 56+1 }, { 59, 63+1 } }, + { { 44, 52+1 }, { 54, 59+1 }, { 60, 63+1 } }, + { { 41, 49+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } }, + }; + + static const struct region (*bws_table[])[LC3_BANDWIDTH_SWB+1] = { + [LC3_DT_2M5] = LC3_IF_PLUS(bws_table_2m5, NULL), + [LC3_DT_5M ] = LC3_IF_PLUS(bws_table_5m , NULL), + [LC3_DT_7M5] = bws_table_7m5, + [LC3_DT_10M] = bws_table_10m, + }; + + static const int l_table[LC3_NUM_DT][LC3_BANDWIDTH_SWB+1] = { + [LC3_DT_2M5] = { 4, 4, 3, 1 }, + [LC3_DT_5M ] = { 4, 4, 3, 1 }, + [LC3_DT_7M5] = { 4, 4, 3, 2 }, + [LC3_DT_10M] = { 4, 4, 3, 1 }, + }; + + /* --- Stage 1 --- + * Determine bw0 candidate */ + + enum lc3_bandwidth bw0 = LC3_BANDWIDTH_NB; + enum lc3_bandwidth bwn = (enum lc3_bandwidth)sr; + + if (bwn <= bw0 || lc3_hr(sr)) + return bwn; + + const struct region *bwr = bws_table[dt][bwn-1]; + + for (enum lc3_bandwidth bw = bw0; bw < bwn; bw++) { + int i = bwr[bw].is, ie = bwr[bw].ie; + int n = ie - i; + + float se = e[i]; + for (i++; i < ie; i++) + se += e[i]; + + if (se >= (10 << (bw == LC3_BANDWIDTH_NB)) * n) + bw0 = bw + 1; + } + + /* --- Stage 2 --- + * Detect drop above cut-off frequency. + * The Tc condition (13) is precalculated, as + * Tc[] = 10 ^ (n / 10) , n = { 15, 23, 20, 20 } */ + + int hold = bw0 >= bwn; + + if (!hold) { + int i0 = bwr[bw0].is, l = l_table[dt][bw0]; + float tc = (const float []){ + 31.62277660, 199.52623150, 100, 100 }[bw0]; + + for (int i = i0 - l + 1; !hold && i <= i0 + 1; i++) { + hold = e[i-l] > tc * e[i]; + } + + } + + return hold ? bw0 : bwn; +} + +/** + * Return number of bits coding the bandwidth value + */ +int lc3_bwdet_get_nbits(enum lc3_srate sr) +{ + return lc3_hr(sr) ? 0 : (sr > 0) + (sr > 1) + (sr > 3); +} + +/** + * Put bandwidth indication + */ +void lc3_bwdet_put_bw(lc3_bits_t *bits, + enum lc3_srate sr, enum lc3_bandwidth bw) +{ + int nbits_bw = lc3_bwdet_get_nbits(sr); + if (nbits_bw > 0) + lc3_put_bits(bits, bw, nbits_bw); +} + +/** + * Get bandwidth indication + */ +int lc3_bwdet_get_bw(lc3_bits_t *bits, + enum lc3_srate sr, enum lc3_bandwidth *bw) +{ + enum lc3_bandwidth max_bw = (enum lc3_bandwidth)sr; + int nbits_bw = lc3_bwdet_get_nbits(sr); + + *bw = nbits_bw <= 0 ? max_bw : + (enum lc3_bandwidth)lc3_get_bits(bits, nbits_bw); + + return *bw > max_bw ? (*bw = max_bw), -1 : 0; +} diff --git a/App_Resources/iOS/src/lc3/bwdet.h b/App_Resources/iOS/src/lc3/bwdet.h new file mode 100644 index 00000000..773e6bb6 --- /dev/null +++ b/App_Resources/iOS/src/lc3/bwdet.h @@ -0,0 +1,62 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_BWDET_H +#define __LC3_BWDET_H + +#include "common.h" +#include "bits.h" + + +/** + * Bandwidth detector + * dt, sr Duration and samplerate of the frame + * e Energy estimation per bands + * return Return detected bandwitdth + */ +enum lc3_bandwidth lc3_bwdet_run( + enum lc3_dt dt, enum lc3_srate sr, const float *e); + +/** + * Return number of bits coding the bandwidth value + * sr Samplerate of the frame + * return Number of bits coding the bandwidth value + */ +int lc3_bwdet_get_nbits(enum lc3_srate sr); + +/** + * Put bandwidth indication + * bits Bitstream context + * sr Samplerate of the frame + * bw Bandwidth detected + */ +void lc3_bwdet_put_bw(lc3_bits_t *bits, + enum lc3_srate sr, enum lc3_bandwidth bw); + +/** + * Get bandwidth indication + * bits Bitstream context + * sr Samplerate of the frame + * bw Return bandwidth indication + * return 0: Ok -1: Invalid bandwidth indication + */ +int lc3_bwdet_get_bw(lc3_bits_t *bits, + enum lc3_srate sr, enum lc3_bandwidth *bw); + + +#endif /* __LC3_BWDET_H */ diff --git a/App_Resources/iOS/src/lc3/common.h b/App_Resources/iOS/src/lc3/common.h new file mode 100644 index 00000000..2b23a8d3 --- /dev/null +++ b/App_Resources/iOS/src/lc3/common.h @@ -0,0 +1,142 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_COMMON_H +#define __LC3_COMMON_H + +#include +#include "fastmath.h" + +#include +#include +#include + +#ifdef __ARM_ARCH +#include +#endif + + +/** + * Acivation flags for LC3-Plus and LC3-Plus HR features + */ + +#ifndef LC3_PLUS +#define LC3_PLUS 1 +#endif + +#ifndef LC3_PLUS_HR +#define LC3_PLUS_HR 1 +#endif + +#if LC3_PLUS +#define LC3_IF_PLUS(a, b) (a) +#else +#define LC3_IF_PLUS(a, b) (b) +#endif + +#if LC3_PLUS_HR +#define LC3_IF_PLUS_HR(a, b) (a) +#else +#define LC3_IF_PLUS_HR(a, b) (b) +#endif + + +/** + * Hot Function attribute + * Selectively disable sanitizer + */ + +#ifdef __clang__ + +#define LC3_HOT \ + __attribute__((no_sanitize("bounds"))) \ + __attribute__((no_sanitize("integer"))) + +#else /* __clang__ */ + +#define LC3_HOT + +#endif /* __clang__ */ + + +/** + * Macros + * MIN/MAX Minimum and maximum between 2 values + * CLIP Clip a value between low and high limits + * SATXX Signed saturation on 'xx' bits + * ABS Return absolute value + */ + +#define LC3_MIN(a, b) ( (a) < (b) ? (a) : (b) ) +#define LC3_MAX(a, b) ( (a) > (b) ? (a) : (b) ) + +#define LC3_CLIP(v, min, max) LC3_MIN(LC3_MAX(v, min), max) +#define LC3_SAT16(v) LC3_CLIP(v, -(1 << 15), (1 << 15) - 1) +#define LC3_SAT24(v) LC3_CLIP(v, -(1 << 23), (1 << 23) - 1) + +#define LC3_ABS(v) ( (v) < 0 ? -(v) : (v) ) + + +#if defined(__ARM_FEATURE_SAT) && !(__GNUC__ < 10) + +#undef LC3_SAT16 +#define LC3_SAT16(v) __ssat(v, 16) + +#undef LC3_SAT24 +#define LC3_SAT24(v) __ssat(v, 24) + +#endif /* __ARM_FEATURE_SAT */ + + +/** + * Return `true` when high-resolution mode + */ +static inline bool lc3_hr(enum lc3_srate sr) { + return LC3_PLUS_HR && (sr >= LC3_SRATE_48K_HR); +} + + +/** + * Bandwidth, mapped to Nyquist frequency of samplerates + */ + +enum lc3_bandwidth { + LC3_BANDWIDTH_NB = LC3_SRATE_8K, + LC3_BANDWIDTH_WB = LC3_SRATE_16K, + LC3_BANDWIDTH_SSWB = LC3_SRATE_24K, + LC3_BANDWIDTH_SWB = LC3_SRATE_32K, + LC3_BANDWIDTH_FB = LC3_SRATE_48K, + + LC3_BANDWIDTH_FB_HR = LC3_SRATE_48K_HR, + LC3_BANDWIDTH_UB_HR = LC3_SRATE_96K_HR, + + LC3_NUM_BANDWIDTH, +}; + + +/** + * Complex floating point number + */ + +struct lc3_complex +{ + float re, im; +}; + + +#endif /* __LC3_COMMON_H */ diff --git a/App_Resources/iOS/src/lc3/energy.c b/App_Resources/iOS/src/lc3/energy.c new file mode 100644 index 00000000..ffa3f786 --- /dev/null +++ b/App_Resources/iOS/src/lc3/energy.c @@ -0,0 +1,54 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "energy.h" +#include "tables.h" + + +/** + * Energy estimation per band + */ +bool lc3_energy_compute( + enum lc3_dt dt, enum lc3_srate sr, const float *x, float *e) +{ + int nb = lc3_num_bands[dt][sr]; + const int *lim = lc3_band_lim[dt][sr]; + + /* Mean the square of coefficients within each band */ + + float e_sum[2] = { 0, 0 }; + int iband_h = nb - (const int []){ + [LC3_DT_2M5] = 2, [LC3_DT_5M ] = 3, + [LC3_DT_7M5] = 4, [LC3_DT_10M] = 2 }[dt]; + + for (int iband = 0, i = lim[iband]; iband < nb; iband++) { + int ie = lim[iband+1]; + int n = ie - i; + + float sx2 = x[i] * x[i]; + for (i++; i < ie; i++) + sx2 += x[i] * x[i]; + + *e = sx2 / n; + e_sum[iband >= iband_h] += *(e++); + } + + /* Return the near nyquist flag */ + + return e_sum[1] > 30 * e_sum[0]; +} diff --git a/App_Resources/iOS/src/lc3/energy.h b/App_Resources/iOS/src/lc3/energy.h new file mode 100644 index 00000000..8ebd726b --- /dev/null +++ b/App_Resources/iOS/src/lc3/energy.h @@ -0,0 +1,36 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_ENERGY_H +#define __LC3_ENERGY_H + +#include "common.h" + + +/** + * Energy estimation per band + * dt, sr Duration and samplerate of the frame + * x Input MDCT coefficient + * e Energy estimation per bands + * return True when high energy detected near Nyquist frequency + */ +bool lc3_energy_compute( + enum lc3_dt dt, enum lc3_srate sr, const float *x, float *e); + + +#endif /* __LC3_ENERGY_H */ diff --git a/App_Resources/iOS/src/lc3/fastmath.h b/App_Resources/iOS/src/lc3/fastmath.h new file mode 100644 index 00000000..86cbdfa2 --- /dev/null +++ b/App_Resources/iOS/src/lc3/fastmath.h @@ -0,0 +1,219 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_FASTMATH_H +#define __LC3_FASTMATH_H + +#include +#include +#include + + +/** + * IEEE 754 Floating point representation + */ + +#define LC3_IEEE754_SIGN_SHL (31) +#define LC3_IEEE754_SIGN_MASK (1 << LC3_IEEE754_SIGN_SHL) + +#define LC3_IEEE754_EXP_SHL (23) +#define LC3_IEEE754_EXP_MASK (0xff << LC3_IEEE754_EXP_SHL) +#define LC3_IEEE754_EXP_BIAS (127) + + +/** + * Fast multiply floating-point number by integral power of 2 + * x Operand, finite number + * exp Exponent such that 2^x is finite + * return 2^exp + */ +static inline float lc3_ldexpf(float _x, int exp) { + union { float f; int32_t s; } x = { .f = _x }; + + if (x.s & LC3_IEEE754_EXP_MASK) + x.s += exp << LC3_IEEE754_EXP_SHL; + + return x.f; +} + +/** + * Fast convert floating-point number to fractional and integral components + * x Operand, finite number + * exp Return the exponent part + * return The normalized fraction in [0.5:1[ + */ +static inline float lc3_frexpf(float _x, int *exp) { + union { float f; uint32_t u; } x = { .f = _x }; + + int e = (x.u & LC3_IEEE754_EXP_MASK) >> LC3_IEEE754_EXP_SHL; + *exp = e - (LC3_IEEE754_EXP_BIAS - 1); + + x.u = (x.u & ~LC3_IEEE754_EXP_MASK) | + ((LC3_IEEE754_EXP_BIAS - 1) << LC3_IEEE754_EXP_SHL); + + return x.f; +} + +/** + * Fast 2^n approximation + * x Operand, range -100 to 100 + * return 2^x approximation (max relative error ~ 4e-7) + */ +static inline float lc3_exp2f(float x) +{ + /* --- 2^(i/8) for i from 0 to 7 --- */ + + static const float e[] = { + 1.00000000e+00, 1.09050773e+00, 1.18920712e+00, 1.29683955e+00, + 1.41421356e+00, 1.54221083e+00, 1.68179283e+00, 1.83400809e+00 }; + + /* --- Polynomial approx in range 0 to 1/8 --- */ + + static const float p[] = { + 1.00448128e-02, 5.54563260e-02, 2.40228756e-01, 6.93147140e-01 }; + + /* --- Split the operand --- + * + * Such as x = k/8 + y, with k an integer, and |y| < 0.5/8 + * + * Note that `fast-math` compiler option leads to rounding error, + * disable optimisation with `volatile`. */ + + volatile union { float f; int32_t s; } v; + + v.f = x + 0x1.8p20f; + int k = v.s; + x -= v.f - 0x1.8p20f; + + /* --- Compute 2^x, with |x| < 1 --- + * Perform polynomial approximation in range -0.5/8 to 0.5/8, + * and muplity by precomputed value of 2^(i/8), i in [0:7] */ + + union { float f; int32_t s; } y; + + y.f = ( p[0]) * x; + y.f = (y.f + p[1]) * x; + y.f = (y.f + p[2]) * x; + y.f = (y.f + p[3]) * x; + y.f = (y.f + 1.f) * e[k & 7]; + + /* --- Add the exponent --- */ + + y.s += (k >> 3) << LC3_IEEE754_EXP_SHL; + + return y.f; +} + +/** + * Fast log2(x) approximation + * x Operand, greater than 0 + * return log2(x) approximation (max absolute error ~ 1e-4) + */ +static inline float lc3_log2f(float x) +{ + float y; + int e; + + /* --- Polynomial approx in range 0.5 to 1 --- */ + + static const float c[] = { + -1.29479677, 5.11769018, -8.42295281, 8.10557963, -3.50567360 }; + + x = lc3_frexpf(x, &e); + + y = ( c[0]) * x; + y = (y + c[1]) * x; + y = (y + c[2]) * x; + y = (y + c[3]) * x; + y = (y + c[4]); + + /* --- Add log2f(2^e) and return --- */ + + return e + y; +} + +/** + * Fast log10(x) approximation + * x Operand, greater than 0 + * return log10(x) approximation (max absolute error ~ 1e-4) + */ +static inline float lc3_log10f(float x) +{ + return log10f(2) * lc3_log2f(x); +} + +/** + * Fast `10 * log10(x)` (or dB) approximation in fixed Q16 + * x Operand, in range 2^-63 to 2^63 (1e-19 to 1e19) + * return 10 * log10(x) in fixed Q16 (-190 to 192 dB) + * + * - The 0 value is accepted and return the minimum value ~ -191dB + * - This function assumed that float 32 bits is coded IEEE 754 + */ +static inline int32_t lc3_db_q16(float x) +{ + /* --- Table in Q15 --- */ + + static const uint16_t t[][2] = { + + /* [n][0] = 10 * log10(2) * log2(1 + n/32), with n = [0..15] */ + /* [n][1] = [n+1][0] - [n][0] (while defining [16][0]) */ + + { 0, 4379 }, { 4379, 4248 }, { 8627, 4125 }, { 12753, 4009 }, + { 16762, 3899 }, { 20661, 3795 }, { 24456, 3697 }, { 28153, 3603 }, + { 31755, 3514 }, { 35269, 3429 }, { 38699, 3349 }, { 42047, 3272 }, + { 45319, 3198 }, { 48517, 3128 }, { 51645, 3061 }, { 54705, 2996 }, + + /* [n][0] = 10 * log10(2) * log2(1 + n/32) - 10 * log10(2) / 2, */ + /* with n = [16..31] */ + /* [n][1] = [n+1][0] - [n][0] (while defining [32][0]) */ + + { 8381, 2934 }, { 11315, 2875 }, { 14190, 2818 }, { 17008, 2763 }, + { 19772, 2711 }, { 22482, 2660 }, { 25142, 2611 }, { 27754, 2564 }, + { 30318, 2519 }, { 32837, 2475 }, { 35312, 2433 }, { 37744, 2392 }, + { 40136, 2352 }, { 42489, 2314 }, { 44803, 2277 }, { 47080, 2241 }, + + }; + + /* --- Approximation --- + * + * 10 * log10(x^2) = 10 * log10(2) * log2(x^2) + * + * And log2(x^2) = 2 * log2( (1 + m) * 2^e ) + * = 2 * (e + log2(1 + m)) , with m in range [0..1] + * + * Split the float values in : + * e2 Double value of the exponent (2 * e + k) + * hi High 5 bits of mantissa, for precalculated result `t[hi][0]` + * lo Low 16 bits of mantissa, for linear interpolation `t[hi][1]` + * + * Two cases, from the range of the mantissa : + * 0 to 0.5 `k = 0`, use 1st part of the table + * 0.5 to 1 `k = 1`, use 2nd part of the table */ + + union { float f; uint32_t u; } x2 = { .f = x*x }; + + int e2 = (int)(x2.u >> 22) - 2*127; + int hi = (x2.u >> 18) & 0x1f; + int lo = (x2.u >> 2) & 0xffff; + + return e2 * 49321 + t[hi][0] + ((t[hi][1] * lo) >> 16); +} + + +#endif /* __LC3_FASTMATH_H */ diff --git a/App_Resources/iOS/src/lc3/lc3.c b/App_Resources/iOS/src/lc3/lc3.c new file mode 100644 index 00000000..ac4427a1 --- /dev/null +++ b/App_Resources/iOS/src/lc3/lc3.c @@ -0,0 +1,766 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include + +#include "common.h" +#include "bits.h" + +#include "attdet.h" +#include "bwdet.h" +#include "ltpf.h" +#include "mdct.h" +#include "energy.h" +#include "sns.h" +#include "tns.h" +#include "spec.h" +#include "plc.h" + + +/** + * Frame side data + */ + +struct side_data { + enum lc3_bandwidth bw; + bool pitch_present; + lc3_ltpf_data_t ltpf; + lc3_sns_data_t sns; + lc3_tns_data_t tns; + lc3_spec_side_t spec; +}; + + +/* ---------------------------------------------------------------------------- + * General + * -------------------------------------------------------------------------- */ + +/** + * Resolve frame duration in us + * us Frame duration in us + * hrmode High-resolution mode indication + * return Frame duration identifier, or LC3_NUM_DT + */ +static enum lc3_dt resolve_dt(int us, bool hrmode) +{ + return LC3_PLUS && us == 2500 ? LC3_DT_2M5 : + LC3_PLUS && us == 5000 ? LC3_DT_5M : + !hrmode && us == 7500 ? LC3_DT_7M5 : + us == 10000 ? LC3_DT_10M : LC3_NUM_DT; +} + +/** + * Resolve samplerate in Hz + * hz Samplerate in Hz + * hrmode High-resolution mode indication + * return Sample rate identifier, or LC3_NUM_SRATE + */ +static enum lc3_srate resolve_srate(int hz, bool hrmode) +{ + hrmode = LC3_PLUS_HR && hrmode; + + return !hrmode && hz == 8000 ? LC3_SRATE_8K : + !hrmode && hz == 16000 ? LC3_SRATE_16K : + !hrmode && hz == 24000 ? LC3_SRATE_24K : + !hrmode && hz == 32000 ? LC3_SRATE_32K : + !hrmode && hz == 48000 ? LC3_SRATE_48K : + hrmode && hz == 48000 ? LC3_SRATE_48K_HR : + hrmode && hz == 96000 ? LC3_SRATE_96K_HR : LC3_NUM_SRATE; +} + +/** + * Return the number of PCM samples in a frame + */ +LC3_EXPORT int lc3_hr_frame_samples(bool hrmode, int dt_us, int sr_hz) +{ + enum lc3_dt dt = resolve_dt(dt_us, hrmode); + enum lc3_srate sr = resolve_srate(sr_hz, hrmode); + + if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE) + return -1; + + return lc3_ns(dt, sr); +} + +LC3_EXPORT int lc3_frame_samples(int dt_us, int sr_hz) +{ + return lc3_hr_frame_samples(false, dt_us, sr_hz); +} + +/** + * Return the size of frames or frame blocks, from bitrate + */ +LC3_EXPORT int lc3_hr_frame_block_bytes( + bool hrmode, int dt_us, int sr_hz, int nchannels, int bitrate) +{ + enum lc3_dt dt = resolve_dt(dt_us, hrmode); + enum lc3_srate sr = resolve_srate(sr_hz, hrmode); + + if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE + || nchannels < 1 || nchannels > 8 || bitrate < 0) + return -1; + + bitrate = LC3_CLIP(bitrate, 0, 8*LC3_HR_MAX_BITRATE); + + return LC3_CLIP((bitrate * (int)(1 + dt)) / 3200, + nchannels * lc3_min_frame_bytes(dt, sr), + nchannels * lc3_max_frame_bytes(dt, sr) ); +} + +LC3_EXPORT int lc3_frame_bock_bytes(int dt_us, int nchannels, int bitrate) +{ + return lc3_hr_frame_block_bytes(false, dt_us, 8000, nchannels, bitrate); +} + +LC3_EXPORT int lc3_hr_frame_bytes( + bool hrmode, int dt_us, int sr_hz, int bitrate) +{ + return lc3_hr_frame_block_bytes(hrmode, dt_us, sr_hz, 1, bitrate); +} + +LC3_EXPORT int lc3_frame_bytes(int dt_us, int bitrate) +{ + return lc3_hr_frame_bytes(false, dt_us, 8000, bitrate); +} + +/** + * Resolve the bitrate, from the size of frames or frame blocks + */ +LC3_EXPORT int lc3_hr_resolve_bitrate( + bool hrmode, int dt_us, int sr_hz, int nbytes) +{ + enum lc3_dt dt = resolve_dt(dt_us, hrmode); + enum lc3_srate sr = resolve_srate(sr_hz, hrmode); + + if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE || nbytes < 0) + return -1; + + return LC3_MIN(((int64_t)nbytes * 3200 + dt) / (1 + dt), INT_MAX); +} + +LC3_EXPORT int lc3_resolve_bitrate(int dt_us, int nbytes) +{ + return lc3_hr_resolve_bitrate(false, dt_us, 8000, nbytes); +} + +/** + * Return algorithmic delay, as a number of samples + */ +LC3_EXPORT int lc3_hr_delay_samples(bool hrmode, int dt_us, int sr_hz) +{ + enum lc3_dt dt = resolve_dt(dt_us, hrmode); + enum lc3_srate sr = resolve_srate(sr_hz, hrmode); + + if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE) + return -1; + + return 2 * lc3_nd(dt, sr) - lc3_ns(dt, sr); +} + +LC3_EXPORT int lc3_delay_samples(int dt_us, int sr_hz) +{ + return lc3_hr_delay_samples(false, dt_us, sr_hz); +} + + +/* ---------------------------------------------------------------------------- + * Encoder + * -------------------------------------------------------------------------- */ + +/** + * Input PCM Samples from signed 16 bits + * encoder Encoder state + * pcm, stride Input PCM samples, and count between two consecutives + */ +static void load_s16( + struct lc3_encoder *encoder, const void *_pcm, int stride) +{ + const int16_t *pcm = _pcm; + + enum lc3_dt dt = encoder->dt; + enum lc3_srate sr = encoder->sr_pcm; + + int16_t *xt = (int16_t *)encoder->x + encoder->xt_off; + float *xs = encoder->x + encoder->xs_off; + int ns = lc3_ns(dt, sr); + + for (int i = 0; i < ns; i++, pcm += stride) + xt[i] = *pcm, xs[i] = *pcm; +} + +/** + * Input PCM Samples from signed 24 bits + * encoder Encoder state + * pcm, stride Input PCM samples, and count between two consecutives + */ +static void load_s24( + struct lc3_encoder *encoder, const void *_pcm, int stride) +{ + const int32_t *pcm = _pcm; + + enum lc3_dt dt = encoder->dt; + enum lc3_srate sr = encoder->sr_pcm; + + int16_t *xt = (int16_t *)encoder->x + encoder->xt_off; + float *xs = encoder->x + encoder->xs_off; + int ns = lc3_ns(dt, sr); + + for (int i = 0; i < ns; i++, pcm += stride) { + xt[i] = *pcm >> 8; + xs[i] = lc3_ldexpf(*pcm, -8); + } +} + +/** + * Input PCM Samples from signed 24 bits packed + * encoder Encoder state + * pcm, stride Input PCM samples, and count between two consecutives + */ +static void load_s24_3le( + struct lc3_encoder *encoder, const void *_pcm, int stride) +{ + const uint8_t *pcm = _pcm; + + enum lc3_dt dt = encoder->dt; + enum lc3_srate sr = encoder->sr_pcm; + + int16_t *xt = (int16_t *)encoder->x + encoder->xt_off; + float *xs = encoder->x + encoder->xs_off; + int ns = lc3_ns(dt, sr); + + for (int i = 0; i < ns; i++, pcm += 3*stride) { + int32_t in = ((uint32_t)pcm[0] << 8) | + ((uint32_t)pcm[1] << 16) | + ((uint32_t)pcm[2] << 24) ; + + xt[i] = in >> 16; + xs[i] = lc3_ldexpf(in, -16); + } +} + +/** + * Input PCM Samples from float 32 bits + * encoder Encoder state + * pcm, stride Input PCM samples, and count between two consecutives + */ +static void load_float( + struct lc3_encoder *encoder, const void *_pcm, int stride) +{ + const float *pcm = _pcm; + + enum lc3_dt dt = encoder->dt; + enum lc3_srate sr = encoder->sr_pcm; + + int16_t *xt = (int16_t *)encoder->x + encoder->xt_off; + float *xs = encoder->x + encoder->xs_off; + int ns = lc3_ns(dt, sr); + + for (int i = 0; i < ns; i++, pcm += stride) { + xs[i] = lc3_ldexpf(*pcm, 15); + xt[i] = LC3_SAT16((int32_t)xs[i]); + } +} + +/** + * Frame Analysis + * encoder Encoder state + * nbytes Size in bytes of the frame + * side Return frame data + */ +static void analyze(struct lc3_encoder *encoder, + int nbytes, struct side_data *side) +{ + enum lc3_dt dt = encoder->dt; + enum lc3_srate sr = encoder->sr; + enum lc3_srate sr_pcm = encoder->sr_pcm; + + int16_t *xt = (int16_t *)encoder->x + encoder->xt_off; + float *xs = encoder->x + encoder->xs_off; + int ns = lc3_ns(dt, sr_pcm); + int nt = lc3_nt(sr_pcm); + + float *xd = encoder->x + encoder->xd_off; + float *xf = xs; + + /* --- Temporal --- */ + + bool att = lc3_attdet_run(dt, sr_pcm, nbytes, &encoder->attdet, xt); + + side->pitch_present = + lc3_ltpf_analyse(dt, sr_pcm, &encoder->ltpf, xt, &side->ltpf); + + memmove(xt - nt, xt + (ns-nt), nt * sizeof(*xt)); + + /* --- Spectral --- */ + + float e[LC3_MAX_BANDS]; + + lc3_mdct_forward(dt, sr_pcm, sr, xs, xd, xf); + + bool nn_flag = lc3_energy_compute(dt, sr, xf, e); + if (nn_flag) + lc3_ltpf_disable(&side->ltpf); + + side->bw = lc3_bwdet_run(dt, sr, e); + + lc3_sns_analyze(dt, sr, nbytes, e, att, &side->sns, xf, xf); + + lc3_tns_analyze(dt, side->bw, nn_flag, nbytes, &side->tns, xf); + + lc3_spec_analyze(dt, sr, + nbytes, side->pitch_present, &side->tns, + &encoder->spec, xf, &side->spec); +} + +/** + * Encode bitstream + * encoder Encoder state + * side The frame data + * nbytes Target size of the frame (20 to 400) + * buffer Output bitstream buffer of `nbytes` size + */ +static void encode(struct lc3_encoder *encoder, + const struct side_data *side, int nbytes, void *buffer) +{ + enum lc3_dt dt = encoder->dt; + enum lc3_srate sr = encoder->sr; + + float *xf = encoder->x + encoder->xs_off; + enum lc3_bandwidth bw = side->bw; + + lc3_bits_t bits; + + lc3_setup_bits(&bits, LC3_BITS_MODE_WRITE, buffer, nbytes); + + lc3_bwdet_put_bw(&bits, sr, bw); + + lc3_spec_put_side(&bits, dt, sr, &side->spec); + + lc3_tns_put_data(&bits, &side->tns); + + lc3_put_bit(&bits, side->pitch_present); + + lc3_sns_put_data(&bits, &side->sns); + + if (side->pitch_present) + lc3_ltpf_put_data(&bits, &side->ltpf); + + lc3_spec_encode(&bits, dt, sr, bw, nbytes, &side->spec, xf); + + lc3_flush_bits(&bits); +} + +/** + * Return size needed for an encoder + */ +LC3_EXPORT unsigned lc3_hr_encoder_size(bool hrmode, int dt_us, int sr_hz) +{ + if (resolve_dt(dt_us, hrmode) >= LC3_NUM_DT || + resolve_srate(sr_hz, hrmode) >= LC3_NUM_SRATE) + return 0; + + return sizeof(struct lc3_encoder) + + (LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1) * sizeof(float); +} + +LC3_EXPORT unsigned lc3_encoder_size(int dt_us, int sr_hz) +{ + return lc3_hr_encoder_size(false, dt_us, sr_hz); +} + +/** + * Setup encoder + */ +LC3_EXPORT struct lc3_encoder *lc3_hr_setup_encoder( + bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem) +{ + if (sr_pcm_hz <= 0) + sr_pcm_hz = sr_hz; + + enum lc3_dt dt = resolve_dt(dt_us, hrmode); + enum lc3_srate sr = resolve_srate(sr_hz, hrmode); + enum lc3_srate sr_pcm = resolve_srate(sr_pcm_hz, hrmode); + + if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem) + return NULL; + + struct lc3_encoder *encoder = mem; + int ns = lc3_ns(dt, sr_pcm); + int nt = lc3_nt(sr_pcm); + + *encoder = (struct lc3_encoder){ + .dt = dt, .sr = sr, + .sr_pcm = sr_pcm, + + .xt_off = nt, + .xs_off = (nt + ns) / 2, + .xd_off = (nt + ns) / 2 + ns, + }; + + memset(encoder->x, 0, + LC3_ENCODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float)); + + return encoder; +} + +LC3_EXPORT struct lc3_encoder *lc3_setup_encoder( + int dt_us, int sr_hz, int sr_pcm_hz, void *mem) +{ + return lc3_hr_setup_encoder(false, dt_us, sr_hz, sr_pcm_hz, mem); +} + +/** + * Encode a frame + */ +LC3_EXPORT int lc3_encode(struct lc3_encoder *encoder, + enum lc3_pcm_format fmt, const void *pcm, int stride, int nbytes, void *out) +{ + static void (* const load[])(struct lc3_encoder *, const void *, int) = { + [LC3_PCM_FORMAT_S16 ] = load_s16, + [LC3_PCM_FORMAT_S24 ] = load_s24, + [LC3_PCM_FORMAT_S24_3LE] = load_s24_3le, + [LC3_PCM_FORMAT_FLOAT ] = load_float, + }; + + /* --- Check parameters --- */ + + if (!encoder || nbytes < lc3_min_frame_bytes(encoder->dt, encoder->sr) + || nbytes > lc3_max_frame_bytes(encoder->dt, encoder->sr)) + return -1; + + /* --- Processing --- */ + + struct side_data side; + + load[fmt](encoder, pcm, stride); + + analyze(encoder, nbytes, &side); + + encode(encoder, &side, nbytes, out); + + return 0; +} + + +/* ---------------------------------------------------------------------------- + * Decoder + * -------------------------------------------------------------------------- */ + +/** + * Output PCM Samples to signed 16 bits + * decoder Decoder state + * pcm, stride Output PCM samples, and count between two consecutives + */ +static void store_s16( + struct lc3_decoder *decoder, void *_pcm, int stride) +{ + int16_t *pcm = _pcm; + + enum lc3_dt dt = decoder->dt; + enum lc3_srate sr = decoder->sr_pcm; + + float *xs = decoder->x + decoder->xs_off; + int ns = lc3_ns(dt, sr); + + for ( ; ns > 0; ns--, xs++, pcm += stride) { + int32_t s = *xs >= 0 ? (int)(*xs + 0.5f) : (int)(*xs - 0.5f); + *pcm = LC3_SAT16(s); + } +} + +/** + * Output PCM Samples to signed 24 bits + * decoder Decoder state + * pcm, stride Output PCM samples, and count between two consecutives + */ +static void store_s24( + struct lc3_decoder *decoder, void *_pcm, int stride) +{ + int32_t *pcm = _pcm; + + enum lc3_dt dt = decoder->dt; + enum lc3_srate sr = decoder->sr_pcm; + + float *xs = decoder->x + decoder->xs_off; + int ns = lc3_ns(dt, sr); + + for ( ; ns > 0; ns--, xs++, pcm += stride) { + int32_t s = *xs >= 0 ? (int32_t)(lc3_ldexpf(*xs, 8) + 0.5f) + : (int32_t)(lc3_ldexpf(*xs, 8) - 0.5f); + *pcm = LC3_SAT24(s); + } +} + +/** + * Output PCM Samples to signed 24 bits packed + * decoder Decoder state + * pcm, stride Output PCM samples, and count between two consecutives + */ +static void store_s24_3le( + struct lc3_decoder *decoder, void *_pcm, int stride) +{ + uint8_t *pcm = _pcm; + + enum lc3_dt dt = decoder->dt; + enum lc3_srate sr = decoder->sr_pcm; + + float *xs = decoder->x + decoder->xs_off; + int ns = lc3_ns(dt, sr); + + for ( ; ns > 0; ns--, xs++, pcm += 3*stride) { + int32_t s = *xs >= 0 ? (int32_t)(lc3_ldexpf(*xs, 8) + 0.5f) + : (int32_t)(lc3_ldexpf(*xs, 8) - 0.5f); + + s = LC3_SAT24(s); + pcm[0] = (s >> 0) & 0xff; + pcm[1] = (s >> 8) & 0xff; + pcm[2] = (s >> 16) & 0xff; + } +} + +/** + * Output PCM Samples to float 32 bits + * decoder Decoder state + * pcm, stride Output PCM samples, and count between two consecutives + */ +static void store_float( + struct lc3_decoder *decoder, void *_pcm, int stride) +{ + float *pcm = _pcm; + + enum lc3_dt dt = decoder->dt; + enum lc3_srate sr = decoder->sr_pcm; + + float *xs = decoder->x + decoder->xs_off; + int ns = lc3_ns(dt, sr); + + for ( ; ns > 0; ns--, xs++, pcm += stride) { + float s = lc3_ldexpf(*xs, -15); + *pcm = fminf(fmaxf(s, -1.f), 1.f); + } +} + +/** + * Decode bitstream + * decoder Decoder state + * data, nbytes Input bitstream buffer + * side Return the side data + * return 0: Ok < 0: Bitsream error detected + */ +static int decode(struct lc3_decoder *decoder, + const void *data, int nbytes, struct side_data *side) +{ + enum lc3_dt dt = decoder->dt; + enum lc3_srate sr = decoder->sr; + + float *xf = decoder->x + decoder->xs_off; + int ns = lc3_ns(dt, sr); + int ne = lc3_ne(dt, sr); + + lc3_bits_t bits; + int ret = 0; + + lc3_setup_bits(&bits, LC3_BITS_MODE_READ, (void *)data, nbytes); + + if ((ret = lc3_bwdet_get_bw(&bits, sr, &side->bw)) < 0) + return ret; + + if ((ret = lc3_spec_get_side(&bits, dt, sr, &side->spec)) < 0) + return ret; + + if ((ret = lc3_tns_get_data(&bits, dt, side->bw, nbytes, &side->tns)) < 0) + return ret; + + side->pitch_present = lc3_get_bit(&bits); + + if ((ret = lc3_sns_get_data(&bits, &side->sns)) < 0) + return ret; + + if (side->pitch_present) + lc3_ltpf_get_data(&bits, &side->ltpf); + + if ((ret = lc3_spec_decode(&bits, dt, sr, + side->bw, nbytes, &side->spec, xf)) < 0) + return ret; + + memset(xf + ne, 0, (ns - ne) * sizeof(float)); + + return lc3_check_bits(&bits); +} + +/** + * Frame synthesis + * decoder Decoder state + * side Frame data, NULL performs PLC + * nbytes Size in bytes of the frame + */ +static void synthesize(struct lc3_decoder *decoder, + const struct side_data *side, int nbytes) +{ + enum lc3_dt dt = decoder->dt; + enum lc3_srate sr = decoder->sr; + enum lc3_srate sr_pcm = decoder->sr_pcm; + + float *xf = decoder->x + decoder->xs_off; + int ns = lc3_ns(dt, sr_pcm); + int ne = lc3_ne(dt, sr); + + float *xg = decoder->x + decoder->xg_off; + float *xs = xf; + + float *xd = decoder->x + decoder->xd_off; + float *xh = decoder->x + decoder->xh_off; + + if (side) { + enum lc3_bandwidth bw = side->bw; + + lc3_plc_suspend(&decoder->plc); + + lc3_tns_synthesize(dt, bw, &side->tns, xf); + + lc3_sns_synthesize(dt, sr, &side->sns, xf, xg); + + lc3_mdct_inverse(dt, sr_pcm, sr, xg, xd, xs); + + } else { + lc3_plc_synthesize(dt, sr, &decoder->plc, xg, xf); + + memset(xf + ne, 0, (ns - ne) * sizeof(float)); + + lc3_mdct_inverse(dt, sr_pcm, sr, xf, xd, xs); + } + + if (!lc3_hr(sr)) + lc3_ltpf_synthesize(dt, sr_pcm, nbytes, &decoder->ltpf, + side && side->pitch_present ? &side->ltpf : NULL, xh, xs); +} + +/** + * Update decoder state on decoding completion + * decoder Decoder state + */ +static void complete(struct lc3_decoder *decoder) +{ + enum lc3_dt dt = decoder->dt; + enum lc3_srate sr_pcm = decoder->sr_pcm; + int nh = lc3_nh(dt, sr_pcm); + int ns = lc3_ns(dt, sr_pcm); + + decoder->xs_off = decoder->xs_off - decoder->xh_off < nh ? + decoder->xs_off + ns : decoder->xh_off; +} + +/** + * Return size needed for a decoder + */ +LC3_EXPORT unsigned lc3_hr_decoder_size(bool hrmode, int dt_us, int sr_hz) +{ + if (resolve_dt(dt_us, hrmode) >= LC3_NUM_DT || + resolve_srate(sr_hz, hrmode) >= LC3_NUM_SRATE) + return 0; + + return sizeof(struct lc3_decoder) + + (LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1) * sizeof(float); +} + +LC3_EXPORT unsigned lc3_decoder_size(int dt_us, int sr_hz) +{ + return lc3_hr_decoder_size(false, dt_us, sr_hz); +} + +/** + * Setup decoder + */ +LC3_EXPORT struct lc3_decoder *lc3_hr_setup_decoder( + bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem) +{ + if (sr_pcm_hz <= 0) + sr_pcm_hz = sr_hz; + + enum lc3_dt dt = resolve_dt(dt_us, hrmode); + enum lc3_srate sr = resolve_srate(sr_hz, hrmode); + enum lc3_srate sr_pcm = resolve_srate(sr_pcm_hz, hrmode); + + if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem) + return NULL; + + struct lc3_decoder *decoder = mem; + int nh = lc3_nh(dt, sr_pcm); + int ns = lc3_ns(dt, sr_pcm); + int nd = lc3_nd(dt, sr_pcm); + + *decoder = (struct lc3_decoder){ + .dt = dt, .sr = sr, + .sr_pcm = sr_pcm, + + .xh_off = 0, + .xs_off = nh, + .xd_off = nh + ns, + .xg_off = nh + ns + nd, + }; + + lc3_plc_reset(&decoder->plc); + + memset(decoder->x, 0, + LC3_DECODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float)); + + return decoder; +} + +LC3_EXPORT struct lc3_decoder *lc3_setup_decoder( + int dt_us, int sr_hz, int sr_pcm_hz, void *mem) +{ + return lc3_hr_setup_decoder(false, dt_us, sr_hz, sr_pcm_hz, mem); +} + +/** + * Decode a frame + */ +LC3_EXPORT int lc3_decode(struct lc3_decoder *decoder, + const void *in, int nbytes, enum lc3_pcm_format fmt, void *pcm, int stride) +{ + static void (* const store[])(struct lc3_decoder *, void *, int) = { + [LC3_PCM_FORMAT_S16 ] = store_s16, + [LC3_PCM_FORMAT_S24 ] = store_s24, + [LC3_PCM_FORMAT_S24_3LE] = store_s24_3le, + [LC3_PCM_FORMAT_FLOAT ] = store_float, + }; + + /* --- Check parameters --- */ + + if (!decoder) + return -1; + + if (in && (nbytes < LC3_MIN_FRAME_BYTES || + nbytes > lc3_max_frame_bytes(decoder->dt, decoder->sr) )) + return -1; + + /* --- Processing --- */ + + struct side_data side; + + int ret = !in || (decode(decoder, in, nbytes, &side) < 0); + + synthesize(decoder, ret ? NULL : &side, nbytes); + + store[fmt](decoder, pcm, stride); + + complete(decoder); + + return ret; +} diff --git a/App_Resources/iOS/src/lc3/lc3.h b/App_Resources/iOS/src/lc3/lc3.h new file mode 100644 index 00000000..98022f3e --- /dev/null +++ b/App_Resources/iOS/src/lc3/lc3.h @@ -0,0 +1,423 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/** + * Low Complexity Communication Codec (LC3) + * + * This implementation conforms to : + * + * - Low Complexity Communication Codec (LC3) + * Bluetooth Specification v1.0 + * + * - ETSI TS 103 634 v1.4.1 + * Digital Enhanced Cordless Telecommunications (DECT) + * Low Complexity Communication Codec plus (LC3plus) + * + * LC3 and LC3 Plus are audio codecs designed for low-latency audio transport. + * + * - Unlike most other codecs, the LC3 codec is focused on audio streaming + * in constrained (on packet sizes and interval) tranport layer. + * In this way, the LC3 does not handle : + * VBR (Variable Bitrate), based on input signal complexity + * ABR (Adaptative Bitrate). It does not rely on any bit reservoir, + * a frame will be strictly encoded in the bytes budget given by + * the user (or transport layer). + * + * However, the bitrate (bytes budget for encoding a frame) can be + * freely changed at any time. But will not rely on signal complexity, + * it can follow a temporary bandwidth increase or reduction. + * + * - Unlike classic codecs, the LC3 codecs does not run on fixed amount + * of samples as input. It operates only on fixed frame duration, for + * any supported sample rates (8 to 48 KHz). Two frames duration are + * available 7.5ms and 10ms. + * + * + * --- LC3 Plus features --- + * + * In addition to LC3, following features of LC3 Plus are proposed: + * - Frame duration of 2.5 and 5ms. + * - High-Resolution mode, 48 KHz, and 96 kHz sampling rates. + * + * The distinction between LC3 and LC3 plus is made according to : + * + * Frame Duration | 2.5ms | 5ms | 7.5ms | 10 ms | + * ---------------- | ----- | ----- | ----- | ----- | + * LC3 | | | X | X | + * LC3 Plus | X | X | | X | + * + * The 10 ms frame duration is available in LC3 and LC3 plus standard. + * In this mode, the produced bitstream can be referenced either + * as LC3 or LC3 plus. + * + * The LC3 Plus high-resolution mode should be preferred at high bitrates + * and larger audio bandwidth. In this mode, the audio bandwidth is always + * up to the Nyquist frequency, compared to LC3 at 48 KHz, which limits + * the bandwidth to 20 KHz. + * + * + * --- Bit rate --- + * + * The proposed implementation accepts any frame sizes between 20 and 400 Bytes + * for non-high-resolution mode. Mind that the LC3 Plus standard defines + * smaller sizes for frame durations shorter than 10 ms and/or sampling rates + * less than 48 kHz. + * + * In High-Resolution mode, the frame sizes (and bitrates) are restricted + * as follows: + * + * HR Configuration | Frame sizes | Bitrate (kbps) | + * ------------------ | ------------- | -------------- | + * 10 ms - 48 KHz | 156 to 625 | 124.8 - 500 | + * 10 ms - 96 KHz | 187 to 625 | 149.6 - 500 | + * ------------------ | ------------- | -------------- | + * 5 ms - 48 KHz | 93 to 375 | 148.8 - 600 | + * 5 ms - 96 KHz | 109 to 375 | 174.4 - 600 | + * ------------------ | ------------- | -------------- | + * 2.5 ms - 48 KHz | 54 to 210 | 172.8 - 672 | + * 2.5 ms - 96 KHz | 62 to 210 | 198.4 - 672 | + * + * + * --- About 44.1 KHz sample rate --- + * + * The Bluetooth specification and the ETSI TS 103 634 standard references + * the 44.1 KHz sample rate, although there is no support in the core algorithm + * of the codec. + * We can summarize the 44.1 KHz support by "You can put any sample rate around + * the defined base sample rates." Please mind the following items : + * + * 1. The frame size will not be 2.5ms, 5ms, 7.5 ms or 10 ms, but is scaled + * by 'supported sample rate' / 'input sample rate' + * + * 2. The bandwidth will be hard limited (to 20 KHz) if you select 48 KHz. + * The encoded bandwidth will also be affected by the above inverse + * factor of 20 KHz. + * + * Applied to 44.1 KHz, we get : + * + * 1. About 8.16 ms frame duration, instead of 7.5 ms + * About 10.88 ms frame duration, instead of 10 ms + * + * 2. The bandwidth becomes limited to 18.375 KHz + * + * + * --- How to encode / decode --- + * + * An encoder / decoder context needs to be setup. This context keeps states + * on the current stream to proceed, and samples that overlapped across + * frames. + * + * You have two ways to setup the encoder / decoder : + * + * - Using static memory allocation (this module does not rely on + * any dynamic memory allocation). The types `lc3_xxcoder_mem_16k_t`, + * and `lc3_xxcoder_mem_48k_t` have size of the memory needed for + * encoding up to 16 KHz or 48 KHz. + * + * - Using dynamic memory allocation. The `lc3_xxcoder_size()` procedure + * returns the needed memory size, for a given configuration. The memory + * space must be aligned to a pointer size. As an example, you can setup + * encoder like this : + * + * | enc = lc3_setup_encoder(frame_us, sample rate, + * | malloc(lc3_encoder_size(frame_us, sample rate))); + * | ... + * | free(enc); + * + * Note : + * - A NULL memory adress as input, will return a NULL encoder context. + * - The returned encoder handle is set at the address of the allocated + * memory space, you can directly free the handle. + * + * Next, call the `lc3_encode()` encoding procedure, for each frames. + * To handle multichannel streams (Stereo or more), you can proceed with + * interleaved channels PCM stream like this : + * + * | for(int ich = 0; ich < nch: ich++) + * | lc3_encode(encoder[ich], pcm + ich, nch, ...); + * + * with `nch` as the number of channels in the PCM stream + * + * --- + * + * Antoine SOULIER, Tempow / Google LLC + * + */ + +#ifndef __LC3_H +#define __LC3_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#include "lc3_private.h" + + +/** + * Limitations + * - On the bitrate, in bps + * - On the size of the frames in bytes + * - On the number of samples by frames + */ + +#define LC3_MIN_BITRATE 16000 +#define LC3_MAX_BITRATE 320000 +#define LC3_HR_MAX_BITRATE 672000 + +#define LC3_MIN_FRAME_BYTES 20 +#define LC3_MAX_FRAME_BYTES 400 +#define LC3_HR_MAX_FRAME_BYTES 625 + +#define LC3_MIN_FRAME_SAMPLES LC3_NS( 2500, 8000) +#define LC3_MAX_FRAME_SAMPLES LC3_NS(10000, 48000) +#define LC3_HR_MAX_FRAME_SAMPLES LC3_NS(10000, 96000) + + +/** + * Parameters check + * LC3_CHECK_DT_US(us) True when frame duration in us is suitable + * LC3_CHECK_SR_HZ(sr) True when sample rate in Hz is suitable + * + * LC3_HR_CHECK_SR_HZ(hrmode, sr) + * True when sample rate in Hz is suitable, according to the + * selection of the high-resolution mode `hrmode`. + */ + +#define LC3_CHECK_DT_US(us) \ + ( ((us) == 2500) || ((us) == 5000) || \ + ((us) == 7500) || ((us) == 10000) ) + +#define LC3_CHECK_SR_HZ(sr) \ + ( ((sr) == 8000) || ((sr) == 16000) || ((sr) == 24000) || \ + ((sr) == 32000) || ((sr) == 48000) ) + +#define LC3_HR_CHECK_SR_HZ(hrmode, sr) \ + ( (hrmode) ? ((sr) == 48000) || ((sr) == 96000) : LC3_CHECK_SR_HZ(sr) ) + + +/** + * PCM Sample Format + * S16 Signed 16 bits, in 16 bits words (int16_t) + * S24 Signed 24 bits, using low three bytes of 32 bits words (int32_t). + * The high byte sign extends (bits 31..24 set to b23). + * S24_3LE Signed 24 bits packed in 3 bytes little endian + * FLOAT Floating point 32 bits (float type), in range -1 to 1 + */ + +enum lc3_pcm_format { + LC3_PCM_FORMAT_S16, + LC3_PCM_FORMAT_S24, + LC3_PCM_FORMAT_S24_3LE, + LC3_PCM_FORMAT_FLOAT, +}; + + +/** + * Handle + */ + +typedef struct lc3_encoder *lc3_encoder_t; +typedef struct lc3_decoder *lc3_decoder_t; + + +/** + * Static memory of encoder/decoder contexts + * + * Propose types suitable for static memory allocation, supporting + * any frame duration, and maximum sample rates 16k and 48k respectively + * You can customize your type using the `LC3_ENCODER_MEM_T` or + * `LC3_DECODER_MEM_T` macro. + */ + +typedef LC3_ENCODER_MEM_T(10000, 16000) lc3_encoder_mem_16k_t; +typedef LC3_ENCODER_MEM_T(10000, 48000) lc3_encoder_mem_48k_t; + +typedef LC3_DECODER_MEM_T(10000, 16000) lc3_decoder_mem_16k_t; +typedef LC3_DECODER_MEM_T(10000, 48000) lc3_decoder_mem_48k_t; + + +/** + * Return the number of PCM samples in a frame + * hrmode Enable High-Resolution mode (48000 and 96000 sample rates) + * dt_us Frame duration in us, 2500, 5000, 7500 or 10000 + * sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000 + * return Number of PCM samples, -1 on bad parameters + */ +LC3_EXPORT int lc3_hr_frame_samples(bool hrmode, int dt_us, int sr_hz); + +LC3_EXPORT int lc3_frame_samples(int dt_us, int sr_hz); + +/** + * Return the size of frames, from bitrate + * hrmode Enable High-Resolution mode (48000 and 96000 sample rates) + * dt_us Frame duration in us, 2500, 5000, 7500 or 10000 + * sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000 + * bitrate Target bitrate in bit per second, 0 or `INT_MAX` returns + * respectively the minimum and maximum allowed size. + * return The floor size in bytes of the frames, -1 on bad parameters + */ +LC3_EXPORT int lc3_hr_frame_bytes( + bool hrmode, int dt_us, int sr_hz, int bitrate); + +LC3_EXPORT int lc3_frame_bytes(int dt_us, int bitrate); + +/** + * Return the size of frame blocks, from bitrate + * A frame block contains the frame data from all channels. + * hrmode Enable High-Resolution mode (48000 and 96000 sample rates) + * dt_us Frame duration in us, 2500, 5000, 7500 or 10000 + * sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000 + * nchannels The number of channels (or frames) in the block (<= 8) + * bitrate Target bitrate in bit per second, 0 or `INT_MAX` returns + * respectively the minimum and maximum allowed size. + * return The floor size in bytes of the frames, -1 on bad parameters + */ +LC3_EXPORT int lc3_hr_frame_block_bytes( + bool hrmode, int dt_us, int sr_hz, int nchannels, int bitrate); + +LC3_EXPORT int lc3_frame_block_bytes(int dt_us, int nframes, int bitrate); + +/** + * Resolve the bitrate, from the size of frames + * hrmode Enable High-Resolution mode (48000 and 96000 sample rates) + * dt_us Frame duration in us, 2500, 5000, 7500 or 10000 + * sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000 + * nbytes Size in bytes of the frames or frame blocks + * return The ceiled bitrate in bps, -1 on bad parameters + */ +LC3_EXPORT int lc3_hr_resolve_bitrate( + bool hrmode, int dt_us, int sr_hz, int nbytes); + +LC3_EXPORT int lc3_resolve_bitrate(int dt_us, int nbytes); + +/** + * Return algorithmic delay, as a number of samples + * hrmode Enable High-Resolution mode (48000 and 96000 sample rates) + * dt_us Frame duration in us, 2500, 5000, 7500 or 10000 + * sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000 + * return Number of algorithmic delay samples, -1 on bad parameters + */ +LC3_EXPORT int lc3_hr_delay_samples(bool hrmode, int dt_us, int sr_hz); + +LC3_EXPORT int lc3_delay_samples(int dt_us, int sr_hz); + +/** + * Return size needed for an encoder + * hrmode Enable High-Resolution mode (48000 and 96000 sample rates) + * dt_us Frame duration in us, 2500, 5000, 7500 or 10000 + * sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000 + * return Size of then encoder in bytes, 0 on bad parameters + * + * The `sr_hz` parameter is the sample rate of the PCM input stream, + * and will match `sr_pcm_hz` of `lc3_hr_setup_encoder()`. + */ +LC3_EXPORT unsigned lc3_hr_encoder_size(bool hrmode, int dt_us, int sr_hz); + +LC3_EXPORT unsigned lc3_encoder_size(int dt_us, int sr_hz); + +/** + * Setup encoder + * hrmode Enable High-Resolution mode (48000 and 96000 sample rates) + * dt_us Frame duration in us, 2500, 5000, 7500 or 10000 + * sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000 + * sr_pcm_hz Input sample rate, downsampling option of input, or 0 + * mem Encoder memory space, aligned to pointer type + * return Encoder as an handle, NULL on bad parameters + * + * The `sr_pcm_hz` parameter is a downsampling option of PCM input, + * the value `0` fallback to the sample rate of the encoded stream `sr_hz`. + * When used, `sr_pcm_hz` is intended to be higher or equal to the encoder + * sample rate `sr_hz`. The size of the context needed, given by + * `lc3_hr_encoder_size()` will be set accordingly to `sr_pcm_hz`. + */ +LC3_EXPORT lc3_encoder_t lc3_hr_setup_encoder( + bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem); + +LC3_EXPORT lc3_encoder_t lc3_setup_encoder( + int dt_us, int sr_hz, int sr_pcm_hz, void *mem); + +/** + * Encode a frame + * encoder Handle of the encoder + * fmt PCM input format + * pcm, stride Input PCM samples, and count between two consecutives + * nbytes Target size, in bytes, of the frame + * out Output buffer of `nbytes` size + * return 0: On success -1: Wrong parameters + */ +LC3_EXPORT int lc3_encode( + lc3_encoder_t encoder, enum lc3_pcm_format fmt, + const void *pcm, int stride, int nbytes, void *out); + +/** + * Return size needed for an decoder + * hrmode Enable High-Resolution mode (48000 and 96000 sample rates) + * dt_us Frame duration in us, 2500, 5000, 7500 or 10000 + * sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000 + * return Size of then decoder in bytes, 0 on bad parameters + * + * The `sr_hz` parameter is the sample rate of the PCM output stream, + * and will match `sr_pcm_hz` of `lc3_hr_setup_decoder()`. + */ +LC3_EXPORT unsigned lc3_hr_decoder_size(bool hrmode, int dt_us, int sr_hz); + +LC3_EXPORT unsigned lc3_decoder_size(int dt_us, int sr_hz); + +/** + * Setup decoder + * hrmode Enable High-Resolution mode (48000 and 96000 sample rates) + * dt_us Frame duration in us, 2500, 5000, 7500 or 10000 + * sr_hz Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000 + * sr_pcm_hz Output sample rate, upsampling option of output (or 0) + * mem Decoder memory space, aligned to pointer type + * return Decoder as an handle, NULL on bad parameters + * + * The `sr_pcm_hz` parameter is an upsampling option of PCM output, + * the value `0` fallback to the sample rate of the decoded stream `sr_hz`. + * When used, `sr_pcm_hz` is intended to be higher or equal to the decoder + * sample rate `sr_hz`. The size of the context needed, given by + * `lc3_hr_decoder_size()` will be set accordingly to `sr_pcm_hz`. + */ +LC3_EXPORT lc3_decoder_t lc3_hr_setup_decoder( + bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem); + +LC3_EXPORT lc3_decoder_t lc3_setup_decoder( + int dt_us, int sr_hz, int sr_pcm_hz, void *mem); + +/** + * Decode a frame + * decoder Handle of the decoder + * in, nbytes Input bitstream, and size in bytes, NULL performs PLC + * fmt PCM output format + * pcm, stride Output PCM samples, and count between two consecutives + * return 0: On success 1: PLC operated -1: Wrong parameters + */ +LC3_EXPORT int lc3_decode( + lc3_decoder_t decoder, const void *in, int nbytes, + enum lc3_pcm_format fmt, void *pcm, int stride); + + +#ifdef __cplusplus +} +#endif + +#endif /* __LC3_H */ diff --git a/App_Resources/iOS/src/lc3/lc3_cpp.h b/App_Resources/iOS/src/lc3/lc3_cpp.h new file mode 100644 index 00000000..1949eb90 --- /dev/null +++ b/App_Resources/iOS/src/lc3/lc3_cpp.h @@ -0,0 +1,310 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +/** + * Low Complexity Communication Codec (LC3) - C++ interface + */ + +#ifndef __LC3_CPP_H +#define __LC3_CPP_H + +#include +#include +#include +#include + +#include "lc3.h" + +namespace lc3 { + +// PCM Sample Format +// - Signed 16 bits, in 16 bits words (int16_t) +// - Signed 24 bits, using low three bytes of 32 bits words (int32_t) +// The high byte sign extends (bits 31..24 set to b23) +// - Signed 24 bits packed in 3 bytes little endian +// - Floating point 32 bits (float type), in range -1 to 1 + +enum class PcmFormat { + kS16 = LC3_PCM_FORMAT_S16, + kS24 = LC3_PCM_FORMAT_S24, + kS24In3Le = LC3_PCM_FORMAT_S24_3LE, + kF32 = LC3_PCM_FORMAT_FLOAT +}; + +// Base Encoder/Decoder Class +template +class Base { + protected: + Base(int dt_us, int sr_hz, int sr_pcm_hz, size_t nchannels, bool hrmode) + : dt_us_(dt_us), + sr_hz_(sr_hz), + sr_pcm_hz_(sr_pcm_hz == 0 ? sr_hz : sr_pcm_hz), + nchannels_(nchannels), + hrmode_(hrmode) { + states.reserve(nchannels_); + } + + virtual ~Base() = default; + + int dt_us_, sr_hz_; + int sr_pcm_hz_; + size_t nchannels_; + bool hrmode_; + + using state_ptr = std::unique_ptr; + std::vector states; + + public: + // Return the number of PCM samples in a frame + int GetFrameSamples() { + return lc3_hr_frame_samples(hrmode_, dt_us_, sr_pcm_hz_); } + + // Return the size of a frame block, from bitrate + int GetFrameBytes(int bitrate) { + return lc3_hr_frame_block_bytes( + hrmode_, dt_us_, sr_hz_, nchannels_, bitrate); } + + // Resolve the bitrate, from the size of frame blocks + int ResolveBitrate(int nbytes) { + return lc3_hr_resolve_bitrate(hrmode_, dt_us_, sr_hz_, nbytes); } + + // Return algorithmic delay, as a number of samples + int GetDelaySamples() { + return lc3_hr_delay_samples(hrmode_, dt_us_, sr_pcm_hz_); } + +}; // class Base + +// Encoder Class +class Encoder : public Base { + template + int EncodeImpl(PcmFormat fmt, const T *pcm, int block_size, uint8_t *out) { + if (states.size() != nchannels_) return -1; + + enum lc3_pcm_format cfmt = static_cast(fmt); + int ret = 0; + + uint8_t *out_ptr = out; + for (size_t ich = 0; ich < nchannels_; ich++) { + int frame_size = block_size / nchannels_ + + (ich < block_size % nchannels_); + + ret |= lc3_encode(states[ich].get(), cfmt, pcm + ich, nchannels_, + frame_size, out_ptr); + + out_ptr += frame_size; + } + + return ret; + } + + public: + // Encoder construction / destruction + // + // The frame duration `dt_us` is 2500, 5000, 7500 or 10000 us. + // The sample rate `sr_hz` is 8000, 16000, 24000, 32000 or 48000 Hz. + // The `hrmode` flag enables the high-resolution mode, in which case + // the sample rate is 48000 or 96000 Hz. + // + // The `sr_pcm_hz` parameter is a downsampling option of PCM input, + // the value 0 fallback to the sample rate of the encoded stream `sr_hz`. + // When used, `sr_pcm_hz` is intended to be higher or equal to the encoder + // sample rate `sr_hz`. + + Encoder(int dt_us, int sr_hz, int sr_pcm_hz = 0, + size_t nchannels = 1, bool hrmode = false) + : Base(dt_us, sr_hz, sr_pcm_hz, nchannels, hrmode) { + for (size_t ich = 0; ich < nchannels_; ich++) { + auto s = state_ptr((lc3_encoder_t) + malloc(lc3_hr_encoder_size(hrmode_, dt_us_, sr_pcm_hz_)), free); + + if (lc3_hr_setup_encoder(hrmode_, dt_us_, sr_hz_, sr_pcm_hz_, s.get())) + states.push_back(std::move(s)); + } + } + + ~Encoder() override = default; + + // Reset encoder state + + void Reset() { + for (auto &s : states) + lc3_hr_setup_encoder(hrmode_, dt_us_, sr_hz_, sr_pcm_hz_, s.get()); + } + + // Encode + // + // The input PCM samples are given in signed 16 bits, 24 bits, float, + // according the type of `pcm` input buffer, or by selecting a format. + // + // The PCM samples are read in interleaved way, and consecutive + // `nchannels` frames, are output in `out` buffer, of size `buffer_size`. + // + // The value returned is 0 on successs, -1 otherwise. + + int Encode(const int16_t *pcm, int block_size, uint8_t *out) { + return EncodeImpl(PcmFormat::kS16, pcm, block_size, out); + } + + int Encode(const int32_t *pcm, int block_size, uint8_t *out) { + return EncodeImpl(PcmFormat::kS24, pcm, block_size, out); + } + + int Encode(const float *pcm, int block_size, uint8_t *out) { + return EncodeImpl(PcmFormat::kF32, pcm, block_size, out); + } + + int Encode(PcmFormat fmt, const void *pcm, int block_size, uint8_t *out) { + uintptr_t pcm_ptr = reinterpret_cast(pcm); + + switch (fmt) { + case PcmFormat::kS16: + assert(pcm_ptr % alignof(int16_t) == 0); + return EncodeImpl(fmt, reinterpret_cast(pcm), + block_size, out); + + case PcmFormat::kS24: + assert(pcm_ptr % alignof(int32_t) == 0); + return EncodeImpl(fmt, reinterpret_cast(pcm), + block_size, out); + + case PcmFormat::kS24In3Le: + return EncodeImpl(fmt, reinterpret_cast(pcm), + block_size, out); + + case PcmFormat::kF32: + assert(pcm_ptr % alignof(float) == 0); + return EncodeImpl(fmt, reinterpret_cast(pcm), block_size, + out); + } + + return -1; + } + +}; // class Encoder + +// Decoder Class +class Decoder : public Base { + template + int DecodeImpl(const uint8_t *in, int block_size, PcmFormat fmt, T *pcm) { + if (states.size() != nchannels_) return -1; + + enum lc3_pcm_format cfmt = static_cast(fmt); + int ret = 0; + + const uint8_t *in_ptr = in; + for (size_t ich = 0; ich < nchannels_; ich++) { + int frame_size = block_size / nchannels_ + + (ich < block_size % nchannels_); + + ret |= lc3_decode(states[ich].get(), in_ptr, frame_size, + cfmt, pcm + ich, nchannels_); + + in_ptr += frame_size; + } + + return ret; + } + + public: + // Decoder construction / destruction + // + // The frame duration `dt_us` is 2500, 5000, 7500 or 10000 us. + // The sample rate `sr_hz` is 8000, 16000, 24000, 32000 or 48000 Hz. + // The `hrmode` flag enables the high-resolution mode, in which case + // the sample rate is 48000 or 96000 Hz. + // + // The `sr_pcm_hz` parameter is an downsampling option of PCM output, + // the value 0 fallback to the sample rate of the decoded stream `sr_hz`. + // When used, `sr_pcm_hz` is intended to be higher or equal to the decoder + // sample rate `sr_hz`. + + Decoder(int dt_us, int sr_hz, int sr_pcm_hz = 0, + size_t nchannels = 1, bool hrmode = false) + : Base(dt_us, sr_hz, sr_pcm_hz, nchannels, hrmode) { + for (size_t i = 0; i < nchannels_; i++) { + auto s = state_ptr((lc3_decoder_t) + malloc(lc3_hr_decoder_size(hrmode_, dt_us_, sr_pcm_hz_)), free); + + if (lc3_hr_setup_decoder(hrmode_, dt_us_, sr_hz_, sr_pcm_hz_, s.get())) + states.push_back(std::move(s)); + } + } + + ~Decoder() override = default; + + // Reset decoder state + + void Reset() { + for (auto &s : states) + lc3_hr_setup_decoder(hrmode_, dt_us_, sr_hz_, sr_pcm_hz_, s.get()); + } + + // Decode + // + // Decode a frame block of size `block_size`, + // in the `pcm` buffer in interleaved way. + // + // The PCM samples are output in signed 16 bits, 24 bits, float, + // according the type of `pcm` output buffer, or by selecting a format. + // + // The value returned is 0 on successs, 1 when PLC has been performed, + // and -1 otherwise. + + int Decode(const uint8_t *in, int block_size, int16_t *pcm) { + return DecodeImpl(in, block_size, PcmFormat::kS16, pcm); + } + + int Decode(const uint8_t *in, int block_size, int32_t *pcm) { + return DecodeImpl(in, block_size, PcmFormat::kS24In3Le, pcm); + } + + int Decode(const uint8_t *in, int block_size, float *pcm) { + return DecodeImpl(in, block_size, PcmFormat::kF32, pcm); + } + + int Decode(const uint8_t *in, int block_size, PcmFormat fmt, void *pcm) { + uintptr_t pcm_ptr = reinterpret_cast(pcm); + + switch (fmt) { + case PcmFormat::kS16: + assert(pcm_ptr % alignof(int16_t) == 0); + return DecodeImpl(in, block_size, fmt, + reinterpret_cast(pcm)); + + case PcmFormat::kS24: + assert(pcm_ptr % alignof(int32_t) == 0); + return DecodeImpl(in, block_size, fmt, + reinterpret_cast(pcm)); + + case PcmFormat::kS24In3Le: + return DecodeImpl(in, block_size, fmt, + reinterpret_cast(pcm)); + + case PcmFormat::kF32: + assert(pcm_ptr % alignof(float) == 0); + return DecodeImpl(in, block_size, fmt, reinterpret_cast(pcm)); + } + + return -1; + } + +}; // class Decoder + +} // namespace lc3 + +#endif /* __LC3_CPP_H */ diff --git a/App_Resources/iOS/src/lc3/lc3_private.h b/App_Resources/iOS/src/lc3/lc3_private.h new file mode 100644 index 00000000..4f5f657b --- /dev/null +++ b/App_Resources/iOS/src/lc3/lc3_private.h @@ -0,0 +1,187 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_PRIVATE_H +#define __LC3_PRIVATE_H + +#include +#include + + +/** + * Characteristics + * + * - The number of samples within a frame + * + * - The number of MDCT delayed samples, sum of half a frame and + * an ovelap of future by 1.25 ms (2.5ms, 5ms and 10ms frame durations) + * or 2 ms (7.5ms frame duration). + * + * - For decoding, keep 18 ms of history, aligned on a frame + * + * - For encoding, keep 1.25 ms of temporal previous samples + */ + +#define LC3_NS(dt_us, sr_hz) \ + ( (dt_us) * (sr_hz) / 1000 / 1000 ) + +#define LC3_ND(dt_us, sr_hz) \ + ( LC3_NS(dt_us, sr_hz) / 2 + \ + LC3_NS((dt_us) == 7500 ? 2000 : 1250, sr_hz) ) + +#define LC3_NH(dt_us, sr_hz) \ + ( (sr_hz) > 48000 ? 0 : ( LC3_NS(18000, sr_hz) + \ + LC3_NS(dt_us, sr_hz) - (LC3_NS(18000, sr_hz) % LC3_NS(dt_us, sr_hz)) ) ) + +#define LC3_NT(sr_hz) \ + ( LC3_NS(1250, sr_hz) ) + + +/** + * Frame duration + */ + +enum lc3_dt { + LC3_DT_2M5 = 0, + LC3_DT_5M = 1, + LC3_DT_7M5 = 2, + LC3_DT_10M = 3, + + LC3_NUM_DT +}; + + +/** + * Sampling frequency and high-resolution mode + */ + +enum lc3_srate { + LC3_SRATE_8K, + LC3_SRATE_16K, + LC3_SRATE_24K, + LC3_SRATE_32K, + LC3_SRATE_48K, + LC3_SRATE_48K_HR, + LC3_SRATE_96K_HR, + + LC3_NUM_SRATE +}; + + +/** + * Encoder state and memory + */ + +typedef struct lc3_attdet_analysis { + int32_t en1, an1; + int p_att; +} lc3_attdet_analysis_t; + +struct lc3_ltpf_hp50_state { + int64_t s1, s2; +}; + +typedef struct lc3_ltpf_analysis { + bool active; + int pitch; + float nc[2]; + + struct lc3_ltpf_hp50_state hp50; + int16_t x_12k8[384]; + int16_t x_6k4[178]; + int tc; +} lc3_ltpf_analysis_t; + +typedef struct lc3_spec_analysis { + float nbits_off; + int nbits_spare; +} lc3_spec_analysis_t; + +struct lc3_encoder { + enum lc3_dt dt; + enum lc3_srate sr, sr_pcm; + + lc3_attdet_analysis_t attdet; + lc3_ltpf_analysis_t ltpf; + lc3_spec_analysis_t spec; + + int xt_off, xs_off, xd_off; + float x[1]; +}; + +#define LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) \ + ( ( LC3_NS(dt_us, sr_hz) + LC3_NT(sr_hz) ) / 2 + \ + LC3_NS(dt_us, sr_hz) + LC3_ND(dt_us, sr_hz) ) + +#define LC3_ENCODER_MEM_T(dt_us, sr_hz) \ + struct { \ + struct lc3_encoder __e; \ + float __x[LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \ + } + + +/** + * Decoder state and memory + */ + +typedef struct lc3_ltpf_synthesis { + bool active; + int pitch; + float c[2*12], x[12]; +} lc3_ltpf_synthesis_t; + +typedef struct lc3_plc_state { + uint16_t seed; + int count; + float alpha; +} lc3_plc_state_t; + +struct lc3_decoder { + enum lc3_dt dt; + enum lc3_srate sr, sr_pcm; + + lc3_ltpf_synthesis_t ltpf; + lc3_plc_state_t plc; + + int xh_off, xs_off, xd_off, xg_off; + float x[1]; +}; + +#define LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz) \ + ( LC3_NH(dt_us, sr_hz) + LC3_NS(dt_us, sr_hz) + \ + LC3_ND(dt_us, sr_hz) + LC3_NS(dt_us, sr_hz) ) + +#define LC3_DECODER_MEM_T(dt_us, sr_hz) \ + struct { \ + struct lc3_decoder __d; \ + float __x[LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \ + } + + +/** + * Change the visibility of interface functions + */ + +#ifdef _WIN32 +#define LC3_EXPORT __declspec(dllexport) +#else +#define LC3_EXPORT __attribute__((visibility ("default"))) +#endif + + +#endif /* __LC3_PRIVATE_H */ diff --git a/App_Resources/iOS/src/lc3/ltpf.c b/App_Resources/iOS/src/lc3/ltpf.c new file mode 100644 index 00000000..08cbae6b --- /dev/null +++ b/App_Resources/iOS/src/lc3/ltpf.c @@ -0,0 +1,964 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "ltpf.h" +#include "tables.h" + +#include "ltpf_neon.h" +#include "ltpf_arm.h" + + +/* ---------------------------------------------------------------------------- + * Resampling + * -------------------------------------------------------------------------- */ + +/** + * Resampling coefficients + * The coefficients, in fixed Q15, are reordered by phase for each source + * samplerate (coefficient matrix transposed) + */ + +#ifndef resample_8k_12k8 +static const int16_t h_8k_12k8_q15[8*10] = { + 214, 417, -1052, -4529, 26233, -4529, -1052, 417, 214, 0, + 180, 0, -1522, -2427, 24506, -5289, 0, 763, 156, -28, + 92, -323, -1361, 0, 19741, -3885, 1317, 861, 0, -61, + 0, -457, -752, 1873, 13068, 0, 2389, 598, -213, -79, + -61, -398, 0, 2686, 5997, 5997, 2686, 0, -398, -61, + -79, -213, 598, 2389, 0, 13068, 1873, -752, -457, 0, + -61, 0, 861, 1317, -3885, 19741, 0, -1361, -323, 92, + -28, 156, 763, 0, -5289, 24506, -2427, -1522, 0, 180, +}; +#endif /* resample_8k_12k8 */ + +#ifndef resample_16k_12k8 +static const int16_t h_16k_12k8_q15[4*20] = { + -61, 214, -398, 417, 0, -1052, 2686, -4529, 5997, 26233, + 5997, -4529, 2686, -1052, 0, 417, -398, 214, -61, 0, + + -79, 180, -213, 0, 598, -1522, 2389, -2427, 0, 24506, + 13068, -5289, 1873, 0, -752, 763, -457, 156, 0, -28, + + -61, 92, 0, -323, 861, -1361, 1317, 0, -3885, 19741, + 19741, -3885, 0, 1317, -1361, 861, -323, 0, 92, -61, + + -28, 0, 156, -457, 763, -752, 0, 1873, -5289, 13068, + 24506, 0, -2427, 2389, -1522, 598, 0, -213, 180, -79, +}; +#endif /* resample_16k_12k8 */ + +#ifndef resample_32k_12k8 +static const int16_t h_32k_12k8_q15[2*40] = { + -30, -31, 46, 107, 0, -199, -162, 209, 430, 0, + -681, -526, 658, 1343, 0, -2264, -1943, 2999, 9871, 13116, + 9871, 2999, -1943, -2264, 0, 1343, 658, -526, -681, 0, + 430, 209, -162, -199, 0, 107, 46, -31, -30, 0, + + -14, -39, 0, 90, 78, -106, -229, 0, 382, 299, + -376, -761, 0, 1194, 937, -1214, -2644, 0, 6534, 12253, + 12253, 6534, 0, -2644, -1214, 937, 1194, 0, -761, -376, + 299, 382, 0, -229, -106, 78, 90, 0, -39, -14, +}; +#endif /* resample_32k_12k8 */ + +#ifndef resample_24k_12k8 +static const int16_t h_24k_12k8_q15[8*30] = { + -50, 19, 143, -93, -290, 278, 485, -658, -701, 1396, + 901, -3019, -1042, 10276, 17488, 10276, -1042, -3019, 901, 1396, + -701, -658, 485, 278, -290, -93, 143, 19, -50, 0, + + -46, 0, 141, -45, -305, 185, 543, -501, -854, 1153, + 1249, -2619, -1908, 8712, 17358, 11772, 0, -3319, 480, 1593, + -504, -796, 399, 367, -261, -142, 138, 40, -52, -5, + + -41, -17, 133, 0, -304, 91, 574, -334, -959, 878, + 1516, -2143, -2590, 7118, 16971, 13161, 1202, -3495, 0, 1731, + -267, -908, 287, 445, -215, -188, 125, 62, -52, -12, + + -34, -30, 120, 41, -291, 0, 577, -164, -1015, 585, + 1697, -1618, -3084, 5534, 16337, 14406, 2544, -3526, -523, 1800, + 0, -985, 152, 509, -156, -230, 104, 83, -48, -19, + + -26, -41, 103, 76, -265, -83, 554, 0, -1023, 288, + 1791, -1070, -3393, 3998, 15474, 15474, 3998, -3393, -1070, 1791, + 288, -1023, 0, 554, -83, -265, 76, 103, -41, -26, + + -19, -48, 83, 104, -230, -156, 509, 152, -985, 0, + 1800, -523, -3526, 2544, 14406, 16337, 5534, -3084, -1618, 1697, + 585, -1015, -164, 577, 0, -291, 41, 120, -30, -34, + + -12, -52, 62, 125, -188, -215, 445, 287, -908, -267, + 1731, 0, -3495, 1202, 13161, 16971, 7118, -2590, -2143, 1516, + 878, -959, -334, 574, 91, -304, 0, 133, -17, -41, + + -5, -52, 40, 138, -142, -261, 367, 399, -796, -504, + 1593, 480, -3319, 0, 11772, 17358, 8712, -1908, -2619, 1249, + 1153, -854, -501, 543, 185, -305, -45, 141, 0, -46, +}; +#endif /* resample_24k_12k8 */ + +#ifndef resample_48k_12k8 +static const int16_t h_48k_12k8_q15[4*60] = { + -13, -25, -20, 10, 51, 71, 38, -47, -133, -145, + -42, 139, 277, 242, 0, -329, -511, -351, 144, 698, + 895, 450, -535, -1510, -1697, -521, 1999, 5138, 7737, 8744, + 7737, 5138, 1999, -521, -1697, -1510, -535, 450, 895, 698, + 144, -351, -511, -329, 0, 242, 277, 139, -42, -145, + -133, -47, 38, 71, 51, 10, -20, -25, -13, 0, + + -9, -23, -24, 0, 41, 71, 52, -23, -115, -152, + -78, 92, 254, 272, 76, -251, -493, -427, 0, 576, + 900, 624, -262, -1309, -1763, -954, 1272, 4356, 7203, 8679, + 8169, 5886, 2767, 0, -1542, -1660, -809, 240, 848, 796, + 292, -252, -507, -398, -82, 199, 288, 183, 0, -130, + -145, -71, 20, 69, 60, 20, -15, -26, -17, -3, + + -6, -20, -26, -8, 31, 67, 62, 0, -94, -152, + -108, 45, 223, 287, 143, -167, -454, -480, -134, 439, + 866, 758, 0, -1071, -1748, -1295, 601, 3559, 6580, 8485, + 8485, 6580, 3559, 601, -1295, -1748, -1071, 0, 758, 866, + 439, -134, -480, -454, -167, 143, 287, 223, 45, -108, + -152, -94, 0, 62, 67, 31, -8, -26, -20, -6, + + -3, -17, -26, -15, 20, 60, 69, 20, -71, -145, + -130, 0, 183, 288, 199, -82, -398, -507, -252, 292, + 796, 848, 240, -809, -1660, -1542, 0, 2767, 5886, 8169, + 8679, 7203, 4356, 1272, -954, -1763, -1309, -262, 624, 900, + 576, 0, -427, -493, -251, 76, 272, 254, 92, -78, + -152, -115, -23, 52, 71, 41, 0, -24, -23, -9, +}; +#endif /* resample_48k_12k8 */ + +#ifndef resample_96k_12k8 +static const int16_t h_96k_12k8_q15[2*120] = { + -3, -7, -10, -13, -13, -10, -4, 5, 15, 26, + 33, 36, 31, 19, 0, -23, -47, -66, -76, -73, + -54, -21, 23, 70, 111, 139, 143, 121, 72, 0, + -84, -165, -227, -256, -240, -175, -67, 72, 219, 349, + 433, 448, 379, 225, 0, -268, -536, -755, -874, -848, + -648, -260, 301, 1000, 1780, 2569, 3290, 3869, 4243, 4372, + 4243, 3869, 3290, 2569, 1780, 1000, 301, -260, -648, -848, + -874, -755, -536, -268, 0, 225, 379, 448, 433, 349, + 219, 72, -67, -175, -240, -256, -227, -165, -84, 0, + 72, 121, 143, 139, 111, 70, 23, -21, -54, -73, + -76, -66, -47, -23, 0, 19, 31, 36, 33, 26, + 15, 5, -4, -10, -13, -13, -10, -7, -3, 0, + + -1, -5, -8, -12, -13, -12, -8, 0, 10, 21, + 30, 35, 34, 26, 10, -11, -35, -58, -73, -76, + -65, -39, 0, 46, 92, 127, 144, 136, 100, 38, + -41, -125, -199, -246, -254, -214, -126, 0, 146, 288, + 398, 450, 424, 312, 120, -131, -405, -655, -830, -881, + -771, -477, 0, 636, 1384, 2178, 2943, 3601, 4084, 4340, + 4340, 4084, 3601, 2943, 2178, 1384, 636, 0, -477, -771, + -881, -830, -655, -405, -131, 120, 312, 424, 450, 398, + 288, 146, 0, -126, -214, -254, -246, -199, -125, -41, + 38, 100, 136, 144, 127, 92, 46, 0, -39, -65, + -76, -73, -58, -35, -11, 10, 26, 34, 35, 30, + 21, 10, 0, -8, -12, -13, -12, -8, -5, -1, +}; +#endif /* resample_96k_12k8 */ + + +/** + * High-pass 50Hz filtering, at 12.8 KHz samplerate + * hp50 Biquad filter state + * xn Input sample, in fixed Q30 + * return Filtered sample, in fixed Q30 + */ +LC3_HOT static inline int32_t filter_hp50( + struct lc3_ltpf_hp50_state *hp50, int32_t xn) +{ + int32_t yn; + + const int32_t a1 = -2110217691, a2 = 1037111617; + const int32_t b1 = -2110535566, b2 = 1055267782; + + yn = (hp50->s1 + (int64_t)xn * b2) >> 30; + hp50->s1 = (hp50->s2 + (int64_t)xn * b1 - (int64_t)yn * a1); + hp50->s2 = ( (int64_t)xn * b2 - (int64_t)yn * a2); + + return yn; +} + +/** + * Resample from 8 / 16 / 32 KHz to 12.8 KHz Template + * p Resampling factor with compared to 192 KHz (8, 4 or 2) + * h Arrange by phase coefficients table + * hp50 High-Pass biquad filter state + * x [-d..-1] Previous, [0..ns-1] Current samples, Q15 + * y, n [0..n-1] Output `n` processed samples, Q14 + * + * The `x` vector is aligned on 32 bits + * The number of previous samples `d` accessed on `x` is : + * d: { 10, 20, 40 } - 1 for resampling factors 8, 4 and 2. + */ +#if !defined(resample_8k_12k8) || !defined(resample_16k_12k8) \ + || !defined(resample_32k_12k8) +LC3_HOT static inline void resample_x64k_12k8(const int p, const int16_t *h, + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + const int w = 2*(40 / p); + + x -= w - 1; + + for (int i = 0; i < 5*n; i += 5) { + const int16_t *hn = h + (i % p) * w; + const int16_t *xn = x + (i / p); + int32_t un = 0; + + for (int k = 0; k < w; k += 10) { + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + } + + int32_t yn = filter_hp50(hp50, un); + *(y++) = (yn + (1 << 15)) >> 16; + } +} +#endif + +/** + * Resample from 24 / 48 KHz to 12.8 KHz Template + * p Resampling factor with compared to 192 KHz (8 or 4) + * h Arrange by phase coefficients table + * hp50 High-Pass biquad filter state + * x [-d..-1] Previous, [0..ns-1] Current samples, Q15 + * y, n [0..n-1] Output `n` processed samples, Q14 + * + * The `x` vector is aligned on 32 bits + * The number of previous samples `d` accessed on `x` is : + * d: { 30, 60 } - 1 for resampling factors 8 and 4. + */ +#if !defined(resample_24k_12k8) || !defined(resample_48k_12k8) \ + || !defined(resample_96k_12k8) +LC3_HOT static inline void resample_x192k_12k8(const int p, const int16_t *h, + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + const int w = 2*(120 / p); + + x -= w - 1; + + for (int i = 0; i < 15*n; i += 15) { + const int16_t *hn = h + (i % p) * w; + const int16_t *xn = x + (i / p); + int32_t un = 0; + + for (int k = 0; k < w; k += 15) { + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + un += *(xn++) * *(hn++); + } + + int32_t yn = filter_hp50(hp50, un); + *(y++) = (yn + (1 << 15)) >> 16; + } +} +#endif + +/** + * Resample from 8 Khz to 12.8 KHz + * hp50 High-Pass biquad filter state + * x [-10..-1] Previous, [0..ns-1] Current samples, Q15 + * y, n [0..n-1] Output `n` processed samples, Q14 + * + * The `x` vector is aligned on 32 bits + */ +#ifndef resample_8k_12k8 +LC3_HOT static void resample_8k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + resample_x64k_12k8(8, h_8k_12k8_q15, hp50, x, y, n); +} +#endif /* resample_8k_12k8 */ + +/** + * Resample from 16 Khz to 12.8 KHz + * hp50 High-Pass biquad filter state + * x [-20..-1] Previous, [0..ns-1] Current samples, in fixed Q15 + * y, n [0..n-1] Output `n` processed samples, in fixed Q14 + * + * The `x` vector is aligned on 32 bits + */ +#ifndef resample_16k_12k8 +LC3_HOT static void resample_16k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + resample_x64k_12k8(4, h_16k_12k8_q15, hp50, x, y, n); +} +#endif /* resample_16k_12k8 */ + +/** + * Resample from 32 Khz to 12.8 KHz + * hp50 High-Pass biquad filter state + * x [-30..-1] Previous, [0..ns-1] Current samples, in fixed Q15 + * y, n [0..n-1] Output `n` processed samples, in fixed Q14 + * + * The `x` vector is aligned on 32 bits + */ +#ifndef resample_32k_12k8 +LC3_HOT static void resample_32k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + resample_x64k_12k8(2, h_32k_12k8_q15, hp50, x, y, n); +} +#endif /* resample_32k_12k8 */ + +/** + * Resample from 24 Khz to 12.8 KHz + * hp50 High-Pass biquad filter state + * x [-30..-1] Previous, [0..ns-1] Current samples, in fixed Q15 + * y, n [0..n-1] Output `n` processed samples, in fixed Q14 + * + * The `x` vector is aligned on 32 bits + */ +#ifndef resample_24k_12k8 +LC3_HOT static void resample_24k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + resample_x192k_12k8(8, h_24k_12k8_q15, hp50, x, y, n); +} +#endif /* resample_24k_12k8 */ + +/** + * Resample from 48 Khz to 12.8 KHz + * hp50 High-Pass biquad filter state + * x [-60..-1] Previous, [0..ns-1] Current samples, in fixed Q15 + * y, n [0..n-1] Output `n` processed samples, in fixed Q14 + * +* The `x` vector is aligned on 32 bits +*/ +#ifndef resample_48k_12k8 +LC3_HOT static void resample_48k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + resample_x192k_12k8(4, h_48k_12k8_q15, hp50, x, y, n); +} +#endif /* resample_48k_12k8 */ + +/** + * Resample from 96 Khz to 12.8 KHz + * hp50 High-Pass biquad filter state + * x [-120..-1] Previous, [0..ns-1] Current samples, in fixed Q15 + * y, n [0..n-1] Output `n` processed samples, in fixed Q14 + * +* The `x` vector is aligned on 32 bits +*/ +#ifndef resample_96k_12k8 +LC3_HOT static void resample_96k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + resample_x192k_12k8(2, h_96k_12k8_q15, hp50, x, y, n); +} +#endif /* resample_96k_12k8 */ + +/** +* Resample to 6.4 KHz +* x [-3..-1] Previous, [0..n-1] Current samples +* y, n [0..n-1] Output `n` processed samples +* +* The `x` vector is aligned on 32 bits + */ +#ifndef resample_6k4 +LC3_HOT static void resample_6k4(const int16_t *x, int16_t *y, int n) +{ + static const int16_t h[] = { 18477, 15424, 8105 }; + const int16_t *ye = y + n; + + for (x--; y < ye; x += 2) + *(y++) = (x[0] * h[0] + (x[-1] + x[1]) * h[1] + + (x[-2] + x[2]) * h[2]) >> 16; +} +#endif /* resample_6k4 */ + +/** + * LTPF Resample to 12.8 KHz implementations for each samplerates + */ + +static void (* const resample_12k8[]) + (struct lc3_ltpf_hp50_state *, const int16_t *, int16_t *, int ) = +{ + [LC3_SRATE_8K ] = resample_8k_12k8, + [LC3_SRATE_16K ] = resample_16k_12k8, + [LC3_SRATE_24K ] = resample_24k_12k8, + [LC3_SRATE_32K ] = resample_32k_12k8, + [LC3_SRATE_48K ] = resample_48k_12k8, + [LC3_SRATE_48K_HR] = resample_48k_12k8, + [LC3_SRATE_96K_HR] = resample_96k_12k8, +}; + + +/* ---------------------------------------------------------------------------- + * Analysis + * -------------------------------------------------------------------------- */ + +/** + * Return dot product of 2 vectors + * a, b, n The 2 vectors of size `n` (> 0 and <= 128) + * return sum( a[i] * b[i] ), i = [0..n-1] + * + * The size `n` of vectors must be multiple of 16, and less or equal to 128 +*/ +#ifndef dot +LC3_HOT static inline float dot(const int16_t *a, const int16_t *b, int n) +{ + int64_t v = 0; + + for (int i = 0; i < (n >> 4); i++) + for (int j = 0; j < 16; j++) + v += *(a++) * *(b++); + + int32_t v32 = (v + (1 << 5)) >> 6; + return (float)v32; +} +#endif /* dot */ + +/** + * Return vector of correlations + * a, b, n The 2 vector of size `n` (> 0 and <= 128) + * y, nc Output the correlation vector of size `nc` + * + * The first vector `a` is aligned of 32 bits + * The size `n` of vectors is multiple of 16, and less or equal to 128 + */ +#ifndef correlate +LC3_HOT static void correlate( + const int16_t *a, const int16_t *b, int n, float *y, int nc) +{ + for (const float *ye = y + nc; y < ye; ) + *(y++) = dot(a, b--, n); +} +#endif /* correlate */ + +/** + * Search the maximum value and returns its argument + * x, n The input vector of size `n` + * x_max Return the maximum value + * return Return the argument of the maximum + */ +LC3_HOT static int argmax(const float *x, int n, float *x_max) +{ + int arg = 0; + + *x_max = x[arg = 0]; + for (int i = 1; i < n; i++) + if (*x_max < x[i]) + *x_max = x[arg = i]; + + return arg; +} + +/** + * Search the maximum weithed value and returns its argument + * x, n The input vector of size `n` + * w_incr Increment of the weight + * x_max, xw_max Return the maximum not weighted value + * return Return the argument of the weigthed maximum + */ +LC3_HOT static int argmax_weighted( + const float *x, int n, float w_incr, float *x_max) +{ + int arg; + + float xw_max = (*x_max = x[arg = 0]); + float w = 1 + w_incr; + + for (int i = 1; i < n; i++, w += w_incr) + if (xw_max < x[i] * w) + xw_max = (*x_max = x[arg = i]) * w; + + return arg; +} + +/** + * Interpolate from pitch detected value + * x, n [-2..-1] Previous, [0..n] Current input + * d The phase of interpolation (0 to 3) + * return The interpolated vector + * + * The size `n` of vectors must be multiple of 4 + */ +LC3_HOT static void interpolate(const int16_t *x, int n, int d, int16_t *y) +{ + static const int16_t h4_q15[][4] = { + { 6877, 19121, 6877, 0 }, { 3506, 18025, 11000, 220 }, + { 1300, 15048, 15048, 1300 }, { 220, 11000, 18025, 3506 } }; + + const int16_t *h = h4_q15[d]; + int16_t x3 = x[-2], x2 = x[-1], x1, x0; + + x1 = (*x++); + for (const int16_t *ye = y + n; y < ye; ) { + int32_t yn; + + yn = (x0 = *(x++)) * h[0] + x1 * h[1] + x2 * h[2] + x3 * h[3]; + *(y++) = yn >> 15; + + yn = (x3 = *(x++)) * h[0] + x0 * h[1] + x1 * h[2] + x2 * h[3]; + *(y++) = yn >> 15; + + yn = (x2 = *(x++)) * h[0] + x3 * h[1] + x0 * h[2] + x1 * h[3]; + *(y++) = yn >> 15; + + yn = (x1 = *(x++)) * h[0] + x2 * h[1] + x3 * h[2] + x0 * h[3]; + *(y++) = yn >> 15; + } +} + +/** + * Interpolate autocorrelation + * x [-4..-1] Previous, [0..4] Current input + * d The phase of interpolation (-3 to 3) + * return The interpolated value + */ +LC3_HOT static float interpolate_corr(const float *x, int d) +{ + static const float h4[][8] = { + { 1.53572770e-02, -4.72963246e-02, 8.35788573e-02, 8.98638285e-01, + 8.35788573e-02, -4.72963246e-02, 1.53572770e-02, }, + { 2.74547165e-03, 4.59833449e-03, -7.54404636e-02, 8.17488686e-01, + 3.30182571e-01, -1.05835916e-01, 2.86823405e-02, -2.87456116e-03 }, + { -3.00125103e-03, 2.95038503e-02, -1.30305021e-01, 6.03297008e-01, + 6.03297008e-01, -1.30305021e-01, 2.95038503e-02, -3.00125103e-03 }, + { -2.87456116e-03, 2.86823405e-02, -1.05835916e-01, 3.30182571e-01, + 8.17488686e-01, -7.54404636e-02, 4.59833449e-03, 2.74547165e-03 }, + }; + + const float *h = h4[(4+d) % 4]; + + float y = d < 0 ? x[-4] * *(h++) : + d > 0 ? x[ 4] * *(h+7) : 0; + + y += x[-3] * h[0] + x[-2] * h[1] + x[-1] * h[2] + x[0] * h[3] + + x[ 1] * h[4] + x[ 2] * h[5] + x[ 3] * h[6]; + + return y; +} + +/** + * Pitch detection algorithm + * ltpf Context of analysis + * x, n [-114..-17] Previous, [0..n-1] Current 6.4KHz samples + * tc Return the pitch-lag estimation + * return True when pitch present + * + * The `x` vector is aligned on 32 bits + */ +static bool detect_pitch(struct lc3_ltpf_analysis *ltpf, + const int16_t *x, int n, int *tc) +{ + float rm1, rm2; + float r[98]; + + const int r0 = 17, nr = 98; + int k0 = LC3_MAX( 0, ltpf->tc-4); + int nk = LC3_MIN(nr-1, ltpf->tc+4) - k0 + 1; + + correlate(x, x - r0, n, r, nr); + + int t1 = argmax_weighted(r, nr, -.5f/(nr-1), &rm1); + int t2 = k0 + argmax(r + k0, nk, &rm2); + + const int16_t *x1 = x - (r0 + t1); + const int16_t *x2 = x - (r0 + t2); + + float nc1 = rm1 <= 0 ? 0 : + rm1 / sqrtf(dot(x, x, n) * dot(x1, x1, n)); + + float nc2 = rm2 <= 0 ? 0 : + rm2 / sqrtf(dot(x, x, n) * dot(x2, x2, n)); + + int t1sel = nc2 <= 0.85f * nc1; + ltpf->tc = (t1sel ? t1 : t2); + + *tc = r0 + ltpf->tc; + return (t1sel ? nc1 : nc2) > 0.6f; +} + +/** + * Pitch-lag parameter + * x, n [-232..-28] Previous, [0..n-1] Current 12.8KHz samples, Q14 + * tc Pitch-lag estimation + * pitch The pitch value, in fixed .4 + * return The bitstream pitch index value + * + * The `x` vector is aligned on 32 bits + */ +static int refine_pitch(const int16_t *x, int n, int tc, int *pitch) +{ + float r[17], rm; + int e, f; + + int r0 = LC3_MAX( 32, 2*tc - 4); + int nr = LC3_MIN(228, 2*tc + 4) - r0 + 1; + + correlate(x, x - (r0 - 4), n, r, nr + 8); + + e = r0 + argmax(r + 4, nr, &rm); + const float *re = r + (e - (r0 - 4)); + + float dm = interpolate_corr(re, f = 0); + for (int i = 1; i <= 3; i++) { + float d; + + if (e >= 127 && ((i & 1) | (e >= 157))) + continue; + + if ((d = interpolate_corr(re, i)) > dm) + dm = d, f = i; + + if (e > 32 && (d = interpolate_corr(re, -i)) > dm) + dm = d, f = -i; + } + + e -= (f < 0); + f += 4*(f < 0); + + *pitch = 4*e + f; + return e < 127 ? 4*e + f - 128 : + e < 157 ? 2*e + (f >> 1) + 126 : e + 283; +} + +/** + * LTPF Analysis + */ +bool lc3_ltpf_analyse( + enum lc3_dt dt, enum lc3_srate sr, struct lc3_ltpf_analysis *ltpf, + const int16_t *x, struct lc3_ltpf_data *data) +{ + /* --- Resampling to 12.8 KHz --- */ + + int z_12k8 = sizeof(ltpf->x_12k8) / sizeof(*ltpf->x_12k8); + int n_12k8 = (1 + dt) * 32; + + memmove(ltpf->x_12k8, ltpf->x_12k8 + n_12k8, + (z_12k8 - n_12k8) * sizeof(*ltpf->x_12k8)); + + int16_t *x_12k8 = ltpf->x_12k8 + (z_12k8 - n_12k8); + + resample_12k8[sr](<pf->hp50, x, x_12k8, n_12k8); + + x_12k8 -= (dt == LC3_DT_7M5 ? 44 : 24); + + /* --- Resampling to 6.4 KHz --- */ + + int z_6k4 = sizeof(ltpf->x_6k4) / sizeof(*ltpf->x_6k4); + int n_6k4 = n_12k8 >> 1; + + memmove(ltpf->x_6k4, ltpf->x_6k4 + n_6k4, + (z_6k4 - n_6k4) * sizeof(*ltpf->x_6k4)); + + int16_t *x_6k4 = ltpf->x_6k4 + (z_6k4 - n_6k4); + + resample_6k4(x_12k8, x_6k4, n_6k4); + + /* --- Enlarge for small frame size --- */ + + if (dt == LC3_DT_2M5) { + x_12k8 -= n_12k8, x_6k4 -= n_6k4; + n_12k8 += n_12k8; n_6k4 += n_6k4; + } + + /* --- Pitch detection --- */ + + int tc, pitch = 0; + float nc = 0; + + bool pitch_present = detect_pitch(ltpf, x_6k4, n_6k4, &tc); + + if (pitch_present) { + int16_t u[128], v[128]; + + data->pitch_index = refine_pitch(x_12k8, n_12k8, tc, &pitch); + + interpolate(x_12k8, n_12k8, 0, u); + interpolate(x_12k8 - (pitch >> 2), n_12k8, pitch & 3, v); + + nc = dot(u, v, n_12k8) / sqrtf(dot(u, u, n_12k8) * dot(v, v, n_12k8)); + } + + /* --- Activation --- */ + + if (ltpf->active) { + int pitch_diff = + LC3_MAX(pitch, ltpf->pitch) - LC3_MIN(pitch, ltpf->pitch); + float nc_diff = nc - ltpf->nc[0]; + + data->active = !lc3_hr(sr) && pitch_present && + ((nc > 0.9f) || (nc > 0.84f && pitch_diff < 8 && nc_diff > -0.1f)); + + } else { + data->active = !lc3_hr(sr) && pitch_present && + ( (dt == LC3_DT_10M || ltpf->nc[1] > 0.94f) && + (ltpf->nc[0] > 0.94f && nc > 0.94f) ); + } + + ltpf->active = data->active; + ltpf->pitch = pitch; + ltpf->nc[1] = ltpf->nc[0]; + ltpf->nc[0] = nc; + + return pitch_present; +} + + +/* ---------------------------------------------------------------------------- + * Synthesis + * -------------------------------------------------------------------------- */ + +/** + * Width of synthesis filter + */ + +#define MAX_FILTER_WIDTH \ + (LC3_MAX_SRATE_HZ / 4000) + + +/** + * Synthesis filter template + * xh, nh History ring buffer of filtered samples + * lag Lag parameter in the ring buffer + * x0 w-1 previous input samples + * x, n Current samples as input, filtered as output + * c, w Coefficients `den` then `num`, and width of filter + * fade Fading mode of filter -1: Out 1: In 0: None + */ +LC3_HOT static inline void synthesize_template( + const float *xh, int nh, int lag, + const float *x0, float *x, int n, + const float *c, const int w, int fade) +{ + float g = (float)(fade <= 0); + float g_incr = (float)((fade > 0) - (fade < 0)) / n; + float u[MAX_FILTER_WIDTH]; + + /* --- Load previous samples --- */ + + lag += (w >> 1); + + const float *y = x - xh < lag ? x + (nh - lag) : x - lag; + const float *y_end = xh + nh - 1; + + for (int j = 0; j < w-1; j++) { + + u[j] = 0; + + float yi = *y, xi = *(x0++); + y = y < y_end ? y + 1 : xh; + + for (int k = 0; k <= j; k++) + u[j-k] -= yi * c[k]; + + for (int k = 0; k <= j; k++) + u[j-k] += xi * c[w+k]; + } + + u[w-1] = 0; + + /* --- Process by filter length --- */ + + for (int i = 0; i < n; i += w) + for (int j = 0; j < w; j++, g += g_incr) { + + float yi = *y, xi = *x; + y = y < y_end ? y + 1 : xh; + + for (int k = 0; k < w; k++) + u[(j+(w-1)-k)%w] -= yi * c[k]; + + for (int k = 0; k < w; k++) + u[(j+(w-1)-k)%w] += xi * c[w+k]; + + *(x++) = xi - g * u[j]; + u[j] = 0; + } +} + +/** + * Synthesis filter for each samplerates (width of filter) + */ + + +LC3_HOT static void synthesize_4(const float *xh, int nh, int lag, + const float *x0, float *x, int n, const float *c, int fade) +{ + synthesize_template(xh, nh, lag, x0, x, n, c, 4, fade); +} + +LC3_HOT static void synthesize_6(const float *xh, int nh, int lag, + const float *x0, float *x, int n, const float *c, int fade) +{ + synthesize_template(xh, nh, lag, x0, x, n, c, 6, fade); +} + +LC3_HOT static void synthesize_8(const float *xh, int nh, int lag, + const float *x0, float *x, int n, const float *c, int fade) +{ + synthesize_template(xh, nh, lag, x0, x, n, c, 8, fade); +} + +LC3_HOT static void synthesize_12(const float *xh, int nh, int lag, + const float *x0, float *x, int n, const float *c, int fade) +{ + synthesize_template(xh, nh, lag, x0, x, n, c, 12, fade); +} + +static void (* const synthesize[])(const float *, int, int, + const float *, float *, int, const float *, int) = +{ + [LC3_SRATE_8K ] = synthesize_4, + [LC3_SRATE_16K] = synthesize_4, + [LC3_SRATE_24K] = synthesize_6, + [LC3_SRATE_32K] = synthesize_8, + [LC3_SRATE_48K] = synthesize_12, +}; + + +/** + * LTPF Synthesis + */ +void lc3_ltpf_synthesize(enum lc3_dt dt, enum lc3_srate sr, int nbytes, + lc3_ltpf_synthesis_t *ltpf, const lc3_ltpf_data_t *data, + const float *xh, float *x) +{ + int nh = lc3_ns(dt, sr) + lc3_nh(dt, sr); + + /* --- Filter parameters --- */ + + int p_idx = data ? data->pitch_index : 0; + int pitch = + p_idx >= 440 ? (((p_idx ) - 283) << 2) : + p_idx >= 380 ? (((p_idx >> 1) - 63) << 2) + (((p_idx & 1)) << 1) : + (((p_idx >> 2) + 32) << 2) + (((p_idx & 3)) << 0) ; + + pitch = (pitch * lc3_ns(LC3_DT_10M, sr) + 64) / 128; + + int nbits = (nbytes*8 * (1 + LC3_DT_10M)) / (1 + dt); + if (dt == LC3_DT_2M5) + nbits = (6 * nbits + 5) / 10; + if (dt == LC3_DT_5M) + nbits -= 160; + + int g_idx = LC3_MAX(nbits / 80, (int)(3 + sr)) - (3 + sr); + bool active = data && data->active && g_idx < 4; + + int w = LC3_MAX(4, lc3_ns_4m[sr] >> 4); + float c[2 * MAX_FILTER_WIDTH]; + + for (int i = 0; i < w; i++) { + float g = active ? 0.4f - 0.05f * g_idx : 0; + c[ i] = g * lc3_ltpf_cden[sr][pitch & 3][(w-1)-i]; + c[w+i] = 0.85f * g * lc3_ltpf_cnum[sr][LC3_MIN(g_idx, 3)][(w-1)-i]; + } + + /* --- Transition handling --- */ + + int ns = lc3_ns(dt, sr); + int nt = ns / (1 + dt); + float x0[2][MAX_FILTER_WIDTH]; + + memcpy(x0[0], ltpf->x, (w-1) * sizeof(float)); + memcpy(ltpf->x, x + ns - (w-1), (w-1) * sizeof(float)); + + if (active) + memcpy(x0[1], x + nt-(w-1), (w-1) * sizeof(float)); + + if (!ltpf->active && active) + synthesize[sr](xh, nh, pitch/4, x0[0], x, nt, c, 1); + else if (ltpf->active && !active) + synthesize[sr](xh, nh, ltpf->pitch/4, x0[0], x, nt, ltpf->c, -1); + else if (ltpf->active && active && ltpf->pitch == pitch) + synthesize[sr](xh, nh, pitch/4, x0[0], x, nt, c, 0); + else if (ltpf->active && active) { + synthesize[sr](xh, nh, ltpf->pitch/4, x0[0], x, nt, ltpf->c, -1); + synthesize[sr](xh, nh, pitch/4, + (x <= xh ? x + nh : x) - (w-1), x, nt, c, 1); + } + + /* --- Remainder --- */ + + if (active && ns > nt) + synthesize[sr](xh, nh, pitch/4, x0[1], x + nt, ns-nt, c, 0); + + /* --- Update state --- */ + + ltpf->active = active; + ltpf->pitch = pitch; + memcpy(ltpf->c, c, 2*w * sizeof(*ltpf->c)); +} + + +/* ---------------------------------------------------------------------------- + * Bitstream data + * -------------------------------------------------------------------------- */ + +/** + * LTPF disable + */ +void lc3_ltpf_disable(struct lc3_ltpf_data *data) +{ + data->active = false; +} + +/** + * Return number of bits coding the bitstream data + */ +int lc3_ltpf_get_nbits(bool pitch) +{ + return 1 + 10 * pitch; +} + +/** + * Put bitstream data + */ +void lc3_ltpf_put_data(lc3_bits_t *bits, + const struct lc3_ltpf_data *data) +{ + lc3_put_bit(bits, data->active); + lc3_put_bits(bits, data->pitch_index, 9); +} + +/** + * Get bitstream data + */ +void lc3_ltpf_get_data(lc3_bits_t *bits, + struct lc3_ltpf_data *data) +{ + data->active = lc3_get_bit(bits); + data->pitch_index = lc3_get_bits(bits, 9); +} diff --git a/App_Resources/iOS/src/lc3/ltpf.h b/App_Resources/iOS/src/lc3/ltpf.h new file mode 100644 index 00000000..42cedc6a --- /dev/null +++ b/App_Resources/iOS/src/lc3/ltpf.h @@ -0,0 +1,104 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_LTPF_H +#define __LC3_LTPF_H + +#include "common.h" +#include "bits.h" + + +/** + * LTPF data + */ + +typedef struct lc3_ltpf_data { + bool active; + int pitch_index; +} lc3_ltpf_data_t; + + +/* ---------------------------------------------------------------------------- + * Encoding + * -------------------------------------------------------------------------- */ + +/** + * LTPF analysis + * dt, sr Duration and samplerate of the frame + * ltpf Context of analysis + * allowed True when activation of LTPF is allowed + * x [-d..-1] Previous, [0..ns-1] Current samples + * data Return bitstream data + * return True when pitch present, False otherwise + * + * The `x` vector is aligned on 32 bits + * The number of previous samples `d` accessed on `x` is : + * d: { 10, 20, 30, 40, 60 } - 1 for samplerates from 8KHz to 48KHz + */ +bool lc3_ltpf_analyse(enum lc3_dt dt, enum lc3_srate sr, + lc3_ltpf_analysis_t *ltpf, const int16_t *x, lc3_ltpf_data_t *data); + +/** + * LTPF disable + * data LTPF data, disabled activation on return + */ +void lc3_ltpf_disable(lc3_ltpf_data_t *data); + +/** + * Return number of bits coding the bitstream data + * pitch True when pitch present, False otherwise + * return Bit consumption, including the pitch present flag + */ +int lc3_ltpf_get_nbits(bool pitch); + +/** + * Put bitstream data + * bits Bitstream context + * data LTPF data + */ +void lc3_ltpf_put_data(lc3_bits_t *bits, const lc3_ltpf_data_t *data); + + +/* ---------------------------------------------------------------------------- + * Decoding + * -------------------------------------------------------------------------- */ +/** + * Get bitstream data + * bits Bitstream context + * data Return bitstream data + */ +void lc3_ltpf_get_data(lc3_bits_t *bits, lc3_ltpf_data_t *data); + +/** + * LTPF synthesis + * dt, sr Duration and samplerate of the frame + * nbytes Size in bytes of the frame + * ltpf Context of synthesis + * data Bitstream data, NULL when pitch not present + * xr Base address of ring buffer of decoded samples + * x Samples to proceed in the ring buffer, filtered as output + * + * The size of the ring buffer is `nh + ns`. + * The filtering needs an history of at least 18 ms. + */ +void lc3_ltpf_synthesize(enum lc3_dt dt, enum lc3_srate sr, int nbytes, + lc3_ltpf_synthesis_t *ltpf, const lc3_ltpf_data_t *data, + const float *xr, float *x); + + +#endif /* __LC3_LTPF_H */ diff --git a/App_Resources/iOS/src/lc3/ltpf_arm.h b/App_Resources/iOS/src/lc3/ltpf_arm.h new file mode 100644 index 00000000..fab35290 --- /dev/null +++ b/App_Resources/iOS/src/lc3/ltpf_arm.h @@ -0,0 +1,506 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#if (__ARM_FEATURE_SIMD32 && !(__GNUC__ < 10) || defined(TEST_ARM)) + +#ifndef TEST_ARM + +#include + +static inline int16x2_t __pkhbt(int16x2_t a, int16x2_t b) +{ + int16x2_t r; + __asm("pkhbt %0, %1, %2" : "=r" (r) : "r" (a), "r" (b)); + return r; +} + +#endif /* TEST_ARM */ + + +/** + * Import + */ + +static inline int32_t filter_hp50(struct lc3_ltpf_hp50_state *, int32_t); +static inline float dot(const int16_t *, const int16_t *, int); + + +/** + * Resample from 8 / 16 / 32 KHz to 12.8 KHz Template + */ +#if !defined(resample_8k_12k8) || !defined(resample_16k_12k8) \ + || !defined(resample_32k_12k8) +static inline void arm_resample_x64k_12k8(const int p, const int16x2_t *h, + struct lc3_ltpf_hp50_state *hp50, const int16x2_t *x, int16_t *y, int n) +{ + const int w = 40 / p; + + x -= w; + + for (int i = 0; i < 5*n; i += 5) { + const int16x2_t *hn = h + (i % (2*p)) * (48 / p); + const int16x2_t *xn = x + (i / (2*p)); + + int32_t un = __smlad(*(xn++), *(hn++), 0); + + for (int k = 0; k < w; k += 5) { + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + } + + int32_t yn = filter_hp50(hp50, un); + *(y++) = (yn + (1 << 15)) >> 16; + } +} +#endif + +/** + * Resample from 24 / 48 KHz to 12.8 KHz Template + */ +#if !defined(resample_24k_12k8) || !defined(resample_48k_12k8) +static inline void arm_resample_x192k_12k8(const int p, const int16x2_t *h, + struct lc3_ltpf_hp50_state *hp50, const int16x2_t *x, int16_t *y, int n) +{ + const int w = 120 / p; + + x -= w; + + for (int i = 0; i < 15*n; i += 15) { + const int16x2_t *hn = h + (i % (2*p)) * (128 / p); + const int16x2_t *xn = x + (i / (2*p)); + + int32_t un = __smlad(*(xn++), *(hn++), 0); + + for (int k = 0; k < w; k += 15) { + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + un = __smlad(*(xn++), *(hn++), un); + } + + int32_t yn = filter_hp50(hp50, un); + *(y++) = (yn + (1 << 15)) >> 16; + } +} +#endif + +/** + * Resample from 8 Khz to 12.8 KHz + */ +#ifndef resample_8k_12k8 + +static void arm_resample_8k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + alignas(int32_t) static const int16_t h[2*8*12] = { + 0, 214, 417, -1052, -4529, 26233, -4529, -1052, 417, 214, 0, 0, + 0, 180, 0, -1522, -2427, 24506, -5289, 0, 763, 156, -28, 0, + 0, 92, -323, -1361, 0, 19741, -3885, 1317, 861, 0, -61, 0, + 0, 0, -457, -752, 1873, 13068, 0, 2389, 598, -213, -79, 0, + 0, -61, -398, 0, 2686, 5997, 5997, 2686, 0, -398, -61, 0, + 0, -79, -213, 598, 2389, 0, 13068, 1873, -752, -457, 0, 0, + 0, -61, 0, 861, 1317, -3885, 19741, 0, -1361, -323, 92, 0, + 0, -28, 156, 763, 0, -5289, 24506, -2427, -1522, 0, 180, 0, + 0, 0, 214, 417, -1052, -4529, 26233, -4529, -1052, 417, 214, 0, + 0, 0, 180, 0, -1522, -2427, 24506, -5289, 0, 763, 156, -28, + 0, 0, 92, -323, -1361, 0, 19741, -3885, 1317, 861, 0, -61, + 0, 0, 0, -457, -752, 1873, 13068, 0, 2389, 598, -213, -79, + 0, 0, -61, -398, 0, 2686, 5997, 5997, 2686, 0, -398, -61, + 0, 0, -79, -213, 598, 2389, 0, 13068, 1873, -752, -457, 0, + 0, 0, -61, 0, 861, 1317, -3885, 19741, 0, -1361, -323, 92, + 0, 0, -28, 156, 763, 0, -5289, 24506, -2427, -1522, 0, 180, + }; + + arm_resample_x64k_12k8( + 8, (const int16x2_t *)h, hp50, (int16x2_t *)x, y, n); +} + +#ifndef TEST_ARM +#define resample_8k_12k8 arm_resample_8k_12k8 +#endif + +#endif /* resample_8k_12k8 */ + +/** + * Resample from 16 Khz to 12.8 KHz + */ +#ifndef resample_16k_12k8 + +static void arm_resample_16k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + alignas(int32_t) static const int16_t h[2*4*24] = { + + 0, -61, 214, -398, 417, 0, -1052, 2686, + -4529, 5997, 26233, 5997, -4529, 2686, -1052, 0, + 417, -398, 214, -61, 0, 0, 0, 0, + + + 0, -79, 180, -213, 0, 598, -1522, 2389, + -2427, 0, 24506, 13068, -5289, 1873, 0, -752, + 763, -457, 156, 0, -28, 0, 0, 0, + + + 0, -61, 92, 0, -323, 861, -1361, 1317, + 0, -3885, 19741, 19741, -3885, 0, 1317, -1361, + 861, -323, 0, 92, -61, 0, 0, 0, + + 0, -28, 0, 156, -457, 763, -752, 0, + 1873, -5289, 13068, 24506, 0, -2427, 2389, -1522, + 598, 0, -213, 180, -79, 0, 0, 0, + + + 0, 0, -61, 214, -398, 417, 0, -1052, + 2686, -4529, 5997, 26233, 5997, -4529, 2686, -1052, + 0, 417, -398, 214, -61, 0, 0, 0, + + + 0, 0, -79, 180, -213, 0, 598, -1522, + 2389, -2427, 0, 24506, 13068, -5289, 1873, 0, + -752, 763, -457, 156, 0, -28, 0, 0, + + + 0, 0, -61, 92, 0, -323, 861, -1361, + 1317, 0, -3885, 19741, 19741, -3885, 0, 1317, + -1361, 861, -323, 0, 92, -61, 0, 0, + + 0, 0, -28, 0, 156, -457, 763, -752, + 0, 1873, -5289, 13068, 24506, 0, -2427, 2389, + -1522, 598, 0, -213, 180, -79, 0, 0, + }; + + arm_resample_x64k_12k8( + 4, (const int16x2_t *)h, hp50, (int16x2_t *)x, y, n); +} + +#ifndef TEST_ARM +#define resample_16k_12k8 arm_resample_16k_12k8 +#endif + +#endif /* resample_16k_12k8 */ + +/** + * Resample from 32 Khz to 12.8 KHz + */ +#ifndef resample_32k_12k8 + +static void arm_resample_32k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + alignas(int32_t) static const int16_t h[2*2*48] = { + + 0, -30, -31, 46, 107, 0, -199, -162, + 209, 430, 0, -681, -526, 658, 1343, 0, + -2264, -1943, 2999, 9871, 13116, 9871, 2999, -1943, + -2264, 0, 1343, 658, -526, -681, 0, 430, + 209, -162, -199, 0, 107, 46, -31, -30, + 0, 0, 0, 0, 0, 0, 0, 0, + + 0, -14, -39, 0, 90, 78, -106, -229, + 0, 382, 299, -376, -761, 0, 1194, 937, + -1214, -2644, 0, 6534, 12253, 12253, 6534, 0, + -2644, -1214, 937, 1194, 0, -761, -376, 299, + 382, 0, -229, -106, 78, 90, 0, -39, + -14, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, -30, -31, 46, 107, 0, -199, + -162, 209, 430, 0, -681, -526, 658, 1343, + 0, -2264, -1943, 2999, 9871, 13116, 9871, 2999, + -1943, -2264, 0, 1343, 658, -526, -681, 0, + 430, 209, -162, -199, 0, 107, 46, -31, + -30, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, -14, -39, 0, 90, 78, -106, + -229, 0, 382, 299, -376, -761, 0, 1194, + 937, -1214, -2644, 0, 6534, 12253, 12253, 6534, + 0, -2644, -1214, 937, 1194, 0, -761, -376, + 299, 382, 0, -229, -106, 78, 90, 0, + -39, -14, 0, 0, 0, 0, 0, 0, + }; + + arm_resample_x64k_12k8( + 2, (const int16x2_t *)h, hp50, (int16x2_t *)x, y, n); +} + +#ifndef TEST_ARM +#define resample_32k_12k8 arm_resample_32k_12k8 +#endif + +#endif /* resample_32k_12k8 */ + +/** + * Resample from 24 Khz to 12.8 KHz + */ +#ifndef resample_24k_12k8 + +static void arm_resample_24k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + alignas(int32_t) static const int16_t h[2*8*32] = { + + 0, -50, 19, 143, -93, -290, 278, 485, + -658, -701, 1396, 901, -3019, -1042, 10276, 17488, + 10276, -1042, -3019, 901, 1396, -701, -658, 485, + 278, -290, -93, 143, 19, -50, 0, 0, + + 0, -46, 0, 141, -45, -305, 185, 543, + -501, -854, 1153, 1249, -2619, -1908, 8712, 17358, + 11772, 0, -3319, 480, 1593, -504, -796, 399, + 367, -261, -142, 138, 40, -52, -5, 0, + + 0, -41, -17, 133, 0, -304, 91, 574, + -334, -959, 878, 1516, -2143, -2590, 7118, 16971, + 13161, 1202, -3495, 0, 1731, -267, -908, 287, + 445, -215, -188, 125, 62, -52, -12, 0, + + 0, -34, -30, 120, 41, -291, 0, 577, + -164, -1015, 585, 1697, -1618, -3084, 5534, 16337, + 14406, 2544, -3526, -523, 1800, 0, -985, 152, + 509, -156, -230, 104, 83, -48, -19, 0, + + 0, -26, -41, 103, 76, -265, -83, 554, + 0, -1023, 288, 1791, -1070, -3393, 3998, 15474, + 15474, 3998, -3393, -1070, 1791, 288, -1023, 0, + 554, -83, -265, 76, 103, -41, -26, 0, + + 0, -19, -48, 83, 104, -230, -156, 509, + 152, -985, 0, 1800, -523, -3526, 2544, 14406, + 16337, 5534, -3084, -1618, 1697, 585, -1015, -164, + 577, 0, -291, 41, 120, -30, -34, 0, + + 0, -12, -52, 62, 125, -188, -215, 445, + 287, -908, -267, 1731, 0, -3495, 1202, 13161, + 16971, 7118, -2590, -2143, 1516, 878, -959, -334, + 574, 91, -304, 0, 133, -17, -41, 0, + + 0, -5, -52, 40, 138, -142, -261, 367, + 399, -796, -504, 1593, 480, -3319, 0, 11772, + 17358, 8712, -1908, -2619, 1249, 1153, -854, -501, + 543, 185, -305, -45, 141, 0, -46, 0, + + 0, 0, -50, 19, 143, -93, -290, 278, + 485, -658, -701, 1396, 901, -3019, -1042, 10276, + 17488, 10276, -1042, -3019, 901, 1396, -701, -658, + 485, 278, -290, -93, 143, 19, -50, 0, + + 0, 0, -46, 0, 141, -45, -305, 185, + 543, -501, -854, 1153, 1249, -2619, -1908, 8712, + 17358, 11772, 0, -3319, 480, 1593, -504, -796, + 399, 367, -261, -142, 138, 40, -52, -5, + + 0, 0, -41, -17, 133, 0, -304, 91, + 574, -334, -959, 878, 1516, -2143, -2590, 7118, + 16971, 13161, 1202, -3495, 0, 1731, -267, -908, + 287, 445, -215, -188, 125, 62, -52, -12, + + 0, 0, -34, -30, 120, 41, -291, 0, + 577, -164, -1015, 585, 1697, -1618, -3084, 5534, + 16337, 14406, 2544, -3526, -523, 1800, 0, -985, + 152, 509, -156, -230, 104, 83, -48, -19, + + 0, 0, -26, -41, 103, 76, -265, -83, + 554, 0, -1023, 288, 1791, -1070, -3393, 3998, + 15474, 15474, 3998, -3393, -1070, 1791, 288, -1023, + 0, 554, -83, -265, 76, 103, -41, -26, + + 0, 0, -19, -48, 83, 104, -230, -156, + 509, 152, -985, 0, 1800, -523, -3526, 2544, + 14406, 16337, 5534, -3084, -1618, 1697, 585, -1015, + -164, 577, 0, -291, 41, 120, -30, -34, + + 0, 0, -12, -52, 62, 125, -188, -215, + 445, 287, -908, -267, 1731, 0, -3495, 1202, + 13161, 16971, 7118, -2590, -2143, 1516, 878, -959, + -334, 574, 91, -304, 0, 133, -17, -41, + + 0, 0, -5, -52, 40, 138, -142, -261, + 367, 399, -796, -504, 1593, 480, -3319, 0, + 11772, 17358, 8712, -1908, -2619, 1249, 1153, -854, + -501, 543, 185, -305, -45, 141, 0, -46, + }; + + arm_resample_x192k_12k8( + 8, (const int16x2_t *)h, hp50, (int16x2_t *)x, y, n); +} + +#ifndef TEST_ARM +#define resample_24k_12k8 arm_resample_24k_12k8 +#endif + +#endif /* resample_24k_12k8 */ + +/** + * Resample from 48 Khz to 12.8 KHz + */ +#ifndef resample_48k_12k8 + +static void arm_resample_48k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + alignas(int32_t) static const int16_t h[2*4*64] = { + + 0, -13, -25, -20, 10, 51, 71, 38, + -47, -133, -145, -42, 139, 277, 242, 0, + -329, -511, -351, 144, 698, 895, 450, -535, + -1510, -1697, -521, 1999, 5138, 7737, 8744, 7737, + 5138, 1999, -521, -1697, -1510, -535, 450, 895, + 698, 144, -351, -511, -329, 0, 242, 277, + 139, -42, -145, -133, -47, 38, 71, 51, + 10, -20, -25, -13, 0, 0, 0, 0, + + 0, -9, -23, -24, 0, 41, 71, 52, + -23, -115, -152, -78, 92, 254, 272, 76, + -251, -493, -427, 0, 576, 900, 624, -262, + -1309, -1763, -954, 1272, 4356, 7203, 8679, 8169, + 5886, 2767, 0, -1542, -1660, -809, 240, 848, + 796, 292, -252, -507, -398, -82, 199, 288, + 183, 0, -130, -145, -71, 20, 69, 60, + 20, -15, -26, -17, -3, 0, 0, 0, + + 0, -6, -20, -26, -8, 31, 67, 62, + 0, -94, -152, -108, 45, 223, 287, 143, + -167, -454, -480, -134, 439, 866, 758, 0, + -1071, -1748, -1295, 601, 3559, 6580, 8485, 8485, + 6580, 3559, 601, -1295, -1748, -1071, 0, 758, + 866, 439, -134, -480, -454, -167, 143, 287, + 223, 45, -108, -152, -94, 0, 62, 67, + 31, -8, -26, -20, -6, 0, 0, 0, + + 0, -3, -17, -26, -15, 20, 60, 69, + 20, -71, -145, -130, 0, 183, 288, 199, + -82, -398, -507, -252, 292, 796, 848, 240, + -809, -1660, -1542, 0, 2767, 5886, 8169, 8679, + 7203, 4356, 1272, -954, -1763, -1309, -262, 624, + 900, 576, 0, -427, -493, -251, 76, 272, + 254, 92, -78, -152, -115, -23, 52, 71, + 41, 0, -24, -23, -9, 0, 0, 0, + + 0, 0, -13, -25, -20, 10, 51, 71, + 38, -47, -133, -145, -42, 139, 277, 242, + 0, -329, -511, -351, 144, 698, 895, 450, + -535, -1510, -1697, -521, 1999, 5138, 7737, 8744, + 7737, 5138, 1999, -521, -1697, -1510, -535, 450, + 895, 698, 144, -351, -511, -329, 0, 242, + 277, 139, -42, -145, -133, -47, 38, 71, + 51, 10, -20, -25, -13, 0, 0, 0, + + 0, 0, -9, -23, -24, 0, 41, 71, + 52, -23, -115, -152, -78, 92, 254, 272, + 76, -251, -493, -427, 0, 576, 900, 624, + -262, -1309, -1763, -954, 1272, 4356, 7203, 8679, + 8169, 5886, 2767, 0, -1542, -1660, -809, 240, + 848, 796, 292, -252, -507, -398, -82, 199, + 288, 183, 0, -130, -145, -71, 20, 69, + 60, 20, -15, -26, -17, -3, 0, 0, + + 0, 0, -6, -20, -26, -8, 31, 67, + 62, 0, -94, -152, -108, 45, 223, 287, + 143, -167, -454, -480, -134, 439, 866, 758, + 0, -1071, -1748, -1295, 601, 3559, 6580, 8485, + 8485, 6580, 3559, 601, -1295, -1748, -1071, 0, + 758, 866, 439, -134, -480, -454, -167, 143, + 287, 223, 45, -108, -152, -94, 0, 62, + 67, 31, -8, -26, -20, -6, 0, 0, + + 0, 0, -3, -17, -26, -15, 20, 60, + 69, 20, -71, -145, -130, 0, 183, 288, + 199, -82, -398, -507, -252, 292, 796, 848, + 240, -809, -1660, -1542, 0, 2767, 5886, 8169, + 8679, 7203, 4356, 1272, -954, -1763, -1309, -262, + 624, 900, 576, 0, -427, -493, -251, 76, + 272, 254, 92, -78, -152, -115, -23, 52, + 71, 41, 0, -24, -23, -9, 0, 0, + }; + + arm_resample_x192k_12k8( + 4, (const int16x2_t *)h, hp50, (int16x2_t *)x, y, n); +} + +#ifndef TEST_ARM +#define resample_48k_12k8 arm_resample_48k_12k8 +#endif + +#endif /* resample_48k_12k8 */ + +/** + * Return vector of correlations + */ +#ifndef correlate + +static void arm_correlate( + const int16_t *a, const int16_t *b, int n, float *y, int nc) +{ + /* --- Check alignment of `b` --- */ + + if ((uintptr_t)b & 3) + *(y++) = dot(a, b--, n), nc--; + + /* --- Processing by pair --- */ + + for ( ; nc >= 2; nc -= 2) { + const int16x2_t *an = (const int16x2_t *)(a ); + const int16x2_t *bn = (const int16x2_t *)(b--); + + int16x2_t ax, b0, b1; + int64_t v0 = 0, v1 = 0; + + b1 = (int16x2_t)*(b--) << 16; + + for (int i = 0; i < (n >> 4); i++ ) + for (int j = 0; j < 4; j++) { + + ax = *(an++), b0 = *(bn++); + v0 = __smlald (ax, b0, v0); + v1 = __smlaldx(ax, __pkhbt(b0, b1), v1); + + ax = *(an++), b1 = *(bn++); + v0 = __smlald (ax, b1, v0); + v1 = __smlaldx(ax, __pkhbt(b1, b0), v1); + } + + *(y++) = (float)((int32_t)((v0 + (1 << 5)) >> 6)); + *(y++) = (float)((int32_t)((v1 + (1 << 5)) >> 6)); + } + + /* --- Odd element count --- */ + + if (nc > 0) + *(y++) = dot(a, b, n); +} + +#ifndef TEST_ARM +#define correlate arm_correlate +#endif + +#endif /* correlate */ + +#endif /* __ARM_FEATURE_SIMD32 */ diff --git a/App_Resources/iOS/src/lc3/ltpf_neon.h b/App_Resources/iOS/src/lc3/ltpf_neon.h new file mode 100644 index 00000000..27ba5d8e --- /dev/null +++ b/App_Resources/iOS/src/lc3/ltpf_neon.h @@ -0,0 +1,281 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#if __ARM_NEON && __ARM_ARCH_ISA_A64 && \ + !defined(TEST_ARM) || defined(TEST_NEON) + +#ifndef TEST_NEON +#include +#endif /* TEST_NEON */ + + +/** + * Import + */ + +static inline int32_t filter_hp50(struct lc3_ltpf_hp50_state *, int32_t); + + +/** + * Resample from 16 Khz to 12.8 KHz + */ +#ifndef resample_16k_12k8 + +LC3_HOT static void neon_resample_16k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + static const int16_t h[4][20] = { + + { -61, 214, -398, 417, 0, -1052, 2686, -4529, 5997, 26233, + 5997, -4529, 2686, -1052, 0, 417, -398, 214, -61, 0 }, + + { -79, 180, -213, 0, 598, -1522, 2389, -2427, 0, 24506, + 13068, -5289, 1873, 0, -752, 763, -457, 156, 0, -28 }, + + { -61, 92, 0, -323, 861, -1361, 1317, 0, -3885, 19741, + 19741, -3885, 0, 1317, -1361, 861, -323, 0, 92, -61 }, + + { -28, 0, 156, -457, 763, -752, 0, 1873, -5289, 13068, + 24506, 0, -2427, 2389, -1522, 598, 0, -213, 180, -79 }, + + }; + + x -= 20 - 1; + + for (int i = 0; i < 5*n; i += 5) { + const int16_t *hn = h[i & 3]; + const int16_t *xn = x + (i >> 2); + int32x4_t un; + + un = vmull_s16( vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4; + un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4; + un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4; + un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4; + un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4; + + int32_t yn = filter_hp50(hp50, vaddvq_s32(un)); + *(y++) = (yn + (1 << 15)) >> 16; + } +} + +#ifndef TEST_NEON +#define resample_16k_12k8 neon_resample_16k_12k8 +#endif + +#endif /* resample_16k_12k8 */ + +/** + * Resample from 32 Khz to 12.8 KHz + */ +#ifndef resample_32k_12k8 + +LC3_HOT static void neon_resample_32k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + x -= 40 - 1; + + static const int16_t h[2][40] = { + + { -30, -31, 46, 107, 0, -199, -162, 209, 430, 0, + -681, -526, 658, 1343, 0, -2264, -1943, 2999, 9871, 13116, + 9871, 2999, -1943, -2264, 0, 1343, 658, -526, -681, 0, + 430, 209, -162, -199, 0, 107, 46, -31, -30, 0 }, + + { -14, -39, 0, 90, 78, -106, -229, 0, 382, 299, + -376, -761, 0, 1194, 937, -1214, -2644, 0, 6534, 12253, + 12253, 6534, 0, -2644, -1214, 937, 1194, 0, -761, -376, + 299, 382, 0, -229, -106, 78, 90, 0, -39, -14 }, + + }; + + for (int i = 0; i < 5*n; i += 5) { + const int16_t *hn = h[i & 1]; + const int16_t *xn = x + (i >> 1); + + int32x4_t un = vmull_s16(vld1_s16(xn), vld1_s16(hn)); + xn += 4, hn += 4; + + for (int i = 1; i < 10; i++) + un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4; + + int32_t yn = filter_hp50(hp50, vaddvq_s32(un)); + *(y++) = (yn + (1 << 15)) >> 16; + } +} + +#ifndef TEST_NEON +#define resample_32k_12k8 neon_resample_32k_12k8 +#endif + +#endif /* resample_32k_12k8 */ + +/** + * Resample from 48 Khz to 12.8 KHz + */ +#ifndef resample_48k_12k8 + +LC3_HOT static void neon_resample_48k_12k8( + struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n) +{ + alignas(16) static const int16_t h[4][64] = { + + { -13, -25, -20, 10, 51, 71, 38, -47, -133, -145, + -42, 139, 277, 242, 0, -329, -511, -351, 144, 698, + 895, 450, -535, -1510, -1697, -521, 1999, 5138, 7737, 8744, + 7737, 5138, 1999, -521, -1697, -1510, -535, 450, 895, 698, + 144, -351, -511, -329, 0, 242, 277, 139, -42, -145, + -133, -47, 38, 71, 51, 10, -20, -25, -13, 0 }, + + { -9, -23, -24, 0, 41, 71, 52, -23, -115, -152, + -78, 92, 254, 272, 76, -251, -493, -427, 0, 576, + 900, 624, -262, -1309, -1763, -954, 1272, 4356, 7203, 8679, + 8169, 5886, 2767, 0, -1542, -1660, -809, 240, 848, 796, + 292, -252, -507, -398, -82, 199, 288, 183, 0, -130, + -145, -71, 20, 69, 60, 20, -15, -26, -17, -3 }, + + { -6, -20, -26, -8, 31, 67, 62, 0, -94, -152, + -108, 45, 223, 287, 143, -167, -454, -480, -134, 439, + 866, 758, 0, -1071, -1748, -1295, 601, 3559, 6580, 8485, + 8485, 6580, 3559, 601, -1295, -1748, -1071, 0, 758, 866, + 439, -134, -480, -454, -167, 143, 287, 223, 45, -108, + -152, -94, 0, 62, 67, 31, -8, -26, -20, -6 }, + + { -3, -17, -26, -15, 20, 60, 69, 20, -71, -145, + -130, 0, 183, 288, 199, -82, -398, -507, -252, 292, + 796, 848, 240, -809, -1660, -1542, 0, 2767, 5886, 8169, + 8679, 7203, 4356, 1272, -954, -1763, -1309, -262, 624, 900, + 576, 0, -427, -493, -251, 76, 272, 254, 92, -78, + -152, -115, -23, 52, 71, 41, 0, -24, -23, -9 }, + + }; + + x -= 60 - 1; + + for (int i = 0; i < 15*n; i += 15) { + const int16_t *hn = h[i & 3]; + const int16_t *xn = x + (i >> 2); + + int32x4_t un = vmull_s16(vld1_s16(xn), vld1_s16(hn)); + xn += 4, hn += 4; + + for (int i = 1; i < 15; i++) + un = vmlal_s16(un, vld1_s16(xn), vld1_s16(hn)), xn += 4, hn += 4; + + int32_t yn = filter_hp50(hp50, vaddvq_s32(un)); + *(y++) = (yn + (1 << 15)) >> 16; + } +} + +#ifndef TEST_NEON +#define resample_48k_12k8 neon_resample_48k_12k8 +#endif + +#endif /* resample_48k_12k8 */ + +/** + * Return dot product of 2 vectors + */ +#ifndef dot + +LC3_HOT static inline float neon_dot(const int16_t *a, const int16_t *b, int n) +{ + int64x2_t v = vmovq_n_s64(0); + + for (int i = 0; i < (n >> 4); i++) { + int32x4_t u; + + u = vmull_s16( vld1_s16(a), vld1_s16(b)), a += 4, b += 4; + u = vmlal_s16(u, vld1_s16(a), vld1_s16(b)), a += 4, b += 4; + v = vpadalq_s32(v, u); + + u = vmull_s16( vld1_s16(a), vld1_s16(b)), a += 4, b += 4; + u = vmlal_s16(u, vld1_s16(a), vld1_s16(b)), a += 4, b += 4; + v = vpadalq_s32(v, u); + } + + int32_t v32 = (vaddvq_s64(v) + (1 << 5)) >> 6; + return (float)v32; +} + +#ifndef TEST_NEON +#define dot neon_dot +#endif + +#endif /* dot */ + +/** + * Return vector of correlations + */ +#ifndef correlate + +LC3_HOT static void neon_correlate( + const int16_t *a, const int16_t *b, int n, float *y, int nc) +{ + for ( ; nc >= 4; nc -= 4, b -= 4) { + const int16_t *an = (const int16_t *)a; + const int16_t *bn = (const int16_t *)b; + + int64x2_t v0 = vmovq_n_s64(0), v1 = v0, v2 = v0, v3 = v0; + int16x4_t ax, b0, b1; + + b0 = vld1_s16(bn-4); + + for (int i=0; i < (n >> 4); i++ ) + for (int j = 0; j < 2; j++) { + int32x4_t u0, u1, u2, u3; + + b1 = b0; + b0 = vld1_s16(bn), bn += 4; + ax = vld1_s16(an), an += 4; + + u0 = vmull_s16(ax, b0); + u1 = vmull_s16(ax, vext_s16(b1, b0, 3)); + u2 = vmull_s16(ax, vext_s16(b1, b0, 2)); + u3 = vmull_s16(ax, vext_s16(b1, b0, 1)); + + b1 = b0; + b0 = vld1_s16(bn), bn += 4; + ax = vld1_s16(an), an += 4; + + u0 = vmlal_s16(u0, ax, b0); + u1 = vmlal_s16(u1, ax, vext_s16(b1, b0, 3)); + u2 = vmlal_s16(u2, ax, vext_s16(b1, b0, 2)); + u3 = vmlal_s16(u3, ax, vext_s16(b1, b0, 1)); + + v0 = vpadalq_s32(v0, u0); + v1 = vpadalq_s32(v1, u1); + v2 = vpadalq_s32(v2, u2); + v3 = vpadalq_s32(v3, u3); + } + + *(y++) = (float)((int32_t)((vaddvq_s64(v0) + (1 << 5)) >> 6)); + *(y++) = (float)((int32_t)((vaddvq_s64(v1) + (1 << 5)) >> 6)); + *(y++) = (float)((int32_t)((vaddvq_s64(v2) + (1 << 5)) >> 6)); + *(y++) = (float)((int32_t)((vaddvq_s64(v3) + (1 << 5)) >> 6)); + } + + for ( ; nc > 0; nc--) + *(y++) = neon_dot(a, b--, n); +} +#endif /* correlate */ + +#ifndef TEST_NEON +#define correlate neon_correlate +#endif + +#endif /* __ARM_NEON && __ARM_ARCH_ISA_A64 */ diff --git a/App_Resources/iOS/src/lc3/mdct.c b/App_Resources/iOS/src/lc3/mdct.c new file mode 100644 index 00000000..51d9579c --- /dev/null +++ b/App_Resources/iOS/src/lc3/mdct.c @@ -0,0 +1,475 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "mdct.h" +#include "tables.h" + +#include "mdct_neon.h" + + +/* ---------------------------------------------------------------------------- + * FFT processing + * -------------------------------------------------------------------------- */ + +/** + * FFT 5 Points + * x, y Input and output coefficients, of size 5xn + * n Number of interleaved transform to perform (n % 2 = 0) + */ +#ifndef fft_5 +LC3_HOT static inline void fft_5( + const struct lc3_complex *x, struct lc3_complex *y, int n) +{ + static const float cos1 = 0.3090169944; /* cos(-2Pi 1/5) */ + static const float cos2 = -0.8090169944; /* cos(-2Pi 2/5) */ + + static const float sin1 = -0.9510565163; /* sin(-2Pi 1/5) */ + static const float sin2 = -0.5877852523; /* sin(-2Pi 2/5) */ + + for (int i = 0; i < n; i++, x++, y+= 5) { + + struct lc3_complex s14 = + { x[1*n].re + x[4*n].re, x[1*n].im + x[4*n].im }; + struct lc3_complex d14 = + { x[1*n].re - x[4*n].re, x[1*n].im - x[4*n].im }; + + struct lc3_complex s23 = + { x[2*n].re + x[3*n].re, x[2*n].im + x[3*n].im }; + struct lc3_complex d23 = + { x[2*n].re - x[3*n].re, x[2*n].im - x[3*n].im }; + + y[0].re = x[0].re + s14.re + s23.re; + + y[0].im = x[0].im + s14.im + s23.im; + + y[1].re = x[0].re + s14.re * cos1 - d14.im * sin1 + + s23.re * cos2 - d23.im * sin2; + + y[1].im = x[0].im + s14.im * cos1 + d14.re * sin1 + + s23.im * cos2 + d23.re * sin2; + + y[2].re = x[0].re + s14.re * cos2 - d14.im * sin2 + + s23.re * cos1 + d23.im * sin1; + + y[2].im = x[0].im + s14.im * cos2 + d14.re * sin2 + + s23.im * cos1 - d23.re * sin1; + + y[3].re = x[0].re + s14.re * cos2 + d14.im * sin2 + + s23.re * cos1 - d23.im * sin1; + + y[3].im = x[0].im + s14.im * cos2 - d14.re * sin2 + + s23.im * cos1 + d23.re * sin1; + + y[4].re = x[0].re + s14.re * cos1 + d14.im * sin1 + + s23.re * cos2 + d23.im * sin2; + + y[4].im = x[0].im + s14.im * cos1 - d14.re * sin1 + + s23.im * cos2 - d23.re * sin2; + } +} +#endif /* fft_5 */ + +/** + * FFT Butterfly 3 Points + * x, y Input and output coefficients + * twiddles Twiddles factors, determine size of transform + * n Number of interleaved transforms + */ +#ifndef fft_bf3 +LC3_HOT static inline void fft_bf3( + const struct lc3_fft_bf3_twiddles *twiddles, + const struct lc3_complex *x, struct lc3_complex *y, int n) +{ + int n3 = twiddles->n3; + const struct lc3_complex (*w0)[2] = twiddles->t; + const struct lc3_complex (*w1)[2] = w0 + n3, (*w2)[2] = w1 + n3; + + const struct lc3_complex *x0 = x, *x1 = x0 + n*n3, *x2 = x1 + n*n3; + struct lc3_complex *y0 = y, *y1 = y0 + n3, *y2 = y1 + n3; + + for (int i = 0; i < n; i++, y0 += 3*n3, y1 += 3*n3, y2 += 3*n3) + for (int j = 0; j < n3; j++, x0++, x1++, x2++) { + + y0[j].re = x0->re + x1->re * w0[j][0].re - x1->im * w0[j][0].im + + x2->re * w0[j][1].re - x2->im * w0[j][1].im; + + y0[j].im = x0->im + x1->im * w0[j][0].re + x1->re * w0[j][0].im + + x2->im * w0[j][1].re + x2->re * w0[j][1].im; + + y1[j].re = x0->re + x1->re * w1[j][0].re - x1->im * w1[j][0].im + + x2->re * w1[j][1].re - x2->im * w1[j][1].im; + + y1[j].im = x0->im + x1->im * w1[j][0].re + x1->re * w1[j][0].im + + x2->im * w1[j][1].re + x2->re * w1[j][1].im; + + y2[j].re = x0->re + x1->re * w2[j][0].re - x1->im * w2[j][0].im + + x2->re * w2[j][1].re - x2->im * w2[j][1].im; + + y2[j].im = x0->im + x1->im * w2[j][0].re + x1->re * w2[j][0].im + + x2->im * w2[j][1].re + x2->re * w2[j][1].im; + } +} +#endif /* fft_bf3 */ + +/** + * FFT Butterfly 2 Points + * twiddles Twiddles factors, determine size of transform + * x, y Input and output coefficients + * n Number of interleaved transforms + */ +#ifndef fft_bf2 +LC3_HOT static inline void fft_bf2( + const struct lc3_fft_bf2_twiddles *twiddles, + const struct lc3_complex *x, struct lc3_complex *y, int n) +{ + int n2 = twiddles->n2; + const struct lc3_complex *w = twiddles->t; + + const struct lc3_complex *x0 = x, *x1 = x0 + n*n2; + struct lc3_complex *y0 = y, *y1 = y0 + n2; + + for (int i = 0; i < n; i++, y0 += 2*n2, y1 += 2*n2) { + + for (int j = 0; j < n2; j++, x0++, x1++) { + + y0[j].re = x0->re + x1->re * w[j].re - x1->im * w[j].im; + y0[j].im = x0->im + x1->im * w[j].re + x1->re * w[j].im; + + y1[j].re = x0->re - x1->re * w[j].re + x1->im * w[j].im; + y1[j].im = x0->im - x1->im * w[j].re - x1->re * w[j].im; + } + } +} +#endif /* fft_bf2 */ + +/** + * Perform FFT + * x, y0, y1 Input, and 2 scratch buffers of size `n` + * n Number of points 30, 40, 60, 80, 90, 120, 160, 180, 240, 480 + * return The buffer `y0` or `y1` that hold the result + * + * Input `x` can be the same as the `y0` second scratch buffer + */ +static struct lc3_complex *fft(const struct lc3_complex *x, int n, + struct lc3_complex *y0, struct lc3_complex *y1) +{ + struct lc3_complex *y[2] = { y1, y0 }; + int i2, i3, is = 0; + + /* The number of points `n` can be decomposed as : + * + * n = 5^1 * 3^n3 * 2^n2 + * + * for n = 10, 20, 40, 80, 160 n3 = 0, n2 = [1..5] + * n = 30, 60, 120, 240, 480 n3 = 1, n2 = [1..5] + * n = 90, 180 n3 = 2, n2 = [1..2] + * + * Note that the expression `n & (n-1) == 0` is equivalent + * to the check that `n` is a power of 2. */ + + fft_5(x, y[is], n /= 5); + + for (i3 = 0; n & (n-1); i3++, is ^= 1) + fft_bf3(lc3_fft_twiddles_bf3[i3], y[is], y[is ^ 1], n /= 3); + + for (i2 = 0; n > 1; i2++, is ^= 1) + fft_bf2(lc3_fft_twiddles_bf2[i2][i3], y[is], y[is ^ 1], n >>= 1); + + return y[is]; +} + + +/* ---------------------------------------------------------------------------- + * MDCT processing + * -------------------------------------------------------------------------- */ + +/** + * Windowing of samples before MDCT + * dt, sr Duration and samplerate + * x, y Input current and delayed samples + * y, d Output windowed samples, and delayed ones + */ +LC3_HOT static void mdct_window( + enum lc3_dt dt, enum lc3_srate sr, + const float *x, float *d, float *y) +{ + const float *win = lc3_mdct_win[dt][sr]; + int ns = lc3_ns(dt, sr), nd = lc3_nd(dt, sr); + + const float *w0 = win, *w1 = w0 + ns; + const float *w2 = w1, *w3 = w2 + nd; + + const float *x0 = x + ns-nd, *x1 = x0; + float *y0 = y + ns/2, *y1 = y0; + float *d0 = d, *d1 = d + nd; + + while (x1 > x) { + *(--y0) = *d0 * *(w0++) - *(--x1) * *(--w1); + *(y1++) = (*(d0++) = *(x0++)) * *(w2++); + + *(--y0) = *d0 * *(w0++) - *(--x1) * *(--w1); + *(y1++) = (*(d0++) = *(x0++)) * *(w2++); + } + + for (x1 += ns; x0 < x1; ) { + *(--y0) = *d0 * *(w0++) - *(--d1) * *(--w1); + *(y1++) = (*(d0++) = *(x0++)) * *(w2++) + (*d1 = *(--x1)) * *(--w3); + + *(--y0) = *d0 * *(w0++) - *(--d1) * *(--w1); + *(y1++) = (*(d0++) = *(x0++)) * *(w2++) + (*d1 = *(--x1)) * *(--w3); + } +} + +/** + * Pre-rotate MDCT coefficients of N/2 points, before FFT N/4 points FFT + * def Size and twiddles factors + * x, y Input and output coefficients + * + * `x` and y` can be the same buffer + */ +LC3_HOT static void mdct_pre_fft(const struct lc3_mdct_rot_def *def, + const float *x, struct lc3_complex *y) +{ + int n4 = def->n4; + + const float *x0 = x, *x1 = x0 + 2*n4; + const struct lc3_complex *w0 = def->w, *w1 = w0 + n4; + struct lc3_complex *y0 = y, *y1 = y0 + n4; + + while (x0 < x1) { + struct lc3_complex u, uw = *(w0++); + u.re = - *(--x1) * uw.re + *x0 * uw.im; + u.im = *(x0++) * uw.re + *x1 * uw.im; + + struct lc3_complex v, vw = *(--w1); + v.re = - *(--x1) * vw.im + *x0 * vw.re; + v.im = - *(x0++) * vw.im - *x1 * vw.re; + + *(y0++) = u; + *(--y1) = v; + } +} + +/** + * Post-rotate FFT N/4 points coefficients, resulting MDCT N points + * def Size and twiddles factors + * x, y Input and output coefficients + * + * `x` and y` can be the same buffer + */ +LC3_HOT static void mdct_post_fft(const struct lc3_mdct_rot_def *def, + const struct lc3_complex *x, float *y) +{ + int n4 = def->n4, n8 = n4 >> 1; + + const struct lc3_complex *w0 = def->w + n8, *w1 = w0 - 1; + const struct lc3_complex *x0 = x + n8, *x1 = x0 - 1; + + float *y0 = y + n4, *y1 = y0; + + for ( ; y1 > y; x0++, x1--, w0++, w1--) { + + float u0 = x0->im * w0->im + x0->re * w0->re; + float u1 = x1->re * w1->im - x1->im * w1->re; + + float v0 = x0->re * w0->im - x0->im * w0->re; + float v1 = x1->im * w1->im + x1->re * w1->re; + + *(y0++) = u0; *(y0++) = u1; + *(--y1) = v0; *(--y1) = v1; + } +} + +/** + * Pre-rotate IMDCT coefficients of N points, before FFT N/4 points FFT + * def Size and twiddles factors + * x, y Input and output coefficients + * + * `x` and `y` can be the same buffer + * The real and imaginary parts of `y` are swapped, + * to operate on FFT instead of IFFT + */ +LC3_HOT static void imdct_pre_fft(const struct lc3_mdct_rot_def *def, + const float *x, struct lc3_complex *y) +{ + int n4 = def->n4; + + const float *x0 = x, *x1 = x0 + 2*n4; + + const struct lc3_complex *w0 = def->w, *w1 = w0 + n4; + struct lc3_complex *y0 = y, *y1 = y0 + n4; + + while (x0 < x1) { + float u0 = *(x0++), u1 = *(--x1); + float v0 = *(x0++), v1 = *(--x1); + struct lc3_complex uw = *(w0++), vw = *(--w1); + + (y0 )->re = - u0 * uw.re - u1 * uw.im; + (y0++)->im = - u1 * uw.re + u0 * uw.im; + + (--y1)->re = - v1 * vw.re - v0 * vw.im; + ( y1)->im = - v0 * vw.re + v1 * vw.im; + } +} + +/** + * Post-rotate FFT N/4 points coefficients, resulting IMDCT N points + * def Size and twiddles factors + * x, y Input and output coefficients + * + * `x` and y` can be the same buffer + * The real and imaginary parts of `x` are swapped, + * to operate on FFT instead of IFFT + */ +LC3_HOT static void imdct_post_fft(const struct lc3_mdct_rot_def *def, + const struct lc3_complex *x, float *y) +{ + int n4 = def->n4; + + const struct lc3_complex *w0 = def->w, *w1 = w0 + n4; + const struct lc3_complex *x0 = x, *x1 = x0 + n4; + + float *y0 = y, *y1 = y0 + 2*n4; + + while (x0 < x1) { + struct lc3_complex uz = *(x0++), vz = *(--x1); + struct lc3_complex uw = *(w0++), vw = *(--w1); + + *(y0++) = uz.re * uw.im - uz.im * uw.re; + *(--y1) = uz.re * uw.re + uz.im * uw.im; + + *(--y1) = vz.re * vw.im - vz.im * vw.re; + *(y0++) = vz.re * vw.re + vz.im * vw.im; + } +} + +/** + * Apply windowing of samples + * dt, sr Duration and samplerate + * x, d Middle half of IMDCT coefficients and delayed samples + * y, d Output samples and delayed ones + */ +LC3_HOT static void imdct_window( + enum lc3_dt dt, enum lc3_srate sr, + const float *x, float *d, float *y) +{ + /* The full MDCT coefficients is given by symmetry : + * T[ 0 .. n/4-1] = -half[n/4-1 .. 0 ] + * T[ n/4 .. n/2-1] = half[0 .. n/4-1] + * T[ n/2 .. 3n/4-1] = half[n/4 .. n/2-1] + * T[3n/4 .. n-1] = half[n/2-1 .. n/4 ] */ + + const float *win = lc3_mdct_win[dt][sr]; + int n4 = lc3_ns(dt, sr) >> 1, nd = lc3_nd(dt, sr); + const float *w2 = win, *w0 = w2 + 3*n4, *w1 = w0; + + const float *x0 = d + nd-n4, *x1 = x0; + float *y0 = y + nd-n4, *y1 = y0, *y2 = d + nd, *y3 = d; + + while (y0 > y) { + *(--y0) = *(--x0) - *(x ) * *(w1++); + *(y1++) = *(x1++) + *(x++) * *(--w0); + + *(--y0) = *(--x0) - *(x ) * *(w1++); + *(y1++) = *(x1++) + *(x++) * *(--w0); + } + + while (y1 < y + nd) { + *(y1++) = *(x1++) + *(x++) * *(--w0); + *(y1++) = *(x1++) + *(x++) * *(--w0); + } + + while (y1 < y + 2*n4) { + *(y1++) = *(x ) * *(--w0); + *(--y2) = *(x++) * *(w2++); + + *(y1++) = *(x ) * *(--w0); + *(--y2) = *(x++) * *(w2++); + } + + while (y2 > y3) { + *(y3++) = *(x ) * *(--w0); + *(--y2) = *(x++) * *(w2++); + + *(y3++) = *(x ) * *(--w0); + *(--y2) = *(x++) * *(w2++); + } +} + +/** + * Rescale samples + * x, n Input and count of samples, scaled as output + * scale Scale factor + */ +LC3_HOT static void rescale(float *x, int n, float f) +{ + for (int i = 0; i < (n >> 2); i++) { + *(x++) *= f; *(x++) *= f; + *(x++) *= f; *(x++) *= f; + } +} + +/** + * Forward MDCT transformation + */ +void lc3_mdct_forward( + enum lc3_dt dt, enum lc3_srate sr, enum lc3_srate sr_dst, + const float *x, float *d, float *y) +{ + const struct lc3_mdct_rot_def *rot = lc3_mdct_rot[dt][sr]; + int ns_dst = lc3_ns(dt, sr_dst); + int ns = lc3_ns(dt, sr); + + struct lc3_complex buffer[LC3_MAX_NS / 2]; + struct lc3_complex *z = (struct lc3_complex *)y; + union { float *f; struct lc3_complex *z; } u = { .z = buffer }; + + mdct_window(dt, sr, x, d, u.f); + + mdct_pre_fft(rot, u.f, u.z); + u.z = fft(u.z, ns/2, u.z, z); + mdct_post_fft(rot, u.z, y); + + if (ns != ns_dst) + rescale(y, ns_dst, sqrtf((float)ns_dst / ns)); +} + +/** + * Inverse MDCT transformation + */ +void lc3_mdct_inverse( + enum lc3_dt dt, enum lc3_srate sr, enum lc3_srate sr_src, + const float *x, float *d, float *y) +{ + const struct lc3_mdct_rot_def *rot = lc3_mdct_rot[dt][sr]; + int ns_src = lc3_ns(dt, sr_src); + int ns = lc3_ns(dt, sr); + + struct lc3_complex buffer[LC3_MAX_NS / 2]; + struct lc3_complex *z = (struct lc3_complex *)y; + union { float *f; struct lc3_complex *z; } u = { .z = buffer }; + + imdct_pre_fft(rot, x, z); + z = fft(z, ns/2, z, u.z); + imdct_post_fft(rot, z, u.f); + + if (ns != ns_src) + rescale(u.f, ns, sqrtf((float)ns / ns_src)); + + imdct_window(dt, sr, u.f, d, y); +} diff --git a/App_Resources/iOS/src/lc3/mdct.h b/App_Resources/iOS/src/lc3/mdct.h new file mode 100644 index 00000000..18d08b99 --- /dev/null +++ b/App_Resources/iOS/src/lc3/mdct.h @@ -0,0 +1,52 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_MDCT_H +#define __LC3_MDCT_H + +#include "common.h" + + +/** + * Forward MDCT transformation + * dt, sr Duration and samplerate (size of the transform) + * sr_dst Samplerate destination, scale transforam accordingly + * x, d Temporal samples and delayed buffer + * y, d Output `ns` coefficients and `nd` delayed samples + * + * `x` and `y` can be the same buffer + */ +void lc3_mdct_forward( + enum lc3_dt dt, enum lc3_srate sr, enum lc3_srate sr_dst, + const float *x, float *d, float *y); + +/** + * Inverse MDCT transformation + * dt, sr Duration and samplerate (size of the transform) + * sr_src Samplerate source, scale transforam accordingly + * x, d Frequency coefficients and delayed buffer + * y, d Output `ns` samples and `nd` delayed ones + * + * `x` and `y` can be the same buffer + */ +void lc3_mdct_inverse( + enum lc3_dt dt, enum lc3_srate sr, enum lc3_srate sr_src, + const float *x, float *d, float *y); + + +#endif /* __LC3_MDCT_H */ diff --git a/App_Resources/iOS/src/lc3/mdct_neon.h b/App_Resources/iOS/src/lc3/mdct_neon.h new file mode 100644 index 00000000..a970d4ad --- /dev/null +++ b/App_Resources/iOS/src/lc3/mdct_neon.h @@ -0,0 +1,296 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#if __ARM_NEON && __ARM_ARCH_ISA_A64 && \ + !defined(TEST_ARM) || defined(TEST_NEON) + +#ifndef TEST_NEON +#include +#endif /* TEST_NEON */ + + +/** + * FFT 5 Points + * The number of interleaved transform `n` assumed to be even + */ +#ifndef fft_5 + +LC3_HOT static inline void neon_fft_5( + const struct lc3_complex *x, struct lc3_complex *y, int n) +{ + static const union { float f[2]; uint64_t u64; } + __cos1 = { { 0.3090169944, 0.3090169944 } }, + __cos2 = { { -0.8090169944, -0.8090169944 } }, + __sin1 = { { 0.9510565163, -0.9510565163 } }, + __sin2 = { { 0.5877852523, -0.5877852523 } }; + + float32x2_t sin1 = vcreate_f32(__sin1.u64); + float32x2_t sin2 = vcreate_f32(__sin2.u64); + float32x2_t cos1 = vcreate_f32(__cos1.u64); + float32x2_t cos2 = vcreate_f32(__cos2.u64); + + float32x4_t sin1q = vcombine_f32(sin1, sin1); + float32x4_t sin2q = vcombine_f32(sin2, sin2); + float32x4_t cos1q = vcombine_f32(cos1, cos1); + float32x4_t cos2q = vcombine_f32(cos2, cos2); + + for (int i = 0; i < n; i += 2, x += 2, y += 10) { + + float32x4_t y0, y1, y2, y3, y4; + + float32x4_t x0 = vld1q_f32( (float *)(x + 0*n) ); + float32x4_t x1 = vld1q_f32( (float *)(x + 1*n) ); + float32x4_t x2 = vld1q_f32( (float *)(x + 2*n) ); + float32x4_t x3 = vld1q_f32( (float *)(x + 3*n) ); + float32x4_t x4 = vld1q_f32( (float *)(x + 4*n) ); + + float32x4_t s14 = vaddq_f32(x1, x4); + float32x4_t s23 = vaddq_f32(x2, x3); + + float32x4_t d14 = vrev64q_f32( vsubq_f32(x1, x4) ); + float32x4_t d23 = vrev64q_f32( vsubq_f32(x2, x3) ); + + y0 = vaddq_f32( x0, vaddq_f32(s14, s23) ); + + y4 = vfmaq_f32( x0, s14, cos1q ); + y4 = vfmaq_f32( y4, s23, cos2q ); + + y1 = vfmaq_f32( y4, d14, sin1q ); + y1 = vfmaq_f32( y1, d23, sin2q ); + + y4 = vfmsq_f32( y4, d14, sin1q ); + y4 = vfmsq_f32( y4, d23, sin2q ); + + y3 = vfmaq_f32( x0, s14, cos2q ); + y3 = vfmaq_f32( y3, s23, cos1q ); + + y2 = vfmaq_f32( y3, d14, sin2q ); + y2 = vfmsq_f32( y2, d23, sin1q ); + + y3 = vfmsq_f32( y3, d14, sin2q ); + y3 = vfmaq_f32( y3, d23, sin1q ); + + vst1_f32( (float *)(y + 0), vget_low_f32(y0) ); + vst1_f32( (float *)(y + 1), vget_low_f32(y1) ); + vst1_f32( (float *)(y + 2), vget_low_f32(y2) ); + vst1_f32( (float *)(y + 3), vget_low_f32(y3) ); + vst1_f32( (float *)(y + 4), vget_low_f32(y4) ); + + vst1_f32( (float *)(y + 5), vget_high_f32(y0) ); + vst1_f32( (float *)(y + 6), vget_high_f32(y1) ); + vst1_f32( (float *)(y + 7), vget_high_f32(y2) ); + vst1_f32( (float *)(y + 8), vget_high_f32(y3) ); + vst1_f32( (float *)(y + 9), vget_high_f32(y4) ); + } +} + +#ifndef TEST_NEON +#define fft_5 neon_fft_5 +#endif + +#endif /* fft_5 */ + +/** + * FFT Butterfly 3 Points + */ +#ifndef fft_bf3 + +LC3_HOT static inline void neon_fft_bf3( + const struct lc3_fft_bf3_twiddles *twiddles, + const struct lc3_complex *x, struct lc3_complex *y, int n) +{ + int n3 = twiddles->n3; + const struct lc3_complex (*w0_ptr)[2] = twiddles->t; + const struct lc3_complex (*w1_ptr)[2] = w0_ptr + n3; + const struct lc3_complex (*w2_ptr)[2] = w1_ptr + n3; + + const struct lc3_complex *x0_ptr = x; + const struct lc3_complex *x1_ptr = x0_ptr + n*n3; + const struct lc3_complex *x2_ptr = x1_ptr + n*n3; + + struct lc3_complex *y0_ptr = y; + struct lc3_complex *y1_ptr = y0_ptr + n3; + struct lc3_complex *y2_ptr = y1_ptr + n3; + + for (int j, i = 0; i < n; i++, + y0_ptr += 3*n3, y1_ptr += 3*n3, y2_ptr += 3*n3) { + + /* --- Process by pair --- */ + + for (j = 0; j < (n3 >> 1); j++, + x0_ptr += 2, x1_ptr += 2, x2_ptr += 2) { + + float32x4_t x0 = vld1q_f32( (float *)x0_ptr ); + float32x4_t x1 = vld1q_f32( (float *)x1_ptr ); + float32x4_t x2 = vld1q_f32( (float *)x2_ptr ); + + float32x4_t x1r = vtrn1q_f32( vrev64q_f32(vnegq_f32(x1)), x1 ); + float32x4_t x2r = vtrn1q_f32( vrev64q_f32(vnegq_f32(x2)), x2 ); + + float32x4x2_t wn; + float32x4_t yn; + + wn = vld2q_f32( (float *)(w0_ptr + 2*j) ); + + yn = vfmaq_f32( x0, x1 , vtrn1q_f32(wn.val[0], wn.val[0]) ); + yn = vfmaq_f32( yn, x1r, vtrn1q_f32(wn.val[1], wn.val[1]) ); + yn = vfmaq_f32( yn, x2 , vtrn2q_f32(wn.val[0], wn.val[0]) ); + yn = vfmaq_f32( yn, x2r, vtrn2q_f32(wn.val[1], wn.val[1]) ); + vst1q_f32( (float *)(y0_ptr + 2*j), yn ); + + wn = vld2q_f32( (float *)(w1_ptr + 2*j) ); + + yn = vfmaq_f32( x0, x1 , vtrn1q_f32(wn.val[0], wn.val[0]) ); + yn = vfmaq_f32( yn, x1r, vtrn1q_f32(wn.val[1], wn.val[1]) ); + yn = vfmaq_f32( yn, x2 , vtrn2q_f32(wn.val[0], wn.val[0]) ); + yn = vfmaq_f32( yn, x2r, vtrn2q_f32(wn.val[1], wn.val[1]) ); + vst1q_f32( (float *)(y1_ptr + 2*j), yn ); + + wn = vld2q_f32( (float *)(w2_ptr + 2*j) ); + + yn = vfmaq_f32( x0, x1 , vtrn1q_f32(wn.val[0], wn.val[0]) ); + yn = vfmaq_f32( yn, x1r, vtrn1q_f32(wn.val[1], wn.val[1]) ); + yn = vfmaq_f32( yn, x2 , vtrn2q_f32(wn.val[0], wn.val[0]) ); + yn = vfmaq_f32( yn, x2r, vtrn2q_f32(wn.val[1], wn.val[1]) ); + vst1q_f32( (float *)(y2_ptr + 2*j), yn ); + + } + + /* --- Last iteration --- */ + + if (n3 & 1) { + + float32x2x2_t wn; + float32x2_t yn; + + float32x2_t x0 = vld1_f32( (float *)(x0_ptr++) ); + float32x2_t x1 = vld1_f32( (float *)(x1_ptr++) ); + float32x2_t x2 = vld1_f32( (float *)(x2_ptr++) ); + + float32x2_t x1r = vtrn1_f32( vrev64_f32(vneg_f32(x1)), x1 ); + float32x2_t x2r = vtrn1_f32( vrev64_f32(vneg_f32(x2)), x2 ); + + wn = vld2_f32( (float *)(w0_ptr + 2*j) ); + + yn = vfma_f32( x0, x1 , vtrn1_f32(wn.val[0], wn.val[0]) ); + yn = vfma_f32( yn, x1r, vtrn1_f32(wn.val[1], wn.val[1]) ); + yn = vfma_f32( yn, x2 , vtrn2_f32(wn.val[0], wn.val[0]) ); + yn = vfma_f32( yn, x2r, vtrn2_f32(wn.val[1], wn.val[1]) ); + vst1_f32( (float *)(y0_ptr + 2*j), yn ); + + wn = vld2_f32( (float *)(w1_ptr + 2*j) ); + + yn = vfma_f32( x0, x1 , vtrn1_f32(wn.val[0], wn.val[0]) ); + yn = vfma_f32( yn, x1r, vtrn1_f32(wn.val[1], wn.val[1]) ); + yn = vfma_f32( yn, x2 , vtrn2_f32(wn.val[0], wn.val[0]) ); + yn = vfma_f32( yn, x2r, vtrn2_f32(wn.val[1], wn.val[1]) ); + vst1_f32( (float *)(y1_ptr + 2*j), yn ); + + wn = vld2_f32( (float *)(w2_ptr + 2*j) ); + + yn = vfma_f32( x0, x1 , vtrn1_f32(wn.val[0], wn.val[0]) ); + yn = vfma_f32( yn, x1r, vtrn1_f32(wn.val[1], wn.val[1]) ); + yn = vfma_f32( yn, x2 , vtrn2_f32(wn.val[0], wn.val[0]) ); + yn = vfma_f32( yn, x2r, vtrn2_f32(wn.val[1], wn.val[1]) ); + vst1_f32( (float *)(y2_ptr + 2*j), yn ); + } + + } +} + +#ifndef TEST_NEON +#define fft_bf3 neon_fft_bf3 +#endif + +#endif /* fft_bf3 */ + +/** + * FFT Butterfly 2 Points + */ +#ifndef fft_bf2 + +LC3_HOT static inline void neon_fft_bf2( + const struct lc3_fft_bf2_twiddles *twiddles, + const struct lc3_complex *x, struct lc3_complex *y, int n) +{ + int n2 = twiddles->n2; + const struct lc3_complex *w_ptr = twiddles->t; + + const struct lc3_complex *x0_ptr = x; + const struct lc3_complex *x1_ptr = x0_ptr + n*n2; + + struct lc3_complex *y0_ptr = y; + struct lc3_complex *y1_ptr = y0_ptr + n2; + + for (int j, i = 0; i < n; i++, y0_ptr += 2*n2, y1_ptr += 2*n2) { + + /* --- Process by pair --- */ + + for (j = 0; j < (n2 >> 1); j++, x0_ptr += 2, x1_ptr += 2) { + + float32x4_t x0 = vld1q_f32( (float *)x0_ptr ); + float32x4_t x1 = vld1q_f32( (float *)x1_ptr ); + float32x4_t y0, y1; + + float32x4_t x1r = vtrn1q_f32( vrev64q_f32(vnegq_f32(x1)), x1 ); + + float32x4_t w = vld1q_f32( (float *)(w_ptr + 2*j) ); + float32x4_t w_re = vtrn1q_f32(w, w); + float32x4_t w_im = vtrn2q_f32(w, w); + + y0 = vfmaq_f32( x0, x1 , w_re ); + y0 = vfmaq_f32( y0, x1r, w_im ); + vst1q_f32( (float *)(y0_ptr + 2*j), y0 ); + + y1 = vfmsq_f32( x0, x1 , w_re ); + y1 = vfmsq_f32( y1, x1r, w_im ); + vst1q_f32( (float *)(y1_ptr + 2*j), y1 ); + } + + /* --- Last iteration --- */ + + if (n2 & 1) { + + float32x2_t x0 = vld1_f32( (float *)(x0_ptr++) ); + float32x2_t x1 = vld1_f32( (float *)(x1_ptr++) ); + float32x2_t y0, y1; + + float32x2_t x1r = vtrn1_f32( vrev64_f32(vneg_f32(x1)), x1 ); + + float32x2_t w = vld1_f32( (float *)(w_ptr + 2*j) ); + float32x2_t w_re = vtrn1_f32(w, w); + float32x2_t w_im = vtrn2_f32(w, w); + + y0 = vfma_f32( x0, x1 , w_re ); + y0 = vfma_f32( y0, x1r, w_im ); + vst1_f32( (float *)(y0_ptr + 2*j), y0 ); + + y1 = vfms_f32( x0, x1 , w_re ); + y1 = vfms_f32( y1, x1r, w_im ); + vst1_f32( (float *)(y1_ptr + 2*j), y1 ); + } + } +} + +#ifndef TEST_NEON +#define fft_bf2 neon_fft_bf2 +#endif + +#endif /* fft_bf2 */ + +#endif /* __ARM_NEON && __ARM_ARCH_ISA_A64 */ diff --git a/App_Resources/iOS/src/lc3/plc.c b/App_Resources/iOS/src/lc3/plc.c new file mode 100644 index 00000000..f225ca66 --- /dev/null +++ b/App_Resources/iOS/src/lc3/plc.c @@ -0,0 +1,62 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "plc.h" +#include "tables.h" + + +/** + * Reset Packet Loss Concealment state + */ +void lc3_plc_reset(struct lc3_plc_state *plc) +{ + plc->seed = 24607; + lc3_plc_suspend(plc); +} + +/** + * Suspend PLC execution (Good frame received) + */ +void lc3_plc_suspend(struct lc3_plc_state *plc) +{ + plc->count = 1; + plc->alpha = 1.0f; +} + +/** + * Synthesis of a PLC frame + */ +void lc3_plc_synthesize(enum lc3_dt dt, enum lc3_srate sr, + struct lc3_plc_state *plc, const float *x, float *y) +{ + uint16_t seed = plc->seed; + float alpha = plc->alpha; + int ne = lc3_ne(dt, sr); + + alpha *= (plc->count < 4 ? 1.0f : + plc->count < 8 ? 0.9f : 0.85f); + + for (int i = 0; i < ne; i++) { + seed = (16831 + seed * 12821) & 0xffff; + y[i] = alpha * (seed & 0x8000 ? -x[i] : x[i]); + } + + plc->seed = seed; + plc->alpha = alpha; + plc->count++; +} diff --git a/App_Resources/iOS/src/lc3/plc.h b/App_Resources/iOS/src/lc3/plc.h new file mode 100644 index 00000000..a7990bd6 --- /dev/null +++ b/App_Resources/iOS/src/lc3/plc.h @@ -0,0 +1,50 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_PLC_H +#define __LC3_PLC_H + +#include "common.h" + + +/** + * Reset PLC state + * plc PLC State to reset + */ +void lc3_plc_reset(lc3_plc_state_t *plc); + +/** + * Suspend PLC synthesis (Error-free frame decoded) + * plc PLC State + */ +void lc3_plc_suspend(lc3_plc_state_t *plc); + +/** + * Synthesis of a PLC frame + * dt, sr Duration and samplerate of the frame + * plc PLC State + * x Last good spectral coefficients + * y Return emulated ones + * + * `x` and `y` can be the same buffer + */ +void lc3_plc_synthesize(enum lc3_dt dt, enum lc3_srate sr, + lc3_plc_state_t *plc, const float *x, float *y); + + +#endif /* __LC3_PLC_H */ diff --git a/App_Resources/iOS/src/lc3/sns.c b/App_Resources/iOS/src/lc3/sns.c new file mode 100644 index 00000000..85660d00 --- /dev/null +++ b/App_Resources/iOS/src/lc3/sns.c @@ -0,0 +1,929 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "sns.h" +#include "tables.h" + + +/* ---------------------------------------------------------------------------- + * DCT-16 + * -------------------------------------------------------------------------- */ + +/** + * Matrix of DCT-16 coefficients + * + * M[n][k] = 2f cos( Pi k (2n + 1) / 2N ) + * + * k = [0..N-1], n = [0..N-1], N = 16 + * f = sqrt(1/4N) for k=0, sqrt(1/2N) otherwise + */ +static const float dct16_m[16][16] = { + + { 2.50000000e-01, 3.51850934e-01, 3.46759961e-01, 3.38329500e-01, + 3.26640741e-01, 3.11806253e-01, 2.93968901e-01, 2.73300467e-01, + 2.50000000e-01, 2.24291897e-01, 1.96423740e-01, 1.66663915e-01, + 1.35299025e-01, 1.02631132e-01, 6.89748448e-02, 3.46542923e-02 }, + + { 2.50000000e-01, 3.38329500e-01, 2.93968901e-01, 2.24291897e-01, + 1.35299025e-01, 3.46542923e-02, -6.89748448e-02, -1.66663915e-01, + -2.50000000e-01, -3.11806253e-01, -3.46759961e-01, -3.51850934e-01, + -3.26640741e-01, -2.73300467e-01, -1.96423740e-01, -1.02631132e-01 }, + + { 2.50000000e-01, 3.11806253e-01, 1.96423740e-01, 3.46542923e-02, + -1.35299025e-01, -2.73300467e-01, -3.46759961e-01, -3.38329500e-01, + -2.50000000e-01, -1.02631132e-01, 6.89748448e-02, 2.24291897e-01, + 3.26640741e-01, 3.51850934e-01, 2.93968901e-01, 1.66663915e-01 }, + + { 2.50000000e-01, 2.73300467e-01, 6.89748448e-02, -1.66663915e-01, + -3.26640741e-01, -3.38329500e-01, -1.96423740e-01, 3.46542923e-02, + 2.50000000e-01, 3.51850934e-01, 2.93968901e-01, 1.02631132e-01, + -1.35299025e-01, -3.11806253e-01, -3.46759961e-01, -2.24291897e-01 }, + + { 2.50000000e-01, 2.24291897e-01, -6.89748448e-02, -3.11806253e-01, + -3.26640741e-01, -1.02631132e-01, 1.96423740e-01, 3.51850934e-01, + 2.50000000e-01, -3.46542923e-02, -2.93968901e-01, -3.38329500e-01, + -1.35299025e-01, 1.66663915e-01, 3.46759961e-01, 2.73300467e-01 }, + + { 2.50000000e-01, 1.66663915e-01, -1.96423740e-01, -3.51850934e-01, + -1.35299025e-01, 2.24291897e-01, 3.46759961e-01, 1.02631132e-01, + -2.50000000e-01, -3.38329500e-01, -6.89748448e-02, 2.73300467e-01, + 3.26640741e-01, 3.46542923e-02, -2.93968901e-01, -3.11806253e-01 }, + + { 2.50000000e-01, 1.02631132e-01, -2.93968901e-01, -2.73300467e-01, + 1.35299025e-01, 3.51850934e-01, 6.89748448e-02, -3.11806253e-01, + -2.50000000e-01, 1.66663915e-01, 3.46759961e-01, 3.46542923e-02, + -3.26640741e-01, -2.24291897e-01, 1.96423740e-01, 3.38329500e-01 }, + + { 2.50000000e-01, 3.46542923e-02, -3.46759961e-01, -1.02631132e-01, + 3.26640741e-01, 1.66663915e-01, -2.93968901e-01, -2.24291897e-01, + 2.50000000e-01, 2.73300467e-01, -1.96423740e-01, -3.11806253e-01, + 1.35299025e-01, 3.38329500e-01, -6.89748448e-02, -3.51850934e-01 }, + + { 2.50000000e-01, -3.46542923e-02, -3.46759961e-01, 1.02631132e-01, + 3.26640741e-01, -1.66663915e-01, -2.93968901e-01, 2.24291897e-01, + 2.50000000e-01, -2.73300467e-01, -1.96423740e-01, 3.11806253e-01, + 1.35299025e-01, -3.38329500e-01, -6.89748448e-02, 3.51850934e-01 }, + + { 2.50000000e-01, -1.02631132e-01, -2.93968901e-01, 2.73300467e-01, + 1.35299025e-01, -3.51850934e-01, 6.89748448e-02, 3.11806253e-01, + -2.50000000e-01, -1.66663915e-01, 3.46759961e-01, -3.46542923e-02, + -3.26640741e-01, 2.24291897e-01, 1.96423740e-01, -3.38329500e-01 }, + + { 2.50000000e-01, -1.66663915e-01, -1.96423740e-01, 3.51850934e-01, + -1.35299025e-01, -2.24291897e-01, 3.46759961e-01, -1.02631132e-01, + -2.50000000e-01, 3.38329500e-01, -6.89748448e-02, -2.73300467e-01, + 3.26640741e-01, -3.46542923e-02, -2.93968901e-01, 3.11806253e-01 }, + + { 2.50000000e-01, -2.24291897e-01, -6.89748448e-02, 3.11806253e-01, + -3.26640741e-01, 1.02631132e-01, 1.96423740e-01, -3.51850934e-01, + 2.50000000e-01, 3.46542923e-02, -2.93968901e-01, 3.38329500e-01, + -1.35299025e-01, -1.66663915e-01, 3.46759961e-01, -2.73300467e-01 }, + + { 2.50000000e-01, -2.73300467e-01, 6.89748448e-02, 1.66663915e-01, + -3.26640741e-01, 3.38329500e-01, -1.96423740e-01, -3.46542923e-02, + 2.50000000e-01, -3.51850934e-01, 2.93968901e-01, -1.02631132e-01, + -1.35299025e-01, 3.11806253e-01, -3.46759961e-01, 2.24291897e-01 }, + + { 2.50000000e-01, -3.11806253e-01, 1.96423740e-01, -3.46542923e-02, + -1.35299025e-01, 2.73300467e-01, -3.46759961e-01, 3.38329500e-01, + -2.50000000e-01, 1.02631132e-01, 6.89748448e-02, -2.24291897e-01, + 3.26640741e-01, -3.51850934e-01, 2.93968901e-01, -1.66663915e-01 }, + + { 2.50000000e-01, -3.38329500e-01, 2.93968901e-01, -2.24291897e-01, + 1.35299025e-01, -3.46542923e-02, -6.89748448e-02, 1.66663915e-01, + -2.50000000e-01, 3.11806253e-01, -3.46759961e-01, 3.51850934e-01, + -3.26640741e-01, 2.73300467e-01, -1.96423740e-01, 1.02631132e-01 }, + + { 2.50000000e-01, -3.51850934e-01, 3.46759961e-01, -3.38329500e-01, + 3.26640741e-01, -3.11806253e-01, 2.93968901e-01, -2.73300467e-01, + 2.50000000e-01, -2.24291897e-01, 1.96423740e-01, -1.66663915e-01, + 1.35299025e-01, -1.02631132e-01, 6.89748448e-02, -3.46542923e-02 }, + +}; + +/** + * Forward DCT-16 transformation + * x, y Input and output 16 values + */ +LC3_HOT static void dct16_forward(const float *x, float *y) +{ + for (int i = 0, j; i < 16; i++) + for (y[i] = 0, j = 0; j < 16; j++) + y[i] += x[j] * dct16_m[j][i]; +} + +/** + * Inverse DCT-16 transformation + * x, y Input and output 16 values + */ +LC3_HOT static void dct16_inverse(const float *x, float *y) +{ + for (int i = 0, j; i < 16; i++) + for (y[i] = 0, j = 0; j < 16; j++) + y[i] += x[j] * dct16_m[i][j]; +} + + +/* ---------------------------------------------------------------------------- + * Scale factors + * -------------------------------------------------------------------------- */ + +/** + * Scale factors + * dt, sr Duration and samplerate of the frame + * nbytes Size in bytes of the frame + * eb Energy estimation per bands + * att 1: Attack detected 0: Otherwise + * scf Output 16 scale factors + */ +LC3_HOT static void compute_scale_factors( + enum lc3_dt dt, enum lc3_srate sr, int nbytes, + const float *eb, bool att, float *scf) +{ + /* Pre-emphasis gain table : + * Ge[b] = 10 ^ (b * g_tilt) / 630 , b = [0..63] */ + + static const float ge_14[LC3_MAX_BANDS] = { /* g_tilt = 14 */ + 1.00000000e+00, 1.05250029e+00, 1.10775685e+00, 1.16591440e+00, + 1.22712524e+00, 1.29154967e+00, 1.35935639e+00, 1.43072299e+00, + 1.50583635e+00, 1.58489319e+00, 1.66810054e+00, 1.75567629e+00, + 1.84784980e+00, 1.94486244e+00, 2.04696827e+00, 2.15443469e+00, + 2.26754313e+00, 2.38658979e+00, 2.51188643e+00, 2.64376119e+00, + 2.78255940e+00, 2.92864456e+00, 3.08239924e+00, 3.24422608e+00, + 3.41454887e+00, 3.59381366e+00, 3.78248991e+00, 3.98107171e+00, + 4.19007911e+00, 4.41005945e+00, 4.64158883e+00, 4.88527357e+00, + 5.14175183e+00, 5.41169527e+00, 5.69581081e+00, 5.99484250e+00, + 6.30957344e+00, 6.64082785e+00, 6.98947321e+00, 7.35642254e+00, + 7.74263683e+00, 8.14912747e+00, 8.57695899e+00, 9.02725178e+00, + 9.50118507e+00, 1.00000000e+01, 1.05250029e+01, 1.10775685e+01, + 1.16591440e+01, 1.22712524e+01, 1.29154967e+01, 1.35935639e+01, + 1.43072299e+01, 1.50583635e+01, 1.58489319e+01, 1.66810054e+01, + 1.75567629e+01, 1.84784980e+01, 1.94486244e+01, 2.04696827e+01, + 2.15443469e+01, 2.26754313e+01, 2.38658979e+01, 2.51188643e+01 }; + + static const float ge_18[LC3_MAX_BANDS] = { /* g_tilt = 18 */ + 1.00000000e+00, 1.06800043e+00, 1.14062492e+00, 1.21818791e+00, + 1.30102522e+00, 1.38949549e+00, 1.48398179e+00, 1.58489319e+00, + 1.69266662e+00, 1.80776868e+00, 1.93069773e+00, 2.06198601e+00, + 2.20220195e+00, 2.35195264e+00, 2.51188643e+00, 2.68269580e+00, + 2.86512027e+00, 3.05994969e+00, 3.26802759e+00, 3.49025488e+00, + 3.72759372e+00, 3.98107171e+00, 4.25178630e+00, 4.54090961e+00, + 4.84969343e+00, 5.17947468e+00, 5.53168120e+00, 5.90783791e+00, + 6.30957344e+00, 6.73862717e+00, 7.19685673e+00, 7.68624610e+00, + 8.20891416e+00, 8.76712387e+00, 9.36329209e+00, 1.00000000e+01, + 1.06800043e+01, 1.14062492e+01, 1.21818791e+01, 1.30102522e+01, + 1.38949549e+01, 1.48398179e+01, 1.58489319e+01, 1.69266662e+01, + 1.80776868e+01, 1.93069773e+01, 2.06198601e+01, 2.20220195e+01, + 2.35195264e+01, 2.51188643e+01, 2.68269580e+01, 2.86512027e+01, + 3.05994969e+01, 3.26802759e+01, 3.49025488e+01, 3.72759372e+01, + 3.98107171e+01, 4.25178630e+01, 4.54090961e+01, 4.84969343e+01, + 5.17947468e+01, 5.53168120e+01, 5.90783791e+01, 6.30957344e+01 }; + + static const float ge_22[LC3_MAX_BANDS] = { /* g_tilt = 22 */ + 1.00000000e+00, 1.08372885e+00, 1.17446822e+00, 1.27280509e+00, + 1.37937560e+00, 1.49486913e+00, 1.62003281e+00, 1.75567629e+00, + 1.90267705e+00, 2.06198601e+00, 2.23463373e+00, 2.42173704e+00, + 2.62450630e+00, 2.84425319e+00, 3.08239924e+00, 3.34048498e+00, + 3.62017995e+00, 3.92329345e+00, 4.25178630e+00, 4.60778348e+00, + 4.99358789e+00, 5.41169527e+00, 5.86481029e+00, 6.35586411e+00, + 6.88803330e+00, 7.46476041e+00, 8.08977621e+00, 8.76712387e+00, + 9.50118507e+00, 1.02967084e+01, 1.11588399e+01, 1.20931568e+01, + 1.31057029e+01, 1.42030283e+01, 1.53922315e+01, 1.66810054e+01, + 1.80776868e+01, 1.95913107e+01, 2.12316686e+01, 2.30093718e+01, + 2.49359200e+01, 2.70237760e+01, 2.92864456e+01, 3.17385661e+01, + 3.43959997e+01, 3.72759372e+01, 4.03970086e+01, 4.37794036e+01, + 4.74450028e+01, 5.14175183e+01, 5.57226480e+01, 6.03882412e+01, + 6.54444792e+01, 7.09240702e+01, 7.68624610e+01, 8.32980665e+01, + 9.02725178e+01, 9.78309319e+01, 1.06022203e+02, 1.14899320e+02, + 1.24519708e+02, 1.34945600e+02, 1.46244440e+02, 1.58489319e+02 }; + + static const float ge_26[LC3_MAX_BANDS] = { /* g_tilt = 26 */ + 1.00000000e+00, 1.09968890e+00, 1.20931568e+00, 1.32987103e+00, + 1.46244440e+00, 1.60823388e+00, 1.76855694e+00, 1.94486244e+00, + 2.13874364e+00, 2.35195264e+00, 2.58641621e+00, 2.84425319e+00, + 3.12779366e+00, 3.43959997e+00, 3.78248991e+00, 4.15956216e+00, + 4.57422434e+00, 5.03022373e+00, 5.53168120e+00, 6.08312841e+00, + 6.68954879e+00, 7.35642254e+00, 8.08977621e+00, 8.89623710e+00, + 9.78309319e+00, 1.07583590e+01, 1.18308480e+01, 1.30102522e+01, + 1.43072299e+01, 1.57335019e+01, 1.73019574e+01, 1.90267705e+01, + 2.09235283e+01, 2.30093718e+01, 2.53031508e+01, 2.78255940e+01, + 3.05994969e+01, 3.36499270e+01, 3.70044512e+01, 4.06933843e+01, + 4.47500630e+01, 4.92111475e+01, 5.41169527e+01, 5.95118121e+01, + 6.54444792e+01, 7.19685673e+01, 7.91430346e+01, 8.70327166e+01, + 9.57089124e+01, 1.05250029e+02, 1.15742288e+02, 1.27280509e+02, + 1.39968963e+02, 1.53922315e+02, 1.69266662e+02, 1.86140669e+02, + 2.04696827e+02, 2.25102829e+02, 2.47543082e+02, 2.72220379e+02, + 2.99357729e+02, 3.29200372e+02, 3.62017995e+02, 3.98107171e+02 }; + + static const float ge_30[LC3_MAX_BANDS] = { /* g_tilt = 30 */ + 1.00000000e+00, 1.11588399e+00, 1.24519708e+00, 1.38949549e+00, + 1.55051578e+00, 1.73019574e+00, 1.93069773e+00, 2.15443469e+00, + 2.40409918e+00, 2.68269580e+00, 2.99357729e+00, 3.34048498e+00, + 3.72759372e+00, 4.15956216e+00, 4.64158883e+00, 5.17947468e+00, + 5.77969288e+00, 6.44946677e+00, 7.19685673e+00, 8.03085722e+00, + 8.96150502e+00, 1.00000000e+01, 1.11588399e+01, 1.24519708e+01, + 1.38949549e+01, 1.55051578e+01, 1.73019574e+01, 1.93069773e+01, + 2.15443469e+01, 2.40409918e+01, 2.68269580e+01, 2.99357729e+01, + 3.34048498e+01, 3.72759372e+01, 4.15956216e+01, 4.64158883e+01, + 5.17947468e+01, 5.77969288e+01, 6.44946677e+01, 7.19685673e+01, + 8.03085722e+01, 8.96150502e+01, 1.00000000e+02, 1.11588399e+02, + 1.24519708e+02, 1.38949549e+02, 1.55051578e+02, 1.73019574e+02, + 1.93069773e+02, 2.15443469e+02, 2.40409918e+02, 2.68269580e+02, + 2.99357729e+02, 3.34048498e+02, 3.72759372e+02, 4.15956216e+02, + 4.64158883e+02, 5.17947468e+02, 5.77969288e+02, 6.44946677e+02, + 7.19685673e+02, 8.03085722e+02, 8.96150502e+02, 1.00000000e+03 }; + +#if LC3_PLUS_HR + + static const float ge_34[LC3_MAX_BANDS] = { /* g_tilt = 34 */ + 1.00000000e+00, 1.13231759e+00, 1.28214312e+00, 1.45179321e+00, + 1.64389099e+00, 1.86140669e+00, 2.10770353e+00, 2.38658979e+00, + 2.70237760e+00, 3.05994969e+00, 3.46483486e+00, 3.92329345e+00, + 4.44241419e+00, 5.03022373e+00, 5.69581081e+00, 6.44946677e+00, + 7.30284467e+00, 8.26913948e+00, 9.36329209e+00, 1.06022203e+01, + 1.20050806e+01, 1.35935639e+01, 1.53922315e+01, 1.74288945e+01, + 1.97350438e+01, 2.23463373e+01, 2.53031508e+01, 2.86512027e+01, + 3.24422608e+01, 3.67349426e+01, 4.15956216e+01, 4.70994540e+01, + 5.33315403e+01, 6.03882412e+01, 6.83786677e+01, 7.74263683e+01, + 8.76712387e+01, 9.92716858e+01, 1.12407076e+02, 1.27280509e+02, + 1.44121960e+02, 1.63191830e+02, 1.84784980e+02, 2.09235283e+02, + 2.36920791e+02, 2.68269580e+02, 3.03766364e+02, 3.43959997e+02, + 3.89471955e+02, 4.41005945e+02, 4.99358789e+02, 5.65432741e+02, + 6.40249439e+02, 7.24965701e+02, 8.20891416e+02, 9.29509790e+02, + 1.05250029e+03, 1.19176459e+03, 1.34945600e+03, 1.52801277e+03, + 1.73019574e+03, 1.95913107e+03, 2.21835857e+03, 2.51188643e+03 }; + +#endif /* LC3_PLUS_HR */ + + static const float *ge_table[LC3_NUM_SRATE] = { + [LC3_SRATE_8K ] = ge_14, [LC3_SRATE_16K ] = ge_18, + [LC3_SRATE_24K ] = ge_22, [LC3_SRATE_32K ] = ge_26, + [LC3_SRATE_48K ] = ge_30, + +#if LC3_PLUS_HR + [LC3_SRATE_48K_HR] = ge_30, [LC3_SRATE_96K_HR] = ge_34, +#endif /* LC3_PLUS_HR */ + + }; + + float e[LC3_MAX_BANDS]; + + /* --- Copy and padding --- */ + + int nb = lc3_num_bands[dt][sr]; + int n4 = nb < 32 ? 32 % nb : 0; + int n2 = nb < 32 ? nb - n4 : LC3_MAX_BANDS - nb; + + for (int i4 = 0; i4 < n4; i4++) + e[4*i4 + 0] = e[4*i4 + 1] = + e[4*i4 + 2] = e[4*i4 + 3] = eb[i4]; + + for (int i2 = n4; i2 < n4+n2; i2++) + e[2*(n4+i2) + 0] = e[2*(n4+i2) + 1] = eb[i2]; + + memcpy(e + 4*n4 + 2*n2, eb + n4 + n2, (nb - n4 - n2) * sizeof(float)); + + /* --- Smoothing, pre-emphasis and logarithm --- */ + + const float *ge = ge_table[sr]; + + float e0 = e[0], e1 = e[0], e2; + float e_sum = 0; + + for (int i = 0; i < LC3_MAX_BANDS-1; ) { + e[i] = (e0 * 0.25f + e1 * 0.5f + (e2 = e[i+1]) * 0.25f) * ge[i]; + e_sum += e[i++]; + + e[i] = (e1 * 0.25f + e2 * 0.5f + (e0 = e[i+1]) * 0.25f) * ge[i]; + e_sum += e[i++]; + + e[i] = (e2 * 0.25f + e0 * 0.5f + (e1 = e[i+1]) * 0.25f) * ge[i]; + e_sum += e[i++]; + } + + e[LC3_MAX_BANDS-1] = (e0 * 0.25f + e1 * 0.75f) * ge[LC3_MAX_BANDS-1]; + e_sum += e[LC3_MAX_BANDS-1]; + + float noise_floor = fmaxf(e_sum * (1e-4f / 64), 0x1p-32f); + + for (int i = 0; i < LC3_MAX_BANDS; i++) + e[i] = lc3_log2f(fmaxf(e[i], noise_floor)) * 0.5f; + + /* --- Grouping & scaling --- */ + + float scf_sum; + + scf[0] = (e[0] + e[4]) * 1.f/12 + + (e[0] + e[3]) * 2.f/12 + + (e[1] + e[2]) * 3.f/12 ; + scf_sum = scf[0]; + + for (int i = 1; i < 15; i++) { + scf[i] = (e[4*i-1] + e[4*i+4]) * 1.f/12 + + (e[4*i ] + e[4*i+3]) * 2.f/12 + + (e[4*i+1] + e[4*i+2]) * 3.f/12 ; + scf_sum += scf[i]; + } + + scf[15] = (e[59] + e[63]) * 1.f/12 + + (e[60] + e[63]) * 2.f/12 + + (e[61] + e[62]) * 3.f/12 ; + scf_sum += scf[15]; + + float cf = lc3_hr(sr) ? 0.6f : 0.85f; + if (lc3_hr(sr) && 8 * nbytes > + (dt < LC3_DT_10M ? 1150 * (int)(1 + dt) : 4400)) + cf *= dt < LC3_DT_10M ? 0.25f : 0.35f; + + for (int i = 0; i < 16; i++) + scf[i] = cf * (scf[i] - scf_sum * 1.f/16); + + /* --- Attack handling --- */ + + if (!att) + return; + + float s0, s1 = scf[0], s2 = scf[1], s3 = scf[2], s4 = scf[3]; + float sn = s1 + s2; + + scf[0] = (sn += s3) * 1.f/3; + scf[1] = (sn += s4) * 1.f/4; + scf_sum = scf[0] + scf[1]; + + for (int i = 2; i < 14; i++, sn -= s0) { + s0 = s1, s1 = s2, s2 = s3, s3 = s4, s4 = scf[i+2]; + scf[i] = (sn += s4) * 1.f/5; + scf_sum += scf[i]; + } + + scf[14] = (sn ) * 1.f/4; + scf[15] = (sn -= s1) * 1.f/3; + scf_sum += scf[14] + scf[15]; + + for (int i = 0; i < 16; i++) + scf[i] = (dt == LC3_DT_7M5 ? 0.3f : 0.5f) * + (scf[i] - scf_sum * 1.f/16); +} + +/** + * Codebooks + * scf Input 16 scale factors + * lf/hfcb_idx Output the low and high frequency codebooks index + */ +LC3_HOT static void resolve_codebooks( + const float *scf, int *lfcb_idx, int *hfcb_idx) +{ + float dlfcb_max = 0, dhfcb_max = 0; + *lfcb_idx = *hfcb_idx = 0; + + for (int icb = 0; icb < 32; icb++) { + const float *lfcb = lc3_sns_lfcb[icb]; + const float *hfcb = lc3_sns_hfcb[icb]; + float dlfcb = 0, dhfcb = 0; + + for (int i = 0; i < 8; i++) { + dlfcb += (scf[ i] - lfcb[i]) * (scf[ i] - lfcb[i]); + dhfcb += (scf[8+i] - hfcb[i]) * (scf[8+i] - hfcb[i]); + } + + if (icb == 0 || dlfcb < dlfcb_max) + *lfcb_idx = icb, dlfcb_max = dlfcb; + + if (icb == 0 || dhfcb < dhfcb_max) + *hfcb_idx = icb, dhfcb_max = dhfcb; + } +} + +/** + * Unit energy normalize pulse configuration + * c Pulse configuration + * cn Normalized pulse configuration + */ +LC3_HOT static void normalize(const int *c, float *cn) +{ + int c2_sum = 0; + for (int i = 0; i < 16; i++) + c2_sum += c[i] * c[i]; + + float c_norm = 1.f / sqrtf(c2_sum); + + for (int i = 0; i < 16; i++) + cn[i] = c[i] * c_norm; +} + +/** + * Sub-procedure of `quantize()`, add unit pulse + * x, y, n Transformed residual, and vector of pulses with length + * start, end Current number of pulses, limit to reach + * corr, energy Correlation (x,y) and y energy, updated at output + */ +LC3_HOT static void add_pulse(const float *x, int *y, int n, + int start, int end, float *corr, float *energy) +{ + for (int k = start; k < end; k++) { + float best_c2 = (*corr + x[0]) * (*corr + x[0]); + float best_e = *energy + 2*y[0] + 1; + int nbest = 0; + + for (int i = 1; i < n; i++) { + float c2 = (*corr + x[i]) * (*corr + x[i]); + float e = *energy + 2*y[i] + 1; + + if (c2 * best_e > e * best_c2) + best_c2 = c2, best_e = e, nbest = i; + } + + *corr += x[nbest]; + *energy += 2*y[nbest] + 1; + y[nbest]++; + } +} + +/** + * Quantization of codebooks residual + * scf Input 16 scale factors, output quantized version + * lf/hfcb_idx Codebooks index + * c, cn Output 4 pulse configurations candidates, normalized + * shape/gain_idx Output selected shape/gain indexes + */ +LC3_HOT static void quantize(const float *scf, int lfcb_idx, int hfcb_idx, + int (*c)[16], float (*cn)[16], int *shape_idx, int *gain_idx) +{ + /* --- Residual --- */ + + const float *lfcb = lc3_sns_lfcb[lfcb_idx]; + const float *hfcb = lc3_sns_hfcb[hfcb_idx]; + float r[16], x[16]; + + for (int i = 0; i < 8; i++) { + r[ i] = scf[ i] - lfcb[i]; + r[8+i] = scf[8+i] - hfcb[i]; + } + + dct16_forward(r, x); + + /* --- Shape 3 candidate --- + * Project to or below pyramid N = 16, K = 6, + * then add unit pulses until you reach K = 6, over N = 16 */ + + float xm[16]; + float xm_sum = 0; + + for (int i = 0; i < 16; i++) { + xm[i] = fabsf(x[i]); + xm_sum += xm[i]; + } + + float proj_factor = (6 - 1) / fmaxf(xm_sum, 1e-31f); + float corr = 0, energy = 0; + int npulses = 0; + + for (int i = 0; i < 16; i++) { + c[3][i] = floorf(xm[i] * proj_factor); + npulses += c[3][i]; + corr += c[3][i] * xm[i]; + energy += c[3][i] * c[3][i]; + } + + add_pulse(xm, c[3], 16, npulses, 6, &corr, &energy); + npulses = 6; + + /* --- Shape 2 candidate --- + * Add unit pulses until you reach K = 8 on shape 3 */ + + memcpy(c[2], c[3], sizeof(c[2])); + + add_pulse(xm, c[2], 16, npulses, 8, &corr, &energy); + npulses = 8; + + /* --- Shape 1 candidate --- + * Remove any unit pulses from shape 2 that are not part of 0 to 9 + * Update energy and correlation terms accordingly + * Add unit pulses until you reach K = 10, over N = 10 */ + + memcpy(c[1], c[2], sizeof(c[1])); + + for (int i = 10; i < 16; i++) { + c[1][i] = 0; + npulses -= c[2][i]; + corr -= c[2][i] * xm[i]; + energy -= c[2][i] * c[2][i]; + } + + add_pulse(xm, c[1], 10, npulses, 10, &corr, &energy); + npulses = 10; + + /* --- Shape 0 candidate --- + * Add unit pulses until you reach K = 1, on shape 1 */ + + memcpy(c[0], c[1], sizeof(c[0])); + + add_pulse(xm + 10, c[0] + 10, 6, 0, 1, &corr, &energy); + + /* --- Add sign and unit energy normalize --- */ + + for (int j = 0; j < 16; j++) + for (int i = 0; i < 4; i++) + c[i][j] = x[j] < 0 ? -c[i][j] : c[i][j]; + + for (int i = 0; i < 4; i++) + normalize(c[i], cn[i]); + + /* --- Determe shape & gain index --- + * Search the Mean Square Error, within (shape, gain) combinations */ + + float mse_min = FLT_MAX; + *shape_idx = *gain_idx = 0; + + for (int ic = 0; ic < 4; ic++) { + const struct lc3_sns_vq_gains *cgains = lc3_sns_vq_gains + ic; + float cmse_min = FLT_MAX; + int cgain_idx = 0; + + for (int ig = 0; ig < cgains->count; ig++) { + float g = cgains->v[ig]; + + float mse = 0; + for (int i = 0; i < 16; i++) + mse += (x[i] - g * cn[ic][i]) * (x[i] - g * cn[ic][i]); + + if (mse < cmse_min) { + cgain_idx = ig, + cmse_min = mse; + } + } + + if (cmse_min < mse_min) { + *shape_idx = ic, *gain_idx = cgain_idx; + mse_min = cmse_min; + } + } +} + +/** + * Unquantization of codebooks residual + * lf/hfcb_idx Low and high frequency codebooks index + * c Table of normalized pulse configuration + * shape/gain Selected shape/gain indexes + * scf Return unquantized scale factors + */ +LC3_HOT static void unquantize(int lfcb_idx, int hfcb_idx, + const float *c, int shape, int gain, float *scf) +{ + const float *lfcb = lc3_sns_lfcb[lfcb_idx]; + const float *hfcb = lc3_sns_hfcb[hfcb_idx]; + float g = lc3_sns_vq_gains[shape].v[gain]; + + dct16_inverse(c, scf); + + for (int i = 0; i < 8; i++) + scf[i] = lfcb[i] + g * scf[i]; + + for (int i = 8; i < 16; i++) + scf[i] = hfcb[i-8] + g * scf[i]; +} + +/** + * Sub-procedure of `sns_enumerate()`, enumeration of a vector + * c, n Table of pulse configuration, and length + * idx, ls Return enumeration set + */ +static void enum_mvpq(const int *c, int n, int *idx, bool *ls) +{ + int ci, i; + + /* --- Scan for 1st significant coeff --- */ + + for (i = 0, c += n; (ci = *(--c)) == 0 && i < 15; i++); + + *idx = 0; + *ls = ci < 0; + + /* --- Scan remaining coefficients --- */ + + unsigned j = LC3_ABS(ci); + + for (i++; i < n; i++, j += LC3_ABS(ci)) { + + if ((ci = *(--c)) != 0) { + *idx = (*idx << 1) | *ls; + *ls = ci < 0; + } + + *idx += lc3_sns_mpvq_offsets[i][LC3_MIN(j, 10)]; + } +} + +/** + * Sub-procedure of `sns_deenumerate()`, deenumeration of a vector + * idx, ls Enumeration set + * npulses Number of pulses in the set + * c, n Table of pulses configuration, and length + */ +static void deenum_mvpq(int idx, bool ls, int npulses, int *c, int n) +{ + int i; + + /* --- Scan for coefficients --- */ + + for (i = n-1; i >= 0 && idx; i--) { + + int ci = 0; + + for (ci = 0; idx < lc3_sns_mpvq_offsets[i][npulses - ci]; ci++); + idx -= lc3_sns_mpvq_offsets[i][npulses - ci]; + + *(c++) = ls ? -ci : ci; + npulses -= ci; + if (ci > 0) { + ls = idx & 1; + idx >>= 1; + } + } + + /* --- Set last significant --- */ + + int ci = npulses; + + if (i-- >= 0) + *(c++) = ls ? -ci : ci; + + while (i-- >= 0) + *(c++) = 0; +} + +/** + * SNS Enumeration of PVQ configuration + * shape Selected shape index + * c Selected pulse configuration + * idx_a, ls_a Return enumeration set A + * idx_b, ls_b Return enumeration set B (shape = 0) + */ +static void enumerate(int shape, const int *c, + int *idx_a, bool *ls_a, int *idx_b, bool *ls_b) +{ + enum_mvpq(c, shape < 2 ? 10 : 16, idx_a, ls_a); + + if (shape == 0) + enum_mvpq(c + 10, 6, idx_b, ls_b); +} + +/** + * SNS Deenumeration of PVQ configuration + * shape Selected shape index + * idx_a, ls_a enumeration set A + * idx_b, ls_b enumeration set B (shape = 0) + * c Return pulse configuration + */ +static void deenumerate(int shape, + int idx_a, bool ls_a, int idx_b, bool ls_b, int *c) +{ + int npulses_a = (const int []){ 10, 10, 8, 6 }[shape]; + + deenum_mvpq(idx_a, ls_a, npulses_a, c, shape < 2 ? 10 : 16); + + if (shape == 0) + deenum_mvpq(idx_b, ls_b, 1, c + 10, 6); + else if (shape == 1) + memset(c + 10, 0, 6 * sizeof(*c)); +} + + +/* ---------------------------------------------------------------------------- + * Filtering + * -------------------------------------------------------------------------- */ + +/** + * Spectral shaping + * dt, sr Duration and samplerate of the frame + * scf_q Quantized scale factors + * inv True on inverse shaping, False otherwise + * x Spectral coefficients + * y Return shapped coefficients + * + * `x` and `y` can be the same buffer + */ +LC3_HOT static void spectral_shaping(enum lc3_dt dt, enum lc3_srate sr, + const float *scf_q, bool inv, const float *x, float *y) +{ + /* --- Interpolate scale factors --- */ + + float scf[LC3_MAX_BANDS]; + float s0, s1 = inv ? -scf_q[0] : scf_q[0]; + + scf[0] = scf[1] = s1; + for (int i = 0; i < 15; i++) { + s0 = s1, s1 = inv ? -scf_q[i+1] : scf_q[i+1]; + scf[4*i+2] = s0 + 0.125f * (s1 - s0); + scf[4*i+3] = s0 + 0.375f * (s1 - s0); + scf[4*i+4] = s0 + 0.625f * (s1 - s0); + scf[4*i+5] = s0 + 0.875f * (s1 - s0); + } + scf[62] = s1 + 0.125f * (s1 - s0); + scf[63] = s1 + 0.375f * (s1 - s0); + + int nb = lc3_num_bands[dt][sr]; + int n4 = nb < 32 ? 32 % nb : 0; + int n2 = nb < 32 ? nb - n4 : LC3_MAX_BANDS - nb; + + for (int i4 = 0; i4 < n4; i4++) + scf[i4] = 0.25f * (scf[4*i4+0] + scf[4*i4+1] + + scf[4*i4+2] + scf[4*i4+3]); + + for (int i2 = n4; i2 < n4+n2; i2++) + scf[i2] = 0.5f * (scf[2*(n4+i2)] + scf[2*(n4+i2)+1]); + + memmove(scf + n4 + n2, scf + 4*n4 + 2*n2, (nb - n4 - n2) * sizeof(float)); + + /* --- Spectral shaping --- */ + + const int *lim = lc3_band_lim[dt][sr]; + + for (int i = 0, ib = 0; ib < nb; ib++) { + float g_sns = lc3_exp2f(-scf[ib]); + + for ( ; i < lim[ib+1]; i++) + y[i] = x[i] * g_sns; + } +} + + +/* ---------------------------------------------------------------------------- + * Interface + * -------------------------------------------------------------------------- */ + +/** + * SNS analysis + */ +void lc3_sns_analyze( + enum lc3_dt dt, enum lc3_srate sr, int nbytes, + const float *eb, bool att, struct lc3_sns_data *data, + const float *x, float *y) +{ + /* Processing steps : + * - Determine 16 scale factors from bands energy estimation + * - Get codebooks indexes that match thoses scale factors + * - Quantize the residual with the selected codebook + * - The pulse configuration `c[]` is enumerated + * - Finally shape the spectrum coefficients accordingly */ + + float scf[16], cn[4][16]; + int c[4][16]; + + compute_scale_factors(dt, sr, nbytes, eb, att, scf); + + resolve_codebooks(scf, &data->lfcb, &data->hfcb); + + quantize(scf, data->lfcb, data->hfcb, + c, cn, &data->shape, &data->gain); + + unquantize(data->lfcb, data->hfcb, + cn[data->shape], data->shape, data->gain, scf); + + enumerate(data->shape, c[data->shape], + &data->idx_a, &data->ls_a, &data->idx_b, &data->ls_b); + + spectral_shaping(dt, sr, scf, false, x, y); +} + +/** + * SNS synthesis + */ +void lc3_sns_synthesize( + enum lc3_dt dt, enum lc3_srate sr, + const lc3_sns_data_t *data, const float *x, float *y) +{ + float scf[16], cn[16]; + int c[16]; + + deenumerate(data->shape, + data->idx_a, data->ls_a, data->idx_b, data->ls_b, c); + + normalize(c, cn); + + unquantize(data->lfcb, data->hfcb, cn, data->shape, data->gain, scf); + + spectral_shaping(dt, sr, scf, true, x, y); +} + +/** + * Return number of bits coding the bitstream data + */ +int lc3_sns_get_nbits(void) +{ + return 38; +} + +/** + * Put bitstream data + */ +void lc3_sns_put_data(lc3_bits_t *bits, const struct lc3_sns_data *data) +{ + /* --- Codebooks --- */ + + lc3_put_bits(bits, data->lfcb, 5); + lc3_put_bits(bits, data->hfcb, 5); + + /* --- Shape, gain and vectors --- * + * Write MSB bit of shape index, next LSB bits of shape and gain, + * and MVPQ vectors indexes are muxed */ + + int shape_msb = data->shape >> 1; + lc3_put_bit(bits, shape_msb); + + if (shape_msb == 0) { + const int size_a = 2390004; + int submode = data->shape & 1; + + int mux_high = submode == 0 ? + 2 * (data->idx_b + 1) + data->ls_b : data->gain & 1; + int mux_code = mux_high * size_a + data->idx_a; + + lc3_put_bits(bits, data->gain >> submode, 1); + lc3_put_bits(bits, data->ls_a, 1); + lc3_put_bits(bits, mux_code, 25); + + } else { + const int size_a = 15158272; + int submode = data->shape & 1; + + int mux_code = submode == 0 ? + data->idx_a : size_a + 2 * data->idx_a + (data->gain & 1); + + lc3_put_bits(bits, data->gain >> submode, 2); + lc3_put_bits(bits, data->ls_a, 1); + lc3_put_bits(bits, mux_code, 24); + } +} + +/** + * Get bitstream data + */ +int lc3_sns_get_data(lc3_bits_t *bits, struct lc3_sns_data *data) +{ + /* --- Codebooks --- */ + + *data = (struct lc3_sns_data){ + .lfcb = lc3_get_bits(bits, 5), + .hfcb = lc3_get_bits(bits, 5) + }; + + /* --- Shape, gain and vectors --- */ + + int shape_msb = lc3_get_bit(bits); + data->gain = lc3_get_bits(bits, 1 + shape_msb); + data->ls_a = lc3_get_bit(bits); + + int mux_code = lc3_get_bits(bits, 25 - shape_msb); + + if (shape_msb == 0) { + const int size_a = 2390004; + + if (mux_code >= size_a * 14) + return -1; + + data->idx_a = mux_code % size_a; + mux_code = mux_code / size_a; + + data->shape = (mux_code < 2); + + if (data->shape == 0) { + data->idx_b = (mux_code - 2) / 2; + data->ls_b = (mux_code - 2) % 2; + } else { + data->gain = (data->gain << 1) + (mux_code % 2); + } + + } else { + const int size_a = 15158272; + + if (mux_code >= size_a + 1549824) + return -1; + + data->shape = 2 + (mux_code >= size_a); + if (data->shape == 2) { + data->idx_a = mux_code; + } else { + mux_code -= size_a; + data->idx_a = mux_code / 2; + data->gain = (data->gain << 1) + (mux_code % 2); + } + } + + return 0; +} diff --git a/App_Resources/iOS/src/lc3/sns.h b/App_Resources/iOS/src/lc3/sns.h new file mode 100644 index 00000000..ecb80efb --- /dev/null +++ b/App_Resources/iOS/src/lc3/sns.h @@ -0,0 +1,98 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_SNS_H +#define __LC3_SNS_H + +#include "common.h" +#include "bits.h" + + +/** + * Bitstream data + */ + +typedef struct lc3_sns_data { + int lfcb, hfcb; + int shape, gain; + int idx_a, idx_b; + bool ls_a, ls_b; +} lc3_sns_data_t; + + +/* ---------------------------------------------------------------------------- + * Encoding + * -------------------------------------------------------------------------- */ + +/** + * SNS analysis + * dt, sr Duration and samplerate of the frame + * nbytes Size in bytes of the frame + * eb Energy estimation per bands, and count of bands + * att 1: Attack detected 0: Otherwise + * data Return bitstream data + * x Spectral coefficients + * y Return shapped coefficients + * + * `x` and `y` can be the same buffer + */ +void lc3_sns_analyze( + enum lc3_dt dt, enum lc3_srate sr, int nbytes, + const float *eb, bool att, lc3_sns_data_t *data, + const float *x, float *y); + +/** + * Return number of bits coding the bitstream data + * return Bit consumption + */ +int lc3_sns_get_nbits(void); + +/** + * Put bitstream data + * bits Bitstream context + * data Bitstream data + */ +void lc3_sns_put_data(lc3_bits_t *bits, const lc3_sns_data_t *data); + + +/* ---------------------------------------------------------------------------- + * Decoding + * -------------------------------------------------------------------------- */ + +/** + * Get bitstream data + * bits Bitstream context + * data Return SNS data + * return 0: Ok -1: Invalid SNS data + */ +int lc3_sns_get_data(lc3_bits_t *bits, lc3_sns_data_t *data); + +/** + * SNS synthesis + * dt, sr Duration and samplerate of the frame + * data Bitstream data + * x Spectral coefficients + * y Return shapped coefficients + * + * `x` and `y` can be the same buffer + */ +void lc3_sns_synthesize(enum lc3_dt dt, enum lc3_srate sr, + const lc3_sns_data_t *data, const float *x, float *y); + + +#endif /* __LC3_SNS_H */ diff --git a/App_Resources/iOS/src/lc3/spec.c b/App_Resources/iOS/src/lc3/spec.c new file mode 100644 index 00000000..25a28d43 --- /dev/null +++ b/App_Resources/iOS/src/lc3/spec.c @@ -0,0 +1,959 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "spec.h" +#include "bits.h" +#include "tables.h" + + +/* ---------------------------------------------------------------------------- + * Global Gain / Quantization + * -------------------------------------------------------------------------- */ + +/** + * Resolve quantized gain index offset + * sr, nbytes Samplerate and size of the frame + * return Gain index offset + */ +static int resolve_gain_offset(enum lc3_srate sr, int nbytes) +{ + int sr_ind = lc3_hr(sr) ? 4 + (sr - LC3_SRATE_48K_HR) : sr; + + int g_off = (nbytes * 8) / (10 * (1 + sr_ind)); + return LC3_MIN(sr >= LC3_SRATE_96K_HR ? 181 : 255, + 105 + 5*(1 + sr_ind) + LC3_MIN(g_off, 115)); +} + +/** + * Unquantize gain + * g_int Quantization gain value + * return Unquantized gain value + */ +static float unquantize_gain(int g_int) +{ + /* Unquantization gain table : + * G[i] = 10 ^ (i / 28) , i = [0..27] */ + + static const float iq_table[] = { + 1.00000000e+00, 1.08571112e+00, 1.17876863e+00, 1.27980221e+00, + 1.38949549e+00, 1.50859071e+00, 1.63789371e+00, 1.77827941e+00, + 1.93069773e+00, 2.09617999e+00, 2.27584593e+00, 2.47091123e+00, + 2.68269580e+00, 2.91263265e+00, 3.16227766e+00, 3.43332002e+00, + 3.72759372e+00, 4.04708995e+00, 4.39397056e+00, 4.77058270e+00, + 5.17947468e+00, 5.62341325e+00, 6.10540230e+00, 6.62870316e+00, + 7.19685673e+00, 7.81370738e+00, 8.48342898e+00, 9.21055318e+00 + }; + + float g = 1.f; + + for ( ; g_int < 0; g_int += 28, g *= 0.1f); + for ( ; g_int >= 28; g_int -= 28, g *= 10.f); + + return g * iq_table[g_int]; +} + +/** + * Global Gain Estimation + * dt, sr Duration and samplerate of the frame + * x Spectral coefficients + * nbytes Size of the frame + * nbits_budget Number of bits available coding the spectrum + * nbits_off Offset on the available bits, temporarily smoothed + * g_off Gain index offset + * reset_off Return True when the nbits_off must be reset + * g_min Return lower bound of quantized gain value + * return The quantized gain value + */ +LC3_HOT static int estimate_gain( + enum lc3_dt dt, enum lc3_srate sr, const float *x, + int nbytes, int nbits_budget, float nbits_off, int g_off, + bool *reset_off, int *g_min) +{ + int n4 = lc3_ne(dt, sr) / 4; + union { float f; int32_t q16; } e[LC3_MAX_NE / 4]; + + /* --- Signal adaptative noise floor --- */ + + int reg_bits = 0; + float low_bits = 0; + + if (lc3_hr(sr)) { + int reg_c = (const int [LC3_NUM_DT][LC3_NUM_SRATE - LC3_SRATE_48K_HR]){ + [LC3_DT_2M5] = { -6, -6 }, + [LC3_DT_5M ] = { 0, 0 }, + [LC3_DT_10M] = { 2, 5 } + }[dt][sr - LC3_SRATE_48K_HR]; + + reg_bits = (8*nbytes * 4) / (125 * (1 + dt)); + reg_bits = LC3_CLIP(reg_bits + reg_c, 6, 23); + + float m0 = 1e-5f, m1 = 1e-5f, k = 0; + + for (int i = 0; i < n4; i++) { + m0 += fabsf(x[4*i + 0]), m1 += fabsf(x[4*i + 0]) * k++; + m0 += fabsf(x[4*i + 1]), m1 += fabsf(x[4*i + 1]) * k++; + m0 += fabsf(x[4*i + 2]), m1 += fabsf(x[4*i + 2]) * k++; + m0 += fabsf(x[4*i + 3]), m1 += fabsf(x[4*i + 3]) * k++; + } + + int m = roundf((1.6f * m0) / ((1 + dt) * m1)); + low_bits = 8 - LC3_MIN(m, 8); + } + + /* --- Energy (dB) by 4 MDCT blocks --- */ + + float x2_max = 0; + + for (int i = 0; i < n4; i++) { + float x0 = x[4*i + 0] * x[4*i + 0]; + float x1 = x[4*i + 1] * x[4*i + 1]; + float x2 = x[4*i + 2] * x[4*i + 2]; + float x3 = x[4*i + 3] * x[4*i + 3]; + + x2_max = fmaxf(x2_max, x0); + x2_max = fmaxf(x2_max, x1); + x2_max = fmaxf(x2_max, x2); + x2_max = fmaxf(x2_max, x3); + + e[i].f = x0 + x1 + x2 + x3; + } + + float x_max = sqrtf(x2_max); + float nf = lc3_hr(sr) ? + lc3_ldexpf(x_max, -reg_bits) * lc3_exp2f(-low_bits) : 0; + + for (int i = 0; i < n4; i++) + e[i].q16 = lc3_db_q16(fmaxf(e[i].f + nf, 1e-10f)); + + /* --- Determine gain index --- */ + + int nbits = nbits_budget + nbits_off + 0.5f; + int g_int = 255 - g_off; + + const int k_20_28 = 20.f/28 * 0x1p16f + 0.5f; + const int k_2u7 = 2.7f * 0x1p16f + 0.5f; + const int k_1u4 = 1.4f * 0x1p16f + 0.5f; + + for (int i = 128, j, j0 = n4-1, j1 ; i > 0; i >>= 1) { + int gn = (g_int - i) * k_20_28; + int v = 0; + + for (j = j0; j >= 0 && e[j].q16 < gn; j--); + + for (j1 = j; j >= 0; j--) { + int e_diff = e[j].q16 - gn; + + v += e_diff < 0 ? k_2u7 : + e_diff < 43 << 16 ? e_diff + ( 7 << 16) + : 2*e_diff - (36 << 16); + } + + if (v > nbits * k_1u4) + j0 = j1; + else + g_int = g_int - i; + } + + /* --- Limit gain index --- */ + + float x_lim = lc3_hr(sr) ? 0x7fffp8f : 0x7fffp0f; + + *g_min = 255 - g_off; + for (int i = 128 ; i > 0; i >>= 1) + if (x_lim * unquantize_gain(*g_min - i) > x_max) + *g_min -= i; + + *reset_off = g_int < *g_min || x_max == 0; + if (*reset_off) + g_int = *g_min; + + return g_int; +} + +/** + * Global Gain Adjustment + * dt, sr Duration and samplerate of the frame + * g_idx The estimated quantized gain index + * nbits Computed number of bits coding the spectrum + * nbits_budget Number of bits available for coding the spectrum + * g_idx_min Minimum gain index value + * return Gain adjust value (-1 to 2) + */ +LC3_HOT static int adjust_gain( + enum lc3_dt dt, enum lc3_srate sr, + int g_idx, int nbits, int nbits_budget, int g_idx_min) +{ + /* --- Compute delta threshold --- */ + + const int *t = (const int [LC3_NUM_SRATE][3]){ + { 80, 500, 850 }, { 230, 1025, 1700 }, { 380, 1550, 2550 }, + { 530, 2075, 3400 }, { 680, 2600, 4250 }, + { 680, 2600, 4250 }, { 830, 3125, 5100 } + }[sr]; + + int delta, den = 48; + + if (nbits < t[0]) { + delta = 3*(nbits + 48); + + } else if (nbits < t[1]) { + int n0 = 3*(t[0] + 48), range = t[1] - t[0]; + delta = n0 * range + (nbits - t[0]) * (t[1] - n0); + den *= range; + + } else { + delta = LC3_MIN(nbits, t[2]); + } + + delta = (delta + den/2) / den; + + /* --- Adjust gain --- */ + + if (lc3_hr(sr) && nbits > nbits_budget) { + int factor = 1 + (dt <= LC3_DT_5M) + + (dt <= LC3_DT_2M5) * (1 + (nbits >= 520)); + + int g_incr = factor + (factor * (nbits - nbits_budget)) / delta; + return LC3_MIN(g_idx + g_incr, 255) - g_idx; + } + + if (!lc3_hr(sr) && nbits < nbits_budget - (delta + 2)) + return -(g_idx > g_idx_min); + + if (!lc3_hr(sr) && nbits > nbits_budget) + return (g_idx < 255) + (g_idx < 254 && nbits >= nbits_budget + delta); + + return 0; +} + +/** + * Spectrum quantization + * dt, sr Duration and samplerate of the frame + * g_int Quantization gain value + * x Spectral coefficients, scaled as output + * n Return count of significants + */ +LC3_HOT static void quantize( + enum lc3_dt dt, enum lc3_srate sr, int g_int, float *x, int *n) +{ + float g_inv = unquantize_gain(-g_int); + int ne = lc3_ne(dt, sr); + + *n = ne; + + for (int i = 0; i < ne; i += 2) { + float xq_min = lc3_hr(sr) ? 0.5f : 10.f/16; + + x[i+0] *= g_inv; + x[i+1] *= g_inv; + + *n = fabsf(x[i+0]) >= xq_min || + fabsf(x[i+1]) >= xq_min ? ne : *n - 2; + } +} + +/** + * Spectrum quantization inverse + * dt, sr Duration and samplerate of the frame + * g_int Quantization gain value + * x, nq Spectral quantized, and count of significants + * return Unquantized gain value + */ +LC3_HOT static float unquantize( + enum lc3_dt dt, enum lc3_srate sr, + int g_int, float *x, int nq) +{ + float g = unquantize_gain(g_int); + int i, ne = lc3_ne(dt, sr); + + for (i = 0; i < nq; i++) + x[i] = x[i] * g; + + for ( ; i < ne; i++) + x[i] = 0; + + return g; +} + + +/* ---------------------------------------------------------------------------- + * Spectrum coding + * -------------------------------------------------------------------------- */ + +/** + * Resolve High-bitrate and LSB modes according size of the frame + * sr, nbytes Samplerate and size of the frame + * p_lsb_mode True when LSB mode allowed, when not NULL + * return True when High-Rate mode enabled + */ +static bool resolve_modes(enum lc3_srate sr, int nbytes, bool *p_lsb_mode) +{ + int sr_ind = lc3_hr(sr) ? 4 + (sr - LC3_SRATE_48K_HR) : sr; + + if (p_lsb_mode) + *p_lsb_mode = (nbytes >= 20 * (3 + sr_ind)) && (sr < LC3_SRATE_96K_HR); + + return (nbytes > 20 * (1 + sr_ind)) && (sr < LC3_SRATE_96K_HR); +} + +/** + * Bit consumption + * dt, sr, nbytes Duration, samplerate and size of the frame + * x Spectral quantized coefficients + * n Count of significant coefficients, updated on truncation + * nbits_budget Truncate to stay in budget, when not zero + * p_lsb_mode Return True when LSB's are not AC coded, or NULL + * return The number of bits coding the spectrum + */ +LC3_HOT static int compute_nbits( + enum lc3_dt dt, enum lc3_srate sr, int nbytes, + const float *x, int *n, int nbits_budget, bool *p_lsb_mode) +{ + bool lsb_mode, high_rate = resolve_modes(sr, nbytes, &lsb_mode); + int ne = lc3_ne(dt, sr); + + /* --- Loop on quantized coefficients --- */ + + int nbits = 0, nbits_lsb = 0; + uint8_t state = 0; + + int nbits_end = 0; + int n_end = 0; + + nbits_budget = nbits_budget ? nbits_budget * 2048 : INT_MAX; + + for (int i = 0, h = 0; h < 2; h++) { + const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h]; + + for ( ; i < LC3_MIN(*n, (ne + 2) >> (1 - h)) + && nbits <= nbits_budget; i += 2) { + + float xq_off = lc3_hr(sr) ? 0.5f : 6.f/16; + uint32_t a = fabsf(x[i+0]) + xq_off; + uint32_t b = fabsf(x[i+1]) + xq_off; + + const uint8_t *lut = lut_coeff[state]; + + /* --- Sign values --- */ + + int s = (a != 0) + (b != 0); + nbits += s * 2048; + + /* --- LSB values Reduce to 2*2 bits MSB values --- + * Reduce to 2x2 bits MSB values. The LSB's pair are arithmetic + * coded with an escape code followed by 1 bit for each values. + * The LSB mode does not arthmetic code the first LSB, + * add the sign of the LSB when one of pair was at value 1 */ + + uint32_t m = (a | b) >> 2; + unsigned k = 0; + + if (m) { + + if (lsb_mode) { + nbits += lc3_spectrum_bits[lut[k++]][16] - 2*2048; + nbits_lsb += 2 + (a == 1) + (b == 1); + } + + for (m >>= lsb_mode; m; m >>= 1, k++) + nbits += lc3_spectrum_bits[lut[LC3_MIN(k, 3)]][16]; + + nbits += k * 2*2048; + a >>= k; + b >>= k; + + k = LC3_MIN(k, 3); + } + + /* --- MSB values --- */ + + nbits += lc3_spectrum_bits[lut[k]][a + 4*b]; + + /* --- Update state --- */ + + if (s && nbits <= nbits_budget) { + n_end = i + 2; + nbits_end = nbits; + } + + state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1)); + } + } + + /* --- Return --- */ + + *n = n_end; + + if (p_lsb_mode) + *p_lsb_mode = lsb_mode && + nbits_end + nbits_lsb * 2048 > nbits_budget; + + if (nbits_budget >= INT_MAX) + nbits_end += nbits_lsb * 2048; + + return (nbits_end + 2047) / 2048; +} + +/** + * Put quantized spectrum + * bits Bitstream context + * dt, sr, nbytes Duration, samplerate and size of the frame + * x Spectral quantized coefficients + * nq, lsb_mode Count of significants, and LSB discard indication + */ +LC3_HOT static void put_quantized(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_srate sr, int nbytes, + const float *x, int nq, bool lsb_mode) +{ + bool high_rate = resolve_modes(sr, nbytes, NULL); + int ne = lc3_ne(dt, sr); + + /* --- Loop on quantized coefficients --- */ + + uint8_t state = 0; + + for (int i = 0, h = 0; h < 2; h++) { + const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h]; + + for ( ; i < LC3_MIN(nq, (ne + 2) >> (1 - h)); i += 2) { + + float xq_off = lc3_hr(sr) ? 0.5f : 6.f/16; + uint32_t a = fabsf(x[i+0]) + xq_off; + uint32_t b = fabsf(x[i+1]) + xq_off; + + const uint8_t *lut = lut_coeff[state]; + + /* --- LSB values Reduce to 2*2 bits MSB values --- + * Reduce to 2x2 bits MSB values. The LSB's pair are arithmetic + * coded with an escape code and 1 bits for each values. + * The LSB mode discard the first LSB (at this step) */ + + uint32_t m = (a | b) >> 2; + unsigned k = 0, shr = 0; + + if (m) { + + if (lsb_mode) + lc3_put_symbol(bits, + lc3_spectrum_models + lut[k++], 16); + + for (m >>= lsb_mode; m; m >>= 1, k++) { + lc3_put_bit(bits, (a >> k) & 1); + lc3_put_bit(bits, (b >> k) & 1); + lc3_put_symbol(bits, + lc3_spectrum_models + lut[LC3_MIN(k, 3)], 16); + } + + a >>= lsb_mode; + b >>= lsb_mode; + + shr = k - lsb_mode; + k = LC3_MIN(k, 3); + } + + /* --- Sign values --- */ + + if (a) lc3_put_bit(bits, x[i+0] < 0); + if (b) lc3_put_bit(bits, x[i+1] < 0); + + /* --- MSB values --- */ + + a >>= shr; + b >>= shr; + + lc3_put_symbol(bits, lc3_spectrum_models + lut[k], a + 4*b); + + /* --- Update state --- */ + + state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1)); + } + } +} + +/** + * Get quantized spectrum + * bits Bitstream context + * dt, sr, nbytes Duration, samplerate and size of the frame + * nq, lsb_mode Count of significants, and LSB discard indication + * x Return `nq` spectral quantized coefficients + * nf_seed Return the noise factor seed associated + * return 0: Ok -1: Invalid bitstream data + */ +LC3_HOT static int get_quantized(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_srate sr, int nbytes, + int nq, bool lsb_mode, float *x, uint16_t *nf_seed) +{ + bool high_rate = resolve_modes(sr, nbytes, NULL); + int ne = lc3_ne(dt, sr); + + *nf_seed = 0; + + /* --- Loop on quantized coefficients --- */ + + uint8_t state = 0; + + for (int i = 0, h = 0; h < 2; h++) { + const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h]; + + for ( ; i < LC3_MIN(nq, (ne + 2) >> (1 - h)); i += 2) { + + const uint8_t *lut = lut_coeff[state]; + int max_shl = lc3_hr(sr) ? 22 : 14; + + /* --- LSB values --- + * Until the symbol read indicates the escape value 16, + * read an LSB bit for each values. + * The LSB mode discard the first LSB (at this step) */ + + int u = 0, v = 0; + int k = 0, shl = 0; + + unsigned s = lc3_get_symbol(bits, lc3_spectrum_models + lut[k]); + + if (lsb_mode && s >= 16) { + s = lc3_get_symbol(bits, lc3_spectrum_models + lut[++k]); + shl++; + } + + for ( ; s >= 16 && shl < max_shl; shl++) { + u |= lc3_get_bit(bits) << shl; + v |= lc3_get_bit(bits) << shl; + + k += (k < 3); + s = lc3_get_symbol(bits, lc3_spectrum_models + lut[k]); + } + + if (s >= 16) + return -1; + + /* --- MSB & sign values --- */ + + int a = s % 4; + int b = s / 4; + + u |= a << shl; + v |= b << shl; + + x[i+0] = u && lc3_get_bit(bits) ? -u : u; + x[i+1] = v && lc3_get_bit(bits) ? -v : v; + + *nf_seed = (*nf_seed + (u & 0x7fff) * (i ) + + (v & 0x7fff) * (i+1)) & 0xffff; + + /* --- Update state --- */ + + state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1)); + } + } + + return 0; +} + +/** + * Put residual bits of quantization + * bits Bitstream context + * nbits Maximum number of bits to output + * hrmode High-Resolution mode + * x, n Spectral quantized, and count of significants + */ +LC3_HOT static void put_residual(lc3_bits_t *bits, + int nbits, bool hrmode, float *x, int n) +{ + float xq_lim = hrmode ? 0.5f : 10.f/16; + float xq_off = xq_lim / 2; + + for (int iter = 0; iter < (hrmode ? 20 : 1) && nbits > 0; iter++) { + for (int i = 0; i < n && nbits > 0; i++) { + + float xr = fabsf(x[i]); + if (xr < xq_lim) + continue; + + bool b = (xr - truncf(xr) < xq_lim) ^ (x[i] < 0); + lc3_put_bit(bits, b); + nbits--; + + x[i] += b ? -xq_off : xq_off; + } + + xq_off *= xq_lim; + } +} + +/** + * Get residual bits of quantization + * bits Bitstream context + * nbits Maximum number of bits to output + * hrmode High-Resolution mode + * x, nq Spectral quantized, and count of significants + */ +LC3_HOT static void get_residual(lc3_bits_t *bits, + int nbits, bool hrmode, float *x, int n) +{ + float xq_off_1 = hrmode ? 0.25f : 5.f/16; + float xq_off_2 = hrmode ? 0.25f : 3.f/16; + + for (int iter = 0; iter < (hrmode ? 20 : 1) && nbits > 0; iter++) { + for (int i = 0; i < n && nbits > 0; i++) { + + if (x[i] == 0) + continue; + + if (lc3_get_bit(bits) == 0) + x[i] -= x[i] < 0 ? xq_off_1 : xq_off_2; + else + x[i] += x[i] > 0 ? xq_off_1 : xq_off_2; + + nbits--; + } + + xq_off_1 *= 0.5f; + xq_off_2 *= 0.5f; + } +} + +/** + * Put LSB values of quantized spectrum values + * bits Bitstream context + * nbits Maximum number of bits to output + * hrmode High-Resolution mode + * x, n Spectral quantized, and count of significants + */ +LC3_HOT static void put_lsb(lc3_bits_t *bits, + int nbits, bool hrmode, const float *x, int n) +{ + for (int i = 0; i < n && nbits > 0; i += 2) { + + float xq_off = hrmode ? 0.5f : 6.f/16; + uint32_t a = fabsf(x[i+0]) + xq_off; + uint32_t b = fabsf(x[i+1]) + xq_off; + + if ((a | b) >> 2 == 0) + continue; + + if (nbits-- > 0) + lc3_put_bit(bits, a & 1); + + if (a == 1 && nbits-- > 0) + lc3_put_bit(bits, x[i+0] < 0); + + if (nbits-- > 0) + lc3_put_bit(bits, b & 1); + + if (b == 1 && nbits-- > 0) + lc3_put_bit(bits, x[i+1] < 0); + } +} + +/** + * Get LSB values of quantized spectrum values + * bits Bitstream context + * nbits Maximum number of bits to output + * x, nq Spectral quantized, and count of significants + * nf_seed Update the noise factor seed according + */ +LC3_HOT static void get_lsb(lc3_bits_t *bits, + int nbits, float *x, int nq, uint16_t *nf_seed) +{ + for (int i = 0; i < nq && nbits > 0; i += 2) { + + float a = fabsf(x[i]), b = fabsf(x[i+1]); + + if (fmaxf(a, b) < 4) + continue; + + if (nbits-- > 0 && lc3_get_bit(bits)) { + if (a) { + x[i] += x[i] < 0 ? -1 : 1; + *nf_seed = (*nf_seed + i) & 0xffff; + } else if (nbits-- > 0) { + x[i] = lc3_get_bit(bits) ? -1 : 1; + *nf_seed = (*nf_seed + i) & 0xffff; + } + } + + if (nbits-- > 0 && lc3_get_bit(bits)) { + if (b) { + x[i+1] += x[i+1] < 0 ? -1 : 1; + *nf_seed = (*nf_seed + i+1) & 0xffff; + } else if (nbits-- > 0) { + x[i+1] = lc3_get_bit(bits) ? -1 : 1; + *nf_seed = (*nf_seed + i+1) & 0xffff; + } + } + } +} + + +/* ---------------------------------------------------------------------------- + * Noise coding + * -------------------------------------------------------------------------- */ + +/** + * Estimate noise level + * dt, bw Duration and bandwidth of the frame + * hrmode High-Resolution mode + * x, n Spectral quantized, and count of significants + * return Noise factor (0 to 7) + */ +LC3_HOT static int estimate_noise( + enum lc3_dt dt, enum lc3_bandwidth bw, bool hrmode, const float *x, int n) +{ + int bw_stop = lc3_ne(dt, (enum lc3_srate)LC3_MIN(bw, LC3_BANDWIDTH_FB)); + int w = 1 + (dt >= LC3_DT_7M5) + (dt>= LC3_DT_10M); + + float xq_lim = hrmode ? 0.5f : 10.f/16; + float sum = 0; + int i, ns = 0, z = 0; + + for (i = 6 * (1 + dt) - w; i < LC3_MIN(n, bw_stop); i++) { + z = fabsf(x[i]) < xq_lim ? z + 1 : 0; + if (z > 2*w) + sum += fabsf(x[i - w]), ns++; + } + + for ( ; i < bw_stop + w; i++) + if (++z > 2*w) + sum += fabsf(x[i - w]), ns++; + + int nf = ns ? 8 - (int)((16 * sum) / ns + 0.5f) : 8; + + return LC3_CLIP(nf, 0, 7); +} + +/** + * Noise filling + * dt, bw Duration and bandwidth of the frame + * nf, nf_seed The noise factor and pseudo-random seed + * g Quantization gain + * x, nq Spectral quantized, and count of significants + */ +LC3_HOT static void fill_noise(enum lc3_dt dt, enum lc3_bandwidth bw, + int nf, uint16_t nf_seed, float g, float *x, int nq) +{ + int bw_stop = lc3_ne(dt, (enum lc3_srate)LC3_MIN(bw, LC3_BANDWIDTH_FB)); + int w = 1 + (dt >= LC3_DT_7M5) + (dt>= LC3_DT_10M); + + float s = g * (float)(8 - nf) / 16; + int i, z = 0; + + for (i = 6 * (1 + dt) - w; i < LC3_MIN(nq, bw_stop); i++) { + z = x[i] ? 0 : z + 1; + if (z > 2*w) { + nf_seed = (13849 + nf_seed*31821) & 0xffff; + x[i - w] = nf_seed & 0x8000 ? -s : s; + } + } + + for ( ; i < bw_stop + w; i++) + if (++z > 2*w) { + nf_seed = (13849 + nf_seed*31821) & 0xffff; + x[i - w] = nf_seed & 0x8000 ? -s : s; + } +} + +/** + * Put noise factor + * bits Bitstream context + * nf Noise factor (0 to 7) + */ +static void put_noise_factor(lc3_bits_t *bits, int nf) +{ + lc3_put_bits(bits, nf, 3); +} + +/** + * Get noise factor + * bits Bitstream context + * return Noise factor (0 to 7) + */ +static int get_noise_factor(lc3_bits_t *bits) +{ + return lc3_get_bits(bits, 3); +} + + +/* ---------------------------------------------------------------------------- + * Encoding + * -------------------------------------------------------------------------- */ + +/** + * Bit consumption of the number of coded coefficients + * dt, sr, nbytes Duration, samplerate and size of the frame + * return Bit consumpution of the number of coded coefficients + */ +static int get_nbits_nq(enum lc3_dt dt, enum lc3_srate sr) +{ + int ne = lc3_ne(dt, sr); + return 4 + (ne > 32) + (ne > 64) + (ne > 128) + (ne > 256) + (ne > 512); +} + +/** + * Bit consumption of the arithmetic coder + * dt, sr, nbytes Duration, samplerate and size of the frame + * return Bit consumption of bitstream data + */ +static int get_nbits_ac(enum lc3_dt dt, enum lc3_srate sr, int nbytes) +{ + return get_nbits_nq(dt, sr) + + 3 + lc3_hr(sr) + LC3_MIN((nbytes-1) / 160, 2); +} + +/** + * Spectrum analysis + */ +void lc3_spec_analyze( + enum lc3_dt dt, enum lc3_srate sr, int nbytes, + bool pitch, const lc3_tns_data_t *tns, + struct lc3_spec_analysis *spec, + float *x, struct lc3_spec_side *side) +{ + bool reset_off; + + /* --- Bit budget --- */ + + const int nbits_gain = 8; + const int nbits_nf = 3; + + int nbits_budget = 8*nbytes - get_nbits_ac(dt, sr, nbytes) - + lc3_bwdet_get_nbits(sr) - lc3_ltpf_get_nbits(pitch) - + lc3_sns_get_nbits() - lc3_tns_get_nbits(tns) - nbits_gain - nbits_nf; + + /* --- Global gain --- */ + + float nbits_off = spec->nbits_off + spec->nbits_spare; + nbits_off = fminf(fmaxf(nbits_off, -40), 40); + nbits_off = 0.8f * spec->nbits_off + 0.2f * nbits_off; + + int g_off = resolve_gain_offset(sr, nbytes); + + int g_min, g_int = estimate_gain(dt, sr, + x, nbytes, nbits_budget, nbits_off, g_off, &reset_off, &g_min); + + /* --- Quantization --- */ + + quantize(dt, sr, g_int, x, &side->nq); + + int nbits = compute_nbits(dt, sr, nbytes, x, &side->nq, 0, NULL); + + spec->nbits_off = reset_off ? 0 : nbits_off; + spec->nbits_spare = reset_off ? 0 : nbits_budget - nbits; + + /* --- Adjust gain and requantize --- */ + + int g_adj = adjust_gain(dt, sr, + g_off + g_int, nbits, nbits_budget, g_off + g_min); + + if (g_adj) + quantize(dt, sr, g_adj, x, &side->nq); + + side->g_idx = g_int + g_adj + g_off; + nbits = compute_nbits(dt, sr, nbytes, + x, &side->nq, nbits_budget, &side->lsb_mode); +} + +/** + * Put spectral quantization side data + */ +void lc3_spec_put_side(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_srate sr, + const struct lc3_spec_side *side) +{ + int nbits_nq = get_nbits_nq(dt, sr); + + lc3_put_bits(bits, LC3_MAX(side->nq >> 1, 1) - 1, nbits_nq); + lc3_put_bits(bits, side->lsb_mode, 1); + lc3_put_bits(bits, side->g_idx, 8); +} + +/** + * Encode spectral coefficients + */ +void lc3_spec_encode(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw, + int nbytes, const lc3_spec_side_t *side, float *x) +{ + bool lsb_mode = side->lsb_mode; + int nq = side->nq; + + put_noise_factor(bits, estimate_noise(dt, bw, lc3_hr(sr), x, nq)); + + put_quantized(bits, dt, sr, nbytes, x, nq, lsb_mode); + + int nbits_left = lc3_get_bits_left(bits); + + if (lsb_mode) + put_lsb(bits, nbits_left, lc3_hr(sr), x, nq); + else + put_residual(bits, nbits_left, lc3_hr(sr), x, nq); +} + + +/* ---------------------------------------------------------------------------- + * Decoding + * -------------------------------------------------------------------------- */ + +/** + * Get spectral quantization side data + */ +int lc3_spec_get_side(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_srate sr, struct lc3_spec_side *side) +{ + int nbits_nq = get_nbits_nq(dt, sr); + int ne = lc3_ne(dt, sr); + + side->nq = (lc3_get_bits(bits, nbits_nq) + 1) << 1; + side->lsb_mode = lc3_get_bit(bits); + side->g_idx = lc3_get_bits(bits, 8); + + return side->nq > ne ? (side->nq = ne), -1 : 0; +} + +/** + * Decode spectral coefficients + */ +int lc3_spec_decode(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw, + int nbytes, const lc3_spec_side_t *side, float *x) +{ + bool lsb_mode = side->lsb_mode; + int nq = side->nq; + int ret = 0; + + int nf = get_noise_factor(bits); + uint16_t nf_seed; + + if ((ret = get_quantized(bits, dt, sr, nbytes, + nq, lsb_mode, x, &nf_seed)) < 0) + return ret; + + int nbits_left = lc3_get_bits_left(bits); + + if (lsb_mode) + get_lsb(bits, nbits_left, x, nq, &nf_seed); + else + get_residual(bits, nbits_left, lc3_hr(sr), x, nq); + + int g_int = side->g_idx - resolve_gain_offset(sr, nbytes); + float g = unquantize(dt, sr, g_int, x, nq); + + if (nq > 2 || x[0] || x[1] || side->g_idx > 0 || nf < 7) + fill_noise(dt, bw, nf, nf_seed, g, x, nq); + + return 0; +} diff --git a/App_Resources/iOS/src/lc3/spec.h b/App_Resources/iOS/src/lc3/spec.h new file mode 100644 index 00000000..92843638 --- /dev/null +++ b/App_Resources/iOS/src/lc3/spec.h @@ -0,0 +1,105 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_SPEC_H +#define __LC3_SPEC_H + +#include "common.h" +#include "tables.h" +#include "bwdet.h" +#include "ltpf.h" +#include "tns.h" +#include "sns.h" + + +/** + * Spectral quantization side data + */ +typedef struct lc3_spec_side { + int g_idx, nq; + bool lsb_mode; +} lc3_spec_side_t; + + +/* ---------------------------------------------------------------------------- + * Encoding + * -------------------------------------------------------------------------- */ + +/** + * Spectrum analysis + * dt, sr, nbytes Duration, samplerate and size of the frame + * pitch, tns Pitch present indication and TNS bistream data + * spec Context of analysis + * x Spectral coefficients, scaled as output + * side Return quantization data + */ +void lc3_spec_analyze( + enum lc3_dt dt, enum lc3_srate sr, int nbytes, + bool pitch, const lc3_tns_data_t *tns, lc3_spec_analysis_t *spec, + float *x, lc3_spec_side_t *side); + +/** + * Put spectral quantization side data + * bits Bitstream context + * dt, sr Duration and samplerate of the frame + * side Spectral quantization side data + */ +void lc3_spec_put_side(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_srate sr, const lc3_spec_side_t *side); + +/** + * Encode spectral coefficients + * bits Bitstream context + * dt, sr, bw Duration, samplerate, bandwidth + * nbytes and size of the frame + * side, x Quantization data, and scaled coefficients + */ +void lc3_spec_encode(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw, + int nbytes, const lc3_spec_side_t *side, float *x); + + +/* ---------------------------------------------------------------------------- + * Decoding + * -------------------------------------------------------------------------- */ + +/** + * Get spectral quantization side data + * bits Bitstream context + * dt, sr Duration and samplerate of the frame + * side Return quantization side data + * return 0: Ok -1: Invalid bandwidth indication + */ +int lc3_spec_get_side(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_srate sr, lc3_spec_side_t *side); + +/** + * Decode spectral coefficients + * bits Bitstream context + * dt, sr, bw Duration, samplerate, bandwidth + * nbytes and size of the frame + * side Quantization side data + * x Spectral coefficients + * return 0: Ok -1: Invalid bitstream data + */ +int lc3_spec_decode(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw, + int nbytes, const lc3_spec_side_t *side, float *x); + + +#endif /* __LC3_SPEC_H */ diff --git a/App_Resources/iOS/src/lc3/tables.c b/App_Resources/iOS/src/lc3/tables.c new file mode 100644 index 00000000..947813d5 --- /dev/null +++ b/App_Resources/iOS/src/lc3/tables.c @@ -0,0 +1,5762 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "tables.h" + + +/** + * Characteristics + */ + +const int lc3_ns_2m5[LC3_NUM_SRATE] = { + [LC3_SRATE_8K ] = LC3_NS(2500, 8000), + [LC3_SRATE_16K ] = LC3_NS(2500, 16000), + [LC3_SRATE_24K ] = LC3_NS(2500, 24000), + [LC3_SRATE_32K ] = LC3_NS(2500, 32000), + [LC3_SRATE_48K ] = LC3_NS(2500, 48000), + [LC3_SRATE_48K_HR] = LC3_NS(2500, 48000), + [LC3_SRATE_96K_HR] = LC3_NS(2500, 96000), +}; + +const int lc3_ne_2m5[LC3_NUM_SRATE] = { + [LC3_SRATE_8K ] = LC3_NS(2500, 8000), + [LC3_SRATE_16K ] = LC3_NS(2500, 16000), + [LC3_SRATE_24K ] = LC3_NS(2500, 24000), + [LC3_SRATE_32K ] = LC3_NS(2500, 32000), + [LC3_SRATE_48K ] = LC3_NS(2500, 40000), + [LC3_SRATE_48K_HR] = LC3_NS(2500, 48000), + [LC3_SRATE_96K_HR] = LC3_NS(2500, 96000), +}; + +const int lc3_ns_4m[LC3_NUM_SRATE] = { + [LC3_SRATE_8K ] = LC3_NS(4000, 8000), + [LC3_SRATE_16K ] = LC3_NS(4000, 16000), + [LC3_SRATE_24K ] = LC3_NS(4000, 24000), + [LC3_SRATE_32K ] = LC3_NS(4000, 32000), + [LC3_SRATE_48K ] = LC3_NS(4000, 48000), + [LC3_SRATE_48K_HR] = LC3_NS(4000, 48000), + [LC3_SRATE_96K_HR] = LC3_NS(4000, 96000), +}; + + +/** + * Limits on size of frame + * For fallback operation, half-size should be accepted. + */ + +const int lc3_frame_bytes_hr_lim + [LC3_NUM_DT][LC3_NUM_SRATE - LC3_SRATE_48K_HR][2] = { + + [LC3_DT_2M5][LC3_SRATE_48K_HR - LC3_SRATE_48K_HR] = { 54/2, 210 }, + [LC3_DT_2M5][LC3_SRATE_96K_HR - LC3_SRATE_48K_HR] = { 62/2, 210 }, + + [LC3_DT_5M ][LC3_SRATE_48K_HR - LC3_SRATE_48K_HR] = { 93/2, 375 }, + [LC3_DT_5M ][LC3_SRATE_96K_HR - LC3_SRATE_48K_HR] = { 109/2, 375 }, + + [LC3_DT_10M][LC3_SRATE_48K_HR - LC3_SRATE_48K_HR] = { 156/2, 625 }, + [LC3_DT_10M][LC3_SRATE_96K_HR - LC3_SRATE_48K_HR] = { 187/2, 625 }, + +}; + + +/** + * Twiddles FFT 3 points + * + * T[0..N-1] = + * { cos(-2Pi * i/N) + j sin(-2Pi * i/N), + * cos(-2Pi * 2i/N) + j sin(-2Pi * 2i/N) } , N=15, 45 + */ + +static const struct lc3_fft_bf3_twiddles fft_twiddles_15 = { + .n3 = 15/3, .t = (const struct lc3_complex [][2]){ + { { 1.0000000e+0, -0.0000000e+0 }, { 1.0000000e+0, -0.0000000e+0 } }, + { { 9.1354546e-1, -4.0673664e-1 }, { 6.6913061e-1, -7.4314483e-1 } }, + { { 6.6913061e-1, -7.4314483e-1 }, { -1.0452846e-1, -9.9452190e-1 } }, + { { 3.0901699e-1, -9.5105652e-1 }, { -8.0901699e-1, -5.8778525e-1 } }, + { { -1.0452846e-1, -9.9452190e-1 }, { -9.7814760e-1, 2.0791169e-1 } }, + { { -5.0000000e-1, -8.6602540e-1 }, { -5.0000000e-1, 8.6602540e-1 } }, + { { -8.0901699e-1, -5.8778525e-1 }, { 3.0901699e-1, 9.5105652e-1 } }, + { { -9.7814760e-1, -2.0791169e-1 }, { 9.1354546e-1, 4.0673664e-1 } }, + { { -9.7814760e-1, 2.0791169e-1 }, { 9.1354546e-1, -4.0673664e-1 } }, + { { -8.0901699e-1, 5.8778525e-1 }, { 3.0901699e-1, -9.5105652e-1 } }, + { { -5.0000000e-1, 8.6602540e-1 }, { -5.0000000e-1, -8.6602540e-1 } }, + { { -1.0452846e-1, 9.9452190e-1 }, { -9.7814760e-1, -2.0791169e-1 } }, + { { 3.0901699e-1, 9.5105652e-1 }, { -8.0901699e-1, 5.8778525e-1 } }, + { { 6.6913061e-1, 7.4314483e-1 }, { -1.0452846e-1, 9.9452190e-1 } }, + { { 9.1354546e-1, 4.0673664e-1 }, { 6.6913061e-1, 7.4314483e-1 } }, + } +}; + +static const struct lc3_fft_bf3_twiddles fft_twiddles_45 = { + .n3 = 45/3, .t = (const struct lc3_complex [][2]){ + { { 1.0000000e+0, -0.0000000e+0 }, { 1.0000000e+0, -0.0000000e+0 } }, + { { 9.9026807e-1, -1.3917310e-1 }, { 9.6126170e-1, -2.7563736e-1 } }, + { { 9.6126170e-1, -2.7563736e-1 }, { 8.4804810e-1, -5.2991926e-1 } }, + { { 9.1354546e-1, -4.0673664e-1 }, { 6.6913061e-1, -7.4314483e-1 } }, + { { 8.4804810e-1, -5.2991926e-1 }, { 4.3837115e-1, -8.9879405e-1 } }, + { { 7.6604444e-1, -6.4278761e-1 }, { 1.7364818e-1, -9.8480775e-1 } }, + { { 6.6913061e-1, -7.4314483e-1 }, { -1.0452846e-1, -9.9452190e-1 } }, + { { 5.5919290e-1, -8.2903757e-1 }, { -3.7460659e-1, -9.2718385e-1 } }, + { { 4.3837115e-1, -8.9879405e-1 }, { -6.1566148e-1, -7.8801075e-1 } }, + { { 3.0901699e-1, -9.5105652e-1 }, { -8.0901699e-1, -5.8778525e-1 } }, + { { 1.7364818e-1, -9.8480775e-1 }, { -9.3969262e-1, -3.4202014e-1 } }, + { { 3.4899497e-2, -9.9939083e-1 }, { -9.9756405e-1, -6.9756474e-2 } }, + { { -1.0452846e-1, -9.9452190e-1 }, { -9.7814760e-1, 2.0791169e-1 } }, + { { -2.4192190e-1, -9.7029573e-1 }, { -8.8294759e-1, 4.6947156e-1 } }, + { { -3.7460659e-1, -9.2718385e-1 }, { -7.1933980e-1, 6.9465837e-1 } }, + { { -5.0000000e-1, -8.6602540e-1 }, { -5.0000000e-1, 8.6602540e-1 } }, + { { -6.1566148e-1, -7.8801075e-1 }, { -2.4192190e-1, 9.7029573e-1 } }, + { { -7.1933980e-1, -6.9465837e-1 }, { 3.4899497e-2, 9.9939083e-1 } }, + { { -8.0901699e-1, -5.8778525e-1 }, { 3.0901699e-1, 9.5105652e-1 } }, + { { -8.8294759e-1, -4.6947156e-1 }, { 5.5919290e-1, 8.2903757e-1 } }, + { { -9.3969262e-1, -3.4202014e-1 }, { 7.6604444e-1, 6.4278761e-1 } }, + { { -9.7814760e-1, -2.0791169e-1 }, { 9.1354546e-1, 4.0673664e-1 } }, + { { -9.9756405e-1, -6.9756474e-2 }, { 9.9026807e-1, 1.3917310e-1 } }, + { { -9.9756405e-1, 6.9756474e-2 }, { 9.9026807e-1, -1.3917310e-1 } }, + { { -9.7814760e-1, 2.0791169e-1 }, { 9.1354546e-1, -4.0673664e-1 } }, + { { -9.3969262e-1, 3.4202014e-1 }, { 7.6604444e-1, -6.4278761e-1 } }, + { { -8.8294759e-1, 4.6947156e-1 }, { 5.5919290e-1, -8.2903757e-1 } }, + { { -8.0901699e-1, 5.8778525e-1 }, { 3.0901699e-1, -9.5105652e-1 } }, + { { -7.1933980e-1, 6.9465837e-1 }, { 3.4899497e-2, -9.9939083e-1 } }, + { { -6.1566148e-1, 7.8801075e-1 }, { -2.4192190e-1, -9.7029573e-1 } }, + { { -5.0000000e-1, 8.6602540e-1 }, { -5.0000000e-1, -8.6602540e-1 } }, + { { -3.7460659e-1, 9.2718385e-1 }, { -7.1933980e-1, -6.9465837e-1 } }, + { { -2.4192190e-1, 9.7029573e-1 }, { -8.8294759e-1, -4.6947156e-1 } }, + { { -1.0452846e-1, 9.9452190e-1 }, { -9.7814760e-1, -2.0791169e-1 } }, + { { 3.4899497e-2, 9.9939083e-1 }, { -9.9756405e-1, 6.9756474e-2 } }, + { { 1.7364818e-1, 9.8480775e-1 }, { -9.3969262e-1, 3.4202014e-1 } }, + { { 3.0901699e-1, 9.5105652e-1 }, { -8.0901699e-1, 5.8778525e-1 } }, + { { 4.3837115e-1, 8.9879405e-1 }, { -6.1566148e-1, 7.8801075e-1 } }, + { { 5.5919290e-1, 8.2903757e-1 }, { -3.7460659e-1, 9.2718385e-1 } }, + { { 6.6913061e-1, 7.4314483e-1 }, { -1.0452846e-1, 9.9452190e-1 } }, + { { 7.6604444e-1, 6.4278761e-1 }, { 1.7364818e-1, 9.8480775e-1 } }, + { { 8.4804810e-1, 5.2991926e-1 }, { 4.3837115e-1, 8.9879405e-1 } }, + { { 9.1354546e-1, 4.0673664e-1 }, { 6.6913061e-1, 7.4314483e-1 } }, + { { 9.6126170e-1, 2.7563736e-1 }, { 8.4804810e-1, 5.2991926e-1 } }, + { { 9.9026807e-1, 1.3917310e-1 }, { 9.6126170e-1, 2.7563736e-1 } }, + } +}; + +const struct lc3_fft_bf3_twiddles *lc3_fft_twiddles_bf3[] = + { &fft_twiddles_15, &fft_twiddles_45 }; + + +/** + * Twiddles FFT 2 points + * + * T[0..N/2-1] = + * cos(-2Pi * i/N) + j sin(-2Pi * i/N) , N=10, 20, ... + */ + +static const struct lc3_fft_bf2_twiddles fft_twiddles_10 = { + .n2 = 10/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 8.0901699e-01, -5.8778525e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { -3.0901699e-01, -9.5105652e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, + } +}; + +static const struct lc3_fft_bf2_twiddles fft_twiddles_20 = { + .n2 = 20/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.5105652e-01, -3.0901699e-01 }, + { 8.0901699e-01, -5.8778525e-01 }, { 5.8778525e-01, -8.0901699e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 6.1232340e-17, -1.0000000e+00 }, + { -3.0901699e-01, -9.5105652e-01 }, { -5.8778525e-01, -8.0901699e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -9.5105652e-01, -3.0901699e-01 }, + } +}; + +static const struct lc3_fft_bf2_twiddles fft_twiddles_30 = { + .n2 = 30/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.7814760e-01, -2.0791169e-01 }, + { 9.1354546e-01, -4.0673664e-01 }, { 8.0901699e-01, -5.8778525e-01 }, + { 6.6913061e-01, -7.4314483e-01 }, { 5.0000000e-01, -8.6602540e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 1.0452846e-01, -9.9452190e-01 }, + { -1.0452846e-01, -9.9452190e-01 }, { -3.0901699e-01, -9.5105652e-01 }, + { -5.0000000e-01, -8.6602540e-01 }, { -6.6913061e-01, -7.4314483e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -9.1354546e-01, -4.0673664e-01 }, + { -9.7814760e-01, -2.0791169e-01 }, + } +}; + +static const struct lc3_fft_bf2_twiddles fft_twiddles_40 = { + .n2 = 40/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.8768834e-01, -1.5643447e-01 }, + { 9.5105652e-01, -3.0901699e-01 }, { 8.9100652e-01, -4.5399050e-01 }, + { 8.0901699e-01, -5.8778525e-01 }, { 7.0710678e-01, -7.0710678e-01 }, + { 5.8778525e-01, -8.0901699e-01 }, { 4.5399050e-01, -8.9100652e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 1.5643447e-01, -9.8768834e-01 }, + { 6.1232340e-17, -1.0000000e+00 }, { -1.5643447e-01, -9.8768834e-01 }, + { -3.0901699e-01, -9.5105652e-01 }, { -4.5399050e-01, -8.9100652e-01 }, + { -5.8778525e-01, -8.0901699e-01 }, { -7.0710678e-01, -7.0710678e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -8.9100652e-01, -4.5399050e-01 }, + { -9.5105652e-01, -3.0901699e-01 }, { -9.8768834e-01, -1.5643447e-01 }, + } +}; + +static const struct lc3_fft_bf2_twiddles fft_twiddles_60 = { + .n2 = 60/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.9452190e-01, -1.0452846e-01 }, + { 9.7814760e-01, -2.0791169e-01 }, { 9.5105652e-01, -3.0901699e-01 }, + { 9.1354546e-01, -4.0673664e-01 }, { 8.6602540e-01, -5.0000000e-01 }, + { 8.0901699e-01, -5.8778525e-01 }, { 7.4314483e-01, -6.6913061e-01 }, + { 6.6913061e-01, -7.4314483e-01 }, { 5.8778525e-01, -8.0901699e-01 }, + { 5.0000000e-01, -8.6602540e-01 }, { 4.0673664e-01, -9.1354546e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 2.0791169e-01, -9.7814760e-01 }, + { 1.0452846e-01, -9.9452190e-01 }, { 2.8327694e-16, -1.0000000e+00 }, + { -1.0452846e-01, -9.9452190e-01 }, { -2.0791169e-01, -9.7814760e-01 }, + { -3.0901699e-01, -9.5105652e-01 }, { -4.0673664e-01, -9.1354546e-01 }, + { -5.0000000e-01, -8.6602540e-01 }, { -5.8778525e-01, -8.0901699e-01 }, + { -6.6913061e-01, -7.4314483e-01 }, { -7.4314483e-01, -6.6913061e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -8.6602540e-01, -5.0000000e-01 }, + { -9.1354546e-01, -4.0673664e-01 }, { -9.5105652e-01, -3.0901699e-01 }, + { -9.7814760e-01, -2.0791169e-01 }, { -9.9452190e-01, -1.0452846e-01 }, + } +}; + +static const struct lc3_fft_bf2_twiddles fft_twiddles_80 = { + .n2 = 80/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.9691733e-01, -7.8459096e-02 }, + { 9.8768834e-01, -1.5643447e-01 }, { 9.7236992e-01, -2.3344536e-01 }, + { 9.5105652e-01, -3.0901699e-01 }, { 9.2387953e-01, -3.8268343e-01 }, + { 8.9100652e-01, -4.5399050e-01 }, { 8.5264016e-01, -5.2249856e-01 }, + { 8.0901699e-01, -5.8778525e-01 }, { 7.6040597e-01, -6.4944805e-01 }, + { 7.0710678e-01, -7.0710678e-01 }, { 6.4944805e-01, -7.6040597e-01 }, + { 5.8778525e-01, -8.0901699e-01 }, { 5.2249856e-01, -8.5264016e-01 }, + { 4.5399050e-01, -8.9100652e-01 }, { 3.8268343e-01, -9.2387953e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 2.3344536e-01, -9.7236992e-01 }, + { 1.5643447e-01, -9.8768834e-01 }, { 7.8459096e-02, -9.9691733e-01 }, + { 6.1232340e-17, -1.0000000e+00 }, { -7.8459096e-02, -9.9691733e-01 }, + { -1.5643447e-01, -9.8768834e-01 }, { -2.3344536e-01, -9.7236992e-01 }, + { -3.0901699e-01, -9.5105652e-01 }, { -3.8268343e-01, -9.2387953e-01 }, + { -4.5399050e-01, -8.9100652e-01 }, { -5.2249856e-01, -8.5264016e-01 }, + { -5.8778525e-01, -8.0901699e-01 }, { -6.4944805e-01, -7.6040597e-01 }, + { -7.0710678e-01, -7.0710678e-01 }, { -7.6040597e-01, -6.4944805e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -8.5264016e-01, -5.2249856e-01 }, + { -8.9100652e-01, -4.5399050e-01 }, { -9.2387953e-01, -3.8268343e-01 }, + { -9.5105652e-01, -3.0901699e-01 }, { -9.7236992e-01, -2.3344536e-01 }, + { -9.8768834e-01, -1.5643447e-01 }, { -9.9691733e-01, -7.8459096e-02 }, + } +}; + +static const struct lc3_fft_bf2_twiddles fft_twiddles_90 = { + .n2 = 90/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.9756405e-01, -6.9756474e-02 }, + { 9.9026807e-01, -1.3917310e-01 }, { 9.7814760e-01, -2.0791169e-01 }, + { 9.6126170e-01, -2.7563736e-01 }, { 9.3969262e-01, -3.4202014e-01 }, + { 9.1354546e-01, -4.0673664e-01 }, { 8.8294759e-01, -4.6947156e-01 }, + { 8.4804810e-01, -5.2991926e-01 }, { 8.0901699e-01, -5.8778525e-01 }, + { 7.6604444e-01, -6.4278761e-01 }, { 7.1933980e-01, -6.9465837e-01 }, + { 6.6913061e-01, -7.4314483e-01 }, { 6.1566148e-01, -7.8801075e-01 }, + { 5.5919290e-01, -8.2903757e-01 }, { 5.0000000e-01, -8.6602540e-01 }, + { 4.3837115e-01, -8.9879405e-01 }, { 3.7460659e-01, -9.2718385e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 2.4192190e-01, -9.7029573e-01 }, + { 1.7364818e-01, -9.8480775e-01 }, { 1.0452846e-01, -9.9452190e-01 }, + { 3.4899497e-02, -9.9939083e-01 }, { -3.4899497e-02, -9.9939083e-01 }, + { -1.0452846e-01, -9.9452190e-01 }, { -1.7364818e-01, -9.8480775e-01 }, + { -2.4192190e-01, -9.7029573e-01 }, { -3.0901699e-01, -9.5105652e-01 }, + { -3.7460659e-01, -9.2718385e-01 }, { -4.3837115e-01, -8.9879405e-01 }, + { -5.0000000e-01, -8.6602540e-01 }, { -5.5919290e-01, -8.2903757e-01 }, + { -6.1566148e-01, -7.8801075e-01 }, { -6.6913061e-01, -7.4314483e-01 }, + { -7.1933980e-01, -6.9465837e-01 }, { -7.6604444e-01, -6.4278761e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -8.4804810e-01, -5.2991926e-01 }, + { -8.8294759e-01, -4.6947156e-01 }, { -9.1354546e-01, -4.0673664e-01 }, + { -9.3969262e-01, -3.4202014e-01 }, { -9.6126170e-01, -2.7563736e-01 }, + { -9.7814760e-01, -2.0791169e-01 }, { -9.9026807e-01, -1.3917310e-01 }, + { -9.9756405e-01, -6.9756474e-02 }, + } +}; + +static const struct lc3_fft_bf2_twiddles fft_twiddles_120 = { + .n2 = 120/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.9862953e-01, -5.2335956e-02 }, + { 9.9452190e-01, -1.0452846e-01 }, { 9.8768834e-01, -1.5643447e-01 }, + { 9.7814760e-01, -2.0791169e-01 }, { 9.6592583e-01, -2.5881905e-01 }, + { 9.5105652e-01, -3.0901699e-01 }, { 9.3358043e-01, -3.5836795e-01 }, + { 9.1354546e-01, -4.0673664e-01 }, { 8.9100652e-01, -4.5399050e-01 }, + { 8.6602540e-01, -5.0000000e-01 }, { 8.3867057e-01, -5.4463904e-01 }, + { 8.0901699e-01, -5.8778525e-01 }, { 7.7714596e-01, -6.2932039e-01 }, + { 7.4314483e-01, -6.6913061e-01 }, { 7.0710678e-01, -7.0710678e-01 }, + { 6.6913061e-01, -7.4314483e-01 }, { 6.2932039e-01, -7.7714596e-01 }, + { 5.8778525e-01, -8.0901699e-01 }, { 5.4463904e-01, -8.3867057e-01 }, + { 5.0000000e-01, -8.6602540e-01 }, { 4.5399050e-01, -8.9100652e-01 }, + { 4.0673664e-01, -9.1354546e-01 }, { 3.5836795e-01, -9.3358043e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 2.5881905e-01, -9.6592583e-01 }, + { 2.0791169e-01, -9.7814760e-01 }, { 1.5643447e-01, -9.8768834e-01 }, + { 1.0452846e-01, -9.9452190e-01 }, { 5.2335956e-02, -9.9862953e-01 }, + { 2.8327694e-16, -1.0000000e+00 }, { -5.2335956e-02, -9.9862953e-01 }, + { -1.0452846e-01, -9.9452190e-01 }, { -1.5643447e-01, -9.8768834e-01 }, + { -2.0791169e-01, -9.7814760e-01 }, { -2.5881905e-01, -9.6592583e-01 }, + { -3.0901699e-01, -9.5105652e-01 }, { -3.5836795e-01, -9.3358043e-01 }, + { -4.0673664e-01, -9.1354546e-01 }, { -4.5399050e-01, -8.9100652e-01 }, + { -5.0000000e-01, -8.6602540e-01 }, { -5.4463904e-01, -8.3867057e-01 }, + { -5.8778525e-01, -8.0901699e-01 }, { -6.2932039e-01, -7.7714596e-01 }, + { -6.6913061e-01, -7.4314483e-01 }, { -7.0710678e-01, -7.0710678e-01 }, + { -7.4314483e-01, -6.6913061e-01 }, { -7.7714596e-01, -6.2932039e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -8.3867057e-01, -5.4463904e-01 }, + { -8.6602540e-01, -5.0000000e-01 }, { -8.9100652e-01, -4.5399050e-01 }, + { -9.1354546e-01, -4.0673664e-01 }, { -9.3358043e-01, -3.5836795e-01 }, + { -9.5105652e-01, -3.0901699e-01 }, { -9.6592583e-01, -2.5881905e-01 }, + { -9.7814760e-01, -2.0791169e-01 }, { -9.8768834e-01, -1.5643447e-01 }, + { -9.9452190e-01, -1.0452846e-01 }, { -9.9862953e-01, -5.2335956e-02 }, + } +}; + +static const struct lc3_fft_bf2_twiddles fft_twiddles_160 = { + .n2 = 160/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.9922904e-01, -3.9259816e-02 }, + { 9.9691733e-01, -7.8459096e-02 }, { 9.9306846e-01, -1.1753740e-01 }, + { 9.8768834e-01, -1.5643447e-01 }, { 9.8078528e-01, -1.9509032e-01 }, + { 9.7236992e-01, -2.3344536e-01 }, { 9.6245524e-01, -2.7144045e-01 }, + { 9.5105652e-01, -3.0901699e-01 }, { 9.3819134e-01, -3.4611706e-01 }, + { 9.2387953e-01, -3.8268343e-01 }, { 9.0814317e-01, -4.1865974e-01 }, + { 8.9100652e-01, -4.5399050e-01 }, { 8.7249601e-01, -4.8862124e-01 }, + { 8.5264016e-01, -5.2249856e-01 }, { 8.3146961e-01, -5.5557023e-01 }, + { 8.0901699e-01, -5.8778525e-01 }, { 7.8531693e-01, -6.1909395e-01 }, + { 7.6040597e-01, -6.4944805e-01 }, { 7.3432251e-01, -6.7880075e-01 }, + { 7.0710678e-01, -7.0710678e-01 }, { 6.7880075e-01, -7.3432251e-01 }, + { 6.4944805e-01, -7.6040597e-01 }, { 6.1909395e-01, -7.8531693e-01 }, + { 5.8778525e-01, -8.0901699e-01 }, { 5.5557023e-01, -8.3146961e-01 }, + { 5.2249856e-01, -8.5264016e-01 }, { 4.8862124e-01, -8.7249601e-01 }, + { 4.5399050e-01, -8.9100652e-01 }, { 4.1865974e-01, -9.0814317e-01 }, + { 3.8268343e-01, -9.2387953e-01 }, { 3.4611706e-01, -9.3819134e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 2.7144045e-01, -9.6245524e-01 }, + { 2.3344536e-01, -9.7236992e-01 }, { 1.9509032e-01, -9.8078528e-01 }, + { 1.5643447e-01, -9.8768834e-01 }, { 1.1753740e-01, -9.9306846e-01 }, + { 7.8459096e-02, -9.9691733e-01 }, { 3.9259816e-02, -9.9922904e-01 }, + { 6.1232340e-17, -1.0000000e+00 }, { -3.9259816e-02, -9.9922904e-01 }, + { -7.8459096e-02, -9.9691733e-01 }, { -1.1753740e-01, -9.9306846e-01 }, + { -1.5643447e-01, -9.8768834e-01 }, { -1.9509032e-01, -9.8078528e-01 }, + { -2.3344536e-01, -9.7236992e-01 }, { -2.7144045e-01, -9.6245524e-01 }, + { -3.0901699e-01, -9.5105652e-01 }, { -3.4611706e-01, -9.3819134e-01 }, + { -3.8268343e-01, -9.2387953e-01 }, { -4.1865974e-01, -9.0814317e-01 }, + { -4.5399050e-01, -8.9100652e-01 }, { -4.8862124e-01, -8.7249601e-01 }, + { -5.2249856e-01, -8.5264016e-01 }, { -5.5557023e-01, -8.3146961e-01 }, + { -5.8778525e-01, -8.0901699e-01 }, { -6.1909395e-01, -7.8531693e-01 }, + { -6.4944805e-01, -7.6040597e-01 }, { -6.7880075e-01, -7.3432251e-01 }, + { -7.0710678e-01, -7.0710678e-01 }, { -7.3432251e-01, -6.7880075e-01 }, + { -7.6040597e-01, -6.4944805e-01 }, { -7.8531693e-01, -6.1909395e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -8.3146961e-01, -5.5557023e-01 }, + { -8.5264016e-01, -5.2249856e-01 }, { -8.7249601e-01, -4.8862124e-01 }, + { -8.9100652e-01, -4.5399050e-01 }, { -9.0814317e-01, -4.1865974e-01 }, + { -9.2387953e-01, -3.8268343e-01 }, { -9.3819134e-01, -3.4611706e-01 }, + { -9.5105652e-01, -3.0901699e-01 }, { -9.6245524e-01, -2.7144045e-01 }, + { -9.7236992e-01, -2.3344536e-01 }, { -9.8078528e-01, -1.9509032e-01 }, + { -9.8768834e-01, -1.5643447e-01 }, { -9.9306846e-01, -1.1753740e-01 }, + { -9.9691733e-01, -7.8459096e-02 }, { -9.9922904e-01, -3.9259816e-02 }, + } +}; + +static const struct lc3_fft_bf2_twiddles fft_twiddles_180 = { + .n2 = 180/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.9939083e-01, -3.4899497e-02 }, + { 9.9756405e-01, -6.9756474e-02 }, { 9.9452190e-01, -1.0452846e-01 }, + { 9.9026807e-01, -1.3917310e-01 }, { 9.8480775e-01, -1.7364818e-01 }, + { 9.7814760e-01, -2.0791169e-01 }, { 9.7029573e-01, -2.4192190e-01 }, + { 9.6126170e-01, -2.7563736e-01 }, { 9.5105652e-01, -3.0901699e-01 }, + { 9.3969262e-01, -3.4202014e-01 }, { 9.2718385e-01, -3.7460659e-01 }, + { 9.1354546e-01, -4.0673664e-01 }, { 8.9879405e-01, -4.3837115e-01 }, + { 8.8294759e-01, -4.6947156e-01 }, { 8.6602540e-01, -5.0000000e-01 }, + { 8.4804810e-01, -5.2991926e-01 }, { 8.2903757e-01, -5.5919290e-01 }, + { 8.0901699e-01, -5.8778525e-01 }, { 7.8801075e-01, -6.1566148e-01 }, + { 7.6604444e-01, -6.4278761e-01 }, { 7.4314483e-01, -6.6913061e-01 }, + { 7.1933980e-01, -6.9465837e-01 }, { 6.9465837e-01, -7.1933980e-01 }, + { 6.6913061e-01, -7.4314483e-01 }, { 6.4278761e-01, -7.6604444e-01 }, + { 6.1566148e-01, -7.8801075e-01 }, { 5.8778525e-01, -8.0901699e-01 }, + { 5.5919290e-01, -8.2903757e-01 }, { 5.2991926e-01, -8.4804810e-01 }, + { 5.0000000e-01, -8.6602540e-01 }, { 4.6947156e-01, -8.8294759e-01 }, + { 4.3837115e-01, -8.9879405e-01 }, { 4.0673664e-01, -9.1354546e-01 }, + { 3.7460659e-01, -9.2718385e-01 }, { 3.4202014e-01, -9.3969262e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 2.7563736e-01, -9.6126170e-01 }, + { 2.4192190e-01, -9.7029573e-01 }, { 2.0791169e-01, -9.7814760e-01 }, + { 1.7364818e-01, -9.8480775e-01 }, { 1.3917310e-01, -9.9026807e-01 }, + { 1.0452846e-01, -9.9452190e-01 }, { 6.9756474e-02, -9.9756405e-01 }, + { 3.4899497e-02, -9.9939083e-01 }, { 6.1232340e-17, -1.0000000e+00 }, + { -3.4899497e-02, -9.9939083e-01 }, { -6.9756474e-02, -9.9756405e-01 }, + { -1.0452846e-01, -9.9452190e-01 }, { -1.3917310e-01, -9.9026807e-01 }, + { -1.7364818e-01, -9.8480775e-01 }, { -2.0791169e-01, -9.7814760e-01 }, + { -2.4192190e-01, -9.7029573e-01 }, { -2.7563736e-01, -9.6126170e-01 }, + { -3.0901699e-01, -9.5105652e-01 }, { -3.4202014e-01, -9.3969262e-01 }, + { -3.7460659e-01, -9.2718385e-01 }, { -4.0673664e-01, -9.1354546e-01 }, + { -4.3837115e-01, -8.9879405e-01 }, { -4.6947156e-01, -8.8294759e-01 }, + { -5.0000000e-01, -8.6602540e-01 }, { -5.2991926e-01, -8.4804810e-01 }, + { -5.5919290e-01, -8.2903757e-01 }, { -5.8778525e-01, -8.0901699e-01 }, + { -6.1566148e-01, -7.8801075e-01 }, { -6.4278761e-01, -7.6604444e-01 }, + { -6.6913061e-01, -7.4314483e-01 }, { -6.9465837e-01, -7.1933980e-01 }, + { -7.1933980e-01, -6.9465837e-01 }, { -7.4314483e-01, -6.6913061e-01 }, + { -7.6604444e-01, -6.4278761e-01 }, { -7.8801075e-01, -6.1566148e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -8.2903757e-01, -5.5919290e-01 }, + { -8.4804810e-01, -5.2991926e-01 }, { -8.6602540e-01, -5.0000000e-01 }, + { -8.8294759e-01, -4.6947156e-01 }, { -8.9879405e-01, -4.3837115e-01 }, + { -9.1354546e-01, -4.0673664e-01 }, { -9.2718385e-01, -3.7460659e-01 }, + { -9.3969262e-01, -3.4202014e-01 }, { -9.5105652e-01, -3.0901699e-01 }, + { -9.6126170e-01, -2.7563736e-01 }, { -9.7029573e-01, -2.4192190e-01 }, + { -9.7814760e-01, -2.0791169e-01 }, { -9.8480775e-01, -1.7364818e-01 }, + { -9.9026807e-01, -1.3917310e-01 }, { -9.9452190e-01, -1.0452846e-01 }, + { -9.9756405e-01, -6.9756474e-02 }, { -9.9939083e-01, -3.4899497e-02 }, + } +}; + +static const struct lc3_fft_bf2_twiddles fft_twiddles_240 = { + .n2 = 240/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.9965732e-01, -2.6176948e-02 }, + { 9.9862953e-01, -5.2335956e-02 }, { 9.9691733e-01, -7.8459096e-02 }, + { 9.9452190e-01, -1.0452846e-01 }, { 9.9144486e-01, -1.3052619e-01 }, + { 9.8768834e-01, -1.5643447e-01 }, { 9.8325491e-01, -1.8223553e-01 }, + { 9.7814760e-01, -2.0791169e-01 }, { 9.7236992e-01, -2.3344536e-01 }, + { 9.6592583e-01, -2.5881905e-01 }, { 9.5881973e-01, -2.8401534e-01 }, + { 9.5105652e-01, -3.0901699e-01 }, { 9.4264149e-01, -3.3380686e-01 }, + { 9.3358043e-01, -3.5836795e-01 }, { 9.2387953e-01, -3.8268343e-01 }, + { 9.1354546e-01, -4.0673664e-01 }, { 9.0258528e-01, -4.3051110e-01 }, + { 8.9100652e-01, -4.5399050e-01 }, { 8.7881711e-01, -4.7715876e-01 }, + { 8.6602540e-01, -5.0000000e-01 }, { 8.5264016e-01, -5.2249856e-01 }, + { 8.3867057e-01, -5.4463904e-01 }, { 8.2412619e-01, -5.6640624e-01 }, + { 8.0901699e-01, -5.8778525e-01 }, { 7.9335334e-01, -6.0876143e-01 }, + { 7.7714596e-01, -6.2932039e-01 }, { 7.6040597e-01, -6.4944805e-01 }, + { 7.4314483e-01, -6.6913061e-01 }, { 7.2537437e-01, -6.8835458e-01 }, + { 7.0710678e-01, -7.0710678e-01 }, { 6.8835458e-01, -7.2537437e-01 }, + { 6.6913061e-01, -7.4314483e-01 }, { 6.4944805e-01, -7.6040597e-01 }, + { 6.2932039e-01, -7.7714596e-01 }, { 6.0876143e-01, -7.9335334e-01 }, + { 5.8778525e-01, -8.0901699e-01 }, { 5.6640624e-01, -8.2412619e-01 }, + { 5.4463904e-01, -8.3867057e-01 }, { 5.2249856e-01, -8.5264016e-01 }, + { 5.0000000e-01, -8.6602540e-01 }, { 4.7715876e-01, -8.7881711e-01 }, + { 4.5399050e-01, -8.9100652e-01 }, { 4.3051110e-01, -9.0258528e-01 }, + { 4.0673664e-01, -9.1354546e-01 }, { 3.8268343e-01, -9.2387953e-01 }, + { 3.5836795e-01, -9.3358043e-01 }, { 3.3380686e-01, -9.4264149e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 2.8401534e-01, -9.5881973e-01 }, + { 2.5881905e-01, -9.6592583e-01 }, { 2.3344536e-01, -9.7236992e-01 }, + { 2.0791169e-01, -9.7814760e-01 }, { 1.8223553e-01, -9.8325491e-01 }, + { 1.5643447e-01, -9.8768834e-01 }, { 1.3052619e-01, -9.9144486e-01 }, + { 1.0452846e-01, -9.9452190e-01 }, { 7.8459096e-02, -9.9691733e-01 }, + { 5.2335956e-02, -9.9862953e-01 }, { 2.6176948e-02, -9.9965732e-01 }, + { 2.8327694e-16, -1.0000000e+00 }, { -2.6176948e-02, -9.9965732e-01 }, + { -5.2335956e-02, -9.9862953e-01 }, { -7.8459096e-02, -9.9691733e-01 }, + { -1.0452846e-01, -9.9452190e-01 }, { -1.3052619e-01, -9.9144486e-01 }, + { -1.5643447e-01, -9.8768834e-01 }, { -1.8223553e-01, -9.8325491e-01 }, + { -2.0791169e-01, -9.7814760e-01 }, { -2.3344536e-01, -9.7236992e-01 }, + { -2.5881905e-01, -9.6592583e-01 }, { -2.8401534e-01, -9.5881973e-01 }, + { -3.0901699e-01, -9.5105652e-01 }, { -3.3380686e-01, -9.4264149e-01 }, + { -3.5836795e-01, -9.3358043e-01 }, { -3.8268343e-01, -9.2387953e-01 }, + { -4.0673664e-01, -9.1354546e-01 }, { -4.3051110e-01, -9.0258528e-01 }, + { -4.5399050e-01, -8.9100652e-01 }, { -4.7715876e-01, -8.7881711e-01 }, + { -5.0000000e-01, -8.6602540e-01 }, { -5.2249856e-01, -8.5264016e-01 }, + { -5.4463904e-01, -8.3867057e-01 }, { -5.6640624e-01, -8.2412619e-01 }, + { -5.8778525e-01, -8.0901699e-01 }, { -6.0876143e-01, -7.9335334e-01 }, + { -6.2932039e-01, -7.7714596e-01 }, { -6.4944805e-01, -7.6040597e-01 }, + { -6.6913061e-01, -7.4314483e-01 }, { -6.8835458e-01, -7.2537437e-01 }, + { -7.0710678e-01, -7.0710678e-01 }, { -7.2537437e-01, -6.8835458e-01 }, + { -7.4314483e-01, -6.6913061e-01 }, { -7.6040597e-01, -6.4944805e-01 }, + { -7.7714596e-01, -6.2932039e-01 }, { -7.9335334e-01, -6.0876143e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -8.2412619e-01, -5.6640624e-01 }, + { -8.3867057e-01, -5.4463904e-01 }, { -8.5264016e-01, -5.2249856e-01 }, + { -8.6602540e-01, -5.0000000e-01 }, { -8.7881711e-01, -4.7715876e-01 }, + { -8.9100652e-01, -4.5399050e-01 }, { -9.0258528e-01, -4.3051110e-01 }, + { -9.1354546e-01, -4.0673664e-01 }, { -9.2387953e-01, -3.8268343e-01 }, + { -9.3358043e-01, -3.5836795e-01 }, { -9.4264149e-01, -3.3380686e-01 }, + { -9.5105652e-01, -3.0901699e-01 }, { -9.5881973e-01, -2.8401534e-01 }, + { -9.6592583e-01, -2.5881905e-01 }, { -9.7236992e-01, -2.3344536e-01 }, + { -9.7814760e-01, -2.0791169e-01 }, { -9.8325491e-01, -1.8223553e-01 }, + { -9.8768834e-01, -1.5643447e-01 }, { -9.9144486e-01, -1.3052619e-01 }, + { -9.9452190e-01, -1.0452846e-01 }, { -9.9691733e-01, -7.8459096e-02 }, + { -9.9862953e-01, -5.2335956e-02 }, { -9.9965732e-01, -2.6176948e-02 }, + } +}; + +#if LC3_PLUS_HR + +static const struct lc3_fft_bf2_twiddles fft_twiddles_480 = { + .n2 = 480/2, .t = (const struct lc3_complex []){ + { 1.0000000e+00, -0.0000000e+00 }, { 9.9991433e-01, -1.3089596e-02 }, + { 9.9965732e-01, -2.6176948e-02 }, { 9.9922904e-01, -3.9259816e-02 }, + { 9.9862953e-01, -5.2335956e-02 }, { 9.9785892e-01, -6.5403129e-02 }, + { 9.9691733e-01, -7.8459096e-02 }, { 9.9580493e-01, -9.1501619e-02 }, + { 9.9452190e-01, -1.0452846e-01 }, { 9.9306846e-01, -1.1753740e-01 }, + { 9.9144486e-01, -1.3052619e-01 }, { 9.8965139e-01, -1.4349262e-01 }, + { 9.8768834e-01, -1.5643447e-01 }, { 9.8555606e-01, -1.6934950e-01 }, + { 9.8325491e-01, -1.8223553e-01 }, { 9.8078528e-01, -1.9509032e-01 }, + { 9.7814760e-01, -2.0791169e-01 }, { 9.7534232e-01, -2.2069744e-01 }, + { 9.7236992e-01, -2.3344536e-01 }, { 9.6923091e-01, -2.4615329e-01 }, + { 9.6592583e-01, -2.5881905e-01 }, { 9.6245524e-01, -2.7144045e-01 }, + { 9.5881973e-01, -2.8401534e-01 }, { 9.5501994e-01, -2.9654157e-01 }, + { 9.5105652e-01, -3.0901699e-01 }, { 9.4693013e-01, -3.2143947e-01 }, + { 9.4264149e-01, -3.3380686e-01 }, { 9.3819134e-01, -3.4611706e-01 }, + { 9.3358043e-01, -3.5836795e-01 }, { 9.2880955e-01, -3.7055744e-01 }, + { 9.2387953e-01, -3.8268343e-01 }, { 9.1879121e-01, -3.9474386e-01 }, + { 9.1354546e-01, -4.0673664e-01 }, { 9.0814317e-01, -4.1865974e-01 }, + { 9.0258528e-01, -4.3051110e-01 }, { 8.9687274e-01, -4.4228869e-01 }, + { 8.9100652e-01, -4.5399050e-01 }, { 8.8498764e-01, -4.6561452e-01 }, + { 8.7881711e-01, -4.7715876e-01 }, { 8.7249601e-01, -4.8862124e-01 }, + { 8.6602540e-01, -5.0000000e-01 }, { 8.5940641e-01, -5.1129309e-01 }, + { 8.5264016e-01, -5.2249856e-01 }, { 8.4572782e-01, -5.3361452e-01 }, + { 8.3867057e-01, -5.4463904e-01 }, { 8.3146961e-01, -5.5557023e-01 }, + { 8.2412619e-01, -5.6640624e-01 }, { 8.1664156e-01, -5.7714519e-01 }, + { 8.0901699e-01, -5.8778525e-01 }, { 8.0125381e-01, -5.9832460e-01 }, + { 7.9335334e-01, -6.0876143e-01 }, { 7.8531693e-01, -6.1909395e-01 }, + { 7.7714596e-01, -6.2932039e-01 }, { 7.6884183e-01, -6.3943900e-01 }, + { 7.6040597e-01, -6.4944805e-01 }, { 7.5183981e-01, -6.5934582e-01 }, + { 7.4314483e-01, -6.6913061e-01 }, { 7.3432251e-01, -6.7880075e-01 }, + { 7.2537437e-01, -6.8835458e-01 }, { 7.1630194e-01, -6.9779046e-01 }, + { 7.0710678e-01, -7.0710678e-01 }, { 6.9779046e-01, -7.1630194e-01 }, + { 6.8835458e-01, -7.2537437e-01 }, { 6.7880075e-01, -7.3432251e-01 }, + { 6.6913061e-01, -7.4314483e-01 }, { 6.5934582e-01, -7.5183981e-01 }, + { 6.4944805e-01, -7.6040597e-01 }, { 6.3943900e-01, -7.6884183e-01 }, + { 6.2932039e-01, -7.7714596e-01 }, { 6.1909395e-01, -7.8531693e-01 }, + { 6.0876143e-01, -7.9335334e-01 }, { 5.9832460e-01, -8.0125381e-01 }, + { 5.8778525e-01, -8.0901699e-01 }, { 5.7714519e-01, -8.1664156e-01 }, + { 5.6640624e-01, -8.2412619e-01 }, { 5.5557023e-01, -8.3146961e-01 }, + { 5.4463904e-01, -8.3867057e-01 }, { 5.3361452e-01, -8.4572782e-01 }, + { 5.2249856e-01, -8.5264016e-01 }, { 5.1129309e-01, -8.5940641e-01 }, + { 5.0000000e-01, -8.6602540e-01 }, { 4.8862124e-01, -8.7249601e-01 }, + { 4.7715876e-01, -8.7881711e-01 }, { 4.6561452e-01, -8.8498764e-01 }, + { 4.5399050e-01, -8.9100652e-01 }, { 4.4228869e-01, -8.9687274e-01 }, + { 4.3051110e-01, -9.0258528e-01 }, { 4.1865974e-01, -9.0814317e-01 }, + { 4.0673664e-01, -9.1354546e-01 }, { 3.9474386e-01, -9.1879121e-01 }, + { 3.8268343e-01, -9.2387953e-01 }, { 3.7055744e-01, -9.2880955e-01 }, + { 3.5836795e-01, -9.3358043e-01 }, { 3.4611706e-01, -9.3819134e-01 }, + { 3.3380686e-01, -9.4264149e-01 }, { 3.2143947e-01, -9.4693013e-01 }, + { 3.0901699e-01, -9.5105652e-01 }, { 2.9654157e-01, -9.5501994e-01 }, + { 2.8401534e-01, -9.5881973e-01 }, { 2.7144045e-01, -9.6245524e-01 }, + { 2.5881905e-01, -9.6592583e-01 }, { 2.4615329e-01, -9.6923091e-01 }, + { 2.3344536e-01, -9.7236992e-01 }, { 2.2069744e-01, -9.7534232e-01 }, + { 2.0791169e-01, -9.7814760e-01 }, { 1.9509032e-01, -9.8078528e-01 }, + { 1.8223553e-01, -9.8325491e-01 }, { 1.6934950e-01, -9.8555606e-01 }, + { 1.5643447e-01, -9.8768834e-01 }, { 1.4349262e-01, -9.8965139e-01 }, + { 1.3052619e-01, -9.9144486e-01 }, { 1.1753740e-01, -9.9306846e-01 }, + { 1.0452846e-01, -9.9452190e-01 }, { 9.1501619e-02, -9.9580493e-01 }, + { 7.8459096e-02, -9.9691733e-01 }, { 6.5403129e-02, -9.9785892e-01 }, + { 5.2335956e-02, -9.9862953e-01 }, { 3.9259816e-02, -9.9922904e-01 }, + { 2.6176948e-02, -9.9965732e-01 }, { 1.3089596e-02, -9.9991433e-01 }, + { 2.8327694e-16, -1.0000000e+00 }, { -1.3089596e-02, -9.9991433e-01 }, + { -2.6176948e-02, -9.9965732e-01 }, { -3.9259816e-02, -9.9922904e-01 }, + { -5.2335956e-02, -9.9862953e-01 }, { -6.5403129e-02, -9.9785892e-01 }, + { -7.8459096e-02, -9.9691733e-01 }, { -9.1501619e-02, -9.9580493e-01 }, + { -1.0452846e-01, -9.9452190e-01 }, { -1.1753740e-01, -9.9306846e-01 }, + { -1.3052619e-01, -9.9144486e-01 }, { -1.4349262e-01, -9.8965139e-01 }, + { -1.5643447e-01, -9.8768834e-01 }, { -1.6934950e-01, -9.8555606e-01 }, + { -1.8223553e-01, -9.8325491e-01 }, { -1.9509032e-01, -9.8078528e-01 }, + { -2.0791169e-01, -9.7814760e-01 }, { -2.2069744e-01, -9.7534232e-01 }, + { -2.3344536e-01, -9.7236992e-01 }, { -2.4615329e-01, -9.6923091e-01 }, + { -2.5881905e-01, -9.6592583e-01 }, { -2.7144045e-01, -9.6245524e-01 }, + { -2.8401534e-01, -9.5881973e-01 }, { -2.9654157e-01, -9.5501994e-01 }, + { -3.0901699e-01, -9.5105652e-01 }, { -3.2143947e-01, -9.4693013e-01 }, + { -3.3380686e-01, -9.4264149e-01 }, { -3.4611706e-01, -9.3819134e-01 }, + { -3.5836795e-01, -9.3358043e-01 }, { -3.7055744e-01, -9.2880955e-01 }, + { -3.8268343e-01, -9.2387953e-01 }, { -3.9474386e-01, -9.1879121e-01 }, + { -4.0673664e-01, -9.1354546e-01 }, { -4.1865974e-01, -9.0814317e-01 }, + { -4.3051110e-01, -9.0258528e-01 }, { -4.4228869e-01, -8.9687274e-01 }, + { -4.5399050e-01, -8.9100652e-01 }, { -4.6561452e-01, -8.8498764e-01 }, + { -4.7715876e-01, -8.7881711e-01 }, { -4.8862124e-01, -8.7249601e-01 }, + { -5.0000000e-01, -8.6602540e-01 }, { -5.1129309e-01, -8.5940641e-01 }, + { -5.2249856e-01, -8.5264016e-01 }, { -5.3361452e-01, -8.4572782e-01 }, + { -5.4463904e-01, -8.3867057e-01 }, { -5.5557023e-01, -8.3146961e-01 }, + { -5.6640624e-01, -8.2412619e-01 }, { -5.7714519e-01, -8.1664156e-01 }, + { -5.8778525e-01, -8.0901699e-01 }, { -5.9832460e-01, -8.0125381e-01 }, + { -6.0876143e-01, -7.9335334e-01 }, { -6.1909395e-01, -7.8531693e-01 }, + { -6.2932039e-01, -7.7714596e-01 }, { -6.3943900e-01, -7.6884183e-01 }, + { -6.4944805e-01, -7.6040597e-01 }, { -6.5934582e-01, -7.5183981e-01 }, + { -6.6913061e-01, -7.4314483e-01 }, { -6.7880075e-01, -7.3432251e-01 }, + { -6.8835458e-01, -7.2537437e-01 }, { -6.9779046e-01, -7.1630194e-01 }, + { -7.0710678e-01, -7.0710678e-01 }, { -7.1630194e-01, -6.9779046e-01 }, + { -7.2537437e-01, -6.8835458e-01 }, { -7.3432251e-01, -6.7880075e-01 }, + { -7.4314483e-01, -6.6913061e-01 }, { -7.5183981e-01, -6.5934582e-01 }, + { -7.6040597e-01, -6.4944805e-01 }, { -7.6884183e-01, -6.3943900e-01 }, + { -7.7714596e-01, -6.2932039e-01 }, { -7.8531693e-01, -6.1909395e-01 }, + { -7.9335334e-01, -6.0876143e-01 }, { -8.0125381e-01, -5.9832460e-01 }, + { -8.0901699e-01, -5.8778525e-01 }, { -8.1664156e-01, -5.7714519e-01 }, + { -8.2412619e-01, -5.6640624e-01 }, { -8.3146961e-01, -5.5557023e-01 }, + { -8.3867057e-01, -5.4463904e-01 }, { -8.4572782e-01, -5.3361452e-01 }, + { -8.5264016e-01, -5.2249856e-01 }, { -8.5940641e-01, -5.1129309e-01 }, + { -8.6602540e-01, -5.0000000e-01 }, { -8.7249601e-01, -4.8862124e-01 }, + { -8.7881711e-01, -4.7715876e-01 }, { -8.8498764e-01, -4.6561452e-01 }, + { -8.9100652e-01, -4.5399050e-01 }, { -8.9687274e-01, -4.4228869e-01 }, + { -9.0258528e-01, -4.3051110e-01 }, { -9.0814317e-01, -4.1865974e-01 }, + { -9.1354546e-01, -4.0673664e-01 }, { -9.1879121e-01, -3.9474386e-01 }, + { -9.2387953e-01, -3.8268343e-01 }, { -9.2880955e-01, -3.7055744e-01 }, + { -9.3358043e-01, -3.5836795e-01 }, { -9.3819134e-01, -3.4611706e-01 }, + { -9.4264149e-01, -3.3380686e-01 }, { -9.4693013e-01, -3.2143947e-01 }, + { -9.5105652e-01, -3.0901699e-01 }, { -9.5501994e-01, -2.9654157e-01 }, + { -9.5881973e-01, -2.8401534e-01 }, { -9.6245524e-01, -2.7144045e-01 }, + { -9.6592583e-01, -2.5881905e-01 }, { -9.6923091e-01, -2.4615329e-01 }, + { -9.7236992e-01, -2.3344536e-01 }, { -9.7534232e-01, -2.2069744e-01 }, + { -9.7814760e-01, -2.0791169e-01 }, { -9.8078528e-01, -1.9509032e-01 }, + { -9.8325491e-01, -1.8223553e-01 }, { -9.8555606e-01, -1.6934950e-01 }, + { -9.8768834e-01, -1.5643447e-01 }, { -9.8965139e-01, -1.4349262e-01 }, + { -9.9144486e-01, -1.3052619e-01 }, { -9.9306846e-01, -1.1753740e-01 }, + { -9.9452190e-01, -1.0452846e-01 }, { -9.9580493e-01, -9.1501619e-02 }, + { -9.9691733e-01, -7.8459096e-02 }, { -9.9785892e-01, -6.5403129e-02 }, + { -9.9862953e-01, -5.2335956e-02 }, { -9.9922904e-01, -3.9259816e-02 }, + { -9.9965732e-01, -2.6176948e-02 }, { -9.9991433e-01, -1.3089596e-02 }, + } +}; + +#endif /* LC3_PLUS_HR */ + +const struct lc3_fft_bf2_twiddles *lc3_fft_twiddles_bf2[][3] = { + { &fft_twiddles_10 , &fft_twiddles_30 , &fft_twiddles_90 }, + { &fft_twiddles_20 , &fft_twiddles_60 , &fft_twiddles_180 }, + { &fft_twiddles_40 , &fft_twiddles_120 }, + { &fft_twiddles_80 , &fft_twiddles_240 }, + { &fft_twiddles_160, LC3_IF_PLUS_HR(&fft_twiddles_480, NULL) } +}; + + +/** + * MDCT Rotation twiddles + * + * 2Pi (n + 1/8) / N + * W[n] = e * sqrt( sqrt( 4/N ) ), n = [0..N/4-1] + */ + +#if LC3_PLUS + +static const struct lc3_mdct_rot_def mdct_rot_40 = { + .n4 = 40/4, .w = (const struct lc3_complex []){ + { 5.6223293e-01, 1.1040837e-02 }, { 5.5358374e-01, 9.8857513e-02 }, + { 5.3130348e-01, 1.8423999e-01 }, { 4.9594077e-01, 2.6508587e-01 }, + { 4.4836635e-01, 3.3940445e-01 }, { 3.8975167e-01, 4.0536577e-01 }, + { 3.2154000e-01, 4.6134563e-01 }, { 2.4541095e-01, 5.0596564e-01 }, + { 1.6323907e-01, 5.3812710e-01 }, { 7.7047702e-02, 5.5703808e-01 }, + } +}; + +static const struct lc3_mdct_rot_def mdct_rot_80 = { + .n4 = 80/4, .w = (const struct lc3_complex []){ + { 4.7284802e-01, 4.6423237e-03 }, { 4.7102615e-01, 4.1727241e-02 }, + { 4.6630025e-01, 7.8554895e-02 }, { 4.5869946e-01, 1.1489823e-01 }, + { 4.4827063e-01, 1.5053319e-01 }, { 4.3507806e-01, 1.8524005e-01 }, + { 4.1920310e-01, 2.1880485e-01 }, { 4.0074360e-01, 2.5102064e-01 }, + { 3.7981339e-01, 2.8168881e-01 }, { 3.5654150e-01, 3.1062027e-01 }, + { 3.3107142e-01, 3.3763666e-01 }, { 3.0356017e-01, 3.6257140e-01 }, + { 2.7417737e-01, 3.8527077e-01 }, { 2.4310418e-01, 4.0559482e-01 }, + { 2.1053217e-01, 4.2341824e-01 }, { 1.7666215e-01, 4.3863114e-01 }, + { 1.4170296e-01, 4.5113974e-01 }, { 1.0587012e-01, 4.6086692e-01 }, + { 6.9384558e-02, 4.6775269e-01 }, { 3.2471215e-02, 4.7175462e-01 }, + } +}; + +#endif /* LC3_PLUS */ + +static const struct lc3_mdct_rot_def mdct_rot_120 = { + .n4 = 120/4, .w = (const struct lc3_complex []){ + { 4.2727785e-01, 2.7965670e-03 }, { 4.2654592e-01, 2.5154729e-02 }, + { 4.2464486e-01, 4.7443945e-02 }, { 4.2157988e-01, 6.9603119e-02 }, + { 4.1735937e-01, 9.1571516e-02 }, { 4.1199491e-01, 1.1328892e-01 }, + { 4.0550120e-01, 1.3469581e-01 }, { 3.9789604e-01, 1.5573351e-01 }, + { 3.8920028e-01, 1.7634435e-01 }, { 3.7943774e-01, 1.9647185e-01 }, + { 3.6863519e-01, 2.1606083e-01 }, { 3.5682224e-01, 2.3505760e-01 }, + { 3.4403126e-01, 2.5341009e-01 }, { 3.3029732e-01, 2.7106801e-01 }, + { 3.1565806e-01, 2.8798294e-01 }, { 3.0015360e-01, 3.0410854e-01 }, + { 2.8382644e-01, 3.1940060e-01 }, { 2.6672133e-01, 3.3381720e-01 }, + { 2.4888515e-01, 3.4731883e-01 }, { 2.3036680e-01, 3.5986848e-01 }, + { 2.1121703e-01, 3.7143176e-01 }, { 1.9148833e-01, 3.8197697e-01 }, + { 1.7123477e-01, 3.9147521e-01 }, { 1.5051187e-01, 3.9990044e-01 }, + { 1.2937643e-01, 4.0722957e-01 }, { 1.0788637e-01, 4.1344252e-01 }, + { 8.6100606e-02, 4.1852225e-01 }, { 6.4078846e-02, 4.2245483e-01 }, + { 4.1881450e-02, 4.2522950e-01 }, { 1.9569261e-02, 4.2683865e-01 }, + } +}; + +static const struct lc3_mdct_rot_def mdct_rot_160 = { + .n4 = 160/4, .w = (const struct lc3_complex []){ + { 3.9763057e-01, 1.9518802e-03 }, { 3.9724738e-01, 1.7561278e-02 }, + { 3.9625167e-01, 3.3143598e-02 }, { 3.9464496e-01, 4.8674813e-02 }, + { 3.9242974e-01, 6.4130975e-02 }, { 3.8960942e-01, 7.9488252e-02 }, + { 3.8618835e-01, 9.4722964e-02 }, { 3.8217181e-01, 1.0981162e-01 }, + { 3.7756598e-01, 1.2473095e-01 }, { 3.7237798e-01, 1.3945796e-01 }, + { 3.6661580e-01, 1.5396993e-01 }, { 3.6028832e-01, 1.6824450e-01 }, + { 3.5340530e-01, 1.8225964e-01 }, { 3.4597736e-01, 1.9599375e-01 }, + { 3.3801594e-01, 2.0942566e-01 }, { 3.2953333e-01, 2.2253464e-01 }, + { 3.2054261e-01, 2.3530049e-01 }, { 3.1105762e-01, 2.4770353e-01 }, + { 3.0109302e-01, 2.5972462e-01 }, { 2.9066414e-01, 2.7134524e-01 }, + { 2.7978709e-01, 2.8254746e-01 }, { 2.6847862e-01, 2.9331402e-01 }, + { 2.5675618e-01, 3.0362831e-01 }, { 2.4463784e-01, 3.1347442e-01 }, + { 2.3214228e-01, 3.2283718e-01 }, { 2.1928878e-01, 3.3170215e-01 }, + { 2.0609715e-01, 3.4005565e-01 }, { 1.9258774e-01, 3.4788482e-01 }, + { 1.7878136e-01, 3.5517757e-01 }, { 1.6469932e-01, 3.6192266e-01 }, + { 1.5036333e-01, 3.6810970e-01 }, { 1.3579549e-01, 3.7372914e-01 }, + { 1.2101826e-01, 3.7877231e-01 }, { 1.0605442e-01, 3.8323145e-01 }, + { 9.0927064e-02, 3.8709967e-01 }, { 7.5659501e-02, 3.9037101e-01 }, + { 6.0275277e-02, 3.9304042e-01 }, { 4.4798112e-02, 3.9510380e-01 }, + { 2.9251872e-02, 3.9655795e-01 }, { 1.3660528e-02, 3.9740065e-01 }, + } +}; + +static const struct lc3_mdct_rot_def mdct_rot_240 = { + .n4 = 240/4, .w = (const struct lc3_complex []){ + { 3.5930219e-01, 1.1758179e-03 }, { 3.5914828e-01, 1.0580850e-02 }, + { 3.5874824e-01, 1.9978630e-02 }, { 3.5810233e-01, 2.9362718e-02 }, + { 3.5721099e-01, 3.8726682e-02 }, { 3.5607483e-01, 4.8064105e-02 }, + { 3.5469464e-01, 5.7368587e-02 }, { 3.5307136e-01, 6.6633752e-02 }, + { 3.5120611e-01, 7.5853249e-02 }, { 3.4910015e-01, 8.5020760e-02 }, + { 3.4675494e-01, 9.4130002e-02 }, { 3.4417208e-01, 1.0317473e-01 }, + { 3.4135334e-01, 1.1214875e-01 }, { 3.3830065e-01, 1.2104591e-01 }, + { 3.3501611e-01, 1.2986011e-01 }, { 3.3150197e-01, 1.3858531e-01 }, + { 3.2776063e-01, 1.4721553e-01 }, { 3.2379466e-01, 1.5574485e-01 }, + { 3.1960678e-01, 1.6416744e-01 }, { 3.1519986e-01, 1.7247752e-01 }, + { 3.1057691e-01, 1.8066938e-01 }, { 3.0574111e-01, 1.8873743e-01 }, + { 3.0069577e-01, 1.9667612e-01 }, { 2.9544435e-01, 2.0448002e-01 }, + { 2.8999045e-01, 2.1214378e-01 }, { 2.8433780e-01, 2.1966215e-01 }, + { 2.7849028e-01, 2.2702998e-01 }, { 2.7245189e-01, 2.3424220e-01 }, + { 2.6622679e-01, 2.4129389e-01 }, { 2.5981922e-01, 2.4818021e-01 }, + { 2.5323358e-01, 2.5489644e-01 }, { 2.4647440e-01, 2.6143798e-01 }, + { 2.3954629e-01, 2.6780034e-01 }, { 2.3245401e-01, 2.7397916e-01 }, + { 2.2520241e-01, 2.7997021e-01 }, { 2.1779647e-01, 2.8576938e-01 }, + { 2.1024127e-01, 2.9137270e-01 }, { 2.0254198e-01, 2.9677633e-01 }, + { 1.9470387e-01, 3.0197657e-01 }, { 1.8673233e-01, 3.0696984e-01 }, + { 1.7863281e-01, 3.1175273e-01 }, { 1.7041086e-01, 3.1632196e-01 }, + { 1.6207212e-01, 3.2067440e-01 }, { 1.5362230e-01, 3.2480707e-01 }, + { 1.4506720e-01, 3.2871713e-01 }, { 1.3641268e-01, 3.3240190e-01 }, + { 1.2766467e-01, 3.3585887e-01 }, { 1.1882916e-01, 3.3908565e-01 }, + { 1.0991221e-01, 3.4208003e-01 }, { 1.0091994e-01, 3.4483998e-01 }, + { 9.1858496e-02, 3.4736359e-01 }, { 8.2734100e-02, 3.4964913e-01 }, + { 7.3553002e-02, 3.5169504e-01 }, { 6.4321494e-02, 3.5349992e-01 }, + { 5.5045904e-02, 3.5506252e-01 }, { 4.5732588e-02, 3.5638178e-01 }, + { 3.6387929e-02, 3.5745680e-01 }, { 2.7018332e-02, 3.5828683e-01 }, + { 1.7630217e-02, 3.5887131e-01 }, { 8.2300199e-03, 3.5920984e-01 }, + } +}; + +static const struct lc3_mdct_rot_def mdct_rot_320 = { + .n4 = 320/4, .w = (const struct lc3_complex []){ + { 3.3436915e-01, 8.2066700e-04 }, { 3.3428858e-01, 7.3854098e-03 }, + { 3.3407914e-01, 1.3947305e-02 }, { 3.3374091e-01, 2.0503824e-02 }, + { 3.3327401e-01, 2.7052438e-02 }, { 3.3267863e-01, 3.3590623e-02 }, + { 3.3195499e-01, 4.0115858e-02 }, { 3.3110338e-01, 4.6625627e-02 }, + { 3.3012413e-01, 5.3117422e-02 }, { 3.2901760e-01, 5.9588738e-02 }, + { 3.2778423e-01, 6.6037082e-02 }, { 3.2642450e-01, 7.2459968e-02 }, + { 3.2493892e-01, 7.8854919e-02 }, { 3.2332807e-01, 8.5219469e-02 }, + { 3.2159257e-01, 9.1551166e-02 }, { 3.1973310e-01, 9.7847569e-02 }, + { 3.1775035e-01, 1.0410625e-01 }, { 3.1564512e-01, 1.1032479e-01 }, + { 3.1341819e-01, 1.1650081e-01 }, { 3.1107043e-01, 1.2263191e-01 }, + { 3.0860275e-01, 1.2871573e-01 }, { 3.0601610e-01, 1.3474993e-01 }, + { 3.0331148e-01, 1.4073218e-01 }, { 3.0048992e-01, 1.4666018e-01 }, + { 2.9755251e-01, 1.5253164e-01 }, { 2.9450040e-01, 1.5834429e-01 }, + { 2.9133475e-01, 1.6409590e-01 }, { 2.8805678e-01, 1.6978424e-01 }, + { 2.8466777e-01, 1.7540713e-01 }, { 2.8116900e-01, 1.8096240e-01 }, + { 2.7756185e-01, 1.8644790e-01 }, { 2.7384768e-01, 1.9186153e-01 }, + { 2.7002795e-01, 1.9720119e-01 }, { 2.6610411e-01, 2.0246482e-01 }, + { 2.6207768e-01, 2.0765040e-01 }, { 2.5795022e-01, 2.1275592e-01 }, + { 2.5372331e-01, 2.1777943e-01 }, { 2.4939859e-01, 2.2271898e-01 }, + { 2.4497772e-01, 2.2757266e-01 }, { 2.4046241e-01, 2.3233861e-01 }, + { 2.3585439e-01, 2.3701499e-01 }, { 2.3115545e-01, 2.4159999e-01 }, + { 2.2636739e-01, 2.4609186e-01 }, { 2.2149206e-01, 2.5048885e-01 }, + { 2.1653135e-01, 2.5478927e-01 }, { 2.1148716e-01, 2.5899147e-01 }, + { 2.0636143e-01, 2.6309382e-01 }, { 2.0115615e-01, 2.6709474e-01 }, + { 1.9587332e-01, 2.7099270e-01 }, { 1.9051498e-01, 2.7478618e-01 }, + { 1.8508318e-01, 2.7847372e-01 }, { 1.7958004e-01, 2.8205391e-01 }, + { 1.7400766e-01, 2.8552536e-01 }, { 1.6836821e-01, 2.8888674e-01 }, + { 1.6266384e-01, 2.9213674e-01 }, { 1.5689676e-01, 2.9527412e-01 }, + { 1.5106920e-01, 2.9829767e-01 }, { 1.4518339e-01, 3.0120621e-01 }, + { 1.3924162e-01, 3.0399864e-01 }, { 1.3324616e-01, 3.0667387e-01 }, + { 1.2719933e-01, 3.0923087e-01 }, { 1.2110347e-01, 3.1166865e-01 }, + { 1.1496092e-01, 3.1398628e-01 }, { 1.0877405e-01, 3.1618287e-01 }, + { 1.0254525e-01, 3.1825755e-01 }, { 9.6276910e-02, 3.2020955e-01 }, + { 8.9971456e-02, 3.2203810e-01 }, { 8.3631316e-02, 3.2374249e-01 }, + { 7.7258935e-02, 3.2532208e-01 }, { 7.0856769e-02, 3.2677625e-01 }, + { 6.4427286e-02, 3.2810444e-01 }, { 5.7972965e-02, 3.2930614e-01 }, + { 5.1496295e-02, 3.3038089e-01 }, { 4.4999772e-02, 3.3132827e-01 }, + { 3.8485901e-02, 3.3214791e-01 }, { 3.1957192e-02, 3.3283951e-01 }, + { 2.5416164e-02, 3.3340279e-01 }, { 1.8865337e-02, 3.3383753e-01 }, + { 1.2307237e-02, 3.3414358e-01 }, { 5.7443922e-03, 3.3432081e-01 }, + } +}; + +static const struct lc3_mdct_rot_def mdct_rot_360 = { + .n4 = 360/4, .w = (const struct lc3_complex []){ + { 3.2466714e-01, 7.0831495e-04 }, { 3.2460533e-01, 6.3744300e-03 }, + { 3.2444464e-01, 1.2038603e-02 }, { 3.2418513e-01, 1.7699110e-02 }, + { 3.2382686e-01, 2.3354225e-02 }, { 3.2336995e-01, 2.9002226e-02 }, + { 3.2281454e-01, 3.4641392e-02 }, { 3.2216080e-01, 4.0270007e-02 }, + { 3.2140893e-01, 4.5886355e-02 }, { 3.2055915e-01, 5.1488725e-02 }, + { 3.1961172e-01, 5.7075412e-02 }, { 3.1856694e-01, 6.2644713e-02 }, + { 3.1742512e-01, 6.8194931e-02 }, { 3.1618661e-01, 7.3724377e-02 }, + { 3.1485178e-01, 7.9231366e-02 }, { 3.1342105e-01, 8.4714220e-02 }, + { 3.1189485e-01, 9.0171269e-02 }, { 3.1027364e-01, 9.5600851e-02 }, + { 3.0855792e-01, 1.0100131e-01 }, { 3.0674821e-01, 1.0637101e-01 }, + { 3.0484506e-01, 1.1170830e-01 }, { 3.0284905e-01, 1.1701157e-01 }, + { 3.0076079e-01, 1.2227919e-01 }, { 2.9858092e-01, 1.2750957e-01 }, + { 2.9631010e-01, 1.3270110e-01 }, { 2.9394901e-01, 1.3785221e-01 }, + { 2.9149839e-01, 1.4296134e-01 }, { 2.8895897e-01, 1.4802691e-01 }, + { 2.8633154e-01, 1.5304740e-01 }, { 2.8361688e-01, 1.5802126e-01 }, + { 2.8081584e-01, 1.6294699e-01 }, { 2.7792925e-01, 1.6782308e-01 }, + { 2.7495800e-01, 1.7264806e-01 }, { 2.7190300e-01, 1.7742044e-01 }, + { 2.6876518e-01, 1.8213878e-01 }, { 2.6554548e-01, 1.8680164e-01 }, + { 2.6224490e-01, 1.9140760e-01 }, { 2.5886443e-01, 1.9595525e-01 }, + { 2.5540512e-01, 2.0044321e-01 }, { 2.5186800e-01, 2.0487012e-01 }, + { 2.4825416e-01, 2.0923462e-01 }, { 2.4456471e-01, 2.1353538e-01 }, + { 2.4080075e-01, 2.1777110e-01 }, { 2.3696345e-01, 2.2194049e-01 }, + { 2.3305396e-01, 2.2604227e-01 }, { 2.2907348e-01, 2.3007519e-01 }, + { 2.2502323e-01, 2.3403803e-01 }, { 2.2090443e-01, 2.3792959e-01 }, + { 2.1671834e-01, 2.4174866e-01 }, { 2.1246624e-01, 2.4549410e-01 }, + { 2.0814942e-01, 2.4916476e-01 }, { 2.0376919e-01, 2.5275952e-01 }, + { 1.9932689e-01, 2.5627728e-01 }, { 1.9482388e-01, 2.5971698e-01 }, + { 1.9026152e-01, 2.6307757e-01 }, { 1.8564121e-01, 2.6635803e-01 }, + { 1.8096434e-01, 2.6955734e-01 }, { 1.7623236e-01, 2.7267455e-01 }, + { 1.7144669e-01, 2.7570870e-01 }, { 1.6660880e-01, 2.7865887e-01 }, + { 1.6172015e-01, 2.8152415e-01 }, { 1.5678225e-01, 2.8430368e-01 }, + { 1.5179659e-01, 2.8699661e-01 }, { 1.4676469e-01, 2.8960211e-01 }, + { 1.4168808e-01, 2.9211940e-01 }, { 1.3656831e-01, 2.9454771e-01 }, + { 1.3140695e-01, 2.9688629e-01 }, { 1.2620555e-01, 2.9913444e-01 }, + { 1.2096571e-01, 3.0129147e-01 }, { 1.1568903e-01, 3.0335673e-01 }, + { 1.1037710e-01, 3.0532958e-01 }, { 1.0503156e-01, 3.0720942e-01 }, + { 9.9654017e-02, 3.0899568e-01 }, { 9.4246121e-02, 3.1068782e-01 }, + { 8.8809517e-02, 3.1228533e-01 }, { 8.3345860e-02, 3.1378770e-01 }, + { 7.7856816e-02, 3.1519450e-01 }, { 7.2344055e-02, 3.1650528e-01 }, + { 6.6809258e-02, 3.1771965e-01 }, { 6.1254110e-02, 3.1883725e-01 }, + { 5.5680304e-02, 3.1985772e-01 }, { 5.0089536e-02, 3.2078076e-01 }, + { 4.4483511e-02, 3.2160608e-01 }, { 3.8863936e-02, 3.2233345e-01 }, + { 3.3232523e-02, 3.2296262e-01 }, { 2.7590986e-02, 3.2349342e-01 }, + { 2.1941045e-02, 3.2392568e-01 }, { 1.6284421e-02, 3.2425927e-01 }, + { 1.0622836e-02, 3.2449408e-01 }, { 4.9580159e-03, 3.2463006e-01 }, + } +}; + +static const struct lc3_mdct_rot_def mdct_rot_480 = { + .n4 = 480/4, .w = (const struct lc3_complex []){ + { 3.0213714e-01, 4.9437117e-04 }, { 3.0210478e-01, 4.4491817e-03 }, + { 3.0202066e-01, 8.4032299e-03 }, { 3.0188479e-01, 1.2355838e-02 }, + { 3.0169719e-01, 1.6306330e-02 }, { 3.0145790e-01, 2.0254027e-02 }, + { 3.0116696e-01, 2.4198254e-02 }, { 3.0082441e-01, 2.8138334e-02 }, + { 3.0043032e-01, 3.2073593e-02 }, { 2.9998475e-01, 3.6003357e-02 }, + { 2.9948778e-01, 3.9926952e-02 }, { 2.9893950e-01, 4.3843705e-02 }, + { 2.9833999e-01, 4.7752946e-02 }, { 2.9768936e-01, 5.1654004e-02 }, + { 2.9698773e-01, 5.5546213e-02 }, { 2.9623521e-01, 5.9428903e-02 }, + { 2.9543193e-01, 6.3301411e-02 }, { 2.9457803e-01, 6.7163072e-02 }, + { 2.9367365e-01, 7.1013225e-02 }, { 2.9271896e-01, 7.4851211e-02 }, + { 2.9171411e-01, 7.8676371e-02 }, { 2.9065928e-01, 8.2488050e-02 }, + { 2.8955464e-01, 8.6285595e-02 }, { 2.8840039e-01, 9.0068356e-02 }, + { 2.8719672e-01, 9.3835684e-02 }, { 2.8594385e-01, 9.7586934e-02 }, + { 2.8464198e-01, 1.0132146e-01 }, { 2.8329133e-01, 1.0503863e-01 }, + { 2.8189215e-01, 1.0873780e-01 }, { 2.8044466e-01, 1.1241834e-01 }, + { 2.7894913e-01, 1.1607962e-01 }, { 2.7740579e-01, 1.1972100e-01 }, + { 2.7581493e-01, 1.2334187e-01 }, { 2.7417680e-01, 1.2694161e-01 }, + { 2.7249170e-01, 1.3051960e-01 }, { 2.7075991e-01, 1.3407523e-01 }, + { 2.6898172e-01, 1.3760788e-01 }, { 2.6715744e-01, 1.4111695e-01 }, + { 2.6528739e-01, 1.4460184e-01 }, { 2.6337188e-01, 1.4806196e-01 }, + { 2.6141125e-01, 1.5149671e-01 }, { 2.5940582e-01, 1.5490549e-01 }, + { 2.5735595e-01, 1.5828774e-01 }, { 2.5526198e-01, 1.6164286e-01 }, + { 2.5312427e-01, 1.6497029e-01 }, { 2.5094319e-01, 1.6826945e-01 }, + { 2.4871911e-01, 1.7153978e-01 }, { 2.4645242e-01, 1.7478072e-01 }, + { 2.4414349e-01, 1.7799171e-01 }, { 2.4179274e-01, 1.8117220e-01 }, + { 2.3940055e-01, 1.8432165e-01 }, { 2.3696735e-01, 1.8743951e-01 }, + { 2.3449354e-01, 1.9052526e-01 }, { 2.3197955e-01, 1.9357836e-01 }, + { 2.2942581e-01, 1.9659830e-01 }, { 2.2683276e-01, 1.9958454e-01 }, + { 2.2420085e-01, 2.0253659e-01 }, { 2.2153052e-01, 2.0545394e-01 }, + { 2.1882223e-01, 2.0833608e-01 }, { 2.1607645e-01, 2.1118253e-01 }, + { 2.1329364e-01, 2.1399279e-01 }, { 2.1047429e-01, 2.1676638e-01 }, + { 2.0761888e-01, 2.1950284e-01 }, { 2.0472788e-01, 2.2220168e-01 }, + { 2.0180182e-01, 2.2486245e-01 }, { 1.9884117e-01, 2.2748469e-01 }, + { 1.9584645e-01, 2.3006795e-01 }, { 1.9281818e-01, 2.3261179e-01 }, + { 1.8975686e-01, 2.3511577e-01 }, { 1.8666303e-01, 2.3757947e-01 }, + { 1.8353722e-01, 2.4000246e-01 }, { 1.8037996e-01, 2.4238433e-01 }, + { 1.7719180e-01, 2.4472466e-01 }, { 1.7397327e-01, 2.4702306e-01 }, + { 1.7072493e-01, 2.4927914e-01 }, { 1.6744734e-01, 2.5149250e-01 }, + { 1.6414106e-01, 2.5366278e-01 }, { 1.6080666e-01, 2.5578958e-01 }, + { 1.5744470e-01, 2.5787256e-01 }, { 1.5405576e-01, 2.5991136e-01 }, + { 1.5064043e-01, 2.6190562e-01 }, { 1.4719929e-01, 2.6385500e-01 }, + { 1.4373292e-01, 2.6575918e-01 }, { 1.4024192e-01, 2.6761782e-01 }, + { 1.3672690e-01, 2.6943060e-01 }, { 1.3318845e-01, 2.7119722e-01 }, + { 1.2962718e-01, 2.7291736e-01 }, { 1.2604369e-01, 2.7459075e-01 }, + { 1.2243861e-01, 2.7621709e-01 }, { 1.1881255e-01, 2.7779609e-01 }, + { 1.1516614e-01, 2.7932750e-01 }, { 1.1149999e-01, 2.8081105e-01 }, + { 1.0781473e-01, 2.8224648e-01 }, { 1.0411100e-01, 2.8363355e-01 }, + { 1.0038943e-01, 2.8497202e-01 }, { 9.6650664e-02, 2.8626167e-01 }, + { 9.2895335e-02, 2.8750226e-01 }, { 8.9124088e-02, 2.8869359e-01 }, + { 8.5337570e-02, 2.8983546e-01 }, { 8.1536430e-02, 2.9092766e-01 }, + { 7.7721319e-02, 2.9197001e-01 }, { 7.3892891e-02, 2.9296234e-01 }, + { 7.0051802e-02, 2.9390447e-01 }, { 6.6198710e-02, 2.9479624e-01 }, + { 6.2334275e-02, 2.9563750e-01 }, { 5.8459159e-02, 2.9642810e-01 }, + { 5.4574027e-02, 2.9716791e-01 }, { 5.0679543e-02, 2.9785681e-01 }, + { 4.6776376e-02, 2.9849466e-01 }, { 4.2865195e-02, 2.9908137e-01 }, + { 3.8946668e-02, 2.9961684e-01 }, { 3.5021468e-02, 3.0010097e-01 }, + { 3.1090267e-02, 3.0053367e-01 }, { 2.7153740e-02, 3.0091488e-01 }, + { 2.3212559e-02, 3.0124454e-01 }, { 1.9267401e-02, 3.0152257e-01 }, + { 1.5318942e-02, 3.0174894e-01 }, { 1.1367858e-02, 3.0192361e-01 }, + { 7.4148264e-03, 3.0204654e-01 }, { 3.4605241e-03, 3.0211772e-01 }, + } +}; + +static const struct lc3_mdct_rot_def mdct_rot_640 = { + .n4 = 640/4, .w = (const struct lc3_complex []){ + { 2.8117045e-01, 3.4504823e-04 }, { 2.8115351e-01, 3.1053717e-03 }, + { 2.8110948e-01, 5.8653959e-03 }, { 2.8103835e-01, 8.6248547e-03 }, + { 2.8094013e-01, 1.1383482e-02 }, { 2.8081484e-01, 1.4141013e-02 }, + { 2.8066248e-01, 1.6897180e-02 }, { 2.8048307e-01, 1.9651719e-02 }, + { 2.8027662e-01, 2.2404364e-02 }, { 2.8004317e-01, 2.5154849e-02 }, + { 2.7978272e-01, 2.7902910e-02 }, { 2.7949530e-01, 3.0648282e-02 }, + { 2.7918095e-01, 3.3390700e-02 }, { 2.7883969e-01, 3.6129899e-02 }, + { 2.7847155e-01, 3.8865616e-02 }, { 2.7807658e-01, 4.1597587e-02 }, + { 2.7765480e-01, 4.4325549e-02 }, { 2.7720626e-01, 4.7049239e-02 }, + { 2.7673100e-01, 4.9768394e-02 }, { 2.7622908e-01, 5.2482752e-02 }, + { 2.7570052e-01, 5.5192052e-02 }, { 2.7514540e-01, 5.7896032e-02 }, + { 2.7456376e-01, 6.0594433e-02 }, { 2.7395565e-01, 6.3286992e-02 }, + { 2.7332114e-01, 6.5973453e-02 }, { 2.7266028e-01, 6.8653554e-02 }, + { 2.7197315e-01, 7.1327039e-02 }, { 2.7125980e-01, 7.3993649e-02 }, + { 2.7052031e-01, 7.6653127e-02 }, { 2.6975475e-01, 7.9305217e-02 }, + { 2.6896318e-01, 8.1949664e-02 }, { 2.6814570e-01, 8.4586212e-02 }, + { 2.6730236e-01, 8.7214608e-02 }, { 2.6643327e-01, 8.9834598e-02 }, + { 2.6553849e-01, 9.2445929e-02 }, { 2.6461813e-01, 9.5048350e-02 }, + { 2.6367225e-01, 9.7641610e-02 }, { 2.6270097e-01, 1.0022546e-01 }, + { 2.6170436e-01, 1.0279965e-01 }, { 2.6068253e-01, 1.0536393e-01 }, + { 2.5963558e-01, 1.0791806e-01 }, { 2.5856360e-01, 1.1046178e-01 }, + { 2.5746670e-01, 1.1299486e-01 }, { 2.5634499e-01, 1.1551705e-01 }, + { 2.5519857e-01, 1.1802810e-01 }, { 2.5402755e-01, 1.2052778e-01 }, + { 2.5283205e-01, 1.2301584e-01 }, { 2.5161218e-01, 1.2549204e-01 }, + { 2.5036806e-01, 1.2795615e-01 }, { 2.4909981e-01, 1.3040793e-01 }, + { 2.4780754e-01, 1.3284714e-01 }, { 2.4649140e-01, 1.3527354e-01 }, + { 2.4515150e-01, 1.3768691e-01 }, { 2.4378797e-01, 1.4008700e-01 }, + { 2.4240094e-01, 1.4247360e-01 }, { 2.4099055e-01, 1.4484646e-01 }, + { 2.3955693e-01, 1.4720536e-01 }, { 2.3810023e-01, 1.4955007e-01 }, + { 2.3662057e-01, 1.5188037e-01 }, { 2.3511811e-01, 1.5419603e-01 }, + { 2.3359299e-01, 1.5649683e-01 }, { 2.3204535e-01, 1.5878255e-01 }, + { 2.3047535e-01, 1.6105296e-01 }, { 2.2888313e-01, 1.6330785e-01 }, + { 2.2726886e-01, 1.6554699e-01 }, { 2.2563268e-01, 1.6777019e-01 }, + { 2.2397475e-01, 1.6997721e-01 }, { 2.2229524e-01, 1.7216785e-01 }, + { 2.2059430e-01, 1.7434190e-01 }, { 2.1887210e-01, 1.7649914e-01 }, + { 2.1712880e-01, 1.7863937e-01 }, { 2.1536458e-01, 1.8076239e-01 }, + { 2.1357960e-01, 1.8286798e-01 }, { 2.1177403e-01, 1.8495594e-01 }, + { 2.0994805e-01, 1.8702608e-01 }, { 2.0810184e-01, 1.8907820e-01 }, + { 2.0623557e-01, 1.9111209e-01 }, { 2.0434942e-01, 1.9312756e-01 }, + { 2.0244358e-01, 1.9512442e-01 }, { 2.0051823e-01, 1.9710247e-01 }, + { 1.9857355e-01, 1.9906152e-01 }, { 1.9660973e-01, 2.0100139e-01 }, + { 1.9462696e-01, 2.0292188e-01 }, { 1.9262543e-01, 2.0482282e-01 }, + { 1.9060533e-01, 2.0670401e-01 }, { 1.8856687e-01, 2.0856528e-01 }, + { 1.8651023e-01, 2.1040645e-01 }, { 1.8443562e-01, 2.1222734e-01 }, + { 1.8234322e-01, 2.1402778e-01 }, { 1.8023326e-01, 2.1580759e-01 }, + { 1.7810592e-01, 2.1756659e-01 }, { 1.7596142e-01, 2.1930463e-01 }, + { 1.7379995e-01, 2.2102153e-01 }, { 1.7162174e-01, 2.2271713e-01 }, + { 1.6942698e-01, 2.2439126e-01 }, { 1.6721590e-01, 2.2604377e-01 }, + { 1.6498869e-01, 2.2767449e-01 }, { 1.6274559e-01, 2.2928326e-01 }, + { 1.6048680e-01, 2.3086994e-01 }, { 1.5821254e-01, 2.3243436e-01 }, + { 1.5592304e-01, 2.3397638e-01 }, { 1.5361850e-01, 2.3549585e-01 }, + { 1.5129916e-01, 2.3699263e-01 }, { 1.4896524e-01, 2.3846656e-01 }, + { 1.4661696e-01, 2.3991751e-01 }, { 1.4425454e-01, 2.4134533e-01 }, + { 1.4187823e-01, 2.4274989e-01 }, { 1.3948824e-01, 2.4413106e-01 }, + { 1.3708480e-01, 2.4548869e-01 }, { 1.3466815e-01, 2.4682267e-01 }, + { 1.3223853e-01, 2.4813285e-01 }, { 1.2979616e-01, 2.4941912e-01 }, + { 1.2734127e-01, 2.5068135e-01 }, { 1.2487412e-01, 2.5191942e-01 }, + { 1.2239493e-01, 2.5313321e-01 }, { 1.1990394e-01, 2.5432260e-01 }, + { 1.1740139e-01, 2.5548748e-01 }, { 1.1488753e-01, 2.5662774e-01 }, + { 1.1236260e-01, 2.5774326e-01 }, { 1.0982684e-01, 2.5883394e-01 }, + { 1.0728049e-01, 2.5989967e-01 }, { 1.0472380e-01, 2.6094035e-01 }, + { 1.0215702e-01, 2.6195588e-01 }, { 9.9580393e-02, 2.6294617e-01 }, + { 9.6994168e-02, 2.6391111e-01 }, { 9.4398594e-02, 2.6485061e-01 }, + { 9.1793922e-02, 2.6576459e-01 }, { 8.9180402e-02, 2.6665295e-01 }, + { 8.6558287e-02, 2.6751562e-01 }, { 8.3927830e-02, 2.6835249e-01 }, + { 8.1289283e-02, 2.6916351e-01 }, { 7.8642901e-02, 2.6994858e-01 }, + { 7.5988940e-02, 2.7070763e-01 }, { 7.3327655e-02, 2.7144059e-01 }, + { 7.0659302e-02, 2.7214739e-01 }, { 6.7984139e-02, 2.7282796e-01 }, + { 6.5302424e-02, 2.7348224e-01 }, { 6.2614414e-02, 2.7411015e-01 }, + { 5.9920370e-02, 2.7471165e-01 }, { 5.7220550e-02, 2.7528667e-01 }, + { 5.4515216e-02, 2.7583516e-01 }, { 5.1804627e-02, 2.7635706e-01 }, + { 4.9089045e-02, 2.7685232e-01 }, { 4.6368731e-02, 2.7732090e-01 }, + { 4.3643949e-02, 2.7776275e-01 }, { 4.0914960e-02, 2.7817783e-01 }, + { 3.8182028e-02, 2.7856610e-01 }, { 3.5445415e-02, 2.7892752e-01 }, + { 3.2705387e-02, 2.7926206e-01 }, { 2.9962206e-02, 2.7956968e-01 }, + { 2.7216137e-02, 2.7985036e-01 }, { 2.4467445e-02, 2.8010406e-01 }, + { 2.1716395e-02, 2.8033077e-01 }, { 1.8963252e-02, 2.8053046e-01 }, + { 1.6208281e-02, 2.8070310e-01 }, { 1.3451748e-02, 2.8084870e-01 }, + { 1.0693918e-02, 2.8096723e-01 }, { 7.9350576e-03, 2.8105867e-01 }, + { 5.1754324e-03, 2.8112303e-01 }, { 2.4153085e-03, 2.8116029e-01 }, + } +}; + +static const struct lc3_mdct_rot_def mdct_rot_720 = { + .n4 = 720/4, .w = (const struct lc3_complex []){ + { 2.7301192e-01, 2.9780993e-04 }, { 2.7299893e-01, 2.6802468e-03 }, + { 2.7296515e-01, 5.0624796e-03 }, { 2.7291057e-01, 7.4443269e-03 }, + { 2.7283522e-01, 9.8256072e-03 }, { 2.7273909e-01, 1.2206139e-02 }, + { 2.7262218e-01, 1.4585742e-02 }, { 2.7248452e-01, 1.6964234e-02 }, + { 2.7232611e-01, 1.9341434e-02 }, { 2.7214695e-01, 2.1717161e-02 }, + { 2.7194708e-01, 2.4091234e-02 }, { 2.7172649e-01, 2.6463472e-02 }, + { 2.7148521e-01, 2.8833695e-02 }, { 2.7122325e-01, 3.1201723e-02 }, + { 2.7094064e-01, 3.3567374e-02 }, { 2.7063740e-01, 3.5930469e-02 }, + { 2.7031354e-01, 3.8290828e-02 }, { 2.6996910e-01, 4.0648270e-02 }, + { 2.6960411e-01, 4.3002618e-02 }, { 2.6921858e-01, 4.5353690e-02 }, + { 2.6881255e-01, 4.7701309e-02 }, { 2.6838604e-01, 5.0045294e-02 }, + { 2.6793910e-01, 5.2385469e-02 }, { 2.6747176e-01, 5.4721655e-02 }, + { 2.6698404e-01, 5.7053673e-02 }, { 2.6647599e-01, 5.9381346e-02 }, + { 2.6594765e-01, 6.1704497e-02 }, { 2.6539906e-01, 6.4022949e-02 }, + { 2.6483026e-01, 6.6336526e-02 }, { 2.6424128e-01, 6.8645051e-02 }, + { 2.6363219e-01, 7.0948348e-02 }, { 2.6300302e-01, 7.3246242e-02 }, + { 2.6235382e-01, 7.5538558e-02 }, { 2.6168464e-01, 7.7825122e-02 }, + { 2.6099553e-01, 8.0105759e-02 }, { 2.6028655e-01, 8.2380295e-02 }, + { 2.5955774e-01, 8.4648558e-02 }, { 2.5880917e-01, 8.6910375e-02 }, + { 2.5804089e-01, 8.9165573e-02 }, { 2.5725296e-01, 9.1413981e-02 }, + { 2.5644543e-01, 9.3655427e-02 }, { 2.5561838e-01, 9.5889741e-02 }, + { 2.5477186e-01, 9.8116753e-02 }, { 2.5390594e-01, 1.0033629e-01 }, + { 2.5302069e-01, 1.0254819e-01 }, { 2.5211616e-01, 1.0475228e-01 }, + { 2.5119244e-01, 1.0694839e-01 }, { 2.5024958e-01, 1.0913636e-01 }, + { 2.4928767e-01, 1.1131602e-01 }, { 2.4830678e-01, 1.1348720e-01 }, + { 2.4730697e-01, 1.1564973e-01 }, { 2.4628833e-01, 1.1780346e-01 }, + { 2.4525094e-01, 1.1994822e-01 }, { 2.4419487e-01, 1.2208384e-01 }, + { 2.4312020e-01, 1.2421017e-01 }, { 2.4202702e-01, 1.2632704e-01 }, + { 2.4091541e-01, 1.2843429e-01 }, { 2.3978545e-01, 1.3053175e-01 }, + { 2.3863723e-01, 1.3261928e-01 }, { 2.3747083e-01, 1.3469670e-01 }, + { 2.3628636e-01, 1.3676387e-01 }, { 2.3508388e-01, 1.3882063e-01 }, + { 2.3386351e-01, 1.4086681e-01 }, { 2.3262533e-01, 1.4290226e-01 }, + { 2.3136943e-01, 1.4492683e-01 }, { 2.3009591e-01, 1.4694037e-01 }, + { 2.2880487e-01, 1.4894272e-01 }, { 2.2749640e-01, 1.5093372e-01 }, + { 2.2617061e-01, 1.5291323e-01 }, { 2.2482759e-01, 1.5488109e-01 }, + { 2.2346746e-01, 1.5683716e-01 }, { 2.2209030e-01, 1.5878128e-01 }, + { 2.2069624e-01, 1.6071332e-01 }, { 2.1928536e-01, 1.6263311e-01 }, + { 2.1785779e-01, 1.6454052e-01 }, { 2.1641363e-01, 1.6643540e-01 }, + { 2.1495298e-01, 1.6831760e-01 }, { 2.1347597e-01, 1.7018699e-01 }, + { 2.1198270e-01, 1.7204341e-01 }, { 2.1047328e-01, 1.7388674e-01 }, + { 2.0894784e-01, 1.7571682e-01 }, { 2.0740648e-01, 1.7753352e-01 }, + { 2.0584933e-01, 1.7933670e-01 }, { 2.0427651e-01, 1.8112622e-01 }, + { 2.0268812e-01, 1.8290195e-01 }, { 2.0108431e-01, 1.8466375e-01 }, + { 1.9946518e-01, 1.8641149e-01 }, { 1.9783085e-01, 1.8814503e-01 }, + { 1.9618147e-01, 1.8986424e-01 }, { 1.9451714e-01, 1.9156900e-01 }, + { 1.9283800e-01, 1.9325917e-01 }, { 1.9114417e-01, 1.9493462e-01 }, + { 1.8943579e-01, 1.9659522e-01 }, { 1.8771298e-01, 1.9824085e-01 }, + { 1.8597588e-01, 1.9987139e-01 }, { 1.8422461e-01, 2.0148670e-01 }, + { 1.8245932e-01, 2.0308667e-01 }, { 1.8068013e-01, 2.0467118e-01 }, + { 1.7888718e-01, 2.0624010e-01 }, { 1.7708060e-01, 2.0779331e-01 }, + { 1.7526055e-01, 2.0933070e-01 }, { 1.7342714e-01, 2.1085214e-01 }, + { 1.7158053e-01, 2.1235753e-01 }, { 1.6972085e-01, 2.1384675e-01 }, + { 1.6784825e-01, 2.1531968e-01 }, { 1.6596286e-01, 2.1677622e-01 }, + { 1.6406484e-01, 2.1821624e-01 }, { 1.6215432e-01, 2.1963965e-01 }, + { 1.6023145e-01, 2.2104633e-01 }, { 1.5829638e-01, 2.2243618e-01 }, + { 1.5634925e-01, 2.2380909e-01 }, { 1.5439022e-01, 2.2516496e-01 }, + { 1.5241943e-01, 2.2650368e-01 }, { 1.5043704e-01, 2.2782514e-01 }, + { 1.4844319e-01, 2.2912926e-01 }, { 1.4643803e-01, 2.3041593e-01 }, + { 1.4442172e-01, 2.3168506e-01 }, { 1.4239441e-01, 2.3293654e-01 }, + { 1.4035626e-01, 2.3417028e-01 }, { 1.3830742e-01, 2.3538618e-01 }, + { 1.3624805e-01, 2.3658417e-01 }, { 1.3417830e-01, 2.3776413e-01 }, + { 1.3209834e-01, 2.3892599e-01 }, { 1.3000831e-01, 2.4006965e-01 }, + { 1.2790838e-01, 2.4119503e-01 }, { 1.2579872e-01, 2.4230205e-01 }, + { 1.2367947e-01, 2.4339061e-01 }, { 1.2155080e-01, 2.4446063e-01 }, + { 1.1941288e-01, 2.4551204e-01 }, { 1.1726586e-01, 2.4654476e-01 }, + { 1.1510992e-01, 2.4755869e-01 }, { 1.1294520e-01, 2.4855378e-01 }, + { 1.1077189e-01, 2.4952993e-01 }, { 1.0859014e-01, 2.5048709e-01 }, + { 1.0640012e-01, 2.5142516e-01 }, { 1.0420200e-01, 2.5234410e-01 }, + { 1.0199594e-01, 2.5324381e-01 }, { 9.9782117e-02, 2.5412424e-01 }, + { 9.7560694e-02, 2.5498531e-01 }, { 9.5331841e-02, 2.5582697e-01 }, + { 9.3095728e-02, 2.5664915e-01 }, { 9.0852525e-02, 2.5745178e-01 }, + { 8.8602403e-02, 2.5823480e-01 }, { 8.6345534e-02, 2.5899816e-01 }, + { 8.4082090e-02, 2.5974180e-01 }, { 8.1812242e-02, 2.6046565e-01 }, + { 7.9536165e-02, 2.6116967e-01 }, { 7.7254030e-02, 2.6185380e-01 }, + { 7.4966012e-02, 2.6251799e-01 }, { 7.2672284e-02, 2.6316219e-01 }, + { 7.0373023e-02, 2.6378635e-01 }, { 6.8068403e-02, 2.6439042e-01 }, + { 6.5758598e-02, 2.6497435e-01 }, { 6.3443786e-02, 2.6553810e-01 }, + { 6.1124143e-02, 2.6608164e-01 }, { 5.8799845e-02, 2.6660491e-01 }, + { 5.6471069e-02, 2.6710788e-01 }, { 5.4137992e-02, 2.6759050e-01 }, + { 5.1800793e-02, 2.6805275e-01 }, { 4.9459648e-02, 2.6849459e-01 }, + { 4.7114738e-02, 2.6891597e-01 }, { 4.4766239e-02, 2.6931688e-01 }, + { 4.2414331e-02, 2.6969728e-01 }, { 4.0059193e-02, 2.7005714e-01 }, + { 3.7701004e-02, 2.7039644e-01 }, { 3.5339945e-02, 2.7071514e-01 }, + { 3.2976194e-02, 2.7101323e-01 }, { 3.0609932e-02, 2.7129068e-01 }, + { 2.8241338e-02, 2.7154747e-01 }, { 2.5870594e-02, 2.7178357e-01 }, + { 2.3497880e-02, 2.7199899e-01 }, { 2.1123377e-02, 2.7219369e-01 }, + { 1.8747265e-02, 2.7236765e-01 }, { 1.6369725e-02, 2.7252088e-01 }, + { 1.3990938e-02, 2.7265336e-01 }, { 1.1611086e-02, 2.7276507e-01 }, + { 9.2303502e-03, 2.7285601e-01 }, { 6.8489111e-03, 2.7292617e-01 }, + { 4.4669505e-03, 2.7297554e-01 }, { 2.0846497e-03, 2.7300413e-01 }, + } +}; + +static const struct lc3_mdct_rot_def mdct_rot_960 = { + .n4 = 960/4, .w = (const struct lc3_complex []){ + { 2.5406629e-01, 2.0785754e-04 }, { 2.5405949e-01, 1.8707012e-03 }, + { 2.5404180e-01, 3.5334647e-03 }, { 2.5401323e-01, 5.1960769e-03 }, + { 2.5397379e-01, 6.8584664e-03 }, { 2.5392346e-01, 8.5205622e-03 }, + { 2.5386225e-01, 1.0182293e-02 }, { 2.5379017e-01, 1.1843588e-02 }, + { 2.5370722e-01, 1.3504375e-02 }, { 2.5361340e-01, 1.5164584e-02 }, + { 2.5350872e-01, 1.6824143e-02 }, { 2.5339318e-01, 1.8482981e-02 }, + { 2.5326678e-01, 2.0141028e-02 }, { 2.5312953e-01, 2.1798212e-02 }, + { 2.5298144e-01, 2.3454462e-02 }, { 2.5282252e-01, 2.5109708e-02 }, + { 2.5265276e-01, 2.6763878e-02 }, { 2.5247218e-01, 2.8416901e-02 }, + { 2.5228079e-01, 3.0068707e-02 }, { 2.5207859e-01, 3.1719225e-02 }, + { 2.5186559e-01, 3.3368385e-02 }, { 2.5164180e-01, 3.5016115e-02 }, + { 2.5140723e-01, 3.6662344e-02 }, { 2.5116189e-01, 3.8307004e-02 }, + { 2.5090580e-01, 3.9950022e-02 }, { 2.5063895e-01, 4.1591330e-02 }, + { 2.5036137e-01, 4.3230855e-02 }, { 2.5007306e-01, 4.4868529e-02 }, + { 2.4977405e-01, 4.6504281e-02 }, { 2.4946433e-01, 4.8138040e-02 }, + { 2.4914393e-01, 4.9769738e-02 }, { 2.4881285e-01, 5.1399303e-02 }, + { 2.4847112e-01, 5.3026667e-02 }, { 2.4811874e-01, 5.4651759e-02 }, + { 2.4775573e-01, 5.6274511e-02 }, { 2.4738211e-01, 5.7894851e-02 }, + { 2.4699789e-01, 5.9512712e-02 }, { 2.4660310e-01, 6.1128023e-02 }, + { 2.4619774e-01, 6.2740716e-02 }, { 2.4578183e-01, 6.4350721e-02 }, + { 2.4535539e-01, 6.5957969e-02 }, { 2.4491845e-01, 6.7562392e-02 }, + { 2.4447101e-01, 6.9163921e-02 }, { 2.4401310e-01, 7.0762488e-02 }, + { 2.4354474e-01, 7.2358023e-02 }, { 2.4306594e-01, 7.3950458e-02 }, + { 2.4257673e-01, 7.5539726e-02 }, { 2.4207714e-01, 7.7125757e-02 }, + { 2.4156717e-01, 7.8708485e-02 }, { 2.4104685e-01, 8.0287842e-02 }, + { 2.4051621e-01, 8.1863759e-02 }, { 2.3997527e-01, 8.3436169e-02 }, + { 2.3942404e-01, 8.5005005e-02 }, { 2.3886256e-01, 8.6570200e-02 }, + { 2.3829085e-01, 8.8131686e-02 }, { 2.3770893e-01, 8.9689398e-02 }, + { 2.3711683e-01, 9.1243267e-02 }, { 2.3651456e-01, 9.2793227e-02 }, + { 2.3590217e-01, 9.4339213e-02 }, { 2.3527968e-01, 9.5881158e-02 }, + { 2.3464710e-01, 9.7418995e-02 }, { 2.3400447e-01, 9.8952659e-02 }, + { 2.3335182e-01, 1.0048208e-01 }, { 2.3268918e-01, 1.0200721e-01 }, + { 2.3201656e-01, 1.0352796e-01 }, { 2.3133401e-01, 1.0504427e-01 }, + { 2.3064154e-01, 1.0655609e-01 }, { 2.2993920e-01, 1.0806334e-01 }, + { 2.2922701e-01, 1.0956597e-01 }, { 2.2850500e-01, 1.1106390e-01 }, + { 2.2777320e-01, 1.1255707e-01 }, { 2.2703164e-01, 1.1404542e-01 }, + { 2.2628036e-01, 1.1552888e-01 }, { 2.2551938e-01, 1.1700740e-01 }, + { 2.2474874e-01, 1.1848090e-01 }, { 2.2396848e-01, 1.1994933e-01 }, + { 2.2317862e-01, 1.2141262e-01 }, { 2.2237920e-01, 1.2287071e-01 }, + { 2.2157026e-01, 1.2432354e-01 }, { 2.2075182e-01, 1.2577104e-01 }, + { 2.1992393e-01, 1.2721315e-01 }, { 2.1908662e-01, 1.2864982e-01 }, + { 2.1823992e-01, 1.3008097e-01 }, { 2.1738388e-01, 1.3150655e-01 }, + { 2.1651852e-01, 1.3292650e-01 }, { 2.1564388e-01, 1.3434075e-01 }, + { 2.1476001e-01, 1.3574925e-01 }, { 2.1386694e-01, 1.3715193e-01 }, + { 2.1296471e-01, 1.3854874e-01 }, { 2.1205336e-01, 1.3993962e-01 }, + { 2.1113292e-01, 1.4132449e-01 }, { 2.1020344e-01, 1.4270332e-01 }, + { 2.0926495e-01, 1.4407603e-01 }, { 2.0831750e-01, 1.4544257e-01 }, + { 2.0736113e-01, 1.4680288e-01 }, { 2.0639587e-01, 1.4815690e-01 }, + { 2.0542177e-01, 1.4950458e-01 }, { 2.0443887e-01, 1.5084585e-01 }, + { 2.0344722e-01, 1.5218066e-01 }, { 2.0244685e-01, 1.5350895e-01 }, + { 2.0143780e-01, 1.5483066e-01 }, { 2.0042013e-01, 1.5614574e-01 }, + { 1.9939388e-01, 1.5745414e-01 }, { 1.9835908e-01, 1.5875578e-01 }, + { 1.9731578e-01, 1.6005063e-01 }, { 1.9626403e-01, 1.6133862e-01 }, + { 1.9520388e-01, 1.6261970e-01 }, { 1.9413536e-01, 1.6389382e-01 }, + { 1.9305853e-01, 1.6516091e-01 }, { 1.9197343e-01, 1.6642093e-01 }, + { 1.9088010e-01, 1.6767382e-01 }, { 1.8977860e-01, 1.6891953e-01 }, + { 1.8866896e-01, 1.7015800e-01 }, { 1.8755125e-01, 1.7138918e-01 }, + { 1.8642550e-01, 1.7261302e-01 }, { 1.8529177e-01, 1.7382947e-01 }, + { 1.8415009e-01, 1.7503847e-01 }, { 1.8300053e-01, 1.7623997e-01 }, + { 1.8184314e-01, 1.7743392e-01 }, { 1.8067795e-01, 1.7862027e-01 }, + { 1.7950502e-01, 1.7979897e-01 }, { 1.7832440e-01, 1.8096997e-01 }, + { 1.7713614e-01, 1.8213322e-01 }, { 1.7594030e-01, 1.8328866e-01 }, + { 1.7473692e-01, 1.8443625e-01 }, { 1.7352605e-01, 1.8557595e-01 }, + { 1.7230775e-01, 1.8670769e-01 }, { 1.7108207e-01, 1.8783143e-01 }, + { 1.6984906e-01, 1.8894713e-01 }, { 1.6860878e-01, 1.9005474e-01 }, + { 1.6736127e-01, 1.9115420e-01 }, { 1.6610659e-01, 1.9224547e-01 }, + { 1.6484480e-01, 1.9332851e-01 }, { 1.6357595e-01, 1.9440327e-01 }, + { 1.6230008e-01, 1.9546970e-01 }, { 1.6101727e-01, 1.9652776e-01 }, + { 1.5972756e-01, 1.9757740e-01 }, { 1.5843101e-01, 1.9861857e-01 }, + { 1.5712767e-01, 1.9965124e-01 }, { 1.5581760e-01, 2.0067536e-01 }, + { 1.5450085e-01, 2.0169087e-01 }, { 1.5317749e-01, 2.0269775e-01 }, + { 1.5184756e-01, 2.0369595e-01 }, { 1.5051113e-01, 2.0468542e-01 }, + { 1.4916826e-01, 2.0566612e-01 }, { 1.4781899e-01, 2.0663801e-01 }, + { 1.4646339e-01, 2.0760105e-01 }, { 1.4510152e-01, 2.0855520e-01 }, + { 1.4373343e-01, 2.0950041e-01 }, { 1.4235918e-01, 2.1043665e-01 }, + { 1.4097884e-01, 2.1136388e-01 }, { 1.3959246e-01, 2.1228205e-01 }, + { 1.3820009e-01, 2.1319113e-01 }, { 1.3680181e-01, 2.1409107e-01 }, + { 1.3539767e-01, 2.1498185e-01 }, { 1.3398773e-01, 2.1586341e-01 }, + { 1.3257204e-01, 2.1673573e-01 }, { 1.3115068e-01, 2.1759876e-01 }, + { 1.2972370e-01, 2.1845247e-01 }, { 1.2829117e-01, 2.1929683e-01 }, + { 1.2685313e-01, 2.2013179e-01 }, { 1.2540967e-01, 2.2095732e-01 }, + { 1.2396083e-01, 2.2177339e-01 }, { 1.2250668e-01, 2.2257995e-01 }, + { 1.2104729e-01, 2.2337698e-01 }, { 1.1958271e-01, 2.2416445e-01 }, + { 1.1811300e-01, 2.2494231e-01 }, { 1.1663824e-01, 2.2571053e-01 }, + { 1.1515848e-01, 2.2646909e-01 }, { 1.1367379e-01, 2.2721794e-01 }, + { 1.1218422e-01, 2.2795706e-01 }, { 1.1068986e-01, 2.2868642e-01 }, + { 1.0919075e-01, 2.2940598e-01 }, { 1.0768696e-01, 2.3011571e-01 }, + { 1.0617856e-01, 2.3081559e-01 }, { 1.0466561e-01, 2.3150558e-01 }, + { 1.0314818e-01, 2.3218565e-01 }, { 1.0162633e-01, 2.3285577e-01 }, + { 1.0010013e-01, 2.3351592e-01 }, { 9.8569638e-02, 2.3416607e-01 }, + { 9.7034924e-02, 2.3480619e-01 }, { 9.5496054e-02, 2.3543625e-01 }, + { 9.3953093e-02, 2.3605622e-01 }, { 9.2406107e-02, 2.3666608e-01 }, + { 9.0855163e-02, 2.3726580e-01 }, { 8.9300327e-02, 2.3785536e-01 }, + { 8.7741666e-02, 2.3843473e-01 }, { 8.6179246e-02, 2.3900389e-01 }, + { 8.4613135e-02, 2.3956281e-01 }, { 8.3043399e-02, 2.4011147e-01 }, + { 8.1470106e-02, 2.4064984e-01 }, { 7.9893322e-02, 2.4117790e-01 }, + { 7.8313117e-02, 2.4169563e-01 }, { 7.6729556e-02, 2.4220301e-01 }, + { 7.5142709e-02, 2.4270001e-01 }, { 7.3552643e-02, 2.4318662e-01 }, + { 7.1959427e-02, 2.4366281e-01 }, { 7.0363128e-02, 2.4412856e-01 }, + { 6.8763814e-02, 2.4458385e-01 }, { 6.7161555e-02, 2.4502867e-01 }, + { 6.5556419e-02, 2.4546299e-01 }, { 6.3948475e-02, 2.4588679e-01 }, + { 6.2337792e-02, 2.4630007e-01 }, { 6.0724438e-02, 2.4670279e-01 }, + { 5.9108483e-02, 2.4709494e-01 }, { 5.7489996e-02, 2.4747651e-01 }, + { 5.5869046e-02, 2.4784748e-01 }, { 5.4245703e-02, 2.4820783e-01 }, + { 5.2620036e-02, 2.4855755e-01 }, { 5.0992116e-02, 2.4889662e-01 }, + { 4.9362011e-02, 2.4922503e-01 }, { 4.7729791e-02, 2.4954276e-01 }, + { 4.6095527e-02, 2.4984980e-01 }, { 4.4459288e-02, 2.5014615e-01 }, + { 4.2821145e-02, 2.5043177e-01 }, { 4.1181167e-02, 2.5070667e-01 }, + { 3.9539426e-02, 2.5097083e-01 }, { 3.7895990e-02, 2.5122424e-01 }, + { 3.6250931e-02, 2.5146688e-01 }, { 3.4604320e-02, 2.5169876e-01 }, + { 3.2956226e-02, 2.5191985e-01 }, { 3.1306720e-02, 2.5213015e-01 }, + { 2.9655874e-02, 2.5232965e-01 }, { 2.8003757e-02, 2.5251834e-01 }, + { 2.6350440e-02, 2.5269621e-01 }, { 2.4695994e-02, 2.5286326e-01 }, + { 2.3040491e-02, 2.5301948e-01 }, { 2.1384001e-02, 2.5316486e-01 }, + { 1.9726595e-02, 2.5329940e-01 }, { 1.8068343e-02, 2.5342308e-01 }, + { 1.6409318e-02, 2.5353591e-01 }, { 1.4749590e-02, 2.5363788e-01 }, + { 1.3089230e-02, 2.5372898e-01 }, { 1.1428309e-02, 2.5380921e-01 }, + { 9.7668984e-03, 2.5387857e-01 }, { 8.1050697e-03, 2.5393706e-01 }, + { 6.4428938e-03, 2.5398467e-01 }, { 4.7804419e-03, 2.5402140e-01 }, + { 3.1177852e-03, 2.5404724e-01 }, { 1.4549950e-03, 2.5406221e-01 }, + } +}; + +#if LC3_PLUS_HR + +static const struct lc3_mdct_rot_def mdct_rot_1920 = { + .n4 = 1920/4, .w = (const struct lc3_complex []){ + { 2.1364349e-01, 8.7393339e-05 }, { 2.1364206e-01, 7.8653829e-04 }, + { 2.1363834e-01, 1.4856748e-03 }, { 2.1363233e-01, 2.1847954e-03 }, + { 2.1362404e-01, 2.8838927e-03 }, { 2.1361346e-01, 3.5829590e-03 }, + { 2.1360059e-01, 4.2819870e-03 }, { 2.1358543e-01, 4.9809691e-03 }, + { 2.1356799e-01, 5.6798979e-03 }, { 2.1354826e-01, 6.3787658e-03 }, + { 2.1352624e-01, 7.0775654e-03 }, { 2.1350193e-01, 7.7762893e-03 }, + { 2.1347534e-01, 8.4749298e-03 }, { 2.1344647e-01, 9.1734796e-03 }, + { 2.1341530e-01, 9.8719312e-03 }, { 2.1338185e-01, 1.0570277e-02 }, + { 2.1334612e-01, 1.1268510e-02 }, { 2.1330810e-01, 1.1966622e-02 }, + { 2.1326780e-01, 1.2664605e-02 }, { 2.1322521e-01, 1.3362454e-02 }, + { 2.1318034e-01, 1.4060159e-02 }, { 2.1313319e-01, 1.4757713e-02 }, + { 2.1308375e-01, 1.5455110e-02 }, { 2.1303204e-01, 1.6152341e-02 }, + { 2.1297804e-01, 1.6849399e-02 }, { 2.1292176e-01, 1.7546276e-02 }, + { 2.1286320e-01, 1.8242966e-02 }, { 2.1280236e-01, 1.8939460e-02 }, + { 2.1273924e-01, 1.9635751e-02 }, { 2.1267384e-01, 2.0331833e-02 }, + { 2.1260617e-01, 2.1027696e-02 }, { 2.1253622e-01, 2.1723334e-02 }, + { 2.1246399e-01, 2.2418740e-02 }, { 2.1238949e-01, 2.3113905e-02 }, + { 2.1231271e-01, 2.3808823e-02 }, { 2.1223366e-01, 2.4503486e-02 }, + { 2.1215233e-01, 2.5197887e-02 }, { 2.1206874e-01, 2.5892018e-02 }, + { 2.1198287e-01, 2.6585871e-02 }, { 2.1189473e-01, 2.7279440e-02 }, + { 2.1180433e-01, 2.7972716e-02 }, { 2.1171165e-01, 2.8665693e-02 }, + { 2.1161671e-01, 2.9358363e-02 }, { 2.1151950e-01, 3.0050719e-02 }, + { 2.1142003e-01, 3.0742753e-02 }, { 2.1131829e-01, 3.1434457e-02 }, + { 2.1121429e-01, 3.2125825e-02 }, { 2.1110803e-01, 3.2816849e-02 }, + { 2.1099951e-01, 3.3507522e-02 }, { 2.1088873e-01, 3.4197835e-02 }, + { 2.1077568e-01, 3.4887783e-02 }, { 2.1066039e-01, 3.5577357e-02 }, + { 2.1054283e-01, 3.6266549e-02 }, { 2.1042302e-01, 3.6955354e-02 }, + { 2.1030096e-01, 3.7643762e-02 }, { 2.1017664e-01, 3.8331768e-02 }, + { 2.1005008e-01, 3.9019363e-02 }, { 2.0992126e-01, 3.9706540e-02 }, + { 2.0979020e-01, 4.0393292e-02 }, { 2.0965689e-01, 4.1079611e-02 }, + { 2.0952134e-01, 4.1765490e-02 }, { 2.0938354e-01, 4.2450923e-02 }, + { 2.0924350e-01, 4.3135900e-02 }, { 2.0910121e-01, 4.3820416e-02 }, + { 2.0895669e-01, 4.4504462e-02 }, { 2.0880993e-01, 4.5188032e-02 }, + { 2.0866094e-01, 4.5871117e-02 }, { 2.0850971e-01, 4.6553712e-02 }, + { 2.0835625e-01, 4.7235808e-02 }, { 2.0820055e-01, 4.7917398e-02 }, + { 2.0804263e-01, 4.8598475e-02 }, { 2.0788248e-01, 4.9279031e-02 }, + { 2.0772010e-01, 4.9959060e-02 }, { 2.0755549e-01, 5.0638554e-02 }, + { 2.0738867e-01, 5.1317505e-02 }, { 2.0721962e-01, 5.1995907e-02 }, + { 2.0704836e-01, 5.2673752e-02 }, { 2.0687487e-01, 5.3351033e-02 }, + { 2.0669918e-01, 5.4027742e-02 }, { 2.0652126e-01, 5.4703873e-02 }, + { 2.0634114e-01, 5.5379418e-02 }, { 2.0615881e-01, 5.6054370e-02 }, + { 2.0597427e-01, 5.6728722e-02 }, { 2.0578752e-01, 5.7402466e-02 }, + { 2.0559857e-01, 5.8075596e-02 }, { 2.0540742e-01, 5.8748104e-02 }, + { 2.0521406e-01, 5.9419982e-02 }, { 2.0501851e-01, 6.0091224e-02 }, + { 2.0482077e-01, 6.0761823e-02 }, { 2.0462083e-01, 6.1431770e-02 }, + { 2.0441870e-01, 6.2101060e-02 }, { 2.0421438e-01, 6.2769685e-02 }, + { 2.0400787e-01, 6.3437638e-02 }, { 2.0379918e-01, 6.4104911e-02 }, + { 2.0358831e-01, 6.4771498e-02 }, { 2.0337525e-01, 6.5437391e-02 }, + { 2.0316002e-01, 6.6102584e-02 }, { 2.0294262e-01, 6.6767068e-02 }, + { 2.0272303e-01, 6.7430838e-02 }, { 2.0250128e-01, 6.8093885e-02 }, + { 2.0227736e-01, 6.8756203e-02 }, { 2.0205127e-01, 6.9417785e-02 }, + { 2.0182302e-01, 7.0078623e-02 }, { 2.0159261e-01, 7.0738711e-02 }, + { 2.0136004e-01, 7.1398041e-02 }, { 2.0112531e-01, 7.2056607e-02 }, + { 2.0088843e-01, 7.2714401e-02 }, { 2.0064940e-01, 7.3371416e-02 }, + { 2.0040822e-01, 7.4027646e-02 }, { 2.0016489e-01, 7.4683083e-02 }, + { 1.9991942e-01, 7.5337720e-02 }, { 1.9967181e-01, 7.5991550e-02 }, + { 1.9942206e-01, 7.6644566e-02 }, { 1.9917017e-01, 7.7296762e-02 }, + { 1.9891615e-01, 7.7948130e-02 }, { 1.9866000e-01, 7.8598663e-02 }, + { 1.9840173e-01, 7.9248354e-02 }, { 1.9814132e-01, 7.9897197e-02 }, + { 1.9787880e-01, 8.0545184e-02 }, { 1.9761416e-01, 8.1192308e-02 }, + { 1.9734740e-01, 8.1838563e-02 }, { 1.9707853e-01, 8.2483942e-02 }, + { 1.9680754e-01, 8.3128437e-02 }, { 1.9653445e-01, 8.3772042e-02 }, + { 1.9625926e-01, 8.4414750e-02 }, { 1.9598196e-01, 8.5056553e-02 }, + { 1.9570257e-01, 8.5697446e-02 }, { 1.9542107e-01, 8.6337421e-02 }, + { 1.9513749e-01, 8.6976472e-02 }, { 1.9485182e-01, 8.7614591e-02 }, + { 1.9456405e-01, 8.8251772e-02 }, { 1.9427421e-01, 8.8888007e-02 }, + { 1.9398228e-01, 8.9523291e-02 }, { 1.9368828e-01, 9.0157616e-02 }, + { 1.9339221e-01, 9.0790976e-02 }, { 1.9309406e-01, 9.1423363e-02 }, + { 1.9279384e-01, 9.2054771e-02 }, { 1.9249156e-01, 9.2685193e-02 }, + { 1.9218722e-01, 9.3314623e-02 }, { 1.9188082e-01, 9.3943054e-02 }, + { 1.9157237e-01, 9.4570478e-02 }, { 1.9126186e-01, 9.5196890e-02 }, + { 1.9094930e-01, 9.5822282e-02 }, { 1.9063470e-01, 9.6446648e-02 }, + { 1.9031806e-01, 9.7069981e-02 }, { 1.8999938e-01, 9.7692274e-02 }, + { 1.8967867e-01, 9.8313522e-02 }, { 1.8935592e-01, 9.8933716e-02 }, + { 1.8903115e-01, 9.9552851e-02 }, { 1.8870435e-01, 1.0017092e-01 }, + { 1.8837554e-01, 1.0078792e-01 }, { 1.8804470e-01, 1.0140383e-01 }, + { 1.8771185e-01, 1.0201866e-01 }, { 1.8737699e-01, 1.0263240e-01 }, + { 1.8704012e-01, 1.0324504e-01 }, { 1.8670125e-01, 1.0385657e-01 }, + { 1.8636039e-01, 1.0446700e-01 }, { 1.8601752e-01, 1.0507630e-01 }, + { 1.8567266e-01, 1.0568448e-01 }, { 1.8532582e-01, 1.0629152e-01 }, + { 1.8497699e-01, 1.0689743e-01 }, { 1.8462618e-01, 1.0750219e-01 }, + { 1.8427339e-01, 1.0810580e-01 }, { 1.8391863e-01, 1.0870825e-01 }, + { 1.8356190e-01, 1.0930954e-01 }, { 1.8320320e-01, 1.0990966e-01 }, + { 1.8284254e-01, 1.1050860e-01 }, { 1.8247992e-01, 1.1110636e-01 }, + { 1.8211535e-01, 1.1170293e-01 }, { 1.8174883e-01, 1.1229830e-01 }, + { 1.8138036e-01, 1.1289247e-01 }, { 1.8100995e-01, 1.1348543e-01 }, + { 1.8063761e-01, 1.1407718e-01 }, { 1.8026332e-01, 1.1466770e-01 }, + { 1.7988711e-01, 1.1525699e-01 }, { 1.7950897e-01, 1.1584506e-01 }, + { 1.7912891e-01, 1.1643188e-01 }, { 1.7874692e-01, 1.1701745e-01 }, + { 1.7836303e-01, 1.1760177e-01 }, { 1.7797722e-01, 1.1818483e-01 }, + { 1.7758951e-01, 1.1876663e-01 }, { 1.7719990e-01, 1.1934715e-01 }, + { 1.7680839e-01, 1.1992639e-01 }, { 1.7641498e-01, 1.2050436e-01 }, + { 1.7601969e-01, 1.2108103e-01 }, { 1.7562251e-01, 1.2165640e-01 }, + { 1.7522345e-01, 1.2223047e-01 }, { 1.7482252e-01, 1.2280323e-01 }, + { 1.7441971e-01, 1.2337468e-01 }, { 1.7401503e-01, 1.2394480e-01 }, + { 1.7360849e-01, 1.2451360e-01 }, { 1.7320010e-01, 1.2508107e-01 }, + { 1.7278984e-01, 1.2564719e-01 }, { 1.7237774e-01, 1.2621197e-01 }, + { 1.7196379e-01, 1.2677540e-01 }, { 1.7154800e-01, 1.2733747e-01 }, + { 1.7113037e-01, 1.2789818e-01 }, { 1.7071091e-01, 1.2845751e-01 }, + { 1.7028962e-01, 1.2901548e-01 }, { 1.6986650e-01, 1.2957206e-01 }, + { 1.6944157e-01, 1.3012725e-01 }, { 1.6901482e-01, 1.3068105e-01 }, + { 1.6858627e-01, 1.3123344e-01 }, { 1.6815590e-01, 1.3178444e-01 }, + { 1.6772374e-01, 1.3233402e-01 }, { 1.6728978e-01, 1.3288219e-01 }, + { 1.6685403e-01, 1.3342893e-01 }, { 1.6641649e-01, 1.3397424e-01 }, + { 1.6597717e-01, 1.3451812e-01 }, { 1.6553608e-01, 1.3506056e-01 }, + { 1.6509321e-01, 1.3560155e-01 }, { 1.6464857e-01, 1.3614109e-01 }, + { 1.6420217e-01, 1.3667917e-01 }, { 1.6375401e-01, 1.3721579e-01 }, + { 1.6330409e-01, 1.3775093e-01 }, { 1.6285243e-01, 1.3828461e-01 }, + { 1.6239902e-01, 1.3881680e-01 }, { 1.6194388e-01, 1.3934750e-01 }, + { 1.6148700e-01, 1.3987672e-01 }, { 1.6102839e-01, 1.4040443e-01 }, + { 1.6056805e-01, 1.4093064e-01 }, { 1.6010600e-01, 1.4145535e-01 }, + { 1.5964223e-01, 1.4197853e-01 }, { 1.5917676e-01, 1.4250020e-01 }, + { 1.5870957e-01, 1.4302034e-01 }, { 1.5824069e-01, 1.4353895e-01 }, + { 1.5777011e-01, 1.4405602e-01 }, { 1.5729785e-01, 1.4457155e-01 }, + { 1.5682390e-01, 1.4508553e-01 }, { 1.5634827e-01, 1.4559796e-01 }, + { 1.5587096e-01, 1.4610883e-01 }, { 1.5539199e-01, 1.4661813e-01 }, + { 1.5491135e-01, 1.4712586e-01 }, { 1.5442905e-01, 1.4763202e-01 }, + { 1.5394510e-01, 1.4813660e-01 }, { 1.5345950e-01, 1.4863959e-01 }, + { 1.5297226e-01, 1.4914099e-01 }, { 1.5248338e-01, 1.4964079e-01 }, + { 1.5199287e-01, 1.5013899e-01 }, { 1.5150072e-01, 1.5063558e-01 }, + { 1.5100696e-01, 1.5113055e-01 }, { 1.5051158e-01, 1.5162391e-01 }, + { 1.5001459e-01, 1.5211565e-01 }, { 1.4951599e-01, 1.5260575e-01 }, + { 1.4901579e-01, 1.5309423e-01 }, { 1.4851399e-01, 1.5358106e-01 }, + { 1.4801060e-01, 1.5406625e-01 }, { 1.4750563e-01, 1.5454978e-01 }, + { 1.4699908e-01, 1.5503167e-01 }, { 1.4649095e-01, 1.5551189e-01 }, + { 1.4598126e-01, 1.5599045e-01 }, { 1.4547000e-01, 1.5646733e-01 }, + { 1.4495718e-01, 1.5694254e-01 }, { 1.4444281e-01, 1.5741607e-01 }, + { 1.4392690e-01, 1.5788792e-01 }, { 1.4340944e-01, 1.5835807e-01 }, + { 1.4289045e-01, 1.5882653e-01 }, { 1.4236993e-01, 1.5929328e-01 }, + { 1.4184788e-01, 1.5975834e-01 }, { 1.4132431e-01, 1.6022168e-01 }, + { 1.4079923e-01, 1.6068330e-01 }, { 1.4027264e-01, 1.6114320e-01 }, + { 1.3974455e-01, 1.6160138e-01 }, { 1.3921497e-01, 1.6205783e-01 }, + { 1.3868389e-01, 1.6251254e-01 }, { 1.3815133e-01, 1.6296551e-01 }, + { 1.3761729e-01, 1.6341673e-01 }, { 1.3708177e-01, 1.6386621e-01 }, + { 1.3654479e-01, 1.6431393e-01 }, { 1.3600634e-01, 1.6475989e-01 }, + { 1.3546644e-01, 1.6520409e-01 }, { 1.3492508e-01, 1.6564652e-01 }, + { 1.3438228e-01, 1.6608717e-01 }, { 1.3383805e-01, 1.6652605e-01 }, + { 1.3329238e-01, 1.6696314e-01 }, { 1.3274528e-01, 1.6739844e-01 }, + { 1.3219676e-01, 1.6783195e-01 }, { 1.3164682e-01, 1.6826366e-01 }, + { 1.3109548e-01, 1.6869358e-01 }, { 1.3054273e-01, 1.6912168e-01 }, + { 1.2998858e-01, 1.6954797e-01 }, { 1.2943304e-01, 1.6997245e-01 }, + { 1.2887611e-01, 1.7039511e-01 }, { 1.2831781e-01, 1.7081594e-01 }, + { 1.2775813e-01, 1.7123495e-01 }, { 1.2719708e-01, 1.7165212e-01 }, + { 1.2663467e-01, 1.7206745e-01 }, { 1.2607090e-01, 1.7248094e-01 }, + { 1.2550579e-01, 1.7289258e-01 }, { 1.2493933e-01, 1.7330237e-01 }, + { 1.2437153e-01, 1.7371030e-01 }, { 1.2380240e-01, 1.7411638e-01 }, + { 1.2323194e-01, 1.7452059e-01 }, { 1.2266016e-01, 1.7492293e-01 }, + { 1.2208708e-01, 1.7532339e-01 }, { 1.2151268e-01, 1.7572198e-01 }, + { 1.2093698e-01, 1.7611869e-01 }, { 1.2035999e-01, 1.7651351e-01 }, + { 1.1978170e-01, 1.7690644e-01 }, { 1.1920214e-01, 1.7729748e-01 }, + { 1.1862130e-01, 1.7768662e-01 }, { 1.1803918e-01, 1.7807385e-01 }, + { 1.1745581e-01, 1.7845918e-01 }, { 1.1687117e-01, 1.7884260e-01 }, + { 1.1628529e-01, 1.7922410e-01 }, { 1.1569816e-01, 1.7960368e-01 }, + { 1.1510979e-01, 1.7998134e-01 }, { 1.1452018e-01, 1.8035707e-01 }, + { 1.1392935e-01, 1.8073087e-01 }, { 1.1333730e-01, 1.8110274e-01 }, + { 1.1274404e-01, 1.8147266e-01 }, { 1.1214957e-01, 1.8184065e-01 }, + { 1.1155390e-01, 1.8220668e-01 }, { 1.1095703e-01, 1.8257076e-01 }, + { 1.1035898e-01, 1.8293289e-01 }, { 1.0975974e-01, 1.8329306e-01 }, + { 1.0915933e-01, 1.8365126e-01 }, { 1.0855775e-01, 1.8400750e-01 }, + { 1.0795501e-01, 1.8436177e-01 }, { 1.0735111e-01, 1.8471407e-01 }, + { 1.0674606e-01, 1.8506438e-01 }, { 1.0613987e-01, 1.8541272e-01 }, + { 1.0553254e-01, 1.8575906e-01 }, { 1.0492408e-01, 1.8610342e-01 }, + { 1.0431449e-01, 1.8644579e-01 }, { 1.0370379e-01, 1.8678616e-01 }, + { 1.0309198e-01, 1.8712453e-01 }, { 1.0247907e-01, 1.8746089e-01 }, + { 1.0186506e-01, 1.8779525e-01 }, { 1.0124995e-01, 1.8812760e-01 }, + { 1.0063377e-01, 1.8845793e-01 }, { 1.0001650e-01, 1.8878624e-01 }, + { 9.9398167e-02, 1.8911253e-01 }, { 9.8778766e-02, 1.8943680e-01 }, + { 9.8158308e-02, 1.8975904e-01 }, { 9.7536799e-02, 1.9007924e-01 }, + { 9.6914245e-02, 1.9039742e-01 }, { 9.6290653e-02, 1.9071355e-01 }, + { 9.5666030e-02, 1.9102763e-01 }, { 9.5040382e-02, 1.9133968e-01 }, + { 9.4413717e-02, 1.9164967e-01 }, { 9.3786040e-02, 1.9195761e-01 }, + { 9.3157359e-02, 1.9226350e-01 }, { 9.2527681e-02, 1.9256733e-01 }, + { 9.1897011e-02, 1.9286909e-01 }, { 9.1265358e-02, 1.9316879e-01 }, + { 9.0632727e-02, 1.9346642e-01 }, { 8.9999125e-02, 1.9376198e-01 }, + { 8.9364560e-02, 1.9405546e-01 }, { 8.8729037e-02, 1.9434687e-01 }, + { 8.8092565e-02, 1.9463619e-01 }, { 8.7455149e-02, 1.9492343e-01 }, + { 8.6816796e-02, 1.9520858e-01 }, { 8.6177514e-02, 1.9549164e-01 }, + { 8.5537309e-02, 1.9577261e-01 }, { 8.4896187e-02, 1.9605148e-01 }, + { 8.4254157e-02, 1.9632825e-01 }, { 8.3611224e-02, 1.9660292e-01 }, + { 8.2967396e-02, 1.9687549e-01 }, { 8.2322680e-02, 1.9714594e-01 }, + { 8.1677081e-02, 1.9741429e-01 }, { 8.1030608e-02, 1.9768052e-01 }, + { 8.0383268e-02, 1.9794463e-01 }, { 7.9735066e-02, 1.9820662e-01 }, + { 7.9086011e-02, 1.9846649e-01 }, { 7.8436108e-02, 1.9872424e-01 }, + { 7.7785366e-02, 1.9897986e-01 }, { 7.7133791e-02, 1.9923334e-01 }, + { 7.6481389e-02, 1.9948470e-01 }, { 7.5828169e-02, 1.9973391e-01 }, + { 7.5174136e-02, 1.9998099e-01 }, { 7.4519298e-02, 2.0022592e-01 }, + { 7.3863663e-02, 2.0046872e-01 }, { 7.3207236e-02, 2.0070936e-01 }, + { 7.2550025e-02, 2.0094785e-01 }, { 7.1892038e-02, 2.0118420e-01 }, + { 7.1233280e-02, 2.0141839e-01 }, { 7.0573760e-02, 2.0165042e-01 }, + { 6.9913484e-02, 2.0188029e-01 }, { 6.9252459e-02, 2.0210800e-01 }, + { 6.8590692e-02, 2.0233354e-01 }, { 6.7928191e-02, 2.0255692e-01 }, + { 6.7264963e-02, 2.0277813e-01 }, { 6.6601014e-02, 2.0299717e-01 }, + { 6.5936352e-02, 2.0321403e-01 }, { 6.5270983e-02, 2.0342872e-01 }, + { 6.4604916e-02, 2.0364123e-01 }, { 6.3938157e-02, 2.0385156e-01 }, + { 6.3270713e-02, 2.0405971e-01 }, { 6.2602592e-02, 2.0426567e-01 }, + { 6.1933800e-02, 2.0446944e-01 }, { 6.1264345e-02, 2.0467102e-01 }, + { 6.0594234e-02, 2.0487041e-01 }, { 5.9923474e-02, 2.0506761e-01 }, + { 5.9252072e-02, 2.0526261e-01 }, { 5.8580035e-02, 2.0545541e-01 }, + { 5.7907372e-02, 2.0564601e-01 }, { 5.7234088e-02, 2.0583441e-01 }, + { 5.6560191e-02, 2.0602061e-01 }, { 5.5885688e-02, 2.0620460e-01 }, + { 5.5210587e-02, 2.0638638e-01 }, { 5.4534895e-02, 2.0656595e-01 }, + { 5.3858619e-02, 2.0674331e-01 }, { 5.3181766e-02, 2.0691845e-01 }, + { 5.2504343e-02, 2.0709138e-01 }, { 5.1826358e-02, 2.0726209e-01 }, + { 5.1147818e-02, 2.0743058e-01 }, { 5.0468731e-02, 2.0759685e-01 }, + { 4.9789103e-02, 2.0776090e-01 }, { 4.9108941e-02, 2.0792272e-01 }, + { 4.8428254e-02, 2.0808232e-01 }, { 4.7747048e-02, 2.0823968e-01 }, + { 4.7065331e-02, 2.0839482e-01 }, { 4.6383110e-02, 2.0854773e-01 }, + { 4.5700392e-02, 2.0869840e-01 }, { 4.5017184e-02, 2.0884683e-01 }, + { 4.4333495e-02, 2.0899303e-01 }, { 4.3649330e-02, 2.0913699e-01 }, + { 4.2964699e-02, 2.0927872e-01 }, { 4.2279607e-02, 2.0941820e-01 }, + { 4.1594062e-02, 2.0955544e-01 }, { 4.0908072e-02, 2.0969043e-01 }, + { 4.0221644e-02, 2.0982318e-01 }, { 3.9534785e-02, 2.0995368e-01 }, + { 3.8847503e-02, 2.1008193e-01 }, { 3.8159805e-02, 2.1020793e-01 }, + { 3.7471698e-02, 2.1033169e-01 }, { 3.6783189e-02, 2.1045319e-01 }, + { 3.6094287e-02, 2.1057243e-01 }, { 3.5404998e-02, 2.1068942e-01 }, + { 3.4715331e-02, 2.1080416e-01 }, { 3.4025291e-02, 2.1091663e-01 }, + { 3.3334887e-02, 2.1102685e-01 }, { 3.2644126e-02, 2.1113481e-01 }, + { 3.1953015e-02, 2.1124051e-01 }, { 3.1261563e-02, 2.1134394e-01 }, + { 3.0569775e-02, 2.1144511e-01 }, { 2.9877660e-02, 2.1154402e-01 }, + { 2.9185225e-02, 2.1164066e-01 }, { 2.8492478e-02, 2.1173504e-01 }, + { 2.7799425e-02, 2.1182714e-01 }, { 2.7106075e-02, 2.1191698e-01 }, + { 2.6412434e-02, 2.1200455e-01 }, { 2.5718511e-02, 2.1208985e-01 }, + { 2.5024312e-02, 2.1217288e-01 }, { 2.4329845e-02, 2.1225363e-01 }, + { 2.3635117e-02, 2.1233212e-01 }, { 2.2940137e-02, 2.1240833e-01 }, + { 2.2244911e-02, 2.1248226e-01 }, { 2.1549446e-02, 2.1255392e-01 }, + { 2.0853751e-02, 2.1262330e-01 }, { 2.0157832e-02, 2.1269040e-01 }, + { 1.9461698e-02, 2.1275523e-01 }, { 1.8765355e-02, 2.1281778e-01 }, + { 1.8068811e-02, 2.1287805e-01 }, { 1.7372074e-02, 2.1293604e-01 }, + { 1.6675151e-02, 2.1299175e-01 }, { 1.5978049e-02, 2.1304518e-01 }, + { 1.5280776e-02, 2.1309633e-01 }, { 1.4583339e-02, 2.1314519e-01 }, + { 1.3885746e-02, 2.1319177e-01 }, { 1.3188005e-02, 2.1323607e-01 }, + { 1.2490122e-02, 2.1327809e-01 }, { 1.1792105e-02, 2.1331782e-01 }, + { 1.1093963e-02, 2.1335527e-01 }, { 1.0395701e-02, 2.1339043e-01 }, + { 9.6973279e-03, 2.1342331e-01 }, { 8.9988511e-03, 2.1345390e-01 }, + { 8.3002779e-03, 2.1348220e-01 }, { 7.6016158e-03, 2.1350822e-01 }, + { 6.9028723e-03, 2.1353196e-01 }, { 6.2040549e-03, 2.1355340e-01 }, + { 5.5051711e-03, 2.1357256e-01 }, { 4.8062283e-03, 2.1358943e-01 }, + { 4.1072340e-03, 2.1360402e-01 }, { 3.4081957e-03, 2.1361632e-01 }, + { 2.7091210e-03, 2.1362633e-01 }, { 2.0100172e-03, 2.1363405e-01 }, + { 1.3108919e-03, 2.1363948e-01 }, { 6.1175255e-04, 2.1364263e-01 }, + } +}; + +#endif /* LC3_PLUS_HR */ + +const struct lc3_mdct_rot_def * lc3_mdct_rot[LC3_NUM_DT][LC3_NUM_SRATE] = { + + [LC3_DT_2M5] = { + LC3_IF_PLUS(&mdct_rot_40, NULL), + LC3_IF_PLUS(&mdct_rot_80, NULL), &mdct_rot_120, + &mdct_rot_160, &mdct_rot_240, &mdct_rot_240, &mdct_rot_480 }, + + [LC3_DT_5M ] = { + LC3_IF_PLUS(&mdct_rot_80, NULL), &mdct_rot_160, &mdct_rot_240, + &mdct_rot_320, &mdct_rot_480, &mdct_rot_480, &mdct_rot_960 }, + + [LC3_DT_7M5] = { + &mdct_rot_120, &mdct_rot_240, &mdct_rot_360, + &mdct_rot_480, &mdct_rot_720 }, + + [LC3_DT_10M] = { + &mdct_rot_160, &mdct_rot_320, &mdct_rot_480, + &mdct_rot_640, &mdct_rot_960, &mdct_rot_960, + LC3_IF_PLUS_HR(&mdct_rot_1920, NULL) }, + +}; + + +/** + * Low delay MDCT windows + */ + +#define __LC3_MDCT_WIN_LEN(_dt_us, _sr_hz) \ + ( LC3_NS(_dt_us, _sr_hz) + LC3_ND(_dt_us, _sr_hz) ) + +#if LC3_PLUS + +static const float mdct_win_2m5_8k[__LC3_MDCT_WIN_LEN(2500, 8000)] = { + 6.73791440e-03, 2.73228958e-02, 6.16356097e-02, 1.11912504e-01, + 1.78705350e-01, 2.60752499e-01, 3.54977638e-01, 4.56769675e-01, + 5.60523927e-01, 6.60366535e-01, 7.50943422e-01, 8.28138232e-01, + 8.89585018e-01, 9.34874773e-01, 9.65405703e-01, 9.83902633e-01, + 9.93718088e-01, 9.98098731e-01, 9.99626637e-01, 9.99977291e-01, + 9.99977291e-01, 9.99626637e-01, 9.98098731e-01, 9.93718088e-01, + 9.83902633e-01, 9.65405703e-01, 9.34874773e-01, 8.89585018e-01, + 8.28138232e-01, 7.50943422e-01, 6.60366535e-01, 5.60523927e-01, + 4.56769675e-01, 3.54977638e-01, 2.60752499e-01, 1.78705350e-01, + 1.11912504e-01, 6.16356097e-02, 2.73228958e-02, 6.73791440e-03, +}; + +static const float mdct_win_2m5_16k[__LC3_MDCT_WIN_LEN(2500, 16000)] = { + 4.76441615e-03, 1.20463628e-02, 2.22639654e-02, 3.58022311e-02, + 5.29905465e-02, 7.40851840e-02, 9.92538592e-02, 1.28563118e-01, + 1.61969244e-01, 1.99313241e-01, 2.40320282e-01, 2.84603818e-01, + 3.31674323e-01, 3.80952458e-01, 4.31786202e-01, 4.83471316e-01, + 5.35274327e-01, 5.86457098e-01, 6.36301916e-01, 6.84136019e-01, + 7.29354445e-01, 7.71440129e-01, 8.09980291e-01, 8.44678279e-01, + 8.75360204e-01, 9.01975984e-01, 9.24594627e-01, 9.43393949e-01, + 9.58645225e-01, 9.70693650e-01, 9.79935831e-01, 9.86795807e-01, + 9.91701328e-01, 9.95062145e-01, 9.97251917e-01, 9.98595014e-01, + 9.99358895e-01, 9.99752127e-01, 9.99927440e-01, 9.99988650e-01, + 9.99988650e-01, 9.99927440e-01, 9.99752127e-01, 9.99358895e-01, + 9.98595014e-01, 9.97251917e-01, 9.95062145e-01, 9.91701328e-01, + 9.86795807e-01, 9.79935831e-01, 9.70693650e-01, 9.58645225e-01, + 9.43393949e-01, 9.24594627e-01, 9.01975984e-01, 8.75360204e-01, + 8.44678279e-01, 8.09980291e-01, 7.71440129e-01, 7.29354445e-01, + 6.84136019e-01, 6.36301916e-01, 5.86457098e-01, 5.35274327e-01, + 4.83471316e-01, 4.31786202e-01, 3.80952458e-01, 3.31674323e-01, + 2.84603818e-01, 2.40320282e-01, 1.99313241e-01, 1.61969244e-01, + 1.28563118e-01, 9.92538592e-02, 7.40851840e-02, 5.29905465e-02, + 3.58022311e-02, 2.22639654e-02, 1.20463628e-02, 4.76441615e-03, +}; + +static const float mdct_win_2m5_24k[__LC3_MDCT_WIN_LEN(2500, 24000)] = { + 3.89013421e-03, 8.20259508e-03, 1.37023556e-02, 2.05296853e-02, + 2.88030773e-02, 3.86278514e-02, 5.00956972e-02, 6.32829654e-02, + 7.82488109e-02, 9.50334651e-02, 1.13656749e-01, 1.34116888e-01, + 1.56389671e-01, 1.80427981e-01, 2.06161727e-01, 2.33498181e-01, + 2.62322720e-01, 2.92499991e-01, 3.23875450e-01, 3.56277288e-01, + 3.89518694e-01, 4.23400416e-01, 4.57713594e-01, 4.92242792e-01, + 5.26769190e-01, 5.61073868e-01, 5.94941120e-01, 6.28161737e-01, + 6.60536189e-01, 6.91877646e-01, 7.22014766e-01, 7.50794208e-01, + 7.78082793e-01, 8.03769285e-01, 8.27765737e-01, 8.50008365e-01, + 8.70457945e-01, 8.89099694e-01, 9.05942652e-01, 9.21018560e-01, + 9.34380273e-01, 9.46099727e-01, 9.56265526e-01, 9.64980202e-01, + 9.72357239e-01, 9.78517931e-01, 9.83588198e-01, 9.87695434e-01, + 9.90965519e-01, 9.93520077e-01, 9.95474078e-01, 9.96933861e-01, + 9.97995624e-01, 9.98744422e-01, 9.99253666e-01, 9.99585105e-01, + 9.99789244e-01, 9.99906118e-01, 9.99966358e-01, 9.99992433e-01, + 9.99992433e-01, 9.99966358e-01, 9.99906118e-01, 9.99789244e-01, + 9.99585105e-01, 9.99253666e-01, 9.98744422e-01, 9.97995624e-01, + 9.96933861e-01, 9.95474078e-01, 9.93520077e-01, 9.90965519e-01, + 9.87695434e-01, 9.83588198e-01, 9.78517931e-01, 9.72357239e-01, + 9.64980202e-01, 9.56265526e-01, 9.46099727e-01, 9.34380273e-01, + 9.21018560e-01, 9.05942652e-01, 8.89099694e-01, 8.70457945e-01, + 8.50008365e-01, 8.27765737e-01, 8.03769285e-01, 7.78082793e-01, + 7.50794208e-01, 7.22014766e-01, 6.91877646e-01, 6.60536189e-01, + 6.28161737e-01, 5.94941120e-01, 5.61073868e-01, 5.26769190e-01, + 4.92242792e-01, 4.57713594e-01, 4.23400416e-01, 3.89518694e-01, + 3.56277288e-01, 3.23875450e-01, 2.92499991e-01, 2.62322720e-01, + 2.33498181e-01, 2.06161727e-01, 1.80427981e-01, 1.56389671e-01, + 1.34116888e-01, 1.13656749e-01, 9.50334651e-02, 7.82488109e-02, + 6.32829654e-02, 5.00956972e-02, 3.86278514e-02, 2.88030773e-02, + 2.05296853e-02, 1.37023556e-02, 8.20259508e-03, 3.89013421e-03, +}; + +static const float mdct_win_2m5_32k[__LC3_MDCT_WIN_LEN(2500, 32000)] = { + 3.36895835e-03, 6.45555741e-03, 1.01430808e-02, 1.45212685e-02, + 1.96507673e-02, 2.55835280e-02, 3.23662853e-02, 4.00411787e-02, + 4.86456418e-02, 5.82120708e-02, 6.87674290e-02, 8.03328498e-02, + 9.29232660e-02, 1.06547081e-01, 1.21205893e-01, 1.36894275e-01, + 1.53599623e-01, 1.71302066e-01, 1.89974455e-01, 2.09582413e-01, + 2.30084468e-01, 2.51432255e-01, 2.73570790e-01, 2.96438816e-01, + 3.19969212e-01, 3.44089476e-01, 3.68722258e-01, 3.93785956e-01, + 4.19195353e-01, 4.44862309e-01, 4.70696475e-01, 4.96606050e-01, + 5.22498546e-01, 5.48281575e-01, 5.73863636e-01, 5.99154896e-01, + 6.24067964e-01, 6.48518633e-01, 6.72426605e-01, 6.95716161e-01, + 7.18316799e-01, 7.40163807e-01, 7.61198780e-01, 7.81370064e-01, + 8.00633131e-01, 8.18950870e-01, 8.36293796e-01, 8.52640176e-01, + 8.67976054e-01, 8.82295205e-01, 8.95598976e-01, 9.07896060e-01, + 9.19202165e-01, 9.29539615e-01, 9.38936863e-01, 9.47427941e-01, + 9.55051846e-01, 9.61851871e-01, 9.67874900e-01, 9.73170662e-01, + 9.77790986e-01, 9.81789034e-01, 9.85218555e-01, 9.88133167e-01, + 9.90585664e-01, 9.92627388e-01, 9.94307658e-01, 9.95673273e-01, + 9.96768094e-01, 9.97632718e-01, 9.98304240e-01, 9.98816100e-01, + 9.99198030e-01, 9.99476075e-01, 9.99672688e-01, 9.99806905e-01, + 9.99894561e-01, 9.99948558e-01, 9.99979163e-01, 9.99994325e-01, + 9.99994325e-01, 9.99979163e-01, 9.99948558e-01, 9.99894561e-01, + 9.99806905e-01, 9.99672688e-01, 9.99476075e-01, 9.99198030e-01, + 9.98816100e-01, 9.98304240e-01, 9.97632718e-01, 9.96768094e-01, + 9.95673273e-01, 9.94307658e-01, 9.92627388e-01, 9.90585664e-01, + 9.88133167e-01, 9.85218555e-01, 9.81789034e-01, 9.77790986e-01, + 9.73170662e-01, 9.67874900e-01, 9.61851871e-01, 9.55051846e-01, + 9.47427941e-01, 9.38936863e-01, 9.29539615e-01, 9.19202165e-01, + 9.07896060e-01, 8.95598976e-01, 8.82295205e-01, 8.67976054e-01, + 8.52640176e-01, 8.36293796e-01, 8.18950870e-01, 8.00633131e-01, + 7.81370064e-01, 7.61198780e-01, 7.40163807e-01, 7.18316799e-01, + 6.95716161e-01, 6.72426605e-01, 6.48518633e-01, 6.24067964e-01, + 5.99154896e-01, 5.73863636e-01, 5.48281575e-01, 5.22498546e-01, + 4.96606050e-01, 4.70696475e-01, 4.44862309e-01, 4.19195353e-01, + 3.93785956e-01, 3.68722258e-01, 3.44089476e-01, 3.19969212e-01, + 2.96438816e-01, 2.73570790e-01, 2.51432255e-01, 2.30084468e-01, + 2.09582413e-01, 1.89974455e-01, 1.71302066e-01, 1.53599623e-01, + 1.36894275e-01, 1.21205893e-01, 1.06547081e-01, 9.29232660e-02, + 8.03328498e-02, 6.87674290e-02, 5.82120708e-02, 4.86456418e-02, + 4.00411787e-02, 3.23662853e-02, 2.55835280e-02, 1.96507673e-02, + 1.45212685e-02, 1.01430808e-02, 6.45555741e-03, 3.36895835e-03, +}; + +static const float mdct_win_2m5_48k[__LC3_MDCT_WIN_LEN(2500, 48000)] = { + 2.75074638e-03, 4.77524515e-03, 6.99126548e-03, 9.47011816e-03, + 1.22441576e-02, 1.53355947e-02, 1.87626677e-02, 2.25415434e-02, + 2.66870142e-02, 3.12127707e-02, 3.61315008e-02, 4.14549100e-02, + 4.71937052e-02, 5.33575588e-02, 5.99550649e-02, 6.69936920e-02, + 7.44797342e-02, 8.24182658e-02, 9.08130967e-02, 9.96667332e-02, + 1.08980342e-01, 1.18753719e-01, 1.28985265e-01, 1.39671964e-01, + 1.50809372e-01, 1.62391604e-01, 1.74411338e-01, 1.86859813e-01, + 1.99726844e-01, 2.13000841e-01, 2.26668825e-01, 2.40716465e-01, + 2.55128110e-01, 2.69886836e-01, 2.84974488e-01, 3.00371741e-01, + 3.16058154e-01, 3.32012241e-01, 3.48211540e-01, 3.64632687e-01, + 3.81251499e-01, 3.98043054e-01, 4.14981787e-01, 4.32041576e-01, + 4.49195839e-01, 4.66417630e-01, 4.83679744e-01, 5.00954812e-01, + 5.18215407e-01, 5.35434150e-01, 5.52583807e-01, 5.69637402e-01, + 5.86568309e-01, 6.03350364e-01, 6.19957957e-01, 6.36366131e-01, + 6.52550680e-01, 6.68488237e-01, 6.84156362e-01, 6.99533624e-01, + 7.14599684e-01, 7.29335364e-01, 7.43722715e-01, 7.57745082e-01, + 7.71387158e-01, 7.84635031e-01, 7.97476230e-01, 8.09899758e-01, + 8.21896119e-01, 8.33457339e-01, 8.44576978e-01, 8.55250134e-01, + 8.65473441e-01, 8.75245054e-01, 8.84564635e-01, 8.93433321e-01, + 9.01853689e-01, 9.09829718e-01, 9.17366735e-01, 9.24471360e-01, + 9.31151439e-01, 9.37415982e-01, 9.43275077e-01, 9.48739819e-01, + 9.53822215e-01, 9.58535102e-01, 9.62892048e-01, 9.66907259e-01, + 9.70595479e-01, 9.73971891e-01, 9.77052016e-01, 9.79851615e-01, + 9.82386589e-01, 9.84672882e-01, 9.86726389e-01, 9.88562863e-01, + 9.90197830e-01, 9.91646510e-01, 9.92923740e-01, 9.94043905e-01, + 9.95020875e-01, 9.95867954e-01, 9.96597827e-01, 9.97222527e-01, + 9.97753399e-01, 9.98201077e-01, 9.98575471e-01, 9.98885756e-01, + 9.99140376e-01, 9.99347044e-01, 9.99512763e-01, 9.99643838e-01, + 9.99745907e-01, 9.99823966e-01, 9.99882403e-01, 9.99925037e-01, + 9.99955157e-01, 9.99975561e-01, 9.99988598e-01, 9.99996217e-01, + 9.99996217e-01, 9.99988598e-01, 9.99975561e-01, 9.99955157e-01, + 9.99925037e-01, 9.99882403e-01, 9.99823966e-01, 9.99745907e-01, + 9.99643838e-01, 9.99512763e-01, 9.99347044e-01, 9.99140376e-01, + 9.98885756e-01, 9.98575471e-01, 9.98201077e-01, 9.97753399e-01, + 9.97222527e-01, 9.96597827e-01, 9.95867954e-01, 9.95020875e-01, + 9.94043905e-01, 9.92923740e-01, 9.91646510e-01, 9.90197830e-01, + 9.88562863e-01, 9.86726389e-01, 9.84672882e-01, 9.82386589e-01, + 9.79851615e-01, 9.77052016e-01, 9.73971891e-01, 9.70595479e-01, + 9.66907259e-01, 9.62892048e-01, 9.58535102e-01, 9.53822215e-01, + 9.48739819e-01, 9.43275077e-01, 9.37415982e-01, 9.31151439e-01, + 9.24471360e-01, 9.17366735e-01, 9.09829718e-01, 9.01853689e-01, + 8.93433321e-01, 8.84564635e-01, 8.75245054e-01, 8.65473441e-01, + 8.55250134e-01, 8.44576978e-01, 8.33457339e-01, 8.21896119e-01, + 8.09899758e-01, 7.97476230e-01, 7.84635031e-01, 7.71387158e-01, + 7.57745082e-01, 7.43722715e-01, 7.29335364e-01, 7.14599684e-01, + 6.99533624e-01, 6.84156362e-01, 6.68488237e-01, 6.52550680e-01, + 6.36366131e-01, 6.19957957e-01, 6.03350364e-01, 5.86568309e-01, + 5.69637402e-01, 5.52583807e-01, 5.35434150e-01, 5.18215407e-01, + 5.00954812e-01, 4.83679744e-01, 4.66417630e-01, 4.49195839e-01, + 4.32041576e-01, 4.14981787e-01, 3.98043054e-01, 3.81251499e-01, + 3.64632687e-01, 3.48211540e-01, 3.32012241e-01, 3.16058154e-01, + 3.00371741e-01, 2.84974488e-01, 2.69886836e-01, 2.55128110e-01, + 2.40716465e-01, 2.26668825e-01, 2.13000841e-01, 1.99726844e-01, + 1.86859813e-01, 1.74411338e-01, 1.62391604e-01, 1.50809372e-01, + 1.39671964e-01, 1.28985265e-01, 1.18753719e-01, 1.08980342e-01, + 9.96667332e-02, 9.08130967e-02, 8.24182658e-02, 7.44797342e-02, + 6.69936920e-02, 5.99550649e-02, 5.33575588e-02, 4.71937052e-02, + 4.14549100e-02, 3.61315008e-02, 3.12127707e-02, 2.66870142e-02, + 2.25415434e-02, 1.87626677e-02, 1.53355947e-02, 1.22441576e-02, + 9.47011816e-03, 6.99126548e-03, 4.77524515e-03, 2.75074638e-03, +}; + +#if LC3_PLUS_HR + +static const float mdct_win_2m5_48k_hr[__LC3_MDCT_WIN_LEN(2500, 48000)] = { + 1.92887526e-07, 1.26862312e-06, 3.73694297e-06, 8.64938647e-06, + 1.75249988e-05, 3.25100409e-05, 5.65499504e-05, 9.35865319e-05, + 1.48780979e-04, 2.28761899e-04, 3.41896375e-04, 4.98580979e-04, + 7.11548259e-04, 9.96182440e-04, 1.37083745e-03, 1.85714674e-03, + 2.48031598e-03, 3.26938415e-03, 4.25744150e-03, 5.48178842e-03, + 6.98402245e-03, 8.81003775e-03, 1.10099232e-02, 1.36377569e-02, + 1.67512707e-02, 2.04114113e-02, 2.46817525e-02, 2.96278261e-02, + 3.53163108e-02, 4.18141559e-02, 4.91876006e-02, 5.75011559e-02, + 6.68165460e-02, 7.71916136e-02, 8.86792317e-02, 1.01326235e-01, + 1.15172401e-01, 1.30249396e-01, 1.46579877e-01, 1.64176553e-01, + 1.83041364e-01, 2.03164726e-01, 2.24524856e-01, 2.47087196e-01, + 2.70803988e-01, 2.95613915e-01, 3.21442008e-01, 3.48199695e-01, + 3.75785023e-01, 4.04083431e-01, 4.32968378e-01, 4.62302625e-01, + 4.91939783e-01, 5.21726012e-01, 5.51502347e-01, 5.81106782e-01, + 6.10377192e-01, 6.39154077e-01, 6.67283058e-01, 6.94617987e-01, + 7.21023440e-01, 7.46377110e-01, 7.70571768e-01, 7.93517113e-01, + 8.15140784e-01, 8.35389018e-01, 8.54227006e-01, 8.71638596e-01, + 8.87625158e-01, 9.02204990e-01, 9.15411413e-01, 9.27291155e-01, + 9.37902570e-01, 9.47313428e-01, 9.55598950e-01, 9.62839723e-01, + 9.69119847e-01, 9.74524975e-01, 9.79140759e-01, 9.83051181e-01, + 9.86337543e-01, 9.89076972e-01, 9.91342008e-01, 9.93199587e-01, + 9.94710743e-01, 9.95930433e-01, 9.96907234e-01, 9.97683644e-01, + 9.98296261e-01, 9.98776138e-01, 9.99149203e-01, 9.99436796e-01, + 9.99656200e-01, 9.99821365e-01, 9.99943137e-01, 1.00003016e+00, + 1.00008917e+00, 1.00012529e+00, 1.00014281e+00, 1.00014508e+00, + 1.00013494e+00, 1.00011492e+00, 1.00008726e+00, 1.00005412e+00, + 1.00001764e+00, 9.99979734e-01, 9.99942362e-01, 9.99907196e-01, + 9.99876022e-01, 9.99850094e-01, 9.99830663e-01, 9.99818563e-01, + 9.99814391e-01, 9.99818325e-01, 9.99830186e-01, 9.99849498e-01, + 9.99875486e-01, 9.99906898e-01, 9.99942422e-01, 9.99980509e-01, + 1.00001943e+00, 1.00005758e+00, 1.00009310e+00, 1.00012457e+00, + 1.00015044e+00, 1.00016987e+00, 1.00018167e+00, 1.00018561e+00, + 1.00018144e+00, 1.00016928e+00, 1.00014985e+00, 1.00012374e+00, + 1.00009227e+00, 1.00005662e+00, 1.00001836e+00, 9.99978960e-01, + 9.99939740e-01, 9.99902129e-01, 9.99867082e-01, 9.99835134e-01, + 9.99806285e-01, 9.99779761e-01, 9.99753773e-01, 9.99725282e-01, + 9.99689877e-01, 9.99641180e-01, 9.99570787e-01, 9.99467850e-01, + 9.99318600e-01, 9.99105930e-01, 9.98809040e-01, 9.98403072e-01, + 9.97858584e-01, 9.97141182e-01, 9.96211350e-01, 9.95023966e-01, + 9.93528485e-01, 9.91668522e-01, 9.89382327e-01, 9.86602664e-01, + 9.83257711e-01, 9.79271173e-01, 9.74563420e-01, 9.69052374e-01, + 9.62654769e-01, 9.55287457e-01, 9.46869195e-01, 9.37322080e-01, + 9.26573634e-01, 9.14558887e-01, 9.01221931e-01, 8.86518419e-01, + 8.70416999e-01, 8.52901220e-01, 8.33971083e-01, 8.13643873e-01, + 7.91955233e-01, 7.68959403e-01, 7.44728804e-01, 7.19353676e-01, + 6.92940772e-01, 6.65611804e-01, 6.37501359e-01, 6.08754635e-01, + 5.79524696e-01, 5.49970031e-01, 5.20251453e-01, 4.90529478e-01, + 4.60961968e-01, 4.31701392e-01, 4.02893007e-01, 3.74672860e-01, + 3.47166419e-01, 3.20487350e-01, 2.94736743e-01, 2.70002425e-01, + 2.46358722e-01, 2.23866433e-01, 2.02572897e-01, 1.82512373e-01, + 1.63706377e-01, 1.46164373e-01, 1.29884347e-01, 1.14853561e-01, + 1.01049446e-01, 8.84404257e-02, 7.69868940e-02, 6.66421950e-02, + 5.73536530e-02, 4.90636751e-02, 4.17107828e-02, 3.52307148e-02, + 2.95574907e-02, 2.46244166e-02, 2.03650557e-02, 1.67141166e-02, + 1.36082442e-02, 1.09867034e-02, 8.79194960e-03, 6.97008055e-03, + 5.47116203e-03, 4.24943818e-03, 3.26343346e-03, 2.47595203e-03, + 1.85399409e-03, 1.36859657e-03, 9.94618051e-04, 7.10477470e-04, + 4.97864152e-04, 3.41428356e-04, 2.28464938e-04, 1.48598730e-04, + 9.34789787e-05, 5.64894217e-05, 3.24779357e-05, 1.75092246e-05, + 8.64240701e-06, 3.73430225e-06, 1.26786131e-06, 1.92776696e-07, +}; + +static const float mdct_win_2m5_96k_hr[__LC3_MDCT_WIN_LEN(2500, 96000)] = { + 1.36335345e-07, 4.57767612e-07, 9.97567554e-07, 1.84077624e-06, + 3.09224833e-06, 4.88094383e-06, 7.36381799e-06, 1.07300075e-05, + 1.52053863e-05, 2.10575054e-05, 2.86009363e-05, 3.82030121e-05, + 5.02899893e-05, 6.53535899e-05, 8.39579952e-05, 1.06747175e-04, + 1.34452668e-04, 1.67901671e-04, 2.08025551e-04, 2.55868625e-04, + 3.12597229e-04, 3.79509147e-04, 4.58043127e-04, 5.49788703e-04, + 6.56496093e-04, 7.80086033e-04, 9.22659819e-04, 1.08650920e-03, + 1.27412600e-03, 1.48821168e-03, 1.73168664e-03, 2.00769864e-03, + 2.31963093e-03, 2.67111068e-03, 3.06601473e-03, 3.50847607e-03, + 4.00288915e-03, 4.55391267e-03, 5.16647473e-03, 5.84577024e-03, + 6.59726607e-03, 7.42669497e-03, 8.34005512e-03, 9.34360363e-03, + 1.04438523e-02, 1.16475578e-02, 1.29617099e-02, 1.43935224e-02, + 1.59504171e-02, 1.76400058e-02, 1.94700807e-02, 2.14485861e-02, + 2.35835947e-02, 2.58832965e-02, 2.83559617e-02, 3.10099237e-02, + 3.38535421e-02, 3.68951820e-02, 4.01431806e-02, 4.36058082e-02, + 4.72912528e-02, 5.12075722e-02, 5.53626679e-02, 5.97642474e-02, + 6.44197986e-02, 6.93365484e-02, 7.45214298e-02, 7.99810365e-02, + 8.57216269e-02, 9.17490497e-02, 9.80687290e-02, 1.04685634e-01, + 1.11604236e-01, 1.18828513e-01, 1.26361862e-01, 1.34207115e-01, + 1.42366499e-01, 1.50841609e-01, 1.59633383e-01, 1.68742076e-01, + 1.78167209e-01, 1.87907621e-01, 1.97961360e-01, 2.08325714e-01, + 2.18997195e-01, 2.29971498e-01, 2.41243511e-01, 2.52807260e-01, + 2.64655977e-01, 2.76782036e-01, 2.89176911e-01, 3.01831275e-01, + 3.14734906e-01, 3.27876776e-01, 3.41245025e-01, 3.54826927e-01, + 3.68608981e-01, 3.82576853e-01, 3.96715522e-01, 4.11009163e-01, + 4.25441355e-01, 4.39994961e-01, 4.54652220e-01, 4.69394863e-01, + 4.84204173e-01, 4.99060899e-01, 5.13945460e-01, 5.28838038e-01, + 5.43718576e-01, 5.58566749e-01, 5.73362410e-01, 5.88085234e-01, + 6.02715075e-01, 6.17232025e-01, 6.31616414e-01, 6.45848989e-01, + 6.59910858e-01, 6.73783839e-01, 6.87450290e-01, 7.00893283e-01, + 7.14096606e-01, 7.27045000e-01, 7.39724100e-01, 7.52120554e-01, + 7.64221907e-01, 7.76016891e-01, 7.87495315e-01, 7.98648119e-01, + 8.09467375e-01, 8.19946468e-01, 8.30079675e-01, 8.39862764e-01, + 8.49292517e-01, 8.58366787e-01, 8.67084682e-01, 8.75446379e-01, + 8.83453131e-01, 8.91107082e-01, 8.98411512e-01, 9.05370474e-01, + 9.11989033e-01, 9.18272913e-01, 9.24228728e-01, 9.29863691e-01, + 9.35185611e-01, 9.40202892e-01, 9.44924474e-01, 9.49359715e-01, + 9.53518271e-01, 9.57410157e-01, 9.61045623e-01, 9.64435160e-01, + 9.67589259e-01, 9.70518589e-01, 9.73233819e-01, 9.75745618e-01, + 9.78064537e-01, 9.80201006e-01, 9.82165277e-01, 9.83967602e-01, + 9.85617757e-01, 9.87125397e-01, 9.88499880e-01, 9.89750206e-01, + 9.90885139e-01, 9.91913080e-01, 9.92841959e-01, 9.93679583e-01, + 9.94433045e-01, 9.95109439e-01, 9.95715141e-01, 9.96256351e-01, + 9.96738791e-01, 9.97167945e-01, 9.97548699e-01, 9.97885823e-01, + 9.98183608e-01, 9.98446047e-01, 9.98676717e-01, 9.98879075e-01, + 9.99056041e-01, 9.99210536e-01, 9.99344945e-01, 9.99461591e-01, + 9.99562562e-01, 9.99649644e-01, 9.99724448e-01, 9.99788404e-01, + 9.99842882e-01, 9.99888957e-01, 9.99927700e-01, 9.99959946e-01, + 9.99986470e-01, 1.00000799e+00, 1.00002515e+00, 1.00003839e+00, + 1.00004816e+00, 1.00005496e+00, 1.00005913e+00, 1.00006092e+00, + 1.00006080e+00, 1.00005877e+00, 1.00005519e+00, 1.00005031e+00, + 1.00004435e+00, 1.00003731e+00, 1.00002968e+00, 1.00002134e+00, + 1.00001252e+00, 1.00000334e+00, 9.99994040e-01, 9.99984682e-01, + 9.99975443e-01, 9.99966383e-01, 9.99957621e-01, 9.99949336e-01, + 9.99941528e-01, 9.99934316e-01, 9.99927878e-01, 9.99922156e-01, + 9.99917269e-01, 9.99913335e-01, 9.99910295e-01, 9.99908268e-01, + 9.99907196e-01, 9.99907196e-01, 9.99908149e-01, 9.99910176e-01, + 9.99913156e-01, 9.99917090e-01, 9.99921918e-01, 9.99927640e-01, + 9.99934137e-01, 9.99941349e-01, 9.99949217e-01, 9.99957681e-01, + 9.99966562e-01, 9.99975860e-01, 9.99985397e-01, 9.99995112e-01, + 1.00000489e+00, 1.00001454e+00, 1.00002408e+00, 1.00003338e+00, + 1.00004232e+00, 1.00005078e+00, 1.00005865e+00, 1.00006592e+00, + 1.00007236e+00, 1.00007808e+00, 1.00008297e+00, 1.00008690e+00, + 1.00008988e+00, 1.00009179e+00, 1.00009286e+00, 1.00009274e+00, + 1.00009179e+00, 1.00008965e+00, 1.00008667e+00, 1.00008261e+00, + 1.00007772e+00, 1.00007200e+00, 1.00006545e+00, 1.00005817e+00, + 1.00005031e+00, 1.00004172e+00, 1.00003278e+00, 1.00002337e+00, + 1.00001371e+00, 1.00000370e+00, 9.99993682e-01, 9.99983490e-01, + 9.99973357e-01, 9.99963224e-01, 9.99953210e-01, 9.99943376e-01, + 9.99933660e-01, 9.99924064e-01, 9.99914587e-01, 9.99905109e-01, + 9.99895573e-01, 9.99885738e-01, 9.99875546e-01, 9.99864578e-01, + 9.99852657e-01, 9.99839306e-01, 9.99824166e-01, 9.99806523e-01, + 9.99785900e-01, 9.99761462e-01, 9.99732316e-01, 9.99697566e-01, + 9.99655962e-01, 9.99606311e-01, 9.99547124e-01, 9.99476731e-01, + 9.99393344e-01, 9.99294996e-01, 9.99179363e-01, 9.99043941e-01, + 9.98886049e-01, 9.98702645e-01, 9.98490512e-01, 9.98246014e-01, + 9.97965276e-01, 9.97644246e-01, 9.97278214e-01, 9.96862471e-01, + 9.96391773e-01, 9.95860577e-01, 9.95262980e-01, 9.94592726e-01, + 9.93843138e-01, 9.93007302e-01, 9.92077827e-01, 9.91046965e-01, + 9.89906728e-01, 9.88648713e-01, 9.87264156e-01, 9.85744119e-01, + 9.84079301e-01, 9.82260108e-01, 9.80276704e-01, 9.78119195e-01, + 9.75777209e-01, 9.73240614e-01, 9.70498860e-01, 9.67541456e-01, + 9.64357972e-01, 9.60937798e-01, 9.57270682e-01, 9.53346252e-01, + 9.49154437e-01, 9.44685459e-01, 9.39929724e-01, 9.34878111e-01, + 9.29521918e-01, 9.23852861e-01, 9.17863250e-01, 9.11546052e-01, + 9.04894829e-01, 8.97903919e-01, 8.90568554e-01, 8.82884681e-01, + 8.74849200e-01, 8.66460025e-01, 8.57716084e-01, 8.48617196e-01, + 8.39164436e-01, 8.29359889e-01, 8.19206893e-01, 8.08709860e-01, + 7.97874272e-01, 7.86707044e-01, 7.75215983e-01, 7.63410211e-01, + 7.51299858e-01, 7.38896310e-01, 7.26211846e-01, 7.13259816e-01, + 7.00054646e-01, 6.86611593e-01, 6.72946632e-01, 6.59076810e-01, + 6.45019531e-01, 6.30793154e-01, 6.16416335e-01, 6.01908267e-01, + 5.87288618e-01, 5.72577178e-01, 5.57793975e-01, 5.42959273e-01, + 5.28093219e-01, 5.13215959e-01, 4.98347521e-01, 4.83507633e-01, + 4.68715757e-01, 4.53991085e-01, 4.39352304e-01, 4.24817562e-01, + 4.10404533e-01, 3.96130294e-01, 3.82011205e-01, 3.68063033e-01, + 3.54300767e-01, 3.40738595e-01, 3.27390045e-01, 3.14267725e-01, + 3.01383466e-01, 2.88748264e-01, 2.76372313e-01, 2.64264882e-01, + 2.52434462e-01, 2.40888610e-01, 2.29634136e-01, 2.18676925e-01, + 2.08022103e-01, 1.97673932e-01, 1.87635899e-01, 1.77910715e-01, + 1.68500274e-01, 1.59405768e-01, 1.50627658e-01, 1.42165691e-01, + 1.34018898e-01, 1.26185730e-01, 1.18663922e-01, 1.11450672e-01, + 1.04542568e-01, 9.79356542e-02, 9.16254595e-02, 8.56070295e-02, + 7.98749477e-02, 7.44233727e-02, 6.92460760e-02, 6.43364564e-02, + 5.96875995e-02, 5.52923009e-02, 5.11430874e-02, 4.72322591e-02, + 4.35519405e-02, 4.00940813e-02, 3.68505120e-02, 3.38129811e-02, + 3.09731625e-02, 2.83227116e-02, 2.58532818e-02, 2.35565584e-02, + 2.14242823e-02, 1.94482822e-02, 1.76204946e-02, 1.59329921e-02, + 1.43779973e-02, 1.29479105e-02, 1.16353221e-02, 1.04330294e-02, + 9.33405478e-03, 8.33165180e-03, 7.41932075e-03, 6.59081247e-03, + 5.84013900e-03, 5.16157458e-03, 4.54966258e-03, 3.99921415e-03, + 3.50530911e-03, 3.06329457e-03, 2.66878284e-03, 2.31764605e-03, + 2.00601248e-03, 1.73026032e-03, 1.48701016e-03, 1.27311819e-03, + 1.08566787e-03, 9.21960978e-04, 7.79508497e-04, 6.56021410e-04, + 5.49400807e-04, 4.57728049e-04, 3.79254925e-04, 3.12393560e-04, + 2.55706662e-04, 2.07897814e-04, 1.67801816e-04, 1.34375354e-04, + 1.06687941e-04, 8.39131390e-05, 6.53200550e-05, 5.02652765e-05, + 3.81850987e-05, 2.85881870e-05, 2.10486251e-05, 1.51993554e-05, + 1.07260339e-05, 7.36129550e-06, 4.87941543e-06, 3.09137795e-06, + 1.84032115e-06, 9.97358256e-07, 4.57689993e-07, 1.36315705e-07, +}; + +#endif /* LC3_PLUS_HR */ + +static const float mdct_win_5m_8k[__LC3_MDCT_WIN_LEN(5000, 8000)] = { + 9.95908659e-04, 3.81905679e-03, 9.54083261e-03, 1.92165980e-02, + 3.38271908e-02, 5.42483167e-02, 8.12077767e-02, 1.15217189e-01, + 1.56494233e-01, 2.04936342e-01, 2.60116658e-01, 3.21281416e-01, + 3.87347300e-01, 4.56949708e-01, 5.28519296e-01, 6.00352249e-01, + 6.70689638e-01, 7.37804446e-01, 8.00092531e-01, 8.56140918e-01, + 9.04827229e-01, 9.45368503e-01, 9.77350743e-01, 1.00080087e+00, + 1.01617159e+00, 1.02431525e+00, 1.02641543e+00, 1.02385837e+00, + 1.01813571e+00, 1.01079482e+00, 1.00340651e+00, 9.96783127e-01, + 9.92099552e-01, 9.89220694e-01, 9.87965832e-01, 9.88127353e-01, + 9.89480554e-01, 9.91784992e-01, 9.94784758e-01, 9.98211967e-01, + 1.00179124e+00, 1.00524258e+00, 1.00828305e+00, 1.01063128e+00, + 1.01201530e+00, 1.01218075e+00, 1.01089677e+00, 1.00796336e+00, + 1.00322726e+00, 9.96605055e-01, 9.86828423e-01, 9.73125029e-01, + 9.54063648e-01, 9.28386428e-01, 8.95091686e-01, 8.53476936e-01, + 8.03209093e-01, 7.44473520e-01, 6.78078703e-01, 6.05397045e-01, + 5.28207751e-01, 4.48655296e-01, 3.69187599e-01, 2.92456641e-01, + 2.21071854e-01, 1.57314858e-01, 1.03052576e-01, 5.98273224e-02, + 2.87183192e-02, 9.68388493e-03, +}; + +static const float mdct_win_5m_16k[__LC3_MDCT_WIN_LEN(5000, 16000)] = { + 6.14338818e-04, 1.48958283e-03, 2.88410496e-03, 4.93429883e-03, + 7.77913046e-03, 1.15491061e-02, 1.63715562e-02, 2.23711616e-02, + 2.96615969e-02, 3.83566333e-02, 4.85561099e-02, 6.03505574e-02, + 7.38228820e-02, 8.90356369e-02, 1.06035623e-01, 1.24853486e-01, + 1.45493189e-01, 1.67943556e-01, 1.92172862e-01, 2.18123826e-01, + 2.45725974e-01, 2.74883943e-01, 3.05482471e-01, 3.37387380e-01, + 3.70441593e-01, 4.04474963e-01, 4.39300436e-01, 4.74722545e-01, + 5.10534149e-01, 5.46520192e-01, 5.82465810e-01, 6.18145266e-01, + 6.53341146e-01, 6.87836730e-01, 7.21417603e-01, 7.53888797e-01, + 7.85054657e-01, 8.14739745e-01, 8.42781936e-01, 8.69037674e-01, + 8.93393548e-01, 9.15748356e-01, 9.36027020e-01, 9.54173114e-01, + 9.70163547e-01, 9.84003644e-01, 9.95719942e-01, 1.00537427e+00, + 1.01304666e+00, 1.01884338e+00, 1.02289695e+00, 1.02535529e+00, + 1.02638288e+00, 1.02615553e+00, 1.02485397e+00, 1.02266460e+00, + 1.01977940e+00, 1.01639169e+00, 1.01269703e+00, 1.00888519e+00, + 1.00537874e+00, 1.00156378e+00, 9.98253156e-01, 9.95434664e-01, + 9.93095027e-01, 9.91217091e-01, 9.89780519e-01, 9.88762494e-01, + 9.88138324e-01, 9.87881941e-01, 9.87966213e-01, 9.88363051e-01, + 9.89043407e-01, 9.89977232e-01, 9.91133456e-01, 9.92480044e-01, + 9.93984121e-01, 9.95612147e-01, 9.97330059e-01, 9.99103363e-01, + 1.00089744e+00, 1.00267709e+00, 1.00440719e+00, 1.00605229e+00, + 1.00757693e+00, 1.00894586e+00, 1.01012424e+00, 1.01107797e+00, + 1.01177396e+00, 1.01218036e+00, 1.01226671e+00, 1.01200406e+00, + 1.01136522e+00, 1.01032500e+00, 1.00886073e+00, 1.00695298e+00, + 1.00458627e+00, 1.00174990e+00, 9.98438663e-01, 9.94650033e-01, + 9.89575685e-01, 9.83830313e-01, 9.76999916e-01, 9.68914116e-01, + 9.59403812e-01, 9.48308632e-01, 9.35486022e-01, 9.20810131e-01, + 9.04173226e-01, 8.85488225e-01, 8.64686495e-01, 8.41723747e-01, + 8.16587571e-01, 7.89298635e-01, 7.59917189e-01, 7.28547452e-01, + 6.95328294e-01, 6.60433402e-01, 6.24066143e-01, 5.86446142e-01, + 5.47816066e-01, 5.08449976e-01, 4.68636143e-01, 4.28678989e-01, + 3.88903272e-01, 3.49643142e-01, 3.11236082e-01, 2.74012847e-01, + 2.38284723e-01, 2.04337983e-01, 1.72430586e-01, 1.42793979e-01, + 1.15638588e-01, 9.11582177e-02, 6.95274904e-02, 5.08897541e-02, + 3.53343019e-02, 2.28668041e-02, 1.33800502e-02, 6.64050653e-03, +}; + +static const float mdct_win_5m_24k[__LC3_MDCT_WIN_LEN(5000, 24000)] = { + 5.08722763e-04, 9.95908659e-04, 1.68220801e-03, 2.60969726e-03, + 3.81905679e-03, 5.34931959e-03, 7.24390638e-03, 9.54083261e-03, + 1.22763764e-02, 1.54895024e-02, 1.92165980e-02, 2.34936962e-02, + 2.83519958e-02, 3.38271908e-02, 3.99493954e-02, 4.67477524e-02, + 5.42483167e-02, 6.24777078e-02, 7.14583592e-02, 8.12077767e-02, + 9.17440041e-02, 1.03076496e-01, 1.15217189e-01, 1.28166571e-01, + 1.41926438e-01, 1.56494233e-01, 1.71859319e-01, 1.88013425e-01, + 2.04936342e-01, 2.22612306e-01, 2.41015124e-01, 2.60116658e-01, + 2.79887101e-01, 3.00288014e-01, 3.21281416e-01, 3.42820846e-01, + 3.64859656e-01, 3.87347300e-01, 4.10229495e-01, 4.33449453e-01, + 4.56949708e-01, 4.80669640e-01, 5.04547382e-01, 5.28519296e-01, + 5.52519610e-01, 5.76487245e-01, 6.00352249e-01, 6.24050987e-01, + 6.47518259e-01, 6.70689638e-01, 6.93502907e-01, 7.15892752e-01, + 7.37804446e-01, 7.59178724e-01, 7.79958661e-01, 8.00092531e-01, + 8.19531865e-01, 8.38228896e-01, 8.56140918e-01, 8.73231695e-01, + 8.89470202e-01, 9.04827229e-01, 9.19273638e-01, 9.32794041e-01, + 9.45368503e-01, 9.56988393e-01, 9.67648642e-01, 9.77350743e-01, + 9.86102783e-01, 9.93912241e-01, 1.00080087e+00, 1.00678781e+00, + 1.01190127e+00, 1.01617159e+00, 1.01963641e+00, 1.02233661e+00, + 1.02431525e+00, 1.02562130e+00, 1.02630344e+00, 1.02641543e+00, + 1.02600793e+00, 1.02513744e+00, 1.02385837e+00, 1.02222694e+00, + 1.02030055e+00, 1.01813571e+00, 1.01579215e+00, 1.01332597e+00, + 1.01079482e+00, 1.00826513e+00, 1.00604687e+00, 1.00340651e+00, + 1.00097740e+00, 9.98770454e-01, 9.96783127e-01, 9.95011891e-01, + 9.93452397e-01, 9.92099552e-01, 9.90947600e-01, 9.89990243e-01, + 9.89220694e-01, 9.88631804e-01, 9.88216090e-01, 9.87965832e-01, + 9.87873077e-01, 9.87929693e-01, 9.88127353e-01, 9.88457554e-01, + 9.88911587e-01, 9.89480554e-01, 9.90155346e-01, 9.90926656e-01, + 9.91784992e-01, 9.92720684e-01, 9.93723921e-01, 9.94784758e-01, + 9.95893149e-01, 9.97038957e-01, 9.98211967e-01, 9.99402034e-01, + 1.00059832e+00, 1.00179124e+00, 1.00296984e+00, 1.00412379e+00, + 1.00524258e+00, 1.00631572e+00, 1.00733269e+00, 1.00828305e+00, + 1.00915642e+00, 1.00994254e+00, 1.01063128e+00, 1.01121274e+00, + 1.01167723e+00, 1.01201530e+00, 1.01221778e+00, 1.01227579e+00, + 1.01218075e+00, 1.01192443e+00, 1.01149892e+00, 1.01089677e+00, + 1.01011097e+00, 1.00913509e+00, 1.00796336e+00, 1.00659076e+00, + 1.00501312e+00, 1.00322726e+00, 1.00123106e+00, 9.99023556e-01, + 9.96605055e-01, 9.93989471e-01, 9.90453920e-01, 9.86828423e-01, + 9.82771674e-01, 9.78220667e-01, 9.73125029e-01, 9.67432353e-01, + 9.61094704e-01, 9.54063648e-01, 9.46295299e-01, 9.37748911e-01, + 9.28386428e-01, 9.18176261e-01, 9.07086156e-01, 8.95091686e-01, + 8.82169624e-01, 8.68302529e-01, 8.53476936e-01, 8.37685201e-01, + 8.20927526e-01, 8.03209093e-01, 7.84545048e-01, 7.64955485e-01, + 7.44473520e-01, 7.23134807e-01, 7.00986056e-01, 6.78078703e-01, + 6.54468651e-01, 6.30221215e-01, 6.05397045e-01, 5.80071577e-01, + 5.54312928e-01, 5.28207751e-01, 5.01836972e-01, 4.75290296e-01, + 4.48655296e-01, 4.22028112e-01, 3.95505797e-01, 3.69187599e-01, + 3.43173285e-01, 3.17563302e-01, 2.92456641e-01, 2.67946378e-01, + 2.44123133e-01, 2.21071854e-01, 1.98871915e-01, 1.77596763e-01, + 1.57314858e-01, 1.38090336e-01, 1.19983750e-01, 1.03052576e-01, + 8.73508501e-02, 7.29281158e-02, 5.98273224e-02, 4.80817884e-02, + 3.77113530e-02, 2.87183192e-02, 2.10835203e-02, 1.47628941e-02, + 9.68388493e-03, 5.64216879e-03, +}; + +static const float mdct_win_5m_32k[__LC3_MDCT_WIN_LEN(5000, 32000)] = { + 4.59588635e-04, 7.91932361e-04, 1.22792717e-03, 1.78365327e-03, + 2.47954941e-03, 3.32979945e-03, 4.35353548e-03, 5.56496516e-03, + 6.98610836e-03, 8.62988232e-03, 1.05134341e-02, 1.26508264e-02, + 1.50609045e-02, 1.77559123e-02, 2.07547598e-02, 2.40681372e-02, + 2.77120786e-02, 3.16993325e-02, 3.60460964e-02, 4.07612864e-02, + 4.58603812e-02, 5.13513668e-02, 5.72478022e-02, 6.35585474e-02, + 7.02945073e-02, 7.74598720e-02, 8.50663537e-02, 9.31164162e-02, + 1.01616296e-01, 1.10569081e-01, 1.19978929e-01, 1.29841729e-01, + 1.40162380e-01, 1.50937156e-01, 1.62163230e-01, 1.73835412e-01, + 1.85952036e-01, 1.98500883e-01, 2.11477855e-01, 2.24873256e-01, + 2.38676395e-01, 2.52872945e-01, 2.67454701e-01, 2.82403147e-01, + 2.97705015e-01, 3.13341912e-01, 3.29297670e-01, 3.45549016e-01, + 3.62079505e-01, 3.78864867e-01, 3.95885158e-01, 4.13114379e-01, + 4.30530830e-01, 4.48107672e-01, 4.65822779e-01, 4.83646639e-01, + 5.01556485e-01, 5.19522807e-01, 5.37519704e-01, 5.55518384e-01, + 5.73495781e-01, 5.91418665e-01, 6.09262289e-01, 6.26998116e-01, + 6.44600201e-01, 6.62038458e-01, 6.79290655e-01, 6.96325643e-01, + 7.13119439e-01, 7.29646991e-01, 7.45886459e-01, 7.61809472e-01, + 7.77395845e-01, 7.92620875e-01, 8.07466639e-01, 8.21910156e-01, + 8.35934316e-01, 8.49518047e-01, 8.62648584e-01, 8.75308323e-01, + 8.87488472e-01, 8.99173772e-01, 9.10352743e-01, 9.21014413e-01, + 9.31155619e-01, 9.40764474e-01, 9.49838224e-01, 9.58373260e-01, + 9.66369041e-01, 9.73823562e-01, 9.80744251e-01, 9.87129797e-01, + 9.92987227e-01, 9.98324140e-01, 1.00315076e+00, 1.00747371e+00, + 1.01130915e+00, 1.01466668e+00, 1.01756334e+00, 1.02001468e+00, + 1.02203987e+00, 1.02365426e+00, 1.02488162e+00, 1.02573929e+00, + 1.02625071e+00, 1.02643667e+00, 1.02632086e+00, 1.02592292e+00, + 1.02526998e+00, 1.02438219e+00, 1.02328494e+00, 1.02200083e+00, + 1.02055597e+00, 1.01897139e+00, 1.01727518e+00, 1.01548913e+00, + 1.01363936e+00, 1.01174775e+00, 1.00984084e+00, 1.00793976e+00, + 1.00640740e+00, 1.00437483e+00, 1.00246981e+00, 1.00068907e+00, + 9.99034600e-01, 9.97502490e-01, 9.96094155e-01, 9.94805124e-01, + 9.93636273e-01, 9.92582654e-01, 9.91644701e-01, 9.90817076e-01, + 9.90099845e-01, 9.89487369e-01, 9.88979432e-01, 9.88570179e-01, + 9.88259191e-01, 9.88040442e-01, 9.87913369e-01, 9.87871810e-01, + 9.87915076e-01, 9.88036894e-01, 9.88236456e-01, 9.88507369e-01, + 9.88848709e-01, 9.89253949e-01, 9.89722041e-01, 9.90246329e-01, + 9.90825634e-01, 9.91453181e-01, 9.92127681e-01, 9.92842250e-01, + 9.93595510e-01, 9.94380481e-01, 9.95195724e-01, 9.96034188e-01, + 9.96894383e-01, 9.97769201e-01, 9.98657113e-01, 9.99550974e-01, + 1.00044923e+00, 1.00134469e+00, 1.00223579e+00, 1.00311529e+00, + 1.00398160e+00, 1.00482747e+00, 1.00565128e+00, 1.00644577e+00, + 1.00720935e+00, 1.00793478e+00, 1.00862050e+00, 1.00925931e+00, + 1.00984974e+00, 1.01038469e+00, 1.01086278e+00, 1.01127704e+00, + 1.01162625e+00, 1.01190357e+00, 1.01210795e+00, 1.01223276e+00, + 1.01227709e+00, 1.01223451e+00, 1.01210432e+00, 1.01188029e+00, + 1.01156197e+00, 1.01114337e+00, 1.01062432e+00, 1.00999915e+00, + 1.00926803e+00, 1.00842570e+00, 1.00747277e+00, 1.00640448e+00, + 1.00522200e+00, 1.00392116e+00, 1.00250376e+00, 1.00096633e+00, + 9.99311401e-01, 9.97536270e-01, 9.95644231e-01, 9.93633392e-01, + 9.90867748e-01, 9.88232633e-01, 9.85362057e-01, 9.82230509e-01, + 9.78818585e-01, 9.75102633e-01, 9.71063185e-01, 9.66675967e-01, + 9.61924219e-01, 9.56784199e-01, 9.51239430e-01, 9.45270024e-01, + 9.38861570e-01, 9.31994644e-01, 9.24659203e-01, 9.16838340e-01, + 9.08521803e-01, 8.99696701e-01, 8.90356205e-01, 8.80487793e-01, + 8.70088421e-01, 8.59149213e-01, 8.47668639e-01, 8.35642897e-01, + 8.23075389e-01, 8.09964930e-01, 7.96320451e-01, 7.82146054e-01, + 7.67454182e-01, 7.52256346e-01, 7.36570205e-01, 7.20409055e-01, + 7.03797511e-01, 6.86754281e-01, 6.69304189e-01, 6.51471096e-01, + 6.33285483e-01, 6.14768539e-01, 5.95955378e-01, 5.76873796e-01, + 5.57553429e-01, 5.38032014e-01, 5.18345403e-01, 4.98525942e-01, + 4.78615607e-01, 4.58647304e-01, 4.38664366e-01, 4.18704689e-01, + 3.98812306e-01, 3.79026292e-01, 3.59391483e-01, 3.39947413e-01, + 3.20739242e-01, 3.01806111e-01, 2.83190595e-01, 2.64928837e-01, + 2.47060855e-01, 2.29620112e-01, 2.12643372e-01, 1.96160182e-01, + 1.80203520e-01, 1.64799688e-01, 1.49978755e-01, 1.35764352e-01, + 1.22184253e-01, 1.09260199e-01, 9.70178845e-02, 8.54768028e-02, + 7.46597638e-02, 6.45825432e-02, 5.52628119e-02, 4.67097698e-02, + 3.89324443e-02, 3.19297601e-02, 2.56981064e-02, 2.02225927e-02, + 1.54831778e-02, 1.14492491e-02, 8.07648266e-03, 5.30004408e-03, +}; + +static const float mdct_win_5m_48k[__LC3_MDCT_WIN_LEN(5000, 48000)] = { + 4.09010650e-04, 6.14338818e-04, 8.57175988e-04, 1.14701506e-03, + 1.48958283e-03, 1.88977038e-03, 2.35300080e-03, 2.88410496e-03, + 3.48821379e-03, 4.17004043e-03, 4.93429883e-03, 5.78707651e-03, + 6.73381174e-03, 7.77913046e-03, 8.92704496e-03, 1.01820289e-02, + 1.15491061e-02, 1.30334922e-02, 1.46395129e-02, 1.63715562e-02, + 1.82345538e-02, 2.02330949e-02, 2.23711616e-02, 2.46523735e-02, + 2.70810194e-02, 2.96615969e-02, 3.23988485e-02, 3.52960177e-02, + 3.83566333e-02, 4.15844793e-02, 4.49832242e-02, 4.85561099e-02, + 5.23059648e-02, 5.62362458e-02, 6.03505574e-02, 6.46518632e-02, + 6.91419575e-02, 7.38228820e-02, 7.86970933e-02, 8.37676164e-02, + 8.90356369e-02, 9.45019924e-02, 1.00168019e-01, 1.06035623e-01, + 1.12106022e-01, 1.18378855e-01, 1.24853486e-01, 1.31530285e-01, + 1.38410308e-01, 1.45493189e-01, 1.52777295e-01, 1.60260884e-01, + 1.67943556e-01, 1.75824562e-01, 1.83902012e-01, 1.92172862e-01, + 2.00634430e-01, 2.09285388e-01, 2.18123826e-01, 2.27146226e-01, + 2.36347921e-01, 2.45725974e-01, 2.55277155e-01, 2.64998109e-01, + 2.74883943e-01, 2.84929644e-01, 2.95130651e-01, 3.05482471e-01, + 3.15979964e-01, 3.26616979e-01, 3.37387380e-01, 3.48285592e-01, + 3.59305769e-01, 3.70441593e-01, 3.81686239e-01, 3.93032978e-01, + 4.04474963e-01, 4.16005110e-01, 4.27615960e-01, 4.39300436e-01, + 4.51051633e-01, 4.62861605e-01, 4.74722545e-01, 4.86626671e-01, + 4.98566451e-01, 5.10534149e-01, 5.22521279e-01, 5.34519051e-01, + 5.46520192e-01, 5.58517277e-01, 5.70502154e-01, 5.82465810e-01, + 5.94399172e-01, 6.06294818e-01, 6.18145266e-01, 6.29942202e-01, + 6.41676874e-01, 6.53341146e-01, 6.64927754e-01, 6.76429270e-01, + 6.87836730e-01, 6.99142147e-01, 7.10337961e-01, 7.21417603e-01, + 7.32374610e-01, 7.43200803e-01, 7.53888797e-01, 7.64431550e-01, + 7.74822315e-01, 7.85054657e-01, 7.95122352e-01, 8.05019386e-01, + 8.14739745e-01, 8.24277441e-01, 8.33626711e-01, 8.42781936e-01, + 8.51738619e-01, 8.60492087e-01, 8.69037674e-01, 8.77372045e-01, + 8.85492794e-01, 8.93393548e-01, 9.01072709e-01, 9.08524940e-01, + 9.15748356e-01, 9.22741384e-01, 9.29501747e-01, 9.36027020e-01, + 9.42314305e-01, 9.48362979e-01, 9.54173114e-01, 9.59743838e-01, + 9.65073839e-01, 9.70163547e-01, 9.75014336e-01, 9.79627719e-01, + 9.84003644e-01, 9.88142677e-01, 9.92047045e-01, 9.95719942e-01, + 9.99164081e-01, 1.00238131e+00, 1.00537427e+00, 1.00814672e+00, + 1.01070312e+00, 1.01304666e+00, 1.01518127e+00, 1.01711164e+00, + 1.01884338e+00, 1.02038171e+00, 1.02173110e+00, 1.02289695e+00, + 1.02388546e+00, 1.02470297e+00, 1.02535529e+00, 1.02584824e+00, + 1.02618837e+00, 1.02638288e+00, 1.02643810e+00, 1.02636013e+00, + 1.02615553e+00, 1.02583146e+00, 1.02539543e+00, 1.02485397e+00, + 1.02421348e+00, 1.02348118e+00, 1.02266460e+00, 1.02177090e+00, + 1.02080692e+00, 1.01977940e+00, 1.01869600e+00, 1.01756442e+00, + 1.01639169e+00, 1.01518492e+00, 1.01395084e+00, 1.01269703e+00, + 1.01143075e+00, 1.01015835e+00, 1.00888519e+00, 1.00759272e+00, + 1.00680560e+00, 1.00537874e+00, 1.00404905e+00, 1.00277836e+00, + 1.00156378e+00, 1.00040492e+00, 9.99301484e-01, 9.98253156e-01, + 9.97259546e-01, 9.96320213e-01, 9.95434664e-01, 9.94602354e-01, + 9.93822688e-01, 9.93095027e-01, 9.92418692e-01, 9.91792966e-01, + 9.91217091e-01, 9.90690276e-01, 9.90211700e-01, 9.89780519e-01, + 9.89395860e-01, 9.89056824e-01, 9.88762494e-01, 9.88511936e-01, + 9.88304203e-01, 9.88138324e-01, 9.88013316e-01, 9.87928190e-01, + 9.87881941e-01, 9.87873551e-01, 9.87901987e-01, 9.87966213e-01, + 9.88065178e-01, 9.88197816e-01, 9.88363051e-01, 9.88559796e-01, + 9.88786953e-01, 9.89043407e-01, 9.89328032e-01, 9.89639690e-01, + 9.89977232e-01, 9.90339493e-01, 9.90725297e-01, 9.91133456e-01, + 9.91562775e-01, 9.92012044e-01, 9.92480044e-01, 9.92965548e-01, + 9.93467321e-01, 9.93984121e-01, 9.94514697e-01, 9.95057793e-01, + 9.95612147e-01, 9.96176492e-01, 9.96749554e-01, 9.97330059e-01, + 9.97916725e-01, 9.98508264e-01, 9.99103363e-01, 9.99700348e-01, + 1.00029974e+00, 1.00089744e+00, 1.00149396e+00, 1.00208762e+00, + 1.00267709e+00, 1.00326105e+00, 1.00383818e+00, 1.00440719e+00, + 1.00496675e+00, 1.00551556e+00, 1.00605229e+00, 1.00657564e+00, + 1.00708429e+00, 1.00757693e+00, 1.00805228e+00, 1.00850902e+00, + 1.00894586e+00, 1.00936153e+00, 1.00975474e+00, 1.01012424e+00, + 1.01046877e+00, 1.01078709e+00, 1.01107797e+00, 1.01134021e+00, + 1.01157260e+00, 1.01177396e+00, 1.01194314e+00, 1.01207898e+00, + 1.01218036e+00, 1.01224617e+00, 1.01227531e+00, 1.01226671e+00, + 1.01221932e+00, 1.01213211e+00, 1.01200406e+00, 1.01183421e+00, + 1.01162157e+00, 1.01136522e+00, 1.01106425e+00, 1.01071779e+00, + 1.01032500e+00, 1.00988506e+00, 1.00939721e+00, 1.00886073e+00, + 1.00827495e+00, 1.00763922e+00, 1.00695298e+00, 1.00621571e+00, + 1.00542694e+00, 1.00458627e+00, 1.00369338e+00, 1.00274798e+00, + 1.00174990e+00, 1.00069900e+00, 9.99595249e-01, 9.98438663e-01, + 9.97229342e-01, 9.95967277e-01, 9.94650033e-01, 9.93240400e-01, + 9.91251152e-01, 9.89575685e-01, 9.87771321e-01, 9.85857748e-01, + 9.83830313e-01, 9.81682263e-01, 9.79407449e-01, 9.76999916e-01, + 9.74452836e-01, 9.71759750e-01, 9.68914116e-01, 9.65910162e-01, + 9.62742183e-01, 9.59403812e-01, 9.55888998e-01, 9.52192243e-01, + 9.48308632e-01, 9.44233254e-01, 9.39960724e-01, 9.35486022e-01, + 9.30805297e-01, 9.25914697e-01, 9.20810131e-01, 9.15487360e-01, + 9.09942607e-01, 9.04173226e-01, 8.98176373e-01, 8.91949024e-01, + 8.85488225e-01, 8.78791944e-01, 8.71858584e-01, 8.64686495e-01, + 8.57273814e-01, 8.49619586e-01, 8.41723747e-01, 8.33586272e-01, + 8.25207443e-01, 8.16587571e-01, 8.07728037e-01, 7.98631159e-01, + 7.89298635e-01, 7.79733095e-01, 7.69937953e-01, 7.59917189e-01, + 7.49675842e-01, 7.39217684e-01, 7.28547452e-01, 7.17671448e-01, + 7.06596231e-01, 6.95328294e-01, 6.83873906e-01, 6.72239531e-01, + 6.60433402e-01, 6.48464360e-01, 6.36339500e-01, 6.24066143e-01, + 6.11653033e-01, 5.99109864e-01, 5.86446142e-01, 5.73669485e-01, + 5.60788103e-01, 5.47816066e-01, 5.34761979e-01, 5.21636515e-01, + 5.08449976e-01, 4.95213509e-01, 4.81938756e-01, 4.68636143e-01, + 4.55317077e-01, 4.41993995e-01, 4.28678989e-01, 4.15383779e-01, + 4.02121106e-01, 3.88903272e-01, 3.75742544e-01, 3.62651518e-01, + 3.49643142e-01, 3.36729082e-01, 3.23922808e-01, 3.11236082e-01, + 2.98680794e-01, 2.86269467e-01, 2.74012847e-01, 2.61922833e-01, + 2.50009844e-01, 2.38284723e-01, 2.26757849e-01, 2.15439100e-01, + 2.04337983e-01, 1.93463677e-01, 1.82825032e-01, 1.72430586e-01, + 1.62288635e-01, 1.52407188e-01, 1.42793979e-01, 1.33456585e-01, + 1.24402392e-01, 1.15638588e-01, 1.07172155e-01, 9.90098587e-02, + 9.11582177e-02, 8.36234486e-02, 7.64114037e-02, 6.95274904e-02, + 6.29765645e-02, 5.67628424e-02, 5.08897541e-02, 4.53598330e-02, + 4.01745731e-02, 3.53343019e-02, 3.08380606e-02, 2.66835542e-02, + 2.28668041e-02, 1.93823634e-02, 1.62231272e-02, 1.33800502e-02, + 1.08421860e-02, 8.59675398e-03, 6.64050653e-03, 5.17270311e-03, +}; + +#if LC3_PLUS_HR + +static const float mdct_win_5m_48k_hr[__LC3_MDCT_WIN_LEN(5000, 48000)] = { + 9.75247545e-08, 6.41356849e-07, 1.88872264e-06, 4.37003746e-06, + 8.85053487e-06, 1.64097619e-05, 2.85265469e-05, 4.71757776e-05, + 7.49369574e-05, 1.15113864e-04, 1.71864056e-04, 2.50336452e-04, + 3.56814737e-04, 4.98863636e-04, 6.85475010e-04, 9.27209505e-04, + 1.23633002e-03, 1.62692170e-03, 2.11499492e-03, 2.71856366e-03, + 3.45769688e-03, 4.35453700e-03, 5.43327769e-03, 6.72010100e-03, + 8.24306626e-03, 1.00319488e-02, 1.21180220e-02, 1.45337880e-02, + 1.73126478e-02, 2.04885192e-02, 2.40953956e-02, 2.81668510e-02, + 3.27355117e-02, 3.78324650e-02, 4.34866548e-02, 4.97242436e-02, + 5.65679595e-02, 6.40364513e-02, 7.21436515e-02, 8.08981732e-02, + 9.03027356e-02, 1.00353681e-01, 1.11040540e-01, 1.22345708e-01, + 1.34244218e-01, 1.46703660e-01, 1.59684196e-01, 1.73138753e-01, + 1.87013358e-01, 2.01247633e-01, 2.15775400e-01, 2.30525494e-01, + 2.45422661e-01, 2.60388613e-01, 2.75343060e-01, 2.90205121e-01, + 3.04894298e-01, 3.19332004e-01, 3.33442599e-01, 3.47154707e-01, + 3.60402405e-01, 3.73126328e-01, 3.85274231e-01, 3.96802038e-01, + 4.07674283e-01, 4.17864561e-01, 4.27355647e-01, 4.36139554e-01, + 4.44217294e-01, 4.51598674e-01, 4.58301634e-01, 4.64351624e-01, + 4.69781011e-01, 4.74628091e-01, 4.78936344e-01, 4.82753456e-01, + 4.86130476e-01, 4.89120960e-01, 4.91780102e-01, 4.94164050e-01, + 4.96329218e-01, 4.98331696e-01, 5.00226736e-01, 5.02068341e-01, + 5.03908992e-01, 5.05799294e-01, 5.07787943e-01, 5.09921193e-01, + 5.12243330e-01, 5.14795899e-01, 5.17618179e-01, 5.20746589e-01, + 5.24214983e-01, 5.28054178e-01, 5.32292068e-01, 5.36953092e-01, + 5.42058468e-01, 5.47625661e-01, 5.53668439e-01, 5.60196400e-01, + 5.67215025e-01, 5.74725628e-01, 5.82724869e-01, 5.91205239e-01, + 6.00154579e-01, 6.09556615e-01, 6.19390607e-01, 6.29631937e-01, + 6.40252173e-01, 6.51219368e-01, 6.62498534e-01, 6.74051821e-01, + 6.85839176e-01, 6.97818637e-01, 7.09946930e-01, 7.22179890e-01, + 7.34472811e-01, 7.46781170e-01, 7.59061038e-01, 7.71269321e-01, + 7.83364296e-01, 7.95306087e-01, 8.07056785e-01, 8.18580806e-01, + 8.29845190e-01, 8.40819776e-01, 8.51477146e-01, 8.61792982e-01, + 8.71745944e-01, 8.81317794e-01, 8.90493214e-01, 8.99259806e-01, + 9.07608211e-01, 9.15531754e-01, 9.23026323e-01, 9.30090547e-01, + 9.36725318e-01, 9.42933977e-01, 9.48721945e-01, 9.54096615e-01, + 9.59067523e-01, 9.63645637e-01, 9.67843831e-01, 9.71676290e-01, + 9.75158513e-01, 9.78307128e-01, 9.81139660e-01, 9.83674467e-01, + 9.85930443e-01, 9.87926722e-01, 9.89682734e-01, 9.91217852e-01, + 9.92551088e-01, 9.93701279e-01, 9.94686544e-01, 9.95524466e-01, + 9.96231675e-01, 9.96824026e-01, 9.97316301e-01, 9.97722328e-01, + 9.98054802e-01, 9.98325348e-01, 9.98544455e-01, 9.98721540e-01, + 9.98864949e-01, 9.98981953e-01, 9.99078929e-01, 9.99161184e-01, + 9.99233246e-01, 9.99298692e-01, 9.99360621e-01, 9.99421120e-01, + 9.99481916e-01, 9.99544203e-01, 9.99608576e-01, 9.99675393e-01, + 9.99744534e-01, 9.99815881e-01, 9.99888837e-01, 9.99962866e-01, + 1.00003707e+00, 1.00011098e+00, 1.00018346e+00, 1.00025380e+00, + 1.00032115e+00, 1.00038469e+00, 1.00044382e+00, 1.00049770e+00, + 1.00054598e+00, 1.00058782e+00, 1.00062299e+00, 1.00065112e+00, + 1.00067186e+00, 1.00068521e+00, 1.00069082e+00, 1.00068903e+00, + 1.00067961e+00, 1.00066280e+00, 1.00063896e+00, 1.00060833e+00, + 1.00057113e+00, 1.00052810e+00, 1.00047958e+00, 1.00042605e+00, + 1.00036812e+00, 1.00030637e+00, 1.00024164e+00, 1.00017428e+00, + 1.00010526e+00, 1.00003517e+00, 9.99964774e-01, 9.99894679e-01, + 9.99825716e-01, 9.99758482e-01, 9.99693692e-01, 9.99632061e-01, + 9.99574184e-01, 9.99520719e-01, 9.99472201e-01, 9.99429166e-01, + 9.99392092e-01, 9.99361455e-01, 9.99337614e-01, 9.99320924e-01, + 9.99311507e-01, 9.99309599e-01, 9.99315262e-01, 9.99328554e-01, + 9.99349296e-01, 9.99377370e-01, 9.99412537e-01, 9.99454379e-01, + 9.99502480e-01, 9.99556363e-01, 9.99615431e-01, 9.99678969e-01, + 9.99746263e-01, 9.99816597e-01, 9.99889076e-01, 9.99962866e-01, + 1.00003707e+00, 1.00011098e+00, 1.00018346e+00, 1.00025380e+00, + 1.00032115e+00, 1.00038469e+00, 1.00044382e+00, 1.00049770e+00, + 1.00054598e+00, 1.00058782e+00, 1.00062299e+00, 1.00065112e+00, + 1.00067186e+00, 1.00068521e+00, 1.00069082e+00, 1.00068903e+00, + 1.00067961e+00, 1.00066280e+00, 1.00063896e+00, 1.00060833e+00, + 1.00057113e+00, 1.00052810e+00, 1.00047958e+00, 1.00042605e+00, + 1.00036812e+00, 1.00030637e+00, 1.00024164e+00, 1.00017428e+00, + 1.00010526e+00, 1.00003517e+00, 9.99964774e-01, 9.99894679e-01, + 9.99825716e-01, 9.99758482e-01, 9.99693692e-01, 9.99632061e-01, + 9.99574184e-01, 9.99520719e-01, 9.99472201e-01, 9.99429166e-01, + 9.99392092e-01, 9.99361455e-01, 9.99337614e-01, 9.99320924e-01, + 9.99311507e-01, 9.99309599e-01, 9.99315262e-01, 9.99328554e-01, + 9.99349296e-01, 9.99377370e-01, 9.99412537e-01, 9.99454379e-01, + 9.99502480e-01, 9.99556363e-01, 9.99615431e-01, 9.99678969e-01, + 9.99746263e-01, 9.99816597e-01, 9.99889076e-01, 9.99962866e-01, + 1.00003707e+00, 1.00011075e+00, 1.00018275e+00, 1.00025201e+00, + 1.00031757e+00, 1.00037789e+00, 1.00043166e+00, 1.00047719e+00, + 1.00051260e+00, 1.00053585e+00, 1.00054419e+00, 1.00053477e+00, + 1.00050414e+00, 1.00044823e+00, 1.00036228e+00, 1.00024092e+00, + 1.00007784e+00, 9.99865890e-01, 9.99597013e-01, 9.99261975e-01, + 9.98850465e-01, 9.98351038e-01, 9.97750819e-01, 9.97035682e-01, + 9.96189833e-01, 9.95196044e-01, 9.94035423e-01, 9.92687285e-01, + 9.91129041e-01, 9.89336133e-01, 9.87281919e-01, 9.84937787e-01, + 9.82272744e-01, 9.79253709e-01, 9.75845337e-01, 9.72010076e-01, + 9.67708528e-01, 9.62899387e-01, 9.57539737e-01, 9.51585472e-01, + 9.44991708e-01, 9.37713265e-01, 9.29705381e-01, 9.20924187e-01, + 9.11327481e-01, 9.00875807e-01, 8.89532745e-01, 8.77266228e-01, + 8.64049077e-01, 8.49859893e-01, 8.34683776e-01, 8.18513036e-01, + 8.01347792e-01, 7.83196509e-01, 7.64076352e-01, 7.44013488e-01, + 7.23043203e-01, 7.01209962e-01, 6.78567350e-01, 6.55177712e-01, + 6.31111801e-01, 6.06448233e-01, 5.81272960e-01, 5.55678487e-01, + 5.29762745e-01, 5.03628492e-01, 4.77382004e-01, 4.51132149e-01, + 4.24988985e-01, 3.99062574e-01, 3.73461813e-01, 3.48293066e-01, + 3.23658854e-01, 2.99656719e-01, 2.76377857e-01, 2.53906131e-01, + 2.32316986e-01, 2.11676583e-01, 1.92040950e-01, 1.73455566e-01, + 1.55954808e-01, 1.39561892e-01, 1.24288827e-01, 1.10136725e-01, + 9.70961973e-02, 8.51479918e-02, 7.42638558e-02, 6.44074306e-02, + 5.55353425e-02, 4.75983508e-02, 4.05424982e-02, 3.43103148e-02, + 2.88419761e-02, 2.40764078e-02, 1.99523065e-02, 1.64090749e-02, + 1.33876354e-02, 1.08311241e-02, 8.68547149e-03, 6.89984858e-03, + 5.42699778e-03, 4.22345474e-03, 3.24966502e-03, 2.47000973e-03, + 1.85274973e-03, 1.36990519e-03, 9.97077208e-04, 7.13227608e-04, + 5.00426511e-04, 3.43578606e-04, 2.30138365e-04, 1.49821601e-04, + 9.43217892e-05, 5.70367956e-05, 3.28110509e-05, 1.76969679e-05, + 8.73827867e-06, 3.77677043e-06, 1.28252009e-06, 1.95021386e-07, +}; + +static const float mdct_win_5m_96k_hr[__LC3_MDCT_WIN_LEN(5000, 96000)] = { + 6.89548827e-08, 2.31516253e-07, 5.04477669e-07, 9.30795125e-07, + 1.56340695e-06, 2.46738750e-06, 3.72189174e-06, 5.42224188e-06, + 7.68219343e-06, 1.06363877e-05, 1.44429905e-05, 1.92865264e-05, + 2.53808976e-05, 3.29725954e-05, 4.23441015e-05, 5.38174427e-05, + 6.77579446e-05, 8.45781324e-05, 1.04741775e-04, 1.28768093e-04, + 1.57236034e-04, 1.90788676e-04, 2.30137754e-04, 2.76068167e-04, + 3.29442613e-04, 3.91206064e-04, 4.62390453e-04, 5.44119219e-04, + 6.37611491e-04, 7.44186866e-04, 8.65269103e-04, 1.00239040e-03, + 1.15719519e-03, 1.33144355e-03, 1.52701419e-03, 1.74590782e-03, + 1.99024938e-03, 2.26228987e-03, 2.56440835e-03, 2.89911311e-03, + 3.26904119e-03, 3.67696048e-03, 4.12576646e-03, 4.61848313e-03, + 5.15825953e-03, 5.74836833e-03, 6.39220094e-03, 7.09326472e-03, + 7.85517693e-03, 8.68165866e-03, 9.57652833e-03, 1.05436966e-02, + 1.15871523e-02, 1.27109587e-02, 1.39192408e-02, 1.52161736e-02, + 1.66059695e-02, 1.80928707e-02, 1.96811259e-02, 2.13749874e-02, + 2.31786855e-02, 2.50964165e-02, 2.71323286e-02, 2.92905010e-02, + 3.15749235e-02, 3.39894816e-02, 3.65379415e-02, 3.92239206e-02, + 4.20508720e-02, 4.50220704e-02, 4.81405817e-02, 5.14092445e-02, + 5.48306555e-02, 5.84071539e-02, 6.21407814e-02, 6.60332814e-02, + 7.00860694e-02, 7.43002295e-02, 7.86764771e-02, 8.32151473e-02, + 8.79162028e-02, 9.27791744e-02, 9.78031904e-02, 1.02986939e-01, + 1.08328678e-01, 1.13826200e-01, 1.19476855e-01, 1.25277504e-01, + 1.31224588e-01, 1.37314022e-01, 1.43541321e-01, 1.49901465e-01, + 1.56389058e-01, 1.62998185e-01, 1.69722542e-01, 1.76555380e-01, + 1.83489516e-01, 1.90517426e-01, 1.97631180e-01, 2.04822496e-01, + 2.12082773e-01, 2.19403118e-01, 2.26774365e-01, 2.34187096e-01, + 2.41631702e-01, 2.49098375e-01, 2.56577194e-01, 2.64058143e-01, + 2.71531105e-01, 2.78985947e-01, 2.86412567e-01, 2.93800950e-01, + 3.01141053e-01, 3.08423132e-01, 3.15637439e-01, 3.22774589e-01, + 3.29825372e-01, 3.36780816e-01, 3.43632400e-01, 3.50371778e-01, + 3.56991231e-01, 3.63483250e-01, 3.69840890e-01, 3.76057625e-01, + 3.82127434e-01, 3.88044775e-01, 3.93804729e-01, 3.99402857e-01, + 4.04835284e-01, 4.10098761e-01, 4.15190488e-01, 4.20108408e-01, + 4.24850911e-01, 4.29417044e-01, 4.33806360e-01, 4.38019037e-01, + 4.42055762e-01, 4.45917755e-01, 4.49606776e-01, 4.53125089e-01, + 4.56475437e-01, 4.59661037e-01, 4.62685496e-01, 4.65552896e-01, + 4.68267679e-01, 4.70834643e-01, 4.73258942e-01, 4.75546002e-01, + 4.77701575e-01, 4.79731590e-01, 4.81642276e-01, 4.83440012e-01, + 4.85131353e-01, 4.86723036e-01, 4.88221824e-01, 4.89634633e-01, + 4.90968436e-01, 4.92230296e-01, 4.93427187e-01, 4.94566232e-01, + 4.95654404e-01, 4.96698737e-01, 4.97706175e-01, 4.98683631e-01, + 4.99637932e-01, 5.00575840e-01, 5.01503944e-01, 5.02428830e-01, + 5.03356874e-01, 5.04294455e-01, 5.05247772e-01, 5.06222844e-01, + 5.07225573e-01, 5.08261740e-01, 5.09337008e-01, 5.10456860e-01, + 5.11626601e-01, 5.12851536e-01, 5.14136553e-01, 5.15486538e-01, + 5.16906321e-01, 5.18400311e-01, 5.19972920e-01, 5.21628320e-01, + 5.23370624e-01, 5.25203526e-01, 5.27130723e-01, 5.29155731e-01, + 5.31281710e-01, 5.33511758e-01, 5.35848677e-01, 5.38295090e-01, + 5.40853441e-01, 5.43525815e-01, 5.46314240e-01, 5.49220264e-01, + 5.52245498e-01, 5.55391014e-01, 5.58657765e-01, 5.62046468e-01, + 5.65557480e-01, 5.69190919e-01, 5.72946727e-01, 5.76824427e-01, + 5.80823362e-01, 5.84942579e-01, 5.89180827e-01, 5.93536615e-01, + 5.98008096e-01, 6.02593303e-01, 6.07289851e-01, 6.12095237e-01, + 6.17006540e-01, 6.22020781e-01, 6.27134562e-01, 6.32344365e-01, + 6.37646437e-01, 6.43036783e-01, 6.48511291e-01, 6.54065490e-01, + 6.59694970e-01, 6.65394902e-01, 6.71160460e-01, 6.76986754e-01, + 6.82868540e-01, 6.88800693e-01, 6.94777906e-01, 7.00794756e-01, + 7.06845820e-01, 7.12925553e-01, 7.19028473e-01, 7.25149035e-01, + 7.31281698e-01, 7.37420917e-01, 7.43561149e-01, 7.49696970e-01, + 7.55823016e-01, 7.61933863e-01, 7.68024206e-01, 7.74088979e-01, + 7.80123055e-01, 7.86121488e-01, 7.92079389e-01, 7.97992110e-01, + 8.03855121e-01, 8.09663892e-01, 8.15414310e-01, 8.21102202e-01, + 8.26723635e-01, 8.32274854e-01, 8.37752342e-01, 8.43152702e-01, + 8.48472714e-01, 8.53709280e-01, 8.58859718e-01, 8.63921225e-01, + 8.68891478e-01, 8.73768151e-01, 8.78549099e-01, 8.83232594e-01, + 8.87816727e-01, 8.92300129e-01, 8.96681368e-01, 9.00959313e-01, + 9.05132890e-01, 9.09201384e-01, 9.13164079e-01, 9.17020440e-01, + 9.20770228e-01, 9.24413145e-01, 9.27949250e-01, 9.31378603e-01, + 9.34701502e-01, 9.37918305e-01, 9.41029668e-01, 9.44036126e-01, + 9.46938515e-01, 9.49737847e-01, 9.52435017e-01, 9.55031335e-01, + 9.57527936e-01, 9.59926248e-01, 9.62227762e-01, 9.64434028e-01, + 9.66546714e-01, 9.68567610e-01, 9.70498502e-01, 9.72341299e-01, + 9.74098027e-01, 9.75770652e-01, 9.77361381e-01, 9.78872299e-01, + 9.80305672e-01, 9.81663704e-01, 9.82948720e-01, 9.84162986e-01, + 9.85308945e-01, 9.86388862e-01, 9.87405181e-01, 9.88360226e-01, + 9.89256442e-01, 9.90096152e-01, 9.90881741e-01, 9.91615593e-01, + 9.92299914e-01, 9.92937148e-01, 9.93529499e-01, 9.94079232e-01, + 9.94588435e-01, 9.95059371e-01, 9.95494127e-01, 9.95894730e-01, + 9.96263146e-01, 9.96601343e-01, 9.96911168e-01, 9.97194529e-01, + 9.97453094e-01, 9.97688591e-01, 9.97902572e-01, 9.98096704e-01, + 9.98272479e-01, 9.98431206e-01, 9.98574317e-01, 9.98703122e-01, + 9.98818815e-01, 9.98922586e-01, 9.99015450e-01, 9.99098480e-01, + 9.99172688e-01, 9.99238908e-01, 9.99298036e-01, 9.99350786e-01, + 9.99397993e-01, 9.99440193e-01, 9.99478102e-01, 9.99512255e-01, + 9.99543130e-01, 9.99571264e-01, 9.99597073e-01, 9.99620855e-01, + 9.99643087e-01, 9.99663949e-01, 9.99683797e-01, 9.99702871e-01, + 9.99721289e-01, 9.99739230e-01, 9.99756932e-01, 9.99774456e-01, + 9.99791920e-01, 9.99809384e-01, 9.99826908e-01, 9.99844551e-01, + 9.99862373e-01, 9.99880314e-01, 9.99898374e-01, 9.99916673e-01, + 9.99935031e-01, 9.99953508e-01, 9.99972105e-01, 9.99990702e-01, + 1.00000930e+00, 1.00002778e+00, 1.00004625e+00, 1.00006461e+00, + 1.00008273e+00, 1.00010061e+00, 1.00011826e+00, 1.00013554e+00, + 1.00015235e+00, 1.00016880e+00, 1.00018466e+00, 1.00020003e+00, + 1.00021482e+00, 1.00022900e+00, 1.00024247e+00, 1.00025523e+00, + 1.00026727e+00, 1.00027859e+00, 1.00028908e+00, 1.00029874e+00, + 1.00030744e+00, 1.00031543e+00, 1.00032246e+00, 1.00032854e+00, + 1.00033379e+00, 1.00033808e+00, 1.00034130e+00, 1.00034368e+00, + 1.00034511e+00, 1.00034559e+00, 1.00034511e+00, 1.00034368e+00, + 1.00034142e+00, 1.00033808e+00, 1.00033391e+00, 1.00032878e+00, + 1.00032282e+00, 1.00031602e+00, 1.00030828e+00, 1.00029981e+00, + 1.00029051e+00, 1.00028050e+00, 1.00026977e+00, 1.00025833e+00, + 1.00024617e+00, 1.00023329e+00, 1.00021994e+00, 1.00020599e+00, + 1.00019157e+00, 1.00017655e+00, 1.00016105e+00, 1.00014532e+00, + 1.00012910e+00, 1.00011253e+00, 1.00009573e+00, 1.00007868e+00, + 1.00006139e+00, 1.00004399e+00, 1.00002646e+00, 1.00000882e+00, + 9.99991179e-01, 9.99973595e-01, 9.99956071e-01, 9.99938667e-01, + 9.99921381e-01, 9.99904335e-01, 9.99887526e-01, 9.99870956e-01, + 9.99854743e-01, 9.99838948e-01, 9.99823511e-01, 9.99808550e-01, + 9.99794066e-01, 9.99780118e-01, 9.99766707e-01, 9.99753952e-01, + 9.99741793e-01, 9.99730289e-01, 9.99719560e-01, 9.99709487e-01, + 9.99700248e-01, 9.99691784e-01, 9.99684095e-01, 9.99677300e-01, + 9.99671280e-01, 9.99666214e-01, 9.99662042e-01, 9.99658763e-01, + 9.99656379e-01, 9.99655008e-01, 9.99654531e-01, 9.99654949e-01, + 9.99656379e-01, 9.99658763e-01, 9.99662101e-01, 9.99666333e-01, + 9.99671519e-01, 9.99677658e-01, 9.99684691e-01, 9.99692619e-01, + 9.99701381e-01, 9.99711037e-01, 9.99721527e-01, 9.99732792e-01, + 9.99744833e-01, 9.99757588e-01, 9.99771118e-01, 9.99785244e-01, + 9.99800026e-01, 9.99815404e-01, 9.99831259e-01, 9.99847651e-01, + 9.99864519e-01, 9.99881744e-01, 9.99899328e-01, 9.99917269e-01, + 9.99935389e-01, 9.99953687e-01, 9.99972165e-01, 9.99990702e-01, + 1.00000930e+00, 1.00002778e+00, 1.00004625e+00, 1.00006461e+00, + 1.00008273e+00, 1.00010061e+00, 1.00011826e+00, 1.00013554e+00, + 1.00015235e+00, 1.00016880e+00, 1.00018466e+00, 1.00020003e+00, + 1.00021482e+00, 1.00022900e+00, 1.00024247e+00, 1.00025523e+00, + 1.00026727e+00, 1.00027859e+00, 1.00028908e+00, 1.00029874e+00, + 1.00030744e+00, 1.00031543e+00, 1.00032246e+00, 1.00032854e+00, + 1.00033379e+00, 1.00033808e+00, 1.00034130e+00, 1.00034368e+00, + 1.00034511e+00, 1.00034559e+00, 1.00034511e+00, 1.00034368e+00, + 1.00034142e+00, 1.00033808e+00, 1.00033391e+00, 1.00032878e+00, + 1.00032282e+00, 1.00031602e+00, 1.00030828e+00, 1.00029981e+00, + 1.00029051e+00, 1.00028050e+00, 1.00026977e+00, 1.00025833e+00, + 1.00024617e+00, 1.00023329e+00, 1.00021994e+00, 1.00020599e+00, + 1.00019157e+00, 1.00017655e+00, 1.00016105e+00, 1.00014532e+00, + 1.00012910e+00, 1.00011253e+00, 1.00009573e+00, 1.00007868e+00, + 1.00006139e+00, 1.00004399e+00, 1.00002646e+00, 1.00000882e+00, + 9.99991179e-01, 9.99973595e-01, 9.99956071e-01, 9.99938667e-01, + 9.99921381e-01, 9.99904335e-01, 9.99887526e-01, 9.99870956e-01, + 9.99854743e-01, 9.99838948e-01, 9.99823511e-01, 9.99808550e-01, + 9.99794066e-01, 9.99780118e-01, 9.99766707e-01, 9.99753952e-01, + 9.99741793e-01, 9.99730289e-01, 9.99719560e-01, 9.99709487e-01, + 9.99700248e-01, 9.99691784e-01, 9.99684095e-01, 9.99677300e-01, + 9.99671280e-01, 9.99666214e-01, 9.99662042e-01, 9.99658763e-01, + 9.99656379e-01, 9.99655008e-01, 9.99654531e-01, 9.99654949e-01, + 9.99656379e-01, 9.99658763e-01, 9.99662101e-01, 9.99666333e-01, + 9.99671519e-01, 9.99677658e-01, 9.99684691e-01, 9.99692619e-01, + 9.99701381e-01, 9.99711037e-01, 9.99721527e-01, 9.99732792e-01, + 9.99744833e-01, 9.99757588e-01, 9.99771118e-01, 9.99785244e-01, + 9.99800026e-01, 9.99815404e-01, 9.99831259e-01, 9.99847651e-01, + 9.99864519e-01, 9.99881744e-01, 9.99899328e-01, 9.99917269e-01, + 9.99935389e-01, 9.99953687e-01, 9.99972165e-01, 9.99990702e-01, + 1.00000930e+00, 1.00002778e+00, 1.00004613e+00, 1.00006425e+00, + 1.00008214e+00, 1.00009966e+00, 1.00011683e+00, 1.00013340e+00, + 1.00014925e+00, 1.00016439e+00, 1.00017869e+00, 1.00019193e+00, + 1.00020397e+00, 1.00021482e+00, 1.00022411e+00, 1.00023162e+00, + 1.00023735e+00, 1.00024080e+00, 1.00024199e+00, 1.00024033e+00, + 1.00023568e+00, 1.00022769e+00, 1.00021589e+00, 1.00020003e+00, + 1.00017941e+00, 1.00015378e+00, 1.00012243e+00, 1.00008476e+00, + 1.00004041e+00, 9.99988317e-01, 9.99928057e-01, 9.99858677e-01, + 9.99779403e-01, 9.99689281e-01, 9.99587417e-01, 9.99472737e-01, + 9.99344110e-01, 9.99200404e-01, 9.99040425e-01, 9.98862803e-01, + 9.98666167e-01, 9.98449087e-01, 9.98209953e-01, 9.97947097e-01, + 9.97658968e-01, 9.97343540e-01, 9.96999085e-01, 9.96623516e-01, + 9.96214747e-01, 9.95770633e-01, 9.95288789e-01, 9.94766831e-01, + 9.94202256e-01, 9.93592501e-01, 9.92934704e-01, 9.92226064e-01, + 9.91463542e-01, 9.90644097e-01, 9.89764392e-01, 9.88821149e-01, + 9.87810850e-01, 9.86729801e-01, 9.85574305e-01, 9.84340370e-01, + 9.83024001e-01, 9.81621027e-01, 9.80127096e-01, 9.78537738e-01, + 9.76848423e-01, 9.75054383e-01, 9.73150730e-01, 9.71132576e-01, + 9.68994796e-01, 9.66732204e-01, 9.64339435e-01, 9.61811244e-01, + 9.59142029e-01, 9.56326306e-01, 9.53358531e-01, 9.50233042e-01, + 9.46944118e-01, 9.43486214e-01, 9.39853668e-01, 9.36040819e-01, + 9.32042122e-01, 9.27852154e-01, 9.23465431e-01, 9.18876767e-01, + 9.14081097e-01, 9.09073353e-01, 9.03848886e-01, 8.98403168e-01, + 8.92731845e-01, 8.86831045e-01, 8.80696952e-01, 8.74326289e-01, + 8.67715955e-01, 8.60863328e-01, 8.53766203e-01, 8.46422672e-01, + 8.38831365e-01, 8.30991328e-01, 8.22902203e-01, 8.14563930e-01, + 8.05977046e-01, 7.97142744e-01, 7.88062632e-01, 7.78738797e-01, + 7.69173980e-01, 7.59371519e-01, 7.49335289e-01, 7.39069760e-01, + 7.28579819e-01, 7.17871130e-01, 7.06949770e-01, 6.95822597e-01, + 6.84496701e-01, 6.72980070e-01, 6.61280870e-01, 6.49408042e-01, + 6.37370944e-01, 6.25179410e-01, 6.12843752e-01, 6.00374699e-01, + 5.87783396e-01, 5.75081468e-01, 5.62280834e-01, 5.49393654e-01, + 5.36432624e-01, 5.23410499e-01, 5.10340393e-01, 4.97235566e-01, + 4.84109521e-01, 4.70975846e-01, 4.57848251e-01, 4.44740474e-01, + 4.31666315e-01, 4.18639511e-01, 4.05673832e-01, 3.92782807e-01, + 3.79979968e-01, 3.67278606e-01, 3.54691803e-01, 3.42232376e-01, + 3.29912812e-01, 3.17745358e-01, 3.05741847e-01, 2.93913603e-01, + 2.82271683e-01, 2.70826548e-01, 2.59588152e-01, 2.48565957e-01, + 2.37768814e-01, 2.27205008e-01, 2.16882199e-01, 2.06807390e-01, + 1.96986943e-01, 1.87426537e-01, 1.78131178e-01, 1.69105172e-01, + 1.60352126e-01, 1.51874945e-01, 1.43675804e-01, 1.35756254e-01, + 1.28117070e-01, 1.20758407e-01, 1.13679729e-01, 1.06879868e-01, + 1.00357018e-01, 9.41087753e-02, 8.81321430e-02, 8.24235976e-02, + 7.69790635e-02, 7.17940032e-02, 6.68634027e-02, 6.21818379e-02, + 5.77434972e-02, 5.35422154e-02, 4.95715141e-02, 4.58246432e-02, + 4.22946103e-02, 3.89742292e-02, 3.58561426e-02, 3.29328589e-02, + 3.01968064e-02, 2.76403390e-02, 2.52557844e-02, 2.30354760e-02, + 2.09717732e-02, 1.90570969e-02, 1.72839500e-02, 1.56449396e-02, + 1.41328052e-02, 1.27404351e-02, 1.14608845e-02, 1.02873892e-02, + 9.21338331e-03, 8.23251065e-03, 7.33863330e-03, 6.52584061e-03, + 5.78845851e-03, 5.12105133e-03, 4.51842742e-03, 3.97564145e-03, + 3.48799396e-03, 3.05103138e-03, 2.66054412e-03, 2.31256452e-03, + 2.00336217e-03, 1.72943878e-03, 1.48752402e-03, 1.27456791e-03, + 1.08773448e-03, 9.24394117e-04, 7.82115792e-04, 6.58658682e-04, + 5.51963516e-04, 4.60143900e-04, 3.81477352e-04, 3.14396282e-04, + 2.57478940e-04, 2.09440448e-04, 1.69123945e-04, 1.35491777e-04, + 1.07617016e-04, 8.46750627e-05, 6.59356156e-05, 5.07548866e-05, + 3.85681342e-05, 2.88825358e-05, 2.12704090e-05, 1.53627971e-05, + 1.08434460e-05, 7.44312956e-06, 4.93438165e-06, 3.12659085e-06, + 1.86147406e-06, 1.00890213e-06, 4.63012810e-07, 1.37904777e-07, +}; + +#endif /* LC3_PLUS_HR */ +#endif /* LC3_PLUS */ + +static const float mdct_win_7m5_8k[__LC3_MDCT_WIN_LEN(7500, 8000)] = { + 2.95060859e-03, 7.17541132e-03, 1.37695374e-02, 2.30953556e-02, + 3.54036230e-02, 5.08289304e-02, 6.94696293e-02, 9.13884278e-02, + 1.16604575e-01, 1.45073546e-01, 1.76711174e-01, 2.11342953e-01, + 2.48768614e-01, 2.88701102e-01, 3.30823871e-01, 3.74814544e-01, + 4.20308013e-01, 4.66904918e-01, 5.14185341e-01, 5.61710041e-01, + 6.09026346e-01, 6.55671016e-01, 7.01218384e-01, 7.45240679e-01, + 7.87369206e-01, 8.27223833e-01, 8.64513675e-01, 8.98977415e-01, + 9.30407518e-01, 9.58599937e-01, 9.83447719e-01, 1.00488283e+00, + 1.02285381e+00, 1.03740495e+00, 1.04859791e+00, 1.05656184e+00, + 1.06149371e+00, 1.06362578e+00, 1.06325973e+00, 1.06074505e+00, + 1.05643590e+00, 1.05069500e+00, 1.04392435e+00, 1.03647725e+00, + 1.02872867e+00, 1.02106486e+00, 1.01400658e+00, 1.00727455e+00, + 1.00172250e+00, 9.97309592e-01, 9.93985158e-01, 9.91683335e-01, + 9.90325325e-01, 9.89822613e-01, 9.90074734e-01, 9.90975314e-01, + 9.92412851e-01, 9.94273149e-01, 9.96439157e-01, 9.98791616e-01, + 1.00120985e+00, 1.00357357e+00, 1.00575984e+00, 1.00764515e+00, + 1.00910687e+00, 1.01002476e+00, 1.01028203e+00, 1.00976919e+00, + 1.00838641e+00, 1.00605124e+00, 1.00269767e+00, 9.98280464e-01, + 9.92777987e-01, 9.86186892e-01, 9.77634164e-01, 9.67447270e-01, + 9.55129725e-01, 9.40389877e-01, 9.22959280e-01, 9.02607350e-01, + 8.79202689e-01, 8.52641750e-01, 8.22881272e-01, 7.89971715e-01, + 7.54030328e-01, 7.15255742e-01, 6.73936911e-01, 6.30414716e-01, + 5.85078858e-01, 5.38398518e-01, 4.90833753e-01, 4.42885823e-01, + 3.95091024e-01, 3.48004343e-01, 3.02196710e-01, 2.58227431e-01, + 2.16641416e-01, 1.77922122e-01, 1.42480547e-01, 1.10652194e-01, + 8.26995967e-02, 5.88334516e-02, 3.92030848e-02, 2.38629107e-02, + 1.26976223e-02, 5.35665361e-03, +}; + +static const float mdct_win_7m5_16k[__LC3_MDCT_WIN_LEN(7500, 16000)] = { + 2.20824874e-03, 3.81014420e-03, 5.91552473e-03, 8.58361457e-03, + 1.18759723e-02, 1.58335301e-02, 2.04918652e-02, 2.58883593e-02, + 3.20415894e-02, 3.89616721e-02, 4.66742169e-02, 5.51849337e-02, + 6.45038384e-02, 7.46411071e-02, 8.56000162e-02, 9.73846703e-02, + 1.09993603e-01, 1.23419277e-01, 1.37655457e-01, 1.52690437e-01, + 1.68513363e-01, 1.85093105e-01, 2.02410419e-01, 2.20450365e-01, + 2.39167941e-01, 2.58526168e-01, 2.78498539e-01, 2.99038432e-01, + 3.20104862e-01, 3.41658622e-01, 3.63660034e-01, 3.86062695e-01, + 4.08815272e-01, 4.31871046e-01, 4.55176988e-01, 4.78676593e-01, + 5.02324813e-01, 5.26060916e-01, 5.49831283e-01, 5.73576883e-01, + 5.97241338e-01, 6.20770242e-01, 6.44099662e-01, 6.67176382e-01, + 6.89958854e-01, 7.12379980e-01, 7.34396372e-01, 7.55966688e-01, + 7.77036981e-01, 7.97558114e-01, 8.17490856e-01, 8.36796950e-01, + 8.55447310e-01, 8.73400798e-01, 8.90635719e-01, 9.07128770e-01, + 9.22848784e-01, 9.37763323e-01, 9.51860206e-01, 9.65130600e-01, + 9.77556541e-01, 9.89126209e-01, 9.99846919e-01, 1.00970073e+00, + 1.01868229e+00, 1.02681455e+00, 1.03408981e+00, 1.04051196e+00, + 1.04610837e+00, 1.05088565e+00, 1.05486289e+00, 1.05807221e+00, + 1.06053414e+00, 1.06227662e+00, 1.06333815e+00, 1.06375557e+00, + 1.06356632e+00, 1.06282156e+00, 1.06155996e+00, 1.05981709e+00, + 1.05765876e+00, 1.05512006e+00, 1.05223985e+00, 1.04908779e+00, + 1.04569860e+00, 1.04210831e+00, 1.03838099e+00, 1.03455276e+00, + 1.03067200e+00, 1.02679167e+00, 1.02295558e+00, 1.01920733e+00, + 1.01587289e+00, 1.01221017e+00, 1.00884559e+00, 1.00577851e+00, + 1.00300262e+00, 1.00051460e+00, 9.98309229e-01, 9.96378601e-01, + 9.94718132e-01, 9.93316216e-01, 9.92166957e-01, 9.91258603e-01, + 9.90581104e-01, 9.90123118e-01, 9.89873712e-01, 9.89818707e-01, + 9.89946800e-01, 9.90243175e-01, 9.90695564e-01, 9.91288540e-01, + 9.92009469e-01, 9.92842693e-01, 9.93775067e-01, 9.94790398e-01, + 9.95875534e-01, 9.97014367e-01, 9.98192871e-01, 9.99394506e-01, + 1.00060586e+00, 1.00181040e+00, 1.00299457e+00, 1.00414155e+00, + 1.00523688e+00, 1.00626393e+00, 1.00720890e+00, 1.00805489e+00, + 1.00878802e+00, 1.00939182e+00, 1.00985296e+00, 1.01015529e+00, + 1.01028602e+00, 1.01022988e+00, 1.00997541e+00, 1.00950846e+00, + 1.00881848e+00, 1.00789488e+00, 1.00672876e+00, 1.00530991e+00, + 1.00363456e+00, 1.00169363e+00, 9.99485663e-01, 9.97006370e-01, + 9.94254687e-01, 9.91231967e-01, 9.87937115e-01, 9.84375125e-01, + 9.79890963e-01, 9.75269879e-01, 9.70180498e-01, 9.64580027e-01, + 9.58425534e-01, 9.51684014e-01, 9.44320232e-01, 9.36290624e-01, + 9.27580507e-01, 9.18153414e-01, 9.07976524e-01, 8.97050058e-01, + 8.85351360e-01, 8.72857927e-01, 8.59579819e-01, 8.45502615e-01, + 8.30619943e-01, 8.14946648e-01, 7.98489378e-01, 7.81262450e-01, + 7.63291769e-01, 7.44590843e-01, 7.25199287e-01, 7.05153668e-01, + 6.84490545e-01, 6.63245210e-01, 6.41477162e-01, 6.19235334e-01, + 5.96559133e-01, 5.73519989e-01, 5.50173851e-01, 5.26568538e-01, + 5.02781159e-01, 4.78860889e-01, 4.54877894e-01, 4.30898123e-01, + 4.06993964e-01, 3.83234031e-01, 3.59680098e-01, 3.36408100e-01, + 3.13496418e-01, 2.91010565e-01, 2.69019585e-01, 2.47584348e-01, + 2.26788433e-01, 2.06677771e-01, 1.87310343e-01, 1.68739644e-01, + 1.51012382e-01, 1.34171842e-01, 1.18254662e-01, 1.03290734e-01, + 8.93117360e-02, 7.63429787e-02, 6.44077291e-02, 5.35243715e-02, + 4.37084453e-02, 3.49667099e-02, 2.72984629e-02, 2.06895808e-02, + 1.51125125e-02, 1.05228754e-02, 6.85547314e-03, 4.02351119e-03, +}; + +static const float mdct_win_7m5_24k[__LC3_MDCT_WIN_LEN(7500, 24000)] = { + 1.97084908e-03, 2.95060859e-03, 4.12447721e-03, 5.52688664e-03, + 7.17541132e-03, 9.08757730e-03, 1.12819105e-02, 1.37695374e-02, + 1.65600266e-02, 1.96650895e-02, 2.30953556e-02, 2.68612894e-02, + 3.09632560e-02, 3.54036230e-02, 4.01915610e-02, 4.53331403e-02, + 5.08289304e-02, 5.66815448e-02, 6.28935304e-02, 6.94696293e-02, + 7.64106314e-02, 8.37160016e-02, 9.13884278e-02, 9.94294008e-02, + 1.07834725e-01, 1.16604575e-01, 1.25736503e-01, 1.35226811e-01, + 1.45073546e-01, 1.55273819e-01, 1.65822194e-01, 1.76711174e-01, + 1.87928776e-01, 1.99473180e-01, 2.11342953e-01, 2.23524554e-01, + 2.36003100e-01, 2.48768614e-01, 2.61813811e-01, 2.75129161e-01, + 2.88701102e-01, 3.02514034e-01, 3.16558805e-01, 3.30823871e-01, + 3.45295567e-01, 3.59963992e-01, 3.74814544e-01, 3.89831817e-01, + 4.05001010e-01, 4.20308013e-01, 4.35739515e-01, 4.51277817e-01, + 4.66904918e-01, 4.82609041e-01, 4.98375466e-01, 5.14185341e-01, + 5.30021478e-01, 5.45869352e-01, 5.61710041e-01, 5.77528151e-01, + 5.93304696e-01, 6.09026346e-01, 6.24674189e-01, 6.40227555e-01, + 6.55671016e-01, 6.70995935e-01, 6.86184559e-01, 7.01218384e-01, + 7.16078449e-01, 7.30756084e-01, 7.45240679e-01, 7.59515122e-01, + 7.73561955e-01, 7.87369206e-01, 8.00923138e-01, 8.14211386e-01, + 8.27223833e-01, 8.39952374e-01, 8.52386102e-01, 8.64513675e-01, + 8.76324079e-01, 8.87814288e-01, 8.98977415e-01, 9.09803319e-01, + 9.20284312e-01, 9.30407518e-01, 9.40169652e-01, 9.49567795e-01, + 9.58599937e-01, 9.67260260e-01, 9.75545166e-01, 9.83447719e-01, + 9.90971957e-01, 9.98119269e-01, 1.00488283e+00, 1.01125773e+00, + 1.01724436e+00, 1.02285381e+00, 1.02808734e+00, 1.03293706e+00, + 1.03740495e+00, 1.04150164e+00, 1.04523236e+00, 1.04859791e+00, + 1.05160340e+00, 1.05425505e+00, 1.05656184e+00, 1.05853400e+00, + 1.06017414e+00, 1.06149371e+00, 1.06249943e+00, 1.06320577e+00, + 1.06362578e+00, 1.06376487e+00, 1.06363778e+00, 1.06325973e+00, + 1.06264695e+00, 1.06180496e+00, 1.06074505e+00, 1.05948492e+00, + 1.05804533e+00, 1.05643590e+00, 1.05466218e+00, 1.05274047e+00, + 1.05069500e+00, 1.04853894e+00, 1.04627898e+00, 1.04392435e+00, + 1.04149540e+00, 1.03901003e+00, 1.03647725e+00, 1.03390793e+00, + 1.03131989e+00, 1.02872867e+00, 1.02614832e+00, 1.02358988e+00, + 1.02106486e+00, 1.01856262e+00, 1.01655770e+00, 1.01400658e+00, + 1.01162953e+00, 1.00938590e+00, 1.00727455e+00, 1.00529616e+00, + 1.00344526e+00, 1.00172250e+00, 1.00012792e+00, 9.98657533e-01, + 9.97309592e-01, 9.96083571e-01, 9.94976569e-01, 9.93985158e-01, + 9.93107530e-01, 9.92341305e-01, 9.91683335e-01, 9.91130070e-01, + 9.90678325e-01, 9.90325325e-01, 9.90067562e-01, 9.89901282e-01, + 9.89822613e-01, 9.89827845e-01, 9.89913241e-01, 9.90074734e-01, + 9.90308256e-01, 9.90609852e-01, 9.90975314e-01, 9.91400330e-01, + 9.91880966e-01, 9.92412851e-01, 9.92991779e-01, 9.93613381e-01, + 9.94273149e-01, 9.94966958e-01, 9.95690370e-01, 9.96439157e-01, + 9.97208572e-01, 9.97994275e-01, 9.98791616e-01, 9.99596062e-01, + 1.00040410e+00, 1.00120985e+00, 1.00200976e+00, 1.00279924e+00, + 1.00357357e+00, 1.00432828e+00, 1.00505850e+00, 1.00575984e+00, + 1.00642767e+00, 1.00705768e+00, 1.00764515e+00, 1.00818549e+00, + 1.00867427e+00, 1.00910687e+00, 1.00947916e+00, 1.00978659e+00, + 1.01002476e+00, 1.01018954e+00, 1.01027669e+00, 1.01028203e+00, + 1.01020174e+00, 1.01003208e+00, 1.00976919e+00, 1.00940939e+00, + 1.00894931e+00, 1.00838641e+00, 1.00771780e+00, 1.00694031e+00, + 1.00605124e+00, 1.00504879e+00, 1.00393183e+00, 1.00269767e+00, + 1.00134427e+00, 9.99872092e-01, 9.98280464e-01, 9.96566569e-01, + 9.94731737e-01, 9.92777987e-01, 9.90701374e-01, 9.88504165e-01, + 9.86186892e-01, 9.83711989e-01, 9.80584643e-01, 9.77634164e-01, + 9.74455033e-01, 9.71062916e-01, 9.67447270e-01, 9.63593926e-01, + 9.59491398e-01, 9.55129725e-01, 9.50501326e-01, 9.45592810e-01, + 9.40389877e-01, 9.34886760e-01, 9.29080559e-01, 9.22959280e-01, + 9.16509579e-01, 9.09724456e-01, 9.02607350e-01, 8.95155084e-01, + 8.87356154e-01, 8.79202689e-01, 8.70699698e-01, 8.61847424e-01, + 8.52641750e-01, 8.43077833e-01, 8.33154905e-01, 8.22881272e-01, + 8.12257597e-01, 8.01285439e-01, 7.89971715e-01, 7.78318177e-01, + 7.66337710e-01, 7.54030328e-01, 7.41407991e-01, 7.28477501e-01, + 7.15255742e-01, 7.01751739e-01, 6.87975632e-01, 6.73936911e-01, + 6.59652573e-01, 6.45139489e-01, 6.30414716e-01, 6.15483622e-01, + 6.00365852e-01, 5.85078858e-01, 5.69649536e-01, 5.54084810e-01, + 5.38398518e-01, 5.22614738e-01, 5.06756805e-01, 4.90833753e-01, + 4.74866033e-01, 4.58876566e-01, 4.42885823e-01, 4.26906539e-01, + 4.10970973e-01, 3.95091024e-01, 3.79291327e-01, 3.63587417e-01, + 3.48004343e-01, 3.32563201e-01, 3.17287485e-01, 3.02196710e-01, + 2.87309403e-01, 2.72643992e-01, 2.58227431e-01, 2.44072856e-01, + 2.30208977e-01, 2.16641416e-01, 2.03398481e-01, 1.90486162e-01, + 1.77922122e-01, 1.65726674e-01, 1.53906397e-01, 1.42480547e-01, + 1.31453980e-01, 1.20841778e-01, 1.10652194e-01, 1.00891734e-01, + 9.15718851e-02, 8.26995967e-02, 7.42815529e-02, 6.63242382e-02, + 5.88334516e-02, 5.18140676e-02, 4.52698346e-02, 3.92030848e-02, + 3.36144159e-02, 2.85023308e-02, 2.38629107e-02, 1.96894227e-02, + 1.59720527e-02, 1.26976223e-02, 9.84937739e-03, 7.40724463e-03, + 5.35665361e-03, 3.83226552e-03, +}; + +static const float mdct_win_7m5_32k[__LC3_MDCT_WIN_LEN(7500, 32000)] = { + 1.84833037e-03, 2.56481839e-03, 3.36762118e-03, 4.28736617e-03, + 5.33830143e-03, 6.52679223e-03, 7.86112587e-03, 9.34628179e-03, + 1.09916868e-02, 1.28011172e-02, 1.47805911e-02, 1.69307043e-02, + 1.92592307e-02, 2.17696937e-02, 2.44685983e-02, 2.73556543e-02, + 3.04319230e-02, 3.36980464e-02, 3.71583577e-02, 4.08148180e-02, + 4.46708068e-02, 4.87262995e-02, 5.29820633e-02, 5.74382470e-02, + 6.20968580e-02, 6.69609767e-02, 7.20298364e-02, 7.73039146e-02, + 8.27825574e-02, 8.84682102e-02, 9.43607566e-02, 1.00460272e-01, + 1.06763824e-01, 1.13273679e-01, 1.19986420e-01, 1.26903521e-01, + 1.34020853e-01, 1.41339557e-01, 1.48857211e-01, 1.56573685e-01, + 1.64484622e-01, 1.72589077e-01, 1.80879090e-01, 1.89354320e-01, + 1.98012244e-01, 2.06854141e-01, 2.15875319e-01, 2.25068672e-01, + 2.34427407e-01, 2.43948314e-01, 2.53627993e-01, 2.63464061e-01, + 2.73450494e-01, 2.83582189e-01, 2.93853469e-01, 3.04257373e-01, + 3.14790914e-01, 3.25449123e-01, 3.36227410e-01, 3.47118760e-01, + 3.58120177e-01, 3.69224663e-01, 3.80427793e-01, 3.91720023e-01, + 4.03097022e-01, 4.14551955e-01, 4.26081719e-01, 4.37676318e-01, + 4.49330196e-01, 4.61034855e-01, 4.72786043e-01, 4.84576777e-01, + 4.96401707e-01, 5.08252458e-01, 5.20122078e-01, 5.32002077e-01, + 5.43888090e-01, 5.55771601e-01, 5.67645739e-01, 5.79502786e-01, + 5.91335035e-01, 6.03138367e-01, 6.14904172e-01, 6.26623941e-01, + 6.38288834e-01, 6.49893375e-01, 6.61432360e-01, 6.72902514e-01, + 6.84293750e-01, 6.95600460e-01, 7.06811784e-01, 7.17923425e-01, + 7.28931386e-01, 7.39832773e-01, 7.50618982e-01, 7.61284053e-01, + 7.71818919e-01, 7.82220992e-01, 7.92481330e-01, 8.02599448e-01, + 8.12565230e-01, 8.22377129e-01, 8.32030518e-01, 8.41523208e-01, + 8.50848313e-01, 8.60002412e-01, 8.68979881e-01, 8.77778347e-01, + 8.86395904e-01, 8.94829421e-01, 9.03077626e-01, 9.11132652e-01, + 9.18993585e-01, 9.26652937e-01, 9.34111420e-01, 9.41364344e-01, + 9.48412967e-01, 9.55255630e-01, 9.61892013e-01, 9.68316363e-01, + 9.74530156e-01, 9.80528338e-01, 9.86313928e-01, 9.91886049e-01, + 9.97246345e-01, 1.00239190e+00, 1.00731946e+00, 1.01202707e+00, + 1.01651654e+00, 1.02079430e+00, 1.02486082e+00, 1.02871471e+00, + 1.03235170e+00, 1.03577375e+00, 1.03898432e+00, 1.04198786e+00, + 1.04478564e+00, 1.04737818e+00, 1.04976743e+00, 1.05195405e+00, + 1.05394290e+00, 1.05573463e+00, 1.05734177e+00, 1.05875726e+00, + 1.05998674e+00, 1.06103672e+00, 1.06190651e+00, 1.06260369e+00, + 1.06313289e+00, 1.06350237e+00, 1.06370981e+00, 1.06376322e+00, + 1.06366765e+00, 1.06343012e+00, 1.06305656e+00, 1.06255421e+00, + 1.06192235e+00, 1.06116702e+00, 1.06029469e+00, 1.05931469e+00, + 1.05823465e+00, 1.05705891e+00, 1.05578948e+00, 1.05442979e+00, + 1.05298793e+00, 1.05147505e+00, 1.04989930e+00, 1.04826213e+00, + 1.04656691e+00, 1.04481699e+00, 1.04302125e+00, 1.04118768e+00, + 1.03932339e+00, 1.03743168e+00, 1.03551757e+00, 1.03358511e+00, + 1.03164371e+00, 1.02969955e+00, 1.02775944e+00, 1.02582719e+00, + 1.02390791e+00, 1.02200805e+00, 1.02013910e+00, 1.01826310e+00, + 1.01687901e+00, 1.01492195e+00, 1.01309662e+00, 1.01134205e+00, + 1.00965912e+00, 1.00805036e+00, 1.00651754e+00, 1.00505799e+00, + 1.00366956e+00, 1.00235327e+00, 1.00110981e+00, 9.99937523e-01, + 9.98834524e-01, 9.97800606e-01, 9.96835756e-01, 9.95938881e-01, + 9.95108459e-01, 9.94343411e-01, 9.93642921e-01, 9.93005832e-01, + 9.92430984e-01, 9.91917493e-01, 9.91463898e-01, 9.91068214e-01, + 9.90729218e-01, 9.90446225e-01, 9.90217819e-01, 9.90041963e-01, + 9.89917085e-01, 9.89841975e-01, 9.89815048e-01, 9.89834329e-01, + 9.89898211e-01, 9.90005403e-01, 9.90154189e-01, 9.90342427e-01, + 9.90568459e-01, 9.90830953e-01, 9.91128038e-01, 9.91457566e-01, + 9.91817881e-01, 9.92207559e-01, 9.92624757e-01, 9.93067358e-01, + 9.93533398e-01, 9.94021410e-01, 9.94529685e-01, 9.95055964e-01, + 9.95598351e-01, 9.96155580e-01, 9.96725627e-01, 9.97306092e-01, + 9.97895214e-01, 9.98491441e-01, 9.99092890e-01, 9.99697063e-01, + 1.00030303e+00, 1.00090793e+00, 1.00151084e+00, 1.00210923e+00, + 1.00270118e+00, 1.00328513e+00, 1.00385926e+00, 1.00442111e+00, + 1.00496860e+00, 1.00550040e+00, 1.00601455e+00, 1.00650869e+00, + 1.00698104e+00, 1.00743004e+00, 1.00785364e+00, 1.00824962e+00, + 1.00861604e+00, 1.00895138e+00, 1.00925390e+00, 1.00952134e+00, + 1.00975175e+00, 1.00994371e+00, 1.01009550e+00, 1.01020488e+00, + 1.01027007e+00, 1.01028975e+00, 1.01026227e+00, 1.01018562e+00, + 1.01005820e+00, 1.00987882e+00, 1.00964593e+00, 1.00935753e+00, + 1.00901228e+00, 1.00860959e+00, 1.00814837e+00, 1.00762674e+00, + 1.00704343e+00, 1.00639775e+00, 1.00568877e+00, 1.00491559e+00, + 1.00407768e+00, 1.00317429e+00, 1.00220424e+00, 1.00116684e+00, + 1.00006248e+00, 9.98891422e-01, 9.97652252e-01, 9.96343856e-01, + 9.94967462e-01, 9.93524663e-01, 9.92013927e-01, 9.90433283e-01, + 9.88785147e-01, 9.87072681e-01, 9.85297443e-01, 9.83401161e-01, + 9.80949418e-01, 9.78782729e-01, 9.76468238e-01, 9.74042850e-01, + 9.71498848e-01, 9.68829968e-01, 9.66030974e-01, 9.63095104e-01, + 9.60018198e-01, 9.56795738e-01, 9.53426267e-01, 9.49903482e-01, + 9.46222115e-01, 9.42375820e-01, 9.38361702e-01, 9.34177798e-01, + 9.29823124e-01, 9.25292320e-01, 9.20580120e-01, 9.15679793e-01, + 9.10590604e-01, 9.05315030e-01, 8.99852756e-01, 8.94199497e-01, + 8.88350152e-01, 8.82301631e-01, 8.76054874e-01, 8.69612385e-01, + 8.62972799e-01, 8.56135198e-01, 8.49098179e-01, 8.41857024e-01, + 8.34414055e-01, 8.26774617e-01, 8.18939244e-01, 8.10904891e-01, + 8.02675318e-01, 7.94253751e-01, 7.85641662e-01, 7.76838609e-01, + 7.67853193e-01, 7.58685181e-01, 7.49330658e-01, 7.39809171e-01, + 7.30109944e-01, 7.20247781e-01, 7.10224161e-01, 7.00044326e-01, + 6.89711890e-01, 6.79231154e-01, 6.68608179e-01, 6.57850997e-01, + 6.46965718e-01, 6.35959617e-01, 6.24840336e-01, 6.13603503e-01, + 6.02265091e-01, 5.90829083e-01, 5.79309408e-01, 5.67711124e-01, + 5.56037416e-01, 5.44293664e-01, 5.32489768e-01, 5.20636084e-01, + 5.08743273e-01, 4.96811166e-01, 4.84849881e-01, 4.72868107e-01, + 4.60875918e-01, 4.48881081e-01, 4.36891039e-01, 4.24912022e-01, + 4.12960603e-01, 4.01035896e-01, 3.89157867e-01, 3.77322199e-01, + 3.65543767e-01, 3.53832356e-01, 3.42196115e-01, 3.30644820e-01, + 3.19187559e-01, 3.07833309e-01, 2.96588182e-01, 2.85463717e-01, + 2.74462409e-01, 2.63609584e-01, 2.52883101e-01, 2.42323489e-01, + 2.31925746e-01, 2.21690837e-01, 2.11638058e-01, 2.01766920e-01, + 1.92082236e-01, 1.82589160e-01, 1.73305997e-01, 1.64229200e-01, + 1.55362654e-01, 1.46717079e-01, 1.38299391e-01, 1.30105078e-01, + 1.22145310e-01, 1.14423458e-01, 1.06941076e-01, 9.97025893e-02, + 9.27124283e-02, 8.59737427e-02, 7.94893311e-02, 7.32616579e-02, + 6.72934102e-02, 6.15874081e-02, 5.61458003e-02, 5.09700747e-02, + 4.60617047e-02, 4.14220117e-02, 3.70514189e-02, 3.29494666e-02, + 2.91153327e-02, 2.55476401e-02, 2.22437711e-02, 1.92000659e-02, + 1.64122205e-02, 1.38747611e-02, 1.15806353e-02, 9.52213664e-03, + 7.69137380e-03, 6.07207833e-03, 4.62581217e-03, 3.60685164e-03, +}; + +static const float mdct_win_7m5_48k[__LC3_MDCT_WIN_LEN(7500, 48000)] = { + 1.72152668e-03, 2.20824874e-03, 2.68901752e-03, 3.22613342e-03, + 3.81014420e-03, 4.45371932e-03, 5.15369240e-03, 5.91552473e-03, + 6.73869158e-03, 7.62861841e-03, 8.58361457e-03, 9.60938437e-03, + 1.07060753e-02, 1.18759723e-02, 1.31190130e-02, 1.44390108e-02, + 1.58335301e-02, 1.73063081e-02, 1.88584711e-02, 2.04918652e-02, + 2.22061476e-02, 2.40057166e-02, 2.58883593e-02, 2.78552326e-02, + 2.99059145e-02, 3.20415894e-02, 3.42610013e-02, 3.65680973e-02, + 3.89616721e-02, 4.14435824e-02, 4.40140796e-02, 4.66742169e-02, + 4.94214625e-02, 5.22588489e-02, 5.51849337e-02, 5.82005143e-02, + 6.13059845e-02, 6.45038384e-02, 6.77913923e-02, 7.11707833e-02, + 7.46411071e-02, 7.82028053e-02, 8.18549521e-02, 8.56000162e-02, + 8.94357617e-02, 9.33642589e-02, 9.73846703e-02, 1.01496718e-01, + 1.05698760e-01, 1.09993603e-01, 1.14378287e-01, 1.18853508e-01, + 1.23419277e-01, 1.28075997e-01, 1.32820581e-01, 1.37655457e-01, + 1.42578648e-01, 1.47590522e-01, 1.52690437e-01, 1.57878853e-01, + 1.63152529e-01, 1.68513363e-01, 1.73957969e-01, 1.79484737e-01, + 1.85093105e-01, 1.90784835e-01, 1.96556497e-01, 2.02410419e-01, + 2.08345433e-01, 2.14359825e-01, 2.20450365e-01, 2.26617296e-01, + 2.32856279e-01, 2.39167941e-01, 2.45550642e-01, 2.52003951e-01, + 2.58526168e-01, 2.65118408e-01, 2.71775911e-01, 2.78498539e-01, + 2.85284606e-01, 2.92132459e-01, 2.99038432e-01, 3.06004256e-01, + 3.13026529e-01, 3.20104862e-01, 3.27237324e-01, 3.34423210e-01, + 3.41658622e-01, 3.48944976e-01, 3.56279252e-01, 3.63660034e-01, + 3.71085146e-01, 3.78554327e-01, 3.86062695e-01, 3.93610554e-01, + 4.01195225e-01, 4.08815272e-01, 4.16468460e-01, 4.24155411e-01, + 4.31871046e-01, 4.39614744e-01, 4.47384019e-01, 4.55176988e-01, + 4.62990138e-01, 4.70824619e-01, 4.78676593e-01, 4.86545433e-01, + 4.94428714e-01, 5.02324813e-01, 5.10229471e-01, 5.18142927e-01, + 5.26060916e-01, 5.33982818e-01, 5.41906817e-01, 5.49831283e-01, + 5.57751234e-01, 5.65667636e-01, 5.73576883e-01, 5.81476666e-01, + 5.89364661e-01, 5.97241338e-01, 6.05102013e-01, 6.12946170e-01, + 6.20770242e-01, 6.28572094e-01, 6.36348526e-01, 6.44099662e-01, + 6.51820973e-01, 6.59513822e-01, 6.67176382e-01, 6.74806795e-01, + 6.82400711e-01, 6.89958854e-01, 6.97475722e-01, 7.04950145e-01, + 7.12379980e-01, 7.19765434e-01, 7.27103833e-01, 7.34396372e-01, + 7.41638561e-01, 7.48829639e-01, 7.55966688e-01, 7.63049259e-01, + 7.70072273e-01, 7.77036981e-01, 7.83941108e-01, 7.90781257e-01, + 7.97558114e-01, 8.04271381e-01, 8.10914901e-01, 8.17490856e-01, + 8.23997094e-01, 8.30432785e-01, 8.36796950e-01, 8.43089298e-01, + 8.49305847e-01, 8.55447310e-01, 8.61511037e-01, 8.67496281e-01, + 8.73400798e-01, 8.79227518e-01, 8.84972438e-01, 8.90635719e-01, + 8.96217173e-01, 9.01716414e-01, 9.07128770e-01, 9.12456578e-01, + 9.17697261e-01, 9.22848784e-01, 9.27909917e-01, 9.32882596e-01, + 9.37763323e-01, 9.42553356e-01, 9.47252428e-01, 9.51860206e-01, + 9.56376060e-01, 9.60800602e-01, 9.65130600e-01, 9.69366689e-01, + 9.73508812e-01, 9.77556541e-01, 9.81507226e-01, 9.85364580e-01, + 9.89126209e-01, 9.92794201e-01, 9.96367545e-01, 9.99846919e-01, + 1.00322812e+00, 1.00651341e+00, 1.00970073e+00, 1.01279029e+00, + 1.01578293e+00, 1.01868229e+00, 1.02148657e+00, 1.02419772e+00, + 1.02681455e+00, 1.02933598e+00, 1.03176043e+00, 1.03408981e+00, + 1.03632326e+00, 1.03846361e+00, 1.04051196e+00, 1.04246831e+00, + 1.04433331e+00, 1.04610837e+00, 1.04779018e+00, 1.04938334e+00, + 1.05088565e+00, 1.05229923e+00, 1.05362522e+00, 1.05486289e+00, + 1.05601521e+00, 1.05708746e+00, 1.05807221e+00, 1.05897524e+00, + 1.05979447e+00, 1.06053414e+00, 1.06119412e+00, 1.06177366e+00, + 1.06227662e+00, 1.06270324e+00, 1.06305569e+00, 1.06333815e+00, + 1.06354800e+00, 1.06368607e+00, 1.06375557e+00, 1.06375743e+00, + 1.06369358e+00, 1.06356632e+00, 1.06337707e+00, 1.06312782e+00, + 1.06282156e+00, 1.06245782e+00, 1.06203634e+00, 1.06155996e+00, + 1.06102951e+00, 1.06044797e+00, 1.05981709e+00, 1.05914163e+00, + 1.05842136e+00, 1.05765876e+00, 1.05685377e+00, 1.05600761e+00, + 1.05512006e+00, 1.05419505e+00, 1.05323346e+00, 1.05223985e+00, + 1.05121668e+00, 1.05016637e+00, 1.04908779e+00, 1.04798366e+00, + 1.04685334e+00, 1.04569860e+00, 1.04452056e+00, 1.04332348e+00, + 1.04210831e+00, 1.04087907e+00, 1.03963603e+00, 1.03838099e+00, + 1.03711403e+00, 1.03583813e+00, 1.03455276e+00, 1.03326200e+00, + 1.03196750e+00, 1.03067200e+00, 1.02937564e+00, 1.02808244e+00, + 1.02679167e+00, 1.02550635e+00, 1.02422655e+00, 1.02295558e+00, + 1.02169299e+00, 1.02044475e+00, 1.01920733e+00, 1.01799992e+00, + 1.01716022e+00, 1.01587289e+00, 1.01461783e+00, 1.01339738e+00, + 1.01221017e+00, 1.01105652e+00, 1.00993444e+00, 1.00884559e+00, + 1.00778956e+00, 1.00676790e+00, 1.00577851e+00, 1.00482173e+00, + 1.00389592e+00, 1.00300262e+00, 1.00214091e+00, 1.00131213e+00, + 1.00051460e+00, 9.99748988e-01, 9.99013486e-01, 9.98309229e-01, + 9.97634934e-01, 9.96991885e-01, 9.96378601e-01, 9.95795982e-01, + 9.95242217e-01, 9.94718132e-01, 9.94222122e-01, 9.93755313e-01, + 9.93316216e-01, 9.92905809e-01, 9.92522422e-01, 9.92166957e-01, + 9.91837704e-01, 9.91535508e-01, 9.91258603e-01, 9.91007878e-01, + 9.90781723e-01, 9.90581104e-01, 9.90404336e-01, 9.90252267e-01, + 9.90123118e-01, 9.90017726e-01, 9.89934325e-01, 9.89873712e-01, + 9.89834110e-01, 9.89816359e-01, 9.89818707e-01, 9.89841998e-01, + 9.89884438e-01, 9.89946800e-01, 9.90027287e-01, 9.90126680e-01, + 9.90243175e-01, 9.90377594e-01, 9.90528134e-01, 9.90695564e-01, + 9.90878043e-01, 9.91076302e-01, 9.91288540e-01, 9.91515602e-01, + 9.91755666e-01, 9.92009469e-01, 9.92275155e-01, 9.92553486e-01, + 9.92842693e-01, 9.93143533e-01, 9.93454080e-01, 9.93775067e-01, + 9.94104689e-01, 9.94443742e-01, 9.94790398e-01, 9.95145361e-01, + 9.95506800e-01, 9.95875534e-01, 9.96249681e-01, 9.96629919e-01, + 9.97014367e-01, 9.97403799e-01, 9.97796404e-01, 9.98192871e-01, + 9.98591286e-01, 9.98992436e-01, 9.99394506e-01, 9.99798247e-01, + 1.00020179e+00, 1.00060586e+00, 1.00100858e+00, 1.00141070e+00, + 1.00181040e+00, 1.00220846e+00, 1.00260296e+00, 1.00299457e+00, + 1.00338148e+00, 1.00376444e+00, 1.00414155e+00, 1.00451348e+00, + 1.00487832e+00, 1.00523688e+00, 1.00558730e+00, 1.00593027e+00, + 1.00626393e+00, 1.00658905e+00, 1.00690380e+00, 1.00720890e+00, + 1.00750238e+00, 1.00778498e+00, 1.00805489e+00, 1.00831287e+00, + 1.00855700e+00, 1.00878802e+00, 1.00900405e+00, 1.00920593e+00, + 1.00939182e+00, 1.00956244e+00, 1.00971590e+00, 1.00985296e+00, + 1.00997177e+00, 1.01007317e+00, 1.01015529e+00, 1.01021893e+00, + 1.01026225e+00, 1.01028602e+00, 1.01028842e+00, 1.01027030e+00, + 1.01022988e+00, 1.01016802e+00, 1.01008292e+00, 1.00997541e+00, + 1.00984369e+00, 1.00968863e+00, 1.00950846e+00, 1.00930404e+00, + 1.00907371e+00, 1.00881848e+00, 1.00853675e+00, 1.00822947e+00, + 1.00789488e+00, 1.00753391e+00, 1.00714488e+00, 1.00672876e+00, + 1.00628393e+00, 1.00581146e+00, 1.00530991e+00, 1.00478053e+00, + 1.00422177e+00, 1.00363456e+00, 1.00301719e+00, 1.00237067e+00, + 1.00169363e+00, 1.00098749e+00, 1.00025108e+00, 9.99485663e-01, + 9.98689592e-01, 9.97863666e-01, 9.97006370e-01, 9.96119199e-01, + 9.95201404e-01, 9.94254687e-01, 9.93277595e-01, 9.92270651e-01, + 9.91231967e-01, 9.90163286e-01, 9.89064394e-01, 9.87937115e-01, + 9.86779736e-01, 9.85592773e-01, 9.84375125e-01, 9.83129288e-01, + 9.81348463e-01, 9.79890963e-01, 9.78400459e-01, 9.76860435e-01, + 9.75269879e-01, 9.73627353e-01, 9.71931341e-01, 9.70180498e-01, + 9.68372652e-01, 9.66506952e-01, 9.64580027e-01, 9.62592318e-01, + 9.60540986e-01, 9.58425534e-01, 9.56244393e-01, 9.53998416e-01, + 9.51684014e-01, 9.49301185e-01, 9.46846884e-01, 9.44320232e-01, + 9.41718404e-01, 9.39042580e-01, 9.36290624e-01, 9.33464050e-01, + 9.30560854e-01, 9.27580507e-01, 9.24519592e-01, 9.21378471e-01, + 9.18153414e-01, 9.14844696e-01, 9.11451652e-01, 9.07976524e-01, + 9.04417545e-01, 9.00776308e-01, 8.97050058e-01, 8.93238398e-01, + 8.89338681e-01, 8.85351360e-01, 8.81274023e-01, 8.77109638e-01, + 8.72857927e-01, 8.68519505e-01, 8.64092796e-01, 8.59579819e-01, + 8.54976007e-01, 8.50285220e-01, 8.45502615e-01, 8.40630470e-01, + 8.35667925e-01, 8.30619943e-01, 8.25482007e-01, 8.20258909e-01, + 8.14946648e-01, 8.09546696e-01, 8.04059978e-01, 7.98489378e-01, + 7.92831417e-01, 7.87090668e-01, 7.81262450e-01, 7.75353947e-01, + 7.69363613e-01, 7.63291769e-01, 7.57139016e-01, 7.50901711e-01, + 7.44590843e-01, 7.38205136e-01, 7.31738075e-01, 7.25199287e-01, + 7.18588225e-01, 7.11905687e-01, 7.05153668e-01, 6.98332634e-01, + 6.91444101e-01, 6.84490545e-01, 6.77470119e-01, 6.70388375e-01, + 6.63245210e-01, 6.56045780e-01, 6.48788627e-01, 6.41477162e-01, + 6.34114323e-01, 6.26702000e-01, 6.19235334e-01, 6.11720596e-01, + 6.04161612e-01, 5.96559133e-01, 5.88914401e-01, 5.81234783e-01, + 5.73519989e-01, 5.65770616e-01, 5.57988067e-01, 5.50173851e-01, + 5.42330194e-01, 5.34460798e-01, 5.26568538e-01, 5.18656324e-01, + 5.10728813e-01, 5.02781159e-01, 4.94819491e-01, 4.86845139e-01, + 4.78860889e-01, 4.70869928e-01, 4.62875144e-01, 4.54877894e-01, + 4.46882512e-01, 4.38889325e-01, 4.30898123e-01, 4.22918322e-01, + 4.14950878e-01, 4.06993964e-01, 3.99052648e-01, 3.91134614e-01, + 3.83234031e-01, 3.75354653e-01, 3.67502060e-01, 3.59680098e-01, + 3.51887312e-01, 3.44130166e-01, 3.36408100e-01, 3.28728966e-01, + 3.21090505e-01, 3.13496418e-01, 3.05951565e-01, 2.98454319e-01, + 2.91010565e-01, 2.83621109e-01, 2.76285415e-01, 2.69019585e-01, + 2.61812445e-01, 2.54659232e-01, 2.47584348e-01, 2.40578694e-01, + 2.33647009e-01, 2.26788433e-01, 2.20001992e-01, 2.13301325e-01, + 2.06677771e-01, 2.00140409e-01, 1.93683630e-01, 1.87310343e-01, + 1.81027384e-01, 1.74839476e-01, 1.68739644e-01, 1.62737273e-01, + 1.56825277e-01, 1.51012382e-01, 1.45298230e-01, 1.39687469e-01, + 1.34171842e-01, 1.28762544e-01, 1.23455562e-01, 1.18254662e-01, + 1.13159677e-01, 1.08171439e-01, 1.03290734e-01, 9.85202978e-02, + 9.38600023e-02, 8.93117360e-02, 8.48752103e-02, 8.05523737e-02, + 7.63429787e-02, 7.22489246e-02, 6.82699120e-02, 6.44077291e-02, + 6.06620003e-02, 5.70343711e-02, 5.35243715e-02, 5.01334690e-02, + 4.68610790e-02, 4.37084453e-02, 4.06748365e-02, 3.77612269e-02, + 3.49667099e-02, 3.22919275e-02, 2.97357669e-02, 2.72984629e-02, + 2.49787186e-02, 2.27762542e-02, 2.06895808e-02, 1.87178169e-02, + 1.68593418e-02, 1.51125125e-02, 1.34757094e-02, 1.19462709e-02, + 1.05228754e-02, 9.20130941e-03, 7.98124316e-03, 6.85547314e-03, + 5.82657334e-03, 4.87838525e-03, 4.02351119e-03, 3.15418663e-03, +}; + +static const float mdct_win_10m_8k[__LC3_MDCT_WIN_LEN(10000, 8000)] = { + -7.07854671e-04, -2.09819773e-03, -4.52519808e-03, -8.23397633e-03, + -1.33771310e-02, -1.99972156e-02, -2.80090946e-02, -3.72150208e-02, + -4.73176826e-02, -5.79465483e-02, -6.86760675e-02, -7.90464744e-02, + -8.85970547e-02, -9.68830362e-02, -1.03496124e-01, -1.08076646e-01, + -1.10324226e-01, -1.09980985e-01, -1.06817214e-01, -1.00619042e-01, + -9.11645251e-02, -7.82061748e-02, -6.14668812e-02, -4.06336286e-02, + -1.53632952e-02, 1.47015507e-02, 4.98973651e-02, 9.05036926e-02, + 1.36691102e-01, 1.88468639e-01, 2.45645680e-01, 3.07778908e-01, + 3.74164237e-01, 4.43811480e-01, 5.15473546e-01, 5.87666172e-01, + 6.58761977e-01, 7.27057670e-01, 7.90875299e-01, 8.48664336e-01, + 8.99132024e-01, 9.41334815e-01, 9.74763483e-01, 9.99411473e-01, + 1.01576037e+00, 1.02473616e+00, 1.02763429e+00, 1.02599149e+00, + 1.02142721e+00, 1.01543986e+00, 1.00936693e+00, 1.00350816e+00, + 9.98889821e-01, 9.95313390e-01, 9.92594392e-01, 9.90577196e-01, + 9.89137162e-01, 9.88179075e-01, 9.87624927e-01, 9.87405628e-01, + 9.87452485e-01, 9.87695113e-01, 9.88064062e-01, 9.88492687e-01, + 9.88923003e-01, 9.89307497e-01, 9.89614633e-01, 9.89831927e-01, + 9.89969310e-01, 9.90060335e-01, 9.90157502e-01, 9.90325529e-01, + 9.90630379e-01, 9.91129889e-01, 9.91866549e-01, 9.92861973e-01, + 9.94115607e-01, 9.95603378e-01, 9.97279311e-01, 9.99078484e-01, + 1.00092237e+00, 1.00272811e+00, 1.00441604e+00, 1.00591922e+00, + 1.00718935e+00, 1.00820015e+00, 1.00894949e+00, 1.00945824e+00, + 1.00976898e+00, 1.00994034e+00, 1.01003945e+00, 1.01013232e+00, + 1.01027252e+00, 1.01049435e+00, 1.01080807e+00, 1.01120107e+00, + 1.01164127e+00, 1.01208013e+00, 1.01245818e+00, 1.01270696e+00, + 1.01275501e+00, 1.01253013e+00, 1.01196233e+00, 1.01098214e+00, + 1.00951244e+00, 1.00746086e+00, 1.00470868e+00, 1.00111141e+00, + 9.96504102e-01, 9.90720000e-01, 9.82376587e-01, 9.70882175e-01, + 9.54673298e-01, 9.32155386e-01, 9.01800368e-01, 8.62398408e-01, + 8.13281737e-01, 7.54455197e-01, 6.86658072e-01, 6.11348804e-01, + 5.30618165e-01, 4.47130985e-01, 3.63911468e-01, 2.84164703e-01, + 2.11020945e-01, 1.47228797e-01, 9.48266535e-02, 5.48243661e-02, + 2.70146141e-02, 9.99674359e-03, +}; + +static const float mdct_win_10m_16k[__LC3_MDCT_WIN_LEN(10000, 16000)] = { + -4.61989875e-04, -9.74716672e-04, -1.66447310e-03, -2.59710692e-03, + -3.80628516e-03, -5.32460872e-03, -7.17588528e-03, -9.38248086e-03, + -1.19527030e-02, -1.48952816e-02, -1.82066640e-02, -2.18757093e-02, + -2.58847194e-02, -3.02086274e-02, -3.48159779e-02, -3.96706799e-02, + -4.47269805e-02, -4.99422586e-02, -5.52633479e-02, -6.06371724e-02, + -6.60096152e-02, -7.13196627e-02, -7.65117823e-02, -8.15296401e-02, + -8.63113754e-02, -9.08041129e-02, -9.49537776e-02, -9.87073651e-02, + -1.02020268e-01, -1.04843883e-01, -1.07138231e-01, -1.08869014e-01, + -1.09996966e-01, -1.10489847e-01, -1.10322584e-01, -1.09462175e-01, + -1.07883429e-01, -1.05561251e-01, -1.02465016e-01, -9.85701457e-02, + -9.38468492e-02, -8.82630999e-02, -8.17879272e-02, -7.43878560e-02, + -6.60218980e-02, -5.66565564e-02, -4.62445689e-02, -3.47458578e-02, + -2.21158161e-02, -8.31042570e-03, 6.71769764e-03, 2.30064206e-02, + 4.06010646e-02, 5.95323909e-02, 7.98335419e-02, 1.01523314e-01, + 1.24617139e-01, 1.49115252e-01, 1.75006740e-01, 2.02269985e-01, + 2.30865538e-01, 2.60736512e-01, 2.91814469e-01, 3.24009570e-01, + 3.57217518e-01, 3.91314689e-01, 4.26157164e-01, 4.61592545e-01, + 4.97447159e-01, 5.33532682e-01, 5.69654673e-01, 6.05608382e-01, + 6.41183084e-01, 6.76165350e-01, 7.10340055e-01, 7.43494372e-01, + 7.75428189e-01, 8.05943723e-01, 8.34858937e-01, 8.62010834e-01, + 8.87259971e-01, 9.10486312e-01, 9.31596250e-01, 9.50522086e-01, + 9.67236671e-01, 9.81739750e-01, 9.94055718e-01, 1.00424751e+00, + 1.01240743e+00, 1.01865099e+00, 1.02311884e+00, 1.02597245e+00, + 1.02739752e+00, 1.02758583e+00, 1.02673867e+00, 1.02506178e+00, + 1.02275651e+00, 1.02000914e+00, 1.01699650e+00, 1.01391595e+00, + 1.01104487e+00, 1.00777386e+00, 1.00484875e+00, 1.00224501e+00, + 9.99939317e-01, 9.97905542e-01, 9.96120338e-01, 9.94559753e-01, + 9.93203161e-01, 9.92029727e-01, 9.91023065e-01, 9.90166895e-01, + 9.89448837e-01, 9.88855636e-01, 9.88377852e-01, 9.88005163e-01, + 9.87729546e-01, 9.87541274e-01, 9.87432981e-01, 9.87394992e-01, + 9.87419705e-01, 9.87497321e-01, 9.87620124e-01, 9.87778192e-01, + 9.87963798e-01, 9.88167801e-01, 9.88383520e-01, 9.88602222e-01, + 9.88818277e-01, 9.89024798e-01, 9.89217866e-01, 9.89392368e-01, + 9.89546334e-01, 9.89677201e-01, 9.89785920e-01, 9.89872536e-01, + 9.89941079e-01, 9.89994556e-01, 9.90039402e-01, 9.90081472e-01, + 9.90129379e-01, 9.90190227e-01, 9.90273445e-01, 9.90386228e-01, + 9.90537983e-01, 9.90734883e-01, 9.90984259e-01, 9.91290512e-01, + 9.91658694e-01, 9.92090615e-01, 9.92588721e-01, 9.93151653e-01, + 9.93779087e-01, 9.94466818e-01, 9.95211663e-01, 9.96006862e-01, + 9.96846133e-01, 9.97720337e-01, 9.98621352e-01, 9.99538258e-01, + 1.00046196e+00, 1.00138055e+00, 1.00228487e+00, 1.00316385e+00, + 1.00400915e+00, 1.00481138e+00, 1.00556397e+00, 1.00625986e+00, + 1.00689557e+00, 1.00746662e+00, 1.00797244e+00, 1.00841147e+00, + 1.00878601e+00, 1.00909776e+00, 1.00935176e+00, 1.00955240e+00, + 1.00970709e+00, 1.00982209e+00, 1.00990696e+00, 1.00996902e+00, + 1.01001789e+00, 1.01006081e+00, 1.01010656e+00, 1.01016113e+00, + 1.01023108e+00, 1.01031948e+00, 1.01043047e+00, 1.01056410e+00, + 1.01072136e+00, 1.01089966e+00, 1.01109699e+00, 1.01130817e+00, + 1.01152919e+00, 1.01175301e+00, 1.01197388e+00, 1.01218284e+00, + 1.01237303e+00, 1.01253506e+00, 1.01266098e+00, 1.01274058e+00, + 1.01276592e+00, 1.01272696e+00, 1.01261590e+00, 1.01242289e+00, + 1.01214046e+00, 1.01175881e+00, 1.01126996e+00, 1.01066368e+00, + 1.00993075e+00, 1.00905825e+00, 1.00803431e+00, 1.00684335e+00, + 1.00547001e+00, 1.00389477e+00, 1.00209885e+00, 1.00006069e+00, + 9.97760020e-01, 9.95174643e-01, 9.92286108e-01, 9.89075787e-01, + 9.84736245e-01, 9.79861353e-01, 9.74137862e-01, 9.67333198e-01, + 9.59253976e-01, 9.49698408e-01, 9.38463416e-01, 9.25356797e-01, + 9.10198679e-01, 8.92833832e-01, 8.73143784e-01, 8.51042044e-01, + 8.26483991e-01, 7.99468149e-01, 7.70043128e-01, 7.38302860e-01, + 7.04381434e-01, 6.68461648e-01, 6.30775533e-01, 5.91579959e-01, + 5.51170316e-01, 5.09891542e-01, 4.68101711e-01, 4.26177297e-01, + 3.84517234e-01, 3.43522867e-01, 3.03600465e-01, 2.65143468e-01, + 2.28528397e-01, 1.94102191e-01, 1.62173542e-01, 1.33001524e-01, + 1.06784043e-01, 8.36505724e-02, 6.36518811e-02, 4.67653841e-02, + 3.28807275e-02, 2.18305756e-02, 1.33638143e-02, 6.75812489e-03, +}; + +static const float mdct_win_10m_24k[__LC3_MDCT_WIN_LEN(10000, 24000)] = { + -3.61349642e-04, -7.07854671e-04, -1.07444364e-03, -1.53347854e-03, + -2.09819773e-03, -2.77842087e-03, -3.58412992e-03, -4.52519808e-03, + -5.60932724e-03, -6.84323454e-03, -8.23397633e-03, -9.78531476e-03, + -1.14988030e-02, -1.33771310e-02, -1.54218168e-02, -1.76297991e-02, + -1.99972156e-02, -2.25208056e-02, -2.51940630e-02, -2.80090946e-02, + -3.09576509e-02, -3.40299627e-02, -3.72150208e-02, -4.05005325e-02, + -4.38721922e-02, -4.73176826e-02, -5.08232534e-02, -5.43716664e-02, + -5.79465483e-02, -6.15342620e-02, -6.51170816e-02, -6.86760675e-02, + -7.21944781e-02, -7.56569598e-02, -7.90464744e-02, -8.23444256e-02, + -8.55332458e-02, -8.85970547e-02, -9.15209110e-02, -9.42884745e-02, + -9.68830362e-02, -9.92912326e-02, -1.01500847e-01, -1.03496124e-01, + -1.05263700e-01, -1.06793998e-01, -1.08076646e-01, -1.09099730e-01, + -1.09852449e-01, -1.10324226e-01, -1.10508462e-01, -1.10397741e-01, + -1.09980985e-01, -1.09249277e-01, -1.08197423e-01, -1.06817214e-01, + -1.05099580e-01, -1.03036011e-01, -1.00619042e-01, -9.78412002e-02, + -9.46930422e-02, -9.11645251e-02, -8.72464453e-02, -8.29304391e-02, + -7.82061748e-02, -7.30614243e-02, -6.74846818e-02, -6.14668812e-02, + -5.49949726e-02, -4.80544442e-02, -4.06336286e-02, -3.27204559e-02, + -2.43012258e-02, -1.53632952e-02, -5.89143427e-03, 4.12659586e-03, + 1.47015507e-02, 2.58473819e-02, 3.75765277e-02, 4.98973651e-02, + 6.28203403e-02, 7.63539773e-02, 9.05036926e-02, 1.05274712e-01, + 1.20670347e-01, 1.36691102e-01, 1.53334389e-01, 1.70595471e-01, + 1.88468639e-01, 2.06944996e-01, 2.26009300e-01, 2.45645680e-01, + 2.65834602e-01, 2.86554381e-01, 3.07778908e-01, 3.29476944e-01, + 3.51617148e-01, 3.74164237e-01, 3.97073959e-01, 4.20304305e-01, + 4.43811480e-01, 4.67544229e-01, 4.91449863e-01, 5.15473546e-01, + 5.39555764e-01, 5.63639982e-01, 5.87666172e-01, 6.11569531e-01, + 6.35289059e-01, 6.58761977e-01, 6.81923097e-01, 7.04709282e-01, + 7.27057670e-01, 7.48906896e-01, 7.70199019e-01, 7.90875299e-01, + 8.10878869e-01, 8.30157914e-01, 8.48664336e-01, 8.66354816e-01, + 8.83189685e-01, 8.99132024e-01, 9.14154056e-01, 9.28228255e-01, + 9.41334815e-01, 9.53461939e-01, 9.64604825e-01, 9.74763483e-01, + 9.83943539e-01, 9.92152910e-01, 9.99411473e-01, 1.00574608e+00, + 1.01118397e+00, 1.01576037e+00, 1.01951507e+00, 1.02249094e+00, + 1.02473616e+00, 1.02630410e+00, 1.02725098e+00, 1.02763429e+00, + 1.02751106e+00, 1.02694280e+00, 1.02599149e+00, 1.02471615e+00, + 1.02317598e+00, 1.02142721e+00, 1.01952157e+00, 1.01751012e+00, + 1.01543986e+00, 1.01346092e+00, 1.01165490e+00, 1.00936693e+00, + 1.00726318e+00, 1.00531319e+00, 1.00350816e+00, 1.00184079e+00, + 1.00030393e+00, 9.98889821e-01, 9.97591528e-01, 9.96401528e-01, + 9.95313390e-01, 9.94320108e-01, 9.93415896e-01, 9.92594392e-01, + 9.91851028e-01, 9.91179799e-01, 9.90577196e-01, 9.90038105e-01, + 9.89559439e-01, 9.89137162e-01, 9.88768437e-01, 9.88449792e-01, + 9.88179075e-01, 9.87952836e-01, 9.87769137e-01, 9.87624927e-01, + 9.87517995e-01, 9.87445813e-01, 9.87405628e-01, 9.87395112e-01, + 9.87411537e-01, 9.87452485e-01, 9.87514989e-01, 9.87596889e-01, + 9.87695113e-01, 9.87807582e-01, 9.87931200e-01, 9.88064062e-01, + 9.88203257e-01, 9.88347108e-01, 9.88492687e-01, 9.88638659e-01, + 9.88782558e-01, 9.88923003e-01, 9.89058172e-01, 9.89186767e-01, + 9.89307497e-01, 9.89419640e-01, 9.89522076e-01, 9.89614633e-01, + 9.89697035e-01, 9.89769260e-01, 9.89831927e-01, 9.89885257e-01, + 9.89930764e-01, 9.89969310e-01, 9.90002569e-01, 9.90032156e-01, + 9.90060335e-01, 9.90088981e-01, 9.90120659e-01, 9.90157502e-01, + 9.90202395e-01, 9.90257541e-01, 9.90325529e-01, 9.90408791e-01, + 9.90509649e-01, 9.90630379e-01, 9.90772711e-01, 9.90938744e-01, + 9.91129889e-01, 9.91347632e-01, 9.91592856e-01, 9.91866549e-01, + 9.92169132e-01, 9.92501085e-01, 9.92861973e-01, 9.93251918e-01, + 9.93670021e-01, 9.94115607e-01, 9.94587315e-01, 9.95083740e-01, + 9.95603378e-01, 9.96143992e-01, 9.96703453e-01, 9.97279311e-01, + 9.97869086e-01, 9.98469709e-01, 9.99078484e-01, 9.99691901e-01, + 1.00030819e+00, 1.00092237e+00, 1.00153264e+00, 1.00213546e+00, + 1.00272811e+00, 1.00330745e+00, 1.00387093e+00, 1.00441604e+00, + 1.00494055e+00, 1.00544214e+00, 1.00591922e+00, 1.00637030e+00, + 1.00679393e+00, 1.00718935e+00, 1.00755557e+00, 1.00789267e+00, + 1.00820015e+00, 1.00847842e+00, 1.00872788e+00, 1.00894949e+00, + 1.00914411e+00, 1.00931322e+00, 1.00945824e+00, 1.00958128e+00, + 1.00968409e+00, 1.00976898e+00, 1.00983831e+00, 1.00989455e+00, + 1.00994034e+00, 1.00997792e+00, 1.01001023e+00, 1.01003945e+00, + 1.01006820e+00, 1.01009839e+00, 1.01013232e+00, 1.01017166e+00, + 1.01021810e+00, 1.01027252e+00, 1.01033649e+00, 1.01041022e+00, + 1.01049435e+00, 1.01058887e+00, 1.01069350e+00, 1.01080807e+00, + 1.01093144e+00, 1.01106288e+00, 1.01120107e+00, 1.01134470e+00, + 1.01149190e+00, 1.01164127e+00, 1.01179028e+00, 1.01193757e+00, + 1.01208013e+00, 1.01221624e+00, 1.01234291e+00, 1.01245818e+00, + 1.01255888e+00, 1.01264286e+00, 1.01270696e+00, 1.01274895e+00, + 1.01276580e+00, 1.01275501e+00, 1.01271380e+00, 1.01263978e+00, + 1.01253013e+00, 1.01238231e+00, 1.01219407e+00, 1.01196233e+00, + 1.01168517e+00, 1.01135914e+00, 1.01098214e+00, 1.01055072e+00, + 1.01006213e+00, 1.00951244e+00, 1.00889869e+00, 1.00821592e+00, + 1.00746086e+00, 1.00662774e+00, 1.00571234e+00, 1.00470868e+00, + 1.00361147e+00, 1.00241429e+00, 1.00111141e+00, 9.99696165e-01, + 9.98162595e-01, 9.96504102e-01, 9.94714888e-01, 9.92789191e-01, + 9.90720000e-01, 9.88479371e-01, 9.85534766e-01, 9.82376587e-01, + 9.78974733e-01, 9.75162381e-01, 9.70882175e-01, 9.66080552e-01, + 9.60697640e-01, 9.54673298e-01, 9.47947935e-01, 9.40460905e-01, + 9.32155386e-01, 9.22977548e-01, 9.12874535e-01, 9.01800368e-01, + 8.89716328e-01, 8.76590897e-01, 8.62398408e-01, 8.47120080e-01, + 8.30747973e-01, 8.13281737e-01, 7.94729145e-01, 7.75110884e-01, + 7.54455197e-01, 7.32796355e-01, 7.10179084e-01, 6.86658072e-01, + 6.62296243e-01, 6.37168412e-01, 6.11348804e-01, 5.84920660e-01, + 5.57974743e-01, 5.30618165e-01, 5.02952396e-01, 4.75086883e-01, + 4.47130985e-01, 4.19204992e-01, 3.91425291e-01, 3.63911468e-01, + 3.36783777e-01, 3.10162784e-01, 2.84164703e-01, 2.58903371e-01, + 2.34488060e-01, 2.11020945e-01, 1.88599764e-01, 1.67310081e-01, + 1.47228797e-01, 1.28422307e-01, 1.10942255e-01, 9.48266535e-02, + 8.00991437e-02, 6.67676585e-02, 5.48243661e-02, 4.42458885e-02, + 3.49936100e-02, 2.70146141e-02, 2.02437018e-02, 1.46079676e-02, + 9.99674359e-03, 5.30523510e-03, +}; + +static const float mdct_win_10m_32k[__LC3_MDCT_WIN_LEN(10000, 32000)] = { + -3.02115349e-04, -5.86773749e-04, -8.36650400e-04, -1.12663536e-03, + -1.47049294e-03, -1.87347339e-03, -2.33929236e-03, -2.87200807e-03, + -3.47625639e-03, -4.15596382e-03, -4.91456379e-03, -5.75517250e-03, + -6.68062338e-03, -7.69381692e-03, -8.79676075e-03, -9.99050307e-03, + -1.12757412e-02, -1.26533415e-02, -1.41243899e-02, -1.56888962e-02, + -1.73451209e-02, -1.90909737e-02, -2.09254671e-02, -2.28468479e-02, + -2.48520772e-02, -2.69374670e-02, -2.90995249e-02, -3.13350463e-02, + -3.36396073e-02, -3.60082097e-02, -3.84360174e-02, -4.09174603e-02, + -4.34465489e-02, -4.60178672e-02, -4.86259851e-02, -5.12647420e-02, + -5.39264475e-02, -5.66038431e-02, -5.92911675e-02, -6.19826820e-02, + -6.46702555e-02, -6.73454222e-02, -7.00009902e-02, -7.26305701e-02, + -7.52278496e-02, -7.77852594e-02, -8.02948025e-02, -8.27492454e-02, + -8.51412546e-02, -8.74637912e-02, -8.97106934e-02, -9.18756408e-02, + -9.39517698e-02, -9.59313774e-02, -9.78084326e-02, -9.95785130e-02, + -1.01236117e-01, -1.02774104e-01, -1.04186122e-01, -1.05468025e-01, + -1.06616088e-01, -1.07625538e-01, -1.08491230e-01, -1.09208742e-01, + -1.09773615e-01, -1.10180886e-01, -1.10427188e-01, -1.10510836e-01, + -1.10428147e-01, -1.10173922e-01, -1.09743736e-01, -1.09135313e-01, + -1.08346734e-01, -1.07373994e-01, -1.06213016e-01, -1.04860615e-01, + -1.03313240e-01, -1.01567316e-01, -9.96200551e-02, -9.74680323e-02, + -9.51072362e-02, -9.25330338e-02, -8.97412522e-02, -8.67287769e-02, + -8.34921384e-02, -8.00263990e-02, -7.63267954e-02, -7.23880616e-02, + -6.82057680e-02, -6.37761143e-02, -5.90938600e-02, -5.41531632e-02, + -4.89481272e-02, -4.34734711e-02, -3.77246130e-02, -3.16958761e-02, + -2.53817983e-02, -1.87768910e-02, -1.18746138e-02, -4.66909925e-03, + 2.84409675e-03, 1.06697612e-02, 1.88135595e-02, 2.72815601e-02, + 3.60781047e-02, 4.52070276e-02, 5.46723880e-02, 6.44786605e-02, + 7.46286220e-02, 8.51249057e-02, 9.59698399e-02, 1.07165078e-01, + 1.18711585e-01, 1.30610107e-01, 1.42859645e-01, 1.55458473e-01, + 1.68404161e-01, 1.81694789e-01, 1.95327388e-01, 2.09296321e-01, + 2.23594564e-01, 2.38216022e-01, 2.53152972e-01, 2.68396157e-01, + 2.83936139e-01, 2.99762426e-01, 3.15861908e-01, 3.32221055e-01, + 3.48826468e-01, 3.65664038e-01, 3.82715297e-01, 3.99961186e-01, + 4.17384327e-01, 4.34966962e-01, 4.52687640e-01, 4.70524201e-01, + 4.88453925e-01, 5.06454555e-01, 5.24500675e-01, 5.42567437e-01, + 5.60631204e-01, 5.78667265e-01, 5.96647704e-01, 6.14545890e-01, + 6.32336194e-01, 6.49992632e-01, 6.67487403e-01, 6.84793267e-01, + 7.01883546e-01, 7.18732254e-01, 7.35312821e-01, 7.51600199e-01, + 7.67569925e-01, 7.83197457e-01, 7.98458386e-01, 8.13329535e-01, + 8.27789227e-01, 8.41817856e-01, 8.55396130e-01, 8.68506898e-01, + 8.81133444e-01, 8.93259678e-01, 9.04874884e-01, 9.15965761e-01, + 9.26521530e-01, 9.36533999e-01, 9.45997703e-01, 9.54908841e-01, + 9.63265812e-01, 9.71068890e-01, 9.78320416e-01, 9.85022676e-01, + 9.91179208e-01, 9.96798994e-01, 1.00189402e+00, 1.00647434e+00, + 1.01055206e+00, 1.01414254e+00, 1.01726259e+00, 1.01992884e+00, + 1.02215987e+00, 1.02397632e+00, 1.02540073e+00, 1.02645534e+00, + 1.02716451e+00, 1.02755273e+00, 1.02764446e+00, 1.02746325e+00, + 1.02703590e+00, 1.02638907e+00, 1.02554820e+00, 1.02453713e+00, + 1.02338080e+00, 1.02210370e+00, 1.02072836e+00, 1.01927533e+00, + 1.01776518e+00, 1.01621736e+00, 1.01466531e+00, 1.01324907e+00, + 1.01194801e+00, 1.01018909e+00, 1.00855796e+00, 1.00701129e+00, + 1.00554876e+00, 1.00416842e+00, 1.00286727e+00, 1.00164177e+00, + 1.00048907e+00, 9.99406080e-01, 9.98389887e-01, 9.97437085e-01, + 9.96544484e-01, 9.95709855e-01, 9.94930241e-01, 9.94202405e-01, + 9.93524160e-01, 9.92893043e-01, 9.92306810e-01, 9.91763378e-01, + 9.91259764e-01, 9.90795450e-01, 9.90367789e-01, 9.89975161e-01, + 9.89616034e-01, 9.89289016e-01, 9.88992851e-01, 9.88726033e-01, + 9.88486872e-01, 9.88275104e-01, 9.88089217e-01, 9.87927711e-01, + 9.87789826e-01, 9.87674344e-01, 9.87580750e-01, 9.87507202e-01, + 9.87452945e-01, 9.87416974e-01, 9.87398469e-01, 9.87395830e-01, + 9.87408003e-01, 9.87434340e-01, 9.87473624e-01, 9.87524314e-01, + 9.87585620e-01, 9.87656379e-01, 9.87735892e-01, 9.87822558e-01, + 9.87915097e-01, 9.88013273e-01, 9.88115695e-01, 9.88221131e-01, + 9.88328903e-01, 9.88437831e-01, 9.88547679e-01, 9.88656841e-01, + 9.88764587e-01, 9.88870854e-01, 9.88974432e-01, 9.89074727e-01, + 9.89171004e-01, 9.89263102e-01, 9.89350722e-01, 9.89433065e-01, + 9.89509692e-01, 9.89581081e-01, 9.89646747e-01, 9.89706737e-01, + 9.89760693e-01, 9.89809448e-01, 9.89853013e-01, 9.89891471e-01, + 9.89925419e-01, 9.89955420e-01, 9.89982449e-01, 9.90006512e-01, + 9.90028481e-01, 9.90049748e-01, 9.90070956e-01, 9.90092836e-01, + 9.90116392e-01, 9.90142748e-01, 9.90173428e-01, 9.90208733e-01, + 9.90249864e-01, 9.90298369e-01, 9.90354850e-01, 9.90420508e-01, + 9.90495930e-01, 9.90582515e-01, 9.90681257e-01, 9.90792209e-01, + 9.90916546e-01, 9.91055074e-01, 9.91208461e-01, 9.91376861e-01, + 9.91560583e-01, 9.91760421e-01, 9.91976718e-01, 9.92209110e-01, + 9.92457914e-01, 9.92723123e-01, 9.93004954e-01, 9.93302728e-01, + 9.93616108e-01, 9.93945371e-01, 9.94289515e-01, 9.94648168e-01, + 9.95020303e-01, 9.95405817e-01, 9.95803871e-01, 9.96213027e-01, + 9.96632469e-01, 9.97061531e-01, 9.97499058e-01, 9.97943743e-01, + 9.98394057e-01, 9.98849312e-01, 9.99308343e-01, 9.99768922e-01, + 1.00023113e+00, 1.00069214e+00, 1.00115201e+00, 1.00160853e+00, + 1.00206049e+00, 1.00250721e+00, 1.00294713e+00, 1.00337891e+00, + 1.00380137e+00, 1.00421381e+00, 1.00461539e+00, 1.00500462e+00, + 1.00538063e+00, 1.00574328e+00, 1.00609151e+00, 1.00642491e+00, + 1.00674243e+00, 1.00704432e+00, 1.00733022e+00, 1.00759940e+00, + 1.00785206e+00, 1.00808818e+00, 1.00830803e+00, 1.00851125e+00, + 1.00869814e+00, 1.00886952e+00, 1.00902566e+00, 1.00916672e+00, + 1.00929336e+00, 1.00940640e+00, 1.00950702e+00, 1.00959526e+00, + 1.00967215e+00, 1.00973908e+00, 1.00979668e+00, 1.00984614e+00, + 1.00988808e+00, 1.00992409e+00, 1.00995538e+00, 1.00998227e+00, + 1.01000630e+00, 1.01002862e+00, 1.01005025e+00, 1.01007195e+00, + 1.01009437e+00, 1.01011892e+00, 1.01014650e+00, 1.01017711e+00, + 1.01021176e+00, 1.01025100e+00, 1.01029547e+00, 1.01034523e+00, + 1.01040032e+00, 1.01046156e+00, 1.01052862e+00, 1.01060152e+00, + 1.01067979e+00, 1.01076391e+00, 1.01085343e+00, 1.01094755e+00, + 1.01104595e+00, 1.01114849e+00, 1.01125440e+00, 1.01136308e+00, + 1.01147330e+00, 1.01158500e+00, 1.01169742e+00, 1.01180892e+00, + 1.01191926e+00, 1.01202724e+00, 1.01213215e+00, 1.01223273e+00, + 1.01232756e+00, 1.01241638e+00, 1.01249789e+00, 1.01257043e+00, + 1.01263330e+00, 1.01268528e+00, 1.01272556e+00, 1.01275258e+00, + 1.01276506e+00, 1.01276236e+00, 1.01274338e+00, 1.01270648e+00, + 1.01265084e+00, 1.01257543e+00, 1.01247947e+00, 1.01236111e+00, + 1.01221981e+00, 1.01205436e+00, 1.01186400e+00, 1.01164722e+00, + 1.01140252e+00, 1.01112965e+00, 1.01082695e+00, 1.01049292e+00, + 1.01012635e+00, 1.00972589e+00, 1.00929006e+00, 1.00881730e+00, + 1.00830503e+00, 1.00775283e+00, 1.00715783e+00, 1.00651805e+00, + 1.00583140e+00, 1.00509559e+00, 1.00430863e+00, 1.00346750e+00, + 1.00256950e+00, 1.00161271e+00, 1.00059427e+00, 9.99511170e-01, + 9.98360922e-01, 9.97140929e-01, 9.95848886e-01, 9.94481854e-01, + 9.93037528e-01, 9.91514656e-01, 9.89913680e-01, 9.88193062e-01, + 9.85942259e-01, 9.83566790e-01, 9.81142303e-01, 9.78521444e-01, + 9.75663604e-01, 9.72545344e-01, 9.69145663e-01, 9.65440618e-01, + 9.61404362e-01, 9.57011307e-01, 9.52236767e-01, 9.47054884e-01, + 9.41440374e-01, 9.35369161e-01, 9.28819009e-01, 9.21766289e-01, + 9.14189628e-01, 9.06069468e-01, 8.97389168e-01, 8.88133200e-01, + 8.78289389e-01, 8.67846957e-01, 8.56797064e-01, 8.45133465e-01, + 8.32854281e-01, 8.19959478e-01, 8.06451101e-01, 7.92334648e-01, + 7.77620449e-01, 7.62320618e-01, 7.46448649e-01, 7.30020573e-01, + 7.13056738e-01, 6.95580544e-01, 6.77617323e-01, 6.59195531e-01, + 6.40348643e-01, 6.21107220e-01, 6.01504928e-01, 5.81578761e-01, + 5.61367451e-01, 5.40918863e-01, 5.20273683e-01, 4.99478073e-01, + 4.78577418e-01, 4.57617260e-01, 4.36649021e-01, 4.15722146e-01, + 3.94885659e-01, 3.74190319e-01, 3.53686890e-01, 3.33426002e-01, + 3.13458647e-01, 2.93833790e-01, 2.74599264e-01, 2.55803064e-01, + 2.37490219e-01, 2.19703603e-01, 2.02485542e-01, 1.85874992e-01, + 1.69906780e-01, 1.54613227e-01, 1.40023821e-01, 1.26163740e-01, + 1.13053443e-01, 1.00708497e-01, 8.91402439e-02, 7.83561210e-02, + 6.83582123e-02, 5.91421154e-02, 5.06989301e-02, 4.30171776e-02, + 3.60802073e-02, 2.98631634e-02, 2.43372266e-02, 1.94767524e-02, + 1.52571017e-02, 1.16378749e-02, 8.43308778e-03, 4.44966900e-03, +}; + +static const float mdct_win_10m_48k[__LC3_MDCT_WIN_LEN(10000, 48000)] = { + -2.35303215e-04, -4.61989875e-04, -6.26293154e-04, -7.92918043e-04, + -9.74716672e-04, -1.18025689e-03, -1.40920904e-03, -1.66447310e-03, + -1.94659161e-03, -2.25708173e-03, -2.59710692e-03, -2.96760762e-03, + -3.37045488e-03, -3.80628516e-03, -4.27687377e-03, -4.78246990e-03, + -5.32460872e-03, -5.90340381e-03, -6.52041973e-03, -7.17588528e-03, + -7.87142282e-03, -8.60658604e-03, -9.38248086e-03, -1.01982718e-02, + -1.10552055e-02, -1.19527030e-02, -1.28920591e-02, -1.38726348e-02, + -1.48952816e-02, -1.59585662e-02, -1.70628856e-02, -1.82066640e-02, + -1.93906598e-02, -2.06135542e-02, -2.18757093e-02, -2.31752632e-02, + -2.45122745e-02, -2.58847194e-02, -2.72926374e-02, -2.87339090e-02, + -3.02086274e-02, -3.17144037e-02, -3.32509886e-02, -3.48159779e-02, + -3.64089241e-02, -3.80274232e-02, -3.96706799e-02, -4.13357542e-02, + -4.30220337e-02, -4.47269805e-02, -4.64502229e-02, -4.81889149e-02, + -4.99422586e-02, -5.17069080e-02, -5.34816204e-02, -5.52633479e-02, + -5.70512315e-02, -5.88427175e-02, -6.06371724e-02, -6.24310403e-02, + -6.42230355e-02, -6.60096152e-02, -6.77896227e-02, -6.95599687e-02, + -7.13196627e-02, -7.30658127e-02, -7.47975891e-02, -7.65117823e-02, + -7.82071142e-02, -7.98801069e-02, -8.15296401e-02, -8.31523735e-02, + -8.47472895e-02, -8.63113754e-02, -8.78437445e-02, -8.93416436e-02, + -9.08041129e-02, -9.22279576e-02, -9.36123287e-02, -9.49537776e-02, + -9.62515531e-02, -9.75028462e-02, -9.87073651e-02, -9.98627129e-02, + -1.00968022e-01, -1.02020268e-01, -1.03018380e-01, -1.03959636e-01, + -1.04843883e-01, -1.05668684e-01, -1.06434282e-01, -1.07138231e-01, + -1.07779996e-01, -1.08357063e-01, -1.08869014e-01, -1.09313559e-01, + -1.09690356e-01, -1.09996966e-01, -1.10233226e-01, -1.10397281e-01, + -1.10489847e-01, -1.10508642e-01, -1.10453743e-01, -1.10322584e-01, + -1.10114583e-01, -1.09827693e-01, -1.09462175e-01, -1.09016396e-01, + -1.08490885e-01, -1.07883429e-01, -1.07193718e-01, -1.06419636e-01, + -1.05561251e-01, -1.04616281e-01, -1.03584904e-01, -1.02465016e-01, + -1.01256900e-01, -9.99586457e-02, -9.85701457e-02, -9.70891114e-02, + -9.55154582e-02, -9.38468492e-02, -9.20830006e-02, -9.02217102e-02, + -8.82630999e-02, -8.62049382e-02, -8.40474215e-02, -8.17879272e-02, + -7.94262503e-02, -7.69598078e-02, -7.43878560e-02, -7.17079700e-02, + -6.89199478e-02, -6.60218980e-02, -6.30134942e-02, -5.98919191e-02, + -5.66565564e-02, -5.33040616e-02, -4.98342724e-02, -4.62445689e-02, + -4.25345569e-02, -3.87019577e-02, -3.47458578e-02, -3.06634152e-02, + -2.64542508e-02, -2.21158161e-02, -1.76474054e-02, -1.30458136e-02, + -8.31042570e-03, -3.43826866e-03, 1.57031548e-03, 6.71769764e-03, + 1.20047702e-02, 1.74339832e-02, 2.30064206e-02, 2.87248142e-02, + 3.45889635e-02, 4.06010646e-02, 4.67610292e-02, 5.30713391e-02, + 5.95323909e-02, 6.61464781e-02, 7.29129318e-02, 7.98335419e-02, + 8.69080741e-02, 9.41381377e-02, 1.01523314e-01, 1.09065152e-01, + 1.16762655e-01, 1.24617139e-01, 1.32627295e-01, 1.40793819e-01, + 1.49115252e-01, 1.57592141e-01, 1.66222480e-01, 1.75006740e-01, + 1.83943194e-01, 1.93031818e-01, 2.02269985e-01, 2.11656743e-01, + 2.21188852e-01, 2.30865538e-01, 2.40683799e-01, 2.50642064e-01, + 2.60736512e-01, 2.70965907e-01, 2.81325902e-01, 2.91814469e-01, + 3.02427028e-01, 3.13160350e-01, 3.24009570e-01, 3.34971959e-01, + 3.46042294e-01, 3.57217518e-01, 3.68491565e-01, 3.79859512e-01, + 3.91314689e-01, 4.02853287e-01, 4.14468833e-01, 4.26157164e-01, + 4.37911390e-01, 4.49725632e-01, 4.61592545e-01, 4.73506703e-01, + 4.85460018e-01, 4.97447159e-01, 5.09459723e-01, 5.21490984e-01, + 5.33532682e-01, 5.45578981e-01, 5.57621716e-01, 5.69654673e-01, + 5.81668558e-01, 5.93656062e-01, 6.05608382e-01, 6.17519206e-01, + 6.29379661e-01, 6.41183084e-01, 6.52920354e-01, 6.64584079e-01, + 6.76165350e-01, 6.87657395e-01, 6.99051154e-01, 7.10340055e-01, + 7.21514933e-01, 7.32569177e-01, 7.43494372e-01, 7.54284633e-01, + 7.64931365e-01, 7.75428189e-01, 7.85767017e-01, 7.95941465e-01, + 8.05943723e-01, 8.15768707e-01, 8.25408622e-01, 8.34858937e-01, + 8.44112583e-01, 8.53165119e-01, 8.62010834e-01, 8.70645634e-01, + 8.79063156e-01, 8.87259971e-01, 8.95231329e-01, 9.02975168e-01, + 9.10486312e-01, 9.17762555e-01, 9.24799743e-01, 9.31596250e-01, + 9.38149486e-01, 9.44458839e-01, 9.50522086e-01, 9.56340292e-01, + 9.61911452e-01, 9.67236671e-01, 9.72315664e-01, 9.77150119e-01, + 9.81739750e-01, 9.86086587e-01, 9.90190638e-01, 9.94055718e-01, + 9.97684240e-01, 1.00108096e+00, 1.00424751e+00, 1.00718858e+00, + 1.00990665e+00, 1.01240743e+00, 1.01469470e+00, 1.01677466e+00, + 1.01865099e+00, 1.02033046e+00, 1.02181733e+00, 1.02311884e+00, + 1.02424026e+00, 1.02518972e+00, 1.02597245e+00, 1.02659694e+00, + 1.02706918e+00, 1.02739752e+00, 1.02758790e+00, 1.02764895e+00, + 1.02758583e+00, 1.02740852e+00, 1.02712299e+00, 1.02673867e+00, + 1.02626166e+00, 1.02570100e+00, 1.02506178e+00, 1.02435398e+00, + 1.02358239e+00, 1.02275651e+00, 1.02188060e+00, 1.02096387e+00, + 1.02000914e+00, 1.01902729e+00, 1.01801944e+00, 1.01699650e+00, + 1.01595743e+00, 1.01492344e+00, 1.01391595e+00, 1.01304757e+00, + 1.01221613e+00, 1.01104487e+00, 1.00991459e+00, 1.00882489e+00, + 1.00777386e+00, 1.00676170e+00, 1.00578665e+00, 1.00484875e+00, + 1.00394608e+00, 1.00307885e+00, 1.00224501e+00, 1.00144473e+00, + 1.00067619e+00, 9.99939317e-01, 9.99232085e-01, 9.98554813e-01, + 9.97905542e-01, 9.97284268e-01, 9.96689095e-01, 9.96120338e-01, + 9.95576126e-01, 9.95056572e-01, 9.94559753e-01, 9.94086038e-01, + 9.93633779e-01, 9.93203161e-01, 9.92792187e-01, 9.92401518e-01, + 9.92029727e-01, 9.91676778e-01, 9.91340877e-01, 9.91023065e-01, + 9.90721643e-01, 9.90436680e-01, 9.90166895e-01, 9.89913101e-01, + 9.89673564e-01, 9.89448837e-01, 9.89237484e-01, 9.89040193e-01, + 9.88855636e-01, 9.88684347e-01, 9.88524761e-01, 9.88377852e-01, + 9.88242327e-01, 9.88118564e-01, 9.88005163e-01, 9.87903202e-01, + 9.87811174e-01, 9.87729546e-01, 9.87657198e-01, 9.87594984e-01, + 9.87541274e-01, 9.87496906e-01, 9.87460625e-01, 9.87432981e-01, + 9.87412641e-01, 9.87400475e-01, 9.87394992e-01, 9.87396916e-01, + 9.87404906e-01, 9.87419705e-01, 9.87439972e-01, 9.87466328e-01, + 9.87497321e-01, 9.87533893e-01, 9.87574654e-01, 9.87620124e-01, + 9.87668980e-01, 9.87722156e-01, 9.87778192e-01, 9.87837649e-01, + 9.87899199e-01, 9.87963798e-01, 9.88030030e-01, 9.88098468e-01, + 9.88167801e-01, 9.88239030e-01, 9.88310769e-01, 9.88383520e-01, + 9.88456016e-01, 9.88529420e-01, 9.88602222e-01, 9.88674940e-01, + 9.88746626e-01, 9.88818277e-01, 9.88888248e-01, 9.88957438e-01, + 9.89024798e-01, 9.89091125e-01, 9.89155170e-01, 9.89217866e-01, + 9.89277956e-01, 9.89336519e-01, 9.89392368e-01, 9.89446283e-01, + 9.89497212e-01, 9.89546334e-01, 9.89592362e-01, 9.89636265e-01, + 9.89677201e-01, 9.89716220e-01, 9.89752029e-01, 9.89785920e-01, + 9.89817027e-01, 9.89846207e-01, 9.89872536e-01, 9.89897514e-01, + 9.89920005e-01, 9.89941079e-01, 9.89960061e-01, 9.89978226e-01, + 9.89994556e-01, 9.90010350e-01, 9.90024832e-01, 9.90039402e-01, + 9.90053211e-01, 9.90067475e-01, 9.90081472e-01, 9.90096693e-01, + 9.90112245e-01, 9.90129379e-01, 9.90147465e-01, 9.90168060e-01, + 9.90190227e-01, 9.90215190e-01, 9.90242442e-01, 9.90273445e-01, + 9.90307127e-01, 9.90344891e-01, 9.90386228e-01, 9.90432448e-01, + 9.90482565e-01, 9.90537983e-01, 9.90598060e-01, 9.90664037e-01, + 9.90734883e-01, 9.90812038e-01, 9.90894786e-01, 9.90984259e-01, + 9.91079525e-01, 9.91181924e-01, 9.91290512e-01, 9.91406471e-01, + 9.91528801e-01, 9.91658694e-01, 9.91795272e-01, 9.91939622e-01, + 9.92090615e-01, 9.92249503e-01, 9.92415240e-01, 9.92588721e-01, + 9.92768871e-01, 9.92956911e-01, 9.93151653e-01, 9.93353924e-01, + 9.93562689e-01, 9.93779087e-01, 9.94001643e-01, 9.94231202e-01, + 9.94466818e-01, 9.94709344e-01, 9.94957285e-01, 9.95211663e-01, + 9.95471264e-01, 9.95736795e-01, 9.96006862e-01, 9.96282303e-01, + 9.96561799e-01, 9.96846133e-01, 9.97133827e-01, 9.97425669e-01, + 9.97720337e-01, 9.98018509e-01, 9.98318587e-01, 9.98621352e-01, + 9.98925543e-01, 9.99231731e-01, 9.99538258e-01, 9.99846116e-01, + 1.00015391e+00, 1.00046196e+00, 1.00076886e+00, 1.00107561e+00, + 1.00138055e+00, 1.00168424e+00, 1.00198543e+00, 1.00228487e+00, + 1.00258098e+00, 1.00287441e+00, 1.00316385e+00, 1.00345006e+00, + 1.00373157e+00, 1.00400915e+00, 1.00428146e+00, 1.00454934e+00, + 1.00481138e+00, 1.00506827e+00, 1.00531880e+00, 1.00556397e+00, + 1.00580227e+00, 1.00603455e+00, 1.00625986e+00, 1.00647902e+00, + 1.00669054e+00, 1.00689557e+00, 1.00709305e+00, 1.00728380e+00, + 1.00746662e+00, 1.00764273e+00, 1.00781104e+00, 1.00797244e+00, + 1.00812588e+00, 1.00827260e+00, 1.00841147e+00, 1.00854357e+00, + 1.00866802e+00, 1.00878601e+00, 1.00889653e+00, 1.00900077e+00, + 1.00909776e+00, 1.00918888e+00, 1.00927316e+00, 1.00935176e+00, + 1.00942394e+00, 1.00949118e+00, 1.00955240e+00, 1.00960889e+00, + 1.00965997e+00, 1.00970709e+00, 1.00974924e+00, 1.00978774e+00, + 1.00982209e+00, 1.00985371e+00, 1.00988150e+00, 1.00990696e+00, + 1.00992957e+00, 1.00995057e+00, 1.00996902e+00, 1.00998650e+00, + 1.01000236e+00, 1.01001789e+00, 1.01003217e+00, 1.01004672e+00, + 1.01006081e+00, 1.01007567e+00, 1.01009045e+00, 1.01010656e+00, + 1.01012323e+00, 1.01014176e+00, 1.01016113e+00, 1.01018264e+00, + 1.01020559e+00, 1.01023108e+00, 1.01025795e+00, 1.01028773e+00, + 1.01031948e+00, 1.01035408e+00, 1.01039064e+00, 1.01043047e+00, + 1.01047227e+00, 1.01051710e+00, 1.01056410e+00, 1.01061427e+00, + 1.01066629e+00, 1.01072136e+00, 1.01077842e+00, 1.01083825e+00, + 1.01089966e+00, 1.01096373e+00, 1.01102919e+00, 1.01109699e+00, + 1.01116586e+00, 1.01123661e+00, 1.01130817e+00, 1.01138145e+00, + 1.01145479e+00, 1.01152919e+00, 1.01160368e+00, 1.01167880e+00, + 1.01175301e+00, 1.01182748e+00, 1.01190094e+00, 1.01197388e+00, + 1.01204489e+00, 1.01211499e+00, 1.01218284e+00, 1.01224902e+00, + 1.01231210e+00, 1.01237303e+00, 1.01243046e+00, 1.01248497e+00, + 1.01253506e+00, 1.01258168e+00, 1.01262347e+00, 1.01266098e+00, + 1.01269276e+00, 1.01271979e+00, 1.01274058e+00, 1.01275575e+00, + 1.01276395e+00, 1.01276592e+00, 1.01276030e+00, 1.01274782e+00, + 1.01272696e+00, 1.01269861e+00, 1.01266140e+00, 1.01261590e+00, + 1.01256083e+00, 1.01249705e+00, 1.01242289e+00, 1.01233923e+00, + 1.01224492e+00, 1.01214046e+00, 1.01202430e+00, 1.01189756e+00, + 1.01175881e+00, 1.01160845e+00, 1.01144516e+00, 1.01126996e+00, + 1.01108126e+00, 1.01087961e+00, 1.01066368e+00, 1.01043418e+00, + 1.01018968e+00, 1.00993075e+00, 1.00965566e+00, 1.00936525e+00, + 1.00905825e+00, 1.00873476e+00, 1.00839308e+00, 1.00803431e+00, + 1.00765666e+00, 1.00726014e+00, 1.00684335e+00, 1.00640701e+00, + 1.00594915e+00, 1.00547001e+00, 1.00496799e+00, 1.00444353e+00, + 1.00389477e+00, 1.00332190e+00, 1.00272313e+00, 1.00209885e+00, + 1.00144728e+00, 1.00076851e+00, 1.00006069e+00, 9.99324268e-01, + 9.98557350e-01, 9.97760020e-01, 9.96930604e-01, 9.96069427e-01, + 9.95174643e-01, 9.94246644e-01, 9.93283713e-01, 9.92286108e-01, + 9.91252309e-01, 9.90182742e-01, 9.89075787e-01, 9.87931302e-01, + 9.86355322e-01, 9.84736245e-01, 9.83175095e-01, 9.81558334e-01, + 9.79861353e-01, 9.78061749e-01, 9.76157432e-01, 9.74137862e-01, + 9.71999011e-01, 9.69732741e-01, 9.67333198e-01, 9.64791512e-01, + 9.62101150e-01, 9.59253976e-01, 9.56242718e-01, 9.53060091e-01, + 9.49698408e-01, 9.46149812e-01, 9.42407161e-01, 9.38463416e-01, + 9.34311297e-01, 9.29944987e-01, 9.25356797e-01, 9.20540463e-01, + 9.15489628e-01, 9.10198679e-01, 9.04662060e-01, 8.98875519e-01, + 8.92833832e-01, 8.86533719e-01, 8.79971272e-01, 8.73143784e-01, + 8.66047653e-01, 8.58681252e-01, 8.51042044e-01, 8.43129723e-01, + 8.34943514e-01, 8.26483991e-01, 8.17750537e-01, 8.08744982e-01, + 7.99468149e-01, 7.89923516e-01, 7.80113773e-01, 7.70043128e-01, + 7.59714574e-01, 7.49133097e-01, 7.38302860e-01, 7.27229876e-01, + 7.15920192e-01, 7.04381434e-01, 6.92619693e-01, 6.80643883e-01, + 6.68461648e-01, 6.56083014e-01, 6.43517927e-01, 6.30775533e-01, + 6.17864165e-01, 6.04795463e-01, 5.91579959e-01, 5.78228937e-01, + 5.64753589e-01, 5.51170316e-01, 5.37490509e-01, 5.23726350e-01, + 5.09891542e-01, 4.96000807e-01, 4.82066294e-01, 4.68101711e-01, + 4.54121700e-01, 4.40142182e-01, 4.26177297e-01, 4.12241789e-01, + 3.98349961e-01, 3.84517234e-01, 3.70758372e-01, 3.57088679e-01, + 3.43522867e-01, 3.30076376e-01, 3.16764033e-01, 3.03600465e-01, + 2.90599616e-01, 2.77775850e-01, 2.65143468e-01, 2.52716188e-01, + 2.40506985e-01, 2.28528397e-01, 2.16793343e-01, 2.05313990e-01, + 1.94102191e-01, 1.83168087e-01, 1.72522195e-01, 1.62173542e-01, + 1.52132068e-01, 1.42405280e-01, 1.33001524e-01, 1.23926066e-01, + 1.15185830e-01, 1.06784043e-01, 9.87263751e-02, 9.10137900e-02, + 8.36505724e-02, 7.66350831e-02, 6.99703341e-02, 6.36518811e-02, + 5.76817602e-02, 5.20524422e-02, 4.67653841e-02, 4.18095054e-02, + 3.71864025e-02, 3.28807275e-02, 2.88954850e-02, 2.52098057e-02, + 2.18305756e-02, 1.87289619e-02, 1.59212782e-02, 1.33638143e-02, + 1.10855888e-02, 8.94347419e-03, 6.75812489e-03, 3.50443813e-03, +}; + +#if LC3_PLUS_HR + +static const float mdct_win_10m_48k_hr[__LC3_MDCT_WIN_LEN(10000, 48000)] = { + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 9.42341174e-08, 6.19838374e-07, 1.82603810e-06, 4.22741550e-06, + 8.56822135e-06, 1.59013834e-05, 2.76738483e-05, 4.58246141e-05, + 7.28956657e-05, 1.12155336e-04, 1.67733029e-04, 2.44763592e-04, + 3.49539070e-04, 4.89664846e-04, 6.74216484e-04, 9.13893222e-04, + 1.22116262e-03, 1.61039189e-03, 2.09795963e-03, 2.70234118e-03, + 3.44416290e-03, 4.34621749e-03, 5.43343695e-03, 6.73281262e-03, + 8.27326626e-03, 1.00854570e-02, 1.22015327e-02, 1.46548180e-02, + 1.74794346e-02, 2.07098722e-02, 2.43804958e-02, 2.85249949e-02, + 3.31758074e-02, 3.83635014e-02, 4.41161096e-02, 5.04585020e-02, + 5.74117042e-02, 6.49922863e-02, 7.32117295e-02, 8.20759088e-02, + 9.15845558e-02, 1.01730898e-01, 1.12501279e-01, 1.23875007e-01, + 1.35824218e-01, 1.48313895e-01, 1.61302090e-01, 1.74740151e-01, + 1.88573152e-01, 2.02740535e-01, 2.17176691e-01, 2.31811777e-01, + 2.46572644e-01, 2.61383832e-01, 2.76168495e-01, 2.90849626e-01, + 3.05351138e-01, 3.19598824e-01, 3.33521664e-01, 3.47052664e-01, + 3.60129982e-01, 3.72697920e-01, 3.84707332e-01, 3.96116525e-01, + 4.06891733e-01, 4.17007536e-01, 4.26446915e-01, 4.35201406e-01, + 4.43271041e-01, 4.50664014e-01, 4.57396388e-01, 4.63491529e-01, + 4.68979478e-01, 4.73896384e-01, 4.78283674e-01, 4.82187212e-01, + 4.85656589e-01, 4.88744229e-01, 4.91504699e-01, 4.93993789e-01, + 4.96267974e-01, 4.98383760e-01, 5.00396967e-01, 5.02362430e-01, + 5.04333496e-01, 5.06361604e-01, 5.08496106e-01, 5.10783911e-01, + 5.13269365e-01, 5.15994072e-01, 5.18996596e-01, 5.22312462e-01, + 5.25973916e-01, 5.30009925e-01, 5.34445822e-01, 5.39303243e-01, + 5.44600070e-01, 5.50350249e-01, 5.56563497e-01, 5.63245535e-01, + 5.70397854e-01, 5.78017771e-01, 5.86098313e-01, 5.94628513e-01, + 6.03593290e-01, 6.12973869e-01, 6.22747838e-01, 6.32889450e-01, + 6.43370092e-01, 6.54158235e-01, 6.65220201e-01, 6.76520288e-01, + 6.88021243e-01, 6.99684739e-01, 7.11471498e-01, 7.23342001e-01, + 7.35256732e-01, 7.47176409e-01, 7.59062469e-01, 7.70877421e-01, + 7.82584906e-01, 7.94150114e-01, 8.05540025e-01, 8.16723466e-01, + 8.27671409e-01, 8.38356972e-01, 8.48755658e-01, 8.58845115e-01, + 8.68605733e-01, 8.78019989e-01, 8.87072980e-01, 8.95752132e-01, + 9.04047191e-01, 9.11950290e-01, 9.19455826e-01, 9.26560223e-01, + 9.33262229e-01, 9.39562619e-01, 9.45464134e-01, 9.50971425e-01, + 9.56090987e-01, 9.60831106e-01, 9.65201676e-01, 9.69214201e-01, + 9.72881556e-01, 9.76217866e-01, 9.79238510e-01, 9.81959701e-01, + 9.84398544e-01, 9.86572623e-01, 9.88499999e-01, 9.90198970e-01, + 9.91687655e-01, 9.92984235e-01, 9.94106293e-01, 9.95071113e-01, + 9.95895147e-01, 9.96594131e-01, 9.97182965e-01, 9.97675478e-01, + 9.98084545e-01, 9.98421967e-01, 9.98698533e-01, 9.98923838e-01, + 9.99106586e-01, 9.99254227e-01, 9.99373496e-01, 9.99470055e-01, + 9.99548733e-01, 9.99613643e-01, 9.99668002e-01, 9.99714673e-01, + 9.99755740e-01, 9.99792874e-01, 9.99827385e-01, 9.99860168e-01, + 9.99891937e-01, 9.99923110e-01, 9.99953985e-01, 9.99984682e-01, + 1.00001526e+00, 1.00004578e+00, 1.00007617e+00, 1.00010622e+00, + 1.00013602e+00, 1.00016546e+00, 1.00019431e+00, 1.00022256e+00, + 1.00025010e+00, 1.00027692e+00, 1.00030291e+00, 1.00032794e+00, + 1.00035203e+00, 1.00037491e+00, 1.00039685e+00, 1.00041747e+00, + 1.00043690e+00, 1.00045502e+00, 1.00047183e+00, 1.00048721e+00, + 1.00050116e+00, 1.00051367e+00, 1.00052476e+00, 1.00053418e+00, + 1.00054228e+00, 1.00054872e+00, 1.00055361e+00, 1.00055695e+00, + 1.00055885e+00, 1.00055909e+00, 1.00055790e+00, 1.00055504e+00, + 1.00055087e+00, 1.00054502e+00, 1.00053787e+00, 1.00052929e+00, + 1.00051928e+00, 1.00050783e+00, 1.00049520e+00, 1.00048113e+00, + 1.00046599e+00, 1.00044954e+00, 1.00043201e+00, 1.00041330e+00, + 1.00039363e+00, 1.00037301e+00, 1.00035143e+00, 1.00032890e+00, + 1.00030565e+00, 1.00028157e+00, 1.00025690e+00, 1.00023150e+00, + 1.00020564e+00, 1.00017929e+00, 1.00015235e+00, 1.00012517e+00, + 1.00009763e+00, 1.00006998e+00, 1.00004208e+00, 1.00001407e+00, + 9.99985993e-01, 9.99957979e-01, 9.99930084e-01, 9.99902308e-01, + 9.99874830e-01, 9.99847591e-01, 9.99820769e-01, 9.99794364e-01, + 9.99768496e-01, 9.99743164e-01, 9.99718428e-01, 9.99694467e-01, + 9.99671161e-01, 9.99648750e-01, 9.99627173e-01, 9.99606490e-01, + 9.99586821e-01, 9.99568224e-01, 9.99550641e-01, 9.99534249e-01, + 9.99519050e-01, 9.99505103e-01, 9.99492407e-01, 9.99481022e-01, + 9.99471009e-01, 9.99462426e-01, 9.99455214e-01, 9.99449492e-01, + 9.99445200e-01, 9.99442458e-01, 9.99441206e-01, 9.99441504e-01, + 9.99443293e-01, 9.99446690e-01, 9.99451578e-01, 9.99458075e-01, + 9.99466062e-01, 9.99475598e-01, 9.99486566e-01, 9.99499083e-01, + 9.99513030e-01, 9.99528408e-01, 9.99545157e-01, 9.99563277e-01, + 9.99582708e-01, 9.99603331e-01, 9.99625206e-01, 9.99648154e-01, + 9.99672174e-01, 9.99697208e-01, 9.99723136e-01, 9.99749959e-01, + 9.99777496e-01, 9.99805748e-01, 9.99834597e-01, 9.99863982e-01, + 9.99893785e-01, 9.99923885e-01, 9.99954224e-01, 9.99984741e-01, + 1.00001526e+00, 1.00004578e+00, 1.00007617e+00, 1.00010622e+00, + 1.00013602e+00, 1.00016546e+00, 1.00019431e+00, 1.00022256e+00, + 1.00025010e+00, 1.00027692e+00, 1.00030291e+00, 1.00032794e+00, + 1.00035203e+00, 1.00037491e+00, 1.00039685e+00, 1.00041747e+00, + 1.00043690e+00, 1.00045502e+00, 1.00047183e+00, 1.00048721e+00, + 1.00050116e+00, 1.00051367e+00, 1.00052476e+00, 1.00053418e+00, + 1.00054228e+00, 1.00054872e+00, 1.00055361e+00, 1.00055695e+00, + 1.00055885e+00, 1.00055909e+00, 1.00055790e+00, 1.00055504e+00, + 1.00055087e+00, 1.00054502e+00, 1.00053787e+00, 1.00052929e+00, + 1.00051928e+00, 1.00050783e+00, 1.00049520e+00, 1.00048113e+00, + 1.00046599e+00, 1.00044954e+00, 1.00043201e+00, 1.00041330e+00, + 1.00039363e+00, 1.00037301e+00, 1.00035143e+00, 1.00032890e+00, + 1.00030565e+00, 1.00028157e+00, 1.00025690e+00, 1.00023150e+00, + 1.00020564e+00, 1.00017929e+00, 1.00015235e+00, 1.00012517e+00, + 1.00009763e+00, 1.00006998e+00, 1.00004208e+00, 1.00001407e+00, + 9.99985993e-01, 9.99957979e-01, 9.99930084e-01, 9.99902308e-01, + 9.99874830e-01, 9.99847591e-01, 9.99820769e-01, 9.99794364e-01, + 9.99768496e-01, 9.99743164e-01, 9.99718428e-01, 9.99694467e-01, + 9.99671161e-01, 9.99648750e-01, 9.99627173e-01, 9.99606490e-01, + 9.99586821e-01, 9.99568224e-01, 9.99550641e-01, 9.99534249e-01, + 9.99519050e-01, 9.99505103e-01, 9.99492407e-01, 9.99481022e-01, + 9.99471009e-01, 9.99462426e-01, 9.99455214e-01, 9.99449492e-01, + 9.99445200e-01, 9.99442458e-01, 9.99441206e-01, 9.99441504e-01, + 9.99443293e-01, 9.99446690e-01, 9.99451578e-01, 9.99458075e-01, + 9.99466062e-01, 9.99475598e-01, 9.99486566e-01, 9.99499083e-01, + 9.99513030e-01, 9.99528408e-01, 9.99545157e-01, 9.99563277e-01, + 9.99582708e-01, 9.99603331e-01, 9.99625206e-01, 9.99648154e-01, + 9.99672174e-01, 9.99697208e-01, 9.99723136e-01, 9.99749959e-01, + 9.99777496e-01, 9.99805748e-01, 9.99834597e-01, 9.99863982e-01, + 9.99893785e-01, 9.99923885e-01, 9.99954224e-01, 9.99984741e-01, + 1.00001526e+00, 1.00004578e+00, 1.00007617e+00, 1.00010622e+00, + 1.00013602e+00, 1.00016546e+00, 1.00019431e+00, 1.00022256e+00, + 1.00025010e+00, 1.00027692e+00, 1.00030291e+00, 1.00032794e+00, + 1.00035203e+00, 1.00037491e+00, 1.00039685e+00, 1.00041747e+00, + 1.00043690e+00, 1.00045502e+00, 1.00047183e+00, 1.00048721e+00, + 1.00050116e+00, 1.00051367e+00, 1.00052476e+00, 1.00053418e+00, + 1.00054228e+00, 1.00054872e+00, 1.00055361e+00, 1.00055695e+00, + 1.00055885e+00, 1.00055909e+00, 1.00055790e+00, 1.00055504e+00, + 1.00055087e+00, 1.00054502e+00, 1.00053787e+00, 1.00052929e+00, + 1.00051928e+00, 1.00050783e+00, 1.00049520e+00, 1.00048113e+00, + 1.00046599e+00, 1.00044954e+00, 1.00043201e+00, 1.00041330e+00, + 1.00039363e+00, 1.00037301e+00, 1.00035143e+00, 1.00032890e+00, + 1.00030565e+00, 1.00028157e+00, 1.00025690e+00, 1.00023150e+00, + 1.00020564e+00, 1.00017929e+00, 1.00015235e+00, 1.00012517e+00, + 1.00009763e+00, 1.00006998e+00, 1.00004208e+00, 1.00001407e+00, + 9.99985993e-01, 9.99957979e-01, 9.99930084e-01, 9.99902308e-01, + 9.99874830e-01, 9.99847591e-01, 9.99820769e-01, 9.99794364e-01, + 9.99768496e-01, 9.99743164e-01, 9.99718428e-01, 9.99694467e-01, + 9.99671161e-01, 9.99648750e-01, 9.99627173e-01, 9.99606490e-01, + 9.99586821e-01, 9.99568224e-01, 9.99550641e-01, 9.99534249e-01, + 9.99519050e-01, 9.99505103e-01, 9.99492407e-01, 9.99481022e-01, + 9.99471009e-01, 9.99462426e-01, 9.99455214e-01, 9.99449492e-01, + 9.99445200e-01, 9.99442458e-01, 9.99441206e-01, 9.99441504e-01, + 9.99443293e-01, 9.99446690e-01, 9.99451578e-01, 9.99458075e-01, + 9.99466062e-01, 9.99475598e-01, 9.99486566e-01, 9.99499083e-01, + 9.99513030e-01, 9.99528408e-01, 9.99545157e-01, 9.99563277e-01, + 9.99582708e-01, 9.99603331e-01, 9.99625206e-01, 9.99648154e-01, + 9.99672174e-01, 9.99697208e-01, 9.99723136e-01, 9.99749959e-01, + 9.99777496e-01, 9.99805748e-01, 9.99834597e-01, 9.99863982e-01, + 9.99893785e-01, 9.99923885e-01, 9.99954224e-01, 9.99984741e-01, + 1.00001526e+00, 1.00004554e+00, 1.00007534e+00, 1.00010443e+00, + 1.00013220e+00, 1.00015819e+00, 1.00018132e+00, 1.00020075e+00, + 1.00021482e+00, 1.00022173e+00, 1.00021923e+00, 1.00020432e+00, + 1.00017369e+00, 1.00012279e+00, 1.00004685e+00, 9.99939978e-01, + 9.99795198e-01, 9.99604583e-01, 9.99359250e-01, 9.99048889e-01, + 9.98662114e-01, 9.98186171e-01, 9.97606814e-01, 9.96908367e-01, + 9.96073723e-01, 9.95083988e-01, 9.93918717e-01, 9.92555678e-01, + 9.90970671e-01, 9.89137888e-01, 9.87029374e-01, 9.84615326e-01, + 9.81863916e-01, 9.78741586e-01, 9.75212753e-01, 9.71240282e-01, + 9.66785491e-01, 9.61808383e-01, 9.56268132e-01, 9.50123310e-01, + 9.43332374e-01, 9.35854316e-01, 9.27648962e-01, 9.18677926e-01, + 9.08904910e-01, 8.98296535e-01, 8.86823177e-01, 8.74459147e-01, + 8.61183822e-01, 8.46981943e-01, 8.31844091e-01, 8.15767467e-01, + 7.98755884e-01, 7.80820429e-01, 7.61979520e-01, 7.42259145e-01, + 7.21692860e-01, 7.00321794e-01, 6.78194642e-01, 6.55367255e-01, + 6.31902635e-01, 6.07870460e-01, 5.83346546e-01, 5.58412433e-01, + 5.33154905e-01, 5.07664979e-01, 4.82037485e-01, 4.56370175e-01, + 4.30762708e-01, 4.05315757e-01, 3.80130053e-01, 3.55305195e-01, + 3.30938727e-01, 3.07124883e-01, 2.83953428e-01, 2.61508703e-01, + 2.39868388e-01, 2.19102606e-01, 1.99272946e-01, 1.80431694e-01, + 1.62621215e-01, 1.45873442e-01, 1.30209655e-01, 1.15640387e-01, + 1.02165572e-01, 8.97749513e-02, 7.84486011e-02, 6.81576878e-02, + 5.88653944e-02, 5.05279638e-02, 4.30957973e-02, 3.65146622e-02, + 3.07268873e-02, 2.56725382e-02, 2.12905537e-02, 1.75197981e-02, + 1.43000064e-02, 1.15726292e-02, 9.28153656e-03, 7.37359654e-03, + 5.79912262e-03, 4.51218896e-03, 3.47083295e-03, 2.63714185e-03, + 1.97724649e-03, 1.46123092e-03, 1.06297329e-03, 7.59930001e-04, + 5.32880833e-04, 3.65644053e-04, 2.44775380e-04, 1.59260671e-04, + 1.00211051e-04, 6.05685127e-05, 3.48275607e-05, 1.87775731e-05, + 9.26902067e-06, 4.00523413e-06, 1.35989160e-06, 2.06769442e-07, +}; + +static const float mdct_win_10m_96k_hr[__LC3_MDCT_WIN_LEN(10000, 96000)] = { + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, + 6.66310811e-08, 2.23723916e-07, 4.87541854e-07, 8.99672727e-07, + 1.51141830e-06, 2.38590815e-06, 3.60002150e-06, 5.24645884e-06, + 7.43599958e-06, 1.02999529e-05, 1.39928125e-05, 1.86951092e-05, + 2.46164800e-05, 3.19989194e-05, 4.11202636e-05, 5.22978444e-05, + 6.58923600e-05, 8.23118899e-05, 1.02016144e-04, 1.25520819e-04, + 1.53402099e-04, 1.86301360e-04, 2.24929841e-04, 2.70073535e-04, + 3.22598062e-04, 3.83453589e-04, 4.53679706e-04, 5.34410414e-04, + 6.26878755e-04, 7.32421642e-04, 8.52484489e-04, 9.88625223e-04, + 1.14251883e-03, 1.31596089e-03, 1.51087111e-03, 1.72929652e-03, + 1.97341433e-03, 2.24553375e-03, 2.54809810e-03, 2.88368552e-03, + 3.25501012e-03, 3.66492104e-03, 4.11640201e-03, 4.61257016e-03, + 5.15667303e-03, 5.75208431e-03, 6.40230207e-03, 7.11094262e-03, + 7.88173359e-03, 8.71850923e-03, 9.62519925e-03, 1.06058242e-02, + 1.16644828e-02, 1.28053408e-02, 1.40326228e-02, 1.53505951e-02, + 1.67635549e-02, 1.82758160e-02, 1.98916886e-02, 2.16154736e-02, + 2.34514344e-02, 2.54037846e-02, 2.74766665e-02, 2.96741407e-02, + 3.20001543e-02, 3.44585292e-02, 3.70529443e-02, 3.97869013e-02, + 4.26637232e-02, 4.56865206e-02, 4.88581695e-02, 5.21813035e-02, + 5.56582808e-02, 5.92911765e-02, 6.30817562e-02, 6.70314580e-02, + 7.11413696e-02, 7.54122287e-02, 7.98443928e-02, 8.44378322e-02, + 8.91921073e-02, 9.41063836e-02, 9.91793722e-02, 1.04409374e-01, + 1.09794252e-01, 1.15331404e-01, 1.21017799e-01, 1.26849949e-01, + 1.32823929e-01, 1.38935357e-01, 1.45179421e-01, 1.51550874e-01, + 1.58044025e-01, 1.64652810e-01, 1.71370730e-01, 1.78190947e-01, + 1.85106188e-01, 1.92108899e-01, 1.99191183e-01, 2.06344813e-01, + 2.13561311e-01, 2.20831960e-01, 2.28147790e-01, 2.35499650e-01, + 2.42878228e-01, 2.50274092e-01, 2.57677704e-01, 2.65079439e-01, + 2.72469670e-01, 2.79838771e-01, 2.87177145e-01, 2.94475257e-01, + 3.01723719e-01, 3.08913231e-01, 3.16034675e-01, 3.23079228e-01, + 3.30038190e-01, 3.36903185e-01, 3.43666196e-01, 3.50319386e-01, + 3.56855512e-01, 3.63267571e-01, 3.69548947e-01, 3.75693500e-01, + 3.81695598e-01, 3.87549996e-01, 3.93251985e-01, 3.98797333e-01, + 4.04182315e-01, 4.09403801e-01, 4.14459109e-01, 4.19346124e-01, + 4.24063236e-01, 4.28609401e-01, 4.32984143e-01, 4.37187403e-01, + 4.41219747e-01, 4.45082188e-01, 4.48776275e-01, 4.52303976e-01, + 4.55667824e-01, 4.58870709e-01, 4.61916000e-01, 4.64807451e-01, + 4.67549264e-01, 4.70145911e-01, 4.72602278e-01, 4.74923581e-01, + 4.77115244e-01, 4.79183048e-01, 4.81132984e-01, 4.82971221e-01, + 4.84704226e-01, 4.86338496e-01, 4.87880766e-01, 4.89337832e-01, + 4.90716666e-01, 4.92024213e-01, 4.93267536e-01, 4.94453669e-01, + 4.95589703e-01, 4.96682733e-01, 4.97739762e-01, 4.98767793e-01, + 4.99773741e-01, 5.00764489e-01, 5.01746774e-01, 5.02727270e-01, + 5.03712595e-01, 5.04709125e-01, 5.05723178e-01, 5.06760955e-01, + 5.07828474e-01, 5.08931518e-01, 5.10075927e-01, 5.11267185e-01, + 5.12510598e-01, 5.13811469e-01, 5.15174806e-01, 5.16605377e-01, + 5.18107831e-01, 5.19686580e-01, 5.21345973e-01, 5.23089945e-01, + 5.24922311e-01, 5.26846766e-01, 5.28866649e-01, 5.30985177e-01, + 5.33205211e-01, 5.35529494e-01, 5.37960529e-01, 5.40500462e-01, + 5.43151379e-01, 5.45914948e-01, 5.48792660e-01, 5.51785827e-01, + 5.54895282e-01, 5.58121800e-01, 5.61465800e-01, 5.64927518e-01, + 5.68506777e-01, 5.72203338e-01, 5.76016545e-01, 5.79945564e-01, + 5.83989203e-01, 5.88146091e-01, 5.92414677e-01, 5.96792936e-01, + 6.01278901e-01, 6.05870068e-01, 6.10563993e-01, 6.15357757e-01, + 6.20248437e-01, 6.25232756e-01, 6.30307317e-01, 6.35468543e-01, + 6.40712619e-01, 6.46035612e-01, 6.51433527e-01, 6.56902015e-01, + 6.62436843e-01, 6.68033481e-01, 6.73687398e-01, 6.79393888e-01, + 6.85148239e-01, 6.90945625e-01, 6.96781278e-01, 7.02650130e-01, + 7.08547413e-01, 7.14468122e-01, 7.20407307e-01, 7.26359963e-01, + 7.32321203e-01, 7.38286138e-01, 7.44249880e-01, 7.50207603e-01, + 7.56154597e-01, 7.62086034e-01, 7.67997444e-01, 7.73884177e-01, + 7.79741824e-01, 7.85566032e-01, 7.91352570e-01, 7.97097266e-01, + 8.02796185e-01, 8.08445334e-01, 8.14041018e-01, 8.19579542e-01, + 8.25057447e-01, 8.30471396e-01, 8.35818112e-01, 8.41094613e-01, + 8.46297920e-01, 8.51425231e-01, 8.56473923e-01, 8.61441612e-01, + 8.66325855e-01, 8.71124566e-01, 8.75835657e-01, 8.80457282e-01, + 8.84987772e-01, 8.89425457e-01, 8.93768966e-01, 8.98017049e-01, + 9.02168512e-01, 9.06222403e-01, 9.10177886e-01, 9.14034188e-01, + 9.17790771e-01, 9.21447217e-01, 9.25003231e-01, 9.28458691e-01, + 9.31813419e-01, 9.35067594e-01, 9.38221455e-01, 9.41275299e-01, + 9.44229603e-01, 9.47084904e-01, 9.49841976e-01, 9.52501595e-01, + 9.55064654e-01, 9.57532167e-01, 9.59905326e-01, 9.62185323e-01, + 9.64373529e-01, 9.66471374e-01, 9.68480289e-01, 9.70402002e-01, + 9.72238123e-01, 9.73990440e-01, 9.75660801e-01, 9.77251112e-01, + 9.78763342e-01, 9.80199575e-01, 9.81561780e-01, 9.82852161e-01, + 9.84072864e-01, 9.85226095e-01, 9.86314118e-01, 9.87339139e-01, + 9.88303483e-01, 9.89209354e-01, 9.90059078e-01, 9.90854919e-01, + 9.91599143e-01, 9.92294014e-01, 9.92941797e-01, 9.93544638e-01, + 9.94104803e-01, 9.94624376e-01, 9.95105505e-01, 9.95550215e-01, + 9.95960534e-01, 9.96338427e-01, 9.96685863e-01, 9.97004628e-01, + 9.97296572e-01, 9.97563362e-01, 9.97806728e-01, 9.98028338e-01, + 9.98229563e-01, 9.98412073e-01, 9.98577118e-01, 9.98726189e-01, + 9.98860478e-01, 9.98981178e-01, 9.99089479e-01, 9.99186397e-01, + 9.99273062e-01, 9.99350369e-01, 9.99419153e-01, 9.99480307e-01, + 9.99534547e-01, 9.99582708e-01, 9.99625325e-01, 9.99663055e-01, + 9.99696434e-01, 9.99726057e-01, 9.99752283e-01, 9.99775589e-01, + 9.99796331e-01, 9.99814928e-01, 9.99831557e-01, 9.99846578e-01, + 9.99860287e-01, 9.99872804e-01, 9.99884307e-01, 9.99895036e-01, + 9.99905109e-01, 9.99914587e-01, 9.99923646e-01, 9.99932408e-01, + 9.99940872e-01, 9.99949098e-01, 9.99957144e-01, 9.99965072e-01, + 9.99972939e-01, 9.99980748e-01, 9.99988437e-01, 9.99996126e-01, + 1.00000381e+00, 1.00001144e+00, 1.00001907e+00, 1.00002670e+00, + 1.00003433e+00, 1.00004184e+00, 1.00004935e+00, 1.00005686e+00, + 1.00006437e+00, 1.00007176e+00, 1.00007904e+00, 1.00008631e+00, + 1.00009358e+00, 1.00010073e+00, 1.00010777e+00, 1.00011480e+00, + 1.00012159e+00, 1.00012839e+00, 1.00013518e+00, 1.00014174e+00, + 1.00014830e+00, 1.00015461e+00, 1.00016093e+00, 1.00016701e+00, + 1.00017309e+00, 1.00017893e+00, 1.00018466e+00, 1.00019026e+00, + 1.00019574e+00, 1.00020099e+00, 1.00020623e+00, 1.00021124e+00, + 1.00021613e+00, 1.00022078e+00, 1.00022531e+00, 1.00022972e+00, + 1.00023389e+00, 1.00023794e+00, 1.00024176e+00, 1.00024545e+00, + 1.00024891e+00, 1.00025225e+00, 1.00025535e+00, 1.00025833e+00, + 1.00026107e+00, 1.00026357e+00, 1.00026596e+00, 1.00026822e+00, + 1.00027013e+00, 1.00027204e+00, 1.00027359e+00, 1.00027502e+00, + 1.00027621e+00, 1.00027728e+00, 1.00027812e+00, 1.00027883e+00, + 1.00027919e+00, 1.00027955e+00, 1.00027955e+00, 1.00027943e+00, + 1.00027919e+00, 1.00027859e+00, 1.00027800e+00, 1.00027704e+00, + 1.00027597e+00, 1.00027478e+00, 1.00027335e+00, 1.00027168e+00, + 1.00026989e+00, 1.00026786e+00, 1.00026572e+00, 1.00026345e+00, + 1.00026095e+00, 1.00025821e+00, 1.00025535e+00, 1.00025237e+00, + 1.00024927e+00, 1.00024593e+00, 1.00024235e+00, 1.00023878e+00, + 1.00023496e+00, 1.00023091e+00, 1.00022686e+00, 1.00022256e+00, + 1.00021827e+00, 1.00021374e+00, 1.00020909e+00, 1.00020421e+00, + 1.00019932e+00, 1.00019431e+00, 1.00018907e+00, 1.00018382e+00, + 1.00017846e+00, 1.00017297e+00, 1.00016737e+00, 1.00016165e+00, + 1.00015581e+00, 1.00014985e+00, 1.00014389e+00, 1.00013781e+00, + 1.00013161e+00, 1.00012529e+00, 1.00011897e+00, 1.00011253e+00, + 1.00010610e+00, 1.00009954e+00, 1.00009298e+00, 1.00008631e+00, + 1.00007963e+00, 1.00007284e+00, 1.00006604e+00, 1.00005913e+00, + 1.00005233e+00, 1.00004542e+00, 1.00003850e+00, 1.00003147e+00, + 1.00002456e+00, 1.00001752e+00, 1.00001049e+00, 1.00000346e+00, + 9.99996483e-01, 9.99989510e-01, 9.99982476e-01, 9.99975502e-01, + 9.99968529e-01, 9.99961555e-01, 9.99954641e-01, 9.99947727e-01, + 9.99940813e-01, 9.99933958e-01, 9.99927163e-01, 9.99920428e-01, + 9.99913692e-01, 9.99907076e-01, 9.99900460e-01, 9.99893904e-01, + 9.99887466e-01, 9.99881029e-01, 9.99874711e-01, 9.99868453e-01, + 9.99862254e-01, 9.99856174e-01, 9.99850154e-01, 9.99844253e-01, + 9.99838412e-01, 9.99832690e-01, 9.99827087e-01, 9.99821603e-01, + 9.99816179e-01, 9.99810934e-01, 9.99805748e-01, 9.99800682e-01, + 9.99795794e-01, 9.99791026e-01, 9.99786317e-01, 9.99781847e-01, + 9.99777436e-01, 9.99773204e-01, 9.99769092e-01, 9.99765158e-01, + 9.99761343e-01, 9.99757707e-01, 9.99754190e-01, 9.99750853e-01, + 9.99747694e-01, 9.99744654e-01, 9.99741852e-01, 9.99739170e-01, + 9.99736667e-01, 9.99734342e-01, 9.99732137e-01, 9.99730170e-01, + 9.99728382e-01, 9.99726772e-01, 9.99725342e-01, 9.99724090e-01, + 9.99723017e-01, 9.99722123e-01, 9.99721408e-01, 9.99720931e-01, + 9.99720633e-01, 9.99720514e-01, 9.99720573e-01, 9.99720812e-01, + 9.99721289e-01, 9.99721944e-01, 9.99722779e-01, 9.99723792e-01, + 9.99725044e-01, 9.99726474e-01, 9.99728084e-01, 9.99729872e-01, + 9.99731898e-01, 9.99734104e-01, 9.99736488e-01, 9.99739051e-01, + 9.99741793e-01, 9.99744713e-01, 9.99747872e-01, 9.99751151e-01, + 9.99754667e-01, 9.99758303e-01, 9.99762177e-01, 9.99766171e-01, + 9.99770403e-01, 9.99774754e-01, 9.99779284e-01, 9.99783993e-01, + 9.99788821e-01, 9.99793828e-01, 9.99799013e-01, 9.99804318e-01, + 9.99809742e-01, 9.99815404e-01, 9.99821126e-01, 9.99826968e-01, + 9.99832988e-01, 9.99839127e-01, 9.99845386e-01, 9.99851763e-01, + 9.99858260e-01, 9.99864876e-01, 9.99871552e-01, 9.99878347e-01, + 9.99885261e-01, 9.99892235e-01, 9.99899328e-01, 9.99906480e-01, + 9.99913692e-01, 9.99920964e-01, 9.99928296e-01, 9.99935687e-01, + 9.99943137e-01, 9.99950647e-01, 9.99958158e-01, 9.99965727e-01, + 9.99973297e-01, 9.99980927e-01, 9.99988556e-01, 9.99996185e-01, + 1.00000381e+00, 1.00001144e+00, 1.00001907e+00, 1.00002670e+00, + 1.00003433e+00, 1.00004184e+00, 1.00004935e+00, 1.00005686e+00, + 1.00006437e+00, 1.00007176e+00, 1.00007904e+00, 1.00008631e+00, + 1.00009358e+00, 1.00010073e+00, 1.00010777e+00, 1.00011480e+00, + 1.00012159e+00, 1.00012839e+00, 1.00013518e+00, 1.00014174e+00, + 1.00014830e+00, 1.00015461e+00, 1.00016093e+00, 1.00016701e+00, + 1.00017309e+00, 1.00017893e+00, 1.00018466e+00, 1.00019026e+00, + 1.00019574e+00, 1.00020099e+00, 1.00020623e+00, 1.00021124e+00, + 1.00021613e+00, 1.00022078e+00, 1.00022531e+00, 1.00022972e+00, + 1.00023389e+00, 1.00023794e+00, 1.00024176e+00, 1.00024545e+00, + 1.00024891e+00, 1.00025225e+00, 1.00025535e+00, 1.00025833e+00, + 1.00026107e+00, 1.00026357e+00, 1.00026596e+00, 1.00026822e+00, + 1.00027013e+00, 1.00027204e+00, 1.00027359e+00, 1.00027502e+00, + 1.00027621e+00, 1.00027728e+00, 1.00027812e+00, 1.00027883e+00, + 1.00027919e+00, 1.00027955e+00, 1.00027955e+00, 1.00027943e+00, + 1.00027919e+00, 1.00027859e+00, 1.00027800e+00, 1.00027704e+00, + 1.00027597e+00, 1.00027478e+00, 1.00027335e+00, 1.00027168e+00, + 1.00026989e+00, 1.00026786e+00, 1.00026572e+00, 1.00026345e+00, + 1.00026095e+00, 1.00025821e+00, 1.00025535e+00, 1.00025237e+00, + 1.00024927e+00, 1.00024593e+00, 1.00024235e+00, 1.00023878e+00, + 1.00023496e+00, 1.00023091e+00, 1.00022686e+00, 1.00022256e+00, + 1.00021827e+00, 1.00021374e+00, 1.00020909e+00, 1.00020421e+00, + 1.00019932e+00, 1.00019431e+00, 1.00018907e+00, 1.00018382e+00, + 1.00017846e+00, 1.00017297e+00, 1.00016737e+00, 1.00016165e+00, + 1.00015581e+00, 1.00014985e+00, 1.00014389e+00, 1.00013781e+00, + 1.00013161e+00, 1.00012529e+00, 1.00011897e+00, 1.00011253e+00, + 1.00010610e+00, 1.00009954e+00, 1.00009298e+00, 1.00008631e+00, + 1.00007963e+00, 1.00007284e+00, 1.00006604e+00, 1.00005913e+00, + 1.00005233e+00, 1.00004542e+00, 1.00003850e+00, 1.00003147e+00, + 1.00002456e+00, 1.00001752e+00, 1.00001049e+00, 1.00000346e+00, + 9.99996483e-01, 9.99989510e-01, 9.99982476e-01, 9.99975502e-01, + 9.99968529e-01, 9.99961555e-01, 9.99954641e-01, 9.99947727e-01, + 9.99940813e-01, 9.99933958e-01, 9.99927163e-01, 9.99920428e-01, + 9.99913692e-01, 9.99907076e-01, 9.99900460e-01, 9.99893904e-01, + 9.99887466e-01, 9.99881029e-01, 9.99874711e-01, 9.99868453e-01, + 9.99862254e-01, 9.99856174e-01, 9.99850154e-01, 9.99844253e-01, + 9.99838412e-01, 9.99832690e-01, 9.99827087e-01, 9.99821603e-01, + 9.99816179e-01, 9.99810934e-01, 9.99805748e-01, 9.99800682e-01, + 9.99795794e-01, 9.99791026e-01, 9.99786317e-01, 9.99781847e-01, + 9.99777436e-01, 9.99773204e-01, 9.99769092e-01, 9.99765158e-01, + 9.99761343e-01, 9.99757707e-01, 9.99754190e-01, 9.99750853e-01, + 9.99747694e-01, 9.99744654e-01, 9.99741852e-01, 9.99739170e-01, + 9.99736667e-01, 9.99734342e-01, 9.99732137e-01, 9.99730170e-01, + 9.99728382e-01, 9.99726772e-01, 9.99725342e-01, 9.99724090e-01, + 9.99723017e-01, 9.99722123e-01, 9.99721408e-01, 9.99720931e-01, + 9.99720633e-01, 9.99720514e-01, 9.99720573e-01, 9.99720812e-01, + 9.99721289e-01, 9.99721944e-01, 9.99722779e-01, 9.99723792e-01, + 9.99725044e-01, 9.99726474e-01, 9.99728084e-01, 9.99729872e-01, + 9.99731898e-01, 9.99734104e-01, 9.99736488e-01, 9.99739051e-01, + 9.99741793e-01, 9.99744713e-01, 9.99747872e-01, 9.99751151e-01, + 9.99754667e-01, 9.99758303e-01, 9.99762177e-01, 9.99766171e-01, + 9.99770403e-01, 9.99774754e-01, 9.99779284e-01, 9.99783993e-01, + 9.99788821e-01, 9.99793828e-01, 9.99799013e-01, 9.99804318e-01, + 9.99809742e-01, 9.99815404e-01, 9.99821126e-01, 9.99826968e-01, + 9.99832988e-01, 9.99839127e-01, 9.99845386e-01, 9.99851763e-01, + 9.99858260e-01, 9.99864876e-01, 9.99871552e-01, 9.99878347e-01, + 9.99885261e-01, 9.99892235e-01, 9.99899328e-01, 9.99906480e-01, + 9.99913692e-01, 9.99920964e-01, 9.99928296e-01, 9.99935687e-01, + 9.99943137e-01, 9.99950647e-01, 9.99958158e-01, 9.99965727e-01, + 9.99973297e-01, 9.99980927e-01, 9.99988556e-01, 9.99996185e-01, + 1.00000381e+00, 1.00001144e+00, 1.00001907e+00, 1.00002670e+00, + 1.00003433e+00, 1.00004184e+00, 1.00004935e+00, 1.00005686e+00, + 1.00006437e+00, 1.00007176e+00, 1.00007904e+00, 1.00008631e+00, + 1.00009358e+00, 1.00010073e+00, 1.00010777e+00, 1.00011480e+00, + 1.00012159e+00, 1.00012839e+00, 1.00013518e+00, 1.00014174e+00, + 1.00014830e+00, 1.00015461e+00, 1.00016093e+00, 1.00016701e+00, + 1.00017309e+00, 1.00017893e+00, 1.00018466e+00, 1.00019026e+00, + 1.00019574e+00, 1.00020099e+00, 1.00020623e+00, 1.00021124e+00, + 1.00021613e+00, 1.00022078e+00, 1.00022531e+00, 1.00022972e+00, + 1.00023389e+00, 1.00023794e+00, 1.00024176e+00, 1.00024545e+00, + 1.00024891e+00, 1.00025225e+00, 1.00025535e+00, 1.00025833e+00, + 1.00026107e+00, 1.00026357e+00, 1.00026596e+00, 1.00026822e+00, + 1.00027013e+00, 1.00027204e+00, 1.00027359e+00, 1.00027502e+00, + 1.00027621e+00, 1.00027728e+00, 1.00027812e+00, 1.00027883e+00, + 1.00027919e+00, 1.00027955e+00, 1.00027955e+00, 1.00027943e+00, + 1.00027919e+00, 1.00027859e+00, 1.00027800e+00, 1.00027704e+00, + 1.00027597e+00, 1.00027478e+00, 1.00027335e+00, 1.00027168e+00, + 1.00026989e+00, 1.00026786e+00, 1.00026572e+00, 1.00026345e+00, + 1.00026095e+00, 1.00025821e+00, 1.00025535e+00, 1.00025237e+00, + 1.00024927e+00, 1.00024593e+00, 1.00024235e+00, 1.00023878e+00, + 1.00023496e+00, 1.00023091e+00, 1.00022686e+00, 1.00022256e+00, + 1.00021827e+00, 1.00021374e+00, 1.00020909e+00, 1.00020421e+00, + 1.00019932e+00, 1.00019431e+00, 1.00018907e+00, 1.00018382e+00, + 1.00017846e+00, 1.00017297e+00, 1.00016737e+00, 1.00016165e+00, + 1.00015581e+00, 1.00014985e+00, 1.00014389e+00, 1.00013781e+00, + 1.00013161e+00, 1.00012529e+00, 1.00011897e+00, 1.00011253e+00, + 1.00010610e+00, 1.00009954e+00, 1.00009298e+00, 1.00008631e+00, + 1.00007963e+00, 1.00007284e+00, 1.00006604e+00, 1.00005913e+00, + 1.00005233e+00, 1.00004542e+00, 1.00003850e+00, 1.00003147e+00, + 1.00002456e+00, 1.00001752e+00, 1.00001049e+00, 1.00000346e+00, + 9.99996483e-01, 9.99989510e-01, 9.99982476e-01, 9.99975502e-01, + 9.99968529e-01, 9.99961555e-01, 9.99954641e-01, 9.99947727e-01, + 9.99940813e-01, 9.99933958e-01, 9.99927163e-01, 9.99920428e-01, + 9.99913692e-01, 9.99907076e-01, 9.99900460e-01, 9.99893904e-01, + 9.99887466e-01, 9.99881029e-01, 9.99874711e-01, 9.99868453e-01, + 9.99862254e-01, 9.99856174e-01, 9.99850154e-01, 9.99844253e-01, + 9.99838412e-01, 9.99832690e-01, 9.99827087e-01, 9.99821603e-01, + 9.99816179e-01, 9.99810934e-01, 9.99805748e-01, 9.99800682e-01, + 9.99795794e-01, 9.99791026e-01, 9.99786317e-01, 9.99781847e-01, + 9.99777436e-01, 9.99773204e-01, 9.99769092e-01, 9.99765158e-01, + 9.99761343e-01, 9.99757707e-01, 9.99754190e-01, 9.99750853e-01, + 9.99747694e-01, 9.99744654e-01, 9.99741852e-01, 9.99739170e-01, + 9.99736667e-01, 9.99734342e-01, 9.99732137e-01, 9.99730170e-01, + 9.99728382e-01, 9.99726772e-01, 9.99725342e-01, 9.99724090e-01, + 9.99723017e-01, 9.99722123e-01, 9.99721408e-01, 9.99720931e-01, + 9.99720633e-01, 9.99720514e-01, 9.99720573e-01, 9.99720812e-01, + 9.99721289e-01, 9.99721944e-01, 9.99722779e-01, 9.99723792e-01, + 9.99725044e-01, 9.99726474e-01, 9.99728084e-01, 9.99729872e-01, + 9.99731898e-01, 9.99734104e-01, 9.99736488e-01, 9.99739051e-01, + 9.99741793e-01, 9.99744713e-01, 9.99747872e-01, 9.99751151e-01, + 9.99754667e-01, 9.99758303e-01, 9.99762177e-01, 9.99766171e-01, + 9.99770403e-01, 9.99774754e-01, 9.99779284e-01, 9.99783993e-01, + 9.99788821e-01, 9.99793828e-01, 9.99799013e-01, 9.99804318e-01, + 9.99809742e-01, 9.99815404e-01, 9.99821126e-01, 9.99826968e-01, + 9.99832988e-01, 9.99839127e-01, 9.99845386e-01, 9.99851763e-01, + 9.99858260e-01, 9.99864876e-01, 9.99871552e-01, 9.99878347e-01, + 9.99885261e-01, 9.99892235e-01, 9.99899328e-01, 9.99906480e-01, + 9.99913692e-01, 9.99920964e-01, 9.99928296e-01, 9.99935687e-01, + 9.99943137e-01, 9.99950647e-01, 9.99958158e-01, 9.99965727e-01, + 9.99973297e-01, 9.99980927e-01, 9.99988556e-01, 9.99996185e-01, + 1.00000381e+00, 1.00001132e+00, 1.00001884e+00, 1.00002635e+00, + 1.00003362e+00, 1.00004077e+00, 1.00004780e+00, 1.00005460e+00, + 1.00006104e+00, 1.00006711e+00, 1.00007272e+00, 1.00007772e+00, + 1.00008214e+00, 1.00008571e+00, 1.00008833e+00, 1.00008976e+00, + 1.00008988e+00, 1.00008845e+00, 1.00008512e+00, 1.00007975e+00, + 1.00007200e+00, 1.00006139e+00, 1.00004768e+00, 1.00003028e+00, + 1.00000894e+00, 9.99982893e-01, 9.99951661e-01, 9.99914646e-01, + 9.99871135e-01, 9.99820411e-01, 9.99761701e-01, 9.99694109e-01, + 9.99616742e-01, 9.99528646e-01, 9.99428689e-01, 9.99315858e-01, + 9.99188840e-01, 9.99046445e-01, 9.98887360e-01, 9.98710036e-01, + 9.98513043e-01, 9.98294711e-01, 9.98053491e-01, 9.97787535e-01, + 9.97494996e-01, 9.97173846e-01, 9.96822178e-01, 9.96437728e-01, + 9.96018291e-01, 9.95561540e-01, 9.95064974e-01, 9.94526088e-01, + 9.93942142e-01, 9.93310452e-01, 9.92628038e-01, 9.91891921e-01, + 9.91099000e-01, 9.90245998e-01, 9.89329517e-01, 9.88346159e-01, + 9.87292290e-01, 9.86164153e-01, 9.84957933e-01, 9.83669639e-01, + 9.82295156e-01, 9.80830312e-01, 9.79270697e-01, 9.77612019e-01, + 9.75849628e-01, 9.73978817e-01, 9.71994996e-01, 9.69893157e-01, + 9.67668533e-01, 9.65315938e-01, 9.62830484e-01, 9.60206985e-01, + 9.57440197e-01, 9.54525113e-01, 9.51456368e-01, 9.48228836e-01, + 9.44837391e-01, 9.41276729e-01, 9.37541902e-01, 9.33627844e-01, + 9.29529607e-01, 9.25242424e-01, 9.20761466e-01, 9.16082382e-01, + 9.11200643e-01, 9.06112134e-01, 9.00812864e-01, 8.95299196e-01, + 8.89567554e-01, 8.83614719e-01, 8.77437830e-01, 8.71034324e-01, + 8.64401877e-01, 8.57538521e-01, 8.50442827e-01, 8.43113542e-01, + 8.35549891e-01, 8.27751517e-01, 8.19718421e-01, 8.11451137e-01, + 8.02950621e-01, 7.94218183e-01, 7.85255671e-01, 7.76065350e-01, + 7.66650081e-01, 7.57013023e-01, 7.47157931e-01, 7.37088978e-01, + 7.26810873e-01, 7.16328681e-01, 7.05648124e-01, 6.94775164e-01, + 6.83716357e-01, 6.72478795e-01, 6.61069810e-01, 6.49497330e-01, + 6.37769580e-01, 6.25895321e-01, 6.13883674e-01, 6.01744056e-01, + 5.89486361e-01, 5.77120781e-01, 5.64657867e-01, 5.52108407e-01, + 5.39483547e-01, 5.26794672e-01, 5.14053404e-01, 5.01271665e-01, + 4.88461435e-01, 4.75634992e-01, 4.62804615e-01, 4.49982822e-01, + 4.37182158e-01, 4.24415171e-01, 4.11694527e-01, 3.99032772e-01, + 3.86442453e-01, 3.73936087e-01, 3.61525953e-01, 3.49224269e-01, + 3.37043047e-01, 3.24994087e-01, 3.13088894e-01, 3.01338732e-01, + 2.89754450e-01, 2.78346658e-01, 2.67125458e-01, 2.56100595e-01, + 2.45281324e-01, 2.34676436e-01, 2.24294156e-01, 2.14142203e-01, + 2.04227716e-01, 1.94557235e-01, 1.85136691e-01, 1.75971389e-01, + 1.67065978e-01, 1.58424467e-01, 1.50050193e-01, 1.41945809e-01, + 1.34113312e-01, 1.26554012e-01, 1.19268581e-01, 1.12257004e-01, + 1.05518632e-01, 9.90521908e-02, 9.28557739e-02, 8.69268849e-02, + 8.12624842e-02, 7.58589506e-02, 7.07121640e-02, 6.58175275e-02, + 6.11699894e-02, 5.67640625e-02, 5.25939018e-02, 4.86532971e-02, + 4.49357443e-02, 4.14344519e-02, 3.81424055e-02, 3.50523964e-02, + 3.21570449e-02, 2.94488575e-02, 2.69202497e-02, 2.45635863e-02, + 2.23712083e-02, 2.03354694e-02, 1.84487645e-02, 1.67035554e-02, + 1.50924018e-02, 1.36079816e-02, 1.22431125e-02, 1.09907771e-02, + 9.84413363e-03, 8.79654102e-03, 7.84156192e-03, 6.97298534e-03, + 6.18482940e-03, 5.47135156e-03, 4.82705561e-03, 4.24669450e-03, + 3.72527563e-03, 3.25805834e-03, 2.84055714e-03, 2.46853800e-03, + 2.13801605e-03, 1.84525200e-03, 1.58674677e-03, 1.35923503e-03, + 1.15967961e-03, 9.85263032e-04, 8.33379803e-04, 7.01628160e-04, + 5.87800692e-04, 4.89875500e-04, 4.06006613e-04, 3.34514218e-04, + 2.73875427e-04, 2.22714254e-04, 1.79792376e-04, 1.43999539e-04, + 1.14344395e-04, 8.99455481e-05, 7.00227974e-05, 5.38887325e-05, + 4.09407221e-05, 3.06531692e-05, 2.25702297e-05, 1.62988836e-05, + 1.15024377e-05, 7.89443584e-06, 5.23298331e-06, 3.31548563e-06, + 1.97379018e-06, 1.06971811e-06, 4.90905393e-07, 1.46209757e-07, +}; + +#endif /* LC3_PLUS_HR */ + +#undef __LC3_MDCT_WIN_LEN + +const float *lc3_mdct_win[LC3_NUM_DT][LC3_NUM_SRATE] = { + + [LC3_DT_2M5] = { + LC3_IF_PLUS( mdct_win_2m5_8k , NULL ), + LC3_IF_PLUS( mdct_win_2m5_16k, NULL ), + LC3_IF_PLUS( mdct_win_2m5_24k, NULL ), + LC3_IF_PLUS( mdct_win_2m5_32k, NULL ), + LC3_IF_PLUS( mdct_win_2m5_48k, NULL ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( mdct_win_2m5_48k_hr, NULL ), NULL ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( mdct_win_2m5_96k_hr, NULL ), NULL ) }, + + [LC3_DT_5M ] = { + LC3_IF_PLUS( mdct_win_5m_8k , NULL ), + LC3_IF_PLUS( mdct_win_5m_16k, NULL ), + LC3_IF_PLUS( mdct_win_5m_24k, NULL ), + LC3_IF_PLUS( mdct_win_5m_32k, NULL ), + LC3_IF_PLUS( mdct_win_5m_48k, NULL ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( mdct_win_5m_48k_hr, NULL ), NULL ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( mdct_win_5m_96k_hr, NULL ), NULL ) }, + + [LC3_DT_7M5] = { + mdct_win_7m5_8k , mdct_win_7m5_16k, mdct_win_7m5_24k, + mdct_win_7m5_32k, mdct_win_7m5_48k }, + + [LC3_DT_10M] = { + mdct_win_10m_8k , mdct_win_10m_16k, mdct_win_10m_24k, + mdct_win_10m_32k, mdct_win_10m_48k, + LC3_IF_PLUS_HR( mdct_win_10m_48k_hr, NULL), + LC3_IF_PLUS_HR( mdct_win_10m_96k_hr, NULL), }, +}; + + +/** + * Bands limits + */ + +#if LC3_PLUS + +static const int band_lim_2m5_8k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20 +}; + +static const int band_lim_2m5_16k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 32, 34, 36, 38, 40 +}; + +static const int band_lim_2m5_24k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 28, 30, 32, + 34, 36, 38, 40, 42, 44, 47, 50, 53, 56, + 60 +}; + +static const int band_lim_2m5_32k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, + 36, 38, 40, 43, 46, 49, 52, 55, 59, 63, + 67, 71, 75, 80 +}; + +static const int band_lim_2m5_48k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, + 40, 43, 46, 49, 52, 56, 60, 64, 68, 72, + 77, 82, 87, 93, 100 +}; + +#if LC3_PLUS_HR + +static const int band_lim_2m5_48k_hr[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 21, 23, 25, 27, 29, 31, 33, 35, 37, 40, + 43, 46, 49, 53, 57, 61, 65, 69, 74, 79, + 85, 91, 97, 104, 112, 120 +}; + +static const int band_lim_2m5_96k_hr[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, + 24, 26, 28, 30, 32, 35, 38, 41, 45, 49, + 53, 57, 62, 67, 73, 79, 85, 92, 100, 108, + 117, 127, 137, 149, 161, 174, 189, 204, 221, 240 +}; + +#endif /* LC3_PLUS_HR */ + +static const int band_lim_5m_8k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 40 +}; + +static const int band_lim_5m_16k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, + 50, 52, 54, 57, 60, 63, 66, 69, 72, 76, + 80 +}; + +static const int band_lim_5m_24k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, + 36, 38, 40, 42, 44, 47, 50, 53, 56, 59, + 62, 65, 69, 73, 77, 81, 86, 91, 96, 101, + 107, 113, 120 +}; + +static const int band_lim_5m_32k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 24, 26, 28, 30, 32, 34, 36, + 38, 40, 42, 45, 48, 51, 54, 57, 61, 65, + 69, 73, 78, 83, 88, 93, 99, 105, 112, 119, + 126, 134, 142, 151, 160 +}; + +static const int band_lim_5m_48k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, + 40, 43, 46, 49, 52, 55, 59, 63, 67, 72, + 77, 82, 87, 93, 99, 105, 112, 120, 128, 136, + 145, 155, 165, 176, 187, 200 +}; + +#if LC3_PLUS_HR + +static const int band_lim_5m_48k_hr[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 21, 23, 25, 27, 29, 31, 33, 35, 38, 41, + 44, 47, 50, 54, 58, 62, 66, 71, 76, 81, + 87, 93, 100, 107, 114, 122, 131, 140, 149, 160, + 171, 183, 196, 209, 224, 240 +}; + +static const int band_lim_5m_96k_hr[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 19, 21, + 23, 25, 27, 29, 31, 34, 37, 40, 44, 48, + 52, 56, 61, 66, 71, 77, 83, 90, 98, 106, + 115, 124, 135, 146, 158, 171, 185, 200, 217, 235, + 254, 275, 298, 323, 349, 378, 409, 443, 480 +}; + +#endif /* LC3_PLUS_HR */ +#endif /* LC3_PLUS */ + +static const int band_lim_7m5_8k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60 +}; + +static const int band_lim_7m5_16k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 36, 38, 40, 42, 44, + 46, 48, 50, 52, 54, 56, 58, 60, 62, 65, + 68, 71, 74, 77, 80, 83, 86, 90, 94, 98, + 102, 106, 110, 115, 120 +}; + +static const int band_lim_7m5_24k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 29, 31, + 33, 35, 37, 39, 41, 43, 45, 47, 49, 52, + 55, 58, 61, 64, 67, 70, 74, 78, 82, 86, + 90, 95, 100, 105, 110, 115, 121, 127, 134, 141, + 148, 155, 163, 171, 180 +}; + +static const int band_lim_7m5_32k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 26, 28, 30, 32, 34, + 36, 38, 40, 42, 45, 48, 51, 54, 57, 60, + 63, 67, 71, 75, 79, 84, 89, 94, 99, 105, + 111, 117, 124, 131, 138, 146, 154, 163, 172, 182, + 192, 203, 215, 227, 240 +}; + +static const int band_lim_7m5_48k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 24, 26, 28, 30, 32, 34, 36, + 38, 40, 43, 46, 49, 52, 55, 59, 63, 67, + 71, 75, 80, 85, 90, 96, 102, 108, 115, 122, + 129, 137, 146, 155, 165, 175, 186, 197, 209, 222, + 236, 251, 266, 283, 300 +}; + +static const int band_lim_10m_8k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, + 71, 73, 75, 77, 80 +}; + +static const int band_lim_10m_16k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, + 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, + 52, 55, 58, 61, 64, 67, 70, 73, 76, 80, + 84, 88, 92, 96, 101, 106, 111, 116, 121, 127, + 133, 139, 146, 153, 160 +}; + +static const int band_lim_10m_24k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 25, 27, 29, 31, 33, 35, + 37, 39, 41, 43, 46, 49, 52, 55, 58, 61, + 64, 68, 72, 76, 80, 85, 90, 95, 100, 106, + 112, 118, 125, 132, 139, 147, 155, 164, 173, 183, + 193, 204, 215, 227, 240 +}; + +static const int band_lim_10m_32k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, + 41, 44, 47, 50, 53, 56, 60, 64, 68, 72, + 76, 81, 86, 91, 97, 103, 109, 116, 123, 131, + 139, 148, 157, 166, 176, 187, 199, 211, 224, 238, + 252, 268, 284, 302, 320 +}; + +static const int band_lim_10m_48k[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, + 22, 24, 26, 28, 30, 32, 34, 36, 39, 42, + 45, 48, 51, 55, 59, 63, 67, 71, 76, 81, + 86, 92, 98, 105, 112, 119, 127, 135, 144, 154, + 164, 175, 186, 198, 211, 225, 240, 256, 273, 291, + 310, 330, 352, 375, 400 +}; + +#if LC3_PLUS_HR + +static const int band_lim_10m_48k_hr[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 19, 21, + 23, 25, 27, 29, 31, 33, 36, 39, 42, 45, + 48, 51, 55, 59, 63, 67, 72, 77, 83, 89, + 95, 101, 108, 116, 124, 133, 142, 152, 163, 174, + 187, 200, 214, 229, 244, 262, 280, 299, 320, 343, + 367, 392, 419, 449, 480 +}; + +static const int band_lim_10m_96k_hr[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8 , 9, + 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, + 28, 30, 33, 36, 39, 42, 46, 50, 54, 59, + 64, 69, 75, 82, 89, 96, 104, 113, 122, 132, + 143, 155, 168, 181, 196, 213, 230, 249, 270, 292, + 316, 342, 371, 401, 434, 470, 509, 551, 596, 646, + 699, 757, 819, 887, 960 +}; + +#endif /* LC3_PLUS_HR */ + +const int *lc3_band_lim[LC3_NUM_DT][LC3_NUM_SRATE] = { + + [LC3_DT_2M5] = { + LC3_IF_PLUS( band_lim_2m5_8k , NULL ), + LC3_IF_PLUS( band_lim_2m5_16k, NULL ), + LC3_IF_PLUS( band_lim_2m5_24k, NULL ), + LC3_IF_PLUS( band_lim_2m5_32k, NULL ), + LC3_IF_PLUS( band_lim_2m5_48k, NULL ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( band_lim_2m5_48k_hr, NULL ), NULL ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( band_lim_2m5_96k_hr, NULL ), NULL ) }, + + [LC3_DT_5M] = { + LC3_IF_PLUS( band_lim_5m_8k , NULL ), + LC3_IF_PLUS( band_lim_5m_16k, NULL ), + LC3_IF_PLUS( band_lim_5m_24k, NULL ), + LC3_IF_PLUS( band_lim_5m_32k, NULL ), + LC3_IF_PLUS( band_lim_5m_48k, NULL ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( band_lim_5m_48k_hr, NULL ), NULL ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( band_lim_5m_96k_hr, NULL ), NULL ) }, + + [LC3_DT_7M5] = { + band_lim_7m5_8k , band_lim_7m5_16k, band_lim_7m5_24k, + band_lim_7m5_32k, band_lim_7m5_48k }, + + [LC3_DT_10M] = { + band_lim_10m_8k , band_lim_10m_16k, band_lim_10m_24k, + band_lim_10m_32k, band_lim_10m_48k, + LC3_IF_PLUS_HR( band_lim_10m_48k_hr, NULL ), + LC3_IF_PLUS_HR( band_lim_10m_96k_hr, NULL ) }, +}; + +#define __LC3_NUM_BANDS(_lim) \ + ( sizeof(_lim) / sizeof(*(_lim)) - 1 ) + +const int lc3_num_bands[LC3_NUM_DT][LC3_NUM_SRATE] = { + + [LC3_DT_2M5] = { + LC3_IF_PLUS( __LC3_NUM_BANDS( band_lim_2m5_8k ), 0 ), + LC3_IF_PLUS( __LC3_NUM_BANDS( band_lim_2m5_16k ), 0 ), + LC3_IF_PLUS( __LC3_NUM_BANDS( band_lim_2m5_24k ), 0 ), + LC3_IF_PLUS( __LC3_NUM_BANDS( band_lim_2m5_32k ), 0 ), + LC3_IF_PLUS( __LC3_NUM_BANDS( band_lim_2m5_48k ), 0 ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( + __LC3_NUM_BANDS( band_lim_2m5_48k_hr ), 0 ), 0 ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( + __LC3_NUM_BANDS( band_lim_2m5_96k_hr ), 0 ), 0 ) }, + + [LC3_DT_5M] = { + LC3_IF_PLUS( __LC3_NUM_BANDS( band_lim_5m_8k ), 0 ), + LC3_IF_PLUS( __LC3_NUM_BANDS( band_lim_5m_16k ), 0 ), + LC3_IF_PLUS( __LC3_NUM_BANDS( band_lim_5m_24k ), 0 ), + LC3_IF_PLUS( __LC3_NUM_BANDS( band_lim_5m_32k ), 0 ), + LC3_IF_PLUS( __LC3_NUM_BANDS( band_lim_5m_48k ), 0 ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( + __LC3_NUM_BANDS( band_lim_5m_48k_hr ), 0 ), 0 ), + LC3_IF_PLUS( LC3_IF_PLUS_HR( + __LC3_NUM_BANDS( band_lim_5m_96k_hr ), 0 ), 0 ) }, + + [LC3_DT_7M5] = { + __LC3_NUM_BANDS( band_lim_7m5_8k ), + __LC3_NUM_BANDS( band_lim_7m5_16k ), + __LC3_NUM_BANDS( band_lim_7m5_24k ), + __LC3_NUM_BANDS( band_lim_7m5_32k ), + __LC3_NUM_BANDS( band_lim_7m5_48k ) }, + + [LC3_DT_10M] = { + __LC3_NUM_BANDS( band_lim_10m_8k ), + __LC3_NUM_BANDS( band_lim_10m_16k ), + __LC3_NUM_BANDS( band_lim_10m_24k ), + __LC3_NUM_BANDS( band_lim_10m_32k ), + __LC3_NUM_BANDS( band_lim_10m_48k ), + LC3_IF_PLUS_HR( __LC3_NUM_BANDS( band_lim_10m_48k_hr ), 0 ), + LC3_IF_PLUS_HR( __LC3_NUM_BANDS( band_lim_10m_96k_hr ), 0 ) }, +}; + +#undef __LC3_NUM_BANDS + + +/** + * SNS Quantization + */ + +const float lc3_sns_lfcb[32][8] = { + + { 2.26283366e+00, 8.13311269e-01, -5.30193495e-01, -1.35664836e+00, + -1.59952177e+00, -1.44098768e+00, -1.14381648e+00, -7.55203768e-01 }, + + { 2.94516479e+00, 2.41143318e+00, 9.60455106e-01, -4.43226488e-01, + -1.22913612e+00, -1.55590039e+00, -1.49688656e+00, -1.11689987e+00 }, + + { -2.18610707e+00, -1.97152136e+00, -1.78718620e+00, -1.91865896e+00, + -1.79399122e+00, -1.35738404e+00, -7.05444279e-01, -4.78172945e-02 }, + + { 6.93688237e-01, 9.55609857e-01, 5.75230787e-01, -1.14603419e-01, + -6.46050637e-01, -9.52351370e-01, -1.07405247e+00, -7.58087707e-01 }, + + { -1.29752132e+00, -7.40369057e-01, -3.45372484e-01, -3.13285696e-01, + -4.02977243e-01, -3.72020853e-01, -7.83414177e-02, 9.70441304e-02 }, + + { 9.14652038e-01, 1.74293043e+00, 1.90906627e+00, 1.54408484e+00, + 1.09344961e+00, 6.47479550e-01, 3.61790752e-02, -2.97092807e-01 }, + + { -2.51428813e+00, -2.89175271e+00, -2.00450667e+00, -7.50912274e-01, + 4.41202105e-01, 1.20190988e+00, 1.32742857e+00, 1.22049081e+00 }, + + { -9.22188405e-01, 6.32495141e-01, 1.08736431e+00, 6.08628625e-01, + 1.31174568e-01, -2.96149158e-01, -2.07013517e-01, 1.34924917e-01 }, + + { 7.90322288e-01, 6.28401262e-01, 3.93117924e-01, 4.80007711e-01, + 4.47815138e-01, 2.09734215e-01, 6.56691996e-03, -8.61242342e-02 }, + + { 1.44775580e+00, 2.72399952e+00, 2.31083269e+00, 9.35051270e-01, + -2.74743911e-01, -9.02077697e-01, -9.40681512e-01, -6.33697039e-01 }, + + { 7.93354526e-01, 1.43931186e-02, -5.67834845e-01, -6.54760468e-01, + -4.79458998e-01, -1.73894662e-01, 6.80162706e-02, 2.95125948e-01 }, + + { 2.72425347e+00, 2.95947572e+00, 1.84953559e+00, 5.63284922e-01, + 1.39917088e-01, 3.59641093e-01, 6.89461355e-01, 6.39790177e-01 }, + + { -5.30830198e-01, -2.12690683e-01, 5.76613628e-03, 4.24871484e-01, + 4.73128952e-01, 8.58894199e-01, 1.19111161e+00, 9.96189670e-01 }, + + { 1.68728411e+00, 2.43614509e+00, 2.33019429e+00, 1.77983778e+00, + 1.44411295e+00, 1.51995177e+00, 1.47199394e+00, 9.77682474e-01 }, + + { -2.95183273e+00, -1.59393497e+00, -1.09918773e-01, 3.88609073e-01, + 5.12932650e-01, 6.28112597e-01, 8.22621796e-01, 8.75891425e-01 }, + + { 1.01878343e-01, 5.89857324e-01, 6.19047647e-01, 1.26731314e+00, + 2.41961048e+00, 2.25174253e+00, 5.26537031e-01, -3.96591513e-01 }, + + { 2.68254575e+00, 1.32738011e+00, 1.30185274e-01, -3.38533089e-01, + -3.68219236e-01, -1.91689947e-01, -1.54782377e-01, -2.34207178e-01 }, + + { 4.82697924e+00, 3.11947804e+00, 1.39513671e+00, 2.50295316e-01, + -3.93613839e-01, -6.43458173e-01, -6.42570737e-01, -7.23193223e-01 }, + + { 8.78419936e-02, -5.69586840e-01, -1.14506016e+00, -1.66968488e+00, + -1.84534418e+00, -1.56468027e+00, -1.11746759e+00, -5.33981663e-01 }, + + { 1.39102308e+00, 1.98146479e+00, 1.11265796e+00, -2.20107509e-01, + -7.74965612e-01, -5.94063874e-01, 1.36937681e-01, 8.18242891e-01 }, + + { 3.84585894e-01, -1.60588786e-01, -5.39366810e-01, -5.29309079e-01, + 1.90433547e-01, 2.56062918e+00, 2.81896398e+00, 6.56670876e-01 }, + + { 1.93227399e+00, 3.01030180e+00, 3.06543894e+00, 2.50110161e+00, + 1.93089593e+00, 5.72153811e-01, -8.11741794e-01, -1.17641811e+00 }, + + { 1.75080463e-01, -7.50522832e-01, -1.03943893e+00, -1.13577509e+00, + -1.04197904e+00, -1.52060099e-02, 2.07048392e+00, 3.42948918e+00 }, + + { -1.18817020e+00, 3.66792874e-01, 1.30957830e+00, 1.68330687e+00, + 1.25100924e+00, 9.42375752e-01, 8.26250483e-01, 4.39952741e-01 }, + + { 2.53322203e+00, 2.11274643e+00, 1.26288412e+00, 7.61513512e-01, + 5.22117938e-01, 1.18680070e-01, -4.52346828e-01, -7.00352426e-01 }, + + { 3.99889837e+00, 4.07901751e+00, 2.82285661e+00, 1.72607213e+00, + 6.47144377e-01, -3.31148521e-01, -8.84042571e-01, -1.12697341e+00 }, + + { 5.07902593e-01, 1.58838450e+00, 1.72899024e+00, 1.00692230e+00, + 3.77121232e-01, 4.76370767e-01, 1.08754740e+00, 1.08756266e+00 }, + + { 3.16856825e+00, 3.25853458e+00, 2.42230591e+00, 1.79446078e+00, + 1.52177911e+00, 1.17196707e+00, 4.89394597e-01, -6.22795716e-02 }, + + { 1.89414767e+00, 1.25108695e+00, 5.90451211e-01, 6.08358583e-01, + 8.78171010e-01, 1.11912511e+00, 1.01857662e+00, 6.20453891e-01 }, + + { 9.48880605e-01, 2.13239439e+00, 2.72345350e+00, 2.76986077e+00, + 2.54286973e+00, 2.02046264e+00, 8.30045859e-01, -2.75569174e-02 }, + + { -1.88026757e+00, -1.26431073e+00, 3.11424977e-01, 1.83670210e+00, + 2.25634192e+00, 2.04818998e+00, 2.19526837e+00, 2.02659614e+00 }, + + { 2.46375746e-01, 9.55621773e-01, 1.52046777e+00, 1.97647400e+00, + 1.94043867e+00, 2.23375847e+00, 1.98835978e+00, 1.27232673e+00 }, + +}; + +const float lc3_sns_hfcb[32][8] = { + + { 2.32028419e-01, -1.00890271e+00, -2.14223503e+00, -2.37533814e+00, + -2.23041933e+00, -2.17595881e+00, -2.29065914e+00, -2.53286398e+00 }, + + { -1.29503937e+00, -1.79929965e+00, -1.88703148e+00, -1.80991660e+00, + -1.76340038e+00, -1.83418428e+00, -1.80480981e+00, -1.73679545e+00 }, + + { 1.39285716e-01, -2.58185126e-01, -6.50804573e-01, -1.06815732e+00, + -1.61928742e+00, -2.18762566e+00, -2.63757587e+00, -2.97897750e+00 }, + + { -3.16513102e-01, -4.77747657e-01, -5.51162076e-01, -4.84788283e-01, + -2.38388394e-01, -1.43024507e-01, 6.83186674e-02, 8.83061717e-02 }, + + { 8.79518405e-01, 2.98340096e-01, -9.15386396e-01, -2.20645975e+00, + -2.74142181e+00, -2.86139074e+00, -2.88841597e+00, -2.95182608e+00 }, + + { -2.96701922e-01, -9.75004919e-01, -1.35857500e+00, -9.83721106e-01, + -6.52956939e-01, -9.89986993e-01, -1.61467225e+00, -2.40712302e+00 }, + + { 3.40981100e-01, 2.68899789e-01, 5.63335685e-02, 4.99114047e-02, + -9.54130727e-02, -7.60166146e-01, -2.32758120e+00, -3.77155485e+00 }, + + { -1.41229759e+00, -1.48522119e+00, -1.18603580e+00, -6.25001634e-01, + 1.53902497e-01, 5.76386498e-01, 7.95092604e-01, 5.96564632e-01 }, + + { -2.28839512e-01, -3.33719070e-01, -8.09321359e-01, -1.63587877e+00, + -1.88486397e+00, -1.64496691e+00, -1.40515778e+00, -1.46666471e+00 }, + + { -1.07148629e+00, -1.41767015e+00, -1.54891762e+00, -1.45296062e+00, + -1.03182970e+00, -6.90642640e-01, -4.28843805e-01, -4.94960215e-01 }, + + { -5.90988511e-01, -7.11737759e-02, 3.45719523e-01, 3.00549461e-01, + -1.11865218e+00, -2.44089151e+00, -2.22854732e+00, -1.89509228e+00 }, + + { -8.48434099e-01, -5.83226811e-01, 9.00423688e-02, 8.45025008e-01, + 1.06572385e+00, 7.37582999e-01, 2.56590452e-01, -4.91963360e-01 }, + + { 1.14069146e+00, 9.64016892e-01, 3.81461206e-01, -4.82849341e-01, + -1.81632721e+00, -2.80279513e+00, -3.23385725e+00, -3.45908714e+00 }, + + { -3.76283238e-01, 4.25675462e-02, 5.16547697e-01, 2.51716882e-01, + -2.16179968e-01, -5.34074091e-01, -6.40786096e-01, -8.69745032e-01 }, + + { 6.65004121e-01, 1.09790765e+00, 1.38342667e+00, 1.34327359e+00, + 8.22978837e-01, 2.15876799e-01, -4.04925753e-01, -1.07025606e+00 }, + + { -8.26265954e-01, -6.71181233e-01, -2.28495593e-01, 5.18980853e-01, + 1.36721896e+00, 2.18023038e+00, 2.53596093e+00, 2.20121099e+00 }, + + { 1.41008327e+00, 7.54441908e-01, -1.30550585e+00, -1.87133711e+00, + -1.24008685e+00, -1.26712925e+00, -2.03670813e+00, -2.89685162e+00 }, + + { 3.61386818e-01, -2.19991705e-02, -5.79368834e-01, -8.79427961e-01, + -8.50685023e-01, -7.79397050e-01, -7.32182927e-01, -8.88348515e-01 }, + + { 4.37469239e-01, 3.05440420e-01, -7.38786566e-03, -4.95649855e-01, + -8.06651271e-01, -1.22431892e+00, -1.70157770e+00, -2.24491914e+00 }, + + { 6.48100319e-01, 6.82299134e-01, 2.53247464e-01, 7.35842144e-02, + 3.14216709e-01, 2.34729881e-01, 1.44600134e-01, -6.82120179e-02 }, + + { 1.11919833e+00, 1.23465533e+00, 5.89170238e-01, -1.37192460e+00, + -2.37095707e+00, -2.00779783e+00, -1.66688540e+00, -1.92631846e+00 }, + + { 1.41847497e-01, -1.10660071e-01, -2.82824593e-01, -6.59813475e-03, + 2.85929280e-01, 4.60445530e-02, -6.02596416e-01, -2.26568729e+00 }, + + { 5.04046955e-01, 8.26982163e-01, 1.11981236e+00, 1.17914044e+00, + 1.07987429e+00, 6.97536239e-01, -9.12548817e-01, -3.57684747e+00 }, + + { -5.01076050e-01, -3.25678006e-01, 2.80798195e-02, 2.62054555e-01, + 3.60590806e-01, 6.35623722e-01, 9.59012467e-01, 1.30745157e+00 }, + + { 3.74970983e+00, 1.52342612e+00, -4.57715662e-01, -7.98711008e-01, + -3.86819329e-01, -3.75901062e-01, -6.57836900e-01, -1.28163964e+00 }, + + { -1.15258991e+00, -1.10800886e+00, -5.62615117e-01, -2.20562124e-01, + -3.49842880e-01, -7.53432770e-01, -9.88596593e-01, -1.28790472e+00 }, + + { 1.02827246e+00, 1.09770519e+00, 7.68645546e-01, 2.06081978e-01, + -3.42805735e-01, -7.54939405e-01, -1.04196178e+00, -1.50335653e+00 }, + + { 1.28831972e-01, 6.89439395e-01, 1.12346905e+00, 1.30934523e+00, + 1.35511965e+00, 1.42311381e+00, 1.15706449e+00, 4.06319438e-01 }, + + { 1.34033030e+00, 1.38996825e+00, 1.04467922e+00, 6.35822746e-01, + -2.74733756e-01, -1.54923372e+00, -2.44239710e+00, -3.02457607e+00 }, + + { 2.13843105e+00, 4.24711267e+00, 2.89734110e+00, 9.32730658e-01, + -2.92822250e-01, -8.10404297e-01, -7.88868099e-01, -9.35353149e-01 }, + + { 5.64830487e-01, 1.59184978e+00, 2.39771699e+00, 3.03697344e+00, + 2.66424350e+00, 1.39304485e+00, 4.03834024e-01, -6.56270971e-01 }, + + { -4.22460548e-01, 3.26149625e-01, 1.39171313e+00, 2.23146615e+00, + 2.61179442e+00, 2.66540340e+00, 2.40103554e+00, 1.75920380e+00 }, + +}; + +const struct lc3_sns_vq_gains lc3_sns_vq_gains[4] = { + + { 2, (const float []){ + 8915.f / 4096, 12054.f / 4096 } }, + + { 4, (const float []){ + 6245.f / 4096, 15043.f / 4096, 17861.f / 4096, 21014.f / 4096 } }, + + { 4, (const float []){ + 7099.f / 4096, 9132.f / 4096, 11253.f / 4096, 14808.f / 4096 } }, + + { 8, (const float []){ + 4336.f / 4096, 5067.f / 4096, 5895.f / 4096, 8149.f / 4096, + 10235.f / 4096, 12825.f / 4096, 16868.f / 4096, 19882.f / 4096 } } +}; + +const int32_t lc3_sns_mpvq_offsets[][11] = { + { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, + { 0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }, + { 0, 1, 5, 13, 25, 41, 61, 85, 113, 145, 181 }, + { 0, 1, 7, 25, 63, 129, 231, 377, 575, 833, 1159 }, + { 0, 1, 9, 41, 129, 321, 681, 1289, 2241, 3649, 5641 }, + { 0, 1, 11, 61, 231, 681, 1683, 3653, 7183, 13073 , 22363 }, + { 0, 1, 13, 85, 377, 1289, 3653, 8989, 19825, 40081, 75517 }, + { 0, 1, 15, 113, 575, 2241, 7183, 19825, 48639, 108545, 224143 }, + { 0, 1, 17, 145, 833, 3649, 13073, 40081, 108545, 265729, 598417 }, + { 0, 1, 19, 181, 1159, 5641, 22363, 75517, 224143, 598417, 1462563 }, + { 0, 1, 21, 221, 1561, 8361, 36365, 134245, 433905, 1256465, 3317445 }, + { 0, 1, 23, 265, 2047, 11969, 56695, 227305, 795455, 2485825, 7059735 }, + { 0, 1, 25, 313, 2625, 16641, 85305, 369305,1392065, 4673345,14218905 }, + { 0, 1, 27, 365, 3303, 22569, 124515, 579125,2340495, 8405905,27298155 }, + { 0, 1, 29, 421, 4089, 29961, 177045, 880685,3800305,14546705,50250765 }, + { 0, 1, 31, 481, 4991, 39041, 246047,1303777,5984767,24331777,89129247 }, +}; + + +/** + * TNS Arithmetic Coding + * The number of bits are given at 2048th of bits + */ + +const struct lc3_ac_model lc3_tns_order_models[] = { + + { { { 0, 3 }, { 3, 9 }, { 12, 23 }, { 35, 54 }, + { 89, 111 }, { 200, 190 }, { 390, 268 }, { 658, 366 }, + { 1024, 0 }, { 1024, 0 }, { 1024, 0 }, { 1024, 0 }, + { 1024, 0 }, { 1024, 0 }, { 1024, 0 }, { 1024, 0 }, + { 1024, 0 } } }, + + { { { 0, 14 }, { 14, 42 }, { 56, 100 }, { 156, 157 }, + { 313, 181 }, { 494, 178 }, { 672, 167 }, { 839, 185 }, + { 1024, 0 }, { 1024, 0 }, { 1024, 0 }, { 1024, 0 }, + { 1024, 0 }, { 1024, 0 }, { 1024, 0 }, { 1024, 0 }, + { 1024, 0 } } }, +}; + +const uint16_t lc3_tns_order_bits[][8] = { + { 17234, 13988, 11216, 8694, 6566, 4977, 3961, 3040 }, + { 12683, 9437, 6874, 5541, 5121, 5170, 5359, 5056 } +}; + +const struct lc3_ac_model lc3_tns_coeffs_models[] = { + + { { { 0, 1 }, { 1, 5 }, { 6, 15 }, { 21, 31 }, + { 52, 54 }, { 106, 86 }, { 192, 97 }, { 289, 120 }, + { 409, 159 }, { 568, 152 }, { 720, 111 }, { 831, 104 }, + { 935, 59 }, { 994, 22 }, { 1016, 6 }, { 1022, 1 }, + { 1023, 1 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, + { 4, 13 }, { 17, 43 }, { 60, 94 }, { 154, 139 }, + { 293, 173 }, { 466, 160 }, { 626, 154 }, { 780, 131 }, + { 911, 78 }, { 989, 27 }, { 1016, 6 }, { 1022, 1 }, + { 1023, 1 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, + { 4, 9 }, { 13, 43 }, { 56, 106 }, { 162, 199 }, + { 361, 217 }, { 578, 210 }, { 788, 141 }, { 929, 74 }, + { 1003, 17 }, { 1020, 1 }, { 1021, 1 }, { 1022, 1 }, + { 1023, 1 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, + { 4, 2 }, { 6, 11 }, { 17, 49 }, { 66, 204 }, + { 270, 285 }, { 555, 297 }, { 852, 120 }, { 972, 39 }, + { 1011, 9 }, { 1020, 1 }, { 1021, 1 }, { 1022, 1 }, + { 1023, 1 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, + { 4, 1 }, { 5, 7 }, { 12, 42 }, { 54, 241 }, + { 295, 341 }, { 636, 314 }, { 950, 58 }, { 1008, 9 }, + { 1017, 3 }, { 1020, 1 }, { 1021, 1 }, { 1022, 1 }, + { 1023, 1 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, + { 4, 1 }, { 5, 1 }, { 6, 13 }, { 19, 205 }, + { 224, 366 }, { 590, 377 }, { 967, 47 }, { 1014, 5 }, + { 1019, 1 }, { 1020, 1 }, { 1021, 1 }, { 1022, 1 }, + { 1023, 1 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, + { 4, 1 }, { 5, 1 }, { 6, 13 }, { 19, 281 }, + { 300, 330 }, { 630, 371 }, { 1001, 17 }, { 1018, 1 }, + { 1019, 1 }, { 1020, 1 }, { 1021, 1 }, { 1022, 1 }, + { 1023, 1 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, + { 4, 1 }, { 5, 1 }, { 6, 5 }, { 11, 297 }, + { 308, 1 }, { 309, 682 }, { 991, 26 }, { 1017, 2 }, + { 1019, 1 }, { 1020, 1 }, { 1021, 1 }, { 1022, 1 }, + { 1023, 1 } } }, + +}; + +const uint16_t lc3_tns_coeffs_bits[][17] = { + + { 20480, 15725, 12479, 10334, 8694, 7320, 6964, 6335, + 5504, 5637, 6566, 6758, 8433, 11348, 15186, 20480, 20480 }, + + { 20480, 20480, 20480, 20480, 12902, 9368, 7057, 5901, + 5254, 5485, 5598, 6076, 7608, 10742, 15186, 20480, 20480 }, + + { 20480, 20480, 20480, 20480, 13988, 9368, 6702, 4841, + 4585, 4682, 5859, 7764, 12109, 20480, 20480, 20480, 20480 }, + + { 20480, 20480, 20480, 20480, 18432, 13396, 8982, 4767, + 3779, 3658, 6335, 9656, 13988, 20480, 20480, 20480, 20480 }, + + { 20480, 20480, 20480, 20480, 20480, 14731, 9437, 4275, + 3249, 3493, 8483, 13988, 17234, 20480, 20480, 20480, 20480 }, + + { 20480, 20480, 20480, 20480, 20480, 20480, 12902, 4753, + 3040, 2953, 9105, 15725, 20480, 20480, 20480, 20480, 20480 }, + + { 20480, 20480, 20480, 20480, 20480, 20480, 12902, 3821, + 3346, 3000, 12109, 20480, 20480, 20480, 20480, 20480, 20480 }, + + { 20480, 20480, 20480, 20480, 20480, 20480, 15725, 3658, + 20480, 1201, 10854, 18432, 20480, 20480, 20480, 20480, 20480 } + +}; + + +/** + * Long Term Postfilter Synthesis + * with - addition of a 0 for num coefficients + * - remove of first 0 den coefficients + */ + +const float *lc3_ltpf_cnum[LC3_NUM_SRATE][4] = { + + [LC3_SRATE_8K] = { + (const float []){ + 6.02361821e-01, 4.19760926e-01, -1.88342453e-02, 0. }, + (const float []){ + 5.99476858e-01, 4.19760926e-01, -1.59492828e-02, 0. }, + (const float []){ + 5.96776466e-01, 4.19760926e-01, -1.32488910e-02, 0. }, + (const float []){ + 5.94241012e-01, 4.19760926e-01, -1.07134366e-02, 0. }, + }, + + [LC3_SRATE_16K] = { + (const float []){ + 6.02361821e-01, 4.19760926e-01, -1.88342453e-02, 0. }, + (const float []){ + 5.99476858e-01, 4.19760926e-01, -1.59492828e-02, 0. }, + (const float []){ + 5.96776466e-01, 4.19760926e-01, -1.32488910e-02, 0. }, + (const float []){ + 5.94241012e-01, 4.19760926e-01, -1.07134366e-02, 0. }, + }, + + [LC3_SRATE_24K] = { + (const float []){ + 3.98969559e-01, 5.14250861e-01, 1.00438297e-01, -1.27889396e-02, + -1.57228008e-03, 0. }, + (const float []){ + 3.94863491e-01, 5.12381921e-01, 1.04319493e-01, -1.09199996e-02, + -1.34740833e-03, 0. }, + (const float []){ + 3.90984448e-01, 5.10605352e-01, 1.07983252e-01, -9.14343107e-03, + -1.13212462e-03, 0. }, + (const float []){ + 3.87309389e-01, 5.08912208e-01, 1.11451738e-01, -7.45028713e-03, + -9.25551405e-04, 0. }, + }, + + [LC3_SRATE_32K] = { + (const float []){ + 2.98237945e-01, 4.65280920e-01, 2.10599743e-01, 3.76678038e-02, + -1.01569616e-02, -2.53588100e-03, -3.18294617e-04, 0. }, + (const float []){ + 2.94383415e-01, 4.61929400e-01, 2.12946577e-01, 4.06617500e-02, + -8.69327230e-03, -2.17830711e-03, -2.74288806e-04, 0. }, + (const float []){ + 2.90743921e-01, 4.58746191e-01, 2.15145697e-01, 4.35010477e-02, + -7.29549535e-03, -1.83439564e-03, -2.31692019e-04, 0. }, + (const float []){ + 2.87297585e-01, 4.55714889e-01, 2.17212695e-01, 4.62008888e-02, + -5.95746380e-03, -1.50293428e-03, -1.90385191e-04, 0. }, + }, + + [LC3_SRATE_48K] = { + (const float []){ + 1.98136374e-01, 3.52449490e-01, 2.51369527e-01, 1.42414624e-01, + 5.70473102e-02, 9.29336624e-03, -7.22602537e-03, -3.17267989e-03, + -1.12183596e-03, -2.90295724e-04, -4.27081559e-05, 0. }, + (const float []){ + 1.95070943e-01, 3.48466041e-01, 2.50998846e-01, 1.44116741e-01, + 5.92894732e-02, 1.10892383e-02, -6.19290811e-03, -2.72670551e-03, + -9.66712583e-04, -2.50810092e-04, -3.69993877e-05, 0. }, + (const float []){ + 1.92181006e-01, 3.44694556e-01, 2.50622009e-01, 1.45710245e-01, + 6.14113213e-02, 1.27994140e-02, -5.20372109e-03, -2.29732451e-03, + -8.16560813e-04, -2.12385575e-04, -3.14127133e-05, 0. }, + (const float []){ + 1.89448531e-01, 3.41113925e-01, 2.50240688e-01, 1.47206563e-01, + 6.34247723e-02, 1.44320343e-02, -4.25444914e-03, -1.88308147e-03, + -6.70961906e-04, -1.74936334e-04, -2.59386474e-05, 0. }, + } +}; + +const float *lc3_ltpf_cden[LC3_NUM_SRATE][4] = { + + [LC3_SRATE_8K] = { + (const float []){ + 2.09880463e-01, 5.83527575e-01, 2.09880463e-01, 0.00000000e+00 }, + (const float []){ + 1.06999186e-01, 5.50075002e-01, 3.35690625e-01, 6.69885837e-03 }, + (const float []){ + 3.96711478e-02, 4.59220930e-01, 4.59220930e-01, 3.96711478e-02 }, + (const float []){ + 6.69885837e-03, 3.35690625e-01, 5.50075002e-01, 1.06999186e-01 }, + }, + + [LC3_SRATE_16K] = { + (const float []){ + 2.09880463e-01, 5.83527575e-01, 2.09880463e-01, 0.00000000e+00 }, + (const float []){ + 1.06999186e-01, 5.50075002e-01, 3.35690625e-01, 6.69885837e-03 }, + (const float []){ + 3.96711478e-02, 4.59220930e-01, 4.59220930e-01, 3.96711478e-02 }, + (const float []){ + 6.69885837e-03, 3.35690625e-01, 5.50075002e-01, 1.06999186e-01 }, + }, + + [LC3_SRATE_24K] = { + (const float []){ + 6.32223163e-02, 2.50730961e-01, 3.71390943e-01, 2.50730961e-01, + 6.32223163e-02, 0.00000000e+00 }, + (const float []){ + 3.45927217e-02, 1.98651560e-01, 3.62641173e-01, 2.98675055e-01, + 1.01309287e-01, 4.26354371e-03 }, + (const float []){ + 1.53574678e-02, 1.47434488e-01, 3.37425955e-01, 3.37425955e-01, + 1.47434488e-01, 1.53574678e-02 }, + (const float []){ + 4.26354371e-03, 1.01309287e-01, 2.98675055e-01, 3.62641173e-01, + 1.98651560e-01, 3.45927217e-02 }, + }, + + [LC3_SRATE_32K] = { + (const float []){ + 2.90040188e-02, 1.12985742e-01, 2.21202403e-01, 2.72390947e-01, + 2.21202403e-01, 1.12985742e-01, 2.90040188e-02, 0.00000000e+00 }, + (const float []){ + 1.70315342e-02, 8.72250379e-02, 1.96140776e-01, 2.68923798e-01, + 2.42499910e-01, 1.40577336e-01, 4.47487717e-02, 3.12703024e-03 }, + (const float []){ + 8.56367375e-03, 6.42622294e-02, 1.68767671e-01, 2.58744594e-01, + 2.58744594e-01, 1.68767671e-01, 6.42622294e-02, 8.56367375e-03 }, + (const float []){ + 3.12703024e-03, 4.47487717e-02, 1.40577336e-01, 2.42499910e-01, + 2.68923798e-01, 1.96140776e-01, 8.72250379e-02, 1.70315342e-02 }, + }, + + [LC3_SRATE_48K] = { + (const float []){ + 1.08235939e-02, 3.60896922e-02, 7.67640147e-02, 1.24153058e-01, + 1.62759644e-01, 1.77677142e-01, 1.62759644e-01, 1.24153058e-01, + 7.67640147e-02, 3.60896922e-02, 1.08235939e-02, 0.00000000e+00 }, + (const float []){ + 7.04140493e-03, 2.81970232e-02, 6.54704494e-02, 1.12464799e-01, + 1.54841896e-01, 1.76712238e-01, 1.69150721e-01, 1.35290158e-01, + 8.85142501e-02, 4.49935385e-02, 1.55761371e-02, 2.03972196e-03 }, + (const float []){ + 4.14699847e-03, 2.13575731e-02, 5.48273558e-02, 1.00497144e-01, + 1.45606034e-01, 1.73843984e-01, 1.73843984e-01, 1.45606034e-01, + 1.00497144e-01, 5.48273558e-02, 2.13575731e-02, 4.14699847e-03 }, + (const float []){ + 2.03972196e-03, 1.55761371e-02, 4.49935385e-02, 8.85142501e-02, + 1.35290158e-01, 1.69150721e-01, 1.76712238e-01, 1.54841896e-01, + 1.12464799e-01, 6.54704494e-02, 2.81970232e-02, 7.04140493e-03 }, + } +}; + + +/** + * Spectral Data Arithmetic Coding + * The number of bits are given at 2048th of bits + * + * The dimensions of the lookup table are set as following : + * 1: Rate selection + * 2: Half spectrum selection (1st half / 2nd half) + * 3: State of the arithmetic coder + * 4: Number of msb bits (significant - 2), limited to 3 + * + * table[r][h][s][k] = table(normative)[s + h*256 + r*512 + k*1024] + */ + +const uint8_t lc3_spectrum_lookup[2][2][256][4] = { + + { { { 1,13, 0, 0 }, { 39,13, 0, 0 }, { 7,13, 0, 0 }, { 25,13, 0, 0 }, + { 22,13, 0, 0 }, { 22,13, 0, 0 }, { 28,13, 0, 0 }, { 22,13, 0, 0 }, + { 22,60, 0, 0 }, { 22,60, 0, 0 }, { 22,60, 0, 0 }, { 28,60, 0, 0 }, + { 28,60, 0, 0 }, { 28,60,13, 0 }, { 34,60,13, 0 }, { 31,16,13, 0 }, + { 31,16,13, 0 }, { 40, 0, 0, 0 }, { 43, 0, 0, 0 }, { 46, 0, 0, 0 }, + { 49, 0, 0, 0 }, { 52, 0, 0, 0 }, { 14, 0, 0, 0 }, { 17, 0, 0, 0 }, + { 36, 0, 0, 0 }, { 36, 0, 0, 0 }, { 36, 0, 0, 0 }, { 38, 0, 0, 0 }, + { 0, 0, 0, 0 }, { 57, 0, 0, 0 }, { 38,13, 0, 0 }, { 22,60, 0, 0 }, + { 0, 0, 0, 0 }, { 8, 0, 0, 0 }, { 9, 0, 0, 0 }, { 11, 0, 0, 0 }, + { 47, 0, 0, 0 }, { 14, 0, 0, 0 }, { 14, 0, 0, 0 }, { 17, 0, 0, 0 }, + { 36, 0, 0, 0 }, { 36, 0, 0, 0 }, { 36, 0, 0, 0 }, { 38, 0, 0, 0 }, + { 59, 0, 0, 0 }, { 59, 0, 0, 0 }, { 38,13, 0, 0 }, { 22,60, 0, 0 }, + { 22,60, 0, 0 }, { 26, 0, 0, 0 }, { 46, 0, 0, 0 }, { 29, 0, 0, 0 }, + { 30, 0, 0, 0 }, { 32, 0, 0, 0 }, { 33, 0, 0, 0 }, { 35, 0, 0, 0 }, + { 36, 0, 0, 0 }, { 36, 0, 0, 0 }, { 36, 0, 0, 0 }, { 38, 0, 0, 0 }, + { 0,13, 0, 0 }, { 59,13, 0, 0 }, { 23,13, 0, 0 }, { 22,60, 0, 0 }, + { 46,60, 0, 0 }, { 46, 0, 0, 0 }, { 45, 0, 0, 0 }, { 47, 0, 0, 0 }, + { 48, 0, 0, 0 }, { 50, 0, 0, 0 }, { 50, 0, 0, 0 }, { 18, 0, 0, 0 }, + { 54, 0, 0, 0 }, { 54, 0, 0, 0 }, { 54, 0, 0, 0 }, { 38, 0, 0, 0 }, + { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 22,60, 0, 0 }, + { 0,60, 0, 0 }, { 62, 0, 0, 0 }, { 63, 0, 0, 0 }, { 3, 0, 0, 0 }, + { 33, 0, 0, 0 }, { 2, 0, 0, 0 }, { 2, 0, 0, 0 }, { 61, 0, 0, 0 }, + { 20, 0, 0, 0 }, { 20, 0, 0, 0 }, { 20,13, 0, 0 }, { 21,13, 0, 0 }, + { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 39,13, 0, 0 }, { 28,60, 0, 0 }, + { 28,60, 0, 0 }, { 63, 0, 0, 0 }, { 63, 0, 0, 0 }, { 3, 0, 0, 0 }, + { 33, 0, 0, 0 }, { 2, 0, 0, 0 }, { 2, 0, 0, 0 }, { 61, 0, 0, 0 }, + { 38, 0, 0, 0 }, { 38, 0, 0, 0 }, { 38,13, 0, 0 }, { 21,13, 0, 0 }, + { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 39,13, 0, 0 }, { 28,60, 0, 0 }, + { 28,60, 0, 0 }, { 6, 0, 0, 0 }, { 6, 0, 0, 0 }, { 6, 0, 0, 0 }, + { 2, 0, 0, 0 }, { 18, 0, 0, 0 }, { 61, 0, 0, 0 }, { 20, 0, 0, 0 }, + { 21, 0, 0, 0 }, { 21, 0, 0, 0 }, { 21,13, 0, 0 }, { 59,13, 0, 0 }, + { 39,13, 0, 0 }, { 39,13, 0, 0 }, { 7,13, 0, 0 }, { 34,60,13, 0 }, + { 34,60,13, 0 }, { 34,60,13, 0 }, { 34,60,13, 0 }, { 34,60,13, 0 }, + { 34,60,13, 0 }, { 34,60,13, 0 }, { 34,60,13, 0 }, { 34,60,13, 0 }, + { 34,60,13, 0 }, { 34,60,13, 0 }, { 34,60,13, 0 }, { 34,60,13, 0 }, + { 34,60,13, 0 }, { 34,60,13, 0 }, { 34,60,13, 0 }, { 34,60,13, 0 }, + { 34,60,13, 0 }, { 51, 0, 0, 0 }, { 51, 0, 0, 0 }, { 51, 0, 0, 0 }, + { 53, 0, 0, 0 }, { 54, 0, 0, 0 }, { 20, 0, 0, 0 }, { 38, 0, 0, 0 }, + { 38, 0, 0, 0 }, { 57, 0, 0, 0 }, { 39,13, 0, 0 }, { 39,13, 0, 0 }, + { 39,13, 0, 0 }, { 7,13, 0, 0 }, { 24,13, 0, 0 }, { 34,60,13, 0 }, + { 4,60, 0, 0 }, { 4,60, 0, 0 }, { 4,60, 0, 0 }, { 4,60, 0, 0 }, + { 4,60, 0, 0 }, { 4,60, 0, 0 }, { 4,60, 0, 0 }, { 4,60, 0, 0 }, + { 4,60, 0, 0 }, { 4,60, 0, 0 }, { 4,60, 0, 0 }, { 4,60, 0, 0 }, + { 4,60, 0, 0 }, { 4,60, 0, 0 }, { 4,60, 0, 0 }, { 4,60, 0, 0 }, + { 4,60, 0, 0 }, { 4, 0, 0, 0 }, { 4, 0, 0, 0 }, { 4, 0, 0, 0 }, + { 4, 0, 0, 0 }, { 56, 0, 0, 0 }, { 38, 0, 0, 0 }, { 57, 0, 0, 0 }, + { 57,13, 0, 0 }, { 59,13, 0, 0 }, { 7,13, 0, 0 }, { 7,13, 0, 0 }, + { 7,13, 0, 0 }, { 42,13, 0, 0 }, { 42,13, 0, 0 }, { 34,60,13, 0 }, + { 0,60,13, 0 }, { 0,60,13, 0 }, { 0,60,13, 0 }, { 0,60,13, 0 }, + { 0,60,13, 0 }, { 0,60,13, 0 }, { 0,60,13, 0 }, { 0,60,13, 0 }, + { 0,60,13, 0 }, { 0,60,13, 0 }, { 0,60,13, 0 }, { 0,60,13, 0 }, + { 0,60,13, 0 }, { 0,60,13, 0 }, { 0,60,13, 0 }, { 0,60,13, 0 }, + { 0,60,13, 0 }, { 5, 0, 0, 0 }, { 4, 0, 0, 0 }, { 4, 0, 0, 0 }, + { 5, 0, 0, 0 }, { 21, 0, 0, 0 }, { 21, 0, 0, 0 }, { 59,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 7,13, 0, 0 }, + { 25,13, 0, 0 }, { 25,13, 0, 0 }, { 25,13, 0, 0 }, { 34,60,13, 0 }, + { 4,13, 0, 0 }, { 4,13, 0, 0 }, { 4,13, 0, 0 }, { 4,13, 0, 0 }, + { 5,13, 0, 0 }, { 23,13, 0, 0 }, { 23,13, 0, 0 }, { 39,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 42,13, 0, 0 }, + { 25,13, 0, 0 }, { 25,13, 0, 0 }, { 22,13, 0, 0 }, { 31,60,13, 0 }, + { 31,60,13, 0 }, { 39,60, 0, 0 }, { 39,60, 0, 0 }, { 39,60, 0, 0 }, + { 39,60, 0, 0 }, { 7,60, 0, 0 }, { 7,60, 0, 0 }, { 42,60, 0, 0 }, + { 0,60, 0, 0 }, { 25,60, 0, 0 }, { 22,60, 0, 0 }, { 22,60, 0, 0 }, + { 22,60, 0, 0 }, { 28,60, 0, 0 }, { 34,60, 0, 0 }, { 31,16,13, 0 } }, + + { { 55, 0,13, 0 }, { 55, 0,13, 0 }, { 55, 0,13, 0 }, { 55, 0,13, 0 }, + { 55, 0,13, 0 }, { 55, 0,13, 0 }, { 55, 0,13, 0 }, { 55, 0,13, 0 }, + { 55, 0,13, 0 }, { 55, 0,13, 0 }, { 55, 0,13, 0 }, { 55, 0,13, 0 }, + { 55, 0,13, 0 }, { 55, 0,13, 0 }, { 55, 0,13, 0 }, { 55, 0,13, 0 }, + { 55, 0,13, 0 }, { 55, 0, 0, 0 }, { 40, 0, 0, 0 }, { 8, 0, 0, 0 }, + { 9, 0, 0, 0 }, { 49, 0, 0, 0 }, { 49, 0, 0, 0 }, { 52, 0, 0, 0 }, + { 17, 0, 0, 0 }, { 17, 0, 0, 0 }, { 17, 0, 0, 0 }, { 4,13, 0, 0 }, + { 0,13, 0, 0 }, { 20,13, 0, 0 }, { 17, 0, 0, 0 }, { 60,13,60,13 }, + { 40, 0, 0,13 }, { 40, 0, 0, 0 }, { 8, 0, 0, 0 }, { 43, 0, 0, 0 }, + { 27, 0, 0, 0 }, { 49, 0, 0, 0 }, { 49, 0, 0, 0 }, { 14, 0, 0, 0 }, + { 17, 0, 0, 0 }, { 17, 0, 0, 0 }, { 17, 0, 0, 0 }, { 36, 0, 0, 0 }, + { 42,13, 0, 0 }, { 42,13, 0, 0 }, { 17, 0, 0, 0 }, { 57,60,13, 0 }, + { 57, 0,13, 0 }, { 40, 0, 0, 0 }, { 8, 0, 0, 0 }, { 26, 0, 0, 0 }, + { 27, 0, 0, 0 }, { 49, 0, 0, 0 }, { 12, 0, 0, 0 }, { 14, 0, 0, 0 }, + { 17, 0, 0, 0 }, { 17, 0, 0, 0 }, { 17, 0, 0, 0 }, { 36, 0, 0, 0 }, + { 0, 0,13, 0 }, { 38, 0,13, 0 }, { 36,13, 0, 0 }, { 1,60, 0, 0 }, + { 8,60, 0, 0 }, { 8, 0, 0, 0 }, { 43, 0, 0, 0 }, { 9, 0, 0, 0 }, + { 11, 0, 0, 0 }, { 49, 0, 0, 0 }, { 12, 0, 0, 0 }, { 14, 0, 0, 0 }, + { 14, 0,13, 0 }, { 33, 0,13, 0 }, { 50, 0,13, 0 }, { 50, 0, 0, 0 }, + { 50, 0,13, 0 }, { 61, 0,13, 0 }, { 36,13, 0, 0 }, { 39,60, 0, 0 }, + { 8,60, 0, 0 }, { 8, 0, 0, 0 }, { 43, 0, 0, 0 }, { 46, 0, 0, 0 }, + { 49, 0, 0, 0 }, { 52, 0, 0, 0 }, { 30, 0, 0, 0 }, { 14, 0, 0, 0 }, + { 14, 0,13, 0 }, { 33, 0,13, 0 }, { 50, 0,13, 0 }, { 50, 0,13, 0 }, + { 50,13,13, 0 }, { 50,13, 0, 0 }, { 18,13,13, 0 }, { 25,60,13, 0 }, + { 8,60,13,13 }, { 8, 0, 0,13 }, { 43, 0, 0,13 }, { 46, 0, 0,13 }, + { 49, 0, 0,13 }, { 52, 0, 0, 0 }, { 30, 0, 0, 0 }, { 14, 0, 0, 0 }, + { 14, 0, 0, 0 }, { 18, 0,60, 0 }, { 5, 0, 0,13 }, { 5, 0, 0,13 }, + { 5, 0, 0,13 }, { 61,13, 0,13 }, { 18,13,13, 0 }, { 23,13,60, 0 }, + { 43,13, 0,13 }, { 43, 0, 0,13 }, { 43, 0, 0,13 }, { 9, 0, 0,13 }, + { 49, 0, 0,13 }, { 52, 0, 0, 0 }, { 3, 0, 0, 0 }, { 14, 0, 0, 0 }, + { 14, 0, 0, 0 }, { 50, 0, 0, 0 }, { 50,13,13, 0 }, { 50,13,13, 0 }, + { 50,13,13, 0 }, { 61, 0, 0, 0 }, { 17,13,13, 0 }, { 24,60,13, 0 }, + { 43,60,13, 0 }, { 43,60,13, 0 }, { 43,60,13, 0 }, { 43,60,13, 0 }, + { 43,60,13, 0 }, { 43,60,13, 0 }, { 43,60,13, 0 }, { 43,60,13, 0 }, + { 43,60,13, 0 }, { 43,60,13, 0 }, { 43,60,13, 0 }, { 43,60,13, 0 }, + { 43,60,13, 0 }, { 43,60,13, 0 }, { 43,60,13, 0 }, { 43,60,13, 0 }, + { 43,60,13, 0 }, { 43, 0, 0, 0 }, { 43, 0,19, 0 }, { 9, 0, 0, 0 }, + { 11, 0, 0, 0 }, { 52, 0, 0, 0 }, { 52, 0, 0, 0 }, { 14, 0, 0, 0 }, + { 14, 0, 0, 0 }, { 17, 0, 0, 0 }, { 61,13, 0, 0 }, { 61,13, 0, 0 }, + { 61,13, 0, 0 }, { 54, 0, 0, 0 }, { 17, 0,13,13 }, { 39,13,13, 0 }, + { 45,13,13, 0 }, { 45,13,13, 0 }, { 45,13,13, 0 }, { 45,13,13, 0 }, + { 45,13,13, 0 }, { 45,13,13, 0 }, { 45,13,13, 0 }, { 45,13,13, 0 }, + { 45,13,13, 0 }, { 45,13,13, 0 }, { 45,13,13, 0 }, { 45,13,13, 0 }, + { 45,13,13, 0 }, { 45,13,13, 0 }, { 45,13,13, 0 }, { 45,13,13, 0 }, + { 45,13,13, 0 }, { 45, 0,13, 0 }, { 44, 0,13, 0 }, { 27, 0, 0, 0 }, + { 29, 0, 0, 0 }, { 52, 0, 0, 0 }, { 48, 0, 0, 0 }, { 52, 0, 0, 0 }, + { 52, 0, 0, 0 }, { 17, 0, 0, 0 }, { 17, 0, 0, 0 }, { 17, 0,19, 0 }, + { 17, 0,13, 0 }, { 2, 0,13, 0 }, { 17, 0,13, 0 }, { 7,13, 0, 0 }, + { 27, 0, 0,13 }, { 27, 0, 0,13 }, { 27, 0, 0,13 }, { 27, 0, 0,13 }, + { 27, 0, 0,13 }, { 27, 0, 0,13 }, { 27, 0, 0,13 }, { 27, 0, 0,13 }, + { 27, 0, 0,13 }, { 27, 0, 0,13 }, { 27, 0, 0,13 }, { 27, 0, 0,13 }, + { 27, 0, 0,13 }, { 27, 0, 0,13 }, { 27, 0, 0,13 }, { 27, 0, 0,13 }, + { 27, 0, 0,13 }, { 27, 0, 0,13 }, { 9, 0, 0,13 }, { 27, 0, 0,13 }, + { 27, 0, 0,13 }, { 12, 0, 0,13 }, { 52, 0, 0,13 }, { 14, 0, 0,13 }, + { 14, 0, 0,13 }, { 58, 0, 0,13 }, { 41, 0, 0,13 }, { 41, 0, 0,13 }, + { 41, 0, 0,13 }, { 6, 0, 0,13 }, { 17,60, 0,13 }, { 37, 0,19,13 }, + { 9, 0, 0,13 }, { 9,16, 0,13 }, { 9, 0, 0,13 }, { 27, 0, 0,13 }, + { 11, 0, 0,13 }, { 49, 0, 0, 0 }, { 12, 0, 0, 0 }, { 52, 0, 0, 0 }, + { 14, 0, 0, 0 }, { 14, 0, 0, 0 }, { 14, 0, 0, 0 }, { 50, 0, 0, 0 }, + { 0, 0, 0,13 }, { 53, 0, 0,13 }, { 17, 0, 0,13 }, { 28, 0,13, 0 }, + { 52, 0,13, 0 }, { 52, 0,13, 0 }, { 49, 0,13, 0 }, { 52, 0, 0, 0 }, + { 12, 0, 0, 0 }, { 52, 0, 0, 0 }, { 30, 0, 0, 0 }, { 14, 0, 0, 0 }, + { 14, 0, 0, 0 }, { 17, 0, 0, 0 }, { 2, 0, 0, 0 }, { 2, 0, 0, 0 }, + { 2, 0, 0, 0 }, { 38, 0, 0, 0 }, { 38, 0, 0, 0 }, { 34, 0, 0, 0 } } }, + + { { { 31,16,60,13 }, { 34,16,13, 0 }, { 34,16,13, 0 }, { 31,16,13, 0 }, + { 31,16,13, 0 }, { 31,16,13, 0 }, { 31,16,13, 0 }, { 19,16,60, 0 }, + { 19,16,60, 0 }, { 19,16,60, 0 }, { 19,16,60, 0 }, { 19,16,60, 0 }, + { 19,16,60, 0 }, { 19,16,60, 0 }, { 31,16,60,13 }, { 19,37,16,60 }, + { 44, 0, 0,60 }, { 44, 0, 0, 0 }, { 62, 0, 0, 0 }, { 30, 0, 0, 0 }, + { 32, 0, 0, 0 }, { 58, 0, 0, 0 }, { 35, 0, 0, 0 }, { 36, 0, 0, 0 }, + { 36, 0, 0, 0 }, { 38,13, 0, 0 }, { 0,13, 0, 0 }, { 59,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 39,13, 0, 0 }, { 34,60,13, 0 }, + { 34, 0,13, 0 }, { 45, 0, 0, 0 }, { 47, 0, 0, 0 }, { 48, 0, 0, 0 }, + { 33, 0, 0, 0 }, { 35, 0, 0, 0 }, { 35, 0, 0, 0 }, { 36, 0, 0, 0 }, + { 38,13, 0, 0 }, { 38,13, 0, 0 }, { 38,13, 0, 0 }, { 59,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 39,13, 0, 0 }, { 34,60,13, 0 }, + { 34, 0,13, 0 }, { 62, 0, 0, 0 }, { 30, 0, 0, 0 }, { 15, 0, 0, 0 }, + { 50, 0, 0, 0 }, { 53, 0, 0, 0 }, { 53, 0, 0, 0 }, { 54,13, 0, 0 }, + { 21,13, 0, 0 }, { 21,13, 0, 0 }, { 21,13, 0, 0 }, { 59,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 34,60,13, 0 }, + { 30, 0,13, 0 }, { 30, 0, 0, 0 }, { 48, 0, 0, 0 }, { 33, 0, 0, 0 }, + { 58, 0, 0, 0 }, { 18, 0, 0, 0 }, { 18, 0, 0, 0 }, { 56,13, 0, 0 }, + { 23,13, 0, 0 }, { 23,13, 0, 0 }, { 23,13, 0, 0 }, { 59,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 24,13, 0, 0 }, { 34,60,13, 0 }, + { 34, 0,13, 0 }, { 6, 0, 0, 0 }, { 6, 0, 0, 0 }, { 58, 0, 0, 0 }, + { 53, 0, 0, 0 }, { 54, 0, 0, 0 }, { 54, 0, 0, 0 }, { 21,13, 0, 0 }, + { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 39,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 42,60, 0, 0 }, { 34,16,13, 0 }, + { 6, 0,13, 0 }, { 6, 0, 0, 0 }, { 33, 0, 0, 0 }, { 58, 0, 0, 0 }, + { 53, 0, 0, 0 }, { 54, 0, 0, 0 }, { 61, 0, 0, 0 }, { 21,13, 0, 0 }, + { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 39,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 42,60, 0, 0 }, { 34,16,13, 0 }, + { 34, 0,13, 0 }, { 51, 0, 0, 0 }, { 51, 0, 0, 0 }, { 53, 0, 0, 0 }, + { 54, 0, 0, 0 }, { 56,13, 0, 0 }, { 56,13, 0, 0 }, { 57,13, 0, 0 }, + { 39,13, 0, 0 }, { 39,13, 0, 0 }, { 39,13, 0, 0 }, { 7,13, 0, 0 }, + { 42,13, 0, 0 }, { 42,13, 0, 0 }, { 25,60, 0, 0 }, { 31,16,13, 0 }, + { 31, 0,13, 0 }, { 31, 0,13, 0 }, { 31, 0,13, 0 }, { 31, 0,13, 0 }, + { 31, 0,13, 0 }, { 31, 0,13, 0 }, { 31, 0,13, 0 }, { 31, 0,13, 0 }, + { 31, 0,13, 0 }, { 31, 0,13, 0 }, { 31, 0,13, 0 }, { 31, 0,13, 0 }, + { 31, 0,13, 0 }, { 31, 0,13, 0 }, { 31, 0,13, 0 }, { 31, 0,13, 0 }, + { 31, 0,13, 0 }, { 4, 0, 0, 0 }, { 4, 0, 0, 0 }, { 4, 0, 0, 0 }, + { 5,13, 0, 0 }, { 23,13, 0, 0 }, { 23,13, 0, 0 }, { 39,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 42,13, 0, 0 }, + { 25,13, 0, 0 }, { 25,13, 0, 0 }, { 22,60, 0, 0 }, { 31,16,60, 0 }, + { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, + { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, + { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, + { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, + { 31,13, 0, 0 }, { 5,13, 0, 0 }, { 5,13, 0, 0 }, { 5,13, 0, 0 }, + { 5,13, 0, 0 }, { 57,13, 0, 0 }, { 57,13, 0, 0 }, { 39,13, 0, 0 }, + { 24,13, 0, 0 }, { 24,13, 0, 0 }, { 24,13, 0, 0 }, { 42,13, 0, 0 }, + { 22,13, 0, 0 }, { 22,60, 0, 0 }, { 28,60,13, 0 }, { 31,16,60, 0 }, + { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, + { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, + { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, + { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, { 31,13, 0, 0 }, + { 31,13, 0, 0 }, { 41,13, 0, 0 }, { 41,13, 0, 0 }, { 41,13, 0, 0 }, + { 41,13, 0, 0 }, { 39,13, 0, 0 }, { 39,13, 0, 0 }, { 7,13, 0, 0 }, + { 42,13, 0, 0 }, { 42,13, 0, 0 }, { 42,13, 0, 0 }, { 25,13, 0, 0 }, + { 28,13, 0, 0 }, { 28,60, 0, 0 }, { 28,60,13, 0 }, { 31,16,60,13 }, + { 31,13, 0, 0 }, { 41,13, 0, 0 }, { 41,13, 0, 0 }, { 41,13, 0, 0 }, + { 41,13, 0, 0 }, { 39,13, 0, 0 }, { 39,13, 0, 0 }, { 24,13, 0, 0 }, + { 25,60, 0, 0 }, { 25,60, 0, 0 }, { 25,60, 0, 0 }, { 22,60, 0, 0 }, + { 28,60, 0, 0 }, { 28,60, 0, 0 }, { 34,60,13, 0 }, { 31,16,60,13 }, + { 31,60,13,13 }, { 10,60,13, 0 }, { 10,60,13, 0 }, { 10,60,13, 0 }, + { 10,60,13, 0 }, { 10,60,13, 0 }, { 10,60,13, 0 }, { 28,60,13, 0 }, + { 34,60,13, 0 }, { 34,60,13, 0 }, { 34,16,13, 0 }, { 34,16,13, 0 }, + { 34,16,60, 0 }, { 34,16,60, 0 }, { 31,16,60, 0 }, { 19,37,16,13 } }, + + { { 8, 0,16, 0 }, { 8, 0,16, 0 }, { 8, 0,16, 0 }, { 8, 0,16, 0 }, + { 8, 0,16, 0 }, { 8, 0,16, 0 }, { 8, 0,16, 0 }, { 8, 0,16, 0 }, + { 8, 0,16, 0 }, { 8, 0,16, 0 }, { 8, 0,16, 0 }, { 8, 0,16, 0 }, + { 8, 0,16, 0 }, { 8, 0,16, 0 }, { 8, 0,16, 0 }, { 8, 0,16, 0 }, + { 8, 0,16, 0 }, { 8, 0, 0, 0 }, { 9, 0, 0, 0 }, { 11, 0, 0, 0 }, + { 47, 0, 0, 0 }, { 32, 0, 0, 0 }, { 50, 0, 0, 0 }, { 18, 0, 0, 0 }, + { 18, 0, 0, 0 }, { 20, 0, 0, 0 }, { 21, 0, 0, 0 }, { 21, 0, 0, 0 }, + { 21,13, 0, 0 }, { 39,13, 0, 0 }, { 59,13, 0, 0 }, { 34,16,60, 0 }, + { 26, 0, 0, 0 }, { 26, 0, 0, 0 }, { 27, 0, 0, 0 }, { 29, 0, 0, 0 }, + { 30, 0, 0, 0 }, { 33, 0, 0, 0 }, { 50, 0, 0, 0 }, { 18, 0, 0, 0 }, + { 18, 0, 0, 0 }, { 20, 0, 0, 0 }, { 57, 0, 0, 0 }, { 57,13, 0, 0 }, + { 57,13, 0, 0 }, { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 34,16,60, 0 }, + { 27, 0, 0, 0 }, { 27, 0, 0, 0 }, { 11, 0, 0, 0 }, { 12, 0, 0, 0 }, + { 48, 0, 0, 0 }, { 50, 0, 0, 0 }, { 58, 0, 0, 0 }, { 61, 0, 0, 0 }, + { 61, 0, 0, 0 }, { 56, 0, 0, 0 }, { 57,13, 0, 0 }, { 57,13, 0, 0 }, + { 57,13, 0, 0 }, { 59,13, 0, 0 }, { 39,13, 0, 0 }, { 34,16,60, 0 }, + { 45, 0, 0, 0 }, { 45, 0, 0, 0 }, { 12, 0, 0, 0 }, { 30, 0, 0, 0 }, + { 32, 0, 0, 0 }, { 2, 0, 0, 0 }, { 2, 0, 0, 0 }, { 61, 0, 0, 0 }, + { 38, 0, 0, 0 }, { 38, 0, 0, 0 }, { 38,13, 0, 0 }, { 57,13, 0, 0 }, + { 0,13, 0, 0 }, { 59,13, 0, 0 }, { 39,13, 0, 0 }, { 34,16,60, 0 }, + { 63, 0, 0, 0 }, { 63, 0, 0, 0 }, { 3, 0, 0, 0 }, { 32, 0, 0, 0 }, + { 58, 0, 0, 0 }, { 18, 0, 0, 0 }, { 18, 0, 0, 0 }, { 20, 0, 0, 0 }, + { 21, 0, 0, 0 }, { 21, 0, 0, 0 }, { 21,13, 0, 0 }, { 59,13, 0, 0 }, + { 39,13, 0, 0 }, { 39,13, 0, 0 }, { 7,13,13, 0 }, { 31,16,60, 0 }, + { 31, 0, 0, 0 }, { 3, 0, 0, 0 }, { 3, 0, 0, 0 }, { 33, 0, 0, 0 }, + { 58, 0, 0, 0 }, { 18, 0, 0, 0 }, { 18, 0, 0, 0 }, { 20, 0, 0, 0 }, + { 21, 0, 0, 0 }, { 21, 0, 0, 0 }, { 21,13, 0, 0 }, { 59,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 7,13,13, 0 }, { 31,16,60, 0 }, + { 6, 0, 0, 0 }, { 6, 0, 0, 0 }, { 51, 0, 0, 0 }, { 51, 0, 0, 0 }, + { 53, 0, 0, 0 }, { 54, 0, 0, 0 }, { 54, 0, 0, 0 }, { 38, 0, 0, 0 }, + { 57,13, 0, 0 }, { 57,13, 0, 0 }, { 57,13, 0, 0 }, { 39,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 42,60,13, 0 }, { 31,16,60, 0 }, + { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, + { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, + { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, + { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, + { 31, 0, 0, 0 }, { 51, 0, 0, 0 }, { 53, 0, 0, 0 }, { 53, 0, 0, 0 }, + { 54, 0, 0, 0 }, { 56, 0, 0, 0 }, { 56, 0, 0, 0 }, { 57,13, 0, 0 }, + { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 59,13, 0, 0 }, { 7,13, 0, 0 }, + { 24,13, 0, 0 }, { 24,13, 0, 0 }, { 25,60,13, 0 }, { 31,16,60, 0 }, + { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, + { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, + { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, + { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, { 31, 0, 0, 0 }, + { 31, 0, 0, 0 }, { 4, 0, 0, 0 }, { 4, 0, 0, 0 }, { 4, 0, 0, 0 }, + { 54, 0, 0, 0 }, { 21,13, 0, 0 }, { 21, 0, 0, 0 }, { 57,13, 0, 0 }, + { 39,13, 0, 0 }, { 39,13, 0, 0 }, { 39,13, 0, 0 }, { 7,13, 0, 0 }, + { 42,13,13, 0 }, { 42,13,13, 0 }, { 22,60,13, 0 }, { 31,16,60, 0 }, + { 31,16, 0, 0 }, { 31,16, 0, 0 }, { 31,16, 0, 0 }, { 31,16, 0, 0 }, + { 31,16, 0, 0 }, { 31,16, 0, 0 }, { 31,16, 0, 0 }, { 31,16, 0, 0 }, + { 31,16, 0, 0 }, { 31,16, 0, 0 }, { 31,16, 0, 0 }, { 31,16, 0, 0 }, + { 31,16, 0, 0 }, { 31,16, 0, 0 }, { 31,16, 0, 0 }, { 31,16, 0, 0 }, + { 31,16, 0, 0 }, { 5, 0, 0, 0 }, { 5, 0, 0, 0 }, { 5, 0, 0, 0 }, + { 5,13, 0, 0 }, { 23,13, 0, 0 }, { 23,13, 0, 0 }, { 59,13, 0, 0 }, + { 7,13, 0, 0 }, { 7,13, 0, 0 }, { 7,13,13, 0 }, { 42,13,13, 0 }, + { 22,60,13, 0 }, { 22,60,13, 0 }, { 28,60,13, 0 }, { 31,16,60, 0 }, + { 31,13, 0, 0 }, { 4,13, 0, 0 }, { 4,13, 0, 0 }, { 4,13, 0, 0 }, + { 5,13, 0, 0 }, { 23,13, 0, 0 }, { 23,13, 0, 0 }, { 39,13,13, 0 }, + { 24,60,13, 0 }, { 24,60,13, 0 }, { 24,60,13, 0 }, { 25,60,13, 0 }, + { 28,60,13, 0 }, { 28,60,13, 0 }, { 34,16,13, 0 }, { 31,16,60, 0 }, + { 31,16,13, 0 }, { 10,16,13, 0 }, { 10,16,13, 0 }, { 10,16,13, 0 }, + { 10,16,13, 0 }, { 10,16,60, 0 }, { 10,16,60, 0 }, { 28,16,60, 0 }, + { 34,16,60, 0 }, { 34,16,60, 0 }, { 34,16,60, 0 }, { 31,16,60, 0 }, + { 31,16,60, 0 }, { 31,16,60, 0 }, { 31,16,60, 0 }, { 19,37,60, 0 } } } +}; + +const struct lc3_ac_model lc3_spectrum_models[] = { + + { { { 0, 1 }, { 1, 1 }, { 2, 175 }, { 177, 48 }, + { 225, 1 }, { 226, 1 }, { 227, 109 }, { 336, 36 }, + { 372, 171 }, { 543, 109 }, { 652, 47 }, { 699, 20 }, + { 719, 49 }, { 768, 36 }, { 804, 20 }, { 824, 10 }, + { 834, 190 } } }, + + { { { 0, 18 }, { 18, 26 }, { 44, 17 }, { 61, 10 }, + { 71, 27 }, { 98, 37 }, { 135, 24 }, { 159, 16 }, + { 175, 22 }, { 197, 32 }, { 229, 22 }, { 251, 14 }, + { 265, 17 }, { 282, 26 }, { 308, 20 }, { 328, 13 }, + { 341, 683 } } }, + + { { { 0, 71 }, { 71, 92 }, { 163, 49 }, { 212, 25 }, + { 237, 81 }, { 318, 102 }, { 420, 61 }, { 481, 33 }, + { 514, 42 }, { 556, 57 }, { 613, 39 }, { 652, 23 }, + { 675, 22 }, { 697, 30 }, { 727, 22 }, { 749, 15 }, + { 764, 260 } } }, + + { { { 0, 160 }, { 160, 130 }, { 290, 46 }, { 336, 18 }, + { 354, 121 }, { 475, 123 }, { 598, 55 }, { 653, 24 }, + { 677, 45 }, { 722, 55 }, { 777, 31 }, { 808, 15 }, + { 823, 19 }, { 842, 24 }, { 866, 15 }, { 881, 9 }, + { 890, 134 } } }, + + { { { 0, 71 }, { 71, 73 }, { 144, 33 }, { 177, 18 }, + { 195, 71 }, { 266, 76 }, { 342, 43 }, { 385, 26 }, + { 411, 34 }, { 445, 44 }, { 489, 30 }, { 519, 20 }, + { 539, 20 }, { 559, 27 }, { 586, 21 }, { 607, 15 }, + { 622, 402 } } }, + + { { { 0, 48 }, { 48, 60 }, { 108, 32 }, { 140, 19 }, + { 159, 58 }, { 217, 68 }, { 285, 42 }, { 327, 27 }, + { 354, 31 }, { 385, 42 }, { 427, 30 }, { 457, 21 }, + { 478, 19 }, { 497, 27 }, { 524, 21 }, { 545, 16 }, + { 561, 463 } } }, + + { { { 0, 138 }, { 138, 109 }, { 247, 43 }, { 290, 18 }, + { 308, 111 }, { 419, 112 }, { 531, 53 }, { 584, 25 }, + { 609, 46 }, { 655, 55 }, { 710, 32 }, { 742, 17 }, + { 759, 21 }, { 780, 27 }, { 807, 18 }, { 825, 11 }, + { 836, 188 } } }, + + { { { 0, 16 }, { 16, 24 }, { 40, 22 }, { 62, 17 }, + { 79, 24 }, { 103, 36 }, { 139, 31 }, { 170, 25 }, + { 195, 20 }, { 215, 30 }, { 245, 25 }, { 270, 20 }, + { 290, 15 }, { 305, 22 }, { 327, 19 }, { 346, 16 }, + { 362, 662 } } }, + + { { { 0, 579 }, { 579, 150 }, { 729, 12 }, { 741, 2 }, + { 743, 154 }, { 897, 73 }, { 970, 10 }, { 980, 2 }, + { 982, 14 }, { 996, 11 }, { 1007, 3 }, { 1010, 1 }, + { 1011, 3 }, { 1014, 3 }, { 1017, 1 }, { 1018, 1 }, + { 1019, 5 } } }, + + { { { 0, 398 }, { 398, 184 }, { 582, 25 }, { 607, 5 }, + { 612, 176 }, { 788, 114 }, { 902, 23 }, { 925, 6 }, + { 931, 25 }, { 956, 23 }, { 979, 8 }, { 987, 3 }, + { 990, 6 }, { 996, 6 }, { 1002, 3 }, { 1005, 2 }, + { 1007, 17 } } }, + + { { { 0, 13 }, { 13, 21 }, { 34, 18 }, { 52, 11 }, + { 63, 20 }, { 83, 29 }, { 112, 22 }, { 134, 15 }, + { 149, 14 }, { 163, 20 }, { 183, 16 }, { 199, 12 }, + { 211, 10 }, { 221, 14 }, { 235, 12 }, { 247, 10 }, + { 257, 767 } } }, + + { { { 0, 281 }, { 281, 183 }, { 464, 37 }, { 501, 9 }, + { 510, 171 }, { 681, 139 }, { 820, 37 }, { 857, 10 }, + { 867, 35 }, { 902, 36 }, { 938, 15 }, { 953, 6 }, + { 959, 9 }, { 968, 10 }, { 978, 6 }, { 984, 3 }, + { 987, 37 } } }, + + { { { 0, 198 }, { 198, 164 }, { 362, 46 }, { 408, 13 }, + { 421, 154 }, { 575, 147 }, { 722, 51 }, { 773, 16 }, + { 789, 43 }, { 832, 49 }, { 881, 24 }, { 905, 10 }, + { 915, 13 }, { 928, 16 }, { 944, 10 }, { 954, 5 }, + { 959, 65 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 93 }, { 95, 44 }, + { 139, 1 }, { 140, 1 }, { 141, 72 }, { 213, 38 }, + { 251, 86 }, { 337, 70 }, { 407, 43 }, { 450, 25 }, + { 475, 40 }, { 515, 36 }, { 551, 25 }, { 576, 16 }, + { 592, 432 } } }, + + { { { 0, 133 }, { 133, 141 }, { 274, 64 }, { 338, 28 }, + { 366, 117 }, { 483, 122 }, { 605, 59 }, { 664, 27 }, + { 691, 39 }, { 730, 48 }, { 778, 29 }, { 807, 15 }, + { 822, 15 }, { 837, 20 }, { 857, 13 }, { 870, 8 }, + { 878, 146 } } }, + + { { { 0, 128 }, { 128, 125 }, { 253, 49 }, { 302, 18 }, + { 320, 123 }, { 443, 134 }, { 577, 59 }, { 636, 23 }, + { 659, 49 }, { 708, 59 }, { 767, 32 }, { 799, 15 }, + { 814, 19 }, { 833, 24 }, { 857, 15 }, { 872, 9 }, + { 881, 143 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 23 }, { 25, 17 }, + { 42, 1 }, { 43, 1 }, { 44, 23 }, { 67, 18 }, + { 85, 20 }, { 105, 21 }, { 126, 18 }, { 144, 15 }, + { 159, 15 }, { 174, 17 }, { 191, 14 }, { 205, 12 }, + { 217, 807 } } }, + + { { { 0, 70 }, { 70, 96 }, { 166, 63 }, { 229, 38 }, + { 267, 89 }, { 356, 112 }, { 468, 65 }, { 533, 36 }, + { 569, 37 }, { 606, 47 }, { 653, 32 }, { 685, 20 }, + { 705, 17 }, { 722, 23 }, { 745, 17 }, { 762, 12 }, + { 774, 250 } } }, + + { { { 0, 55 }, { 55, 75 }, { 130, 45 }, { 175, 25 }, + { 200, 68 }, { 268, 90 }, { 358, 58 }, { 416, 33 }, + { 449, 39 }, { 488, 54 }, { 542, 39 }, { 581, 25 }, + { 606, 22 }, { 628, 31 }, { 659, 24 }, { 683, 16 }, + { 699, 325 } } }, + + { { { 0, 1 }, { 1, 2 }, { 3, 2 }, { 5, 2 }, + { 7, 2 }, { 9, 2 }, { 11, 2 }, { 13, 2 }, + { 15, 2 }, { 17, 2 }, { 19, 2 }, { 21, 2 }, + { 23, 2 }, { 25, 2 }, { 27, 2 }, { 29, 2 }, + { 31, 993 } } }, + + { { { 0, 34 }, { 34, 51 }, { 85, 38 }, { 123, 24 }, + { 147, 49 }, { 196, 69 }, { 265, 52 }, { 317, 35 }, + { 352, 34 }, { 386, 47 }, { 433, 37 }, { 470, 27 }, + { 497, 21 }, { 518, 31 }, { 549, 25 }, { 574, 19 }, + { 593, 431 } } }, + + { { { 0, 30 }, { 30, 43 }, { 73, 32 }, { 105, 22 }, + { 127, 43 }, { 170, 59 }, { 229, 45 }, { 274, 31 }, + { 305, 30 }, { 335, 42 }, { 377, 34 }, { 411, 25 }, + { 436, 19 }, { 455, 28 }, { 483, 23 }, { 506, 18 }, + { 524, 500 } } }, + + { { { 0, 9 }, { 9, 15 }, { 24, 14 }, { 38, 13 }, + { 51, 14 }, { 65, 22 }, { 87, 21 }, { 108, 18 }, + { 126, 13 }, { 139, 20 }, { 159, 18 }, { 177, 16 }, + { 193, 11 }, { 204, 17 }, { 221, 15 }, { 236, 14 }, + { 250, 774 } } }, + + { { { 0, 30 }, { 30, 44 }, { 74, 31 }, { 105, 20 }, + { 125, 41 }, { 166, 58 }, { 224, 42 }, { 266, 28 }, + { 294, 28 }, { 322, 39 }, { 361, 30 }, { 391, 22 }, + { 413, 18 }, { 431, 26 }, { 457, 21 }, { 478, 16 }, + { 494, 530 } } }, + + { { { 0, 15 }, { 15, 23 }, { 38, 20 }, { 58, 15 }, + { 73, 22 }, { 95, 33 }, { 128, 28 }, { 156, 22 }, + { 178, 18 }, { 196, 26 }, { 222, 23 }, { 245, 18 }, + { 263, 13 }, { 276, 20 }, { 296, 18 }, { 314, 15 }, + { 329, 695 } } }, + + { { { 0, 11 }, { 11, 17 }, { 28, 16 }, { 44, 13 }, + { 57, 17 }, { 74, 26 }, { 100, 23 }, { 123, 19 }, + { 142, 15 }, { 157, 22 }, { 179, 20 }, { 199, 17 }, + { 216, 12 }, { 228, 18 }, { 246, 16 }, { 262, 14 }, + { 276, 748 } } }, + + { { { 0, 448 }, { 448, 171 }, { 619, 20 }, { 639, 4 }, + { 643, 178 }, { 821, 105 }, { 926, 18 }, { 944, 4 }, + { 948, 23 }, { 971, 20 }, { 991, 7 }, { 998, 2 }, + { 1000, 5 }, { 1005, 5 }, { 1010, 2 }, { 1012, 1 }, + { 1013, 11 } } }, + + { { { 0, 332 }, { 332, 188 }, { 520, 29 }, { 549, 6 }, + { 555, 186 }, { 741, 133 }, { 874, 29 }, { 903, 7 }, + { 910, 30 }, { 940, 30 }, { 970, 11 }, { 981, 4 }, + { 985, 6 }, { 991, 7 }, { 998, 4 }, { 1002, 2 }, + { 1004, 20 } } }, + + { { { 0, 8 }, { 8, 13 }, { 21, 13 }, { 34, 11 }, + { 45, 13 }, { 58, 20 }, { 78, 18 }, { 96, 16 }, + { 112, 12 }, { 124, 17 }, { 141, 16 }, { 157, 13 }, + { 170, 10 }, { 180, 14 }, { 194, 13 }, { 207, 12 }, + { 219, 805 } } }, + + { { { 0, 239 }, { 239, 176 }, { 415, 42 }, { 457, 11 }, + { 468, 163 }, { 631, 145 }, { 776, 44 }, { 820, 13 }, + { 833, 39 }, { 872, 42 }, { 914, 19 }, { 933, 7 }, + { 940, 11 }, { 951, 13 }, { 964, 7 }, { 971, 4 }, + { 975, 49 } } }, + + { { { 0, 165 }, { 165, 145 }, { 310, 49 }, { 359, 16 }, + { 375, 138 }, { 513, 139 }, { 652, 55 }, { 707, 20 }, + { 727, 47 }, { 774, 54 }, { 828, 28 }, { 856, 12 }, + { 868, 16 }, { 884, 20 }, { 904, 12 }, { 916, 7 }, + { 923, 101 } } }, + + { { { 0, 3 }, { 3, 5 }, { 8, 5 }, { 13, 5 }, + { 18, 5 }, { 23, 7 }, { 30, 7 }, { 37, 7 }, + { 44, 4 }, { 48, 7 }, { 55, 7 }, { 62, 6 }, + { 68, 4 }, { 72, 6 }, { 78, 6 }, { 84, 6 }, + { 90, 934 } } }, + + { { { 0, 115 }, { 115, 122 }, { 237, 52 }, { 289, 22 }, + { 311, 111 }, { 422, 125 }, { 547, 61 }, { 608, 27 }, + { 635, 45 }, { 680, 57 }, { 737, 34 }, { 771, 17 }, + { 788, 19 }, { 807, 25 }, { 832, 17 }, { 849, 10 }, + { 859, 165 } } }, + + { { { 0, 107 }, { 107, 114 }, { 221, 51 }, { 272, 21 }, + { 293, 106 }, { 399, 122 }, { 521, 61 }, { 582, 28 }, + { 610, 46 }, { 656, 58 }, { 714, 35 }, { 749, 18 }, + { 767, 20 }, { 787, 26 }, { 813, 18 }, { 831, 11 }, + { 842, 182 } } }, + + { { { 0, 6 }, { 6, 10 }, { 16, 10 }, { 26, 9 }, + { 35, 10 }, { 45, 15 }, { 60, 15 }, { 75, 14 }, + { 89, 9 }, { 98, 14 }, { 112, 13 }, { 125, 12 }, + { 137, 8 }, { 145, 12 }, { 157, 11 }, { 168, 10 }, + { 178, 846 } } }, + + { { { 0, 72 }, { 72, 88 }, { 160, 50 }, { 210, 26 }, + { 236, 84 }, { 320, 102 }, { 422, 60 }, { 482, 32 }, + { 514, 41 }, { 555, 53 }, { 608, 36 }, { 644, 21 }, + { 665, 20 }, { 685, 27 }, { 712, 20 }, { 732, 13 }, + { 745, 279 } } }, + + { { { 0, 45 }, { 45, 63 }, { 108, 45 }, { 153, 30 }, + { 183, 61 }, { 244, 83 }, { 327, 58 }, { 385, 36 }, + { 421, 34 }, { 455, 47 }, { 502, 34 }, { 536, 23 }, + { 559, 19 }, { 578, 27 }, { 605, 21 }, { 626, 15 }, + { 641, 383 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 7 }, { 9, 7 }, + { 16, 1 }, { 17, 1 }, { 18, 8 }, { 26, 8 }, + { 34, 6 }, { 40, 8 }, { 48, 7 }, { 55, 7 }, + { 62, 6 }, { 68, 7 }, { 75, 7 }, { 82, 6 }, + { 88, 936 } } }, + + { { { 0, 29 }, { 29, 44 }, { 73, 35 }, { 108, 24 }, + { 132, 42 }, { 174, 62 }, { 236, 48 }, { 284, 34 }, + { 318, 30 }, { 348, 43 }, { 391, 35 }, { 426, 26 }, + { 452, 19 }, { 471, 29 }, { 500, 24 }, { 524, 19 }, + { 543, 481 } } }, + + { { { 0, 20 }, { 20, 31 }, { 51, 25 }, { 76, 17 }, + { 93, 30 }, { 123, 43 }, { 166, 34 }, { 200, 25 }, + { 225, 22 }, { 247, 32 }, { 279, 26 }, { 305, 21 }, + { 326, 16 }, { 342, 23 }, { 365, 20 }, { 385, 16 }, + { 401, 623 } } }, + + { { { 0, 742 }, { 742, 103 }, { 845, 5 }, { 850, 1 }, + { 851, 108 }, { 959, 38 }, { 997, 4 }, { 1001, 1 }, + { 1002, 7 }, { 1009, 5 }, { 1014, 2 }, { 1016, 1 }, + { 1017, 2 }, { 1019, 1 }, { 1020, 1 }, { 1021, 1 }, + { 1022, 2 } } }, + + { { { 0, 42 }, { 42, 52 }, { 94, 27 }, { 121, 16 }, + { 137, 49 }, { 186, 58 }, { 244, 36 }, { 280, 23 }, + { 303, 27 }, { 330, 36 }, { 366, 26 }, { 392, 18 }, + { 410, 17 }, { 427, 24 }, { 451, 19 }, { 470, 14 }, + { 484, 540 } } }, + + { { { 0, 13 }, { 13, 20 }, { 33, 18 }, { 51, 15 }, + { 66, 19 }, { 85, 29 }, { 114, 26 }, { 140, 21 }, + { 161, 17 }, { 178, 25 }, { 203, 22 }, { 225, 18 }, + { 243, 13 }, { 256, 19 }, { 275, 17 }, { 292, 15 }, + { 307, 717 } } }, + + { { { 0, 501 }, { 501, 169 }, { 670, 19 }, { 689, 4 }, + { 693, 155 }, { 848, 88 }, { 936, 16 }, { 952, 4 }, + { 956, 19 }, { 975, 16 }, { 991, 6 }, { 997, 2 }, + { 999, 5 }, { 1004, 4 }, { 1008, 2 }, { 1010, 1 }, + { 1011, 13 } } }, + + { { { 0, 445 }, { 445, 136 }, { 581, 22 }, { 603, 6 }, + { 609, 158 }, { 767, 98 }, { 865, 23 }, { 888, 7 }, + { 895, 31 }, { 926, 28 }, { 954, 10 }, { 964, 4 }, + { 968, 9 }, { 977, 9 }, { 986, 5 }, { 991, 2 }, + { 993, 31 } } }, + + { { { 0, 285 }, { 285, 157 }, { 442, 37 }, { 479, 10 }, + { 489, 161 }, { 650, 129 }, { 779, 39 }, { 818, 12 }, + { 830, 40 }, { 870, 42 }, { 912, 18 }, { 930, 7 }, + { 937, 12 }, { 949, 14 }, { 963, 8 }, { 971, 4 }, + { 975, 49 } } }, + + { { { 0, 349 }, { 349, 179 }, { 528, 33 }, { 561, 8 }, + { 569, 162 }, { 731, 121 }, { 852, 31 }, { 883, 9 }, + { 892, 31 }, { 923, 30 }, { 953, 12 }, { 965, 5 }, + { 970, 8 }, { 978, 9 }, { 987, 5 }, { 992, 2 }, + { 994, 30 } } }, + + { { { 0, 199 }, { 199, 156 }, { 355, 47 }, { 402, 15 }, + { 417, 146 }, { 563, 137 }, { 700, 50 }, { 750, 17 }, + { 767, 44 }, { 811, 49 }, { 860, 24 }, { 884, 10 }, + { 894, 15 }, { 909, 17 }, { 926, 10 }, { 936, 6 }, + { 942, 82 } } }, + + { { { 0, 141 }, { 141, 134 }, { 275, 50 }, { 325, 18 }, + { 343, 128 }, { 471, 135 }, { 606, 58 }, { 664, 22 }, + { 686, 48 }, { 734, 57 }, { 791, 31 }, { 822, 14 }, + { 836, 18 }, { 854, 23 }, { 877, 14 }, { 891, 8 }, + { 899, 125 } } }, + + { { { 0, 243 }, { 243, 194 }, { 437, 56 }, { 493, 17 }, + { 510, 139 }, { 649, 126 }, { 775, 45 }, { 820, 16 }, + { 836, 33 }, { 869, 36 }, { 905, 18 }, { 923, 8 }, + { 931, 10 }, { 941, 12 }, { 953, 7 }, { 960, 4 }, + { 964, 60 } } }, + + { { { 0, 91 }, { 91, 106 }, { 197, 51 }, { 248, 23 }, + { 271, 99 }, { 370, 117 }, { 487, 63 }, { 550, 30 }, + { 580, 45 }, { 625, 59 }, { 684, 37 }, { 721, 20 }, + { 741, 20 }, { 761, 27 }, { 788, 19 }, { 807, 12 }, + { 819, 205 } } }, + + { { { 0, 107 }, { 107, 94 }, { 201, 41 }, { 242, 20 }, + { 262, 92 }, { 354, 97 }, { 451, 52 }, { 503, 28 }, + { 531, 42 }, { 573, 53 }, { 626, 34 }, { 660, 20 }, + { 680, 21 }, { 701, 29 }, { 730, 21 }, { 751, 14 }, + { 765, 259 } } }, + + { { { 0, 168 }, { 168, 171 }, { 339, 68 }, { 407, 25 }, + { 432, 121 }, { 553, 123 }, { 676, 55 }, { 731, 24 }, + { 755, 34 }, { 789, 41 }, { 830, 24 }, { 854, 12 }, + { 866, 13 }, { 879, 16 }, { 895, 11 }, { 906, 6 }, + { 912, 112 } } }, + + { { { 0, 67 }, { 67, 80 }, { 147, 44 }, { 191, 23 }, + { 214, 76 }, { 290, 94 }, { 384, 57 }, { 441, 31 }, + { 472, 41 }, { 513, 54 }, { 567, 37 }, { 604, 23 }, + { 627, 21 }, { 648, 30 }, { 678, 22 }, { 700, 15 }, + { 715, 309 } } }, + + { { { 0, 46 }, { 46, 63 }, { 109, 39 }, { 148, 23 }, + { 171, 58 }, { 229, 78 }, { 307, 52 }, { 359, 32 }, + { 391, 36 }, { 427, 49 }, { 476, 37 }, { 513, 24 }, + { 537, 21 }, { 558, 30 }, { 588, 24 }, { 612, 17 }, + { 629, 395 } } }, + + { { { 0, 848 }, { 848, 70 }, { 918, 2 }, { 920, 1 }, + { 921, 75 }, { 996, 16 }, { 1012, 1 }, { 1013, 1 }, + { 1014, 2 }, { 1016, 1 }, { 1017, 1 }, { 1018, 1 }, + { 1019, 1 }, { 1020, 1 }, { 1021, 1 }, { 1022, 1 }, + { 1023, 1 } } }, + + { { { 0, 36 }, { 36, 52 }, { 88, 35 }, { 123, 22 }, + { 145, 48 }, { 193, 67 }, { 260, 48 }, { 308, 32 }, + { 340, 32 }, { 372, 45 }, { 417, 35 }, { 452, 24 }, + { 476, 20 }, { 496, 29 }, { 525, 23 }, { 548, 17 }, + { 565, 459 } } }, + + { { { 0, 24 }, { 24, 37 }, { 61, 29 }, { 90, 20 }, + { 110, 35 }, { 145, 51 }, { 196, 41 }, { 237, 29 }, + { 266, 26 }, { 292, 38 }, { 330, 31 }, { 361, 24 }, + { 385, 18 }, { 403, 27 }, { 430, 23 }, { 453, 18 }, + { 471, 553 } } }, + + { { { 0, 85 }, { 85, 97 }, { 182, 48 }, { 230, 23 }, + { 253, 91 }, { 344, 110 }, { 454, 61 }, { 515, 30 }, + { 545, 45 }, { 590, 58 }, { 648, 37 }, { 685, 21 }, + { 706, 21 }, { 727, 29 }, { 756, 20 }, { 776, 13 }, + { 789, 235 } } }, + + { { { 0, 22 }, { 22, 33 }, { 55, 27 }, { 82, 20 }, + { 102, 33 }, { 135, 48 }, { 183, 39 }, { 222, 30 }, + { 252, 26 }, { 278, 37 }, { 315, 30 }, { 345, 23 }, + { 368, 17 }, { 385, 25 }, { 410, 21 }, { 431, 17 }, + { 448, 576 } } }, + + { { { 0, 1 }, { 1, 1 }, { 2, 54 }, { 56, 33 }, + { 89, 1 }, { 90, 1 }, { 91, 49 }, { 140, 32 }, + { 172, 49 }, { 221, 47 }, { 268, 35 }, { 303, 25 }, + { 328, 30 }, { 358, 30 }, { 388, 24 }, { 412, 18 }, + { 430, 594 } } }, + + { { { 0, 45 }, { 45, 64 }, { 109, 43 }, { 152, 25 }, + { 177, 62 }, { 239, 81 }, { 320, 56 }, { 376, 35 }, + { 411, 37 }, { 448, 51 }, { 499, 38 }, { 537, 26 }, + { 563, 22 }, { 585, 31 }, { 616, 24 }, { 640, 18 }, + { 658, 366 } } }, + + { { { 0, 247 }, { 247, 148 }, { 395, 38 }, { 433, 12 }, + { 445, 154 }, { 599, 130 }, { 729, 42 }, { 771, 14 }, + { 785, 44 }, { 829, 46 }, { 875, 21 }, { 896, 9 }, + { 905, 15 }, { 920, 17 }, { 937, 9 }, { 946, 5 }, + { 951, 73 } } }, + + { { { 0, 231 }, { 231, 136 }, { 367, 41 }, { 408, 15 }, + { 423, 134 }, { 557, 119 }, { 676, 47 }, { 723, 19 }, + { 742, 44 }, { 786, 49 }, { 835, 25 }, { 860, 12 }, + { 872, 17 }, { 889, 20 }, { 909, 12 }, { 921, 7 }, + { 928, 96 } } } + +}; + +const uint16_t lc3_spectrum_bits[][17] = { + + { 20480, 20480, 5220, 9042, 20480, 20480, 6619, 9892, + 5289, 6619, 9105, 11629, 8982, 9892, 11629, 13677, 4977 }, + + { 11940, 10854, 12109, 13677, 10742, 9812, 11090, 12288, + 11348, 10240, 11348, 12683, 12109, 10854, 11629, 12902, 1197 }, + + { 7886, 7120, 8982, 10970, 7496, 6815, 8334, 10150, + 9437, 8535, 9656, 11216, 11348, 10431, 11348, 12479, 4051 }, + + { 5485, 6099, 9168, 11940, 6311, 6262, 8640, 11090, + 9233, 8640, 10334, 12479, 11781, 11090, 12479, 13988, 6009 }, + + { 7886, 7804, 10150, 11940, 7886, 7685, 9368, 10854, + 10061, 9300, 10431, 11629, 11629, 10742, 11485, 12479, 2763 }, + + { 9042, 8383, 10240, 11781, 8483, 8013, 9437, 10742, + 10334, 9437, 10431, 11485, 11781, 10742, 11485, 12288, 2346 }, + + { 5922, 6619, 9368, 11940, 6566, 6539, 8750, 10970, + 9168, 8640, 10240, 12109, 11485, 10742, 11940, 13396, 5009 }, + + { 12288, 11090, 11348, 12109, 11090, 9892, 10334, 10970, + 11629, 10431, 10970, 11629, 12479, 11348, 11781, 12288, 1289 }, + + { 1685, 5676, 13138, 18432, 5598, 7804, 13677, 18432, + 12683, 13396, 17234, 20480, 17234, 17234, 20480, 20480, 15725 }, + + { 2793, 5072, 10970, 15725, 5204, 6487, 11216, 15186, + 10970, 11216, 14336, 17234, 15186, 15186, 17234, 18432, 12109 }, + + { 12902, 11485, 11940, 13396, 11629, 10531, 11348, 12479, + 12683, 11629, 12288, 13138, 13677, 12683, 13138, 13677, 854 }, + + { 3821, 5088, 9812, 13988, 5289, 5901, 9812, 13677, + 9976, 9892, 12479, 15186, 13988, 13677, 15186, 17234, 9812 }, + + { 4856, 5412, 9168, 12902, 5598, 5736, 8863, 12288, + 9368, 8982, 11090, 13677, 12902, 12288, 13677, 15725, 8147 }, + + { 20480, 20480, 7088, 9300, 20480, 20480, 7844, 9733, + 7320, 7928, 9368, 10970, 9581, 9892, 10970, 12288, 2550 }, + + { 6031, 5859, 8192, 10635, 6410, 6286, 8433, 10742, + 9656, 9042, 10531, 12479, 12479, 11629, 12902, 14336, 5756 }, + + { 6144, 6215, 8982, 11940, 6262, 6009, 8433, 11216, + 8982, 8433, 10240, 12479, 11781, 11090, 12479, 13988, 5817 }, + + { 20480, 20480, 11216, 12109, 20480, 20480, 11216, 11940, + 11629, 11485, 11940, 12479, 12479, 12109, 12683, 13138, 704 }, + + { 7928, 6994, 8239, 9733, 7218, 6539, 8147, 9892, + 9812, 9105, 10240, 11629, 12109, 11216, 12109, 13138, 4167 }, + + { 8640, 7724, 9233, 10970, 8013, 7185, 8483, 10150, + 9656, 8694, 9656, 10970, 11348, 10334, 11090, 12288, 3391 }, + + { 20480, 18432, 18432, 18432, 18432, 18432, 18432, 18432, + 18432, 18432, 18432, 18432, 18432, 18432, 18432, 18432, 91 }, + + { 10061, 8863, 9733, 11090, 8982, 7970, 8806, 9976, + 10061, 9105, 9812, 10742, 11485, 10334, 10970, 11781, 2557 }, + + { 10431, 9368, 10240, 11348, 9368, 8433, 9233, 10334, + 10431, 9437, 10061, 10970, 11781, 10635, 11216, 11940, 2119 }, + + { 13988, 12479, 12683, 12902, 12683, 11348, 11485, 11940, + 12902, 11629, 11940, 12288, 13396, 12109, 12479, 12683, 828 }, + + { 10431, 9300, 10334, 11629, 9508, 8483, 9437, 10635, + 10635, 9656, 10431, 11348, 11940, 10854, 11485, 12288, 1946 }, + + { 12479, 11216, 11629, 12479, 11348, 10150, 10635, 11348, + 11940, 10854, 11216, 11940, 12902, 11629, 11940, 12479, 1146 }, + + { 13396, 12109, 12288, 12902, 12109, 10854, 11216, 11781, + 12479, 11348, 11629, 12109, 13138, 11940, 12288, 12683, 928 }, + + { 2443, 5289, 11629, 16384, 5170, 6730, 11940, 16384, + 11216, 11629, 14731, 18432, 15725, 15725, 18432, 20480, 13396 }, + + { 3328, 5009, 10531, 15186, 5040, 6031, 10531, 14731, + 10431, 10431, 13396, 16384, 15186, 14731, 16384, 18432, 11629 }, + + { 14336, 12902, 12902, 13396, 12902, 11629, 11940, 12288, + 13138, 12109, 12288, 12902, 13677, 12683, 12902, 13138, 711 }, + + { 4300, 5204, 9437, 13396, 5430, 5776, 9300, 12902, + 9656, 9437, 11781, 14731, 13396, 12902, 14731, 16384, 8982 }, + + { 5394, 5776, 8982, 12288, 5922, 5901, 8640, 11629, + 9105, 8694, 10635, 13138, 12288, 11629, 13138, 14731, 6844 }, + + { 17234, 15725, 15725, 15725, 15725, 14731, 14731, 14731, + 16384, 14731, 14731, 15186, 16384, 15186, 15186, 15186, 272 }, + + { 6461, 6286, 8806, 11348, 6566, 6215, 8334, 10742, + 9233, 8535, 10061, 12109, 11781, 10970, 12109, 13677, 5394 }, + + { 6674, 6487, 8863, 11485, 6702, 6286, 8334, 10635, + 9168, 8483, 9976, 11940, 11629, 10854, 11940, 13396, 5105 }, + + { 15186, 13677, 13677, 13988, 13677, 12479, 12479, 12683, + 13988, 12683, 12902, 13138, 14336, 13138, 13396, 13677, 565 }, + + { 7844, 7252, 8922, 10854, 7389, 6815, 8383, 10240, + 9508, 8750, 9892, 11485, 11629, 10742, 11629, 12902, 3842 }, + + { 9233, 8239, 9233, 10431, 8334, 7424, 8483, 9892, + 10061, 9105, 10061, 11216, 11781, 10742, 11485, 12479, 2906 }, + + { 20480, 20480, 14731, 14731, 20480, 20480, 14336, 14336, + 15186, 14336, 14731, 14731, 15186, 14731, 14731, 15186, 266 }, + + { 10531, 9300, 9976, 11090, 9437, 8286, 9042, 10061, + 10431, 9368, 9976, 10854, 11781, 10531, 11090, 11781, 2233 }, + + { 11629, 10334, 10970, 12109, 10431, 9368, 10061, 10970, + 11348, 10240, 10854, 11485, 12288, 11216, 11629, 12288, 1469 }, + + { 952, 6787, 15725, 20480, 6646, 9733, 16384, 20480, + 14731, 15725, 18432, 20480, 18432, 20480, 20480, 20480, 18432 }, + + { 9437, 8806, 10742, 12288, 8982, 8483, 9892, 11216, + 10742, 9892, 10854, 11940, 12109, 11090, 11781, 12683, 1891 }, + + { 12902, 11629, 11940, 12479, 11781, 10531, 10854, 11485, + 12109, 10970, 11348, 11940, 12902, 11781, 12109, 12479, 1054 }, + + { 2113, 5323, 11781, 16384, 5579, 7252, 12288, 16384, + 11781, 12288, 15186, 18432, 15725, 16384, 18432, 20480, 12902 }, + + { 2463, 5965, 11348, 15186, 5522, 6934, 11216, 14731, + 10334, 10635, 13677, 16384, 13988, 13988, 15725, 18432, 10334 }, + + { 3779, 5541, 9812, 13677, 5467, 6122, 9656, 13138, + 9581, 9437, 11940, 14731, 13138, 12683, 14336, 16384, 8982 }, + + { 3181, 5154, 10150, 14336, 5448, 6311, 10334, 13988, + 10334, 10431, 13138, 15725, 14336, 13988, 15725, 18432, 10431 }, + + { 4841, 5560, 9105, 12479, 5756, 5944, 8922, 12109, + 9300, 8982, 11090, 13677, 12479, 12109, 13677, 15186, 7460 }, + + { 5859, 6009, 8922, 11940, 6144, 5987, 8483, 11348, + 9042, 8535, 10334, 12683, 11940, 11216, 12683, 14336, 6215 }, + + { 4250, 4916, 8587, 12109, 5901, 6191, 9233, 12288, + 10150, 9892, 11940, 14336, 13677, 13138, 14731, 16384, 8383 }, + + { 7153, 6702, 8863, 11216, 6904, 6410, 8239, 10431, + 9233, 8433, 9812, 11629, 11629, 10742, 11781, 13138, 4753 }, + + { 6674, 7057, 9508, 11629, 7120, 6964, 8806, 10635, + 9437, 8750, 10061, 11629, 11485, 10531, 11485, 12683, 4062 }, + + { 5341, 5289, 8013, 10970, 6311, 6262, 8640, 11090, + 10061, 9508, 11090, 13138, 12902, 12288, 13396, 15186, 6539 }, + + { 8057, 7533, 9300, 11216, 7685, 7057, 8535, 10334, + 9508, 8694, 9812, 11216, 11485, 10431, 11348, 12479, 3541 }, + + { 9168, 8239, 9656, 11216, 8483, 7608, 8806, 10240, + 9892, 8982, 9812, 11090, 11485, 10431, 11090, 12109, 2815 }, + + { 558, 7928, 18432, 20480, 7724, 12288, 20480, 20480, + 18432, 20480, 20480, 20480, 20480, 20480, 20480, 20480, 20480 }, + + { 9892, 8806, 9976, 11348, 9042, 8057, 9042, 10240, + 10240, 9233, 9976, 11090, 11629, 10531, 11216, 12109, 2371 }, + + { 11090, 9812, 10531, 11629, 9976, 8863, 9508, 10531, + 10854, 9733, 10334, 11090, 11940, 10742, 11216, 11940, 1821 }, + + { 7354, 6964, 9042, 11216, 7153, 6592, 8334, 10431, + 9233, 8483, 9812, 11485, 11485, 10531, 11629, 12902, 4349 }, + + { 11348, 10150, 10742, 11629, 10150, 9042, 9656, 10431, + 10854, 9812, 10431, 11216, 12109, 10970, 11485, 12109, 1700 }, + + { 20480, 20480, 8694, 10150, 20480, 20480, 8982, 10240, + 8982, 9105, 9976, 10970, 10431, 10431, 11090, 11940, 1610 }, + + { 9233, 8192, 9368, 10970, 8286, 7496, 8587, 9976, + 9812, 8863, 9733, 10854, 11348, 10334, 11090, 11940, 3040 }, + + { 4202, 5716, 9733, 13138, 5598, 6099, 9437, 12683, + 9300, 9168, 11485, 13988, 12479, 12109, 13988, 15725, 7804 }, + + { 4400, 5965, 9508, 12479, 6009, 6360, 9105, 11781, + 9300, 8982, 10970, 13138, 12109, 11629, 13138, 14731, 6994 } + +}; diff --git a/App_Resources/iOS/src/lc3/tables.h b/App_Resources/iOS/src/lc3/tables.h new file mode 100644 index 00000000..2dc7810c --- /dev/null +++ b/App_Resources/iOS/src/lc3/tables.h @@ -0,0 +1,161 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_TABLES_H +#define __LC3_TABLES_H + +#include "common.h" +#include "bits.h" + + +/** + * Characteristics + * + * ns Number of temporal samples / frequency coefficients within a frame + * + * ne Number of encoded frequency coefficients + * + * nd Number of MDCT delayed samples, sum of half a frame and an ovelap + * of future by 1.25 ms (2.5ms, 5ms and 10ms frame durations), + * or 2 ms (7.5ms frame duration). + * + * nh Number of 18 ms samples of the history buffer, aligned on a frame + * + * nt Number of 1.25 ms previous samples + */ + +extern const int lc3_ns_2m5[LC3_NUM_SRATE]; +extern const int lc3_ne_2m5[LC3_NUM_SRATE]; +extern const int lc3_ns_4m [LC3_NUM_SRATE]; + +static inline int lc3_ns(enum lc3_dt dt, enum lc3_srate sr) { + return lc3_ns_2m5[sr] * (1 + dt); +} + +static inline int lc3_ne(enum lc3_dt dt, enum lc3_srate sr) { + return lc3_ne_2m5[sr] * (1 + dt); +} + +static inline int lc3_nd(enum lc3_dt dt, enum lc3_srate sr) { + return ( lc3_ns(dt, sr) + + (dt == LC3_DT_7M5 ? lc3_ns_4m[sr] : lc3_ns_2m5[sr]) ) >> 1; +} + +static inline int lc3_nh(enum lc3_dt dt, enum lc3_srate sr) { + return sr > LC3_SRATE_48K_HR ? 0 : + (8 + (dt == LC3_DT_7M5)) * lc3_ns_2m5[sr]; +} + +static inline int lc3_nt(enum lc3_srate sr) { + return lc3_ns_2m5[sr] >> 1; +} + +#define LC3_MAX_SRATE_HZ ( LC3_PLUS_HR ? 96000 : 48000 ) + +#define LC3_MAX_NS ( LC3_NS(10000, LC3_MAX_SRATE_HZ) ) +#define LC3_MAX_NE ( LC3_PLUS_HR ? LC3_MAX_NS : LC3_NS(10000, 40000) ) + + +/** + * Limits on size of frame + */ + +extern const int lc3_frame_bytes_hr_lim + [LC3_NUM_DT][LC3_NUM_SRATE - LC3_SRATE_48K_HR][2]; + +static inline int lc3_min_frame_bytes(enum lc3_dt dt, enum lc3_srate sr) { + return !lc3_hr(sr) ? LC3_MIN_FRAME_BYTES : + lc3_frame_bytes_hr_lim[dt][sr - LC3_SRATE_48K_HR][0]; +} + +static inline int lc3_max_frame_bytes(enum lc3_dt dt, enum lc3_srate sr) { + return !lc3_hr(sr) ? LC3_MAX_FRAME_BYTES : + lc3_frame_bytes_hr_lim[dt][sr - LC3_SRATE_48K_HR][1]; +} + + +/** + * MDCT Twiddles and window coefficients + */ + +struct lc3_fft_bf3_twiddles { int n3; const struct lc3_complex (*t)[2]; }; +struct lc3_fft_bf2_twiddles { int n2; const struct lc3_complex *t; }; +struct lc3_mdct_rot_def { int n4; const struct lc3_complex *w; }; + +extern const struct lc3_fft_bf3_twiddles *lc3_fft_twiddles_bf3[]; +extern const struct lc3_fft_bf2_twiddles *lc3_fft_twiddles_bf2[][3]; +extern const struct lc3_mdct_rot_def *lc3_mdct_rot[LC3_NUM_DT][LC3_NUM_SRATE]; + +extern const float *lc3_mdct_win[LC3_NUM_DT][LC3_NUM_SRATE]; + + +/** + * Limits of bands + */ + +#define LC3_MAX_BANDS 64 + +extern const int lc3_num_bands[LC3_NUM_DT][LC3_NUM_SRATE]; +extern const int *lc3_band_lim[LC3_NUM_DT][LC3_NUM_SRATE]; + + +/** + * SNS Quantization + */ + +extern const float lc3_sns_lfcb[32][8]; +extern const float lc3_sns_hfcb[32][8]; + +struct lc3_sns_vq_gains { + int count; const float *v; +}; + +extern const struct lc3_sns_vq_gains lc3_sns_vq_gains[4]; + +extern const int32_t lc3_sns_mpvq_offsets[][11]; + + +/** + * TNS Arithmetic Coding + */ + +extern const struct lc3_ac_model lc3_tns_order_models[]; +extern const uint16_t lc3_tns_order_bits[][8]; + +extern const struct lc3_ac_model lc3_tns_coeffs_models[]; +extern const uint16_t lc3_tns_coeffs_bits[][17]; + + +/** + * Long Term Postfilter + */ + +extern const float *lc3_ltpf_cnum[LC3_NUM_SRATE][4]; +extern const float *lc3_ltpf_cden[LC3_NUM_SRATE][4]; + + +/** + * Spectral Data Arithmetic Coding + */ + +extern const uint8_t lc3_spectrum_lookup[2][2][256][4]; +extern const struct lc3_ac_model lc3_spectrum_models[]; +extern const uint16_t lc3_spectrum_bits[][17]; + + +#endif /* __LC3_TABLES_H */ diff --git a/App_Resources/iOS/src/lc3/tns.c b/App_Resources/iOS/src/lc3/tns.c new file mode 100644 index 00000000..b2b9305e --- /dev/null +++ b/App_Resources/iOS/src/lc3/tns.c @@ -0,0 +1,510 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#include "tns.h" +#include "tables.h" + + +/* ---------------------------------------------------------------------------- + * Filter Coefficients + * -------------------------------------------------------------------------- */ + +/** + * Resolve LPC Weighting indication according bitrate + * dt, nbytes Duration and size of the frame + * return True when LPC Weighting enabled + */ +static bool resolve_lpc_weighting(enum lc3_dt dt, int nbytes) +{ + return nbytes * 8 < 120 * (int)(1 + dt); +} + +/** + * Return dot product of 2 vectors + * a, b, n The 2 vectors of size `n` + * return sum( a[i] * b[i] ), i = [0..n-1] + */ +LC3_HOT static inline float dot(const float *a, const float *b, int n) +{ + float v = 0; + + while (n--) + v += *(a++) * *(b++); + + return v; +} + +/** + * LPC Coefficients + * dt, bw Duration and bandwidth of the frame + * maxorder Maximum order of filter + * x Spectral coefficients + * gain, a Output the prediction gains and LPC coefficients + */ +LC3_HOT static void compute_lpc_coeffs( + enum lc3_dt dt, enum lc3_bandwidth bw, int maxorder, + const float *x, float *gain, float (*a)[9]) +{ + +#if LC3_PLUS + + static const int sub_2m5_nb[] = { 3, 10, 20 }; + static const int sub_2m5_wb[] = { 3, 20, 40 }; + static const int sub_2m5_sswb[] = { 3, 30, 60 }; + static const int sub_2m5_swb[] = { 3, 40, 80 }; + static const int sub_2m5_fb[] = { 3, 51, 100 }; + + static const int sub_5m_nb[] = { 6, 23, 40 }; + static const int sub_5m_wb[] = { 6, 43, 80 }; + static const int sub_5m_sswb[] = { 6, 63, 120 }; + static const int sub_5m_swb[] = { 6, 43, 80, 120, 160 }; + static const int sub_5m_fb[] = { 6, 53, 100, 150, 200 }; + +#endif /* LC3_PLUS */ + + static const int sub_7m5_nb[] = { 9, 26, 43, 60 }; + static const int sub_7m5_wb[] = { 9, 46, 83, 120 }; + static const int sub_7m5_sswb[] = { 9, 66, 123, 180 }; + static const int sub_7m5_swb[] = { 9, 46, 82, 120, 159, 200, 240 }; + static const int sub_7m5_fb[] = { 9, 56, 103, 150, 200, 250, 300 }; + + static const int sub_10m_nb[] = { 12, 34, 57, 80 }; + static const int sub_10m_wb[] = { 12, 61, 110, 160 }; + static const int sub_10m_sswb[] = { 12, 88, 164, 240 }; + static const int sub_10m_swb[] = { 12, 61, 110, 160, 213, 266, 320 }; + static const int sub_10m_fb[] = { 12, 74, 137, 200, 266, 333, 400 }; + + static const float lag_window[] = { + 1.00000000e+00, 9.98028026e-01, 9.92135406e-01, 9.82391584e-01, + 9.68910791e-01, 9.51849807e-01, 9.31404933e-01, 9.07808230e-01, + 8.81323137e-01 + }; + + const int *sub = (const int * const [LC3_NUM_DT][LC3_NUM_BANDWIDTH]){ + +#if LC3_PLUS + + [LC3_DT_2M5] = { + sub_2m5_nb, sub_2m5_wb, sub_2m5_sswb, sub_2m5_swb, + sub_2m5_fb, sub_2m5_fb, sub_2m5_fb }, + + [LC3_DT_5M] = { + sub_5m_nb , sub_5m_wb , sub_5m_sswb , sub_5m_swb , + sub_5m_fb , sub_5m_fb , sub_5m_fb }, + +#endif /* LC3_PLUS */ + + [LC3_DT_7M5] = { + sub_7m5_nb, sub_7m5_wb, sub_7m5_sswb, sub_7m5_swb, + sub_7m5_fb }, + + [LC3_DT_10M] = { + sub_10m_nb, sub_10m_wb, sub_10m_sswb, sub_10m_swb, + sub_10m_fb, sub_10m_fb, sub_10m_fb }, + + }[dt][bw]; + + /* --- Normalized autocorrelation --- */ + + int nfilters = 1 + (dt >= LC3_DT_5M && bw >= LC3_BANDWIDTH_SWB); + int nsubdivisions = 2 + (dt >= LC3_DT_7M5); + + const float *xs, *xe = x + *sub; + float r[2][9]; + + for (int f = 0; f < nfilters; f++) { + float c[9][3] = { 0 }; + + for (int s = 0; s < nsubdivisions; s++) { + xs = xe, xe = x + *(++sub); + + for (int k = 0; k <= maxorder; k++) + c[k][s] = dot(xs, xs + k, (xe - xs) - k); + } + + r[f][0] = nsubdivisions; + if (nsubdivisions == 2) { + float e0 = c[0][0], e1 = c[0][1]; + for (int k = 1; k <= maxorder; k++) + r[f][k] = e0 == 0 || e1 == 0 ? 0 : + (c[k][0]/e0 + c[k][1]/e1) * lag_window[k]; + + } else { + float e0 = c[0][0], e1 = c[0][1], e2 = c[0][2]; + for (int k = 1; k <= maxorder; k++) + r[f][k] = e0 == 0 || e1 == 0 || e2 == 0 ? 0 : + (c[k][0]/e0 + c[k][1]/e1 + c[k][2]/e2) * lag_window[k]; + } + } + + /* --- Levinson-Durbin recursion --- */ + + for (int f = 0; f < nfilters; f++) { + float *a0 = a[f], a1[9]; + float err = r[f][0], rc; + + gain[f] = err; + + a0[0] = 1; + for (int k = 1; k <= maxorder; ) { + + rc = -r[f][k]; + for (int i = 1; i < k; i++) + rc -= a0[i] * r[f][k-i]; + + rc /= err; + err *= 1 - rc * rc; + + for (int i = 1; i < k; i++) + a1[i] = a0[i] + rc * a0[k-i]; + a1[k++] = rc; + + rc = -r[f][k]; + for (int i = 1; i < k; i++) + rc -= a1[i] * r[f][k-i]; + + rc /= err; + err *= 1 - rc * rc; + + for (int i = 1; i < k; i++) + a0[i] = a1[i] + rc * a1[k-i]; + a0[k++] = rc; + } + + gain[f] /= err; + } +} + +/** + * LPC Weighting + * gain, a Prediction gain and LPC coefficients, weighted as output + */ +LC3_HOT static void lpc_weighting(float pred_gain, float *a) +{ + float gamma = 1.f - (1.f - 0.85f) * (2.f - pred_gain) / (2.f - 1.5f); + float g = 1.f; + + for (int i = 1; i < 9; i++) + a[i] *= (g *= gamma); +} + +/** + * LPC reflection + * a, maxorder LPC coefficients, and maximum order (4 or 8) + * rc Output refelection coefficients + */ +LC3_HOT static void lpc_reflection( + const float *a, int maxorder, float *rc) +{ + float e, b[2][7], *b0, *b1; + + rc[maxorder-1] = a[maxorder]; + e = 1 - rc[maxorder-1] * rc[maxorder-1]; + + b1 = b[1]; + for (int i = 0; i < maxorder-1; i++) + b1[i] = (a[1+i] - rc[maxorder-1] * a[(maxorder-1)-i]) / e; + + for (int k = maxorder-2; k > 0; k--) { + b0 = b1, b1 = b[k & 1]; + + rc[k] = b0[k]; + e = 1 - rc[k] * rc[k]; + + for (int i = 0; i < k; i++) + b1[i] = (b0[i] - rc[k] * b0[k-1-i]) / e; + } + + rc[0] = b1[0]; +} + +/** + * Quantization of RC coefficients + * rc, maxorder Refelection coefficients, and maximum order (4 or 8) + * order Return order of coefficients + * rc_i Return quantized coefficients + */ +static void quantize_rc(const float *rc, int maxorder, int *order, int *rc_q) +{ + /* Quantization table, sin(delta * (i + 0.5)), delta = Pi / 17, + * rounded to fixed point Q15 value (LC3-Plus HR requirements). */ + + static float q_thr[] = { + 0x0bcfp-15, 0x2307p-15, 0x390ep-15, 0x4d23p-15, + 0x5e98p-15, 0x6cd4p-15, 0x775bp-15, 0x7dd2p-15, + }; + + *order = maxorder; + + for (int i = 0; i < maxorder; i++) { + float rc_m = fabsf(rc[i]); + + rc_q[i] = 4 * (rc_m >= q_thr[4]); + for (int j = 0; j < 4 && rc_m >= q_thr[rc_q[i]]; j++, rc_q[i]++); + + if (rc[i] < 0) + rc_q[i] = -rc_q[i]; + + *order = rc_q[i] != 0 ? maxorder : *order - 1; + } +} + +/** + * Unquantization of RC coefficients + * rc_q, order Quantized coefficients, and order + * rc Return refelection coefficients + */ +static void unquantize_rc(const int *rc_q, int order, float rc[8]) +{ + /* Quantization table, sin(delta * i), delta = Pi / 17, + * rounded to fixed point Q15 value (LC3-Plus HR requirements). */ + + static float q_inv[] = { + 0x0000p-15, 0x1785p-15, 0x2e3dp-15, 0x4362p-15, + 0x563cp-15, 0x6625p-15, 0x7295p-15, 0x7b1dp-15, 0x7f74p-15, + }; + + int i; + + for (i = 0; i < order; i++) { + float rc_m = q_inv[LC3_ABS(rc_q[i])]; + rc[i] = rc_q[i] < 0 ? -rc_m : rc_m; + } +} + + +/* ---------------------------------------------------------------------------- + * Filtering + * -------------------------------------------------------------------------- */ + +/** + * Forward filtering + * dt, bw Duration and bandwidth of the frame + * rc_order, rc Order of coefficients, and coefficients + * x Spectral coefficients, filtered as output + */ +LC3_HOT static void forward_filtering( + enum lc3_dt dt, enum lc3_bandwidth bw, + const int rc_order[2], float (* const rc)[8], float *x) +{ + int nfilters = 1 + (dt >= LC3_DT_5M && bw >= LC3_BANDWIDTH_SWB); + int nf = lc3_ne(dt, (enum lc3_srate)LC3_MIN(bw, LC3_BANDWIDTH_FB)) + >> (nfilters - 1); + int i0, ie = 3*(1 + dt); + + float s[8] = { 0 }; + + for (int f = 0; f < nfilters; f++) { + + i0 = ie; + ie = nf * (1 + f); + + if (!rc_order[f]) + continue; + + for (int i = i0; i < ie; i++) { + float xi = x[i]; + float s0, s1 = xi; + + for (int k = 0; k < rc_order[f]; k++) { + s0 = s[k]; + s[k] = s1; + + s1 = rc[f][k] * xi + s0; + xi += rc[f][k] * s0; + } + + x[i] = xi; + } + } +} + +/** + * Inverse filtering + * dt, bw Duration and bandwidth of the frame + * rc_order, rc Order of coefficients, and unquantized coefficients + * x Spectral coefficients, filtered as output + */ +LC3_HOT static void inverse_filtering( + enum lc3_dt dt, enum lc3_bandwidth bw, + const int rc_order[2], float (* const rc)[8], float *x) +{ + int nfilters = 1 + (dt >= LC3_DT_5M && bw >= LC3_BANDWIDTH_SWB); + int nf = lc3_ne(dt, (enum lc3_srate)LC3_MIN(bw, LC3_BANDWIDTH_FB)) + >> (nfilters - 1); + int i0, ie = 3*(1 + dt); + + float s[8] = { 0 }; + + for (int f = 0; f < nfilters; f++) { + + i0 = ie; + ie = nf * (1 + f); + + if (!rc_order[f]) + continue; + + for (int i = i0; i < ie; i++) { + float xi = x[i]; + + xi -= s[7] * rc[f][7]; + for (int k = 6; k >= 0; k--) { + xi -= s[k] * rc[f][k]; + s[k+1] = s[k] + rc[f][k] * xi; + } + s[0] = xi; + x[i] = xi; + } + + for (int k = 7; k >= rc_order[f]; k--) + s[k] = 0; + } +} + + +/* ---------------------------------------------------------------------------- + * Interface + * -------------------------------------------------------------------------- */ + +/** + * TNS analysis + */ +void lc3_tns_analyze(enum lc3_dt dt, enum lc3_bandwidth bw, + bool nn_flag, int nbytes, struct lc3_tns_data *data, float *x) +{ + /* Processing steps : + * - Determine the LPC (Linear Predictive Coding) Coefficients + * - Check is the filtering is disabled + * - The coefficients are weighted on low bitrates and predicition gain + * - Convert to reflection coefficients and quantize + * - Finally filter the spectral coefficients */ + + float pred_gain[2], a[2][9]; + float rc[2][8]; + + data->lpc_weighting = resolve_lpc_weighting(dt, nbytes); + data->nfilters = 1 + (dt >= LC3_DT_5M && bw >= LC3_BANDWIDTH_SWB); + int maxorder = dt <= LC3_DT_5M ? 4 : 8; + + compute_lpc_coeffs(dt, bw, maxorder, x, pred_gain, a); + + for (int f = 0; f < data->nfilters; f++) { + + data->rc_order[f] = 0; + if (nn_flag || pred_gain[f] <= 1.5f) + continue; + + if (data->lpc_weighting && pred_gain[f] < 2.f) + lpc_weighting(pred_gain[f], a[f]); + + lpc_reflection(a[f], maxorder, rc[f]); + + quantize_rc(rc[f], maxorder, &data->rc_order[f], data->rc[f]); + unquantize_rc(data->rc[f], data->rc_order[f], rc[f]); + } + + forward_filtering(dt, bw, data->rc_order, rc, x); +} + +/** + * TNS synthesis + */ +void lc3_tns_synthesize(enum lc3_dt dt, enum lc3_bandwidth bw, + const struct lc3_tns_data *data, float *x) +{ + float rc[2][8] = { 0 }; + + for (int f = 0; f < data->nfilters; f++) + if (data->rc_order[f]) + unquantize_rc(data->rc[f], data->rc_order[f], rc[f]); + + inverse_filtering(dt, bw, data->rc_order, rc, x); +} + +/** + * Bit consumption of bitstream data + */ +int lc3_tns_get_nbits(const struct lc3_tns_data *data) +{ + int nbits = 0; + + for (int f = 0; f < data->nfilters; f++) { + + int nbits_2048 = 2048; + int rc_order = data->rc_order[f]; + + nbits_2048 += rc_order > 0 ? lc3_tns_order_bits + [data->lpc_weighting][rc_order-1] : 0; + + for (int i = 0; i < rc_order; i++) + nbits_2048 += lc3_tns_coeffs_bits[i][8 + data->rc[f][i]]; + + nbits += (nbits_2048 + (1 << 11) - 1) >> 11; + } + + return nbits; +} + +/** + * Put bitstream data + */ +void lc3_tns_put_data(lc3_bits_t *bits, const struct lc3_tns_data *data) +{ + for (int f = 0; f < data->nfilters; f++) { + int rc_order = data->rc_order[f]; + + lc3_put_bits(bits, rc_order > 0, 1); + if (rc_order <= 0) + continue; + + lc3_put_symbol(bits, + lc3_tns_order_models + data->lpc_weighting, rc_order-1); + + for (int i = 0; i < rc_order; i++) + lc3_put_symbol(bits, + lc3_tns_coeffs_models + i, 8 + data->rc[f][i]); + } +} + +/** + * Get bitstream data + */ +int lc3_tns_get_data(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_bandwidth bw, int nbytes, lc3_tns_data_t *data) +{ + data->nfilters = 1 + (dt >= LC3_DT_5M && bw >= LC3_BANDWIDTH_SWB); + data->lpc_weighting = resolve_lpc_weighting(dt, nbytes); + + for (int f = 0; f < data->nfilters; f++) { + + data->rc_order[f] = lc3_get_bit(bits); + if (!data->rc_order[f]) + continue; + + data->rc_order[f] += lc3_get_symbol(bits, + lc3_tns_order_models + data->lpc_weighting); + if (dt <= LC3_DT_5M && data->rc_order[f] > 4) + return -1; + + for (int i = 0; i < data->rc_order[f]; i++) + data->rc[f][i] = (int)lc3_get_symbol(bits, + lc3_tns_coeffs_models + i) - 8; + } + + return 0; +} diff --git a/App_Resources/iOS/src/lc3/tns.h b/App_Resources/iOS/src/lc3/tns.h new file mode 100644 index 00000000..95d4e672 --- /dev/null +++ b/App_Resources/iOS/src/lc3/tns.h @@ -0,0 +1,93 @@ +/****************************************************************************** + * + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_TNS_H +#define __LC3_TNS_H + +#include "common.h" +#include "bits.h" + + +/** + * Bitstream data + */ + +typedef struct lc3_tns_data { + int nfilters; + bool lpc_weighting; + int rc_order[2]; + int rc[2][8]; +} lc3_tns_data_t; + + +/* ---------------------------------------------------------------------------- + * Encoding + * -------------------------------------------------------------------------- */ + +/** + * TNS analysis + * dt, bw Duration and bandwidth of the frame + * nn_flag True when high energy detected near Nyquist frequency + * nbytes Size in bytes of the frame + * data Return bitstream data + * x Spectral coefficients, filtered as output + */ +void lc3_tns_analyze(enum lc3_dt dt, enum lc3_bandwidth bw, + bool nn_flag, int nbytes, lc3_tns_data_t *data, float *x); + +/** + * Return number of bits coding the data + * data Bitstream data + * return Bit consumption + */ +int lc3_tns_get_nbits(const lc3_tns_data_t *data); + +/** + * Put bitstream data + * bits Bitstream context + * data Bitstream data + */ +void lc3_tns_put_data(lc3_bits_t *bits, const lc3_tns_data_t *data); + + +/* ---------------------------------------------------------------------------- + * Decoding + * -------------------------------------------------------------------------- */ + +/** + * Get bitstream data + * bits Bitstream context + * dt, bw Duration and bandwidth of the frame + * nbytes Size in bytes of the frame + * data Bitstream data + * return 0: Ok -1: Invalid bitstream data + */ +int lc3_tns_get_data(lc3_bits_t *bits, + enum lc3_dt dt, enum lc3_bandwidth bw, int nbytes, lc3_tns_data_t *data); + +/** + * TNS synthesis + * dt, bw Duration and bandwidth of the frame + * data Bitstream data + * x Spectral coefficients, filtered as output + */ +void lc3_tns_synthesize(enum lc3_dt dt, enum lc3_bandwidth bw, + const lc3_tns_data_t *data, float *x); + + +#endif /* __LC3_TNS_H */ diff --git a/App_Resources/iOS/src/module.modulemap b/App_Resources/iOS/src/module.modulemap new file mode 100644 index 00000000..14291369 --- /dev/null +++ b/App_Resources/iOS/src/module.modulemap @@ -0,0 +1,14 @@ +module FaceclawGraphics { + header "FaceclawGraphics.h" + header "FaceclawFontRenderer.h" + header "FaceclawSocket.h" + header "FaceclawLc3Decoder.h" + header "FaceclawSpeech.h" + header "FaceclawLocation.h" + header "FaceclawCalendar.h" + header "FaceclawConfigPort.h" + header "FaceclawEvenHubWebView.h" + header "FaceclawCrypto.h" + header "FaceclawQrScanner.h" + export * +} diff --git a/CHANGELOG b/CHANGELOG index 2ab4edad..e3f76a11 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,13 +1,75 @@ # Changelog -## 0.7.1 - Unreleased +## 0.7.3 - Future +### Depth and Animation + +* A new display-list system for glasses-side drawing, unlocking z-effects and animation +* Menus animate when changing selection and scrolling +* Menus bounce at the ends, and show part of the next row when there is more below +* Fade-in/out effects when turning the screen on/off +* Custom auto-brightness control algorithm, with configurable min, max and curve +* Depth effects for the window switcher, top bar, and modals +* Glanceboard has configurable distance + +### Other Features and Bugfixes + +* Add tokens and faceclaw-input script, which allows using a CLI utility as an input device for the glasses +* Navigate app: Add map-without-destination mode +* Nightscout app: Glanceboard widget still shows out-of-bounds values when out of space +* EvenHub: Fix login form being stuck open without a cancel button +* Calendar: Fix a timezone issue that could cause all-day events to appear on the preceding day +* Compass: Make the calibration readiness visible by default + +### iOS port: + +* Add iOS calendar integration +* iOS main screen matches the Android version's styling +* Hub apps on iOS no longer have their timers stopped while the phone app is in the background + +## 0.7.2 - 2026-09-19 + +* Rebase to firmware 2.3.0, fixing an issue with some R1 ring input events being lost +* Receive lower-level ring events: ring-press as opposed to click, bypass filtering, swipe speeds +* Modify built in games to use ring-press rather than click + +## 0.7.1 - 2026-09-19 + +### iOS Developer Beta + +This release contains a developer beta of an iOS port. Most of the +functionality present in the Android version is working, but it is +considerably buggier, missing some functionality entirely, and generally not +quite ready for non-developer users. To use it at this stage, you will have to +compile it from source code yourself; it is not present in the App Store and +does not have a Testflight. + +Current status: +* Custom firmware flashing: Tested end to end and working +* Voice input: Working. On-device transcription with Siri dictation only; cloud transcription providers are not yet supported. +* AI agent integration: Working. Cloud providers only; on-device LLM is not yet supported. +* Notifications: Working, with rough edges. Notifications appear on the glasses, but they don't have distinguishing icons and the only action you can take on a notification is dismissing it. +* Music player integration: Not yet implemented at all +* Calendar: Not yet implemented at all +* EvenHub: Partially working. Hub apps install and run, sometimes. They freeze when the app is in the background or the phone is locked. + +### Minor Features + +* Mark stock firmware 2.3 as validated for installing custom firmware over +* Add on-device Whisper as a voice transcription option * Add Wear OS watch battery indicator to the top bar and Glanceboard -* Glanceboard tap setting includes a Disable option * Refinements to builtin games -* Optimize bluetooth message transport -* Short-the-long gesture from screen off activates the glanceboard -* Glanceboard has a 2x3 layout option +* Short-then-long gesture from screen off activates the glanceboard +* Compass: No longer discards calibration when off +* Glanceboard: Tap setting includes a Disable option +* Glanceboard: Add a 2x3 layout option +* Convert a large chunk of the codebase from Android-specific Java to cross-platform Kotlin for iOS porting + +### Bluetooth Protocol Changes + +* Custom bluetooth messages now use a new SID, rather than modifying the stock image-update message type +* Compression is done at the transport layer, rather than individual image data buffers +* Expand the texture cache to 256kb ## 0.7.0 - 2026-09-12 diff --git a/CLAUDE.md b/CLAUDE.md index 3ef2a977..7dd039ef 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1,13 +1,16 @@ This is Faceclaw, an Android program that provides user interface on the Even Realities G2 smart glasses. It is written in a mix of Typescript/NativeScript -(for the user interface parts) and Java (for the low-level bluetooth parts and +(for the user interface parts) and Kotlin (for the low-level bluetooth parts and for interfacing with Android SDK). -Typescript parts are in app/. Java parts are in App_Resources/Android/src/main/java/com/faceclaw/app/. +Typescript parts are in app/. Android-specific Kotlin parts are in +App_Resources/Android/src/main/java/com/faceclaw/app/ (compiled by the NativeScript +Android build; only the vendored com.k2fsa.sherpa.onnx JNI classes there remain Java). +Shared Kotlin Multiplatform code is in native/kotlin/shared/ (see native/kotlin/README.md). wear/ is a separate Gradle project (Kotlin + Compose for Wear OS): the watch remote that drives Faceclaw over the Wearable Data Layer. Its phone-side -counterparts are FaceclawWearBridge.java, app/native/wear-bridge.ts and +counterparts are FaceclawWearBridge.kt, app/native/wear-bridge.ts and app/g2/wear-remote.ts; the message format is in wear/PROTOCOL.md. `build.sh` and `build_wear.sh` build the Android and Wear OS apps respectively. diff --git a/README.md b/README.md index b944a4ec..a8f40e87 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,11 @@ walk you through pairing with the glasses and installing custom firmware. ## Compiling +For the NativeScript iOS port, see [iOS development](notes/ios-development.md). +Use `scripts/pull_config_ios.sh` and `scripts/push_config_ios.sh` to transfer iPhone +settings; see [iOS config transfer](scripts/ios-config.md) for options and Android XML support. +The instructions below build the Android app. + If you want to customize faceclaw, the best way to do it is to download the source code and using a coding agent such as Codex or Claude Code. @@ -180,4 +185,3 @@ mention them and link to that provider's privacy policy. For services that involve a user-provided API key, we assume that the user agreed to any terms associated with that service when they generated the key. For services that don't involve API keys, more caution may be required. - diff --git a/app/app.css b/app/app.css index 1d3cfffd..6e2738bf 100644 --- a/app/app.css +++ b/app/app.css @@ -1,5 +1,6 @@ -@import '@nativescript/theme/css/core.css'; +@import '@nativescript/theme/css/core.css'; @import '@nativescript/theme/css/default.css'; +@import './phone-ui/remote-controls.css'; Page { background-color: #ffffff; @@ -171,139 +172,6 @@ Button.-primary { margin-bottom: 12; } -/* The phone's controller for the glasses (phone-ui/remote-controls.xml). */ -.remote-controls { - margin: 0 8 0 8; -} -/* The Settings / Watch / Ring tab bar above the controls. */ -.controls-tab-bar { - margin-bottom: 8; -} -.tab-button { - font-size: 14; - padding: 8 0; - margin: 0 2; - border-radius: 6; - background-color: #e2e2e2; - color: #333333; -} -.tab-button-selected { - background-color: #424242; - color: #ffffff; -} -/* The wakeword and keyboard buttons at the tab bar's right edge: icon-only, - * narrower than the tabs, and never in the selected state (they aren't tabs). - * Each is a button-styled GridLayout (Buttons can't hold images), so it - * borrows the .tab-button look and adds the elevation MaterialButtons get for - * free. */ -.tab-icon-button { - width: 56; - /* The real MaterialButtons render 48dp tall inside a 60dp layout box (6dp - * insets top and bottom); the margins and explicit height reproduce that. - * The height also bounds the tile's measure, without which its star row - * would collapse to content and pin the icon to the top. */ - height: 48; - margin: 6 2; - android-elevation: 2; -} -.tab-icon { - height: 20; - tint-color: #333333; -} -/* Landscape uses the full page height and a shared left edge for tabs/pad. */ -.landscape-layout .remote-controls { - margin: 0; -} -.landscape-layout .controls-tab-bar { - margin-bottom: 2; -} -.landscape-layout .tab-button { - margin-left: 1; - margin-right: 1; - min-width: 0; -} -.landscape-layout .tab-button[col="0"] { - margin-left: 0; -} -.landscape-layout .tab-icon-button { - width: 48; -} -.landscape-layout .tab-icon-button[col="4"] { - margin-right: 0; -} -.ns-dark .tab-icon { - tint-color: #cccccc; -} -.ns-dark .tab-button { - background-color: #333333; - color: #cccccc; -} -.ns-dark .tab-button-selected { - background-color: #e2e2e2; - color: #222222; -} -/* Settings tab rows. */ -.settings-row { - margin-bottom: 8; -} -.settings-row-label { - font-size: 14; - margin-right: 10; -} -.mode-button { - font-size: 13; - padding: 6 10; - margin-right: 6; -} -.brightness-icon { - font-size: 16; - margin: 0 4 0 4; - color: #444444; -} -.auto-toggle Label { - font-size: 13; - margin-right: 2; -} -.touchpad { - height: 230; - background-color: #1f1f1f; - border-radius: 14; - border-width: 1; - border-color: #3a3a3a; - color: #f0f0f0; - margin-top: 2; -} -.pad-focus { - font-size: 20; - color: #ffffff; -} -.pad-legend { - font-size: 11; - color: #8a8a8a; - margin-bottom: 8; -} -.action-row { - margin-top: 6; -} -.action-button { - margin: 2; - font-size: 14; -} -/* Ring tab: the same touchpad, but portrait like the ring's own pad. */ -.ring-touchpad { - width: 150; - height: 250; -} -/* Cosmetic marks mirroring the six rectangles on the physical ring's touch - strip; barely lighter than the pad so they read as texture, not buttons. */ -.ring-pad-marking { - width: 18; - height: 5; - border-radius: 2; - background-color: #2a2a2a; - horizontal-align: center; - vertical-align: center; -} /* * Pairing scan list (phone-ui/pairing-page). Rows are Repeater items: product * art (or a colourway swatch) on the left, decoded SKU + proximity + serial in diff --git a/app/app.ios.css b/app/app.ios.css new file mode 100644 index 00000000..121a6bf7 --- /dev/null +++ b/app/app.ios.css @@ -0,0 +1,206 @@ +@import '@nativescript/theme/css/core.css'; +@import './phone-ui/remote-controls.css'; + +.pairing-instructions { + color: #555555; + font-size: 14; +} + +.pairing-status { + font-size: 14; + margin-left: 8; +} + +.pairing-section { + font-size: 16; + font-weight: bold; + margin-bottom: 4; +} + +.pairing-empty { + color: #777777; + font-size: 14; + padding: 8 0; +} + +.pairing-row { + background-color: #f4f4f4; + border-color: #dddddd; + border-width: 1; + border-radius: 10; + padding: 10; + margin-bottom: 8; +} + +.pairing-row-selected { + background-color: #e6f0ff; + border-color: #3b6fd6; +} + +.pairing-row-incomplete { + background-color: #fafafa; + border-color: #e6e6e6; +} + +.pairing-art { + vertical-align: center; +} + +.pairing-title { + font-size: 16; + font-weight: bold; + color: #222222; +} + +.pairing-badge { + font-size: 11; + font-weight: bold; + color: #2a5bc4; + background-color: #dbe6fb; + border-radius: 9; + padding: 1 7; +} + +.pairing-variant { + font-size: 13; + font-weight: bold; + color: #333333; +} + +.pairing-proximity { + font-size: 12; + color: #666666; +} + +.pairing-proximity-immediate { + color: #2a5bc4; + font-weight: bold; +} + +.pairing-serial { + font-size: 12; + color: #666666; + font-family: monospace; +} + +.pairing-arms { + font-size: 12; + color: #444444; +} + +.pairing-warning { + font-size: 12; + color: #a33a1f; + margin-top: 4; +} + +.pairing-swatch-label { + font-size: 11; + color: #555555; +} + +.pairing-chevron { + font-size: 22; + color: #444444; + margin-left: 6; +} + +.pairing-selection { + background-color: #eef4ff; + border-radius: 10; + padding: 10; +} + +Button.pairing-link { + background-color: transparent; + color: #2a5bc4; + font-size: 14; + border-width: 0; +} + +/* + * Permissions screen (phone-ui/permissions-page). Cards reuse the pairing-row + * / pairing-title / pairing-badge look above; these add the explanation body + * and the already-granted checkmark. + */ +.permissions-description { + font-size: 13; + color: #555555; + margin-top: 2; +} + +.permissions-check { + font-size: 24; + color: #2e7d32; + margin-left: 10; +} + +/* Continue with required permissions still missing: looks disabled but stays + * tappable (the tap raises an are-you-sure prompt instead of being ignored). */ +Button.permissions-primary-dimmed { + opacity: 0.45; +} + + +.body-text { font-size: 18; } +.h1 { font-size: 28; font-weight: bold; } +.footer-link { color: #285c45; padding: 4 8; text-decoration: underline; } + +Page { + background-color: #f5f5f2; + color: #202820; +} + +Button { + background-color: #285c45; + color: #ffffff; + border-radius: 12; + padding: 12 8; + margin-left: 4; + margin-right: 4; + font-size: 15; +} + + +Page.main-page { + background-color: #ffffff; + color: #222222; +} +.main-page .main-header { + background-color: #424242; + color: #ffffff; + padding: 0 0 0 16; + height: 56; +} +.main-page .action-bar-title { font-size: 18; font-weight: bold; color: #ffffff; } +.main-page .action-bar-status { font-size: 14; color: #ffffff; margin-right: 12; } +.main-page .overflow-button { + background-color: transparent; + color: #ffffff; + font-size: 24; + width: 44; + height: 48; + margin: 0; + padding: 0; +} +/* Match Android's button layout boxes, including the inset around each tile. */ +.main-page Button.tab-button { height: 48; margin-top: 6; margin-bottom: 6; } +.main-page .mode-button { background-color: #e2e2e2; color: #333333; border-radius: 6; } +.main-page .ble-bandwidth-indicator { color: #777777; font-size: 12; padding: 2 8; } + +.main-page .system-name-modal { + background-color: #eeeeee; + color: #222222; + padding: 16; +} +.main-page .system-name-modal TextView { + margin: 0; + padding: 4; + background-color: #ffffff; + color: #222222; + placeholder-color: #666666; +} + +.main-landscape .system-name-modal { padding: 6 12; } +.main-landscape .system-name-modal .h2 { visibility: collapse; } +.main-landscape .system-name-modal Button { padding: 6 8; margin-top: 0; margin-bottom: 0; } diff --git a/app/app.ts b/app/app.ts index ceea5419..ea0c6024 100644 --- a/app/app.ts +++ b/app/app.ts @@ -4,27 +4,7 @@ You can use this file to perform app-level initialization, but the primary purpose of the file is to pass control to the app’s first module. */ -import { Application } from '@nativescript/core' -import { registerShareIntentHandler } from './native/share-intents' -import { installNativeUserAgent } from './util/http' -import { registerPhoneRotation } from './native/phone-rotation' -import { startLiveHealthSync, startAlignedRingPull } from './health/health-live' - -installNativeUserAgent() -registerShareIntentHandler() -registerPhoneRotation() - -// Ring health records live only in the communicator's memory until something -// stores them. Both health surfaces store on open, but a pull that lands while -// they are closed would be lost if the process restarted first, so this keeps -// them reaching disk regardless. No-op when there is nothing new. -startLiveHealthSync() - -// Collect from the ring on a wall-clock cadence (:01 and :31). Without this -// nothing drives a pull on a timer at all - only onRingReady() on reconnect - -// so a ring that stays connected is never read. -startAlignedRingPull() - -Application.run({ moduleName: 'app-root' }) +// NativeScript resolves the matching .android.ts or .ios.ts implementation. +import './bootstrap' // Don't place any code after the application has been started diff --git a/app/apps/ai-chat/ai-chat-app.ts b/app/apps/ai-chat/ai-chat-app.ts index 195477a2..256c90af 100644 --- a/app/apps/ai-chat/ai-chat-app.ts +++ b/app/apps/ai-chat/ai-chat-app.ts @@ -1,7 +1,7 @@ import { GrayImage, type UiFont } from "../../graphics/image"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { truncateText, wrapText } from "../../graphics/textwrap"; -import { ASSISTANT_MODEL_VALUES, assistantModelLabel, resolveAssistantModel } from "../../assistant/models"; +import { ASSISTANT_MODEL_CHOICES, assistantModelLabel, resolveAssistantModel } from "../../assistant/models"; import type { AssistantTranscriptEntry } from "../../assistant/session"; import type { AssistantConversations, ReasoningLevel } from "../../assistant/conversations"; import { anthropicApiKeySetting, assistantBackendSetting, openAiApiKeySetting } from "../../ui/dashboard-settings"; @@ -102,7 +102,7 @@ class AiChatLayer implements Layer { } case "long-press": if (!this.conversations.current().session?.isTurnActive()) { - if (!shell.isAssistantAvailable()) { shell.showAlert("Set an API key or download the on-phone model in Settings."); return; } + if (!shell.isAssistantAvailable()) { shell.showAlert(global.isIOS ? "Set an OpenAI or Anthropic API key in Settings." : "Set an API key or download the on-phone model in Settings."); return; } void this.draft.start(); } return; @@ -126,7 +126,7 @@ class AiChatLayer implements Layer { onSelect: (ctx) => { if (this.conversations.select(item.id)) ctx.stack.clearToBase(); }, }))) }, { label: `Model: ${assistantModelLabel(record.model)}`, disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Model", - ASSISTANT_MODEL_VALUES.map((model) => ({ + ASSISTANT_MODEL_CHOICES.map((model) => ({ label: `${model === record.model ? "✓ " : ""}${assistantModelLabel(model)}`, disabled: () => !this.conversations.available(model, record.reasoning), onSelect: (ctx) => { if (this.conversations.configure(model, record.reasoning)) ctx.stack.clearToBase(); }, diff --git a/app/apps/app-definition.ts b/app/apps/app-definition.ts index 0f73ad9d..b2e69a01 100644 --- a/app/apps/app-definition.ts +++ b/app/apps/app-definition.ts @@ -54,6 +54,8 @@ export type GlanceboardProvider = { showOnLongPress: () => boolean; /** Whether the head-tilt wake goes to the board rather than the regular UI. */ showOnHeadTilt: () => boolean; + /** Stereo depth (firmware units; positive is nearer) while the board covers the screen. */ + depth: () => number; /** A fresh board; requestRender asks the host to repaint it. */ createBoard: (requestRender: () => void) => GlanceBoardInstance; }; diff --git a/app/apps/blocks/blocks-app.worker.ts b/app/apps/blocks/blocks-app.worker.ts index 850eedc9..b17adaf0 100644 --- a/app/apps/blocks/blocks-app.worker.ts +++ b/app/apps/blocks/blocks-app.worker.ts @@ -11,12 +11,14 @@ * focus, tap-then-hold opens the window menu. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; import { getFont } from "../../graphics/bdffont"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; +import { playWorkerBuzzerSequence } from "../../native/worker-buzzer"; import { getActiveDisplay } from "../../native/active-display"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; @@ -33,7 +35,6 @@ import { } from "../../ui/gestures"; declare const global: any; -declare const com: any; const largeFont = getFont("terminus32"); const mediumFont = getFont("terminus24"); @@ -187,6 +188,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": { const window: BlocksWindow = { windowId: message.windowId, @@ -295,16 +302,13 @@ function inferForeground(window: BlocksWindow, focused: boolean): void { /** * Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the - * steps on its own timer, and the Java call is safe from the worker thread - * (same path as frame submission). Effects never exceed one message, so no - * phrase pacing is needed. + * steps on its own timer; the platform bridge routes it from the worker. + * Effects never exceed one message, so no phrase pacing is needed. */ function playSfx(window: BlocksWindow, steps: Step[]): void { if (!window.soundOn || steps.length === 0) return; try { - const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); - if (!communicator) return; - communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer); + playWorkerBuzzerSequence(buildSoundSequencePayload(steps)); } catch (error) { console.warn(`blocks sfx failed: ${error}`); } diff --git a/app/apps/calculator/calculator.ts b/app/apps/calculator/calculator.ts index 5f47d785..1a02dfed 100644 --- a/app/apps/calculator/calculator.ts +++ b/app/apps/calculator/calculator.ts @@ -82,10 +82,11 @@ export class CalculatorLayer implements Layer { constructor( private readonly coordinator: MathCoordinator, private readonly actions: LayerActions, + private readonly textInput?: () => Promise, ) { this.coordinator.setUsesDegrees(calculatorDegreesSetting.get()); this.coordinator.onChanged = () => this.handleCoordinatorChanged(); - this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => this.onTranscript(event)); + if (!this.textInput) this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => this.onTranscript(event)); } private get mode(): CalculatorMode { @@ -125,6 +126,7 @@ export class CalculatorLayer implements Layer { } private refreshListening(): void { + if (this.textInput) return; if (!this.foreground) return; if (this.listening === "continuous") { if (!this.continuousActive) { @@ -244,6 +246,7 @@ export class CalculatorLayer implements Layer { * that did not. */ beginDictation(): void { + if (this.textInput) { void this.textInput(); return; } if (this.dictating) return; this.dictating = true; void this.actions.startVoiceCapture(true); @@ -513,7 +516,8 @@ export class CalculatorLayer implements Layer { bodyBottom: number, ): void { const prompt = - this.listening === "continuous" + this.textInput ? "Tap to type a problem on your phone" + : this.listening === "continuous" ? `Say a problem, then "${modeLabel(this.mode).toLowerCase()} it"` : "Tap to say a problem — or use Voice input to type one"; const lines = wrapText(font, prompt, width - 96); @@ -527,7 +531,7 @@ export class CalculatorLayer implements Layer { private paintHeardLine(image: GrayImage, font: UiFont, width: number, y: number): void { if (!this.lastInput) return; - const label = "Heard"; + const label = this.textInput ? "Entered" : "Heard"; image.drawText(font, 24, y, label, 110); const x = 24 + font.measureText(label) + 10; image.drawText(font, x, y, truncateText(font, this.lastInput, width - x - 24), 160); @@ -579,7 +583,7 @@ export class CalculatorLayer implements Layer { this.mode === "explain" && this.coordinator.stepPosition !== null ? "steps" : "mode"; return gestureHints([ [GESTURE_SCROLL, scrollTarget], - [GESTURE_CLICK, "mic"], + [GESTURE_CLICK, this.textInput ? "type" : "mic"], [GESTURE_DOUBLE_CLICK, "back"], ]); } diff --git a/app/apps/calendar/calendar-app.ts b/app/apps/calendar/calendar-app.ts index fddf39fd..80098e9b 100644 --- a/app/apps/calendar/calendar-app.ts +++ b/app/apps/calendar/calendar-app.ts @@ -1,5 +1,5 @@ -import { ensureCalendarPermission, hasCalendarPermission } from "../../g2/android-permissions"; -import { invalidateCalendarCache } from "../../native/calendar"; +import { ensureCalendarPermission, hasCalendarPermission } from "../../native/calendar-permissions"; +import { invalidateCalendarCache, onCalendarChanged } from "../../native/calendar"; import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer"; import { CalendarLayer } from "./calendar"; import { renderCalendarDateIcon } from "./calendar-icon"; @@ -14,25 +14,27 @@ export const CALENDAR_WINDOW_ID = "calendar"; export const CALENDAR_SURFACE_ID = "window:calendar"; /** - * The Calendar app: a single screen listing upcoming events from the Android - * Calendar provider. If calendar permission is missing it shows a prompt and + * The Calendar app: a single screen listing upcoming events from the phone's + * calendars. If calendar permission is missing it shows a prompt and * fires the system permission dialog (on launch and on any tap); once granted * it re-renders with the event list. */ export function createCalendarAppWindow(options: InProcessAppOptions): InProcessWindow { let requesting = false; let app: InProcessWindow; + let unsubscribe = () => {}; + let tick: ReturnType | null = null; const requestPermission = () => { if (requesting || hasCalendarPermission()) return; requesting = true; void ensureCalendarPermission().then((granted) => { - requesting = false; if (granted) { invalidateCalendarCache(); app.requestRender(); } - }); + }).catch(error => console.warn("Calendar permission failed", error)) + .finally(() => { requesting = false; }); }; app = createInProcessWindow({ @@ -48,9 +50,15 @@ export function createCalendarAppWindow(options: InProcessAppOptions): InProcess submitFrame: options.submitFrame, setSurfaceVisible: options.setSurfaceVisible, removeSurface: options.removeSurface, - onClosed: options.onClosed, + onClosed: () => { + unsubscribe(); + if (tick !== null) clearInterval(tick); + options.onClosed(); + }, }); + unsubscribe = onCalendarChanged(app.requestRender); + tick = setInterval(app.requestRender, 30_000); // Prompt immediately on launch so the user doesn't have to discover the tap. requestPermission(); return app; diff --git a/app/apps/calendar/calendar.ts b/app/apps/calendar/calendar.ts index d2cc8629..4272cd97 100644 --- a/app/apps/calendar/calendar.ts +++ b/app/apps/calendar/calendar.ts @@ -2,10 +2,10 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage, type UiFont } from "../../graphics/image"; import { wrapText, truncateText } from "../../graphics/textwrap"; import { clamp } from "../../util/numeric-util"; -import { readUpcomingEvents, type CalendarEvent } from "../../native/calendar"; +import { getCalendarReadState, readUpcomingEvents, type CalendarEvent } from "../../native/calendar"; import { timeFormatSetting } from "../../ui/dashboard-settings"; import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures"; -import { hasCalendarPermission } from "../../g2/android-permissions"; +import { hasCalendarPermission } from "../../native/calendar-permissions"; import { type Layer, type LayerContext } from "../../ui/layers"; import { lineStep } from "../../ui/metrics"; @@ -58,7 +58,9 @@ export class CalendarLayer implements Layer { const events = readUpcomingEvents(MAX_EVENTS); if (!events.length) { image.drawText(font, TITLE_X, TITLE_Y, "Calendar", 220); - image.drawText(font, 24, 72, "No upcoming events.", 190); + const state = getCalendarReadState(); + image.drawText(font, 24, 72, state === "loading" ? "Loading calendar..." + : state === "error" ? "Calendar unavailable. Try again shortly." : "No upcoming events.", 190); return image; } diff --git a/app/apps/compass/compass-app.ts b/app/apps/compass/compass-app.ts index 85e0f287..3c7b9b1c 100644 --- a/app/apps/compass/compass-app.ts +++ b/app/apps/compass/compass-app.ts @@ -21,13 +21,12 @@ import { type InProcessWindow, } from "../../ui/shell/in-process-window"; import { shell } from "../../ui/shell/shell"; -import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions"; +import { ensureLocationPermission, hasLocationPermission } from "../../native/location-permissions"; import { isCompassCalibrated, normalizeHeading } from "./calibration"; import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "./compass-rose"; import { CompassCalibrationLayer } from "./calibration-layer"; import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "./declination"; import { getNorthReference, resolveHeading, setNorthReference, type NorthReference } from "./heading"; -import { compassDebugLines, isCompassDebugEnabled, setCompassDebugEnabled } from "./debug"; export const COMPASS_WINDOW_ID = "compass"; export const COMPASS_SURFACE_ID = "window:compass"; @@ -92,11 +91,6 @@ class CompassLayer implements Layer { this.requestRender(); } - toggleDebugInfo(): void { - setCompassDebugEnabled(!isCompassDebugEnabled()); - this.requestRender(); - } - stop(): void { if (this.removed) return; this.removed = true; @@ -162,17 +156,9 @@ class CompassLayer implements Layer { const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`; const statusLines = wrapText(small, this.statusText(), width - STACK_GAP * 2); const smallStep = lineStep(small); - const debugLines = isCompassDebugEnabled() ? compassDebugLines(this.diagnostics) : []; - const debugWidth = Math.max(0, ...debugLines.map((line) => small.measureText(line))); - const debugGap = debugLines.length ? 12 : 0; - // Keep diagnostics beside the heading even in narrow windows. The rose - // stays centred on the optical axis; the combined readout fits the viewport. - const headingFont = large.measureText(headingText) + debugGap + debugWidth <= width - EDGE_PAD * 2 - ? large : small; - const headingWidth = headingFont.measureText(headingText); - const readoutWidth = headingWidth + debugGap + debugWidth; - const debugHeight = debugLines.length ? (debugLines.length - 1) * smallStep + small.lineHeight : 0; - const readoutHeight = Math.max(headingFont.lineHeight, debugHeight); + const headingFont = large.measureText(headingText) <= width - EDGE_PAD * 2 ? large : small; + const readoutWidth = headingFont.measureText(headingText); + const readoutHeight = headingFont.lineHeight; // One column centred on the display's true centre, so the rose sits where // the wearer is looking rather than 32px right of it. @@ -204,17 +190,14 @@ class CompassLayer implements Layer { const image = this.background.image.clone(); drawCompassRose(image, cx, cy, radius, heading); const readoutX = Math.round(Math.max(EDGE_PAD, Math.min(width - EDGE_PAD - readoutWidth, cx - readoutWidth / 2))); - image.drawText(headingFont, readoutX, y + Math.floor((readoutHeight - headingFont.lineHeight) / 2), - headingText, heading === null ? 150 : 255); - for (let i = 0; i < debugLines.length; i++) { - image.drawText(small, readoutX + headingWidth + debugGap, - y + Math.floor((readoutHeight - debugHeight) / 2) + i * smallStep, debugLines[i]!, 175); - } + image.drawText(headingFont, readoutX, y, headingText, heading === null ? 150 : 255); y += readoutHeight + 4; for (const line of statusLines) { image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125); y += smallStep; } + const accuracyText = magneticAccuracyText(this.diagnostics); + image.drawText(small, width - EDGE_PAD - small.measureText(accuracyText), TOP_PAD, accuracyText, 175); return image; } @@ -255,19 +238,12 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW layer.toggleNorthReference(); }, }, - { - label: `Debug information: ${isCompassDebugEnabled() ? "On" : "Off"}`, - description: "Show magnetic accuracy (0–3), anomaly flags (0–2), and orientation source beside the heading.", - onSelect: (ctx) => { - ctx.stack.pop(); - layer.toggleDebugInfo(); - }, - }, ], baseLayer: new YieldAtRootLayer(layer), submitFrame: options.submitFrame, setSurfaceVisible: options.setSurfaceVisible, removeSurface: options.removeSurface, + reconfigureSurface: options.reconfigureSurface, onClosed: () => { layer.stop(); options.onClosed(); @@ -298,3 +274,15 @@ function frameStatusText(): string { return "Magnetic heading - waiting for location"; } } + +/** + * The firmware's magnetometer calibration readiness (0-3) as filled/empty + * dots, e.g. "Calibration: ●●○" for 2/3. This is the glasses' own sensor + * calibration, not the wearer-fit offset set on the calibration screen. + */ +function magneticAccuracyText(diagnostics: CompassDiagnostics | null): string { + const accuracy = diagnostics && (diagnostics.flags & 0x80) ? diagnostics.magneticAccuracy : -1; + if (accuracy < 0) return "Calibration: --"; + const filled = Math.min(3, accuracy); + return `Calibration: ${"●".repeat(filled)}${"○".repeat(3 - filled)}`; +} diff --git a/app/apps/compass/debug.ts b/app/apps/compass/debug.ts deleted file mode 100644 index 72cdec70..00000000 --- a/app/apps/compass/debug.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { CompassDiagnostics } from "../../native/compass"; -import { getBooleanSetting, setBooleanSetting } from "../../native/settings-store"; - -const DEBUG_KEY = "compass.debugInfo"; - -export function isCompassDebugEnabled(): boolean { - return getBooleanSetting(DEBUG_KEY, false); -} - -export function setCompassDebugEnabled(enabled: boolean): void { - setBooleanSetting(DEBUG_KEY, enabled); -} - -export function compassDebugLines(diagnostics: CompassDiagnostics | null): string[] { - if (!diagnostics) return ["Diagnostics", "unavailable"]; - if (!(diagnostics.flags & 0x80)) return ["Sample data", "unavailable"]; - const accuracy = diagnostics.magneticAccuracy < 0 ? "--" : `${diagnostics.magneticAccuracy}/3`; - const anomalies = diagnostics.magneticAnomalies < 0 ? "--" : String(diagnostics.magneticAnomalies); - const source = ["--", "GRV", "GMRV", "RV"][diagnostics.orientationSource] ?? "--"; - return [`Mag: ${accuracy}`, `Anom: ${anomalies}`, `Src: ${source}`]; -} diff --git a/app/apps/compass/declination.ts b/app/apps/compass/declination.ts index beb9cfe5..a3d04cca 100644 --- a/app/apps/compass/declination.ts +++ b/app/apps/compass/declination.ts @@ -2,16 +2,16 @@ * Local magnetic declination for the compass, so the app can show true north. * * Declination only varies over tens of kilometres, so the location it needs is - * whatever the phone can cheaply produce: the same one-shot coarse lookup the - * Weather app uses, refreshed no more than daily. The last fix is persisted so - * a fresh start has a usable declination before location comes back, and the - * model is re-evaluated at the current time when it does. + * whatever the phone can cheaply produce, refreshed no more than daily. The + * last fix is persisted so a fresh start has a usable correction immediately. + * Android re-evaluates its magnetic model; iOS caches the correction supplied + * by Core Location (true heading minus magnetic heading). */ -import { hasLocationPermission } from "../../g2/android-permissions"; -import { magneticDeclinationDegrees } from "../../native/geomagnetic"; -import { getCurrentLocation } from "../../native/location"; +import { hasLocationPermission } from "../../native/location-permissions"; +import { getCurrentDeclination, restoreDeclination } from "../../native/declination"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; +const DEGREES_KEY = "compass.declination.degrees"; const LATITUDE_KEY = "compass.declination.latitude"; const LONGITUDE_KEY = "compass.declination.longitude"; const LOCATED_AT_KEY = "compass.declination.locatedAtMs"; @@ -40,7 +40,7 @@ function loadStoredFix(): void { const locatedAtMs = parseInt(getStringSetting(LOCATED_AT_KEY, ""), 10); if (Number.isFinite(latitude) && Number.isFinite(longitude) && Number.isFinite(locatedAtMs)) { fix = { latitude, longitude, locatedAtMs }; - declination = magneticDeclinationDegrees(latitude, longitude); + declination = restoreDeclination(latitude, longitude, parseFloat(getStringSetting(DEGREES_KEY, ""))); } } @@ -73,16 +73,17 @@ export function refreshDeclination(): void { loadStoredFix(); if (refreshing || !hasLocationPermission()) return; const now = Date.now(); - if (fix !== null && now - fix.locatedAtMs < FIX_MAX_AGE_MS) return; + if (declination !== null && fix !== null && now - fix.locatedAtMs < FIX_MAX_AGE_MS) return; if (now - lastAttemptMs < RETRY_INTERVAL_MS) return; lastAttemptMs = now; refreshing = true; - getCurrentLocation() + getCurrentDeclination() .then((location) => { - const value = magneticDeclinationDegrees(location.latitude, location.longitude); - if (value === null) return; + const value = location.degrees; + if (!Number.isFinite(value)) return; fix = { latitude: location.latitude, longitude: location.longitude, locatedAtMs: now }; declination = value; + setStringSetting(DEGREES_KEY, String(value)); setStringSetting(LATITUDE_KEY, String(location.latitude)); setStringSetting(LONGITUDE_KEY, String(location.longitude)); setStringSetting(LOCATED_AT_KEY, String(now)); diff --git a/app/apps/developer/bandwidth-benchmark.ts b/app/apps/developer/bandwidth-benchmark.ts index 8af80ceb..192b967e 100644 --- a/app/apps/developer/bandwidth-benchmark.ts +++ b/app/apps/developer/bandwidth-benchmark.ts @@ -2,8 +2,8 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { Layer, type LayerContext } from "../../ui/layers"; import { type InputEvent } from "../../ui/gestures"; -import { drawSelectionHighlight } from "../../ui/menu"; -import { tightRowHeight } from "../../ui/metrics"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, tightRowHeight } from "../../ui/metrics"; import { cancelBandwidthBenchmark, getBandwidthBenchmarkStatus, @@ -29,6 +29,7 @@ const LIST_X = 20; const VALUE_X = 150; type Phase = "config" | "arming" | "running" | "done"; +type ConfigRow = "size" | "window" | "link" | "start"; /** * BLE bandwidth benchmark: pick a message size and pipeline window, stream @@ -38,7 +39,14 @@ type Phase = "config" | "arming" | "running" | "done"; */ export class BandwidthBenchmarkLayer implements Layer { private phase: Phase = "config"; - private selectedRow = 0; // size, window, link preference, start + private readonly menu = new Menu({ + items: ["size", "window", "link", "start"], + wrap: false, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()) + 4, + draw: (args) => this.drawRow(args), + }); private sizeIndex = 2; private windowIndex = 2; private linkModeIndex = 0; @@ -134,26 +142,14 @@ export class BandwidthBenchmarkLayer implements Layer { } const rowH = tightRowHeight(font) + 4; - const rows: Array<[string, string]> = [ - ["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`], - ["Window size", `${WINDOW_SIZES[this.windowIndex]}`], - ["Link request", LINK_MODES[this.linkModeIndex]!], - [this.phase === "done" ? "Run again" : "Start", ""], - ]; - for (let index = 0; index < rows.length; index++) { - const [label, value] = rows[index]!; - const y = HEADER_HEIGHT + index * rowH; - const selected = index === this.selectedRow; - if (selected) { - drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4); - } - image.drawText(font, LIST_X, y + 2, label, selected ? 255 : 200); - if (value) { - image.drawText(font, VALUE_X, y + 2, value, selected ? 235 : 160); - } - } + const rowCount = this.menu.items.length; + this.menu.paint( + image, + { x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: rowCount * rowH }, + ctx.stack.isFocused(), + ); - let y = HEADER_HEIGHT + rows.length * rowH + 10; + let y = HEADER_HEIGHT + rowCount * rowH + 10; if (this.error) { image.drawText(font, LIST_X, y, this.error, 180); } else if (this.result) { @@ -176,6 +172,21 @@ export class BandwidthBenchmarkLayer implements Layer { return image; } + private drawRow({ image, item, x, y, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const [label, value]: [string, string] = + item === "size" ? ["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`] + : item === "window" ? ["Window size", `${WINDOW_SIZES[this.windowIndex]}`] + : item === "link" ? ["Link request", LINK_MODES[this.linkModeIndex]!] + : [this.phase === "done" ? "Run again" : "Start", ""]; + const textY = centeredTextY(font, y, height); + // Text sits 6px in from the row box (LIST_X), values at VALUE_X. + image.drawText(font, x + 6, textY, label, selected ? 255 : 200); + if (value) { + image.drawText(font, x + 6 + VALUE_X - LIST_X, textY, value, selected ? 235 : 160); + } + } + async handleInput(event: InputEvent, ctx: LayerContext): Promise { if (this.phase === "arming" || this.phase === "running") { // A static screen during the run: only allow bailing out. @@ -186,22 +197,22 @@ export class BandwidthBenchmarkLayer implements Layer { } switch (event.type) { case "scroll-up": - this.selectedRow = Math.max(0, this.selectedRow - 1); - return; case "scroll-down": - this.selectedRow = Math.min(3, this.selectedRow + 1); + await this.menu.handleInput(event); return; - case "click": - if (this.selectedRow === 0) { + case "click": { + const row = this.menu.selectedItem; + if (row === "size") { this.sizeIndex = (this.sizeIndex + 1) % MESSAGE_SIZES.length; - } else if (this.selectedRow === 1) { + } else if (row === "window") { this.windowIndex = (this.windowIndex + 1) % WINDOW_SIZES.length; - } else if (this.selectedRow === 2) { + } else if (row === "link") { this.linkModeIndex = (this.linkModeIndex + 1) % LINK_MODES.length; - } else { + } else if (row === "start") { this.startRun(); } return; + } case "double-click": ctx.stack.pop(); return; diff --git a/app/apps/developer/buzzer-demo.ts b/app/apps/developer/buzzer-demo.ts index c88e18c2..1772d555 100644 --- a/app/apps/developer/buzzer-demo.ts +++ b/app/apps/developer/buzzer-demo.ts @@ -1,10 +1,9 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; -import { clamp } from "../../util/numeric-util"; import { Layer, type LayerActions, type LayerContext } from "../../ui/layers"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures"; -import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu"; -import { tightRowHeight } from "../../ui/metrics"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, tightRowHeight } from "../../ui/metrics"; import { buildSoundSequencePayload, CFW_SEQ_MAX, @@ -28,8 +27,14 @@ function sleep(ms: number): Promise { * sequencer message lands as the previous phrase finishes. */ export class BuzzerDemoLayer implements Layer { - private selectedIndex = 0; - private scrollRow = 0; + private readonly menu = new Menu({ + items: SOUND_EFFECTS, + wrap: false, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => drawEffectRow(args), + }); private playing: string | null = null; paint(ctx: LayerContext): GrayImage { @@ -42,21 +47,11 @@ export class BuzzerDemoLayer implements Layer { image.drawText(font, width - 24 - font.measureText(status), 8, status, 140); const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT; - const rowH = tightRowHeight(font); - const visibleRows = Math.max(1, (listHeight / rowH) | 0); - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, SOUND_EFFECTS.length); - - const lastVisible = Math.min(SOUND_EFFECTS.length, this.scrollRow + visibleRows); - for (let index = this.scrollRow; index < lastVisible; index++) { - const effect = SOUND_EFFECTS[index]!; - const y = HEADER_HEIGHT + (index - this.scrollRow) * rowH; - const selected = index === this.selectedIndex; - if (selected) { - drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4); - } - image.drawText(font, LIST_X, y + 1, effect.name, selected ? 255 : 200); - image.drawText(font, LIST_X + 110, y + 1, effect.desc, selected ? 180 : 120); - } + this.menu.paint( + image, + { x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: listHeight }, + ctx.stack.isFocused(), + ); return image; } @@ -64,13 +59,11 @@ export class BuzzerDemoLayer implements Layer { async handleInput(event: InputEvent, ctx: LayerContext): Promise { switch (event.type) { case "scroll-up": - this.selectedIndex = Math.max(0, this.selectedIndex - 1); - return; case "scroll-down": - this.selectedIndex = Math.min(SOUND_EFFECTS.length - 1, this.selectedIndex + 1); + await this.menu.handleInput(event); return; case "click": { - const effect = SOUND_EFFECTS[clamp(this.selectedIndex, 0, SOUND_EFFECTS.length - 1)]; + const effect = this.menu.selectedItem; if (effect && !this.playing) { // Fire-and-forget: playback paces itself with sleeps between // phrases; awaiting it here would stall input for seconds. @@ -106,3 +99,11 @@ export class BuzzerDemoLayer implements Layer { } } } + +/** Effect name, then its description in a second column 110px to the right. */ +function drawEffectRow({ image, item: effect, x, y, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const textY = centeredTextY(font, y, height); + image.drawText(font, x + 6, textY, effect.name, selected ? 255 : 200); + image.drawText(font, x + 6 + 110, textY, effect.desc, selected ? 180 : 120); +} diff --git a/app/apps/developer/developer-app.ts b/app/apps/developer/developer-app.ts index c81d0945..8a8366da 100644 --- a/app/apps/developer/developer-app.ts +++ b/app/apps/developer/developer-app.ts @@ -1,11 +1,14 @@ import { MenuLayer, drawSubmenuIndicator, type MenuItem } from "../../ui/menu"; import { ScreenTestLayer } from "./screen-test"; +import { InputEventsLayer } from "./input-events"; +import { shell } from "../../ui/shell/shell"; import { BuzzerDemoLayer } from "./buzzer-demo"; import { AccelerometerDemoLayer } from "./accelerometer-demo"; import { BandwidthBenchmarkLayer } from "./bandwidth-benchmark"; import { LightSensorDemoLayer } from "./light-sensor-demo"; import { ResourceUsageLayer } from "./resource-usage"; import { LoadAppFromQrLayer, LoadAppFromUrlLayer } from "./load-app"; +import { unicodeTestMenu } from "./unicode-test"; import { type AppContext } from "../app-definition"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { appViewportSize } from "../../ui/shell/geometry"; @@ -47,10 +50,11 @@ function submenuItem(label: string, onSelect: MenuItem["onSelect"]): MenuItem { } /** The diagnostic demos, one level down from the root menu. */ -function debugTestsMenu(): MenuLayer { +function debugTestsMenu(openInputEvents: (ctx: Parameters[0]) => void): MenuLayer { return new MenuLayer( "Debug tests", [ + { label: "Input events", onSelect: openInputEvents }, { label: "Dither test", onSelect: (ctx) => { @@ -59,18 +63,24 @@ function debugTestsMenu(): MenuLayer { }, { label: "Buzzer demo", + disabled: global.isIOS, + description: global.isIOS ? "Glasses sound playback is not available on iOS yet." : undefined, onSelect: (ctx) => { ctx.stack.push(new BuzzerDemoLayer()); }, }, { label: "Accelerometer demo", + disabled: global.isIOS, + description: global.isIOS ? "Not available on iOS yet." : undefined, onSelect: (ctx) => { ctx.stack.push(new AccelerometerDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender)); }, }, { label: "Light sensor", + disabled: global.isIOS, + description: global.isIOS ? "Not available on iOS yet." : undefined, onSelect: (ctx) => { ctx.stack.push(new LightSensorDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender)); }, @@ -81,6 +91,9 @@ function debugTestsMenu(): MenuLayer { ctx.stack.push(new BandwidthBenchmarkLayer(ctx.actions.requestRender)); }, }, + submenuItem("Unicode test", (ctx) => { + ctx.stack.push(unicodeTestMenu(MENU_LAYOUT)); + }), ], MENU_LAYOUT, ); @@ -93,6 +106,7 @@ function debugTestsMenu(): MenuLayer { * under "Debug tests". */ export function createDeveloperAppWindow(appContext: AppContext, options: InProcessAppOptions): InProcessWindow { + let inputEvents: InputEventsLayer | null = null; const menu = new MenuLayer( "Developer", [ @@ -114,12 +128,20 @@ export function createDeveloperAppWindow(appContext: AppContext, options: InProc }, { label: "Show resource usage", + disabled: global.isIOS, + description: global.isIOS ? "Not available on iOS yet." : undefined, onSelect: (ctx) => { ctx.stack.push(new ResourceUsageLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender)); }, }, submenuItem("Debug tests", (ctx) => { - ctx.stack.push(debugTestsMenu()); + ctx.stack.push(debugTestsMenu((inputCtx) => { + const page = new InputEventsLayer(inputCtx.actions.requestRender, + () => shell.isWindowVisible(DEVELOPER_WINDOW_ID), + () => { if (inputEvents === page) inputEvents = null; }); + inputEvents = page; + inputCtx.stack.push(page); + })); }), ], MENU_LAYOUT, @@ -133,6 +155,7 @@ export function createDeveloperAppWindow(appContext: AppContext, options: InProc icon: "wrench", closeable: true, actions: options.actions, + menuItems: () => inputEvents?.menuItems() ?? [], // Dictating a URL is the one thing worth speaking at in this app; the // load pages take the text and every other page ignores it. receiveTextInput: (text) => { diff --git a/app/apps/developer/input-events.ts b/app/apps/developer/input-events.ts new file mode 100644 index 00000000..a779d5a9 --- /dev/null +++ b/app/apps/developer/input-events.ts @@ -0,0 +1,108 @@ +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { GrayImage } from "../../graphics/image"; +import { truncateText } from "../../graphics/textwrap"; +import type { InputEvent } from "../../ui/gestures"; +import { addInputListener, InputEventLog, inputTimestamp } from "../../ui/input-monitor"; +import type { Layer, LayerContext } from "../../ui/layers"; +import { drawListScrollbar, type MenuItem } from "../../ui/menu"; + +/** Gestures are samples here; back/clear/pause live in the tap-then-hold menu. */ +export class InputEventsLayer implements Layer { + readonly acceptsDirectional = true; + readonly log = new InputEventLog(); + private scrollRow = 0; + private phoneTime = false; + private visibleRows = 1; + private readonly unsubscribe: () => void; + + constructor(requestRender: () => void, isVisible: () => boolean, + private readonly removed: () => void = () => {}) { + this.unsubscribe = addInputListener((event, filtered) => { + if (!isVisible() || this.log.paused) return; + this.log.add(event, filtered); + this.scrollRow = 0; + requestRender(); + }); + } + + onRemoved(): void { this.unsubscribe(); this.removed(); } + + menuItems(): MenuItem[] { + return [ + { label: this.log.paused ? "Resume capture" : "Pause capture", onSelect: (ctx) => { + this.log.paused = !this.log.paused; + this.scrollRow = 0; + ctx.stack.pop(); + } }, + { label: "Clear history", onSelect: (ctx) => { + this.log.clear(); + this.scrollRow = 0; + ctx.stack.pop(); + } }, + { label: this.phoneTime ? "Show ring timestamps" : "Show phone receive time", onSelect: (ctx) => { + this.phoneTime = !this.phoneTime; + ctx.stack.pop(); + } }, + { label: "Back to Debug tests", onSelect: (ctx) => { ctx.stack.popThrough(this); } }, + ]; + } + + paint(ctx: LayerContext): GrayImage { + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const font = getDefaultSmallFont(); + image.drawText(font, 12, 8, "Input events", 230); + const status = `${this.log.paused ? "Paused" : "Live"} ${this.log.count} events`; + image.drawText(font, width - 12 - font.measureText(status), 8, status, 180); + image.drawText(font, 12, 28, this.phoneTime ? "Phone receive time | gap ms | source | event" : "Time (R=ring tick) | gap | source | event", 140); + const lineHeight = Math.max(18, font.lineHeight + 2); + const rowHeight = lineHeight * 2 + 4; + this.visibleRows = Math.max(1, Math.floor((height - 76) / rowHeight)); + this.scrollRow = Math.min(this.scrollRow, Math.max(0, this.log.entries.length - this.visibleRows)); + const timeX = 12; + const gapX = timeX + font.measureText("00:00:00.000") + 12; + const sourceX = gapX + font.measureText("+99999") + 12; + const eventX = sourceX + font.measureText("right-arm") + 12; + if (!this.log.entries.length) { + image.drawText(font, 12, 62, "Touch, tap, double-tap, hold, or swipe the ring.", 210); + image.drawText(font, 12, 82, "Press arrives before an interpreted gesture.", 150); + } + for (const [index, entry] of this.log.entries.slice(this.scrollRow, this.scrollRow + this.visibleRows).entries()) { + const { event } = entry; + const ringTime = !this.phoneTime && event.ringInput; + const gapMs = ringTime ? entry.ringGapTicks : entry.gapMs; + const y = 50 + index * rowHeight; + const gap = gapMs === null ? "-" : `${gapMs >= 0 ? "+" : ""}${gapMs}`; + const source = "source" in event ? event.source ?? "-" : "-"; + const label = (entry.filtered ? "*" : "") + (event.type === "unknown" + ? event.ringInput ? `ring type ${event.ringInput.type}` : `${event.kind} ${event.eventSource}/${event.eventType}` + : event.type); + image.drawText(font, timeX, y, ringTime ? `R${ringTime.tick}` : inputTimestamp(event.timestampMs), 170); + image.drawText(font, gapX, y, truncateText(font, gap, sourceX - gapX - 4), 160); + image.drawText(font, sourceX, y, source, 180); + image.drawText(font, eventX, y, truncateText(font, label, width - eventX - 12), event.type === "ring-press" ? 255 : 200); + if (event.ringInput) { + const { type, aux, speed } = event.ringInput; + // These are the original report bytes, not physical distance/speed units. + const details = `Raw ring: type ${type} AUX ${aux} speed ${speed}`; + image.drawText(font, timeX + 12, y + lineHeight, + truncateText(font, details, width - timeX - 36), 170); + } + } + if (this.log.paused) drawListScrollbar(image, width - 5, 50, height - 78, + this.scrollRow, this.visibleRows, this.log.entries.length); + image.drawText(font, 12, height - 20, + this.log.paused ? "Scroll: history Tap-hold: controls" : "Unfiltered (*=dedup) Tap-hold: controls", 140); + return image; + } + + handleInput(event: InputEvent): void { + // Live gestures, including double-click, must remain on this page. + if (!this.log.paused) return; + if (event.type === "scroll-up" || event.type === "swipe-up") { + this.scrollRow = Math.max(0, this.scrollRow - 1); + } else if (event.type === "scroll-down" || event.type === "swipe-down") { + this.scrollRow = Math.min(Math.max(0, this.log.entries.length - this.visibleRows), this.scrollRow + 1); + } + } +} diff --git a/app/apps/developer/load-app.ts b/app/apps/developer/load-app.ts index f8ac5c2f..ba6c9555 100644 --- a/app/apps/developer/load-app.ts +++ b/app/apps/developer/load-app.ts @@ -7,7 +7,7 @@ import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/g import { Layer, type LayerContext } from "../../ui/layers"; import { lineStep } from "../../ui/metrics"; import { developerAppUrlSetting } from "../../ui/dashboard-settings"; -import { isQrScannerAvailable, scanQrCode } from "../../native/qr-scan"; +import { cancelQrScan, isQrScannerAvailable, scanQrCode } from "../../native/qr-scan"; const MARGIN = 20; @@ -114,20 +114,23 @@ export class LoadAppFromUrlLayer implements Layer { } /** - * "Load app from QR code": hand off to an installed scanner app, then launch + * "Load app from QR code": open the phone's scanner, then launch * whatever URL it decodes. The scanned URL is kept in developerAppUrlSetting so * the URL page can reload it later without rescanning. */ export class LoadAppFromQrLayer implements Layer { private status = "Starting scanner..."; private busy = false; + private removed = false; constructor(private readonly ctx: AppContext) {} /** Launches the scanner; call right after pushing the layer. */ open(ctx: LayerContext): void { + if (this.busy || this.removed) return; if (!isQrScannerAvailable()) { - this.status = "No QR scanner on this phone (needs Play Services or a scanner app)."; + this.status = global.isIOS ? "No camera available. Scan using a physical iPhone." + : "No QR scanner on this phone (needs Play Services or a scanner app)."; ctx.actions.requestRender(); return; } @@ -135,6 +138,11 @@ export class LoadAppFromQrLayer implements Layer { void this.scan(ctx); } + onRemoved(): void { + this.removed = true; + if (this.busy) cancelQrScan(); + } + paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); const { width, height } = ctx.stack.getBaseSize(); @@ -156,12 +164,7 @@ export class LoadAppFromQrLayer implements Layer { handleInput(event: InputEvent, ctx: LayerContext): void { switch (event.type) { case "click": - if (!this.busy && isQrScannerAvailable()) { - this.busy = true; - this.status = "Starting scanner..."; - ctx.actions.requestRender(); - void this.scan(ctx); - } + this.open(ctx); return; case "double-click": ctx.stack.pop(); @@ -176,6 +179,7 @@ export class LoadAppFromQrLayer implements Layer { this.status = "Point the phone camera at the QR code."; ctx.actions.requestRender(); const scanned = await scanQrCode(); + if (this.removed) return; if (scanned === null) { this.status = "Scan cancelled."; return; @@ -184,17 +188,18 @@ export class LoadAppFromQrLayer implements Layer { this.status = `Not a URL: ${scanned}`; return; } - developerAppUrlSetting.set(scanned); + developerAppUrlSetting.set(scanned.trim()); this.status = `Loading ${scanned}`; ctx.actions.requestRender(); await openEvenHubUrl(this.ctx, scanned); - ctx.stack.pop(); + if (!this.removed) ctx.stack.pop(); } catch (error) { + if (this.removed) return; this.status = cleanError(error); this.ctx.appendLog(`evenhub qr launch failed: ${this.status}`); } finally { this.busy = false; - ctx.actions.requestRender(); + if (!this.removed) ctx.actions.requestRender(); } } } diff --git a/app/apps/developer/unicode-test.ts b/app/apps/developer/unicode-test.ts new file mode 100644 index 00000000..35c3ff37 --- /dev/null +++ b/app/apps/developer/unicode-test.ts @@ -0,0 +1,283 @@ +import { wrapText, truncateText } from "../../graphics/textwrap"; +import { fontSelectionLabel, getDefaultSmallFont, getUiFontSelection } from "../../graphics/ui-fonts"; +import { GrayImage, type UiFont } from "../../graphics/image"; +import { type InputEvent } from "../../ui/gestures"; +import { Layer, type LayerContext } from "../../ui/layers"; +import { MenuLayer, type MenuLayout } from "../../ui/menu"; + +/** + * Unicode test: sample texts chosen to exercise text-rendering features that + * need special support beyond one-glyph-per-codepoint left-to-right layout. + * Each sample is rendered with the user's configured UI font so gaps (tofu, + * unshaped Arabic, misplaced combining marks, reversed RTL runs, missing CJK + * coverage) show up the way they would in real app content. + * + * Every sample lists what it exercises so a tester knows what "correct" looks + * like without reading this file. + */ +export type UnicodeSample = { + /** Menu label: the language, with the feature in parentheses. */ + label: string; + /** What a correct rendering has to get right, shown under the title. */ + exercises: string; + /** The text itself; paragraphs separated by "\n". */ + text: string; +}; + +// Sentences are mostly Article 1 of the Universal Declaration of Human Rights +// (the same passage in every language, so a tester who knows one can compare +// line lengths and shapes across scripts), plus a pangram or two. +export const UNICODE_SAMPLES: readonly UnicodeSample[] = [ + { + label: "English (ligatures)", + exercises: "fi/fl/ff pairs stay separate glyphs; kerning (AV, To, Wa)", + text: + "All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.\n" + + "Ligature pairs: office affluent waffle fjord shuffle difficult\n" + + "Kerning pairs: AVATAR Toward Wavy LTA. Yo Te Ty\n" + + "The quick brown fox jumps over the lazy dog. 0123456789", + }, + { + label: "French (precomposed accents)", + exercises: "é è ê ç œ æ ù as single precomposed codepoints", + text: + "Tous les êtres humains naissent libres et égaux en dignité et en droits. Ils sont doués de raison et de conscience et doivent agir les uns envers les autres dans un esprit de fraternité.\n" + + "Portez ce vieux whisky au juge blond qui fume. Œuvre, cœur, æther, à côté, où, naïve, Noël.", + }, + { + label: "German / Polish / Turkish", + exercises: "ß ä ö ü; ą ę ł ń ś ź ż; dotted İ and dotless ı", + text: + "Falsches Üben von Xylophonmusik quält jeden größeren Zwerg. Straße, Fußgänger, Ärger, Öl, Übung.\n" + + "Pchnąć w tę łódź jeża lub ośm skrzyń fig. Zażółć gęślą jaźń.\n" + + "Pijamalı hasta yağız şoföre çabucak güvendi. İstanbul, ılık, Iğdır, iyi.", + }, + { + label: "Vietnamese (stacked marks)", + exercises: "tone mark above a vowel that already has a diacritic (ấ ệ ở ữ)", + text: + "Tất cả mọi người sinh ra đều được tự do và bình đẳng về nhân phẩm và quyền lợi. Mọi con người đều được tạo hóa ban cho lý trí và lương tâm và cần phải đối xử với nhau trong tình bằng hữu.\n" + + "Tiếng Việt: Hà Nội, Huế, Đà Nẵng, Sài Gòn. ấ ầ ẩ ẫ ậ ế ề ể ễ ệ ố ồ ổ ỗ ộ ớ ờ ở ỡ ợ ứ ừ ử ữ ự", + }, + { + label: "Combining marks (decomposed)", + exercises: "base letter + separate combining accent must overlay, not advance", + text: + // Written with explicit combining codepoints (no String.normalize, so + // the sample is the same on every JS engine). + "Decomposed: café naïve résumé Việt Nam Saõ Paulo\n" + + "Precomposed: café naïve résumé Việt Nam São Paulo\n" + + "Both lines should look identical.\n" + + "Stacked: à́̂ ë̄ ô̧ ñ́ ů̈\n" + + "Marks with no base: ́ ̈ ̧ (should attach to nothing, or a dotted circle)", + }, + { + label: "Greek", + exercises: "Greek alphabet with tonos/dialytika; final sigma ς vs σ", + text: + "Όλοι οι άνθρωποι γεννιούνται ελεύθεροι και ίσοι στην αξιοπρέπεια και τα δικαιώματα. Είναι προικισμένοι με λογική και συνείδηση, και οφείλουν να συμπεριφέρονται μεταξύ τους με πνεύμα αδελφοσύνης.\n" + + "Ξεσκεπάζω την ψυχοφθόρα βδελυγμία. ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ αβγδεζηθικλμνξοπρσςτυφχψω", + }, + { + label: "Russian (Cyrillic)", + exercises: "Cyrillic coverage incl. й ё ъ ь; italic-free shapes", + text: + "Все люди рождаются свободными и равными в своем достоинстве и правах. Они наделены разумом и совестью и должны поступать в отношении друг друга в духе братства.\n" + + "Съешь же ещё этих мягких французских булок, да выпей чаю. АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ", + }, + { + label: "Arabic (RTL, joining)", + exercises: "right-to-left; letters take initial/medial/final forms; lam-alef لا ligature", + text: + "يولد جميع الناس أحرارًا متساوين في الكرامة والحقوق. وقد وهبوا عقلاً وضميرًا وعليهم أن يعامل بعضهم بعضًا بروح الإخاء.\n" + + "السلام عليكم. العربية لغة جميلة.\n" + + "Arabic-Indic digits: ٠١٢٣٤٥٦٧٨٩ and Persian: پ چ ژ گ", + }, + { + label: "Hebrew (RTL, niqqud)", + exercises: "right-to-left; vowel points (niqqud) below/inside letters; final forms ך ם ן ף ץ", + text: + "כל בני אדם נולדו בני חורין ושווים בערכם ובזכויותיהם. כולם חוננו בתבונה ובמצפון, לפיכך חובה עליהם לנהוג איש ברעהו ברוח של אחוה.\n" + + "With niqqud: שָׁלוֹם עוֹלָם. בְּרֵאשִׁית בָּרָא אֱלֹהִים\n" + + "Final forms: מלך שלום דין סוף ארץ", + }, + { + label: "Bidirectional (mixed)", + exercises: "RTL runs inside LTR lines and vice versa; numbers and punctuation keep their side", + text: + "The Hebrew word שלום means peace, and the Arabic word سلام means the same.\n" + + "Price: 120 ש\"ח (about $32) — call 555-1234.\n" + + "מספר הטלפון שלי הוא 052-1234567, ואני גר ב-Tel Aviv.\n" + + "أعيش في London منذ 5 سنوات.\n" + + "Nested: he said \"مرحبا (hello) بكم\" and left.", + }, + { + label: "Hindi (Devanagari)", + exercises: "conjuncts (क्ष त्र ज्ञ), vowel sign ि drawn BEFORE its consonant, headline bar", + text: + "सभी मनुष्यों को गौरव और अधिकारों के मामले में जन्मजात स्वतन्त्रता और समानता प्राप्त है। उन्हें बुद्धि और अन्तरात्मा की देन प्राप्त है और परस्पर उन्हें भाईचारे के भाव से बर्ताव करना चाहिए।\n" + + "Reordering: कि (ka + i) की (ka + ii) दिल्ली हिन्दी\n" + + "Conjuncts: क्ष त्र ज्ञ द्ध श्र प्र स्त्री", + }, + { + label: "Thai (no spaces, stacked)", + exercises: "no inter-word spaces (wrap by syllable); vowels and tone marks stack above/below", + text: + "มนุษย์ทั้งหลายเกิดมามีอิสระและเสมอภาคกันในเกียรติศักดิ์และสิทธิ ต่างมีเหตุผลและมโนธรรม และควรปฏิบัติต่อกันด้วยเจตนารมณ์แห่งภราดรภาพ\n" + + "สวัสดีครับ ภาษาไทย กรุงเทพมหานคร\n" + + "Stacked marks: ปั้น ที่ กี้ น้ำ ป์ ฐ์", + }, + { + label: "Chinese (Simplified)", + exercises: "CJK coverage; full-width punctuation ,。;wrapping between any two characters", + text: + "人人生而自由,在尊严和权利上一律平等。他们赋有理性和良心,并应以兄弟关系的精神相对待。\n" + + "你好,世界!中文测试。繁體:臺灣、香港、澳門。\n" + + "Mixed: 我有 3 个 apples 和 2 个 oranges。", + }, + { + label: "Japanese (kana + kanji)", + exercises: "hiragana, katakana, kanji, half-width katakana カタカナ, long vowel mark ー", + text: + "すべての人間は、生れながらにして自由であり、かつ、尊厳と権利とについて平等である。人間は、理性と良心とを授けられており、互いに同胞の精神をもって行動しなければならない。\n" + + "いろはにほへと ちりぬるを。コンピューター、テレビ、ゲーム。\n" + + "半角カナ: コンピューター 全角英数: ABC123", + }, + { + label: "Korean (Hangul)", + exercises: "precomposed syllables; conjoining jamo (ㅎㅏㄴ) should compose to 한", + text: + "모든 인간은 태어날 때부터 자유로우며 그 존엄과 권리에 있어 동등하다. 인간은 천부적으로 이성과 양심을 부여받았으며 서로 형제애의 정신으로 행동하여야 한다.\n" + + "안녕하세요, 세계! 한국어 테스트.\n" + + "Jamo: 한 (= 한) Compatibility jamo: ㅎ ㅏ ㄴ", + }, + { + label: "Emoji (ZWJ, modifiers)", + exercises: "color emoji as monochrome; skin-tone modifiers, ZWJ families, flag pairs, VS16 render as ONE glyph", + text: + "Basic: 😀 👍 ❤ ☀ ★\n" + + "Variation selector: ❤️ ☀️ ✈️ (same as above but VS16)\n" + + "Skin tone: 👍🏻 👍🏽 👍🏿 👋🏾\n" + + "ZWJ: 👨‍👩‍👧‍👦 👩‍💻 🏳️‍🌈\n" + + "Flags: 🇯🇵 🇺🇸 🇩🇪 🇧🇷\n" + + "Keycaps: 1️⃣ #️⃣", + }, + { + label: "Symbols & misc", + exercises: "math, arrows, box drawing, currency, non-BMP (astral) codepoints", + text: + "Math: ∑ ∫ √ ∞ ≠ ≤ ≥ ± × ÷ π θ λ\n" + + "Arrows: ← ↑ → ↓ ↔ ⇒ ⇔\n" + + "Box: ┌─┬─┐ │ │ │ ├─┼─┤ └─┴─┘ ▲▼● ░▒▓█\n" + + "Currency: $ € £ ¥ ₹ ₩ ₪ ¢\n" + + "Astral: 𝔘𝔫𝔦𝔠𝔬𝔡𝔢 𝕳𝖊𝖑𝖑𝖔 𐐀𐐁 🜁🜂\n" + + "Zero-width: a​b (ZWSP) c‍d (ZWJ) e⁠f (WJ) should read as ab cd ef", + }, +]; + +const MARGIN_X = 18; +const TITLE_Y = 8; +const FOOTER_MARGIN = 20; +const BODY_GAP = 6; + +/** + * Shows one Unicode sample wrapped to the page, with the feature under test + * under the title and the current font in the footer. Scroll pages through + * the text; double-click goes back to the language list. + */ +export class UnicodeSampleLayer implements Layer { + private lines: string[] | null = null; + private wrappedForWidth = 0; + private wrappedWithFont = 0; + private firstLine = 0; + private bodyLineCount = 1; + + constructor(private readonly sample: UnicodeSample) {} + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const textWidth = width - 2 * MARGIN_X; + + image.drawText(font, MARGIN_X, TITLE_Y, truncateText(font, this.sample.label, textWidth), 220); + const noteY = TITLE_Y + font.lineHeight; + const noteLines = wrapText(font, this.sample.exercises, textWidth); + for (let i = 0; i < noteLines.length; i++) { + image.drawText(font, MARGIN_X, noteY + i * font.lineHeight, noteLines[i]!, 120); + } + + const bodyY = noteY + noteLines.length * font.lineHeight + BODY_GAP; + const footerY = height - FOOTER_MARGIN; + this.bodyLineCount = Math.max(1, Math.floor((footerY - bodyY) / font.lineHeight)); + + const lines = this.getLines(font, textWidth); + this.clampScroll(lines.length); + const visible = lines.slice(this.firstLine, this.firstLine + this.bodyLineCount); + for (let i = 0; i < visible.length; i++) { + image.drawText(font, MARGIN_X, bodyY + i * font.lineHeight, visible[i]!, 235); + } + + const page = Math.floor(this.firstLine / this.pageStep()) + 1; + image.drawText(font, MARGIN_X, footerY, `Page ${page}/${this.pageCount(lines.length)}`, 110); + const fontLabel = fontSelectionLabel(getUiFontSelection()); + image.drawText(font, width - MARGIN_X - font.measureText(fontLabel), footerY, fontLabel, 110); + return image; + } + + handleInput(event: InputEvent, ctx: LayerContext): void { + switch (event.type) { + case "scroll-down": + this.firstLine += this.pageStep(); + return; + case "scroll-up": + this.firstLine = Math.max(0, this.firstLine - this.pageStep()); + return; + case "double-click": + ctx.stack.pop(); + return; + } + } + + private pageStep(): number { + return Math.max(1, this.bodyLineCount - 1); + } + + private clampScroll(lineCount: number): void { + if (lineCount <= this.bodyLineCount) { + this.firstLine = 0; + return; + } + const lastPageStart = (this.pageCount(lineCount) - 1) * this.pageStep(); + this.firstLine = Math.max(0, Math.min(lastPageStart, this.firstLine)); + } + + private pageCount(lineCount: number): number { + if (lineCount <= this.bodyLineCount) return 1; + return Math.ceil((lineCount - this.bodyLineCount) / this.pageStep()) + 1; + } + + private getLines(font: UiFont, width: number): string[] { + if (this.lines === null || this.wrappedForWidth !== width || this.wrappedWithFont !== font.fingerprintId) { + // breakLongWords: CJK and Thai have no spaces to wrap at. + this.lines = wrapText(font, this.sample.text, width, { breakLongWords: true }); + this.wrappedForWidth = width; + this.wrappedWithFont = font.fingerprintId; + } + return this.lines; + } +} + +/** The language list; selecting a row opens its sample. */ +export function unicodeTestMenu(layout: MenuLayout): MenuLayer { + return new MenuLayer( + "Unicode test", + UNICODE_SAMPLES.map((sample) => ({ + label: sample.label, + onSelect: (ctx) => { + ctx.stack.push(new UnicodeSampleLayer(sample)); + }, + })), + layout, + ); +} diff --git a/app/apps/evenhub/api-key-dialog.ts b/app/apps/evenhub/api-key-dialog.ts index bd41fec8..825357bf 100644 --- a/app/apps/evenhub/api-key-dialog.ts +++ b/app/apps/evenhub/api-key-dialog.ts @@ -3,8 +3,8 @@ import { GrayImage } from "../../graphics/image"; import { truncateText } from "../../graphics/textwrap"; import { type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers"; -import { drawSelectionHighlight } from "../../ui/menu"; -import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { lineStep, listRowHeight } from "../../ui/metrics"; const DIALOG_X = 8; const DIALOG_Y = 8; @@ -21,6 +21,8 @@ const SERVICE_LABELS: Record = { export type ApiKeyRequestItem = { id: string; configured: boolean }; +type ConsentAction = "Allow" | "Deny"; + /** * Consent dialog shown when an EvenHub app calls requestApiKeyAccess: lists the * requested API-key services (marking any the user hasn't configured) and asks @@ -28,8 +30,14 @@ export type ApiKeyRequestItem = { id: string; configured: boolean }; * double-click) shares nothing. */ export class EvenHubApiKeyDialogLayer implements Layer { - /** 0 = Allow, 1 = Deny. */ - private selectedIndex = 0; + private readonly menu = new Menu({ + items: ["Allow", "Deny"], + wrap: true, + rowGap: 1, + highlight: { radius: 8 }, + getHeight: () => listRowHeight(getDefaultSmallFont()), + draw: (args) => drawActionRow(args), + }); private resolved = false; constructor( @@ -67,16 +75,11 @@ export class EvenHubApiKeyDialogLayer implements Layer { } y += 4; - const focused = ctx.stack.isFocused(); - const actions = ["Allow", "Deny"]; - for (let index = 0; index < actions.length; index++) { - const rowY = y + index * actionRowH; - const selected = index === this.selectedIndex; - if (selected) { - drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8); - } - image.drawText(font, DIALOG_X + 22, rowY + 3, actions[index]!, selected ? 255 : 200); - } + this.menu.paint( + image, + { x: DIALOG_X + 12, y, width: DIALOG_WIDTH - 24, height: Math.max(0, DIALOG_Y + height - PADDING - y) }, + ctx.stack.isFocused(), + ); return image; } @@ -84,10 +87,10 @@ export class EvenHubApiKeyDialogLayer implements Layer { switch (event.type) { case "scroll-up": case "scroll-down": - this.selectedIndex = (this.selectedIndex + 1) % 2; + void this.menu.handleInput(event); return; case "click": - this.resolve(ctx, this.selectedIndex === 0); + this.resolve(ctx, this.menu.selectedItem === "Allow"); return; case "double-click": this.resolve(ctx, false); @@ -113,3 +116,7 @@ export class EvenHubApiKeyDialogLayer implements Layer { else this.onCancel(); } } + +function drawActionRow({ image, item, x, y, selected }: MenuDrawArgs): void { + image.drawText(getDefaultSmallFont(), x + 10, y + 3, item, selected ? 255 : 200); +} diff --git a/app/apps/evenhub/compass-router.ts b/app/apps/evenhub/compass-router.ts index 0d8e7910..e9a33d46 100644 --- a/app/apps/evenhub/compass-router.ts +++ b/app/apps/evenhub/compass-router.ts @@ -80,7 +80,7 @@ class EvenHubCompassRouter { }); } if (!this.enabled) { - setCompassEnabled(true); + setCompassEnabled(true, "evenhub"); this.enabled = true; // Uses location only if the wearer already granted it; an app turning // on the compass is not a reason to raise a system permission prompt. @@ -88,7 +88,7 @@ class EvenHubCompassRouter { } } else { if (this.enabled) { - setCompassEnabled(false); + setCompassEnabled(false, "evenhub"); this.enabled = false; } this.unsubscribe?.(); diff --git a/app/apps/evenhub/ehpk.ts b/app/apps/evenhub/ehpk.ts index f082a2be..e7f56bae 100644 --- a/app/apps/evenhub/ehpk.ts +++ b/app/apps/evenhub/ehpk.ts @@ -18,6 +18,11 @@ const RECORD_FILE = 0xe4; const RECORD_DIR = 0xe5; const RECORD_FOOTER = 0xe3; +/** Archive paths are relative to one package, never to the phone container. */ +export function safeEhpkPath(value: string): boolean { + return !!value && !/[\\\u0000:]/.test(value) && value.split('/').every(part => !!part && part !== '.' && part !== '..'); +} + export type EhpkArchive = { /** Relative path (e.g. "app.json", "dist/index.html") to contents. */ files: Map; @@ -71,41 +76,55 @@ export function utf8Decode(bytes: Uint8Array): string { return out; } -export function parseEhpk(data: Uint8Array): EhpkArchive { - if (data.length < 20 || data[0] !== 0x45 || data[1] !== 0x48 || data[2] !== 0x50 || data[3] !== 0x4b) { +/** Index compressed records without expanding assets merely to read app.json. */ +export function openEhpk(data: Uint8Array) { + if (data.length < 20 || data[0] !== 0x45 || data[1] !== 0x48 || data[2] !== 0x50 || data[3] !== 0x4b) throw new Error("not an EHPK file"); - } const view = new DataView(data.buffer, data.byteOffset, data.byteLength); - const firstRecord = view.getUint32(8, true); - const files = new Map(); - - let p = firstRecord; + const records = new Map(); + const requireBytes = (offset: number, length: number) => { + if (offset < 20 || length < 0 || offset + length > data.length) throw new Error("ehpk: truncated record"); + }; + let p = view.getUint32(8, true); + requireBytes(p, 0); while (p < data.length) { + requireBytes(p, 4); const type = data[p]!; - if (data[p + 1] !== 0xba || data[p + 2] !== 0xa9 || data[p + 3] !== 0xba) { + if (data[p + 1] !== 0xba || data[p + 2] !== 0xa9 || data[p + 3] !== 0xba) throw new Error(`ehpk: bad record magic at ${p}`); - } if (type === RECORD_FILE) { - const compressedLength = view.getUint32(p + 4, true); - const uncompressedLength = view.getUint32(p + 8, true); + requireBytes(p, 16); + const compressed = view.getUint32(p + 4, true), size = view.getUint32(p + 8, true); const nameLength = view.getUint16(p + 14, true); + requireBytes(p + 16, nameLength + compressed); const name = utf8Decode(unxor(data, p + 16, nameLength)); - const blob = unxor(data, p + 16 + nameLength, compressedLength); - const content = decompress(blob, new Uint8Array(uncompressedLength)); - if (content.length !== uncompressedLength) { - throw new Error(`ehpk: ${name}: expected ${uncompressedLength} bytes, got ${content.length}`); - } - files.set(name, content); - p += 16 + nameLength + compressedLength; + if (!safeEhpkPath(name)) throw new Error(`ehpk: invalid file path ${name}`); + records.set(name, { offset: p + 16 + nameLength, compressed, size }); + p += 16 + nameLength + compressed; } else if (type === RECORD_DIR) { - const nameLength = view.getUint16(p + 6, true); - p += 8 + nameLength; - } else if (type === RECORD_FOOTER) { - break; - } else { - throw new Error(`ehpk: unknown record type 0x${type.toString(16)} at ${p}`); - } + requireBytes(p, 8); + const length = view.getUint16(p + 6, true); + requireBytes(p + 8, length); p += 8 + length; + } else if (type === RECORD_FOOTER) break; + else throw new Error(`ehpk: unknown record type 0x${type.toString(16)} at ${p}`); } + return { files: { + keys: () => records.keys(), + has: (name: string) => records.has(name), + get: (name: string): Uint8Array | undefined => { + const record = records.get(name); + if (!record) return undefined; + const content = decompress(unxor(data, record.offset, record.compressed), new Uint8Array(record.size)); + if (content.length !== record.size) throw new Error(`ehpk: ${name}: expected ${record.size} bytes, got ${content.length}`); + return content; + }, + } }; +} +export type EhpkIndex = ReturnType; + +export function parseEhpk(data: Uint8Array): EhpkArchive { + const index = openEhpk(data), files = new Map(); + for (const name of index.files.keys()) files.set(name, index.files.get(name)!); return { files }; } @@ -119,6 +138,7 @@ export function parseManifest(appJsonText: string): EvenHubManifest { const name = String(raw.name ?? packageId); const version = String(raw.version ?? "0"); const entrypoint = String(raw.entrypoint ?? "index.html"); + if (!safeEhpkPath(entrypoint)) throw new Error('Invalid EHPK entrypoint'); const permissions = parsePermissions(raw.permissions); const privacyPolicyUrl = firstSafeHttpsUrl( diff --git a/app/apps/evenhub/even-api.ts b/app/apps/evenhub/even-api.ts index 971a10ec..051568f9 100644 --- a/app/apps/evenhub/even-api.ts +++ b/app/apps/evenhub/even-api.ts @@ -5,16 +5,17 @@ * it only discovers and downloads public .ehpk packages. Authentication is * the same signed-header scheme used by the firmware API. */ -import { Application } from "@nativescript/core"; +import { hmacSha256Base64, getPhoneOpenUdid } from "./even-platform"; +export { getPhoneOpenUdid } from "./even-platform"; import { getEvenHubToken, hasEvenHubCredentials, invalidateEvenHubToken, saveEvenHubSession, } from "./credentials"; +import { downloadBinary } from "../../native/binary-http"; import { fetchWithUserAgent } from "../../util/http"; -declare const android: any; const API_HOST = "https://api.evenrealities.com"; const PUBLIC_CDN_HOST = "https://cdn-pub.evenhub.evenrealities.com"; @@ -128,9 +129,9 @@ export class EvenHubApiClient { const url = typeof metadata.url === "string" ? metadata.url : ""; if (!url.startsWith("https://")) throw new Error("EvenHub returned no package download URL."); - const response = await fetchWithTimeout(url, {}, 60_000); - if (!response.ok) throw new Error(`EvenHub package download failed (HTTP ${response.status}).`); - const bytes = new Uint8Array(await response.arrayBuffer()); + const response = await downloadBinary(url, 60_000); + if (response.status < 200 || response.status >= 300) throw new Error(`EvenHub package download failed (HTTP ${response.status}).`); + const bytes = response.bytes; const expectedSize = finiteNumber(metadata.size); if (expectedSize > 0 && bytes.length !== expectedSize) { throw new Error(`EvenHub package was truncated (${bytes.length} of ${expectedSize} bytes).`); @@ -156,11 +157,11 @@ export class EvenHubApiClient { if (!/^prod\/[A-Za-z0-9._~!$&'()*+,;=:@%/-]+\.(?:svg|png|webp|jpe?g)$/i.test(cleanPath) || cleanPath.includes("..")) { throw new Error("EvenHub returned an invalid public asset path."); } - const response = await fetchWithTimeout(`${PUBLIC_CDN_HOST}/${cleanPath}`, {}, 30_000); - if (!response.ok) throw new Error(`EvenHub icon download failed (HTTP ${response.status}).`); - const contentLength = Number(response.headers.get("Content-Length") ?? 0); + const response = await downloadBinary(`${PUBLIC_CDN_HOST}/${cleanPath}`, 30_000); + if (response.status < 200 || response.status >= 300) throw new Error(`EvenHub icon download failed (HTTP ${response.status}).`); + const contentLength = response.contentLength; if (contentLength > 1_000_000) throw new Error("EvenHub icon is unexpectedly large."); - const bytes = new Uint8Array(await response.arrayBuffer()); + const bytes = response.bytes; if (bytes.length === 0 || bytes.length > 1_000_000) throw new Error("EvenHub icon is empty or too large."); const extension = cleanPath.toLowerCase().match(/\.([a-z0-9]+)$/)?.[1] ?? ""; return { bytes, extension: extension === "jpeg" ? "jpg" : extension }; @@ -180,12 +181,18 @@ export class EvenHubApiClient { path: string, options: { query?: Record; body?: Record } = {}, ): Promise { - let token = await this.ensureLogin(); + const token = await this.ensureLogin(); const response = await this.request(method, path, { ...options, token, commonVersion: 3 }); - if (response.httpStatus === 401) { - invalidateEvenHubToken(); + try { + return unwrap(response.envelope, response.httpStatus, path); + } catch (error) { + // A rejected session fails every later request too, so drop the token + // (unless a fresh sign-in replaced it while this request was in flight). + if (error instanceof EvenHubAuthenticationError && getEvenHubToken() === token) { + invalidateEvenHubToken(); + } + throw error; } - return unwrap(response.envelope, response.httpStatus, path); } private ensureLogin(): Promise { @@ -287,9 +294,9 @@ function buildCommon(version: 1 | 3): string { buildTime: "26060821", appId: "1001", v: version, - // This is Android's app-scoped SSAID. Even's login endpoint registers a - // previously unseen value as a terminal, so Faceclaw does not need the - // official Even app's differently-scoped ID. + // Keep the established wire profile on both platforms. Even registers + // this installation-scoped terminal ID at login (SSAID on Android, + // a persisted random ID on iOS). openUdid: getPhoneOpenUdid(), os: "1", sn: "", @@ -333,13 +340,7 @@ function formEncode(value: string): string { } function hmacSign(message: string): string { - if (!global.isAndroid) throw new Error("The EvenHub storefront is currently available only on Android."); - const charset = java.nio.charset.StandardCharsets.UTF_8; - const key = new javax.crypto.spec.SecretKeySpec(new java.lang.String(SIGN_KEY).getBytes(charset), "HmacSHA256"); - const mac = javax.crypto.Mac.getInstance("HmacSHA256"); - mac.init(key); - const digest = mac.doFinal(new java.lang.String(message).getBytes(charset)); - return String(android.util.Base64.encodeToString(digest, android.util.Base64.NO_WRAP)); + return hmacSha256Base64(SIGN_KEY, message); } async function fetchWithTimeout(url: string, init: RequestInit, timeoutMs = REQUEST_TIMEOUT_MS): Promise { @@ -355,30 +356,21 @@ async function fetchWithTimeout(url: string, init: RequestInit, timeoutMs = REQU } function unwrap(envelope: ApiEnvelope, httpStatus: number, path: string): unknown { - if (httpStatus === 401) { - throw new EvenHubAuthenticationError("Even rejected the login session."); + const message = typeof envelope.msg === "string" ? envelope.msg.trim() : ""; + // An expired session can come back as HTTP 401, or as HTTP 200 with code + // 401 in the envelope ("Your login is expired"). + if (httpStatus === 401 || Number(envelope.code) === 401) { + throw new EvenHubAuthenticationError(message || "Even rejected the login session."); } if (httpStatus < 200 || httpStatus >= 300) { throw new Error(`EvenHub request failed (HTTP ${httpStatus}, ${path}).`); } if (envelope.code !== 0) { - const detail = typeof envelope.msg === "string" && envelope.msg ? `: ${envelope.msg}` : ""; - throw new Error(`EvenHub API error ${envelope.code ?? "unknown"}${detail}`); + throw new Error(`EvenHub API error ${envelope.code ?? "unknown"}${message ? `: ${message}` : ""}`); } return envelope.data; } -/** Return this installation's Android ID, which Even calls `openUdid`. */ -export function getPhoneOpenUdid(): string { - if (!global.isAndroid) throw new Error("The EvenHub storefront is currently available only on Android."); - const resolver = Application.android.context?.getContentResolver(); - const openUdid = resolver - ? String(android.provider.Settings.Secure.getString(resolver, android.provider.Settings.Secure.ANDROID_ID) ?? "") - : ""; - if (!openUdid) throw new Error("Android did not provide a device ID for EvenHub authentication."); - return openUdid; -} - /** * Parse a list response. The leaderboard returns `{list, total, page, * page_size}`; the ranking/search endpoints were only recovered from the diff --git a/app/apps/evenhub/even-platform.ios.ts b/app/apps/evenhub/even-platform.ios.ts new file mode 100644 index 00000000..3753f1eb --- /dev/null +++ b/app/apps/evenhub/even-platform.ios.ts @@ -0,0 +1,17 @@ +import { getStringSetting, setStringSetting } from "../../native/settings-store"; +declare const FaceclawCrypto: any; + +export function hmacSha256Base64(secret: string, message: string): string { + return FaceclawCrypto.hmacSha256Message(secret, message); +} + +/** A stable random terminal ID, independent of phone hardware identifiers. */ +export function getPhoneOpenUdid(): string { + const key = "integrations.evenhub.terminalId"; + let id = getStringSetting(key, ""); + if (!id) { + id = NSUUID.UUID().UUIDString.replace(/-/g, "").slice(0, 16).toLowerCase(); + setStringSetting(key, id); + } + return id; +} diff --git a/app/apps/evenhub/even-platform.ts b/app/apps/evenhub/even-platform.ts new file mode 100644 index 00000000..bee53a2c --- /dev/null +++ b/app/apps/evenhub/even-platform.ts @@ -0,0 +1,17 @@ +import { Application } from "@nativescript/core"; + +export function hmacSha256Base64(secret: string, message: string): string { + const charset = java.nio.charset.StandardCharsets.UTF_8; + const key = new javax.crypto.spec.SecretKeySpec(new java.lang.String(secret).getBytes(charset), "HmacSHA256"); + const mac = javax.crypto.Mac.getInstance("HmacSHA256"); + mac.init(key); + const digest = mac.doFinal(new java.lang.String(message).getBytes(charset)); + return String(android.util.Base64.encodeToString(digest, android.util.Base64.NO_WRAP)); +} + +export function getPhoneOpenUdid(): string { + const resolver = Application.android.context?.getContentResolver(); + const id = resolver ? String(android.provider.Settings.Secure.getString(resolver, android.provider.Settings.Secure.ANDROID_ID) ?? "") : ""; + if (!id) throw new Error("Android did not provide a device ID for EvenHub authentication."); + return id; +} diff --git a/app/apps/evenhub/installed-apps.ts b/app/apps/evenhub/installed-apps.ts index f9743cde..ea05c4f1 100644 --- a/app/apps/evenhub/installed-apps.ts +++ b/app/apps/evenhub/installed-apps.ts @@ -13,10 +13,10 @@ import { writeBinaryFile, } from "../../native/file-access"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; -import { loadImageFileAsGray } from "../../native/image-files"; +import { loadIconImage } from "../../native/icon-image"; import { renderSvgIcon } from "../../graphics/icons"; import { type GrayImage } from "../../graphics/image"; -import { parseEhpk, parseManifest, utf8Decode, type EhpkArchive, type EvenHubManifest } from "./ehpk"; +import { openEhpk, parseManifest, utf8Decode, type EhpkIndex, type EvenHubManifest } from "./ehpk"; const STORAGE_KEY = "evenhub.installedApps.v1"; const APP_ID_PREFIX = "evenhub-installed:"; @@ -209,7 +209,7 @@ export function renderInstalledEvenHubIcon( const svg = readTextFile(path); if (svg) image = renderSvgIcon(cacheKey, svg, size); } else { - image = loadImageFileAsGray(path, size, size); + image = loadIconImage(path, size, size); } renderedIconCache.set(cacheKey, image); return image; @@ -220,8 +220,8 @@ export function getInstalledEvenHubFingerprint(): string { return getStringSetting(STORAGE_KEY, ""); } -function inspectEhpk(bytes: Uint8Array): { archive: EhpkArchive; manifest: EvenHubManifest } { - const archive = parseEhpk(bytes); +function inspectEhpk(bytes: Uint8Array): { archive: EhpkIndex; manifest: EvenHubManifest } { + const archive = openEhpk(bytes); const appJson = archive.files.get("app.json"); if (!appJson) throw new Error("The EHPK package has no app.json manifest."); return { archive, manifest: parseManifest(utf8Decode(appJson)) }; @@ -242,7 +242,7 @@ function saveInstalledApps(apps: InstalledEvenHubApp[]): void { ); } -function findEmbeddedIcon(archive: EhpkArchive, manifest: EvenHubManifest): EvenHubInstallIcon | null { +function findEmbeddedIcon(archive: EhpkIndex, manifest: EvenHubManifest): EvenHubInstallIcon | null { const candidates: string[] = []; for (const key of ["icon", "icon_path", "iconPath", "app_icon", "appIcon"]) { const value = manifest.raw[key]; @@ -270,11 +270,11 @@ function findEmbeddedIcon(archive: EhpkArchive, manifest: EvenHubManifest): Even if (content && extension) return { bytes: content, extension }; } } - const fallback = Array.from(archive.files.entries()).find(([path]) => + const fallback = Array.from(archive.files.keys()).find((path) => /(?:^|\/)(?:app-?)?(?:icon|favicon)\.(?:svg|png|webp|jpe?g)$/i.test(path), ); - const extension = fallback ? iconExtension(fallback[0]) : null; - return fallback && extension ? { bytes: fallback[1], extension } : null; + const extension = fallback ? iconExtension(fallback) : null; + return fallback && extension ? { bytes: archive.files.get(fallback)!, extension } : null; } function normalizeInstallIcon(icon: EvenHubInstallIcon | undefined): EvenHubInstallIcon | null { diff --git a/app/apps/evenhub/manager.ts b/app/apps/evenhub/manager.ts index 12b45346..7b3b327c 100644 --- a/app/apps/evenhub/manager.ts +++ b/app/apps/evenhub/manager.ts @@ -15,16 +15,16 @@ import { type AppContext } from "../app-definition"; import { appFilesDirPath, deletePathRecursively, - readBinaryFile, - writeBinaryFile, } from "../../native/file-access"; -import { parseEhpk, parseManifest, utf8Decode, type EvenHubManifest } from "./ehpk"; +import { type EvenHubManifest } from "./ehpk"; +import { unpackRuntime } from "./unpack-runtime"; import { type EvenHubPermission } from "./permissions"; import { EvenHubSession } from "./session"; import { createEvenHubWindow } from "./evenhub-window"; import { createEvenHubWebView, type EvenHubWebView } from "./webview"; import { shell } from "../../ui/shell/shell"; import { + readEvenHubPackageManifest, installedEvenHubAppId, installedEvenHubPackagePath, type InstalledEvenHubApp, @@ -64,18 +64,6 @@ export async function launchPackage( ehpkPath: string, options: { appId?: string; singleton?: boolean } = {}, ): Promise { - const data = readBinaryFile(ehpkPath); - if (!data) { - ctx.appendLog(`evenhub: could not read ${ehpkPath}`); - return; - } - const archive = parseEhpk(data); - const appJson = archive.files.get("app.json"); - if (!appJson) { - ctx.appendLog("evenhub: package has no app.json"); - return; - } - const manifest = parseManifest(utf8Decode(appJson)); const appId = options.appId ?? "evenhub"; if (options.singleton) { const existing = Array.from(running.values()).find((app) => app.appId === appId); @@ -86,18 +74,19 @@ export async function launchPackage( } } - // Unpack fresh into app-private storage, keyed by package id. + const manifest = readEvenHubPackageManifest(ehpkPath); + if (!manifest) throw new Error('Could not read the EHPK manifest.'); + // Unpack off the iOS UI thread; reserve a unique directory before awaiting. const safeId = manifest.packageId.replace(/[^A-Za-z0-9._-]/g, "_"); - const baseDir = `${appFilesDirPath()}/evenhub-apps/${safeId}`; - deletePathRecursively(baseDir); - for (const [name, content] of Array.from(archive.files)) { - if (!writeBinaryFile(`${baseDir}/${name}`, content)) { - ctx.appendLog(`evenhub: failed to write ${name}`); - return; - } - } - - await startApp(ctx, manifest, `${baseDir}/dist`, "", appId); + if (!safeId || safeId === '.' || safeId === '..') throw new Error('Invalid EHPK package ID'); + const baseDir = `${appFilesDirPath()}/evenhub-apps/${safeId}${global.isIOS ? `-${Date.now()}-${nextSerial++}` : ''}`; + const started = Date.now(); + ctx.appendLog(`evenhub: unpacking ${manifest.name}`); + try { + const loadedManifest = await unpackRuntime(ehpkPath, baseDir); + ctx.appendLog(`evenhub: unpacked ${loadedManifest.name} in ${Date.now() - started} ms`); + await startApp(ctx, loadedManifest, `${baseDir}/dist`, "", appId, global.isIOS ? () => deletePathRecursively(baseDir) : undefined); + } catch (error) { deletePathRecursively(baseDir); throw error; } } /** @@ -111,9 +100,11 @@ async function startApp( distDir: string, remoteUrl: string, appId: string, + cleanup?: () => void, ): Promise { const windowId = `evenhub:app:${nextSerial++}`; - const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl); + const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl, + payload => ctx.actions.playBuzzerSequence(payload)); const webView = createEvenHubWebView(session); session.attachWebView({ evaluateJs: webView.evaluateJs, @@ -123,6 +114,7 @@ async function startApp( phoneShownWindowId = null; } webView.destroy(); + cleanup?.(); running.delete(windowId); }, }); @@ -130,9 +122,11 @@ async function startApp( ensureBackHandler(); ctx.appendLog(`evenhub: launching ${manifest.name} ${manifest.version} (${manifest.packageId})`); - await ctx.launchInProcessApp(windowId, `window:${windowId}`, (options) => - createEvenHubWindow(windowId, appId, session, options, () => showOnPhone(windowId)), - ); + try { + await ctx.launchInProcessApp(windowId, `window:${windowId}`, (options) => + createEvenHubWindow(windowId, appId, session, options, () => showOnPhone(windowId)), + ); + } catch (error) { session.close(); throw error; } } /** @@ -210,6 +204,7 @@ export function closeRunningPackage(packageId: string): void { export function showOnPhone(windowId: string): void { const app = running.get(windowId); if (!app) return; + if (phoneShownWindowId && phoneShownWindowId !== windowId) hidePhone(); app.webView.showOnPhone(); phoneShownWindowId = windowId; } diff --git a/app/apps/evenhub/package-extraction.ts b/app/apps/evenhub/package-extraction.ts new file mode 100644 index 00000000..bbe9720c --- /dev/null +++ b/app/apps/evenhub/package-extraction.ts @@ -0,0 +1,20 @@ +import { deletePathRecursively, readBinaryFile, writeBinaryFile } from '../../native/file-access'; +import { openEhpk, parseManifest, utf8Decode, type EvenHubManifest } from './ehpk'; + +/** Called on a worker on iOS. Hold only one expanded asset at a time. */ +export function extractPackage(path: string, directory: string): EvenHubManifest { + try { + const bytes = readBinaryFile(path); + if (!bytes) throw new Error('Could not read the EHPK file.'); + const archive = openEhpk(bytes); + const json = archive.files.get('app.json'); + if (!json) throw new Error('The EHPK package has no app.json.'); + const manifest = parseManifest(utf8Decode(json)); + if (!archive.files.has(`dist/${manifest.entrypoint}`)) throw new Error('The EHPK entrypoint is missing from dist/.'); + deletePathRecursively(directory); + for (const name of archive.files.keys()) { + if (!writeBinaryFile(`${directory}/${name}`, archive.files.get(name)!)) throw new Error(`Could not unpack ${name}.`); + } + return manifest; + } catch (error) { deletePathRecursively(directory); throw error; } +} diff --git a/app/apps/evenhub/package-extraction.worker.ts b/app/apps/evenhub/package-extraction.worker.ts new file mode 100644 index 00000000..3d6d0adc --- /dev/null +++ b/app/apps/evenhub/package-extraction.worker.ts @@ -0,0 +1,9 @@ +import '@nativescript/core/globals'; +import { extractPackage } from './package-extraction'; + +global.onmessage = (event: { data: { path: string; directory: string } }) => { + try { + const manifest = extractPackage(event.data.path, event.data.directory); + global.postMessage({ manifest }); + } catch (error) { global.postMessage({ error: String((error as Error)?.message ?? error) }); } +}; diff --git a/app/apps/evenhub/privacy-policy.ios.ts b/app/apps/evenhub/privacy-policy.ios.ts new file mode 100644 index 00000000..98d20710 --- /dev/null +++ b/app/apps/evenhub/privacy-policy.ios.ts @@ -0,0 +1,5 @@ +import { Utils } from '@nativescript/core' +/** Open outside the app runtime; this page has no EvenHub bridge. */ +export function openPrivacyPolicyOnPhone(url: string, _appName: string): void { + if (/^https:\/\//i.test(url)) Utils.openUrl(url) +} diff --git a/app/apps/evenhub/privacy-policy.ts b/app/apps/evenhub/privacy-policy.ts index 53fc2681..e5e0c0be 100644 --- a/app/apps/evenhub/privacy-policy.ts +++ b/app/apps/evenhub/privacy-policy.ts @@ -1,5 +1,6 @@ import { Application } from "@nativescript/core"; import { fetchWithUserAgent } from "../../util/http"; +import { copyToJavaByteBuffer } from "../../native/java-direct-buffer"; declare const android: any; declare const androidx: any; @@ -93,8 +94,7 @@ async function downloadAndOpenPdf(url: string, appName: string, activity: any): const file = new java.io.File(directory, `policy-${hash.toString(16)}.pdf`); const stream = new java.io.FileOutputStream(file); try { - const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); - stream.getChannel().write(buffer); + stream.getChannel().write(copyToJavaByteBuffer(bytes)); } finally { stream.close(); } diff --git a/app/apps/evenhub/session.ts b/app/apps/evenhub/session.ts index c9341c8c..d0e4421b 100644 --- a/app/apps/evenhub/session.ts +++ b/app/apps/evenhub/session.ts @@ -61,12 +61,10 @@ import { type ImuReading } from "../../native/imu"; import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/tool-registry"; import { getCurrentLocation } from "../../native/location"; import { LocationTracker, type TrackedLocation } from "../../native/location-tracker"; -import { ensureFineLocationPermission } from "../../g2/android-permissions"; +import { ensureFineLocationPermission } from "../../native/location-permissions"; const UPNG = require("upng-js"); -declare const com: any; - /** OsEventTypeList values (PB ordinals). */ const CLICK_EVENT = 0; const SCROLL_TOP_EVENT = 1; @@ -334,6 +332,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH private windowHooks: EvenHubWindowHooks | null = null; private closed = false; private launchContextPushed = false; + private loadError = ''; private systemExitSent = false; /** The one-shot launch FOREGROUND_ENTER has been delivered (page exists). */ private launchEnterSent = false; @@ -370,6 +369,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH distDir: string, log: (message: string) => void, remoteUrl = "", + private readonly sendBuzzerSequence: (payload: Uint8Array) => Promise | void = () => {}, ) { this.manifest = manifest; this.distDir = distDir; @@ -383,6 +383,11 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH this.webViewHandle = handle; } + webViewFailed(message: string): void { + if (this.closed) return; + this.loadError = message; this.log(`evenhub: ${message}`); this.windowHooks?.requestRender(); + } + /** The WebView finished loading the app: push the one-shot launch context. */ webViewLoaded(): void { if (this.launchContextPushed) return; @@ -416,11 +421,12 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH // Focus changes always trigger a repaint of the foreground window, so paint // is a reliable place to sample shell input-focus for lifecycle events. this.updateShellFocus(focused); - if (!this.pageCreated || !this.page) { + if (this.loadError || !this.pageCreated || !this.page) { const image = new GrayImage(size.width, size.height, 0); const font = EvenHubFont.get(); - font.drawText(image, 16, 16, `Loading ${this.manifest.name}...`, 255); - font.drawText(image, 16, 16 + 2 * font.lineHeight, "(waiting for the app to build its page)", 120); + font.drawText(image, 16, 16, `${this.loadError ? 'Could not run' : 'Loading'} ${this.manifest.name}...`, 255); + font.drawTextWrapped(image, 16, 16 + 2 * font.lineHeight, Math.max(1, size.width - 32), + this.loadError || "(waiting for the app to build its page)", 120); return image; } return compositePage(this.page, size, focused); @@ -604,6 +610,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH * flows (foreground + screen on + no assistant modal). */ private audioControl(data: Record): boolean { + if (global.isIOS) return false; // EvenHub PCM routing is not ported yet. if (!permissionsIncludeMicrophone(this.manifest.permissions)) { this.log("evenhub: audioControl denied (app declares no microphone permission)"); return false; @@ -697,6 +704,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH * to the foreground app. reportFrq is one of 100..1000 (step 100). */ private imuControl(data: Record): boolean { + if (global.isIOS) return false; const enable = readEnableFlag(data); const freq = clampImuFreq(readOptionalNumber(data, "reportFrq") ?? readOptionalNumber(data, "reportFreq") ?? 100); // Payload field spelling isn't fully captured; log once per toggle. @@ -1155,13 +1163,11 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH private async playBuzzer(rawSteps: unknown[]): Promise { const steps = normalizeBuzzerSteps(rawSteps); if (steps.length === 0) return; - const communicator = activeCommunicator(); - if (!communicator) return; for (let i = 0; i < steps.length; i += CFW_SEQ_MAX) { const chunk = steps.slice(i, i + CFW_SEQ_MAX); const payload = buildSoundSequencePayload(chunk); try { - communicator.playBuzzerSequence(payload.buffer); + await this.sendBuzzerSequence(payload); } catch (error) { this.log(`evenhub: playBuzzer failed: ${error}`); return; @@ -1265,15 +1271,6 @@ function clampImuFreq(freq: number): number { return Math.min(1000, Math.max(100, snapped)); } -function activeCommunicator(): any { - if (!global.isAndroid) return null; - try { - return com.faceclaw.app.FaceclawBleCommunicator.getActive(); - } catch { - return null; - } -} - /** * Copy content into a `preserve` container from the same-named, same-kind * container in the previous page (if any), so a replaceLayout keeps its pixels / diff --git a/app/apps/evenhub/store-detail-layer.ts b/app/apps/evenhub/store-detail-layer.ts index ab179023..ba179d86 100644 --- a/app/apps/evenhub/store-detail-layer.ts +++ b/app/apps/evenhub/store-detail-layer.ts @@ -3,7 +3,7 @@ import { GrayImage } from "../../graphics/image"; import { truncateText, wrapText } from "../../graphics/textwrap"; import { type InputEvent } from "../../ui/gestures"; import { type Layer, type LayerContext } from "../../ui/layers"; -import { drawSelectionHighlight } from "../../ui/menu"; +import { Menu } from "../../ui/menu-core"; import { TextViewerLayer } from "../files/text-viewer"; import { evenHubApi, type EvenHubStoreApp } from "./even-api"; import { @@ -18,17 +18,36 @@ import { lineStep, listRowHeight } from "../../ui/metrics"; const X = 18; +type DetailAction = "about" | "whats-new" | "primary"; +const ACTIONS: readonly DetailAction[] = ["about", "whats-new", "primary"]; + export type EvenHubStoreDetailOptions = { launchApp: (appId: string) => Promise | void; appendLog: (message: string) => void; /** A package was installed, updated, or reinstalled from this page. */ onInstalled?: (app: InstalledEvenHubApp) => void; + /** Offered every failure first; returns true if it was a rejected session (and was dealt with). */ + onAuthError?: (ctx: LayerContext, error: unknown) => boolean; }; /** Storefront metadata and the Install/Launch action for one public app. */ export class EvenHubStoreDetailLayer implements Layer { - /** About, What's New, Install/Launch. */ - private selectedIndex = 2; + /** About, What's New, Install/Launch; starts on the primary action. */ + private readonly actions = new Menu({ + items: ACTIONS, + selectedIndex: 2, + rowGap: 1, + highlight: { radius: 8 }, + getHeight: () => listRowHeight(getDefaultSmallFont()) + 4, + draw: ({ image, item, x, y, width, selected }) => { + const font = getDefaultSmallFont(); + const label = item === "about" ? "About" : item === "whats-new" ? "What's New" : this.primaryLabel; + const value = this.working && item === "primary" ? 145 : selected ? 245 : 190; + image.drawText(font, x + 11, y + 5, truncateText(font, label, width - 18), value); + }, + }); + /** The primary action's label for the paint in progress. */ + private primaryLabel = ""; private working = false; private detailPromise: Promise | null = null; private status = ""; @@ -47,7 +66,7 @@ export class EvenHubStoreDetailLayer implements Layer { const actionRowH = listRowHeight(font) + 4; const installed = getInstalledEvenHubApp(this.app.packageId); - const actions = ["About", "What's New", this.working ? "Installing..." : this.primaryActionLabel(installed)]; + this.primaryLabel = this.working ? "Installing..." : this.primaryActionLabel(installed); // Detail lines as (text, shade, extra gap below); built per candidate // width so the layout choice below can measure before drawing. @@ -90,7 +109,7 @@ export class EvenHubStoreDetailLayer implements Layer { // Stacked layout (details full-width, actions at the bottom) when it // fits; otherwise the actions become a narrow top-right menu and the // details flow down a left column beside it. - const stackedActionTop = height - 6 - actions.length * actionRowH; + const stackedActionTop = height - 6 - ACTIONS.length * actionRowH; const stacked = 8 + linesHeight(buildLines(width - X * 2)) + 6 <= stackedActionTop; const menuW = 150; const menuX = width - menuW - 12; @@ -105,15 +124,11 @@ export class EvenHubStoreDetailLayer implements Layer { const actionX = stacked ? X : menuX; const actionW = stacked ? textWidth : menuW; const actionTop = stacked ? stackedActionTop : 8; - for (let index = 0; index < actions.length; index++) { - const actionY = actionTop + index * actionRowH; - const selected = index === this.selectedIndex; - if (selected) { - drawSelectionHighlight(image, actionX - 5, actionY, actionW + 10, actionRowH - 1, ctx.stack.isFocused(), 8); - } - const value = this.working && index === 2 ? 145 : selected ? 245 : 190; - image.drawText(font, actionX + 6, actionY + 5, truncateText(font, actions[index]!, actionW - 8), value); - } + this.actions.paint( + image, + { x: actionX - 5, y: actionTop, width: actionW + 10, height: ACTIONS.length * actionRowH - 1 }, + ctx.stack.isFocused(), + ); return image; } @@ -123,34 +138,44 @@ export class EvenHubStoreDetailLayer implements Layer { return; } if (this.working) return; - if (event.type === "scroll-up") { - this.selectedIndex = Math.max(0, this.selectedIndex - 1); - return; - } - if (event.type === "scroll-down") { - this.selectedIndex = Math.min(2, this.selectedIndex + 1); + if (event.type === "scroll-up" || event.type === "scroll-down") { + await this.actions.handleInput(event); return; } if (event.type !== "click") return; - await this.ensureDetail(ctx); + // Never hold the serialized input queue across network work or a dialog + // that needs a subsequent input to resolve. + void this.activate(ctx); + } - if (this.selectedIndex === 0) { - ctx.stack.push(new TextViewerLayer(this.app.description || "No description supplied.", "About")); - return; - } - if (this.selectedIndex === 1) { - ctx.stack.push(new TextViewerLayer(this.app.changelog || "No release notes supplied.", "What's New")); - return; - } + private async activate(ctx: LayerContext): Promise { + this.working = true; + try { + await this.ensureDetail(ctx); - const installed = getInstalledEvenHubApp(this.app.packageId); - if (installed && this.primaryActionLabel(installed) === "Launch") { - await this.options.launchApp(installedEvenHubAppId(installed.packageId)); - return; - } + const action = this.actions.selectedItem; + if (action === "about") { + ctx.stack.push(new TextViewerLayer(this.app.description || "No description supplied.", "About")); + return; + } + if (action === "whats-new") { + ctx.stack.push(new TextViewerLayer(this.app.changelog || "No release notes supplied.", "What's New")); + return; + } - await this.install(ctx); + const installed = getInstalledEvenHubApp(this.app.packageId); + if (installed && this.primaryActionLabel(installed) === "Launch") { + await this.options.launchApp(installedEvenHubAppId(installed.packageId)); + return; + } + + await this.install(ctx); + } catch (error) { + if (this.options.onAuthError?.(ctx, error)) return; + this.status = cleanError(error); + this.options.appendLog(`evenhub store: ${this.status}`); + } finally { this.working = false; ctx.actions.requestRender(); } } /** Install for a new app; Update / Reinstall when the installed copy is stale or gone. */ @@ -181,6 +206,7 @@ export class EvenHubStoreDetailLayer implements Layer { ctx.actions.requestRender(); await this.options.launchApp(installedEvenHubAppId(result.packageId)); } catch (error) { + if (this.options.onAuthError?.(ctx, error)) return; this.status = cleanError(error); this.options.appendLog(`evenhub store: ${this.status}`); } finally { @@ -204,6 +230,7 @@ export class EvenHubStoreDetailLayer implements Layer { return undefined; }) .catch((error) => { + if (this.options.onAuthError?.(ctx, error)) return; this.options.appendLog(`evenhub store: app details unavailable: ${cleanError(error)}`); }) .finally(() => ctx.actions.requestRender()); diff --git a/app/apps/evenhub/store-install.ts b/app/apps/evenhub/store-install.ts index 5c63e1a9..faa60f8c 100644 --- a/app/apps/evenhub/store-install.ts +++ b/app/apps/evenhub/store-install.ts @@ -53,8 +53,6 @@ export async function installStoreApp( const needsPrompt = (manifest.permissions.length > 0 || privacyPolicyUrl) && !alreadyGranted(app.packageId, manifest); if (needsPrompt) { - options.onStatus(""); - ctx.actions.requestRender(); const accepted = await new Promise((resolve) => { ctx.stack.push( new EvenHubPermissionDialogLayer( @@ -65,6 +63,10 @@ export async function installStoreApp( () => resolve(false), ), ); + // Only once the dialog is stacked: an in-process window's requestRender + // paints synchronously, and nothing else repaints after a download. + options.onStatus(""); + ctx.actions.requestRender(); }); if (!accepted) return null; } diff --git a/app/apps/evenhub/store-layer.ts b/app/apps/evenhub/store-layer.ts index 82df0fee..966fc279 100644 --- a/app/apps/evenhub/store-layer.ts +++ b/app/apps/evenhub/store-layer.ts @@ -1,16 +1,16 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage, type UiFont } from "../../graphics/image"; import { truncateText, wrapText } from "../../graphics/textwrap"; -import { clamp } from "../../util/numeric-util"; import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures"; import { type Layer, type LayerActions, type LayerContext, type LayerStack } from "../../ui/layers"; -import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu"; +import { drawSelectionHighlight, type MenuItem } from "../../ui/menu"; +import { Menu } from "../../ui/menu-core"; import { shell } from "../../ui/shell/shell"; import { ConfigSettingString } from "../../ui/dashboard-settings"; import { evenHubApi, EvenHubAuthenticationError, isEvenHubStoreConfigured, type EvenHubStoreApp, type EvenHubStorePage } from "./even-api"; import { clearEvenHubLoginForm, clearEvenHubSession, clearTransientEvenHubSession, evenHubLoginEmailSetting, evenHubLoginPasswordSetting, evenHubRememberMeSetting, resetEvenHubLoginForm } from "./credentials"; import { getInstalledEvenHubApps, uninstallEvenHubPackage, type InstalledEvenHubApp } from "./installed-apps"; -import { EvenHubStoreDetailLayer } from "./store-detail-layer"; +import { EvenHubStoreDetailLayer, type EvenHubStoreDetailOptions } from "./store-detail-layer"; import { cleanError, installStoreApp } from "./store-install"; import { checkForEvenHubUpdates, describeUpdate, type EvenHubAppUpdate } from "./updates"; import { lineStep } from "../../ui/metrics"; @@ -24,6 +24,11 @@ function headerHeight(font: UiFont): number { return 8 + lineStep(font) + font.lineHeight + 6; } +/** Row pitch of the two-line app list (title + detail), including the 2px gap between highlights. */ +function storeRowHeight(font: UiFont): number { + return Math.max(30, 2 * lineStep(font) + 4); +} + /** Last search typed on the phone; kept so reopening the store shows it again. */ export const evenHubSearchQuerySetting = new ConfigSettingString({ id: "evenhub-search-query", @@ -64,27 +69,46 @@ type TabState = { loading: boolean; /** Progress or error text for the subtitle line. */ status: string; - selectedIndex: number; - scrollRow: number; + /** Selection and scroll over `rows` (resynced before each paint and input). */ + menu: Menu; /** Search only: the query the rows belong to. */ query: string; }; function emptyTab(): TabState { + const rows: StoreRow[] = []; return { - rows: [], + rows, total: 0, nextPage: 1, exhausted: false, loaded: false, loading: false, status: "", - selectedIndex: 0, - scrollRow: 0, + menu: new Menu({ + items: rows, + rowGap: 2, + highlight: { radius: 5 }, + getHeight: () => storeRowHeight(getDefaultSmallFont()), + draw: ({ image, item, x, y, width }) => { + const font = getDefaultSmallFont(); + const { title, detail, titleValue } = rowText(item); + const textX = x + 6; + const textW = width - 12; + image.drawText(font, textX, y + 2, truncateText(font, title, textW), titleValue); + image.drawText(font, textX, y + 2 + lineStep(font), truncateText(font, detail, textW), 115); + }, + }), query: "", }; } +/** The tab's menu, with its items brought up to date with `rows` (which loads replace or extend). */ +function tabMenu(tab: TabState): Menu { + if (tab.menu.items !== tab.rows) tab.menu.setItems(tab.rows); + return tab.menu; +} + export type EvenHubStoreLayerOptions = { launchApp: (appId: string) => Promise | void; appendLog: (message: string) => void; @@ -124,9 +148,14 @@ export class EvenHubStoreLayer implements Layer { paint(ctx: LayerContext): GrayImage { if (!this.started) { this.started = true; - if (this.showingLogin) { - this.openCredentialEditor(ctx); - } else { + // Do NOT open the phone credential editor here. This runs on the first + // paint, which includes the restore of a still-open EvenHub at app + // startup -- so an unfinished sign-in re-opened the form on every + // launch, unasked. The login pane already says "Enter your Even account + // email and password in the phone app" and already opens the editor on + // click (handleInput), so the editor is still one tap away for someone + // who actually wants it. + if (!this.showingLogin) { void this.ensureTabLoaded(ctx, this.activeTab); } } @@ -163,25 +192,16 @@ export class EvenHubStoreLayer implements Layer { return image; } - tab.selectedIndex = clamp(tab.selectedIndex, 0, tab.rows.length - 1); - const rowH = Math.max(30, 2 * lineStep(font) + 4); + const rowH = storeRowHeight(font); const visibleRows = Math.max(1, Math.floor((height - headerH - 4) / rowH)); - tab.scrollRow = scrollToKeepSelectionVisible(tab.scrollRow, tab.selectedIndex, visibleRows, tab.rows.length); - const last = Math.min(tab.rows.length, tab.scrollRow + visibleRows); const listFocused = ctx.stack.isFocused() && this.focus === "list"; - for (let index = tab.scrollRow; index < last; index++) { - const row = tab.rows[index]!; - const y = headerH + (index - tab.scrollRow) * rowH; - if (index === tab.selectedIndex) { - drawSelectionHighlight(image, LIST_X - 6, y, width - LIST_X * 2 + 12, rowH - 2, listFocused, 5); - } - const { title, detail, titleValue } = this.rowText(row); - image.drawText(font, LIST_X, y + 2, truncateText(font, title, width - LIST_X * 2), titleValue); - image.drawText(font, LIST_X, y + 2 + lineStep(font), truncateText(font, detail, width - LIST_X * 2), 115); - } - if (tab.rows.length > visibleRows) { - drawListScrollbar(image, width - 5, headerH, visibleRows * rowH - 3, tab.scrollRow, visibleRows, tab.rows.length); - } + const menu = tabMenu(tab); + menu.paint( + image, + { x: LIST_X - 6, y: headerH, width: width - LIST_X * 2 + 12, height: visibleRows * rowH - 2 }, + listFocused, + ); + menu.drawScrollbar(image, width - 5, headerH, visibleRows * rowH - 3); return image; } @@ -243,41 +263,6 @@ export class EvenHubStoreLayer implements Layer { } } - private rowText(row: StoreRow): { title: string; detail: string; titleValue: number } { - switch (row.kind) { - case "search-query": { - const query = evenHubSearchQuerySetting.get(); - return { - title: query ? `Search: ${query}` : "Search: (enter a query)", - detail: "Click to type on the phone, or use voice input.", - titleValue: 200, - }; - } - case "orphan": - return { - title: row.update.installed.name, - detail: row.update.error - ? `Package missing · store lookup failed: ${row.update.error}` - : "Package missing · not in the store · click to forget", - titleValue: 170, - }; - case "app": { - const { app, update } = row; - if (!update) { - return { - title: app.name, - detail: app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`, - titleValue: 215, - }; - } - const detail = update.packageMissing - ? `Package missing · reinstall ${app.version ? `version ${app.version}` : ""}`.trim() - : `Version ${update.installed.version} installed · ${app.version} available`; - return { title: app.name, detail, titleValue: 215 }; - } - } - } - async handleInput(event: InputEvent, ctx: LayerContext): Promise { if (this.showingLogin) { if (event.type === "click" && !this.loginBusy) { @@ -292,16 +277,17 @@ export class EvenHubStoreLayer implements Layer { return; } const tab = this.tabs[this.activeTab]; + const menu = tabMenu(tab); switch (event.type) { case "scroll-up": - tab.selectedIndex = Math.max(0, tab.selectedIndex - 1); + await menu.handleInput(event); return; case "scroll-down": - tab.selectedIndex = Math.min(Math.max(0, tab.rows.length - 1), tab.selectedIndex + 1); - if (tab.selectedIndex >= tab.rows.length - 4) void this.loadNextPage(ctx, this.activeTab); + await menu.handleInput(event); + if ((menu.selectedIndex ?? 0) >= tab.rows.length - 4) void this.loadNextPage(ctx, this.activeTab); return; case "click": - await this.activateRow(ctx, tab.rows[tab.selectedIndex]); + await this.activateRow(ctx, menu.selectedItem ?? undefined); return; case "double-click": this.focus = "tabs"; @@ -360,13 +346,7 @@ export class EvenHubStoreLayer implements Layer { } case "app": if (this.updatingAll) return; - ctx.stack.push( - new EvenHubStoreDetailLayer(row.app, { - launchApp: this.options.launchApp, - appendLog: this.options.appendLog, - onInstalled: (installed) => this.onInstalled(installed), - }), - ); + ctx.stack.push(new EvenHubStoreDetailLayer(row.app, this.detailOptions())); return; } } @@ -384,13 +364,7 @@ export class EvenHubStoreLayer implements Layer { } this.pendingPackage = null; stack.clearToBase(); - stack.push( - new EvenHubStoreDetailLayer(placeholderStoreApp(installed), { - launchApp: this.options.launchApp, - appendLog: this.options.appendLog, - onInstalled: (result) => this.onInstalled(result), - }), - ); + stack.push(new EvenHubStoreDetailLayer(placeholderStoreApp(installed), this.detailOptions())); } /** Dictated text becomes a search (the shell's "Type Into App" / voice input). */ @@ -533,7 +507,7 @@ export class EvenHubStoreLayer implements Layer { state.rows = [{ kind: "search-query" }]; state.loaded = !query; // Keep the cursor on the first result once there are some. - state.selectedIndex = query ? 1 : 0; + state.menu.setItems(state.rows, query ? 1 : 0); this.tabs.search = state; ctx.actions.requestRender(); if (query) await this.loadNextPage(ctx, "search"); @@ -559,8 +533,8 @@ export class EvenHubStoreLayer implements Layer { update.latest ? { kind: "app", app: update.latest, update } : { kind: "orphan", update }, ); state.rows = rows; - state.selectedIndex = 0; - state.scrollRow = 0; + state.menu.setItems(rows, 0); + state.menu.scrollTop = 0; state.status = ""; state.exhausted = true; } catch (error) { @@ -607,6 +581,8 @@ export class EvenHubStoreLayer implements Layer { failures.push(`${row.app.name} (declined)`); } } catch (error) { + // An expired session fails every remaining update the same way. + if (this.handleAuthError(ctx, error)) return; const message = cleanError(error); this.options.appendLog(`evenhub store: update of ${row.app.packageId} failed: ${message}`); failures.push(row.app.name); @@ -636,21 +612,43 @@ export class EvenHubStoreLayer implements Layer { state.rows = state.rows.filter((row) => row.kind === "search-query" ? true : row.kind === "app" ? row.app.packageId !== packageId : row.update.installed.packageId !== packageId, ); - state.selectedIndex = clamp(state.selectedIndex, 0, Math.max(0, state.rows.length - 1)); + state.menu.setItems(state.rows); } // ---- login and phone editors -------------------------------------------- - /** Route a rejected session back to the login form; true if it was one. */ + /** + * Drop back to the logged-out pane when Even rejects the session (an + * expired login); true if the error was one. Anything open above the list + * (an app page, a permission dialog) needed the session too, so it goes. + * The phone form is not opened: this can happen on the first load of a + * store window restored at startup (see paint), and the pane's click + * opens it. + */ private handleAuthError(ctx: LayerContext, error: unknown): boolean { if (!(error instanceof EvenHubAuthenticationError)) return false; const message = cleanError(error); - this.options.appendLog(`evenhub store login failed: ${message}`); - this.enterLogin(ctx, `Login failed: ${message}`); + this.options.appendLog(`evenhub store: session rejected: ${message}`); + if (this.closed) return true; + ctx.stack.clearToBase(); + this.enterLogin(ctx, `Signed out: ${message}`, false); return true; } - private enterLogin(ctx: LayerContext, status = "Sign in to browse public apps."): void { + private detailOptions(): EvenHubStoreDetailOptions { + return { + launchApp: this.options.launchApp, + appendLog: this.options.appendLog, + onInstalled: (installed) => this.onInstalled(installed), + onAuthError: (ctx, error) => this.handleAuthError(ctx, error), + }; + } + + private enterLogin( + ctx: LayerContext, + status = "Sign in to browse public apps.", + openEditor = true, + ): void { const wasShowingLogin = this.showingLogin; this.closePhoneEditor(ctx.actions, "search"); this.showingLogin = true; @@ -659,7 +657,7 @@ export class EvenHubStoreLayer implements Layer { for (const tab of TABS) this.tabs[tab.id] = emptyTab(); this.focus = "list"; if (!wasShowingLogin) resetEvenHubLoginForm(); - this.openCredentialEditor(ctx); + if (openEditor) this.openCredentialEditor(ctx); ctx.actions.requestRender(); } @@ -674,6 +672,7 @@ export class EvenHubStoreLayer implements Layer { void this.submitCredentials(ctx); }, { setting: evenHubRememberMeSetting, label: "Remember me" }, + () => { this.phoneEditor = "none"; clearEvenHubLoginForm(); }, ); } @@ -684,7 +683,7 @@ export class EvenHubStoreLayer implements Layer { this.phoneEditor = "none"; if (this.closed) return; void this.runSearch(ctx, evenHubSearchQuerySetting.get()); - }); + }, undefined, () => { this.phoneEditor = "none"; }); } /** Close the phone editor if one is open (optionally only a given kind). */ @@ -698,7 +697,17 @@ export class EvenHubStoreLayer implements Layer { const email = evenHubLoginEmailSetting.get(); const password = evenHubLoginPasswordSetting.get(); if (!email.trim() || !password) { - this.enterLogin(ctx, "Email and password are required."); + // A wholly empty submission (the keyboard's Done key on an untouched + // form) is treated as a cancel and leaves the editor closed -- the login + // pane still says how to get back in, and a click reopens the editor. + // A half-filled form is a real mistake, so that still reopens with the + // message. + const cancelled = !email.trim() && !password; + this.enterLogin( + ctx, + cancelled ? "Sign in to browse public apps." : "Email and password are required.", + !cancelled, + ); return; } this.loginBusy = true; @@ -731,6 +740,42 @@ export class EvenHubStoreLayer implements Layer { } } +/** A list row's title (with its shade) and dim detail line. */ +function rowText(row: StoreRow): { title: string; detail: string; titleValue: number } { + switch (row.kind) { + case "search-query": { + const query = evenHubSearchQuerySetting.get(); + return { + title: query ? `Search: ${query}` : "Search: (enter a query)", + detail: "Click to type on the phone, or use voice input.", + titleValue: 200, + }; + } + case "orphan": + return { + title: row.update.installed.name, + detail: row.update.error + ? `Package missing · store lookup failed: ${row.update.error}` + : "Package missing · not in the store · click to forget", + titleValue: 170, + }; + case "app": { + const { app, update } = row; + if (!update) { + return { + title: app.name, + detail: app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`, + titleValue: 215, + }; + } + const detail = update.packageMissing + ? `Package missing · reinstall ${app.version ? `version ${app.version}` : ""}`.trim() + : `Version ${update.installed.version} installed · ${app.version} available`; + return { title: app.name, detail, titleValue: 215 }; + } + } +} + /** A registry entry dressed as a store record, for pages opened before the store record arrives. */ function placeholderStoreApp(installed: InstalledEvenHubApp): EvenHubStoreApp { return { diff --git a/app/apps/evenhub/unpack-runtime.ios.ts b/app/apps/evenhub/unpack-runtime.ios.ts new file mode 100644 index 00000000..478294a3 --- /dev/null +++ b/app/apps/evenhub/unpack-runtime.ios.ts @@ -0,0 +1,21 @@ +import type { EvenHubManifest } from './ehpk'; + +/** Paths and a small manifest cross isolates; package bytes never become JSON. */ +export function unpackRuntime(path: string, directory: string): Promise { + return new Promise((resolve, reject) => { + const worker = new Worker('./package-extraction.worker'); + let settled = false; + const finish = (error?: string, manifest?: EvenHubManifest) => { + if (settled) return; + settled = true; clearTimeout(timeout); worker.terminate(); + if (error) reject(new Error(error)); + else if (manifest) resolve(manifest); + else reject(new Error('The package worker returned no manifest.')); + }; + const timeout = setTimeout(() => finish('Unpacking the EvenHub app timed out.'), 180_000); + worker.onmessage = (event: MessageEvent) => finish(event.data.error, event.data.manifest); + worker.onerror = event => { finish(String(event.message ?? event)); return true; }; + try { worker.postMessage({ path, directory }); } + catch (error) { finish(String(error)); } + }); +} diff --git a/app/apps/evenhub/unpack-runtime.ts b/app/apps/evenhub/unpack-runtime.ts new file mode 100644 index 00000000..5d7ceab6 --- /dev/null +++ b/app/apps/evenhub/unpack-runtime.ts @@ -0,0 +1,5 @@ +import { extractPackage } from './package-extraction'; +import type { EvenHubManifest } from './ehpk'; +export async function unpackRuntime(path: string, directory: string): Promise { + return extractPackage(path, directory); +} diff --git a/app/apps/evenhub/webview.ios.ts b/app/apps/evenhub/webview.ios.ts new file mode 100644 index 00000000..e32ecdbd --- /dev/null +++ b/app/apps/evenhub/webview.ios.ts @@ -0,0 +1,45 @@ +import { EVENHUB_BRIDGE_INJECT_SCRIPT, buildFaceclawExtensionsScript, type EvenHubSession } from './session' +import { FACECLAW_VERSION } from '../../version' +import type { EvenHubWebView } from './webview' +declare const FaceclawEvenHubWebView: any + +export const IOS_EVENHUB_TRANSPORT_SCRIPT = ` +window.__faceclawEvenHub = { postMessage: function(name, args, id) { + window.webkit.messageHandlers.faceclaw.postMessage({kind:'call', name:name, args:args, id:id}); +}}; +(function() { + ['log','warn','error'].forEach(function(level) { + var original = console[level]; + console[level] = function() { + original.apply(console, arguments); + window.webkit.messageHandlers.faceclaw.postMessage({kind:'console', message:Array.prototype.map.call(arguments, String).join(' ').slice(0,4000)}); + }; + }); + window.addEventListener('error', function(e) { console.error(e.message); }); + window.addEventListener('unhandledrejection', function(e) { console.error(String(e.reason)); }); +})(); +` + +export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView { + const host = FaceclawEvenHubWebView.new() + host.packageIdentifier = session.manifest.packageId + host.eventHandler = (json: string) => { + try { + const event = JSON.parse(json) + if (event.kind === 'call' && typeof event.name === 'string' && typeof event.args === 'string' && Number.isSafeInteger(event.id)) + session.handleBridgeCall(event.name, event.args, event.id) + else if (event.kind === 'loaded') session.webViewLoaded() + else if (event.kind === 'error') session.webViewFailed(String(event.message)) + else if (event.kind === 'console') console.log(`[evenhub:${session.manifest.name}] ${event.message}`) + } catch (error) { console.warn(`EvenHub bridge: ${error}`) } + } + // Defer loading until manager.ts has attached the session/window handles. + const script = IOS_EVENHUB_TRANSPORT_SCRIPT + EVENHUB_BRIDGE_INJECT_SCRIPT + buildFaceclawExtensionsScript(`Faceclaw/${FACECLAW_VERSION}`) + const loading = setTimeout(() => { + if (session.remoteUrl) host.startURLScript(session.remoteUrl, script) + else host.startEntrypointScript(session.distDir, session.manifest.entrypoint, script) + }, 0) + return { native: host, evaluateJs: js => host.evaluate(js), + destroy: () => { clearTimeout(loading); host.destroy() }, + showOnPhone: () => host.showOnPhone(), hideOnPhone: () => host.hideOnPhone() } +} diff --git a/app/apps/files/file-browser.ts b/app/apps/files/file-browser.ts index 819f53ef..a9470f4d 100644 --- a/app/apps/files/file-browser.ts +++ b/app/apps/files/file-browser.ts @@ -2,9 +2,10 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { truncateText, truncateLeft } from "../../graphics/textwrap"; import { GrayImage, type UiFont } from "../../graphics/image"; import { renderIcon, type IconName } from "../../graphics/icons"; -import { clamp } from "../../util/numeric-util"; -import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu"; -import { iconGridMinRowHeight, tightRowHeight } from "../../ui/metrics"; +import { type MenuItem } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { IconGrid, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid"; +import { centeredTextY, tightRowHeight } from "../../ui/metrics"; import { ConfigSettingEnum } from "../../ui/dashboard-settings"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; import { @@ -15,21 +16,18 @@ import { statPath, type DirectoryEntry, } from "../../native/file-access"; -import { directionalFallback, isWatchInput, type InputEvent } from "../../ui/gestures"; +import { directionalFallback, type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext } from "../../ui/layers"; -import { shell } from "../../ui/shell/shell"; const LIST_X = 20; +/** List view: the selection box extends this far left and right of the row text. */ +const LIST_HIGHLIGHT_PAD = 6; /** Header: one line holding both the title and the current path. */ function headerHeight(font: UiFont): number { return 10 + font.lineHeight; } -const GRID_COLS = 5; -const ICON_SIZE = 44; -const LABEL_GAP = 2; - export type FilesViewMode = "icons" | "list"; export const filesViewModeSetting = new ConfigSettingEnum({ @@ -102,23 +100,14 @@ type EntryItem = { type BrowserItem = { kind: "grant"; label: string } | { kind: "info"; label: string } | EntryItem; -type IconRow = - | { kind: "special"; item: BrowserItem; flatIndex: number } - | { kind: "grid"; items: EntryItem[]; firstIndex: number }; - -type IconMode = "row" | "item"; - /** * Filesystem browser with two views. The top "Places" level lists bookmarks - * and the storage roots; descending browses real directories. Icons view is a - * 5-column grid with launcher-style two-level selection (scroll picks a row, - * click drops into the row, scroll picks the item); list view is a flat list. + * and the storage roots; descending browses real directories. Icons view is + * the launcher's IconGrid (two-level ring selection, four-way watch + * selection; notice rows span the full width); list view is a flat list. * Click descends into a directory or picks a supported file; double-click * backs out one level (item selection, then parent directory, then Places, - * then leaving the browser). That is the ring's scheme; in icons view the - * watch (see handleIconsWatchInput) skips row mode and moves one cell at a - * time in four directions, exactly as in the launcher. Sized to its hosting - * stack. + * then leaving the browser). Sized to its hosting stack. */ export class FileBrowserLayer implements Layer { // Watch swipes are spatial in icons view: up/down move between rows, @@ -133,11 +122,26 @@ export class FileBrowserLayer implements Layer { private entries: DirectoryEntry[] | null = null; private listingFailed = false; - private listIndex = 0; - private selectedRow = 0; - private selectedCol = 0; - private iconMode: IconMode = "row"; - private scrollRow = 0; + /** List view. Both views index the same flatRows(), which is how setViewMode carries the selection across. */ + private readonly listMenu = new Menu({ + wrap: false, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawListRow(args), + }); + /** Icons view. */ + private readonly grid = new IconGrid({ + items: () => this.flatRows(), + isWide: (item) => item.kind !== "entry", + describe: (item, selected) => + item.kind === "entry" + ? { label: item.label, icon: gridIcon(item), labelValue: item.supported ? 210 : 100 } + : { label: item.label, labelValue: itemValue(item, selected) }, + onActivate: (item, _index, ctx) => this.activateItem(item, ctx), + onBack: () => this.navigateUp(), + scrollbar: true, + }); constructor(private readonly options: FileBrowserOptions) {} @@ -145,7 +149,6 @@ export class FileBrowserLayer implements Layer { const font = getDefaultSmallFont(); const { width, height } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - const rows = this.flatRows(); image.drawText(font, 20, 8, "Files", 220); const pathLabel = this.location === null ? "Places" : this.location; @@ -153,97 +156,30 @@ export class FileBrowserLayer implements Layer { image.drawText(font, pathX, 8, truncateLeft(font, pathLabel, width - pathX - 20), 130); if (filesViewModeSetting.get() === "list") { - this.paintList(image, font, rows, ctx); + this.paintList(image, font, ctx); } else { - this.paintIcons(image, font, rows, ctx); + const headerH = headerHeight(font); + this.grid.paint(image, { x: 0, y: headerH, width, height: height - headerH - 6 }, ctx.stack.isFocused()); } return image; } - private paintList(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void { + private paintList(image: GrayImage, font: UiFont, ctx: LayerContext): void { const { width, height } = ctx.stack.getBaseSize(); - this.listIndex = clamp(this.listIndex, 0, Math.max(0, rows.length - 1)); - - const rowH = tightRowHeight(font); const headerH = headerHeight(font); - const listHeight = height - headerH - 4; - const visibleRows = Math.max(1, (listHeight / rowH) | 0); - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.listIndex, visibleRows, rows.length); - - const lastVisible = Math.min(rows.length, this.scrollRow + visibleRows); - for (let index = this.scrollRow; index < lastVisible; index++) { - const row = rows[index]!; - const y = headerH + (index - this.scrollRow) * rowH; - const selected = index === this.listIndex; - if (selected) { - drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4); - } - const value = itemValue(row, selected); - image.drawText(font, LIST_X, y + 1, truncateText(font, row.label, width - 2 * LIST_X), value); - } + this.listMenu.setItems(this.flatRows()); + // Row boxes start one pixel above the header's bottom edge. + this.listMenu.paint( + image, + { x: LIST_X - LIST_HIGHLIGHT_PAD, y: headerH - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: height - headerH - 4 }, + ctx.stack.isFocused(), + ); } - private paintIcons(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void { - const { width, height } = ctx.stack.getBaseSize(); - const focused = ctx.stack.isFocused(); - const iconRows = buildIconRows(rows); - this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1)); - - const gridTop = headerHeight(font); - const gridBottom = height - 6; - const visibleRows = Math.max(1, Math.floor((gridBottom - gridTop) / iconGridMinRowHeight(font, ICON_SIZE, LABEL_GAP))); - const rowH = Math.floor((gridBottom - gridTop) / visibleRows); - const colW = width / GRID_COLS; - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, visibleRows, iconRows.length); - - const lastVisible = Math.min(iconRows.length, this.scrollRow + visibleRows); - for (let rowIndex = this.scrollRow; rowIndex < lastVisible; rowIndex++) { - const row = iconRows[rowIndex]!; - const y = gridTop + (rowIndex - this.scrollRow) * rowH; - const selected = rowIndex === this.selectedRow; - - if (row.kind === "special") { - const textY = y + (((rowH - font.lineHeight) / 2) | 0); - if (selected) { - drawSelectionHighlight(image, LIST_X - 6, textY - 2, width - 2 * LIST_X + 12, font.lineHeight + 4, focused, 4); - } - image.drawText(font, LIST_X, textY, truncateText(font, row.item.label, width - 2 * LIST_X), itemValue(row.item, selected)); - continue; - } - - if (selected) { - // Same preview rule as the launcher: while defocused with the watch - // as the last-used source, show the cell a click would land on (watch - // focus enters item mode directly; see onFocus) instead of a row band - // the watch scheme never shows. - const cellHighlight = this.iconMode === "item" || (!focused && shell.lastInputWasWatch()); - if (cellHighlight) { - this.selectedCol = clamp(this.selectedCol, 0, row.items.length - 1); - drawSelectionHighlight(image, this.selectedCol * colW + 4, y + 1, colW - 8, rowH - 2, focused, 6); - } else { - drawSelectionHighlight(image, 4, y + 1, width - 8, rowH - 2, focused, 6); - } - } - - for (let col = 0; col < row.items.length; col++) { - const item = row.items[col]!; - const centerX = col * colW + colW / 2; - const blockTop = y + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2); - const icon = renderIcon(item.icon, ICON_SIZE); - if (icon) { - image.bitBlt(item.supported ? icon : dimmedIcon(item.icon, icon), Math.round(centerX - icon.width / 2), Math.round(blockTop), { - transparentZero: true, - }); - } - const label = truncateText(font, item.label, colW - 8); - const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP); - image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, item.supported ? 210 : 100); - } - } - - if (iconRows.length > visibleRows) { - drawListScrollbar(image, width - 5, gridTop, visibleRows * rowH - 4, this.scrollRow, visibleRows, iconRows.length); - } + private drawListRow({ image, item, x, y, width, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const label = truncateText(font, item.label, width - 2 * LIST_HIGHLIGHT_PAD); + image.drawText(font, x + LIST_HIGHLIGHT_PAD, centeredTextY(font, y, height), label, itemValue(item, selected)); } async handleInput(event: InputEvent, ctx: LayerContext): Promise { @@ -252,40 +188,24 @@ export class FileBrowserLayer implements Layer { // standard scroll / select / back meanings (the layer opted into // directional delivery for the icons view, so the stack won't). await this.handleListInput(directionalFallback(event), ctx); - } else if (isWatchInput(event)) { - await this.handleIconsWatchInput(event, ctx); } else { - await this.handleIconsInput(event, ctx); + await this.grid.handleInput(event, ctx); } } - /** - * Focus arriving from the watch goes straight to item selection, as in the - * launcher: the watch has left/right swipes, so it never needs row mode. - * Any other source keeps the two-level scheme and enters in row mode. - */ onFocus(lastInput: InputEvent | null): void { - if (filesViewModeSetting.get() !== "icons") return; - if (lastInput && isWatchInput(lastInput)) { - this.iconMode = "item"; - const iconRows = buildIconRows(this.flatRows()); - this.clampColToRow(iconRows[clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1))]); - } else { - this.iconMode = "row"; - } + if (filesViewModeSetting.get() === "icons") this.grid.onFocus(lastInput); } private async handleListInput(event: InputEvent, ctx: LayerContext): Promise { - const rows = this.flatRows(); + this.listMenu.setItems(this.flatRows()); switch (event.type) { case "scroll-up": - this.listIndex = Math.max(0, this.listIndex - 1); - return; case "scroll-down": - this.listIndex = Math.min(Math.max(0, rows.length - 1), this.listIndex + 1); + await this.listMenu.handleInput(event); return; case "click": { - const row = rows[clamp(this.listIndex, 0, Math.max(0, rows.length - 1))]; + const row = this.listMenu.selectedItem; if (row) await this.activateItem(row, ctx); return; } @@ -297,147 +217,6 @@ export class FileBrowserLayer implements Layer { } } - private async handleIconsInput(event: InputEvent, ctx: LayerContext): Promise { - const rows = this.flatRows(); - const iconRows = buildIconRows(rows); - this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1)); - const row = iconRows[this.selectedRow]; - switch (event.type) { - case "scroll-up": - case "scroll-down": { - const delta = event.type === "scroll-down" ? 1 : -1; - if (this.iconMode === "item") { - // Item selection traverses linearly: past a row's edge it continues - // onto the adjacent row (a special row counts as a single item). - if (row?.kind === "grid") { - const next = clamp(this.selectedCol, 0, row.items.length - 1) + delta; - if (next >= 0 && next < row.items.length) { - this.selectedCol = next; - return; - } - } - const adjacentIndex = this.selectedRow + delta; - if (adjacentIndex < 0 || adjacentIndex >= iconRows.length) return; - this.selectedRow = adjacentIndex; - const adjacent = iconRows[adjacentIndex]!; - if (adjacent.kind === "grid") { - this.selectedCol = delta > 0 ? 0 : adjacent.items.length - 1; - } - } else { - this.selectedRow = clamp(this.selectedRow + delta, 0, Math.max(0, iconRows.length - 1)); - } - return; - } - case "click": { - if (!row) return; - if (row.kind === "special") { - await this.activateItem(row.item, ctx); - return; - } - if (this.iconMode === "row") { - this.iconMode = "item"; - // Default to the middle column (clamped to the row's item count). - this.selectedCol = Math.min(Math.floor(GRID_COLS / 2), row.items.length - 1); - } else { - const item = row.items[clamp(this.selectedCol, 0, row.items.length - 1)]; - if (item) await this.activateItem(item, ctx); - } - return; - } - case "double-click": - if (this.iconMode === "item") { - this.iconMode = "row"; - } else { - this.navigateUp(); - } - return; - default: - return; - } - } - - /** - * The watch's scheme for the icons view — the launcher's: there is no row - * mode, the selection is always one cell (a special row counts as its - * single full-width item). Up/down (and the crown) move between rows - * keeping the column, right/left move within the row, and left from the - * leftmost column keeps going out — parent directory, Places, then the - * sidebar. Row mode is restored on the way out so a ring user finds the - * grid as they left it. - */ - private async handleIconsWatchInput(event: InputEvent, ctx: LayerContext): Promise { - const iconRows = buildIconRows(this.flatRows()); - this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1)); - const row = iconRows[this.selectedRow]; - if (this.iconMode === "row") { - this.iconMode = "item"; - this.clampColToRow(row); - } - switch (event.type) { - case "swipe-up": - case "swipe-down": - case "scroll-up": - case "scroll-down": { - const delta = event.type === "swipe-down" || event.type === "scroll-down" ? 1 : -1; - this.selectedRow = clamp(this.selectedRow + delta, 0, Math.max(0, iconRows.length - 1)); - // Keep the column; a shorter row (or a special one) clamps it. - this.clampColToRow(iconRows[this.selectedRow]); - return; - } - case "swipe-right": - if (row?.kind === "grid") { - this.selectedCol = clamp(this.selectedCol + 1, 0, row.items.length - 1); - } - return; - case "swipe-left": - if (row?.kind === "grid" && this.selectedCol > 0) { - this.selectedCol--; - } else { - this.watchNavigateUp(); - } - return; - case "click": { - if (!row) return; - if (row.kind === "special") { - await this.activateItem(row.item, ctx); - } else { - const item = row.items[clamp(this.selectedCol, 0, row.items.length - 1)]; - if (item) await this.activateItem(item, ctx); - } - // Descending into a directory resets the selection for the ring (row - // mode at the top); the watch scheme stays in single-item selection. - this.iconMode = "item"; - return; - } - case "double-click": - this.watchNavigateUp(); - return; - default: - return; - } - } - - private clampColToRow(row: IconRow | undefined): void { - if (row?.kind === "grid") { - this.selectedCol = clamp(this.selectedCol, 0, row.items.length - 1); - } - } - - /** - * navigateUp for the watch scheme: inside the browser the selection stays - * in item mode (resetSelection/selectPath put it back in row mode for the - * ring); leaving from Places restores row mode on the way out. - */ - private watchNavigateUp(): void { - if (this.location === null) { - this.iconMode = "row"; - this.options.onLeave(); - return; - } - this.navigateUp(); - this.iconMode = "item"; - } - /** * App-specific entries for the window long-press menu: the view switch, * plus bookmark/unbookmark for the selected entry (in icons view only once @@ -461,8 +240,7 @@ export class FileBrowserLayer implements Layer { label: isBookmarked(path) ? `Remove bookmark: ${name}` : `Bookmark: ${name}`, onSelect: (ctx) => { toggleBookmark(path); - const rows = this.flatRows(); - this.listIndex = clamp(this.listIndex, 0, Math.max(0, rows.length - 1)); + this.listMenu.setItems(this.flatRows()); ctx.stack.pop(); }, }); @@ -475,34 +253,27 @@ export class FileBrowserLayer implements Layer { const rows = this.flatRows(); if (!rows.length) return null; if (filesViewModeSetting.get() === "list") { - return rows[clamp(this.listIndex, 0, rows.length - 1)] ?? null; + this.listMenu.setItems(rows); + return this.listMenu.selectedItem; } - const iconRows = buildIconRows(rows); - const row = iconRows[clamp(this.selectedRow, 0, iconRows.length - 1)]; - if (!row) return null; - if (row.kind === "special") return row.item; - if (this.iconMode !== "item") return null; - return row.items[clamp(this.selectedCol, 0, row.items.length - 1)] ?? null; + return this.grid.selectedItem; } private setViewMode(mode: FilesViewMode): void { if (filesViewModeSetting.get() === mode) return; - const rows = this.flatRows(); - const iconRows = buildIconRows(rows); + this.listMenu.setItems(this.flatRows()); if (mode === "list") { - // Carry the icon-grid selection into the flat list (clamped column when - // only a row was selected). - const row = iconRows[clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1))]; - if (row) { - this.listIndex = - row.kind === "special" ? row.flatIndex : row.firstIndex + clamp(this.selectedCol, 0, row.items.length - 1); - } + // Carry the icon-grid selection into the flat list (the remembered + // column when only a row was selected). + const index = this.grid.cursorIndex; + if (index !== null) this.listMenu.select(index); } else { - this.setGridSelectionFromFlatIndex(rows, clamp(this.listIndex, 0, Math.max(0, rows.length - 1))); - this.iconMode = "row"; + const index = this.listMenu.selectedIndex ?? 0; + this.grid.resetSelection(); + this.grid.selectIndex(index); } filesViewModeSetting.set(mode); - this.scrollRow = 0; + this.listMenu.scrollTop = 0; } private async activateItem(item: BrowserItem, ctx: LayerContext): Promise { @@ -528,11 +299,12 @@ export class FileBrowserLayer implements Layer { this.resetSelection(); } - private navigateUp(): void { + /** Back out one level; true when that left the browser (from Places). */ + private navigateUp(): boolean { const from = this.location; if (from === null) { this.options.onLeave(); - return; + return true; } if (from === this.navRoot || from === "/") { this.location = null; @@ -540,19 +312,18 @@ export class FileBrowserLayer implements Layer { this.resetSelection(); this.selectPath(this.navRoot ?? from); this.navRoot = null; - return; + return false; } const parentRaw = from.slice(0, from.lastIndexOf("/")); this.navigateTo(parentRaw.length ? parentRaw : "/"); this.selectPath(from); + return false; } private resetSelection(): void { - this.listIndex = 0; - this.selectedRow = 0; - this.selectedCol = 0; - this.iconMode = "row"; - this.scrollRow = 0; + this.listMenu.select(0); + this.listMenu.scrollTop = 0; + this.grid.resetSelection(); } /** Select the item with the given path (e.g. the directory just left). */ @@ -560,33 +331,25 @@ export class FileBrowserLayer implements Layer { const rows = this.flatRows(); const index = rows.findIndex((row) => row.kind === "entry" && row.entry.path === path); if (index < 0) return; - this.listIndex = index; - this.setGridSelectionFromFlatIndex(rows, index); - } - - private setGridSelectionFromFlatIndex(rows: BrowserItem[], index: number): void { - const specials = leadingSpecialCount(rows); - if (index < specials) { - this.selectedRow = index; - this.selectedCol = 0; - return; - } - const ordinal = index - specials; - this.selectedRow = specials + Math.floor(ordinal / GRID_COLS); - this.selectedCol = ordinal % GRID_COLS; + // Items first, then the selection, so the viewport scrolls minimally from the top. + this.listMenu.setItems(rows); + this.listMenu.select(index); + this.grid.selectIndex(index); } private flatRows(): BrowserItem[] { const rows: BrowserItem[] = []; - if (!hasAllFilesAccess()) { - rows.push({ kind: "grant", label: "Grant file access (opens phone Settings)" }); - } if (this.location === null) { + // Only on Places: without All Files access some folders (e.g. Download) + // may still be readable, and the prompt shouldn't crowd their listings. + if (!hasAllFilesAccess()) { + rows.push({ kind: "grant", label: "Grant file access (opens phone Settings)" }); + } for (const path of getBookmarkedPaths()) { rows.push(this.bookmarkItem(path)); } - rows.push(this.placeItem("Internal storage", externalStorageRootPath())); - rows.push(this.placeItem("/", "/")); + rows.push(this.placeItem(global.isIOS ? "Faceclaw documents" : "Internal storage", externalStorageRootPath())); + if (!global.isIOS) rows.push(this.placeItem("/", "/")); return rows; } this.loadEntries(); @@ -654,27 +417,6 @@ export class FileBrowserLayer implements Layer { } } -function buildIconRows(flat: BrowserItem[]): IconRow[] { - const rows: IconRow[] = []; - let index = 0; - // Special rows (grant prompt, empty/unreadable info) lead the list; each - // gets a full-width row of its own. Entries follow, chunked into grid rows. - while (index < flat.length && flat[index]!.kind !== "entry") { - rows.push({ kind: "special", item: flat[index]!, flatIndex: index }); - index++; - } - for (; index < flat.length; index += GRID_COLS) { - rows.push({ kind: "grid", items: flat.slice(index, index + GRID_COLS) as EntryItem[], firstIndex: index }); - } - return rows; -} - -function leadingSpecialCount(rows: BrowserItem[]): number { - let count = 0; - while (count < rows.length && rows[count]!.kind !== "entry") count++; - return count; -} - function itemValue(item: BrowserItem, selected: boolean): number { if (item.kind === "info") return 120; if (item.kind === "grant") return selected ? 255 : 210; @@ -682,6 +424,11 @@ function itemValue(item: BrowserItem, selected: boolean): number { return selected ? 255 : 200; } +function gridIcon(item: EntryItem): GrayImage | null { + const icon = renderIcon(item.icon, ICON_GRID_ICON_SIZE); + return icon && !item.supported ? dimmedIcon(item.icon, icon) : icon; +} + // Unsupported files draw their grid icon at half brightness; rendered once // per icon name and cached (all grid icons share one size). const dimmedIconCache = new Map(); diff --git a/app/apps/files/file-info-dialog.ts b/app/apps/files/file-info-dialog.ts index ff44fec2..5a35528b 100644 --- a/app/apps/files/file-info-dialog.ts +++ b/app/apps/files/file-info-dialog.ts @@ -4,7 +4,7 @@ import { wrapText } from "../../graphics/textwrap"; import type { DirectoryEntry } from "../../native/file-access"; import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers"; -import { drawSelectionHighlight } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; const DIALOG_X = 8; @@ -25,12 +25,21 @@ export type FileInfoAction = { * double-click closes it. */ export class FileInfoDialogLayer implements Layer { - private selectedIndex = 0; + private readonly menu: Menu; constructor( private readonly entry: DirectoryEntry, private readonly actions: FileInfoAction[], - ) {} + ) { + this.menu = new Menu({ + items: actions, + wrap: true, + rowGap: 1, + highlight: { radius: 8 }, + getHeight: () => listRowHeight(getDefaultSmallFont()), + draw: (args) => drawActionRow(args), + }); + } paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { const font = getDefaultSmallFont(); @@ -69,32 +78,23 @@ export class FileInfoDialogLayer implements Layer { image.drawText(font, DIALOG_X + PADDING + 2, y + 3, `${GESTURE_DOUBLE_CLICK} close`, 110); return image; } - const focused = ctx.stack.isFocused(); - for (let index = 0; index < this.actions.length; index++) { - const rowY = y + index * rowH; - const selected = index === this.selectedIndex; - if (selected) { - drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, rowH - 1, focused, 8); - } - image.drawText(font, DIALOG_X + 22, rowY + LIST_ROW_TEXT_INSET, this.actions[index]!.label, selected ? 255 : 200); - } + // The rows fill the rest of the dialog (and scroll if a clamped dialog is too short). + this.menu.paint( + image, + { x: DIALOG_X + 12, y, width: DIALOG_WIDTH - 24, height: Math.max(0, DIALOG_Y + height - PADDING - y) }, + ctx.stack.isFocused(), + ); return image; } async handleInput(event: InputEvent, ctx: LayerContext): Promise { switch (event.type) { case "scroll-up": - if (this.actions.length) { - this.selectedIndex = (this.selectedIndex + this.actions.length - 1) % this.actions.length; - } - return; case "scroll-down": - if (this.actions.length) { - this.selectedIndex = (this.selectedIndex + 1) % this.actions.length; - } + await this.menu.handleInput(event); return; case "click": - await this.actions[this.selectedIndex]?.onSelect(ctx); + await this.menu.selectedItem?.onSelect(ctx); return; case "double-click": ctx.stack.pop(); @@ -105,6 +105,10 @@ export class FileInfoDialogLayer implements Layer { } } +function drawActionRow({ image, item, x, y, selected }: MenuDrawArgs): void { + image.drawText(getDefaultSmallFont(), x + 10, y + LIST_ROW_TEXT_INSET, item.label, selected ? 255 : 200); +} + function formatSize(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; diff --git a/app/apps/files/files-app.ts b/app/apps/files/files-app.ts index 01eaa065..615c8f9a 100644 --- a/app/apps/files/files-app.ts +++ b/app/apps/files/files-app.ts @@ -45,7 +45,7 @@ export type FilesAppOptions = InProcessAppOptions & { export function createFilesAppWindow(options: FilesAppOptions): InProcessWindow { let created: InProcessWindow | null = null; const browser = new FileBrowserLayer({ - isSupportedFile: (name) => TEXT_FILE.test(name) || isDecodableImageFile(name) || EHPK_FILE.test(name) || isFontFile(name), + isSupportedFile: (name) => TEXT_FILE.test(name) || isFontFile(name) || EHPK_FILE.test(name) || (!global.isIOS && isDecodableImageFile(name)), // The browser handles double-click itself (up a level), so it is not // wrapped in YieldAtRootLayer; it yields explicitly from the top level. onLeave: () => shell.yieldFocusToSidebar(), @@ -170,6 +170,7 @@ function confirmEhpkPermissions(ctx: LayerContext, entry: DirectoryEntry, procee * window for viewable types, empty for everything else (metadata only). */ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileInfoAction[] { + if (global.isIOS && !TEXT_FILE.test(entry.name) && !isFontFile(entry.name) && !EHPK_FILE.test(entry.name)) return []; if (TEXT_FILE.test(entry.name)) { return [ { @@ -191,7 +192,7 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI ]; } if (EHPK_FILE.test(entry.name)) { - return [ + const actions: FileInfoAction[] = [ { label: "Run app", onSelect: (ctx) => { @@ -209,6 +210,7 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI }, }, ]; + return actions; } if (isDecodableImageFile(entry.name)) { return [ diff --git a/app/apps/files/font-previewer.ts b/app/apps/files/font-previewer.ts index 46a4aea2..a486497a 100644 --- a/app/apps/files/font-previewer.ts +++ b/app/apps/files/font-previewer.ts @@ -27,7 +27,7 @@ const PARAGRAPH = /** * Font previewer for a font file on disk (TTF/OTF/TTC), rendered through the - * Android text stack: a single-line pangram, a character-set sample, and a + * native text stack: a single-line pangram, a character-set sample, and a * wrapped paragraph, all at an adjustable pixel size. Scroll changes size, * click cycles the coverage gamma (for judging antialiasing on the lens), * double-click closes. Sized to its hosting stack (a Files document window, diff --git a/app/apps/files/index.ts b/app/apps/files/index.ts index 0b744e48..92301cca 100644 --- a/app/apps/files/index.ts +++ b/app/apps/files/index.ts @@ -64,6 +64,7 @@ const filesApp: AppDefinition = { openEhpkApp: (path) => { void openEvenHubPackage(ctx, path).catch((error) => { ctx.appendLog(`evenhub launch failed: ${error}`); + openTextDocumentWindow(ctx, 'Could not run app', String(error)); }); }, installEhpkApp: async (path) => { diff --git a/app/apps/flappy/flappy-app.worker.ts b/app/apps/flappy/flappy-app.worker.ts index b0b998db..d880c239 100644 --- a/app/apps/flappy/flappy-app.worker.ts +++ b/app/apps/flappy/flappy-app.worker.ts @@ -18,19 +18,21 @@ * small and rare. * * Controls (a watch swipe in either direction is the primary flap): - * swipe-up, swipe-down, click, or scroll-up flaps; double-click pauses; + * ring-press flaps immediately; watch swipes/clicks also flap. Double-click pauses; * tap-then-hold opens the window menu. Ready / paused / game over: click or * a swipe starts or resumes, double-click yields focus. Losing input focus * mid-flight (a shell overlay such as the system menu or a notification, * focus to the sidebar) pauses, as does backgrounding or screen-off. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; import { getFont } from "../../graphics/bdffont"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; +import { playWorkerBuzzerSequence } from "../../native/worker-buzzer"; import { getActiveDisplay } from "../../native/active-display"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; @@ -64,7 +66,6 @@ import { } from "./flappy-sprites"; declare const global: any; -declare const com: any; const largeFont = getFont("terminus32"); const mediumFont = getFont("terminus24"); @@ -180,6 +181,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": { const window: FlappyWindow = { windowId: message.windowId, @@ -311,7 +318,7 @@ function saveHighScore(window: FlappyWindow): void { /** * Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the - * steps on its own timer, and the Java call is safe from the worker thread. + * steps on its own timer; the platform bridge routes it from the worker. * Minor (frequent) effects are dropped when they'd arrive within the * rate-limit gap; newsworthy ones always play. */ @@ -321,9 +328,7 @@ function playSfx(window: FlappyWindow, steps: Step[], minor = false): void { if (minor && now - window.lastMinorSfxAtMs < MINOR_SFX_MIN_GAP_MS) return; if (minor) window.lastMinorSfxAtMs = now; try { - const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); - if (!communicator) return; - communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer); + playWorkerBuzzerSequence(buildSoundSequencePayload(steps)); } catch (error) { console.warn(`flappy sfx failed: ${error}`); } @@ -412,10 +417,16 @@ function handleFlightInput(window: FlappyWindow, event: InputEvent, frameId: num // which way the thumb went shouldn't matter. case "swipe-up": case "swipe-down": - case "click": - case "scroll-up": + case "ring-press": flap(window); break; + case "click": + // The ring already flapped on down; its later click must not flap again. + if (event.source !== "ring") flap(window); + break; + case "scroll-up": + if (event.source === "watch") flap(window); + break; case "double-click": if (window.phase === "playing") { window.phase = "paused"; @@ -698,7 +709,7 @@ function paintHud(image: GrayImage, window: FlappyWindow): void { if (window.highScore > 0) { drawCenteredIn(image, smallFont, 0, width, 44, `best ${window.highScore}`, 150); } - drawCenteredIn(image, smallFont, 0, width, groundTop(window) - 26, `swipe / ${GESTURE_CLICK} flap`, 150); + drawCenteredIn(image, smallFont, 0, width, groundTop(window) - 26, "Touch ring / swipe to flap", 150); break; case "paused": paintDialog(image, window, "PAUSED", [`${GESTURE_CLICK} resume`, `${GESTURE_DOUBLE_CLICK} leave`]); @@ -757,17 +768,19 @@ function renderAndSubmit(window: FlappyWindow, inputFrameId: number): void { } const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes)); const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer()); - communicator.submitSurfaceFrame( - buffer.buffer, - window.surfaceId, - 0, - 0, - image.width, - image.height, - fingerprint, - paintMs, - frameId, - frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)), + frameTimings.span(frameId, "submitSurfaceFrame", () => + communicator.submitSurfaceFrame( + buffer.buffer, + window.surfaceId, + 0, + 0, + image.width, + image.height, + fingerprint, + paintMs, + frameId, + frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)), + ) ); window.lastSubmittedFingerprint = fingerprint; } catch (error) { diff --git a/app/apps/freecell/freecell-app.worker.ts b/app/apps/freecell/freecell-app.worker.ts index 070f90ae..ae4f2dc0 100644 --- a/app/apps/freecell/freecell-app.worker.ts +++ b/app/apps/freecell/freecell-app.worker.ts @@ -18,6 +18,7 @@ * Safe cards auto-play to the foundations after every move. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -25,6 +26,7 @@ import { getFont } from "../../graphics/bdffont"; import { ensurePreinstalledFonts, installedFontPath } from "../../graphics/installed-fonts"; import { TtfFont } from "../../graphics/ttf-font"; import * as frameTimings from "../../native/frame-timings"; +import { playWorkerBuzzerSequence } from "../../native/worker-buzzer"; import { getActiveDisplay } from "../../native/active-display"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; @@ -34,7 +36,6 @@ import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-win import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, type InputEvent } from "../../ui/gestures"; declare const global: any; -declare const com: any; ensurePreinstalledFonts(); const fontPath = installedFontPath("Roboto-Regular.ttf"); @@ -144,6 +145,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": { const window: FreecellWindow = { windowId: message.windowId, @@ -206,14 +213,12 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { /** * Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the - * steps on its own timer, and the Java call is safe from the worker thread. + * steps on its own timer; the platform bridge routes it from the worker. */ function playSfx(window: FreecellWindow, steps: Step[]): void { if (!window.soundOn || steps.length === 0) return; try { - const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); - if (!communicator) return; - communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer); + playWorkerBuzzerSequence(buildSoundSequencePayload(steps)); } catch (error) { console.warn(`freecell sfx failed: ${error}`); } diff --git a/app/apps/glanceboard/configuration.ts b/app/apps/glanceboard/configuration.ts new file mode 100644 index 00000000..a783df47 --- /dev/null +++ b/app/apps/glanceboard/configuration.ts @@ -0,0 +1,11 @@ +import { getStringSetting } from "../../native/settings-store"; +import { QUADRANT_LAYOUT, SIX_SLOT_LAYOUT } from "./layout"; + +/** Configuration-only query usable by workers without loading widget/UI modules. */ +export function isTerminalWidgetConfigured(): boolean { + const layout = getStringSetting("glanceboard.layout", "2x2") === "2x3" ? SIX_SLOT_LAYOUT : QUADRANT_LAYOUT; + // Both layouts store their slots under the original quadrants key. None + // of the default slots needs a worker, so absent keys cannot retain one. + return layout.slots.some((_, index) => + getStringSetting(`glanceboard.${QUADRANT_LAYOUT.id}.slot.${index}`, "none") === "terminal"); +} diff --git a/app/apps/glanceboard/glanceboard-app.ts b/app/apps/glanceboard/glanceboard-app.ts index a0e2b236..e6bc38f7 100644 --- a/app/apps/glanceboard/glanceboard-app.ts +++ b/app/apps/glanceboard/glanceboard-app.ts @@ -3,15 +3,17 @@ import { truncateText, wrapText } from "../../graphics/textwrap"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from "../../ui/dashboard-settings"; import { type InputEvent } from "../../ui/gestures"; -import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers"; -import { drawSelectionHighlight, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu"; -import { LIST_ROW_TEXT_INSET, lineStep } from "../../ui/metrics"; +import { type Layer, type LayerContext } from "../../ui/layers"; +import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu"; +import { Menu } from "../../ui/menu-core"; +import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window"; import { shell } from "../../ui/shell/shell"; import { GlanceBoard } from "./board"; import { clearConflictingSlots, glanceboardEnabledSetting, + glanceDepthSetting, glanceLayout, glanceLayoutSetting, glanceShowLinesSetting, @@ -35,6 +37,15 @@ const PARAGRAPH_X = 24; const PREVIEW_MARGIN = 8; const PREVIEW_MIN_LINE_VALUE = 20; const PREVIEW_LINE_VALUE = 100; +/** The entry menu's row boxes: left inset, gap below the paragraph, and inset from the page's bottom. */ +const HOME_MENU_X = 20; +const HOME_MENU_TOP_GAP = 12; +const HOME_MENU_BOTTOM_MARGIN = 8; +/** Horizontal inset of row text (and custom row content) from its selection box. */ +const HOME_ROW_TEXT_X = 10; +// A throwaway menu to satisfy MenuItem.onSelect's second parameter; the +// entry page's items never use it (they act via ctx only). +const NO_MENU = new MenuLayer(null, []); const INTRO = "Your Glanceboard is a display for things you want to look at quickly. Starting " + @@ -55,25 +66,21 @@ function pageMenuLayout(width: number) { * a double-tap returns focus to the menu. */ class GlanceboardHomeLayer implements Layer { - private menu: MenuLayer | null = null; - private menuTop = 0; - private menuHeight = 0; + private readonly menu: Menu; private focus: "menu" | "preview" = "menu"; private selectedSlot = 0; + /** The context of the paint in progress, handed to item render callbacks. */ + private paintCtx: LayerContext | null = null; - constructor(private readonly setPreviewVisible: (visible: boolean) => void) {} - - private currentMenu(ctx: LayerContext, menuTop: number): MenuLayer { - const { width, height } = ctx.stack.getBaseSize(); - if (!this.menu || this.menuTop !== menuTop || this.menuHeight !== height) { - const half = (width / 2) | 0; - const items: MenuItem[] = [ + constructor(private readonly setPreviewVisible: (visible: boolean) => void) { + this.menu = new Menu({ + items: [ toggleSettingMenuItem(glanceboardEnabledSetting), { label: "Preview", - onSelect: (menuCtx) => { - menuCtx.stack.push(new GlancePreviewLayer( - () => menuCtx.actions.requestRender(), + onSelect: (ctx) => { + ctx.stack.push(new GlancePreviewLayer( + () => ctx.actions.requestRender(), () => this.setPreviewVisible(false), )); this.setPreviewVisible(true); @@ -87,17 +94,32 @@ class GlanceboardHomeLayer implements Layer { }, { label: "Settings", - onSelect: (menuCtx) => menuCtx.stack.push(new GlanceSettingsLayer()), + onSelect: openGlanceSettings, }, - ]; - this.menuTop = menuTop; - this.menuHeight = height; - this.menu = new MenuLayer(null, items, { - x: 8, y: menuTop, width: half - 16, - showBorder: false, minHeight: 0, maxHeight: height - menuTop, - }); - } - return this.menu; + ], + wrap: true, + rowGap: 1, + getHeight: () => listRowHeight(getDefaultSmallFont()), + draw: ({ image, item, x, y, width, height, selected }) => { + const disabled = isMenuItemDisabled(item); + if (item.render) { + item.render({ + image, + x: x + HOME_ROW_TEXT_X, + y, + width: width - 2 * HOME_ROW_TEXT_X, + height: height - 2, + selected, + disabled, + text: item.label, + ctx: this.paintCtx!, + }); + } else { + image.drawText(getDefaultSmallFont(), x + HOME_ROW_TEXT_X, y + LIST_ROW_TEXT_INSET, item.label, + disabled ? 70 : selected ? 255 : 200); + } + }, + }); } /** Where the scaled board sits: the right half, board aspect, vertically centred. */ @@ -128,13 +150,19 @@ class GlanceboardHomeLayer implements Layer { image.drawText(font, PARAGRAPH_X, y, line, 160); y += step; } - // The menu paints onto this page below the paragraph (its fill is raster, - // so the deferred paragraph glyphs above stay untouched either way). - // While the preview has focus the menu keeps its outline-only selection. - const menu = this.currentMenu(ctx, y + 4); - const menuFocused = ctx.stack.isFocused() && this.focus === "menu"; - const menuCtx: LayerContext = { ...ctx, stack: Object.assign(Object.create(ctx.stack), { isFocused: () => menuFocused }) }; - menu.paint(menuCtx, () => image); + // The menu sits below the paragraph in the left half. While the preview + // has focus the menu keeps its outline-only selection. + const menuTop = y + HOME_MENU_TOP_GAP; + const menuWidth = half - 2 * HOME_MENU_X; + const menuHeight = Math.max(0, height - menuTop - HOME_MENU_BOTTOM_MARGIN); + this.paintCtx = ctx; + try { + this.menu.paint(image, { x: HOME_MENU_X, y: menuTop, width: menuWidth, height: menuHeight }, + ctx.stack.isFocused() && this.focus === "menu"); + } finally { + this.paintCtx = null; + } + this.menu.drawScrollbar(image, HOME_MENU_X + menuWidth + 5, menuTop, menuHeight - 4); this.selectedSlot = Math.min(this.selectedSlot, glanceLayout().slots.length - 1); const preview = this.previewRect(width, height); @@ -173,7 +201,12 @@ class GlanceboardHomeLayer implements Layer { shell.yieldFocusToSidebar(); return; } - await this.currentMenu(ctx, this.menuTop).handleInput(event, ctx); + if (event.type === "click") { + const item = this.menu.selectedItem; + if (item && !isMenuItemDisabled(item)) await item.onSelect(ctx, NO_MENU); + return; + } + await this.menu.handleInput(event); } } @@ -265,32 +298,17 @@ function openSlotPicker(ctx: LayerContext, slotIndex: number): void { openModalMenu(ctx, setting.label, items, Math.max(0, setting.values.indexOf(current))); } -/** Settings: layout, sleep gestures, duration, and slot lines. */ -class GlanceSettingsLayer implements Layer { - private menu: MenuLayer | null = null; - - private currentMenu(ctx: LayerContext): MenuLayer { - if (!this.menu) { - const { width } = ctx.stack.getBaseSize(); - const items: MenuItem[] = [ - enumSettingMenuItem(glanceLayoutSetting), - enumSettingMenuItem(glanceTapDurationSetting), - toggleSettingMenuItem(glanceShowOnLongPressSetting), - toggleSettingMenuItem(glanceShowOnHeadTiltSetting), - toggleSettingMenuItem(glanceShowLinesSetting), - ]; - this.menu = new MenuLayer("Glanceboard settings", items, pageMenuLayout(width)); - } - return this.menu; - } - - paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { - return this.currentMenu(ctx).paint(ctx, paintBelow); - } - - async handleInput(event: InputEvent, ctx: LayerContext): Promise { - await this.currentMenu(ctx).handleInput(event, ctx); - } +/** Settings: layout, sleep gestures, duration, slot lines, and stereo depth. */ +function openGlanceSettings(ctx: LayerContext): void { + const items: MenuItem[] = [ + enumSettingMenuItem(glanceLayoutSetting), + enumSettingMenuItem(glanceTapDurationSetting), + toggleSettingMenuItem(glanceShowOnLongPressSetting), + toggleSettingMenuItem(glanceShowOnHeadTiltSetting), + toggleSettingMenuItem(glanceShowLinesSetting), + enumSettingMenuItem(glanceDepthSetting), + ]; + ctx.stack.push(new MenuLayer("Glanceboard settings", items, pageMenuLayout(ctx.stack.getBaseSize().width))); } /** The live board in the window; any click or double-click returns to the list. */ diff --git a/app/apps/glanceboard/glanceboard-settings.ts b/app/apps/glanceboard/glanceboard-settings.ts index b7233af8..38b4e42f 100644 --- a/app/apps/glanceboard/glanceboard-settings.ts +++ b/app/apps/glanceboard/glanceboard-settings.ts @@ -104,6 +104,31 @@ export const glanceShowOnHeadTiltSetting = new ConfigSettingBoolean({ description: "Tilting your head up while asleep shows the Glanceboard instead of waking the regular UI.", }); +const DEPTH_VALUES = ["-64", "-48", "-32", "-16", "0", "16", "32", "48", "64"] as const; +export type GlanceDepth = (typeof DEPTH_VALUES)[number]; + +/** + * Stereo depth of the board, in the firmware's depth units: the lenses shift + * it by half this many pixels each, in opposite directions. + */ +export const glanceDepthSetting = new ConfigSettingEnum({ + id: "glanceboard-depth", + label: "Depth", + storageKey: "glanceboard.depth", + defaultValue: "0", + values: DEPTH_VALUES, + formatValue: (value) => { + const depth = Number(value); + return depth === 0 ? "0" : depth > 0 ? `+${depth} (nearer)` : `${depth} (farther)`; + }, + description: + "Moves the Glanceboard nearer (positive) or farther away (negative) by shifting it in opposite directions on the two lenses.", +}); + +export function glanceDepth(): number { + return Number(glanceDepthSetting.get()); +} + /** Hairlines between the board's slots. */ export const glanceShowLinesSetting = new ConfigSettingBoolean({ id: "glanceboard-show-lines", diff --git a/app/apps/glanceboard/index.ts b/app/apps/glanceboard/index.ts index 354bef25..65affdfa 100644 --- a/app/apps/glanceboard/index.ts +++ b/app/apps/glanceboard/index.ts @@ -3,6 +3,7 @@ import { GlanceBoard } from "./board"; import { createGlanceboardAppWindow, GLANCEBOARD_SURFACE_ID, GLANCEBOARD_WINDOW_ID } from "./glanceboard-app"; import { glanceboardEnabledSetting, + glanceDepth, glanceLayout, glanceShowOnHeadTiltSetting, glanceShowOnLongPressSetting, @@ -25,6 +26,7 @@ const glanceboardApp: AppDefinition = { tapTimeoutMs: glanceTapTimeoutMs, showOnLongPress: () => glanceShowOnLongPressSetting.get(), showOnHeadTilt: () => glanceShowOnHeadTiltSetting.get(), + depth: glanceDepth, createBoard: (requestRender) => new GlanceBoard(requestRender), }, }; diff --git a/app/apps/glanceboard/widgets/calendar-widget.ts b/app/apps/glanceboard/widgets/calendar-widget.ts index 85f74ff4..4607cd55 100644 --- a/app/apps/glanceboard/widgets/calendar-widget.ts +++ b/app/apps/glanceboard/widgets/calendar-widget.ts @@ -1,8 +1,8 @@ import { type GrayImage, type UiFont } from "../../../graphics/image"; import { truncateText } from "../../../graphics/textwrap"; import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts"; -import { hasCalendarPermission } from "../../../g2/android-permissions"; -import { readUpcomingEvents, type CalendarEvent } from "../../../native/calendar"; +import { hasCalendarPermission } from "../../../native/calendar-permissions"; +import { getCalendarReadState, onCalendarChanged, readUpcomingEvents, type CalendarEvent } from "../../../native/calendar"; import { lineStep } from "../../../ui/metrics"; import { dayHeaderLabel, formatEventTime } from "../../calendar/calendar"; import { type GlanceWidget } from "../widget"; @@ -22,14 +22,19 @@ const HOUR_MS = 60 * MINUTE_MS; */ export class CalendarWidget implements GlanceWidget { private tick: ReturnType | null = null; + private unsubscribe: (() => void) | null = null; start(requestRender: () => void): void { + this.stop(); + this.unsubscribe = onCalendarChanged(requestRender); this.tick = setInterval(requestRender, MINUTE_MS); } stop(): void { if (this.tick !== null) clearInterval(this.tick); this.tick = null; + this.unsubscribe?.(); + this.unsubscribe = null; } paint(image: GrayImage): void { @@ -50,7 +55,9 @@ export class CalendarWidget implements GlanceWidget { const events = readUpcomingEvents().filter((event) => event.endMs > nowMs); if (!events.length) { image.drawText(small, PAD, PAD, "Calendar", 150); - image.drawText(small, PAD, PAD + step + 4, "No upcoming events", 140); + const state = getCalendarReadState(); + image.drawText(small, PAD, PAD + step + 4, state === "loading" ? "Loading calendar..." + : state === "error" ? "Calendar unavailable" : "No upcoming events", 140); return; } diff --git a/app/apps/glanceboard/widgets/compass-widget.ts b/app/apps/glanceboard/widgets/compass-widget.ts index 0a3a6d1d..8e6716f0 100644 --- a/app/apps/glanceboard/widgets/compass-widget.ts +++ b/app/apps/glanceboard/widgets/compass-widget.ts @@ -1,6 +1,6 @@ import { type GrayImage } from "../../../graphics/image"; import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts"; -import { hasLocationPermission } from "../../../g2/android-permissions"; +import { hasLocationPermission } from "../../../native/location-permissions"; import { addCompassListener, COMPASS_CHANGED, setCompassEnabled } from "../../../native/compass"; import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration"; import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "../../compass/compass-rose"; diff --git a/app/apps/glanceboard/widgets/nightscout-widget.ts b/app/apps/glanceboard/widgets/nightscout-widget.ts index d9425d84..14139486 100644 --- a/app/apps/glanceboard/widgets/nightscout-widget.ts +++ b/app/apps/glanceboard/widgets/nightscout-widget.ts @@ -1,4 +1,4 @@ -import { type GrayImage } from "../../../graphics/image"; +import { GrayImage } from "../../../graphics/image"; import { getDefaultLargeFont, getDefaultSmallFont } from "../../../graphics/ui-fonts"; import { truncateText } from "../../../graphics/textwrap"; import { nightscoutBridge, type NightscoutState } from "../../../native/nightscout-bridge"; @@ -91,22 +91,6 @@ export class NightscoutWidget implements GlanceWidget { image.drawText(small, PAD, y, `IOB ${state.iob === null ? "--" : state.iob.toFixed(2)}`, 160); y += step; - // Breached limits only, one per line while they fit, inverted like the - // app's warning fields. Nothing here means nothing is out of range. - const alerts = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs); - const warnings: string[] = []; - if (alerts.cannula) warnings.push(`CAGE ${formatAgeShortFromTimestamp(state.cageTimestampMs, nowMs)}`); - if (alerts.cartridge) warnings.push(`Reservoir ${state.reservoirUnits === null ? "--" : Math.round(state.reservoirUnits)}U`); - if (alerts.battery) warnings.push(`Battery ${state.batteryVoltage === null ? "--" : state.batteryVoltage.toFixed(2)}V`); - if (alerts.loop) warnings.push(`Loop ${formatAgeShortFromTimestamp(state.loopTimestampMs, nowMs)}`); - for (const warning of warnings) { - if (y + small.lineHeight > image.height - 2) break; - const text = truncateText(small, warning, READOUT_WIDTH - 4); - image.fillRect(PAD - 2, y - 1, small.measureText(text) + 4, small.lineHeight + 2, 230); - image.drawText(small, PAD, y, text, 1); - y += step; - } - const graphX = PAD + READOUT_WIDTH + PAD; drawNightscoutGraph( image, @@ -115,5 +99,32 @@ export class NightscoutWidget implements GlanceWidget { nowMs, small, ); + + // Breached limits only, inverted like the app's warning fields. Continue + // at the top of a second column over the graph when the readout is full. + const alerts = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs); + const warnings: string[] = []; + if (alerts.cannula) warnings.push(`CAGE ${formatAgeShortFromTimestamp(state.cageTimestampMs, nowMs)}`); + if (alerts.cartridge) warnings.push(`Reservoir ${state.reservoirUnits === null ? "--" : Math.round(state.reservoirUnits)}U`); + if (alerts.battery) warnings.push(`Battery ${state.batteryVoltage === null ? "--" : state.batteryVoltage.toFixed(2)}V`); + if (alerts.loop) warnings.push(`Loop ${formatAgeShortFromTimestamp(state.loopTimestampMs, nowMs)}`); + let warningX = PAD; + for (const warning of warnings) { + if (y + small.lineHeight > image.height - 2) { + warningX = graphX; + y = PAD; + } + const text = truncateText(small, warning, READOUT_WIDTH - 4); + const width = Math.ceil(small.measureText(text)) + 4; + if (warningX === PAD) { + image.fillRect(warningX - 2, y - 1, width, small.lineHeight + 2, 230); + } else { + // Graph labels are deferred glyphs: a deferred opaque background + // covers those as well as the graph's raster lines. + image.drawImage(new GrayImage(width, small.lineHeight + 2, 230), warningX - 2, y - 1); + } + image.drawText(small, warningX, y, text, 1); + y += step; + } } } diff --git a/app/apps/ios-availability.ts b/app/apps/ios-availability.ts new file mode 100644 index 00000000..c2f062c3 --- /dev/null +++ b/app/apps/ios-availability.ts @@ -0,0 +1,9 @@ +/** Native services still outside the current iOS app-screen milestone. */ +const unavailable: Record = { + music: 'Media-player integration is not available on iOS yet.', + transcribe: 'Voice capture and transcription are not available on iOS yet.', + microphones: 'Glasses microphone capture is not available on iOS yet.', +} +export function iosAppUnavailableReason(appId: string): string | null { + return unavailable[appId] ?? null +} diff --git a/app/apps/launcher/index.ts b/app/apps/launcher/index.ts index 1d3786cc..1c16f4b9 100644 --- a/app/apps/launcher/index.ts +++ b/app/apps/launcher/index.ts @@ -7,6 +7,28 @@ import { renderInstalledEvenHubIcon, } from "../evenhub/installed-apps"; +/** Shared built-in and installed-app listing for both phone hosts. */ +export function launcherEntries(apps: readonly AppDefinition[]) { + return [ + ...apps + .filter((app) => app.showInLauncher !== false) + .map((app) => ({ + appId: app.appId, + label: app.title, + icon: app.icon, + renderIcon: app.renderIcon, + })), + ...getInstalledEvenHubApps().map((app) => ({ + appId: installedEvenHubAppId(app.packageId), + label: app.name, + icon: "package" as const, + renderIcon: (size: number) => renderInstalledEvenHubIcon(app.packageId, size, app), + iconKey: `${app.installedAt}:${app.iconFile ?? ""}`, + uninstallable: true, + })), + ]; +} + const launcherApp: AppDefinition = { appId: "launcher", title: "Apps", @@ -22,24 +44,7 @@ const launcherApp: AppDefinition = { ...ctx.actions, requestRender: () => shell.foregroundWindow()?.requestRender(), }, - apps: () => [ - ...ctx.apps - .filter((app) => app.showInLauncher !== false) - .map((app) => ({ - appId: app.appId, - label: app.title, - icon: app.icon, - renderIcon: app.renderIcon, - })), - ...getInstalledEvenHubApps().map((app) => ({ - appId: installedEvenHubAppId(app.packageId), - label: app.name, - icon: "package" as const, - renderIcon: (size: number) => renderInstalledEvenHubIcon(app.packageId, size, app), - iconKey: `${app.installedAt}:${app.iconFile ?? ""}`, - uninstallable: true, - })), - ], + apps: () => launcherEntries(ctx.apps), launchApp: (appId) => ctx.launchApp(appId), uninstallApp: (appId) => ctx.uninstallApp(appId), submitFrame: (planes, paintMs, frameId) => ctx.submitWindowFrame(LAUNCHER_SURFACE_ID, planes, paintMs, frameId), diff --git a/app/apps/launcher/launcher-app.ts b/app/apps/launcher/launcher-app.ts index 28a137c5..28ba2a90 100644 --- a/app/apps/launcher/launcher-app.ts +++ b/app/apps/launcher/launcher-app.ts @@ -4,11 +4,10 @@ import { truncateText } from "../../graphics/textwrap"; import { GrayImage } from "../../graphics/image"; import { type Plane } from "../../graphics/plane"; import { renderIcon, type IconName } from "../../graphics/icons"; -import { clamp } from "../../util/numeric-util"; -import { InputEvent, isWatchInput } from "../../ui/gestures"; +import { InputEvent } from "../../ui/gestures"; +import { IconGrid, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid"; import { Layer, LayerActions, LayerContext } from "../../ui/layers"; -import { drawSelectionHighlight, MenuLayer, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu"; -import { iconGridMinRowHeight } from "../../ui/metrics"; +import { MenuLayer, type MenuItem } from "../../ui/menu"; import { WINDOW_MENU_LAYOUT } from "../../ui/window-menu"; import { onAnySettingChanged } from "../../ui/dashboard-settings"; import { createInProcessWindow } from "../../ui/shell/in-process-window"; @@ -45,12 +44,7 @@ export type LauncherOptions = { export const LAUNCHER_WINDOW_ID = "launcher"; export const LAUNCHER_SURFACE_ID = "window:launcher"; -const COLS = 5; const GRID_TOP = 6; -const ICON_SIZE = 44; -const LABEL_GAP = 2; - -type LauncherMode = "row" | "item"; /** One cell of the grid: an app, or a folder holding some of the apps. */ type LauncherGridEntry = @@ -58,15 +52,11 @@ type LauncherGridEntry = | { kind: "folder"; label: string; name: string }; /** - * The launcher grid: app and folder icons with labels, arranged in a grid. - * Navigation has two levels so the max number of swipes to any app is halved: - * entering from the sidebar starts in "row" mode (scroll picks a row); a tap - * drops into "item" mode on that row, defaulting to the middle column (scroll - * picks the item); a tap launches an app or opens a folder (the same grid, - * restricted to that folder's apps). Double-click backs out one level (item → - * row, folder → top grid), and from the top level yields to the sidebar. - * That is the ring's scheme; the watch (see handleWatchInput) skips row mode - * and moves one cell at a time in four directions. + * The launcher grid: app and folder icons with labels, in an IconGrid (which + * owns layout, selection and the ring/watch navigation schemes). Opening a + * folder shows the same grid restricted to that folder's apps; backing out + * of a folder returns to the top grid, and from the top grid yields to the + * sidebar. * * The folder grouping lives in the settings store (see launcher-folders.ts) * and is re-read every paint, so assistant folder tools take effect without @@ -78,28 +68,24 @@ class LauncherGridLayer implements Layer { // open folder if there is one, else yields to the sidebar — "left" points // toward the sidebar all the way out. readonly acceptsDirectional = true; - private mode: LauncherMode = "row"; - private selectedRow = 0; - private selectedCol = 1; - private scrollRow = 0; /** Folder whose contents the grid is showing, or null for the top grid. */ private currentFolder: string | null = null; + private readonly grid = new IconGrid({ + items: () => this.entries(), + describe: (entry) => ({ + label: entry.label, + icon: entry.kind === "app" + ? entry.renderIcon?.(ICON_GRID_ICON_SIZE) ?? renderIcon(entry.icon, ICON_GRID_ICON_SIZE) + : renderIcon("folder-filled", ICON_GRID_ICON_SIZE), + }), + onActivate: (entry) => this.openEntry(entry), + onBack: () => this.back(), + }); constructor(private readonly options: LauncherOptions) {} - /** - * Focus arriving from the watch goes straight to item selection: the watch - * has left/right swipes, so it never needs row mode (see handleWatchInput), - * and starting there would paint a row band its scheme can't produce. Any - * other source keeps the two-level scheme and enters in row mode. - */ onFocus(lastInput: InputEvent | null): void { - if (lastInput && isWatchInput(lastInput)) { - this.mode = "item"; - this.selectedCol = clamp(this.selectedCol, 0, this.itemsInRow(this.entries().length, this.selectedRow) - 1); - } else { - this.mode = "row"; - } + this.grid.onFocus(lastInput); } /** @@ -150,22 +136,38 @@ class LauncherGridLayer implements Layer { return entries.sort((a, b) => a.label.localeCompare(b.label)); } - private rowCount(entryCount: number): number { - return Math.max(1, Math.ceil(entryCount / COLS)); - } - - private itemsInRow(entryCount: number, row: number): number { - return Math.max(0, Math.min(COLS, entryCount - row * COLS)); - } - /** Leave the current folder, putting the selection back on its cell. */ private exitFolder(): void { const folder = this.currentFolder; this.currentFolder = null; - this.mode = "row"; + this.grid.resetSelection(); const index = this.entries().findIndex((entry) => entry.kind === "folder" && entry.name === folder); - this.selectedRow = index >= 0 ? Math.floor(index / COLS) : 0; - this.selectedCol = index >= 0 ? index % COLS : 0; + if (index >= 0) this.grid.selectIndex(index); + } + + /** Enter a folder or launch an app; true when that left the grid (a launch). */ + private async openEntry(entry: LauncherGridEntry): Promise { + if (entry.kind === "folder") { + this.currentFolder = entry.name; + this.grid.resetSelection(); + return false; + } + // Return to the top grid before launching, so the launcher never + // shows (even briefly) stale folder contents when re-entered. + if (this.currentFolder !== null) this.exitFolder(); + this.grid.enterRowMode(); + await this.options.launchApp(entry.appId); + return true; + } + + /** Leave the open folder, else yield to the sidebar; true when that left the grid. */ + private back(): boolean { + if (this.currentFolder !== null) { + this.exitFolder(); + return false; + } + shell.yieldFocusToSidebar(); + return true; } /** @@ -174,8 +176,7 @@ class LauncherGridLayer implements Layer { * so anywhere else the menu falls back to just the defaults. */ menuItems(): MenuItem[] { - if (this.mode !== "item") return []; - const entry = this.entries()[this.selectedRow * COLS + this.selectedCol]; + const entry = this.grid.selectedItem; if (!entry || entry.kind !== "app") return []; return [ { @@ -202,7 +203,7 @@ class LauncherGridLayer implements Layer { confirmCtx.stack.pop(); setAppFolder(entry.appId, null); await this.options.uninstallApp(entry.appId); - this.mode = "row"; + this.grid.enterRowMode(); }, }, { label: "Cancel", onSelect: (confirmCtx) => confirmCtx.stack.pop() }, @@ -257,246 +258,23 @@ class LauncherGridLayer implements Layer { const font = getDefaultSmallFont(); const { width, height } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - const focused = ctx.stack.isFocused(); - const entries = this.entries(); - const rows = this.rowCount(entries.length); - this.selectedRow = clamp(this.selectedRow, 0, rows - 1); - this.selectedCol = clamp( - this.selectedCol, - 0, - Math.max(0, this.itemsInRow(entries.length, this.selectedRow) - 1), - ); - // The folder-name header band scales with the font. const gridTop = this.currentFolder !== null ? GRID_TOP + font.lineHeight + 4 : GRID_TOP; if (this.currentFolder !== null) { image.drawText(font, 8, GRID_TOP - 2, truncateText(font, this.currentFolder, width - 16), 160); } const gridBottom = height - 4; - // Rows are exactly as tall as their content (icon + label + breathing - // room), so row height tracks the font instead of stretching to fill the - // viewport. Leftover space below the last full row shows the top of the - // next row when there are more apps to scroll to. - const rowH = iconGridMinRowHeight(font, ICON_SIZE, LABEL_GAP); - const fullRows = Math.max(1, Math.floor((gridBottom - gridTop) / rowH)); - const colW = width / COLS; - - // Scroll to keep the selected row among the fully-visible rows. - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, fullRows, rows); - - const rowY = (row: number) => gridTop + (row - this.scrollRow) * rowH; - - // Selection highlight: a row band, or a single cell in item mode. While - // defocused with the watch as the last-used source, the cell outline is - // shown even in row mode: watch focus enters item mode directly (see - // onFocus), so the outline previews the cell a click would land on - // rather than a row band the watch scheme never shows. - const cellHighlight = this.mode === "item" || (!focused && shell.lastInputWasWatch()); - const selY = rowY(this.selectedRow); - if (cellHighlight) { - drawSelectionHighlight(image, this.selectedCol * colW + 6, selY + 2, colW - 12, rowH - 4, focused, 6); - } else { - drawSelectionHighlight(image, 4, selY + 2, width - 8, rowH - 4, focused, 6); - } - - for (let index = 0; index < entries.length; index++) { - const entry = entries[index]!; - const row = Math.floor(index / COLS); - if (row < this.scrollRow) continue; - const blockTop = rowY(row) + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2); - if (blockTop >= gridBottom) break; // fully below the grid - const centerX = (index % COLS) * colW + colW / 2; - const icon = entry.kind === "app" - ? entry.renderIcon?.(ICON_SIZE) ?? renderIcon(entry.icon, ICON_SIZE) - : renderIcon("folder-filled", ICON_SIZE); - if (icon) { - // Clip the icon at the grid bottom so a peeking row shows only its top. - // A fully visible icon goes through the deferred-image path (texture - // cacheable); the clipped peeking row falls back to a raster blit, - // since cached draws are whole-image only. - // Icons narrower/shorter than ICON_SIZE (a non-square logo scaled to - // fit) are centered in the square the row reserves for them. - const iconX = Math.round(centerX - icon.width / 2); - const iconY = Math.round(blockTop + Math.max(0, (ICON_SIZE - icon.height) / 2)); - const clipHeight = Math.min(icon.height, Math.floor(gridBottom - iconY)); - if (clipHeight >= icon.height) { - image.drawImage(icon, iconX, iconY); - } else if (clipHeight > 0) { - image.bitBlt(icon, iconX, iconY, { height: clipHeight, transparentZero: true }); - } - } - const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP); - if (labelY + font.lineHeight <= gridBottom) { - const label = truncateText(font, entry.label, colW - 8); - image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, 210); - } - } - + this.grid.paint(image, { x: 0, y: gridTop, width, height: gridBottom - gridTop }, ctx.stack.isFocused()); return image; } async handleInput(event: InputEvent, ctx: LayerContext): Promise { - if (isWatchInput(event)) { - await this.handleWatchInput(event, ctx); - return; - } - const entries = this.entries(); - const rows = this.rowCount(entries.length); - switch (event.type) { - case "scroll-up": - case "scroll-down": { - const delta = event.type === "scroll-down" ? 1 : -1; - if (this.mode === "row") { - this.selectedRow = clamp(this.selectedRow + delta, 0, rows - 1); - } else { - // Item selection traverses the grid linearly: past a row's edge it - // continues onto the adjacent row (stopping at the grid's ends). - const itemCount = this.itemsInRow(entries.length, this.selectedRow); - const next = clamp(this.selectedCol, 0, itemCount - 1) + delta; - if (next >= 0 && next < itemCount) { - this.selectedCol = next; - } else if (next < 0 && this.selectedRow > 0) { - this.selectedRow--; - this.selectedCol = this.itemsInRow(entries.length, this.selectedRow) - 1; - } else if (next >= itemCount && this.selectedRow < rows - 1) { - this.selectedRow++; - this.selectedCol = 0; - } - } - return; - } - case "click": { - if (this.mode === "row") { - this.mode = "item"; - // Default to the middle column (clamped to the row's item count). - this.selectedCol = Math.min( - Math.floor(COLS / 2), - this.itemsInRow(entries.length, this.selectedRow) - 1, - ); - } else { - await this.openSelected(entries); - } - return; - } - case "double-click": - if (this.mode === "item") { - this.mode = "row"; - } else if (this.currentFolder !== null) { - this.exitFolder(); - } else { - shell.yieldFocusToSidebar(); - } - return; - default: - return; - } + await this.grid.handleInput(event, ctx); } - /** - * The watch's scheme: there is no row mode to enter first. The selection is - * always one cell; up/down/left/right (and the crown) move it spatially, - * select opens it, back leaves the folder or the grid, and stepping left - * past the first column leaves for the sidebar, which is where "left" goes. - * Row mode is restored on the way out so a ring user finds the grid as - * they left it. - */ - private async handleWatchInput(event: InputEvent, ctx: LayerContext): Promise { - const entries = this.entries(); - const rows = this.rowCount(entries.length); - if (this.mode === "row") { - this.mode = "item"; - this.selectedCol = clamp(this.selectedCol, 0, this.itemsInRow(entries.length, this.selectedRow) - 1); - } - const leave = () => { - this.mode = "row"; - shell.yieldFocusToSidebar(); - }; - switch (event.type) { - case "swipe-up": - case "swipe-down": - case "scroll-up": - case "scroll-down": { - const delta = event.type === "swipe-down" || event.type === "scroll-down" ? 1 : -1; - this.selectedRow = clamp(this.selectedRow + delta, 0, rows - 1); - // Keep the column; a shorter last row clamps it. - this.selectedCol = clamp(this.selectedCol, 0, this.itemsInRow(entries.length, this.selectedRow) - 1); - return; - } - case "swipe-right": - this.selectedCol = clamp(this.selectedCol + 1, 0, this.itemsInRow(entries.length, this.selectedRow) - 1); - return; - case "swipe-left": - if (this.selectedCol > 0) { - this.selectedCol--; - } else if (this.currentFolder !== null) { - this.exitFolder(); - this.mode = "item"; - } else { - leave(); - } - return; - case "click": - await this.openSelected(entries, true); - return; - case "double-click": - if (this.currentFolder !== null) { - this.exitFolder(); - this.mode = "item"; - } else { - leave(); - } - return; - default: - return; - } - } - - /** - * A touch on the phone's mirror: the cell under it becomes the selection - * and opens (the same as a watch select on it). Uses the geometry paint - * lays the grid out with, so it lands on what the mirror showed. - */ + /** A touch on the phone's mirror opens the cell under it. */ async hitTest(x: number, y: number, ctx: LayerContext): Promise { - const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); - const entries = this.entries(); - const gridTop = this.currentFolder !== null ? GRID_TOP + font.lineHeight + 4 : GRID_TOP; - const gridBottom = height - 4; - if (y < gridTop || y >= gridBottom || x < 0 || x >= width) return false; - const rowH = iconGridMinRowHeight(font, ICON_SIZE, LABEL_GAP); - const colW = width / COLS; - const row = this.scrollRow + Math.floor((y - gridTop) / rowH); - const col = Math.floor(x / colW); - if (row < 0 || row >= this.rowCount(entries.length)) return false; - if (col < 0 || col >= this.itemsInRow(entries.length, row)) return false; - this.mode = "item"; - this.selectedRow = row; - this.selectedCol = col; - await this.openSelected(entries); - return true; - } - - /** - * Open the selected cell: enter a folder, or launch the app. A folder - * entered by watch input stays in single-item selection (the watch scheme - * has no row mode), starting at the folder's first cell. - */ - private async openSelected(entries: readonly LauncherGridEntry[], watch = false): Promise { - const entry = entries[this.selectedRow * COLS + this.selectedCol]; - if (!entry) return; - if (entry.kind === "folder") { - this.currentFolder = entry.name; - this.mode = watch ? "item" : "row"; - this.selectedRow = 0; - this.selectedCol = watch ? 0 : this.selectedCol; - this.scrollRow = 0; - } else { - // Return to the top grid before launching, so the launcher never - // shows (even briefly) stale folder contents when re-entered. - if (this.currentFolder !== null) this.exitFolder(); - this.mode = "row"; - await this.options.launchApp(entry.appId); - } + return await this.grid.hitTest(x, y, ctx); } } diff --git a/app/apps/minesweeper/minesweeper-app.worker.ts b/app/apps/minesweeper/minesweeper-app.worker.ts index 2e0b5c5f..ebc22c9f 100644 --- a/app/apps/minesweeper/minesweeper-app.worker.ts +++ b/app/apps/minesweeper/minesweeper-app.worker.ts @@ -18,6 +18,7 @@ * launches. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -25,6 +26,7 @@ import { getFont } from "../../graphics/bdffont"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; import * as frameTimings from "../../native/frame-timings"; +import { playWorkerBuzzerSequence } from "../../native/worker-buzzer"; import { getActiveDisplay } from "../../native/active-display"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; @@ -43,7 +45,6 @@ import { import { clamp } from "../../util/numeric-util"; declare const global: any; -declare const com: any; const largeFont = getFont("terminus32"); const mediumFont = getFont("terminus24"); @@ -152,6 +153,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": { const window: MinesweeperWindow = { windowId: message.windowId, @@ -278,14 +285,12 @@ function saveDifficultyIndex(window: MinesweeperWindow): void { /** * Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the - * steps on its own timer, and the Java call is safe from the worker thread. + * steps on its own timer; the platform bridge routes it from the worker. */ function playSfx(window: MinesweeperWindow, steps: Step[]): void { if (!window.soundOn || steps.length === 0) return; try { - const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); - if (!communicator) return; - communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer); + playWorkerBuzzerSequence(buildSoundSequencePayload(steps)); } catch (error) { console.warn(`minesweeper sfx failed: ${error}`); } diff --git a/app/apps/music/media-browse.ts b/app/apps/music/media-browse.ts index 51e50d9a..3876c67e 100644 --- a/app/apps/music/media-browse.ts +++ b/app/apps/music/media-browse.ts @@ -1,15 +1,9 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage, type UiFont } from "../../graphics/image"; -import { clamp } from "../../util/numeric-util"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures"; -import { - MenuLayer, - drawListScrollbar, - drawSelectionHighlight, - drawSubmenuIndicator, - scrollToKeepSelectionVisible, -} from "../../ui/menu"; -import { lineStep, tightRowHeight } from "../../ui/metrics"; +import { MenuLayer, drawSubmenuIndicator } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics"; import { mediaBrowserBridge, type MediaBrowseItem, @@ -22,6 +16,8 @@ function headerHeight(font: UiFont): number { return 8 + lineStep(font) + font.lineHeight; } const LIST_X = 20; +/** The selection box extends this far left and right of the row text. */ +const LIST_HIGHLIGHT_PAD = 6; export type MediaBrowseOptions = { app: MediaBrowserApp; @@ -37,8 +33,8 @@ type BrowseLevel = { /** null while the listing is loading. */ items: MediaBrowseItem[] | null; error: string; - selectedIndex: number; - scrollRow: number; + /** Holds the loaded items; keeps this level's selection and scroll while a child level is open. */ + menu: Menu; }; type BrowsePhase = "idle" | "connecting" | "ready" | "failed"; @@ -97,50 +93,16 @@ export class MediaBrowseLayer implements Layer { return image; } - level.selectedIndex = clamp(level.selectedIndex, 0, items.length - 1); - const listHeight = height - headerHeight(font) - font.lineHeight - 10; - const rowH = tightRowHeight(font); - const visibleRows = Math.max(1, (listHeight / rowH) | 0); - level.scrollRow = scrollToKeepSelectionVisible(level.scrollRow, level.selectedIndex, visibleRows, items.length); - - const rowWidth = width - 2 * LIST_X; - const lastVisible = Math.min(items.length, level.scrollRow + visibleRows); - for (let index = level.scrollRow; index < lastVisible; index++) { - const item = items[index]!; - const y = headerHeight(font) + (index - level.scrollRow) * rowH; - const selected = index === level.selectedIndex; - const highlightX = LIST_X - 6; - const highlightY = y - 1; - const highlightWidth = rowWidth + 12; - const highlightHeight = rowH - 1; - if (selected) { - drawSelectionHighlight(image, highlightX, highlightY, highlightWidth, highlightHeight, ctx.stack.isFocused(), 4); - } - const value = selected ? 255 : 200; - const title = truncateRight(font, item.title || "(untitled)", rowWidth - 16); - image.drawText(font, LIST_X, y + 1, title, value); - if (item.subtitle) { - const titleWidth = font.measureText(title); - const subtitleWidth = font.measureText(item.subtitle); - if (titleWidth + subtitleWidth + 24 <= rowWidth - 16) { - image.drawText(font, LIST_X + rowWidth - 16 - subtitleWidth, y + 1, item.subtitle, 110); - } - } - if (item.browsable) { - drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, selected ? value : 110); - } - } - if (items.length > visibleRows) { - drawListScrollbar( - image, - width - 12, - headerHeight(font), - visibleRows * rowH - 4, - level.scrollRow, - visibleRows, - items.length, - ); - } + const listTop = headerHeight(font); + const listHeight = height - listTop - font.lineHeight - 10; + const visibleRows = Math.max(1, (listHeight / tightRowHeight(font)) | 0); + // Row boxes start one pixel above the header's bottom edge. + level.menu.paint( + image, + { x: LIST_X - LIST_HIGHLIGHT_PAD, y: listTop - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: listHeight }, + ctx.stack.isFocused(), + ); + level.menu.drawScrollbar(image, width - 12, listTop, visibleRows * tightRowHeight(font) - 4); image.drawText( font, @@ -175,13 +137,11 @@ export class MediaBrowseLayer implements Layer { if (!level || !items || !items.length) return; switch (event.type) { case "scroll-up": - level.selectedIndex = Math.max(0, level.selectedIndex - 1); - return; case "scroll-down": - level.selectedIndex = Math.min(items.length - 1, level.selectedIndex + 1); + await level.menu.handleInput(event); return; case "click": { - const item = items[clamp(level.selectedIndex, 0, items.length - 1)]; + const item = level.menu.selectedItem; if (!item) return; if (item.browsable && item.playable) { ctx.stack.push( @@ -233,18 +193,46 @@ export class MediaBrowseLayer implements Layer { } private descendInto(ctx: LayerContext, item: MediaBrowseItem): void { - const level: BrowseLevel = { - parentId: item.mediaId, - title: item.title || "(untitled)", - items: null, - error: "", - selectedIndex: 0, - scrollRow: 0, - }; + const level = this.createLevel(item.mediaId, item.title || "(untitled)"); this.levels.push(level); this.loadLevel(ctx, level); } + private createLevel(parentId: string, title: string): BrowseLevel { + return { + parentId, + title, + items: null, + error: "", + menu: new Menu({ + wrap: false, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawItem(args), + }), + }; + } + + private drawItem({ image, item, x, y, width, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const value = selected ? 255 : 200; + const textWidth = width - 2 * LIST_HIGHLIGHT_PAD - 16; + const textY = centeredTextY(font, y, height); + const title = truncateRight(font, item.title || "(untitled)", textWidth); + image.drawText(font, x + LIST_HIGHLIGHT_PAD, textY, title, value); + if (item.subtitle) { + const titleWidth = font.measureText(title); + const subtitleWidth = font.measureText(item.subtitle); + if (titleWidth + subtitleWidth + 24 <= textWidth) { + image.drawText(font, x + LIST_HIGHLIGHT_PAD + textWidth - subtitleWidth, textY, item.subtitle, 110); + } + } + if (item.browsable) { + drawSubmenuIndicator(image, font, x, y, width, height, selected ? value : 110); + } + } + private startConnect(ctx: LayerContext): void { this.phase = "connecting"; mediaBrowserBridge @@ -252,14 +240,7 @@ export class MediaBrowseLayer implements Layer { .then((rootId) => { if (this.removed) return; this.phase = "ready"; - const root: BrowseLevel = { - parentId: rootId, - title: "Library", - items: null, - error: "", - selectedIndex: 0, - scrollRow: 0, - }; + const root = this.createLevel(rootId, "Library"); this.levels = [root]; this.loadLevel(ctx, root); ctx.actions.requestRender(); @@ -278,6 +259,7 @@ export class MediaBrowseLayer implements Layer { .then((items) => { if (this.removed) return; level.items = items; + level.menu.setItems(items, 0); ctx.actions.requestRender(); }) .catch((error) => { diff --git a/app/apps/music/music-app.ts b/app/apps/music/music-app.ts index 40eb5600..7c08867a 100644 --- a/app/apps/music/music-app.ts +++ b/app/apps/music/music-app.ts @@ -3,8 +3,9 @@ import { GrayImage } from "../../graphics/image"; import { wrapText, truncateText } from "../../graphics/textwrap"; import { clamp } from "../../util/numeric-util"; import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL_DOWN, GESTURE_SCROLL_UP, type InputEvent, isDirectionalInput } from "../../ui/gestures"; -import { MenuLayer, drawSelectionHighlight, drawSubmenuIndicator } from "../../ui/menu"; -import { lineStep, tightRowHeight } from "../../ui/metrics"; +import { MenuLayer, drawSubmenuIndicator } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics"; import { mediaControllerBridge, type MediaControllerState, type MediaQueueItem } from "../../native/media-controller"; import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser"; import { MediaBrowseLayer } from "./media-browse"; @@ -28,6 +29,8 @@ const ACTION_X = 22; const ACTION_WIDTH = 154; const COLUMN_DIVIDER_X = 190; const QUEUE_X = 204; +/** Horizontal inset of row text from its selection box. */ +const LIST_TEXT_INSET = 6; const PLAYLIST_ACTION_INDEX = 1; type MusicAction = @@ -49,9 +52,20 @@ class MusicAppLayer implements Layer { // Watch swipes skip tracks: right = next, left = previous. readonly acceptsDirectional = true; private focusColumn: FocusColumn = "actions"; - private selectedActionIndex = 0; - private selectedQueueIndex = 0; - private queueScrollRow = 0; + private readonly actionsMenu = new Menu({ + wrap: true, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawAction(args), + }); + private readonly queueMenu = new Menu({ + wrap: false, + rowGap: 1, + highlight: { radius: 4 }, + getHeight: () => tightRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawQueueItem(args), + }); private art: GrayImage | null = null; private artKey = ""; @@ -124,83 +138,49 @@ class MusicAppLayer implements Layer { const actions = this.buildActions(media, queue); this.reconcileSelection(actions, queue); const listHeight = height - LIST_TOP - LIST_BOTTOM_MARGIN; - const rowH = tightRowHeight(font); - const visibleRows = Math.max(1, (listHeight / rowH) | 0); - if (this.selectedQueueIndex < this.queueScrollRow) { - this.queueScrollRow = this.selectedQueueIndex; - } else if (this.selectedQueueIndex >= this.queueScrollRow + visibleRows) { - this.queueScrollRow = this.selectedQueueIndex - visibleRows + 1; - } - this.queueScrollRow = clamp(this.queueScrollRow, 0, Math.max(0, queue.length - visibleRows)); - - for (let index = 0; index < actions.length; index++) { - const action = actions[index]!; - const y = LIST_TOP + index * rowH; - const selected = index === this.selectedActionIndex; - const highlightX = ACTION_X - 6; - const highlightY = y - 1; - const highlightWidth = ACTION_WIDTH + 8; - const highlightHeight = rowH - 1; - if (selected) { - drawSelectionHighlight( - image, - highlightX, - highlightY, - highlightWidth, - highlightHeight, - ctx.stack.isFocused() && this.focusColumn === "actions", - 4, - ); - } - const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200; - image.drawText(font, ACTION_X, y + 1, truncateText(font, action.label, ACTION_WIDTH - 16), value); - if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) { - drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, value); - } - } + const focused = ctx.stack.isFocused(); + // Row boxes start one pixel above LIST_TOP, level with the column divider. + this.actionsMenu.paint( + image, + { x: ACTION_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: ACTION_WIDTH + 2 * LIST_TEXT_INSET - 4, height: listHeight }, + focused && this.focusColumn === "actions", + ); image.drawLine(COLUMN_DIVIDER_X, LIST_TOP - 3, COLUMN_DIVIDER_X, height - LIST_BOTTOM_MARGIN - 3, 45); if (!queue.length) { image.drawText(font, QUEUE_X, LIST_TOP + 1, "Playlist unavailable", 90); } else { const queueWidth = width - QUEUE_X - 20; - const lastVisible = Math.min(queue.length, this.queueScrollRow + visibleRows); - for (let index = this.queueScrollRow; index < lastVisible; index++) { - const item = queue[index]!; - const y = LIST_TOP + (index - this.queueScrollRow) * rowH; - const selected = index === this.selectedQueueIndex; - if (selected) { - drawSelectionHighlight( - image, - QUEUE_X - 6, - y - 1, - queueWidth + 8, - rowH - 1, - ctx.stack.isFocused() && this.focusColumn === "playlist", - 4, - ); - } - const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`; - image.drawText(font, QUEUE_X, y + 1, truncateText(font, label, queueWidth - 4), selected ? 255 : 200); - } - if (queue.length > visibleRows) { - const trackHeight = visibleRows * rowH - 4; - const trackX = width - 12; - image.fillRect(trackX, LIST_TOP, 3, trackHeight, 30); - const thumbHeight = Math.max(8, (trackHeight * visibleRows / queue.length) | 0); - const maxScrollRow = queue.length - visibleRows; - const thumbY = LIST_TOP + (((trackHeight - thumbHeight) * this.queueScrollRow / maxScrollRow) | 0); - image.fillRect(trackX, thumbY, 3, thumbHeight, 120); - } + this.queueMenu.paint( + image, + { x: QUEUE_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: queueWidth + 2 * LIST_TEXT_INSET - 4, height: listHeight }, + focused && this.focusColumn === "playlist", + ); + this.queueMenu.drawScrollbar(image, width - 12, LIST_TOP, listHeight - 4); } return image; } + private drawAction({ image, item: action, x, y, width, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200; + image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, action.label, ACTION_WIDTH - 16), value); + if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) { + drawSubmenuIndicator(image, font, x, y, width, height, value); + } + } + + private drawQueueItem({ image, item, x, y, width, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`; + image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, label, width - 2 * LIST_TEXT_INSET), selected ? 255 : 200); + } + async handleInput(event: InputEvent, ctx: LayerContext): Promise { if (event.type === "double-click" && this.focusColumn === "playlist") { this.focusColumn = "actions"; - this.selectedActionIndex = PLAYLIST_ACTION_INDEX; + this.actionsMenu.select(PLAYLIST_ACTION_INDEX); return; } const media = mediaControllerBridge.snapshot(); @@ -237,31 +217,20 @@ class MusicAppLayer implements Layer { case "swipe-down": return this.handleInput({ type: "scroll-down", timestampMs: event.timestampMs }, ctx); case "scroll-up": - if (this.focusColumn === "playlist") { - this.selectedQueueIndex = Math.max(0, this.selectedQueueIndex - 1); - } else { - this.selectedActionIndex = (this.selectedActionIndex + actions.length - 1) % actions.length; - } - return; case "scroll-down": - if (this.focusColumn === "playlist") { - this.selectedQueueIndex = Math.min(Math.max(0, queue.length - 1), this.selectedQueueIndex + 1); - } else { - this.selectedActionIndex = (this.selectedActionIndex + 1) % actions.length; - } + await (this.focusColumn === "playlist" ? this.queueMenu : this.actionsMenu).handleInput(event); return; case "click": { if (this.focusColumn === "playlist") { - const item = queue[this.selectedQueueIndex]; + const item = this.queueMenu.selectedItem; if (item) { await mediaControllerBridge.skipToQueueItem(item.id); // The player rebuilds the queue with the chosen track at index 0. - this.selectedQueueIndex = 0; - this.queueScrollRow = 0; + this.queueMenu.select(0); } return; } - const action = actions[this.selectedActionIndex]; + const action = this.actionsMenu.selectedItem; if (!action || !action.enabled) return; if (action.kind === "play-pause") await mediaControllerBridge.playPause(); else if (action.kind === "previous") await mediaControllerBridge.skipPrevious(); @@ -271,7 +240,7 @@ class MusicAppLayer implements Layer { ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent())); } else if (action.kind === "playlist") { const activeIndex = queue.findIndex((item) => item.active); - if (activeIndex >= 0) this.selectedQueueIndex = activeIndex; + if (activeIndex >= 0) this.queueMenu.select(activeIndex); this.focusColumn = "playlist"; } return; @@ -332,12 +301,13 @@ class MusicAppLayer implements Layer { ]; } + /** Hand the menus the freshly built rows; each keeps its selection index, clamped. */ private reconcileSelection(actions: MusicAction[], queue: MediaQueueItem[]): void { - this.selectedActionIndex = clamp(this.selectedActionIndex, 0, actions.length - 1); - this.selectedQueueIndex = clamp(this.selectedQueueIndex, 0, Math.max(0, queue.length - 1)); + this.actionsMenu.setItems(actions); + this.queueMenu.setItems(queue); if (!queue.length && this.focusColumn === "playlist") { this.focusColumn = "actions"; - this.selectedActionIndex = PLAYLIST_ACTION_INDEX; + this.actionsMenu.select(PLAYLIST_ACTION_INDEX); } } diff --git a/app/apps/music/music-settings.ts b/app/apps/music/music-settings.ts index 52dac95d..c4c45207 100644 --- a/app/apps/music/music-settings.ts +++ b/app/apps/music/music-settings.ts @@ -10,39 +10,45 @@ import { drawToggleMenuItem, MenuLayer, type MenuItem } from "../../ui/menu"; /** Discovered apps stay visible even when ignored or no longer publishing a session. */ export class MusicSettingsLayer implements Layer { private menu: MenuLayer | null = null; - private signature = ""; - private selectedPackage = ""; + private menuWidth = 0; + /** The package behind each current row, parallel to the menu's items. */ + private packages: string[] = []; constructor() { // Refresh installed browser services, remembering ignored services too. mediaBrowserBridge.listBrowsableApps(true); } + /** Refresh the rows from the remembered apps, keeping the selection on the same package. */ private currentMenu(ctx: LayerContext): MenuLayer { const apps = readMediaApps().sort((a, b) => a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName)); + const items: MenuItem[] = apps.map((app) => ({ + label: app.appName, + onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)), + render: ({ image, x, y, width, selected }) => { + const font = getDefaultSmallFont(); + const duplicateName = apps.some((other) => other !== app && other.appName === app.appName); + const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName; + drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48), + isMediaAppEnabled(app.packageName), selected); + }, + })); + if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} }); + const selectedPackage = this.menu?.selectedIndex != null ? this.packages[this.menu.selectedIndex] : undefined; + const index = apps.findIndex((app) => app.packageName === selectedPackage); + this.packages = apps.map((app) => app.packageName); + const width = ctx.stack.getBaseSize().width; - const signature = JSON.stringify([width, apps.map((app) => [app.packageName, app.appName])]); - if (!this.menu || signature !== this.signature) { - this.signature = signature; - const items: MenuItem[] = apps.map((app) => ({ - label: app.appName, - onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)), - render: ({ image, x, y, width, selected }) => { - if (selected) this.selectedPackage = app.packageName; - const font = getDefaultSmallFont(); - const duplicateName = apps.some((other) => other !== app && other.appName === app.appName); - const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName; - drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48), - isMediaAppEnabled(app.packageName), selected); - }, - })); - if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} }); + if (!this.menu || width !== this.menuWidth) { + this.menuWidth = width; this.menu = new MenuLayer("Music settings", items, { x: 8, y: 8, width: width - 16, opaque: true, showBorder: false, footer: "On = enabled Off = ignored", - }).selectItem(Math.max(0, apps.findIndex((app) => app.packageName === this.selectedPackage))); + }).selectItem(Math.max(0, index)); + } else { + this.menu.setItems(items, index >= 0 ? index : undefined); } return this.menu; } diff --git a/app/apps/navigate/index.ts b/app/apps/navigate/index.ts index a6f2c0cf..6f02096d 100644 --- a/app/apps/navigate/index.ts +++ b/app/apps/navigate/index.ts @@ -1,4 +1,4 @@ -import { ensureFineLocationPermission } from "../../g2/android-permissions"; +import { ensureFineLocationPermission } from "../../native/location-permissions"; import { launchWorkerAppWindow, type AppDefinition } from "../app-definition"; const navigateApp: AppDefinition = { diff --git a/app/apps/navigate/navigate-app.worker.ts b/app/apps/navigate/navigate-app.worker.ts index 3eff4595..14cdd830 100644 --- a/app/apps/navigate/navigate-app.worker.ts +++ b/app/apps/navigate/navigate-app.worker.ts @@ -7,6 +7,7 @@ * guidance text (right pane) updates per GPS fix and ships as small deltas. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -15,14 +16,12 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; import { getActiveDisplay } from "../../native/active-display"; import { - drawListScrollbar, drawRightValueMenuItem, - drawSelectionHighlight, drawSubmenuIndicator, drawToggleMenuItem, - scrollToKeepSelectionVisible, type MenuItem, } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics"; import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu"; import type { LayerContext } from "../../ui/layers"; @@ -64,7 +63,7 @@ import { GESTURE_SHORT_THEN_LONG_PRESS, type InputEvent, } from "../../ui/gestures"; -import { LocationTracker, type TrackedLocation } from "../../native/location-tracker"; +import { LocationTracker, type TrackedLocation } from "./navigation-sensors"; import { fetchRoute, fetchStaticMapGray, @@ -76,8 +75,8 @@ import { type RouteProfile, type StaticMapCamera, } from "../../native/mapbox"; -import { addCompassListener, COMPASS_CHANGED, setCompassEnabled, type CompassEvent } from "../../native/compass"; -import { magneticDeclinationDegrees } from "../../native/geomagnetic"; +import { addCompassListener, COMPASS_CHANGED, setCompassEnabled, type CompassEvent } from "./navigation-sensors"; +import { magneticDeclinationDegrees, handleNavigationSensorEvent } from "./navigation-sensors"; import { calibrateHeading, normalizeHeading } from "../compass/calibration"; import { bearingDegrees, haversineMeters, RouteFollower, type RouteProgress } from "./route-follower"; import { drawManeuverGlyph } from "./maneuver-icons"; @@ -114,7 +113,7 @@ const largeFont = getFont("terminus32"); const mediumFont = getFont("terminus24"); const smallFont = getDefaultSmallFont(); -type NavPhase = "idle" | "acquiring" | "routing" | "navigating" | "arrived"; +type NavPhase = "idle" | "acquiring" | "routing" | "navigating" | "map" | "arrived"; type MapMode = "follow" | "overview"; type NavWindow = { @@ -184,6 +183,8 @@ let lastRerouteAtMs = 0; let rerouteInFlight = false; /** Bumped on every new route/mode so stale map fetches can be discarded. */ let routeGeneration = 0; +/** Invalidates GPS/geocoding/routing work when the route is replaced or closed. */ +let navigationGeneration = 0; /** * Where to go: free text to geocode (optionally shown under a saved @@ -194,17 +195,16 @@ type NavTarget = | { kind: "place"; name: string; place: string; longitude: number; latitude: number }; /** - * An entry on the idle page's list: a destination, or (while no Mapbox - * token is set) one of the setup actions offered in its place. + * An entry on the idle page's list: the map, a destination, or (while no + * Mapbox token is set) one of the setup actions offered in its place. */ type IdleEntry = | { kind: "saved"; destination: SavedDestination } | { kind: "recent"; destination: RecentDestination } | { kind: "action"; label: string; detail: string; run: () => void }; -/** Idle-page list selection (index into idleEntries()). */ -let idleSelection = 0; -let idleScrollRow = 0; +/** The idle page's list; created on first use (idleList) so module load stays free of paint dependencies. */ +let idleMenu: Menu | null = null; /** * In-progress destination edit: the phone text editor is open on `setting` @@ -239,7 +239,6 @@ let compassActive = false; let unsubscribeCompass: (() => void) | null = null; /** Wearer's true heading (degrees clockwise from north) as last drawn, or null before compass data arrives. */ let headHeadingDeg: number | null = null; -let declinationCache: { latitude: number; longitude: number; degrees: number | null } | null = null; let tickTimer: ReturnType | null = null; let firstFixWaiters: Array<(fix: TrackedLocation) => void> = []; @@ -248,7 +247,7 @@ const tracker = new LocationTracker({ onLocation: (fix) => handleFix(fix), onError: (message) => { statusMessage = message; - if (phase === "acquiring") phase = "idle"; + if (phase === "acquiring") stopNavigation(message); render(); }, }); @@ -274,6 +273,15 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; + case "navigation-sensors": + handleNavigationSensorEvent(message.event); + break; case "open-window": window = { windowId: message.windowId, @@ -295,6 +303,10 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { window.viewportHeight = message.viewport.height; window.menu?.resize(message.viewport); window.lastSubmittedFingerprint = ""; + if (phase === "map") { + resetMapState(); + maybeRefreshMap(); + } render(); break; case "close-window": @@ -382,6 +394,31 @@ function startNavigation(query: string, requestedProfile: RouteProfile): Promise ); } +async function startMap(): Promise { + if (!isMapboxConfigured()) return; + stopNavigation(""); + const generation = navigationGeneration; + phase = "acquiring"; + mapMode = "follow"; + zoomOffset = 0; + statusMessage = "Locating you for the map..."; + render(); + try { + ensureTracking(); + await waitForFix(); + if (generation !== navigationGeneration) return; + phase = "map"; + statusMessage = ""; + ensureTickTimer(); + maybeRefreshMap(); + render(); + } catch (error) { + if (generation !== navigationGeneration) return; + stopNavigation(String((error as Error)?.message ?? error)); + render(); + } +} + async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfile): Promise { const query = target.kind === "query" ? target.query : target.name; if (!isMapboxConfigured()) { @@ -390,7 +427,10 @@ async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfi render(); throw new Error(statusMessage); } + const generation = ++navigationGeneration; + const checkCurrent = () => { if (generation !== navigationGeneration) throw new Error("Navigation cancelled."); }; const hadActiveRoute = phase === "navigating" && follower !== null; + const hadActiveMap = phase === "map"; const previous = { destination, destinationName, @@ -404,6 +444,7 @@ async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfi render(); ensureTracking(); const fix = await waitForFix(); + checkCurrent(); phase = "routing"; let picked: GeocodeCandidate; @@ -420,6 +461,7 @@ async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfi statusMessage = `Finding ${query}...`; render(); candidates = await geocodeForward(query, fix, 5); + checkCurrent(); if (!candidates.length) { throw new Error(`No places found matching "${query}".`); } @@ -432,6 +474,7 @@ async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfi statusMessage = `Routing to ${destinationName}...`; render(); const route = await fetchRoute(fix, destination, profile); + checkCurrent(); adoptRoute(route); // Saved destinations already have a row of their own on the idle page; // everything else (voice, assistant, a re-picked recent) goes on the @@ -447,13 +490,13 @@ async function startNavigationTo(target: NavTarget, requestedProfile: RouteProfi if (window) post({ type: "focus-window", windowId: window.windowId }); return startSummary(picked, candidates, route); } catch (error) { + if (generation !== navigationGeneration) throw error; const message = String((error as Error)?.message ?? error); statusMessage = message; - // A failed new destination shouldn't kill guidance that was already - // running; fall back to the ongoing route (and its destination, which - // the geocode step may have partially overwritten). - if (hadActiveRoute) { - phase = "navigating"; + // A failed new destination shouldn't close the existing map or guidance. + // Restore the destination too: geocoding may have overwritten it. + if (hadActiveRoute || hadActiveMap) { + phase = hadActiveRoute ? "navigating" : "map"; destination = previous.destination; destinationName = previous.destinationName; destinationPlace = previous.destinationPlace; @@ -474,7 +517,6 @@ function adoptRoute(route: Route): void { mapMode = "follow"; zoomOffset = 0; statusMessage = ""; - routeGeneration++; resetMapState(); ensureTracking(); ensureTickTimer(); @@ -484,6 +526,7 @@ function adoptRoute(route: Route): void { } function stopNavigation(finalStatus: string): void { + ++navigationGeneration; phase = "idle"; statusMessage = finalStatus; follower = null; @@ -491,7 +534,6 @@ function stopNavigation(finalStatus: string): void { destination = null; destinationName = ""; destinationPlace = ""; - routeGeneration++; resetMapState(); stopTrackingIfIdle(); ensureTickTimer(); @@ -512,6 +554,12 @@ function handleFix(fix: TrackedLocation): void { } for (const waiter of firstFixWaiters.splice(0)) waiter(fix); + if (phase === "map") { + statusMessage = ""; + maybeRefreshMap(); + render(); + return; + } if (phase !== "navigating" || !follower) return; progress = follower.update(fix.longitude, fix.latitude); @@ -533,14 +581,16 @@ function handleFix(fix: TrackedLocation): void { async function maybeReroute(fix: TrackedLocation): Promise { if (rerouteInFlight || !destination) return; if (Date.now() - lastRerouteAtMs < REROUTE_MIN_INTERVAL_MS) return; + const generation = navigationGeneration; rerouteInFlight = true; lastRerouteAtMs = Date.now(); statusMessage = "Rerouting..."; render(); try { const route = await fetchRoute(fix, destination, profile); - if (phase === "navigating") adoptRoute(route); + if (generation === navigationGeneration && phase === "navigating") adoptRoute(route); } catch (error) { + if (generation !== navigationGeneration) return; statusMessage = `Reroute failed: ${String((error as Error)?.message ?? error)}`; render(); } finally { @@ -582,7 +632,7 @@ function waitForFix(): Promise { /** Hold the magnetometer only while its reading is actually on screen. */ function reconcileCompass(): void { const wanted = - window !== null && window.foreground && screenOn && phase === "navigating" && mapMode === "follow"; + window !== null && window.foreground && screenOn && (phase === "navigating" || phase === "map") && mapMode === "follow"; if (wanted === compassActive) return; compassActive = wanted; if (wanted) { @@ -601,7 +651,9 @@ function handleCompassEvent(event: CompassEvent): void { // Wearer-fit offset from the Compass app's calibration, then declination // from our own GPS fix: the map is always true-north referenced. const magnetic = calibrateHeading(event.headingDegrees); - const heading = normalizeHeading(magnetic + (currentDeclination() ?? 0)); + const correction = currentDeclination(); + if (correction === null) return; // Keep the travel-direction arrow until true north is known. + const heading = normalizeHeading(magnetic + correction); if (headHeadingDeg !== null && angularDistance(heading, headHeadingDeg) < HEADING_STEP_DEG) return; headHeadingDeg = heading; render(); @@ -609,19 +661,7 @@ function handleCompassEvent(event: CompassEvent): void { /** Declination at the last fix; it only varies over tens of kilometres, so cache per coarse position. */ function currentDeclination(): number | null { - if (!lastFix) return null; - if ( - !declinationCache || - Math.abs(declinationCache.latitude - lastFix.latitude) > 0.5 || - Math.abs(declinationCache.longitude - lastFix.longitude) > 0.5 - ) { - declinationCache = { - latitude: lastFix.latitude, - longitude: lastFix.longitude, - degrees: magneticDeclinationDegrees(lastFix.latitude, lastFix.longitude), - }; - } - return declinationCache.degrees; + return lastFix ? magneticDeclinationDegrees(lastFix.latitude, lastFix.longitude) : null; } function angularDistance(a: number, b: number): number { @@ -630,7 +670,7 @@ function angularDistance(a: number, b: number): number { } function ensureTickTimer(): void { - const shouldRun = phase === "navigating"; + const shouldRun = phase === "navigating" || phase === "map"; if (shouldRun && tickTimer === null) { tickTimer = setInterval(() => { maybeRefreshMap(); @@ -646,6 +686,7 @@ function ensureTickTimer(): void { // Map pane function resetMapState(): void { + routeGeneration++; mapImage = null; mapFetchedKey = ""; mapInFlight = false; @@ -676,7 +717,7 @@ function currentBearing(): number { function currentZoom(): number { let zoom: number; - if (profile === "walking") { + if (phase === "map" || profile === "walking") { zoom = 16.5; } else if (progress && progress.metersToNextManeuver < 250) { zoom = 16.5; @@ -703,7 +744,7 @@ function desiredMapView(): { camera: StaticMapCamera; key: string; path: Array<[ const position = currentPosition(); if (!position) return null; const zoom = currentZoom(); - const bearing = Math.round(currentBearing() / BEARING_BUCKET_DEG) * BEARING_BUCKET_DEG; + const bearing = phase === "map" ? 0 : Math.round(currentBearing() / BEARING_BUCKET_DEG) * BEARING_BUCKET_DEG; const path = follower && progress ? simplifyPath(follower.routeSliceAround(progress.alongMeters, 250, 3000), 80) @@ -721,6 +762,7 @@ function desiredMapView(): { camera: StaticMapCamera; key: string; path: Array<[ * a few seconds passed while moving. Single-flight with failure backoff. */ function maybeRefreshMap(): void { + if (phase !== "navigating" && phase !== "map") return; if (!window || !window.foreground || !screenOn) return; if (mapInFlight || Date.now() < mapNextRetryAtMs) return; if (!isMapboxConfigured()) return; @@ -730,7 +772,8 @@ function maybeRefreshMap(): void { let stale = mapImage === null || view.key !== mapFetchedKey; if (!stale && view.camera.kind === "center" && mapLastFetchCenter) { const moved = haversineMeters(mapLastFetchCenter, [view.camera.longitude, view.camera.latitude]); - const viewportMeters = MAP_SIZE * metersPerPixel(view.camera.zoom, view.camera.latitude); + const size = mapDimensions(); + const viewportMeters = Math.min(size.width, size.height) * metersPerPixel(view.camera.zoom, view.camera.latitude); if (moved > viewportMeters * MOVE_REFRESH_FRACTION) stale = true; else if (Date.now() - mapLastFetchAtMs > IDLE_REFRESH_MS && moved > IDLE_REFRESH_MIN_MOVE_M) stale = true; } @@ -740,13 +783,12 @@ function maybeRefreshMap(): void { const generation = routeGeneration; fetchStaticMapGray({ camera: view.camera, - width: MAP_SIZE, - height: MAP_SIZE, + ...mapDimensions(), routePath: view.path.length >= 2 ? view.path : undefined, }) .then((image) => { - mapInFlight = false; if (generation !== routeGeneration) return; // Route/mode changed mid-fetch. + mapInFlight = false; levelMap(image); mapImage = image; mapFetchedKey = view.key; @@ -757,6 +799,7 @@ function maybeRefreshMap(): void { render(); }) .catch((error) => { + if (generation !== routeGeneration) return; mapInFlight = false; mapNextRetryAtMs = Date.now() + MAP_RETRY_BACKOFF_MS; mapLastError = String((error as Error)?.message ?? error); @@ -764,6 +807,12 @@ function maybeRefreshMap(): void { }); } +function mapDimensions(): { width: number; height: number } { + return phase === "map" && window + ? { width: window.viewportWidth, height: Math.max(1, window.viewportHeight - 30) } + : { width: MAP_SIZE, height: MAP_SIZE }; +} + /** Keep every Nth point (ends always included) to bound the overlay URL size. */ function simplifyPath(path: Array<[number, number]>, maxPoints: number): Array<[number, number]> { if (path.length <= maxPoints) return path; @@ -845,6 +894,7 @@ function startSummary(picked: GeocodeCandidate, candidates: GeocodeCandidate[], } function describeRouteStatus(): string { + if (phase === "map") return "Showing the map around your current location. No destination set."; if (phase === "arrived") return `Arrived at ${destinationName}.`; if (phase !== "navigating" || !follower) return "Navigation is not active."; if (!progress) return `Navigating to ${destinationName}; waiting for a GPS fix.`; @@ -866,12 +916,12 @@ function describeRouteStatus(): string { */ function windowMenuItems(win: NavWindow): MenuItem[] { const items: MenuItem[] = []; - if (phase === "navigating" || phase === "arrived") { + if (phase !== "idle") { items.push({ - label: "Stop navigation", + label: phase === "map" ? "Close map" : "Stop navigation", onSelect: (ctx) => { ctx.stack.pop(); - stopNavigation("Navigation stopped."); + stopNavigation(phase === "map" ? "" : "Navigation stopped."); render(); }, }); @@ -895,7 +945,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] { onSelect: () => { const enabled = navigateRememberRecentSetting.toggle(); if (!enabled) clearRecentDestinations(); - clampIdleSelection(); + syncIdleList(); }, render: ({ image, x, y, width, selected }) => { drawToggleMenuItem( @@ -910,7 +960,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] { ); }, }); - const selectedEntry = phase === "idle" ? idleEntries()[idleSelection] : undefined; + const selectedEntry = phase === "idle" ? syncIdleList().selectedItem : null; if (selectedEntry?.kind === "recent") { const recent = selectedEntry.destination; items.push({ @@ -918,7 +968,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] { onSelect: (ctx) => { ctx.stack.pop(); removeRecentDestination(recent); - clampIdleSelection(); + syncIdleList(); }, }); } @@ -928,7 +978,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] { onSelect: (ctx) => { ctx.stack.pop(); clearRecentDestinations(); - clampIdleSelection(); + syncIdleList(); }, }); } @@ -1013,7 +1063,7 @@ function customDestinationMenuItems(destination: SavedDestination): MenuItem[] { onSelect: (ctx) => { closeMenusAnd(ctx, () => { removeCustomDestination(destination.id); - clampIdleSelection(); + syncIdleList(); }); }, }, @@ -1068,7 +1118,7 @@ function finishEdit(confirmed: boolean): void { if (!editing) { navigateDestinationNameDraftSetting.set(""); navigateDestinationAddressDraftSetting.set(""); - clampIdleSelection(); + syncIdleList(); render(); } } @@ -1123,7 +1173,7 @@ function tokenSetupEntries(): IdleEntry[] { detail: "Get a free token in the phone browser", run: () => { statusMessage = openUrlOnPhone(MAPBOX_TOKENS_URL) - ? "Opened mapbox.com on your phone. Copy your public token (pk...) and pick Edit token." + ? "Opening mapbox.com on your phone. Copy your public token (pk...) and pick Edit token." : "Could not open a browser on the phone. Visit account.mapbox.com/access-tokens to get a token."; }, }, @@ -1138,18 +1188,42 @@ function tokenSetupEntries(): IdleEntry[] { function idleEntries(): IdleEntry[] { if (!isMapboxConfigured()) return tokenSetupEntries(); - const entries: IdleEntry[] = loadSavedDestinations() + const entries: IdleEntry[] = [{ + kind: "action", + label: "Map around me", + detail: "Follow my location", + run: () => { void startMap(); }, + }]; + entries.push(...loadSavedDestinations() .filter((destination) => destination.address) - .map((destination): IdleEntry => ({ kind: "saved", destination })); + .map((destination): IdleEntry => ({ kind: "saved", destination }))); for (const destination of loadRecentDestinations()) { entries.push({ kind: "recent", destination }); } return entries; } -function clampIdleSelection(): void { - const count = idleEntries().length; - idleSelection = Math.max(0, Math.min(idleSelection, count - 1)); +/** Horizontal inset of the idle list's selection boxes. */ +const IDLE_LIST_X = 20; +/** Pixels between consecutive idle rows' selection boxes. */ +const IDLE_ROW_GAP = 2; + +function idleList(): Menu { + idleMenu ??= new Menu({ + wrap: false, + rowGap: IDLE_ROW_GAP, + highlight: { radius: 6 }, + getHeight: () => listRowHeight(smallFont), + draw: drawIdleRow, + }); + return idleMenu; +} + +/** Refresh the idle list from idleEntries(); the selection keeps its index, clamped into range. */ +function syncIdleList(): Menu { + const list = idleList(); + list.setItems(idleEntries()); + return list; } function navigateToIdleEntry(entry: IdleEntry): void { @@ -1220,10 +1294,10 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void { mapMode = mapMode === "follow" ? "overview" : "follow"; resetMapState(); maybeRefreshMap(); - } else if (phase === "arrived") { + } else if (phase === "arrived" || phase === "map") { stopNavigation(""); } else if (phase === "idle") { - const entry = idleEntries()[idleSelection]; + const entry = syncIdleList().selectedItem; if (!entry) { frameTimings.finishFrame(frameId, "discarded: navigate ignored click"); return; @@ -1237,13 +1311,12 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void { return; } if (event.type === "scroll-up" || event.type === "scroll-down") { - if (phase === "navigating" && mapMode === "follow") { + if ((phase === "navigating" || phase === "map") && mapMode === "follow") { zoomOffset = Math.max(-3, Math.min(2, zoomOffset + (event.type === "scroll-up" ? 0.5 : -0.5))); maybeRefreshMap(); renderAndSubmit(win, frameId); } else if (phase === "idle" && idleEntries().length > 1) { - const count = idleEntries().length; - idleSelection = Math.max(0, Math.min(count - 1, idleSelection + (event.type === "scroll-down" ? 1 : -1))); + syncIdleList().moveSelection(event.type === "scroll-down" ? 1 : -1); renderAndSubmit(win, frameId); } else { frameTimings.finishFrame(frameId, "discarded: navigate ignored scroll"); @@ -1264,6 +1337,8 @@ function paintContent(win: NavWindow): GrayImage { const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0); if (editing) { paintEdit(image, editing); + } else if (phase === "map") { + paintMap(image); } else if (phase === "idle" || phase === "acquiring" || phase === "routing") { paintIdle(image, win); } else { @@ -1300,7 +1375,7 @@ function paintIdle(image: GrayImage, win: NavWindow): void { const hint = !configured ? "Navigation needs a Mapbox public token (free at mapbox.com). Get one, then enter it here or in Settings > API Keys." : entries.length - ? "Pick a destination below, or say one via Voice input (system menu, long-press)." + ? "View the map or pick a destination below. Use Voice input (system menu, long-press) to say a destination." : "Ask the voice assistant to navigate somewhere, or pick Voice input from the system menu (long-press) to say a destination. Save Home, Work and other places from the app menu."; const hintLines = wrapText(smallFont, hint, image.width - 48); const hintStep = smallFont.lineHeight + 2; @@ -1326,64 +1401,38 @@ function paintIdle(image: GrayImage, win: NavWindow): void { } } -/** Saved destinations (name + address) then recent places, one selectable row each. */ +/** Map action, saved destinations, then recent places, one selectable row each. */ function paintIdleList(image: GrayImage, win: NavWindow, entries: IdleEntry[], top: number): void { const rowHeight = listRowHeight(smallFont); const listBottom = image.height - 30; const visibleRows = Math.max(1, Math.floor((listBottom - top) / rowHeight)); - idleSelection = Math.max(0, Math.min(idleSelection, entries.length - 1)); - idleScrollRow = scrollToKeepSelectionVisible(idleScrollRow, idleSelection, visibleRows, entries.length); - const rowX = 20; const rowWidth = image.width - 40 - (entries.length > visibleRows ? 10 : 0); - const labelWidth = Math.floor(rowWidth * 0.4); - for (let row = 0; row < visibleRows; row++) { - const index = idleScrollRow + row; - const entry = entries[index]; - if (!entry) break; - const y = top + row * rowHeight; - const selected = index === idleSelection; - if (selected) drawSelectionHighlight(image, rowX, y, rowWidth, rowHeight - 2, win.focused); - const textX = rowX + 6; - const textY = y + LIST_ROW_TEXT_INSET; - if (entry.kind === "action") { - const label = truncateText(smallFont, entry.label, labelWidth); - image.drawText(smallFont, textX, textY, label, selected ? 245 : 210); - const detailX = textX + labelWidth + 8; - image.drawText( - smallFont, - detailX, - textY, - truncateText(smallFont, entry.detail, rowX + rowWidth - 6 - detailX), - selected ? 170 : 130, - ); - } else if (entry.kind === "saved") { - const label = truncateText(smallFont, entry.destination.name, labelWidth); - image.drawText(smallFont, textX, textY, label, selected ? 245 : 210); - const detailX = textX + labelWidth + 8; - image.drawText( - smallFont, - detailX, - textY, - truncateText(smallFont, entry.destination.address, rowX + rowWidth - 6 - detailX), - selected ? 170 : 130, - ); - } else { - const label = truncateText(smallFont, entry.destination.name, labelWidth); - image.drawText(smallFont, textX, textY, label, selected ? 245 : 210); - const detailX = textX + labelWidth + 8; - const detail = entry.destination.place ? `${entry.destination.place} (recent)` : "(recent)"; - image.drawText( - smallFont, - detailX, - textY, - truncateText(smallFont, detail, rowX + rowWidth - 6 - detailX), - selected ? 170 : 130, - ); - } - } - if (entries.length > visibleRows) { - drawListScrollbar(image, rowX + rowWidth + 4, top, visibleRows * rowHeight - 2, idleScrollRow, visibleRows, entries.length); + const list = idleList(); + list.setItems(entries); + list.paint(image, { x: IDLE_LIST_X, y: top, width: rowWidth, height: visibleRows * rowHeight - IDLE_ROW_GAP }, win.focused); + list.drawScrollbar(image, IDLE_LIST_X + rowWidth + 4, top, visibleRows * rowHeight - 2); +} + +/** Two columns: the entry's name, then its detail (action hint, address or place). */ +function drawIdleRow({ image, item: entry, x, y, width, selected }: MenuDrawArgs): void { + const labelWidth = Math.floor(width * 0.4); + const textX = x + 6; + const textY = y + LIST_ROW_TEXT_INSET; + let label: string; + let detail: string; + if (entry.kind === "action") { + label = entry.label; + detail = entry.detail; + } else if (entry.kind === "saved") { + label = entry.destination.name; + detail = entry.destination.address; + } else { + label = entry.destination.name; + detail = entry.destination.place ? `${entry.destination.place} (recent)` : "(recent)"; } + image.drawText(smallFont, textX, textY, truncateText(smallFont, label, labelWidth), selected ? 245 : 210); + const detailX = textX + labelWidth + 8; + image.drawText(smallFont, detailX, textY, truncateText(smallFont, detail, x + width - 6 - detailX), selected ? 170 : 130); } function paintNavigating(image: GrayImage, win: NavWindow): void { @@ -1455,6 +1504,21 @@ function paintNavigating(image: GrayImage, win: NavWindow): void { drawFooter(image, `${GESTURE_SCROLL} zoom ${GESTURE_CLICK} ${modeHint} ${GESTURE_DOUBLE_CLICK} back`, PANEL_X + 8); } +function paintMap(image: GrayImage): void { + const { width, height } = mapDimensions(); + if (mapImage) { + image.bitBlt(mapImage, 0, 0); + drawChevron(image, width / 2, height / 2, headHeadingDeg ?? 0); + } else { + image.drawText(smallFont, 24, height / 2 - 8, mapLastError ? "Map unavailable; retrying..." : "Loading map...", 170); + } + if (statusMessage) { + image.fillRect(0, 0, width, smallFont.lineHeight + 8, 0); + image.drawText(smallFont, 16, 4, truncateText(smallFont, statusMessage, width - 32), 200); + } + drawFooter(image, `${GESTURE_SCROLL} zoom ${GESTURE_CLICK} destinations ${GESTURE_DOUBLE_CLICK} back`); +} + /** * Position chevron, fixed at the map center and rotated `angleDeg` clockwise * from straight up (the map's render bearing) to the wearer's head heading. diff --git a/app/apps/navigate/navigation-sensors-messages.ts b/app/apps/navigate/navigation-sensors-messages.ts new file mode 100644 index 00000000..964571a7 --- /dev/null +++ b/app/apps/navigate/navigation-sensors-messages.ts @@ -0,0 +1,11 @@ +import { type TrackedLocation } from '../../native/location-types' +import { type CompassEvent } from '../../native/compass-types' + +export type NavigationSensorRequest = + | { action: 'start'; id: number; intervalMs: number } + | { action: 'stop'; id: number } + | { action: 'compass'; enabled: boolean } +export type NavigationSensorEvent = + | { kind: 'location'; id: number; location: TrackedLocation } + | { kind: 'error'; id: number; message: string } + | { kind: 'compass'; event: CompassEvent; declination: number | null } diff --git a/app/apps/navigate/navigation-sensors.ios.ts b/app/apps/navigate/navigation-sensors.ios.ts new file mode 100644 index 00000000..2a8739e4 --- /dev/null +++ b/app/apps/navigate/navigation-sensors.ios.ts @@ -0,0 +1,45 @@ +import { type LocationTrackerCallbacks } from '../../native/location-types' +import { type CompassEvent } from '../../native/compass-types' +import { type NavigationSensorEvent, type NavigationSensorRequest } from './navigation-sensors-messages' +export { type TrackedLocation } from '../../native/location-types' +export { COMPASS_CHANGED, type CompassEvent } from '../../native/compass-types' +declare const global: any + +const listeners = new Set<(event: CompassEvent) => void>() +const trackers = new Map() +let nextId = 0 +let declination: number | null = null +function send(request: NavigationSensorRequest): void { + global.postMessage({ type: 'navigation-sensors', request }) +} +export class LocationTracker { + private id = 0 + constructor(private readonly callbacks: LocationTrackerCallbacks) {} + isRunning(): boolean { return this.id !== 0 } + start(intervalMs = 1000): void { + if (this.id) return + this.id = ++nextId; trackers.set(this.id, this) + send({ action: 'start', id: this.id, intervalMs }) + } + stop(): void { + if (!this.id) return + const id = this.id; this.id = 0; trackers.delete(id) + send({ action: 'stop', id }) + } + receive(event: NavigationSensorEvent): void { + if (event.kind === 'location') this.callbacks.onLocation(event.location) + else if (event.kind === 'error') { this.stop(); this.callbacks.onError(event.message) } + } +} +export function handleNavigationSensorEvent(event: NavigationSensorEvent): void { + if (event.kind === 'compass') { + declination = event.declination + for (const listener of [...listeners]) listener(event.event) + } else trackers.get(event.id)?.receive(event) +} +export function addCompassListener(listener: (event: CompassEvent) => void): () => void { + listeners.add(listener) + return () => { listeners.delete(listener) } +} +export function setCompassEnabled(enabled: boolean, _owner?: string): void { send({ action: 'compass', enabled }) } +export function magneticDeclinationDegrees(_latitude: number, _longitude: number): number | null { return declination } diff --git a/app/apps/navigate/navigation-sensors.ts b/app/apps/navigate/navigation-sensors.ts new file mode 100644 index 00000000..4bf4793d --- /dev/null +++ b/app/apps/navigate/navigation-sensors.ts @@ -0,0 +1,13 @@ +import { type NavigationSensorEvent } from './navigation-sensors-messages' +export { LocationTracker, type TrackedLocation } from '../../native/location-tracker' +export { addCompassListener, setCompassEnabled, COMPASS_CHANGED, type CompassEvent } from '../../native/compass' +import { magneticDeclinationDegrees as calculate } from '../../native/geomagnetic' +let cached: { latitude: number; longitude: number; degrees: number | null } | null = null +export function magneticDeclinationDegrees(latitude: number, longitude: number): number | null { + if (!cached || Math.abs(latitude - cached.latitude) > 0.5 || Math.abs(longitude - cached.longitude) > 0.5) + cached = { latitude, longitude, degrees: calculate(latitude, longitude) } + return cached.degrees +} + +/** Android delivers native callbacks directly on the worker's Looper. */ +export function handleNavigationSensorEvent(_event: NavigationSensorEvent): void {} diff --git a/app/apps/nightscout/nightscout-app.ts b/app/apps/nightscout/nightscout-app.ts index b2fbdb00..2f6c536e 100644 --- a/app/apps/nightscout/nightscout-app.ts +++ b/app/apps/nightscout/nightscout-app.ts @@ -62,6 +62,7 @@ export function createNightscoutAppWindow(options: InProcessAppOptions): InProce submitFrame: options.submitFrame, setSurfaceVisible: options.setSurfaceVisible, removeSurface: options.removeSurface, + reconfigureSurface: options.reconfigureSurface, onClosed: () => { windowRenders.delete(render); syncNightscout(); diff --git a/app/apps/nightscout/nightscout.ts b/app/apps/nightscout/nightscout.ts index 8a0878da..675a07f8 100644 --- a/app/apps/nightscout/nightscout.ts +++ b/app/apps/nightscout/nightscout.ts @@ -420,11 +420,11 @@ export function nightscoutMenuItems(): MenuItem[] { openSettingsSubMenu(ctx, "Nightscout settings", [ textSettingMenuItem(nightscoutSiteUrlSetting), textSettingMenuItem(nightscoutApiTokenSetting), + toggleSettingMenuItem(nightscoutAlwaysShowInTopBarSetting), textSettingMenuItem(nightscoutMaxCannulaAgeSetting), textSettingMenuItem(nightscoutCartridgeLowSetting), textSettingMenuItem(nightscoutBatteryLowSetting), textSettingMenuItem(nightscoutMaxLoopAgeSetting), - toggleSettingMenuItem(nightscoutAlwaysShowInTopBarSetting), ]); }, }, diff --git a/app/apps/pinball/pinball-app.worker.ts b/app/apps/pinball/pinball-app.worker.ts index e9b375a5..80c4b1b4 100644 --- a/app/apps/pinball/pinball-app.worker.ts +++ b/app/apps/pinball/pinball-app.worker.ts @@ -12,8 +12,8 @@ * keep the BLE payload down to the dirty region around the ball. * * Controls (ball ready): scroll sets launch power, click launches. In play: - * click flips both flippers, scroll-up/down flips left/right individually, - * double-click pauses. Watch swipes: left/right work the matching flipper, + * ring-press flips both flippers for 300 ms; click launches from the plunger, + * double-click pauses. Swipes no longer trigger individual flippers; * up/down raise and lower launch power at the plunger. * Paused/game over: click resumes or starts a new game, double-click yields * focus, long-press opens the window menu. Losing input focus mid-game (a @@ -21,12 +21,14 @@ * sidebar) pauses, as does backgrounding or screen-off. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; import { getFont } from "../../graphics/bdffont"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; +import { playWorkerBuzzerSequence } from "../../native/worker-buzzer"; import { getActiveDisplay } from "../../native/active-display"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; @@ -44,7 +46,6 @@ import { import { clamp } from "../../util/numeric-util"; declare const global: any; -declare const com: any; const largeFont = getFont("terminus32"); const mediumFont = getFont("terminus24"); @@ -92,8 +93,8 @@ const FLIPPER_REST_DEG = 32; const FLIPPER_UP_DEG = -34; const FLIPPER_RISE_DEG_PER_S = 950; const FLIPPER_FALL_DEG_PER_S = 480; -/** Held at the top this long before falling, so early flips still connect. */ -const FLIPPER_HOLD_MS = 140; +/** Activation duration from input to release, including the rise animation. */ +const FLIPPER_HOLD_MS = 300; const FLIPPER_E = 0.4; type SegmentKind = "wall" | "gate" | "sling"; @@ -290,6 +291,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": { const window: PinballWindow = { windowId: message.windowId, @@ -429,7 +436,7 @@ function saveHighScore(window: PinballWindow): void { /** * Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the - * steps on its own timer, and the Java call is safe from the worker thread. + * steps on its own timer; the platform bridge routes it from the worker. * Minor (frequent) effects are dropped when they'd arrive within the * rate-limit gap; newsworthy ones always play. */ @@ -439,9 +446,7 @@ function playSfx(window: PinballWindow, steps: Step[], minor = false): void { if (minor && now - window.lastMinorSfxAtMs < MINOR_SFX_MIN_GAP_MS) return; if (minor) window.lastMinorSfxAtMs = now; try { - const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive(); - if (!communicator) return; - communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer); + playWorkerBuzzerSequence(buildSoundSequencePayload(steps)); } catch (error) { console.warn(`pinball sfx failed: ${error}`); } @@ -509,25 +514,14 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n case "scroll-up": if (ready) { window.launchPower = clamp(window.launchPower + 1, 1, LAUNCH_SPEEDS.length); - } else { - flip(window, window.flippers[0]!); } break; case "scroll-down": if (ready) { window.launchPower = clamp(window.launchPower - 1, 1, LAUNCH_SPEEDS.length); - } else { - flip(window, window.flippers[1]!); } break; - // Watch swipes: left/right work the matching flipper; up/down set the - // launch power at the plunger. - case "swipe-left": - if (!ready) flip(window, window.flippers[0]!); - break; - case "swipe-right": - if (!ready) flip(window, window.flippers[1]!); - break; + // Watch swipes up/down still set launch power at the plunger. case "swipe-up": if (ready) { window.launchPower = clamp(window.launchPower + 1, 1, LAUNCH_SPEEDS.length); @@ -538,10 +532,14 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n window.launchPower = clamp(window.launchPower - 1, 1, LAUNCH_SPEEDS.length); } break; + case "ring-press": + flip(window, window.flippers[0]!); + flip(window, window.flippers[1]!); + break; case "click": if (ready) { launchBall(window); - } else { + } else if (event.source !== "ring") { flip(window, window.flippers[0]!); flip(window, window.flippers[1]!); } @@ -618,10 +616,9 @@ function launchBall(window: PinballWindow): void { } function flip(window: PinballWindow, flipper: Flipper): void { - if (flipper.state === "hold") { - flipper.holdUntilMs = Date.now() + FLIPPER_HOLD_MS; - return; - } + // Time the pulse from the input, including the rise animation. + flipper.holdUntilMs = Date.now() + FLIPPER_HOLD_MS; + if (flipper.state === "hold") return; flipper.state = "rising"; playSfx(window, SFX_FLIPPER, true); } @@ -727,7 +724,7 @@ function stepFlippers(window: PinballWindow, dt: number): void { if (flipper.angleDeg <= FLIPPER_UP_DEG) { flipper.angleDeg = FLIPPER_UP_DEG; flipper.state = "hold"; - flipper.holdUntilMs = now + FLIPPER_HOLD_MS; + if (now >= flipper.holdUntilMs) flipper.state = "falling"; } break; case "hold": @@ -1015,7 +1012,7 @@ function paintPanel(image: GrayImage, window: PinballWindow): void { image.drawText(smallFont, PANEL_X, 216, `${GESTURE_SCROLL} power ${GESTURE_CLICK} launch`, 115); image.drawText(smallFont, PANEL_X, 236, `${GESTURE_DOUBLE_CLICK} pause`, 115); } else { - image.drawText(smallFont, PANEL_X, 216, `${GESTURE_CLICK} flip ${GESTURE_SCROLL} L/R flip`, 115); + image.drawText(smallFont, PANEL_X, 216, "Touch ring: both flippers", 115); image.drawText(smallFont, PANEL_X, 236, `${GESTURE_DOUBLE_CLICK} pause`, 115); } } diff --git a/app/apps/roam/roam-app.worker.ts b/app/apps/roam/roam-app.worker.ts index f35d09b3..5ec556eb 100644 --- a/app/apps/roam/roam-app.worker.ts +++ b/app/apps/roam/roam-app.worker.ts @@ -8,6 +8,7 @@ * menu, via the phone app's text editor. */ import "@nativescript/core/globals"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; @@ -165,6 +166,12 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + post({ type: "worker-idle" }); + break; + case "shutdown": + finishWorkerShutdown(); + break; case "open-window": window = { windowId: message.windowId, diff --git a/app/apps/teleprompter/teleprompter-app.ts b/app/apps/teleprompter/teleprompter-app.ts index bbd3be46..c615297b 100644 --- a/app/apps/teleprompter/teleprompter-app.ts +++ b/app/apps/teleprompter/teleprompter-app.ts @@ -52,10 +52,8 @@ export type TeleprompterAppOptions = InProcessAppOptions & { * over the home page. */ export function createTeleprompterAppWindow(options: TeleprompterAppOptions): InProcessWindow { - // The home menu's items are rebuilt in place whenever the recents change - // (MenuLayer reads the array each paint). - const homeItems: MenuItem[] = []; - const home = new MenuLayer("Teleprompter", homeItems, HOME_LAYOUT); + // The home menu's items are replaced (refreshHome) whenever the recents change. + const home = new MenuLayer("Teleprompter", [], HOME_LAYOUT); let created: InProcessWindow | null = null; let notice = ""; @@ -142,10 +140,8 @@ export function createTeleprompterAppWindow(options: TeleprompterAppOptions): In if (notice) { items.push({ label: notice, disabled: true, onSelect: () => {} }); } - homeItems.length = 0; - homeItems.push(...items); // Land on the most recent script (one click resumes it), else on Browse. - home.selectItem(getRecentScripts().length > 0 ? 1 : 0); + home.setItems(items, getRecentScripts().length > 0 ? 1 : 0); } refreshHome(); diff --git a/app/apps/terminal/background.ts b/app/apps/terminal/background.ts new file mode 100644 index 00000000..73e74aaa --- /dev/null +++ b/app/apps/terminal/background.ts @@ -0,0 +1,8 @@ +import { isTerminalWidgetConfigured } from "../glanceboard/configuration"; +import { loadConnections, parseConnectionString } from "./connections"; + +/** Keep session-list connections alive for a configured widget, even while it is hidden. */ +export function hasTerminalBackgroundWork(): boolean { + return isTerminalWidgetConfigured() && + loadConnections().some(connection => connection.enabled && parseConnectionString(connection.url) !== null); +} diff --git a/app/apps/terminal/hub-header.ts b/app/apps/terminal/hub-header.ts new file mode 100644 index 00000000..fc7b5a91 --- /dev/null +++ b/app/apps/terminal/hub-header.ts @@ -0,0 +1,11 @@ +import { wrapText, type WrapFont } from '../../graphics/textwrap'; + +/** Keep short status on the title row; give long errors their full width. */ +export function layoutHubHeader(font: WrapFont, title: string, status: string, width: number, step: number) { + const inlineX = 18 + font.measureText(title) + 16; + const inline = !/[\r\n]/.test(status) && font.measureText(status) <= Math.max(0, width - inlineX - 12); + const lines = inline ? [status] : wrapText(font, status, Math.max(1, width - 30)); + const x = inline ? inlineX : 18; + const y = inline ? 10 : 10 + step; + return { x, y, lines, listTop: inline ? 16 + step : 16 + step * (1 + lines.length) }; +} diff --git a/app/apps/terminal/terminal-app.worker.ts b/app/apps/terminal/terminal-app.worker.ts index 26e748c6..4dfefaf6 100644 --- a/app/apps/terminal/terminal-app.worker.ts +++ b/app/apps/terminal/terminal-app.worker.ts @@ -2,7 +2,8 @@ * Terminal app, hosted in its own worker thread. Window model: * - "terminal:hub": the window opened from the launcher; shows the list of * live g2mirror sessions across every connected host (grouped by host when - * more than one is connected), and hosts the Manage Connections section + * more than one is connected; selecting a host's heading launches a shell + * there), and hosts the Manage Connections section * where g2mirror:// connections are added, removed, and toggled. * - "terminal:view:N": opened by selecting a session in the hub; each has * its own websocket connection (the protocol allows one attached session @@ -31,10 +32,13 @@ * re-attach snapshot resyncs contents and scrollback only once it's needed. */ import "@nativescript/core/globals"; +import { hasTerminalBackgroundWork } from "./background"; +import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle"; import { GrayImage, type UiFont } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; import { getDefaultSmallFont, getTerminalFontConfig } from "../../graphics/ui-fonts"; +import { layoutHubHeader } from "./hub-header"; import { truncateText } from "../../graphics/textwrap"; import { TERMINAL_ICON_GLYPHS, type IconActivity } from "../../graphics/icons"; import { @@ -51,7 +55,8 @@ import { clamp } from "../../util/numeric-util"; import { terminalAutoReconnectSetting, terminalLaunchPresetsSetting, terminalNewConnectionSetting, terminalWakeOnBellSetting } from "../../ui/dashboard-settings"; import { connectionDisplayName, loadConnections, parseConnectionString, saveConnections, TERMINAL_CONNECTIONS_KEY, updateConnection, type TerminalConnection } from "./connections"; import { TerminalEmulator } from "./terminal-emulator"; -import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu"; +import { type MenuItem } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { lineStep, listRowHeight } from "../../ui/metrics"; import { WindowMenu } from "../../ui/window-menu"; import { appViewportSize } from "../../ui/shell/geometry"; @@ -123,8 +128,8 @@ type HubMode = "sessions" | "connections" | "add"; type HubWindow = BaseWindow & { kind: "hub"; mode: HubMode; - selectedIndex: number; - scrollRow: number; + /** The section's item list; created on first paint or input (hubMenu). */ + list: Menu | null; /** * Session recency keys in display order, captured when the window last * became visible so the list doesn't reshuffle under the user while it's @@ -310,6 +315,14 @@ post({ type: "worker-ready" }); global.onmessage = (event: { data: WorkerAppMessage }) => { const message = event.data; switch (message.type) { + case "check-idle": + reportIdle(); + break; + case "shutdown": + for (const control of controls.values()) stopControl(control); + controls.clear(); + finishWorkerShutdown(); + break; case "open-window": openWindow(message.windowId, message.surfaceId, message.title, message.viewport); break; @@ -361,7 +374,8 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { // Enter ("\r") after a wrapper-side pause so paste-detecting apps // (e.g. Claude Code) submit instead of inserting a newline. if (window && window.kind === "view") { - window.client.submitInput(message.text); + if (message.submit === false) window.client.sendInput(message.text); + else window.client.submitInput(message.text); } else if (window && window.kind === "hub" && window.mode === "add") { terminalNewConnectionSetting.set(message.text); scheduleRender(window); @@ -422,12 +436,17 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { // toggles edited in the Settings app, the Add-connection draft typed on the // phone). onSettingsStoreChanged((key) => { + if (key.startsWith("glanceboard.")) { + reportIdle(); + return; + } if (!key.startsWith("terminal.")) return; switch (key) { case TERMINAL_CONNECTIONS_KEY: // Only once the app has actually been opened. if (controlsInitialized) syncControlsFromSettings(); renderHubWindows(); + reportIdle(); return; case NEW_CONNECTION_DRAFT_KEY: // Live keystrokes from the phone editor; repaint the add screen. @@ -436,7 +455,7 @@ onSettingsStoreChanged((key) => { case "terminal.autoReconnect": if (!terminalAutoReconnectSetting.get()) { cancelPendingReconnects(); - } else if (windows.size > 0) { + } else if (windows.size > 0 || hasTerminalBackgroundWork()) { for (const control of controls.values()) { if ((control.state?.phase ?? "idle") === "failed") scheduleControlReconnect(control); } @@ -448,6 +467,10 @@ onSettingsStoreChanged((key) => { } }); +function reportIdle(): void { + if (windows.size === 0 && !hasTerminalBackgroundWork()) post({ type: "worker-idle" }); +} + /** Cancel scheduled retries (auto-reconnect turned off); stale views stay marked. */ function cancelPendingReconnects(): void { for (const control of controls.values()) { @@ -486,8 +509,7 @@ function openWindow(windowId: string, surfaceId: string, title: string, viewport lastSubmittedFingerprint: "", menu: null, mode: "sessions", - selectedIndex: 0, - scrollRow: 0, + list: null, sessionOrder: [], addError: "", }); @@ -519,8 +541,8 @@ function closeWindow(windowId: string): void { windows.delete(windowId); windowIconActivity.delete(windowId); updateHubAnimation(); - // Auto-reconnect only runs while at least one terminal window is open. - if (windows.size === 0) { + // A configured widget owns the control connections after the last window closes. + if (windows.size === 0 && !hasTerminalBackgroundWork()) { for (const control of controls.values()) { cancelControlReconnect(control); } @@ -674,19 +696,18 @@ function noteSessionListRecency(control: ControlConnection, state: G2MirrorState /** * Retry a control connection after a backoff delay. The delay doubles per * scheduled attempt and resets when a connection reaches "connected" (or on - * a manual Connect). No-op when auto-reconnect is off, no terminal window is - * open, or the connection was disabled/removed meanwhile. + * a manual Connect). Windows and a configured widget can both own the client. */ function scheduleControlReconnect(control: ControlConnection): void { if (control.reconnectTimer) return; if (!terminalAutoReconnectSetting.get()) return; - if (windows.size === 0) return; + if (windows.size === 0 && !hasTerminalBackgroundWork()) return; if (!control.config.enabled) return; const delayMs = control.reconnectDelayMs; control.reconnectDelayMs = Math.min(control.reconnectDelayMs * 2, RECONNECT_MAX_DELAY_MS); control.reconnectTimer = setTimeout(() => { control.reconnectTimer = null; - if (!terminalAutoReconnectSetting.get() || windows.size === 0) return; + if (!terminalAutoReconnectSetting.get() || (windows.size === 0 && !hasTerminalBackgroundWork())) return; if (controls.get(control.config.id) !== control || !control.config.enabled) return; if ((control.state?.phase ?? "idle") === "failed") startControl(control); }, delayMs); @@ -1027,10 +1048,7 @@ function windowMenuItems(window: TerminalWindow): MenuItem[] { label, onSelect: (ctx) => { ctx.stack.pop(); - launchAndOpenView(control, preset).catch((error) => { - // The hub status line also shows the server's error message. - console.warn(`terminal launch ${preset} failed: ${error}`); - }); + launchFromUi(control, preset); }, }); } @@ -1160,7 +1178,10 @@ function applyHistoryReply(window: ViewWindow, reply: { start: number; oldest: n type HubItem = { label: string; - /** Group heading (host name): drawn differently and skipped by selection. */ + /** + * Group heading (host name): drawn unindented and dimmer. Selectable only + * when it has an onSelect (launch a shell on that host). + */ heading?: boolean; /** Session with recent output: an animated indicator marks the row. */ active?: boolean; @@ -1169,11 +1190,48 @@ type HubItem = { onSelect?: () => void; }; +/** Vertical offset of a row's text line from the top of its selection box. */ +const HUB_ROW_TEXT_INSET = 4; +/** Horizontal inset of the list's selection boxes from the viewport edges. */ +const HUB_LIST_X = 20; + +/** The hub window's list, created on first use so openWindow stays free of paint dependencies. */ +function hubMenu(window: HubWindow): Menu { + window.list ??= new Menu({ + wrap: false, + rowGap: 1, + getHeight: () => listRowHeight(chromeFont()), + isSelectable: (item) => Boolean(item.onSelect), + onSelect: (item) => item.onSelect?.(), + draw: drawHubRow, + }); + return window.list; +} + +function drawHubRow({ image, item, x, y, width, selected }: MenuDrawArgs): void { + const font = chromeFont(); + if (item.heading) { + image.drawText(font, x, y + HUB_ROW_TEXT_INSET, item.label, selected ? 255 : 140); + return; + } + // Activity gutter, open-window number column, then the label, truncated + // to the row (shared with the Glanceboard's Terminal widget). + drawSessionRow(image, font, { + x: x + 2, + y: y + HUB_ROW_TEXT_INSET, + width: width - 8, + label: item.label, + openGlyph: item.openGlyph ?? null, + active: Boolean(item.active), + phase: hubAnimationPhase, + value: selected ? 255 : 200, + }); +} + /** Switch the hub between its sections, resetting selection state. */ function setHubMode(window: HubWindow, mode: HubMode): void { window.mode = mode; - window.selectedIndex = 0; - window.scrollRow = 0; + window.list?.select(0); window.addError = ""; } @@ -1225,9 +1283,14 @@ function hubSessionItems(window: HubWindow): HubItem[] { ]; const connected = connectedControls(); const multiHost = connected.length > 1; + const canLaunch = launchPresetNames().length > 0; for (const control of connected) { if (multiHost) { - items.push({ label: connectionDisplayName(control.config), heading: true }); + items.push({ + label: connectionDisplayName(control.config), + heading: true, + onSelect: canLaunch ? () => launchOnHost(window, control) : undefined, + }); } const sessions = orderedSessions(window, control); for (const session of sessions) { @@ -1403,34 +1466,6 @@ function endAddConnection(window: HubWindow): void { post({ type: "end-text-setting-edit" }); } -/** Move the hub selection to the next selectable item in `direction`. */ -function moveHubSelection(window: HubWindow, items: HubItem[], direction: -1 | 1): void { - let index = window.selectedIndex + direction; - while (index >= 0 && index < items.length && !items[index]!.onSelect) { - index += direction; - } - if (index >= 0 && index < items.length) { - window.selectedIndex = index; - } -} - -/** Clamp the selection into range and off heading rows (prefer moving down). */ -function clampHubSelection(window: HubWindow, items: HubItem[]): void { - if (!items.length) { - window.selectedIndex = 0; - return; - } - let index = Math.max(0, Math.min(window.selectedIndex, items.length - 1)); - if (!items[index]!.onSelect) { - let forward = index; - while (forward < items.length && !items[forward]!.onSelect) forward++; - let backward = index; - while (backward >= 0 && !items[backward]!.onSelect) backward--; - index = forward < items.length ? forward : Math.max(0, backward); - } - window.selectedIndex = index; -} - function handleHubInput(window: HubWindow, event: InputEvent, frameId: number): void { if (window.mode === "add") { if (event.type === "click") { @@ -1441,23 +1476,18 @@ function handleHubInput(window: HubWindow, event: InputEvent, frameId: number): frameTimings.finishFrame(frameId, "discarded: terminal add-connection ignored input"); return; } - const items = hubItems(window); - clampHubSelection(window, items); + const list = hubMenu(window); + list.setItems(hubItems(window)); switch (event.type) { case "scroll-up": - moveHubSelection(window, items, -1); - renderAndSubmit(window, frameId); - return; case "scroll-down": - moveHubSelection(window, items, 1); + case "click": + // The menu's input handler is async only to await onSelect; hub item + // callbacks are synchronous, so the selection move or action has + // already applied when it returns and the frame can paint right away. + list.handleInput(event).catch((error) => console.error("terminal hub action failed", error)); renderAndSubmit(window, frameId); return; - case "click": { - const item = items[window.selectedIndex]; - item?.onSelect?.(); - renderAndSubmit(window, frameId); - return; - } default: frameTimings.finishFrame(frameId, "discarded: terminal hub ignored input"); return; @@ -1544,6 +1574,37 @@ function launchPresetNames(): string[] { return names; } +/** + * A host heading in the hub was selected: launch a shell there. With a single + * launch preset it starts right away; with several, a submenu titled with the + * host's name picks which. + */ +function launchOnHost(window: HubWindow, control: ControlConnection): void { + const presets = launchPresetNames(); + if (presets.length === 1) { + launchFromUi(control, presets[0]!); + return; + } + windowMenu(window).open( + presets.map((preset) => ({ + label: `Launch ${preset}`, + onSelect: (ctx) => { + ctx.stack.pop(); + launchFromUi(control, preset); + }, + })), + connectionDisplayName(control.config), + ); +} + +/** Launch started from a menu or list row; failures only get logged. */ +function launchFromUi(control: ControlConnection, preset: string): void { + launchAndOpenView(control, preset).catch((error) => { + // The hub status line also shows the server's error message. + console.warn(`terminal launch ${preset} failed: ${error}`); + }); +} + /** Launch a preset on a host and open a view window on the new session. */ async function launchAndOpenView(control: ControlConnection, preset: string): Promise { if (!control.client) throw new Error("Not connected to the g2mirror server."); @@ -1568,62 +1629,31 @@ function paintHub(window: HubWindow): GrayImage { const font = chromeFont(); const step = lineStep(font); // No border box: the shell chrome (top bar + sidebar) already frames the app. - // Title and status share the top line. + // Long statuses get a wrapped block and move the list below it. const title = window.mode === "connections" ? "Terminal - Connections" : "Terminal"; image.drawText(font, 18, 10, title, 220); - const statusX = 18 + font.measureText(title) + 16; - image.drawText(font, statusX, 10, truncateText(font, hubStatusLine(window), Math.max(0, window.viewportWidth - statusX - 12)), 170); + const header = layoutHubHeader(font, title, hubStatusLine(window), window.viewportWidth, step); + header.lines.forEach((line, index) => image.drawText(font, header.x, header.y + index * step, line, 170)); - let listTop = 16 + step; + let listTop = header.listTop; if (window.mode === "sessions" && controls.size === 0) { image.drawText(font, 24, listTop, "Add a g2mirror:// connection to get started, see:", 150); image.drawText(font, 24, listTop + step, "https://github.com/jimrandomh/g2mirror", 190); listTop += 2 * step + 6; } - const items = hubItems(window); - clampHubSelection(window, items); - const hubRowH = listRowHeight(chromeFont()); - const visibleRowCount = Math.max(1, ((window.viewportHeight - 6 - listTop) / hubRowH) | 0); - window.scrollRow = scrollToKeepSelectionVisible(window.scrollRow, window.selectedIndex, visibleRowCount, items.length); - const lastVisibleRow = Math.min(items.length, window.scrollRow + visibleRowCount); - for (let index = window.scrollRow; index < lastVisibleRow; index++) { - const y = listTop + (index - window.scrollRow) * hubRowH; - const item = items[index]!; - if (item.heading) { - image.drawText(chromeFont(), 20, y + 2, item.label, 140); - continue; - } - const selected = index === window.selectedIndex; - if (selected) { - // Match the shell convention: fill only when this window has focus, so - // an outline-only selection signals the sidebar owns input. - drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, hubRowH - 1, window.focused, 8); - } - // Activity gutter, open-window number column, then the label, truncated - // to the row (shared with the Glanceboard's Terminal widget). - drawSessionRow(image, chromeFont(), { - x: 22, - y: y + 2, - width: window.viewportWidth - 22 - 26, - label: item.label, - openGlyph: item.openGlyph ?? null, - active: Boolean(item.active), - phase: hubAnimationPhase, - value: selected ? 255 : 200, - }); - } - if (items.length > visibleRowCount) { - drawListScrollbar( - image, - window.viewportWidth - 10, - listTop, - visibleRowCount * hubRowH - 4, - window.scrollRow, - visibleRowCount, - items.length, - ); - } + const list = hubMenu(window); + list.setItems(hubItems(window)); + // Selection boxes start above the text line; the menu fills the selected + // box only while this window has focus (an outline alone means the sidebar + // owns input), matching the shell convention. + const listHeight = window.viewportHeight - 6 - listTop; + list.paint( + image, + { x: HUB_LIST_X, y: listTop - 2, width: window.viewportWidth - 2 * HUB_LIST_X, height: listHeight }, + window.focused, + ); + list.drawScrollbar(image, window.viewportWidth - 10, listTop, listHeight - 4); return image; } diff --git a/app/apps/timer/timer-app.ts b/app/apps/timer/timer-app.ts index 0555aecf..af61c195 100644 --- a/app/apps/timer/timer-app.ts +++ b/app/apps/timer/timer-app.ts @@ -31,14 +31,12 @@ import { } from "../../ui/gestures"; import { type Layer, type LayerContext } from "../../ui/layers"; import { - drawListScrollbar, drawRightValueMenuItem, - drawSelectionHighlight, drawToggleMenuItem, - scrollToKeepSelectionVisible, TextPageLayer, type MenuItem, } from "../../ui/menu"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window"; import { shell } from "../../ui/shell/shell"; @@ -91,6 +89,8 @@ const LIST_SHARE = 0.4; const LIST_MIN_WIDTH = 190; const LIST_MAX_WIDTH = 240; const LIST_TOP = 6; +/** Pixels between consecutive list rows' selection boxes. */ +const LIST_ROW_GAP = 1; const STAGE_X = 10; /** Gap between the stage's right edge and the list's divider. */ const STAGE_LIST_GAP = 14; @@ -183,8 +183,22 @@ function fittingDigitFont(text: string, maxWidth: number, sizes: readonly number export class TimersLayer implements Layer { readonly acceptsDirectional = true; + /** + * The selected row's key: the source of truth for the selection, which + * survives row rebuilds and is pinned to newly ringing items. The list + * menu is re-synced from it before each paint, hit test and move. + */ private selectionKey = ""; - private scrollRow = 0; + private readonly list = new Menu({ + wrap: true, + rowGap: LIST_ROW_GAP, + highlight: { radius: 6 }, + getHeight: () => listRowHeight(smallFont()), + isSelectable: (row) => row.kind !== "header", + draw: (args) => this.drawListRow(args), + }); + /** Per-paint inputs to drawListRow. */ + private listPaint = { dim: 1, now: 0 }; private mode: Mode = { kind: "list" }; /** Which of the ringing stage's two buttons is highlighted. */ private ringButton = 0; @@ -232,7 +246,8 @@ export class TimersLayer implements Layer { let index = rows.findIndex((row) => row.key === this.selectionKey); if (index < 0) { // The row went away (a dismissed timer, a deleted alarm): stay in place. - index = clamp(this.scrollRow, 0, rows.length - 1); + const firstVisibleRow = Math.floor(this.list.scrollTop / listRowHeight(smallFont())); + index = clamp(firstVisibleRow, 0, rows.length - 1); while (index < rows.length && rows[index]!.kind === "header") index++; if (index >= rows.length) index = rows.findIndex((row) => row.kind !== "header"); this.selectionKey = rows[index]!.key; @@ -244,14 +259,16 @@ export class TimersLayer implements Layer { return rows[this.reconcileSelection(rows)]!; } - private moveSelection(rows: Row[], delta: number): void { - const count = rows.length; - let index = this.reconcileSelection(rows); - for (let step = 0; step < count; step++) { - index = (index + delta + count) % count; - if (rows[index]!.kind !== "header") break; - } - this.selectionKey = rows[index]!.key; + /** Point the list menu at these rows and the current selection. */ + private syncList(rows: Row[]): void { + this.list.setItems(rows, this.reconcileSelection(rows)); + } + + private moveSelection(rows: Row[], delta: -1 | 1): void { + this.syncList(rows); + this.list.moveSelection(delta); + const index = this.list.selectedIndex; + if (index !== null) this.selectionKey = rows[index]!.key; } /** Called on every engine change: jump to a newly ringing item. */ @@ -547,15 +564,14 @@ export class TimersLayer implements Layer { /** A mirror touch: on the list, select the row under it. */ hitTest(x: number, y: number, ctx: LayerContext): boolean { if (this.mode.kind !== "list") return false; - const { width, height } = ctx.stack.getBaseSize(); + const { width } = ctx.stack.getBaseSize(); const listX = width - listWidthFor(width); if (x < listX) return false; const rows = this.buildRows(Date.now()); - this.reconcileSelection(rows); - const rowHeight = listRowHeight(smallFont()); - const index = this.scrollRow + Math.floor((y - LIST_TOP) / rowHeight); - const row = rows[index]; - if (!row || row.kind === "header" || y < LIST_TOP || y > height) return true; + this.syncList(rows); + const index = this.list.indexAt(y); + if (index === null) return true; + const row = rows[index]!; if (this.selectionKey === row.key) { this.activateRow(row); } else { @@ -882,79 +898,78 @@ export class TimersLayer implements Layer { dim: number, now: number, ): void { - const font = smallFont(); - const rowHeight = listRowHeight(font); + const rowHeight = listRowHeight(smallFont()); const visible = Math.max(1, Math.floor((height - 2 * LIST_TOP) / rowHeight)); - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, selectedIndex, visible, rows.length); + this.listPaint = { dim, now }; + // While a picker owns the stage the list has no live selection, so no + // highlight; drawListRow still brightens the selected row by key. + this.list.setItems(rows, dim === 1 ? selectedIndex : null); + this.list.paint(image, { x: listX, y: LIST_TOP, width: listWidth - 12, height: visible * rowHeight - LIST_ROW_GAP }, focused); + this.list.drawScrollbar(image, listX + listWidth - 7, LIST_TOP, visible * rowHeight - 4); + } + + private drawListRow({ image, item: row, x, y, width, height }: MenuDrawArgs): void { + const font = smallFont(); + const { dim, now } = this.listPaint; + const rowHeight = height + LIST_ROW_GAP; const shade = (value: number) => Math.round(value * dim); - const textX = listX + 8; - const textWidth = listWidth - 24; - const last = Math.min(rows.length, this.scrollRow + visible); - for (let index = this.scrollRow; index < last; index++) { - const row = rows[index]!; - const y = LIST_TOP + (index - this.scrollRow) * rowHeight; - const textY = y + LIST_ROW_TEXT_INSET; - if (row.kind === "header") { - image.drawText(font, textX, textY, row.title!, shade(90)); - continue; - } - const selected = index === selectedIndex; - if (selected && dim === 1) { - drawSelectionHighlight(image, listX, y, listWidth - 12, rowHeight - 1, focused, 6); - } - const bright = shade(selected ? 250 : 210); - const soft = shade(selected ? 170 : 130); - switch (row.kind) { - case "timer": { - const timer = row.timer!; - const phase = timerPhase(timer); - const digits = phase === "rung" ? "0:00" : formatCountdown(timerRemainingMs(timer, now)); - const digitsValue = phase === "rung" ? (Math.floor(now / 1000) % 2 ? bright : soft) : phase === "paused" ? soft : bright; - image.drawText(font, textX, textY, digits, digitsValue); - const digitsWidth = font.measureText(digits) + 8; - const nameWidth = textWidth - digitsWidth - (phase === "paused" ? 12 : 0); - if (nameWidth > 12) { - image.drawText(font, textX + digitsWidth, textY, truncateText(font, timerDisplayName(timer), nameWidth), soft); - } - if (phase === "paused") drawPauseMark(image, listX + listWidth - 22, y + Math.floor(rowHeight / 2) - 5, soft); - break; - } - case "new-timer": - image.drawText(font, textX, textY, "+ New timer", shade(selected ? 220 : 150)); - break; - case "stopwatch": { - const stopwatch = timerEngine.state.stopwatch; - const running = stopwatchIsRunning(stopwatch); - const elapsed = formatElapsed(stopwatchElapsedMs(stopwatch, now), false); - image.drawText(font, textX, textY, "Stopwatch", running ? bright : soft); - image.drawText(font, listX + listWidth - 16 - font.measureText(elapsed), textY, elapsed, running ? bright : soft); - break; - } - case "alarm": { - const alarm = row.alarm!; - const on = alarm.enabled; - const time = formatTimeOfDay(alarm.hour, alarm.minute, twelveHour()); - image.drawText(font, textX, textY, time, on ? bright : soft); - const timeWidth = font.measureText(time) + 8; - const daysWidth = textWidth - timeWidth - 14; - if (daysWidth > 12) { - image.drawText(font, textX + timeWidth, textY, truncateText(font, alarm.label || formatDays(alarm.days), daysWidth), on ? soft : shade(80)); - } - const dotX = listX + listWidth - 22; - const dotY = y + Math.floor(rowHeight / 2) - 4; - if (alarm.ringingSinceMs !== null || on) image.fillRoundedRect(dotX, dotY, 8, 8, on ? shade(220) : shade(90), 4); - else image.drawRoundedRect(dotX, dotY, 8, 8, shade(90), 4); - break; - } - case "new-alarm": - image.drawText(font, textX, textY, "+ New alarm", shade(selected ? 220 : 150)); - break; - default: - break; - } + const textX = x + 8; + const textWidth = width - 12; + const textY = y + LIST_ROW_TEXT_INSET; + if (row.kind === "header") { + image.drawText(font, textX, textY, row.title!, shade(90)); + return; } - if (rows.length > visible) { - drawListScrollbar(image, listX + listWidth - 7, LIST_TOP, visible * rowHeight - 4, this.scrollRow, visible, rows.length); + const selected = row.key === this.selectionKey; + const bright = shade(selected ? 250 : 210); + const soft = shade(selected ? 170 : 130); + switch (row.kind) { + case "timer": { + const timer = row.timer!; + const phase = timerPhase(timer); + const digits = phase === "rung" ? "0:00" : formatCountdown(timerRemainingMs(timer, now)); + const digitsValue = phase === "rung" ? (Math.floor(now / 1000) % 2 ? bright : soft) : phase === "paused" ? soft : bright; + image.drawText(font, textX, textY, digits, digitsValue); + const digitsWidth = font.measureText(digits) + 8; + const nameWidth = textWidth - digitsWidth - (phase === "paused" ? 12 : 0); + if (nameWidth > 12) { + image.drawText(font, textX + digitsWidth, textY, truncateText(font, timerDisplayName(timer), nameWidth), soft); + } + if (phase === "paused") drawPauseMark(image, x + width - 10, y + Math.floor(rowHeight / 2) - 5, soft); + break; + } + case "new-timer": + image.drawText(font, textX, textY, "+ New timer", shade(selected ? 220 : 150)); + break; + case "stopwatch": { + const stopwatch = timerEngine.state.stopwatch; + const running = stopwatchIsRunning(stopwatch); + const elapsed = formatElapsed(stopwatchElapsedMs(stopwatch, now), false); + image.drawText(font, textX, textY, "Stopwatch", running ? bright : soft); + image.drawText(font, x + width - 4 - font.measureText(elapsed), textY, elapsed, running ? bright : soft); + break; + } + case "alarm": { + const alarm = row.alarm!; + const on = alarm.enabled; + const time = formatTimeOfDay(alarm.hour, alarm.minute, twelveHour()); + image.drawText(font, textX, textY, time, on ? bright : soft); + const timeWidth = font.measureText(time) + 8; + const daysWidth = textWidth - timeWidth - 14; + if (daysWidth > 12) { + image.drawText(font, textX + timeWidth, textY, truncateText(font, alarm.label || formatDays(alarm.days), daysWidth), on ? soft : shade(80)); + } + const dotX = x + width - 10; + const dotY = y + Math.floor(rowHeight / 2) - 4; + if (alarm.ringingSinceMs !== null || on) image.fillRoundedRect(dotX, dotY, 8, 8, on ? shade(220) : shade(90), 4); + else image.drawRoundedRect(dotX, dotY, 8, 8, shade(90), 4); + break; + } + case "new-alarm": + image.drawText(font, textX, textY, "+ New alarm", shade(selected ? 220 : 150)); + break; + default: + break; } } diff --git a/app/apps/timer/timer-engine.ts b/app/apps/timer/timer-engine.ts index 57bb5e88..34dbf3d7 100644 --- a/app/apps/timer/timer-engine.ts +++ b/app/apps/timer/timer-engine.ts @@ -8,7 +8,7 @@ * * Two expiry paths run side by side: a JS deadline for prompt on-glasses * ringing while the process is awake, and an AlarmManager alarm-clock alarm - * (FaceclawAlarms.java) that rings on the phone at the right moment even if + * (FaceclawAlarms.kt) that rings on the phone at the right moment even if * the process is asleep or dead. The phone shows the item silently first * and adds its own sound when the glasses cannot carry it (not connected, * not worn, charging, not shown within seconds, or not acknowledged within diff --git a/app/apps/weather/weather-app.ts b/app/apps/weather/weather-app.ts index 5636af1a..a0efec28 100644 --- a/app/apps/weather/weather-app.ts +++ b/app/apps/weather/weather-app.ts @@ -1,4 +1,4 @@ -import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions"; +import { ensureLocationPermission, hasLocationPermission } from "../../native/location-permissions"; import { weatherBridge } from "../../native/weather"; import { WeatherLayer } from "./weather"; import { @@ -13,12 +13,13 @@ export const WEATHER_SURFACE_ID = "window:weather"; /** Local current conditions and forecast from the National Weather Service. */ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessWindow { + let closed = false; let requestingPermission = false; let unsubscribe: (() => void) | null = null; let app: InProcessWindow; const requestUpdate = () => { - if (requestingPermission) return; + if (closed || requestingPermission) return; if (hasLocationPermission()) { weatherBridge.start(); return; @@ -26,9 +27,10 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW requestingPermission = true; void ensureLocationPermission().then((granted) => { requestingPermission = false; + if (closed) return; if (granted) weatherBridge.start(); app.requestRender(); - }); + }).catch((error) => { requestingPermission = false; console.warn(`Weather permission: ${error}`); }); }; app = createInProcessWindow({ @@ -52,7 +54,9 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW submitFrame: options.submitFrame, setSurfaceVisible: options.setSurfaceVisible, removeSurface: options.removeSurface, + reconfigureSurface: options.reconfigureSurface, onClosed: () => { + closed = true; unsubscribe?.(); unsubscribe = null; weatherBridge.stop(); diff --git a/app/apps/weather/weather.ts b/app/apps/weather/weather.ts index 5b16fbf8..d32d2e3f 100644 --- a/app/apps/weather/weather.ts +++ b/app/apps/weather/weather.ts @@ -1,12 +1,11 @@ import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage, type UiFont } from "../../graphics/image"; import { wrapText, truncateText } from "../../graphics/textwrap"; -import { clamp } from "../../util/numeric-util"; import { type ForecastPeriod, type WeatherState } from "../../native/weather"; import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures"; import { type Layer, type LayerContext } from "../../ui/layers"; -import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu"; -import { lineStep, tightRowHeight } from "../../ui/metrics"; +import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; +import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics"; const PAGE_X = 18; const HEADER_Y = 8; @@ -18,11 +17,26 @@ const FORECAST_TOP = 124; const FORECAST_MIN_ROW_HEIGHT = 23; /** Gap between forecast columns. */ const FORECAST_COL_GAP = 14; +/** Left edge of the forecast rows' selection boxes. */ +const FORECAST_BOX_X = PAGE_X - 7; +/** Pixels between consecutive forecast rows' selection boxes. */ +const FORECAST_ROW_GAP = 2; + +/** Forecast column x offsets from the row box's left edge. */ +type ForecastColumns = { name: number; temp: number; precip: number; summary: number }; /** Weather's current-conditions summary and scrollable 12-hour forecast. */ export class WeatherLayer implements Layer { - private selectedIndex = 0; - private scrollRow = 0; + /** Forecast table rows: selection only, a click does nothing there. */ + private readonly forecastMenu = new Menu({ + wrap: false, + rowGap: FORECAST_ROW_GAP, + highlight: { radius: 5 }, + getHeight: () => forecastRowHeight(getDefaultSmallFont()), + draw: (args) => this.drawForecastRow(args), + }); + /** Column layout for drawForecastRow, set by each drawForecast. */ + private columns: ForecastColumns = { name: 0, temp: 0, precip: 0, summary: 0 }; constructor( private readonly state: () => WeatherState, @@ -81,10 +95,9 @@ export class WeatherLayer implements Layer { return; } if (!weather.forecast.length || weather.phase !== "ready") return; - if (event.type === "scroll-up") { - this.selectedIndex = Math.max(0, this.selectedIndex - 1); - } else if (event.type === "scroll-down") { - this.selectedIndex = Math.min(weather.forecast.length - 1, this.selectedIndex + 1); + if (event.type === "scroll-up" || event.type === "scroll-down") { + this.forecastMenu.setItems(weather.forecast); + void this.forecastMenu.handleInput(event); } } @@ -134,12 +147,14 @@ export class WeatherLayer implements Layer { image.drawText(font, PAGE_X, FORECAST_TOP, "No upcoming forecast periods.", 160); return; } - this.selectedIndex = clamp(this.selectedIndex, 0, forecast.length - 1); - const rowH = Math.max(FORECAST_MIN_ROW_HEIGHT, tightRowHeight(font) + 3); + const rowH = forecastRowHeight(font); // The last row only needs its text line (not a full row pitch of // clearance below), which usually fits one more row before the bottom. + // The menu only draws rows whose whole selection box fits, so the box + // holds that many rows, cut at the image's bottom edge. const visibleRows = Math.max(1, 1 + Math.floor((height - FORECAST_TOP - (font.lineHeight + 4)) / rowH)); - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, forecast.length); + const boxTop = FORECAST_TOP - 2; + const boxHeight = Math.min(visibleRows * rowH - FORECAST_ROW_GAP, height - boxTop); // Columns are sized to the widest period name so names ("Wednesday // Night") never truncate; the summary column absorbs what's left. @@ -155,20 +170,31 @@ export class WeatherLayer implements Layer { image.drawText(font, tempX, FORECAST_HEADER_Y, "Temp", 105); image.drawText(font, precipX, FORECAST_HEADER_Y, "Rain", 105); - const last = Math.min(forecast.length, this.scrollRow + visibleRows); - for (let index = this.scrollRow; index < last; index++) { - const period = forecast[index]!; - const y = FORECAST_TOP + (index - this.scrollRow) * rowH; - const selected = index === this.selectedIndex; - if (selected) { - drawSelectionHighlight(image, PAGE_X - 7, y - 2, width - 2 * (PAGE_X - 7), rowH - 2, ctx.stack.isFocused(), 5); - } - const shade = selected ? 245 : 190; - image.drawText(font, PAGE_X, y + 2, period.name, shade); - image.drawText(font, tempX, y + 2, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade); - image.drawText(font, precipX, y + 2, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155); - image.drawText(font, summaryX, y + 2, truncateText(font, period.shortForecast, width - summaryX - PAGE_X), selected ? 220 : 165); - } + this.columns = { + name: PAGE_X - FORECAST_BOX_X, + temp: tempX - FORECAST_BOX_X, + precip: precipX - FORECAST_BOX_X, + summary: summaryX - FORECAST_BOX_X, + }; + this.forecastMenu.setItems(forecast); + this.forecastMenu.paint( + image, + { x: FORECAST_BOX_X, y: boxTop, width: width - 2 * FORECAST_BOX_X, height: boxHeight }, + ctx.stack.isFocused(), + ); + } + + private drawForecastRow({ image, item: period, x, y, width, height, selected }: MenuDrawArgs): void { + const font = getDefaultSmallFont(); + const columns = this.columns; + const shade = selected ? 245 : 190; + const textY = centeredTextY(font, y, height); + image.drawText(font, x + columns.name, textY, period.name, shade); + image.drawText(font, x + columns.temp, textY, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade); + image.drawText(font, x + columns.precip, textY, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155); + // The summary stops PAGE_X short of the viewport's right edge, as the name starts PAGE_X in from the left. + const summaryWidth = width - columns.summary - columns.name; + image.drawText(font, x + columns.summary, textY, truncateText(font, period.shortForecast, summaryWidth), selected ? 220 : 165); } private drawMessage( @@ -190,6 +216,11 @@ export class WeatherLayer implements Layer { } } +/** Forecast row pitch, including the gap between selection boxes. */ +function forecastRowHeight(font: UiFont): number { + return Math.max(FORECAST_MIN_ROW_HEIGHT, tightRowHeight(font) + 3); +} + function formatAge(ageMs: number): string { const minutes = Math.max(0, Math.round(ageMs / 60_000)); if (minutes < 1) return "just now"; diff --git a/app/assistant/conversations.ts b/app/assistant/conversations.ts index 7e5df8f8..9d7dd7a3 100644 --- a/app/assistant/conversations.ts +++ b/app/assistant/conversations.ts @@ -1,5 +1,5 @@ import { AssistantSession, type AssistantBackendConfig, type AssistantSessionHistory } from "./session"; -import { ASSISTANT_MODEL_VALUES, type AssistantModel } from "./models"; +import { ASSISTANT_MODEL_VALUES, supportedAssistantModel, type AssistantModel } from "./models"; export type ReasoningLevel = "default" | "low" | "medium" | "high"; export type Conversation = { @@ -31,7 +31,7 @@ export class AssistantConversations { typeof record.id === "string" && ASSISTANT_MODEL_VALUES.includes(record.model) && ["default", "low", "medium", "high"].includes(record.reasoning) && Array.isArray(record.history?.messages) && Array.isArray(record.history?.transcript), - ).map((record) => ({ ...record, session: null })); + ).map((record) => ({ ...record, model: supportedAssistantModel(record.model), session: null })); this.selectedId = data.selectedId; } } catch { /* First launch or invalid saved state. */ } diff --git a/app/assistant/direct-backend.ts b/app/assistant/direct-backend.ts index 451abdf6..753a89cf 100644 --- a/app/assistant/direct-backend.ts +++ b/app/assistant/direct-backend.ts @@ -51,10 +51,13 @@ export class DirectAssistantBackend { let cancelled = false; let streamHandle: LlmStreamHandle | null = null; let priorText = ""; + let deadline: ReturnType | null = null; + const clearDeadline = () => { if (deadline !== null) clearTimeout(deadline); deadline = null; }; const finishError = (message: string) => { if (cancelled) return; cancelled = true; + clearDeadline(); streamHandle?.cancel(); streamHandle = null; options.callbacks.onError(message); }; @@ -102,10 +105,11 @@ export class DirectAssistantBackend { block.type === "tool_use", ); if (result.stopReason !== "tool_use" || toolUses.length === 0) { + clearDeadline(); options.callbacks.onTurnDone({ stopReason: result.stopReason }); return; } - void this.runToolCalls(toolUses, options) + void this.runToolCalls(toolUses, options, () => cancelled) .then((toolResults) => { // Always record the results, even if cancelled mid-flight: an // assistant tool_use with no matching tool_result would make the @@ -131,11 +135,13 @@ export class DirectAssistantBackend { : streamAnthropicMessage(streamOptions); }; + deadline = setTimeout(() => finishError("Assistant stopped: turn took too long"), MAX_TURN_MS); runIteration(0); return { cancel: () => { cancelled = true; + clearDeadline(); streamHandle?.cancel(); streamHandle = null; }, @@ -145,9 +151,14 @@ export class DirectAssistantBackend { private async runToolCalls( toolUses: Array>, options: DirectTurnOptions, + isCancelled: () => boolean, ): Promise { const results: LlmContentBlock[] = []; for (const toolUse of toolUses) { + if (isCancelled()) { + results.push({ type: "tool_result", tool_use_id: toolUse.id, content: "Cancelled", is_error: true }); + continue; + } const canonicalName = options.resolveToolName(toolUse.name); options.callbacks.onToolActivity(canonicalName); const result = await options.registry.callTool(canonicalName, toolUse.input); diff --git a/app/assistant/models.ts b/app/assistant/models.ts index f3cc792d..66d67670 100644 --- a/app/assistant/models.ts +++ b/app/assistant/models.ts @@ -13,6 +13,13 @@ export const ASSISTANT_MODEL_VALUES = [ ] as const; export type AssistantModel = (typeof ASSISTANT_MODEL_VALUES)[number]; +/** Keep saved model IDs portable, while offering only supported choices. */ +export const ASSISTANT_MODEL_CHOICES: readonly AssistantModel[] = + global.isIOS ? ASSISTANT_MODEL_VALUES.filter(value => value !== "qwen") : ASSISTANT_MODEL_VALUES; +export function supportedAssistantModel(value: AssistantModel): AssistantModel { + return global.isIOS && value === "qwen" ? "auto" : value; +} + export type AssistantProvider = "anthropic" | "openai" | "local"; export type AssistantApiKeys = { diff --git a/app/assistant/navigate-tools.ts b/app/assistant/navigate-tools.ts index 127cea41..feec02fc 100644 --- a/app/assistant/navigate-tools.ts +++ b/app/assistant/navigate-tools.ts @@ -4,7 +4,7 @@ * the app closed — so these always-available wrappers launch the app first, * wait for its tools to register, and forward the call. */ -import { ensureFineLocationPermission } from "../g2/android-permissions"; +import { ensureFineLocationPermission } from "../native/location-permissions"; import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry"; import { callAppToolWithLaunch } from "./launch-on-call"; diff --git a/app/assistant/system-tools.ts b/app/assistant/system-tools.ts index bc74f651..75309775 100644 --- a/app/assistant/system-tools.ts +++ b/app/assistant/system-tools.ts @@ -1,4 +1,5 @@ -import { readUpcomingEvents, type CalendarEvent } from "../native/calendar"; +import { readUpcomingEventsAsync, type CalendarEvent } from "../native/calendar"; +import { hasCalendarPermission } from "../native/calendar-permissions"; import { mediaControllerBridge } from "../native/media-controller"; import { dismissNotification, readActiveNotifications } from "../native/notification-icons"; import { shell } from "../ui/shell/shell"; @@ -20,7 +21,7 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void registered = true; // Keep a media listener warm so "what's playing" works without opening Music. - void mediaControllerBridge.start(); + if (!global.isIOS) void mediaControllerBridge.start(); registry.registerSystemTool( { @@ -71,15 +72,23 @@ export function registerSystemTools(registry: ToolRegistry = toolRegistry): void additionalProperties: false, }, }, - (args) => { + async (args) => { + if (!hasCalendarPermission()) return err("Calendar access is not granted. Open Calendar or Permissions on your phone to allow access."); const withinHours = clampNumber(args?.within_hours, 1, 24 * 60, 168); const maxEvents = clampNumber(args?.max_events, 1, 50, 10); - const events = readUpcomingEvents(maxEvents, withinHours * 60 * 60 * 1000); - if (!events.length) return ok("No upcoming events in that window."); - return ok(events.map(formatEvent).join("\n")); + try { + const events = await readUpcomingEventsAsync(maxEvents, withinHours * 60 * 60 * 1000); + if (!events.length) return ok("No upcoming events in that window."); + return ok(events.map(formatEvent).join("\n")); + } catch { + return err("Calendar events could not be read. Check calendar access on your phone and try again."); + } }, ); + // Phone media and Android notification tools are still platform-specific. + if (global.isIOS) return; + registry.registerSystemTool( { name: "media.now_playing", diff --git a/app/bootstrap.android.ts b/app/bootstrap.android.ts new file mode 100644 index 00000000..c567bfe6 --- /dev/null +++ b/app/bootstrap.android.ts @@ -0,0 +1,28 @@ +import { Application } from '@nativescript/core' +import { registerShareIntentHandler } from './native/share-intents' +import { installNativeUserAgent } from './util/http' +import { registerPhoneRotation } from './native/phone-rotation' +import { startLiveHealthSync, startAlignedRingPull } from './health/health-live' + +import { runKotlinBridgeSmokeTest } from './native/kotlin-bridge' + +declare const __DEV__: boolean; + +if (__DEV__) runKotlinBridgeSmokeTest() + +installNativeUserAgent() +registerShareIntentHandler() +registerPhoneRotation() + +// Ring health records live only in the communicator's memory until something +// stores them. Both health surfaces store on open, but a pull that lands while +// they are closed would be lost if the process restarted first, so this keeps +// them reaching disk regardless. No-op when there is nothing new. +startLiveHealthSync() + +// Collect from the ring on a wall-clock cadence (:01 and :31). Without this +// nothing drives a pull on a timer at all - only onRingReady() on reconnect - +// so a ring that stays connected is never read. +startAlignedRingPull() + +Application.run({ moduleName: 'app-root' }) diff --git a/app/bootstrap.ios.ts b/app/bootstrap.ios.ts new file mode 100644 index 00000000..6897e8dc --- /dev/null +++ b/app/bootstrap.ios.ts @@ -0,0 +1,16 @@ +import { Application, Frame } from '@nativescript/core' +import { runKotlinBridgeSmokeTest } from './native/kotlin-bridge' +declare const FaceclawConfigPort: any +declare const __DEV__: boolean; + +if (__DEV__) runKotlinBridgeSmokeTest() + +// Apply transferred preferences before settings getters or app workers load. +FaceclawConfigPort.processPendingRequest() +require('./phone-ui/onboarding-register.ios') +const { hasCompletedOnboarding } = require('./phone-ui/onboarding-state') as typeof import('./phone-ui/onboarding-state') +Application.run({ create: () => { + const frame = new Frame() + frame.navigate({ moduleName: hasCompletedOnboarding() ? 'phone-ui/main-page' : 'phone-ui/onboarding-page' }) + return frame +} }) diff --git a/app/g2/ancs-client.ts b/app/g2/ancs-client.ts new file mode 100644 index 00000000..0f11eedf --- /dev/null +++ b/app/g2/ancs-client.ts @@ -0,0 +1,287 @@ +import type { AndroidNotification } from '../native/notification-types' + +export const ANCS_FIRMWARE_VERSION = 16 +export const ANCS_CONNECT_MESSAGE = 'Connect glasses to receive iPhone notifications.' +const MAX_ACTIVE = 128 +const MAX_RESPONSE = 2048 +const MAX_COMMAND = 80 // Firmware relay limit, including its 8-byte header. +const HEADER = 11 +const read32 = (b: Uint8Array, i: number) => (b[i] | b[i+1]<<8 | b[i+2]<<16 | b[i+3]<<24) >>> 0 +const le32 = (n: number) => [n & 255, n>>>8 & 255, n>>>16 & 255, n>>>24] +// NativeScript does not supply TextDecoder in every isolate. +function utf8(bytes: Uint8Array): string { + let result = '' + for (let i=0; i= 0xc2 && first <= 0xdf ? 2 : first >= 0xe0 && first <= 0xef ? 3 : first >= 0xf0 && first <= 0xf4 ? 4 : 0 + if (!count) { result += '\uFFFD'; i++; continue } + let code = first & (0x7f >> count), valid = i+count <= bytes.length + for (let j=1; j 0x10ffff || code >= 0xd800 && code <= 0xdfff) { + result += '\uFFFD'; i++; continue + } + result += String.fromCodePoint(code); i += count + } + return result +} +function fallbackAppName(identifier: string): string { + return identifier.split('.').filter(Boolean).pop() || 'Unknown app' +} +function notificationDate(value: string): number | undefined { + const parts = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})$/.exec(value) + if (!parts) return undefined + const [year,month,day,hour,minute,second] = parts.slice(1).map(Number) + const date = new Date(year,month-1,day,hour,minute,second) + return date.getFullYear() === year && date.getMonth() === month-1 && date.getDate() === day && + date.getHours() === hour && date.getMinutes() === minute && date.getSeconds() === second ? date.getTime() : undefined +} +type Source = { flags: number; category: number; revision: number; postTime: number; popup: boolean } +type Request = { uid: number; revision: number; action?: number; metadata?: boolean; appId?: string } +export type AncsState = 'disconnected' | 'starting' | 'ready' | 'unavailable' + +/** Owns one phone/lens BLE session. Nothing is persisted. Firmware enforces + * peer ownership; token/sequence checks here additionally reject stale chunks. */ +export class AncsClient { + state: AncsState = 'disconnected' + statusMessage = ANCS_CONNECT_MESSAGE + private resetReceived = false + private token = 0 + private lastStartToken = 0 + private sequence = 0 + private packetKind = -1 + private fragments: number[] = [] + private response: number[] = [] + private sources = new Map() + private notifications = new Map() + private appNames = new Map() + private maxWrite = 20 + private queue: Request[] = [] + private acknowledged = false + private responseDone = false + private pending: Request | null = null + private timer: ReturnType | null = null + private retry: ReturnType | null = null + private revision = 0 + private disposed = true + constructor(private readonly send: (packet: Uint8Array) => Promise, + private readonly changed: (key?: string, popup?: boolean) => void, + private readonly log: (message: string) => void = () => {}) {} + + private status(state: AncsState, message: string): void { + this.state = state; this.statusMessage = message; this.log(message); this.changed() + } + + start(token: number, maxWrite = 20): void { + this.stop(); this.disposed = false; this.token = token >>> 0 || 1 + this.maxWrite = Math.min(MAX_COMMAND, maxWrite) + this.begin() + } + private begin(): void { + if (this.disposed) return + this.clear(); this.resetReceived = false + this.status('starting', 'Connecting to iPhone notifications…') + this.deadline() + const token = this.token + this.lastStartToken = token + void this.send(this.command(0)).catch(() => { if (token === this.token) this.recover() }) + } + stop(message = ANCS_CONNECT_MESSAGE): void { + this.disposed = true + if (this.retry !== null) clearTimeout(this.retry) + this.retry = null; this.clear(); this.token = 0 + this.status('disconnected', message) + } + stopCommand(): Uint8Array { return new Uint8Array([65,78,1,1,...le32(this.lastStartToken)]) } + private clear(): void { + if (this.timer !== null) clearTimeout(this.timer) + this.timer = null; this.sources.clear(); this.notifications.clear(); this.appNames.clear(); this.queue = [] + this.pending = null; this.response = []; this.fragments = []; this.packetKind = -1; this.sequence = 0 + } + private recover(message = 'Notification relay interrupted. Retrying…'): void { + if (this.disposed || this.retry !== null) return + this.clear(); this.status('unavailable', message) + // New token rejects every queued packet/response from the previous attempt. + this.token = (this.token + 1) >>> 0 || 1 + this.retry = setTimeout(() => { this.retry = null; this.begin() }, 5000) + } + private deadline(): void { + if (this.timer !== null) clearTimeout(this.timer) + this.timer = setTimeout(() => this.recover(this.state === 'starting' + ? this.resetReceived ? 'Glasses did not finish subscribing to iPhone notifications. Retrying…' + : 'No notification relay response. Check Share System Notifications on the right lens. Retrying…' + : 'Notification retrieval timed out. Retrying…'), 10000) + } + private command(op: number, payload: number[] = []): Uint8Array { + return new Uint8Array([65,78,1,op,...le32(this.token),...payload]) + } + read(limit = 50): AndroidNotification[] { + return [...this.notifications.values()].sort((a,b) => b.postTime-a.postTime).slice(0, Math.max(0,limit)) + .map(n => ({...n, lines: [...n.lines], actions: n.actions.map(a => ({...a}))})) + } + action(key: string, action: number): boolean { + const notification = [...this.notifications.entries()].find(([,n]) => n.key === key) + if (this.state !== 'ready' || this.queue.length >= MAX_ACTIVE || !notification || ![0,1].includes(action) || + !notification[1].actions.some(a => a.index === action && a.enabled)) return false + const [uid] = notification, source = this.sources.get(uid)! + this.queue.push({uid, revision: source.revision, action}); this.pump(); return true + } + /** Local hide: ANCS negative action can decline a call, so Dismiss must never + * silently translate into a destructive negative action. */ + dismiss(key: string): boolean { + const entry = [...this.notifications.entries()].find(([,n]) => n.key === key) + if (!entry) return false + this.notifications.delete(entry[0]); this.sources.delete(entry[0]); this.changed(); return true + } + receive(packet: Uint8Array): boolean { + if (packet[0] !== 65 || packet[1] !== 78) return false + if (this.disposed || this.state === 'unavailable' || packet.length < HEADER || packet[2] !== 1 || read32(packet,4) !== this.token) return true + const sequence = packet[8] | packet[9]<<8, flags = packet[10], kind = packet[3] + if (sequence !== this.sequence || flags > 3 || kind > 3 || packet.length > 244) { this.recover(); return true } + this.sequence = (sequence + 1) & 65535 + if (flags & 1) { + if (this.packetKind !== -1) { this.recover(); return true } + this.packetKind = kind; this.fragments = [] + } + if (this.packetKind !== kind || this.fragments.length + packet.length - HEADER > 512) { this.recover(); return true } + this.fragments.push(...packet.subarray(HEADER)) + if (flags & 2) { + const data = new Uint8Array(this.fragments); this.fragments = []; this.packetKind = -1 + if (kind === 0) { + if (data.length !== 1 || data[0] > 2) { this.recover(); return true } + if (data[0] === 2) { this.recover('Glasses reported a notification relay error. Retrying…'); return true } + if (data[0] === 0) { this.resetReceived = true; this.log('Glasses accepted notification relay START; awaiting subscription') } + if (data[0] === 1) { + if (this.timer !== null) clearTimeout(this.timer) + this.timer = null; this.status('ready', 'No current iOS notifications.'); this.pump() + } + } else if (kind === 1) this.source(data) + else if (kind === 2) this.attributes(data) + else if (this.pending) { + this.acknowledged = true + if (this.pending.action !== undefined || this.responseDone) this.complete() + } + } + return true + } + private source(data: Uint8Array): void { + if (data.length !== 8 || data[0] > 2) { this.recover(); return } + const uid = read32(data,4) + if (data[0] === 2) { + this.sources.delete(uid); this.notifications.delete(uid) + this.queue = this.queue.filter(r => r.uid !== uid); this.changed(); return + } + const old = this.sources.get(uid) + const source = {flags: data[1], category: data[2], revision: ++this.revision, + postTime: old?.postTime ?? Date.now(), popup: !old && data[0] === 0 && !(data[1] & 4)} + this.sources.set(uid, source) + while (this.sources.size > MAX_ACTIVE) { + const oldest = this.sources.keys().next().value! + this.sources.delete(oldest); this.notifications.delete(oldest) + this.queue = this.queue.filter(r => r.uid !== oldest) + } + this.queue = this.queue.filter(r => r.uid !== uid || r.action !== undefined) + this.queue.push({uid, revision: source.revision}); this.pump() + } + private pump(): void { + if (this.state !== 'ready' || this.pending) return + while (this.queue.length) { + const request = this.queue.shift()! + if (this.sources.get(request.uid)?.revision !== request.revision) continue + this.pending = request; this.response = []; this.acknowledged = this.responseDone = false; this.deadline() + // Notification attributes use two requests, each fitting a 20-byte ATT + // payload. App IDs cannot be fragmented by this firmware's CP relay. + const cp = request.appId !== undefined + ? [1,...Array.from(request.appId, c => c.charCodeAt(0)),0,0] + : request.action === undefined + ? request.metadata ? [0,...le32(request.uid),2,128,0,4,5,6,7] : [0,...le32(request.uid),0,1,128,0,3,0,2] + : [2,...le32(request.uid),request.action] + const token = this.token + void this.send(this.command(2,cp)).catch(() => { if (token === this.token) this.recover() }) + return + } + } + private attributes(data: Uint8Array): void { + if (!this.pending || this.pending.action !== undefined) return + if (this.response.length + data.length > MAX_RESPONSE) { this.recover(); return } + this.response.push(...data) + const b = new Uint8Array(this.response) + if (!b.length) return + const appId = this.pending.appId + let cursor: number + if (appId !== undefined) { + if (b[0] !== 1) { this.recover(); return } + const end = b.indexOf(0,1) + if (end < 0) { if (b.length > appId.length+1) this.recover(); return } + if (utf8(b.subarray(1,end)) !== appId) { this.recover(); return } + cursor = end+1 + } else { + if (b.length < 5) return + if (b[0] !== 0 || read32(b,1) !== this.pending.uid) { this.recover(); return } + cursor = 5 + } + const expected = appId !== undefined ? [0] : this.pending.metadata ? [2,4,5,6,7] : [0,1,3] + const attrs = new Map() + while (cursor < b.length) { + if (cursor+3 > b.length) return + const id = b[cursor], length = b[cursor+1] | b[cursor+2]<<8 + if (!expected.includes(id) || attrs.has(id) || length > 512) { this.recover(); return } + if (cursor+3+length > b.length) return + attrs.set(id,utf8(b.subarray(cursor+3,cursor+3+length))); cursor += 3+length + } + if (attrs.size !== expected.length) return + const {uid,revision} = this.pending, source = this.sources.get(uid) + if (appId !== undefined) { + const displayName = attrs.get(0)!.trim() + const name = displayName && displayName !== appId ? displayName : fallbackAppName(appId) + this.appNames.set(appId,name) + while (this.appNames.size > MAX_ACTIVE) this.appNames.delete(this.appNames.keys().next().value!) + for (const notification of this.notifications.values()) { + if (notification.packageName === appId) notification.appName = name + } + } + if (source?.revision === revision) { + const key = `ancs:${this.token}:${uid}` + if (appId !== undefined) { + this.publish(uid,source) + } else if (this.pending.metadata) { + const notification = this.notifications.get(uid) + if (notification) { + notification.subText = attrs.get(2)! + notification.when = notificationDate(attrs.get(5)!) ?? source.postTime + const size = attrs.get(4)! + if (/^\d+$/.test(size) && Number.isSafeInteger(Number(size))) notification.messageSize = Number(size) + notification.actions = [ + ...(source.flags & 8 && attrs.get(6) ? [{index:0,title:attrs.get(6)!,enabled:true}] : []), + ...(source.flags & 16 && attrs.get(7) ? [{index:1,title:attrs.get(7)!,enabled:true}] : []), + ] + const id = notification.packageName + // Bundle IDs are ASCII. Reject embedded NULs/invalid IDs and use the + // readable fallback if the entire command cannot fit either link. + if (!this.appNames.has(id) && /^[A-Za-z0-9.-]+$/.test(id) && id.length+11 <= this.maxWrite) { + this.queue.unshift({uid,revision,appId:id}) + } else this.publish(uid,source) + } + } else { + const packageName = attrs.get(0)!, title = attrs.get(1)!, text = attrs.get(3)! + this.notifications.set(uid,{key,packageName,appName:this.appNames.get(packageName) || fallbackAppName(packageName),title,text,bigText:text, + subText:'',infoText:'',summaryText:'',category:String(source.category),lines:[],postTime:source.postTime,when:source.postTime,actions:[],dismissLabel:'Hide on glasses'}) + this.queue.unshift({uid,revision,metadata:true}); this.changed(key,false) + } + } + this.responseDone = true + if (this.acknowledged) this.complete() + } + private publish(uid: number, source: Source): void { + const popup = source.popup; source.popup = false + this.changed(`ancs:${this.token}:${uid}`,popup) + } + private complete(): void { + if (this.timer !== null) clearTimeout(this.timer) + this.timer = null; this.pending = null; this.response = []; this.pump() + } +} diff --git a/app/g2/ble-protocol.ios.ts b/app/g2/ble-protocol.ios.ts new file mode 100644 index 00000000..68453a17 --- /dev/null +++ b/app/g2/ble-protocol.ios.ts @@ -0,0 +1,80 @@ +import { type RingInput } from "./ring-input"; +import { type CompassEvent } from '../native/compass-types' +/** iOS binary bridge to the same Kotlin protocol used by Android. */ +import { fromData, toData, nativeArray } from '../native/kotlin-data' +import { OsEventTypeList } from './events' +declare const FaceclawKitIosProtocol: any, FaceclawKitIosMessageReceiver: any +const protocol = FaceclawKitIosProtocol.new() +export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401' +export const G2_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e5402' +export const G2_RENDER_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e6402' +export const RING_NOTIFY = ['bae80011-4f05-4503-8e65-3af1f7329d1f', 'bae80013-4f05-4503-8e65-3af1f7329d1f'] +export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9, cfw: 0xf0 } as const +export function concat(...parts: Uint8Array[]): Uint8Array { + const output = new Uint8Array(parts.reduce((n, p) => n + p.length, 0)) + let offset = 0 + for (const part of parts) { output.set(part, offset); offset += part.length } + return output +} +export const integer = (field: number, value: number): Uint8Array => fromData(protocol.integerFieldValue(field, value)) +export const bytes = (field: number, value: Uint8Array): Uint8Array => fromData(protocol.bytesFieldData(field, toData(value))) +export const authentication = (magic: number) => fromData(protocol.authenticationMagic(magic)) +export const prelude = () => fromData(protocol.prelude()) +export const heartbeat = (magic: number) => fromData(protocol.heartbeatMagic(magic)) +export const audioControl = (magic: number, enabled: boolean) => fromData(protocol.audioControlMagicEnabled(magic, enabled)) +export const settingsQuery = (magic: number) => fromData(protocol.settingsQueryMagic(magic)) +export const shutdown = (magic: number) => fromData(protocol.shutdownMagic(magic)) +export const framebufferLease = (acquire: boolean) => fromData(protocol.framebufferLeaseAcquire(acquire)) +export const createLayout = (magic: number) => fromData(protocol.createLayoutMagic(magic)) +export const crc16 = (data: Uint8Array): number => protocol.crcData(toData(data)) +export function frameMessage(payload: Uint8Array, sid: number, flag: number, sequence: number, maxWrite = 240): Uint8Array[] { + const capacity = Math.min(232, Math.floor(maxWrite) - 8) + if (capacity < 12 || !Number.isFinite(capacity)) throw new Error('Negotiated BLE write size is too small') + if (Math.ceil((payload.length + 2) / capacity) > 255) throw new Error('Protocol message exceeds the fragment limit') + return nativeArray(protocol.frameDataSidFlagSequenceMaxWrite(toData(payload), sid, flag, sequence, Math.floor(maxWrite)), fromData) +} +export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number; packet?: Uint8Array } +export class MessageReceiver { + private native = FaceclawKitIosMessageReceiver.new() + clear(): void { this.native.clear() } + receive(link: string, data: Uint8Array, now = Date.now()): ProtocolMessage[] { + return nativeArray(this.native.receiveLinkDataNow(link, toData(data), now), (message: any) => ({ + sid: message.sid, flag: message.flag, payload: fromData(message.payload), command: message.command, + magic: message.magic, ...(message.packet ? { packet: fromData(message.packet) } : {}), + })) + } +} +export const readInteger = (data: Uint8Array, field: number, fallback = 0): number => protocol.readIntegerDataFieldFallback(toData(data), field, fallback) +export const readBytes = (data: Uint8Array, field: number): Uint8Array | undefined => { + const value = protocol.readBytesDataField(toData(data), field) + return value ? fromData(value) : undefined +} +export const readString = (data: Uint8Array, field: number): string => protocol.readStringDataField(toData(data), field) +export function authenticationSucceeded(message: ProtocolMessage, magic: number): boolean { + return message.sid === SID.auth && message.command === 4 && message.magic === magic && readBytes(message.payload, 3)?.length === 0 +} +export type GlassesInput = { ringInput?: RingInput; kind: 'sys-event' | 'list-click' | 'text-click' | 'display-wake' | 'even-ai'; eventType: number; eventSource: number; systemExitReasonCode: number; containerName: string; frameId: number } +function input(event: any): GlassesInput | null { + return event ? { kind: event.kind === 'sys-event' && event.eventType === OsEventTypeList.HEAD_UP_EVENT ? 'display-wake' : event.kind, + eventType: event.eventType, eventSource: event.eventSource, systemExitReasonCode: event.systemExitReasonCode, + ringInput: Number(event.ringTick) >= 0 ? { tick: Number(event.ringTick), type: Number(event.ringType), + aux: Number(event.ringAux), speed: Number(event.ringSpeed) } : undefined, + containerName: event.containerName, frameId: 0 } : null +} +export const decodeGlassesInput = (message: ProtocolMessage): GlassesInput | null => input(protocol.glassesInputDataSidFlag(toData(message.payload), message.sid, message.flag)) +export const decodeRingInput = (data: Uint8Array): GlassesInput | null => input(protocol.ringInputData(toData(data))) +export function packGray4(gray: Uint8Array, width: number, height: number): Uint8Array { + if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || gray.length !== width * height) throw new Error('Invalid display frame') + return fromData(protocol.packDataWidthHeight(toData(gray), width, height)) +} +export const rle4 = (packed: Uint8Array): Uint8Array => fromData(protocol.rleData(toData(packed))) + +export function decodeCompassInput(message: ProtocolMessage): CompassEvent | null { + const event = protocol.compassInputDataSidFlag(toData(message.payload), message.sid, message.flag) + return event ? { command: event.command, headingDegrees: event.headingDegrees, + ...(event.diagnosticFlags >= 0 ? { diagnostics: { + magneticAccuracy: event.magneticAccuracy, magneticAnomalies: event.magneticAnomalies, + orientationSource: event.orientationSource, flags: event.diagnosticFlags, sampleTimeMs: Number(event.sampleTimeMs), + } } : {}), + } : null +} diff --git a/app/g2/brightness-curve.ts b/app/g2/brightness-curve.ts new file mode 100644 index 00000000..1695ae8d --- /dev/null +++ b/app/g2/brightness-curve.ts @@ -0,0 +1,26 @@ +export const DEFAULT_BRIGHTNESS_CURVE = "0:0,0.2:7,1.5:33,5:100"; +const PREVIOUS_DEFAULT_CURVE = "0:0,1:10,10:30,100:65,1000:100"; + +/** Percent of the configured range; defaults (20–100) give 20/26/46/100. */ +export function brightnessCurveError(value: string): string | null { + const pairs = value.split(",").map(part => part.trim().split(":")); + if (pairs.length < 2 || pairs.length > 16 || pairs.some(p => p.length !== 2 || p.some(s => !s.trim()))) + return "Use 2–16 lux:percent pairs separated by commas."; + const points = pairs.map(pair => pair.map(Number)); + if (points.some(([lux, percent]) => !Number.isFinite(lux) || !Number.isFinite(percent) + || lux < 0 || lux > 1_000_000 || percent < 0 || percent > 100)) + return "Lux must be 0–1000000 and percent must be 0–100."; + if (points[0][0] !== 0 || points[0][1] !== 0 || points[points.length - 1][1] !== 100) + return "Start at 0:0 and end at 100%. Set dark-room brightness with Auto minimum."; + if (points.some(([lux, percent], i) => i > 0 && (lux <= points[i-1][0] || percent < points[i-1][1]))) + return "Lux must increase and brightness must not decrease."; + return null; +} + +/** Keep incomplete drafts intact. Validation must never replace typed input. */ +export function normalizeBrightnessCurve(value: string | null | undefined): string { + if (value == null) return DEFAULT_BRIGHTNESS_CURVE; + if (brightnessCurveError(value)) return value; + const canonical = value.split(",").map(pair => pair.split(":").map(Number).join(":")).join(","); + return canonical === PREVIOUS_DEFAULT_CURVE ? DEFAULT_BRIGHTNESS_CURVE : canonical; +} diff --git a/app/g2/cfw-message-type.ts b/app/g2/cfw-message-type.ts new file mode 100644 index 00000000..5dd24a61 --- /dev/null +++ b/app/g2/cfw-message-type.ts @@ -0,0 +1,37 @@ +/** Message IDs and header flags mirror g2flash/patches/zlib_glue.c. */ +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_BOUNDING_BOX = 3 +export const CFW_MSG_BUZZER = 5 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_FULL_FRAME = 6 +export const CFW_MSG_DIAGNOSTICS = 7 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_MULTI_SEGMENT = 8 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_RECT_COPY = 9 +export const CFW_MSG_COMPASS = 10 +export const CFW_MSG_CLEANUP = 11 +export const CFW_MSG_RETIRED_12 = 12 +export const CFW_MSG_RETIRED_13 = 13 +export const CFW_MSG_RETIRED_14 = 14 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_STOCK_FONT_STRING = 15 +export const CFW_MSG_AMBIENT_LIGHT = 16 +export const CFW_MSG_RING_BATTERY = 17 +export const CFW_MSG_RETIRED_CACHE_WRITE = 18 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_CACHED_IMAGE = 19 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_CACHED_TEXT = 20 +export const CFW_MSG_UPLOAD_RESOURCE = 21 +export const CFW_MSG_EVICT_RESOURCE = 22 +export const CFW_MSG_PANEL_READ = 23 +export const CFW_MSG_PANEL_WRITE = 24 +export const CFW_MSG_PANEL_PATTERN = 25 +export const CFW_MSG_DRAW_CALLS = 26 +export const CFW_MSG_SET_ROOT_DISPLAY_LIST = 27 +export const CFW_MSG_PRESENT = 28 +export const CFW_MSG_CREATE_SURFACE = 29 +export const CFW_MSG_BRIGHTNESS = 30 +export const CFW_MSG_TYPE_MASK = 127 +export const CFW_MSG_FLAG_LENSES_DIFFER = 128 diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index a70a7c55..ad7b6137 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -1,4 +1,6 @@ -import { Application, ImageSource } from "@nativescript/core"; +import { startRemoteInput } from "../remote/service"; +import { acceptInput, resetRingInputFilter } from "../ui/input-monitor"; +import { Application, Dialogs, ImageSource } from "@nativescript/core"; import { EvenAIStatus, EvenAIStatusName, EventSourceType, EventSourceTypeName, OsEventTypeList, OsEventTypeName, WatchGestureType, WatchGestureTypeName } from "./events"; import { isValidMacAddress, loadDeviceAddresses } from "./device-addresses"; import { @@ -51,8 +53,7 @@ import { voiceControlBridge } from "../native/voice-control"; import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane"; import { prepareFrameDraws } from "../graphics/glyph-wire"; -import { getDefaultMediumFont } from "../graphics/ui-fonts"; -import { wrapText } from "../graphics/textwrap"; +import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from "./lock-screen"; import { rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell"; import { type InputEvent } from "../ui/gestures"; import { registerSystemTools } from "../assistant/system-tools"; @@ -70,7 +71,7 @@ import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-a import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible, type WindowHeightMode } from "../ui/shell/geometry"; import { type LayerActions, type TextSettingsEditToggle } from "../ui/layers"; import { type KeyboardInputSession } from "../ui/shell/keyboard-input"; -import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, brightnessSetting, brightnessSettingToLevel, displayModeSetting, navigateDisplayModeSetting, navigateVerticalPositionSetting, terminalDisplayModeSetting, terminalVerticalPositionSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; +import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayModeSetting, navigateDisplayModeSetting, navigateVerticalPositionSetting, terminalDisplayModeSetting, terminalVerticalPositionSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization"; import { getInstalledEvenHubAppById, @@ -141,8 +142,6 @@ type DashboardListener = (snapshot: DashboardSnapshot) => void; const SHELL_SURFACE_ID = "shell"; /** The shell chrome composites above every window surface (zOrder 0). */ const SHELL_SURFACE_Z_ORDER = 1; -const LOCK_SCREEN_SURFACE_ID = "lock-screen"; -const LOCK_SCREEN_MESSAGE = "Glasses locked; unlock the phone to unlock the glasses."; // Top-bar clock refresh; the phone-side preview polls the Java composite so // it reflects every app (including worker apps the TS side never renders). const SHELL_REFRESH_INTERVAL_MS = 60_000; @@ -172,24 +171,6 @@ function createInitialDisplayPreview(): ImageSource | null { return grayImageToPreviewSource(new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0)); } -function createLockScreenImage(): GrayImage { - const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); - const font = getDefaultMediumFont(); - const boxWidth = 480; - const boxHeight = 150; - const boxX = Math.round((G2_LENS_WIDTH - boxWidth) / 2); - const boxY = Math.round((G2_LENS_HEIGHT - boxHeight) / 2); - image.drawRoundedRect(boxX, boxY, boxWidth, boxHeight, 150, 12); - const lines = wrapText(font, LOCK_SCREEN_MESSAGE, boxWidth - 64); - const textHeight = lines.length * font.lineHeight; - const firstY = boxY + Math.round((boxHeight - textHeight) / 2); - lines.forEach((line, index) => { - const x = Math.round((G2_LENS_WIDTH - font.measureText(line)) / 2); - image.drawText(font, x, firstY + index * font.lineHeight, line, 230); - }); - return image; -} - function formatTimestamp(date: Date): string { return date.toISOString().slice(11, 23); } @@ -223,7 +204,22 @@ class DashboardController { private activeTextSettings: ConfigSettingString[] = []; private activeTextEditorTitle = ""; private activeTextEditorOnFinish: (() => void) | null = null; + /** + * The caller's cancel callback. LayerActions has always declared this fifth + * parameter, but neither the wiring below nor startTextSettingsEdit accepted + * it, so it was dropped silently -- a 4-parameter function is assignable to a + * 5-parameter type, so nothing failed to compile. A caller that tracks its own + * "editor is open" state therefore had no way to learn the editor closed. + */ + private activeTextEditorOnCancel: (() => void) | null = null; private activeTextEditorToggle: TextSettingsEditToggle | null = null; + /** + * The active settings' values (and the toggle's) when the edit began. The + * phone fields write through on every keystroke, so this is what the + * editor's Cancel button puts back. + */ + private activeTextSettingOriginalValues: string[] = []; + private activeTextEditorToggleOriginalValue = false; private evenNotificationActive = false; private evenAppConflictMessage = ""; private firmwareWarningMessage = ""; @@ -287,7 +283,6 @@ class DashboardController { private lockSurfaceConfigured = false; private lastLockScreenEnabled = lockScreenEnabledSetting.get(); private offState: (() => void) | null = null; - private offLog: (() => void) | null = null; private offRing: (() => void) | null = null; private offBattery: (() => void) | null = null; private offSilentMode: (() => void) | null = null; @@ -340,7 +335,8 @@ class DashboardController { title: string, onFinish?: () => void, toggle?: TextSettingsEditToggle, - ) => this.startTextSettingsEdit(settings, title, onFinish, toggle), + onCancel?: () => void, + ) => this.startTextSettingsEdit(settings, title, onFinish, toggle, onCancel), endTextSettingEdit: () => this.endTextSettingEdit(), startVoiceCapture: (endpointing = false) => this.startVoiceCapture(endpointing), stopVoiceCapture: () => this.stopVoiceCapture(), @@ -424,6 +420,15 @@ class DashboardController { this.syncAssistantBridge(); // The watch drives the same synthetic-input path as the phone UI's test // buttons, plus app/window/lock commands; it mirrors the state below. + startRemoteInput({ + ready: () => this.phase === "connected" || this.phase === "charging", + locked: () => this.glassesLocked, + input: (gesture, source) => this.injectSyntheticRingInput(gesture, source), + acceptsText: () => !!shell.foregroundWindow()?.receiveTextInput, + text: (text, submit) => { if (!shell.isScreenOn()) shell.wake("window"); shell.sendTextToForegroundWindow(text, { submit }); this.requestShellRender(); }, + assistantAvailable: () => shell.isAssistantAvailable(), + assistant: text => shell.sendToAssistant(text), + }); this.wearRemote = new WearRemote({ apps: LAUNCHABLE_APPS, injectInput: (kind) => this.injectSyntheticRingInput(kind, "watch"), @@ -505,7 +510,7 @@ class DashboardController { window.relayout(); await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, ); } else if (window.closeable) { @@ -543,7 +548,7 @@ class DashboardController { void (async () => { for (const window of Array.from(shell.getWindows())) { if (!changed.includes(window.appId)) continue; - await this.configureWindowSurface(window.surfaceId, window.windowId === shell.foregroundWindow()?.windowId, window.heightMode); + await this.configureWindowSurface(window.surfaceId, this.isForegroundWindow(window.windowId), window.heightMode); window.relayout?.(); } shell.foregroundWindow()?.requestRender(); @@ -555,12 +560,11 @@ class DashboardController { const position = verticalPositionSetting.get(); if (position === this.lastVerticalPosition) return; this.lastVerticalPosition = position; - const foregroundWindowId = shell.foregroundWindow()?.windowId; void (async () => { for (const window of shell.getWindows()) { await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, ); } @@ -910,11 +914,14 @@ class DashboardController { */ private pushBrightness(force = false): void { if (!this.communicator) return; - const value = brightnessSetting.get(); + const preferences = getBrightnessPreferences(); + const value = JSON.stringify(preferences); if (!force && value === this.lastPushedBrightness) return; this.lastPushedBrightness = value; - const level = brightnessSettingToLevel(value); - void this.communicator.setBrightness(level === null, level ?? 0).catch(() => {}); + void this.communicator.configureBrightness(preferences).catch((error) => { + this.lastPushedBrightness = null; + this.appendLog(`brightness configuration failed: ${this.formatError(error)}`); + }); } /** Save the occupied part of the composited screen as a 4-bit grayscale PNG. */ @@ -1013,7 +1020,13 @@ class DashboardController { return this.glassesDisplayLabel(); } if (this.silentMode && this.phase === "connected") { - return "Connected (Silent mode enabled)"; + // Say how to leave it. Silent mode is entered and exited only by the + // same gesture on the glasses, the firmware ignores all input and blanks + // the display while it is on, and nothing here can turn it off -- there + // is no setter, only onSilentMode. So a user who triggered it by + // accident sees a connected pair of glasses that answers nothing, with + // no way to find out why. The instruction belongs where they are looking. + return "Connected (Silent mode — long-press both touchpads on the glasses to exit)"; } // The preview compositor keeps the mirror live (including as a black // frame while the simulated screen is off), so never cover it. @@ -1235,11 +1248,10 @@ class DashboardController { // loop re-applies the current dim from here. await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1); this.appliedUnderlayDim = 1; - const foregroundWindowId = shell.foregroundWindow()?.windowId; for (const window of shell.getWindows()) { await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, target, ); @@ -1337,10 +1349,15 @@ class DashboardController { ring: ringAddress, }); this.communicator = communicator; - this.offLog = communicator.onLog((line) => { - this.appendLog(line); - }); + // Size the compositor before anything else is awaited: windows opened + // during the bridge start-ups below (the terminal hub, restored apps) + // configure their surfaces through this.communicator, and the Java call + // queue is FIFO, so enqueuing the screen size first guarantees it lands + // ahead of them. Mirrors ensurePreviewDisplay. + await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT); + await communicator.configureBrightness(getBrightnessPreferences()); this.offState = communicator.onStateChange((state) => { + if (state.phase !== "connected") resetRingInputFilter(); if (state.phase === "unpaired") { // Java parked its retry loop: an arm's Android bond is gone, so // every redial would fail the same way until the user re-pairs. @@ -1506,7 +1523,6 @@ class DashboardController { await nightscoutBridge.start(); // Register the compositor surfaces: the shell chrome above all windows, // and a surface per live window (only the foreground one is composited). - await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT); await communicator.configureSurface(SHELL_SURFACE_ID, { x: 0, y: 0, @@ -1520,11 +1536,10 @@ class DashboardController { await communicator.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1); this.appliedUnderlayDim = 1; await this.configureLockSurface(communicator); - const foregroundWindowId = shell.foregroundWindow()?.windowId; for (const window of shell.getWindows()) { await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, ); } @@ -1551,8 +1566,6 @@ class DashboardController { const message = this.formatError(error); this.offState?.(); this.offState = null; - this.offLog?.(); - this.offLog = null; this.offRing?.(); this.offRing = null; this.offBattery?.(); @@ -1691,8 +1704,6 @@ class DashboardController { this.clearDashboardTimer(); this.offState?.(); this.offState = null; - this.offLog?.(); - this.offLog = null; this.offRing?.(); this.offRing = null; this.offBattery?.(); @@ -1893,11 +1904,15 @@ class DashboardController { title: string, onFinish?: () => void, toggle?: TextSettingsEditToggle, + onCancel?: () => void, ): void { this.activeTextSettings = Array.from(settings.slice(0, 2)); + this.activeTextSettingOriginalValues = this.activeTextSettings.map((setting) => setting.get()); this.activeTextEditorTitle = title; this.activeTextEditorOnFinish = onFinish ?? null; this.activeTextEditorToggle = toggle ?? null; + this.activeTextEditorToggleOriginalValue = toggle?.setting.get() ?? false; + this.activeTextEditorOnCancel = onCancel ?? null; this.emit(); } @@ -2014,11 +2029,17 @@ class DashboardController { private endTextSettingEdit(): void { const finishedSettings = this.activeTextSettings; + // Closing without the done key is a cancel from the caller's point of view. + // finishTextSettingEdit clears this first, so a completed edit never fires it. + const onCancel = this.activeTextEditorOnCancel; this.activeTextSettings = []; + this.activeTextSettingOriginalValues = []; this.activeTextEditorTitle = ""; this.activeTextEditorOnFinish = null; this.activeTextEditorToggle = null; + this.activeTextEditorOnCancel = null; this.emit(); + onCancel?.(); if (finishedSettings.includes(nightscoutSiteUrlSetting) || finishedSettings.includes(nightscoutApiTokenSetting)) { void this.refreshNightscoutAfterSettingsChange(); } @@ -2031,7 +2052,15 @@ class DashboardController { */ finishActiveTextSettingEdit(): void { if (!this.activeTextSettings.length) return; + const invalid = this.activeTextSettings.find(setting => setting.validationError()); + if (invalid) { + void Dialogs.alert({ title: invalid.editorTitle, message: invalid.validationError()!, okButtonText: "OK" }); + return; + } const onFinish = this.activeTextEditorOnFinish; + // Completing is not cancelling: drop the cancel callback before + // endTextSettingEdit, which fires whatever is still set. + this.activeTextEditorOnCancel = null; const closesGlassesEditor = this.activeTextSettings.length === 1; this.endTextSettingEdit(); if (closesGlassesEditor && this.textEditorHost?.closeTextEditor()) { @@ -2040,6 +2069,28 @@ class DashboardController { onFinish?.(); } + /** + * The phone editor's Cancel button: put back the values the edit started + * with, end the edit as a cancel (the caller's onCancel fires), and + * navigate the Settings app's glasses editor out of the edit page. + */ + cancelActiveTextSettingEdit(): void { + if (!this.activeTextSettings.length) return; + this.activeTextSettings.forEach((setting, index) => { + const original = this.activeTextSettingOriginalValues[index] ?? ""; + if (setting.get() !== original) setting.set(original); + }); + const toggle = this.activeTextEditorToggle; + if (toggle && toggle.setting.get() !== this.activeTextEditorToggleOriginalValue) { + toggle.setting.set(this.activeTextEditorToggleOriginalValue); + } + const closesGlassesEditor = this.activeTextSettings.length === 1; + this.endTextSettingEdit(); + if (closesGlassesEditor && this.textEditorHost?.closeTextEditor()) { + this.textEditorHost.requestRender(); + } + } + private updateTextSetting(setting: ConfigSettingString, value: string): void { if (setting.get() !== value) { setting.set(value); @@ -2068,6 +2119,7 @@ class DashboardController { let frameOwned = false; try { const inputEvent = rawInputEventToInputEvent(event); + if (!acceptInput(inputEvent)) return; // The gesture, plus which app is on screen and whether input goes to it, // the sidebar, or a shell overlay. Java only knows the raw event codes, // and without the target the export says what was pressed but not who @@ -2221,8 +2273,7 @@ class DashboardController { reconfigureSurface: (heightMode) => { // Resize the surface rect to the new band; a foreground window stays // visible. The shell re-renders so the chrome (top bar) follows. - const visible = shell.foregroundWindow()?.windowId === windowId; - void this.configureWindowSurface(surfaceId, visible, heightMode); + void this.configureWindowSurface(surfaceId, this.isForegroundWindow(windowId), heightMode); this.requestShellRender(); }, onClosed: () => { @@ -2244,6 +2295,7 @@ class DashboardController { const host = new WorkerAppHost({ appId, worker: createWorker(), + onStopping: () => { if (this.appHosts.get(appId) === host) this.appHosts.delete(appId); }, configureSurface: (surfaceId, visible, heightMode) => this.configureWindowSurface(surfaceId, visible, heightMode), setSurfaceVisible: (surfaceId, visible) => this.setWindowSurfaceVisible(surfaceId, visible), @@ -2374,10 +2426,21 @@ class DashboardController { } } - /** Create/refresh a window surface on the compositor, if a display target exists. */ + /** + * Create/refresh a window surface on the compositor, if a display target exists. + * + * `visible` may be a function, asked only after the surface is configured, + * right when the visibility call is queued. Callers that loop over windows + * or resize one in place must pass one (isForegroundWindow): the configure + * call yields, the foreground can change meanwhile, and a visibility decided + * before the await then lands AFTER the focus change's own calls, since the + * bridge runs Java calls in order. At startup, restoreOpenApps focuses + * windows while connect() registers surfaces, so a foreground read before + * that loop left the old window visible and the new foreground hidden. + */ private async configureWindowSurface( surfaceId: string, - visible: boolean, + visible: boolean | (() => boolean), heightMode: WindowHeightMode = "min", // ensurePreviewDisplay passes its not-yet-published target explicitly. target: DisplayTarget | null = this.display, @@ -2388,7 +2451,12 @@ class DashboardController { zOrder: 0, transparency: "opaque", }); - await target.setSurfaceVisible(surfaceId, visible); + await target.setSurfaceVisible(surfaceId, typeof visible === "function" ? visible() : visible); + } + + /** A late answer to "is this window in front?", for configureWindowSurface. */ + private isForegroundWindow(windowId: string): () => boolean { + return () => shell.foregroundWindow()?.windowId === windowId; } private removeWindowSurface(surfaceId: string): void { @@ -2475,7 +2543,7 @@ class DashboardController { beginRenderPass(!wantFreshData); const paintStartedAtMs = Date.now(); const planes = frameTimings.span(frameId, "paint", () => - frameTimings.runWithFrame(frameId, () => shell.paintSurface()), + frameTimings.runWithFrame(frameId, () => shell.paintScene()), ); const paintMs = Date.now() - paintStartedAtMs; const paintUsedStaleData = endRenderPass(); @@ -2492,32 +2560,7 @@ class DashboardController { frameTimings.finishFrame(frameId, "discarded: shell render with no display target"); return; } - // A shell overlay that dims what it covers (a context menu) must dim the - // window surfaces too, which live below the shell surface in the - // compositor: forward the factor before this frame composites. - const underlayDim = shell.underlayDim(); - if (underlayDim !== this.appliedUnderlayDim) { - this.appliedUnderlayDim = underlayDim; - await display.setUnderlayDim(SHELL_SURFACE_Z_ORDER, underlayDim); - } - const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes)); - const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes)); - const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer()); - const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)); - // Spanned because the bridge serializes Java calls: a frame can sit here - // behind another surface's submission, which is otherwise an unexplained - // jump between the paint spans and the composite. - await frameTimings.spanAsync(frameId, "submit", () => - display.submitSurfaceFrame( - SHELL_SURFACE_ID, - buffer, - { x: 0, y: 0, width: image.width, height: image.height }, - fingerprint, - paintMs, - frameId, - preparedDraws, - ), - ); + await frameTimings.spanAsync(frameId, "submit", () => display.submitShellScene(planes, paintMs, frameId)); // Backpressure: the next shell render waits for this one to reach the // glasses. Timing out here means the loop was blocked for the full timeout // and any input arriving meanwhile had its chrome repaint delayed, so say @@ -2626,7 +2669,7 @@ class DashboardController { } private appendLog(line: string): void { - console.log(`[${formatTimestamp(new Date())}] ${line}`); + //console.log(`[${formatTimestamp(new Date())}] ${line}`); } private setDisplayPreview(preview: ImageSource | null): void { @@ -2643,6 +2686,22 @@ class DashboardController { */ private previewTrailingTimer: ReturnType | null = null; private lastRecordCaptureAtMs = 0; + private phonePreviewVisible: (() => boolean) | null = null; + + /** The main page owns the mirror; other phone pages must not keep it rendering. */ + attachPhonePreview(isVisible: () => boolean): () => void { + this.phonePreviewVisible = isVisible; + this.lastConnectedPreviewUpdateAtMs = 0; + this.updateCompositePreview(); + return () => { + if (this.phonePreviewVisible !== isVisible) return; + this.phonePreviewVisible = null; + if (this.previewTrailingTimer) { + clearTimeout(this.previewTrailingTimer); + this.previewTrailingTimer = null; + } + }; + } /** * Refresh the preview now if the floor allows, otherwise once the floor @@ -2650,6 +2709,7 @@ class DashboardController { * of waiting for the safety-net poll. */ private schedulePreviewUpdate(): void { + if (!this.phonePreviewVisible && !this.screenRecordingActive) return; if (this.previewTrailingTimer) return; const wait = this.lastConnectedPreviewUpdateAtMs + CONNECTED_PREVIEW_MIN_UPDATE_MS - Date.now(); if (wait <= 0) { @@ -2677,6 +2737,13 @@ class DashboardController { return; } this.lastConnectedPreviewUpdateAtMs = now; + // A user-requested GIF is an independent consumer, including while the + // phone is asleep or another page/app is foregrounded. + if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) { + this.lastRecordCaptureAtMs = now; + display.recordScreenFrame(); + } + if (!this.phonePreviewVisible?.()) return; // The connected foreground service intentionally keeps this controller // alive after the phone UI is backgrounded. Do not keep constructing // 640x480 Android Bitmaps for a window that cannot display them: besides @@ -2685,10 +2752,8 @@ class DashboardController { if (global.isAndroid) { const activity = Application.android.foregroundActivity; if (!activity || !activity.hasWindowFocus()) return; - } - if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) { - this.lastRecordCaptureAtMs = now; - display.recordScreenFrame(); + const power = activity.getSystemService(android.content.Context.POWER_SERVICE) as android.os.PowerManager; + if (!power?.isInteractive()) return; } const preview = display.getCompositePreview(previewColorSetting.get() === "green"); if (preview) { diff --git a/app/g2/events.ts b/app/g2/events.ts index 8e6a7ee2..1b49436c 100644 --- a/app/g2/events.ts +++ b/app/g2/events.ts @@ -77,6 +77,8 @@ export const OsEventTypeList = { * this eventType; the glance host treats it as a press. */ HEAD_UP_EVENT: 12, + /** Faceclaw/18: R1 touch-down, before click/hold/slide recognition. */ + RING_PRESS_EVENT: 14, } as const; export const OsEventTypeName: Record = { @@ -93,4 +95,5 @@ export const OsEventTypeName: Record = { 10: "RING_LONG_PRESS_RELEASE_EVENT", 11: "SHORT_THEN_LONG_PRESS_EVENT", 12: "HEAD_UP_EVENT", + 14: "RING_PRESS_EVENT", }; diff --git a/app/g2/firmware-builder.ts b/app/g2/firmware-builder.ts index cd728af0..bff6e2b5 100644 --- a/app/g2/firmware-builder.ts +++ b/app/g2/firmware-builder.ts @@ -1,7 +1,7 @@ /** * Builds the Faceclaw custom firmware on-device: downloads the stock Even * Realities G2 image the patch set was built against (CFW_PATCH_SET.base, - * currently 2.2.9.22) from Even's CDN, verifies its SHA-256, applies + * currently 2.3.0.24) from Even's CDN, verifies its SHA-256, applies * the committed byte-patch set (cfw-patches.ts), extracts the stock EvenHub * fonts for phone-side rendering, verifies the patched SHA-256, and writes the * result to app storage. @@ -23,15 +23,15 @@ import { fetchWithUserAgent } from "../util/http"; import { bytesToHex, hexToBytes as hexToBytesLenient } from "../util/hex-util"; import { EvenHubFont } from "../graphics/evenhub-font"; -declare const com: any; +import { firmwareSha256, writeFirmwareFile } from "../native/firmware-files"; // The CDN names firmware files by MD5. This must be the image whose SHA-256 is // CFW_PATCH_SET.baseSha256 — keep it in sync with FW_URL in g2flash/build_cfw.sh // whenever cfw-patches.ts is regenerated against a new stock base. -// (2.2.9.22 = fc250b05…; the older 2.2.6.10 base was e2873843….) -const FIRMWARE_URL = "https://cdn.evenreal.co/firmware/fc250b05e98a9ff998b4b68f5f99f994.bin"; -// Output names follow the patch set's base name (e.g. g2_2.2.9.22.bin → -// g2_2.2.9.22_cfw.bin) so a rebase can't leave stale version numbers here. +// (2.3.0.24 = 1dbdf37b…; the older 2.2.9.22 base was fc250b05….) +const FIRMWARE_URL = "https://cdn.evenreal.co/firmware/1dbdf37b03a1169c384945e94d671371.bin"; +// Output names follow the patch set's base name (e.g. g2_2.3.0.24.bin → +// g2_2.3.0.24_cfw.bin) so a rebase can't leave stale version numbers here. const STOCK_OUTPUT_FILENAME = CFW_PATCH_SET.base; const CFW_OUTPUT_FILENAME = CFW_PATCH_SET.base.replace(/\.bin$/, "") + "_cfw.bin"; @@ -281,12 +281,12 @@ function tightBuffer(bytes: Uint8Array): ArrayBuffer { return bytes.slice().buffer; } -/** SHA-256 hex digest of an ArrayBuffer, via Android's MessageDigest. */ +/** SHA-256 hex digest using the platform's native crypto implementation. */ function sha256Hex(buffer: ArrayBuffer): string { - return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(buffer)); + return firmwareSha256(buffer); } -/** Write an ArrayBuffer's bytes to `path`, via a native FileChannel write. */ +/** Persist the verified image using the platform's native file API. */ function writeFile(path: string, buffer: ArrayBuffer): void { - com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, buffer); + writeFirmwareFile(path, buffer); } diff --git a/app/g2/firmware-compat.ts b/app/g2/firmware-compat.ts index 7a85eb00..a52a311f 100644 --- a/app/g2/firmware-compat.ts +++ b/app/g2/firmware-compat.ts @@ -26,7 +26,7 @@ export type FirmwareInfo = { }; /** The Faceclaw firmware revision this build of the app needs. */ -export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 13; +export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 35; const FACECLAW_PREFIX = "Faceclaw/"; const LEGACY_PREFIX = "EVENCFW"; @@ -34,9 +34,9 @@ const LEGACY_PREFIX = "EVENCFW"; // The stock firmware release Faceclaw's custom image is built from. Stock at or // below this can be flashed with our patched image; a newer stock version is // unrecognized (its layout may differ from what our patch set targets). -export const BASE_STOCK_VERSION = [2, 2, 9, 22]; +export const BASE_STOCK_VERSION = [2, 3, 0, 24]; export const BASE_STOCK_VERSION_TEXT = BASE_STOCK_VERSION.join("."); -export const VALIDATED_STOCK_VERSION = [2, 2, 10, 10]; +export const VALIDATED_STOCK_VERSION = [2, 3, 0, 24]; export const VALIDATED_STOCK_VERSION_TEXT = VALIDATED_STOCK_VERSION.join("."); export type FirmwareExtension = diff --git a/app/g2/firmware-fonts.ts b/app/g2/firmware-fonts.ts index 58df3769..214f6416 100644 --- a/app/g2/firmware-fonts.ts +++ b/app/g2/firmware-fonts.ts @@ -25,12 +25,13 @@ type FontSpec = { // is built against (CFW_PATCH_SET.base), so they move on every firmware // rebase: run scripts/find-firmware-fonts.py against the new stock .bin and // match rows by cmap count / code-point count (12/8081, 9/9566, 4/11590). +// 2.3.0.24: 0x007b699c, 0x007b69b0, 0x007b68c0 // 2.2.9.22: 0x007a8954, 0x007a8968, 0x007a8878 // 2.2.6.10: 0x0077f414, 0x0077f428, 0x0077f374 const FONT_SPECS: FontSpec[] = [ - { descriptor: 0x007a8954, lineHeight: 27, baseLine: 5 }, - { descriptor: 0x007a8968, lineHeight: 26, baseLine: 5 }, - { descriptor: 0x007a8878, lineHeight: 20, baseLine: 2 }, + { descriptor: 0x007b699c, lineHeight: 27, baseLine: 5 }, + { descriptor: 0x007b69b0, lineHeight: 26, baseLine: 5 }, + { descriptor: 0x007b68c0, lineHeight: 20, baseLine: 2 }, ]; type ExtractedGlyph = { diff --git a/app/g2/firmware-types.ts b/app/g2/firmware-types.ts new file mode 100644 index 00000000..e3503894 --- /dev/null +++ b/app/g2/firmware-types.ts @@ -0,0 +1,13 @@ +export type FlashState = "validating" | "connecting" | "flashing" | "rebooting" | "done" | "error"; + +export type FlashProgress = { + lens: string; + componentIndex: number; + componentCount: number; + blockIndex: number; + blockCount: number; + /** Bytes flashed so far across the whole lens image — the size-weighted progress signal. */ + bytesSent: number; + bytesTotal: number; +}; + diff --git a/app/g2/firmware/cfw-patches.ts b/app/g2/firmware/cfw-patches.ts index 445e4551..1f1b4979 100644 --- a/app/g2/firmware/cfw-patches.ts +++ b/app/g2/firmware/cfw-patches.ts @@ -1,6 +1,6 @@ // AUTO-GENERATED from g2flash/patches/cfw_patches.json — do not edit by hand. // Regenerate with scripts/update_deltas.sh. -// Turns the stock G2 2.2.9.22 image into the Faceclaw custom firmware. +// Turns the stock G2 2.3.0.24 image into the Faceclaw custom firmware. export type FirmwarePatchOp = { offset: number; @@ -17,194 +17,236 @@ export type FirmwarePatchSet = { }; export const CFW_PATCH_SET: FirmwarePatchSet = { - "base": "g2_2.2.9.22.bin", - "baseSha256": "a03fbea9f68a9de6bc271daabb9f3a41c59053d1086622c76a4e990f829cc561", - "outputSha256": "32cea69474252a2b2f9c0fdc5f89bf0d5d6f745b4eec10aea8e9c34e3f106b3f", + "base": "g2_2.3.0.24.bin", + "baseSha256": "187ccf2bcc5c17a212106e8a376745511e8289c4232b634a7ea94b9bf25a0979", + "outputSha256": "d7971b68add0c5187fec81d1cb1ef8cc12322db426a98cb445f1b3a274272817", "patches": [ { - "offset": 935280, + "offset": 1045797, + "old": "38b584b0", + "new": "5df3b9b8", + "desc": "ring receiver entry: timestamped unfiltered reports under framebuffer lease" + }, + { + "offset": 1437929, + "old": "15e0", + "new": "fae7", + "desc": "route ANCS write completions through relay event hook" + }, + { + "offset": 1437923, + "old": "fef788ff", + "new": "fdf2c8fd", + "desc": "cfw_ancs_event" + }, + { + "offset": 1437951, + "old": "fef7e8fb", + "new": "fdf202fb", + "desc": "cfw_ancs_open" + }, + { + "offset": 1437969, + "old": "fef7e7fb", + "new": "fdf219fb", + "desc": "cfw_ancs_close" + }, + { + "offset": 1388387, + "old": "39694d00", + "new": "79637d00", + "desc": "connection-bound ANCS EUS write callback" + }, + { + "offset": 971497, "old": "fff717f8", - "new": "64f395fb", - "desc": "bl cfw_message_bridge_received (ordered private bridge delivery before worker pool)" + "new": "6ff3e9f9", + "desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)" }, { - "offset": 935628, + "offset": 971845, "old": "fef769ff", - "new": "64f3e7fa", - "desc": "bl cfw_message_bridge_received (ordered private bridge delivery before worker pool)" + "new": "6ff33bf9", + "desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)" }, { - "offset": 943818, - "old": "fcf76aff", - "new": "62f3e8fa", - "desc": "bl cfw_message_bridge_received (ordered private bridge delivery before worker pool)" + "offset": 979579, + "old": "fdf74ef8", + "new": "6df320fa", + "desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)" }, { - "offset": 1415004, - "old": "fcf745f8", - "new": "eef23dfb", + "offset": 1428895, + "old": "f8f76cfd", + "new": "fcf238fa", "desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)" }, { - "offset": 1363762, + "offset": 1376071, "old": "7c20", "new": "7d20", "desc": "Set Local Feature: enable LE 2M bit 8" }, { - "offset": 4410298, + "offset": 4468651, "old": "0c001800", "new": "06000600", "desc": "fast connection interval min=max=7.5 ms; latency remains 0" }, { - "offset": 1053266, + "offset": 1057215, "old": "0500", "new": "a325", "desc": "_connectParamReq_impl: force requested mode to fast (0xa3); disables idle slow requests" }, { - "offset": 1124178, + "offset": 1130227, "old": "5ff43432", "new": "5ff43332", "desc": "reserve final 1 KiB of primary TLSF arena for CFW context anchor" }, { - "offset": 1242342, - "old": "d4f790fb", - "new": "1af33cfc", + "offset": 1248487, + "old": "d4f712fb", + "new": "2af302fb", "desc": "bl settings_send_wrapper (append caps field 100)" }, { - "offset": 1240038, - "old": "f5f710f9", - "new": "1af3dafd", + "offset": 1246183, + "old": "f5f7e2f8", + "new": "2af386fc", "desc": "bl settings_decode_wrapper (Faceclaw lease field 101)" }, { - "offset": 939336, - "old": "0bf02af9", - "new": "64f351f8", - "desc": "bl faceclaw_display_start @ 0x45f146 (fail-open wake takeover)" + "offset": 975219, + "old": "02f083fc", + "new": "6cf3f2fc", + "desc": "bl faceclaw_display_start @ 0x467d68 (fail-open wake takeover)" }, { - "offset": 939528, - "old": "0bf0caf8", - "new": "64f3bff8", - "desc": "bl faceclaw_display_start_headup @ 0x45f206 (fail-open wake takeover)" + "offset": 975411, + "old": "02f023fc", + "new": "6cf356fd", + "desc": "bl faceclaw_display_start_headup @ 0x467e28 (fail-open wake takeover)" }, { - "offset": 939016, - "old": "10f096f8", - "new": "65f397fd", + "offset": 975103, + "old": "07f085fb", + "new": "6ef3f2f9", "desc": "bl headup_gate (head-up sensor event -> EvenHub sys event 12 under lease)" }, { - "offset": 939036, - "old": "fff765fb", - "new": "63f3ddff", - "desc": "bl faceclaw_idle_input_gate (idle tap/long/release -> field-102 events 2/3/4)" + "offset": 830549, + "old": "19f03efb", + "new": "91f3a1fb", + "desc": "bl gesture_ring_press_mode (observe ring touch-down, preserve stock dispatch)" }, { - "offset": 831036, - "old": "1df057fc", - "new": "7ff373ff", - "desc": "bl gesture_press (Faceclaw lease gates event 9 vs stock UI event 8)" + "offset": 830795, + "old": "1ff071ff", + "new": "91f35afb", + "desc": "bl gesture_press (owned event 9 before stock menu gate)" }, { - "offset": 831042, - "old": "24f023f9", - "new": "80f314f8", + "offset": 830887, + "old": "1ff043ff", + "new": "91f354fb", "desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)" }, { - "offset": 831800, - "old": "1df0d9fa", - "new": "7ff3f9fd", - "desc": "bl gesture_release (source-qualified event 10 vs stock UI event 0x4a)" + "offset": 831763, + "old": "1ff08dfd", + "new": "91f38af9", + "desc": "bl gesture_release (owned event 10 before stock menu gate)" }, { - "offset": 1553752, + "offset": 1570209, "old": "7fb50600", - "new": "cef2f1bb", + "new": "dbf293be", "desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline" }, { - "offset": 1047796, - "old": "f6f7edff", - "new": "45f32ffd", - "desc": "bl display_copy_hook @ 0x4798f2 (640x480 direct framebuffer)" + "offset": 1051545, + "old": "f6f787f9", + "new": "55f321f9", + "desc": "bl display_copy_hook @ 0x47a78e (640x480 direct framebuffer)" }, { - "offset": 1048112, - "old": "f6f74fff", - "new": "45f391fc", - "desc": "bl display_copy_hook @ 0x479a2e (640x480 direct framebuffer)" + "offset": 1051861, + "old": "f6f7e9f8", + "new": "55f383f8", + "desc": "bl display_copy_hook @ 0x47a8ca (640x480 direct framebuffer)" }, { - "offset": 1255404, - "old": "d9f758ff", - "new": "16f3f9fd", - "desc": "bl faceclaw_send_wear_event @ 0x4ac3ea (outside onboarding)" + "offset": 1262237, + "old": "d9f7d0fd", + "new": "26f343fb", + "desc": "bl faceclaw_send_wear_event @ 0x4ade92 (outside onboarding)" }, { - "offset": 1255504, - "old": "d9f726ff", - "new": "16f3c7fd", - "desc": "bl faceclaw_send_wear_event @ 0x4ac44e (outside onboarding)" + "offset": 1262337, + "old": "d9f79efd", + "new": "26f311fb", + "desc": "bl faceclaw_send_wear_event @ 0x4adef6 (outside onboarding)" }, { - "offset": 1297700, - "old": "65f0affd", - "new": "0cf351f8", + "offset": 1309405, + "old": "67f02dff", + "new": "1af31bfc", "desc": "bl compass_decode_capture (sample-matched GAF diagnostics)" }, { - "offset": 1296176, - "old": "fff759fc", - "new": "0cf3c7fb", + "offset": 1307865, + "old": "fff7c9fb", + "new": "1af399ff", "desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)" }, { - "offset": 4476518, + "offset": 1300937, + "old": "00210170", + "new": "1cf37ffc", + "desc": "bl compass_preserve_accuracy (retain magnetic calibration under Faceclaw framebuffer lease)" + }, + { + "offset": 4537963, "old": "", - "new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fa5b04ef66c6b4ff2a84416460d46ccf2ff0bc2f229044ff6ff7900274ff00108019006e048f2c731c0f24501bff32f8f8847d4f800a0bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0bb804ff4377005f039f8002800f0a1824ff62451cff6ff7142183d3182f8dc7282f8dd7282f8de7282f8df7282f8e07282f8e17282f8e27282f8e37282f8e47282f8e57282f8e67282f8e77282f8e87282f8e97282f8ea7282f8eb7282f8ec7282f8ed7282f8ee7282f8ef7282f8f07282f8f17282f8f27282f8f37282f8f47282f8f57282f8f67282f8f77282f8f87282f8f97282f8fa7282f8fb7282f8fc7282f8fd7282f8fe7282f8ff7282f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382d1c0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001f51457ff436afbff35f8f44e8000121b154e8001f5145f8d02be7aa4645ea064100260127c0f8701206e048f2c731c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0bc804ff4377004f05eff002800f0c6814ff62451cff6ff7100bf42183d3182f8dc6282f8dd6282f8de6282f8df6282f8e06282f8e16282f8e26282f8e36282f8e46282f8e56282f8e66282f8e76282f8e86282f8e96282f8ea6282f8eb6282f8ec6282f8ed6282f8ee6282f8ef6282f8f06282f8f16282f8f26282f8f36282f8f46282f8f56282f8f66282f8f76282f8f86282f8f96282f8fa6282f8fb6282f8fc6282f8fd6282f8fe6282f8ff6282f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382d1c0f800b08772a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff437afbff35f8f00bf44e8000121b154e8001fa942f8d02be7d0f8746242f6f767c0f24407002ebff35f8f00f0eb8007f19a0230464ff0ff314ff0ff379047002840f0418143f67750c0f24f00804700274ff001080397adf8107006e048f2c731c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0e1804ff4377004f068fe002800f0d9804ff62451cff6ff7100bf42183d3182f8dc7282f8dd7282f8de7282f8df7282f8e07282f8e17282f8e27282f8e37282f8e47282f8e57282f8e67282f8e77282f8e87282f8e97282f8ea7282f8eb7282f8ec7282f8ed7282f8ee7282f8ef7282f8f07282f8f17282f8f27282f8f37282f8f47282f8f57282f8f67282f8f77282f8f87282f8f97282f8fa7282f8fb7282f8fc7282f8fd7282f8fe7282f8ff7282f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382d1c0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff436afbff35f8f44e8000149b354e8001fa942f8d02be705460020b847b0b105f51d7151e8006f4eb9bff35f8f00bf41e8000292b151e8006f002ef8d00021bff32f8fbff35f8f21b10646f9e64ff0ff3742e007f5a9718847f2e60121f1e70020019d42f6f764002d4ff0010bc0f2440418bfbaf1000f33d10df108098046484602f07df903ab28465146012205f0abfc9df8111007464846002918bf4ff0ff3702f0a3fabbf1000f044608d19df8100028b949f28340c0f247004c308047b8f1000f18bfc8f80c4042f6f764c0f2440404f5807130468847384625b0bde8f08f2a7802f07f01a1f1150313f1030f0bd802f07b030b2b07d002f07e02082a03d0032901d00629b7d140b149f28341c0f247010746884797f8641111b14ff0ff37d6e738464ff0000ba6e770b500284dd001684ef66c62ccf2ff02914218bf70bd90f8441090f8455046f6756c8d42c0f2510c2ad205eb8501014491f8492091f84a4091f846e091f8473091f8481042ea0424012c4ff0010298bf14466e1c054680f84560b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f84410bde870406047e047286c38b142f64d42c0f244022146bde87040104770bd02464ff64b70cff6ff7042f6653c7844c0f2440c00210023604700bf2de9f04f9fb04ff2a840c2f229000668bff35f8f30466ff39600b0f1005f42d130684ef66c65ccf2ff05a8423bd196f8640130b3d6f860411cb340f6b400c2f20000076804a802f0a5f8002f00f06a8140f2030a384621464ff416320296cff6807a4ff6ff760397fff752fb4ff6245b48f2c738cff6ff7b00270124c0f245081ce096f8660170b1d6f8680140b146f68051c2f207010968401a002802f37080002086f8660140f6d100c0f247001fb0bde8f04f0047bff32f8fc0474ff2a840c2f22900d0f80090bff35f8f0068bff35f8f00ea0a01b1f1005f03d10168a94200f0aa804ff4377004f050fc002802f02f80594642183d3182f8dc7282f8dd7282f8de7282f8df7282f8e07282f8e17282f8e27282f8e37282f8e47282f8e57282f8e67282f8e77282f8e87282f8e97282f8ea7282f8eb7282f8ec7282f8ed7282f8ee7282f8ef7282f8f07282f8f17282f8f27282f8f37282f8f47282f8f57282f8f67282f8f77282f8f87282f8f97282f8fa7282f8fb7282f8fc7282f8fd7282f8fe7282f8ff7282f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382d14ff2a842056084728683c683068446848684c684068546858685c685068646868686c68606874687c2f2290252e8001f49457ff449afbff35f8f42e8000121b152e8001f4945f8d03ee7817a002941f08887027e0021002a8df81c107cd04ff2a84507aac2f22905535c002b00f0b68053185f78002f74d09f78002f73d0df78002f72d01f79002f71d05f79002f70d09f79002f6fd0df79002f6ed01f7a002f6dd05f7a002f6cd09f7a002f6bd0df7a002f6ad01f7b002f69d05f7b002f68d09f7b002f67d0df7b002f66d01f7c002f65d05f7c002f64d09f7c002f63d0df7c002f62d01f7d002f61d05f7d002f60d09f7d002f5fd0df7d002f5ed01f7e002f5dd05f7e002f5cd09f7e002f5bd0df7e002f5ad01f7f002f59d05f7f002f58d09f7f002f57d0db7f002b56d0203160297ff49aaf76e0002086f86401c6f8600186f86601012086f8650140f6d100c0f24700804701f019bf4ff2a845c2f2290564e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2921d84ff0520353541dd00a4445235e29537018d04f235d29937014d052235c29d37010d044235b2913710cd045235a29537108d052235929937104d020235829d37141f0d68300218df87b100121427e002a00f0c280002207ab9f5c002f00f0a0809f187e78002e5ed0be78002e5dd0fe78002e5cd03e79002e5bd07e79002e5ad0be79002e59d0fe79002e58d03e7a002e57d07e7a002e56d0be7a002e55d0fe7a002e54d03e7b002e53d07e7b002e52d0be7b002e51d0fe7b002e50d03e7c002e4fd07e7c002e4ed0be7c002e4dd0fe7c002e4cd03e7d002e4bd07e7d002e4ad0be7d002e49d0fe7d002e48d03e7e002e47d07e7e002e46d0be7e002e45d0fe7e002e44d03e7f002e43d07e7f002e42d0be7f002e41d0ff7f002f40d02032602a7ff49aaf54e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a15d84ff053079f5411d013444b275e2a5f700cd049275d2a9f7008d050275c2adf7004d020275b2a1f7140f0f08100228df87b200131827e002a00f0be80002107aa535c002b00f0a08053185f78002f5ed09f78002f5dd0df78002f5cd01f79002f5bd05f79002f5ad09f79002f59d0df79002f58d01f7a002f57d05f7a002f56d09f7a002f55d0df7a002f54d01f7b002f53d05f7b002f52d09f7b002f51d0df7b002f50d01f7c002f4fd05f7c002f4ed09f7c002f4dd0df7c002f4cd01f7d002f4bd05f7d002f4ad09f7d002f49d0df7d002f48d01f7e002f47d05f7e002f46d09f7e002f45d0df7e002f44d01f7f002f43d05f7f002f42d09f7f002f41d0db7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2911d84ff0440353540dd00a4455235e29537008d050235d29937004d020235c29d37040f0098100218df87b1001216a6846f23f53c5f28803b3eb520f40f08b80d20700f08d80002107aa535c002b00f0858153185f78002f00f0f9809f78002f00f0f980df78002f00f0f9801f79002f00f0f9805f79002f00f0f9809f79002f00f0f980df79002f00f0f9801f7a002f00f0fc805f7a002f00f0fc809f7a002f00f0fc80df7a002f00f0fc801f7b002f00f0fc805f7b002f00f0fc809f7b002f00f0fc80df7b002f00f0fc801f7c002f00f0fc805f7c002f00f0fc809f7c002f00f0fc80df7c002f00f0fc801f7d002f00f0fc805f7d002f00f0fc809f7d002f00f0fc80df7d002f00f0fc801f7e002f00f0fc805f7e002f00f0fc809f7e002f00f0fc80df7e002f00f0fc801f7f002f00f00d815f7f002f00f00b819f7f002f00f00981db7f002b00f00781203160297ff47baf1de14cf67e22caf210126a60002940f01981002107aa535c002b00f0dc8053185f78002f61d09f78002f60d0df78002f5fd01f79002f5ed05f79002f5fd09f79002f60d0df79002f61d01f7a002f62d05f7a002f63d09f7a002f64d0df7a002f68d01f7b002f69d05f7b002f6ad09f7b002f6bd0df7b002f6cd01f7c002f6dd05f7c002f6ed09f7c002f6fd0df7c002f70d01f7d002f71d05f7d002f72d09f7d002f73d0df7d002f74d01f7e002f75d05f7e002f76d09f7e002f77d0df7e002f78d01f7f002f79d05f7f002f7ad09f7f002f7bd0db7f002b7cd0203160297ff49aaface000211171f6e6013173e0023171e003316fe004316de0013184e0053169e0023180e0063165e003317ce0073161e0043178e008315de0053174e0093159e0063170e00a3155e007316ce000225a710fe60b314ee0083165e00c314ae0093161e00d3146e00a315de00e3142e00b3159e00f313ee00c3155e010313ae00d3151e0113136e00e314de0123132e00f3149e013312ee0103145e014312ae0113141e0153126e012313de0163122e0133139e017311ee0143135e018311ae0153131e0193116e016312de01a3112e0173129e01b310ee0183125e01c310ae0193121e01d3106e01a311de01e3102e01b3119e01f315f2931d84ff04f0353542dd00a444b235e29537028d020235d29937024d00021d17024e01c3104e01d3102e01e3100e01f315f2918d84ff04103535414d00a444c235e2953700fd05d2993700cd04f235c29d37008d043235b29137104d020235a29537141f0948000218df87b1000210df11c0c1cf80120002a00f0a1800ceb01025378002b5ed09378002b5dd0d378002b5cd01379002b5bd05379002b5ad09379002b59d0d379002b58d0137a002b57d0537a002b56d0937a002b55d0d37a002b54d0137b002b53d0537b002b52d0937b002b51d0d37b002b50d0137c002b4fd0537c002b4ed0937c002b4dd0d37c002b4cd0137d002b4bd0537d002b4ad0937d002b49d0d37d002b48d0137e002b47d0537e002b46d0937e002b45d0d37e002b44d0137f002b43d0537f002b42d0937f002b41d0d27f002a40d0203160297ff498af43e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d84ff077020cf8012003d100218df87b1002e0614400224a70c2684ff0000e1cf80e10002900f0c4800ceb0e035978002962d09978002962d0d978002962d01979002962d05979002962d09979002962d0d979002962d0197a002962d0597a002962d0997a002962d0d97a002962d0197b002962d0597b002962d0997b002962d0d97b002962d0197c002962d0597c002962d0997c002962d0d97c002962d0197d002962d0597d002962d0997d002962d0d97d002962d0197e002962d0597e002962d0997e002962d0d97e002962d0197f002962d0597f002962d0997f002962d0d97f002962d00ef1200ebef1600f7ff496af4ff0600e5be00ef1010e58e00ef1020e55e00ef1030e52e00ef1040e4fe00ef1050e4ce00ef1060e49e00ef1070e46e00ef1080e43e00ef1090e40e00ef10a0e3de00ef10b0e3ae00ef10c0e37e00ef10d0e34e00ef10e0e31e00ef10f0e2ee00ef1100e2be00ef1110e28e00ef1120e25e00ef1130e22e00ef1140e1fe00ef1150e1ce00ef1160e19e00ef1170e16e00ef1180e13e00ef1190e10e00ef11a0e0de00ef11b0e0ae00ef11c0e07e00ef11d0e04e00ef11e0e01e00ef11f0e01230a2a1ed364274ff47a7642f2107503eb83014900b2fbf1f40a2c11d303fb07f1b2fbf1f40a2c0bd303fb06f1b2fbf1f40a2c05d36b43b2fbf3f10929e7d800e00b46f146bef15f0f98bf4ff05f09c9f1000818eb0e0b52d0b2fbf3f40a2b04f130010cf80e104cf6cd41ccf6cc41a3fb011646d31bf1010141d004fb1324f708b4fbf7fa0ceb0e020af13001517048f21f51c5f2eb11642ba3fb011634d31bf102012cd00afb17447709b4fbf7f6b3f57a7f06f13001917044f6d351c1f26201a3fb015123d31bf1030518d006fb1744890941f25976b4fbf1f5cdf2b716a3fb066705f13006d67005fb114242f210710ef1040e8b424fea5733aad209e0ce4607e00ef1010e04e00ef1020e01e00ef1030e0022e3460cf80e201bf80210002900f0a1800beb02014b78002b5ed08b78002b5dd0cb78002b5cd00b79002b5bd04b79002b5ad08b79002b59d0cb79002b58d00b7a002b57d04b7a002b56d08b7a002b55d0cb7a002b54d00b7b002b53d04b7b002b52d08b7b002b51d0cb7b002b50d00b7c002b4fd04b7c002b4ed08b7c002b4dd0cb7c002b4cd00b7d002b4bd04b7d002b4ad08b7d002b49d0cb7d002b48d00b7e002b47d04b7e002b46d08b7e002b45d0cb7e002b44d00b7f002b43d04b7f002b42d08b7f002b41d0c97f002940d02032602a7ff498af52e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a13d84ff075010bf802100ed00beb020173235e2a4b7008d020235d2a8b7004d070235c2acb7040f09d8000218df87b1002694ff000081bf80810002900f0c7800beb08035978002962d09978002962d0d978002962d01979002962d05979002962d09979002962d0d979002962d0197a002962d0597a002962d0997a002962d0d97a002962d0197b002962d0597b002962d0997b002962d0d97b002965d0197c002965d0597c002965d0997c002965d0d97c002965d0197d002965d0597d002965d0997d002965d0d97d002965d0197e002965d0597e002965d0997e002965d0d97e002965d0197f002965d0597f002965d0997f002965d0d97f002965d008f12008b8f1600f7ff496af4ff060085ee008f101085be008f1020858e008f1030855e008f1040852e008f105084fe008f106084ce008f1070849e008f1080846e008f1090843e008f10a0840e008f10b083de008f10c083ae008f10d0837e008f10e0834e000220a7162e708f10f082ee008f110082be008f1110828e008f1120825e008f1130822e008f114081fe008f115081ce008f1160819e008f1170816e008f1180813e008f1190810e008f11a080de008f11b080ae008f11c0807e008f11d0804e008f11e0801e008f11f0801230a2a1ed364274ff47a7642f2107503eb83014900b2fbf1f40a2c11d303fb07f1b2fbf1f40a2c0bd303fb06f1b2fbf1f40a2c05d36b43b2fbf3f10929e7d800e00b46c646b8f15f0f98bf4ff05f0ecef1000a42f2107c1aeb080950d0b2fbf3f40a2b04f130010bf808104cf6cd41ccf6cc41a3fb011644d319f101013fd004fb1324f708b4fbf7f60beb080206f13001517048f21f51c5f2eb11642ba3fb015132d319f102052ad006fb17444e09b4fbf6f7b3f57a7f07f13001917044f6d351c1f26201a3fb015121d319f1030516d007fb1644890941f25976b4fbf1f5cdf2b716a3fb066705f13006d67005fb114208f1040863454fea5733acd209e0f04607e008f1010804e008f1020801e008f10308039f00220bf808201bf80210002900f0a1800beb02014b78002b5ed08b78002b5dd0cb78002b5cd00b79002b5bd04b79002b5ad08b79002b59d0cb79002b58d00b7a002b57d04b7a002b56d08b7a002b55d0cb7a002b54d00b7b002b53d04b7b002b52d08b7b002b51d0cb7b002b50d00b7c002b4fd04b7c002b4ed08b7c002b4dd0cb7c002b4cd00b7d002b4bd04b7d002b4ad08b7d002b49d0cb7d002b48d00b7e002b47d04b7e002b46d08b7e002b45d0cb7e002b44d00b7f002b43d04b7f002b42d08b7f002b41d0c97f002940d02032602a7ff498af4ae001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a0bd84ff075010bf8021006d00beb020173235e2a4b7040f08780044600208df87b00384602215a4608f09ef8d4f870c272208df81c0078208df81d0020204ff000091ffa8cf28df81e008df81f901bf80900002800f0c8800beb09035878002866d09878002866d0d878002866d01879002866d05879002866d09879002866d0d879002866d0187a002866d0587a002866d0987a002866d0d87a002866d0187b002866d0587b002866d0987b002866d0d87b002866d0187c002866d0587c002866d0987c002866d0d87c002866d0187d002866d0587d002866d0987d002866d0d87d002866d0187e002866d0587e002866d0987e002866d0d87e002866d0187f002866d0587f002866d0987f002866d0d87f002866d009f12009b9f1600f7ff496af4ff060095fe004460020887078e709f1010958e009f1020955e009f1030952e009f104094fe009f105094ce009f1060949e009f1070946e009f1080943e009f1090940e009f10a093de009f10b093ae009f10c0937e009f10d0934e009f10e0931e009f10f092ee009f110092be009f1110928e009f1120925e009f1130922e009f114091fe009f115091ce009f1160919e009f1170916e009f1180913e009f1190910e009f11a090de009f11b090ae009f11c0907e009f11d0904e009f11e0901e009f11f0901230a2a1ed364274ff47a7642f2107503eb83004000b2fbf0f40a2c11d303fb07f0b2fbf0f40a2c0bd303fb06f0b2fbf0f40a2c05d36b43b2fbf3f00928e7d800e00346ca46b9f15f0f98bf4ff05f0acaf1000842f2107e18eb09054dd0b2fbf3f04cf6cd4100f13004ccf6cc410bf809400a2ba3fb016441d36e1c3dd000fb1320e408b0fbf4f748f21f5107f130060beb0902c5f2eb115670642ba3fb011630d3a91c29d007fb14007409b0fbf4f7b3f57a7f07f13001917044f6d351c1f26201a3fb011620d3e91c16d007fb1400b10941f25975b0fbf1f4cdf2b715a3fb055604f13005d57004fb110209f1040973454fea5633afd209e0d14607e009f1010904e009f1020901e009f1030900220bf809201bf80200002800f0a1800beb0201487800285ed0887800285dd0c87800285cd0087900285bd0487900285ad08879002859d0c879002858d0087a002857d0487a002856d0887a002855d0c87a002854d0087b002853d0487b002852d0887b002851d0c87b002850d0087c00284fd0487c00284ed0887c00284dd0c87c00284cd0087d00284bd0487d00284ad0887d002849d0c87d002848d0087e002847d0487e002846d0887e002845d0c87e002844d0087f002843d0487f002842d0887f002841d0c87f002840d02032602a7ff498af56e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a17d84ff020000bf8020012d00beb020163205e2a48700cd072205d2a887008d063205c2ac87004d020205b2a087140f0668100208df87b0037214fea1c7001eb1c71bcf1204f38bf00f13001ccf303608df8171000f137010a2838bf00f13001ccf303508df8181000f137010a2838bf00f13001ccf303408df8191000f137010a2838bf00f1300100208df81b001f208df81a100beb000111f81f2c002a00f0818011f81e2c002a7ed011f81d2c002a7cd011f81c2c002a7ad011f81b2c002a78d011f81a2c002a76d011f8192c002a74d011f8182c002a72d011f8172c002a70d011f8162c002a6ed011f8152c002a6cd011f8142c002a6ad011f8132c002a68d011f8122c002a66d011f8112c002a64d011f8102c002a62d011f80f2c002a60d011f80e2c002a5ed011f80d2c002a5cd011f80c2c002a5ad011f80b2c002a58d011f80a2c002a56d011f8092c002a54d011f8082c002a52d011f8072c002a50d011f8062c002a4ed011f8052c002a4cd011f8042c002a4ad011f8032c002a48d011f8022c002a46d011f8011c002944d01bf80010002941d020307f287ff479af61e01f383ae01e3838e01d3836e01c3834e01b3832e01a3830e019382ee018382ce017382ae0163828e0153826e0143824e0133822e0123820e011381ee010381ce00f381ae00e3818e00d3816e00c3814e00b3812e00a3810e009380ee008380ce007380ae0063808e0053806e0043804e0033802e0023800e001385f2822d8c0f15d0ec0f15e02c0f15f03c0f1600c584400260df11705af5d8755b7b1b34211d0af1979788419617079b1b2420ad0b978a17051b1b64505d0f978e17029b10436b445e8d100208df87b00039842f2a00845f2a80440f2403507aa0e21c2f22008c2f21304c2f2000507f047fde869ae46a0425fd1def82800b0f54d2f5ad1def82c00a04256d145f61c530020c2f213034cf6394508f1040c002763454ad81c6834f003061cd098454ff0ff3228bf0c2c42d3a8eb03018e424ff0ff313ed83946be4288bf3146e2071cbf0f4630449919013d01f10403e0d12ce00020487199e60421b7f5807f1ed300223e46b6f5007f14d3b6f5806f13d3b6f5006f12d30901b2eb163f4fea1616f0d10ce000219171fef76bbf00211172fef729bc490002e0890000e0c900634504d1494207ea0102014603e04ff0ff324ff0ff3146f68050c2f20700d0f8880008f1080342f6a00c9842c2f2270c29d142f61c574ff00008c2f2200746f6b9750cf1040b00264ff0ff394ff0ff3a5f4518d83c6834f003005cd0bc4528bf0c2c12d3aceb0703984210d8b042334688bf0346e4071cbf1e4680443844013d00f10407e4d14ff0ff394ff0ff3adef800300cf10800834240f08b80def80c30b3f5333f40f08580def81030834240f080804ff2a84043f21c53c2f229004ff0000bc2f2270342f6f934a0f1040ca0f1080e00264ff0ff3863456ad81d6835f0030730d09e454ff0ff30019028bf0c2d62d3aeeb0300874260d83046b74288bf3846ed071cbf0646bb44d819013c00f10403e1d14de073460425b6f5807f2dd34ff0000e3046b0f5007f22d3b0f5806f21d3b0f5006f20d32d01beeb103f4fea1010f0d11ae00425b6f5807f29d300273446b4f5007f1fd3b4f5806f1ed3b4f5006f1dd32d01b7eb143f4fea1414f0d117e06d0002e0ad0000e0ed005f4505d1684206ea0009c2469e4680e74ff0ff394ff0ff3a9e467ae76d0002e0ad0000e0ed00634505d16b4206ea03000190d84604e04ff0ff3001904ff0ff3866208df81c0072208df81d0065208df81e008df81f002f208df820006d204b238df8210061208df8253069238df8220078208df82630422356278df8230020208df827303a238df82b70472707ac8df824008df828308df829004c238df82c708df82e00002720468df82a308df82d308df82f7007f04efde05d002800f0a080e019417800295ed0817800295dd0c17800295cd0017900295bd0417900295ad08179002959d0c179002958d0017a002957d0417a002956d0817a002955d0c17a002954d0017b002953d0417b002952d0817b002951d0c17b002950d0017c00294fd0417c00294ed0817c00294dd0c17c00294cd0017d00294bd0417d00294ad0817d002949d0c17d002948d0017e002947d0417e002946d0817e002945d0c17e002944d0017f002943d0417f002942d0817f002941d0c07f002840d02037602f7ff49aaf64e001373ae0023738e0033736e0043734e0053732e0063730e007372ee008372ce009372ae00a3728e00b3726e00c3724e00d3722e00e3720e00f371ee010371ce011371ae0123718e0133716e0143714e0153712e0163710e017370ee018370ce019370ae01a3708e01b3706e01c3704e01d3702e01e3700e01f375f2f25d84ff02000e05521d0e01945215e2f41701cd076215d2f817018d065215c2fc17014d06e215b2f017110d048215a2f41710cd07521592f817108d06221582fc17104d02021572f017240f0f88000208df87b00204651464a4607f07afc0020215c002900f0a08021184a78002a5ed08a78002a5dd0ca78002a5cd00a79002a5bd04a79002a5ad08a79002a59d0ca79002a58d00a7a002a57d04a7a002a56d08a7a002a55d0ca7a002a54d00a7b002a53d04a7b002a52d08a7b002a51d0ca7b002a50d00a7c002a4fd04a7c002a4ed08a7c002a4dd0ca7c002a4cd00a7d002a4bd04a7d002a4ad08a7d002a49d0ca7d002a48d00a7e002a47d04a7e002a46d08a7e002a45d0ca7e002a44d00a7f002a43d04a7f002a42d08a7f002a41d0c97f002940d0203060287ff49aaf5be001303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f281cd84ff02001215418d021184f225e284a7013d074225d288a700fd068225c28ca700bd065225b280a7107d072225a284a7103d0202259288a7128d100208df87b0007ac019a2046414607f0adfb03981a21224607f02afa029e002086f86401c6f8600103984cf6036107904ff41630089007a8c0f247018847012086f8660104a800f041f930611fb0bde8f08f0020c871d6e70021417207e700bf2de9f04f81b0009041f204024cf65560cef20002ccf2ac50c2f8ac0f4ef6fc50cef2000001684ef66c6641f08071016052f8040c4ff2a8474ff6245a48f2c738ccf2ff06c2f229074ff6ff7440f00100cff6ff7a00254ff0010bc0f2450842f8040c02e0bff32f8fc047d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168b14200f0a8804ff4377002f0b2fb002800f0de80514642183d3182f8dc5282f8dd5282f8de5282f8df5282f8e05282f8e15282f8e25282f8e35282f8e45282f8e55282f8e65282f8e75282f8e85282f8e95282f8ea5282f8eb5282f8ec5282f8ed5282f8ee5282f8ef5282f8f05282f8f15282f8f25282f8f35282f8f45282f8f55282f8f65282f8f75282f8f85282f8f95282f8fa5282f8fb5282f8fc5282f8fd5282f8fe5282f8ff5282f8005382f8015382f8025382f8035382f8045382f8055382f8065382f8075382f8085382f8095382f80a5382f80b5382f80c5382f80d5382f80e5382f80f5382f8105382f8115382f8125382f8135382f8145382f8155382f8165382f8175382f8185382d1066080f80ab08483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff44fafbff35f8f00bf47e8000121b157e8001f4945f8d043e74169d1bb46f6805245f6e466c2f20702cff6f8761368314617689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f421ed041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94206d041f20401cef200010968c91a416141f20400cef2000000680099086001b0bde8f08f00bf2de9f04f81b041f20401cef200014ef66c674ff2a846d1f8009000684ff6245bccf2ff07c2f229064ff6ff74cff6ff7b00254ff00108009007e000bf48f2c731c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168b94200f0a8804ff4377002f08cfa002800f0fc80594642183d3182f8dc5282f8dd5282f8de5282f8df5282f8e05282f8e15282f8e25282f8e35282f8e45282f8e55282f8e65282f8e75282f8e85282f8e95282f8ea5282f8eb5282f8ec5282f8ed5282f8ee5282f8ef5282f8f05282f8f15282f8f25282f8f35282f8f45282f8f55282f8f65282f8f75282f8f85282f8f95282f8fa5282f8fb5282f8fc5282f8fd5282f8fe5282f8ff5282f8005382f8015382f8025382f8035382f8045382f8055382f8065382f8075382f8085382f8095382f80a5382f80b5382f80c5382f80d5382f80e5382f80f5382f8105382f8115382f8125382f8135382f8145382f8155382f8165382f8175382f8185382d1076080f80a808483c483048444848484c484048544858485c485048644868486c4860487448756e8001f51457ff44bafbff35f8f00bf46e8000121b156e8001f5145f8d03fe74169009d61b144f6d350c1f26200a1fb0001880901284ff0010088bf88094ce046f6805245f6e466c2f20702cff6f8761368314617689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f422fd041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94217d041f20401cef20001096844f6d357ca1ac1f26207a2fb07764261b00901284ff0010088bfb009994208bffa2001e0009dfa20a9eb0501b1fbf0f001b0bde8f08f002858d1002956d0082a54d30b78aa2b51d18b79f02b4ed12de9f04f83b00b2a4ed3c878a2f10803834249d14878212848d10879012845d14879012842d1c87900283fd10c4614f8080f10f03c0f39d18818884610f8021c10f8010c164641ea0025a2f10a0081b2204606f0affd854228d14cf6dd70c0f245008047811eb1fa81f14f09012808bf0227002f00f02b81207887f00301084200f02781012039464246334600f06cfa0446002898f8080018bf06241ae14ff2e933c0f24c0318470b2400e00a244cf6dd70c0f245008047811eb1fa81f14d09012808bf0225002d00f0fd804ef66c6a4ff2a8474ff624580294ccf2ff0ac2f229074ff6ff74cff6ff7800264ff0010b06e048f2c731c0f24501bff32f8f8847d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168514500f0a9804ff4377002f0e3f8002800f0ca80414600bf42183d3182f8dc6282f8dd6282f8de6282f8df6282f8e06282f8e16282f8e26282f8e36282f8e46282f8e56282f8e66282f8e76282f8e86282f8e96282f8ea6282f8eb6282f8ec6282f8ed6282f8ee6282f8ef6282f8f06282f8f16282f8f26282f8f36282f8f46282f8f56282f8f66282f8f76282f8f86282f8f96282f8fa6282f8fb6282f8fc6282f8fd6282f8fe6282f8ff6282f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382d1c0f800a080f80ab08483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff449afbff35f8f47e8000121b157e8001f4945f8d03ee705eb450100eb011090f85d1201b300f513760024304629462a460323009406f047fd706840b146f22b11c0f25d018847706806f0b7fb74603068b475002818bf06f0b0fb0020029c3060b060308200e0029c204603b0bde8f08f0624f9e700243842f6d0404631463a463b4600f064f9002818bf0446ece70b291dd3c278a1f10803934218d10278aa2a13d14278212a10d10279012a0dd14279012a0ad18279f02a07d1c2792ab9024612f8083f13f03c0f03d00a2070470b20704710b5084410f8023c10f8010c43ea0024a1f10a0081b2104606f034fc201a18bf0a2010bd002808bf7047b0b582b00446407cd0b1002520460323009506f0dafc606840b146f22b11c0f25d018847606806f04afb65602068a575002818bf06f043fb00202060a060208202b0bde8b040704700bf2de9f04f81b04ef66c654ff2a8464ff624598046ccf2ff05c2f229064ff6ff77cff6ff7900244ff0010a06e048f2c731c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168a94200f0a9804ff4377001f074ff002800f0a180494642183d3182f8dc4282f8dd4282f8de4282f8df4282f8e04282f8e14282f8e24282f8e34282f8e44282f8e54282f8e64282f8e74282f8e84282f8e94282f8ea4282f8eb4282f8ec4282f8ed4282f8ee4282f8ef4282f8f04282f8f14282f8f24282f8f34282f8f44282f8f54282f8f64282f8f74282f8f84282f8f94282f8fa4282f8fb4282f8fc4282f8fd4282f8fe4282f8ff4282f8004382f8014382f8024382f8034382f8044382f8054382f8064382f8074382f8084382f8094382f80a4382f80b4382f80c4382f80d4382f80e4382f80f4382f8104382f8114382f8124382f8134382f8144382f8154382f8164382f8174382f8184382d1056080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f59457ff44bafbff35f8f00bf46e8000129b156e8001f5945f8d03fe7002008eb480100eb0111002818bf01f5137001b0bde8f08fb0b5c4b0dc08202c03d94ff0ff3044b0b0bd0df107048df807008df80810a01c11461a461d46fcf771fda81c4af28d5582b2c0f24605f02021460023a84744b0b0bd00bf2de9f04f8bb04ef66c654ff2a8464ff6245948f2c73bccf2ff05c2f229064ff6ff77cff6ff7900244ff0010ac0f2450bcde90612cde9080303e000bfbff32f8fd847d6f80080bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168a94200f0a8804ff4377001f06cfe002800f04484494642183d3182f8dc4282f8dd4282f8de4282f8df4282f8e04282f8e14282f8e24282f8e34282f8e44282f8e54282f8e64282f8e74282f8e84282f8e94282f8ea4282f8eb4282f8ec4282f8ed4282f8ee4282f8ef4282f8f04282f8f14282f8f24282f8f34282f8f44282f8f54282f8f64282f8f74282f8f84282f8f94282f8fa4282f8fb4282f8fc4282f8fd4282f8fe4282f8ff4282f8004382f8014382f8024382f8034382f8044382f8054382f8064382f8074382f8084382f8094382f80a4382f80b4382f80c4382f80d4382f80e4382f80f4382f8104382f8114382f8124382f8134382f8144382f8154382f8164382f8174382f8184382d1056080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f41457ff44fafbff35f8f00bf46e8000121b156e8001f4145f8d043e70999089d01eb410100eb011095f9081000f51376b1f1ff3f01f0030409dd374617f8110fd0bb3068002818bf06f044f92ee3307cf0b1707ce0b10799099a002730460323009706f0bdfa706840b146f22b11c0f25d018847706806f02df977603068b775002818bf06f026f900203060b06030823068002818bf06f01df900204ff480713060b0603182a978b081092006f11107f174b17434757076b07ca978884240f0da82317da14240f0d682069b411ca3f10b00727e02971e2828bf1e209042b17488bf7076981e84b200270a2c4ff00008c0f0e682092241f221090920039419e030684f46002818bf06f0e4f8b089376001303774b760b081002c18bf4ff00608039cd146ddf814a00aeb0b0082b2a24280f0c682317c80b20429824600f28880aa5c00f1010a4b1c0129337405dd022906d0032970d1f28106e021b9727503e0338943ea02223281a24580f0a98204296ed015f80a204ff0000c831c8f1c0129377406ddff290bd0032967460dd19a4652e000291cbf378947ea0222328100e072756746a34204d38ae2f281a34280f08782022936d8ea5c00f1030acb1c0129337407dd022937d0fe2907d17275a2450cd375e231b9338943ea02223281a24504d36de2f281a24580f06a8202292fd015f80a20031d41f00407fc29377408ddfd2909d0fe291cbf378947ea0222328103e079b9f28100e072756746a34280f04f8209b19a4613e0ea5c00f1050a0520307401e09a466746f189308941ea0221f18128b106f050f80028306000f0698233897089191a1ffa81fb1ffa8af1621a5a45b8bf93465fea0b4208d03268294410441ffa8bf2fcf75bfb70893389584481b2994270817ff44faf0025cdf81080d6f800808df82b50707db8f1000f06930df12b0108bf88460f2806d8317d00f00302511ab1fa81f14d09000706d4317e707e814238bf30760fe000bf706840b146f22b11c0f25d018847706805f0faff7760707e30760120f082cdf814a09db1707db57d400714d4002d4ff0000400f0888106984ff6ff7381b2002900f00d8101295ed10020d2e0707d400753d4002477e14ff4803005f0dfff002d804600f06f81b07d002800f06b81b8f1000f00f067817568002d00f0ff8030890428c0f05f8131680a1812f8043c002b40f0588112f8033c002b40f0538112f8023cff2b40f04e8112f8012cff2a40f049814ff4803429606860c5f80c802c610028019000f0668146f22b10c0f25d0000f58572284600219047002840f032812969002900f02e8168688c420c46e7d1019a0c468242e3d124e14ff4803005f08dff04461fe1069a4ff6fe701040a44642426ff0010000bf08eb0005ac78ed7883ea04231cb25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead73787ea430383ea05231fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead737023087ea430309ead5371518023587ea430395d1023000276446069ad20732d018f8000083ea002002b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead33282ea4003f0899ab282426fd14046fcf7d0fa00286ad141f2210a4ff00009012375e0382005f0cdfe00285ed00546077047708770c770077147718771c771077247728772c772077347738773c773077447748774c774077547758775c775077647768776c776077747778777c77780f8287080f8297080f82a7080f82b7080f82c7080f82d7080f82e7080f82f7080f8307080f8317080f8327080f8337080f8347080f8357080f8367080f8377046f28510c0f200007844286246f28d10c0f200007844686246f22b10c0f25d0047f2f85200f13c0c28460f21c0f27b023823e04740b3284605f062fe4446706841f2210a40b146f22b11c0f25d018847706805f055fe77604ff000090323b77506980799099a089dddf8108087b23046009705f0cbff002c214604463ff456ad084605f03dfe51e5756074e6c4f58030b4f5803f444606902f60ef607ff647aecde70799099a002430460323009405f0adff706840b146f22b11c0f25d018847706805f01dfe74603068b475002818bf05f016fe0020387000203060b06030824ff00a0840460bb0bde8f08f287a4006f8d5307c08b302980078e0b10799099a002430460323009405f080ff706840b146f22b11c0f25d018847706805f0f0fd74603068b475002818bf05f0e9fd00203060b06030824ff00b083068002818bf05f0defd00203060b0603082cae702980078e0b10799099a002430460323009405f054ff706840b146f22b11c0f25d018847706805f0c4fd74603068b475002818bf05f0bdfd00203060b06030824ff00608a7e72de9f041f02846d100294ff00b0000f08880022ac0f085804cf6dd70c0f245000e4617468047811eb1fa81f14c09012808bf0224002c4ff00a0072d07578e91ec9b2fe2938bfbde8f081307833463946022829d0012866d1ac4204bf0020bde8f0814808842884bf0b20bde8f081881e03f1020887b240463946fff7e1f920b306462846fff738fa21462a46fff70cfa3046bde8f0814cf6dd77c0f2450707f5e27cbde8f0416047ac4219d1a1f1210010f1160f06d36ff0480048432330c0b2242810d90b20bde8f081b07a204207d04046394622462b46bde8f041fff710bb0020bde8f0811a4612f8020f40f00207032f18d19b796340032b14d10b2904d0032804bf0b20bde8f081881e4df22d7783b2c0f247070120f021b847002818bf0620bde8f0810a20bde8f081b0b54cf28544c0f251041d46a0474ff2a841c2f229010968bff35f8f0a466ff39602b2f1005f08d10a684ef66c63ccf2ff039a4218bf002100e00021421c18bf002900d1b0bd95f84d20002afad091f84321002a08bfb0bd45f2e823c2f206031a68132aeed8c2ebc20403eb04135b6801ebc2014ff40042c1f8c031a1f8c6212a7a01f5e071002a0fbf802248f20112ca808122ab7c33b1ab7d23b1022342f002028b71ca7195f820303bb195f8243023b1032342f004028b71ca7195f846304bb195f8443042f008020b7195f84530ca714b71b0bd47f27e32c2f207021378002b14bf9378ff230b711378002b14bf5278ff224a71b0bdf0b589b045f6e532c0f24b020c46054690474ff2a841c2f229010e68bff35f8f31466ff39601b1f1005f08d131684ef66c62ccf2ff02914208bf092d01d009b0f0bde1082c29fad896f843110029f6d04cf6dd71c0f2450107468847014638460129ecd1ff22079244f6435245f2e8210020cff60172c2f20601089006920b685a1e132a4fd8c3ebc30501eb05111a1fa1f5de7191f85451ad0618d4d51c42d091f8e450ad0616d4951c3cd091f87450ad0613d4551c36d00d79ad0610d4043aa1f5e071151da3f10403e3d12be05a1e01f5a87104e09a1ee03101e0da1e703106ebc203096893f9c721b2f1ff3f1adc03f5e0731e688e4215d11e798df81b605e798df81c609b798df81e200a0a8df820108df821200a0c090e8df81d308df822208df823100f228df8012010228df80220522208218df80420b4f5804f4ff080028df800108df8061038bf4ff48042b4f5001f6ff07f0604f07f0138bf0026802c28bf80318df807109df809509df80a108df8030004d29df808200223002415e0c4f3c610b4f5804f024403d20323012400200be0b4f5001f03d204230224012003e0052303240120002135468df8053004f108038df808208df809508df80a1070b96846a222c118c254062048700c208870c81c06a90c22fbf7e3fe04f117034df60f166a46c0f2470601200821b047384609b0f0bd70b582b00546ff208df8040044f25220c0f2010000904ef6a970c0f2470016460c46804798b147f2a620c2f2070000880122c1b264298df8032008d80721b0f5807f38bf03218df803108df80400301b28bf08280cd3d221281929550621417005218170033069460522fbf79ffe0834204602b070bd00bf024600284ff0ff3009d0022918bf70471078112801d1507810b14ff0ff30704710b586b04cf6dd70c0f24500804701284ff000003ed1cde9020040f20830c0f210000190ff208df8140044f25220c0f2010004904ef6a970c0f24700804798b147f2a620c2f2070000880122c1b264298df8132008d80721b0f5807f38bf03218df813108df8140040f2d260adf80800052001ac8df80a00e01d04a90522fbf749fe4df60f1cc0f2470c0120092122460c23e04706b010bd4ff2a840c2f22900016808466ff39600b0f1005f16d108684ef66c62ccf2ff0290420fd1d1f8380160b146f68052c2f207021268801a0028cebf01200020c1f838017047002070474ff2a840c2f22900026810466ff39600b0f1005f2bd110684ef66c61ccf2ff01884224d1d2f8680108b346f68051c2f207010968401a0028c4bf01207047d2f86c1100200029c2f8680182f8660108bf704710b5c2f86c0148f2c732c0f245020446084690472046bde8104070470020704700bff0b101684ef66c62ccf2ff02914218bf704790f8421199b110b54af29f344ff0000cc0f24604864680f842c10120002100220023aef840c1a446bde810406047704700bff0b581b004464ef2e960c0f245008047002c18bf012801d101b0f0bd21796279a379e77941ea022141ea034151ea076109d00e2904d00329eed10646032504e00646042501e006460225fff759ff00283046e1d04ff2a841c2f229010e6831466ff39601b1f1005fd6d131684ef66c62ccf2ff029142cfd14cf6dd71a778c0f2450104468847014620460129c4d1b221082086f86412062186f86002032086f8651286f86612462186f86102102086f86712432186f86202002086f86812012186f86b724df6090706f5187286f8630286f8691286f86c02c0f24707012009210d2386f86a52b847204601b0f0bd00bf2046fff785bf00bf2de9f04f81b04ef66c694ff2a8464ff624580446ccf2ff09c2f229064ff6ff77cff6ff7800254ff0010a06e048f2c731c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168494500f0a8804ff4377000f04efe002800f0cd80414642183d3182f8dc5282f8dd5282f8de5282f8df5282f8e05282f8e15282f8e25282f8e35282f8e45282f8e55282f8e65282f8e75282f8e85282f8e95282f8ea5282f8eb5282f8ec5282f8ed5282f8ee5282f8ef5282f8f05282f8f15282f8f25282f8f35282f8f45282f8f55282f8f65282f8f75282f8f85282f8f95282f8fa5282f8fb5282f8fc5282f8fd5282f8fe5282f8ff5282f8005382f8015382f8025382f8035382f8045382f8055382f8065382f8075382f8085382f8095382f80a5382f80b5382f80c5382f80d5382f80e5382f80f5382f8105382f8115382f8125382f8135382f8145382f8155382f8165382f8175382f8185382d1c0f8009080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f59457ff44aafbff35f8f46e8000121b156e8001f5945f8d03fe7002780f857712a270821032380f85871042780f8541180f85531102380f8597180f85a1101214df60f1c074600f5aa7280f8563180f85b1180f85c31002c18bf0124c0f2470c012010210a2387f85d4101b0bde8f04f604701b0bde8f08f00bf80b582b04ff0010ccdf800c000f002f802b080bd2de9f04381b04ff2a847c2f229073f684ff000093e466ff39606b6f1005f07d13e684ef66c65ccf2ff05ae4208bfb946012811d1ddf820801c4615460e460746fff7d2fd2346044631462a463846002c18bfb9f1000f07d14af29f3cc0f2460c01b0bde8f043604799f8420188b1b8f1010f40f08580d9f83c01002189f84211a9f84011e0b342f68d41c0f24401884736e0b9f840010121013080b2012898bf0846a9f84001d9f83c0142f68d44c0f2440489f8421168b94ff6dd30cff6ff70a4f59477784400214a460023b847c9f83c0140b1a047d9f83c01a4f140024ff4c8719047a8b199f84201002848d0d9f83c01002189f84211a9f8401100b1a0474af29f3cc0f2460c0120002100220023a6e74cf6dd70c0f24500804701282fd1082089f84401032089f84501102089f84601002089f84701b22089f84801062089f8490189f84a01462089f84b01432089f84c01012089f84d01b9f840014df6090c070a09f5a27289f84e8189f84f01c0f2470c012009210d2389f850716fe701b0bde8f08300bf80b582b04ff0050ccdf800c0fff734ff02b080bd2de9f04f83b04468002c00f0c1828368002b00f0bd8204eb030b4ff000090027002614e046f68053c2f207031b6845f69077c0f201073b44c6f838310ceb0a0300275b451c464ff0000680f0a182a04618f9013b03f07f05bd40002b46ea050610d406f00703012b16dc0bb3012b40f08f82abeb0803082bc0f08a8204f10903dee700bfd84580f08382182f07f107074446dcd97ce200bf022b3ad0052b40f07782abeb0803042bc0f07282631dc7e7d84580f06d8294f90130002b04f10203bed55b4580f0648294f90230b3f1ff3f04f10303b4dc5b4580f05a8294f90330b3f1ff3f04f10403aadc5b4580f0508294f90430b3f1ff3f04f10503a0dc5b4580f0468294f90530002b00f14182a31d96e7d84580f03c8294f9013004f1020c002b03f07f0a1ed5dc4580f0318294f90270e31c07f07f05b7f1ff3f4aeac51a02dde0469c460ee05b4580f0218294f9037004f1040c07f07f05b7f1ff3f4aea853a33dd9846abeb0c039a4500f21082f308672b1dd0652b7ff460afbaf1060ffff45caf9cf80030462b7ff457af98f80230432b7ff452af98f80330012b7ff44daf4ff2a844c2f229040290cde9002132e0044660460e4651461746654600f01bfcac4631463a46204637e7dc4580f0df8194f90470631d07f07f05b7f1ff3f4aea455aaddc5b4580f0d28194f90570002f00f1cd8104f1060c4aea077ab1e748f2c7313046c0f24501bff32f8f8847dde900210298ac462768bff35f8f2668bff35f8f33466ff39603b3f1005f07d133684ef66c65ccf2ff05ab4200f0bc804ff43770654600f07dfbdde9002106460298ac46002e3ff4f1ae4ff62453cff6ff7300bff4183d3384f8dc9284f8dd9284f8de9284f8df9284f8e09284f8e19284f8e29284f8e39284f8e49284f8e59284f8e69284f8e79284f8e89284f8e99284f8ea9284f8eb9284f8ec9284f8ed9284f8ee9284f8ef9284f8f09284f8f19284f8f29284f8f39284f8f49284f8f59284f8f69284f8f79284f8f89284f8f99284f8fa9284f8fb9284f8fc9284f8fd9284f8fe9284f8ff9284f8009384f8019384f8029384f8039384f8049384f8059384f8069384f8079384f8089384f8099384f80a9384f80b9384f80c9384f80d9384f80e9384f80f9384f8109384f8119384f8129384f8139384f8149384f8159384f8169384f8179384f8189382d14ef66c63ccf2ff0333600123b3724ff6ff734ff2a844b383f38333847384b384f38433857385b385f38533867386b386f38633877387c2f2290454e8003fbb427ff42fafbff35f8f00bf44e8006323b154e8003fbb42f8d023e798f8043098f8057098f80650032b47ea052433dd052b54dc042b74d0052b7ff431aed6f868313bb146f68057c2f207073f68db1b002b14dcd6f86c3186f866917bb10c4648f2c73180461846c0f24501c6f86c91174665468847ac4621463a46404646f68053c2f207031b6845f69077c0f201073b44c6f8683103e6012b3ff4f5ad022b00f08380032b7ff4fbad174688466546fff7fefa03460298ac4641463a46002b3ff4eead96f84231002b3ff4e9ad304641f28831a6f8404105f04ef847e0062b4ad0072b7ff4dcad80464cf23330c0f24a0016460f466446804703465d1ea446394632464046012d3ff6caad981eb0fa80f04009fff7c8fba446394632464046bee5174688466546fff7c2fa03460298ac4641463a46002b3ff4b2ad96f84231002b3ff4adadb6f84031a3427ff4a8add6f83c3186f84291002ba6f840913ff49fad42f68d411846c0f2440188470298ac4641463a4693e5d6f86c31c6f86891002b86f866913ff48bad0f4648f2c73104461846c0f24501c6f86c91164665468847ac463946324640e696f84231c6f83891002b3ff474ad0446d6f83c0165468846174686f84291a6f8409120b142f68d41c0f2440188474af29f360120002100220023c0f24606b04720463a464146ac4655e54df60923c0f2490303b0bde8f04f18477fb50646002806d1fff750fa002807d19de80f00064645f25b1cc0f24f0c60477fbd00bf2de9f04182b04df609061d4614460929c0f247064cd180460f46f22d10d8a22160196155062141700b21817045f64251c0f20001033079440b22fbf763f80e35204629464ff4807200f038f80546ff208df8040044f25220c0f20100009006f5bd50804798b147f2a620c2f2070000880122c1b264298df8032008d80721b0f5807f38bf03218df803108df80400f82d0cd8d221601961550621417005218170033069460522fbf72df808353946404622462b46b04702b0bde8f081f0b587b00c464ff2a841c2f229010d68bff35f8f29466ff39601b1f1005f52d129684ef66c63ccf2ff0399424bd14d218df8041043218df8051001218df8061095f8701195f871318df807108df8083095f87511b5f878318df80b108df80c30190ab5f87a318df80d10190a8df80f1095f8761195f8737195f874618df80e3095f877318df810104cf6dd71c0f245018df809708df80a608df81130064617468847d5f87c3101468df81210190a8df81410190c3a4630468df813308df81510190ea02328e04d218df8041043218df8051001218df80610002100238df807108df808108df809108df80a108df80b108df80c108df80d108df80e108df80f108df810108df811108df812108df813108df814108df815108df816108df817300023111b8df8183028bf18290cd3c222011902550620487015208870c81c01a91522faf781ff1834204607b0f0bd00bf2de9f04f81b04ff2a840c2f229000468bff35f8f20466ff39600b0f1005f0dd120684ef66c63ccf2ff03984206d194f8700118b1002a18bf002902d101b0bde8f08f94f877010028f8d0d4f880010028f4d046f68059c2f20709d9f80030c01a0128ebdb94f87101012802d14ff0010b06e094f873b1bbf1010f18bf4ff0020b94f876510020eb078846adf80200adf8000013d0bbf1020f10d194f8750168b96b464bf2f1270df10201c0f25a07404615461a46b8472a4694f876511646b2f5c86f28bf4ff4c86606f1150a5046174600f050f80028b1d0b7f5c86f88bf45f08005d9f80010d4f87c2153230271120a42710a0a8171c2710a0c090e03704d234172a02143700123c17200218370c570027280f80ab0017394f87511bdf802204173bdf800108373c173090a0174110a8174310a017500f115014274c6740746084641463246faf7d7fe4df66d22c0f24702384651469047d4f87c0148f2c7310130c4f87c01c0f245013846884701b0bde8f08fb0b54ff2ac44c2f22904216846f23f52c5f28802b2eb510f48f283314cf67e25c0f24501caf2101518bf25608847002804bf691c2160b0bd10b301684ef66c62ccf2ff0291421bd190f87711c1b1d0f88011b1b146f68052c2f207021368cb1a012b0edb1268d0f88401002808bf7047891a42f64d42c0f2440208bf01211047704710b5002143f2c942c0f8801180f87711c0f25902002044f24361904748f65354c0f25a04a04704f1bc00bde81040004700bf2de9f04f81b00429c0f0f7800c46014600784d2840f0f1804878432840f0ed808878012840f0e9804ef66c684ff2a847ccf2ff08c2f229074ff6ff7600254ff0010b8a4607e048f2c731c0f24501bff32f8f88475146d7f80090bff35f8f3868bff35f8f02466ff39602b2f1005f03d10268424500f0ac804ff43770fff766ff002800f0ba804ff624525146cff6ff7283183d3283f8dc5283f8dd5283f8de5283f8df5283f8e05283f8e15283f8e25283f8e35283f8e45283f8e55283f8e65283f8e75283f8e85283f8e95283f8ea5283f8eb5283f8ec5283f8ed5283f8ee5283f8ef5283f8f05283f8f15283f8f25283f8f35283f8f45283f8f55283f8f65283f8f75283f8f85283f8f95283f8fa5283f8fb5283f8fc5283f8fd5283f8fe5283f8ff5283f8005383f8015383f8025383f8035383f8045383f8055383f8065383f8075383f8085383f8095383f80a5383f80b5383f80c5383f80d5383f80e5383f80f5383f8105383f8115383f8125383f8135383f8145383f8155383f8165383f8175383f8185382d1c0f8008080f80ab08683c683068446848684c684068546858685c685068646868686c6860687468757e8002f4a457ff445afbff35f8f47e8000222b157e8002f4a45f8d03ae7ca78022a04dc012a0dd0022a79d00ce0032a0dd0042a08d190f8701129b101b0bde8f04faae00d2c06d201b0bde8f08f044600f045f963e090f87721aab1002143f2c94280f87711c0f259020446002044f24361904748f65355c0f25a05a84705f1bc008047204651460a790446012a88bf002280f871214a7902f00303012b4ff0010298bf134680f873318f79012f88bf002780f87471cf79022f88bf002780f875710f7a4e7a47ea0627502f98bf5027b7f5f07f28bf4ff4f077a0f878718f7ace7a47ea062741f28836b74238bf3e46a0f87a61097b80f8702101f0030180f8761103f0010101eb530180f872110021c0f87c1100f009f8204600f03ef8204601b0bde8f04f6ae000bfb0b5044690f87601800758bfb0bd94f8710148f6a3450128c0f25a0507d105f1b000804705f58b71002088470ee005f5b670804794f87101012801d1002004e094f87301013818bf0120a8474ff6a122cff6ff7243f271337a44c0f25903002044f243619847012084f87701b0bd00bf70b5044646f68050c2f20700006894f8771145f69075c0f2010500292844c4f8800108bf70bdd4f8840142f66536c0f2440658b94ff6f930cff6ff707844002122460023b047c4f8840150b106f594718847d4f8840106f1e8022946bde87040104770bd70b5044600f5c475082084f88801032084f88901102084f88a01c22084f88c01062084f88d014d2084f88f01432084f89001012084f8910194f8700194f8711184f8920184f8931194f87501b4f8781184f8960184f89711080ab4f87a1184f89801080a84f89a0194f8760194f8732194f8743184f8991194f8771184f89b014cf6dd700026c0f2450084f88b6184f8942184f8953184f89c118047d4f87c1184f89d01080a84f89f01080c84f8a001080e84f8a101a02084f8a36115264df6090c84f89e1184f8a201c0f2470c012009212a461c2384f88e61bde87040604770b54ff2a840c2f229000468bff35f8f20466ff39600b0f1005f33d120684ef66c61ccf2ff01884218bf70bd94f877010025c4f8805188b143f2c942c0f25902002044f2436184f87751904748f65356c0f25a06b04706f1bc008047d4f8840184f8705170b142f68d45c0f24405a847d4f8840105f166018847002804bf0020c4f8840170bd00bf2de9f04182b04ff2a841c2f229010c68bff35f8f21466ff39601b1f1005f75d121684ef66c62ccf2ff0291426ed194f8a81179b394f8aa01c0070ad0d4f8680178b346f68051c2f207010968401a002827dd46f2d835c2f2070555f8040c002854d04ff27717c0f24b0747f62938002607f5437101a8c0f24b0801968847a8b1032084f8ab612860b4f8ac01c04702b0bde8f081d4f8b411c1b302b0bde8f0410847204602b0bde8f04133e001984ef6ed56c0f24b06a861b04706f5af708047e861b847012084f8ab0103202860b4f8ac01c047d4f8bc01a0b10199d4f8b821b4f8ae71531a38bf8b1abb420ad2b4f8b01159b146f68052c2f207021268101a884203d32046022100f06ef802b0bde8f08100bfb0b590f8a841002c42d043f60852c2f2000211880829114622d0114631f8083f082b1dd0114631f8103f082b18d0114631f8183f082b13d0114631f8203f082b0ed0114631f8283f082b09d0114631f8303f082b04d032f8381f0829114614d14a68b2f5870f10d36ff35602012a0cd14a684ff66f63cff6ff737b449a4204d1d0f8b421002a18bf4a60002180f8a81190f8a911b1b1002180f8a91144f68241c2f20701097869b946f2d431c2f20701096839b148f26111c0f24b010546042088472846002c08bfb0bd0321bde8b04000e000bfb0b588b004464cf6dd70c0f245000d468047012840f08f8008208df8010003208df8020010208df8030000208df80400ca208df8050006208df8060018208df8070046f2f030c2f2070050f81c2c94f8a811002a18bf0122002944f68141c2f207014b7818bf0232002b94f8ab3118bf0432002b4ff0410318bf08328df808304c238df8093001238df80a308df80b508df80c2050f8182c4df609058df80d200268c0f24705130a8df80e208df80f30130c120e8df810308df811204268130a8df812208df81330130c120e8df814308df8152002698df81620120a8df8172009788df8181081688df819106821c2f2000109688df81a10090a8df81b1046f68051c2f207010a68130a8df81c208df81d30130c120e8df81e308df81f2000680df10102c4f8b80108680921c4f8bc0101201f23a84708b0b0bd00bf2de9f04f83b0022902d24ff0ff35fee0cde901100120cff680704ef66c644ff2a8464ff6245bccf2ff04c2f229064ff6ff77851ccff6ff7b4ff000084ff0010907e000bf48f2c731c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f00ea0501b1f1005f03d10168a14200f0a7804ff43770fff74bfb0028c5d0594642183d3182f8dc8282f8dd8282f8de8282f8df8282f8e08282f8e18282f8e28282f8e38282f8e48282f8e58282f8e68282f8e78282f8e88282f8e98282f8ea8282f8eb8282f8ec8282f8ed8282f8ee8282f8ef8282f8f08282f8f18282f8f28282f8f38282f8f48282f8f58282f8f68282f8f78282f8f88282f8f98282f8fa8282f8fb8282f8fc8282f8fd8282f8fe8282f8ff8282f8008382f8018382f8028382f8038382f8048382f8058382f8068382f8078382f8088382f8098382f80a8382f80b8382f80c8382f80d8382f80e8382f80f8382f8108382f8118382f8128382f8138382f8148382f8158382f8168382f8178382f8188382d1046080f80a908783c783078447848784c784078547858785c785078647868786c7860787478756e8001f51457ff44dafbff35f8f00bf46e8000121b156e8001f5145f8d041e704464cf6dd70c0f24500804701281dd1029d6978022916d00129204607d000297ff413af00210025fff76cfe0fe0019f41f28831022f0ed14ff4fa730022012741f2883624e02046fff7f2fd0025284603b0bde8f08faa78052f11d3eb782e79072f43ea062310d395f805c0ae79092f4cea0627e4d3ee792d7a46ea052607e041f288364ff4fa7301e041f288360127642b98bf64238b4228bf0b4690f8a81180f8aa21a0f8ac31a0f8ae71a0f8b06171b146f2d431c2f207010968002954d001210446fff71efe0025c4f8bc51bee743f60852c2f2000211880829114622d0114631f8083f082b1dd0114631f8103f082b18d0114631f8183f082b13d0114631f8203f082b0ed0114631f8283f082b09d0114631f8303f082b04d032f8381f0829114632d14a686ff00105b2f5870ffff48daf0123cff680731a40012a7ff486af4b684ff6cd12cff6ff727a44934214bfc0f8b431d0f8b431002b3ff477af4a60012180f8a811a3e748f2ef01c0f24b010420884720b105462046fff758fd65e70121204684f8a91199e76ff001055de700bf334600f005b800bf334600f037b800bf2de9f04182b042f2d7281e4614460d460746c0f24608fef771fa002e18bf002808d108f1160338462946224602b0bde8f041184746f26870c2f207000068c0470028eed00068e028ebd130784bf6d32601900027c0f24e0600200021002209230097b047012002b0bde8f0812de9f04182b042f2d7281e4614460d460746c0f24608fef73bfa002e18bf002808d108f1160338462946224602b0bde8f041184746f26870c2f207000068c0470028eed00068e028ebd130784bf6d32601900027c0f24e060020002100220a230097b047012002b0bde8f08110b582b00446fef70dfa0146002c4ff0000018bf002901d102b010bd46f26870c2f20700006842f2d721c0f24601884790b10068e0284ff00000edd121784bf6d32c0191c0f24e0c002100220b230090e047012002b010bd002002b010bd00bf08b53046fff7ccff002805d002b044f6e970c0f24400004748f68b43c0f24603984708bd012818bf002900d170478968002908bf704710b582b00a794b798c79c97942ea032242ea044242ea0161062910d14ef22151c0f24501044688470146204639b144f6e100c0f2440080470146204619b102b0bde810407047fef7a2f9014620460029f5d046f26870c2f20700006842f2d721c0f246018847014620460029e7d00968e029e4d14bf6d32e4ff0000cc0f24e0e0020002100220c23cdf804c0cdf800c0f0472046d3e710b5059c4ff23710c0f2460080472146fff7a2ff10bd00bf2de9f04f8bb0044600284ff0ff3000f031830d46002900f02d83217801f07f070a2f0edd102f27dc0b2f00f08482102f40f02482204629460bb0bde8f04ffff71fbd052f00f03081072f00f016830a2f40f01482022dc0f00d834ef66c6b4ff2a847ccf2ff0bc2f229074ff6ff764ff0000a4ff0010858e0112f00f0f682122f40f0fc81b5f1010b01f0728004f1010a00230021abeb03000628c0f0b1860aeb0304207962799d1d40ea0222abeb050082424ff0ff3000f2dd821af803306778a678e47843ea072343ea064343ea0463b3f5802f00f2ce82c3f580239a4200f2c982002a02eb050318bf01215b45d1d3002901f03d80fef7f3f8002800f080864ef66c674ff2a845ccf2ff07c2f229054ff6ff764ff0000800f02fbc48f2c731c0f24501bff32f8f8847d7f80090bff35f8f38680321cff680710140b1f1005fbff35f8f03d10168594500f0a2834ff43770fff71df8002800f052864ff62451cff6ff7142183d3182f8dca282f8dda282f8dea282f8dfa282f8e0a282f8e1a282f8e2a282f8e3a282f8e4a282f8e5a282f8e6a282f8e7a282f8e8a282f8e9a282f8eaa282f8eba282f8eca282f8eda282f8eea282f8efa282f8f0a282f8f1a282f8f2a282f8f3a282f8f4a282f8f5a282f8f6a282f8f7a282f8f8a282f8f9a282f8faa282f8fba282f8fca282f8fda282f8fea282f8ffa282f800a382f801a382f802a382f803a382f804a382f805a382f806a382f807a382f808a382f809a382f80aa382f80ba382f80ca382f80da382f80ea382f80fa382f810a382f811a382f812a382f813a382f814a382f815a382f816a382f817a382f818a382d1c0f800b080f80a808683c683068446848684c684068546858685c685068646868686c6860687468757e8001f49457ff446afbff35f8f47e80001002900f0fa8257e8001f4945f6d039e74ef66c664ff2a847ccf2ff06c2f229074ff6ff7b4ff000084ff00109022d2cbf6078ff20059006e048f2c731c0f24501bff32f8f8847d7f800a0bff35f8f38680321cff680710140b1f1005fbff35f8f03d10168b14200f091824ff43770fef743ff002800f0d9844ff62451cff6ff7142183d3182f8dc8282f8dd8282f8de8282f8df8282f8e08282f8e18282f8e28282f8e38282f8e48282f8e58282f8e68282f8e78282f8e88282f8e98282f8ea8282f8eb8282f8ec8282f8ed8282f8ee8282f8ef8282f8f08282f8f18282f8f28282f8f38282f8f48282f8f58282f8f68282f8f78282f8f88282f8f98282f8fa8282f8fb8282f8fc8282f8fd8282f8fe8282f8ff8282f8008382f8018382f8028382f8038382f8048382f8058382f8068382f8078382f8088382f8098382f80a8382f80b8382f80c8382f80d8382f80e8382f80f8382f8108382f8118382f8128382f8138382f8148382f8158382f8168382f8178382f8188382d1066080f80a90a0f81cb0a0f81eb0a0f820b0a0f822b0a0f824b0a0f826b0a0f828b0a0f82ab0a0f82cb0a0f82eb0a0f830b0a0f832b0a0f834b0a0f836b0a0f838b0a0f83ab057e8001f51457ff437afbff35f8f00bf47e80001002900f0d98157e8001f5145f6d029e70f2f9b4600f0f481a7f11300012840f2ef81092f00f05d83082f40f0d583002a4ff0ff3000f0e880022dc0f0e58094f80190b9f1000f00f0ad8402264ff0010848f2482ab21caa4200f29c84a019a15d407851ea002100f095848e18ae4200f29184a05cde4600f07f07153fffb2fd2f16d8c0b20f280ed9a0f1830cbcf10c0f4ff0ff3000f2b88008fa0cf741f249031f4205d1b0e008fa00f717ea0a0febd0a01800227346fff76dfd002840f06a84b9f10109c6d100f06dbc4ff2a846c2f229063468bff35f8f20466ff39600b0f1005f40f00f8620684ef66c67ccf2ff07b8424ff0000040f08780c4f8680184f8660184f86401d4f86c11c4f8600184f8650139b148f2c732c4f86c01c0f2450208469047d4f87802002818bf03f08ef90021c4f87812206c40f2030942f68d48cff68079c0f24408a4f8441040b1c047206c08f166018847002804bf0020206446f67560c0f2510080473568bff35f8f05ea0900b0f1005f2bd12868b84228d195f877014ff0000ac5f880a188b143f2c942c0f25902002044f2436185f877a1904748f6535bc0f25a0bd8470bf1bc008047d5f8840185f870a150b1c047d5f8840108f166018847002804bf0020c5f884013068bff35f8f00ea0901b1f1005f40f029850168b94240f0258590f8a811002900f01a85fff705f900f01cbd022d08bf112900f02b810bb0bde8f08f4ef66c6a4ff2a847ccf2ff0ac2f229074ff6ff764ff0000b4ff0010806e048f2c731c0f24501bff32f8f8847d7f80090bff35f8f38680321cff680710140b1f1005fbff35f8f03d10168514500f05e834ff43770fef75ffd002800f056834ff62451cff6ff7142183d3182f8dcb282f8ddb282f8deb282f8dfb282f8e0b282f8e1b282f8e2b282f8e3b282f8e4b282f8e5b282f8e6b282f8e7b282f8e8b282f8e9b282f8eab282f8ebb282f8ecb282f8edb282f8eeb282f8efb282f8f0b282f8f1b282f8f2b282f8f3b282f8f4b282f8f5b282f8f6b282f8f7b282f8f8b282f8f9b282f8fab282f8fbb282f8fcb282f8fdb282f8feb282f8ffb282f800b382f801b382f802b382f803b382f804b382f805b382f806b382f807b382f808b382f809b382f80ab382f80bb382f80cb382f80db382f80eb382f80fb382f810b382f811b382f812b382f813b382f814b382f815b382f816b382f817b382f818b382d1c0f800a080f80a808683c683068446848684c684068546858685c685068646868686c6860687468757e8001f49457ff446afbff35f8f47e80001002900f0b68257e8001f4945f6d039e790f84410059f61b1016c39b142f68d42c0f244020646084690473046002180f844100121002f40f04082032dc0f03d82a078082800f28a8446f67561c0f251019631884700f082bc164600f0a9fc002800f0c482611c142fa5f1010200f0b782132f5b4640f0b78200f0eefdb5e26678002e00f04581022e00f2b08240f03d84062dc0f0ab82a178e2782379677941ea022143ea072232290691079280f02784322100f029bc6078002840f097824cf6dd70c0f24500804701284ff000007ff4c7aecde9070040f20830c0f210000690ff208df8280044f25220c0f2010009904ef6a970c0f24700804798b147f2a620c2f2070000880122c1b264298df8272008d80721b0f5807f38bf03218df827108df8280040f2d260adf81c00052006ac8df81e00e01d09a90522f9f7e1fa4df60f17c0f247070120092122460c23b8470bb0bde8f08f48f2c7314846c0f24501bff32f8f88472c68bff35f8fd5f800900320cff6807000ea0900b0f1005fbff35f8f04d1d9f80000b84200f03b824ff43770fef7f3fb002800f0288281464ff62450cff6ff7000bf09eb00013d3081f8dc8281f8dd8281f8de8281f8df8281f8e08281f8e18281f8e28281f8e38281f8e48281f8e58281f8e68281f8e78281f8e88281f8e98281f8ea8281f8eb8281f8ec8281f8ed8281f8ee8281f8ef8281f8f08281f8f18281f8f28281f8f38281f8f48281f8f58281f8f68281f8f78281f8f88281f8f98281f8fa8281f8fb8281f8fc8281f8fd8281f8fe8281f8ff8281f8008381f8018381f8028381f8038381f8048381f8058381f8068381f8078381f8088381f8098381f80a8381f80b8381f80c8381f80d8381f80e8381f80f8381f8108381f8118381f8128381f8138381f8148381f8158381f8168381f8178381f8188381d10120c9f8007089f80a00a9f81c60a9f81e60a9f82060a9f82260a9f82460a9f82660a9f82860a9f82a60a9f82c60a9f82e60a9f83060a9f83260a9f83460a9f83660a9f83860a9f83a6055e8000fa0427ff42fafbff35f8f45e80090002800f07f8155e8000fa042f6d022e7002480f843414cf6dd70c0f2450080470146204601297ff49bad4df25f50c0f255000bb0bde8f04f004708064ff0210158bf11218d42c0f05181002804f101060ad54cf6dd70c0f24500154680472a46022818bf04f111063079717950ea01244ff0ff301ebfb179f37951ea03253ff46fad317b737b41ea03218c427ff468adb17bf37b41ea03218d427ff461ad3078717840ea012804eb0800b0f5207f4ff0ff303ff655adb178f37841ea0327e919b1f5f07f3ff64cad317a737a41ea032904eb0901b1f5207f3ff642adb17af37a41ea0326a919b1f5f07f3ff639ad924600f0defa002800f0f98041463a464b46cde90145009602f0adf9dbf800100f2909d80bebc1020131a2f80690168154819581cbf80010baf1000f40f0e78095e2f81e18bf0120bb1f18bf012318424ff0ff307ff40dad032dfff40aad032f00f0b281062f7ff404ad00200021164602f0edfa00f0a5fa002800f0c0800246601c691e4ff4f0774ff4a073009702f01efc002800f0b380dbf800100f2900f2b3800bebc1020023d38013814ff4207353814ff4f07301319381cbf80010a3e0059f00210020002f3ff4c0ad012f19d1052d17d3a378581e06284ff000003ff6c8ace17803293ff6c4ac2279002a3ff4c0ac46f67560c0f2510000f59a771846b84734e2022f05d146f67560c0f2510080472ce2032f40f01381072dc0f01081a078e278217940ea02206579a67901284ff0010498bf204644f62062904228bf104646f67562c0f2510202f5e272642928bf6421904746f2a070c2f207000068002800f0048245ea0621012988bf0c4642f68d41c0f24401a1f1400221469047f5e10020022d2cbf6178ff21002800f0ee81024602294ff0000025d0012920d000297ff466ac4cf67e21caf21011907610837960108150609683d683168456849684d684168556859685d685168656869686d6861687568782f83c000bb0bde8f08f01209072c2e190720bb0bde8f08f5b4600f0ecfe01e001f067fb20b14ff0ff300bb0bde8f08f002e00f0b081584601f0a7ffabe1d9f86c0100286ed14ff48020fef7b4f90028e9d000210022831881544032b2f5802f59709970d970197159719971d971197259729972d972197359739973d973197459749974d974197559759975d975197659769976d976197759779977d97783f8201083f8211083f8221083f8231083f8241083f8251083f8261083f8271083f8281083f8291083f82a1083f82b1083f82c1083f82d1083f82e1083f82f1083f8301083f8311083f8321083f8331083f8341083f8351083f8361083f8371083f8381083f8391083f83a1083f83b1083f83c1083f83d1083f83e1083f83f109ad1c9f86c0100210aeb010006794579437846ea0525d9f86c611af8012087788c1d06eb0321c078114401eb074101eb00600aeb04012a46f8f7f0ff291959454ff00000e0d3fff799bb3a1f80460020b2fa82f2022d88bf012052091040084001284ff000007ff489ab4cf6cd42e91eccf6cc42a578a1fb0212b5eb920f88bf95082f4608f14603302d28bf3027e11c07eb870204461846f8f7c0ff002d88f8447088f8454000f0b480d8f8400038b94046f9f71dfc0028c8f8400000f0fc8042f68d41c0f24401a1f1400201219047d2e0080600f1d480082dfff415af07214ff0050c0123e71897f802804ff0ff30b8f1000f3ff442abfe78002e3ff43eab14f8039005914fea880101eb8903b3f5207f04913ff632ab97f801a0770007eb4a03b3f5f07f3ff629ab04eb0c0014f80c104078039241ea002081b2012002f00bf900f0c3f800283ff4deae05990aeb8a020c4400ebc2104fea4803691a00eb49022046009702f037fa00283ff4ccaedbf800100f290dd84fea89020bebc106f280049a4fea4a03013133817281b781cbf80010039900297ff4beae6ce090f8a911002918bffef7e7fb94f84301002684f8436158b14cf6dd70c0f245008047012804d14df25f50c0f255008047d4f83c0194f84251c4f8386184f84261a4f8406150b1c047d4f83c0108f166018847002804bf0020c4f83c010120a072ddb34af29f37c0f246070021002200230024b84720460bb0bde8f08fb1f5fa6f02d94ff4fa6106910121044680f843114cf6dd70c0f24500804701281dd14df25f55c0f25505a5f188008047022e08bf002801d058b910e048f2d31206a9c0f24b020220904740b10646a8473046002184f843110bb0bde8f08f00200bb0bde8f08f0c2dfff441aee078e17988427ff43cae2079217a88427ff437ae4cf6dd70c0f24500164680470523022808bf01230b214ff0090c324616e746f675600024c0f2510088f844408047a3e700bf2de9f04f81b04ef66c6b4ff2a8454ff6245848f2c73accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0a9804ff43770fdf7b5ff002800f01a81414600bf42183d3182f8dc7282f8dd7282f8de7282f8df7282f8e07282f8e17282f8e27282f8e37282f8e47282f8e57282f8e67282f8e77282f8e87282f8e97282f8ea7282f8eb7282f8ec7282f8ed7282f8ee7282f8ef7282f8f07282f8f17282f8f27282f8f37282f8f47282f8f57282f8f67282f8f77282f8f87282f8f97282f8fa7282f8fb7282f8fc7282f8fd7282f8fe7282f8ff7282f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382d1c0f800b080f80a908683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa1427ff44eafbff35f8f45e8000121b155e8001fa142f8d043e7d0f87812002974d104464ff4163002f0a5fa014600284ff0000069d0002200bf8b1888544032b2f5163f58709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f009ad1c4f8781200e00021084601b0bde8f08f00bf092a1cbf4ff0ff3070472de9f04f8db08a4683460693fcf72dff002800f015824ef66c684ff2a847ccf2ff08c2f229074ff6ff740026012507e000bf48f2c731c0f24501bff32f8f8847d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168414500f0ad804ff43770fdf758fe002800f0a5804ff62451cff6ff7100bf42183d3182f8dc6282f8dd6282f8de6282f8df6282f8e06282f8e16282f8e26282f8e36282f8e46282f8e56282f8e66282f8e76282f8e86282f8e96282f8ea6282f8eb6282f8ec6282f8ed6282f8ee6282f8ef6282f8f06282f8f16282f8f26282f8f36282f8f46282f8f56282f8f66282f8f76282f8f86282f8f96282f8fa6282f8fb6282f8fc6282f8fd6282f8fe6282f8ff6282f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382d1c0f8008085728483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff447afbff35f8f00bf47e8000129b157e8001f4945f8d03be70020002800f03f819af800109af801209af802309af8037041ea022141ea034141ea07624ff6fe77c0f20307ba424ff0ff3100f28982d0f86c310846002b00f02481995c4ff0ff30002900f01e81134493f80190b9f1000f00f0178109fb01f09d1c4ff0000c4ff0000368b3a7f101089e18be4200f007819e462b441c78240904d00126a0421bd2fde000bf464500f0fa805c781cb10226a04211d2f3e00524cff6fc743444042cc0f0ed809c78db7854ea032400f0e7800426a042c0f0e380001b06eb0e03d3d19af804009af8052098469af806309af807609af80870cdf8009040ea022943ea0626b8060f2307f00f0403eae07000fb04f389204343db0a8df824300e2317f0200f08bf4ff00f0c08bf012304fb03f300fb03f34fead3238df825304ff00d0308bf022304fb03f300fb03f34fead3238df826304ff00c0308bf032304fb03f300fb03f34fead3238df827304ff00b0308bf042304fb03f300fb03f34fead3238df828304ff00a0308bf052304fb03f300fb03f34fead3238df829304ff0090308bf062304fb03f300fb03f34fead3238df82a304ff0080308bf072304fb03f300fb03f34fead3238df82b304ff0070308bf082304fb03f300fb03f34fead3238df82c304ff0060308bf092304fb03f300fb03f34fead3238df82d304ff0050308bf0a2304fb03f300fb03f34fead3238df82e304ff0040308bf0b2304fb03f300fb03f34fead3238df82f304ff0030308bf0c2304fb03f300fb03f34fead3238df830304ff0020308bf0d2304fb03f300fb03f34fead3238df831304ff0010308bf0e2363434343db0a8df832300cfb04f358434ff0010ec00ab8f1000fcdf814808df8330000f06f8107f010008eea1010002403904ff0000804950ce04ff0ff300db0bde8f08fdde90704049d04440598844280f05881285d0894020901d0012313e0059a171b012f00f04d812b195a780ab1022309e0042fc0f045819f78db7857ea032200f03f810423079310f00f0309a810f803a018bf012303984fea0a1e40ea030c12f00300474600f08080b8fbf1f300259f19b7f1ff3fc8bf012505ea0c05012d1dd103fb11834b44dd09042d17d87d090e2d14d807eb87050beb85155f08ec5ddb076ff00f03019408bf0f230290504608bf7046019c234003430298eb55012808f1010751d0b7fbf1f300249d19b5f1ff3fc8bf012404ea0c04012c1ad103fb11734b44dc09042c14d86c090e2c11d805eb85040beb84145d08db070290605d6ff00f03574604bf0f237746034002983b436355022808f1020726d0b7fbf1f300259c19b4f1ff3fc8bf012505ea0c05012d18d103fb11734b44dd09042d12d865090e2d0fd804eb84040beb84145d08db07605d6ff00f03574604bf0f23774603403b43635508f10307531e032b904405d246e700bf0437b8453ff442afb7fbf1f40025a319b3f1ff3fc8bf012505ea0c05012d18d104fb11744c44e509042d12d85d090e2d0fd803eb83030beb831365085a5de4076ff00f04504604bf0f2470462240104358557c1cb4fbf1f50020ab19b3f1ff3fc8bf012000ea0c00012819d105fb114000eb0904e009042812d858090e280fd803eb83000beb80106208835ce4076ff00f04554604bf0f24754623402b438354bc1cb4fbf1f50020ab19b3f1ff3fc8bf012000ea0c00012819d105fb114000eb0904e009042812d858090e280fd803eb83000beb80106208835ce4076ff00f04554604bf0f24754623402b438354fc1cb4fbf1f50020ab19b3f1ff3fc8bf012000ea0c0001287ff47baf05fb114000eb0904e00904283ff673af58090e283ff66faf03eb83000beb80106208835ce4076ff00f04554604bf0f24754623402b4383545de708460db0bde8f08f09eb01000099b0f5207f06eb0103a8bf4ff42070b3f5f07fa8bf4ff4f0730699484588bfb34203d800200db0bde8f08f0f680f2ff8d89a1ba0eb090001ebc7035881781ca3f806901e819a810860ebe70a2a3cbf4ff0ff3070472de9f04f95b017468a4605461c46fcf77afb002800f0f9809af8096006f10a0087424ff0ff3040f0f2809af800109af801209af802309af8037041ea022141ea034141ea07624ff68061c0f203018a4200f2dd8010960e95cde90c429af808004ef66c644ff2a8454ff62456ccf2ff04c2f229054ff6ff77cff6ff764ff0000b4ff001080f9007e000bf48f2c731c0f24501bff32f8f8847d5f80090bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168a14200f0ad804ff43770fdf77afa002800f0e681314642183d3182f8dcb282f8ddb282f8deb282f8dfb282f8e0b282f8e1b282f8e2b282f8e3b282f8e4b282f8e5b282f8e6b282f8e7b282f8e8b282f8e9b282f8eab282f8ebb282f8ecb282f8edb282f8eeb282f8efb282f8f0b282f8f1b282f8f2b282f8f3b282f8f4b282f8f5b282f8f6b282f8f7b282f8f8b282f8f9b282f8fab282f8fbb282f8fcb282f8fdb282f8feb282f8ffb282f800b382f801b382f802b382f803b382f804b382f805b382f806b382f807b382f808b382f809b382f80ab382f80bb382f80cb382f80db382f80eb382f80fb382f810b382f811b382f812b382f813b382f814b382f815b382f816b382f817b382f818b382d1046080f80a808783c783078447848784c784078547858785c785078647868786c7860787478755e8001f49457ff44bafbff35f8f00bf45e8000149b155e8001f4945f8d03fe74ff0ff3015b0bde8f08fd0f86c91109eb9f1000f00f039810f9c0f23a10604f00f0203eae171514389235943c90a8df844100e219af804c09af805709af806e09af8075014f0200f08bf012102fb01f103fb01f14fead1218df845104ff00d0108bf022102fb01f103fb01f14fead1218df846104ff00c0108bf032102fb01f103fb01f14fead1218df847104ff00b0108bf042102fb01f103fb01f14fead1218df848104ff00a0108bf052102fb01f103fb01f14fead1218df849104ff0090108bf062102fb01f103fb01f14fead1218df84a104ff0080108bf072102fb01f103fb01f14fead1218df84b104ff0070108bf082102fb01f103fb01f14fead1218df84c104ff0060108bf092102fb01f103fb01f14fead1218df84d104ff0050108bf0a2102fb01f103fb01f14fead1218df84e104ff0040108bf0b2102fb01f103fb01f14fead1218df84f104ff0030108bf0c2102fb01f103fb01f14fead1218df850104ff0020108bf0d2102fb01f103fb01f14fead1218df851104ff0010108bf0e2102fb01f103fb01f14fead1218df852104ff0000108bf0f21514359430024c90a002e8df853106ad00d994ff6fe7449440af10a0a4cea07284eea052b4ff0000cc0f203040d9104e010990cf1010c8c455ed01af80c104a1e1f2af5d34ab2202a4bdb0d9a02eb810111f8802c11f87f3c11f87e7c11f87d1c42ea032242ea074242ea0165a54238d819f805104ff0ff34a9b309eb050253788bb34ff6fe7402f1020e03fb01f60023c0f2030406e000bf0121be4221d3f61b0b44c5d05a19a2421bd00eeb03010f783f09f1d1671eba4213d04f781fb10221be42ecd20de00527cff6fc773a44042a07d38a78c97852ea012702d00421be42ddd24ff0ff34204615b0bde8f08f4ff0ff3015b0bde8f08f0c990122063100910f99002601f0100182ea1111cde9071a1af806101f2908d84144109f0b3901360024be428846f3d1dde7d0f86c214ff0ff34002ad7d00d9b03eb810111f8803c11f87f7c11f87e5c11f87d1c43ea072343ea054343ea01614ff6fe73c0f203039942c0d812f801e0bef1000fbbd053185f78002fb7d007fb0ef2ddf83890002acde9027600f0ac819d1c00244ff6fe736618c0f203039e42a3d02f193b781b0901d0012718e04ff6fe73c0f20303013b9e4296d07b780bb102270de00523cff6fc733344042bfff48cafbb78ff7853ea07233ff486af04279a42fff482afd21a3c44d3d1002c00f07b81002100220f91cde9054509e0dde90a12059c0a44ddf820a0069da24280f06b81a95c0b920e0901d0012212e0a71a012f00f06181ab185e780eb1022209e0042fc0f059819f78db7857ea032600f0538104220a9211f00f0111aa525c18bf0121079b0f9c19434fea021a16f00305a446099600f08c80b4fbfef7002607fb1e434344b3f1ff3fc8bf01260e40012e1fd117eb0b071cd4de09042e19d87e090e2e16d807eb870709eb871c5e081cf80670db076ff00f03019708bf0f230495154608bf5546019f3b402b43049d0cf80630012d04f1010c5ad0bcfbfef6002706fb1ec34344b3f1ff3fc8bf01270f40012f1fd116eb0b071cd4de09042e19d87e090e2e16d807eb870709eb871c5e081cf80670db076ff00f03019708bf0f230495154608bf5546019f3b402b43049d0cf80630022d04f1020c29d0bcfbfef5002705fb1ec34344b3f1ff3fc8bf01270f40012f1ad115eb0b0717d4de09042e14d87e090e2e11d807eb870709eb87175e08db0717f806c06ff00f03154604bf0f23554603ea0c032b43bb5504f1030c099d6b1e032b2c440f9407d237e700bf0f9b0cf1040c63453ff431afbcfbfef5002605fb1ec34344b3f1ff3fc8bf01260e40012e1bd115eb0b0518d4de09042e15d86e090e2e12d805eb850509eb85155e08db07af5d6ff00f03914604bf0f23d1463b4043ea0903ddf83890ab550cf10103b3fbfef5002605fb1e334344b3f1ff3fc8bf01260e40012e18d115eb0b0515d4de09042e12d86e090e2e0fd805eb850509eb85155e08db07af5d6ff00f03144604bf0f2354463b402343ab550cf10203b3fbfef5002405fb1e334344b3f1ff3fc8bf01240c40012c18d115eb0b0515d4dc09042c12d86c090e2c0fd805eb850409eb84145d08db07665d6ff00f03174604bf0f23574633403b4363550cf10303b3fbfef5002405fb1e334344b3f1ff3fc8bf01240c40012c7ff471af15eb0b053ff56dafdc09042c3ff669af6c090e2c3ff665af05eb850409eb84145d08db07665d6ff00f03174604bf0f23574633403b43635553e7029a08eb0e010beb0203b1f5207f0d4628eae872a8bf4ff42075b3f5f07fa8bf4ff4f07395421edd039e109f5b457ff606aeddf830e0def800400f2c3ff6ffada3eb0b0c009ba5eb020803ebc40523f83420621ca5f802b0a5f80480a5f806c0cef80020ebe5039e109fe8e5062a3cbf4ff0ff3070472de9f04fadf2944d9a46154688460f90fbf7fbfe002800f0d68098f80510881d854240f0298446f6ac10c2f20700d0f800b0bbf1000f00f01f849bf80c009bf80d209bf80e309bf80f7040ea022040ea034040ea0769a9f1810010f1800f4ff0ff30c0f0b2809bf810209bf811309bf8137042ea03229bf8123042ea034242ea07620692813a12f5817f40f29e800029cdf828b000f09d8008f1060b002426aa4ff4e01c00270ae000bf05f1004042f8240001378f4204f1010480f08e801bf80750681e1e28f0d968b21f2801dd012075e0a5f1c2000beb07061d28a1eb07000fd8012800f0c88396f9010010f1410f00f3c28300f03f0365f38a1302201d465de005f0f003e02b24d10328c0f0b48396f9013013f1410f00f3ae8396f9020010f1410f00f3a883e02ddeb202d1a02ec0f0a283ed2d02d19f2e00f29d832b039bb206f03f0643ea861300f03f001d18032034e004284ff0ff3036d3a5f1f50313f1050f31d396f9013013f1410f2cdc96f9020010f1410f00f37e8396f9036016f1410f00f37883f02ddbb202d1902bc0f07283f42d02d18f2b00f26d830cea854e03f03f054eea053300f03f0043ea801006f03f0326aac518042000bf074442f8245076e74ff0ff300df2944dbde8f08f0021012007915ee0002c57d0a4f1010bcdf86090cdf810a000250df1780a5f46914608e026aaab4505f1010509f10409a7f1010739d052f825100029f3d400202b4687421dd009eb80067268b2f1ff3f18dc5a1c934514d0b268b2f1ff3f11dc9a1c93450dd0f268b2f1ff3f0adcda1c934506d032690433002a00f10400e0d400e000220a9853461aae009601f0ecf8002800f008831a980028c3d046f2a1415046c0f24e018847bce7ddf810a0ddf828b0ddf860900020079404e00020ddf828b00790012098f804400f21a20604f00f0501eae271694389225143c90a8df8681001260e2198f800c098f801e098f8023098f8037014f0200f08bf0e2608bf012105fb01f102fb01f14fead1218df869104ff00d0108bf022105fb01f102fb01f14fead1218df86a104ff00c0108bf032105fb01f102fb01f14fead1218df86b104ff00b0108bf042105fb01f102fb01f14fead1218df86c104ff00a0108bf052105fb01f102fb01f14fead1218df86d104ff0090108bf062105fb01f102fb01f14fead1218df86e104ff0080108bf072105fb01f102fb01f14fead1218df86f104ff0070108bf082105fb01f102fb01f14fead1218df870104ff0060108bf092105fb01f102fb01f14fead1218df871104ff0050108bf0a2105fb01f102fb01f14fead1218df872104ff0040108bf0b2105fb01f102fb01f14fead1218df873104ff0030108bf0c2105fb01f102fb01f14fead1218df874104ff0020108bf0d2105fb01f102fb01f14fead1218df8751006fb05f102fb01f14fead1218df876104ff0000108bf0f2169435143c90a00284ff000008df877107ff4cdae43ea0720079a484403904cea0e290af10600571e00260df198080190cdf810a0029718e000bf46f2a1411ea8c0f24e0188479df87c009df87d10dde90ab9029fdde9072640ea01200df1980801369642814400f0ff8158f82610b1f1ff3f1edd33469f421fd008eb83004268b2f1ff3f1adc0133bb4216d08268b2f1ff3f13dc0133bb420fd0c268b2f1ff3f0cdc0133bb4208d002690133002ae2d404e001f01f000b38d2e7002258461eab19ad009500f0abff002800f0c781199900290d9100f0ba819df880209df881309df87e009df87f1052ea03259df8822040ea01279df8833010979df884704fea024208969df8856042ea0362069b4fea074747ea06672b4403eb2743039fcdf82c90a7eb030609eb22491aaf149500f05e810902109b014400f0030809f101004ff0010c099009f102000ceb530305900d980c934b1e0e930b9b0130121415900ceb510003eb020e9a1acde91180a1eb08001890d0074ff0000a17904feace70169013960ae014980af1010a824512981599139e0144159100f029810aeb06014b090e2befd810980028ecd00e9801eb810103280f984ff0000600eb811e80f0848011980028ded0dde90c105feac67c0afb01084fea560018f8000000f00f03c1b208bf0b09b3fa83f0400910ea141f14d106eb0901c80904280fd848081ef80020fb5c02f00f0b02f0f0021a431aafc90708bf4bea03120ef8002011980128b1d0701c400818f80000bcf1000fc1b24fea111308bf00f00f03d8b2b0fa80f0400910ea141f14d109983118c80904280fd848081ef80020fb5c02f00f0b02f0f0021a431aafc90708bf4bea03120ef80020119802283ff486afb01c400818f80000bcf1000f00f00f01c2b208bf1109b1fa81f0400910ea141f7ff474af05983318d80904283ff66eaf58081ef80020795c02f00f0602f0f0020a43db0708bf46ea01120ef800205de7ddf8548007e000bf18980436b04208f102083ff471af18f8011c0023c9b2102938bf012313ea141f09eb060c16d14feadc13042b12d84fea5c0309091ef80320795c02f00f0b02f0f0020a431aaf5feac97008bf4bea01120ef8032018f8010c00f00f01b1fa81f0400910ea141f15d10cf10103d809042810d858081ef80020795c02f00f03179d43ea011302f0f002002d08bf42ea01030ef8003098f80000c1b210294ff0000038bf012010ea141f15d10cf10203d809042810d8080959081ef80120385c02f00f0302f0f002169d0243002d08bf43ea00120ef8012098f8000000f00f01b1fa81f0400910ea141f8ad10cf10303d80904283ff685af58081ef80020795c02f00f03179d43ea011302f0f002002d08bf42ea01030ef8003072e700bf109809eb00011498b1f5207f06eb000229eae970a8bf4ff42071b2f5f07fa8bf4ff4f072ddf810a081427ff70fae26eae6739a427ff70aaedaf800700f2f3ff605ae019dd21a091a05ebc70625f83700781c7380b180f280caf80000f6e59df87c009df87d10079a40ea012001e64ff0ff300df2944dbde8f08f00200df2944dbde8f08f2de9f04f83b04ef66c654ff2a8464ff6245948f2c73bccf2ff05c2f229064ff6ff77cff6ff7900244ff0010ac0f2450b029002e0bff32f8fd847d6f80080bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168a94200f0a8804ff43770fcf7e4f9002800f0d280494642183d3182f8dc4282f8dd4282f8de4282f8df4282f8e04282f8e14282f8e24282f8e34282f8e44282f8e54282f8e64282f8e74282f8e84282f8e94282f8ea4282f8eb4282f8ec4282f8ed4282f8ee4282f8ef4282f8f04282f8f14282f8f24282f8f34282f8f44282f8f54282f8f64282f8f74282f8f84282f8f94282f8fa4282f8fb4282f8fc4282f8fd4282f8fe4282f8ff4282f8004382f8014382f8024382f8034382f8044382f8054382f8064382f8074382f8084382f8094382f80a4382f80b4382f80c4382f80d4382f80e4382f80f4382f8104382f8114382f8124382f8134382f8144382f8154382f8164382f8174382f8184382d1056080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f41457ff44fafbff35f8f00bf46e8000121b156e8001f4145f8d043e70446fef757f9002c18bf002805d102990120487103b0bde8f08fc4f8600101254ff4f07084f8645101904ff4207049f683570090c0f2470700200021002200230026b84740b184f864610298c4f86061457103b0bde8f08f0298057103b0bde8f08f0024cde700bf2de9f04f87b0dde91065924643ea0102ddf848b02a43d20722d14fea550800eb510900eb5304abf1010700252846564588bf384600eb0a01304401eb810100eb800009eb811104eb80104246f6f7f2ff013f781c05f10105e8d107b0bde8f08f4ff6fc722a4005f0030406926a1e002701920494cde902a005e000bf059f039801375f45e9d0fa435645059788bf0beb020707eb0a0202eb820200eb821cba1902eb8202042d00eb821e4ff00008c0f09c80ddf8049000bf40468b4288bf484600eb010200eb03044fea52001cf800706ff0010088ea000608f1010088bfa8196ff0020686ea080608f1020b88bf05eb060b6ff0030686ea080608f1030a4feac27288bf05eb060a07f00f06002a4fea540208bf3e091ef80270e40707f00f0407f0f007374408bf44ea06170ef80270421854081cf804401844d20704f00f064fea500208bf26091ef80240c00704f00f0004f0f004344408bf40ea06140beb01000ef8024042081cf802200beb0304c00702f00f064fea540008bf16091ef80020e40702f00f0402f0f002324408bf44ea06120ef800200aeb010042081cf802200aeb0304c00702f00f064fea540008bf16091ef80020e40702f00f0402f0f002324408bf44ea06120ef80020069808f104088045a9f104097ff46dafddf808a0109eddf848b0049c002c3ff446af6fea080047468b4288bf2f18781842081f441cf80220c0074fea57091ef8090002f00f0408bf140900f00f0200f0f0002044ff0708bf42ea0410049c0ef80900012c3ff423af08f10102a5eb08098b4288bfa9f102025418670802eb030ae4074fea5a041cf807701ef8042007f00f0002f00f0b02f0f00208bf380902445feaca77ddf808a008bf4bea00120498109eddf848b00ef80420049c02283ff4f6ae08f102008b4288bfa9f10300421854081cf8047000eb030cd2074fea5c021ef8020007f00f0400f00f0800f0f00008bf3c0920445feacc7708bf48ea0410049c0ef80200d2e600bf2de9f04f83b04ef66c664ff2a8474ff6245a48f2c738ccf2ff06c2f229074ff6ff74cff6ff7a00254ff0010bc0f24508cde9010103e000bfbff32f8fc047d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168b14200f0a8804ff43770fbf76cff002800f00581514642183d3182f8dc5282f8dd5282f8de5282f8df5282f8e05282f8e15282f8e25282f8e35282f8e45282f8e55282f8e65282f8e75282f8e85282f8e95282f8ea5282f8eb5282f8ec5282f8ed5282f8ee5282f8ef5282f8f05282f8f15282f8f25282f8f35282f8f45282f8f55282f8f65282f8f75282f8f85282f8f95282f8fa5282f8fb5282f8fc5282f8fd5282f8fe5282f8ff5282f8005382f8015382f8025382f8035382f8045382f8055382f8065382f8075382f8085382f8095382f80a5382f80b5382f80c5382f80d5382f80e5382f80f5382f8105382f8115382f8125382f8135382f8145382f8155382f8165382f8175382f8185382d1066080f80ab08483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff44fafbff35f8f00bf47e8000121b157e8001f4945f8d043e7019a029b01210172b2b3014631f81c2f9a422cd0c28b9a4229d0028c9a4226d0428c9a4223d0828c9a4220d0c28c9a421dd0028d9a421ad0428d9a4217d0828d9a4214d0c28d9a4211d0028e9a420ed0428e9a420bd0828e9a4208d0c28e9a4205d0028f9a4202d0428f9a4208d10121817603b0bde8f08f417203b0bde8f08f427a72b98288029b9a1a13b2b3f1ff3f05dd92b2022a24bf0122427601e00122027600224272029ac38882809a4284bf029ac28090f83c20029b21f81230511c01f00f0180f83c1003b0bde8f08f00bf2de9f04f89b0129f029003fb07f000294fea400b069300f0d58100200890029807924ff0010a4ff000084ff0000e002400264ff0000c0022009107e04ff0000c03244e4601328a4200f0c181bcf1000f40f0bb8110f80290022cefd0012c15d0ddf81cc0129b002c554640f095805fea191409f00f070596089700f09b80a34580f0a1804ff0010c002499e1ddf81cc0129bb9f1000f00f08980cb45c0f0828008980392069a5546abeb090b00eb001700010596cdf810b0019016e010f80810089e01f0f001314300f8081008f10108a9f101090125904524bf4ff000080ef1010eb9f1000f00f062819e4580f05a810eeb8e00002d0ceb8010dfd0a2eb0801b1eb590f88bf4fea590169b38c0800eb080b1cd000220bf10106a2eb840a6ff0030c00bf06eb0c020cf1040c0aeb0c040434d770177157719771f3d10cf10406dde9062c129b11f0030404d10ce0002611f0030408d0012c0bf8067004d05e44022c777018bfb770a9eb4109b9f1000f88440dd09045aed210f80810019c01f00f012143a9f10109002500f80810a8e74ff000099fe700bf56ea09260ed0b34580f088804ff0010c00244be74ff0000c022447e74ff0000c012401e14ff0010c002400263ee708980392069aabeb040b00eb00170001cdf810b0019015e000bf1af80800089900f0f00008440af8080008f10108013c0125904524bf4ff000080ef1010e002c00f0d2809e4580f0ca800eeb8e00002d0ceb801ae1d0a2eb0800b0eb540f88bf600880b381080aeb08091ed00022ab4609f10106a2eb81016ff0030c00bf06eb0c020cf1040c01eb0c050435d770177157719771f3d10cf10401dde9062c129b5d4610f0030604d10de0002110f0030609d0012e09f8017005d04944022e4f7018bf8f7000bfa4eb400480446cb19045afd21af80800019900f00f000843013c00250af80800aae700bf0024a1e708980392069aabeb060b314600eb00170001cdf810b00190059613e01af80800089e00f0f00030430af8080008f1010801390125904524bf4ff000080ef1010e00295ad09e4553d20eeb8e00002d0ceb801ae3d0a2eb0800b0eb510f88bf480880b384080aeb08091ed00022ab4609f10106a2eb84046ff0030c00bf06eb0c020cf1040c04eb0c050435d770177157719771f3d10cf10406dde9062c129b5d4610f0030404d10de0002610f0030409d0012c09f8067005d04e44022c777018bfb77000bfa1eb4001804479b19045b1d21af80800019c00f00f002043013900250af808000029add107e000bf0021a1e7aa464ff0010c002403e0aa4600244ff0000c0df1080b00999be80508059e3fe60120012106e04ff0010cb4fa84f08cf001014009bbfa8bf252091140084009b0bde8f08f014640f258304cf20732c2f20000c0f248021047b0b54ff2ac44c2f229040146206846f23f52c5f28802b2eb500f40f258304cf2e9124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf2de9f04188b046f24f461c46c0f24e0613460a46ddf83880002706f1ae05214627706770a770e77027716771a771e77127726772a772e77227736773a773e77327746774a774e77427756775a775e77527766776a776e77627776777a777e777a8470028c8f800705cd020786178a278e3780843104318434ff0010008d0e179a27911431ebf617a227a51ea020102d108b0bde8f081a07b14284ff00000f7d1217d8df8040041f0010121758df805008df806008df807008df808008df809008df80a008df80b008df80c008df80d008df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f0001a92046b047c8f8000018b1012008b0bde8f08106f1520120468847d8f80000002818bf012008b0bde8f081f0b500294ff6ff7e00f0af80012903d10022c90775d1a8e021f00102c2f1000c6ff0010241f221038418a578e7788eea05252cb26e0036b203ead43484ea450503ead6366c0086ea450624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead53585ea460686ea07273eb27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead636023286ea470703ead5360ceb0205023586ea470e95d10232c90733d0805c41f221038eea002001b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead23181ea400e1ffa8ef0f0bd2de9f04fcbb00646c07c8b468df80600b08954998df80700000a8df808008df80a10080af1898df80b00080a17468df80d000020012b8df805308df809b08df80c108df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f008df820008df821008df822004cd1307e96f81780a0f1090144f62510c2f29240a1fb0002891a02eb510100970df10500b8eb910f88bf4fea9108c8ebc80706f11a0400f1010a00f10509093021463a46f6f739f906f1210506f1280029460722f6f731f9284621460722f6f72cf920465146032207f10905f6f725f906f11d0049460422f6f71ff9f07d02289cbf0130f075009fbb450cd0b8f1240f13d94ff0ff304bb0bde8f08fbb454ff00905f07509d14df22d77abb20df10502c0f247070120f02115e002200df12304aab28df82300a01c0df105018df82470f6f7f3f8a81c4af28d5782b2c0f24607f02021460023b8474bb0bde8f08f2ab1a2fb0103002b1cbf0020704702fb01f0b7e50846abe570b50446d0f83c0142f665360d46c0f2440668b94af2d550cff6ff707844002122460023b0470028c4f83c0108bf70bd06f594718847d4f83c0106f1e8022946bde87040104700bf2de9f04f81b0934612788ab1022300bf0beb030717f8012c62b11bf8032062b17a784ab1ba780433002af1d1023b04e0002205e0013b00e0013303eb4302520042eae272931c02f1010c01eb810203f0030e23f0030a00eb8213a3f5a0784ff0ff3906e009f10109b9f10d0f08f5a0786bd009eb010202eb8202bcf1030f00eb821605d24ff0ff34bef1000f2ed1e9e70024474604e000bfa24507f1020721d0621cd209042a03d83a7802f0f0023a70a21cd209042a03d87a7802f00f027a70e21cd209042a03d87a7802f0f0027a700434e209042adfd86208b55c05f00f05b554d9e7013cbef1000fbbd0a21cd509042d08d85208b55ce7076ff00f0708bf0f273d40b554bef1010fabd0e21cd509042d09d85208b55ce7074ff00f0708bf6ff00f073d40b554bef1020f9ad0221dd509042d96d85208e407b55c6ff00f0408bf0f242c40b4548ce79bf80010cdf800b0002900f0ce80022041f2410a41f2420b41f2430808e0009a063012f8011f03330029009200f0bd80a1f17f0717f15f0ff1d340f65c42c0f2000201eb41017a4402eb81014ff20014a1f5c079cff6ff7402e014f52074ded0494619f9027bc509042d09d8b7f1ff3f06dc1a1992f8016f46f0f00682f8016f5ffa87fe5fea4e6208d5042d06d81a1992f8017f47f00f0782f8017f5fea8e620ad540f27d22904206d81a1992f8027f47f0f00782f8027f5feace6209d5b0f51f7f06d81a1992f8027f47f00f0782f8027f5fea0e724fea900c09d5bcf19e0f06d81a1992f8036f46f0f00682f8036f5fea4e720ad540f27a22904206d81a1992f8037f47f00f0782f8037f91f90110042d09d8b1f1ff3f06dc1a1912f80a7047f0f00702f80a70c9b24a0608d5042d06d81a1912f80a7047f00f0702f80a708a060ad540f27d22904206d81a1912f80b7047f0f00702f80b70ca0609d5b0f51f7f06d81a1912f80b7047f00f0702f80b700a0709d5bcf19e0f06d81a1912f8087047f0f00702f8087049077ff565af40f27a2188423ff660af191911f8082042f00f0201f8082057e701b0bde8f08f00bf2de9f04f81b0134681464a1c00f08f808e0a4ff0000b19f80b20002a00f0888109eb0b077a78002a00f00a81ba78002a00f00981fa78002a00f008813a79002a00f007817a79002a00f00681ba79002a00f00581fa79002a00f004813a7a002a00f003817a7a002a00f00281ba7a002a00f00181fa7a002a00f000813a7b002a00f0ff807a7b002a00f0fe80ba7b002a00f0fd80fa7b002a00f0fc803a7c002a00f0fb807a7c002a00f0fa80ba7c002a00f0fb80fa7c002a00f0fc803a7d002a00f0fd807a7d002a00f0fe80ba7d002a00f0ff80fa7d002a00f000813a7e002a00f001817a7e002a00f00281ba7e002a00f00381fa7e002a00f004813a7f002a00f005817a7f002a00f00681ba7f002a00f00781fa7f002a00f008810bf1200bbbf1600f7ff477af4ff0600b00e1002119f80120002a00f0af8109eb01056a78002a00f0b080aa78002a00f0b180ea78002a00f0b2802a79002a00f0b3806a79002a00f0b480aa79002a00f0b580ea79002a00f0b6802a7a002a00f0b7806a7a002a00f0b880aa7a002a00f0b980ea7a002a00f0ba802a7b002a00f0bb806a7b002a00f0bc80aa7b002a00f0bd80ea7b002a00f050812a7c002a00f04e816a7c002a00f04c81aa7c002a00f04a81ea7c002a00f048812a7d002a00f046816a7d002a00f04481aa7d002a00f04281ea7d002a00f040812a7e002a00f03e816a7e002a00f03c81aa7e002a00f03a81ea7e002a00f038812a7f002a00f036816a7f002a00f03481aa7f002a00f03281ea7f002a00f03081203160297ff479af32e10bf1010b74e00bf1020b71e00bf1030b6ee00bf1040b6be00bf1050b68e00bf1060b65e00bf1070b62e00bf1080b5fe00bf1090b5ce00bf10a0b59e00bf10b0b56e00bf10c0b53e00bf10d0b50e00bf10e0b4de00bf10f0b4ae00bf1100b47e00bf1110b44e00131f6e00bf1120b3fe00231f1e00bf1130b3ae00331ece00bf1140b35e00431e7e00bf1150b30e00531e2e00bf1160b2be00631dde00bf1170b26e00731d8e00bf1180b21e00831d3e00bf1190b1ce00931cee00bf11a0b17e00a31c9e00bf11b0b12e00b31c4e00bf11c0b0de00c31bfe00bf11d0b08e00d31bae00bf11e0b03e00e31b5e00bf11f0bb1f5205f4ff0010100931fd364234ff47a7442f2107700bf01eb81025200b6fbf2f50a2d11d301fb03f2b6fbf2f50a2d0bd301fb04f2b6fbf2f50a2d05d37943b6fbf1f2092ae7d800e011465846bbf15f0f98bf5f20c0f1000c1ceb0b0a53d0b6fbf1f74cf6cd4207f13003ccf6cc4209f80b300a29a1fb023547d31af1010342d007fb11674fead508b7fbf8fe48f21f520ef1300309eb0b06c5f2eb1273706429a1fb023434d31af102032cd00efb18776409b7fbf4f544f6d35205f13003c1f26202b370b1f57a7fa1fb022323d31af1030218d005fb14729b0941f25975b2fbf3f7cdf2b715a1fb055407f13005f57007fb132642f210720bf1040b91424fea5431a9d209e0834607e00bf1010b04e00bf1020b01e00bf1030b0020009b09f80b002de00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d849444ff03f020a7003d1002189f85f1001e000204870002119f80100002800f0a18009eb0105687800285ed0a87800285dd0e87800285cd0287900285bd0687900285ad0a879002859d0e879002858d0287a002857d0687a002856d0a87a002855d0e87a002854d0287b002853d0687b002852d0a87b002851d0e87b002850d0287c00284fd0687c00284ed0a87c00284dd0e87c00284cd0287d00284bd0687d00284ad0a87d002849d0e87d002848d0287e002847d0687e002846d0a87e002845d0e87e002844d0287f002843d0687f002842d0a87f002841d0e87f002840d0203160297ff498af44e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2905d849444ff02f00087040f09580002089f85f00581c00f094809d0a4ff0000e19f80e00002800f08d8109eb0e067078002800f00f81b078002800f00e81f078002800f00d813079002800f00c817079002800f00b81b079002800f00a81f079002800f00981307a002800f00881707a002800f00781b07a002800f00681f07a002800f00581307b002800f00481707b002800f00381b07b002800f00281f07b002800f00181307c002800f00081707c002800f0ff80b07c002800f00081f07c002800f00181307d002800f00281707d002800f00381b07d002800f00481f07d002800f00581307e002800f00681707e002800f00781b07e002800f00881f07e002800f00981307f002800f00a81707f002800f00b81b07f002800f00c81f07f002800f00d810ef1200ebef1600f7ff477af4ff0600e05e100204870581c7ff46caf002119f80100002800f0aa8109eb01025078002800f0b0809078002800f0b180d078002800f0b2801079002800f0b3805079002800f0b4809079002800f0b580d079002800f0b680107a002800f0b780507a002800f0b880907a002800f0b980d07a002800f0ba80107b002800f0bb80507b002800f0bc80907b002800f0bd80d07b002800f04b81107c002800f04981507c002800f04781907c002800f04581d07c002800f04381107d002800f04181507d002800f03f81907d002800f03d81d07d002800f03b81107e002800f03981507e002800f03781907e002800f03581d07e002800f03381107f002800f03181507f002800f02f81907f002800f02d81d07f002800f02b81203160297ff479af2de10ef1010e74e00ef1020e71e00ef1030e6ee00ef1040e6be00ef1050e68e00ef1060e65e00ef1070e62e00ef1080e5fe00ef1090e5ce00ef10a0e59e00ef10b0e56e00ef10c0e53e00ef10d0e50e00ef10e0e4de00ef10f0e4ae00ef1100e47e00ef1110e44e00131f1e00ef1120e3fe00231ece00ef1130e3ae00331e7e00ef1140e35e00431e2e00ef1150e30e00531dde00ef1160e2be00631d8e00ef1170e26e00731d3e00ef1180e21e00831cee00ef1190e1ce00931c9e00ef11a0e17e00a31c4e00ef11b0e12e00b31bfe00ef11c0e0de00c31bae00ef11d0e08e00d31b5e00ef11e0e03e00e31b0e00ef11f0e0122b3f5205f1ed364234ff47a7442f2107602eb82004100b5fbf1f00a2811d302fb03f1b5fbf1f00a280bd302fb04f1b5fbf1f00a2805d37243b5fbf2f00928e7d800e00a46f046bef15f0f98bf4ff05f08c8f1000c42f2107a1ceb0e074dd0b5fbf2f30a2a03f1300009f80e004cf6cd40ccf6cc40a2fb000641d3781c3dd003fb1253f608b3fbf6f409eb0e0504f13000687048f21f50c5f2eb10642aa2fb000130d3b81c29d004fb16334e09b3fbf6f4b2f57a7f04f13000a87044f6d350c1f26200a2fb000120d3f81c16d004fb1630890941f25974b0fbf1f3cdf2b714a2fb044603f13004ec7003fb11050ef1040e52454fea5632afd209e0c64607e00ef1010e04e00ef1020e01e00ef1030e002009f80e0001b0bde8f08f0f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d849444ff03f00087005d1002089f85f0001b0bde8f08f0020487001b0bde8f08f434d01ffff0000000000000052420100ff00000008031000000000000000000046616365636c61772f31330000000000000000000000000000002020202020002020000000505050000000000000000000005050f85050f85050000000002070a8a07028a8702000000048a85010202854480000000020505020689090680000002020200000000000000000000010204040404020100000000040201010101020400000000000005020f82050000000000000002020f82020000000000000000000000020204000000000000000f800000000000000000000000000202000000000080810102020404000000000708898a8c88888700000000020602020202020700000000070888808102040f8000000007088083008088870000000000818284888f8080800000000f88080f00808887000000000708080f08888887000000000f808081010202020000000007088887088888870000000007088888878080870000000000000202000002020000000000000202000002020400000000008102040201008000000000000f80000f8000000000000004020100810204000000000708888102000202000000000708898a8a89880780000000070888888f888888800000000f08888f0888888f000000000708880808080887000000000e0908888888890e000000000f88080f0808080f800000000f88080f0808080800000000070888080b888887000000000888888f888888888000000007020202020202070000000003810101010909060000000008890a0c0c0a090880000000080808080808080f80000000088d8a8a888888888000000008888c8a89888888800000000708888888888887000000000f0888888f080808000000000708888888888a87008000000f0888888f0a0908800000000708880700808887000000000f8202020202020200000000088888888888888700000000088888850505020200000000088888888a8a8d88800000000888850202050888800000000888850502020202000000000f8081020408080f8000000007040404040404070000000004040202010100808000000007010101010101070000000205088000000000000000000000000000000000000f80040200000000000000000000000000000700878888878000000008080f088888888f00000000000007088808088700000000008087888888888780000000000007088f8808078000000001820702020202020000000000000788888888878087000008080f08888888888000000202000602020202070000000080800180808080808483000004040485060605048000000006020202020202070000000000000f0a8a8a8a8a8000000000000f08888888888000000000000708888888870000000000000f088888888f0808000000000788888888878080800000000b8c0808080800000000000007880700808f00000000020207020202020180000000000008888888888780000000000008888505020200000000000008888a8a8a870000000000000885020205088000000000000888888888878087000000000f810204080f800000000182020402020201800000000202020202020202000000000601010081010106000000048a8900000000000000000", + "new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf80b5002a18bf10f0030301d186460ce000f1010c864600bf013a0ef8011b04d01cf003030cf1010cf6d1102a11d35ffa81fc4ff001330cfb03fc00bf103acef80cc0cef808c0cef804c04ef810cb0f2af4d8042a0bd35ffa81fc4ff001330cfb03fc00bf043a032a4ef804cbfad8002a08bf80bdaef10103013a03f8011ffbd180bd00bf2de9f04fc5b04ff62c484ef66c694ff29c5b1446cff6ff78ccf2ff09c2f2290b4ff6ff75002701260d93cde90e0106e048f2a351c0f24501bff32f8f8847dbf800a0bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0b6fa002800f0078241464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001f51457ff436afbff35f8f4be8000121b15be8001f5145f8d02be70f99012641ea04410024c0f8701206e048f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0daf9002800f02b8141464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb9427ff436afbff35f8f4be8000121b15be8001fb942f8d02be7d0f874a242f6f766c0f24406baf1000fbff35f8f4ad006f19a0250464ff0ff314ff0ff379047002840f09d810f98002861d00e980778a7f1170002285bd80f980c2840f086810e9e7078b178084300f08081f0780138022800f27b81b279f379747a42ea0322337a307942ea0342717942ea0463b27af67a192f42ea062200f01082172f40f02682002a40f06281fe2800f07786812800f06a86002840f07d8651ea030040f0558100f0c7be04460020b047e8b104f51d7151e800afbaf1000f0cd1bff35f8f00bf41e80002002a00f0f58051e800afbaf1000ff5d00021bff32f8f0029bff35f8f00f0e480824692e74ff0ff3737e147f6df40c0f24f008047002701241197adf8487006e048f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0c1804ff4757007f080f8002800f0b98041464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009084728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff436afbff35f8f4be8000159b15be8001fb142f8d02be706f5a9718847ace6012110e700200e9999b10f9a8ab1097801f07f02b2f10b0618bf0126b2f11c0718bf01278a2924dc0b2926d01c2924d03fe00d9010a804f0a7fa4ff0ff3701249df84900002810a818bf4ff0ff3704f0e3fb054644b99df8480028b94af21f30c0f247004c3080470d98002818bfc56013e09c2901d08b291bd160b14af21f31c0f247010446884794f8641119b994f80413204669b14ff0ff3742f6f760c0f2440000f5807150468847384645b0bde8f08f0d9010a806ea070404f065fa0e98017801f07f061e2e04d10f9908f086fb0746b5e71d2e01f07e0018bf1a2811d1dbf800100322cff680720a40b2f1005fbff35f8f0ad10a684a4504bf002281f8812303e0a6f115010129e9d90f2e0adc092e00f37381052e00f0ef82072e40f01c8300279ae0142e00f32182102e00f0eb82112e40f011830f98022841f0d5800e990878112841f0d0804878002841f0cc8045f64d50c0f24600804701284ff000077ff46daf40f20830c0f210003590ff208df8d00044f25220c0f20100339040f2c960c0f24800cde93677804798b148f27e30c2f2070000880122c1b264298df8cf2008d80721b0f5807f38bf03218df8cf108df8d00040f2d260adf8d800052035ae8df8da00f01d33a90522fff7dbfa4ff22507c0f247070120092132460c23b84774e700294ff0ff3708bf002b7ff453af1328bff450af102a4ff0000138bf01211228b2fa82f218bf510901297ff443af00f0b3bc182f4ff0ff3708bf00297ff43aaf002b7ff437af352800f33684002800f08b84012800f09784312840f08984082abff428af00f098bc48f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f019864ff4757006f081fe002800f0118641464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470af0121c0f8009081728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff435afbff35f8f4be80001002900f061855be8001fb142f6d028e70a2e00f088810b2e40f0a981dbf800500320cff680702840b0f1005fbff35f8f41f008802868002748457ff40baed5f8700342f68d48c0f244080028a5f8807318bfc0470021a5f8b013c5f8ac13c5f8681185f8661185f86411d5f86c01c5f8601185f86511c5f86c11c5f80013a5f86c1320b148f2a351c0f245018847d5f87802002818bf04f04bfb0026c5f87862d5f8680320b148f2a351c0f245018847c5f86863a5f86c63286ca5f8446040b1c047286c08f166018847002804bf002028644bf63940c0f251008047dbf800700320cff680703840b0f1005fbff35f8f2ed1386848452bd197f877010026c7f8806188b149f25962c0f25902002044f2436187f8776190474ef6e378c0f25a08c04708f1bc008047d7f8840142f68d48c0f2440887f8706150b1c047d7f8840108f166018847002804bf0020c7f88401dbf800000321cff680710140b1f1005fbff35f8f40f04a860168494540f0468690f8a811002900f03b8607f095f800f03dbe152e00f0ec80162e40f0f0800f9801380228c0f0b3860e9a4ff0ff375078917840ea0120b0f5007f3ff653ad03210f9b01eb400199427ff44cad002700288df8d4708df8d5708df8d6708df8d7708df8d8708df8d9708df8da708df8db708df8dc708df8dd708df8de708df8df708df8e0708df8e1708df8e2708df8e3708df8e4708df8e5708df8e6708df8e7708df8e8708df8e9708df8ea708df8eb708df8ec708df8ed708df8ee708df8ef708df8f0708df8f1708df8f2708df8f3708df8f4708df8f5708df8f6708df8f7708df8f8708df8f9708df8fa708df8fb708df8fc708df8fd708df8fe708df8ff708df800718df801718df802718df803718df804718df805718df806718df807718df808718df809718df80a718df80b718df80c718df80d718df80e718df80f718df810718df811718df812718df813713ff4c8acd11c35ae01220c944b78012b00f215860f7847ea0323db08f45c07f0070702fa07f7274240f00185f45c013847ea0407f7540c9c01f10201e6d105f0d7fc002800f0f384002700f021bc0f980c94022835d30e98407833e0dde90e0107f0d4f8dee40f980228c0f0e88500f0a1fe002800f0e3850e994d78002d00f0a983022d00f2db8540f0548601460f982646062880f03c854ff0ff37344679e40e980f990130013908f098f8bae41d2e40f029810e980f990130013908f0a2fdb0e4ff20002701240b9006e048f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757006f0ecfb002800f0758341464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009084728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff436afbff35f8f4be8000121b15be8001fb142f8d02be790f84410ddf83880dde90b6461b1016c39b142f68d42c0f244020546084690472846002180f8441001210f9a032a0ed36eb998f80200082800f26a854bf63941c0f25101963188470027fff769bb0f9a052ac0f0e180012e40f0de8098f802000027411e06293ff65bab98f8031003293ff656ab98f80420002a3ff451ab4bf63943c0f2510303f59a7398470027fff747bb1a2818bf1c2e40f0978405f06efb002800f0928400f04bfd814608f08affb9f1000f4ff0ff3718bf00283ff430ab804647f63400c2f20700d0f81806d9f874131b2ea0eb0100c9f87c0300f054841a2e40f076840e9900254f1c0f994ff4f072a1f1010b4ff4a0714ff48053cde93b5035a84ff42076cde93535cde90950cde90121484639465a4643463d95cde90755cde90555cde90355009609f0d4fa002840f04a844ff48050cde93505d9f87c033946cde93c0535a80a90012009904ff4a07002904ff4f070019048465a4643464ff420773b95cde90755cde90555cde90355009709f0affafff719bb36286dd0462868d0572855d1002e7ff4f5aa65e00c294ff0ff377ff4efaab3f5005f5dd3fff7eaba481e1f284ff0ff373ff6e4aac1f51630834251d9fff7deba002b7ff4d9aa421f18bf0122b0f1350318bf01231a40b0f1590318bf01231a40b0f1470318bf0123134208bf012937d0b0f1370218bf0122b0f1c10318bf01231a4208bf02292bd09f284ff0ff377ff4b5aa042924d0fff7b1ba022e40f062814bf63940c0f2510080470027fff779ba022abff4a3aa13e0002a7ff49faaa0f1fd0102290cd397287ff498aa08e06ff0780002427ff492aa02e0402abff48eaa45f64d50c0f2460080470e990128c9784ff001004ff0010408bf022008424ff0000708d1fff77aba48f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757006f0d5f900283ff457aa41464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009084728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff436afbff35f8f4be8000121b15be8001fb142f8d02be74af21f36c0f247060546b04795f8640100287ff499a995f80403294600287ff493a90e98007817281bd0d1f8640342f68d47c0f2440768b947f61f20c0f20000a7f594747844012100220023a047c5f8640340b3a7f140024ff47a719047294608bb01f206300c460e990c22fef7e5fc0d98002184f805030120bff35f8f84f804034ff4f07001904ff4207006f5106400900020002200230027a04700283ff455a985f8047306f14c0080474ff0ff37fff74cb90f9a072ac0f06d81032e40f06a8198f8020098f8032098f8041040ea022098f8055098f8067001284ff0010698bf304644f62062904228bf10464bf63942c0f2510202f5e272642928bf6421904747f63400c2f207000068002800f0e88245ea0721012988bf0e4642f68d41c0f24401a1f14002314690470027fff7e0b8002780f8437145f64d50c0f24600804701287ff4d5a843f2a310c0f256008047fff716b9ddf83880dde90b64002100204be500200f9902290e992cbf4978ff21002800f0b58202294ff000072cd0012926d000297ff4b4a84cf67e21caf2101187760783cbf80410078147608583c583058445848584c584058545858585c585058645868586c5860587458780f83c7080f8b17380f8b073c0f8ac73fff790b801218172fff78cb88772fff789b848f2a351c0f24501bff32f8f8847dbf80040bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f007814ff4757006f011f8002800f0b58041464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470af0121c0f8009081728583c583058445848584c584058545858585c585058645868586c586058745875be8001fa1427ff435afbff35f8f4be8000100294fd05be8001fa142f7d029e70c9c4ff0ff37fef7aebf80460f98a14603284ff000073bd3301fb0fa80f04009084035d00f980e9a4cf6cd410338ccf6cc419478a0fb0101b4eb910f88bf8c082646302c28bf302608f14600d11c06eb8602fef72cfb002788f8446088f84570b4b1d8f8400038b9404600f0ccfa0028c8f8400000f07b8142f68d41c0f24401a1f140020121904700274c46fef76bbf4c46fef768bfd0f86c110027002900f05d814ff400624ff0ff3cb64605e02b681a44b2f5403f80f0c5808d18ab68b7eb532ff4d1dc081ef8044003f0070624fa06f6f6071ebf41f82370c5f808c0ef60e5e70e9fb878fa783b797f7940ea022043ea072232283590369280f0fa803220fee090f8a911002918bf06f056fadbf800000321cff680710140b1f1005fbff35f8f1cd10168494519d1d0f8b813b1b14ff00041bff35f8fc0f8b4134af21f30c0f2470000f510674ff4f07001904ff4207600200021002200230096b84795f84301002685f8436158b145f64d50c0f246008047012804d143f2a310c0f256008047d5f83c0195f84271c5f8386185f84261a5f8406150b1c047d5f83c0108f166018847002804bf0020c5f83c010120002fa87200f0cf804af27366c0f246060021002200230027b047fef7ccbe0f9803287ef4c8ae0e994ff4f07748788978424640ea012600200890cde90600cde90400cde902004ff4a0700190484631464ff42073009708f068fc002800f0a9804ff0ff37fef7a7be0f9801287ef4a3ae09f57170bff35f8f50e8001f013140e80012002af8d111a94846012208f087fcfef7d9be4ff0000c4ff400634ff0000ec0f800c30de0bef1000fc7f804c00cbfc0f80033cef804303344b3f5403fbe463bd2cf18ba68ce58013201d07746f3e7f218140c022ce6d88d18ac680134e2d12c6826443e602c682244140c022cdad88d18ac680134d6d12c6826443e602c682244140c022cced88d18ac680134cad12c6826443e602c682244140c022cc2d88d18ac680134bed12c6826443e602c682244b2f5403fcfd3b5e70c9c0027fef73abeb0f5fa6f0846344604d94ff4fa603590084634460121064680f8431145f64d50c0f24600804701281cd143f2a318c0f25608a8f188008047022d074608bf002f01d057b90ee04bf2110235a9c0f24b020220904730b10746c047002086f84301fef708be0027fef705be0c9cfef702be701ca9f86c030027fef7fcbd4bf639400027c0f2510088f8447080474c46fef7f1bd00bf2de9f04f81b04ef66c6b4ff29c554ff62c4848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0be804ff4757005f065fd002800f0b680414600bf4218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f800b080f80a908683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa1427ff43bafbff35f8f00bf45e8000129b155e8001fa142f8d02fe7002001b0bde8f08f70b500284dd001684ef66c62ccf2ff02914218bf70bd90f8441090f845504bf6394c8d42c0f2510c2ad205eb8501014491f8492091f84a4091f846e091f8473091f8481042ea0424012c4ff0010298bf14466e1c054680f84560b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f84410bde870406047e047286c38b142f64d42c0f244022146bde87040104770bd02464ff64b70cff6ff7042f6653c7844c0f2440c00210023604700bf2de9f04fcfb04ff29c57c2f229073c6840f2030a20464ef66c696ff39600cff6807accf2ff09b0f1005fbff35f8f40f0e3812068484540f0df8194f8510347f64c65c2f2070504f5417628b347f61800c2f20700016d4bf67c52c0f2720291421ad1406cc0b1d4f85c032968401a012802db04f095fc78b93046b8460df048fe50b142f69570c0f25b0080474746002084f8520384f8510394f8500368b1d4f860032968401a012802db04f079fc20b9002084f8500384f866013078bff35f8f70b194f84f83ac46b8f1000f01961cd094f8050394f81e13091a18bf012115e094f85103002840f0878194f85003002840f08281d4f86403002800f07d8142f68d41c0f24401884776e1012194f8070394f81133069094f8080394f81d23079094f8065394f80a0394f80b9394f80ce394f80db394f80ea394f80f63934235d194f8102394f81c339a422fd194f81b2396422bd194f81a23924527d194f81923934523d194f8182396451fd194f8172391451bd194f81623904217d194f8092394f815339a4211d194f81423079b93420cd194f81323069b934207d194f81223954203d111b994f805b3a9e00023012284f8203384f8233384f8243384f8253384f8263384f8273384f8283384f8293384f82a3384f82b3384f82c3384f82d3384f82e3384f82f3384f8303384f8313384f8323384f8333384f8343384f8353384f8363384f8373384f8383384f8393384f83a3384f83b3384f83c3384f83d3384f83e3384f83f3384f8403384f8413384f8423384f8433384f8443384f8453384f8463384f8473384f8483384f8493384f84a3384f84b3384f84c3384f84d3384f84e3384f81f2384f8215384f82203dcf8002047f61803170a84f8232384f82473170c120e84f826230822c2f2070384f8257384f84f231a6d4bf67c57c0f2720704f20631ba4204f212381bd15a6ccab140f6b803c2f200031a689ab1182d00f03784172d40f04384fe2804f2273c0d4600f06484002840f06e8401980df0c9fd294602e0022084f8200340460c22fdf7ccfe94f805b394f84f8384f81eb345f64d50c0f246008047811eb1fa81f14fea511a012808bf4ff0020ab8f1000f59d004f21f3009a9059040f261400631c0f2470004910ca9023100254ff00309a0f5b84000240391029014e0039809a91a468df830908df831b0fdf797fe47f00802029ff0200ca90023b8470334e0b24045b1462fd2a8eb0400c7b20420032f28bf4f468df8240006983a468df8250007988df827a08df8260005988df8284050fa84f104988df829808df82a508df82b508df82c50fdf769feb81dda45c3b24ff00306c2d14ef60b670120f02109aac0f24707c9e7bff35f8f01994ff29c574ef66c6940f2030a0020c2f22907ccf2ff09cff6807a08703d68bff35f8f05ea0a00b0f1005f55d12868484552d105f56d700021bff35f8f50e8006f40e80012002af9d147f64c60bff35f8fc2f207000468b6f1004f0fd0b84604f0b1fa58b1002e00f0658195f8cc03320a002800f0fb8195f8cd1304e295f8cc03e8b147f61800c2f207000068b8b145f60960c2f207000478002040f261430c9009900ca909aac0f247032046984709990c9a43f69b53c0f24e0320469847002085f8ce0385f8cc03d5f8b80320b142f68d41c0f244018847d7f80080bff35f8f08ea0a00b0f1005f43d1d8f8000048453fd198f8500338b1002088f86401c8f860014fb0bde8f08f98f86401f8b1d8f86041e4b140f6b800c2f20000056808a802f0c6fc002d00f005834e464ff62c49284621464ff41632cff6ff794ff6ff770795fdf7b5fd00244ff0010b20e098f8660170b1d8f8680140b147f64c61c2f207010968401a002802f39884002088f8660140f6a120c0f2470080474fb0bde8f08f48f2a351c0f24501bff32f8f88474ff29c50c2f229000568bff35f8f0068bff35f8f00ea0a01b1f1005f03d10168b14200f0c5804ff4757005f07bf9002802f05484494600bf4218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470af4ff29c52066080f80ab08783c783078447848784c784078547858785c785078647868786c78607874787c2f2290252e8001fa9427ff431afbff35f8f42e8000151b152e8001fa942f8d026e795f8cc03002840f0db80cbe6817a002942f09283027e0021002a8df8301000f023824ff29c550caac2f22905535c002b00f08d8253185f78002f00f04b829f78002f00f04982df78002f00f047821f79002f00f045825f79002f00f043829f79002f00f04182df79002f00f03f821f7a002f00f03d825f7a002f00f03b829f7a002f00f03982df7a002f00f037821f7b002f00f035825f7b002f00f033829f7b002f00f03182df7b002f00f02f821f7c002f00f02d825f7c002f00f02b829f7c002f00f02982df7c002f00f027821f7d002f00f025825f7d002f00f023829f7d002f00f02182df7d002f00f01f821f7e002f00f01d825f7e002f00f01b829f7e002f00f01982df7e002f00f017821f7f002f00f015825f7f002f00f013829f7f002f00f01182db7f002b00f00f82203160297ff47baf2ee20120a5f8cc0340f20220a5f8d0030220002185f8cf03c5f8c01302f00103c6f300208b42c6f34b2206d123b395f8d233f7b2bb421fd00ce05bb995f8cf13a5f8ca2385f8d013022185f8d113c5f8bc4311e095f8cf3385f8d03385f8d163a5f8ca23c5f8bc4331b9012185f8ce13c6f34951a5f8c813002804bf002185f8ce1385f8cd0385f8d26395f8ce13b9b195f8640160b1d5f8c403bff35f8fb5f8c8136ff39f20884205d195f8ce1339b195f8cf632fe0002085f8ce03c5f8bc43d5f8bc13b5f8ca0395f8d163611a814220d28902b1fbf0f04ff4406395f8d01300fb00f2a3eb40005043000d721a00fb02f34ff62c47002b4ff40073cff6ff7748bf07f5ea7310fb0230c21700eb925001eb9026002147f61800c2f207000068e0b1d5f8c003f4b2a0421ad0002040f261430c9009900ca909aac0f247032046984709990c9a43f69b53c0f24e0320469847c5f8c04395f8ce1302e00020c5f8c003d5f8b80385f8cf6311b14ff47a7108e095f8d113f2b28a424ff0280108bf4ff47a7142f68d42c0f24402403a904795f8ce034746a0b13868bff35f8f00ea0a01b1f1005f42f044820168494542f04082002180f86411c0f860114fb0bde8f08f95f8cd0300287ff451adf0b20228e2d140f6b800c2f2000001680029dbd001684ff416322139002301f8403f403a01f81f3c01f81e3c01f81d3c01f81c3c01f81b3c01f81a3c01f8193c01f8183c01f8173c01f8163c01f8153c01f8143c01f8133c01f8123c01f8113c01f8103c01f80f3c01f80e3c01f80d3c01f80c3c01f80b3c01f80a3c01f8093c01f8083c01f8073c01f8063c01f8053c01f8043c01f8033c01f8023c01f8013c4b708b70cb700b714b718b71cb710b724b728b72cb720b734b738b73cb730b744b748b74cb740b754b758b75cb750b764b768b76cb760b774b778b77cb7781f820309cd100684ef217310c904ff416300d900ca8c0f247018847474666e7002088f86401c8f8600188f86601012088f8650140f6a120c0f24700804702f09eb901980d460df001fb10b302304ff0030008bf022084f820032946fff7cebb84f85003dcf800204ef660231a44c4f86023b8b101980d460df0c6fc0df09afc294660b9fff7babb4ff29c55c2f2290595e00df08ffc002829463ff4afab0320fff7aabb002084f86601fff7a7bb18684a4600eb0b20704400eb0a4000eb06616046fdf76bfa0ee04eea0b2141ea0a4141ea066361464a460df08efa00281cbf032084f8200309f1080084f84f032946fff784bb01313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2921d84ff0520353541dd00a4445235e29537018d04f235d29937014d052235c29d37010d044235b2913710cd045235a29537108d052235929937104d020235829d37141f0058600218df88f100121427e002a00f0c28000220cab9f5c002f00f0a0809f187e78002e5ed0be78002e5dd0fe78002e5cd03e79002e5bd07e79002e5ad0be79002e59d0fe79002e58d03e7a002e57d07e7a002e56d0be7a002e55d0fe7a002e54d03e7b002e53d07e7b002e52d0be7b002e51d0fe7b002e50d03e7c002e4fd07e7c002e4ed0be7c002e4dd0fe7c002e4cd03e7d002e4bd07e7d002e4ad0be7d002e49d0fe7d002e48d03e7e002e47d07e7e002e46d0be7e002e45d0fe7e002e44d03e7f002e43d07e7f002e42d0be7f002e41d0ff7f002f40d02032602a7ff49aaf54e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a15d84ff053079f5411d013444b275e2a5f700cd049275d2a9f7008d050275c2adf7004d020275b2a1f7140f0f08100228df88f200131827e002a00f0be8000210caa535c002b00f0a08053185f78002f5ed09f78002f5dd0df78002f5cd01f79002f5bd05f79002f5ad09f79002f59d0df79002f58d01f7a002f57d05f7a002f56d09f7a002f55d0df7a002f54d01f7b002f53d05f7b002f52d09f7b002f51d0df7b002f50d01f7c002f4fd05f7c002f4ed09f7c002f4dd0df7c002f4cd01f7d002f4bd05f7d002f4ad09f7d002f49d0df7d002f48d01f7e002f47d05f7e002f46d09f7e002f45d0df7e002f44d01f7f002f43d05f7f002f42d09f7f002f41d0db7f002b40d0203160297ff49aaf50e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2911d84ff0440353540dd00a4455235e29537008d050235d29937004d020235c29d37040f0098100218df88f1001216a6846f23f53c5f28803b3eb520f40f08b80d20700f08d8000210caa535c002b00f0858153185f78002f00f0f9809f78002f00f0f980df78002f00f0f9801f79002f00f0f9805f79002f00f0f9809f79002f00f0f980df79002f00f0f9801f7a002f00f0fc805f7a002f00f0fc809f7a002f00f0fc80df7a002f00f0fc801f7b002f00f0fc805f7b002f00f0fc809f7b002f00f0fc80df7b002f00f0fc801f7c002f00f0fc805f7c002f00f0fc809f7c002f00f0fc80df7c002f00f0fc801f7d002f00f0fc805f7d002f00f0fc809f7d002f00f0fc80df7d002f00f0fc801f7e002f00f0fc805f7e002f00f0fc809f7e002f00f0fc80df7e002f00f0fc801f7f002f00f00d815f7f002f00f00b819f7f002f00f00981db7f002b00f00781203160297ff47baf1de14cf67e22caf210126a60002940f0198100210caa535c002b00f0dc8053185f78002f61d09f78002f60d0df78002f5fd01f79002f5ed05f79002f5fd09f79002f60d0df79002f61d01f7a002f62d05f7a002f63d09f7a002f64d0df7a002f68d01f7b002f69d05f7b002f6ad09f7b002f6bd0df7b002f6cd01f7c002f6dd05f7c002f6ed09f7c002f6fd0df7c002f70d01f7d002f71d05f7d002f72d09f7d002f73d0df7d002f74d01f7e002f75d05f7e002f76d09f7e002f77d0df7e002f78d01f7f002f79d05f7f002f7ad09f7f002f7bd0db7f002b7cd0203160297ff49aaface000211171f6e6013173e0023171e003316fe004316de0013184e0053169e0023180e0063165e003317ce0073161e0043178e008315de0053174e0093159e0063170e00a3155e007316ce000225a710fe60b314ee0083165e00c314ae0093161e00d3146e00a315de00e3142e00b3159e00f313ee00c3155e010313ae00d3151e0113136e00e314de0123132e00f3149e013312ee0103145e014312ae0113141e0153126e012313de0163122e0133139e017311ee0143135e018311ae0153131e0193116e016312de01a3112e0173129e01b310ee0183125e01c310ae0193121e01d3106e01a311de01e3102e01b3119e01f315f2931d84ff04f0353542dd00a444b235e29537028d020235d29937024d00021d17024e01c3104e01d3102e01e3100e01f315f2918d84ff04103535414d00a444c235e2953700fd05d2993700cd04f235c29d37008d043235b29137104d020235a29537141f0c38200218df88f1000210caf7a5c002a00f0a0807a185378002b5ed09378002b5dd0d378002b5cd01379002b5bd05379002b5ad09379002b59d0d379002b58d0137a002b57d0537a002b56d0937a002b55d0d37a002b54d0137b002b53d0537b002b52d0937b002b51d0d37b002b50d0137c002b4fd0537c002b4ed0937c002b4dd0d37c002b4cd0137d002b4bd0537d002b4ad0937d002b49d0d37d002b48d0137e002b47d0537e002b46d0937e002b45d0d37e002b44d0137f002b43d0537f002b42d0937f002b41d0d27f002a40d0203160297ff49aaf42e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2903d84ff077027a5403d100218df88f1002e0394400224a70c2684ff0000e17f80e10002900f0c48007eb0e035978002962d09978002962d0d978002962d01979002962d05979002962d09979002962d0d979002962d0197a002962d0597a002962d0997a002962d0d97a002962d0197b002962d0597b002962d0997b002962d0d97b002962d0197c002962d0597c002962d0997c002962d0d97c002962d0197d002962d0597d002962d0997d002962d0d97d002962d0197e002962d0597e002962d0997e002962d0d97e002962d0197f002962d0597f002962d0997f002962d0d97f002962d00ef1200ebef1600f7ff496af4ff0600e5be00ef1010e58e00ef1020e55e00ef1030e52e00ef1040e4fe00ef1050e4ce00ef1060e49e00ef1070e46e00ef1080e43e00ef1090e40e00ef10a0e3de00ef10b0e3ae00ef10c0e37e00ef10d0e34e00ef10e0e31e00ef10f0e2ee00ef1100e2be00ef1110e28e00ef1120e25e00ef1130e22e00ef1140e1fe00ef1150e1ce00ef1160e19e00ef1170e16e00ef1180e13e00ef1190e10e00ef11a0e0de00ef11b0e0ae00ef11c0e07e00ef11d0e04e00ef11e0e01e00ef11f0e01230a2a1ed364274ff47a7542f2107403eb83014900b2fbf1f60a2e11d303fb07f1b2fbf1f60a2e0bd303fb05f1b2fbf1f60a2e05d36343b2fbf3f10929e7d800e00b46f346bef15f0f98bf4ff05f0bcbf1000a0cad1aeb0e0c53d0b2fbf3f60a2b06f1300105f80e104cf6cd41ccf6cc41a3fb011447d31cf1010142d006fb1326e708b6fbf7f905eb0e0209f13001517048f21f51c5f2eb11642ba3fb011435d31cf102012dd009fb17666709b6fbf7f4b3f57a7f04f13001917044f6d351c1f26201a3fb015124d31cf1030524d004fb1767890941f25975b7fbf1f6cdf2b715a3fb055406f13005d57006fb117242f210710ef1040e8b424fea54330cada9d20ce0de460ae00ef1010e07e00ef1020e04e00ef1030e00e0de460cad002205f80e20a95c002900f0a080a9184b78002b5ed08b78002b5dd0cb78002b5cd00b79002b5bd04b79002b5ad08b79002b59d0cb79002b58d00b7a002b57d04b7a002b56d08b7a002b55d0cb7a002b54d00b7b002b53d04b7b002b52d08b7b002b51d0cb7b002b50d00b7c002b4fd04b7c002b4ed08b7c002b4dd0cb7c002b4cd00b7d002b4bd04b7d002b4ad08b7d002b49d0cb7d002b48d00b7e002b47d04b7e002b46d08b7e002b45d0cb7e002b44d00b7f002b43d04b7f002b42d08b7f002b41d0c97f002940d02032602a7ff49aaf50e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a11d84ff07501a9540dd0a91873235e2a4b7008d020235d2a8b7004d070235c2acb7040f09d8000218df88f1002694ff0000a15f80a10002900f0c78005eb0a035978002962d09978002962d0d978002962d01979002962d05979002962d09979002962d0d979002962d0197a002962d0597a002962d0997a002962d0d97a002962d0197b002962d0597b002962d0997b002962d0d97b002965d0197c002965d0597c002965d0997c002965d0d97c002965d0197d002965d0597d002965d0997d002965d0d97d002965d0197e002965d0597e002965d0997e002965d0d97e002965d0197f002965d0597f002965d0997f002965d0d97f002965d00af1200abaf1600f7ff496af4ff0600a5ee00af1010a5be00af1020a58e00af1030a55e00af1040a52e00af1050a4fe00af1060a4ce00af1070a49e00af1080a46e00af1090a43e00af10a0a40e00af10b0a3de00af10c0a3ae00af10d0a37e00af10e0a34e000220a7162e70af10f0a2ee00af1100a2be00af1110a28e00af1120a25e00af1130a22e00af1140a1fe00af1150a1ce00af1160a19e00af1170a16e00af1180a13e00af1190a10e00af11a0a0de00af11b0a0ae00af11c0a07e00af11d0a04e00af11e0a01e00af11f0a01230a2a1ed364274ff47a7542f2107403eb83014900b2fbf1f60a2e11d303fb07f1b2fbf1f60a2e0bd303fb05f1b2fbf1f60a2e05d36343b2fbf3f10929e7d800e00b46d646baf15f0f98bf4ff05f0ecef1000942f2107c0cad19eb0a0b51d0b2fbf3f40a2b04f1300105f80a104cf6cd41ccf6cc41a3fb011747d31bf1010140d004fb1326ff08b6fbf7f405eb0a0204f13001517048f21f51c5f2eb11642ba3fb015135d31bf102052dd004fb17664c09b6fbf4f7b3f57a7f07f13001917044f6d351c1f26201a3fb015124d31bf1030519d007fb1464890941f25976b4fbf1f5cdf2b716a3fb066705f13006d67005fb11420af1040a63454fea57330cadabd20ce0f2460ae0f24607e00af1010a05e00af1020a01e00af1030a0cad002205f80a20a95c002900f0a080a9184b78002b5ed08b78002b5dd0cb78002b5cd00b79002b5bd04b79002b5ad08b79002b59d0cb79002b58d00b7a002b57d04b7a002b56d08b7a002b55d0cb7a002b54d00b7b002b53d04b7b002b52d08b7b002b51d0cb7b002b50d00b7c002b4fd04b7c002b4ed08b7c002b4dd0cb7c002b4cd00b7d002b4bd04b7d002b4ad08b7d002b49d0cb7d002b48d00b7e002b47d04b7e002b46d08b7e002b45d0cb7e002b44d00b7f002b43d04b7f002b42d08b7f002b41d0c97f002940d02032602a7ff49aaf5ce001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a1dd84ff07501a95419d0a91873235e2a4b7014d020235d2a8b7010d074235c2acb700cd031235b2a0b7108d030235a2a4b7104d02023592a8b7140f0cb8500218df88f10d0f8ac234ff0000a15f80a10002900f0c48005eb0a035978002962d09978002962d0d978002962d01979002962d05979002962d09979002962d0d979002962d0197a002962d0597a002962d0997a002962d0d97a002962d0197b002962d0597b002962d0997b002962d0d97b002962d0197c002962d0597c002962d0997c002962d0d97c002962d0197d002962d0597d002962d0997d002962d0d97d002962d0197e002962d0597e002962d0997e002962d0d97e002962d0197f002962d0597f002962d0997f002962d0d97f002962d00af1200abaf1600f7ff496af4ff0600a5be00af1010a58e00af1020a55e00af1030a52e00af1040a4fe00af1050a4ce00af1060a49e00af1070a46e00af1080a43e00af1090a40e00af10a0a3de00af10b0a3ae00af10c0a37e00af10d0a34e00af10e0a31e00af10f0a2ee00af1100a2be00af1110a28e00af1120a25e00af1130a22e00af1140a1fe00af1150a1ce00af1160a19e00af1170a16e00af1180a13e00af1190a10e00af11a0a0de00af11b0a0ae00af11c0a07e00af11d0a04e00af11e0a01e00af11f0a01230a2a1ed364274ff47a7542f2107403eb83014900b2fbf1f60a2e11d303fb07f1b2fbf1f60a2e0bd303fb05f1b2fbf1f60a2e05d36343b2fbf3f10929e7d800e00b46d646baf15f0f98bf4ff05f0ecef1000942f2107c0cad19eb0a0b51d0b2fbf3f40a2b04f1300105f80a104cf6cd41ccf6cc41a3fb011747d31bf1010140d004fb1326ff08b6fbf7f405eb0a0204f13001517048f21f51c5f2eb11642ba3fb015135d31bf102052dd004fb17664c09b6fbf4f7b3f57a7f07f13001917044f6d351c1f26201a3fb015124d31bf1030519d007fb1464890941f25976b4fbf1f5cdf2b716a3fb066705f13006d67005fb11420af1040a63454fea57330cadabd20ce0f2460ae0f24607e00af1010a05e00af1020a01e00af1030a0cad079f002205f80a20a95c002900f0a080a9184b78002b5ed08b78002b5dd0cb78002b5cd00b79002b5bd04b79002b5ad08b79002b59d0cb79002b58d00b7a002b57d04b7a002b56d08b7a002b55d0cb7a002b54d00b7b002b53d04b7b002b52d08b7b002b51d0cb7b002b50d00b7c002b4fd04b7c002b4ed08b7c002b4dd0cb7c002b4cd00b7d002b4bd04b7d002b4ad08b7d002b49d0cb7d002b48d00b7e002b47d04b7e002b46d08b7e002b45d0cb7e002b44d00b7f002b43d04b7f002b42d08b7f002b41d0c97f002940d02032602a7ff49aaf48e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a09d84ff07501a95405d0a91873235e2a4b7040f08780044600208df88f00384602212a460cf0bbfad4f870c272208df8300078208df8310020204ff0000b1ffa8cf28df832008df833b015f80b00002800f0c88005eb0b035878002866d09878002866d0d878002866d01879002866d05879002866d09879002866d0d879002866d0187a002866d0587a002866d0987a002866d0d87a002866d0187b002866d0587b002866d0987b002866d0d87b002866d0187c002866d0587c002866d0987c002866d0d87c002866d0187d002866d0587d002866d0987d002866d0d87d002866d0187e002866d0587e002866d0987e002866d0d87e002866d0187f002866d0587f002866d0987f002866d0d87f002866d00bf1200bbbf1600f7ff496af4ff0600b5fe004460020887078e70bf1010b58e00bf1020b55e00bf1030b52e00bf1040b4fe00bf1050b4ce00bf1060b49e00bf1070b46e00bf1080b43e00bf1090b40e00bf10a0b3de00bf10b0b3ae00bf10c0b37e00bf10d0b34e00bf10e0b31e00bf10f0b2ee00bf1100b2be00bf1110b28e00bf1120b25e00bf1130b22e00bf1140b1fe00bf1150b1ce00bf1160b19e00bf1170b16e00bf1180b13e00bf1190b10e00bf11a0b0de00bf11b0b0ae00bf11c0b07e00bf11d0b04e00bf11e0b01e00bf11f0b01230a2a1ed364204ff47a7742f2107503eb83067600b2fbf6f40a2c11d303fb00f6b2fbf6f40a2c0bd303fb07f6b2fbf6f40a2c05d36b43b2fbf3f6092ee7d800e03346d946bbf15f0f98bf4ff05f09c9f1000a42f2107e1aeb0b004fd0b2fbf3f50ca905f1300401f80b404cf6cd41ccf6cc410a2ba3fb014742d3441c3ed005fb1326fd08b6fbf5f70ca901eb0b0248f21f5107f13004c5f2eb115470642ba3fb011430d3811c29d007fb15656609b5fbf6f7b3f57a7f07f13001917044f6d351c1f26201a3fb011420d3033016d007fb1650a10941f25975b0fbf1f4cdf2b715a3fb055604f13005d57004fb11020bf1040b73454fea5633add209e0cb4607e00bf1010b04e00bf1020b01e00bf1030bddf81c9000220cac04f80b20a05c002800f0a080a118487800285ed0887800285dd0c87800285cd0087900285bd0487900285ad08879002859d0c879002858d0087a002857d0487a002856d0887a002855d0c87a002854d0087b002853d0487b002852d0887b002851d0c87b002850d0087c00284fd0487c00284ed0887c00284dd0c87c00284cd0087d00284bd0487d00284ad0887d002849d0c87d002848d0087e002847d0487e002846d0887e002845d0c87e002844d0087f002843d0487f002842d0887f002841d0c87f002840d02032602a7ff49aaf54e001323ae0023238e0033236e0043234e0053232e0063230e007322ee008322ce009322ae00a3228e00b3226e00c3224e00d3222e00e3220e00f321ee010321ce011321ae0123218e0133216e0143214e0153212e0163210e017320ee018320ce019320ae01a3208e01b3206e01c3204e01d3202e01e3200e01f325f2a15d84ff02000a05411d0a01863215e2a41700cd072215d2a817008d063215c2ac17004d020215b2a017140f06d8100208df88f0037214fea1c7001eb1c71bcf1204f38bf00f13001ccf303608df8241000f137010a2838bf00f13001ccf303508df8251000f137010a2838bf00f13001ccf303408df8261000f137010a2838bf00f1300100208df828001f208df82710211811f81f2c002a00f0808011f81e2c002a7dd011f81d2c002a7bd011f81c2c002a79d011f81b2c002a77d011f81a2c002a75d011f8192c002a73d011f8182c002a71d011f8172c002a6fd011f8162c002a6dd011f8152c002a6bd011f8142c002a69d011f8132c002a67d011f8122c002a65d011f8112c002a63d011f8102c002a61d011f80f2c002a5fd011f80e2c002a5dd011f80d2c002a5bd011f80c2c002a59d011f80b2c002a57d011f80a2c002a55d011f8092c002a53d011f8082c002a51d011f8072c002a4fd011f8062c002a4dd011f8052c002a4bd011f8042c002a49d011f8032c002a47d011f8022c002a45d011f8011c002943d0215c002941d020307f287ff47baf60e01f383ae01e3838e01d3836e01c3834e01b3832e01a3830e019382ee018382ce017382ae0163828e0153826e0143824e0133822e0123820e011381ee010381ce00f381ae00e3818e00d3816e00c3814e00b3812e00a3810e009380ee008380ce007380ae0063808e0053806e0043804e0033802e0023800e001385f2821d8c0f15d0ec0f15e02c0f15f03c0f1600c2044002509ac675d4755b7b1ab4211d0661971784719797079b1aa420ad0b178b97051b1ae4505d0f178f97029b10435ac45e8d100208df88f0042f2941645f29c140caa48460e21c2f22006c2f213040bf06aff4ff6a42040f6b801cff6ff70c2f200010858a0426ad14ff6b020cff6ff700858b0f54d2f62d14ff6b420cff6ff700858a0425bd145f610634ff0000ec2f213034cf6394506f1040c00274ff0ff3263454cd8186830f003041ad09e4228bf0c2846d3f11a8c424ff0ff3143d8bc42394688bf2146c00703eb04001cbf0f46a644013d00f10403e2d130e00021417192e60421b7f5807f22d300223d46b5f5007f18d3b5f5806f17d3b5f5006f16d30901b2eb153f4fea1515f0d110e00022ca71fff734ba00219171fef73cbd00211172fef7fab9490002e0890000e0c900634504d1484207ea0002714603e04ff0ff324ff0ff3147f64c60c2f20700d0f8840006f1080342f6941c9842c2f2270c4cd142f610674ff0000bc2f2200746f6b9700cf1040e00244ff0ff3a4ff0ff3977453bd83e6836f0030515d0bc4528bf0c2e35d3aceb07039d4233d82346a54288bf2b46f6071cbf1c46ab447b19013803f10407e4d121e00420b4f5807f16d300262546b5f5007f0cd3b5f5806f0bd3b5f5006f0ad30001b6eb153f4fea1515f0d104e0400002e0800000e0c000774504d1404204ea000ad94603e04ff0ff3a4ff0ff394ff6882040f6b807cff6ff70c2f200073b580cf10800834264d14ff69423cff6ff73fb58b3f5333f5cd14ff69823cff6ff73fb58834255d14ff29c5043f21063c2f229004ff0000bc2f2270342f6f935a0f1040ca0f1080e0024634542d81e6836f003071ad09e454ff0ff30069028bf0c2e3ad3aeeb030087424ff0ff3034d82046a74288bf3846f6071cbf0446bb44d819013d00f10403dfd123e00427b4f5807f17d34ff0000e2546b5f5007f0cd3b5f5806f0bd3b5f5006f0ad33f01beeb153f4fea1515f0d104e07f0002e0bf0000e0ff00634505d17b4204ea03000690584602e04ff0ff300690064666208df8300072208df8310065208df832008df833002f208df834006d204b238df8350061208df8393069238df8360078208df83a30422356278df8370020208df83b303a238df83f7047270cac8df838008df83c308df83d004c238df840708df84200002720468df83e308df841308df843700bf05dff079de05d002800f0a080e019417800295ed0817800295dd0c17800295cd0017900295bd0417900295ad08179002959d0c179002958d0017a002957d0417a002956d0817a002955d0c17a002954d0017b002953d0417b002952d0817b002951d0c17b002950d0017c00294fd0417c00294ed0817c00294dd0c17c00294cd0017d00294bd0417d00294ad0817d002949d0c17d002948d0017e002947d0417e002946d0817e002945d0c17e002944d0017f002943d0417f002942d0817f002941d0c07f002840d02037602f7ff49aaf64e001373ae0023738e0033736e0043734e0053732e0063730e007372ee008372ce009372ae00a3728e00b3726e00c3724e00d3722e00e3720e00f371ee010371ce011371ae0123718e0133716e0143714e0153712e0163710e017370ee018370ce019370ae01a3708e01b3706e01c3704e01d3702e01e3700e01f375f2f25d84ff02000e05521d0e01945215e2f41701cd076215d2f817018d065215c2fc17014d06e215b2f017110d048215a2f41710cd07521592f817108d06221582fc17104d02021572f017240f0f88000208df88f002046494652460bf088fe0020215c002900f0a08021184a78002a5ed08a78002a5dd0ca78002a5cd00a79002a5bd04a79002a5ad08a79002a59d0ca79002a58d00a7a002a57d04a7a002a56d08a7a002a55d0ca7a002a54d00a7b002a53d04a7b002a52d08a7b002a51d0ca7b002a50d00a7c002a4fd04a7c002a4ed08a7c002a4dd0ca7c002a4cd00a7d002a4bd04a7d002a4ad08a7d002a49d0ca7d002a48d00a7e002a47d04a7e002a46d08a7e002a45d0ca7e002a44d00a7f002a43d04a7f002a42d08a7f002a41d0c97f002940d0203060287ff49aaf5be001303ae0023038e0033036e0043034e0053032e0063030e007302ee008302ce009302ae00a3028e00b3026e00c3024e00d3022e00e3020e00f301ee010301ce011301ae0123018e0133016e0143014e0153012e0163010e017300ee018300ce019300ae01a3008e01b3006e01c3004e01d3002e01e3000e01f305f281cd84ff02001215418d021184f225e284a7013d074225d288a700fd068225c28ca700bd065225b280a7107d072225a284a7103d0202259288a7128d100208df88f000cac069a204631460bf0bbfd28461a2122460bf038fc002088f86401c8f8600107984ef217310c904ff416300d900ca8c0f247018847012088f8660108a800f054f9c8f810004fb0bde8f08f0020c871d6e70021417207e700bf2de9f04f81b0009041f204024cf65560cef20002ccf2ac50c2f8ac0f4ef6fc50cef2000001684ef66c6641f08071016052f8040c4ff29c574ff62c4a48f2a358ccf2ff06c2f229074ff6ff7440f00100cff6ff7a00254ff0010bc0f2450842f8040c02e0bff32f8fc047d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168b14200f0ba804ff4757002f0b8fc002800f0f08051464218463182f8d45382f8d55382f8d65382f8d75382f8d85382f8d95382f8da5382f8db5382f8dc5382f8dd5382f8de5382f8df5382f8e05382f8e15382f8e25382f8e35382f8e45382f8e55382f8e65382f8e75382f8e85382f8e95382f8ea5382f8eb5382f8ec5382f8ed5382f8ee5382f8ef5382f8f05382f8f15382f8f25382f8f35382f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f819547ff470af066080f80ab08483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff43cafbff35f8f47e8000121b157e8001f4945f8d031e74169d1bb47f64c6245f6e466c2f20702cff6f8761368314617689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f421ed041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94206d041f20401cef200010968c91a416141f20400cef2000000680099086001b0bde8f08f00bf2de9f04f81b041f20401cef200014ef66c674ff29c56d1f8009000684ff62c4bccf2ff07c2f229064ff6ff74cff6ff7b00254ff00108009007e000bf48f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168b94200f0ba804ff4757002f080fb002800f00e8159464218463182f8d45382f8d55382f8d65382f8d75382f8d85382f8d95382f8da5382f8db5382f8dc5382f8dd5382f8de5382f8df5382f8e05382f8e15382f8e25382f8e35382f8e45382f8e55382f8e65382f8e75382f8e85382f8e95382f8ea5382f8eb5382f8ec5382f8ed5382f8ee5382f8ef5382f8f05382f8f15382f8f25382f8f35382f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f819547ff470af076080f80a808483c483048444848484c484048544858485c485048644868486c4860487448756e8001f51457ff438afbff35f8f46e8000121b156e8001f5145f8d02de74169009d61b144f6d350c1f26200a1fb0001880901284ff0010088bf88094ce047f64c6245f6e466c2f20702cff6f8761368314617689f420ed117689f420bd117689f4208d117689f4203d1002901f10401efd19f422fd041f20401cef200010b6817681168b9420ed11168b9420bd11168b94208d11168b94203d1002e06f10406efd1b94217d041f20401cef20001096844f6d357ca1ac1f26207a2fb07764261b00901284ff0010088bfb009994208bffa2001e0009dfa20a9eb0501b1fbf0f001b0bde8f08f2ab1a2fb0103002b1cbf0020704702fb01f0ffe7b0b54ff2a054c2f229040146206846f23f52c5f28802b2eb500f40f258304df681124cf67e25c2f20000c0f24802caf2101518bf25609047002804bf691c2160b0bd00bf0846ffe7014640f258304df69f22c2f20000c0f248021047002858d1002956d0082a54d30b78aa2b51d18b79f02b4ed12de9f04f83b00b2a4ed3c878a2f10803834249d14878212848d10879012845d14879012842d1c87900283fd10c4614f8080f10f03c0f39d18818884610f8021c10f8010c164641ea0025a2f10a0081b220460af0bdfa854228d145f64d50c0f246008047811eb1fa81f14f09012808bf0227002f00f03f81207887f00301084200f03b81012039464246334600f092fa0446002898f8080018bf06242ee14ff27143c0f24c0318470b2400e00a2445f64d50c0f246008047811eb1fa81f14d09012808bf0225002d00f011814ef66c6a4ff29c574ff62c480294ccf2ff0ac2f229074ff6ff74cff6ff7800264ff0010b06e048f2a351c0f24501bff32f8f8847d7f80090bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168514500f0bd804ff4757002f08df9002800f0de80414600bf4218463182f8d46382f8d56382f8d66382f8d76382f8d86382f8d96382f8da6382f8db6382f8dc6382f8dd6382f8de6382f8df6382f8e06382f8e16382f8e26382f8e36382f8e46382f8e56382f8e66382f8e76382f8e86382f8e96382f8ea6382f8eb6382f8ec6382f8ed6382f8ee6382f8ef6382f8f06382f8f16382f8f26382f8f36382f8f46382f8f56382f8f66382f8f76382f8f86382f8f96382f8fa6382f8fb6382f8fc6382f8fd6382f8fe6382f8ff6382f8006482f8016482f8026482f8036482f8046482f8056482f8066482f8076482f8086482f8096482f80a6482f80b6482f80c6482f80d6482f80e6482f80f6482f8106482f8116482f8126482f8136482f8146482f8156482f8166482f8176482f8186482f819647ff470afc0f800a080f80ab08483c483048444848484c484048544858485c485048644868486c4860487448757e8001f49457ff436afbff35f8f00bf47e8000121b157e8001f4945f8d02ae705eb450100eb011090f85d1201b300f513760024304629462a46032300940af041fa706840b14bf69b71c0f25d0188477068fff781fe74603068b475002818bffff77afe0020029c3060b060308200e0029c204603b0bde8f08f0624f9e700243842f6d0404631463a463b4600f076f9002818bf0446ece70b291dd3c278a1f10803934218d10278aa2a13d14278212a10d10279012a0dd14279012a0ad18279f02a07d1c2792ab9024612f8083f13f03c0f03d00a2070470b20704710b5084410f8023c10f8010c43ea0024a1f10a0081b210460af02ef9201a18bf0a2010bd002808bf7047b0b582b00446407cd0b100252046032300950af0d4f9606840b14bf69b71c0f25d0188476068fff714fe65602068a575002818bffff70dfe00202060a060208202b0bde8b040704700bf2de9f04f81b04ef66c654ff29c564ff62c498046ccf2ff05c2f229064ff6ff77cff6ff7900244ff0010a06e048f2a351c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168a94200f0bb804ff4757002f00af8002800f0b38049464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470af056080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f59457ff438afbff35f8f46e8000129b156e8001f5945f8d02de7002008eb480100eb0111002818bf01f5137001b0bde8f08fb0b5c4b0dc08202c03d94ff0ff3044b0b0bd0df107048df807008df80810a01c11461a461d46faf731fba81c4af6610582b2c0f24605f02021460023a84744b0b0bd00bf2de9f04f8bb04ef66c654ff29c564ff62c4948f2a35bccf2ff05c2f229064ff6ff77cff6ff7900244ff0010ac0f2450bcde90612cde9080303e000bfbff32f8fd847d6f80080bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168a94200f0ba804ff4757001f0f0fe002800f0578449464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470af056080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f41457ff43cafbff35f8f46e8000121b156e8001f4145f8d031e70999089d01eb410100eb011095f9081000f51376b1f1ff3f01f0030409dd374617f8110fd0bb3068002818bffff7eafb2fe3307cf0b1707ce0b10799099a002730460323009709f093ff706840b14bf69b71c0f25d0188477068fff7d3fb77603068b775002818bffff7ccfb00203060b06030823068002818bffff7c3fb00204ff480713060b0603182a978b081092006f11107f174b17434757076b07ca978884240f0db82317da14240f0d782069b411ca3f10b00727e02971e2828bf1e209042b17488bf7076981e84b200270a2c4ff00008c0f0e782092241f221090920039419e030684f46002818bffff78afbb089376001303774b760b081002c18bf4ff00608039cd146ddf814a00aeb0b0082b2a24280f0c782317c80b20429824600f28880aa5c00f1010a4b1c0129337405dd022906d0032970d1f28106e021b9727503e0338943ea02223281a24580f0aa8204296ed015f80a204ff0000c831c8f1c0129377406ddff290bd0032967460dd19a4652e000291cbf378947ea0222328100e072756746a34204d38be2f281a34280f08882022936d8ea5c00f1030acb1c0129337407dd022937d0fe2907d17275a2450cd376e231b9338943ea02223281a24504d36ee2f281a24580f06b8202292fd015f80a20031d41f00407fc29377408ddfd2909d0fe291cbf378947ea0222328103e079b9f28100e072756746a34280f0508209b19a4613e0ea5c00f1050a0520307401e09a466746f189308941ea0221f18128b1fff7c8fa0028306000f06a8233897089191a1ffa81fb1ffa8af1621a5a45b8bf93465fea0b4208d03268294410441ffa8bf2faf709f970893389584481b2994270817ff44faf0025cdf81080d6f800808df82b50707db8f1000f06930df12b0108bf88460f2806d8317d00f00302511ab1fa81f14d09000706d4317e707e814238bf30760fe000bf706840b14bf69b71c0f25d0188477068fff7a0fa7760707e30760120f082cdf814a09db1707db57d400714d4002d4ff0000400f0898106984ff6ff7381b2002900f00d8101295ed10020d2e0707d400753d4002478e14ff48030fff757fa002d804600f07081b07d002800f06c81b8f1000f00f068817568002d00f0008130890428c0f0608131680a1812f8043c002b40f0598112f8033c002b40f0548112f8023cff2b40f04f8112f8012cff2a40f04a814ff4803429606860c5f80c802c610028019000f067814bf69b70c0f25d0000f58572284600219047002840f033812969002900f02f8168688c420c46e7d1019a0c468242e3d125e14ff48030fff705fa044620e1069a4ff6fe701040a44642426ff0010000bf08eb0005ac78ed7883ea04231cb25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead7375c0087ea430324b25f003fb209ead43484ea430309ead73787ea430383ea05231fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead73787ea430309ead5355f0085ea43033fb25d002db209ead737023087ea430309ead5371518023587ea430395d1023000276446069ad20732d018f8000083ea002002b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead333420083ea400012b243001bb209ead23282ea400009ead33282ea4003f0899ab2824270d1099b4046faf7bff800286ad141f2210a4ff00009012375e03820fff744f900285ed00546077047708770c770077147718771c771077247728772c772077347738773c773077447748774c774077547758775c775077647768776c776077747778777c77780f8287080f8297080f82a7080f82b7080f82c7080f82d7080f82e7080f82f7080f8307080f8317080f8327080f8337080f8347080f8357080f8367080f837704ff2e310cff6ff70784428624ff22f20cff6ff70784468624bf69b70c0f25d0045f6e46200f13c0c28460f21c0f27c023823e04740b32846fff707f94446706841f2210a40b14bf69b71c0f25d0188477068fff7faf877604ff000090323b77506980799099a089dddf8108087b23046009709f0a0fc002c214604463ff455ad0846fff7e2f850e5756073e6c4f58030b4f5803f444606902f60ef607ff646aecde70799099a002430460323009409f082fc706840b14bf69b71c0f25d0188477068fff7c2f874603068b475002818bffff7bbf80020387000203060b06030824ff00a0840460bb0bde8f08f287a4006f8d5307c08b302980078e0b10799099a002430460323009409f055fc706840b14bf69b71c0f25d0188477068fff795f874603068b475002818bffff78ef800203060b06030824ff00b083068002818bffff783f800203060b0603082cae702980078e0b10799099a002430460323009409f029fc706840b14bf69b71c0f25d0188477068fff769f874603068b475002818bffff762f800203060b06030824ff00608a7e700bf2de9f041f0284bd100294ff00b0000f07f80022a7cd345f64d50c0f246000e4617468047811eb1fa81f14c09012808bf0224002c4ff00a006ad07578e91ec9b2fe2938bfbde8f081307833463946032860d002282dd001281cbf0a20bde8f081ac4204bf0020bde8f0814808842884bf0b20bde8f081881e03f1020887b240463946fff7b7f9002869d006462846fff70dfa21462a46fff7e1f93046bde8f08145f64d57c0f2460707f5e27cbde8f0416047ac421cbf0020bde8f081a1f1210010f1160f48d36ff0480048432330c0b2242841d81a4612f8020f40f00207032f4bd19b796340032b47d10b2904d0032804bf0b20bde8f081881e4ef60b6783b2c0f247070120f021b847002818bf0620bde8f081ac422dd1a1f10c0010f1030f1ad31a4612f8020f042815d158796040032811d1d879a0f1310717f1300f0bd39b79984208d9a1f10807c01a874284bf0b20bde8f081cfe70b20bde8f081b07a204207d04046394622462b46bde8f041fff7b2ba0020bde8f0810a20bde8f0814ff29c51c2f229010968bff35f8f0a466ff39602b2f1005f15d10a684ef66c63ccf2ff039a420ed1d1f8681159b147f64c62c2f207021268891a012903db0178042938bf7047002101707047b0b541f23154c0f252041d46a0474ff29c51c2f229010968bff35f8f0a466ff39602b2f1005f08d10a684ef66c63ccf2ff039a4218bf002100e00021421c18bf002900d1b0bd95f84d20002afad091f84321002a08bfb0bd46f25863c2f206031a68132aeed8c2ebc20403eb04135b6801ebc2014ff40042c1f8c031a1f8c6212a7a01f5e071002a0fbf802248f20112ca808122ab7c33b1ab7d23b1022342f002028b71ca7195f820303bb195f8243023b1032342f004028b71ca7195f846304bb195f8443042f008020b7195f84530ca714b71b0bd48f27342c2f207021378002b14bf9378ff230b711378002b14bf5278ff224a71b0bdf0b589b048f66502c0f24b020c46054690474ff29c51c2f229010e68bff35f8f31466ff39601b1f1005f08d131684ef66c62ccf2ff02914208bf092d01d009b0f0bde1082c29fad896f843110029f6d045f64d51c0f2460107468847014638460129ecd1ff22079244f6435246f258610020cff60172c2f20601089006920b685a1e132a4fd8c3ebc30501eb05111a1fa1f5de7191f85451ad0618d4d51c42d091f8e450ad0616d4951c3cd091f87450ad0613d4551c36d00d79ad0610d4043aa1f5e071151da3f10403e3d12be05a1e01f5a87104e09a1ee03101e0da1e703106ebc203096893f9c721b2f1ff3f1adc03f5e0731e688e4215d11e798df81b605e798df81c609b798df81e200a0a8df820108df821200a0c090e8df81d308df822208df823100f228df8012010228df80220522208218df80420b4f5804f4ff080028df800108df8061038bf4ff48042b4f5001f6ff07f0604f07f0138bf0026802c28bf80318df807109df809509df80a108df8030004d29df808200223002415e0c4f3c610b4f5804f024403d20323012400200be0b4f5001f03d204230224012003e0052303240120002135468df8053004f108038df808208df809508df80a1070b96846a222c118c254062048700c208870c81c06a90c22f9f73ffc04f117034ff225066a46c0f2470601200821b047384609b0f0bd70b582b00546ff208df8040044f25220c0f20100009040f2c960c0f2480016460c46804798b148f27e30c2f2070000880122c1b264298df8032008d80721b0f5807f38bf03218df803108df80400301b28bf08280cd3d221281929550621417005218170033069460522f9f7fbfb0834204602b070bd00bf024600284ff0ff3009d0022918bf70471078112801d1507810b14ff0ff30704710b586b045f64d50c0f24600804701284ff000003ed1cde9020040f20830c0f210000190ff208df8140044f25220c0f20100049040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8132008d80721b0f5807f38bf03218df813108df8140040f2d260adf80800052001ac8df80a00e01d04a90522f9f7a5fb4ff2250cc0f2470c0120092122460c23e04706b010bd4ff29c50c2f22900016808466ff39600b0f1005f16d108684ef66c62ccf2ff0290420fd1d1f8380160b147f64c62c2f207021268801a0028cebf01200020c1f838017047002070474ff29c50c2f22900026810466ff39600b0f1005f2ed110684ef66c61ccf2ff01884227d1d2f8680120b347f64c61c2f207010968401a0028c4bf01207047d2f86c1100200029c2f8680182f86601c2f86c01c2f80003a2f86c0308bf704780b548f2a352c0f24502084690470020bde88040704700207047f0b101684ef66c62ccf2ff02914218bf704790f8421199b110b54af273644ff0000cc0f24604864680f842c10120002100220023aef840c1a446bde810406047704700bf02464ff6ab70cff6ff7042f6653c7844c0f2440c00210023604700bf70b5002862d0044600796179a279e37940ea012040ea024050ea036007d00e2803d0032852d1032502e0042500e00225fff756ff002849d04ff29c50c2f22900066830466ff39600b0f1005f3ed130684ef66c61ccf2ff01884218bf70bd45f64d50a478c0f24600804701282ed1082086f86002032086f86102102086f86202b22086f86402062086f8650286f86602462086f8670243204ff0000e86f8680201204ef6057c06f5187286f863e286f8690286f86a5286f86b42c0f2470c09210d2386f86ce2bde87040604770bd00bf2de9f04f81b04ef66c694ff29c564ff62c480446ccf2ff09c2f229064ff6ff77cff6ff7800254ff0010a06e048f2a351c0f24501bff32f8f8847d6f800b0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168494500f0bc804ff4757000f072fe002800f0e18041464218463182f8d45382f8d55382f8d65382f8d75382f8d85382f8d95382f8da5382f8db5382f8dc5382f8dd5382f8de5382f8df5382f8e05382f8e15382f8e25382f8e35382f8e45382f8e55382f8e65382f8e75382f8e85382f8e95382f8ea5382f8eb5382f8ec5382f8ed5382f8ee5382f8ef5382f8f05382f8f15382f8f25382f8f35382f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f819547ff470afc0f8009080f80aa08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f59457ff437afbff35f8f00bf46e8000121b156e8001f5945f8d02be7002780f857712a270821032380f85871042780f8541180f85531102380f8597180f85a1101214ff2250c074600f5aa7280f8563180f85b1180f85c31002c18bf0124c0f2470c012010210a2387f85d4101b0bde8f04f604701b0bde8f08f00bf80b582b04ff0010ccdf800c000f002f802b080bd2de9f04381b04ff29c57c2f229073f684ff000093e466ff39606b6f1005f07d13e684ef66c65ccf2ff05ae4208bfb946012811d1ddf820801c4615460e460746fff7c2fd2346044631462a463846002c18bfb9f1000f07d14af2736cc0f2460c01b0bde8f043604799f8420180b1b8f1010f7cd1d9f83c01002189f84211a9f84011a0b342f68d41c0f2440188472ee0b9f840010121013080b2012898bf0846a9f84001d9f83c0142f68d44c0f2440489f8421128b94846fff708fec9f83c0140b1a047d9f83c01a4f140024ff4c8719047a8b199f84201002848d0d9f83c01002189f84211a9f8401100b1a0474af2736cc0f2460c0120002100220023afe745f64d50c0f24600804701282fd1082089f84401032089f84501102089f84601002089f84701b22089f84801062089f8490189f84a01462089f84b01432089f84c01012089f84d01b9f840014ef6057c070a09f5a27289f84e8189f84f01c0f2470c012009210d2389f8507178e701b0bde8f08380b582b04ff0050ccdf800c0fff73eff02b080bd2de9f04f83b04468002c00f0db828368002b00f0d78204eb030b4ff000090027002614e047f64c63c2f207031b6845f69077c0f201073b44c6f838310ceb0a0300275b451c464ff0000680f0bb82a04618f9013b03f07f05bd40002b46ea050610d406f00703012b16dc0bb3012b40f0a982abeb0803082bc0f0a48204f10903dee700bfd84580f09d82182f07f107074446dcd996e200bf022b3ad0052b40f09182abeb0803042bc0f08c82631dc7e7d84580f0878294f90130002b04f10203bed55b4580f07e8294f90230b3f1ff3f04f10303b4dc5b4580f0748294f90330b3f1ff3f04f10403aadc5b4580f06a8294f90430b3f1ff3f04f10503a0dc5b4580f0608294f90530002b00f15b82a31d96e7d84580f0568294f9013004f1020c002b03f07f0a1ed5dc4580f04b8294f90270e31c07f07f05b7f1ff3f4aeac51a02dde0469c460ee05b4580f03b8294f9037004f1040c07f07f05b7f1ff3f4aea853a33dd9846abeb0c039a4500f22a82f308672b1dd0652b7ff460afbaf1060ffff45caf9cf80030462b7ff457af98f80230432b7ff452af98f80330012b7ff44daf4ff29c54c2f229040290cde9002132e0044660460e4651461746654600f035fcac4631463a46204637e7dc4580f0f98194f90470631d07f07f05b7f1ff3f4aea455aaddc5b4580f0ec8194f90570002f00f1e78104f1060c4aea077ab1e748f2a3513046c0f24501bff32f8f8847dde900210298ac462768bff35f8f2668bff35f8f33466ff39603b3f1005f07d133684ef66c65ccf2ff05ab4200f0ce804ff47570654600f097fbdde9002106460298ac46002e3ff4f1ae4ff62c43cff6ff7300bff418463384f8d49384f8d59384f8d69384f8d79384f8d89384f8d99384f8da9384f8db9384f8dc9384f8dd9384f8de9384f8df9384f8e09384f8e19384f8e29384f8e39384f8e49384f8e59384f8e69384f8e79384f8e89384f8e99384f8ea9384f8eb9384f8ec9384f8ed9384f8ee9384f8ef9384f8f09384f8f19384f8f29384f8f39384f8f49384f8f59384f8f69384f8f79384f8f89384f8f99384f8fa9384f8fb9384f8fc9384f8fd9384f8fe9384f8ff9384f8009484f8019484f8029484f8039484f8049484f8059484f8069484f8079484f8089484f8099484f80a9484f80b9484f80c9484f80d9484f80e9484f80f9484f8109484f8119484f8129484f8139484f8149484f8159484f8169484f8179484f8189484f819947ff470af4ef66c63ccf2ff0333600123b3724ff6ff734ff29c54b383f38333847384b384f38433857385b385f38533867386b386f38633877387c2f2290454e8003fbb427ff41cafbff35f8f44e8006323b154e8003fbb42f8d011e798f8043098f8057098f80650032b47ea052437dd052b58dc042b78d0052b7ff41faed6f868313bb147f64c67c2f207073f68db1b002b18dcd6f86c3186f86691c6f86c91c6f80093a6f86c936bb10c4648f2a35180461846c0f24501174665468847ac4621463a46404647f64c63c2f207031b6845f69077c0f201073b44c6f86831ede5012b3ff4dfad022b00f08780032b7ff4e5ad174688466546fff7e2fa03460298ac4641463a46002b3ff4d8ad96f84231002b3ff4d3ad304641f28831a6f8404109f062f947e0062b4ad0072b7ff4c6ad80464df6db50c0f24a0016460f466446804703465d1ea446394632464046012d3ff6b4ad981eb0fa80f04009fff7a8fba446394632464046a8e5174688466546fff7a6fa03460298ac4641463a46002b3ff49cad96f84231002b3ff497adb6f84031a3427ff492add6f83c3186f84291002ba6f840913ff489ad42f68d411846c0f2440188470298ac4641463a467de5d6f86c31c6f86891002b86f86691c6f86c91c6f80093a6f86c933ff46fad0f4648f2a35104461846c0f24501164665468847ac463946324626e696f84231c6f83891002b3ff45aad0446d6f83c0165468846174686f84291a6f8409120b142f68d41c0f2440188474af273660120002100220023c0f24606b04720463a464146ac463be54ff2a513c0f2490303b0bde8f04f18477fb50646002806d1fff730fa002807d19de80f00064649f29b1cc0f24f0c60477fbd00bff0b583b01d46144609294ed106460f46f22d10d8a22160196155062141700b21817049f65c71c0f20001033079440b22f8f7a4fd0e35204629464ff4807200f03df80546ff208df8080044f25220c0f20100019040f2c960c0f24800804798b148f27e30c2f2070000880122c1b264298df8072008d80721b0f5807f38bf03218df807108df80800f82d0cd8d221601961550621417005218170033001a90522f8f76cfd0835394630464ef60577c0f2470722462b46b84703b0f0bdf0b587b00c464ff29c51c2f229010d68bff35f8f29466ff39601b1f1005f52d129684ef66c63ccf2ff0399424bd14d218df8041043218df8051001218df8061095f8701195f871318df807108df8083095f87511b5f878318df80b108df80c30190ab5f87a318df80d10190a8df80f1095f8761195f8737195f874618df80e3095f877318df8101045f64d51c0f246018df809708df80a608df81130064617468847d5f87c3101468df81210190a8df81410190c3a4630468df813308df81510190ea02328e04d218df8041043218df8051001218df80610002100238df807108df808108df809108df80a108df80b108df80c108df80d108df80e108df80f108df810108df811108df812108df813108df814108df815108df816108df817300023111b8df8183028bf18290cd3c222011902550620487015208870c81c01a91522f8f7bdfc1834204607b0f0bd00bf2de9f04f81b04ff29c50c2f229000468bff35f8f20466ff39600b0f1005f0dd120684ef66c63ccf2ff03984206d194f8700118b1002a18bf002902d101b0bde8f08f94f877010028f8d0d4f880010028f4d047f64c69c2f20709d9f80030c01a0128ebdb94f87101012802d14ff0010b06e094f873b1bbf1010f18bf4ff0020b94f876510020eb078846adf80200adf8000013d0bbf1020f10d194f8750168b96b4641f281570df10201c0f25b07404615461a46b8472a4694f876511646b2f5c86f28bf4ff4c86606f1150a5046174600f050f80028b1d0b7f5c86f88bf45f08005d9f80010d4f87c2153230271120a42710a0a8171c2710a0c090e03704d234172a02143700123c17200218370c570027280f80ab0017394f87511bdf802204173bdf800108373c173090a0174110a8174310a017500f115014274c6740746084641463246f8f713fc4ff27d12c0f24702384651469047d4f87c0148f2a3510130c4f87c01c0f245013846884701b0bde8f08fb0b54ff2a054c2f22904216846f23f52c5f28802b2eb510f48f25f514cf67e25c0f24501caf2101518bf25608847002804bf691c2160b0bd10b301684ef66c62ccf2ff0291421bd190f87711c1b1d0f88011b1b147f64c62c2f207021368cb1a012b0edb1268d0f88401002808bf7047891a42f64d42c0f2440208bf01211047704710b5002149f25962c0f8801180f87711c0f25902002044f2436190474ef6e374c0f25a04a04704f1bc00bde81040004700bf2de9f04f81b00429c0f00b810c46014600784d2840f005814878432840f001818878012840f0fd804ef66c684ff29c57ccf2ff08c2f229074ff6ff7600254ff0010b8a4607e048f2a351c0f24501bff32f8f88475146d7f80090bff35f8f3868bff35f8f02466ff39602b2f1005f03d10268424500f0c0804ff47570fff766ff002800f0ce804ff62c425146cff6ff728318463283f8d45383f8d55383f8d65383f8d75383f8d85383f8d95383f8da5383f8db5383f8dc5383f8dd5383f8de5383f8df5383f8e05383f8e15383f8e25383f8e35383f8e45383f8e55383f8e65383f8e75383f8e85383f8e95383f8ea5383f8eb5383f8ec5383f8ed5383f8ee5383f8ef5383f8f05383f8f15383f8f25383f8f35383f8f45383f8f55383f8f65383f8f75383f8f85383f8f95383f8fa5383f8fb5383f8fc5383f8fd5383f8fe5383f8ff5383f8005483f8015483f8025483f8035483f8045483f8055483f8065483f8075483f8085483f8095483f80a5483f80b5483f80c5483f80d5483f80e5483f80f5483f8105483f8115483f8125483f8135483f8145483f8155483f8165483f8175483f8185483f819547ff470afc0f8008080f80ab08683c683068446848684c684068546858685c685068646868686c6860687468757e8002f4a457ff432afbff35f8f00bf47e8000222b157e8002f4a45f8d026e7ca78022a04dc012a0dd0022a79d00ce0032a0dd0042a08d190f8701129b101b0bde8f04faae00d2c06d201b0bde8f08f044600f045f963e090f87721aab1002149f2596280f87711c0f259020446002044f2436190474ef6e375c0f25a05a84705f1bc008047204651460a790446012a88bf002280f871214a7902f00303012b4ff0010298bf134680f873318f79012f88bf002780f87471cf79022f88bf002780f875710f7a4e7a47ea0627502f98bf5027b7f5f07f28bf4ff4f077a0f878718f7ace7a47ea062741f28836b74238bf3e46a0f87a61097b80f8702101f0030180f8761103f0010101eb530180f872110021c0f87c1100f009f8204600f03ef8204601b0bde8f04f6ae000bfb0b5044690f87601800758bfb0bd94f871014ef633750128c0f25a0507d105f1b000804705f58b71002088470ee005f5b670804794f87101012801d1002004e094f87301013818bf0120a8474ff67922cff6ff7249f201537a44c0f25903002044f243619847012084f87701b0bd00bf70b5044647f64c60c2f20700006894f8771145f69075c0f2010500292844c4f8800108bf70bdd4f8840142f66536c0f2440658b94ff6d130cff6ff707844002122460023b047c4f8840150b106f594718847d4f8840106f1e8022946bde87040104770bd70b5044600f5c475082084f88801032084f88901102084f88a01c22084f88c01062084f88d014d2084f88f01432084f89001012084f8910194f8700194f8711184f8920184f8931194f87501b4f8781184f8960184f89711080ab4f87a1184f89801080a84f89a0194f8760194f8732194f8743184f8991194f8771184f89b0145f64d500026c0f2460084f88b6184f8942184f8953184f89c118047d4f87c1184f89d01080a84f89f01080c84f8a001080e84f8a101a02084f8a36115264ef6057c84f89e1184f8a201c0f2470c012009212a461c2384f88e61bde87040604770b54ff29c50c2f229000468bff35f8f20466ff39600b0f1005f33d120684ef66c61ccf2ff01884218bf70bd94f877010025c4f8805188b149f25962c0f25902002044f2436184f8775190474ef6e376c0f25a06b04706f1bc008047d4f8840184f8705170b142f68d45c0f24405a847d4f8840105f166018847002804bf0020c4f8840170bd00bf2de9f04182b04ff29c51c2f229010c68bff35f8f21466ff39601b1f1005f75d121684ef66c62ccf2ff0291426ed194f8a81179b394f8aa01c0070ad0d4f8680178b347f64c61c2f207010968401a002827dd47f24c45c2f2070555f8040c002854d042f29707c0f24c074af6e908002607f5437101a8c0f24b0801968847a8b1032084f8ab612860b4f8ac01c04702b0bde8f081d4f8b411c1b302b0bde8f0410847204602b0bde8f04133e0019841f60d56c0f24c06a861b04706f5af708047e861b847012084f8ab0103202860b4f8ac01c047d4f8bc01a0b10199d4f8b821b4f8ae71531a38bf8b1abb420ad2b4f8b01159b147f64c62c2f207021268101a884203d32046022100f078f802b0bde8f08100bfb0b590f8a841002c4cd044f28442c2f200021188082911462cd0114631f8083f082b27d0114631f8103f082b22d0114631f8183f082b1dd0114631f8203f082b18d0114631f8283f082b13d0114631f8303f082b0ed0114631f8383f082b09d0114631f8403f082b04d032f8481f0829114614d14a68b2f5870f10d36ff35602012a0cd14a684ff65b63cff6ff737b449a4204d1d0f8b421002a18bf4a60002180f8a81190f8a911b1b1002180f8a91145f60a61c2f20701097869b947f24841c2f20701096839b14af68171c0f24b010546042088472846002c08bfb0bd0321bde8b04000e000bfb0b588b0044645f64d50c0f246000d468047012840f08f8008208df8010003208df8020010208df8030000208df80400ca208df8050006208df8060018208df8070047f26440c2f2070050f81c2c94f8a811002a18bf0122002945f60961c2f207014b7818bf0232002b94f8ab3118bf0432002b4ff0410318bf08328df808304c238df8093001238df80a308df80b508df80c2050f8182c4ef605758df80d200268c0f24705130a8df80e208df80f30130c120e8df810308df811204268130a8df812208df81330130c120e8df814308df8152002698df81620120a8df8172009788df8181081688df819106821c2f2000109688df81a10090a8df81b1047f64c61c2f207010a68130a8df81c208df81d30130c120e8df81e308df81f2000680df10102c4f8b80108680921c4f8bc0101201f23a84708b0b0bd00bf2de9f04f83b0022902d24ff0ff3510e1cde901100120cff680704ef66c644ff29c564ff62c4bccf2ff04c2f229064ff6ff77851ccff6ff7b4ff000084ff0010907e000bf48f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f00ea0501b1f1005f03d10168a14200f0b9804ff47570fff72dfb0028c5d059464218463182f8d48382f8d58382f8d68382f8d78382f8d88382f8d98382f8da8382f8db8382f8dc8382f8dd8382f8de8382f8df8382f8e08382f8e18382f8e28382f8e38382f8e48382f8e58382f8e68382f8e78382f8e88382f8e98382f8ea8382f8eb8382f8ec8382f8ed8382f8ee8382f8ef8382f8f08382f8f18382f8f28382f8f38382f8f48382f8f58382f8f68382f8f78382f8f88382f8f98382f8fa8382f8fb8382f8fc8382f8fd8382f8fe8382f8ff8382f8008482f8018482f8028482f8038482f8048482f8058482f8068482f8078482f8088482f8098482f80a8482f80b8482f80c8482f80d8482f80e8482f80f8482f8108482f8118482f8128482f8138482f8148482f8158482f8168482f8178482f8188482f819847ff470af046080f80a908783c783078447848784c784078547858785c785078647868786c7860787478756e8001f51457ff43aafbff35f8f46e8000121b156e8001f5145f8d02fe7044645f64d50c0f24600804701281dd1029d6978022916d00129204607d000297ff401af00210025fff75afe0fe0019f41f28831022f0ed14ff4fa730022012741f2883624e02046fff7d6fd0025284603b0bde8f08faa78052f11d3eb782e79072f43ea062310d395f805c0ae79092f4cea0627e4d3ee792d7a46ea052607e041f288364ff4fa7301e041f288360127642b98bf64238b4228bf0b4690f8a81180f8aa21a0f8ac31a0f8ae71a0f8b06171b147f24841c2f20701096800295ed001210446fff70cfe0025c4f8bc51bee744f28442c2f200021188082911462cd0114631f8083f082b27d0114631f8103f082b22d0114631f8183f082b1dd0114631f8203f082b18d0114631f8283f082b13d0114631f8303f082b0ed0114631f8383f082b09d0114631f8403f082b04d032f8481f0829114632d14a686ff00105b2f5870ffff483af0123cff680731a40012a7ff47caf4b684ff68112cff6ff727a44934214bfc0f8b431d0f8b431002b3ff46daf4a60012180f8a81199e74af60771c0f24b010420884720b105462046fff732fd5be70121204684f8a9118fe76ff0010553e700bf10b582b00c464df6cb61c0f24501884724b12178042901d1617809b102b010bda1780d29fad1e1780029f7d121790029f4d161790029f1d10028efd00168e029ecd10446fef708fa014620460029e5d004204ff6591e01904ff0000cc0f24e0e0020002100220e23cdf800c0f047204602b010bd3146fff7c3bf00bfb0b582b00c460546fef7e8f90146002d4ff0000018bf002901d102b0b0bd47f2fc70c2f2070000684df6cb61c0f24501884790b10068e0284ff00000edd129784ff6591c0191c0f24e0c0021002223460090e047012002b0b0bd002002b0b0bd08b530460921fff7cbff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf08b530460a21fff7b7ff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf08b530460b21fff7a3ff002805d002b045f28310c0f24400004744f62703c0f24603984708bd00bf10b589b38968012805d104460846fef7e5f9204628e039b30a794b798c79c97942ea032242ea044242ea016106291bd147f29121c0f24601044688470146204691b144f2e970c0f24400804701462046002918bf10bd48f6aa50c0f2000078440c21fff761ffd4e710bd00bf10b5059c4ff20340c0f2460080472146fff7c0ff10bd00bfb0b5c4b0044600284ff0000000f0c38089b20b2940f0bf80207838b96078092804d1a078612801d1e07810b1002044b0b0bdfef725f90028f8d045f64d50c0f24600804701281cd00228efd103208df8070001200df107058df80800a81c21460b22f7f783fc4af66104c0f24604f02029460d220023a047b0fa80f0400944b0b0bd4ff29c50c2f22900006801466ff39601b1f1005fc9d101684ef66c62ccf2ff029142c2d122797f21042a06dc012a0cdcbab1012a08bf00211ae0082a0bdc052a11d0082a08bf0a2112e0022a0dd0042a08bf01210ce0092a09d00a2a08bf0e2106e0092104e0022102e0032100e00b2180f8eb1210216a2580f8ec12a22180f8e652152580f8ee12062180f8e7521a2580f8ef120c2180f8e852132580f8f01252210822022380f8e95280f8f1124921012580f8e42280f8e53280f8ea2280f8ed3280f8f21280f8f35280f8f452217900f5397280f8f5126179192380f8f612a17980f8f712002180f8f812e17980f8f912217a80f8fa12617a80f8fb12a17a4ff2250480f8fc12c0f247040120e021a047284644b0b0bd00bf1fb5fff72fff002803d0bde81f4000207047bde81f4038b584b049f21f13c0f24703184710b5f0281bd1d1b1cab10c78032c16d10d2a1cbf0b2010bd487801281cbf0a2010bd45f64d50c0f246000c468047012803d1a01c0b21fff703ff002010bdbde81040fdf723bd00bf4ff29c52c2f229021268bff35f8f13466ff39603b3f1005f0cd1d2f800c04ef66c63ccf2ff039c4502bf0023c2f8dc32c2f8e03247f2c942c0f24d02104700bf4ff29c50c2f229000068bff35f8f01466ff39601b1f1005f0bd101684ef66c62ccf2ff02914202bf0021c0f8dc12c0f8e01247f2d940c0f24d0000472de9f04184b0dde90a76022f16d340f64705a94212d08eb13578412d0ed175784e2d0bd1014602f0bf0012284ff00d0008bf002b0ed004b0bde8f0810c9d0097cde9016546f63916c0f24d06b04704b0bde8f081082feed3b078012802d1f078022803d90d2004b0bde8f08100294ed045f6f444c2f2060414f8040c814246d12068002843d048f22732c0f254020d46084602219047d0b324682088b8b36088a8b3a08898b3e08888b330797179b279f37940ea012040ea024050ea0368d1d04ff29c50c2f229000068bff35f8f01466ff39601b1f1005f17d101684ef66c62ccf2ff02914210d1f178a1b190f8dc22aa420cd1d0f8e022424508d101295bd1082fafd100f09cf875e0f07818b1082004b0bde8f081082fa4d1f9f751f9c8b10646d0f868010028f1d047f64c61c2f207010968401a0128e9db96f8dc0268b196f8dd02032809d00628e0d1304600f077f8dce7112004b0bde8f08146f63917c0f24d0707f53a60804707f539622846214690470120002786f8dd020df10f02304600210123a6f8de7286f8dc52c6f8e0828df80f70adf80c7000f07ff861884af2bd6603abc0f24c0628460222b047384604b0bde8f081a7f1510189b24ff6b87291424ff00d0119d390f8dd02032808467ff430af334613f8080fa7f108018ab280b9052a0ed9a1884af2bd67c0f24c072846b847002004b0bde8f081084604b0bde8f08101280dd1042a0bd3981810f8021c00297ff425af10f8010c00287ff420afe0e702284ff00d0008bf062a7ff401af707b01283ff614afd4e700bf90f8dd120a1f022a38bf7047022905d0012907d1042180f8dd127047052180f8dd12704780b582b0052180f8dd120121adf8061045f6f441c2f2060190f8dc0209684af2bd6c49880df10603c0f24c0c0222e04702b0bde8804070472de9f04fc7b0054690f8dc0203920591d8b145f6f041c2f206010978884214d148f22732c0f2540202211c46904760b195f8dc0240f66502c0f255020021904718b100690088172802d247b0bde8f08f033880b2f42828bff42007af0b3807f101084ff000094ff0000a01900495029414e000bf0699cde94509adf810112121adf8121143f23d5144a8c0f255018847049d029c1ffa8af0a042d6d2a4eb0a01019a1ffa81fbb5f8de1241235a45b8bf93468df81c304e234a1c8df81d300123a5f8de22d5f8e0228df81e30059b1ffa8af08df81f30130a8df82410090a8df820208df82130130c120e8df82510b0fa80f18df823204a091ffa8bf111fa8afaa2458df8223008bf0232bbf1000f8df826202fd001390329039901eb000c01d2002115e04ff6fc700bea00020cf10103002100bf5e1816f8014c7d187078ec725c5cb67804318a422c736873ae73f1d1049d1bf003020dd01cf80160cb19012ade7207d00ceb01004178022a19731cbf8078587395f8dc020bf10b0406900b2010fa84f649f6cd713046c0f24c01884700283ff47eaf26f003031ffa84fc06f003025e426ff00303c1180433f518043581f8049081f8059081f8069081f80790f2d152b10344012a83f8049005d0022a83f8059018bf83f806900bf10e0202701b22442108230272220480f801908170c370417283723ff43dafbcf1040f01d2002214e047f6fc710cea0103002200bfb95c8618f17218f8021008eb020531736978ad78043293427173b573f0d11cf003033ff41fafbe5c00f10b01012b8e543ff418af561cbd5d022b8d553ff412af0232bb5c8b540de7f0b583b04ff29c51c2f229010c68bff35f8f21466ff39601b1f1005f47d121684ef66c62ccf2ff02914240d194f8dc12e9b3002840d002888a423dd145f6f446c2f2060616f8041c8a4235d1316899b3356894f8dd12032919d8d4f8681139b147f64c62c2f207021268891a00290edc02218df80b100df10b020746204600210123fff791fe2046fff760fe38468278092a1bd141896b8894f8dd72994217d1032f3adc012f6ad0022f3dd004e018b181780939022901d203b0f0bd47f6ed31c0f24d0103b0bde8f040084794f8dd72032ff1d8a2f10d0102290cd2c178d1b302208df8040001aa204600210123fff75bfe03b0f0bd092a08bf032fdcd14189aa889142d8d1c078002800f0818002200df105028df80500e6e7052f04d0042fcad1c07805262fe0c078002848d0062084f8dd0202208df807000df10702204600210123fff730fe052fb5d02046fff7fdfd03b0f0bd4268002aadd00389a3f2012189b2b1f57e4fa6d34089082b48d12988884245d12046012108234ce0c0780526002808bf022650b102208df80a000df10a02204600210123fff705fe6988012294f8dc024af2bd67adf8082002abc0f24c07022284f8dd62b84703b0f0bd052f15d1002094f8dc523668c4f8dc02c4f8e00247f2c944c0f24d0404f11000804728463146224603b0bde8f0401047032084f8dd0201208df806000df1060272e7e98888427ff457af2046022103e0204603210022002303b0bde8f040c3e580b582b04ff4f0704af61f4e01904ff4207cc0f2470e0020002100220023cdf800c0f04702b080bd06290ad14178012907d18178653911f1630f02d3c178012902d94ff0ff307047f0b581b00179427941ea022540f6b8318d421fd80646fdf743fcd8b1f8f722fec0b10446d0f8b80342f66537c0f2440758b941f29770c0f200007844012100220023b847c4f8b80320b107f1e8022821904718b14ff0ff3001b0f0bdb078f178d4f8c4230023c7f6e07340ea452003ea425240ea0120bff35f8f1043bff35f8fc4f8b403002001b0f0bd00bf02293cbf4ff0ff3070472de9f04f9bb00278437842ea0325b5f5007f04d94ff0ff301bb0bde8f08f4ef66c6a4ff29c56ccf2ff0ac2f229064ff6ff774ff000084ff0010b0791099008e000bf48f2a3512046c0f24501bff32f8f8847d6f80090bff35f8f3468bff35f8f20466ff39600b0f1005f03d12068504500f0c2804ff47570fef73ffb002800f0ba8004464ff62c40cff6ff7000bf2118463081f8d48381f8d58381f8d68381f8d78381f8d88381f8d98381f8da8381f8db8381f8dc8381f8dd8381f8de8381f8df8381f8e08381f8e18381f8e28381f8e38381f8e48381f8e58381f8e68381f8e78381f8e88381f8e98381f8ea8381f8eb8381f8ec8381f8ed8381f8ee8381f8ef8381f8f08381f8f18381f8f28381f8f38381f8f48381f8f58381f8f68381f8f78381f8f88381f8f98381f8fa8381f8fb8381f8fc8381f8fd8381f8fe8381f8ff8381f8008481f8018481f8028481f8038481f8048481f8058481f8068481f8078481f8088481f8098481f80a8481f80b8481f80c8481f80d8481f80e8481f80f8481f8108481f8118481f8128481f8138481f8148481f8158481f8168481f8178481f8188481f819847ff470afc4f800a084f80ab0a783e78327846784a784e78427856785a785e78527866786a786e7862787678756e8000f48457ff431afbff35f8f00bf46e8004028b156e8000f4845f8d025e70024ddf824805db1fdf704fb0146002c4ff0ff3018bf002902d11bb0bde8f08fb5fa85f040094ff000098df82c908df82d908df82e908df82f908df830908df831908df832908df833908df834908df835908df836908df837908df838908df839908df83a908df83b908df83c908df83d908df83e908df83f908df840908df841908df842908df843908df844908df845908df846908df847908df848908df849908df84a908df84b908df84c908df84d908df84e908df84f908df850908df851908df852908df853908df854908df855908df856908df857908df858908df859908df85a908df85b908df85c908df85d908df85e908df85f908df860908df861908df862908df863908df864908df865908df866908df867908df868908df869908df86a908df86b90bcb1d4f86c21a2b14ff4006304e03f683b44b3f5403f0cd2d718be680136f6d07e68033626f0030609eb060101f11009ede7002d4ff0020a039500f00581ddf81cc0acf102020a2ac0f00081002608eb0a0b9bf801504ff0ff30012d3ff648af9bf802209bf803309bf8047042ea03229bf8053042ea074747ea0361a1f1011212f5803f0a91fff433af9bf808109bf809e0029141ea0e210af10a020497aceb02034f1e9f42bff423afcde905269bf806209bf807300a9e42ea032cb6eb0c07fff416afb9423ff613af18f80a70089147ea052a4feada000ba90e5c07f00705012707fa05f5354240f003830e5c45ea06050d5407994ff6ff75c0f20205b4b3d4f86c0198b317461a0201924fea0e264ff4006200eb0208d8f8083053454bd0d8f800301a44aa4221d800eb0208d8f80830534540d0d8f800301a44aa4216d800eb0208d8f80830534535d0d8f800301a44aa420bd800eb0208d8f8083053452ad0d8f800301a44aa42d3d900bfbcf1000f40f0be8204980a464ff6fc710330c0f2030108404844944600f1100908980599069e00eb010a0398013686424ff0000028bf0120ddf824804fd2aceb0a02092a3ff64caf49e0d8f804000a9a904240f09782d8f80c006045c0f0928236d9089aa0eb0c00824200f28b827ab3029831180198c1f1000cc2199044a1f1010e002208eb02050beb0203287c9e7ab04240f0778296451cd06e7cdf7abe4240f070820ceb0207be1c13d0ae7c187b864240f06782079ef81c07d0e87c5b7b984240f05f8204329142dbd1b446a7e78c46a5e7ddf81cc0a2e7ddf81cc0002800f05082baeb0c004ff0000118bf0120b9f53e3f88bf0121014303980028c1f100003ff43eae00297ff43baed4f86cb1bbf1000f40f08b804ff44030fef7a6f8002800f02f82834600206ff0200100220beb02030bf8020040320beb0107b2f5006f01f1400158709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d87618775877987787f8400087f8410087f8420083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f0093d14ff43e30cbf800080020cbf804084ff0ff31cbf80c084ff40060c4f86cb1cbf80818c4f800034ff6ff760222c0f2020600210ae00aeb080209f10101ddf8248071454ff000003ff498ad08eb02050a9218f80200aa786f7805922a7a95f803e095f8049095f805c095f806a0eb7908926a7a40ea0728cde906215cb3012f29d84ff400670beb0705a868404500f0a28028680744b7421dd80beb0705a868404500f0988028680744b74213d80beb0705a868404500f08e8028680744b74209d80beb0705a868404500f0848028680744b742d7d90598cdf810a040ea0e2040ea094040ea0c67f81c20f00300d4f8009300f1100a002002930597b9f1000f26d00beb09035bf809105a68514580f09c80eab10beb02005bf80210d0f80490514580f08d80b9f1000f11d00beb09035bf809105a68514580f0878042b10beb02005bf80210d0f804905145d6d377e000204ff400694ff4006504e000bf3d44b5f5403f23d20beb05018a685bf805700132f4d04868ca68033020f0030000f110060beb09001032f6f7b7fb4168c2684bf809608368d4f86cb18a424ff00001b14408bf00f110014bf82310d7e7c9f540310f294ff00002c4f8002311d90beb09004bf809104ff0ff3142608160c260c4f800930be0e868ddf80ce00a9a089f60e010b10268114401604ff000090020b9f1000f7bd00beb09035bf809105a68514522d2002a72d00beb02005bf80210d0f80490514513d2b9f1000f67d00beb09035bf809105a6851450ed2002a5ed00beb02005bf80210d0f804905145d7d30546cc469146184601e01d469446a1eb0a0110290fd309eb0a020beb02034bf802104ff0ff31c3f804c0996000219446d960c5f800a0ddf80ce00a9addf810a0029b089f00280cbfc4f800c3c0f804c005984ff6ff7668600020c0f20206c5f80880e8604aea0321069bddf81c9047ea032a814202f10a087ff4bdae05f110077818099952464144f346f6f7cafae968de466868aa68d4f86cb15144e96081424ff0000018bf07464bf82270a3e600204ff400684ff4006703e02f44b7f5403f23d20beb07018a685bf807500132f4d04868ca68033020f0030000f110060beb08001032f6f7e9fa4168c2684bf808608368d4f86cb18a424ff00001b04408bf00f110014bf82310d7e7c8f5403200210f2ac4f800130dd90beb08034ff0ff304bf8082059609860d960c4f800831bb0bde8f08f00281ebf0168114401604ff0ff301bb0bde8f08f00bf02293cbf4ff0ff3070472de9f04f8bb00278437842ea0325b5f5007f00f2148105eb4502022303eb4202914240f00c810590fcf7dbfe002800f006814ef66c694ff29c564ff62c48ccf2ff09c2f229064ff6ff77cff6ff7800244ff0010b06e048f2a351c0f24501bff32f8f8847d6f800a0bff35f8f3068bff35f8f01466ff39601b1f1005f03d10168494500f0bc804ff47570fdf722fe002800f0d58041464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470afc0f8009080f80ab08783c783078447848784c784078547858785c785078647868786c7860787478756e8001f51457ff437afbff35f8f00bf46e8000121b156e8001f5145f8d02be7d0f86cc10495bcf1000f20d000234ff4006104e03f683944b1f5403f0bd20ceb0107be680136f5d07e68033626f0030633441033eee775b9b3f53e3f40f2e0804ff0ff310bb0bde8f04f08467047002d00f0d680002305994ff0000901f1020a153103914ff0ff31059109eb49060aeb46056c784ff0ff31012ce3d8af78ea7857ea022e1ebf2a796d7952ea0528d9d0bef5207fd6d8b8f5f07fd3d80ef10102520802fb08fb4ff6fb724ff6ff779345c0f20207c6d81af81610059e41ea0421039cb9f1000f28d014f8132c14f8125c42ea05228a42b3d0feb114f80d2c14f80c5c42ea05228a42aad0012e15d014f8072c14f8065c42ea05228a42a0d0022e0bd014f8012c257842ea05228a4297d0043e721c04f11804d6d15646bcf1000f2fd00bf105054ff400640ceb040adaf808208a4230d0daf800201444bc4220d80ceb040adaf808208a4225d0daf800201444bc4215d80ceb040adaf808208a421ad0daf800201444bc420ad80ceb040adaf808208a420fd0daf800201444bc42d3d94ff6fc720bf10801c0f201021140194401f1100320e0daf80410a9427ff44fafdaf80c10a9427ff44aaf9af8101004297ff445af9af811109af8122041ea022171457ff43caf9af813109af8142041ea022141457ff433af049909f1010989450599b24601f1010105917ff43dafb3f53e3f3ff624af00240025019019e000211fe7d0f86cc14a688b684ff6ff7793454ff00002c0f20207c1f80cb008bf01f110024cf82320013575454ff000013ff408af05eb45000aeb40011af8100091f8018091f802e091f803b091f804904979bcf1000f40ea082605912cd0b8f1010f29d84ff400610ceb01039a68b24200f0a8801a681144b9421dd80ceb01039a68b24200f09e801a681144b94213d80ceb01039a68b24200f094801a681144b94209d80ceb01039a68b24200f08a801a681144b942d7d94eea0b21059b013149ea0322490801fb02f75a4607f1050b8df81d004fea1b208df820004fea1b408df8210005208df82500042001218df8270048468df81b1003908df82a000df11b0011218df81c408df81e808df81fb08df822408df824408df823408df82640cdf808e08df828e091468df829208df82b30fff74df800287ff482ae01980021d0f86cc1bcf1000f3fd0ddf810e0b8f1010f3cd84ff400630ceb03018a68b24235d009680b444ff6ff71c0f202018b4226d80ceb03018a68b24228d009680b444ff6ff71c0f202018b4219d80ceb03018a68b2421bd009680b444ff6ff71c0f202018b420cd80ceb03018a68b2420ed009680b444ff6ff71c0f202018b42cbd9002104e0ddf810e02ee7ddf810e0002f3ff41aaf4fea0922029f059b42f001023a44039f1b0252083b4353435a1e032a01d200220ae023f0030700228e18043297427475b475f4753476f7d113f003033ff4f8ae01f11507012bbc543ff4f2ae3a44022b547018bf9470ebe62de9f04f81b04ef66c6b4ff29c554ff62c4848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0bd804ff47570fdf723fb002800f02e81414600bf4218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f800b080f80a908683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa1427ff43bafbff35f8f00bf45e8000121b155e8001fa142f8d02fe7d0f86813002974d104464ff41630fdf761fa014600284ff0000069d0002200bf8b1888544032b2f5163f58709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f009ad1c4f8681300e00021084601b0bde8f08f00bf2de9f04f93b04ff6ff75a9424ff0000c2cd0dde91c874ff4805ecde90aecd0f87ce3cde90087229fdde920a9dde91e6b249c0697239f0aadcdf840c0cde911eccde90845cde9026bcde904a90546079704f060fe84464cb1bcf1000f06d19df8480095f88013084385f88003604613b0bde8f08f2de9f04f93b00446002047f64c68a4f88003c2f20708d8f80000002a8b461abfc4f874030146d4f87413401ac4f87c03fcf720fa002800f07d81d4f86803002800f0788104f08eff002800f073810646b4f86c03002853d0a0f10108d4f87c0300254ff4a0714ff4f0774ff48059cde911050df1280acde901152046414632464ff42073cde90a951095cdf824a0cde90755cde90555cde90355009704f000fe002840f04781d4f87c034146cde91105012008904ff4a0700190204632464ff420734ff4f077cde90a951095cdf824a0cde90655cde90455cde90255009704f0dffd002840f026819df8480094f8801347f64c68084384f88003c2f2070806e0d4f8681330464ff41632f5f729fd4ef66c694ff29c57ccf2ff09c2f229074ff6ff76002507e000bf48f2a351c0f24501bff32f8f8847d7f800a0bff35f8f3868bff35f8f01466ff39601b1f1005f03d10168494500f0c0804ff47570fdf702f9002800f0ec804ff62c41cff6ff7100bf4218463182f8d45382f8d55382f8d65382f8d75382f8d85382f8d95382f8da5382f8db5382f8dc5382f8dd5382f8de5382f8df5382f8e05382f8e15382f8e25382f8e35382f8e45382f8e55382f8e65382f8e75382f8e85382f8e95382f8ea5382f8eb5382f8ec5382f8ed5382f8ee5382f8ef5382f8f05382f8f15382f8f25382f8f35382f8f45382f8f55382f8f65382f8f75382f8f85382f8f95382f8fa5382f8fb5382f8fc5382f8fd5382f8fe5382f8ff5382f8005482f8015482f8025482f8035482f8045482f8055482f8065482f8075482f8085482f8095482f80a5482f80b5482f80c5482f80d5482f80e5482f80f5482f8105482f8115482f8125482f8135482f8145482f8155482f8165482f8175482f8185482f819547ff470af0121c0f8009081728683c683068446848684c684068546858685c685068646868686c6860687468757e8001f51457ff433afbff35f8f00bf47e8000121b157e8001f5145f8d027e7054604f043fe002d18bf002803d101208bf8050022e0c5f860014ff001094ff4f07085f8649101904ff420704af61f460090c0f2470600200021002200230027b04730b185f86471c5f860718bf8059004e09bf805008bf8049030b14ff0ff3013b0bde8f08f0025cbe794f8800384f8810348b3d4f8700342f66535c0f2440528b1d8f800102d31c4f8781311e044f62b70c0f200007844002122460023a847c4f87003d8f8001001f12d01c4f8781320b105f1e8022d21904728b1002084f8810313b0bde8f08f002013b0bde8f08f80b582b04ff4f0704af61f4e01904ff4207cc0f2470e0020002100220023cdf800c0f04702b080bd022a3cbf4ff0ff3070472de9f04fadf2245d1e9008784f788c1850ea072001f102071f9002f07b82209300231d94a74202f06f82781ca04202f06b8238787978ba1c40ea0121a01a814228922b9102f260822098ddf870253290ddf84805ddf868153390ddf84c053b913490ddf8500522933590ddf854053690ddf858053790ddf85c053890ddf860053990ddf864053a901068411e0028116002f03a822b98002802f036822b98841c032c02f03182fe78072e02f22d82289890f800e0f00702d107f1040829e02b9820f00100022802f01f8297f804907d791e9849ea052a514632aaf04602f023fa002842f01182ddf85405c64607f10608012d36900dd8ddf87005806948b14feada0209f00701835c012505fa01f119438154b00707eb040bcdf88cb005d44ff00009042e29d2a1e000bff246d84504d018f9014b4ff0000903e000244ff00109d84645f64d55c0f24605a847012808bf0134601e81b2670800ebd130002c48bf4710a8470128369808bf7f42ddf88cb03844d6463690042e78d3b9f1000f42d1d84540d0424612f8011b36985a453dd098f8012008f10203120642ea01415b4500eb214c3ed008f1030298f802105a4534d098f8032008f10403120642ea01415b454fea214129d008f1050398f804205b4500f0898698f8057008f106035b4542ea072200f0808608f1070798f806305f4501f0b48298f8077008f108083f024ff0000915e03698002100e0d84600eb010c4ff001090022002108e04ff00109002203e04ff0010900220021d846002300273b43379f62440b447fb1dde93876dde93a40bc45d8bfbc46b142d8bf3146a242a8bf22468342a8bf0346ddf88cb00120cde938c1cde93a233790b9f1000f42f04a81bef1040f40f38380bef1060f40f3f280bef1070f00f0e682bef1080f00f06b81bef1090f42f03681d84502f0338118f801cbd84542f02e81bcf10f0f02f22a81ddf86c05002801f04f8337993298dde9342a002900f006863399dde939733b9e994227eae775b8bf0b46b242b8bf1646b542319681f2388338994fea630b21eae1794fea0c112f91319909f10104491b4fea590e4cea0c1211f003084fea540130942e9100f0d786994581f23e8205fb0a045feac97171464e4609d014f80e10309e01f0f00141ea0c0104f80e102e99594521442d9681f21a8204eb0b060c4604f8012bb44281f014828f1cb7424a7081f01082cf1cb7428a7081f00b820f1dca70b7423946ead301f004ba00bfbef1020f36dcbef1010f00f09f81bef1020f42f0b8803298dde933a530903798dde935412a90ddf87005dde93a32c0694ff000093e90d84524a8dde93867cde9428bcdf81091cdf8fc9029922793c2c000f0a48208f10100429098f80090584500f09c8208f10200429098f801004fea002897e2bef1030f00f0ca81bef1040f42f08180ddf87005dde932cec069dde93473dde93691dde93826dde93a5a002440943c90d8453d943eac2ca8cde93e8bcde92f7ecde9292680e80a1000f0e98208f101003e9098f800405845319400f0e38408f102003e9098f801003eac4fea002b00f0debcbef1050f00f02782bef1060f42f04880d84502f0458008f10100584502f0408008f10200584502f03b8008f10300584502f0368008f10400584502f0318008f10500584502f02c8008f10600584502f0278008f10700584502f0228008f1080cabeb0c001028c2f01b8008f11800584542f0168098f8040098f8051050ea01261ebf98f8060098f8071050ea012502f0078098f8000098f80120339940ea0224081bc1f0fd87864201f2fa8798f8020098f80330349a40ea0327d01bc1f0f087854201f2ed87ddf86c05002801f0128235980df1d80e329b30909ee8014fcdf8c4e0cde906478de8010f1846dde93037cdf828c0cde90865059704f058f801f0f9b93298cde94a8b2e9033980021309034980df59479319035980df584782f903698dde937a62d90ddf87005dde93954c0693b9f4c91cde942014846414602f076fa83464846414602f071fa4c99002940f04084dde94a21a846319d8a4200f03984531c8b4200f03584931c8b4200f03184d31c8b4200f02d84131d8b4200f02984531d8b4200f02584931d8b4200f02184d31d8b4200f01d8402f108038b4240f0188411785378d646b446ba4651ea03264ff0ff3700f00f849378d17853ea01212c9100f00884b6f5207f00f204842c99b1f5f07f00f2ff83917925960f29279100f2f983d379102b299300f2f4832a901079ddf8706524904399306a53790843ddf86c153062002901f08a842d982599084440428345c1f283842d9830990027081a834580f2d7832c982a9a40428242c0f2d1832a9aaa4280f2cd83bef1000f01f087842ceaec70269028eae87018903098a042a8bf204630905545a8bf554601f07abcd84501f01a87424612f8010b012801f21487329900282a9133992f913499319135992c91dde936c1309138992991399927913a9926913b99249100f01f825a4501f0fb8608f102025a4501f0f68608f103025a4501f0f18608f104025a4501f0ec8608f105025a4501f0e78608f106025a4501f0e28608f107025a4501f0dd8608f108025a4501f0d88698f8012098f8023098f8046042ea032398f8032098f8054098f8061042ea0622259244ea012a98f8071098f8082008f10904159441ea0221fee1abeb0806052ec1f0b68608f1050ada4501f0b18608f1060998f80540abeb0900a042c1f0a88609eb0400584541f0a38632981c90339831903498269035981b90dde93650309038982d9039982e90dde93ab02f90c4b1002703e000bf0137a74212d219f8070001381e28f7d909eb0700e11b4aaa42ab02f083fd002801f07b864a980744a742ecd3ddf86c05002801f09c8030982c954546002800f062832d9820eae0702d902e9820eae070219031985845a8bf5846319026982f998842a8bf0846269050e3d84501f0558608f10100584501f0508608f10200584541f04b8698f8001098f80100dde9375441ea0021ddf870050df1c80c0990ddf86c059ce80c10089004a8dde93576dde939aeddf8ec9080e810441e98cde900c7cde90265cdf81c9004f006f80746c0e2ddf87005dde93432c069dde93691dde93876dde93a4a5d46cde9428b4ff0000b3e90a8452ea8dde932cecdf810b1cdf8fcb03193cde92964279780e80450cde92c9172d008f10100429098f800b0a8426bd008f10200429098f80100070267e0012044904ff0000842ae3eaf3046394602f0b2f831903046394602f0adf84499074600292f9469d0d3462c46002600202d9001202e963e464490002042af0df1f80a2c903846514602f096f805463846514602f091f84499002940f060828446dde942014ff0ff37884240f05a82319b002b00f15682002e00f15382ddf870253f98116a239508431062309848ea09054a904ff6fe7085422f98da46cde94bb44d9000f0a2834ff6ff70854240f081831e98d0f868934ff420704b904ff4f0704c904ff4a0704d9091e301204490002742ad3eac2846214602f04ef881462846214602f049f8449959b34ff0ff361d9c229b374617e20020319000e2dde94201884291d0024612f8016b8a42429200f02683821c429243788a4246ea032600f01e83c21c429283788a422d9300f09686011d4291c0782e963e46d3462c4600027ce78046dde942108142ced00a4612f801cb8242c9d08c1c4d78011ba9424ff0ff36c4d361198142c1d1ddf870253f98116a08431e9910620029b8d0d1f86c110029b4d047ea0b070020b0eb572faed151f827000028aad0401a8207a7d16ff4403240f60f030244cff6fd739a429ed30a1852f8081cb94299d152f80c3ca3f1011111f5803f92d3002acde92557cde923c43ff48caf52f8041c99427ff487af1446c0f54030269a83423ff680afddf870051d46806940b1d2080bf00701835c012707fa01f119438154c12dfff46faf207801287ff46baf2598002801f08980dde9247ba4f13f0601f01bb85a4501f0dc8408f102025a4501f0d78408f103025a4501f0d28408f104025a4501f0cd8498f8012098f802609300710098f8032098f80460259108f105014fea820a15917100baf1000f169101f0b7841699002901f0b3842f99ca1ac1f0af84924501f2ac843199259a8a1ac1f0a7841699914201f2a384309903eb0c06084301d000230ce05feacc704ff0000307d1002e05d42f990aeb06008842d8bf0123159d169aabeb05001390ddf86c0502fb0af8b0fa80f04009184300244feac0794ff0000e1493cde9179822e02c992a9b07fb013b53081bf803702e9907f00f0c07f0f0070f44d2072d9908bf4cea01070bf80370ddf88cb021998e44dde91b128a424ff0000188bf01210c43e10708bf1544e10704d0f04507d801f050bc00bf5d4518bff04540f20782bbeb050701f0468429780b0901d0012211e0012f01f03e846b780bb102220ae0042fc1f03784aa78eb7852ea032301f03184042200bf1c92a8eb0e02934201f229842598b9f1000f21931b97bfd101f00f022e92219a0901012acde919542d912fd14ff0000c2199ddf88cb0dde91954dde91798c907aad00ceb0e01b1fbfaf202fb1a1117188a1957ea02019fd42f998a429cd231998f4299d2309900293ff480af29998a4292db27998f42fff68faf26998a42bff68baf24998f42fff671af85e72f9d22f0010b4ff0000c16e02c992a9b2e9f04fb01315308cc5cd20704f00f0504f0f0043c442d9a08bf45ea02042f9dcc540cf1020ce345b4d00eeb0c02b2fbfaf404fb1a2704eb000907eb060859ea080428d4a84526d23199894523d2309959b1299988451edb279989451bdb2699884518da2499894515da2c992a9b4fea580709fb01342e9de15d2d9801f00f0301f0f00129445feac8752f9d08bf43ea00012598e155511cb1fbfaf202fb1a1114188a1954ea0201bbd4aa42b9d231998c42b6d230990029a0d029998a42b0db27998c42addb26998a42aada24998c4294dba6e73eac012040904ff0000b3caf2046394601f034fe80462046394601f02ffe409941b14ff0ff371d9c229b002f00f08b8501f05dbbdde93e129142f2d011f8016b9142eed1ddf870353d991a6a11431e9a1962002ae5d0d2f86c114ff0ff370029e1d0319a1d9c4bea020b0022229bb2eb5b2fdad151f82b20002ad6d0a2eb010e5fea8e72d1d16ff44032bc4640f60f077244cff6fd77ba426746c6d301eb0e0c5cf8081c594507d15cf80c1ca1f1011212f5803f80f0db844ff0ff37b5e70ceb0c110afb02f2f4f725fd00f03abd00234ff00109d846fff79eb9002021902d90052e01f004831c98002801f00083fbf7c7f8002801f0fb829af8008008f10600864241f0f48247f65420c2f207000768002f01f0ec82387b797bba7bfb7b40ea012040ea024040ea0363a3f1810010f1800fc1f0dc82387c797cfa7c40ea0120b97c40ea014040ea02601090813810f5817f41f2cc82b8f1000f4ff000002e9317972f9079d0ddf884a04ff0000e00270ae000bf00f100404aa941f826000137474506f1010e17d219f807007646411e1e29efd909eb0700a8eb070142aa3eab02f0aaf900283ff43caf42993e984aaa0f4442f82600e3e7bef1000f00f0dd80aef101094ff000080df5947a09e0be46c14508f10108a6f101060af1040a00f0c8804aa850f828100029f1d40020434686421dd00aeb80077a68b2f1ff3f18dc5a1c914514d0ba68b2f1ff3f11dc9a1c91450dd0fa68b2f1ff3f0adcda1c914506d03a690433002a00f10400e0d400e00022179842ab3eac7746009402f0e8f900283ff4eeae3e980028c0d04af20d3142a8c0f24e018847b9e701204ff0000e88e0d3462c4664e45d4508bff04541f03e82ddf86c05002818bf012014990840012840f05e842c9b2a9825994fea5a0701fb0300139900eb5602169800970190159801f0f5fa002840f04b8401f01fba2f4600f041bc1e9829464aaa674601f025fa00287ff4adaeddf87005319b8069bc4660b10021b1eb552f08d1ea0809f00701875c012505fa01f139438154ddf828912e9a239db9f1000f3ff492ae002a4ff0ff371cbfdde92c1051ea00083ff48aae4b999942fff486aec91a8a423ff682ae4c99b142fff47eae891b88453ff67aaeddf86c15002700293ff474aea5f1011111f1021f7ff66eae2a99002900f02685259921eae17b269921eae17527998a45a8bf8a4629998c42a8bf0c4600f019bd0020ddf884a002e04ff0000e01202a7995f800c095f80180ab78ef7895060f2102f00f0601eae575754389214d43ed0a8df8f8500e25309212f0200f08bf012506fb05f501fb05f54fead5258df8f9504ff00d0508bf022506fb05f501fb05f54fead5258df8fa504ff00c0508bf032506fb05f501fb05f54fead5258df8fb504ff00b0508bf042506fb05f501fb05f54fead5258df8fc504ff00a0508bf052506fb05f501fb05f54fead5258df8fd504ff0090508bf062506fb05f501fb05f54fead5258df8fe504ff0080508bf072506fb05f501fb05f54fead5258df8ff504ff0070508bf082506fb05f501fb05f54fead5258df800514ff0060508bf092506fb05f501fb05f54fead5258df801514ff0050508bf0a2506fb05f501fb05f54fead5258df802514ff0040508bf0b2506fb05f501fb05f54fead5258df803514ff0030508bf0c2506fb05f501fb05f54fead5258df804514ff0020508bf0d2506fb05f501fb05f54fead5258df805514ff0010508bf0e2506fb05f501fb05f54fead5258df806514ff0000508bf0f256e434e43f60a00288df8076140f022824fea086242ea0c412c9a380602eb21492e9940ea0340ddf85c80aef1010501eb204000260f90cdf84ce0119507e001f01f000b3801367645814400f0f8814aa850f82610b1f1ff3ff1dd33469d421cd04aa800eb83004268b2f1ff3f16dc0133ab4212d08268b2f1ff3f0fdc0133ab420bd0c268b2f1ff3f08dc0133ab4204d002690133002ae1d400e00022404642ab3caf009702f033f8002800f0d0813c990029199100f0ba819df810019df81111149650ea0125cdf8589000f09f819df814719df815613f0447ea0667109e9df80f019df80e112e444fea002c41ea00229df8120106eb27470f9e9df813310004f71b169e40ea03604ff0010e06eb204924920eeb5202031418920ceb010201f0030009f10101159109f10201129119992997571e1a97f718f31a01312590101a2a910eeb52012d9a2f90d8074ff0000b2e90f80727912c90239509e000bf2a9827990bf1010bab4508442a9000f04e8129980beb00035345f1db26988342eeda24980028ebd0dde91b104ff0000803fb01071a98032880f09c802598ddf884a0239d0028dbd0dde9181001230bfb010c30984fea58011cf8011000f0100083ea1014c8b201f00f035feac87e08bf03091846002b18bf01202043012815d108eb0906964211db319886420eda7008395c3eadeb5c01f00f0501f0f0011943f60708bf45ea0311239d395425980128a6d008f1010040081cf80000bef1000fc1b24fea111a08bf00f00f0a5ffa8af0002818bf01202043012817d1159808eb0003934212db319883420fda5808395c3eae16f80a6001f00f0501f0f0013143db0708bf45ea0611239d39542598ddf884a002283ff474af08f1020040081cf80000bef1000f00f00f03c1b208bf0b091846002b18bf0120204301287ff460af129808eb00069642fff65aaf31988642bff656af7008395c3eadeb5c01f00f0501f0f0011943f60708bf45ea0311239d395445e7ddf8a8c008e000bf2f9808f1040840450cf1020c3ff458af30981cf8016c00f010030120f6b2002580ea13130f2e88bf01251d43012d09eb080a15d1924513db3198824510da4fea5a053609795d3ea8865d01f00f0401f0f00131435feac97008bf44ea061179551cf8010c10f00f0e704618bf01201843012818d10af10106964214db3198864211da7008395c3eaa12f80e6001f00f052e9c2d9a45ea061501f0f001002c08bf41ea06053d549cf80000c6b200200f2e88bf01201843012816d10af10205954212db319885420fda690830097e5c3ead285c06f00f0506f0f0062c9c0643002c08bf45ea00167e549cf8000010f00f06304618bf0120184301287ff481af0af103039342fff67caf31988342bff678af5808395c3eab9b5d01f00f062e9d46ea031601f0f001002d08bf41ea03063e5466e74af20d3142a8c0f24e0188479df80c019df80d11dde91698119ddde913e605e09df80c019df80d11ddf84ce040ea012002e6002007e000274ff00109d846fef764bd4ff0ff302f901d9c229b2f9ffff765bbd3462c46fff7e7b80f4600e04f1cd90708d02d99994205da39782f9c01f00f0121433970b8f1010f05f1010700f0d480994537da07fb0a045feac97171464e4609d014f80e10309e01f0f00141ea0c0104f80e102e99594521442d9614da04eb0b060c4604f8012bb4420fd28f1cb7424a700cd2cf1cb7428a7008d20f1dca70b7423946edd302e00f4600e04f1cd90708d02d99994205da39782f9c01f00f0121433970b8f1020f05f1020700f09480994580f2908007fb0a045feac9717146c8460ad014f80e10ddf8c08001f0f00141ea0c0104f80e102e995945214414da04eb0b060c4604f8012bb44268d28f1cb7424a7065d2cf1cb7428a7061d20f1dca70b7423946edd35be00f4659e0bcf1000f4ff0ff373ff4d8aa5cf8042c8a427ff4d3aacef5403291423ff6ceaacde92660ddf87005806950b1319a4feadb0302f00702c65c012404fa02f23243c2546046002242ab00f077fe00284ff0000708bf4ff0ff373ff4aeaaddf86c0500283ff4a9aaa8f1011010f1021f4ff000077ff6a1aa2798a0f1011010f1021f7ff69aaa2c98002300284a9300f03c83299820eae0722a9820eae0733098a842a8bf284630902f985045a8bf50462f902be34f1cd90707d0984505da39782f9c01f00f0121433970ef1c3199691a11f1040f10d91d9c229b1f980133834228982b9900eb01077ef459ab00f0cfbd319904378f42eed0994539da07fb0a045feac97171464d4609d014f80e10309d01f0f00141ea0c0104f80e102e995945214418da04eb0b060c4604f8012bb44280f0cd8001f10208b0454a700cd201f10308b0458a7007d201f10408ca70b0454146e9d300e08846d90709d09d4207da98f800102f9c01f00f01214388f80010994536da791c01fb0a015feac97474464d4609d011f80e40309d04f0f00444ea0c0401f80e402e9c5c4501eb040615da01eb0b04314601f8012ba14280f09580b11ca14272700ad2f11ca142b27006d2311df270a1420e46ecd300e03146dc0707d09d4205da0c782f9d04f00f042c430c70994536dab91c01fb0a015feac97474464d4609d011f80e40309d04f0f00444ea0c0401f80e402e9c5c4501eb040615da01eb0b0400bf314601f8012ba14260d2b11ca14272700ad2f11ca142b27006d2311df270a1420e46edd300e03146dc0707d09d4205da0c782f9d04f00f042c430c709945bff64baff91c01fb0a015feac97474464d4609d011f80e40309d04f0f00444ea0c0401f80e402e9c5c4501eb040615da01eb0b0400bf314601f8012ba1422ad2b11ca14272700ad2f11ca142b27006d2311df270a1420e46edd300e03146dc073ff41baf9d42bff618af0c782f9d04f00f042c430c7010e701f10108d9077ff442af4ae7711cdc077ff478af7ee7711cdc07acd1b3e7711cdc07e3d1fde6bbf1010b67d017f8010b411e1f29f7d341b22029fef63baf06eb400116f81000497840ea0122c12afef431af204629464aab00f009fd0028e2d1fef728bf00254ff0000bd345b8bfa542bff63ca9dde9230100eb010ed645bff635a9a445bff632a9abeb02018e457ff72da9a5eb08018c457ff728a9bbeb0e075846b8bf7046f3454ff0000ed8bf774607eb030bd31bb5eb0c07d8bf7746ac450246aaeb0000a8eb0701c8bf6546834238bf1846631b9942299328bf1946309b1195994507eb060326931b913fd1269b9d422fdd119d4ff0010e37e0ddf86c05002800f03a84a9f1011010f1021f4ff000067ef6d1aea8f1011010f1021f7ef6cbae2d98002300284a9300f029842798309920eae072299820eae0732a988142a8bf0146319830915045a8bf5046319000f017bc119d269b0027eb1ab3fa83f35b095a45c8bf012703ea070e1b99002900f06181114602f0010a83185a464fea5b064ff0040b2bea820717971718013f03f0010418967e0816962bea87060aeb040715962e94c61bdde92c740aeb0100274402eb0a044a40490814915908cdf8c4a0139107eb0c01ddf8a4a0491b4fea560b514538bf8a460121cdf8a4a0bbf1010f88bf710801f0030321f003010e9161082491410823913099129301eb5000411c03381090d00721904d9809eb54032d904ff0ff3000eb560027906ff0010000eb56002590981e0f906ff00200199600eb56002f9eddf86cc04ff000080c910d931c90cdf868e008e0299808f101082f9e80454ff000073ff448a86fea08014046bef1000f18bf0ceb01002699319b42182d9902fb01910bb9002305e0189b179ccb5ce34003f00f032c932e9b0bb9002304e0169b159ccb5ce3401b012a9343193098219c03fb060a2398002c249c504421441cd15a46f3f707fddde91aec319840b114992c9a1af8010000f0f00010440af801002e980028b6d013992a9a1af8010000f00f0010430af80100ace703fb06f42d9bbef1000f03fb02f23ed019980228dcd30f98109910442144002200eb0b039c785f78279ec4f3031444ea071701eb0b049642a770c9d05f7810f80b60c7f3031747ea0617259e67709642bed010f80b7013f8016cc7f3031747ea06171c9e01f80b709642b1d013f8017c13f8023cc7f30317043247ea031304389345a1f1040104f8013cc9d1a0e7199b022b9dd3199b082b01d200222be00d9bddf838e003eb020c0c9a144400220ceb02077e781cf80250c6f3031646ea0516a51805f8016cbe787b78c6f3031646ea0313a354fb78be78c3f3031343ea06136b703b79ff780432c3f3031343ea07139645ab70dbd1129b119ddde91aec002b3ff466af531ccf5c8e5c129cc7f3031747ea0617012c87543ff45aaf941c0f5dce5cc7f3031747ea0617129ec754022e3ff44eaf0a44d278095dc2f3031242ea0111015544e70027fef758bf00224aa80690269808eb090105902798cde902b104902f98309f0190dde92d10009701f0dcfa0746fef742bf0020189026902498259940ea0320b0eb510f28bf48082c990023b0eb510f28bf48081b90013838bf184611902a98189a161ad8bf1e462b1a8b42a8bf0b469e422393bef61faf2d99ddf86cc00beb01024fea4c011c910cfb0cf1259b89001a91991824910021032b319138bf01212c9f0023032f38bf012319431991791e1691b91e1491119921924b00494389000e91511c1291299913930b0141ea011110930f916ff00801189b01eb4c0115919842c8bf0346dde92e214ff0000b01fb03212d9112e0012002902798009501900846269902f0cefc2a982d99013639442d91239900278e42bef6cdae2c9af14311448e42b8bf31461c9a6fea41011144012938db1a984ff0ff3201fb11001b99a1eb0b01012902eb410137db01fb01f2824233dd1599a1eb4b0202f10803032b02f1060122db01fb01f787421edd052b02f104011ddb01fb01f7874219dd072b02f1020118db01fb01f7874214dd0bf1040b092ba2f1080103db524382420a46dbdc08310ae04ff0000b35e00bf1010b04e00bf1020b01e00bf1030bbbf1010f29db08318a1f5243824224dcbbf1020f16db0a1f5243824215dcbbf1020f0fd08a1e5243824211dcbbf1040f08db01fb01f282420ddcbbf1040b01f10801e1d14ff0000b07e0abf1010b04e0abf1020b01e0abf1030b2a98dde92e17304400fb07112998219b10282498309a5b44a0eb0b053ff460af2591b6fa86f01999400908434cd11698864249d01499701e891b179518968842b8bf01466fea4100139908440128c0f2a6800e99319a00fb101011998a1a4ff0ff31012a01eb420174db01fb01f7874270dd5100a1f1090202f10807032f02f106015edb01fb01f686425add052f02f1040159db01fb01f6864255dd072f02f1020154db01fb01f6864250dd3199092f01f104013191a2f1080103db524382420a46d9dc083145e02698b4468342d8bf03469542a8bf1546ab4280f2b080ae46d80708d0259d5808295c299a01f0f0011143013329544fea5308b8eb6e0f1cda25982d9e0f9900eb6e0206eb08052f4607f8011b974280f08280a81c9042397080f08280ef1c974201707cd2043606eb080090423970e8d376e0259800eb530072e0319a013204e0319a023201e0319a03323192319a012a23db08318a1f524382421edc319a022a19db0a1f5243824200f3d480319a022a11d08a1e5243824200f3cf80319a042a09db01fb01f2824200f3ca80319a0831043a3192ded1002031903198129901eb000c26988342d8bf0346309eb445b646b8bfe64673454fdad90708d025985908425c299f02f0f0023a43013342544fea5308b8eb6e0f1ada25982d9e00eb6e070f9806eb0802154605f8010bbd4214d2911cb942287023d2d51cbd4208701ed2043606eb0801b9422870ead318e0259800eb530114e0681c02e0511c10e038465feace7107d0734505da0178109a01f00f01114301702a982f9f664656e62946309e5feace7207d0734505da0878109a00f00f001043087031982499c04301eb000801200290279832460190dde9250563462946cdf8008002f005fba845d8bfa8461798b042a8bf304680453eda84465feac8700ad0259b4fea5800195c299a01f0f001114308f1010819544fea5801b1eb6c0f18da25982d9b0f9f00eb6c025d182e4606f8017b964211d2a81c9042377010d2ee1c964207700bd20433581890423770ecd306e0259800eb580002e0681c00e030465feacc7107d0e04505da0178109a01f00f01114301702a982f9f189eebe5319801383ee7319802383be73198033838e70026fef79cba00222c984aa908912599484407912499cde903082698069123990290319805910190dde92e10309f009701f0e8fd0646fef782ba4ff0ff300df2245dbde8f08f381b18bf4ff0ff300df2245dbde8f08f00bfb0b586b008b3d0f86c01f0b10023b3eb512f1ad150f82130bbb11b1a9d0714d16ff4403540f60f041d44cff6fd74a5420bd3184450f8085c8d4206d150f80c1ca1f1011515f5803f03d24ff0ff3006b0b0bd50f8045c8d42f7d1c3f540339942f3d80028f1d0037813f00b0fedd1144601ab0022002500f011f80028e5d00198dde903122060481c40086160a260e06028462561656106b0b0bd00bf91429cbf002070472de9f041855c4ff0050c6e0758bf4ff0030c15f0f30f02d1891a614502d20020bde8f0818418627894f802e0002e05d5e078267942ea0e2240ea062e002a4ff0000018bfbef1000fead0b2f5207fe7d8bef5f07f88bfbde8f081082d38bf0120072d18610ad8501c400800fb0ef0a1eb0c018842d3d804eb0c0828e004eb0c080efb02f5002000bf00eb0c04a142c6d008eb00073e78360903d00127b54213d2bde70c1b012cbad07e781eb10227b5420ad2b4e7042cb2d3bc78fe7854ea0626add00427b542aad3ad1b3844dcd158600120c3f800809a60c3f80ce0bde8f0812de9f04f89b0dde91257069005fb07f000294fea4007029100f0d48100200390089002984ff0010e00264ff0000b4ff000084ff0000c4ff000090497079205930be000bf4ff0000c4ff00308cdf80ca009f10109814500f0ba81bcf1000f40f0b4810699b8f1020f11f809a0ead0b8f1010f15d0ddf81cc0b8f1000f40f086805fea1a100af00f01089100f092800499814280f09a804ff0010c4ff0000882e1079abaf1000f00f07f800499514576d3a1eb0a010491089901eb01100901019114e000bfa15d089f01f0f0013943a1550136aaf1010a4ff0010eae4224bf00260bf1010bbaf1000f00f0598113998b45d1d20bfb0324bef1000fe3d0a91bb1eb5a0f88bf4fea5a0141b38d0804eb060817d0002208f10107a2eb850c6ff003057b1904350ceb05020432d870187158719871f5d1059b079a2f1d11f0030504d10ce0002711f0030508d0012d08f8070004d04744022d787018bfb870aaeb410abaf1000f0e440dd0129dae42b5d2a15d019f01f00f013943aaf1010a4ff0000ea155afe7129d4ff0000aa6e7039a52ea0a2213d004990392914280f08f804ff0010c4ff0000853e74ff0000c4ff002084ee74ff0000c4ff00108f4e000214ff0010c4ff00008039142e7089a091a049102eb02111201cdf80090019214e000bf1af80670089a07f0f00717440af80670013601384ff0010eae4224bf00260bf1010b002800f0d680139a934580f0cd800bfb03cabef1000fe2d0ad1bb5eb500ff14688bf45087db3ac080aeb06081ed0002308f10107a3eb840c6ff0030400bf07eb040e04340ceb040304338ef803108ef804108ef805108ef80610f0d1059bddf81cc0271d15f0030404d10ce0002715f0030408d0012c08f8071004d04744022c797018bfb970a0eb45002e4488b1129dae4210d21af80620019f02f00f023a430af8062001384ff0000e0028abd180e000bf129d0020ce469de7891a0491089901eb011009010191114613e000bf1af80620089c02f0f00222430af80620013601394ff0010eae4224bf00260bf1010b002953d0139a9345bff4ccae0bfb03cabef1000fe3d0ad1bb5eb510f88bf4d085db3ac080aeb060819d0002208f10107a2eb840c6ff0030400bf3a1904340ceb04030433d070107150719071f5d1059bddf81cc0271d15f0030404d10de0002715f0030409d0012c08f8070005d04744022c787018bfb87000bfa1eb45012e4489b1129dae42b6d21af80620019f02f00f023a4301394ff0000e0af806200029b0d103e000bf129d0021a4e74ff000084ff0000c029852e64ff0010c4ff0000803e04ff000084ff0000c0298ddf8009045e60120012107e04ff0010cb8fa88f0049f8cf001014009b7fa87f252091140084009b0bde8f08f00bf2de9f041826882b10121816007220020511e20fa01f1c90708bfbde8f0814ff0ff3191400843bde8f0818c46056841688d42e9d06b1c03602c78ff2c11d18b4200f0dc80ac1c046095f90120b2f1ff3fd6b200f3d080bf2e22d806f03f0602238fe062b2b2f1ff3f02dd07222046cfe7bf2c09d804f03f070e224ff0010e4ff000084ff0000c2ee0df2c0fd804f01f0715224ff0000c4ff001084ff0010e22e0df2e0fd806f01f0603236ae0ef2c0fd804f00f071c224ff0010c4ff000084ff0010e10e0ef2e58d806f00f06042358e0f02c40f093804ff0000820224ff0000c4ff0000e00278b424fea072603d0af1c07606d7803e00125856000251f46c02c45ea06046cd35b1a7d1ab3fa83f3b5fa85f55b096d092b434fea042403d001258560002503e07e1c06603d783746b8f1000f44ea050453d17d1ab5fa85f56d092b434fea042403d001258560002503e07e1c06603d783746bcf1000f44ea05043ed1791ab1fa81f1490919434fea042131d001238360002030e0f02e3ed1002605234ff0010e00250127ea074fea062617d18c4215d0621c0260247801379f4246ea040623d000bf8a424fea062609d0541c0460127801371643bb422246f3d116e02246013701254ff0000e9f4214468560dad112e07b1c0360387840ea0104bef1000f20467ff413afbde8f08114465feace7202d0091b8e4204d9012181600020bde8f081a11907462046002263463960bde8f041ffe78446081ab0f5806f01d90020704761454ff00000fad0002a18bf70472de9f04fabb09fed251a9fed250a9fed253a9e4600264ff0ff380df12c090df10c0ab7ee002ab0ee084ab6ee005ab1ee006ab9ee007a00206746019009e000bf49f826500af8064001368f42bc4600f0cb8217f8012b501e01280fd8012a23d18f4274d09cf901000cf10207b0f1ff3fc5b240f38f8000220724f5e1052a37d0042a30d0032a49d1002e00f0d982701e59f820501af8004093e200bf0000004f000000cf000000008f4254d00cf102079cf801008f4247d09cf802200cf103078f4240ea02203fd09cf803200cf104078f4240ea024037d09cf804200cf10507130600223ae0002e00f0aa82013eace7022ec0f0a582b21e59f82230701e59f8205049f820301af802301af800400af8003049f822500af8024096e702f0f80010281fd1002e00f08b82701e1af80030002b40f0858259f82050152a6fd16d420022002406463de20023012206e001220724002588e101220020002343ea000501242fe2a2f12000062827d8002e00f06782731e1af80300002800f0618259f82300242a40f0858080f00045012400221e4617e2bf2d5ad801220292002205f03f000e244ff0000b00928f4200f019819cf802300cf10308002215e1322a51d002f0fe0030284dd0402a40f0a380032ec0f03682701e1af80020002a00f03082b21e1af80230002b00f02a82033e1af80630002b00f0248209eb8202d2ed000a09eb860209eb8000d2ed001ad0ed002a70eee10a62eea00a71eea00acee1002800f00e82023e1af80600002840f0088259f82600122a58dc102a00f03081112a00f03381122a40f02c81454354e1df2d44d8002205f01f00152400924ff0010b0122bae0002e00f0eb81a2f13100013eb0fa80f01af806304009834240f0e08159f82600302a7bd100ee900af8eee00a94e1002b00f0d481023e1af80630002b00f0ce8159f82630212a40f3ed80222a00f02581232a00f02981252a40f03a8100ee903a01ee900af4ee610af1ee10fa48bff0ee601a11ee905a71e1ef2d72d805f00f001c24012274e0a2f11303022b80f0c480002d00f0a381b0f1004f08bf15f1010300f0eb80132a40f0f18090fbf5f5f1e0a2f1480010f1070f4ff00000c0f05f81002e00f05c81013e1af80600002800f0858109eb8600d0ed000af4ee420af1ee10faf5ee400ac8bff0ee420af1ee10fa48bff0ee430a432a72ee601a00f3b480412a00f0f380422a00f00f81432a40f0008161eea10a72ee600a1de1312a40f0948000ee900a20f00040b0f1ff4f4ff00002f4ee410aa8bf0122f1ee10fa4ff0000080f21c81f4ee400af1ee10fa00f11681002a40f01381fdeee00a10ee905a98e0f02d4ff0000040f00981002220244ff0000b009202928f427ff4e7ae00230122b8460002c02d43ea000535d3781aa8eb0103b0fa80f0b3fa83f340095b0918434fea05270ebf18f8013b00230122bbf1000f43ea07051fd1a8eb0103b3fa83f35b0918430ebf18f8013b002301222f02009d002d43ea07050ed1a8eb0103b3fa83f35b0918434fea05230ebf18f8010b0020012240ea0305474602984ff0ff3838b1601e25fa00f0c0071cbf08fa04f005430024a3e0162a10d1a842b8bf054637e0202a4cd0212a51d100ee903a01ee900a70eee10a8ee005442ae0a842c8bf054626e0451b24e0002800f1c680def800200546824238bf15469042019888bf0120019015e0442a48d0452a5fd0462a4ed161eea10a61e00025132a4ff0000208bf4ff00045002469e090fbf5f202fb15050022002462e000ee903a01ee900a61eea00a57e000ee900af5ee400af1ee10fa00f0928001ee903ac1eea00a4ae000ee903a01ee900a71eea00a43e000ee903a01ee900af4ee610af1ee10fac8bff0ee601a11ee905a37e060eea01a70eea00a74ee600a61eea00a2ce0f4ee450af1ee10fa18d570eea01a60eea10a22e0f4ee450af1ee10fa15d560ee861a60eea11a60eea10a16e060eea00a13e060eea01a60eea10a0ee071eea10a61eea00a72ee600a07e061ee870a61eea00a61eea00a70ee820a10ee905a012400224ff000005ab9202e09d0002c3ff436ad25f00040b0f1ff4ffff630ad00202bb0bde8f08f56b3701e1af8001059f82000d9b102ee100a20f00040b0f1ff4f4ff00001b4ee412aa8bf0121f1ee10fa4ff00000e4dab4ee402af1ee10fadfd40029ddd1bdeec20a10ee100a019adef804101143cef804102bb0bde8f08f00202bb0bde8f08f80b5002970d090f900c0bcf1200f5ffa8cfc07db012010604ff0010ec3f800c0704680bdacf1c20ebef11d0f0fd801295ad090f9011011f1410f55dc02211160407800f03f006cf38a108446e4e70cf0f00ebef1e00f23d1032945d390f901e01ef1410f40dc90f9021011f1410f3bdcbcf1e00f5ffa8ef101d1a02934d3bcf1ed0f01d19f292fd80321116042784fea0c3189b2807802f03f0241ea821136e004294ff0000ebbd3acf1f50111f1050fb6d390f901e01ef1410f15dc90f9021011f1410f10dc90f9031011f1410f0bdcbcf1f00f5ffa8ef101d1902904d3bcf1f40f03d18f2901d9002080bd042111604ff4e011427801ea8c4c817802f03f02c0784cea023201f03f0142ea811100f03f0001eb000c7fe72de9f04188b04af2bb261c46c0f24e0613460a46ddf83880002706f1ae05214627706770a770e77027716771a771e77127726772a772e77227736773a773e77327746774a774e77427756775a775e77527766776a776e77627776777a777e777a8470028c8f800705cd020786178a278e3780843104318434ff0010008d0e179a27911431ebf617a227a51ea020102d108b0bde8f081a07b14284ff00000f7d1217d8df8040041f0010121758df805008df806008df807008df808008df809008df80a008df80b008df80c008df80d008df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f0001a92046b047c8f8000018b1012008b0bde8f08106f1520120468847d8f80000002818bf012008b0bde8f081002804bf4ff0ff3070472de9f04f95b00df1200c8ce80f00f8f788fd002800f02a824ef66c6a4ff29c5b4ff62c4748f2a355ccf2ff0ac2f2290b4ff6ff76cff6ff774ff000080124c0f2450503e000bfbff32f8fa847dbf80090bff35f8fdbf80000bff35f8f01466ff39601b1f1005f03d10168514500f0bb804ff47570f9f7cdfc002800f0f781394600bf4218463182f8d48382f8d58382f8d68382f8d78382f8d88382f8d98382f8da8382f8db8382f8dc8382f8dd8382f8de8382f8df8382f8e08382f8e18382f8e28382f8e38382f8e48382f8e58382f8e68382f8e78382f8e88382f8e98382f8ea8382f8eb8382f8ec8382f8ed8382f8ee8382f8ef8382f8f08382f8f18382f8f28382f8f38382f8f48382f8f58382f8f68382f8f78382f8f88382f8f98382f8fa8382f8fb8382f8fc8382f8fd8382f8fe8382f8ff8382f8008482f8018482f8028482f8038482f8048482f8058482f8068482f8078482f8088482f8098482f80a8482f80b8482f80c8482f80d8482f80e8482f80f8482f8108482f8118482f8128482f8138482f8148482f8158482f8168482f8178482f8188482f819847ff470afc0f800a084728683c683068446848684c684068546858685c685068646868686c686068746875be8001f49457ff43aafbff35f8f4be8000121b15be8001f4945f8d02fe7d0f86c11209e4ff0ff3029b30022b2eb562f21d151f82600002800f03481431a98074ff0ff3017d16ff4403240f60f071a44cff6fd77ba420ed3ca1852f8080cb04240f0208152f80c1ca1f1011010f5803f04d24ff0ff3015b0bde8f08f002a4ff0ff30f8d052f8047c8f42f4d1c3f540339942f0d810ad104600222b460024fef7f0fe002800f0fe80239b0f22990603f00f0002eae17101fb00f289214a43d20a8df830200e2213f0200f08bf0f2408bf012200fb02f201fb02f24fead2228df831204ff00d0208bf022200fb02f201fb02f24fead2228df832204ff00c0208bf032200fb02f201fb02f24fead2228df833204ff00b0208bf042200fb02f201fb02f24fead2228df834204ff00a0208bf052200fb02f201fb02f24fead2228df835204ff0090208bf062200fb02f201fb02f24fead2228df836204ff0080208bf072200fb02f201fb02f24fead2228df837204ff0070208bf082200fb02f201fb02f24fead2228df838204ff0060208bf092200fb02f201fb02f24fead2228df839204ff0050208bf0a2200fb02f201fb02f24fead2228df83a204ff0040208bf0b2200fb02f201fb02f24fead2228df83b204ff0030208bf0c2200fb02f201fb02f24fead2228df83c204ff0020208bf0d2200fb02f201fb02f24fead2228df83d204ff0010208bf0e22424360434843c00a8df83f00c3f3001006900ca84a43cde9045008ac0b9dd20addf88890ddf884a0ddf87c8013cc1e9e8df83e2022462b46cdf80c90cdf808a0cdf80480009600f02df8dde91201a2455044c8bf5446a9454944c8bf4d46b042b8bf06464145b8bf8846a64211dd4146a8450edd249800680f280ad8249f491b321b07ebc0030130dc801d815a8199813860002015b0bde8f08f4ff0ff3015b0bde8f08f2de9f04f8fb0dde91c5a0e9129691e9eddf868c0002900f05c812f68002e40f05381874200f050819af80f100f2940f04b819af80e100e2940f046819af80d100d2940f041819af80c100c2940f03c819af80b100b2940f037819af80a100a2940f032819af80910092940f02d819af80810082940f028819af80710072940f023819af80610062940f01e819af80510052940f019819af80410042940f014819af80310032940f00f819af80210022940f00a819af80110012940f005819af80010002940f000811b9fb2eb0c044ff00001d8bf0c46db1bd8bf0b46d5f808e0ea681d46e919189fa444aeeb0403a7eb0c070694bb42b8bf1f46199b521b0b91591a8a42b8bf1146012f0c910797c0f2e5820c990129c0f2e1820ef101014c08069900eb6c000a9048100990042020ea81000590079801310138cde903014810029004fb05f000f102094ff0000acde9005c089407e000bf0c980af1010a8245a14400f0bb821c980e9b01680aeb050000fb04120b980a9f504400fb03705feacc7303d1099fdde9063810e0099b2e46d35c059d0778eb4007f0f00703f00f033b4400f8013b049bdde902783546d619da074fea580b0d9208d131465a466446f1f7eafda446089c68e000bfb8f1020f64d3abf10102032a04d200211bf0030e35d15be06ff0030202ea58034f44b646c3f1000c3944471c8e18f578b478c5f3031545ea0415bc18e5703579f378c5f3031545ea0313237173793579c3f3031343ea05136371b3797679c3f30313043243ea06130ceb02060436a371dcd1dde9005c089c111d76461bf0030e26d04b1cf75c3246765cc7f30317ddf804c047ea06171646bef1010f475417d08f1cd65dd55cc6f3031646ea0516009dddf804c0bef1020fc654164608d03144c978f25dc1f3031141ea0211c15500bf5feac8713ff45aaf0d9a16f80b10002a4ff0000208bf042210f80b30d14003f00f0242ea011100f80b1047e7874200f095810026a1e16d68002d00f0018200244ff000090ae000bfdde90c14d8f804500c44ddf874a0ac4280f0f281ddf870800d94d8f800100f5d3e0903d001210c910ce000bf2e1b012e00f0e28121444d78002d00f0628102210c912e4617f00f011e9f1af801a0b7fa87f74fea571718bf012147ea010e1b9f4fea0a1b16f003014c4600f08980cde90a16d8f808100026b9fbf1f404fb119161449142a8bf012606ea0e06012e18d13c449c4215db189da94212da199dac420fda0e9d4e0804fb0504c907a75d6ff00f01554604bf0f215d4639401b9f2943a1550a990b9e012909f1010458d0d8f80810b4fbf1f606fb1141002461449142a8bf012404ea0e04012c18d1f4199c4215db189da94212da199dac420fda0e9d574604fb05044d08c907665d6ff00f0104bf0f215f46314039431b9f61550a990b9e022909f102042ad0d8f80810b4fbf1f505fb1141002461449142a8bf012404ea0e04012c19d1ec199c4216db189da94213da199dac4210da0e9d574604fb05044d08c907665d6ff00f0104bf0f215f4631400b9e39431b9f615509f10304711e0329b14405d23ae700bf0434a1453ff436afd8f808100026b4fbf1f505fb114161449142a8bf012606ea0e06012e1bd13d449d4218db189eb14215da199eb54212da0e9ed04605fb06054e08c907af5d6ff00f0104bf0f21d84639401b9f41ea0801ddf87080a955d8f80810661cb6fbf1f505fb1161002661449142a8bf012606ea0e06012e1cd13d449d4219db189eb14216da199eb54213da0e9e574605fb06054e08c90715f806806ff00f0104bf0f215f4601ea0801ddf8708039431b9fa955d8f80810a61cb6fbf1f505fb1161002661449142a8bf012606ea0e06012e1cd13d449d4219db189eb14216da199eb54213da0e9e574605fb06054e08c90715f806806ff00f0104bf0f215f4601ea0801ddf8708039431b9fa955d8f80810e61cb6fbf1f505fb1161002661449142a8bf012606ea0e06012e7ff466af3d449d42fff662af189eb142bff65eaf199eb542bff65aaf0e9e574605fb06054e08c90715f806806ff00f0104bf0f215f4601ea0801ddf8708039431b9fa95544e7042e78d38e78c97856ea012673d0042188e61b99002901dd012609e0b1fa81f149090027bcf1000fc8bf012707ea0106d5f808e0d5f80cb00bfb0ef9b9f1000f59d01e990024b1fa81f149094ff0ff380d91f1070c9107e000bf01340bfb0ef94c45a8f1010846d20c991c9e0029214618bf09eb0801b1fbfef536680ef1010705fb1e117f0805fb07664f08f65d042727ea8107fe4016f00f094f4618bf01270d9e3743012fd8d161449142d5db1b9f3d449d42d1db189fb942ceda199fbd42cbda0e9f1af8096005fb070e4d081ef80570c90707f00f0907f0f00747ea06071c9908bf49ea06170ef80570d1f808e0d1f80cb0b1e70fb0bde8f08f00bf002804bf4ff0ff3070472de9f04f99b01c461546cde90e01f7f7d6ff002800f01f82cde90c544ef66c6b4ff29c594ff62c4648f2a355ccf2ff0bc2f229094ff6ff7acff6ff7600270124c0f2450502e0bff32f8fa847d9f80080bff35f8fd9f80000bff35f8f01466ff39601b1f1005f03d10168594500f0cb804ff47570f8f71bff002800f0ec81314600bf4218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f800b08472a0f81ca0a0f81ea0a0f820a0a0f822a0a0f824a0a0f826a0a0f828a0a0f82aa0a0f82ca0a0f82ea0a0f830a0a0f832a0a0f834a0a0f836a0a0f838a0a0f83aa059e8001f41457ff42aafbff35f8f49e8000121b159e8001f4145f8d01fe7d0f86c21279e249d4ff0ff302ab30021b1eb552f21d152f82500002800f01881811a88074ff0ff3017d16ff4403340f60f070b44cff6fd77bb420ed3571857f8080ca84240f0048157f80cacaaf1011010f5803f04d24ff0ff3019b0bde8f08f57f8040c50454ff0ff30f6d1c1f540318a45f2d8002ff0d0baf1c10fedd33878012840f0e580b0060f2206f00f0102eae070484389225043c00a8df854000e2016f0200f08bf012001fb00f002fb00f04fead0208df855004ff00d0008bf022001fb00f002fb00f04fead0208df856004ff00c0008bf032001fb00f002fb00f04fead0208df857004ff00b0008bf042001fb00f002fb00f04fead0208df858004ff00a0008bf052001fb00f002fb00f04fead0208df859004ff0090008bf062001fb00f002fb00f04fead0208df85a004ff0080008bf072001fb00f002fb00f04fead0208df85b004ff0070008bf082001fb00f002fb00f04fead0208df85c004ff0060008bf092001fb00f002fb00f04fead0208df85d004ff0050008bf0a2001fb00f002fb00f04fead0208df85e004ff0040008bf0b2001fb00f002fb00f04fead0208df85f004ff0030008bf0c2001fb00f002fb00f04fead0208df860004ff0020008bf0d2001fb00f002fb00f04fead0208df861004ff0010008bf0e2001fb00f002fb00f04fead0204ff000038df8620008bf0f235943299d51430020c90a002d8df863103ff427af289cc6f30010a7f13f0810ae0b9001e0013d1cd014f8010b411e1f29f8d341b220290fdb08eb400118f81000497840ea0122c12a06d3384651463346fef74af80028e5d14ff0ff3019b0bde8f08f0d9c2698ddf894b0a042c8bf04462a980894dde9289406300790cde909a704e058440b38013c834653d019f8010b1f28f6d908eb400118f81000497840ea0122c12ad8d3384651463346fef71cf80028d1d00b980c9f069015a8cde90460ddf898a0229ddde90e010d9b239e3a46cde902ba00950196fff71cfadde91201bb4558445144d8bfbb46a8422a46b8bf0246b142a8bf31465a4517dd089b994214dd2a9b1b680f2b10d8089ea2eb0b02a1eb060c079901ebc307ba802a9a21f833b0591c7e80a7f806c01160dde909a710aea8e7002019b0bde8f08f2de9f04fd3b0dde95ce5dde9648cdde9629a0d9075b1dde96050dde95e47a94224eae47b27eae774a8bf29468242a8bf024602e000244ff0000b09eb0e0008eb00055845c8bf83468d420ceb0a07b8bf2946a24588bf54469742b8bf3a468b45b8bf944202db53b0bde8f08f669804f0010701974fea5b07033809970bf101070090580008974e080c90002701200df14c0e0df10c0c02948ce80e00cde90a6b06e000bf0798012700284ff00000dad0c00707906698069708bf0830ddf800906ff007086ff004044ff0000b4ff0000a0f9018bf09f10809cdf838900f994fea5a00085c102300f0f002cc46a14610901194cdf8488000bf1cf8041f0eeb08001cf8014c9cf8015042ea141704f00f041443447242ea111401f00f01847211430eeb0904217242ea15119cf802600772617205f00f011143a17242ea1611817306f00f01114308f10808083b09f10809c173d1d11098ddf83890000110224b465d4600bf13f8041f0eeb050713f8014c5e7840ea141804f00f0404437c7440ea111401f00f010143f97440ea161193f802c0397506f00f010143797540ea1c11b9750cf00f010143083a05f1080587f81080bc74f975d5d1ddf84880119c0af1020a08f120082034baf1100f0bf1200b84d101980699049a48400299059b00eb01090399dde90a6b9145bff65baf0d9803fb090807e000bf0c9809f1020991458044bff64faf0d98dc4609fb03045feacb700bd0099dddf820c0605d1ef8007000f0f00007f00f0738446055b6eb5c0f28d94fea5c0a04eb060b464616f80a0006eb0a041ef80050204600f8015b58451dd205781ef80570a51c5d45077017d228781ef800002870e51c5d4510d2287804361ef80000287006eb0a055d45ddd306e000bf04eb5c05c80705d1b4e7651cdde90a6bc807afd08c45adda28781ef8007000f00f0007f0f00738442870a3e72de9f04f8bb01d9d6c68072c00f286802cb12f89b94200f08180012c7bd100287cd0d0f86c61002e78d00027b7eb512f74d156f82170002f70d0a7eb060c5fea8c776bd16ff440370ceb070e40f60f07cff6fd77be4561d306eb0c0e5ef8086c8e425bd15ef80c6ca6f1011717f5803f54d35ef8047cb74250d1ccf54037be424cd8bef1000f49d0032e47d39ef80070022f43d19446aa69ddf86ca0dde91987dde917b9b2b14fead10801f00704da464ff0010b4f4612f808900bfa04f444ea090402f80840d346ddf86ca0b946dde919876c6805eb44021181721e1c9e01346c600996169e0ef101010396159e0a950296149ecde904b9cde900366346cde90687cdf820a0fbf713fb6968013969600bb0bde8f08f6f89b94204d14ff0ff300bb0bde8f08f022c3ff479afaf89b942f4d0032c3ff473afef89b942eed0042c3ff46daf2f8ab942e8d0052c3ff467af6f8ab942e2d0062c3ff461afaf8ab9427ff45dafdae700bf70b5049d8b42c8bf19469542b8bf2a46914205dadde905e3bef1000f01d103b170bdbef10f0c18bf9c46cb070ed04b10c45cbcf1000f03d004f00f05ae4504d904f0f00444ea0e04c45401314c1000eb61035510bcf1000f00eb620049d0ac425fda00bf1c78764604f00f05beeb141f38bf2609754598bf754645ea06141d4605f8014b854265d25c78764604f00f05beeb141f38bf2609754598bf75469c1c45ea061584425d7040d29c78764604f00f05beeb141f38bf2609754598bf7546dc1c45ea061584429d702fd2dc78764604f00f05beeb141f38bf2609754598bf754645ea06151c1ddd7084422346b9d31ce0ac4219da4eea0e1500bf1c4604f8015b844226d29c1c84425d700ed2dc1c84429d700ad21c1ddd7084422346edd304e01c46d00704d17ae71c46d0073ff477af9142bff674af2078bcf1000f03d0beeb101f98bf70bd00f00f0040ea0e10207070bd5c1cd007ebd161e72de9f04f81b04ef66c6b4ff29c554ff62c4848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0bd804ff47570f8f7cff9002800f02e81414600bf4218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f800b080f80a908683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa1427ff43bafbff35f8f00bf45e8000121b155e8001fa142f8d02fe7d0f87812002974d104464ff41630f5f7b1fe014600284ff0000069d0002200bf8b1888544032b2f5163f58709870d870187158719871d871187258729872d872187358739873d873187458749874d874187558759875d875187658769876d876187758779877d87783f8200083f8210083f8220083f8230083f8240083f8250083f8260083f8270083f8280083f8290083f82a0083f82b0083f82c0083f82d0083f82e0083f82f0083f8300083f8310083f8320083f8330083f8340083f8350083f8360083f8370083f8380083f8390083f83a0083f83b0083f83c0083f83d0083f83e0083f83f009ad1c4f8781200e00021084601b0bde8f08f00bf70b5044690f8800384f8810350b3d4f8700347f64c6642f66535c2f20706c0f2440520b131682d31c4f8781310e040f22f00c0f200007844002122460023a847c4f87003316801f12d01c4f8781330b105f1e8022d219047002808bf70bd002084f8810370bd00bf2de9f047a4b0d0f87442bff35f8fa4b142f691760546c0f2440620460021b04770b1d5f8700340b142f64d42c0f244022d2124b0bde8f047104724b0bde8f08747f64c69c2f20709d9f8007095f8810300283fd0f7f7c0f8e0b3d5f87813781a17d495f8640110b995f80403e0b395f8800385f8810380b3d5f8700342f64d48c0f2440878b1d9f800102d31c5f878131de0d5f8700300b342f64d42c91bc0f24402904719e04ff64b70cff6ff70a8f1e807784400212a460023b847c5f87003d9f8001001f12d01c5f8781310b12d21c04710b1002085f8810306f16601204624b0bde8f04708474af21f37c0f2470700200290adf80c00b84795f8640110b995f8040318b12846fff746ffe5e70df104084046f5f7d6fa02a9284600224ff0000afaf71ffe81464046f5f713fcb9f1000f02d085f881a303e00146284600f009f89df80c000028c7d107f14c008047c3e700bf2de9f04f90f8b13300f5617c4cf82310591c41f69a134b4390f8b0b31b0c03eb8303a1eb4301bbf1090f80f8b1139cbf0bf1010b80f8b0b3bbf1040f0bf0030e05d200244ff000094ff0000a2ee00bf0fc0800244ff000094ff0000a00eb8406d6f88473d6f88853b7fbfbf3d6f88c1303eb090203fb1b73b5fbfbf7d6f8906307fb1b5553443a44b1fbfbf72b44b6fbfbf53a4407fb1b1105eb020905fb1b6219440434a04502eb010ad7d1bef1000f1dd05cf82450bef1010fb5fbfbf606fb1b55b144aa4412d00ceb84056e68bef1020fb6fbfbf404fb1b66a144b24406d0ae68b6fbfbf505fb1b66a944b244bafbfbf202eb0901c0f8ac13bde8f08f00bff0b500294ff6ff7e00f0af80012903d10022c90775d1a8e021f00102c2f1000c6ff0010241f221038418a578e7788eea05252cb26e0036b203ead43484ea450503ead6366c0086ea450624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead53585ea460686ea07273eb27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead63686ea470703ead5357e0085ea470736b27d002db203ead636023286ea470703ead5360ceb0205023586ea470e95d10232c90733d0805c41f221038eea002001b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead232410082ea400009b2420012b203ead13181ea400003ead23181ea400e1ffa8ef0f0bd2de9f04fcbb00646c07c8b468df80600b08954998df80700000a8df808008df80a10080af1898df80b00080a17468df80d000020012b8df805308df809b08df80c108df80e008df80f008df810008df811008df812008df813008df814008df815008df816008df817008df818008df819008df81a008df81b008df81c008df81d008df81e008df81f008df820008df821008df822004cd1307e96f81780a0f1090144f62510c2f29240a1fb0002891a02eb510100970df10500b8eb910f88bf4fea9108c8ebc80706f11a0400f1010a00f10509093021463a46f0f711fa06f1210506f1280029460722f0f709fa284621460722f0f704fa20465146032207f10905f0f7fdf906f11d0049460422f0f7f7f9f07d02289cbf0130f075009fbb450cd0b8f1240f13d94ff0ff304bb0bde8f08fbb454ff00905f07509d14ef60b67abb20df10502c0f247070120f02115e002200df12304aab28df82300a01c0df105018df82470f0f7cbf9a81c4af6610782b2c0f24607f02021460023b8474bb0bde8f08f2de9f04184b0044690f84d0000284ff0000000f09a8094f84f1042f28b0701f078018df8071094f85010c0f25b0701f007018df80610cde9020002a906200022b847054670b9002002adcde902000df1070128460122f0f791f9012029460122b847054694f84e00a8b1002602a906200022cde90266b84760b9cde9026602ae04f1540130460122f0f778f9572031460122b8470543002602a906200022cde90266b84768b90df108080df1060140460122cde90266f0f761f9312041460122b847054302a906200022cde90266b84768b9002002ae04f15301cde9020030460122f0f74bf9462031460122b8470543002602a906200022cde90266b84768b90df1080804f1510140460222cde90266f0f734f9362041460222b847054302a9a3200022cde90266b847054302a997200022cde90266b84742f62b310543c0f2440102208847284615b104b0bde8f081002184f84e1084f84d10a16504b0bde8f08100bf2de9f04f8bb00190233002254ff000080df10c0a4ff001094ff0000b002402900be000bf09fa0bf0002f0bf1010b08bf0443bbf1070f354426d041f66320c0f20000784410f80b0041f65d21c0f20001794411f80b6042f2fb0351463246c0f25b03cde90988cde90788cde90588cde90388984707460028d4d10298514628443246f0f7c9f8cde742f2fb00c0f25b004ff60073002600f1340703a9c0f21f0381200c22cde90966cde90766cde90566cde90366b84730b9029803a928440c22f0f7aaf88026ddf804c040f6b80146ea0400220ac2f200018cf823008cf824200a68002a00f0ad8045f2fd774ff6ff766ff0020241f22103c0f202070d681544ed7886ea052635b203ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea46060d6824b2154403ead4342d7984ea460686ea052635b203ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea460624b275002db203ead43484ea460603ead535740085ea46060d6824b2154403ead4346d7984ea460686ea052635b2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead434750084ea46062db2740024b203ead53585ea460603ead4350332ba4285ea46067ff460afb1b2090a01e0ff26ff218cf83e109cf84c109cf84d208cf83f1026218cf83d608cf84020ff288cf84b101cbf04208cf81c000bb0bde8f08f70b588b042f2fb0514460e46c0f25b05002181280791cde90511cde90311cde90111009107d11b023435694681202246a84720b905e069462246a84708b108b070bd6946054630462246eff7abff284608b070bd2de9f0478cb08579972d17d00446fe2d0fd0fd2d2ad142f69570c0f25b008047002084f84e0084f84d00a0650cb0bde8f08720460cb0bde8f047cbe50020cde9040042f2fb00c0f25b00a0f1700304a9972000229847044642f62b31c0f244010220884720460cb0bde8f08742f2fb08002094f80c90677bc0f25b08cde90a00cde90800cde90600cde9040004a99f200422c047064620b901a804a90422eff757ff002e6ff00100ddd19df80410bd29d9d19df805104029d5d19df806101029d1d194f84d00002852d047f64c60c2f20700006843f698210844a065002604a937200222cde90a66cde90866cde90666cde90466c047002840f0898002a804a90222eff725ff04a947200122cde90a66cde90866cde90666cde90466c047002875d10df1030004a90122eff711ff0020cde90a00cde90800cde90600cde9040004a905200122c047002860d10df1020004a9012249ea0726eff7fafe352d5bdc002d00f08180012d70d19df8020082e0002604a9052001228df80e60adf80c600296cde90a66cde90866cde90666cde90466c047d8bb0df1080a04a950460122eff7d6fe04a935200122cde90a66cde90866cde90666cde90466c04738bb0af1010004a90122eff7c3fe002604a937200222cde90a66cde90866cde90666cde90466c04798b90af1020004a90222eff7affe04a947200122cde90a66cde90866cde90666cde904660126c04778b14ff0ff300cb0bde8f087362d6ed0572d15d194f84e00002800f08580572039e00af1040004a90122eff78bfe04f14f0051460722eff785fe84f84d603ee700208df801006946284601228df8009000f04ef9312d044627d036e09df80200002e00f0380618bf4036704010f0380f10d09df808009df8091041f25a530e22c0f2000302ea96027b449a5a41ea0020904239d8012000218df80110694601228df8006000f024f904460df103014620012200f01df9054602a93620022200f017f945ea040141ea00040025a8f1700604a9a3200022cde90455b047044304a997200022cde90455b0470443aee69df802000e2101ea900041f2e241c0f200017944085a864204d96ff001000cb0bde8f0876946362002228df800708df8019000f0e6f80446d0e70020cde90a00cde90800cde90600cde9040004a9592001220125c04700287ff45caf04f1540004a90122eff7f7fd84f84e5060e740f6b800c2f200000168002904bf4ff0ff30704780b584b000684ef2173102904ff41630039002a8c0f2470188474ff4f07042f29f7e01904ff4207cc0f25b0e0020002100220023cdf800c0f04704b080bd00bf2de9f04340f6b801c2f2000109684ff0000e01f1020c4cf6cd4340f2762808e00ef1010ebef5f07f0cf5a07c08bfbde8f083aef1a0096ff00906644607e000bf07eb0711a170053445452e46e8d290f84c1006f10a054f1e112905fb03f208bf570d122908d1a6f1e6019f294ff000079cbfb9f19f0f077b07eb071104f8021c90f84c1006f10c024f1e112903fb02f208bf570d122908d1a6f1e4019f294ff000079cbfb9f19f0f077b07eb071104f8011c90f84c1006f10e024f1e112903fb02f208bf570d122908d1a6f1e2019f294ff000079cbfb9f19f0f077b07eb0711217090f84c1006f110024f1e112903fb02f208bf570d122908d1a6f1e0019f294ff000079cbfb9f19f0f077b07eb0711617090f84c1006f112024f1e112903fb02f208bf570d12298ad1a6f1de019f294ff000079cbfb9f19f0f077b80e700bf2de9f04383b042f28b0914460e460546c0f25b090027694606200022cde90077c84710b103b0bde8f083e846404631462246cde90077eff70dfd284641462246c84703b0bde8f08370b50446d0f83c010d4638b92046f6f7e5f90028c4f83c0108bf70bd42f64d46c0f2440606f140018847d4f83c0129463246bde8704010472de9f04f81b0934612788ab1022300bf0beb030717f8012c62b11bf8032062b17a784ab1ba780433002af1d1023b04e0002205e0013b00e0013303eb4302520042eae272931c02f1010c01eb810203f0030e23f0030a00eb8213a3f5a0784ff0ff3906e009f10109b9f10d0f08f5a0786bd009eb010202eb8202bcf1030f00eb821605d24ff0ff34bef1000f2ed1e9e70024474604e000bfa24507f1020721d0621cd209042a03d83a7802f0f0023a70a21cd209042a03d87a7802f00f027a70e21cd209042a03d87a7802f0f0027a700434e209042adfd86208b55c05f00f05b554d9e7013cbef1000fbbd0a21cd509042d08d85208b55ce7076ff00f0708bf0f273d40b554bef1010fabd0e21cd509042d09d85208b55ce7074ff00f0708bf6ff00f073d40b554bef1020f9ad0221dd509042d96d85208e407b55c6ff00f0408bf0f242c40b4548ce79bf80010cdf800b0002900f0ce80022041f2410a41f2420b41f2430808e0009a063012f8011f03330029009200f0bd80a1f17f0717f15f0ff1d340f65d42c0f2000201eb41017a4402eb81014ff20014a1f5c079cff6ff7402e014f52074ded0494619f9027bc509042d09d8b7f1ff3f06dc1a1992f8016f46f0f00682f8016f5ffa87fe5fea4e6208d5042d06d81a1992f8017f47f00f0782f8017f5fea8e620ad540f27d22904206d81a1992f8027f47f0f00782f8027f5feace6209d5b0f51f7f06d81a1992f8027f47f00f0782f8027f5fea0e724fea900c09d5bcf19e0f06d81a1992f8036f46f0f00682f8036f5fea4e720ad540f27a22904206d81a1992f8037f47f00f0782f8037f91f90110042d09d8b1f1ff3f06dc1a1912f80a7047f0f00702f80a70c9b24a0608d5042d06d81a1912f80a7047f00f0702f80a708a060ad540f27d22904206d81a1912f80b7047f0f00702f80b70ca0609d5b0f51f7f06d81a1912f80b7047f00f0702f80b700a0709d5bcf19e0f06d81a1912f8087047f0f00702f8087049077ff565af40f27a2188423ff660af191911f8082042f00f0201f8082057e701b0bde8f08f00bf2de9f04f81b0134681464a1c00f08f808e0a4ff0000b19f80b20002a00f0888109eb0b077a78002a00f00a81ba78002a00f00981fa78002a00f008813a79002a00f007817a79002a00f00681ba79002a00f00581fa79002a00f004813a7a002a00f003817a7a002a00f00281ba7a002a00f00181fa7a002a00f000813a7b002a00f0ff807a7b002a00f0fe80ba7b002a00f0fd80fa7b002a00f0fc803a7c002a00f0fb807a7c002a00f0fa80ba7c002a00f0fb80fa7c002a00f0fc803a7d002a00f0fd807a7d002a00f0fe80ba7d002a00f0ff80fa7d002a00f000813a7e002a00f001817a7e002a00f00281ba7e002a00f00381fa7e002a00f004813a7f002a00f005817a7f002a00f00681ba7f002a00f00781fa7f002a00f008810bf1200bbbf1600f7ff477af4ff0600b00e1002119f80120002a00f0af8109eb01056a78002a00f0b080aa78002a00f0b180ea78002a00f0b2802a79002a00f0b3806a79002a00f0b480aa79002a00f0b580ea79002a00f0b6802a7a002a00f0b7806a7a002a00f0b880aa7a002a00f0b980ea7a002a00f0ba802a7b002a00f0bb806a7b002a00f0bc80aa7b002a00f0bd80ea7b002a00f050812a7c002a00f04e816a7c002a00f04c81aa7c002a00f04a81ea7c002a00f048812a7d002a00f046816a7d002a00f04481aa7d002a00f04281ea7d002a00f040812a7e002a00f03e816a7e002a00f03c81aa7e002a00f03a81ea7e002a00f038812a7f002a00f036816a7f002a00f03481aa7f002a00f03281ea7f002a00f03081203160297ff479af32e10bf1010b74e00bf1020b71e00bf1030b6ee00bf1040b6be00bf1050b68e00bf1060b65e00bf1070b62e00bf1080b5fe00bf1090b5ce00bf10a0b59e00bf10b0b56e00bf10c0b53e00bf10d0b50e00bf10e0b4de00bf10f0b4ae00bf1100b47e00bf1110b44e00131f6e00bf1120b3fe00231f1e00bf1130b3ae00331ece00bf1140b35e00431e7e00bf1150b30e00531e2e00bf1160b2be00631dde00bf1170b26e00731d8e00bf1180b21e00831d3e00bf1190b1ce00931cee00bf11a0b17e00a31c9e00bf11b0b12e00b31c4e00bf11c0b0de00c31bfe00bf11d0b08e00d31bae00bf11e0b03e00e31b5e00bf11f0bb1f5205f4ff0010100931fd364234ff47a7442f2107700bf01eb81025200b6fbf2f50a2d11d301fb03f2b6fbf2f50a2d0bd301fb04f2b6fbf2f50a2d05d37943b6fbf1f2092ae7d800e011465846bbf15f0f98bf5f20c0f1000c1ceb0b0a53d0b6fbf1f74cf6cd4207f13003ccf6cc4209f80b300a29a1fb023547d31af1010342d007fb11674fead508b7fbf8fe48f21f520ef1300309eb0b06c5f2eb1273706429a1fb023434d31af102032cd00efb18776409b7fbf4f544f6d35205f13003c1f26202b370b1f57a7fa1fb022323d31af1030218d005fb14729b0941f25975b2fbf3f7cdf2b715a1fb055407f13005f57007fb132642f210720bf1040b91424fea5431a9d209e0834607e00bf1010b04e00bf1020b01e00bf1030b0020009b09f80b002de00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d849444ff03f020a7003d1002189f85f1001e000204870002119f80100002800f0a18009eb0105687800285ed0a87800285dd0e87800285cd0287900285bd0687900285ad0a879002859d0e879002858d0287a002857d0687a002856d0a87a002855d0e87a002854d0287b002853d0687b002852d0a87b002851d0e87b002850d0287c00284fd0687c00284ed0a87c00284dd0e87c00284cd0287d00284bd0687d00284ad0a87d002849d0e87d002848d0287e002847d0687e002846d0a87e002845d0e87e002844d0287f002843d0687f002842d0a87f002841d0e87f002840d0203160297ff498af44e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2905d849444ff02f00087040f09580002089f85f00581c00f094809d0a4ff0000e19f80e00002800f08d8109eb0e067078002800f00f81b078002800f00e81f078002800f00d813079002800f00c817079002800f00b81b079002800f00a81f079002800f00981307a002800f00881707a002800f00781b07a002800f00681f07a002800f00581307b002800f00481707b002800f00381b07b002800f00281f07b002800f00181307c002800f00081707c002800f0ff80b07c002800f00081f07c002800f00181307d002800f00281707d002800f00381b07d002800f00481f07d002800f00581307e002800f00681707e002800f00781b07e002800f00881f07e002800f00981307f002800f00a81707f002800f00b81b07f002800f00c81f07f002800f00d810ef1200ebef1600f7ff477af4ff0600e05e100204870581c7ff46caf002119f80100002800f0aa8109eb01025078002800f0b0809078002800f0b180d078002800f0b2801079002800f0b3805079002800f0b4809079002800f0b580d079002800f0b680107a002800f0b780507a002800f0b880907a002800f0b980d07a002800f0ba80107b002800f0bb80507b002800f0bc80907b002800f0bd80d07b002800f04b81107c002800f04981507c002800f04781907c002800f04581d07c002800f04381107d002800f04181507d002800f03f81907d002800f03d81d07d002800f03b81107e002800f03981507e002800f03781907e002800f03581d07e002800f03381107f002800f03181507f002800f02f81907f002800f02d81d07f002800f02b81203160297ff479af2de10ef1010e74e00ef1020e71e00ef1030e6ee00ef1040e6be00ef1050e68e00ef1060e65e00ef1070e62e00ef1080e5fe00ef1090e5ce00ef10a0e59e00ef10b0e56e00ef10c0e53e00ef10d0e50e00ef10e0e4de00ef10f0e4ae00ef1100e47e00ef1110e44e00131f1e00ef1120e3fe00231ece00ef1130e3ae00331e7e00ef1140e35e00431e2e00ef1150e30e00531dde00ef1160e2be00631d8e00ef1170e26e00731d3e00ef1180e21e00831cee00ef1190e1ce00931c9e00ef11a0e17e00a31c4e00ef11b0e12e00b31bfe00ef11c0e0de00c31bae00ef11d0e08e00d31b5e00ef11e0e03e00e31b0e00ef11f0e0122b3f5205f1ed364234ff47a7442f2107602eb82004100b5fbf1f00a2811d302fb03f1b5fbf1f00a280bd302fb04f1b5fbf1f00a2805d37243b5fbf2f00928e7d800e00a46f046bef15f0f98bf4ff05f08c8f1000c42f2107a1ceb0e074dd0b5fbf2f30a2a03f1300009f80e004cf6cd40ccf6cc40a2fb000641d3781c3dd003fb1253f608b3fbf6f409eb0e0504f13000687048f21f50c5f2eb10642aa2fb000130d3b81c29d004fb16334e09b3fbf6f4b2f57a7f04f13000a87044f6d350c1f26200a2fb000120d3f81c16d004fb1630890941f25974b0fbf1f3cdf2b714a2fb044603f13004ec7003fb11050ef1040e52454fea5632afd209e0c64607e00ef1010e04e00ef1020e01e00ef1030e002009f80e0001b0bde8f08f0f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2904d849444ff03f00087005d1002089f85f0001b0bde8f08f0020487001b0bde8f08f434d01ffff0000000000000052420100ff00000008031000000000000000000046616365636c61772f3335000000000000000000000000000000002020202020002020000000505050000000000000000000005050f85050f85050000000002070a8a07028a8702000000048a85010202854480000000020505020689090680000002020200000000000000000000010204040404020100000000040201010101020400000000000005020f82050000000000000002020f82020000000000000000000000020204000000000000000f800000000000000000000000000202000000000080810102020404000000000708898a8c88888700000000020602020202020700000000070888808102040f8000000007088083008088870000000000818284888f8080800000000f88080f00808887000000000708080f08888887000000000f808081010202020000000007088887088888870000000007088888878080870000000000000202000002020000000000000202000002020400000000008102040201008000000000000f80000f8000000000000004020100810204000000000708888102000202000000000708898a8a89880780000000070888888f888888800000000f08888f0888888f000000000708880808080887000000000e0908888888890e000000000f88080f0808080f800000000f88080f0808080800000000070888080b888887000000000888888f888888888000000007020202020202070000000003810101010909060000000008890a0c0c0a090880000000080808080808080f80000000088d8a8a888888888000000008888c8a89888888800000000708888888888887000000000f0888888f080808000000000708888888888a87008000000f0888888f0a0908800000000708880700808887000000000f8202020202020200000000088888888888888700000000088888850505020200000000088888888a8a8d88800000000888850202050888800000000888850502020202000000000f8081020408080f8000000007040404040404070000000004040202010100808000000007010101010101070000000205088000000000000000000000000000000000000f80040200000000000000000000000000000700878888878000000008080f088888888f00000000000007088808088700000000008087888888888780000000000007088f8808078000000001820702020202020000000000000788888888878087000008080f08888888888000000202000602020202070000000080800180808080808483000004040485060605048000000006020202020202070000000000000f0a8a8a8a8a8000000000000f08888888888000000000000708888888870000000000000f088888888f0808000000000788888888878080800000000b8c0808080800000000000007880700808f00000000020207020202020180000000000008888888888780000000000008888505020200000000000008888a8a8a870000000000000885020205088000000000000888888888878087000000000f810204080f800000000182020402020201800000000202020202020202000000000601010081010106000000048a89000000000000000009f0535593747c181040101010201020c005353a829c51bd314a810260d5b0bfe090000", "desc": "append injected blobs to main-app payload" }, { - "offset": 779114, - "old": "846a3800", - "new": "7c1b3900", + "offset": 779123, + "old": "805a3900", + "new": "34733a00", "desc": "main-app subheader payload size (ps)" }, { "offset": 152, - "old": "046b3800", - "new": "fc1b3900", + "old": "005b3900", + "new": "b4733a00", "desc": "main-app TOC entry size (ps + 128)" }, { - "offset": 779234, - "old": "846a3804", - "new": "7c1b3904", + "offset": 779243, + "old": "805a3904", + "new": "34733a04", "desc": "main-app preamble length (low 24 bits)" }, { - "offset": 779238, - "old": "85c355a2", - "new": "a1afb7e4", + "offset": 779247, + "old": "effdbcea", + "new": "85d22153", "desc": "[5] ota/s200_firmware_ota.bin preamble crc32" }, { "offset": 156, - "old": "ed292ee4", - "new": "b1adc0d0", + "old": "c0c6322a", + "new": "817b5baa", "desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)" }, { - "offset": 779118, - "old": "ed292ee4", - "new": "b1adc0d0", + "offset": 779127, + "old": "c0c6322a", + "new": "817b5baa", "desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)" } ] diff --git a/app/g2/glance-host.ts b/app/g2/glance-host.ts index d58781c7..845e4cf8 100644 --- a/app/g2/glance-host.ts +++ b/app/g2/glance-host.ts @@ -21,8 +21,12 @@ export const GLANCE_SURFACE_ID = "glance"; /** Above the shell (1) and every window (0); below the lock screen (1000). */ export const GLANCE_SURFACE_Z_ORDER = 900; +/** Surface operations needed by the board, shared by Android and iOS. */ +export type GlanceDisplay = Pick; + export type GlanceHostOptions = { - getDisplay: () => DisplayTarget | null; + getDisplay: () => GlanceDisplay | null; getProvider: () => GlanceboardProvider | null; /** Whether a board may show right now (connected or preview, not locked). */ canShow: () => boolean; @@ -47,7 +51,9 @@ export class GlanceHost { /** Whether the surface is currently visible on the compositor. */ private shown = false; private hideTimer: ReturnType | null = null; - private configuredFor: DisplayTarget | null = null; + private configuredFor: GlanceDisplay | null = null; + /** Last depth given to the surface; null forces the next render to set it. */ + private configuredDepth: number | null = null; /** Serializes show/hide against each other and against in-flight renders. */ private queue: Promise = Promise.resolve(); private rendering = false; @@ -144,7 +150,7 @@ export class GlanceHost { }, Math.max(0, hideAtMs - Date.now())); } - private async configureSurface(display: DisplayTarget): Promise { + private async configureSurface(display: GlanceDisplay): Promise { await display.configureSurface(GLANCE_SURFACE_ID, { x: 0, y: 0, @@ -154,6 +160,7 @@ export class GlanceHost { transparency: "opaque", }); await display.setSurfaceVisible(GLANCE_SURFACE_ID, false); + this.configuredDepth = null; this.configuredFor = display; } @@ -252,6 +259,12 @@ export class GlanceHost { const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes)); const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer()); const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)); + // The glasses copy the screen shifted per lens while the board covers it. + const depth = this.options.getProvider()?.depth() ?? 0; + if (depth !== this.configuredDepth) { + await display.setSurfaceDepth(GLANCE_SURFACE_ID, depth); + this.configuredDepth = depth; + } await frameTimings.spanAsync(frameId, "submit", () => display.submitSurfaceFrame( GLANCE_SURFACE_ID, diff --git a/app/g2/ios-peripheral-identity.ts b/app/g2/ios-peripheral-identity.ts new file mode 100644 index 00000000..4dd32f12 --- /dev/null +++ b/app/g2/ios-peripheral-identity.ts @@ -0,0 +1,25 @@ +import { classifyAdvertisement, hasEvenManufacturerSignature, type EvenAdvertisement } from './even-advertisement' +import { hexToBytes } from '../util/hex-util' + +export type IosAdvertisement = { identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean } +export type IosDevice = EvenAdvertisement & { identifier: string } + +/** Only a full vendor-advertised MAC may bind a manually entered address. */ +export function identifyIosPeripheral(raw: IosAdvertisement): IosDevice | null { + if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(raw.identifier)) return null + if (!hasEvenManufacturerSignature(hexToBytes(raw.manufacturerData))) return null + const candidate = classifyAdvertisement({ ...raw, address: '', source: 'scan', bonded: false, + txPower: null, seenAtMs: Date.now() }) + if (!candidate?.embeddedMac) return null + return { ...candidate, address: candidate.embeddedMac, embeddedMacMismatch: false, identifier: raw.identifier.toUpperCase() } +} + +export function deviceAddressError(addresses: { left: string; right: string; ring: string }): string | null { + for (const role of ['right', 'left', 'ring'] as const) { + const address = addresses[role] + if (role === 'ring' && !address) continue + if (!/^[0-9A-F]{2}(?::[0-9A-F]{2}){5}$/.test(address)) return `${role === 'ring' ? 'Ring' : role === 'left' ? 'Left arm' : 'Right arm'} MAC address is invalid.` + } + const values = Object.values(addresses).filter(Boolean) + return new Set(values).size === values.length ? null : 'Each device must have a different MAC address.' +} diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts new file mode 100644 index 00000000..7e8ec2dd --- /dev/null +++ b/app/g2/ios-preview-controller.ts @@ -0,0 +1,908 @@ +import { startRemoteInput } from "../remote/service" +import type { KeyboardInputSession } from '../ui/shell/keyboard-input' +import { acceptInput, resetRingInputFilter } from "../ui/input-monitor"; +import { bindIosNotifications, iosNotificationsChanged, onIosNotificationPopup } from '../native/notification-icons.ios' +import { shouldShowNotificationOnGlasses } from '../native/notification-sources' +import { readActiveNotifications } from '../native/notification-icons.ios' +import { launcherEntries } from '../apps/launcher' +import { getInstalledEvenHubAppById, installedEvenHubPackageId, uninstallEvenHubPackage } from '../apps/evenhub/installed-apps' +import { isInstalledPackagePresent } from '../apps/evenhub/updates' +import { openEvenHubStoreForPackage } from '../apps/evenhub' +import { launchInstalledPackage, closeRunningPackage } from '../apps/evenhub/manager' +import { registerSystemTools } from '../assistant/system-tools' +import { registerWindowTools } from '../assistant/window-tools' +import { registerNavigateTools } from '../assistant/navigate-tools' +import { registerRoamTools } from '../assistant/roam-tools' +import { IosNavigationSensors } from '../native/ios-navigation-sensors' +import { Utils } from '@nativescript/core' +import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios' +import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core' +import { iosVoiceInput } from '../native/ios-voice-input' +import { nightscoutBridge } from '../native/nightscout-bridge' +import { FaceclawCommunicatorBridge, resolveIosPeripherals, type CommunicatorState, type RawInputEvent } from '../native/faceclaw-communicator.ios' +import { PreviewDisplayTarget, type DisplayTarget } from '../native/preview-display.ios' +import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client' +import { GlanceHost, type GlanceDisplay } from './glance-host' +import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen' +import { OsEventTypeList } from './events' +import { loadDeviceAddresses } from './device-addresses' +import { deviceAddressError } from './ios-peripheral-identity' +import { createLauncherWindow, LAUNCHER_SURFACE_ID } from '../apps/launcher/launcher-app' +import { ALL_APPS } from '../apps/all-apps' +import type { AppContext, AppDefinition, AppLaunchParams } from '../apps/app-definition' +import { WorkerAppHost } from '../ui/shell/worker-window' +import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type InProcessWindow } from '../ui/shell/in-process-window' +import { getStringSettingById, nightscoutSiteUrlSetting, nightscoutApiTokenSetting } from '../ui/dashboard-settings' +import { readPhoneBatteryState } from '../native/phone-battery' +import { iosAppUnavailableReason } from '../apps/ios-availability' +import { flattenPlanesWithDraws, planesFingerprint, type Plane } from '../graphics/plane' +import { prepareFrameDraws } from '../graphics/glyph-wire' +import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from '../graphics/image' +import { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/gestures' +import { noopLayerActions, type LayerActions } from '../ui/layers' +import { TextViewerLayer } from '../apps/files/text-viewer' +import { shell, rawInputEventToInputEvent, type ShellWindow } from '../ui/shell/shell' +import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible } from '../ui/shell/geometry' +import { DISPLAY_MODE_VALUES, displayModeLabel, displayModeSetting, onAnySettingChanged, + previewColorSetting, lockScreenEnabledSetting, getBrightnessPreferences } from '../ui/dashboard-settings' +import type { PhoneGesture } from '../phone-ui/phone-gestures' +import { isWelcomeSoundPending, setWelcomeSoundPending } from '../phone-ui/onboarding-state' +import { findSoundEffect, playSoundEffect } from '../ui/sound-effects' + +const SHELL_SURFACE_ID = 'shell' +const SHELL_SURFACE_Z_ORDER = 1 +const LOCK_SURFACE_Z_ORDER = 1000 +/** The shell render loop waits this long for the glasses to take a frame before moving on. */ +const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 2000 + +export type SessionPhase = 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error' +export type SessionState = { phase: SessionPhase; status: string; battery: number | null; charging: boolean | null; + ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string } + +function ancsCapable(capabilities: string): boolean { + return /^Faceclaw\/(\d+)/.test(capabilities) && Number(capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION +} + +/** + * iOS host for the shared app registry, shell and BLE session. The session, + * compositor and transport live in shared Kotlin (FaceclawCommunicatorBridge + * over FaceclawKitIosGlassesSession); with no glasses connected the same + * compositor runs headless (PreviewDisplayTarget) so the phone mirror works. + */ +export class IosPreviewController { + private previewTarget: PreviewDisplayTarget | null = null + private communicator: FaceclawCommunicatorBridge | null = null + private readonly startedCommunicators = new WeakSet() + state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false, + frames: 0, capabilities: '', leftVersion: '', rightVersion: '' } + private notifications: AncsClient | null = null + private readonly sessionOffs: (() => void)[] = [] + private stopping: Promise | null = null + /** Resolves once the current display target has every surface configured. */ + private displayReady: Promise = Promise.resolve() + private connecting = false + private sessionCreated = false + private wearDetectionRequested = false + private readonly glanceDisplay: GlanceDisplay = { + configureSurface: async (id, options) => { await this.display?.configureSurface(id, options) }, + setSurfaceVisible: async (id, visible) => { await this.display?.setSurfaceVisible(id, visible); this.schedulePreviewUpdate() }, + setSurfaceDepth: async (id, depth) => { await this.display?.setSurfaceDepth(id, depth) }, + setScreenBlanked: async blanked => { await this.display?.setScreenBlanked(blanked); this.schedulePreviewUpdate() }, + submitSurfaceFrame: async (id, pixels, rect, fingerprint, paintMs, frameId, draws) => { + await this.display?.submitSurfaceFrame(id, pixels, rect, fingerprint, paintMs, frameId, draws); this.schedulePreviewUpdate() + }, + } + private readonly glance = new GlanceHost({ + getDisplay: () => this.display ? this.glanceDisplay : null, + getProvider: () => ALL_APPS.find(app => app.glanceboard)?.glanceboard ?? null, + canShow: () => this.runtimeNeeded && !this.glassesLocked, + // iOS retains the EvenHub session while the shell sleeps. The board's + // opaque first frame is ready before we unblank the compositor. + ensureSessionActive: async () => { + await this.display?.setScreenBlanked(false); this.schedulePreviewUpdate(); return true + }, + onHiddenWhileAsleep: () => { + if (!shell.isScreenOn()) void this.display?.setScreenBlanked(true).then(() => this.schedulePreviewUpdate()) + else this.schedulePreviewUpdate() + }, + onVisibilityChanged: () => this.schedulePreviewUpdate(), + appendLog: message => this.logBluetooth(message), + }) + private previewTimer: ReturnType | null = null + private clockTimer: ReturnType | null = null + private offSettings: (() => void) | null = null + private lastBrightness: string | null = null + private active = false + private runtimeRunning = false + private clockMinute = -1 + private shellRenderInProgress = false + private shellRenderQueued = false + private nextFrameId = 1 + private nextWorkerFrame = 0 + private inputQueue: Promise = Promise.resolve() + private lastLayout = '' + private readonly appHosts = new Map() + private readonly inProcessApps = new Map() + private readonly batteryObservers: any[] = [] + private readonly lockObservers: any[] = [] + private phoneLocked = false + private glassesWorn: boolean | null = null + private glassesLocked = false + private lockEnabled = lockScreenEnabledSetting.get() + private acknowledgedFrames = 0 + private logLines: string[] = [] + private logTimer: ReturnType | null = null + private readonly actions: LayerActions = { + ...noopLayerActions, + playBuzzerSequence: payload => this.communicator?.playBuzzerSequence(payload), + requestRender: () => this.requestShellRender(), + disconnect: () => this.disconnect(), + startVoiceCapture: endpointing => this.startVoiceCapture(endpointing), + stopVoiceCapture: () => iosVoiceInput.stopPushToTalk(), + startContinuousVoiceCapture: () => this.onError("Voice capture is not available on iOS yet."), + startTextSettingEdit: async setting => { await this.editSetting(setting) }, + startTextSettingsEdit: async (settings, title, finished, toggle, cancelled) => { + for (const setting of settings) if (!await this.editSetting(setting)) { cancelled?.(); return } + if (toggle) { + const choice = await Dialogs.action({ title, cancelButtonText: 'Cancel', actions: [toggle.label, 'This session only'] }) + if (choice === 'Cancel') { cancelled?.(); return } + toggle.setting.set(choice === toggle.label) + } + finished?.() + }, + } + + constructor(private readonly onFrame: (image: ImageSource, focus: string) => void, + private readonly onError: (message: string) => void, + private readonly onConnectionState: (state: SessionState) => void = () => {}, + private readonly onKeyboardInputChanged: (session: KeyboardInputSession | null) => void = () => {}) { + this.ensurePreviewDisplay() + shell.configure({ + actions: this.actions, + voiceInputEnabled: true, + onKeyboardInputChanged: session => this.onKeyboardInputChanged(session ? { + targets: session.targets, + setText: text => { if (this.active && !this.glassesLocked) session.setText(text) }, + send: () => { if (this.active && !this.glassesLocked) session.send() }, + sendTo: id => { if (this.active && !this.glassesLocked) session.sendTo(id) }, + discard: () => session.discard(), + } : null), + prepareVoiceCapture: () => this.prepareVoiceCapture(), + getScreenTimeoutMs: () => null, + requestShellRender: () => this.requestShellRender(), + onWindowsChanged: () => this.requestShellRender(), + onScreenStateChanged: on => { + if (on) this.glance.dismiss() + void this.display?.setScreenBlanked(!on).catch(error => this.fail(error)) + this.requestShellRender() + }, + }) + const launcher = createLauncherWindow({ + actions: this.actions, + apps: () => launcherEntries(ALL_APPS), + launchApp: id => this.launchApp(id), + uninstallApp: id => this.uninstallApp(id), + submitFrame: planes => this.submit(LAUNCHER_SURFACE_ID, planes), + setSurfaceVisible: visible => this.setWindowSurfaceVisible(LAUNCHER_SURFACE_ID, visible), + }) + void this.configureWindow(launcher) + shell.registerWindow(launcher) + shell.wake('window') + shell.focusWindow(launcher.windowId) + onIosNotificationPopup(key => { + const notification = readActiveNotifications(128).find(n => n.key === key) + if (this.glassesLocked || !notification || !shouldShowNotificationOnGlasses(notification.packageName)) return + const woke = !shell.isScreenOn() && shell.wake('sidebar') + shell.openNotificationModal(key, woke) + this.requestShellRender() + }) + startRemoteInput({ + ready: () => this.runtimeRunning, + locked: () => this.glassesLocked, + input: (gesture, source) => { + // Share the controller queue with physical input. A discrete hold includes its release. + const task = this.inputQueue.then(async () => { + await this.receiveInput(makeInputEvent({ type: gesture, source } as InputEventPayload)) + if (gesture === 'long-press') await this.receiveInput(makeInputEvent({ type: 'long-press-release', source })) + }) + this.inputQueue = task.catch(error => this.fail(error)) + return task + }, + acceptsText: () => !!shell.foregroundWindow()?.receiveTextInput, + text: (text, submit) => { if (!shell.isScreenOn()) shell.wake('window'); shell.sendTextToForegroundWindow(text, { submit }); this.requestShellRender() }, + assistantAvailable: () => shell.isAssistantAvailable(), + assistant: text => shell.sendToAssistant(text), + }) + registerSystemTools() + registerWindowTools({ apps: ALL_APPS.filter(app => !iosAppUnavailableReason(app.appId)), + launchApp: id => this.launchApp(id), requestShellRender: () => this.requestShellRender() }) + registerNavigateTools(id => this.launchApp(id)) + registerRoamTools(id => this.launchApp(id)) + for (const app of ALL_APPS) if (app.appId !== 'launcher' && !iosAppUnavailableReason(app.appId)) app.boot?.(this.buildAppContext(app)) + } + + /** The compositor frames go to: the live session, else the headless preview. */ + private get display(): DisplayTarget | null { return this.communicator ?? this.previewTarget } + + private ensurePreviewDisplay(): void { + if (this.communicator || this.previewTarget) return + const target = new PreviewDisplayTarget() + this.previewTarget = target + target.activate(() => this.schedulePreviewUpdate()) + this.displayReady = this.registerSurfaces(target).catch(error => this.fail(error)) + } + + private teardownPreviewDisplay(): void { + const target = this.previewTarget + if (!target) return + this.previewTarget = null + this.glance.reset() + target.release() + } + + /** (Re)create every compositor surface on a fresh display target. */ + private async registerSurfaces(target: DisplayTarget): Promise { + await target.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT) + await target.configureSurface(SHELL_SURFACE_ID, { x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, + zOrder: SHELL_SURFACE_Z_ORDER, transparency: 'color-key' }) + await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1) + await target.configureSurface(LOCK_SCREEN_SURFACE_ID, { + x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: LOCK_SURFACE_Z_ORDER, transparency: 'opaque', + }) + if (this.glassesLocked) await this.submitLockImage(target) + await target.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, this.glassesLocked) + await target.setScreenBlanked(!shell.isScreenOn()) + this.lastLayout = '' + for (const window of shell.getWindows()) await this.configureWindow(window, target) + } + + private async submitLockImage(target: DisplayTarget): Promise { + const image = createLockScreenImage() + await target.submitSurfaceFrame(LOCK_SCREEN_SURFACE_ID, image.to8bppBuffer(), + { x: 0, y: 0, width: image.width, height: image.height }, 'lock-screen', 0, 0) + } + + resume(): void { + if (this.active) return + this.active = true + this.syncRuntime() + this.handlePhoneLockState(!UIApplication.sharedApplication.protectedDataAvailable) + this.logBluetooth('Phone foreground') + if (this.communicator) this.onConnectionState({ ...this.state }) + this.relayout() + shell.foregroundWindow()?.requestRender() + this.requestShellRender() + } + pause(): void { + if (!this.active) return + this.active = false + iosVoiceInput.stopPhoneCapture() + // The phone preview is hidden; the glasses still own their screen and input. + this.syncRuntime() + this.logBluetooth(`Phone background; glasses ${this.state.phase}`) + this.flushBluetoothLog() + } + private get runtimeNeeded(): boolean { + return this.active || ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.state.phase) + } + private syncRuntime(): void { + const running = this.runtimeNeeded + if (running === this.runtimeRunning) return + this.runtimeRunning = running + for (const window of shell.getWindows()) window.setScreenOn?.(running && shell.isScreenOn()) + if (!running) { + void nightscoutBridge.stop().catch(error => this.fail(error)) + this.glance.dismiss() + void this.display?.setScreenBlanked(!shell.isScreenOn()).catch(error => this.fail(error)) + this.offSettings?.(); this.offSettings = null + for (const observer of this.batteryObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer) + for (const observer of this.lockObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer) + UIDevice.currentDevice.batteryMonitoringEnabled = false + if (this.clockTimer !== null) clearInterval(this.clockTimer) + if (this.previewTimer !== null) clearTimeout(this.previewTimer) + this.clockTimer = this.previewTimer = null + return + } + UIDevice.currentDevice.batteryMonitoringEnabled = true + // Keep these observers alive while BLE owns the runtime, including background. + for (const [name, locked] of [ + [UIApplicationProtectedDataWillBecomeUnavailable, true], + [UIApplicationProtectedDataDidBecomeAvailable, false], + ] as const) { + this.lockObservers.push(NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock( + name, null, NSOperationQueue.mainQueue, () => this.handlePhoneLockState(locked))) + } + this.handlePhoneLockState(!UIApplication.sharedApplication.protectedDataAvailable) + this.syncLockSetting() + void nightscoutBridge.start().catch(error => this.fail(error)) + for (const name of [UIDeviceBatteryLevelDidChangeNotification, UIDeviceBatteryStateDidChangeNotification]) { + this.batteryObservers.push(NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(name, null, NSOperationQueue.mainQueue, () => this.requestShellRender())) + } + const phone = readPhoneBatteryState() + this.logBluetooth(`Phone battery ${phone.battery ?? "unknown"}% charging=${phone.charging}`) + this.offSettings = onAnySettingChanged(() => { + this.syncLockSetting() + this.pushBrightness() + this.relayout() + shell.foregroundWindow()?.requestRender() + this.requestShellRender() + }) + this.clockTimer = setInterval(() => this.refreshClock(), 60_000) + } + private pushBrightness(): void { + const communicator = this.communicator + if (!communicator || this.state.phase !== 'connected') { this.lastBrightness = null; return } + const preferences = getBrightnessPreferences() + const value = JSON.stringify(preferences) + if (value === this.lastBrightness) return + this.lastBrightness = value + void communicator.configureBrightness(preferences).catch(error => { + this.lastBrightness = null + this.logBluetooth(`Brightness: ${String(error)}`) + }) + } + private syncLockSetting(): void { + const enabled = lockScreenEnabledSetting.get() + if (enabled === this.lockEnabled) return + this.lockEnabled = enabled + if (!enabled) this.setGlassesLocked(false) + else { + if (this.phoneLocked && this.glassesWorn === false) this.setGlassesLocked(true) + if (this.communicator && this.state.phase === 'connected') + void this.communicator.enableWearDetectionAndRequestState().catch(error => this.fail(error)) + } + } + private handlePhoneLockState(locked: boolean): void { + this.phoneLocked = locked + this.communicator?.onPhoneLockSignal() + if (!locked) this.setGlassesLocked(false) + else if (this.lockEnabled && this.glassesWorn === false) this.setGlassesLocked(true) + } + private handleWearState(wearing: boolean): void { + // Sample false to catch a notification missed during suspension. Do not + // sample true here: WillBecomeUnavailable precedes the property transition. + if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true) + this.glassesWorn = wearing + this.logBluetooth(`Glasses wear state: ${wearing ? 'ON_HEAD' : 'OFF_HEAD'}`) + if (!wearing && this.phoneLocked && this.lockEnabled) this.setGlassesLocked(true) + } + private setGlassesLocked(locked: boolean): void { + if (locked === this.glassesLocked) return + this.glassesLocked = locked + if (locked) { + this.glance.dismiss() + iosVoiceInput.handleSessionEnded('Glasses locked. Unlock your phone to start voice input again.') + } + void (async () => { + await this.displayReady + const display = this.display + if (!display) return + { + if (locked) await this.submitLockImage(display) + await display.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, locked) + this.schedulePreviewUpdate() + } + })().catch(error => this.fail(error)) + this.logBluetooth(`Glasses ${locked ? 'locked' : 'unlocked'}`) + this.requestShellRender() + } + private refreshClock(): void { + const minute = Math.floor(Date.now() / 60_000) + if (minute !== this.clockMinute) { this.clockMinute = minute; this.requestShellRender() } + } + private async configureWindow(window: ShellWindow, target: DisplayTarget | null = this.display): Promise { + if (!target) return + await target.configureSurface(window.surfaceId, { + ...appViewportRect(window.heightMode, window.appId), zOrder: 0, transparency: 'opaque', + }) + await target.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId) + } + private relayout(): void { + const layout = shell.getWindows().map(window => JSON.stringify(appViewportRect(window.heightMode, window.appId))).join(";") + if (layout === this.lastLayout) return + this.lastLayout = layout + for (const window of shell.getWindows()) { + void this.configureWindow(window).catch(error => this.fail(error)) + window.relayout?.() + } + } + /** Submit a painted frame for an in-process window (e.g. the launcher). */ + private async submit(id: string, planes: Plane[]): Promise { + await this.displayReady + const display = this.display + if (!display) return + const fingerprint = planesFingerprint(planes) + const { image, draws } = flattenPlanesWithDraws(planes) + await display.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height }, + fingerprint, -1, 0, prepareFrameDraws(draws)) + this.schedulePreviewUpdate() + } + /** Worker windows ship baked pixels; every submit is new content to the compositor. */ + private async submitWorkerPixels(id: string, pixels: Uint8Array, width: number, height: number, draws: ArrayBuffer | null): Promise { + await this.displayReady + const display = this.display + if (!display) return + await display.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, `${id}#${++this.nextWorkerFrame}`, -1, 0, draws) + this.schedulePreviewUpdate() + } + private setWindowSurfaceVisible(id: string, visible: boolean): void { + void this.display?.setSurfaceVisible(id, visible).then(() => this.schedulePreviewUpdate()).catch(error => this.fail(error)) + } + private removeWindowSurface(id: string): void { + void this.display?.removeSurface(id).then(() => this.schedulePreviewUpdate()).catch(error => this.fail(error)) + } + /** Re-render and resubmit the shell surface; one render in flight, at most one queued. */ + private requestShellRender(): void { + if (this.shellRenderInProgress) { this.shellRenderQueued = true; return } + this.shellRenderInProgress = true + void (async () => { + try { + do { + this.shellRenderQueued = false + await this.renderShell() + } while (this.shellRenderQueued) + } catch (error) { this.fail(error) } + finally { this.shellRenderInProgress = false } + })() + } + private async renderShell(): Promise { + await this.displayReady + const display = this.display + if (!display) return + const startedAt = Date.now() + const bytes = shell.paintScene() + const frameId = this.allocateFrameId() + await display.submitShellScene(bytes, Date.now() - startedAt, frameId) + // Backpressure: the next shell render waits for this one to reach the + // glasses (the preview target resolves immediately; nothing transmits). + await display.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS) + this.schedulePreviewUpdate() + } + private allocateFrameId(): number { + const id = this.nextFrameId + this.nextFrameId = this.nextFrameId >= 0x7fffffff ? 1 : this.nextFrameId + 1 + return id + } + /** Refresh the phone mirror from the composited screen, coalesced at 30 fps. */ + private schedulePreviewUpdate(): void { + if (!this.active || this.previewTimer !== null) return + this.previewTimer = setTimeout(() => { + this.previewTimer = null + if (!this.active) return + try { + const image = this.display?.getCompositePreview(previewColorSetting.get() === 'green') + if (!image) return + this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard' + : `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`) + } catch (error) { this.fail(error) } + }, 33) + } + private fail(error: unknown): void { + const message = error instanceof Error ? error.stack ?? error.message : String(error) + console.error(`[ios-preview] ${message}`) + this.logBluetooth(`App error: ${message}`) + if (this.active) this.onError(error instanceof Error ? error.message : String(error)) + } + gesture(gesture: PhoneGesture, origin: 'watch' | 'ring' | 'mirror', nx = 0, ny = 0): void { + this.inputQueue = this.inputQueue.then(async () => { + if (!this.active) return + if (gesture === 'tap' && origin === 'mirror' && shell.isScreenOn() && !this.glassesLocked) { + await this.mirrorTap(nx, ny) + } else { + let type: string = ({ tap: 'click', 'double-tap': 'double-click' } as Record)[gesture] ?? gesture + if (origin === 'ring' && type.startsWith('swipe-')) { + if (type === 'swipe-left' || type === 'swipe-right') return + type = type === 'swipe-up' ? 'scroll-up' : 'scroll-down' + } + const event = makeInputEvent({ type, source: origin === 'ring' ? 'ring' : 'watch' } as InputEventPayload) + await this.receiveInput(event) + } + console.log(`[ios-preview] ${origin} ${gesture}: ${shell.describeInputTarget()}`) + }).catch(error => this.fail(error)) + } + private async receiveInput(event: InputEvent, headTilt = false): Promise { + if (!acceptInput(event)) return + if (this.glassesLocked) { + // Preserve the locked display's sleep/wake controls without dispatching + // gestures to apps, shell menus, voice input or the Glanceboard. + if (event.type === 'double-click' && (event.source === 'ring' || event.source === 'watch')) { + if (shell.isScreenOn()) shell.sleep() + else shell.wake('sidebar') + } else if (event.type === 'display-wake' && !shell.isScreenOn()) shell.wake('sidebar') + this.requestShellRender() + return + } + if (!shell.isScreenOn()) { + const glanceEvent = this.glance.eventForGesture(headTilt ? 'head-tilt' : event.type) + if (glanceEvent?.type === 'dismiss') this.glance.dismiss() + else if (glanceEvent) { + await this.glance.handleEvent(glanceEvent, 0) + return + } + } + await shell.receiveInput(event) + this.requestShellRender() + } + private async mirrorTap(nx: number, ny: number): Promise { + const x = Math.max(0, Math.min(639, Math.floor(nx * 640))) + const y = Math.max(0, Math.min(479, Math.floor(ny * 480))) + const window = shell.foregroundWindow() + if (!shell.hasOverlay() && sidebarStripVisible(shell.getFocus(), window?.appId) && x < SIDEBAR_WIDTH) { + const target = shell.windowAtSidebarPoint(x, y) + if (target) { shell.focusWindow(target.windowId); target.requestRender(); this.requestShellRender() } + return + } + if (window && !shell.hasOverlay()) { + const rect = appViewportRect(window.heightMode, window.appId) + if (x < rect.x || x >= rect.x + rect.width || y < rect.y || y >= rect.y + rect.height) return + shell.focusWindow(window.windowId) + if (await window.hitTest?.(x - rect.x, y - rect.y)) { this.requestShellRender(); return } + } + await shell.receiveInput(makeInputEvent({ type: 'click', source: 'watch' })) + this.requestShellRender() + } + async launchApp(id: string, params?: AppLaunchParams): Promise { + const app = ALL_APPS.find(app => app.appId === id) + if (!app) { + const installed = getInstalledEvenHubAppById(id) + const host = ALL_APPS.find(app => app.appId === 'evenhub') + if (installed && host) { + try { + if (isInstalledPackagePresent(installed.packageId)) + await launchInstalledPackage(this.buildAppContext({ ...host, appId: id }), installed) + else await openEvenHubStoreForPackage(this.buildAppContext(host), installed) + } + catch (error) { this.fail(error) } + } + return + } + try { + const reason = iosAppUnavailableReason(id) + if (reason) { + await this.launchInProcessApp(id, `window:${id}`, options => createInProcessWindow({ + ...options, appId: id, windowId: id, title: app.title, icon: app.icon, + iconLetter: app.title[0], closeable: true, + baseLayer: new YieldAtRootLayer(new TextViewerLayer(reason, app.title)), + })) + } else await app.launch(this.buildAppContext(app), params) + console.log(`[ios-preview] Launched ${id}`) + } catch (error) { this.fail(error) } + } + private async uninstallApp(id: string): Promise { + const packageId = installedEvenHubPackageId(id) + if (!packageId) return + closeRunningPackage(packageId) + uninstallEvenHubPackage(packageId) + this.requestShellRender() + } + private buildAppContext(app: AppDefinition): AppContext { + return { + appId: app.appId, apps: ALL_APPS, actions: this.actions, + launchApp: (id, params) => this.launchApp(id, params), uninstallApp: id => this.uninstallApp(id), + launchInProcessApp: (id, surface, create) => this.launchInProcessApp(id, surface, create), + ensureWorkerHost: create => this.ensureWorkerHost(app.appId, create), + submitWindowFrame: (id, planes) => this.submit(id, planes), + setWindowSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible), + requestShellRender: () => this.requestShellRender(), appendLog: message => console.log(`[ios-app] ${message}`), + setTextEditorHost: () => {}, + } + } + private async launchInProcessApp(windowId: string, surfaceId: string, + create: (options: InProcessAppOptions) => InProcessWindow): Promise { + const existing = this.inProcessApps.get(windowId) + if (existing) { shell.focusWindow(windowId); existing.requestRender(); this.requestShellRender(); return } + // Apps can request a render while their factory runs (Nightscout's tray + // subscription does). The surface only exists once the factory returns. + // The explicit render below supplies a fresh frame after configuration. + let surfaceReady = false + const app = create({ + actions: this.actions, submitFrame: planes => surfaceReady ? this.submit(surfaceId, planes) : Promise.resolve(), + setSurfaceVisible: visible => this.setWindowSurfaceVisible(surfaceId, visible), + removeSurface: () => { surfaceReady = false; this.removeWindowSurface(surfaceId) }, + reconfigureSurface: () => { + const window = shell.getWindows().find(w => w.windowId === windowId) + if (window) void this.configureWindow(window).catch(error => this.fail(error)) + this.requestShellRender() + }, + onClosed: () => { this.inProcessApps.delete(windowId) }, + }) + this.inProcessApps.set(windowId, app) + await this.configureWindow(app.window) + surfaceReady = true + shell.registerWindow(app.window); shell.focusWindow(windowId) + app.requestRender(); this.requestShellRender() + } + private ensureWorkerHost(appId: string, create: () => Worker): WorkerAppHost { + const existing = this.appHosts.get(appId) + if (existing) return existing + const worker = create() + const navigationSensors = appId === 'navigate' + ? new IosNavigationSensors(event => worker.postMessage({ type: 'navigation-sensors', event })) : undefined + const host = new WorkerAppHost({ + appId, worker, navigationSensors, + onStopping: () => { if (this.appHosts.get(appId) === host) this.appHosts.delete(appId) }, + playBuzzerSequence: payload => this.actions.playBuzzerSequence(payload), + openUrl: url => { void Utils.openUrl(url) }, + configureSurface: async (id, visible, heightMode) => { + const display = this.display + if (!display) return + await display.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' }) + await display.setSurfaceVisible(id, visible) + }, + setSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible), + removeSurface: id => this.removeWindowSurface(id), + submitPixels: (id, pixels, width, height, draws) => { + void this.submitWorkerPixels(id, pixels, width, height, draws).catch(error => this.fail(error)) + }, + requestShellRender: () => this.requestShellRender(), + openSettings: section => { void this.launchApp('settings', { section }) }, + startTextSettingEdit: id => { const setting = getStringSettingById(id); if (setting) void this.editSetting(setting) }, + endTextSettingEdit: () => {}, + startTextInput: () => shell.startVoiceInput(), + }) + this.appHosts.set(appId, host) + return host + } + cycleDisplayMode(): void { + const values = DISPLAY_MODE_VALUES + displayModeSetting.set(values[(values.indexOf(displayModeSetting.get()) + 1) % values.length]) + } + get displayModeLabel(): string { return displayModeLabel(displayModeSetting.get()) } + get connectionState(): SessionState | null { return this.sessionCreated ? this.state : null } + get connectionDetails(): string { + const state = this.connectionState + return state ? `${state.status}\nL: ${state.leftVersion || 'unknown'}\nR: ${state.rightVersion || 'unknown'}\nBattery: ${state.battery ?? '?'}%\nRing: ${state.ring ? 'connected' : 'disconnected'}\nFrames acknowledged: ${state.frames}\n${state.capabilities}\n\n${this.logLines.slice(-12).join('\n')}` : 'Preview only. Configure devices, then connect.' + } + private update(phase: SessionPhase, status: string): void { + this.state = { ...this.state, phase, status } + this.logBluetooth(status) + this.emitState() + } + private emitState(): void { + if (this.state.phase !== 'connected') resetRingInputFilter() + this.pushBrightness() + this.maybePlayWelcomeSound(this.state) + if (this.state.phase !== 'connected' && this.state.phase !== 'connecting') this.glassesWorn = null + if (this.state.phase !== 'connected') iosVoiceInput.handleSessionEnded() + shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging }) + this.syncRuntime() + if (this.active) this.onConnectionState({ ...this.state }) + this.requestShellRender() + } + async connect(): Promise { + if (!this.active || this.connecting || this.stopping) return + if (this.communicator && ['connecting', 'connected', 'retrying', 'disconnecting'].includes(this.state.phase)) return + const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses) + if (error) { this.onError(error); return } + this.connecting = true + this.sessionCreated = true + try { + this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false } + this.update('connecting', 'Finding configured devices…') + let identifiers + try { identifiers = await resolveIosPeripherals(addresses) } + catch (failure) { + this.update('error', failure instanceof Error ? failure.message : String(failure)) + return + } + if (this.communicator) await this.closeCommunicator() + const communicator = new FaceclawCommunicatorBridge(identifiers) + this.teardownPreviewDisplay() + this.communicator = communicator + this.wearDetectionRequested = false + this.bindCommunicator(communicator) + this.displayReady = this.registerSurfaces(communicator) + await this.displayReady + this.update('connecting', 'Connecting to the glasses…') + await communicator.configureBrightness(getBrightnessPreferences()) + this.startedCommunicators.add(communicator) + await communicator.start() + for (const window of shell.getWindows()) window.requestRender() + this.requestShellRender() + } catch (failure) { + this.fail(failure) + await this.closeCommunicator().catch(() => {}) + this.ensurePreviewDisplay() + this.update('error', failure instanceof Error ? failure.message : String(failure)) + } finally { + this.connecting = false + } + } + private bindCommunicator(communicator: FaceclawCommunicatorBridge): void { + const notifications = new AncsClient(packet => communicator.writeRawToRight(packet), (key, popup) => { + iosNotificationsChanged(key, popup); this.requestShellRender() + }, message => this.logBluetooth('ANCS: ' + message)) + this.notifications = notifications + bindIosNotifications(notifications) + bindCompassSession(communicator) + communicator.setRequiresAncs(true) + this.sessionOffs.push( + communicator.onStateChange(state => this.handleSessionState(communicator, state)), + communicator.onRingEvent(input => this.handleRingEvent(input)), + communicator.onBatteryState(state => { + if (this.communicator !== communicator) return + this.state = { ...this.state, battery: state.battery >= 0 && state.battery <= 100 ? state.battery : this.state.battery, + charging: state.chargingStatus >= 0 ? state.chargingStatus > 0 : this.state.charging, + ring: (state.ringBattery ?? -1) >= 0 || this.state.ring } + shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging }) + if (this.active) this.onConnectionState({ ...this.state }) + this.requestShellRender() + }), + communicator.onFirmwareInfo(info => { + if (this.communicator !== communicator) return + this.state = { ...this.state, leftVersion: info.leftVersion || this.state.leftVersion, + rightVersion: info.rightVersion || this.state.rightVersion, capabilities: info.extension || this.state.capabilities } + if (this.active) this.onConnectionState({ ...this.state }) + this.syncNotifications(communicator) + }), + communicator.onFrameMetrics(() => { + if (this.communicator !== communicator) return + this.state = { ...this.state, frames: this.state.frames + 1 } + this.onActivity() + this.maybePlayWelcomeSound(this.state) + if (this.active) this.onConnectionState({ ...this.state }) + this.schedulePreviewUpdate() + }), + communicator.onWearState(wearing => { if (this.communicator === communicator) this.handleWearState(wearing) }), + communicator.addCompassListener(receiveCompassEvent), + communicator.onAncsRelayFrame(frame => { if (this.communicator === communicator) notifications.receive(frame) }), + communicator.onAncsAuthorization(authorized => { + if (this.communicator !== communicator) return + if (authorized) { this.syncNotifications(communicator); return } + const active = notifications.state !== 'disconnected' + const command = notifications.stopCommand() + notifications.stop('Enable Share System Notifications in iPhone Settings → Bluetooth → right lens.') + if (active && this.state.phase === 'connected') void communicator.writeRawToRight(command).catch(() => {}) + }), + ) + } + private handleSessionState(communicator: FaceclawCommunicatorBridge, state: CommunicatorState): void { + if (this.communicator !== communicator) return + // Registering the listener makes the Kotlin session report its current state, which is + // "disconnected" before start(); that must not be mistaken for the session ending. + if (!this.startedCommunicators.has(communicator)) return + const phase: SessionPhase = state.phase === 'charging' ? 'connected' : state.phase === 'unpaired' ? 'error' : state.phase + if (phase === 'connected' && this.state.phase !== 'connected') { + this.state = { ...this.state, frames: this.state.frames } + if (!this.wearDetectionRequested) { + this.wearDetectionRequested = true + void communicator.enableWearDetectionAndRequestState().catch(error => this.logBluetooth(`Wear detection: ${String(error)}`)) + } + } + this.update(phase, state.status) + if (phase === 'connected') this.syncNotifications(communicator) + else if ((phase === 'disconnected' || phase === 'error') && !this.stopping) { + // The session ended on its own (retries exhausted, an arm unpaired): release it and go headless. + void this.disconnect(state.status) + } + } + private handleRingEvent(input: RawInputEvent): void { + this.inputQueue = this.inputQueue.then(async () => { + if (this.state.phase !== 'connected') return + this.onActivity() + if (input.ringInput) this.state = { ...this.state, ring: true } + await this.receiveInput(rawInputEventToInputEvent(input), + input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT) + this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`) + }).catch(error => this.fail(error)) + } + /** Any glasses traffic: CoreBluetooth can wake a suspended process, so refresh what timers would have. */ + private onActivity(): void { + if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true) + this.refreshClock() + } + /** Start iPhone notification relay once the firmware is known to support it and ANCS is authorized. */ + private syncNotifications(communicator: FaceclawCommunicatorBridge): void { + const notifications = this.notifications + if (!notifications || this.state.phase !== 'connected') return + if (!ancsCapable(this.state.capabilities)) { + if (this.state.capabilities) notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`) + return + } + if (notifications.state !== 'disconnected') return + notifications.start((Date.now() ^ Math.floor(Math.random() * 0xffffffff)) >>> 0, communicator.rightWriteLimit()) + } + async disconnect(reason?: string): Promise { + if (this.stopping) return this.stopping + const communicator = this.communicator + if (!communicator) return + // Marked before any bridge call: the session may report "disconnected" from inside disconnect(). + let finished!: () => void + this.stopping = new Promise(resolve => { finished = resolve }) + try { + const notifications = this.notifications + if (notifications && notifications.state !== 'disconnected') { + const command = notifications.stopCommand() + notifications.stop() + if (this.state.phase === 'connected') { try { await communicator.writeRawToRight(command) } catch {} } + } + if (this.state.phase === 'connected' || this.state.phase === 'connecting' || this.state.phase === 'retrying') + this.update('disconnecting', 'Disconnecting…') + await communicator.disconnect().catch(error => this.logBluetooth(`Disconnect: ${String(error)}`)) + } finally { + await this.closeCommunicator() + this.ensurePreviewDisplay() + this.update(reason ? 'error' : 'disconnected', reason ?? 'Preview only') + this.stopping = null + finished() + } + } + private async closeCommunicator(): Promise { + const communicator = this.communicator + if (!communicator) return + for (const off of this.sessionOffs.splice(0)) off() + this.notifications = null + this.communicator = null + this.glance.reset() + await communicator.close().catch(error => this.logBluetooth(`Close: ${String(error)}`)) + } + private maybePlayWelcomeSound(state: SessionState): void { + const firstNewFrame = state.frames > this.acknowledgedFrames + this.acknowledgedFrames = state.frames + // Match Android: wait for an acknowledged frame before consuming the jingle. + if (!firstNewFrame || state.phase !== 'connected' || !isWelcomeSoundPending()) return + setWelcomeSoundPending(false) + const effect = findSoundEffect('questcomplete') + if (effect) void playSoundEffect(effect, payload => this.actions.playBuzzerSequence(payload), + ms => new Promise(resolve => setTimeout(resolve, ms))) + .catch(error => this.logBluetooth(`Welcome sound failed: ${error}`)) + } + startVoiceInput(): void { if (!this.glassesLocked) shell.startVoiceInput() } + private async prepareVoiceCapture(): Promise { + if (this.glassesLocked) return false + const usePhoneMic = this.state.phase !== 'connected' + if (usePhoneMic && !this.active) return false + const ready = await iosVoiceInput.prepare(this.active, usePhoneMic) + if (!ready && this.active) this.onError(iosVoiceInput.statusText) + return ready && !this.glassesLocked && (this.state.phase === 'connected' || this.active) + } + private async startVoiceCapture(endpointing = false): Promise { + if (this.glassesLocked) return + const log = (message: string) => this.logBluetooth(message) + const communicator = this.communicator + if (communicator && this.state.phase === 'connected') { + await iosVoiceInput.startGlassesCapture(communicator, log, endpointing) + } else if (this.active) { + // Recheck microphone permission if the glasses disconnected after prepare. + if (!await iosVoiceInput.prepare(this.active, true) || !this.active || this.glassesLocked) return + await iosVoiceInput.startPhoneCapture(log, endpointing) + } + } + private logBluetooth(message: string): void { + const line = `${new Date().toISOString()} [${this.active ? 'foreground' : 'background'}${UIApplication.sharedApplication.protectedDataAvailable ? '' : ',protected-data-unavailable'}] ${message}` + console.log(`[ios-ble] ${line}`); this.logLines.push(line) + if (this.logLines.length > 1000) this.logLines.shift() + if (this.logTimer !== null) return + this.logTimer = setTimeout(() => { + this.logTimer = null + this.flushBluetoothLog() + }, 500) + } + private flushBluetoothLog(): void { + if (this.logTimer !== null) clearTimeout(this.logTimer) + this.logTimer = null + try { File.fromPath(path.join(knownFolders.documents().path, 'bluetooth.log')).writeTextSync(this.logLines.join('\n')) } + catch (error) { console.warn(`Bluetooth log: ${error}`) } + } + typeIntoApp(): void { + if (this.glassesLocked || !this.active) return + shell.startKeyboardInput() + } + private async editSetting(setting: { editorTitle: string; inputKind?: string; get(): string; set(value: string): void; validationError?(value?: string): string | null }): Promise { + if (!this.active) { this.logBluetooth('Editing text settings requires opening Faceclaw on the phone'); return false } + let draft = setting.get() + while (true) { + const result = await Dialogs.prompt({ title: setting.editorTitle, defaultText: draft, inputType: setting.inputKind, okButtonText: 'Save', cancelButtonText: 'Cancel' }) + if (!result.result) return false + draft = result.text + const error = setting.validationError?.(draft) + if (error) { + await Dialogs.alert({ title: setting.editorTitle, message: error, okButtonText: 'OK' }) + continue + } + setting.set(draft) + if (setting === nightscoutSiteUrlSetting || setting === nightscoutApiTokenSetting) await nightscoutBridge.refreshNow() + return true + } + } +} diff --git a/app/g2/lock-screen.ts b/app/g2/lock-screen.ts new file mode 100644 index 00000000..485504bd --- /dev/null +++ b/app/g2/lock-screen.ts @@ -0,0 +1,24 @@ +import { GrayImage, G2_LENS_WIDTH, G2_LENS_HEIGHT } from "../graphics/image"; +import { getDefaultMediumFont } from "../graphics/ui-fonts"; +import { wrapText } from "../graphics/textwrap"; + +export const LOCK_SCREEN_SURFACE_ID = "lock-screen"; +const LOCK_SCREEN_MESSAGE = "Glasses locked; unlock the phone to unlock the glasses."; + +export function createLockScreenImage(): GrayImage { + const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + const font = getDefaultMediumFont(); + const boxWidth = 480; + const boxHeight = 150; + const boxX = Math.round((G2_LENS_WIDTH - boxWidth) / 2); + const boxY = Math.round((G2_LENS_HEIGHT - boxHeight) / 2); + image.drawRoundedRect(boxX, boxY, boxWidth, boxHeight, 150, 12); + const lines = wrapText(font, LOCK_SCREEN_MESSAGE, boxWidth - 64); + const textHeight = lines.length * font.lineHeight; + const firstY = boxY + Math.round((boxHeight - textHeight) / 2); + lines.forEach((line, index) => { + const x = Math.round((G2_LENS_WIDTH - font.measureText(line)) / 2); + image.drawText(font, x, firstY + index * font.lineHeight, line, 230); + }); + return image; +} diff --git a/app/g2/ring-input.ts b/app/g2/ring-input.ts new file mode 100644 index 00000000..4156d5c9 --- /dev/null +++ b/app/g2/ring-input.ts @@ -0,0 +1,11 @@ +/** Original R1 report metadata. Tick is unsigned ring uptime, not phone time. + * Keep the native clock units: G2's deduplication threshold is 100 ticks. */ +export type RingInput = Readonly<{ tick: number; type: number; aux: number; speed: number }>; + +/** Faceclaw/19 EvenHub SysEvent field 100, version 1. */ +export function decodeRingMetadata(raw: Uint8Array | null | undefined): RingInput | undefined { + if (!raw || raw.length !== 12 || raw[0] !== 0x52 || raw[1] !== 0x49 || + raw[2] !== 1 || raw[3] !== 1 || raw[7] !== 0) return undefined; + return { tick: (raw[8] | raw[9] << 8 | raw[10] << 16 | raw[11] << 24) >>> 0, + type: raw[4], aux: raw[5], speed: raw[6] }; +} diff --git a/app/graphics/display-list.md b/app/graphics/display-list.md new file mode 100644 index 00000000..d0de9997 --- /dev/null +++ b/app/graphics/display-list.md @@ -0,0 +1,116 @@ +# TypeScript display lists + +`GrayImage.drawDisplayList(list, x, y, width, height, depth)` retains a list alongside +the image's ordinary pixels. Existing frame submission (`prepareFrameDraws`) and +shell scene submission carry it to Kotlin. Bounds describe its footprint for plane +occlusion; draw coordinates are relative to its placement. Keep the list, its image +resources, and their pixel buffers immutable after submission. + +```ts +import { DrawExpression as E } from './draw-expression'; +import { DrawOp, type DisplayList } from './display-list'; + +const list: DisplayList = { + resources: [], + timeline: { token: animationId, startedAt: Date.now() }, + calls: [{ + op: DrawOp.ROUNDED_RECT, + x: E.progress(300).ease().lerp(E.f32(0), E.f32(100)).toInt(), + y: 0, + width: 40, height: 20, radius: 6, + background: 4, border: 15, + }], +}; +frame.drawDisplayList(list, 20, 30, 140, 20, 0); +``` + +Supported calls: + +- `ROUNDED_RECT`: literal or expression `x`/`y`, literal dimensions/radius, and + 4-bit colors (0–15). Border 16 means no border. Fill takes the maximum of the + background color and existing pixels. +- `IMAGE`: a local resource index, integer `x`/`y`, and `transparent`. Transparent + images skip pixels quantized to zero. Resources have `width`, `height`, and + gray8 `pixels`; a baked `GrayImage` can be used directly. +- `RECT_COPY`: a local resource index, source rectangle and destination + `dx`/`dy`. Source x/y and `dx`/`dy` accept expressions (revision 29); the + source rectangle must stay inside the source at every time, or firmware + rejects the list. `SCREEN` reads the uncomposed frame; its source coordinates + are also relative to this list's placement. Each call may add its own `depth` + to the list's depth. A screen restore normally uses the negative of the list depth. +- `CLEAR` (revision 35 with a clip): fill with a 4-bit `color`. Under a clip it + fills just the clip rect, which is how to blank a region; without one it + fills the whole screen. +- `DRAWS` (bridge-only): replay glyph and icon draws offset by `x`/`y`, which + may be expressions. `records`/`count` come from glyph-wire's + `encodeReplayDraws(image.draws)`, which registers the rasters and returns null + unless every draw is a replayable glyph (atlas font, ASCII, ink inside the + line box) or plain image. Kotlin compiles them to firmware IMAGE calls on + ImageAtlas resources and TEXT runs on FontResourceAtlas fonts (one run per + line and color). `source` keeps the draws for software painting. If Kotlin + cannot resolve a record, the whole list draws nothing, so the frame's own + pixels show rather than content with a hole in it. + +Any call may carry a `clip` (revision 35): a rect in list coordinates outside +which it draws nothing. It moves with the call's depth, like the call itself. +The firmware flag also nests (a clipped play-list call clips everything inside +it, intersected with the calls' own clips), but TS lists are inlined into the +root, so each call carries its own. + +Kotlin allocates/cache-pins all image resources, resolves the local indices, applies +surface placement, and includes the calls in the root firmware display list. The +phone's existing display-list renderer consumes the same compiled calls. This uses +the current firmware protocol; no firmware update is needed for this bridge change. + +## Expressions and time + +`E.i32` and `E.f32` construct typed constants. Builders support arithmetic, min/max, +integer remainder, negation, explicit conversions, lerp, and the easing functions +in `ExprOp`. `ease()` defaults to smoothstep. Integers wrap at 32 bits; float +operations round to f32. The firmware's existing stack/error semantics apply. +Rounded-rectangle x/y and rect-copy source/destination coordinates accept +expressions. `E.progress(duration, delay)` stays 0 for the first `delay` ms of +the timeline, so one list can run motions that started at different times. + +`maximum.time()` returns `min(milliseconds since PRESENT, maximum)`. The existing +45 ms repaint timer remains active while this is below maximum. `E.elapsed()` is +a bridge binding: Kotlin replaces it with the timeline's elapsed milliseconds when +building a new root list. It is a constant within that PRESENT, not a second clock. +`E.progress(durationMs)` combines the two and clamps to 0..1 so intervening frames +and PRESENTs continue an animation instead of restarting it. + +Keep `timeline.token` and `timeline.startedAt` stable for an animation; use a new +token when restarting it. JS sends an elapsed duration, so JS and native clocks +need not share an epoch. Fingerprints include the token and expression code but +exclude the changing elapsed duration. Without a timeline, raw `time()` expressions +start at each PRESENT. Native compilation strips the bridge-only elapsed opcode. + +`menu-selection-list.ts` authors both the rounded highlight and the transparent row +image using this API. `IconGrid` (app/ui/icon-grid.ts) animates a whole grid +this way: a clipped CLEAR of the grid box, the sliding highlight, then a DRAWS +of every row visible during the scroll, offset by the scroll expression. `menu-scroll-list.ts` authors a menu scroll or bounce: a +viewport-sized copy from a strip resource whose source y animates, then the +highlight. The bounce curve (`app/ui/menu-scroll-motion.ts`) needs no branches: +the easing ops clamp their input to 0..1, so `ease(t / p)` holds at 1 after the +peak and `ease((t - p) / (1 - p))` holds at 0 before it. Navigation eligibility, duration, and easing remain in +TypeScript. There is no animated-menu tag or menu animation policy in Kotlin. + +## Bridge framing + +Tag 7 is followed by a little-endian u32 byte length and a bounded body: +placement (i16 x/y, u16 width/height, i8 depth), timeline (u32 token/elapsed), +u16 resource count, gray8 images (u16 width/height followed by pixels), u16 call +count, then calls. Calls start with the opcode (bit 7 set when a clip follows) +and an i16 relative depth, then the clip if any (i16 x/y, u16 width/height); +operands otherwise use their fixed-width fields, except rounded x/y, rect-copy +x/y/dx/dy and DRAWS x/y which use extended varints. CLEAR is a u8 color. DRAWS +(opcode 32) is followed by a u16 record count and the records, in the frame +draw-buffer formats: glyph `[0][fontId u16][encoding u32][penX s16][lineY s16][value u8]` +and image `[1][imageId u32][x s16][y s16]`. Expressions use the firmware bytecode plus bridge-only +`ELAPSED = 128`. Resource indices are local to this record, never firmware IDs. +Rect-copy source x/y are signed here to permit relative SCREEN coordinates. + +Limits: 256 images per record, 64 KiB per packed image, 4 MiB per bridge record, +4096 calls, and 900 input bytes per expression. Native validates the bound program +against the firmware's 1024-byte limit. The combined scene must also fit the +existing resource cache, root-list size, and VM stack limits. diff --git a/app/graphics/display-list.ts b/app/graphics/display-list.ts new file mode 100644 index 00000000..00109121 --- /dev/null +++ b/app/graphics/display-list.ts @@ -0,0 +1,191 @@ +import { DrawBytes, encodeValue, evaluate, integer, readValue, type DrawValue } from './draw-expression'; +import type { GrayImage } from './image'; + +/** Local resources are assigned firmware cache IDs by Kotlin. SCREEN reads the uncomposed frame. */ +export const SCREEN = 65535; +/** + * Firmware opcodes, plus DRAWS: a bridge-only call that replays glyph and icon + * draws (from glyph-wire's encodeReplayDraws) as firmware image/text calls. + */ +export const DrawOp = { RECT_COPY: 2, IMAGE: 4, ROUNDED_RECT: 8, CLEAR: 9, DRAWS: 32 } as const; +export const DISPLAY_LIST_RECORD = 7; +/** Bridge opcode bit: a clip rect follows the call's depth. */ +const CLIPPED = 128; +export type ListImage = { readonly width: number; readonly height: number; readonly pixels: Uint8Array }; +/** Revision 35: a call draws only inside its clip, in list coordinates (and moves with its depth). */ +export type ListClip = { readonly x: number; readonly y: number; readonly width: number; readonly height: number }; +export type ListCall = ( + | { op: typeof DrawOp.ROUNDED_RECT; x: DrawValue; y: DrawValue; width: number; height: number; radius: number; background: number; border: number } + | { op: typeof DrawOp.IMAGE; resource: number; x: number; y: number; transparent: boolean } + | { op: typeof DrawOp.RECT_COPY; resource: number; x: DrawValue; y: DrawValue; width: number; height: number; dx: DrawValue; dy: DrawValue } + /** Fill with a 4-bit color: the clip rect when there is one, else the whole screen. */ + | { op: typeof DrawOp.CLEAR; color: number } + /** + * Glyph and icon draws offset by (x, y). `records`/`count` are the wire form; + * `source` holds the same draws for software painting (a decoded list has none). + */ + | { op: typeof DrawOp.DRAWS; x: DrawValue; y: DrawValue; records: Uint8Array; count: number; source?: GrayImage } +) & { depth?: number; clip?: ListClip }; +export type DisplayList = { + readonly resources: readonly ListImage[]; + readonly calls: readonly ListCall[]; + /** Stable identity plus JS clock origin. Native binds ELAPSED again for each PRESENT. */ + /** Set by the decoder: the local PRESENT origin for software replay. */ + readonly presentedAt?: number; + readonly timeline?: { readonly token: number; readonly startedAt: number }; +}; +export type PlacedDisplayList = { displayList: DisplayList; x: number; y: number; width: number; height: number; depth: number }; + +class Writer { + bytes: number[] = []; + u8(n: number): void { this.bytes.push(integer(n, 0, 255)); } + i8(n: number): void { this.u8(integer(n, -128, 127) & 255); } + u16(n: number): void { integer(n, 0, 65535); this.bytes.push(n & 255, n >>> 8); } + i16(n: number): void { this.u16(integer(n, -32768, 32767) & 65535); } + u32(n: number): void { integer(n, 0, 4294967295); this.u16(n & 65535); this.u16(n >>> 16); } + raw(bytes: ArrayLike): void { for (let i = 0; i < bytes.length; i++) this.bytes.push(bytes[i]); } +} + +/** Length-delimited bridge envelope; call operands use fixed widths except extended x/y. */ +export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()): Uint8Array { + const { displayList: list } = placed, out = new Writer(); + integer(placed.width, 1, 640); integer(placed.height, 1, 480); + out.i16(placed.x); out.i16(placed.y); out.u16(placed.width); out.u16(placed.height); out.i8(placed.depth); + out.u32(list.timeline?.token ?? 0); + out.u32(list.timeline ? Math.min(2147483647, Math.max(0, Math.trunc(now - list.timeline.startedAt))) : 0); + integer(list.resources.length, 0, 256); out.u16(list.resources.length); + for (const image of list.resources) { + integer(image.width, 1, 640); integer(image.height, 1, 480); + if (image.pixels.length !== image.width * image.height || 5 + Math.ceil(image.width / 2) * image.height > 65536) throw new Error('Invalid display-list image'); + out.u16(image.width); out.u16(image.height); out.raw(image.pixels); + } + integer(list.calls.length, 0, 4096); out.u16(list.calls.length); + for (const call of list.calls) { + out.u8(call.op | (call.clip ? CLIPPED : 0)); out.i16(call.depth ?? 0); + integer(placed.depth + (call.depth ?? 0), -128, 127); + if (call.clip) { out.i16(call.clip.x); out.i16(call.clip.y); out.u16(call.clip.width); out.u16(call.clip.height); } + if (call.op === DrawOp.ROUNDED_RECT) { + out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y)); + out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480)); out.u16(call.radius); + out.u8(integer(call.background, 0, 15)); out.u8(integer(call.border, 0, 16)); + } else if (call.op === DrawOp.CLEAR) { + out.u8(integer(call.color, 0, 15)); + } else if (call.op === DrawOp.DRAWS) { + out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y)); + out.u16(call.count); out.raw(call.records); + } else { + if (call.resource !== SCREEN) integer(call.resource, 0, list.resources.length - 1); + if (call.op === DrawOp.RECT_COPY && call.resource !== SCREEN && [call.x, call.y].some(v => typeof v === 'number' && v < 0)) throw new Error('Negative image source coordinate'); + if (call.op === DrawOp.IMAGE && call.resource === SCREEN) throw new Error('Use rectCopy to read SCREEN'); + out.u16(call.resource); + if (call.op === DrawOp.IMAGE) { out.i16(call.x); out.i16(call.y); out.u8(call.transparent ? 31 : 15); } + else { + out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y)); out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480)); + out.raw(encodeValue(call.dx)); out.raw(encodeValue(call.dy)); + } + } + } + if (out.bytes.length > 4 * 1024 * 1024) throw new Error('Display list exceeds bridge limit'); + const header = new Writer(); header.u8(DISPLAY_LIST_RECORD); header.u32(out.bytes.length); header.raw(out.bytes); + return Uint8Array.from(header.bytes); +} + +/** Replay-record sizes by tag, as in glyph-wire's frame draw buffer. */ +const RECORD_BYTES: Record = { 0: 12, 1: 9 }; + +export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.now()): { placed: PlacedDisplayList; end: number } { + const envelope = new DrawBytes(bytes.subarray(offset)); + if (envelope.u8() !== DISPLAY_LIST_RECORD) throw new Error('Invalid display-list record'); + const length = envelope.u32(); integer(length, 0, 4 * 1024 * 1024); + const r = new DrawBytes(envelope.take(length)); + const x = r.i16(), y = r.i16(), width = r.u16(), height = r.u16(), depth = r.u8() << 24 >> 24; + const token = r.u32(), elapsed = r.u32(), count = r.u16(); integer(count, 0, 256); + const resources: ListImage[] = []; + for (let i = 0; i < count; i++) { const width = r.u16(), height = r.u16(); resources.push({ width, height, pixels: r.take(width * height) }); } + const calls: ListCall[] = [], callCount = r.u16(); integer(callCount, 0, 4096); + for (let i = 0; i < callCount; i++) { + const header = r.u8(), op = header & ~CLIPPED, depth = r.i16(); + const clip = header & CLIPPED ? { x: r.i16(), y: r.i16(), width: r.u16(), height: r.u16() } : undefined; + const common = clip ? { depth, clip } : { depth }; + if (op === DrawOp.ROUNDED_RECT) calls.push({ op, ...common, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() }); + else if (op === DrawOp.IMAGE) calls.push({ op, ...common, resource: r.u16(), x: r.i16(), y: r.i16(), transparent: (r.u8() & 16) !== 0 }); + else if (op === DrawOp.RECT_COPY) calls.push({ op, ...common, resource: r.u16(), x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), dx: readValue(r), dy: readValue(r) }); + else if (op === DrawOp.CLEAR) calls.push({ op, ...common, color: r.u8() }); + else if (op === DrawOp.DRAWS) { + const x = readValue(r), y = readValue(r), records = r.u16(), start = r.offset; + for (let n = 0; n < records; n++) { + const size = RECORD_BYTES[r.bytes[r.offset]!]; + if (!size) throw new Error('Unsupported display-list draw record'); + r.take(size); + } + calls.push({ op, ...common, x, y, count: records, records: r.bytes.slice(start, r.offset) }); + } else throw new Error('Unsupported display-list call'); + } + if (r.offset !== length) throw new Error('Trailing display-list bytes'); + const placed = { x, y, width, height, depth, displayList: { resources, calls, presentedAt: now, timeline: { token, startedAt: now - elapsed } } }; + // The writer is also the shared structural validator for software preview records. + encodeDisplayList(placed, now); + return { placed, end: offset + envelope.offset }; +} + +/** + * The list at `factor` brightness. Replayed icons cannot be dimmed on the + * glasses, so a list with DRAWS calls becomes empty instead: the dimmed frame + * underneath then shows its final state. + */ +export function dimDisplayList(list: DisplayList, factor: number): DisplayList { + if (list.calls.some(c => c.op === DrawOp.DRAWS)) return { ...list, calls: [] }; + const dim = (v: number) => v === 0 ? 0 : Math.max(1, Math.round(v * factor)); + return { ...list, resources: list.resources.map(r => ({ ...r, pixels: r.pixels.map(dim) })), + calls: list.calls.map(c => c.op === DrawOp.ROUNDED_RECT ? { ...c, background: Math.min(15, Math.round(c.background * factor)), border: c.border === 16 ? 16 : Math.min(15, Math.round(c.border * factor)) } + : c.op === DrawOp.CLEAR ? { ...c, color: Math.min(15, Math.round(c.color * factor)) } : c) }; +} + +/** + * Fallback compositor; native preview and glasses replay the compiled list with their existing timers. + * A DRAWS call paints from its `source`, so a decoded list skips them. + */ +export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width: number, height: number, placed: PlacedDisplayList, right = false, now = Date.now()): void { + const q = (n: number) => Math.min(15, (n + 8) >> 4) * 16; + const list = placed.displayList; + const elapsed = list.timeline ? Math.max(0, (list.presentedAt ?? now) - list.timeline.startedAt) : 0; + const presentTime = list.presentedAt === undefined ? 0 : Math.max(0, now - list.presentedAt); + const inside = (x: number, y: number, w: number, h: number, radius: number) => { + if (x < 0 || y < 0 || x >= w || y >= h) return false; + const r = Math.min(radius, Math.floor(w / 2), Math.floor(h / 2)); + const dx = Math.max(0, 2 * r - (2 * Math.min(x, w - 1 - x) + 1)), dy = Math.max(0, 2 * r - (2 * Math.min(y, h - 1 - y) + 1)); + return dx * dx + dy * dy <= 4 * r * r; + }; + for (const c of placed.displayList.calls) { + const depth = placed.depth + (c.depth ?? 0), shift = right ? -Math.floor((depth + 1) / 2) : Math.floor(depth / 2); + // Writable region in screen pixels: the clip moves with the call's depth, like its content. + const clip = c.clip ? { x: placed.x + c.clip.x + shift, y: placed.y + c.clip.y, width: c.clip.width, height: c.clip.height } + : { x: 0, y: 0, width, height }; + const writable = (x: number, y: number) => x >= 0 && y >= 0 && x < width && y < height && + x >= clip.x && y >= clip.y && x < clip.x + clip.width && y < clip.y + clip.height; + if (c.op === DrawOp.CLEAR) { + for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) if (writable(x, y)) output[y * width + x] = c.color * 16; + } else if (c.op === DrawOp.DRAWS) { + const at = (v: DrawValue) => evaluate(v, elapsed, presentTime).value; + c.source?.paintReplayDraws(output, width, height, placed.x + at(c.x) + shift, placed.y + at(c.y), clip); + } else if (c.op === DrawOp.ROUNDED_RECT) { + const x = placed.x + evaluate(c.x, elapsed, presentTime).value + shift, y = placed.y + evaluate(c.y, elapsed, presentTime).value; + for (let yy = 0; yy < c.height; yy++) for (let xx = 0; xx < c.width; xx++) { + if (!writable(x + xx, y + yy) || !inside(xx, yy, c.width, c.height, c.radius)) continue; + const p = (y + yy) * width + x + xx; + output[p] = c.border !== 16 && !inside(xx - 1, yy - 1, c.width - 2, c.height - 2, Math.max(0, c.radius - 1)) ? c.border * 16 : Math.max(output[p], c.background * 16); + } + } else { + const source = c.resource === SCREEN ? { width, height, pixels: screen } : placed.displayList.resources[c.resource]; + const w = c.op === DrawOp.IMAGE ? source.width : c.width, h = c.op === DrawOp.IMAGE ? source.height : c.height; + const at = (v: DrawValue) => evaluate(v, elapsed, presentTime).value; + const sx = c.op === DrawOp.IMAGE ? 0 : at(c.x) + (c.resource === SCREEN ? placed.x : 0), sy = c.op === DrawOp.IMAGE ? 0 : at(c.y) + (c.resource === SCREEN ? placed.y : 0); + const x = placed.x + (c.op === DrawOp.IMAGE ? c.x : at(c.dx)) + shift, y = placed.y + (c.op === DrawOp.IMAGE ? c.y : at(c.dy)); + for (let yy = 0; yy < h; yy++) for (let xx = 0; xx < w; xx++) { + if (!writable(x + xx, y + yy) || sx + xx < 0 || sy + yy < 0 || sx + xx >= source.width || sy + yy >= source.height) continue; + const v = q(source.pixels[(sy + yy) * source.width + sx + xx]); + if (c.op !== DrawOp.IMAGE || !c.transparent || v !== 0) output[(y + yy) * width + x + xx] = v; + } + } + } +} diff --git a/app/graphics/draw-expression.ts b/app/graphics/draw-expression.ts new file mode 100644 index 00000000..dab4604e --- /dev/null +++ b/app/graphics/draw-expression.ts @@ -0,0 +1,160 @@ +/** Revision 27 expression bytecode. ELAPSED is a bridge binding, never sent to firmware. */ +export const ExprOp = { + I32: 1, F32: 2, DUP: 3, DROP: 4, SWAP: 5, + IADD: 16, ISUB: 17, IMUL: 18, IDIV: 19, IMOD: 20, INEG: 21, IMIN: 22, IMAX: 23, + FADD: 32, FSUB: 33, FMUL: 34, FDIV: 35, FNEG: 36, FMIN: 37, FMAX: 38, + I2F: 48, F2I: 49, TIME: 50, LERP: 64, SMOOTHSTEP: 65, + EASE_IN_QUAD: 66, EASE_OUT_QUAD: 67, EASE_IN_OUT_QUAD: 68, + EASE_IN_CUBIC: 69, EASE_OUT_CUBIC: 70, EASE_IN_OUT_CUBIC: 71, + ELAPSED: 128, +} as const; + +export function integer(value: number, min: number, max: number): number { + if (!Number.isInteger(value) || value < min || value > max) throw new Error('Draw integer out of range'); + return value; +} +export function varint(value: number, signed = true): number[] { + integer(value, signed ? -2147483648 : 0, signed ? 2147483647 : 4294967295); + const widths = [7, 14, 21, 28, 32], prefixes = [0, 128, 192, 224, 240]; + const size = widths.findIndex(bits => bits === 32 || (signed + ? value >= -(2 ** (bits - 1)) && value < 2 ** (bits - 1) : value < 2 ** bits)) + 1; + const bytes = [size === 5 ? 240 : prefixes[size - 1] | ((value >>> (8 * (size - 1))) & ((1 << (8 - size)) - 1))]; + for (let i = 1; i < size; i++) bytes.push((value >>> (8 * (size - 1 - i))) & 255); + return bytes; +} + +export class DrawBytes { + offset = 0; + constructor(readonly bytes: Uint8Array) {} + take(size: number): Uint8Array { + integer(size, 0, this.bytes.length - this.offset); + const result = this.bytes.slice(this.offset, this.offset + size); this.offset += size; return result; + } + u8(): number { return this.take(1)[0]; } + u16(): number { return this.u8() | this.u8() << 8; } + i16(): number { return this.u16() << 16 >> 16; } + u32(): number { return (this.u16() | this.u16() << 16) >>> 0; } + variable(signed = true): number { + const first = this.u8(); + const size = first < 128 ? 1 : first < 192 ? 2 : first < 224 ? 3 : first < 240 ? 4 : first === 240 ? 5 : 0; + if (!size) throw new Error('Reserved extended-varint prefix'); + let value = size === 5 ? 0 : first & ((1 << (8 - size)) - 1); + for (let i = 1; i < size; i++) value = (value << 8) | this.u8(); + const bits = [7, 14, 21, 28, 32][size - 1]; + return signed ? bits < 32 ? value << (32 - bits) >> (32 - bits) : value : value >>> 0; + } +} + +/** Immutable, typed expression builder; operations use firmware i32/f32 semantics. */ +export class DrawExpression { + private constructor(readonly type: T, readonly code: readonly number[]) { + // Leave room for native elapsed bindings and surface translation. + if (code.length > 900) throw new Error('Draw expression too large'); + } + static i32(value: number): DrawExpression<'i32'> { return new DrawExpression('i32', [ExprOp.I32, ...varint(value)]); } + static f32(value: number): DrawExpression<'f32'> { + if (!Number.isFinite(Math.fround(value))) throw new Error('Invalid draw float'); + const bytes = new Uint8Array(4); new DataView(bytes.buffer).setFloat32(0, value, true); + return new DrawExpression('f32', [ExprOp.F32, ...bytes]); + } + /** Time already elapsed on this list's timeline when the next PRESENT happens. */ + static elapsed(): DrawExpression<'i32'> { return new DrawExpression('i32', [ExprOp.ELAPSED]); } + /** + * A 0..1 timeline fraction that survives subsequent PRESENT clock resets. + * With a delay, it stays 0 for the first delayMs of the timeline; a + * negative delay continues a motion that started before the timeline. + */ + static progress(durationMs: number, delayMs = 0): DrawExpression<'f32'> { + integer(delayMs, -2147483647, 2147483647); + if (delayMs + durationMs <= 0) return DrawExpression.f32(1); + const duration = DrawExpression.i32(integer(durationMs, 1, 2147483647)); + const end = DrawExpression.i32(integer(delayMs + durationMs, 1, 2147483647)); + const elapsed = DrawExpression.elapsed().min(end); + let total = end.sub(elapsed).max(DrawExpression.i32(0)).time().add(elapsed); + if (delayMs) total = total.sub(DrawExpression.i32(delayMs)).max(DrawExpression.i32(0)); + return total.toFloat().div(duration.toFloat()); + } + static decode(code: Uint8Array): DrawExpression<'i32'> { return new DrawExpression('i32', Array.from(code)); } + private binary(other: DrawExpression, i: number, f: number): DrawExpression { + if (other.type !== this.type) throw new Error('Draw expression type mismatch'); + return new DrawExpression(this.type, [...this.code, ...other.code, this.type === 'i32' ? i : f]); + } + add(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.IADD, ExprOp.FADD); } + sub(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.ISUB, ExprOp.FSUB); } + mul(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.IMUL, ExprOp.FMUL); } + div(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.IDIV, ExprOp.FDIV); } + min(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.IMIN, ExprOp.FMIN); } + max(b: DrawExpression): DrawExpression { return this.binary(b, ExprOp.IMAX, ExprOp.FMAX); } + mod(this: DrawExpression<'i32'>, b: DrawExpression<'i32'>): DrawExpression<'i32'> { return this.binary(b, ExprOp.IMOD, ExprOp.IMOD); } + neg(): DrawExpression { return new DrawExpression(this.type, [...this.code, this.type === 'i32' ? ExprOp.INEG : ExprOp.FNEG]); } + toFloat(this: DrawExpression<'i32'>): DrawExpression<'f32'> { return new DrawExpression('f32', [...this.code, ExprOp.I2F]); } + toInt(this: DrawExpression<'f32'>): DrawExpression<'i32'> { return new DrawExpression('i32', [...this.code, ExprOp.F2I]); } + time(this: DrawExpression<'i32'>): DrawExpression<'i32'> { return new DrawExpression('i32', [...this.code, ExprOp.TIME]); } + ease(this: DrawExpression<'f32'>, op: number = ExprOp.SMOOTHSTEP): DrawExpression<'f32'> { + integer(op, ExprOp.SMOOTHSTEP, ExprOp.EASE_IN_OUT_CUBIC); + return new DrawExpression('f32', [...this.code, op]); + } + lerp(this: DrawExpression<'f32'>, from: DrawExpression<'f32'>, to: DrawExpression<'f32'>): DrawExpression<'f32'> { + return new DrawExpression('f32', [...from.code, ...to.code, ...this.code, ExprOp.LERP]); + } +} +export type DrawValue = number | DrawExpression<'i32'>; +export function encodeValue(value: DrawValue): number[] { + return typeof value === 'number' ? varint(value) : [255, ...varint(value.code.length, false), ...value.code]; +} +export function readValue(reader: DrawBytes): DrawValue { + if (reader.bytes[reader.offset] !== 255) return reader.variable(); + reader.u8(); return DrawExpression.decode(reader.take(reader.variable(false))); +} + +/** Software preview of the same VM; invalid programs return zero and discard pending animation. */ +export function evaluate(value: DrawValue, elapsed: number, presentTime = 0): { value: number; pending: boolean } { + if (typeof value === 'number') return { value, pending: false }; + const reader = new DrawBytes(Uint8Array.from(value.code)), stack: { value: number; float: boolean }[] = []; + let pending = false; + const push = (value: number, float = false) => { + value = float ? Math.fround(value) : value | 0; + if (!Number.isFinite(value) || stack.length === 32) throw new Error('Invalid expression stack'); + stack.push({ value, float }); + }; + const pop = (float: boolean) => { const v = stack.pop(); if (!v || v.float !== float) throw new Error('Expression type mismatch'); return v.value; }; + const toInt = (v: number) => { if (v < -2147483648 || v >= 2147483648) throw new Error('Invalid integer conversion'); return Math.trunc(v); }; + try { + while (reader.offset < reader.bytes.length) { + const op = reader.u8(); + if (op === ExprOp.I32) push(reader.variable()); + else if (op === ExprOp.F32) { const b = reader.take(4); push(new DataView(b.buffer).getFloat32(0, true), true); } + else if (op === ExprOp.ELAPSED) push(Math.min(2147483647, Math.max(0, elapsed))); + else if (op === ExprOp.I2F) push(pop(false), true); + else if (op === ExprOp.F2I) push(toInt(pop(true))); + else if (op === ExprOp.TIME) { const max = pop(false); if (max < 0) throw new Error('Invalid time'); const t = Math.min(Math.max(0, presentTime), max); pending ||= t !== max; push(t); } + else if (op === ExprOp.INEG || op === ExprOp.FNEG) push(-pop(op === ExprOp.FNEG), op === ExprOp.FNEG); + else if (op >= ExprOp.IADD && op <= ExprOp.IMAX || op >= ExprOp.FADD && op <= ExprOp.FMAX) { + const float = op >= ExprOp.FADD, b = pop(float), a = pop(float); + const key = float ? op - ExprOp.FADD : op - ExprOp.IADD; + if ((key === 3 || !float && key === 4) && b === 0) throw new Error('Division by zero'); + const result = key === 0 ? a + b : key === 1 ? a - b : key === 2 ? float ? a * b : Math.imul(a, b) : key === 3 ? a / b : !float && key === 4 ? a % b : key === (float ? 5 : 6) ? Math.min(a, b) : Math.max(a, b); + push(result, float); + } else if (op === ExprOp.LERP) { const t = pop(true), b = pop(true), a = pop(true); push(a + Math.fround(Math.fround(b - a) * t), true); } + else if (op >= ExprOp.SMOOTHSTEP && op <= ExprOp.EASE_IN_OUT_CUBIC) { + const t = Math.max(0, Math.min(1, pop(true))), u = Math.fround(1 - t), f = Math.fround; + let result: number; + switch (op) { + case ExprOp.SMOOTHSTEP: result = f(t * t) * f(3 - f(2 * t)); break; + case ExprOp.EASE_IN_QUAD: result = t * t; break; + case ExprOp.EASE_OUT_QUAD: result = 1 - f(u * u); break; + case ExprOp.EASE_IN_OUT_QUAD: result = t < .5 ? f(2 * t) * t : 1 - f(f(2 * u) * u); break; + case ExprOp.EASE_IN_CUBIC: result = f(t * t) * t; break; + case ExprOp.EASE_OUT_CUBIC: result = 1 - f(f(u * u) * u); break; + default: result = t < .5 ? f(f(4 * t) * t) * t : 1 - f(f(f(4 * u) * u) * u); + } + push(result, true); + } else if (op === ExprOp.DUP) { const a = stack[stack.length - 1]; if (!a) throw new Error('Underflow'); push(a.value, a.float); } + else if (op === ExprOp.DROP) { if (!stack.pop()) throw new Error('Underflow'); } + else if (op === ExprOp.SWAP) { if (stack.length < 2) throw new Error('Underflow'); const a = stack.pop()!, b = stack.pop()!; stack.push(a, b); } + else throw new Error('Unknown expression opcode'); + } + const top = stack.pop(); if (!top) throw new Error('Empty expression'); + return { value: top.float ? toInt(top.value) : top.value, pending }; + } catch { return { value: 0, pending: false }; } +} diff --git a/app/graphics/evenhub-font.ts b/app/graphics/evenhub-font.ts index e1a8f55f..61e5025a 100644 --- a/app/graphics/evenhub-font.ts +++ b/app/graphics/evenhub-font.ts @@ -22,12 +22,10 @@ import { File, knownFolders } from "@nativescript/core"; import { getTextWidth } from "@evenrealities/pretext"; import { EVENHUB_RUNTIME_FONT_FILENAME, isEvenHubFontAsset } from "../g2/firmware-fonts"; -import { toUint8Array } from "../util/array-util"; +import { lvglMetrics, lvglGlyph } from "../native/lvgl-font"; import { GrayImage, grayToNibble, type FwTextFont, type PlacedFwText, type PlacedFwTextGlyph } from "./image"; import { TtfFont } from "./ttf-font"; -declare const com: any; - const CJK_ASSET_PATH = "fonts/source-han-sans/SourceHanSansSC-Light-20.lvgl.bin"; /** @@ -124,7 +122,7 @@ export class EvenHubFont implements FwTextFont { this.glyphs.set(Number(key), asset.glyphs[key]!); } this.cjkPath = knownFolders.currentApp().getFile(CJK_ASSET_PATH).path; - const metrics = toUint8Array(com.faceclaw.app.LvglFontFile.getMetrics(this.cjkPath)); + const metrics = lvglMetrics(this.cjkPath); if (metrics.length < 2) { throw new Error("The bundled Source Han Sans font could not be loaded."); } @@ -388,7 +386,7 @@ export class EvenHubFont implements FwTextFont { private getCjkGlyph(codePoint: number): CjkGlyph | null { const cached = this.cjkGlyphs.get(codePoint); if (cached !== undefined) return cached; - const bytes = toUint8Array(com.faceclaw.app.LvglFontFile.getGlyph(this.cjkPath, codePoint)); + const bytes = lvglGlyph(this.cjkPath, codePoint); if (bytes.length < 8) { this.cjkGlyphs.set(codePoint, null); return null; diff --git a/app/graphics/glyph-wire.ts b/app/graphics/glyph-wire.ts index 3842c499..6d05f5b6 100644 --- a/app/graphics/glyph-wire.ts +++ b/app/graphics/glyph-wire.ts @@ -1,28 +1,29 @@ +import { DrawRecordKind, encodePresentation } from "./presentation-wire"; /** - * Marshals deferred-draw identity (text glyphs and icon images) to the Java - * side for the texture-cache pipeline (CFW modes 12/13/14; see + * Marshals deferred-draw identity (text glyphs and icon images) to the Kotlin + * side for the texture-cache pipeline (CFW modes 18/19/20; see * notes/texture-cache-display-list-design.md). * * Channels, all cheap ByteBuffer crossings: - * - glyph raster registration into the process-wide Java GlyphAtlas, once + * - glyph raster registration into the process-wide Kotlin GlyphAtlas, once * per (font, glyph) per JS context (main thread and each worker register * independently; the atlas dedupes by the font's stable atlasKey); - * - image registration into the Java ImageAtlas, once per distinct image + * - image registration into the Kotlin ImageAtlas, once per distinct image * content per JS context (images are content-addressed: the key is a hash * of dimensions + pixels, so the same icon rendered anywhere dedupes and * a changed icon is simply a new image); * - a per-frame draw list accompanying each submitted surface frame, which - * the Java planner may replay as on-glasses cached draws. + * the Kotlin planner may replay as on-glasses cached draws. * - * Draws that can't participate (no atlasKey, outside the mode-14 ASCII + * Draws that can't participate (no atlasKey, outside the mode-20 ASCII * table, ink outside the line cell, oversized images) are simply left out — * their pixels are baked into the submitted frame either way, so exclusion * only means "no wire savings for this draw". */ import { Glyph } from "./bdffont"; -import { GrayImage, type DeferredDraw, type GlyphFont, type PlacedFwText, type PlacedImage } from "./image"; +import { GrayImage, type DeferredDraw, type GlyphFont, type PlacedFwText, type PlacedGlyph, type PlacedImage } from "./image"; -declare const com: any; +import { textureAtlasAvailable, textureFontId, registerTextureGlyphs, registerFirmwareGlyphs, textureImageId } from "../native/texture-atlas"; const GLYPH_RECORD_BYTES = 12; // [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] const IMAGE_RECORD_BYTES = 9; // [1][imageId u32][x s16][y s16] @@ -32,14 +33,14 @@ const FWTEXT_MEMBER_BYTES = 7; // [cp u32][dx s16][ink u8] type FontWireState = { fontId: number; cellHeight: number; - /** Encodings already registered with the Java atlas from this JS context. */ + /** Encodings already registered with the Kotlin atlas from this JS context. */ registered: Set; }; const fontStates = new Map(); /** - * Java-side image id per source image object, or null for images that can't + * Kotlin-side image id per source image object, or null for images that can't * participate. Sources handed to drawImage are immutable by contract, so * memoizing by object identity is safe; a re-rendered icon is a new object * and re-resolves (usually to the same content-addressed id). @@ -47,12 +48,12 @@ const fontStates = new Map(); const imageIds = new WeakMap(); function fontWireState(font: GlyphFont): FontWireState | null { - if (!global.isAndroid || !font.atlasKey) return null; + if (!textureAtlasAvailable() || !font.atlasKey) return null; if (font.lineHeight <= 0 || font.lineHeight > 255) return null; let state = fontStates.get(font); if (!state) { state = { - fontId: com.faceclaw.app.GlyphAtlas.fontId(font.atlasKey), + fontId: textureFontId(font.atlasKey), cellHeight: font.lineHeight, registered: new Set(), }; @@ -63,7 +64,7 @@ function fontWireState(font: GlyphFont): FontWireState | null { /** * Whether this glyph can be expressed as an on-glasses cached draw: within - * the mode-14 char table (32..127), a nonempty raster that fits the font's + * the mode-20 char table (32..127), a nonempty raster that fits the font's * line cell (the cached image is bbxWidth x lineHeight with the ink placed at * inkTop), and metrics that fit the wire fields. */ @@ -76,14 +77,15 @@ function representableGlyph(font: GlyphFont, glyph: Glyph): boolean { return inkTop >= 0 && inkTop + glyph.bbxHeight <= font.lineHeight; } -/** Firmware-font codepoints already registered with the Java FwGlyphAtlas. */ +/** Firmware-font codepoints already registered with the Kotlin FwGlyphAtlas. */ const fwRegistered = new Set(); /** * Whether a firmware-text run is expressible on the wire, registering any - * unseen ink members' rasters with the Java FwGlyphAtlas as a side effect. + * unseen ink members' rasters with the Kotlin FwGlyphAtlas as a side effect. */ function prepareFwTextRun(placed: PlacedFwText): boolean { + if (!textureAtlasAvailable()) return false; if (!inRange16(placed.x) || !inRange16(placed.y)) return false; if (placed.glyphs.length === 0 || placed.glyphs.length > 255) return false; let registration: Array<{ cp: number; data: NonNullable> }> | null = null; @@ -116,21 +118,21 @@ function prepareFwTextRun(placed: PlacedFwText): boolean { bytes.set(entry.data.nibbles, offset + 10); offset += 10 + entry.data.nibbles.length; } - com.faceclaw.app.FwGlyphAtlas.register(buffer); + registerFirmwareGlyphs(buffer); } return true; } -/** Resolve (registering on first sight) the Java image id for a drawImage source. */ +/** Resolve (registering on first sight) the Kotlin image id for a drawImage source. */ function imageId(placed: PlacedImage): number | null { - if (!global.isAndroid) return null; + if (!textureAtlasAvailable()) return null; const source = placed.source; const memo = imageIds.get(source); if (memo !== undefined) return memo; let id: number | null = null; if (source.width > 0 && source.width <= 255 && source.height > 0 && source.height <= 255) { const key = `img:${source.width}x${source.height}:${source.contentHash32().toString(16)}`; - id = com.faceclaw.app.ImageAtlas.ensure(key, source.width, source.height, source.pixels.buffer); + id = textureImageId(key, source.width, source.height, source.pixels); if (!(typeof id === "number") || id <= 0) id = null; } imageIds.set(source, id); @@ -143,20 +145,21 @@ function inRange16(v: number): boolean { /** * Register any not-yet-registered glyph rasters and image contents among - * `draws` with the Java atlases, then build the per-frame draw buffer to pass + * `draws` with the Kotlin atlases, then build the per-frame draw buffer to pass * alongside the frame's pixels: little-endian tagged records * [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] (glyph) * [1][imageId u32][x s16][y s16] (image) * [2][x s16][y s16][value u8][count u8] (fw text run) * then count x [cp u32][dx s16][ink u8] - * in draw order. Returns null when nothing is expressible (or off-Android). + * in draw order. Returns null when nothing is expressible (or without a native atlas). */ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | null { - if (!global.isAndroid || draws.length === 0) return null; + if (draws.length === 0) return null; // Pass 1: resolve ids, register unseen rasters, size the buffer. let registration: Map | null = null; const fwRunOk = new Map(); + const presentations = new Map(); let bytes = 0; for (const placed of draws) { if (placed.kind === "glyph") { @@ -164,30 +167,18 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | if (!state || !representableGlyph(placed.font, placed.glyph)) continue; if (!inRange16(placed.x) || !inRange16(placed.y)) continue; bytes += GLYPH_RECORD_BYTES; - if (state.registered.has(placed.glyph.encoding)) continue; - state.registered.add(placed.glyph.encoding); - registration ??= new Map(); - let group = registration.get(state); - if (!group) { - group = { font: placed.font, glyphs: [], aaGlyphs: [] }; - registration.set(state, group); - } - (placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph); + registration = queueRegistration(registration, state, placed); } else if (placed.kind === "image") { if (!inRange16(placed.x) || !inRange16(placed.y)) continue; - if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES; + if (placed.presentation) { const record = encodePresentation(placed); presentations.set(placed, record); bytes += record.length; } + else if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES; } else { const ok = prepareFwTextRun(placed); fwRunOk.set(placed, ok); if (ok) bytes += FWTEXT_HEADER_BYTES + placed.glyphs.length * FWTEXT_MEMBER_BYTES; } } - if (registration) { - const plainBuffer = buildRegistrationBuffer(registration); - if (plainBuffer) com.faceclaw.app.GlyphAtlas.register(plainBuffer); - const aaBuffer = buildAaRegistrationBuffer(registration); - if (aaBuffer) com.faceclaw.app.GlyphAtlas.registerAa(aaBuffer); - } + registerQueued(registration); if (bytes === 0) return null; // Pass 2: the frame's draw records. @@ -198,25 +189,16 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | if (!inRange16(placed.x) || !inRange16(placed.y)) continue; const state = fontWireState(placed.font); if (!state || !representableGlyph(placed.font, placed.glyph)) continue; - out.setUint8(offset, 0); - out.setUint16(offset + 1, state.fontId, true); - out.setUint32(offset + 3, placed.glyph.encoding, true); - out.setInt16(offset + 7, placed.x, true); - out.setInt16(offset + 9, placed.y, true); - out.setUint8(offset + 11, placed.value); - offset += GLYPH_RECORD_BYTES; + offset = writeGlyphRecord(out, offset, state, placed); } else if (placed.kind === "image") { if (!inRange16(placed.x) || !inRange16(placed.y)) continue; + if (placed.presentation) { const bytes = presentations.get(placed)!; new Uint8Array(out.buffer).set(bytes, offset); offset += bytes.length; continue; } const id = imageId(placed); if (id === null) continue; - out.setUint8(offset, 1); - out.setUint32(offset + 1, id, true); - out.setInt16(offset + 5, placed.x, true); - out.setInt16(offset + 7, placed.y, true); - offset += IMAGE_RECORD_BYTES; + offset = writeImageRecord(out, offset, id, placed); } else { if (!fwRunOk.get(placed)) continue; - out.setUint8(offset, 2); + out.setUint8(offset, DrawRecordKind.FIRMWARE_TEXT); out.setInt16(offset + 1, placed.x, true); out.setInt16(offset + 3, placed.y, true); out.setUint8(offset + 5, placed.value); @@ -233,6 +215,88 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | return out.buffer; } +/** + * Encode draws for replay inside a display list (DrawOp.DRAWS), registering + * unseen glyph rasters and images as prepareFrameDraws does, in its glyph and + * image record formats. Null unless every draw is a replayable glyph or plain + * image: a list must reproduce all of its content or none of it. + */ +export function encodeReplayDraws(draws: readonly DeferredDraw[]): { records: Uint8Array; count: number } | null { + if (!textureAtlasAvailable()) return null; + let bytes = 0; + for (const placed of draws) { + if (!inRange16(placed.x) || !inRange16(placed.y)) return null; + if (placed.kind === "glyph") { + if (!fontWireState(placed.font) || !representableGlyph(placed.font, placed.glyph)) return null; + bytes += GLYPH_RECORD_BYTES; + } else if (placed.kind === "image" && !placed.presentation && imageId(placed) !== null) { + bytes += IMAGE_RECORD_BYTES; + } else { + return null; + } + } + let registration: Map | null = null; + const out = new DataView(new ArrayBuffer(bytes)); + let offset = 0; + for (const placed of draws) { + if (placed.kind === "glyph") { + const state = fontWireState(placed.font)!; + registration = queueRegistration(registration, state, placed); + offset = writeGlyphRecord(out, offset, state, placed); + } else if (placed.kind === "image") { + offset = writeImageRecord(out, offset, imageId(placed)!, placed); + } + } + registerQueued(registration); + return { records: new Uint8Array(out.buffer), count: draws.length }; +} + +/** [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] */ +function writeGlyphRecord(out: DataView, offset: number, state: FontWireState, placed: PlacedGlyph): number { + out.setUint8(offset, DrawRecordKind.GLYPH); + out.setUint16(offset + 1, state.fontId, true); + out.setUint32(offset + 3, placed.glyph.encoding, true); + out.setInt16(offset + 7, placed.x, true); + out.setInt16(offset + 9, placed.y, true); + out.setUint8(offset + 11, placed.value); + return offset + GLYPH_RECORD_BYTES; +} + +/** [1][imageId u32][x s16][y s16] */ +function writeImageRecord(out: DataView, offset: number, id: number, placed: PlacedImage): number { + out.setUint8(offset, DrawRecordKind.TEXTURE_IMAGE); + out.setUint32(offset + 1, id, true); + out.setInt16(offset + 5, placed.x, true); + out.setInt16(offset + 7, placed.y, true); + return offset + IMAGE_RECORD_BYTES; +} + +/** Queue a glyph raster for registerQueued unless this JS context already registered it. */ +function queueRegistration( + registration: Map | null, + state: FontWireState, + placed: PlacedGlyph, +): Map | null { + if (state.registered.has(placed.glyph.encoding)) return registration; + state.registered.add(placed.glyph.encoding); + registration ??= new Map(); + let group = registration.get(state); + if (!group) { + group = { font: placed.font, glyphs: [], aaGlyphs: [] }; + registration.set(state, group); + } + (placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph); + return registration; +} + +function registerQueued(registration: Map | null): void { + if (!registration) return; + const plainBuffer = buildRegistrationBuffer(registration); + if (plainBuffer) registerTextureGlyphs(plainBuffer, false); + const aaBuffer = buildAaRegistrationBuffer(registration); + if (aaBuffer) registerTextureGlyphs(aaBuffer, true); +} + type RegistrationGroup = { font: GlyphFont; /** 1bpp (BDF) glyphs for GlyphAtlas.register. */ @@ -247,7 +311,7 @@ type RegistrationGroup = { * then per glyph * [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8] * [inkHeight x row u32] (bit (ceil(width/8)*8 - 1 - col) = ink) - * mirroring GlyphAtlas.register on the Java side. Null when the groups hold + * mirroring GlyphAtlas.register on the Kotlin side. Null when the groups hold * no 1bpp glyphs. */ function buildRegistrationBuffer( diff --git a/app/graphics/icons.ts b/app/graphics/icons.ts index dadd1488..49d8afbe 100644 --- a/app/graphics/icons.ts +++ b/app/graphics/icons.ts @@ -1,12 +1,10 @@ import { GrayImage } from "./image"; - -declare const com: any; -declare const global: any; +import { rasterizeSvg } from "../native/svg-rasterizer"; /** * Vector icons for window indicators (and anywhere else). SVGs are rendered - * once to a correctly sized grayscale bitmap by the Java IconRenderer (a - * small SVG subset: path/circle/rect/line/polyline) and cached here. To add + * once to a correctly sized grayscale bitmap by the platform rasterizer + * (Android IconRenderer or iOS SVGKit) and cached here. To add * an icon, drop its SVG source into ICON_SVGS — Lucide icons * (https://lucide.dev, stroked, 24px viewBox) work as-is; simple single-color * Noun Project glyphs also work. @@ -190,18 +188,10 @@ function renderSvgCached(cacheName: string, svg: string, size: number): GrayImag if (cached !== undefined) return cached; let icon: GrayImage | null = null; - if (global.isAndroid) { - try { - const bytes = com.faceclaw.app.IconRenderer.renderSvgGray(svg, Math.round(size), ICON_STROKE_WIDTH); - if (bytes && bytes.length >= size * size) { - icon = new GrayImage(size, size, 0); - for (let i = 0; i < size * size; i++) { - icon.pixels[i] = bytes[i] & 0xff; - } - } - } catch (error) { - console.warn(`renderIcon(${cacheName}) failed: ${error}`); - } + try { + icon = rasterizeSvg(svg, Math.round(size), ICON_STROKE_WIDTH); + } catch (error) { + console.warn(`renderIcon(${cacheName}) failed: ${error}`); } cache.set(key, icon); return icon; diff --git a/app/graphics/image.ts b/app/graphics/image.ts index 70028f61..c841071b 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -1,3 +1,6 @@ +import { menuSelectionList } from "./menu-selection-list"; +import { encodeDisplayList, dimDisplayList, paintDisplayList, type DisplayList } from "./display-list"; +import type { MenuHighlightAnimation } from "../ui/menu-highlight-motion"; import { Glyph } from "./bdffont"; import { wrapText } from "./textwrap"; @@ -73,6 +76,7 @@ export type PlacedGlyph = { * from the moment it is placed. */ export type PlacedImage = { + presentation?: { displayList?: DisplayList; mode?: "image" | "masked-image"; radius: number; background: number; border: number; depth: number; occlusions?: readonly { x: number; y: number; width: number; height: number }[] }; kind: "image"; source: GrayImage; x: number; @@ -148,7 +152,9 @@ export class GrayImage { this.width = width; this.height = height; this.pixels = new Uint8Array(width * height); - this.clear(fill); + if (fill !== 0) { + this.clear(fill); + } } get draws(): readonly DeferredDraw[] { @@ -391,6 +397,9 @@ export class GrayImage { hash = mixInt(hash, placed.source.width); hash = mixInt(hash, placed.source.height); hash = mixInt(hash, placed.source.sourceContentHash32()); + if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0]) hash = mixInt(hash, value); + if (p.displayList) for (const byte of encodeDisplayList({ displayList: p.displayList, x: 0, y: 0, width: placed.source.width, height: placed.source.height, depth: p.depth }, p.displayList.timeline?.startedAt ?? 0)) hash = mixInt(hash, byte); + } } else { hash = mixInt(hash, 0xf17e); hash = mixInt(hash, placed.font.atlasTag); @@ -501,6 +510,59 @@ export class GrayImage { this.drawList.push({ kind: "fwtext", font, x, y, value: clampByte(value), glyphs }); } + /** Retain the selected row separately from the menu surface for glasses-side composition. */ + drawMenuSelection(source: GrayImage, x: number, y: number, background: number, border: number, radius = 8, depth = 2, animation?: MenuHighlightAnimation): void { + const baked = source.withDrawsBaked(); + this.drawList.push({ kind: "image", source: baked, x, y, + presentation: { radius, background, border, depth, displayList: menuSelectionList(baked, background, border, radius, animation) } }); + } + + /** + * Paint this image's glyph and image draws onto a gray8 buffer at (dx, dy), + * inside `clip`, the way the glasses replay them from a display list (see + * DrawOp.DRAWS): glyphs firmware-exact, images skipping pixels whose 4-bit + * level is 0. Raster pixels and other draw kinds are not painted. + */ + paintReplayDraws(pixels: Uint8Array, width: number, height: number, dx: number, dy: number, + clip: { x: number; y: number; width: number; height: number }): void { + const left = Math.max(0, clip.x), top = Math.max(0, clip.y); + const right = Math.min(width, clip.x + clip.width), bottom = Math.min(height, clip.y + clip.height); + const sink: PixelSink = { + setPixel: (x, y, value) => { + if (x >= left && y >= top && x < right && y < bottom) pixels[y * width + x] = clampByte(value); + }, + }; + for (const placed of this.drawList) { + if (placed.kind === "glyph") { + rasterizeGlyph(sink, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value); + } else if (placed.kind === "image" && !placed.presentation) { + const source = placed.source; + for (let y = 0; y < source.height; y++) for (let x = 0; x < source.width; x++) { + const level = grayToNibble(source.pixels[y * source.width + x]!); + if (level) sink.setPixel(placed.x + dx + x, placed.y + dy + y, level * 16); + } + } + } + } + + /** Submit a retained list alongside this frame; coordinates are relative to x/y. */ + drawDisplayList(displayList: DisplayList, x: number, y: number, width: number, height: number, depth = 0): void { + this.drawList.push({ kind: "image", source: new GrayImage(width, height), x, y, + presentation: { radius: 0, background: 0, border: 0, depth, displayList } }); + } + + /** Replay an image at stereo depth. Masked images preserve intentional gray8 black (1). */ + drawDepthImage(source: GrayImage, x: number, y: number, depth: number, masked = false): void { + this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y, + presentation: { mode: masked ? "masked-image" : "image", radius: 0, background: 0, border: 0, depth } }); + } + + copyPresentationsInto(target: GrayImage, dx: number, dy: number): void { + for (const placed of this.drawList) if (placed.kind === "image" && placed.presentation) { + target.drawList.push({ ...placed, x: placed.x + dx, y: placed.y + dy }); + } + } + /** * Bake this image's deferred draws into its own pixels and clear the list. * Used where later raster must be able to cover earlier draws within the @@ -513,7 +575,8 @@ export class GrayImage { if (placed.kind === "glyph") { rasterizeGlyph(this, placed.font, placed.glyph, placed.x, placed.y, placed.value); } else if (placed.kind === "image") { - this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true }); + if (placed.presentation) { const one = new GrayImage(this.width, this.height); one.drawList.push(placed); one.bakeDrawsInto(this, 0, 0); } + else this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true }); } else { placed.font.bakeFwTextRun(this, placed, 0, 0); } @@ -534,7 +597,7 @@ export class GrayImage { const copy = new GrayImage(this.width, this.height, 0); copy.pixels.set(this.pixels); for (const placed of this.drawList) { - if (placed.kind === "image") { + if (placed.kind === "image" && !placed.presentation) { copy.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true }); } } @@ -544,7 +607,10 @@ export class GrayImage { if (value !== 0) pixels[i] = dimValue(value, scale); } for (const placed of this.drawList) { - if (placed.kind !== "image") { + if (placed.kind === "image" && placed.presentation) { + const p = placed.presentation; + copy.drawList.push({ ...placed, source: placed.source.dimmed(factor), presentation: { ...p, background: dimValue(p.background, scale), border: dimValue(p.border, scale), displayList: p.displayList && dimDisplayList(p.displayList, factor) } }); + } else if (placed.kind !== "image") { copy.drawList.push({ ...placed, value: dimValue(placed.value, scale) }); } } @@ -556,11 +622,11 @@ export class GrayImage { * Returns this image unchanged when there are none, otherwise a baked copy * with an empty draw list. */ - withDrawsBaked(): GrayImage { + withDrawsBaked(presentations = true): GrayImage { if (!this.drawList.length) return this; const baked = new GrayImage(this.width, this.height, 0); baked.pixels.set(this.pixels); - this.bakeDrawsInto(baked, 0, 0); + this.bakeDrawsInto(baked, 0, 0, presentations); return baked; } @@ -579,11 +645,33 @@ export class GrayImage { } /** Rasterize this image's deferred draws into target, translated by (dx, dy). */ - bakeDrawsInto(target: GrayImage, dx: number, dy: number): void { + bakeDrawsInto(target: GrayImage, dx: number, dy: number, presentations = true): void { for (const placed of this.drawList) { if (placed.kind === "glyph") { rasterizeGlyph(target, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value); } else if (placed.kind === "image") { + if (placed.presentation) { + if (!presentations) continue; + if (placed.presentation.displayList) { + paintDisplayList(target.pixels, target.pixels.slice(), target.width, target.height, + { displayList: placed.presentation.displayList, x: placed.x + dx, y: placed.y + dy, width: placed.source.width, height: placed.source.height, depth: placed.presentation.depth }); + continue; + } + if (placed.presentation.mode) { + target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true }); + continue; + } + const { radius, background, border } = placed.presentation; + const fill = new GrayImage(placed.source.width, placed.source.height); + fill.fillRoundedRect(0, 0, fill.width, fill.height, background, radius); + for (let y = 0; y < fill.height; y++) for (let x = 0; x < fill.width; x++) { + const tx = placed.x + dx + x, ty = placed.y + dy + y; + if (tx >= 0 && ty >= 0 && tx < target.width && ty < target.height) { + const i = ty * target.width + tx; target.pixels[i] = Math.max(target.pixels[i], fill.pixels[y * fill.width + x]); + } + } + target.drawRoundedRect(placed.x + dx, placed.y + dy, fill.width, fill.height, border, radius); + } target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true }); } else { placed.font.bakeFwTextRun(target, placed, dx, dy); @@ -601,9 +689,12 @@ export function grayToNibble(value: number): number { return value <= 0 ? 0 : Math.min(15, (value + 8) >> 4); } +/** Where rasterizeGlyph writes: an image, or a clipped buffer (paintReplayDraws). */ +type PixelSink = { setPixel(x: number, y: number, value: number): void }; + /** Bake one glyph into an image's pixel buffer (y is the top of the line). */ function rasterizeGlyph( - target: GrayImage, + target: PixelSink, font: GlyphFont, glyph: Glyph, x: number, diff --git a/app/graphics/installed-fonts.ts b/app/graphics/installed-fonts.ts index 14d7259a..8eec0c04 100644 --- a/app/graphics/installed-fonts.ts +++ b/app/graphics/installed-fonts.ts @@ -4,14 +4,14 @@ * app assets on first listing (so the directory is the single source of * truth), and user installs from the Files app ("Install" on a font file). * - * Listing parses each file's name table for family/style (via the Java - * FontFileRenderer) and probes monospace-ness by comparing advances, both + * Listing parses each file's name table for family/style (via the native + * font renderer) and probes monospace-ness by comparing advances, both * memoized per path for the session. */ import { File, Folder, knownFolders, path as nsPath } from "@nativescript/core"; import { canLoadFontFile, fontFileDisplayName, isFontFile } from "../native/font-files"; -declare const com: any; +import { fontRenderer } from "../native/font-renderer"; declare const java: any; declare const global: any; @@ -62,7 +62,7 @@ let preinstalledEnsured = false; * before every listing. */ export function ensurePreinstalledFonts(): void { - if (preinstalledEnsured || !global.isAndroid) return; + if (preinstalledEnsured || (!global.isAndroid && !global.isIOS)) return; const assetsDir = nsPath.join(knownFolders.currentApp().path, "fonts/ttf"); for (const fileName of PREINSTALLED_FONT_FILES) { const target = installedFontPath(fileName); @@ -83,7 +83,7 @@ export function ensurePreinstalledFonts(): void { * same-named file). Returns an error message for the UI, or null on success. */ export function installFontFile(sourcePath: string): string | null { - if (!global.isAndroid) return "Not available on this platform."; + if ((!global.isAndroid && !global.isIOS)) return "Not available on this platform."; if (!isFontFile(sourcePath)) return "Not a font file."; if (!canLoadFontFile(sourcePath)) return "This file could not be loaded as a font."; const fileName = sourcePath.slice(sourcePath.lastIndexOf("/") + 1); @@ -108,7 +108,7 @@ export function isFontInstalled(sourcePath: string): boolean { * then weight. Unloadable files are skipped. */ export function listInstalledFonts(): InstalledFont[] { - if (!global.isAndroid) return []; + if ((!global.isAndroid && !global.isIOS)) return []; ensurePreinstalledFonts(); const fonts: InstalledFont[] = []; try { @@ -195,7 +195,7 @@ function styleWeightOrder(style: string): number { function isMonospaceFont(path: string): boolean { try { const measure = (text: string) => - Number(com.faceclaw.app.FontFileRenderer.measureTextExact(path, text, 48)); + Number(fontRenderer.measureTextExact(path, text, 48)); const narrow = measure("i"); if (!(narrow > 0)) return false; return ( @@ -217,6 +217,14 @@ function isMonospaceFont(path: string): boolean { // isolates on first launch (each resolving the default UI font), and readers // must never see a half-written font file. function copyFileBytes(source: string, target: string): void { + if (source === target) return; + if (global.isIOS) { + const data = NSData.dataWithContentsOfFile(source); + // Foundation's atomic write uses a sibling temporary file and rename. + // Multiple worker isolates can safely preinstall the same bundled face. + if (!data || !data.writeToFileAtomically(target, true)) throw new Error(`Copy to ${target} failed`); + return; + } const temp = `${target}.tmp-${Math.floor(Math.random() * 0x7fffffff).toString(36)}`; const input = new java.io.FileInputStream(source); try { diff --git a/app/graphics/lvgl-font.ts b/app/graphics/lvgl-font.ts new file mode 100644 index 00000000..60dd57e2 --- /dev/null +++ b/app/graphics/lvgl-font.ts @@ -0,0 +1,53 @@ +/** Reader for the bundled Source Han Sans serialized LVGL font. Matches + * Android LvglFontFile's metrics/glyph byte format, without a Java dependency. */ +export class LvglFont { + readonly metrics: Uint8Array + private readonly view: DataView + private readonly ids = new Map() + private readonly bitmaps: number + private readonly descriptors: number + constructor(private readonly bytes: Uint8Array) { + this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength) + if (bytes.length < 64 || bytes.slice(0, 4).some(v => v !== 90)) throw new Error('Missing LVGL font header') + const height = this.u16(0x38), baseline = height - this.u16(0x3a) + const descriptor = this.pointer(this.u32(0x34)) + this.bitmaps = this.pointer(this.u32(descriptor)); this.descriptors = this.pointer(this.u32(descriptor + 4)) + const cmaps = this.pointer(this.u32(descriptor + 8)), packed = this.u16(descriptor + 18), count = packed & 0x1ff + if (!height || baseline < 0 || !count || ((packed >>> 9) & 15) !== 4 || (packed >>> 14) !== 3) throw new Error('Unsupported LVGL font') + this.metrics = new Uint8Array([height, baseline]) + for (let i = 0; i < count; i++) { + const at = cmaps + i * 20, start = this.u32(at), length = this.u16(at + 4), first = this.u16(at + 6) + const unicodePtr = this.u32(at + 8), offsetsPtr = this.u32(at + 12), listLength = this.u16(at + 16), type = this.u8(at + 18) + if (type === 2 || type === 0) { + const offsets = type === 0 ? this.pointer(offsetsPtr) : 0 + for (let n = 0; n < length; n++) { + const offset = type === 0 ? this.u8(offsets + n) : n + if (n && !offset) continue + this.ids.set(start + n, first + offset) + } + } else if (type === 1 || type === 3) { + const unicode = this.pointer(unicodePtr), offsets = offsetsPtr ? this.pointer(offsetsPtr) : -1 + for (let n = 0; n < listLength; n++) { + const offset = offsets < 0 ? n : type === 1 ? this.u16(offsets + n * 2) : this.u8(offsets + n) + if (n && !offset) continue + this.ids.set(start + this.u16(unicode + n * 2), first + offset) + } + } else throw new Error('Unsupported LVGL cmap') + } + } + glyph(codePoint: number): Uint8Array { + const id = this.ids.get(codePoint) + if (!id) return new Uint8Array() + const at = this.descriptors + id * 16, bitmap = this.bitmaps + this.u32(at) + const width = this.u16(at + 8), height = this.u16(at + 10), length = width && height ? (Math.floor(width / 2) + 1) * height : 0 + this.check(bitmap, length) + const result = new Uint8Array(8 + length) + result.set(this.bytes.subarray(at + 8, at + 16)); result.set(this.bytes.subarray(bitmap, bitmap + length), 8) + return result + } + private check(at: number, size: number): void { if (at < 0 || at + size > this.bytes.length) throw new Error('Truncated LVGL font') } + private pointer(address: number): number { const at = address - 0x80100000; this.check(at, 1); return at } + private u8(at: number): number { this.check(at, 1); return this.bytes[at] } + private u16(at: number): number { return this.view.getUint16(at, true) } + private u32(at: number): number { return this.view.getUint32(at, true) } +} diff --git a/app/graphics/menu-scroll-list.ts b/app/graphics/menu-scroll-list.ts new file mode 100644 index 00000000..52f45790 --- /dev/null +++ b/app/graphics/menu-scroll-list.ts @@ -0,0 +1,50 @@ +import type { MenuHighlightAnimation } from '../ui/menu-highlight-motion'; +import { DrawExpression as E, type DrawValue } from './draw-expression'; +import { DrawOp, type DisplayList, type ListCall, type ListImage } from './display-list'; + +export type MenuScrollHighlight = { + /** Row top within the viewport; clamped inside it. */ + y: DrawValue; + width: number; + height: number; + radius: number; + /** Gray8, like drawMenuSelection. */ + background: number; + border: number; +}; + +/** + * A highlight sliding by (0, dy) to rest at `y`, on a list timeline that + * started at `timelineStart` (the slide may have started before or after it). + */ +export function slidingHighlightY(y: number, motion: MenuHighlightAnimation | undefined, timelineStart: number): E<'i32'> { + return slidingCoordinate(y, motion?.dy ?? 0, motion, timelineStart); +} + +/** One coordinate of a highlight sliding by `delta` to rest at `value`; see slidingHighlightY. */ +export function slidingCoordinate(value: number, delta: number, motion: MenuHighlightAnimation | undefined, timelineStart: number): E<'i32'> { + if (!motion || !delta) return E.i32(value); + return E.progress(motion.durationMs, motion.startedAt - timelineStart).ease() + .lerp(E.i32(value + delta).toFloat(), E.i32(value).toFloat()).toInt(); +} + +/** + * A menu viewport moving over a pre-rendered strip. `strip` holds every row + * visible at any point of the motion, and sits `stripX` pixels into the + * viewport; `sourceY` is the strip row at the viewport's top, animated on + * `timeline`. The highlight is max-blended over the copied rows. + */ +export function menuScrollList(strip: ListImage, stripX: number, viewportHeight: number, + timeline: { token: number; startedAt: number }, sourceY: DrawValue, highlight?: MenuScrollHighlight): DisplayList { + const calls: ListCall[] = [ + { op: DrawOp.RECT_COPY, resource: 0, x: 0, y: sourceY, width: strip.width, height: viewportHeight, dx: stripX, dy: 0 }, + ]; + if (highlight) { + const q = (n: number) => Math.min(15, (n + 8) >> 4); + const y = typeof highlight.y === 'number' ? highlight.y + : highlight.y.max(E.i32(0)).min(E.i32(viewportHeight - highlight.height)); + calls.push({ op: DrawOp.ROUNDED_RECT, x: 0, y, width: highlight.width, height: highlight.height, radius: highlight.radius, + background: q(highlight.background), border: q(highlight.border) }); + } + return { resources: [strip], timeline: { token: timeline.token, startedAt: timeline.startedAt }, calls }; +} diff --git a/app/graphics/menu-selection-list.ts b/app/graphics/menu-selection-list.ts new file mode 100644 index 00000000..6a334039 --- /dev/null +++ b/app/graphics/menu-selection-list.ts @@ -0,0 +1,18 @@ +import type { MenuHighlightAnimation } from '../ui/menu-highlight-motion'; +import { DrawExpression as E } from './draw-expression'; +import { DrawOp, type DisplayList, type ListImage } from './display-list'; + +/** Menu policy and easing live in TypeScript; the bridge carries only generic calls/expressions. */ +export function menuSelectionList(image: ListImage, background: number, border: number, radius: number, animation?: MenuHighlightAnimation): DisplayList { + const coordinate = (delta: number) => { + if (!animation || !delta) return 0; + const progress = E.progress(animation.durationMs).ease(); + return progress.lerp(E.i32(delta).toFloat(), E.f32(0)).toInt(); + }; + const q = (n: number) => Math.min(15, (n + 8) >> 4); + return { resources: [image], timeline: animation, + calls: [ + { op: DrawOp.ROUNDED_RECT, x: coordinate(animation?.dx ?? 0), y: coordinate(animation?.dy ?? 0), width: image.width, height: image.height, radius, background: q(background), border: q(border) }, + { op: DrawOp.IMAGE, resource: 0, x: 0, y: 0, transparent: true }, + ] }; +} diff --git a/app/graphics/plane.ts b/app/graphics/plane.ts index 15cf5c98..f644fee8 100644 --- a/app/graphics/plane.ts +++ b/app/graphics/plane.ts @@ -1,4 +1,4 @@ -import { GrayImage, type DeferredDraw } from "./image"; +import { GrayImage, type DeferredDraw, type PlacedImage } from "./image"; /** * The unit apps (and the shell) submit to the compositor: instead of one @@ -8,18 +8,17 @@ import { GrayImage, type DeferredDraw } from "./image"; * transparent, 1 is black (the two quantize to the same 4bpp shade, so * reserving 0 costs nothing visible). * - * This representation exists to carry structure the wire pipeline will later - * exploit: a plane's glyph list can become on-glasses display-list references - * (texture cache), and the offset can diverge per lens for stereo depth. For - * now the compositor boundary just flattens (flattenPlanes) and feeds the - * existing incremental-update and screen-preview pipeline. + * Static planes form the incremental screen buffer. Planes with stereo depth + * and retained presentation draws replay from resources in the root display list. */ export type Plane = { + shellKey?: number; + depth?: number; + dimUnderneath?: number; image: GrayImage; /** * Offset of the plane's top-left within the submitted frame. Applied - * identically to both lenses for now; a per-lens divergence (stereo depth - * for floating overlays) is the planned extension. + * before any per-lens horizontal offset from depth. */ x: number; y: number; @@ -70,8 +69,9 @@ export function flattenPlanes( /** * Flatten planes and also return the frame's deferred draws (glyphs and * images), translated into frame coordinates and in bake order (plane order, - * then each image's draw order). The flattened image has every draw baked in, - * exactly as flattenPlanes produces; the list preserves the draws' identity + * then each image's draw order). Regular draws are baked into the image; + * selected-menu presentation records remain unbaked for root-list replay. + * The list preserves the draws' identity * so the texture-cache pipeline can replay them as on-glasses cached draws * (see graphics/glyph-wire.ts). */ @@ -79,15 +79,54 @@ export function flattenPlanesWithDraws( planes: readonly Plane[], size?: { width: number; height: number }, ): { image: GrayImage; draws: DeferredDraw[] } { - const image = flattenPlanes(planes, size); + const rasters = planes.map(p => p.image.withDrawsBaked(false)); + const image = flattenPlanes(planes.map((plane, i) => ({ ...plane, image: plane.depth ? new GrayImage(plane.image.width, plane.image.height) : rasters[i]! })), size); const draws: DeferredDraw[] = []; - for (const plane of planes) { - for (const placed of plane.image.draws) { - draws.push( - plane.x === 0 && plane.y === 0 - ? placed - : { ...placed, x: placed.x + plane.x, y: placed.y + plane.y }, - ); + for (const [index, plane] of planes.entries()) { + const retained: DeferredDraw[] = []; + if (plane.depth) { + // Keep the whole menu off the screen buffer, including its black background. + const raster = rasters[index]!; + let left = raster.width, top = raster.height, right = -1, bottom = -1; + for (let y = 0; y < raster.height; y++) for (let x = 0; x < raster.width; x++) if (raster.pixels[y*raster.width+x]) { + left = Math.min(left,x); top = Math.min(top,y); right = Math.max(right,x); bottom = Math.max(bottom,y); + } + if (right >= left) { + const crop = new GrayImage(right-left+1,bottom-top+1); + for(let y=0;y d.kind === "image" && d.presentation).map(d => { + const draw = d as PlacedImage; + return { ...draw, presentation: { ...draw.presentation!, depth: draw.presentation!.depth + plane.depth! } }; + })] : plane.image.draws; + for (const placed of planeDraws) { + const translated = { ...placed, x: placed.x + plane.x, y: placed.y + plane.y }; + if (translated.kind === "image" && translated.presentation) { + // Restore opaque pixels from later planes after replaying this row. The screen + // already contains those pixels; rect copies preserve rounded/irregular occlusion. + const occlusions: { x: number; y: number; width: number; height: number }[] = []; + const depth = translated.presentation.depth; + const leftShift = Math.floor(depth / 2), rightShift = -Math.floor((depth + 1) / 2); + const left = Math.max(0, translated.x + Math.min(leftShift, rightShift)); + const right = Math.min(image.width, translated.x + translated.source.width + Math.max(leftShift, rightShift)); + for (let y = Math.max(0, translated.y); y < Math.min(image.height, translated.y + translated.source.height); y++) { + let start = -1; + for (let x = left; x <= right; x++) { + const covered = x < right && planes.some((p, i) => i > index && !p.depth && x >= p.x && y >= p.y && x < p.x + p.image.width && y < p.y + p.image.height && rasters[i]!.pixels[(y-p.y)*p.image.width+x-p.x] !== 0); + if (covered && start < 0) start = x; + if (!covered && start >= 0) { + const previous = occlusions.find(r => r.x === start && r.width === x - start && r.y + r.height === y); + if (previous) previous.height++; else occlusions.push({ x: start, y, width: x-start, height: 1 }); + start = -1; + } + } + } + translated.presentation = { ...translated.presentation, occlusions }; + } + draws.push(translated); } } return { image, draws }; @@ -100,6 +139,6 @@ export function flattenPlanesWithDraws( */ export function planesFingerprint(planes: readonly Plane[]): string { return planes - .map((plane) => `${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`) + .map((plane) => `${plane.depth ?? 0}:${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`) .join("|"); } diff --git a/app/graphics/presentation-wire.ts b/app/graphics/presentation-wire.ts new file mode 100644 index 00000000..0e8c361d --- /dev/null +++ b/app/graphics/presentation-wire.ts @@ -0,0 +1,102 @@ +import type { PlacedImage } from './image' +import { DISPLAY_LIST_RECORD, DrawOp, SCREEN, encodeDisplayList, readDisplayList, paintDisplayList, type DisplayList } from './display-list' + +/** TS/native bridge tags, not firmware draw opcodes. Keep in sync with DrawRecordKind.kt. */ +export const DrawRecordKind = { + GLYPH: 0, + TEXTURE_IMAGE: 1, + FIRMWARE_TEXT: 2, + MENU_SELECTION: 3, + TRANSPARENT_IMAGE: 4, + MASKED_IMAGE: 5, + DISPLAY_LIST: DISPLAY_LIST_RECORD, +} as const + +export function isPresentationKind(kind: number | undefined): boolean { + return kind === DrawRecordKind.MENU_SELECTION || kind === DrawRecordKind.TRANSPARENT_IMAGE || + kind === DrawRecordKind.MASKED_IMAGE || kind === DrawRecordKind.DISPLAY_LIST +} + +/** Presentation record: tag, signed xy, u16 wh/radius, gray fill/stroke, signed depth, restore-count, gray pixels, restore rectangles. */ +export function encodePresentation(draw: PlacedImage): Uint8Array { + const p = draw.presentation! + const { width, height, pixels } = draw.source + if (width < 1 || height < 1 || width > 640 || height > 480 || !p.displayList && 5 + Math.ceil(width / 2) * height > 65536) throw new Error('Presentation image exceeds the resource size limit') + if (!Number.isInteger(p.depth) || p.depth < -128 || p.depth > 127) throw new Error('Invalid menu depth') + if ((p.occlusions?.length ?? 0) > 2048) throw new Error('Too many menu occlusion rectangles') + if (p.displayList) { + const calls = [...p.displayList.calls, ...(p.occlusions ?? []).map(r => ({ + op: DrawOp.RECT_COPY, resource: SCREEN, x: r.x - draw.x, y: r.y - draw.y, + width: r.width, height: r.height, dx: r.x - draw.x, dy: r.y - draw.y, depth: -p.depth, + }))]; + return encodeDisplayList({ displayList: { ...p.displayList, calls }, x: draw.x, y: draw.y, width, height, depth: p.depth }); + } + const header = 16; + const bytes = new Uint8Array(header + width * height + (p.occlusions?.length ?? 0) * 8), view = new DataView(bytes.buffer) + bytes[0] = p.mode === "image" ? DrawRecordKind.TRANSPARENT_IMAGE + : p.mode === "masked-image" ? DrawRecordKind.MASKED_IMAGE : DrawRecordKind.MENU_SELECTION + view.setInt16(1, draw.x, true); view.setInt16(3, draw.y, true) + view.setUint16(5, width, true); view.setUint16(7, height, true); view.setUint16(9, p.radius, true) + bytes[11] = p.background; bytes[12] = p.border; view.setInt8(13, p.depth) + view.setUint16(14, p.occlusions?.length ?? 0, true) + bytes.set(pixels, header) + let offset = header + pixels.length + for (const rect of p.occlusions ?? []) { + view.setUint16(offset, rect.x, true); view.setUint16(offset + 2, rect.y, true) + view.setUint16(offset + 4, rect.width, true); view.setUint16(offset + 6, rect.height, true); offset += 8 + } + return bytes +} + +export type Selection = { displayList?: DisplayList; mode?: "image" | "masked-image"; x: number; y: number; width: number; height: number; radius: number; background: number; border: number; depth: number; pixels: Uint8Array; occlusions: { x: number; y: number; width: number; height: number }[] } +export function readPresentation(bytes: Uint8Array, offset: number): { selection: Selection; end: number } { + if (bytes[offset] === DrawRecordKind.DISPLAY_LIST) { + const { placed, end } = readDisplayList(bytes, offset); + return { selection: { ...placed, radius: 0, background: 0, border: 0, pixels: new Uint8Array(0), occlusions: [] }, end }; + } + const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength), w = v.getUint16(offset + 5, true), h = v.getUint16(offset + 7, true) + const kind = bytes[offset] + const header = 16 + const selection: Selection = { mode: kind === DrawRecordKind.TRANSPARENT_IMAGE ? "image" : kind === DrawRecordKind.MASKED_IMAGE ? "masked-image" : undefined, x: v.getInt16(offset+1,true), y: v.getInt16(offset+3,true), width:w, height:h, + radius:v.getUint16(offset+9,true), background:bytes[offset+11]!, border:bytes[offset+12]!, depth:v.getInt8(offset+13), + pixels:bytes.slice(offset+header,offset+header+w*h), occlusions:[] } + let end=offset+header+w*h + for(let i=0;iMath.min(15,(n+8)>>4), shift=right?-Math.floor((row.depth+1)/2):Math.floor(row.depth/2) + const inside=(x:number,y:number,w:number,h:number,r:number)=>{ + if(x<0||y<0||x>=w||y>=h) return false + r=Math.min(r,Math.floor(w/2),Math.floor(h/2)) + const dx=Math.max(0,2*r-(2*Math.min(x,w-1-x)+1)),dy=Math.max(0,2*r-(2*Math.min(y,h-1-y)+1)) + return dx*dx+dy*dy<=4*r*r + } + const highlightX = row.x, highlightY = row.y + if (!row.mode) for(let yy=0;yy=width||y>=height || !inside(xx,yy,row.width,row.height,row.radius)) continue + const i=y*width+x + output[i]=!inside(xx-1,yy-1,row.width-2,row.height-2,Math.max(0,row.radius-1))?q(row.border)*16:Math.max(output[i]!,q(row.background)*16) + } + for(let yy=0;yy=width||y>=height) continue + const value=q(row.pixels[yy*row.width+xx]!) + if(row.mode === "masked-image" ? row.pixels[yy*row.width+xx] !== 0 : value !== 0) output[y*width+x]=value*16 + } + for(const r of row.occlusions) for(let yy=r.y;yy=0&&yy>=0&&xx 65536) throw new Error(`Shell surface ${plane.shellKey} exceeds 64 KiB (${w}×${h})`) + const cropped = shellCrop(image, left, top, w, h, plane.shellKey!) + const selections = plane.image.draws + .filter((d): d is PlacedImage => d.kind === "image" && !!d.presentation) + .map(d => encodePresentation({ ...d, x: d.x + plane.x, y: d.y + plane.y, + presentation: { ...d.presentation!, depth: d.presentation!.depth + (plane.depth ?? 0) } })) + layers.push({ depth: plane.depth ?? 0, selections, image: cropped.image, x: plane.x + left, y: plane.y + top, key: plane.shellKey!, dim: Math.round((plane.dimUnderneath ?? 1) * 256) }) + } + const result = new Uint8Array(2 + layers.reduce((n, l) => n + 16 + l.image.pixels.length + l.selections.reduce((n, b) => n + b.length, 0), 0)) + const view = new DataView(result.buffer); let p = 0 + const word = (n: number) => { view.setUint16(p, n, true); p += 2 } + word(layers.length) + for (const l of layers) { word(l.key); word(l.x); word(l.y); word(l.image.width); word(l.image.height); word(l.dim); word(l.selections.length); word(l.depth); result.set(l.image.pixels, p); p += l.image.pixels.length; for (const selection of l.selections) { result.set(selection, p); p += selection.length } } + return result +} diff --git a/app/graphics/surface-compositor.ios.ts b/app/graphics/surface-compositor.ios.ts new file mode 100644 index 00000000..be080bfc --- /dev/null +++ b/app/graphics/surface-compositor.ios.ts @@ -0,0 +1,38 @@ +import { fromData, toData } from '../native/kotlin-data' +import type { SurfaceConfiguration, SurfaceRect } from './surface-compositor' +export type { SurfaceConfiguration, SurfaceRect } from './surface-compositor' +declare const FaceclawKitIosSurfaceCompositor: any +/** Retained pixels, clipping, layers and composition live in shared Kotlin. */ +export class SurfaceCompositor { + private native: any + private readonly surfaces = new Set() + constructor(readonly width: number, readonly height: number) { + if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0) throw new Error('Invalid compositor size') + this.native = FaceclawKitIosSurfaceCompositor.alloc().initWithWidthHeight(width, height) + } + configureSurface(id: string, options: SurfaceConfiguration): void { + if (![options.x, options.y, options.width, options.height, options.zOrder].every(Number.isInteger) + || options.width <= 0 || options.height <= 0) throw new Error('Invalid surface geometry') + this.native.configureIdXYWidthHeightZOrderTransparent(id, options.x, options.y, options.width, options.height, options.zOrder, options.transparency === 'color-key') + this.surfaces.add(id) + } + removeSurface(id: string): void { this.native.removeId(id); this.surfaces.delete(id) } + setSurfaceVisible(id: string, visible: boolean): void { this.native.visibleIdVisible(id, visible) } + setSurfaceDepth(id: string, depth: number): void { this.native.depthIdDepth(id, depth) } + setUnderlayDim(belowZOrder: number, factor: number): void { + if (!Number.isFinite(factor)) throw new Error('Invalid dim factor') + this.native.dimBelowFactor(belowZOrder, factor) + } + setScreenBlanked(blanked: boolean): void { this.native.blankBlanked(blanked) } + submitSurfaceFrame(id: string, pixels: Uint8Array, rect: SurfaceRect, draws: ArrayBuffer | null = null): void { + // Keep lifecycle errors catchable in JS; an undeclared Kotlin exception + // crossing the Objective-C boundary terminates the process. + if (!this.surfaces.has(id)) throw new Error(`Unknown surface: ${id}`) + if (![rect.x, rect.y, rect.width, rect.height].every(Number.isInteger) + || rect.width <= 0 || rect.height <= 0 || pixels.length !== rect.width * rect.height) throw new Error('Invalid frame buffer or rectangle') + this.native.submitDrawsIdDataXYWidthHeightDraws(id, toData(pixels), rect.x, rect.y, rect.width, rect.height, + draws ? toData(new Uint8Array(draws)) : null) + } + setShellScene(bytes: Uint8Array): void { this.native.shellData(toData(bytes)) } + composite(): Uint8Array { return fromData(this.native.composite()) } +} diff --git a/app/graphics/surface-compositor.ts b/app/graphics/surface-compositor.ts new file mode 100644 index 00000000..30a86dfa --- /dev/null +++ b/app/graphics/surface-compositor.ts @@ -0,0 +1,103 @@ +import { presentationRecords, readPresentation, paintPresentation, type Selection } from "./presentation-wire" +/** Platform-independent 8bpp surface composition for the local display. */ +export type SurfaceRect = { x: number; y: number; width: number; height: number } +export type SurfaceConfiguration = SurfaceRect & { + zOrder: number + transparency: 'opaque' | 'color-key' +} +type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean; selections: Selection[] } + +export class SurfaceCompositor { + private readonly surfaces = new Map() + private shellScene: Uint8Array | null = null + private shellReceivedAt = 0 + setShellScene(bytes: Uint8Array): void { this.shellScene = new Uint8Array(bytes); this.shellReceivedAt = Date.now() } + private dimBelow = 0 + private dimFactor = 1 + private blanked = false + constructor(readonly width: number, readonly height: number) { + if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0) { + throw new Error('Invalid compositor size') + } + } + configureSurface(id: string, options: SurfaceConfiguration): void { + if (![options.x, options.y, options.width, options.height, options.zOrder].every(Number.isInteger) + || options.width <= 0 || options.height <= 0) throw new Error('Invalid surface geometry') + const previous = this.surfaces.get(id) + const pixels = previous?.width === options.width && previous.height === options.height + ? previous.pixels : new Uint8Array(options.width * options.height) + this.surfaces.set(id, { ...options, pixels, visible: previous?.visible ?? true, selections: previous?.selections ?? [] }) + } + removeSurface(id: string): void { this.surfaces.delete(id) } + setSurfaceVisible(id: string, visible: boolean): void { + const surface = this.surfaces.get(id) + if (surface) surface.visible = visible + } + /** Stereo depth only shifts the lenses; this local composite shows the unshifted screen. */ + setSurfaceDepth(_id: string, _depth: number): void {} + setUnderlayDim(belowZOrder: number, factor: number): void { + if (!Number.isFinite(factor)) throw new Error('Invalid dim factor') + this.dimBelow = belowZOrder + this.dimFactor = Math.max(0, Math.min(1, factor)) + } + setScreenBlanked(blanked: boolean): void { this.blanked = blanked } + submitSurfaceFrame(id: string, pixels: Uint8Array, rect: SurfaceRect, _draws: ArrayBuffer | null = null): void { + const surface = this.surfaces.get(id) + if (!surface) throw new Error(`Unknown surface: ${id}`) + if (![rect.x, rect.y, rect.width, rect.height].every(Number.isInteger) + || rect.width <= 0 || rect.height <= 0 || pixels.length !== rect.width * rect.height) { + throw new Error('Invalid frame buffer or rectangle') + } + surface.selections = presentationRecords(_draws) + const left = Math.max(0, rect.x), right = Math.min(surface.width, rect.x + rect.width) + const top = Math.max(0, rect.y), bottom = Math.min(surface.height, rect.y + rect.height) + for (let y = top; y < bottom; y++) { + if (right <= left) break + const source = (y - rect.y) * rect.width + left - rect.x + surface.pixels.set(pixels.subarray(source, source + right - left), y * surface.width + left) + } + } + composite(): Uint8Array { + const output = new Uint8Array(this.width * this.height) + if (this.blanked) return output + const surfaces = [...this.surfaces.values()].filter(s => s.visible && !(this.shellScene && s === this.surfaces.get('shell'))).sort((a, b) => a.zOrder - b.zOrder) + for (const surface of surfaces) { + const left = Math.max(0, surface.x), right = Math.min(this.width, surface.x + surface.width) + const top = Math.max(0, surface.y), bottom = Math.min(this.height, surface.y + surface.height) + const dim = !this.shellScene && surface.zOrder < this.dimBelow ? this.dimFactor : 1 + for (let y = top; y < bottom; y++) { + let source = (y - surface.y) * surface.width + left - surface.x + let target = y * this.width + left + for (let x = left; x < right; x++, source++, target++) { + const value = surface.pixels[source] + // Test the original value: opaque near-black must still cover the + // window beneath, even when dimming rounds it down to black. + if (surface.transparency === 'color-key' && value === 0) continue + output[target] = Math.round(value * dim) + } + } + } + const screen = output.slice() + if (!surfaces.some(s => s.zOrder > 1) && (this.shellScene || surfaces.some(s => s.selections.length))) { + for(let i=0;i>4)*16 + for(const surface of surfaces) for(const row of surface.selections) paintPresentation(output,screen,this.width,this.height,{...row,x:row.x+surface.x,y:row.y+surface.y,occlusions:row.occlusions.map(r=>({...r,x:r.x+surface.x,y:r.y+surface.y}))}) + } + if (this.shellScene && !surfaces.some(s => s.zOrder > 1)) { + const quantize = (v: number) => Math.min(15, (v + 8) >> 4) + for (let i=0;i=0 && y+yy>=0 && x+xx+shift 0) { + if ((global.isAndroid || global.isIOS) && Number.isFinite(sizePx) && sizePx > 0) { try { - const raw = String(com.faceclaw.app.FontFileRenderer.getFontMetrics(path, sizePx) ?? ""); + const raw = String(fontRenderer.getFontMetrics(path, sizePx) ?? ""); const parts = raw.split(" ").map((part) => parseInt(part, 10)); if (parts.length === 3 && parts.every((part) => Number.isFinite(part))) { const [ascent, descent] = parts as [number, number, number]; @@ -170,7 +169,7 @@ export class TtfFont implements GlyphFont { /** * Kerning between two adjacent codepoints: measure(ab) - a - b, memoized. - * One JNI measurement per distinct pair per JS context. + * One native measurement per distinct pair per JS context. */ private kernBetween(a: number, b: number): number { const key = a * 0x200000 + b; @@ -179,7 +178,7 @@ export class TtfFont implements GlyphFont { let kern = 0; try { const pair = Number( - com.faceclaw.app.FontFileRenderer.measureTextExact( + fontRenderer.measureTextExact( this.path, String.fromCodePoint(a) + String.fromCodePoint(b), this.sizePx, @@ -203,7 +202,7 @@ export class TtfFont implements GlyphFont { let bytes: Uint8Array; try { bytes = toUint8Array( - com.faceclaw.app.FontFileRenderer.renderGlyphCell(this.path, this.sizePx, codePoint, this.gamma), + fontRenderer.renderGlyphCell(this.path, this.sizePx, codePoint, this.gamma), ); } catch (error) { console.warn(`renderGlyphCell failed for U+${codePoint.toString(16)}: ${error}`); diff --git a/app/health/health-ingest.ts b/app/health/health-ingest.ts index d1d93a8e..70afcfc0 100644 --- a/app/health/health-ingest.ts +++ b/app/health/health-ingest.ts @@ -7,7 +7,7 @@ * The live BLE path lives on the `ring-health-protocol` branch, which this one * deliberately does not touch. What is here is the whole conversion - the part * with the judgement calls in it - written against a structural description of - * `RingProtocol.java`'s record types rather than an import of them, so that + * `RingProtocol.kt`'s record types rather than an import of them, so that * branch and this one can be joined later without either having been built * around the other. * @@ -140,7 +140,7 @@ export function convertRecords(records: readonly WireRecord[]): ConversionResult * An UNANCHORED (backlog) page is dropped rather than placed. The decode did * solve where one such page landed, but explicitly could not derive the rule * that produced it and warned that a future page anchored elsewhere would - * break it. `RingProtocol.java` already made the matching call - it hands + * break it. `RingProtocol.kt` already made the matching call - it hands * backlog groups out with `UNKNOWN_TIME` rather than an invented time - and a * health chart is exactly the wrong place to be the first component that * guesses. Dropping loses a page; guessing corrupts the history silently. diff --git a/app/health/health-types.ts b/app/health/health-types.ts index 8739d5fc..f9aeb698 100644 --- a/app/health/health-types.ts +++ b/app/health/health-types.ts @@ -1,7 +1,7 @@ /** * The health record shapes, normalised for storage and display. * - * These mirror `RingProtocol.java`'s `HourlyRecord` / `StepsRecord` / + * These mirror `RingProtocol.kt`'s `HourlyRecord` / `StepsRecord` / * `SleepRecord` (on the `ring-health-protocol` branch) but deliberately are * NOT the same types: the Java types are wire-shaped - raw hour indices, raw * stage ids, ring-relative timestamps, a per-page anchor that may be absent. diff --git a/app/native/active-display.ios.ts b/app/native/active-display.ios.ts new file mode 100644 index 00000000..9c635c29 --- /dev/null +++ b/app/native/active-display.ios.ts @@ -0,0 +1,12 @@ +/** Workers send baked pixels and shared-Kotlin atlas references together. */ +const display = { + submitSurfaceFrame(buffer: ArrayBuffer, surfaceId: string, _x: number, _y: number, width: number, height: number, + _fingerprint?: string, _paintMs?: number, _frameId?: number, draws: ArrayBuffer | null = null): void { + const data = NSData.dataWithBytesLength(interop.handleof(buffer), buffer.byteLength) + const drawData = draws ? NSData.dataWithBytesLength(interop.handleof(draws), draws.byteLength) : null + global.postMessage({ type: 'surface-frame', surfaceId, width, height, + pixels: data.base64EncodedStringWithOptions(0 as NSDataBase64EncodingOptions), + draws: drawData?.base64EncodedStringWithOptions(0 as NSDataBase64EncodingOptions) }) + }, +} +export function getActiveDisplay(): typeof display { return display } diff --git a/app/native/active-display.ts b/app/native/active-display.ts index a82db467..104073c7 100644 --- a/app/native/active-display.ts +++ b/app/native/active-display.ts @@ -1,19 +1,54 @@ +import { JavaDirectBuffer } from "./java-direct-buffer"; + declare const com: any; +// Reused Java-side buffers for this isolate's frames: passing a JS ArrayBuffer +// to Java leaks it (java-direct-buffer.ts). Module state is per isolate, so +// each worker gets its own pair. +const framePixelsBuffer = new JavaDirectBuffer(640 * 480); +const frameDrawsBuffer = new JavaDirectBuffer(); + /** - * The Java object a worker isolate submits surface frames to: the live BLE + * The display a worker isolate submits surface frames to: the live BLE * communicator, or the preview-only compositor standing in when no glasses * are paired (both expose the same submitSurfaceFrame signature). Located * through Java statics because those are shared across isolates, unlike any - * JS-side state. + * JS-side state. Null when neither exists. * - * Frame submission only. Glasses-hardware calls (e.g. playBuzzerSequence) - * should keep using FaceclawBleCommunicator.getActive() directly, so they - * skip cleanly when nothing is connected. + * Frame submission only. Buzzer effects use worker-buzzer, which routes to + * the platform BLE session and skips playback when disconnected. */ -export function getActiveDisplay(): any { +export function getActiveDisplay(): typeof display | null { + return activeJavaDisplay() ? display : null; +} + +function activeJavaDisplay(): any { return ( com.faceclaw.app.FaceclawBleCommunicator.getActive() ?? com.faceclaw.app.FaceclawPreviewCompositor.getActive() ); } + +/** + * Takes the same ArrayBuffer arguments as the iOS active display and copies + * them into the reused Java buffers right before the synchronous Java call. + */ +const display = { + submitSurfaceFrame(buffer: ArrayBuffer, surfaceId: string, x: number, y: number, width: number, height: number, + fingerprint: string, paintMs: number, frameId: number, draws: ArrayBuffer | null = null): void { + const target = activeJavaDisplay(); + if (!target) return; + target.submitSurfaceFrame( + framePixelsBuffer.load(new Uint8Array(buffer)), + surfaceId, + x, + y, + width, + height, + fingerprint, + paintMs, + frameId, + frameDrawsBuffer.loadOptional(draws), + ); + }, +}; diff --git a/app/native/alarms.ts b/app/native/alarms.ts index ca44672c..77181bfb 100644 --- a/app/native/alarms.ts +++ b/app/native/alarms.ts @@ -124,6 +124,7 @@ export function drainPhoneAlarmJournal(): PhoneAlarmAction[] { /** Conditions under which the phone may fail to ring, most serious first. */ export function checkPhoneAlarmReliability(): AlarmReliabilityIssue[] { + if (global.isIOS) return [{ code: "ios-foreground-only", message: "Keep Faceclaw open: background timer and alarm alerts are not available on iOS yet.", fixable: false }]; const context = getContext(); if (!context) return []; let parsed: any; diff --git a/app/native/anthropic.ts b/app/native/anthropic.ts index 725f81fc..071867cb 100644 --- a/app/native/anthropic.ts +++ b/app/native/anthropic.ts @@ -8,13 +8,13 @@ import type { LlmToolDefinition, } from "../assistant/llm-protocol"; -declare const com: any; +import { openSseRequest, type SseListener } from "./sse"; /** * Minimal streaming client for the Anthropic Messages API. There is no * official SDK for the NativeScript runtime, so this speaks raw HTTP/SSE via - * FaceclawSseRequest (okhttp on the Java side), which delivers the response - * body line by line on this isolate's thread. + * the platform transport (OkHttp on Android, URLSession on iOS), which + * delivers response lines on this isolate's thread. */ const ANTHROPIC_URL = "https://api.anthropic.com/v1/messages"; @@ -55,22 +55,19 @@ export function streamAnthropicMessage(options: AnthropicStreamOptions): Anthrop const fail = (message: string) => { if (settled) return; settled = true; + request?.cancel(); options.onError(message); }; const done = () => { if (settled) return; settled = true; + request?.cancel(); options.onDone({ text, content: finalizeBlocks(blocks), stopReason }); }; - if (!global.isAndroid) { - // Deferred so the caller gets its handle back before the callback runs. - setTimeout(() => fail("Anthropic API is only wired up on Android"), 0); - return { cancel: () => {} }; - } if (!apiKey) { setTimeout(() => fail("No Anthropic API key set"), 0); - return { cancel: () => {} }; + return { cancel: () => { settled = true; } }; } const body: any = { @@ -83,7 +80,7 @@ export function streamAnthropicMessage(options: AnthropicStreamOptions): Anthrop if (options.tools?.length) body.tools = options.tools; if (options.effort) body.output_config = { effort: options.effort }; - const listener = new com.faceclaw.app.FaceclawSseListener({ + const listener: SseListener = { onLine: (line: string) => { if (settled) return; const event = parseSseDataLine(String(line)); @@ -144,25 +141,19 @@ export function streamAnthropicMessage(options: AnthropicStreamOptions): Anthrop fail(describeHttpError(Number(code), String(errorBody))); }, onComplete: () => { - // Normally message_stop settles first; a clean EOF without it still - // resolves with whatever streamed. - done(); + // A transport EOF alone must not execute an unfinished tool call. + fail("Anthropic stream ended before the response completed"); }, onFailure: (message: string) => { fail(`Anthropic connection failed: ${String(message)}`); }, - }); + }; - const headers = Array.create("java.lang.String", 4) as string[]; - headers[0] = "x-api-key"; - headers[1] = apiKey; - headers[2] = "anthropic-version"; - headers[3] = ANTHROPIC_VERSION; try { - request = new com.faceclaw.app.FaceclawSseRequest(ANTHROPIC_URL, JSON.stringify(body), headers, listener); + request = openSseRequest(ANTHROPIC_URL, JSON.stringify(body), { "x-api-key": apiKey, "anthropic-version": ANTHROPIC_VERSION }, listener); } catch (error) { setTimeout(() => fail(`Anthropic request failed: ${String((error as Error)?.message ?? error)}`), 0); - return { cancel: () => {} }; + return { cancel: () => { settled = true; } }; } return { diff --git a/app/native/asr-model.ts b/app/native/asr-model.ts index 591bdc73..97fc7e3b 100644 --- a/app/native/asr-model.ts +++ b/app/native/asr-model.ts @@ -18,7 +18,7 @@ declare const java: any; * - "whisper-base-en": sherpa-onnx's offline Whisper backend, base.en, * int8-quantized. Picked over tiny.en for materially better accuracy * (Whisper's own tiny/base WER gap is real and well documented) on the - * reasoning that FaceclawVoiceController.java no longer re-decodes a + * reasoning that FaceclawVoiceController.kt no longer re-decodes a * Whisper segment on every live-partial tick — see its ASR_WHISPER_* * comment — so base.en's extra compute is a one-time cost per utterance, * not a cost paid ~1.4x/second while the user is speaking. tiny.en is an diff --git a/app/native/binary-http.ios.ts b/app/native/binary-http.ios.ts new file mode 100644 index 00000000..d96d9aad --- /dev/null +++ b/app/native/binary-http.ios.ts @@ -0,0 +1,10 @@ +import { Http } from '@nativescript/core'; +import { withUserAgent } from '../util/http'; +import type { BinaryResponse } from './binary-http'; + +/** Bulk NSData -> ArrayBuffer; bypass fetch's Blob/FileReader copies. */ +export async function downloadBinary(url: string, timeoutMs: number): Promise { + const response = await Http.request({ url, method: 'GET', headers: withUserAgent(), timeout: timeoutMs }); + const length = Object.entries(response.headers).find(([name]) => name.toLowerCase() === 'content-length')?.[1]; + return { status: response.statusCode, bytes: new Uint8Array(response.content.toArrayBuffer()), contentLength: Number(length ?? 0) }; +} diff --git a/app/native/binary-http.ts b/app/native/binary-http.ts new file mode 100644 index 00000000..b70b832b --- /dev/null +++ b/app/native/binary-http.ts @@ -0,0 +1,15 @@ +import { fetchWithUserAgent } from '../util/http'; +export type BinaryResponse = { status: number; bytes: Uint8Array; contentLength: number }; +export async function downloadBinary(url: string, timeoutMs: number): Promise { + let timer: ReturnType; + const request = async () => { + const response = await fetchWithUserAgent(url); + return { status: response.status, bytes: new Uint8Array(await response.arrayBuffer()), + contentLength: Number(response.headers.get('Content-Length') ?? 0) }; + }; + try { + return await Promise.race([request(), new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error('Download timed out.')), timeoutMs); + })]); + } finally { clearTimeout(timer!); } +} diff --git a/app/native/ble-permissions.ios.ts b/app/native/ble-permissions.ios.ts new file mode 100644 index 00000000..233c368a --- /dev/null +++ b/app/native/ble-permissions.ios.ts @@ -0,0 +1,2 @@ +import { iosBluetooth } from './ios-bluetooth' +export async function ensureBlePermissions(): Promise { await iosBluetooth().ensureReady() } diff --git a/app/native/ble-permissions.ts b/app/native/ble-permissions.ts new file mode 100644 index 00000000..14a4b9ad --- /dev/null +++ b/app/native/ble-permissions.ts @@ -0,0 +1 @@ +export { ensureBlePermissions } from '../g2/android-permissions' diff --git a/app/native/ble-traffic.ios.ts b/app/native/ble-traffic.ios.ts new file mode 100644 index 00000000..f41aafd4 --- /dev/null +++ b/app/native/ble-traffic.ios.ts @@ -0,0 +1,15 @@ +export type { BleTrafficSample } from './ble-traffic' +declare const FaceclawKitIosBleTraffic: any + +// The Kotlin counters reset on every sample; accumulate here so callers see +// running totals like FaceclawBleManager.sampleOutboundTraffic on Android. +// CoreBluetooth has no per-message boundary; messages counts write calls. +const totals = { messages: 0, bytes: 0, frames: 0 } +export function sampleBleTraffic(): { messages: number; bytes: number; frames: number } { + const sample = FaceclawKitIosBleTraffic.shared.sample() + const written = Number(sample.getIndex(0)), frames = Number(sample.getIndex(2)) + totals.bytes += written + totals.frames += frames + if (written > 0) totals.messages++ + return { ...totals } +} diff --git a/app/native/calendar-permissions.ios.ts b/app/native/calendar-permissions.ios.ts new file mode 100644 index 00000000..5758f457 --- /dev/null +++ b/app/native/calendar-permissions.ios.ts @@ -0,0 +1,44 @@ +import { Dialogs, Utils } from "@nativescript/core"; + +declare const FaceclawCalendar: any; + +export function hasCalendarPermission(): boolean { + return FaceclawCalendar.shared().hasPermission(); +} + +let pending: Promise | null = null; + +export function ensureCalendarPermission(): Promise { + if (hasCalendarPermission()) return Promise.resolve(true); + if (!pending) pending = requestPermission().finally(() => { pending = null; }); + return pending; +} + +async function requestPermission(): Promise { + const calendar = FaceclawCalendar.shared(); + const status = String(calendar.permissionStatus()); + if (status === "denied") { + const open = await Dialogs.confirm({ + title: "Calendar Access", + message: "Allow calendar access in Settings to display your events on the glasses. On iOS 17 or later, choose Full Access.", + okButtonText: "Open Settings", + cancelButtonText: "Cancel", + }); + if (open) await Utils.openUrl("app-settings:"); + return false; + } + if (status === "restricted") { + await Dialogs.alert({ + title: "Calendar Access Restricted", + message: "Calendar access is restricted on this iPhone. Check Screen Time or device management settings.", + okButtonText: "OK", + }); + return false; + } + return new Promise((resolve, reject) => { + calendar.requestPermission((granted: boolean, error: string | null) => { + if (error) reject(new Error(String(error))); + else resolve(granted); + }); + }); +} diff --git a/app/native/calendar-permissions.ts b/app/native/calendar-permissions.ts new file mode 100644 index 00000000..3a3b9625 --- /dev/null +++ b/app/native/calendar-permissions.ts @@ -0,0 +1 @@ +export { ensureCalendarPermission, hasCalendarPermission } from "../g2/android-permissions"; diff --git a/app/native/calendar-types.ts b/app/native/calendar-types.ts new file mode 100644 index 00000000..5b1381d7 --- /dev/null +++ b/app/native/calendar-types.ts @@ -0,0 +1,12 @@ +export type CalendarEvent = { + /** Android event ID or EventKit event identifier. */ + id: number | string; + title: string; + startMs: number; + endMs: number; + allDay: boolean; + location: string; + calendarName: string; +}; + +export type CalendarReadState = "ready" | "loading" | "error"; diff --git a/app/native/calendar.ios.ts b/app/native/calendar.ios.ts new file mode 100644 index 00000000..4b8b1c88 --- /dev/null +++ b/app/native/calendar.ios.ts @@ -0,0 +1,122 @@ +import { hasCalendarPermission } from "./calendar-permissions"; +import { type CalendarEvent, type CalendarReadState } from "./calendar-types"; +export type { CalendarEvent } from "./calendar-types"; + +declare const FaceclawCalendar: any; + +const DEFAULT_WINDOW_MS = 14 * 24 * 60 * 60 * 1000; +const CACHE_MS = 30_000; +type Cache = { + events: CalendarEvent[]; + atMs: number; + maxEvents: number; + windowMs: number; + state: CalendarReadState; + pending: boolean; +}; +let cache: Cache | null = null; +let bound = false; +const listeners = new Set<() => void>(); + +function calendar(): any { + const bridge = FaceclawCalendar.shared(); + if (!bound) { + bound = true; + bridge.changeHandler = invalidateCalendarCache; + } + return bridge; +} + +export function onCalendarChanged(listener: () => void): () => void { + calendar(); + listeners.add(listener); + return () => { listeners.delete(listener); }; +} + +function notifyChanged(): void { + for (const listener of listeners) listener(); +} + +export function invalidateCalendarCache(): void { + // In-flight callbacks retain their old entry, so cannot restore invalid data. + cache = null; + notifyChanged(); +} + +export function getCalendarReadState(): CalendarReadState { + return cache?.state ?? "ready"; +} + +/** Return cached events immediately, refreshing asynchronously when needed. */ +export function readUpcomingEvents(maxEvents = 50, windowMs = DEFAULT_WINDOW_MS, forceRefresh = false): CalendarEvent[] { + if (!hasCalendarPermission()) { + cache = null; + return []; + } + maxEvents = Number.isFinite(maxEvents) ? Math.min(200, Math.max(0, Math.round(maxEvents))) : 0; + windowMs = Number.isFinite(windowMs) ? Math.min(366 * 24 * 60 * 60 * 1000, Math.max(0, Math.round(windowMs))) : 0; + if (!maxEvents || !windowMs) return []; + + const now = Date.now(); + const sameQuery = cache?.maxEvents === maxEvents && cache?.windowMs === windowMs; + if (!sameQuery || !cache!.pending && (forceRefresh || now - cache!.atMs >= CACHE_MS)) { + const entry: Cache = { + events: sameQuery ? cache!.events : [], atMs: now, maxEvents, windowMs, + state: sameQuery && cache!.events.length ? "ready" : "loading", pending: true, + }; + cache = entry; + const finish = (json: string, error: string | null) => { + if (cache !== entry) return; + if (!hasCalendarPermission()) { invalidateCalendarCache(); return; } + try { + entry.events = parseEvents(json, error, maxEvents, now + windowMs); + entry.state = "ready"; + } catch { + entry.events = []; + entry.state = "error"; + } + entry.pending = false; + entry.atMs = Date.now(); + notifyChanged(); + }; + try { calendar().readUpcomingEventsWindowMsCompletion(maxEvents, windowMs, finish); } + catch { finish("[]", "Calendar unavailable"); } + } + return (cache?.events ?? []).filter(event => event.endMs > now); +} + +/** Assistant queries await their own result without replacing the display cache. */ +export async function readUpcomingEventsAsync(maxEvents = 50, windowMs = DEFAULT_WINDOW_MS): Promise { + if (!hasCalendarPermission()) throw new Error("Calendar permission is required."); + maxEvents = Number.isFinite(maxEvents) ? Math.min(200, Math.max(0, Math.round(maxEvents))) : 0; + windowMs = Number.isFinite(windowMs) ? Math.min(366 * 24 * 60 * 60 * 1000, Math.max(0, Math.round(windowMs))) : 0; + if (!maxEvents || !windowMs) return []; + const endMs = Date.now() + windowMs; + return new Promise((resolve, reject) => { + calendar().readUpcomingEventsWindowMsCompletion(maxEvents, windowMs, (json: string, error: string | null) => { + try { + if (!hasCalendarPermission()) throw new Error("Calendar permission is required."); + resolve(parseEvents(json, error, maxEvents, endMs)); + } catch (error) { reject(error); } + }); + }); +} + +function parseEvents(json: string, error: string | null, maxEvents: number, endMs: number): CalendarEvent[] { + if (error) throw new Error(String(error)); + const rows = JSON.parse(String(json)); + if (!Array.isArray(rows)) throw new Error("Invalid calendar response"); + return rows.map(normalizeEvent).filter((event): event is CalendarEvent => event !== null) + .filter(event => event.endMs > Date.now() && event.startMs < endMs) + .sort((a, b) => a.startMs - b.startMs).slice(0, maxEvents); +} + +function normalizeEvent(value: any): CalendarEvent | null { + if (!value || typeof value !== "object" || typeof value.startMs !== "number" || typeof value.endMs !== "number" || + !Number.isFinite(value.startMs) || !Number.isFinite(value.endMs) || value.endMs < value.startMs) return null; + return { + id: String(value.id ?? ""), title: String(value.title ?? ""), + startMs: value.startMs, endMs: value.endMs, allDay: Boolean(value.allDay), + location: String(value.location ?? ""), calendarName: String(value.calendarName ?? ""), + }; +} diff --git a/app/native/calendar.ts b/app/native/calendar.ts index bd8e9e43..8768947b 100644 --- a/app/native/calendar.ts +++ b/app/native/calendar.ts @@ -1,6 +1,8 @@ import { Utils } from "@nativescript/core"; import { hasCalendarPermission } from "../g2/android-permissions"; import { spanCurrent } from "./frame-timings"; +import { type CalendarEvent, type CalendarReadState } from "./calendar-types"; +export type { CalendarEvent } from "./calendar-types"; declare const com: any; @@ -10,15 +12,9 @@ declare const com: any; * the Calendar app don't re-run the content-provider query every frame. */ -export type CalendarEvent = { - id: number; - title: string; - startMs: number; - endMs: number; - allDay: boolean; - location: string; - calendarName: string; -}; +// Android reads synchronously; iOS notifies subscribers after background reads. +export function onCalendarChanged(_listener: () => void): () => void { return () => {}; } +export function getCalendarReadState(): CalendarReadState { return "ready"; } const DEFAULT_MAX_EVENTS = 50; const DEFAULT_WINDOW_MS = 14 * 24 * 60 * 60 * 1000; @@ -73,6 +69,11 @@ export function readUpcomingEvents( } } +/** Awaitable API shared with the iOS background reader. */ +export async function readUpcomingEventsAsync(maxEvents = DEFAULT_MAX_EVENTS, windowMs = DEFAULT_WINDOW_MS): Promise { + return readUpcomingEvents(maxEvents, windowMs); +} + /** Drop the cached events so the next read re-queries the provider. */ export function invalidateCalendarCache(): void { cache = null; diff --git a/app/native/compass-types.ts b/app/native/compass-types.ts new file mode 100644 index 00000000..14ea05b7 --- /dev/null +++ b/app/native/compass-types.ts @@ -0,0 +1,23 @@ +export const COMPASS_CHANGED = 15; +export const COMPASS_CALIBRATION_STARTED = 16; +export const COMPASS_CALIBRATION_COMPLETE = 17; + +export type CompassDiagnostics = { + /** 0 uncalibrated .. 3 well calibrated; -1 unavailable. */ + magneticAccuracy: number; + /** 0 none, 1 small/temperature, 2 large; -1 unavailable. */ + magneticAnomalies: number; + /** 0 unknown, 1 GRV (relative), 2 GMRV, 3 RV. */ + orientationSource: number; + flags: number; + sampleTimeMs: number; +}; + +export type CompassEvent = { + command: number; + /** Magnetic heading in degrees, or -1 for calibration-only events. */ + headingDegrees: number; + /** Absent with stock/older CFW or an unrecognized diagnostic extension. */ + diagnostics?: CompassDiagnostics; +}; + diff --git a/app/native/compass.ios.ts b/app/native/compass.ios.ts new file mode 100644 index 00000000..ab201fce --- /dev/null +++ b/app/native/compass.ios.ts @@ -0,0 +1,29 @@ +import { type CompassEvent } from './compass-types' +export * from './compass-types' + +type CompassSession = { setCompassEnabled(enabled: boolean): void } +const owners = new Set() +const listeners = new Set<(event: CompassEvent) => void>() +let session: CompassSession | null = null + +/** Holds and listeners survive disconnect/reconnect and opening before Connect. */ +export function bindCompassSession(next: CompassSession): void { + if (session && session !== next) session.setCompassEnabled(false) + session = next + session.setCompassEnabled(owners.size > 0) +} + +export function setCompassEnabled(enabled: boolean, owner = 'compass'): void { + if (enabled) owners.add(owner) + else owners.delete(owner) + session?.setCompassEnabled(owners.size > 0) +} + +export function addCompassListener(listener: (event: CompassEvent) => void): () => void { + listeners.add(listener) + return () => { listeners.delete(listener) } +} + +export function receiveCompassEvent(event: CompassEvent): void { + for (const listener of [...listeners]) listener(event) +} diff --git a/app/native/compass.ts b/app/native/compass.ts index 8a0d6491..dae5f42f 100644 --- a/app/native/compass.ts +++ b/app/native/compass.ts @@ -1,28 +1,8 @@ declare const com: any; declare const global: any; -export const COMPASS_CHANGED = 15; -export const COMPASS_CALIBRATION_STARTED = 16; -export const COMPASS_CALIBRATION_COMPLETE = 17; - -export type CompassDiagnostics = { - /** 0 uncalibrated .. 3 well calibrated; -1 unavailable. */ - magneticAccuracy: number; - /** 0 none, 1 small/temperature, 2 large; -1 unavailable. */ - magneticAnomalies: number; - /** 0 unknown, 1 GRV (relative), 2 GMRV, 3 RV. */ - orientationSource: number; - flags: number; - sampleTimeMs: number; -}; - -export type CompassEvent = { - command: number; - /** Magnetic heading in degrees, or -1 for calibration-only events. */ - headingDegrees: number; - /** Absent with stock/older CFW or an unrecognized diagnostic extension. */ - diagnostics?: CompassDiagnostics; -}; +import { type CompassEvent } from "./compass-types"; +export * from "./compass-types"; function activeCommunicator(): any { if (!global.isAndroid) return null; diff --git a/app/native/declination.ios.ts b/app/native/declination.ios.ts new file mode 100644 index 00000000..d8f8678f --- /dev/null +++ b/app/native/declination.ios.ts @@ -0,0 +1,14 @@ +declare const FaceclawLocation: any + +export function restoreDeclination(_latitude: number, _longitude: number, degrees: number): number | null { + return Number.isFinite(degrees) && Math.abs(degrees) <= 180 ? degrees : null +} + +export function getCurrentDeclination(): Promise<{ latitude: number; longitude: number; degrees: number }> { + return new Promise((resolve, reject) => { + FaceclawLocation.shared().requestDeclination((degrees: number, latitude: number, longitude: number, error: string) => { + if (error) reject(new Error(error)) + else resolve({ degrees, latitude, longitude }) + }) + }) +} diff --git a/app/native/declination.ts b/app/native/declination.ts new file mode 100644 index 00000000..751ddae4 --- /dev/null +++ b/app/native/declination.ts @@ -0,0 +1,13 @@ +import { magneticDeclinationDegrees } from './geomagnetic' +import { getCurrentLocation } from './location' + +export function restoreDeclination(latitude: number, longitude: number, _degrees: number): number | null { + return magneticDeclinationDegrees(latitude, longitude) +} + +export async function getCurrentDeclination(): Promise<{ latitude: number; longitude: number; degrees: number }> { + const location = await getCurrentLocation() + const degrees = magneticDeclinationDegrees(location.latitude, location.longitude) + if (degrees === null) throw new Error('Magnetic declination unavailable') + return { ...location, degrees } +} diff --git a/app/native/device-discovery-common.ts b/app/native/device-discovery-common.ts new file mode 100644 index 00000000..6a5c4f37 --- /dev/null +++ b/app/native/device-discovery-common.ts @@ -0,0 +1,52 @@ +import { type RawAdvertisement } from "../g2/even-advertisement"; +import { DiscoveryAggregator } from "../g2/pairing-candidates"; +import type { DiscoveredAddressSet } from "./device-discovery"; + +/** + * Pick one address per role from a batch of raw advertisements — the manual + * address page's "load" helper and the flash flow's address resolution. + * + * Runs the batch through the same `DiscoveryAggregator` the pairing page + * uses, so left/right come from ONE serial-joined pair (the closest complete + * one) rather than from whichever arm of whatever pair advertised last — + * "latest per role" silently welded together arms of two different pairs. + * When no arm carries a serial at all (a bonded-device listing has no + * manufacturer data), there is nothing to join on and the latest arm per side + * is used as before. + */ +export function buildAddressSet(candidates: RawAdvertisement[]): DiscoveredAddressSet { + const aggregator = new DiscoveryAggregator(); + for (const raw of candidates) aggregator.ingest(raw); + const classified = aggregator.advertisements(); + const pairs = aggregator.pairs(); + + let left = ""; + let right = ""; + const completePair = pairs.find((pair) => pair.completeness === "complete"); + if (completePair) { + left = completePair.left!.address; + right = completePair.right!.address; + } else if (classified.every((candidate) => !candidate.serial)) { + for (const role of ["left", "right"] as const) { + const latest = classified + .filter((candidate) => candidate.role === role) + .sort((a, b) => b.seenAtMs - a.seenAtMs)[0]; + if (role === "left") left = latest?.address ?? ""; + else right = latest?.address ?? ""; + } + } + // Arms with serials but no complete pair: leave the addresses empty. The + // summary below names what was heard; guessing would defeat the serial join. + + const lines = classified.map((candidate) => { + const extras = [candidate.serial ? `serial ${candidate.serial}` : null, candidate.rssi != null ? `${candidate.rssi} dBm` : null].filter(Boolean); + return `${candidate.role}: ${candidate.name} ${candidate.address}${extras.length ? ` (${extras.join(", ")})` : ""}`; + }); + return { + left, + right, + ring: aggregator.rings()[0]?.advertisement.address ?? "", + summary: lines.length ? lines.join("\n") : "No matching devices found.", + }; +} + diff --git a/app/native/device-discovery.ios.ts b/app/native/device-discovery.ios.ts new file mode 100644 index 00000000..519cf51d --- /dev/null +++ b/app/native/device-discovery.ios.ts @@ -0,0 +1,52 @@ +import { iosBluetooth } from './ios-bluetooth' +import type { DiscoveryEvents } from './device-discovery' +import type { RawAdvertisement } from '../g2/even-advertisement' +export { buildAddressSet } from './device-discovery-common' + +export class DeviceDiscoveryBridge { + private ble = iosBluetooth() + private off: (() => void) | null = null + private generation = 0 + private cancelCollection: (() => void) | null = null + get bluetoothEnabled(): boolean { return this.ble.state === 5 } + get scanning(): boolean { return !!this.off && this.ble.scanning } + startScan(events: DiscoveryEvents): boolean { + this.stopScan() + const generation = this.generation + if (!this.bluetoothEnabled) return false + this.off = this.ble.onEvent(event => { + if (event.kind === 'device' && 'device' in event) events.onAdvertisement(event.device) + if (event.kind === 'state' && event.state !== 5) events.onScanFailed?.(event.state ?? 0, 'Bluetooth is unavailable. Turn it on, then tap Scan.') + }) + void this.ble.startScan(24 * 60 * 60 * 1000).catch(error => { + if (generation === this.generation) events.onScanFailed?.(-1, String(error.message ?? error)) + }) + return true + } + stopScan(): void { + ++this.generation; this.cancelCollection?.() + if (this.off) { this.off(); this.off = null; this.ble.stopScan() } + } + // CoreBluetooth does not expose the phone's bond list. Fresh advertisements + // supply both identity and the UUID mapping that iOS needs for connections. + emitBondedDevices(): void {} + async getBondedCandidates(): Promise { return [...this.ble.devices.values()] } + async scanCandidates(timeoutMs = 6000): Promise { + const generation = this.generation + await this.ble.ensureReady() + if (generation !== this.generation) throw new Error('Bluetooth scan cancelled.') + return new Promise((resolve, reject) => { + const samples: RawAdvertisement[] = [] + const finish = (error?: Error) => { + clearTimeout(timer); this.cancelCollection = null; this.stopScan() + if (error) reject(error); else resolve(samples) + } + const timer = setTimeout(() => finish(), timeoutMs) + if (!this.startScan({ onAdvertisement: sample => samples.push(sample), + onScanFailed: (_code, message) => finish(new Error(message)) })) { + finish(new Error('Bluetooth is unavailable.')); return + } + this.cancelCollection = () => { this.cancelCollection = null; clearTimeout(timer); reject(new Error('Bluetooth scan cancelled.')) } + }) + } +} diff --git a/app/native/device-discovery.ts b/app/native/device-discovery.ts index e35c13c0..08e3b131 100644 --- a/app/native/device-discovery.ts +++ b/app/native/device-discovery.ts @@ -1,7 +1,7 @@ import { Utils } from "@nativescript/core"; import { type RawAdvertisement } from "../g2/even-advertisement"; -import { DiscoveryAggregator } from "../g2/pairing-candidates"; +export { buildAddressSet } from "./device-discovery-common"; declare const com: any; @@ -108,53 +108,6 @@ export class DeviceDiscoveryBridge { } } -/** - * Pick one address per role from a batch of raw advertisements — the manual - * address page's "load" helper and the flash flow's address resolution. - * - * Runs the batch through the same `DiscoveryAggregator` the pairing page - * uses, so left/right come from ONE serial-joined pair (the closest complete - * one) rather than from whichever arm of whatever pair advertised last — - * "latest per role" silently welded together arms of two different pairs. - * When no arm carries a serial at all (a bonded-device listing has no - * manufacturer data), there is nothing to join on and the latest arm per side - * is used as before. - */ -export function buildAddressSet(candidates: RawAdvertisement[]): DiscoveredAddressSet { - const aggregator = new DiscoveryAggregator(); - for (const raw of candidates) aggregator.ingest(raw); - const classified = aggregator.advertisements(); - const pairs = aggregator.pairs(); - - let left = ""; - let right = ""; - const completePair = pairs.find((pair) => pair.completeness === "complete"); - if (completePair) { - left = completePair.left!.address; - right = completePair.right!.address; - } else if (classified.every((candidate) => !candidate.serial)) { - for (const role of ["left", "right"] as const) { - const latest = classified - .filter((candidate) => candidate.role === role) - .sort((a, b) => b.seenAtMs - a.seenAtMs)[0]; - if (role === "left") left = latest?.address ?? ""; - else right = latest?.address ?? ""; - } - } - // Arms with serials but no complete pair: leave the addresses empty. The - // summary below names what was heard; guessing would defeat the serial join. - - const lines = classified.map((candidate) => { - const extras = [candidate.serial ? `serial ${candidate.serial}` : null, candidate.rssi != null ? `${candidate.rssi} dBm` : null].filter(Boolean); - return `${candidate.role}: ${candidate.name} ${candidate.address}${extras.length ? ` (${extras.join(", ")})` : ""}`; - }); - return { - left, - right, - ring: aggregator.rings()[0]?.advertisement.address ?? "", - summary: lines.length ? lines.join("\n") : "No matching devices found.", - }; -} function parseRawArray(json: string): RawAdvertisement[] { const items = JSON.parse(json) as unknown[]; diff --git a/app/native/device-info-probe.ios.ts b/app/native/device-info-probe.ios.ts new file mode 100644 index 00000000..913c5e12 --- /dev/null +++ b/app/native/device-info-probe.ios.ts @@ -0,0 +1,89 @@ +import { iosBluetooth } from './ios-bluetooth' +import { kotlinListener } from './kotlin-listener.ios' +import type { DeviceInfo, DeviceInfoState } from './device-info-probe' +export type { DeviceInfo, DeviceInfoState } from './device-info-probe' + +declare const FaceclawKitIosDeviceInfoProbe: any, FaceclawKitFaceclawDeviceInfoProbeListener: any + +/** + * iOS twin of the Android DeviceInfoProbe wrapper: the shared Kotlin DeviceInfoProbeFlow + * runs over a CoreBluetooth central (FaceclawKitIosDeviceInfoProbe); TypeScript only maps + * the stored MAC addresses to peripheral identifiers first. Callbacks arrive on the main + * queue and are fanned out asynchronously like the Android wrapper does. + */ +export class DeviceInfoProbe { + private probe: any = null + private listenerProxy: object | null = null + private readonly logListeners = new Set<(line: string) => void>() + private readonly stateListeners = new Set<(state: DeviceInfoState, detail: string) => void>() + private settled = false + private cancelled = false + private resolveFn: ((info: DeviceInfo) => void) | null = null + private rejectFn: ((error: Error) => void) | null = null + + constructor(private readonly rightAddress: string, private readonly leftAddress = '') {} + + onLog(listener: (line: string) => void): () => void { + this.logListeners.add(listener) + return () => this.logListeners.delete(listener) + } + + onStateChange(listener: (state: DeviceInfoState, detail: string) => void): () => void { + this.stateListeners.add(listener) + return () => this.stateListeners.delete(listener) + } + + run(): Promise { + return new Promise((resolve, reject) => { + this.resolveFn = resolve + this.rejectFn = reject + void this.start() + }) + } + + private async start(): Promise { + let identifiers: Record + try { + this.emit(this.stateListeners, 'connecting', 'right') + identifiers = await iosBluetooth().resolveDevices({ right: this.rightAddress, left: this.leftAddress, ring: '' }) + } catch (error) { + this.settle(error instanceof Error ? error : new Error(String(error)), null) + return + } + if (this.cancelled) { this.settle(new Error('Cancelled.'), null); return } + this.probe = FaceclawKitIosDeviceInfoProbe.alloc().initWithRightAddressLeftAddress(identifiers.right ?? '', identifiers.left ?? '') + this.listenerProxy = kotlinListener(FaceclawKitFaceclawDeviceInfoProbeListener, { + onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')), + onStateStateDetail: (state: string, detail: string) => + this.emit(this.stateListeners, String(state) as DeviceInfoState, String(detail ?? '')), + onResultLeftVersionRightVersionExtension: (leftVersion: string, rightVersion: string, extension: string) => + this.settle(null, { leftVersion: String(leftVersion ?? ''), rightVersion: String(rightVersion ?? ''), extension: String(extension ?? '') }), + onErrorMessage: (message: string) => this.settle(new Error(String(message ?? '')), null), + }) + this.probe.setListenerListener(this.listenerProxy) + this.probe.start() + } + + cancel(): void { + this.cancelled = true + try { this.probe?.cancel() } catch { /* ignore */ } + } + + close(): void { + this.cancelled = true + try { this.probe?.close() } catch { /* ignore */ } + } + + private settle(error: Error | null, info: DeviceInfo | null): void { + if (this.settled) return + this.settled = true + this.close() + if (error) this.rejectFn?.(error) + else if (info) this.resolveFn?.(info) + } + + private emit(listeners: Set<(...args: A) => void>, ...args: A): void { + const snapshot = Array.from(listeners) + setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0) + } +} diff --git a/app/native/faceclaw-communicator.ios.ts b/app/native/faceclaw-communicator.ios.ts new file mode 100644 index 00000000..47fe1231 --- /dev/null +++ b/app/native/faceclaw-communicator.ios.ts @@ -0,0 +1,685 @@ +import type { RingInput } from "../g2/ring-input"; +import { File, knownFolders, path, type ImageSource } from "@nativescript/core"; +import { type FirmwareInfo } from "../g2/firmware-compat"; +import { type CompassEvent } from "./compass-types"; +import { fromData, toData } from "./kotlin-data"; +import { previewPixels } from "./ios-graphics"; +import { iosBluetooth } from "./ios-bluetooth"; + +export type { FirmwareInfo }; + +// Kotlin/Native facades exported by FaceclawKit (see native/kotlin/shared/src/iosMain/.../ble). +declare const FaceclawKitIosGlassesSession: any; +declare const FaceclawKitIosProtocolPlatform: any; +declare const FaceclawKitFaceclawBleCommunicatorListener: any; +declare const FaceclawKitIosAncsListener: any; +declare const FaceclawKitIosAudioPacketListener: any; +declare const FaceclawKitFaceclawCompassListener: any; + + +export type CommunicatorPhase = + | "disconnected" + | "connecting" + | "connected" + | "charging" + | "retrying" + | "unpaired" + | "disconnecting"; + +export type CommunicatorState = { + phase: CommunicatorPhase; + status: string; +}; + +export type HeadsetBatteryState = { + battery: number; + chargingStatus: number; + ringBattery?: number; + ringChargingStatus?: number; +}; + +export type FrameMetrics = { + paintMs: number; + transmitMs: number; + tileCount: number; +}; + +/** See the Android twin: compositor surface configuration in screen pixels. */ +export type SurfaceOptions = { + x: number; + y: number; + width: number; + height: number; + zOrder: number; + transparency: "opaque" | "color-key"; +}; + +export type RawInputEvent = { ringInput?: RingInput } & ( + | { kind: "list-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } + | { kind: "text-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } + | { kind: "sys-event"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } + | { kind: "even-ai"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } + | { kind: "watch-gesture"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } + | { kind: "display-wake"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number } +); + +function nonNegativeNumber(value: number): number { + const numeric = Number(value); + return Number.isFinite(numeric) ? Math.max(0, numeric) : 0; +} + +/** A 0..1 brightness factor as the compositor's 0..256 fixed-point form. */ +export function dimFactor256(factor: number): number { + return Math.round(Math.max(0, Math.min(1, factor)) * 256); +} + +function timestamp(): string { + const now = new Date(); + const pad = (value: number, width = 2) => String(value).padStart(width, "0"); + return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`; +} + +export type PeripheralIdentifiers = { right: string; left: string; ring: string }; + +/** + * Map the configured MAC addresses to CoreBluetooth identifiers through the scan + * layer (app/native/ios-bluetooth.ts): the mapping a previous scan persisted, else a + * fresh scan that classifies Even advertisements. The scan layer also owns Bluetooth + * readiness (creating the central triggers the permission prompt on first use). + */ +export async function resolveIosPeripherals(addresses: { right: string; left: string; ring?: string }): Promise { + const resolved = await iosBluetooth().resolveDevices({ right: addresses.right, left: addresses.left, ring: addresses.ring ?? "" }); + return { + right: String(resolved.right ?? "").toUpperCase(), + left: String(resolved.left ?? "").toUpperCase(), + ring: String(resolved.ring ?? "").toUpperCase(), + }; +} + +/** Bluetooth powered on and authorized (prompts for permission the first time); rejects with the user-facing reason. */ +export function ensureIosBluetoothReady(): Promise { + return iosBluetooth().ensureReady(); +} + +@NativeClass() +class SessionListener extends NSObject { + static ObjCProtocols = [FaceclawKitFaceclawBleCommunicatorListener]; + bridge!: FaceclawCommunicatorBridge; + onStateChangePhaseStatus(phase: string, status: string): void { this.bridge.handleStateChange(String(phase ?? ""), String(status ?? "")); } + onRingEventKindContainerNameEventTypeEventSourceSystemExitReasonCodeFrameIdRingTickRingTypeRingAuxRingSpeed( + kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number, + frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number, + ): void { + this.bridge.handleRingEvent(String(kind ?? ""), String(containerName ?? ""), Number(eventType), Number(eventSource), + Number(systemExitReasonCode), Number(frameId), Number(ringTick), Number(ringType), Number(ringAux), Number(ringSpeed)); + } + onBatteryStateHeadsetBatteryHeadsetChargingRingBatteryRingCharging(headsetBattery: number, headsetCharging: number, ringBattery: number, ringCharging: number): void { + this.bridge.handleBatteryState(Number(headsetBattery), Number(headsetCharging), Number(ringBattery), Number(ringCharging)); + } + onSilentModeSilent(silent: boolean): void { this.bridge.handleSilentMode(Boolean(silent)); } + onWearStateWearing(wearing: boolean): void { this.bridge.handleWearState(Boolean(wearing)); } + onPhoneLockStateLocked(locked: boolean): void { this.bridge.handlePhoneLockState(Boolean(locked)); } + onEvenAppConflictMessage(message: string): void { this.bridge.handleEvenAppConflict(String(message ?? "")); } + onFrameMetricsPaintMsTransmitMsTileCount(paintMs: number, transmitMs: number, tileCount: number): void { + this.bridge.handleFrameMetrics(Number(paintMs), Number(transmitMs), Number(tileCount)); + } + onFrameFinishedFrameIdOutcome(frameId: number, outcome: string): void { this.bridge.handleFrameFinished(Number(frameId), String(outcome ?? "")); } + onFirmwareInfoLeftVersionRightVersionExtension(leftVersion: string, rightVersion: string, extension: string): void { + this.bridge.handleFirmwareInfo(String(leftVersion ?? ""), String(rightVersion ?? ""), String(extension ?? "")); + } +} + +@NativeClass() +class AncsListener extends NSObject { + static ObjCProtocols = [FaceclawKitIosAncsListener]; + bridge!: FaceclawCommunicatorBridge; + onAncsAuthorizationAuthorized(authorized: boolean): void { this.bridge.handleAncsAuthorization(Boolean(authorized)); } + onRelayFrameData(data: NSData): void { this.bridge.handleAncsRelayFrame(fromData(data)); } +} + +@NativeClass() +class AudioPacketListener extends NSObject { + static ObjCProtocols = [FaceclawKitIosAudioPacketListener]; + handler!: (packet: Uint8Array, arm: string, arrivalMs: number) => void; + onAudioPacketDataArmArrivalMs(data: NSData, arm: string, arrivalMs: number): void { + this.handler(fromData(data), String(arm ?? ""), Number(arrivalMs)); + } +} + +@NativeClass() +class CompassListener extends NSObject { + static ObjCProtocols = [FaceclawKitFaceclawCompassListener]; + handler!: (event: CompassEvent) => void; + onCompassEventCommandHeadingDegreesMagneticAccuracyMagneticAnomaliesOrientationSourceDiagnosticFlagsSampleTimeMs( + command: number, headingDegrees: number, magneticAccuracy: number, magneticAnomalies: number, + orientationSource: number, diagnosticFlags: number, sampleTimeMs: number, + ): void { + this.handler({ + command: Number(command), headingDegrees: Number(headingDegrees), + diagnostics: diagnosticFlags >= 0 ? { + magneticAccuracy: Number(magneticAccuracy), magneticAnomalies: Number(magneticAnomalies), + orientationSource: Number(orientationSource), flags: Number(diagnosticFlags), sampleTimeMs: Number(sampleTimeMs), + } : undefined, + }); + } +} + +/** + * The iOS twin of the Android communicator bridge: the same public API over + * the shared Kotlin session (FaceclawKitIosGlassesSession, CoreBluetooth + * underneath). Addresses are CoreBluetooth identifiers (resolveIosPeripherals + * maps the configured MACs). Every Kotlin callback already arrives on the + * main queue; listener fan-out is still deferred with setTimeout like on + * Android so callers never re-enter the bridge from a Kotlin frame. + */ +export class FaceclawCommunicatorBridge { + private readonly communicator: any; + private readonly listenerProxy: SessionListener; + private ancsProxy: AncsListener | null = null; + private nativeCallQueue: Promise = Promise.resolve(); + private queuedNativeCalls = 0; + private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>(); + private readonly finishedFrameOutcomes = new Map(); + private readonly frameFinishedWaiters = new Map void>>(); + private readonly stateListeners = new Set<(state: CommunicatorState) => void>(); + private readonly ringListeners = new Set<(event: RawInputEvent) => void>(); + private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>(); + private readonly silentModeListeners = new Set<(silent: boolean) => void>(); + private readonly wearStateListeners = new Set<(wearing: boolean) => void>(); + private readonly phoneLockStateListeners = new Set<(locked: boolean) => void>(); + private latestWearState: boolean | null = null; + private latestPhoneLockState: boolean | null = null; + private readonly evenAppConflictListeners = new Set<(message: string) => void>(); + private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>(); + private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>(); + private readonly ancsAuthorizationListeners = new Set<(authorized: boolean) => void>(); + private readonly ancsRelayListeners = new Set<(frame: Uint8Array) => void>(); + private audioListener: AudioPacketListener | null = null; + private microphoneWork: Promise = Promise.resolve(); + private microphoneToken = 0; + + constructor(addresses: { right: string; left: string; ring?: string }) { + this.communicator = FaceclawKitIosGlassesSession.alloc().initWithRightAddressLeftAddressRingAddress( + String(addresses.right ?? "").toUpperCase(), + String(addresses.left ?? "").toUpperCase(), + String(addresses.ring ?? "").toUpperCase(), + ); + this.listenerProxy = SessionListener.new() as SessionListener; + this.listenerProxy.bridge = this; + this.communicator.setListenerListener(this.listenerProxy); + } + + // ----- Kotlin listener entry points (main queue) ----------------------------------- + + /** @internal */ + handleStateChange(phase: string, status: string): void { + this.emitAsync(this.stateListeners, { phase: phase as CommunicatorPhase, status }); + } + + /** @internal */ + handleRingEvent(kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number, + frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number): void { + const event = { + kind: kind as RawInputEvent["kind"], containerName, eventType, eventSource, systemExitReasonCode, frameId, + ringInput: ringTick >= 0 ? { tick: ringTick, type: ringType, aux: ringAux, speed: ringSpeed } : undefined, + } as RawInputEvent; + this.emitAsync(this.ringListeners, event); + } + + /** @internal */ + handleBatteryState(battery: number, chargingStatus: number, ringBattery: number, ringChargingStatus: number): void { + this.emitAsync(this.batteryListeners, { battery, chargingStatus, ringBattery, ringChargingStatus }); + } + + /** @internal */ + handleSilentMode(silent: boolean): void { this.emitAsync(this.silentModeListeners, silent); } + + /** @internal */ + handleWearState(wearing: boolean): void { + this.latestWearState = wearing; + this.emitAsync(this.wearStateListeners, wearing); + } + + /** @internal */ + handlePhoneLockState(locked: boolean): void { + this.latestPhoneLockState = locked; + this.emitAsync(this.phoneLockStateListeners, locked); + } + + /** @internal */ + handleEvenAppConflict(message: string): void { this.emitAsync(this.evenAppConflictListeners, message); } + + /** @internal */ + handleFrameMetrics(paintMs: number, transmitMs: number, tileCount: number): void { + const metrics = { paintMs: nonNegativeNumber(paintMs), transmitMs: nonNegativeNumber(transmitMs), tileCount: nonNegativeNumber(tileCount) }; + const waiters = Array.from(this.frameMetricWaiters); + this.frameMetricWaiters.clear(); + for (const waiter of waiters) setTimeout(() => waiter(metrics), 0); + this.emitAsync(this.frameMetricsListeners, metrics); + } + + /** @internal */ + handleFrameFinished(frameId: number, outcome: string): void { this.recordFrameFinished(frameId, outcome); } + + /** @internal */ + handleFirmwareInfo(leftVersion: string, rightVersion: string, extension: string): void { + this.emitAsync(this.firmwareInfoListeners, { leftVersion, rightVersion, extension }); + } + + /** @internal */ + handleAncsAuthorization(authorized: boolean): void { this.emitAsync(this.ancsAuthorizationListeners, authorized); } + + /** @internal */ + handleAncsRelayFrame(frame: Uint8Array): void { this.emitAsync(this.ancsRelayListeners, frame); } + + private emitAsync(listeners: Set<(value: T) => void>, value: T): void { + const snapshot = Array.from(listeners); + setTimeout(() => { + for (const listener of snapshot) listener(value); + }, 0); + } + + /** Same FIFO as the Android bridge: native calls stay ordered; the fast path skips a task hop when idle. */ + private enqueueNativeCall(operation: () => T, inlineWhenIdle = false): Promise { + if (inlineWhenIdle && this.queuedNativeCalls === 0) { + try { + return Promise.resolve(operation()); + } catch (error) { + return Promise.reject(error); + } + } + this.queuedNativeCalls++; + const run = () => + new Promise((resolve, reject) => { + setTimeout(() => { + this.queuedNativeCalls--; + try { + resolve(operation()); + } catch (error) { + reject(error); + } + }, 0); + }); + const result = this.nativeCallQueue.then(run, run); + this.nativeCallQueue = result.then(() => undefined, () => undefined); + return result; + } + + // ----- listeners ------------------------------------------------------------------- + + onStateChange(listener: (state: CommunicatorState) => void): () => void { + this.stateListeners.add(listener); + return () => this.stateListeners.delete(listener); + } + + onRingEvent(listener: (event: RawInputEvent) => void): () => void { + this.ringListeners.add(listener); + return () => this.ringListeners.delete(listener); + } + + onBatteryState(listener: (state: HeadsetBatteryState) => void): () => void { + this.batteryListeners.add(listener); + return () => this.batteryListeners.delete(listener); + } + + onSilentMode(listener: (silent: boolean) => void): () => void { + this.silentModeListeners.add(listener); + return () => this.silentModeListeners.delete(listener); + } + + onWearState(listener: (wearing: boolean) => void): () => void { + this.wearStateListeners.add(listener); + if (this.latestWearState !== null) { + const wearing = this.latestWearState; + setTimeout(() => { if (this.wearStateListeners.has(listener)) listener(wearing); }, 0); + } + return () => this.wearStateListeners.delete(listener); + } + + onPhoneLockState(listener: (locked: boolean) => void): () => void { + this.phoneLockStateListeners.add(listener); + if (this.latestPhoneLockState !== null) { + const locked = this.latestPhoneLockState; + setTimeout(() => { if (this.phoneLockStateListeners.has(listener)) listener(locked); }, 0); + } + return () => this.phoneLockStateListeners.delete(listener); + } + + onEvenAppConflict(listener: (message: string) => void): () => void { + this.evenAppConflictListeners.add(listener); + return () => this.evenAppConflictListeners.delete(listener); + } + + onFrameMetrics(listener: (metrics: FrameMetrics) => void): () => void { + this.frameMetricsListeners.add(listener); + return () => this.frameMetricsListeners.delete(listener); + } + + onFirmwareInfo(listener: (info: FirmwareInfo) => void): () => void { + this.firmwareInfoListeners.add(listener); + return () => this.firmwareInfoListeners.delete(listener); + } + + getNativeCommunicator(): any { + return this.communicator; + } + + private recordFrameFinished(frameId: number, outcome: string): void { + if (!Number.isFinite(frameId) || frameId <= 0) return; + this.finishedFrameOutcomes.set(frameId, outcome); + while (this.finishedFrameOutcomes.size > 128) { + const oldest = this.finishedFrameOutcomes.keys().next().value; + if (oldest === undefined) break; + this.finishedFrameOutcomes.delete(oldest); + } + const waiters = this.frameFinishedWaiters.get(frameId); + if (waiters) { + this.frameFinishedWaiters.delete(frameId); + for (const waiter of waiters) setTimeout(() => waiter(outcome), 0); + } + } + + waitForFrameFinished(frameId: number, timeoutMs: number): Promise { + const known = this.finishedFrameOutcomes.get(frameId); + if (known !== undefined) return Promise.resolve(known); + if (frameId <= 0) return Promise.resolve(null); + const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs))); + return new Promise((resolve) => { + let settled = false; + const onFinished = (outcome: string) => { + if (settled) return; + settled = true; + clearTimeout(timeoutHandle); + resolve(outcome); + }; + let waiters = this.frameFinishedWaiters.get(frameId); + if (!waiters) { + waiters = new Set(); + this.frameFinishedWaiters.set(frameId, waiters); + } + waiters.add(onFinished); + const timeoutHandle = setTimeout(() => { + if (settled) return; + settled = true; + const pending = this.frameFinishedWaiters.get(frameId); + if (pending) { + pending.delete(onFinished); + if (pending.size === 0) this.frameFinishedWaiters.delete(frameId); + } + resolve(null); + }, delayMs); + }); + } + + waitForNextFrameMetrics(timeoutMs: number): Promise { + const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs))); + return new Promise((resolve) => { + let settled = false; + let timeoutHandle: ReturnType | null = null; + const complete = (metrics: FrameMetrics | null) => { + if (settled) return; + settled = true; + if (timeoutHandle !== null) clearTimeout(timeoutHandle); + this.frameMetricWaiters.delete(onMetrics); + resolve(metrics); + }; + const onMetrics = (metrics: FrameMetrics) => complete(metrics); + this.frameMetricWaiters.add(onMetrics); + timeoutHandle = setTimeout(() => complete(null), delayMs); + }); + } + + // ----- session --------------------------------------------------------------------- + + async start(): Promise { + await this.enqueueNativeCall(() => { this.communicator.start(); }); + } + + async setG2ScreenOn(screenOn: boolean): Promise { + await this.enqueueNativeCall(() => this.communicator.setG2ScreenOnScreenOn(Boolean(screenOn))); + } + + async setFirmwareDebugFlags(enabled: boolean): Promise { + await this.enqueueNativeCall(() => this.communicator.setFirmwareDebugFlagsEnabled(Boolean(enabled))); + } + + /** Set lens brightness: Faceclaw auto or a fixed level (clamped to 2–100). */ + async setBrightness(autoAdjust: boolean, level: number): Promise { + await this.enqueueNativeCall(() => this.communicator.setBrightnessAutoAdjustBrightnessLevel(Boolean(autoAdjust), Math.round(level))); + } + + async configureBrightness(p: { auto: boolean; level: number; minimum: number; maximum: number; curve: string; fadeMs: number }): Promise { + await this.enqueueNativeCall(() => this.communicator.configureBrightnessAutoLevelMinimumMaximumCurveFadeMs(p.auto, p.level, p.minimum, p.maximum, p.curve, p.fadeMs)); + } + + async enableWearDetectionAndRequestState(): Promise { + await this.enqueueNativeCall(() => this.communicator.enableWearDetectionAndRequestState()); + } + + isSessionReady(): boolean { + return Boolean(this.communicator.isSessionReady()); + } + + /** The platform's lock/screen signal (the iOS host also observes protected data itself). */ + onPhoneLockSignal(): void { + this.communicator.onPhoneLockSignal(); + } + + // ----- compositor ------------------------------------------------------------------ + + async configureCompositorScreen(width: number, height: number): Promise { + await this.enqueueNativeCall(() => { + this.communicator.configureCompositorScreenWidthHeight(Math.round(width), Math.round(height)); + }); + } + + /** Phone-UI preview of the current composited screen, or null if none yet. */ + getCompositePreview(green = false): ImageSource | null { + const frame = this.communicator.previewComposite(); + if (!frame) return null; + const width = Number(this.communicator.compositeWidth()), height = Number(this.communicator.compositeHeight()); + if (width <= 0 || height <= 0) return null; + return previewPixels(fromData(frame.pixels), width, height, green); + } + + /** Save the current composite as a 4-bit grayscale PNG under Documents; returns the path (empty if none). Cropping is not supported on iOS. */ + saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string { + if (crop) return ""; + const png = this.communicator.screenshotPng(); + if (!png) return ""; + const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`); + File.fromPath(target).writeSync(png); + return target; + } + + /** Screen recording is not available on iOS yet. */ + startScreenRecording(): void {} + + recordScreenFrame(): void {} + + stopScreenRecording(): string { return ""; } + + async configureSurface(id: string, options: SurfaceOptions): Promise { + const transparency = options.transparency === "color-key" ? 1 : 0; + await this.enqueueNativeCall(() => { + this.communicator.configureSurfaceIdXYWidthHeightZOrderTransparency( + id, Math.round(options.x), Math.round(options.y), Math.round(options.width), Math.round(options.height), + Math.round(options.zOrder), transparency, + ); + }); + } + + async removeSurface(id: string): Promise { + await this.enqueueNativeCall(() => { this.communicator.removeSurfaceId(id); }); + } + + async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { + const snapshot = new Uint8Array(bytes); + await this.enqueueNativeCall(() => this.communicator.submitShellSceneBytesPaintMsFrameId(toData(snapshot), Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId))), true); + } + + async setUnderlayDim(belowZOrder: number, factor: number): Promise { + await this.enqueueNativeCall(() => { + this.communicator.setUnderlayDimBelowZOrderFactor256(Math.round(belowZOrder), dimFactor256(factor)); + }); + } + + async setSurfaceVisible(id: string, visible: boolean): Promise { + await this.enqueueNativeCall(() => { this.communicator.setSurfaceVisibleIdVisible(id, Boolean(visible)); }); + } + + async setSurfaceDepth(id: string, depth: number): Promise { + await this.enqueueNativeCall(() => { this.communicator.setSurfaceDepthIdDepth(id, Math.round(depth)); }); + } + + async setScreenBlanked(blanked: boolean): Promise { + await this.enqueueNativeCall(() => { this.communicator.setScreenBlankedBlanked(Boolean(blanked)); }); + } + + async submitSurfaceFrame( + surfaceId: string, + pixels8bpp: Uint8Array, + rect: { x: number; y: number; width: number; height: number }, + fingerprint: string, + paintMs = -1, + frameId = 0, + glyphs: ArrayBuffer | null = null, + ): Promise { + const snapshot = new Uint8Array(pixels8bpp); + const draws = glyphs ? toData(new Uint8Array(glyphs)) : null; + await this.enqueueNativeCall(() => { + this.communicator.submitSurfaceFramePixels8bppSurfaceIdRectXRectYRectWidthRectHeightContentFingerprintPaintMsFrameIdGlyphs( + toData(snapshot), surfaceId, Math.round(rect.x), Math.round(rect.y), Math.round(rect.width), Math.round(rect.height), + fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), draws, + ); + }, true); + } + + // ----- controls -------------------------------------------------------------------- + + async disconnect(): Promise { + await this.enqueueNativeCall(() => this.communicator.disconnect()); + } + + async sendShutdown(exitMode = 0): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.sendShutdownExitMode(exitMode))); + } + + async sendCfwCleanup(): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.sendCfwCleanup())); + } + + async suspendEvenHubSession(): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.suspendEvenHubSession())); + } + + async resumeEvenHubSession(): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.resumeEvenHubSession())); + } + + async setFaceclawWakeLeaseEnabled(enabled: boolean): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.setFaceclawWakeLeaseEnabledEnabled(Boolean(enabled)))); + } + + async awaitEvenHubSessionReady(timeoutMs: number): Promise { + return this.enqueueNativeCall(() => Boolean(this.communicator.awaitEvenHubSessionReadyTimeoutMs(Math.round(nonNegativeNumber(timeoutMs))))); + } + + /** Play a CFW mode-5 kind-4 tone sequence (complete wire payload). */ + async playBuzzerSequence(payload: Uint8Array): Promise { + const snapshot = new Uint8Array(payload); + await this.enqueueNativeCall(() => { this.communicator.playBuzzerSequencePayload(toData(snapshot)); }); + } + + async close(): Promise { + this.setAncsListeners(false); + await this.enqueueNativeCall(() => this.communicator.close()); + } + + // ----- iOS extras: compass, microphone, iPhone notifications (ANCS) ---------------------- + + /** CFW mode-10 compass on behalf of `owner`; the magnetometer runs while any owner wants it. */ + setCompassEnabled(enabled: boolean, owner = "compass"): void { + this.communicator.setCompassEnabledForOwnerOwnerEnable(owner, Boolean(enabled)); + } + + /** Compass heading/calibration events, delivered on the main queue. */ + addCompassListener(listener: (event: CompassEvent) => void): () => void { + const proxy = CompassListener.new() as CompassListener; + proxy.handler = listener; + this.communicator.addCompassListenerListener(proxy); + return () => { this.communicator.removeCompassListenerListener(proxy); }; + } + + /** + * Serialized glasses microphone enable/disable (the voice bridge's + * IosMicrophoneSession contract). A release while an enable awaits its ACK + * still wins: the token check drops the stale packets. + */ + setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise { + const token = ++this.microphoneToken; + const work = this.microphoneWork.catch(() => {}).then(async () => { + if (token !== this.microphoneToken) return; + if (!enabled) { + this.audioListener = null; + await this.enqueueNativeCall(() => this.communicator.stopG2AudioCapture()); + return; + } + if (!this.isSessionReady()) throw new Error("Connect the glasses before starting voice input."); + const proxy = AudioPacketListener.new() as AudioPacketListener; + proxy.handler = (packet) => { if (token === this.microphoneToken && listener) listener(packet); }; + this.audioListener = proxy; + const started = await this.enqueueNativeCall(() => Boolean(this.communicator.startG2AudioCaptureNsDataListener(proxy))); + if (!started) { + this.audioListener = null; + throw new Error("The glasses did not enable their microphone. Reconnect and try again."); + } + }); + this.microphoneWork = work; + return work; + } + + /** Ask CoreBluetooth for ANCS on the right arm's next connection (call before start()). */ + setRequiresAncs(required: boolean): void { + this.communicator.setRequiresAncsRequired(Boolean(required)); + this.setAncsListeners(required); + } + + isAncsAuthorized(): boolean { + return Boolean(this.communicator.isAncsAuthorized()); + } + + onAncsAuthorization(listener: (authorized: boolean) => void): () => void { + this.ancsAuthorizationListeners.add(listener); + return () => this.ancsAuthorizationListeners.delete(listener); + } + + /** Relay frames ('A','N',...) received on the right arm, for the ANCS client. */ + onAncsRelayFrame(listener: (frame: Uint8Array) => void): () => void { + this.ancsRelayListeners.add(listener); + return () => this.ancsRelayListeners.delete(listener); + } + + /** Queue one relay packet for the right arm; the Kotlin side writes it off the JS thread. */ + async writeRawToRight(packet: Uint8Array): Promise { + if (!this.isSessionReady()) throw new Error("Glasses are not connected."); + const snapshot = new Uint8Array(packet); + this.communicator.writeRawToRightPacket(toData(snapshot)); + } + + /** The right arm's usable write payload for relay packets (20 until connected). */ + rightWriteLimit(): number { + return Number(this.communicator.rightWriteLimit()) || 20; + } + + private setAncsListeners(install: boolean): void { + if (install && !this.ancsProxy) { + this.ancsProxy = AncsListener.new() as AncsListener; + this.ancsProxy.bridge = this; + this.communicator.setAncsListenerListener(this.ancsProxy); + } else if (!install && this.ancsProxy) { + this.communicator.setAncsListenerListener(null); + this.ancsProxy = null; + } + } +} diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index e312272c..37b5f479 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -1,5 +1,7 @@ +import type { RingInput } from "../g2/ring-input"; import { ImageSource, Utils } from "@nativescript/core"; import * as frameTimings from "./frame-timings"; +import { JavaDirectBuffer } from "./java-direct-buffer"; declare const com: any; @@ -52,7 +54,7 @@ export type SurfaceOptions = { transparency: "opaque" | "color-key"; }; -export type RawInputEvent = +export type RawInputEvent = { ringInput?: RingInput } & ( | { kind: "list-click"; containerName: string; @@ -113,7 +115,7 @@ export type RawInputEvent = eventSource: number; systemExitReasonCode: number; frameId: number; - }; + }); function nonNegativeNumber(value: number): number { const numeric = Number(value); @@ -131,12 +133,18 @@ export class FaceclawCommunicatorBridge { private javaCallQueue: Promise = Promise.resolve(); /** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */ private queuedJavaCalls = 0; + // Reused Java-side buffers for byte payloads; passing a JS ArrayBuffer to + // Java leaks it (see java-direct-buffer.ts). Loaded inside the queued call, + // immediately before the synchronous Java call that consumes them. + private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480); + private readonly frameDrawsBuffer = new JavaDirectBuffer(); + private readonly shellSceneBuffer = new JavaDirectBuffer(); + private readonly buzzerBuffer = new JavaDirectBuffer(); private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>(); // Recent frame-finished outcomes from the Java side, so waitForFrameFinished // does not race against finishes that land before the wait starts. private readonly finishedFrameOutcomes = new Map(); private readonly frameFinishedWaiters = new Map void>>(); - private readonly logListeners = new Set<(line: string) => void>(); private readonly stateListeners = new Set<(state: CommunicatorState) => void>(); private readonly ringListeners = new Set<(event: RawInputEvent) => void>(); private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>(); @@ -160,9 +168,6 @@ export class FaceclawCommunicatorBridge { addresses.ring ?? "", ); this.listenerProxy = new com.faceclaw.app.FaceclawBleCommunicatorListener({ - onLog: (line: string) => { - this.emitAsync(this.logListeners, String(line)); - }, onStateChange: (phase: string, status: string) => { const state = { phase: String(phase) as CommunicatorPhase, @@ -177,6 +182,7 @@ export class FaceclawCommunicatorBridge { eventSource: number, systemExitReasonCode: number, frameId: number, + ringTick: number, ringType: number, ringAux: number, ringSpeed: number, ) => { const event = { kind: String(kind) as RawInputEvent["kind"], @@ -185,6 +191,8 @@ export class FaceclawCommunicatorBridge { eventSource: Number(eventSource), systemExitReasonCode: Number(systemExitReasonCode), frameId: Number(frameId), + ringInput: Number(ringTick) >= 0 ? { tick: Number(ringTick), type: Number(ringType), + aux: Number(ringAux), speed: Number(ringSpeed) } : undefined, }; frameTimings.logFrame(event.frameId, "input event received on JS side"); this.emitAsync(this.ringListeners, event); @@ -289,11 +297,6 @@ export class FaceclawCommunicatorBridge { return result; } - onLog(listener: (line: string) => void): () => void { - this.logListeners.add(listener); - return () => this.logListeners.delete(listener); - } - onStateChange(listener: (state: CommunicatorState) => void): () => void { this.stateListeners.add(listener); return () => this.stateListeners.delete(listener); @@ -447,11 +450,15 @@ export class FaceclawCommunicatorBridge { await this.enqueueJavaCall(() => this.communicator.setFirmwareDebugFlags(Boolean(enabled))); } - /** Set lens brightness: auto (ambient sensor) or an explicit 0-100 level. */ + /** Set lens brightness: Faceclaw auto or a fixed level (clamped to 2–100). */ async setBrightness(autoAdjust: boolean, level: number): Promise { await this.enqueueJavaCall(() => this.communicator.setBrightness(Boolean(autoAdjust), Math.round(level))); } + async configureBrightness(p: { auto: boolean; level: number; minimum: number; maximum: number; curve: string; fadeMs: number }): Promise { + await this.enqueueJavaCall(() => this.communicator.configureBrightness(p.auto, p.level, p.minimum, p.maximum, p.curve, p.fadeMs)); + } + async enableWearDetectionAndRequestState(): Promise { await this.enqueueJavaCall(() => this.communicator.enableWearDetectionAndRequestState()); } @@ -537,6 +544,14 @@ export class FaceclawCommunicatorBridge { * next composite. How a shell overlay's Layer.dimUnderneath reaches the * window surfaces beneath the shell surface. */ + async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { + const snapshot = new Uint8Array(bytes); + await this.enqueueJavaCall( + () => this.communicator.submitShellScene(this.shellSceneBuffer.load(snapshot), paintMs, frameId), + true, + ); + } + async setUnderlayDim(belowZOrder: number, factor: number): Promise { await this.enqueueJavaCall(() => { this.communicator.setUnderlayDim(Math.round(belowZOrder), dimFactor256(factor)); @@ -550,6 +565,16 @@ export class FaceclawCommunicatorBridge { }); } + /** + * Stereo depth for a surface, applied while it is the topmost visible + * surface and opaquely covers the screen; takes effect at its next frame. + */ + async setSurfaceDepth(id: string, depth: number): Promise { + await this.enqueueJavaCall(() => { + this.communicator.setSurfaceDepth(id, Math.round(depth)); + }); + } + /** * Blank (screen off) or unblank the composited output; retained surface * state survives, so unblanking restores the screen without repaints. @@ -580,15 +605,17 @@ export class FaceclawCommunicatorBridge { */ glyphs: ArrayBuffer | null = null, ): Promise { - // Snapshot because the Java call is deferred; the buffer is passed as an - // ArrayBuffer, which NativeScript marshals to a ByteBuffer without the - // ~150ms per-element copy a byte[] parameter would need. + // Snapshot because the Java call is deferred. The bytes reach Java as a + // ByteBuffer (no ~150ms per-element copy, as a byte[] parameter would + // need), but through a reused Java direct buffer rather than by passing + // the ArrayBuffer itself, which NativeScript never frees + // (java-direct-buffer.ts). const snapshot = new Uint8Array(pixels8bpp); // inlineWhenIdle: this is the frame path, the Java side of it measures // ~5ms (composite + pack), and the caller awaits it either way. await this.enqueueJavaCall(() => { this.communicator.submitSurfaceFrame( - snapshot.buffer, + this.framePixelsBuffer.load(snapshot), surfaceId, Math.round(rect.x), Math.round(rect.y), @@ -597,7 +624,7 @@ export class FaceclawCommunicatorBridge { fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), - glyphs, + this.frameDrawsBuffer.loadOptional(glyphs), ); }, true); } @@ -659,7 +686,7 @@ export class FaceclawCommunicatorBridge { async playBuzzerSequence(payload: Uint8Array): Promise { const snapshot = new Uint8Array(payload); await this.enqueueJavaCall(() => { - this.communicator.playBuzzerSequence(snapshot.buffer); + this.communicator.playBuzzerSequence(this.buzzerBuffer.load(snapshot)); }); } diff --git a/app/native/file-access.ios.ts b/app/native/file-access.ios.ts new file mode 100644 index 00000000..214cf2ec --- /dev/null +++ b/app/native/file-access.ios.ts @@ -0,0 +1,51 @@ +import { knownFolders, path as paths } from '@nativescript/core' +export type DirectoryEntry = { name: string; path: string; isDirectory: boolean; sizeBytes: number; modifiedMs: number } +const fm = NSFileManager.defaultManager +// The iOS Files app exposes Documents; access is limited to Faceclaw's container. +export function hasAllFilesAccess(): boolean { return true } +export function requestAllFilesAccess(): void {} +export function externalStorageRootPath(): string { return knownFolders.documents().path } +export function appFilesDirPath(): string { return knownFolders.documents().path } +export function statPath(path: string): DirectoryEntry | null { + try { + const attrs = fm.attributesOfItemAtPathError(path) + if (!attrs) return null + const modified: any = attrs.objectForKey(NSFileModificationDate) + return { name: path.slice(path.lastIndexOf('/') + 1), path, isDirectory: attrs.objectForKey(NSFileType) === NSFileTypeDirectory, + sizeBytes: Number(attrs.objectForKey(NSFileSize)) || 0, + modifiedMs: modified instanceof Date ? modified.getTime() : Number(modified?.timeIntervalSince1970 ?? 0) * 1000 } + } catch { return null } +} +export function listDirectory(path: string): DirectoryEntry[] | null { + try { + const names = fm.contentsOfDirectoryAtPathError(path), entries: DirectoryEntry[] = [] + for (let i = 0; i < names.count; i++) { const entry = statPath(paths.join(path, names.objectAtIndex(i))); if (entry) entries.push(entry) } + return entries.sort((a, b) => Number(b.isDirectory) - Number(a.isDirectory) || a.name.localeCompare(b.name)) + } catch { return null } +} +export function readBinaryFile(path: string): Uint8Array | null { + const data = NSData.dataWithContentsOfFile(path) + return data ? new Uint8Array(interop.bufferFromData(data)).slice() : null +} +export function writeBinaryFile(path: string, bytes: Uint8Array): boolean { + try { + fm.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(path.slice(0, path.lastIndexOf('/')), true, null) + const copy = bytes.slice(), data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength) + return data.writeToFileAtomically(path, true) + } catch { return false } +} +export function readTextFile(path: string): string | null { + const entry = statPath(path) + if (!entry || entry.isDirectory || entry.sizeBytes > 2_000_000) return null + const data = NSData.dataWithContentsOfFile(path) + const text = data ? NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding) : null + return text == null ? null : String(text).slice(0, 500_000) +} +export function writeTextToDownloads(filename: string, text: string): string | null { + const path = paths.join(knownFolders.documents().path, 'Downloads', filename.slice(filename.lastIndexOf('/') + 1)) + const data = NSString.stringWithString(text).dataUsingEncoding(NSUTF8StringEncoding) + return writeBinaryFile(path, new Uint8Array(interop.bufferFromData(data))) ? path : null +} +export function deletePathRecursively(path: string): void { + try { if (fm.fileExistsAtPath(path)) fm.removeItemAtPathError(path) } catch (error) { console.warn(`Delete failed: ${error}`) } +} diff --git a/app/native/file-access.ts b/app/native/file-access.ts index b79f3d5f..5cf818e5 100644 --- a/app/native/file-access.ts +++ b/app/native/file-access.ts @@ -5,6 +5,7 @@ * reasonable for a sideloaded personal tool). */ import { Utils } from "@nativescript/core"; +import { copyToJavaByteBuffer } from "./java-direct-buffer"; declare const android: any; declare const java: any; @@ -150,10 +151,9 @@ export function writeBinaryFile(path: string, bytes: Uint8Array): boolean { if (parent) parent.mkdirs(); const stream = new java.io.FileOutputStream(file); try { - // An ArrayBuffer marshals to java.nio.ByteBuffer (the same conversion - // submitSurfaceFrame relies on), so write through the channel. - const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); - stream.getChannel().write(buffer); + // Write through the channel from a Java direct buffer: a bulk copy, and + // passing the ArrayBuffer itself would leak it (java-direct-buffer.ts). + stream.getChannel().write(copyToJavaByteBuffer(bytes)); } finally { stream.close(); } diff --git a/app/native/firmware-files.ios.ts b/app/native/firmware-files.ios.ts new file mode 100644 index 00000000..4dd8f2e5 --- /dev/null +++ b/app/native/firmware-files.ios.ts @@ -0,0 +1,11 @@ +import { toData, fromData } from './kotlin-data' +declare const FaceclawCrypto: any +export function firmwareSha256(buffer: ArrayBuffer): string { return String(FaceclawCrypto.sha256(toData(new Uint8Array(buffer)))) } +export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { + if (!toData(new Uint8Array(buffer)).writeToFileAtomically(path, true)) throw new Error('Could not save the verified firmware image.') +} +export function readFirmwareFile(path: string): Uint8Array { + const data = NSData.dataWithContentsOfFile(path) + if (!data) throw new Error('Could not read the prepared firmware image.') + return fromData(data) +} diff --git a/app/native/firmware-files.ts b/app/native/firmware-files.ts new file mode 100644 index 00000000..5517af25 --- /dev/null +++ b/app/native/firmware-files.ts @@ -0,0 +1,8 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + +declare const com: any + +// The image crosses as a fresh Java buffer: passing a JS ArrayBuffer to Java +// leaks it (java-direct-buffer.ts). +export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(copyToJavaByteBuffer(new Uint8Array(buffer)))) } +export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, copyToJavaByteBuffer(new Uint8Array(buffer))) } diff --git a/app/native/firmware-flasher.ios.ts b/app/native/firmware-flasher.ios.ts new file mode 100644 index 00000000..c52e3b80 --- /dev/null +++ b/app/native/firmware-flasher.ios.ts @@ -0,0 +1,124 @@ +import { Application } from '@nativescript/core' +import { CFW_PATCH_SET } from '../g2/firmware/cfw-patches' +import { firmwareSha256, readFirmwareFile } from './firmware-files.ios' +import { iosBluetooth } from './ios-bluetooth' +import { kotlinListener } from './kotlin-listener.ios' +import type { FlashState, FlashProgress } from '../g2/firmware-types' +export type { FlashState, FlashProgress } from '../g2/firmware-types' + +declare const FaceclawKitIosFirmwareFlasher: any, FaceclawKitFaceclawFirmwareFlasherListener: any + +/** + * iOS twin of the Android FirmwareFlasher wrapper. The OTA transfer itself is the shared + * Kotlin OtaFlashFlow over a CoreBluetooth central (FaceclawKitIosFirmwareFlasher); this + * wrapper keeps the iOS-only policy around it: the image must still hash to the prepared + * custom (or pinned stock) firmware, the phone must stay in the foreground with the screen + * awake, and MAC addresses are mapped to peripheral identifiers first. + */ +export class FirmwareFlasher { + private flasher: any = null + private listenerProxy: object | null = null + private readonly logListeners = new Set<(line: string) => void>() + private readonly progressListeners = new Set<(progress: FlashProgress) => void>() + private readonly stateListeners = new Set<(state: FlashState, detail: string) => void>() + private readonly completeListeners = new Set<(success: boolean, detail: string) => void>() + private started = false + private finished = false + private cancelled = false + private foregroundError = '' + private previousIdle = false + + constructor(private readonly addresses: { right: string; left: string }, private readonly path: string) {} + + onLog(listener: (line: string) => void): () => void { + this.logListeners.add(listener) + return () => this.logListeners.delete(listener) + } + + onProgress(listener: (progress: FlashProgress) => void): () => void { + this.progressListeners.add(listener) + return () => this.progressListeners.delete(listener) + } + + onStateChange(listener: (state: FlashState, detail: string) => void): () => void { + this.stateListeners.add(listener) + return () => this.stateListeners.delete(listener) + } + + onComplete(listener: (success: boolean, detail: string) => void): () => void { + this.completeListeners.add(listener) + return () => this.completeListeners.delete(listener) + } + + start(): void { + if (this.started) return + this.started = true + void this.run() + } + + private readonly backgrounded = () => { + this.foregroundError = 'Flashing was interrupted because Faceclaw left the foreground. Keep both lenses charged, return to Faceclaw, and retry both lenses.' + this.cancel() + } + + private async run(): Promise { + this.previousIdle = UIApplication.sharedApplication.idleTimerDisabled + UIApplication.sharedApplication.idleTimerDisabled = true + Application.on(Application.suspendEvent, this.backgrounded) + try { + if (UIApplication.sharedApplication.applicationState !== UIApplicationState.Active) throw new Error('Keep Faceclaw open on the iPhone while flashing.') + this.emit(this.stateListeners, 'validating', '') + const image = readFirmwareFile(this.path) + const hash = firmwareSha256(image.buffer) + if (![CFW_PATCH_SET.outputSha256, CFW_PATCH_SET.baseSha256].includes(hash)) throw new Error('Prepared firmware failed SHA-256 verification. Prepare the firmware again before flashing.') + const identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' }) + if (this.cancelled) throw new Error('Cancelled.') + this.flasher = FaceclawKitIosFirmwareFlasher.alloc().initWithRightAddressLeftAddressFirmwarePath(identifiers.right ?? '', identifiers.left ?? '', this.path) + this.listenerProxy = kotlinListener(FaceclawKitFaceclawFirmwareFlasherListener, { + onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')), + onProgressLensComponentIndexComponentCountBlockIndexBlockCountBytesSentBytesTotal: ( + lens: string, componentIndex: number, componentCount: number, blockIndex: number, blockCount: number, bytesSent: number, bytesTotal: number, + ) => this.emit(this.progressListeners, { + lens: String(lens ?? ''), componentIndex: Number(componentIndex), componentCount: Number(componentCount), + blockIndex: Number(blockIndex), blockCount: Number(blockCount), bytesSent: Number(bytesSent), bytesTotal: Number(bytesTotal), + }), + onStateStateDetail: (state: string, detail: string) => { + // The terminal state is reported by finish() so a foreground/cancel reason can replace the detail. + const name = String(state) as FlashState + if (name !== 'done' && name !== 'error') this.emit(this.stateListeners, name, String(detail ?? '')) + }, + onCompleteSuccessDetail: (success: boolean, detail: string) => this.finish(Boolean(success), String(detail ?? '')), + }) + this.flasher.setListenerListener(this.listenerProxy) + this.flasher.start() + } catch (error) { + this.finish(false, String((error as Error).message ?? error)) + } + } + + private finish(success: boolean, detail: string): void { + if (this.finished) return + this.finished = true + Application.off(Application.suspendEvent, this.backgrounded) + UIApplication.sharedApplication.idleTimerDisabled = this.previousIdle + try { this.flasher?.close() } catch { /* ignore */ } + const message = success ? detail + : this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : detail) + this.emit(this.stateListeners, success ? 'done' : 'error', message) + this.emit(this.completeListeners, success, message) + } + + cancel(): void { + this.cancelled = true + try { this.flasher?.cancel() } catch { /* ignore */ } + // Before the Kotlin flow exists (validation, address resolution) nothing will report completion. + if (!this.flasher) this.finish(false, 'Cancelled.') + } + + close(): void { this.cancel() } + + private emit(listeners: Set<(...args: A) => void>, ...args: A): void { + const snapshot = Array.from(listeners) + setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0) + } +} diff --git a/app/native/firmware-flasher.ts b/app/native/firmware-flasher.ts index cd75d0b7..ed9ac5f9 100644 --- a/app/native/firmware-flasher.ts +++ b/app/native/firmware-flasher.ts @@ -2,18 +2,8 @@ import { Utils } from "@nativescript/core"; declare const com: any; -export type FlashState = "validating" | "connecting" | "flashing" | "rebooting" | "done" | "error"; - -export type FlashProgress = { - lens: string; - componentIndex: number; - componentCount: number; - blockIndex: number; - blockCount: number; - /** Bytes flashed so far across the whole lens image — the size-weighted progress signal. */ - bytesSent: number; - bytesTotal: number; -}; +import type { FlashState, FlashProgress } from "../g2/firmware-types"; +export type { FlashState, FlashProgress } from "../g2/firmware-types"; /** * TS wrapper around the native FaceclawFirmwareFlasher — streams a verified diff --git a/app/native/flash-prompt-communicator.ios.ts b/app/native/flash-prompt-communicator.ios.ts new file mode 100644 index 00000000..62e918b3 --- /dev/null +++ b/app/native/flash-prompt-communicator.ios.ts @@ -0,0 +1,98 @@ +import { iosBluetooth } from './ios-bluetooth' +import { kotlinListener } from './kotlin-listener.ios' +import type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator' +export type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator' + +declare const FaceclawKitIosFlashPrompt: any, FaceclawKitFaceclawFlashPromptListener: any + +function normalizePercent(value: number): number | null { + const n = Number(value) + return Number.isFinite(n) && n >= 0 ? n : null +} + +/** + * iOS twin of the Android FlashPromptCommunicator wrapper over the shared Kotlin + * FlashPromptFlow (FaceclawKitIosFlashPrompt): connects and authenticates both arms, shows + * the pre-flash Yes/No page on the glasses (unless skipPrompt) and reads each arm's battery. + */ +export class FlashPromptCommunicator { + private communicator: any = null + private listenerProxy: object | null = null + private readonly logListeners = new Set<(line: string) => void>() + private readonly stateListeners = new Set<(state: FlashPromptState, detail: string) => void>() + private readonly resultListeners = new Set<(approved: boolean) => void>() + private readonly batteryListeners = new Set<(battery: FlashPromptBattery) => void>() + private started = false + private closed = false + + constructor( + private readonly addresses: { right: string; left: string }, + private readonly warningText: string, + private readonly options?: { skipPrompt?: boolean }, + ) {} + + onLog(listener: (line: string) => void): () => void { + this.logListeners.add(listener) + return () => this.logListeners.delete(listener) + } + + onStateChange(listener: (state: FlashPromptState, detail: string) => void): () => void { + this.stateListeners.add(listener) + return () => this.stateListeners.delete(listener) + } + + onBattery(listener: (battery: FlashPromptBattery) => void): () => void { + this.batteryListeners.add(listener) + return () => this.batteryListeners.delete(listener) + } + + onResult(listener: (approved: boolean) => void): () => void { + this.resultListeners.add(listener) + return () => this.resultListeners.delete(listener) + } + + start(): void { + if (this.started) return + this.started = true + void this.run() + } + + private async run(): Promise { + let identifiers: Record + try { + this.emit(this.stateListeners, 'connecting', '') + identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' }) + } catch (error) { + if (!this.closed) this.emit(this.stateListeners, 'error', String((error as Error).message ?? error)) + return + } + if (this.closed) return + this.communicator = FaceclawKitIosFlashPrompt.alloc().initWithRightAddressLeftAddressWarningTextSkipPrompt( + identifiers.right ?? '', identifiers.left ?? '', this.warningText, Boolean(this.options?.skipPrompt)) + this.listenerProxy = kotlinListener(FaceclawKitFaceclawFlashPromptListener, { + onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')), + onStateStateDetail: (state: string, detail: string) => + this.emit(this.stateListeners, String(state) as FlashPromptState, String(detail ?? '')), + onBatteryRightPercentLeftPercent: (rightPercent: number, leftPercent: number) => + this.emit(this.batteryListeners, { right: normalizePercent(rightPercent), left: normalizePercent(leftPercent) }), + onResultApproved: (approved: boolean) => this.emit(this.resultListeners, Boolean(approved)), + }) + this.communicator.setListenerListener(this.listenerProxy) + this.communicator.start() + } + + cancel(): void { + this.closed = true + try { this.communicator?.cancel() } catch { /* ignore */ } + } + + close(): void { + this.closed = true + try { this.communicator?.close() } catch { /* ignore */ } + } + + private emit(listeners: Set<(...args: A) => void>, ...args: A): void { + const snapshot = Array.from(listeners) + setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0) + } +} diff --git a/app/native/font-files.ts b/app/native/font-files.ts index 8d0deb30..d27d976c 100644 --- a/app/native/font-files.ts +++ b/app/native/font-files.ts @@ -1,17 +1,16 @@ /** * Font-file text rendering for the on-glasses font previewer, backed by the - * Java FontFileRenderer (Android Typeface/Paint, so shaping, kerning, and - * line breaking come from minikin/HarfBuzz). Rendered text arrives as + * native font renderer (Android Paint / iOS Core Text). Rendered text arrives as * antialiased 8bpp coverage, which the compositor quantizes to the display's * 16 shades. */ import { GrayImage } from "../graphics/image"; import { grayImageFromPacket } from "./image-files"; -declare const com: any; +import { fontRenderer } from "./font-renderer"; declare const global: any; -/** Font file formats Android's Typeface can load. */ +/** Font file formats supported by the native renderers. */ const FONT_FILE = /\.(ttf|otf|ttc)$/i; export function isFontFile(name: string): boolean { @@ -37,9 +36,9 @@ export const DEFAULT_FONT_GAMMA = 1.0; /** Whether the font file parses as a loadable font. */ export function canLoadFontFile(path: string): boolean { - if (!global.isAndroid) return false; + if (!global.isAndroid && !global.isIOS) return false; try { - return !!com.faceclaw.app.FontFileRenderer.canLoadFont(path); + return !!fontRenderer.canLoadFont(path); } catch { return false; } @@ -50,9 +49,9 @@ export function canLoadFontFile(path: string): boolean { * unavailable (caller should fall back to the filename). */ export function fontFileDisplayName(path: string): { family: string; style: string } | null { - if (!global.isAndroid) return null; + if (!global.isAndroid && !global.isIOS) return null; try { - const raw = String(com.faceclaw.app.FontFileRenderer.getFontName(path) ?? ""); + const raw = String(fontRenderer.getFontName(path) ?? ""); if (!raw) return null; const [family, style] = raw.split("\n"); if (!family) return null; @@ -64,9 +63,9 @@ export function fontFileDisplayName(path: string): { family: string; style: stri /** Line metrics at a pixel size; null when the font cannot be loaded. */ export function fontFileMetrics(path: string, sizePx: number): FontFileMetrics | null { - if (!global.isAndroid) return null; + if (!global.isAndroid && !global.isIOS) return null; try { - const raw = String(com.faceclaw.app.FontFileRenderer.getFontMetrics(path, sizePx) ?? ""); + const raw = String(fontRenderer.getFontMetrics(path, sizePx) ?? ""); const parts = raw.split(" ").map((part) => parseInt(part, 10)); if (parts.length !== 3 || parts.some((part) => !Number.isFinite(part))) return null; return { ascent: parts[0]!, descent: parts[1]!, lineGap: parts[2]! }; @@ -86,9 +85,9 @@ export function renderFontFileText( sizePx: number, gamma = DEFAULT_FONT_GAMMA, ): GrayImage | null { - if (!global.isAndroid) return null; + if (!global.isAndroid && !global.isIOS) return null; try { - return grayImageFromPacket(com.faceclaw.app.FontFileRenderer.renderText(path, text, sizePx, gamma)); + return grayImageFromPacket(fontRenderer.renderText(path, text, sizePx, gamma)); } catch (error) { console.warn(`renderFontFileText failed for ${path}: ${error}`); return null; @@ -96,7 +95,7 @@ export function renderFontFileText( } /** - * Render a paragraph wrapped to maxWidth (StaticLayout line breaking), + * Render a paragraph wrapped to maxWidth (native line breaking), * truncated with an ellipsis past maxLines. */ export function renderFontFileWrapped(opts: { @@ -107,10 +106,10 @@ export function renderFontFileWrapped(opts: { maxLines: number; gamma?: number; }): GrayImage | null { - if (!global.isAndroid) return null; + if (!global.isAndroid && !global.isIOS) return null; try { return grayImageFromPacket( - com.faceclaw.app.FontFileRenderer.renderWrapped( + fontRenderer.renderWrapped( opts.path, opts.text, opts.sizePx, diff --git a/app/native/font-renderer.ios.ts b/app/native/font-renderer.ios.ts new file mode 100644 index 00000000..a828e24f --- /dev/null +++ b/app/native/font-renderer.ios.ts @@ -0,0 +1,13 @@ +declare const FaceclawFontRenderer: any +function bytes(data: NSData | null): Uint8Array { + return data ? new Uint8Array(interop.bufferFromData(data)).slice() : new Uint8Array() +} +export const fontRenderer = { + canLoadFont: (path: string): boolean => !!FaceclawFontRenderer.canLoadFont(path), + getFontName: (path: string): string => String(FaceclawFontRenderer.getFontName(path) ?? ''), + getFontMetrics: (path: string, size: number): string => String(FaceclawFontRenderer.getFontMetricsSize(path, size) ?? ''), + measureTextExact: (path: string, text: string, size: number): number => Number(FaceclawFontRenderer.measureTextExactTextSize(path, text, size)), + renderGlyphCell: (path: string, size: number, cp: number, gamma: number): Uint8Array => bytes(FaceclawFontRenderer.renderGlyphCellSizeCodePointGamma(path, size, cp, gamma)), + renderText: (path: string, text: string, size: number, gamma: number): Uint8Array => bytes(FaceclawFontRenderer.renderTextTextSizeGamma(path, text, size, gamma)), + renderWrapped: (path: string, text: string, size: number, width: number, gamma: number, lines: number): Uint8Array => bytes(FaceclawFontRenderer.renderWrappedTextSizeWidthGammaMaxLines(path, text, size, width, gamma, lines)), +} diff --git a/app/native/font-renderer.ts b/app/native/font-renderer.ts new file mode 100644 index 00000000..fc1a8063 --- /dev/null +++ b/app/native/font-renderer.ts @@ -0,0 +1,12 @@ +/** Platform boundary for font rasterization; layout and glyph caches stay shared. */ +import { toUint8Array } from '../util/array-util' +declare const com: any +export const fontRenderer = { + canLoadFont: (path: string): boolean => !!com.faceclaw.app.FontFileRenderer.canLoadFont(path), + getFontName: (path: string): string => String(com.faceclaw.app.FontFileRenderer.getFontName(path) ?? ''), + getFontMetrics: (path: string, size: number): string => String(com.faceclaw.app.FontFileRenderer.getFontMetrics(path, size) ?? ''), + measureTextExact: (path: string, text: string, size: number): number => Number(com.faceclaw.app.FontFileRenderer.measureTextExact(path, text, size)), + renderGlyphCell: (path: string, size: number, cp: number, gamma: number): Uint8Array => toUint8Array(com.faceclaw.app.FontFileRenderer.renderGlyphCell(path, size, cp, gamma)), + renderText: (path: string, text: string, size: number, gamma: number): Uint8Array => toUint8Array(com.faceclaw.app.FontFileRenderer.renderText(path, text, size, gamma)), + renderWrapped: (path: string, text: string, size: number, width: number, gamma: number, lines: number): Uint8Array => toUint8Array(com.faceclaw.app.FontFileRenderer.renderWrapped(path, text, size, width, gamma, lines)), +} diff --git a/app/native/frame-timings.ts b/app/native/frame-timings.ts index 56e2a5e1..294349e8 100644 --- a/app/native/frame-timings.ts +++ b/app/native/frame-timings.ts @@ -1,6 +1,6 @@ /** * TS-side wrapper for the Java FrameTimings singleton (per-frame latency - * instrumentation; see FrameTimings.java). Frame ID 0 means "no frame" and is + * instrumentation; see FrameTimings.kt). Frame ID 0 means "no frame" and is * ignored by every method, so callers never need to null-check. * * Frames form a tree: pass the causing frame's ID as `parentFrameId` when one diff --git a/app/native/g2mirror-client.ts b/app/native/g2mirror-client.ts index 038dfe03..c0b1274d 100644 --- a/app/native/g2mirror-client.ts +++ b/app/native/g2mirror-client.ts @@ -1,3 +1,4 @@ +import { openSocket } from "./socket"; import { toUint8Array } from "../util/array-util"; declare const com: any; @@ -181,7 +182,7 @@ export class G2MirrorClient { const generation = ++this.connectionGeneration; const url = `${this.options.secure ? "wss" : "ws"}://${this.options.host}:${this.options.port}`; this.setState("connecting", `Connecting to ${this.options.host}:${this.options.port}...`); - this.listenerProxy = new com.faceclaw.app.FaceclawWebSocketListener({ + this.listenerProxy = { onOpen: () => { if (this.stopped || generation !== this.connectionGeneration) return; this.setState("connecting", "Authenticating..."); @@ -206,9 +207,9 @@ export class G2MirrorClient { if (this.stopped || generation !== this.connectionGeneration) return; this.handleConnectionLost(`Connection failed: ${shortenError(String(message))}`); }, - }); + }; try { - this.ws = new com.faceclaw.app.FaceclawWebSocket(url, this.listenerProxy, null, null); + this.ws = openSocket(url, this.listenerProxy); } catch (error) { this.ws = null; this.handleConnectionLost(`Connection failed: ${shortenError(String((error as Error)?.message ?? error))}`); @@ -325,7 +326,9 @@ export class G2MirrorClient { */ submitInput(text: string): void { if (this.phase !== "attached") return; - const textByteLength = new java.lang.String(text).getBytes("UTF-8").length; + const textByteLength = global.isIOS + ? NSString.stringWithString(text).lengthOfBytesUsingEncoding(NSUTF8StringEncoding) + : new java.lang.String(text).getBytes("UTF-8").length; this.send({ type: "input", data: encodeBase64Utf8(`${text}\r`), @@ -571,11 +574,19 @@ export class G2MirrorClient { function decodeBase64(data: string): Uint8Array { if (!data) return new Uint8Array(0); + if (global.isIOS) { + const decoded = NSData.alloc().initWithBase64EncodedStringOptions(data, 0 as NSDataBase64DecodingOptions); + return decoded ? new Uint8Array(interop.bufferFromData(decoded)).slice() : new Uint8Array(); + } return toUint8Array(android.util.Base64.decode(data, android.util.Base64.DEFAULT)); } function decodeBase64Utf8(data: string): string { if (!data) return ""; + if (global.isIOS) { + const decoded = NSData.alloc().initWithBase64EncodedStringOptions(data, 0 as NSDataBase64DecodingOptions); + return decoded ? String(NSString.alloc().initWithDataEncoding(decoded, NSUTF8StringEncoding) ?? "") : ""; + } const bytes = android.util.Base64.decode(data, android.util.Base64.DEFAULT); return String(new java.lang.String(bytes, "UTF-8")); } @@ -587,6 +598,7 @@ function stripAnsi(text: string): string { } function encodeBase64Utf8(text: string): string { + if (global.isIOS) return NSString.stringWithString(text).dataUsingEncoding(NSUTF8StringEncoding).base64EncodedStringWithOptions(0 as NSDataBase64EncodingOptions); const bytes = new java.lang.String(text).getBytes("UTF-8"); return String(android.util.Base64.encodeToString(bytes, android.util.Base64.NO_WRAP)); } diff --git a/app/native/gray-image-preview.ts b/app/native/gray-image-preview.ts index 690c1e4b..fc8b1656 100644 --- a/app/native/gray-image-preview.ts +++ b/app/native/gray-image-preview.ts @@ -1,16 +1,17 @@ import { ImageSource } from "@nativescript/core"; import { type GrayImage } from "../graphics/image"; +import { copyToJavaByteBuffer } from "./java-direct-buffer"; declare const com: any; const PREVIEW_BRIGHTEN_GAMMA = 0.7; /** - * Build the phone-UI preview for a frame. The pixel buffer is passed to Java - * as an ArrayBuffer (marshalled to a ByteBuffer) and expanded to ARGB there; - * doing the per-pixel work in JS or copying element-by-element across the - * bridge costs ~150ms per frame. + * Build the phone-UI preview for a frame. The pixels are copied into a Java + * direct ByteBuffer and expanded to ARGB there; doing the per-pixel work in JS + * or copying element-by-element across the bridge costs ~150ms per frame, and + * passing the ArrayBuffer itself would leak it (java-direct-buffer.ts). */ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { if (!global.isAndroid) { @@ -19,7 +20,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { const baked = image.withDrawsBaked(); const bitmap = com.faceclaw.app.PreviewBitmapUtil.fromGray( - baked.pixels.buffer, + copyToJavaByteBuffer(baked.pixels), baked.width, baked.height, PREVIEW_BRIGHTEN_GAMMA, diff --git a/app/native/icon-image.ios.ts b/app/native/icon-image.ios.ts new file mode 100644 index 00000000..40282046 --- /dev/null +++ b/app/native/icon-image.ios.ts @@ -0,0 +1,9 @@ +import { grayImageFromPacket } from './image-files'; +import { fromData } from './kotlin-data'; +import type { GrayImage } from '../graphics/image'; +declare const FaceclawGraphics: any; + +export function loadIconImage(path: string, maxWidth: number, maxHeight: number): GrayImage | null { + const data = FaceclawGraphics.decodeImageFileMaxWidthMaxHeight(path, Math.round(maxWidth), Math.round(maxHeight)); + return data ? grayImageFromPacket(fromData(data)) : null; +} diff --git a/app/native/icon-image.ts b/app/native/icon-image.ts new file mode 100644 index 00000000..5e2145e4 --- /dev/null +++ b/app/native/icon-image.ts @@ -0,0 +1,3 @@ +import { loadImageFileAsGray } from './image-files'; +/** UI artwork uses flat grayscale, preserving its source aspect ratio. */ +export const loadIconImage = loadImageFileAsGray; diff --git a/app/native/image-bytes.ios.ts b/app/native/image-bytes.ios.ts new file mode 100644 index 00000000..d5cac1c7 --- /dev/null +++ b/app/native/image-bytes.ios.ts @@ -0,0 +1,12 @@ +import { GrayImage } from '../graphics/image' +import { toData, fromData } from './kotlin-data' +declare const FaceclawGraphics: any + +/** Native image decoding and grayscale conversion, safe on a rendering worker. */ +export function decodeImageBytes(bytes: ArrayBuffer, width: number, height: number): GrayImage | null { + const data = FaceclawGraphics.decodeImageWidthHeight(toData(new Uint8Array(bytes)), width, height) + if (!data) return null + const image = new GrayImage(width, height, 0) + image.pixels.set(fromData(data)) + return image +} diff --git a/app/native/image-bytes.ts b/app/native/image-bytes.ts new file mode 100644 index 00000000..cb3f5a0b --- /dev/null +++ b/app/native/image-bytes.ts @@ -0,0 +1,9 @@ +import { grayImageFromPacket } from './image-files' +import { type GrayImage } from '../graphics/image' +import { copyToJavaByteBuffer } from './java-direct-buffer' +declare const com: any + +/** The encoded bytes cross as a fresh Java buffer; passing the ArrayBuffer leaks it (java-direct-buffer.ts). */ +export function decodeImageBytes(bytes: ArrayBuffer, width: number, height: number): GrayImage | null { + return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(copyToJavaByteBuffer(new Uint8Array(bytes)), width, height)) +} diff --git a/app/native/ios-bluetooth.ts b/app/native/ios-bluetooth.ts new file mode 100644 index 00000000..b1016b29 --- /dev/null +++ b/app/native/ios-bluetooth.ts @@ -0,0 +1,136 @@ +import { ApplicationSettings } from '@nativescript/core' +import { identifyIosPeripheral, type IosAdvertisement, type IosDevice } from '../g2/ios-peripheral-identity' +import { hexToBytes } from '../util/hex-util' +import { kotlinListener } from './kotlin-listener.ios' + +declare const FaceclawKitIosBleCentral: any, FaceclawKitIosProtocolPlatform: any, FaceclawKitIosBleScanListener: any + +/** CBManagerState raw values reported by the Kotlin central. */ +export const BLE_STATE_UNSUPPORTED = 2, BLE_STATE_UNAUTHORIZED = 3, BLE_STATE_POWERED_OFF = 4, BLE_STATE_POWERED_ON = 5 + +export type BluetoothEvent = + | { kind: 'state'; state: number; authorization: number } + | { kind: 'advertisement'; identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean } + | { kind: 'device'; device: IosDevice } + | { kind: 'scan-started' } + | { kind: 'scan-stopped' } + +/** + * Scanning and MAC-address-to-identifier resolution over the shared Kotlin CoreBluetooth + * central (FaceclawKitIosBleCentral). GATT sessions no longer pass through here: the + * session and stock-firmware flows own their own Kotlin centrals. What stays in TypeScript + * is the Even classification of advertisements and the persisted identifier map, which the + * Android address model (MACs) needs on iOS. + */ +export class IosBluetooth { + private readonly native = FaceclawKitIosBleCentral.alloc().initWithPlatform(FaceclawKitIosProtocolPlatform.shared) + private readonly listeners = new Set<(event: BluetoothEvent) => void>() + readonly devices = new Map() + private scanTimer: ReturnType | null = null + private scanGeneration = 0 + private readonly scanListener: object + state = 0 + authorization = 0 + scanning = false + constructor() { + this.scanListener = kotlinListener(FaceclawKitIosBleScanListener, { + onBluetoothStateStateAuthorization: (state: number, authorization: number) => { + this.state = Number(state); this.authorization = Number(authorization) + this.emit({ kind: 'state', state: this.state, authorization: this.authorization }) + }, + onAdvertisementIdentifierNameManufacturerDataHexRssiConnectable: (identifier: string, name: string, manufacturerData: string, rssi: number, connectable: boolean) => { + // The Kotlin central already merges split advertisements and bounds its cache. + const raw: IosAdvertisement = { identifier: String(identifier), name: String(name ?? ''), manufacturerData: String(manufacturerData ?? ''), rssi: Number(rssi), connectable: connectable !== false } + this.emit({ kind: 'advertisement', ...raw }) + const device = identifyIosPeripheral(raw) + if (device) { + this.devices.set(device.address, device) + ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role })) + this.emit({ kind: 'device', device }) + } + }, + }) + this.native.setScanListenerListener(this.scanListener) + } + onEvent(listener: (event: BluetoothEvent) => void): () => void { + this.listeners.add(listener); return () => { this.listeners.delete(listener) } + } + private emit(event: BluetoothEvent): void { for (const listener of [...this.listeners]) listener(event) } + /** Creates the central (first use shows the Bluetooth permission prompt) without blocking the JS thread. */ + private ensureCentral(): void { + // A zero timeout returns immediately after creating the central; the state arrives + // through the scan listener on the main queue. + this.native.waitForPoweredOnTimeoutMs(0) + } + ensureReady(): Promise { + if (this.state === BLE_STATE_POWERED_ON) return Promise.resolve() + return new Promise((resolve, reject) => { + const finish = (error?: Error) => { clearTimeout(timer); off(); error ? reject(error) : resolve() } + const off = this.onEvent(event => { + if (event.kind !== 'state') return + if (event.state === BLE_STATE_POWERED_ON) finish() + else if (event.state === BLE_STATE_UNSUPPORTED) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.')) + else if (event.state === BLE_STATE_UNAUTHORIZED) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.')) + else if (event.state === BLE_STATE_POWERED_OFF) finish(new Error('Turn on Bluetooth, then try again.')) + }) + const timer = setTimeout(() => finish(new Error('Waiting for Bluetooth permission timed out. Try again after allowing Bluetooth.')), 30_000) + this.ensureCentral() + }) + } + async startScan(durationMs = 12_000): Promise { + this.stopScan() + const generation = this.scanGeneration + await this.ensureReady() + if (generation !== this.scanGeneration) throw new Error('Bluetooth scan cancelled') + this.scanning = true; this.native.startScan() + this.scanTimer = setTimeout(() => this.stopScan(), durationMs) + this.emit({ kind: 'scan-started' }) + } + stopScan(): void { + ++this.scanGeneration + if (this.scanTimer) clearTimeout(this.scanTimer) + this.scanTimer = null + const wasScanning = this.scanning; this.scanning = false + if (wasScanning) { this.native.stopScan(); this.emit({ kind: 'scan-stopped' }) } + } + /** Peripheral identifiers for the stored MAC addresses, scanning for any that are not remembered. */ + async resolveDevices(addresses: { left: string; right: string; ring: string }): Promise> { + const generation = this.scanGeneration + await this.ensureReady() + if (generation !== this.scanGeneration) throw new Error('Bluetooth connection cancelled') + const result: Record = {} + for (const role of ['left', 'right', 'ring'] as const) { + const address = addresses[role] + if (!address) continue + const live = this.devices.get(address) + if (live && live.role !== role) throw new Error(`${address} advertises as ${live.role}, not ${role}. Check Configure devices.`) + try { + const saved = JSON.parse(ApplicationSettings.getString(`ios.ble.peripheral.${address}`, '{}')) + if (saved.role === role && typeof saved.identifier === 'string') result[role] = saved.identifier + } catch { /* A fresh scan can recover malformed settings. */ } + } + const missing = () => (['left', 'right'] as const).filter(role => !result[role]) + if (!missing().length && (!addresses.ring || result.ring)) return result + await this.startScan(15_000) + await new Promise((resolve, reject) => { + const off = this.onEvent(event => { + if (event.kind === 'device') { + const device = event.device + for (const role of ['left', 'right', 'ring'] as const) { + if (addresses[role] === device.address && device.role === role) result[role] = device.identifier + } + if (!missing().length && (!addresses.ring || result.ring)) { off(); this.stopScan(); resolve() } + } else if (event.kind === 'scan-stopped') { + off() + if (missing().length) reject(new Error(`Could not find the ${missing().join(' and ')} arm. Wake the glasses, disconnect other apps, and scan again.`)) + else resolve() // An optional missing ring must not block the glasses. + } + }) + }) + return result + } + forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) } +} +let instance: IosBluetooth | null = null +export function iosBluetooth(): IosBluetooth { return instance ??= new IosBluetooth() } +export { hexToBytes } diff --git a/app/native/ios-graphics.ts b/app/native/ios-graphics.ts new file mode 100644 index 00000000..6052d625 --- /dev/null +++ b/app/native/ios-graphics.ts @@ -0,0 +1,21 @@ +import { ImageSource } from '@nativescript/core' +import { GrayImage } from '../graphics/image' + +declare const FaceclawGraphics: any + +export function previewPixels(pixels: Uint8Array, width: number, height: number, green = false): ImageSource { + // NSData copies the buffer; the UIImage owns its pixels after this call. + const copy = new Uint8Array(pixels) + const data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength) + const native = FaceclawGraphics.previewWidthHeightGreen(data, width, height, green) + if (!native) throw new Error('Could not create preview image') + return new ImageSource(native) +} + +export function rasterizeSvg(svg: string, size: number): GrayImage | null { + const data = FaceclawGraphics.renderSVGSize(svg, size) + if (!data) return null + const image = new GrayImage(size, size, 0) + image.pixels.set(new Uint8Array(interop.bufferFromData(data))) + return image +} diff --git a/app/native/ios-navigation-sensors.ts b/app/native/ios-navigation-sensors.ts new file mode 100644 index 00000000..36580bd1 --- /dev/null +++ b/app/native/ios-navigation-sensors.ts @@ -0,0 +1,56 @@ +import { LocationTracker } from './location-tracker.ios' +import { ensureFineLocationPermission } from './location-permissions.ios' +import { addCompassListener, setCompassEnabled } from './compass.ios' +import { getDeclinationDegrees, refreshDeclination } from '../apps/compass/declination' +import { type NavigationSensorEvent, type NavigationSensorRequest } from '../apps/navigate/navigation-sensors-messages' + +/** Core Location stays on main; the shared Navigate worker receives only JSON. */ +export class IosNavigationSensors { + private generation = 0 + private id = 0 + private tracker: LocationTracker | null = null + private offCompass: (() => void) | null = null + private readonly owner = 'worker:navigate' + constructor(private readonly reply: (event: NavigationSensorEvent) => void) {} + handle(request: NavigationSensorRequest): void { + if (request.action === 'compass') { + if (request.enabled && !this.offCompass) { + refreshDeclination() + this.offCompass = addCompassListener(event => this.reply({ kind: 'compass', event, declination: getDeclinationDegrees() })) + } else if (!request.enabled) { this.offCompass?.(); this.offCompass = null } + setCompassEnabled(request.enabled, this.owner) + } else if (request.action === 'stop') { + if (request.id === this.id) this.stopLocation() + } else { + this.stopLocation(); this.id = request.id + const generation = this.generation + void ensureFineLocationPermission().then(granted => { + if (generation !== this.generation) return + if (!granted) throw new Error('Precise location is required for navigation. Enable it in the phone’s Settings, then retry.') + refreshDeclination() + this.tracker = new LocationTracker({ + onLocation: location => { + if (generation === this.generation) this.reply({ kind: 'location', id: request.id, location }) + }, + onError: message => { + if (generation !== this.generation) return + this.stopLocation(); this.reply({ kind: 'error', id: request.id, message }) + }, + }) + this.tracker.start(request.intervalMs) + }).catch(error => { + if (generation !== this.generation) return + this.stopLocation(); this.reply({ kind: 'error', id: request.id, message: String(error.message ?? error) }) + }) + } + } + private stopLocation(): void { + ++this.generation; this.id = 0 + this.tracker?.stop(); this.tracker = null + } + stop(): void { + this.stopLocation() + this.offCompass?.(); this.offCompass = null + setCompassEnabled(false, this.owner) + } +} diff --git a/app/native/ios-voice-input.ts b/app/native/ios-voice-input.ts new file mode 100644 index 00000000..5b93f85b --- /dev/null +++ b/app/native/ios-voice-input.ts @@ -0,0 +1 @@ +export { voiceControlBridge as iosVoiceInput } from './voice-control.ios' diff --git a/app/native/java-direct-buffer.ts b/app/native/java-direct-buffer.ts new file mode 100644 index 00000000..b882fd71 --- /dev/null +++ b/app/native/java-direct-buffer.ts @@ -0,0 +1,112 @@ +/** + * Hands bytes to a Java `java.nio.ByteBuffer` parameter without leaking them. + * + * Why this exists: NativeScript Android (9.0.4 through at least 9.1.1) + * converts a JS ArrayBuffer or typed array passed to a Java method by wrapping + * its backing store in `NewDirectByteBuffer` and taking a JNI global ref that is + * never deleted (JsArgConverter.cpp, `buffer = env.NewGlobalRef(buffer)`). The + * ByteBuffer is also linked to the JS ArrayBuffer as its native counterpart. + * The leaked global ref keeps the ByteBuffer reachable forever, so the + * runtime's GC handshake (Runtime.makeInstanceWeakAndCheckIfAlive) always says + * "alive", the JS finalizer re-arms, and V8 never frees the ArrayBuffer. Every + * call leaked its whole buffer plus one global ref. + * + * On the frame path that meant a full surface (576x260, 640x452 or 640x480 + * bytes) per submitted frame, including frames the Java side then discards as + * unchanged. Measured on a Galaxy Z Fold7, native-heap growth matched frame + * bytes submitted to within a few percent, and Samsung's Heimdall memory + * manager killed the app at about 6.4 GB. ART also aborts the process once + * 51,200 global refs are held, so the ref leak is a second ceiling behind the + * memory one. + * + * The workaround keeps the conversion out of the call: the bytes are copied + * into a Java-allocated direct ByteBuffer that is reused, through an + * ArrayBuffer view made once with `ArrayBuffer.from` (an external backing + * store over the Java memory, no global ref), and the Java ByteBuffer object + * itself is passed. A JS proxy for an existing Java object takes the ordinary + * object path, which does not leak. + * + * Contract: the buffer returned by load() is only valid until the next load() + * on the same instance, so the Java callee must consume it synchronously and + * not keep a reference. The Kotlin consumers (SurfaceCompositor via + * AndroidByteReader, playBuzzerSequence, ShellScene.decode) copy or parse + * before returning, which satisfies this. One instance belongs to one JS + * context (main thread or one worker); instances are not shared across + * isolates. + */ + +declare const java: any; + +/** Minimal surface of java.nio.ByteBuffer this module uses. */ +export interface JavaByteBufferLike { + clear(): unknown; + limit(newLimit: number): unknown; +} + +/** How to allocate a direct buffer and view its memory; injectable for tests. */ +export interface DirectBufferBackend { + allocateDirect(capacity: number): JavaByteBufferLike; + viewOf(buffer: JavaByteBufferLike): Uint8Array; +} + +const nativeScriptBackend: DirectBufferBackend = { + allocateDirect: (capacity) => java.nio.ByteBuffer.allocateDirect(capacity), + viewOf: (buffer) => new Uint8Array((ArrayBuffer as any).from(buffer)), +}; + +/** Capacity grows in steps of this many bytes, so a slowly growing payload does not reallocate every call. */ +const CAPACITY_STEP_BYTES = 4096; + +export class JavaDirectBuffer { + private javaBuffer: JavaByteBufferLike | null = null; + private view: Uint8Array | null = null; + /** Number of Java buffers allocated so far (a test and diagnostics hook). */ + allocations = 0; + + constructor( + private readonly initialCapacity = 0, + private readonly backend: DirectBufferBackend = nativeScriptBackend, + ) {} + + /** + * Copy `bytes` into the reused Java buffer and return it with position 0 and + * limit `bytes.length`, so `remaining()` equals the byte count. Valid until + * the next load() on this instance. + */ + load(bytes: Uint8Array): JavaByteBufferLike { + if (!this.javaBuffer || !this.view || this.view.length < bytes.length) { + const wanted = Math.max(bytes.length, this.initialCapacity, 1); + const capacity = Math.ceil(wanted / CAPACITY_STEP_BYTES) * CAPACITY_STEP_BYTES; + this.javaBuffer = this.backend.allocateDirect(capacity); + this.view = this.backend.viewOf(this.javaBuffer); + this.allocations++; + } + this.view.set(bytes); + this.javaBuffer.clear(); + this.javaBuffer.limit(bytes.length); + return this.javaBuffer; + } + + /** As load(), for an optional ArrayBuffer (e.g. a frame's draw list); null stays null. */ + loadOptional(buffer: ArrayBuffer | null | undefined): JavaByteBufferLike | null { + if (!buffer || buffer.byteLength === 0) return null; + return this.load(new Uint8Array(buffer)); + } +} + +/** + * Copy `bytes` into a fresh Java direct buffer (position 0, limit + * `bytes.length`) for a one-off crossing such as an atlas registration or a + * file write. Unlike a JavaDirectBuffer nothing is pinned between calls: the + * Java buffer is an ordinary object, freed once the JS side drops it. + */ +export function copyToJavaByteBuffer( + bytes: Uint8Array, + backend: DirectBufferBackend = nativeScriptBackend, +): JavaByteBufferLike { + // A zero-capacity direct buffer may have no address to view. + const javaBuffer = backend.allocateDirect(Math.max(bytes.length, 1)); + backend.viewOf(javaBuffer).set(bytes); + javaBuffer.limit(bytes.length); + return javaBuffer; +} diff --git a/app/native/kotlin-bridge.ts b/app/native/kotlin-bridge.ts new file mode 100644 index 00000000..6b463807 --- /dev/null +++ b/app/native/kotlin-bridge.ts @@ -0,0 +1,38 @@ +import { isAndroid } from "@nativescript/core"; +import { createKotlinBridge } from "./kotlin-platform"; + +/** Exercise the real native metadata and both directions of return values. */ +export function runKotlinBridgeSmokeTest(): void { + try { + const bridge = createKotlinBridge(); + const platform = bridge.platform(); + const thread = bridge.threadName(); + const callerOnMain = bridge.isMainThread(); + const greeting = bridge.greet("Faceclaw"); + const message = "Faceclaw \u2194 Kotlin \ud83d\udc53"; + let callbackCount = 0; + let callbackMessage = ""; + let callbackOnMain = false; + // Validate after returning; never throw from a native callback. + const reply = bridge.roundTrip(message, (value: string): string => { + callbackCount++; + callbackMessage = value; + callbackOnMain = bridge.isMainThread(); + return `TypeScript received: ${value}`; + }); + if (greeting !== "Hello, Faceclaw, from Kotlin" + || platform !== (isAndroid ? "Android" : "iOS") + || !thread || !callerOnMain || !callbackOnMain + || callbackCount !== 1 + || callbackMessage !== `Kotlin received: ${message}` + || reply !== `Kotlin received callback: TypeScript received: Kotlin received: ${message}`) { + throw new Error(`Unexpected Kotlin bridge result: ${JSON.stringify({ + platform, thread, callerOnMain, callbackOnMain, greeting, callbackCount, callbackMessage, reply, + })}`); + } + console.log("FACECLAW_KOTLIN_BRIDGE_PASS " + JSON.stringify({ platform, thread, reply })); + } catch (error) { + // Diagnostics must not prevent the rest of the app from launching. + console.error("FACECLAW_KOTLIN_BRIDGE_FAIL " + error); + } +} diff --git a/app/native/kotlin-data.ts b/app/native/kotlin-data.ts new file mode 100644 index 00000000..a3729fb6 --- /dev/null +++ b/app/native/kotlin-data.ts @@ -0,0 +1,12 @@ +/** Bulk binary bridge; slices are normalized without per-byte native calls. */ +export function toData(bytes: Uint8Array): NSData { + const buffer = bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength + ? bytes.buffer : bytes.slice().buffer + return NSData.dataWithBytesLength(interop.handleof(buffer), bytes.byteLength) +} +export function fromData(data: NSData): Uint8Array { + return new Uint8Array(interop.bufferFromData(data)).slice() +} +export function nativeArray(items: NSArray, convert: (item: T) => R): R[] { + return Array.from({ length: items.count }, (_, i) => convert(items.objectAtIndex(i))) +} diff --git a/app/native/kotlin-listener.ios.ts b/app/native/kotlin-listener.ios.ts new file mode 100644 index 00000000..8bbab1bb --- /dev/null +++ b/app/native/kotlin-listener.ios.ts @@ -0,0 +1,10 @@ +/** + * Implements a FaceclawKit (Kotlin) listener protocol with plain JavaScript methods. + * NativeScript maps the method names to the protocol's selectors by camel-casing the + * selector parts (`onLog:` -> `onLogLine`, `onState:detail:` -> `onStateStateDetail`, ...). + * Keep a reference to the returned object for as long as the Kotlin side may call it. + */ +export function kotlinListener(protocol: unknown, methods: T): T { + const NativeListener = (NSObject as any).extend(methods, { protocols: [protocol] }) + return NativeListener.new() as T +} diff --git a/app/native/kotlin-platform.android.ts b/app/native/kotlin-platform.android.ts new file mode 100644 index 00000000..3ece2aa1 --- /dev/null +++ b/app/native/kotlin-platform.android.ts @@ -0,0 +1,33 @@ +import { runKotlinProtocolSmokeTest } from './kotlin-protocol-smoke'; +import type { KotlinBridge } from './kotlin-platform'; + +// Handwritten declarations for the small native surface, checked against the +// compiled API. NativeScript runtime metadata is generated from the AAR. +declare namespace com.faceclaw.shared { + class KotlinBridge extends java.lang.Object { + constructor(); + platform(): string; + threadName(): string; + isMainThread(): boolean; + greet(name: string): string; + roundTrip(message: string, listener: KotlinBridgeListener): string; + } + class KotlinBridgeListener extends java.lang.Object { + constructor(implementation: { transform(message: string): string }); + } +} + +export function createKotlinBridge(): KotlinBridge { + runKotlinProtocolSmokeTest(); + const bridge = new com.faceclaw.shared.KotlinBridge(); + return { + platform: () => bridge.platform(), + threadName: () => bridge.threadName(), + isMainThread: () => bridge.isMainThread(), + greet: name => bridge.greet(name), + roundTrip(message, transform) { + const listener = new com.faceclaw.shared.KotlinBridgeListener({ transform }); + return bridge.roundTrip(message, listener); + }, + }; +} diff --git a/app/native/kotlin-platform.d.ts b/app/native/kotlin-platform.d.ts new file mode 100644 index 00000000..0322448c --- /dev/null +++ b/app/native/kotlin-platform.d.ts @@ -0,0 +1,11 @@ +/** Exported native API normalized across Java interfaces and Obj-C protocols. */ +export interface KotlinBridge { + platform(): string; + threadName(): string; + isMainThread(): boolean; + greet(name: string): string; + /** Synchronous callback; must not throw across the native boundary. */ + roundTrip(message: string, transform: (message: string) => string): string; +} + +export function createKotlinBridge(): KotlinBridge; diff --git a/app/native/kotlin-platform.ios.ts b/app/native/kotlin-platform.ios.ts new file mode 100644 index 00000000..f137f627 --- /dev/null +++ b/app/native/kotlin-platform.ios.ts @@ -0,0 +1,44 @@ +import { runKotlinProtocolSmokeTest } from './kotlin-protocol-smoke.ios'; +import type { KotlinBridge } from './kotlin-platform'; + +// Handwritten declarations matching FaceclawKit's Objective-C header. +declare class FaceclawKitKotlinBridge extends NSObject { + static alloc(): FaceclawKitKotlinBridge; + init(): this; + platform(): string; + threadName(): string; + isMainThread(): boolean; + greetName(name: string): string; + roundTripMessageListener(message: string, listener: FaceclawKitKotlinBridgeListener): string; +} +interface FaceclawKitKotlinBridgeListener { + transformMessage(message: string): string; +} +declare const FaceclawKitKotlinBridgeListener: { + prototype: FaceclawKitKotlinBridgeListener; +}; + +@NativeClass() +class KotlinCallback extends NSObject implements FaceclawKitKotlinBridgeListener { + static ObjCProtocols = [FaceclawKitKotlinBridgeListener]; + transform!: (message: string) => string; + + transformMessage(message: string): string { return this.transform(message); } +} + +export function createKotlinBridge(): KotlinBridge { + runKotlinProtocolSmokeTest(); + const bridge = FaceclawKitKotlinBridge.alloc().init(); + return { + platform: () => bridge.platform(), + threadName: () => bridge.threadName(), + isMainThread: () => bridge.isMainThread(), + greet: name => bridge.greetName(name), + roundTrip(message, transform) { + const listener = KotlinCallback.new() as KotlinCallback; + listener.transform = transform; + // Kotlin uses this listener synchronously and never retains it. + return bridge.roundTripMessageListener(message, listener); + }, + }; +} diff --git a/app/native/kotlin-protocol-smoke.ios.ts b/app/native/kotlin-protocol-smoke.ios.ts new file mode 100644 index 00000000..ecd2629e --- /dev/null +++ b/app/native/kotlin-protocol-smoke.ios.ts @@ -0,0 +1,46 @@ +import { GrayImage } from "../graphics/image" +import { encodeShellScene } from "../graphics/shell-scene" +import { decodeImageBytes } from './image-bytes.ios' +import * as protocol from '../g2/ble-protocol.ios' +import { SurfaceCompositor } from '../graphics/surface-compositor.ios' +import { lvglMetrics } from './lvgl-font.ios' + +/** Development-only checks of real NativeScript selectors and bulk data crossings. */ +export function runKotlinProtocolSmokeTest(): void { + const hex = (data: Uint8Array) => Array.from(data, byte => byte.toString(16).padStart(2, '0')).join('') + const assert = (condition: boolean, what: string) => { if (!condition) throw new Error(`Kotlin protocol bridge: ${what}`) } + const auth = protocol.authentication(101) + assert(hex(auth) === '080410651a0408011004', 'authentication') + assert(protocol.crc16(new Uint8Array([1, 2, 3])) === 0xadad, 'CRC') + const receiver = new protocol.MessageReceiver() + const frame = protocol.frameMessage(auth, 128, 0, 64)[0] + const received = receiver.receive('test', frame) + assert(received.length === 1 && received[0].magic === 101 && hex(received[0].payload) === hex(auth), 'reassembly') + const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, 12), protocol.integer(2, 2)))) + const event = protocol.decodeGlassesInput({ sid: 224, flag: 1, payload, command: 0, magic: 0 }) + assert(event?.kind === 'display-wake' && event.eventSource === 2, 'event') + const c = new SurfaceCompositor(3, 2) + c.configureSurface('test', { x: 0, y: 0, width: 3, height: 2, zOrder: 0, transparency: 'opaque' }) + c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 }) + assert(hex(c.composite()) === '0010ff203040', 'composition') + assert(hex(protocol.packGray4(c.composite(), 3, 2)) === '01f02340', 'packing') + c.setShellScene(new Uint8Array([1,0,1,0,0,0,0,0,1,0,1,0,128,0,0,0,0,0,255])) + assert(hex(c.composite()) === 'f00070101020', 'shell scene bridge and 4bpp composition') + c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking') + const selectedSurface = new GrayImage(8, 4, 1) + selectedSurface.drawMenuSelection(new GrayImage(4, 2, 255), 1, 1, 16, 48, 0, 2) + const selectedPreview = new SurfaceCompositor(8, 4) + selectedPreview.setShellScene(encodeShellScene([{ image: selectedSurface, x: 0, y: 0, shellKey: 100 }])) + const selectedPixels = selectedPreview.composite() + assert(selectedPixels[10] === 240 && selectedPixels[9] === 0, 'menu selection depth and bridge') + assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge') + const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0, + payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) }) + if (compass?.headingDegrees !== 359 || compass.diagnostics?.sampleTimeMs !== 0xfedcba98) + throw new Error('Kotlin compass notification mismatch') + const png = NSData.alloc().initWithBase64EncodedStringOptions('iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAEklEQVR4nGP4z8DAAMIM/4EAAB/uBfsL2WiLAAAAAElFTkSuQmCC', 0 as NSDataBase64DecodingOptions) + const decoded = decodeImageBytes(interop.bufferFromData(png), 2, 2) + assert(!!decoded && Array.from(decoded.pixels).join(',') === '54,182,18,255', 'native map image orientation and grayscale') + assert(decodeImageBytes(new ArrayBuffer(0), 2, 2) === null, 'invalid map image') + console.log('FACECLAW_KOTLIN_PROTOCOL_PASS iOS') +} diff --git a/app/native/kotlin-protocol-smoke.ts b/app/native/kotlin-protocol-smoke.ts new file mode 100644 index 00000000..062ae21d --- /dev/null +++ b/app/native/kotlin-protocol-smoke.ts @@ -0,0 +1,22 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + +declare const com: any + +/** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */ +export function runKotlinProtocolSmokeTest(): void { + const native = com.faceclaw.app + const hex = (data: any) => Array.from({ length: data.length }, (_, i) => (data[i] & 255).toString(16).padStart(2, '0')).join('') + const assert = (condition: boolean, what: string) => { if (!condition) throw new Error(`Kotlin protocol bridge: ${what}`) } + const auth = native.BleProtocol.buildAuthenticationRequest(101) + assert(hex(auth) === '080410651a0408011004', 'authentication') + const compositor = new native.SurfaceCompositor() + compositor.configureScreen(3, 2) + compositor.configureSurface('test', 0, 0, 3, 2, 0, 0) + const pixels = copyToJavaByteBuffer(new Uint8Array([0, 16, 255, 32, 48, 64])) + compositor.submitSurface('test', new native.AndroidByteReader(pixels), 0, 0, 3, 2, '1') + assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer') + assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing') + const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels)) + assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer') + console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android') +} diff --git a/app/native/location-permissions.ios.ts b/app/native/location-permissions.ios.ts new file mode 100644 index 00000000..4a8fc468 --- /dev/null +++ b/app/native/location-permissions.ios.ts @@ -0,0 +1,9 @@ +declare const FaceclawLocation: any +export const hasLocationPermission = (): boolean => FaceclawLocation.shared().hasPermission() +export function ensureLocationPermission(): Promise { + return new Promise(resolve => FaceclawLocation.shared().requestPermission(resolve)) +} + +export function ensureFineLocationPermission(): Promise { + return new Promise(resolve => FaceclawLocation.shared().requestPrecisePermission(resolve)) +} diff --git a/app/native/location-permissions.ts b/app/native/location-permissions.ts new file mode 100644 index 00000000..7d05f8b3 --- /dev/null +++ b/app/native/location-permissions.ts @@ -0,0 +1 @@ +export { ensureLocationPermission, ensureFineLocationPermission, hasLocationPermission } from '../g2/android-permissions' diff --git a/app/native/location-tracker.ios.ts b/app/native/location-tracker.ios.ts new file mode 100644 index 00000000..da4b8a4d --- /dev/null +++ b/app/native/location-tracker.ios.ts @@ -0,0 +1,25 @@ +import { type LocationTrackerCallbacks } from './location-types' +export { type TrackedLocation, type LocationTrackerCallbacks } from './location-types' +declare const FaceclawLocationUpdates: any + +/** Owned by the main-thread iOS sensor host; JSON fixes are relayed to app workers. */ +export class LocationTracker { + private native: any = null + constructor(private readonly callbacks: LocationTrackerCallbacks) {} + isRunning(): boolean { return this.native !== null } + start(intervalMs = 1000): void { + if (this.native) return + const native = this.native = FaceclawLocationUpdates.new() + native.eventHandler = (json: string) => { + if (this.native !== native) return + const event = JSON.parse(json) + if (event.error) { this.stop(); this.callbacks.onError(event.error) } + else this.callbacks.onLocation(event) + } + native.startTracking(intervalMs) + } + stop(): void { + const native = this.native; this.native = null + native?.stop() + } +} diff --git a/app/native/location-tracker.ts b/app/native/location-tracker.ts index 6487ddb6..f087a650 100644 --- a/app/native/location-tracker.ts +++ b/app/native/location-tracker.ts @@ -9,22 +9,8 @@ import { Utils } from "@nativescript/core"; declare const com: any; declare const global: any; -export type TrackedLocation = { - latitude: number; - longitude: number; - /** Meters, or null when the fix doesn't report accuracy. */ - accuracyMeters: number | null; - /** Degrees clockwise from true north, or null (common when stationary). */ - bearingDeg: number | null; - /** Meters per second, or null. */ - speedMps: number | null; - timestampMs: number; -}; - -export type LocationTrackerCallbacks = { - onLocation: (location: TrackedLocation) => void; - onError: (message: string) => void; -}; +import { type LocationTrackerCallbacks } from "./location-types"; +export { type LocationTrackerCallbacks, type TrackedLocation } from "./location-types"; export class LocationTracker { private tracker: any = null; diff --git a/app/native/location-types.ts b/app/native/location-types.ts new file mode 100644 index 00000000..bd29a813 --- /dev/null +++ b/app/native/location-types.ts @@ -0,0 +1,23 @@ +export type CurrentLocation = { + latitude: number; + longitude: number; + accuracyMeters: number | null; + timestampMs: number; +}; + +export type TrackedLocation = { + latitude: number; + longitude: number; + /** Meters, or null when the fix doesn't report accuracy. */ + accuracyMeters: number | null; + /** Degrees clockwise from true north, or null (common when stationary). */ + bearingDeg: number | null; + /** Meters per second, or null. */ + speedMps: number | null; + timestampMs: number; +}; + +export type LocationTrackerCallbacks = { + onLocation: (location: TrackedLocation) => void; + onError: (message: string) => void; +}; diff --git a/app/native/location.ios.ts b/app/native/location.ios.ts new file mode 100644 index 00000000..886b6e77 --- /dev/null +++ b/app/native/location.ios.ts @@ -0,0 +1,19 @@ +import { type CurrentLocation } from './location-types' +export { type CurrentLocation } from './location-types' +declare const FaceclawLocationUpdates: any + +/** Main-thread one-shot lookup, independent of navigation and compass managers. */ +export function getCurrentLocation(): Promise { + return new Promise((resolve, reject) => { + const provider = FaceclawLocationUpdates.new() + provider.eventHandler = (json: string) => { + try { + const event = JSON.parse(json) + if (event.error) reject(new Error(event.error)) + else resolve(event) + } catch (error) { reject(error) } + finally { provider.stop() } + } + provider.startOnce() + }) +} diff --git a/app/native/location.ts b/app/native/location.ts index e2b23abd..1dbafb0e 100644 --- a/app/native/location.ts +++ b/app/native/location.ts @@ -2,12 +2,8 @@ import { Utils } from "@nativescript/core"; declare const com: any; -export type CurrentLocation = { - latitude: number; - longitude: number; - accuracyMeters: number | null; - timestampMs: number; -}; +import { type CurrentLocation } from "./location-types"; +export { type CurrentLocation } from "./location-types"; /** Resolve one current foreground location using Android's LocationManager. */ export function getCurrentLocation(): Promise { diff --git a/app/native/lvgl-font.ios.ts b/app/native/lvgl-font.ios.ts new file mode 100644 index 00000000..2f84f401 --- /dev/null +++ b/app/native/lvgl-font.ios.ts @@ -0,0 +1,5 @@ +import { fromData } from './kotlin-data' +declare const FaceclawKitIosProtocol: any +const protocol = FaceclawKitIosProtocol.new() +export function lvglMetrics(path: string): Uint8Array { return fromData(protocol.fontMetricsPath(path)) } +export function lvglGlyph(path: string, codePoint: number): Uint8Array { return fromData(protocol.fontGlyphPathCodePoint(path, codePoint)) } diff --git a/app/native/lvgl-font.ts b/app/native/lvgl-font.ts new file mode 100644 index 00000000..9b403e91 --- /dev/null +++ b/app/native/lvgl-font.ts @@ -0,0 +1,4 @@ +import { toUint8Array } from '../util/array-util' +declare const com: any +export function lvglMetrics(path: string): Uint8Array { return toUint8Array(com.faceclaw.app.LvglFontFile.getMetrics(path)) } +export function lvglGlyph(path: string, codePoint: number): Uint8Array { return toUint8Array(com.faceclaw.app.LvglFontFile.getGlyph(path, codePoint)) } diff --git a/app/native/mapbox.ts b/app/native/mapbox.ts index 364d9252..9f5ad8da 100644 --- a/app/native/mapbox.ts +++ b/app/native/mapbox.ts @@ -6,11 +6,9 @@ */ import { GrayImage } from "../graphics/image"; import { getStringSetting } from "./settings-store"; -import { grayImageFromPacket } from "./image-files"; +import { decodeImageBytes } from "./image-bytes"; import { fetchWithUserAgent } from "../util/http"; -declare const com: any; -declare const global: any; export const MAPBOX_TOKEN_SETTING_KEY = "maps.mapboxApiKey"; const SEARCH_ROOT = "https://api.mapbox.com/search/searchbox/v1"; @@ -231,10 +229,7 @@ export async function fetchStaticMapGray(options: { } throw new Error(`Mapbox static map failed (HTTP ${response.status}).`); } - if (!global.isAndroid) throw new Error("Map decoding is only available on Android."); - const image = grayImageFromPacket( - com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(bytes, Math.round(options.width), Math.round(options.height)), - ); + const image = decodeImageBytes(bytes, Math.round(options.width), Math.round(options.height)); if (!image) throw new Error("Failed to decode the Mapbox map image."); return image; } diff --git a/app/native/nightscout-bridge.ts b/app/native/nightscout-bridge.ts index 5da6237d..646b6622 100644 --- a/app/native/nightscout-bridge.ts +++ b/app/native/nightscout-bridge.ts @@ -167,10 +167,12 @@ export class NightscoutBridge { await this.refreshNow(); return; } - await this.refreshNow(); + // Register polling before awaiting HTTP so stop() during the first fetch + // cannot leave a timer behind, and concurrent starts share one timer. this.refreshHandle = setInterval(() => { void this.refreshNow(); }, NIGHTSCOUT_REFRESH_MS); + await this.refreshNow(); } async stop(): Promise { diff --git a/app/native/notification-access.ios.ts b/app/native/notification-access.ios.ts new file mode 100644 index 00000000..de94e22c --- /dev/null +++ b/app/native/notification-access.ios.ts @@ -0,0 +1,4 @@ +import { iosNotificationState, iosNotificationMessage } from './notification-icons.ios' +export function isNotificationListenerEnabled(): boolean { return iosNotificationState() === 'ready' } +export function requestNotificationListenerAccess(): void { /* iOS exposes no deep link to this Bluetooth setting. */ } +export function notificationEmptyMessage(): string { return iosNotificationMessage() } diff --git a/app/native/notification-access.ts b/app/native/notification-access.ts index 3925e139..97823a72 100644 --- a/app/native/notification-access.ts +++ b/app/native/notification-access.ts @@ -31,3 +31,8 @@ export function requestNotificationListenerAccess(): void { if (!context) return; com.faceclaw.app.FaceclawEvenAppDetector.openNotificationAccessSettings(context); } + +export function notificationEmptyMessage(): string { + return isNotificationListenerEnabled() ? "No current Android notifications." + : "Grant permission on your phone to view notifications on the glasses."; +} diff --git a/app/native/notification-icons.ios.ts b/app/native/notification-icons.ios.ts new file mode 100644 index 00000000..d94b9857 --- /dev/null +++ b/app/native/notification-icons.ios.ts @@ -0,0 +1,44 @@ +import { GrayImage } from '../graphics/image' +import { AncsClient, ANCS_CONNECT_MESSAGE, type AncsState } from '../g2/ancs-client' +import { rememberNotificationSources } from './notification-sources' +export type { AndroidNotification, AndroidNotificationAction } from './notification-types' +export type { NotificationIconsResult, NotificationIconResult } from './notification-icons' + +export const ALL_NOTIFICATIONS = 0x7fffffff +let active: AncsClient | null = null +const listeners = new Set<(key: string) => void>() +const popups = new Set<(key: string) => void>() +export function bindIosNotifications(client: AncsClient): void { active = client } +export function iosNotificationsChanged(key?: string, popup = false): void { + const current = active + if (current) rememberNotificationSources(current.read(ALL_NOTIFICATIONS)) + for (const listener of listeners) listener(key ?? '') + if (popup && key) for (const listener of popups) listener(key) +} +export function onIosNotificationPopup(listener: (key: string) => void): () => void { + popups.add(listener); return () => { popups.delete(listener) } +} +export function iosNotificationMessage(): string { return active?.statusMessage ?? ANCS_CONNECT_MESSAGE } +export function iosNotificationState(): AncsState { return active?.state ?? 'disconnected' } +export function readActiveNotifications(maxNotifications = 50) { return active?.read(maxNotifications) ?? [] } +export function invokeNotificationAction(key: string, index: number): boolean { return active?.action(key,index) ?? false } +export function dismissNotification(key: string): boolean { return active?.dismiss(key) ?? false } +export function onAndroidNotificationPosted(listener: (key: string) => void): () => void { + listeners.add(listener); return () => { listeners.delete(listener) } +} +// ANCS does not provide app icons. Use a local bell; never fetch notification +// sources or content from a third-party icon service. +function bell(): GrayImage { + const image = new GrayImage(24,24,0) + image.fillRoundedRect(6,5,12,13,220,5) + image.fillRect(4,17,16,2,220) + image.fillRect(10,20,4,2,220) + return image +} +export function readActiveNotificationIcons(maxIcons: number, _allowStale: boolean) { + const sources = new Set(readActiveNotifications(ALL_NOTIFICATIONS).map(n => n.packageName)) + return {icons: Array.from(sources).slice(0,Math.max(0,maxIcons)).map(() => bell()),stale:false} +} +export function readNotificationIconByKey(key: string, _allowStale: boolean) { + return {icon: readActiveNotifications(ALL_NOTIFICATIONS).some(n => n.key === key) ? bell() : null,stale:false} +} diff --git a/app/native/notification-icons.ts b/app/native/notification-icons.ts index d3f58d22..6d5c2665 100644 --- a/app/native/notification-icons.ts +++ b/app/native/notification-icons.ts @@ -28,28 +28,8 @@ function invalidateIconCaches(): void { keyedIconCache.clear(); } -export type AndroidNotificationAction = { - index: number; - title: string; - enabled: boolean; -}; - -export type AndroidNotification = { - key: string; - packageName: string; - appName: string; - title: string; - text: string; - bigText: string; - subText: string; - infoText: string; - summaryText: string; - category: string; - lines: string[]; - postTime: number; - when: number; - actions: AndroidNotificationAction[]; -}; +import type { AndroidNotification, AndroidNotificationAction } from "./notification-types"; +export type { AndroidNotification, AndroidNotificationAction } from "./notification-types"; export type NotificationIconsResult = { icons: GrayImage[]; diff --git a/app/native/notification-types.ts b/app/native/notification-types.ts new file mode 100644 index 00000000..b7a6581f --- /dev/null +++ b/app/native/notification-types.ts @@ -0,0 +1,10 @@ +/** Shared notification model; Android names are retained for existing callers. */ +export type AndroidNotificationAction = { index: number; title: string; enabled: boolean }; +export type AndroidNotification = { + key: string; packageName: string; appName: string; title: string; text: string; + bigText: string; subText: string; infoText: string; summaryText: string; + category: string; lines: string[]; postTime: number; when: number; + actions: AndroidNotificationAction[]; dismissLabel?: string; + /** Full message size reported by iOS, even when the fetched text is bounded. */ + messageSize?: number; +}; diff --git a/app/native/open-url.ios.ts b/app/native/open-url.ios.ts new file mode 100644 index 00000000..6c9eac63 --- /dev/null +++ b/app/native/open-url.ios.ts @@ -0,0 +1,9 @@ +import { Utils } from '@nativescript/core' +declare const global: any + +export function openUrlOnPhone(url: string): boolean { + if (!/^https?:\/\//i.test(url)) return false + if (NSThread.isMainThread) void Utils.openUrl(url) + else global.postMessage({ type: 'open-url', url }) + return true +} diff --git a/app/native/openai.ts b/app/native/openai.ts index cb178329..bba3b840 100644 --- a/app/native/openai.ts +++ b/app/native/openai.ts @@ -5,11 +5,11 @@ import type { LlmStreamOptions, } from "../assistant/llm-protocol"; -declare const com: any; +import { openSseRequest, type SseListener } from "./sse"; /** * Minimal streaming client for the OpenAI Responses API. NativeScript cannot - * use the Node SDK, so this shares the okhttp/SSE bridge used by Anthropic. + * use the Node SDK, so this shares the native streaming transport used by Anthropic. */ const OPENAI_RESPONSES_URL = "https://api.openai.com/v1/responses"; @@ -31,11 +31,13 @@ export function streamOpenAiResponse(options: LlmStreamOptions): LlmStreamHandle const fail = (message: string) => { if (settled) return; settled = true; + request?.cancel(); options.onError(message); }; const done = () => { if (settled) return; settled = true; + request?.cancel(); const content = finalizeOutputs(outputs); const hasToolUse = content.some((block) => block.type === "tool_use"); options.onDone({ @@ -45,13 +47,9 @@ export function streamOpenAiResponse(options: LlmStreamOptions): LlmStreamHandle }); }; - if (!global.isAndroid) { - setTimeout(() => fail("OpenAI API is only wired up on Android"), 0); - return { cancel: () => {} }; - } if (!apiKey) { setTimeout(() => fail("No OpenAI API key set"), 0); - return { cancel: () => {} }; + return { cancel: () => { settled = true; } }; } const body: any = { @@ -73,7 +71,7 @@ export function streamOpenAiResponse(options: LlmStreamOptions): LlmStreamHandle } if (options.effort) body.reasoning = { effort: options.effort }; - const listener = new com.faceclaw.app.FaceclawSseListener({ + const listener: SseListener = { onLine: (line: string) => { if (settled) return; const event = parseSseDataLine(String(line)); @@ -164,26 +162,19 @@ export function streamOpenAiResponse(options: LlmStreamOptions): LlmStreamHandle fail(describeHttpError(Number(code), String(errorBody))); }, onComplete: () => { - done(); + // A transport EOF alone must not execute an unfinished tool call. + fail("OpenAI stream ended before the response completed"); }, onFailure: (message: string) => { fail(`OpenAI connection failed: ${String(message)}`); }, - }); + }; - const headers = Array.create("java.lang.String", 2) as string[]; - headers[0] = "Authorization"; - headers[1] = `Bearer ${apiKey}`; try { - request = new com.faceclaw.app.FaceclawSseRequest( - OPENAI_RESPONSES_URL, - JSON.stringify(body), - headers, - listener, - ); + request = openSseRequest(OPENAI_RESPONSES_URL, JSON.stringify(body), { Authorization: `Bearer ${apiKey}` }, listener); } catch (error) { setTimeout(() => fail(`OpenAI request failed: ${String((error as Error)?.message ?? error)}`), 0); - return { cancel: () => {} }; + return { cancel: () => { settled = true; } }; } return { diff --git a/app/native/phone-battery.ts b/app/native/phone-battery.ts index d93be911..b758fcd3 100644 --- a/app/native/phone-battery.ts +++ b/app/native/phone-battery.ts @@ -8,6 +8,15 @@ export type PhoneBatteryState = { }; export function readPhoneBatteryState(): PhoneBatteryState { + if (global.isIOS) { + const device = UIDevice.currentDevice + const level = device.batteryLevel + const state = device.batteryState + return { + battery: level >= 0 ? Math.round(level * 100) : null, + charging: state === UIDeviceBatteryState.Unknown ? null : state === UIDeviceBatteryState.Charging || state === UIDeviceBatteryState.Full, + } + } if (!global.isAndroid) { return { battery: null, charging: null }; } diff --git a/app/native/preview-display.ios.ts b/app/native/preview-display.ios.ts new file mode 100644 index 00000000..2a70a769 --- /dev/null +++ b/app/native/preview-display.ios.ts @@ -0,0 +1,146 @@ +import { File, knownFolders, path, type ImageSource } from "@nativescript/core"; +import { SurfaceCompositor } from "../graphics/surface-compositor"; +import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from "../graphics/image"; +import { previewPixels } from "./ios-graphics"; +import { toData } from "./kotlin-data"; +import type { FaceclawCommunicatorBridge, SurfaceOptions } from "./faceclaw-communicator.ios"; + +declare const FaceclawKitIosProtocol: any; + +/** The display subset of the communicator bridge; see the Android twin. */ +export type DisplayTarget = Pick< + FaceclawCommunicatorBridge, + | "configureCompositorScreen" + | "configureSurface" + | "removeSurface" + | "setSurfaceVisible" + | "setSurfaceDepth" + | "setUnderlayDim" + | "setScreenBlanked" + | "submitSurfaceFrame" + | "submitShellScene" + | "waitForFrameFinished" + | "getCompositePreview" + | "saveScreenshot" + | "startScreenRecording" + | "recordScreenFrame" + | "stopScreenRecording" +>; + +function timestamp(): string { + const now = new Date(); + const pad = (value: number, width = 2) => String(value).padStart(width, "0"); + return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`; +} + +/** + * Headless display target for iOS while no glasses session exists: the same + * shared Kotlin SurfaceCompositor a live session composes with, so the phone + * mirror, apps and shell render identically before connecting. Frames end + * at the compositor; there is no transmit backpressure to wait on. + */ +export class PreviewDisplayTarget implements DisplayTarget { + private compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT); + private onFrameComposited: (() => void) | null = null; + private released = false; + + /** Receive a callback per applied frame, the stand-in for the connected path's frame-finished callback. */ + activate(onFrameComposited: () => void): void { + this.onFrameComposited = onFrameComposited; + } + + release(): void { + this.released = true; + this.onFrameComposited = null; + } + + private composited(): void { + if (!this.released) this.onFrameComposited?.(); + } + + async configureCompositorScreen(width: number, height: number): Promise { + if (Math.round(width) !== this.compositor.width || Math.round(height) !== this.compositor.height) { + this.compositor = new SurfaceCompositor(Math.round(width), Math.round(height)); + } + } + + async configureSurface(id: string, options: SurfaceOptions): Promise { + this.compositor.configureSurface(id, { + x: Math.round(options.x), y: Math.round(options.y), width: Math.round(options.width), height: Math.round(options.height), + zOrder: Math.round(options.zOrder), transparency: options.transparency, + }); + } + + async removeSurface(id: string): Promise { + this.compositor.removeSurface(id); + this.composited(); + } + + async setSurfaceVisible(id: string, visible: boolean): Promise { + this.compositor.setSurfaceVisible(id, Boolean(visible)); + this.composited(); + } + + async setSurfaceDepth(id: string, depth: number): Promise { + this.compositor.setSurfaceDepth(id, Math.round(depth)); + } + + async setUnderlayDim(belowZOrder: number, factor: number): Promise { + this.compositor.setUnderlayDim(Math.round(belowZOrder), Math.max(0, Math.min(1, factor))); + this.composited(); + } + + async setScreenBlanked(blanked: boolean): Promise { + this.compositor.setScreenBlanked(Boolean(blanked)); + this.composited(); + } + + async submitSurfaceFrame( + surfaceId: string, + pixels8bpp: Uint8Array, + rect: { x: number; y: number; width: number; height: number }, + _fingerprint: string, + _paintMs = -1, + _frameId = 0, + glyphs: ArrayBuffer | null = null, + ): Promise { + this.compositor.submitSurfaceFrame(surfaceId, pixels8bpp, + { x: Math.round(rect.x), y: Math.round(rect.y), width: Math.round(rect.width), height: Math.round(rect.height) }, glyphs); + this.composited(); + } + + async submitShellScene(bytes: Uint8Array, _paintMs = 0, _frameId = 0): Promise { + this.compositor.setShellScene(bytes); + this.composited(); + } + + /** Nothing transmits: every frame is finished as soon as it is composited. */ + waitForFrameFinished(frameId: number, _timeoutMs: number): Promise { + return Promise.resolve(frameId > 0 ? "composited" : null); + } + + /** The current composite as gray pixels (row-major, compositor size). */ + composite(): Uint8Array { + return this.compositor.composite(); + } + + getCompositePreview(green = false): ImageSource | null { + return previewPixels(this.compositor.composite(), this.compositor.width, this.compositor.height, green); + } + + /** Save the current composite as a 4-bit grayscale PNG under Documents; cropping is not supported on iOS. */ + saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string { + if (crop) return ""; + const png = FaceclawKitIosProtocol.new().pngDataWidthHeight(toData(this.compositor.composite()), this.compositor.width, this.compositor.height); + if (!png) return ""; + const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`); + File.fromPath(target).writeSync(png); + return target; + } + + startScreenRecording(): void {} + + recordScreenFrame(): void {} + + stopScreenRecording(): string { return ""; } +} diff --git a/app/native/preview-display.ts b/app/native/preview-display.ts index baad5305..53e3e927 100644 --- a/app/native/preview-display.ts +++ b/app/native/preview-display.ts @@ -1,5 +1,6 @@ import { ImageSource, Utils } from "@nativescript/core"; import { dimFactor256, type FaceclawCommunicatorBridge, type SurfaceOptions } from "./faceclaw-communicator"; +import { JavaDirectBuffer } from "./java-direct-buffer"; declare const com: any; declare const java: any; @@ -19,9 +20,11 @@ export type DisplayTarget = Pick< | "configureSurface" | "removeSurface" | "setSurfaceVisible" + | "setSurfaceDepth" | "setUnderlayDim" | "setScreenBlanked" | "submitSurfaceFrame" + | "submitShellScene" | "waitForFrameFinished" | "getCompositePreview" | "saveScreenshot" @@ -47,6 +50,11 @@ function nonNegativeNumber(value: number): number { */ export class PreviewDisplayTarget implements DisplayTarget { private readonly compositor: any; + // Reused Java-side buffers: passing a JS ArrayBuffer to Java leaks it + // (java-direct-buffer.ts). + private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480); + private readonly frameDrawsBuffer = new JavaDirectBuffer(); + private readonly shellSceneBuffer = new JavaDirectBuffer(); constructor() { const context = Utils.android.getApplicationContext(); @@ -97,6 +105,14 @@ export class PreviewDisplayTarget implements DisplayTarget { this.compositor.setSurfaceVisible(id, Boolean(visible)); } + async setSurfaceDepth(id: string, depth: number): Promise { + this.compositor.setSurfaceDepth(id, Math.round(depth)); + } + + async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { + this.compositor.submitShellScene(this.shellSceneBuffer.load(bytes), paintMs, frameId); + } + async setUnderlayDim(belowZOrder: number, factor: number): Promise { this.compositor.setUnderlayDim(Math.round(belowZOrder), dimFactor256(factor)); } @@ -114,11 +130,11 @@ export class PreviewDisplayTarget implements DisplayTarget { frameId = 0, glyphs: ArrayBuffer | null = null, ): Promise { - // Copy for the same reason the bridge does: the source may be a view on a - // larger buffer, and Java receives the backing ArrayBuffer. - const snapshot = new Uint8Array(pixels8bpp); + // Copied into a reused Java direct buffer, sized to exactly this view's + // bytes; passing the ArrayBuffer itself would leak it + // (java-direct-buffer.ts). this.compositor.submitSurfaceFrame( - snapshot.buffer, + this.framePixelsBuffer.load(pixels8bpp), surfaceId, Math.round(rect.x), Math.round(rect.y), @@ -127,7 +143,7 @@ export class PreviewDisplayTarget implements DisplayTarget { fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), - glyphs, + this.frameDrawsBuffer.loadOptional(glyphs), ); // The Java side finishes the frame: the composite is the end of the line, // and worker isolates submit to the same object without coming through here. diff --git a/app/native/qr-scan.ios.ts b/app/native/qr-scan.ios.ts new file mode 100644 index 00000000..bc74376a --- /dev/null +++ b/app/native/qr-scan.ios.ts @@ -0,0 +1,24 @@ +declare const FaceclawQrScanner: any + +let activeScanner: any = null + +export function isQrScannerAvailable(): boolean { return FaceclawQrScanner.isAvailable() } + +export function scanQrCode(): Promise { + if (activeScanner) return Promise.reject(new Error('A QR scan is already open.')) + return new Promise((resolve, reject) => { + const scanner = FaceclawQrScanner.new() + activeScanner = scanner + let settled = false + const finish = (text: string | null, error: string | null) => { + if (settled) return + settled = true + if (activeScanner === scanner) activeScanner = null + error ? reject(new Error(String(error))) : resolve(text == null ? null : String(text)) + } + try { scanner.startWithCompletion(finish) } + catch (error) { finish(null, String(error)) } + }) +} + +export function cancelQrScan(): void { activeScanner?.cancel() } diff --git a/app/native/qr-scan.ts b/app/native/qr-scan.ts index c91c7000..d415098f 100644 --- a/app/native/qr-scan.ts +++ b/app/native/qr-scan.ts @@ -22,6 +22,10 @@ const SCAN_ACTION = "com.google.zxing.client.android.SCAN"; // Arbitrary, only has to be distinct from other startActivityForResult callers. const SCAN_REQUEST_CODE = 0x51d0; +// Android's scanner is owned by Play Services or another app. The load layer +// ignores its late result if the user leaves; iOS also closes its own camera. +export function cancelQrScan(): void {} + function currentActivity(): any { return Application.android?.foregroundActivity ?? Application.android?.startActivity ?? null; } diff --git a/app/native/remote-input.ios.ts b/app/native/remote-input.ios.ts new file mode 100644 index 00000000..3e6c9b07 --- /dev/null +++ b/app/native/remote-input.ios.ts @@ -0,0 +1,16 @@ +import { createListenerPool, type InterfaceAddress } from '../remote/listeners'; +declare const FaceclawKitIosRemoteInput: any; +/** The shared Kotlin RemoteInputSession over POSIX sockets; request notifications arrive on the main queue. */ +export function remoteNative(onRequestReady: () => void) { + return createListenerPool(() => { + const native = FaceclawKitIosRemoteInput.new(); + native.setRequestListenerListener(onRequestReady); + return { start: (port, address) => String(native.startAddressPortAddress(port, address)), stop: () => { native.setRequestListenerListener(null); native.stop(); }, + nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeIdValue(id, response) }; + }); +} +function shared(): any { return FaceclawKitIosRemoteInput.companion.shared; } +export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(shared().interfaces())); } +export function randomTokenSecret(): string { return String(shared().randomSecret()); } +export function tokenHash(value: string): string { return String(shared().tokenDigestValue(value)); } +export function copyRemoteToken(value: string): void { UIPasteboard.generalPasteboard.string = value; } diff --git a/app/native/remote-input.ts b/app/native/remote-input.ts new file mode 100644 index 00000000..0be486be --- /dev/null +++ b/app/native/remote-input.ts @@ -0,0 +1,19 @@ +import { createListenerPool, type InterfaceAddress } from '../remote/listeners'; +import { Utils } from '@nativescript/core'; +declare const com: any; +export function remoteNative(onRequestReady: () => void) { + return createListenerPool(() => { + const native = new com.faceclaw.app.FaceclawRemoteInput(); + const ready = new java.lang.Runnable({ run: onRequestReady }); + native.setRequestListener(ready); + return { start: (port, address) => String(native.startAddress(port, address)), stop: () => { native.setRequestListener(null); native.stop(); }, + nextRequest: () => native.nextRequest(), complete: (id, response) => native.complete(id, response) }; + }); +} +export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(com.faceclaw.app.FaceclawRemoteInput.interfaces())); } +export function randomTokenSecret(): string { return String(com.faceclaw.app.FaceclawRemoteInput.getInstance().randomSecret()); } +export function tokenHash(value: string): string { return String(com.faceclaw.app.FaceclawRemoteInput.getInstance().hash(value)); } +export function copyRemoteToken(value: string): void { + const context = Utils.android.getApplicationContext(); + context.getSystemService('clipboard').setPrimaryClip(android.content.ClipData.newPlainText('Faceclaw input token', value)); +} diff --git a/app/native/settings-store.ios.ts b/app/native/settings-store.ios.ts new file mode 100644 index 00000000..84d49add --- /dev/null +++ b/app/native/settings-store.ios.ts @@ -0,0 +1,62 @@ +import { ApplicationSettings } from '@nativescript/core' + +// NSUserDefaults is shared by all NativeScript isolates. Each isolate polls +// just its observed keys so callbacks always execute on the owning JS thread. +const CHANGE_TOKEN = 'ios.settings.changeToken' +let lastToken = ApplicationSettings.getString(CHANGE_TOKEN, '') +const observed = new Map string; value: string }>() +function observe(key: string, read: () => string): void { + if (!observed.has(key)) observed.set(key, { read, value: read() }) +} +function markChanged(key: string): void { + // A unique token lets other isolates check one key while idle. It does not + // depend on a read/increment/write counter that concurrent writers could lose. + ApplicationSettings.setString(CHANGE_TOKEN, `${Date.now()}:${Math.random()}`) + const item = observed.get(key) + if (item) item.value = item.read() + changed(key) +} +let pollTimer: ReturnType | null = null +const listeners = new Set<(key: string) => void>() +export function disposeSettingsStore(): void { + listeners.clear() + if (pollTimer !== null) clearInterval(pollTimer) + pollTimer = null + observed.clear() +} +function changed(key: string): void { + setTimeout(() => { for (const listener of [...listeners]) listener(key) }, 0) +} +export function getStringSetting(key: string, fallback: string): string { + const value = ApplicationSettings.getString(key, fallback) + observe(key, () => JSON.stringify(ApplicationSettings.getString(key, fallback))) + return value +} +export function setStringSetting(key: string, value: string): void { + if (getStringSetting(key, '') === value && ApplicationSettings.hasKey(key)) return + ApplicationSettings.setString(key, value) + markChanged(key) +} +export function getBooleanSetting(key: string, fallback: boolean): boolean { + const value = ApplicationSettings.getBoolean(key, fallback) + observe(key, () => JSON.stringify(ApplicationSettings.getBoolean(key, fallback))) + return value +} +export function setBooleanSetting(key: string, value: boolean): void { + if (getBooleanSetting(key, false) === value && ApplicationSettings.hasKey(key)) return + ApplicationSettings.setBoolean(key, value) + markChanged(key) +} +export function onSettingsStoreChanged(listener: (key: string) => void): () => void { + listeners.add(listener) + if (!pollTimer) pollTimer = setInterval(() => { + const token = ApplicationSettings.getString(CHANGE_TOKEN, '') + if (token === lastToken) return + lastToken = token + for (const [key, item] of observed) { + const value = item.read() + if (value !== item.value) { item.value = value; changed(key) } + } + }, 250) + return () => { listeners.delete(listener); if (!listeners.size && pollTimer) { clearInterval(pollTimer); pollTimer = null } } +} diff --git a/app/native/settings-store.ts b/app/native/settings-store.ts index e738f410..3f3f43da 100644 --- a/app/native/settings-store.ts +++ b/app/native/settings-store.ts @@ -14,6 +14,13 @@ let javaInstance: any = null; let retainedListenerProxy: any = null; const changeListeners = new Set<(key: string) => void>(); +/** Release this isolate's native observer before its worker is terminated. */ +export function disposeSettingsStore(): void { + changeListeners.clear(); + if (retainedListenerProxy !== null) getJava().unregisterListener(retainedListenerProxy); + retainedListenerProxy = null; +} + function getJava(): any { if (javaInstance === null) { const context = Utils.android?.getApplicationContext?.(); diff --git a/app/native/socket.ios.ts b/app/native/socket.ios.ts new file mode 100644 index 00000000..e0bb5897 --- /dev/null +++ b/app/native/socket.ios.ts @@ -0,0 +1,16 @@ +export type SocketListener = { onOpen(): void; onTextMessage(message: string): void; onClosed(code: number, reason: string): void; onFailure(message: string): void } +declare const FaceclawSocket: any +export function openSocket(url: string, listener: SocketListener): { sendText(text: string): void; close(code: number, reason: string): void } { + const native = FaceclawSocket.alloc().initWithURL(url) + let ended = false + const close = () => { if (ended) return; ended = true; clearInterval(timer); native.close() } + const timer = setInterval(() => { + for (const event of JSON.parse(native.takeEvents())) { + if (ended) break + if (event.kind === 'open') listener.onOpen() + else if (event.kind === 'text') listener.onTextMessage(event.text) + else { close(); if (event.kind === 'closed') listener.onClosed(event.code, event.text); else listener.onFailure(event.text) } + } + }, 25) + return { sendText: text => { if (!ended) native.sendText(text) }, close } +} diff --git a/app/native/socket.ts b/app/native/socket.ts new file mode 100644 index 00000000..7b952acc --- /dev/null +++ b/app/native/socket.ts @@ -0,0 +1,9 @@ +declare const com: any +export type SocketListener = { onOpen(): void; onTextMessage(message: string): void; onClosed(code: number, reason: string): void; onFailure(message: string): void } +export function openSocket(url: string, listener: SocketListener): { sendText(text: string): void; close(code: number, reason: string): void } { + const proxy = new com.faceclaw.app.FaceclawWebSocketListener(listener) + const native = new com.faceclaw.app.FaceclawWebSocket(url, proxy, null, null) + const connection = { listenerProxy: proxy, sendText: (text: string) => native.sendText(text), + close: (code: number, reason: string) => native.close(code, reason) } + return connection +} diff --git a/app/native/sse-types.ts b/app/native/sse-types.ts new file mode 100644 index 00000000..bc275c1e --- /dev/null +++ b/app/native/sse-types.ts @@ -0,0 +1,6 @@ +export type SseListener = { + onLine(line: string): void; + onHttpError(code: number, body: string): void; + onComplete(): void; + onFailure(message: string): void; +}; diff --git a/app/native/sse.ios.ts b/app/native/sse.ios.ts new file mode 100644 index 00000000..2c5c1609 --- /dev/null +++ b/app/native/sse.ios.ts @@ -0,0 +1,19 @@ +import { type SseListener } from './sse-types'; +import { withUserAgent } from '../util/http'; +import { kotlinListener } from './kotlin-listener.ios'; +export { type SseListener } from './sse-types'; +declare const FaceclawKitIosSseRequest: any, FaceclawKitFaceclawSseListener: any; + +/** The shared Kotlin SseStream over NSURLSession; the Kotlin side delivers every callback on the main queue, in order. */ +export function openSseRequest(url: string, body: string, headers: Record, listener: SseListener): { cancel(): void } { + let ended = false; + const once = (deliver: () => void) => { if (ended) return; ended = true; deliver(); }; + const nativeListener = kotlinListener(FaceclawKitFaceclawSseListener, { + onLineLine: (line: string) => { if (!ended) listener.onLine(String(line ?? '')); }, + onHttpErrorCodeBody: (code: number, errorBody: string) => once(() => listener.onHttpError(Number(code), String(errorBody ?? ''))), + onComplete: () => once(() => listener.onComplete()), + onFailureMessage_: (message: string) => once(() => listener.onFailure(String(message ?? ''))), + }); + const native = FaceclawKitIosSseRequest.alloc().initWithUrlBodyHeadersJsonListener(url, body, JSON.stringify(withUserAgent(headers)), nativeListener); + return { cancel: () => { if (ended) return; ended = true; native.cancel(); } }; +} diff --git a/app/native/sse.ts b/app/native/sse.ts new file mode 100644 index 00000000..1a7adabc --- /dev/null +++ b/app/native/sse.ts @@ -0,0 +1,11 @@ +import { type SseListener } from './sse-types'; +export { type SseListener } from './sse-types'; +declare const com: any; + +/** Android delivers callbacks on the requesting isolate's Looper. */ +export function openSseRequest(url: string, body: string, headers: Record, listener: SseListener): { cancel(): void } { + const entries = Object.entries(headers); + const nativeHeaders = Array.create('java.lang.String', entries.length * 2); + entries.forEach(([name, value], index) => { nativeHeaders[index * 2] = name; nativeHeaders[index * 2 + 1] = value; }); + return new com.faceclaw.app.FaceclawSseRequest(url, body, nativeHeaders, new com.faceclaw.app.FaceclawSseListener(listener)); +} diff --git a/app/native/svg-rasterizer.ios.ts b/app/native/svg-rasterizer.ios.ts new file mode 100644 index 00000000..41ed5d9a --- /dev/null +++ b/app/native/svg-rasterizer.ios.ts @@ -0,0 +1,16 @@ +import { rasterizeSvg as render } from './ios-graphics' +export function rasterizeSvg(svg: string, size: number, strokeWidth: number) { + // Shared icon sources rely on the Android renderer's white, rounded + // stroke defaults. Supply those explicitly for a standards SVG renderer. + // Store icons use currentColor without a surrounding document/theme. + // Resolve it explicitly; SVGKit does not inherit a host CSS color here. + const source = svg.replace(/\bcurrentColor\b/gi, '#ffffff').replace(/]*)>/, (_tag, attributes: string) => { + const defaults = { xmlns: 'http://www.w3.org/2000/svg', width: '24', height: '24', + stroke: svg.includes('fill="none"') ? '#ffffff' : 'none', fill: '#ffffff', 'stroke-width': String(strokeWidth), + 'stroke-linecap': 'round', 'stroke-linejoin': 'round' } + const missing = Object.entries(defaults).filter(([key]) => !new RegExp(`\\b${key}=`).test(attributes)) + .map(([key, value]) => ` ${key}="${value}"`).join('') + return `` + }) + return render(source, size) +} diff --git a/app/native/svg-rasterizer.ts b/app/native/svg-rasterizer.ts new file mode 100644 index 00000000..d93ce43f --- /dev/null +++ b/app/native/svg-rasterizer.ts @@ -0,0 +1,9 @@ +import { GrayImage } from '../graphics/image' +declare const com: any +export function rasterizeSvg(svg: string, size: number, strokeWidth: number): GrayImage | null { + const bytes = com.faceclaw.app.IconRenderer.renderSvgGray(svg, size, strokeWidth) + if (!bytes || bytes.length < size * size) return null + const image = new GrayImage(size, size, 0) + for (let i = 0; i < size * size; i++) image.pixels[i] = bytes[i] & 0xff + return image +} diff --git a/app/native/texture-atlas.ios.ts b/app/native/texture-atlas.ios.ts new file mode 100644 index 00000000..9656982c --- /dev/null +++ b/app/native/texture-atlas.ios.ts @@ -0,0 +1,17 @@ +import { toData } from './kotlin-data' +declare const FaceclawKitIosTextureAtlas: any +const atlas = FaceclawKitIosTextureAtlas.new() + +export function textureAtlasAvailable(): boolean { return true } +export function textureFontId(key: string): number { return atlas.fontIdKey(key) } +export function registerTextureGlyphs(buffer: ArrayBuffer, aa: boolean): void { + const data = toData(new Uint8Array(buffer)) + if (aa) atlas.registerAaGlyphsData(data) + else atlas.registerGlyphsData(data) +} +export function registerFirmwareGlyphs(buffer: ArrayBuffer): void { + atlas.registerFirmwareGlyphsData(toData(new Uint8Array(buffer))) +} +export function textureImageId(key: string, width: number, height: number, pixels: Uint8Array): number { + return atlas.imageIdKeyWidthHeightData(key, width, height, toData(pixels)) +} diff --git a/app/native/texture-atlas.ts b/app/native/texture-atlas.ts new file mode 100644 index 00000000..a934ed40 --- /dev/null +++ b/app/native/texture-atlas.ts @@ -0,0 +1,21 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + +declare const com: any + +// Byte payloads cross as fresh Java buffers: passing a JS ArrayBuffer to Java +// leaks it (java-direct-buffer.ts). + +/** Android bridge to the process-wide shared Kotlin atlases. */ +export function textureAtlasAvailable(): boolean { return !!global.isAndroid } +export function textureFontId(key: string): number { return com.faceclaw.app.GlyphAtlas.fontId(key) } +export function registerTextureGlyphs(buffer: ArrayBuffer, aa: boolean): void { + const reader = new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer))) + if (aa) com.faceclaw.app.GlyphAtlas.registerAa(reader) + else com.faceclaw.app.GlyphAtlas.register(reader) +} +export function registerFirmwareGlyphs(buffer: ArrayBuffer): void { + com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer)))) +} +export function textureImageId(key: string, width: number, height: number, pixels: Uint8Array): number { + return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(pixels))) +} diff --git a/app/native/voice-control.ios.ts b/app/native/voice-control.ios.ts new file mode 100644 index 00000000..a1095779 --- /dev/null +++ b/app/native/voice-control.ios.ts @@ -0,0 +1,149 @@ +import type { VoiceControlState, VoiceTranscriptEvent } from './voice-control' + +declare const FaceclawSpeech: any +export type IosMicrophoneSession = { + setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise +} + +/** iOS implementation of the shared voice dialog's event bridge. Audio comes + * from the glasses when connected, or the default phone input in preview mode. + */ +export class IosVoiceControlBridge { + private native: any = null + private readonly statuses = new Set<(state: VoiceControlState) => void>() + private readonly transcripts = new Set<(event: VoiceTranscriptEvent) => void>() + private readonly ends = new Set<() => void>() + private status = 'Voice input ready.' + private generation = 0 + private session: IosMicrophoneSession | null = null + private source: 'glasses' | 'phone' | null = null + private capturing = false + private finalizing = false + private completion: Promise = Promise.resolve() + private resolveCompletion: (() => void) | null = null + private audioTimer: ReturnType | null = null + private packets = 0 + private log: (message: string) => void = () => {} + + onStatus(listener: (state: VoiceControlState) => void): () => void { + this.statuses.add(listener); listener({ status: this.status, listening: this.capturing, detail: '' }); return () => { this.statuses.delete(listener) } + } + onTranscript(listener: (event: VoiceTranscriptEvent) => void): () => void { + this.transcripts.add(listener); return () => { this.transcripts.delete(listener) } + } + onSpeechEnd(listener: () => void): () => void { this.ends.add(listener); return () => { this.ends.delete(listener) } } + onSpeechPause(_listener: () => void): () => void { return () => {} } + private setStatus(status: string): void { this.status = status; for (const fn of [...this.statuses]) fn({ status, listening: this.capturing, detail: '' }) } + private ensureNative(): any { + if (!this.native) { + this.native = FaceclawSpeech.new() + this.native.eventHandler = (json: string) => this.receive(JSON.parse(json)) + } + return this.native + } + async prepare(foreground: boolean, usePhoneMic = false): Promise { + let status = FaceclawSpeech.authorizationStatus() + if (status === 0) { + if (!foreground) { this.setStatus('Open Faceclaw on the phone once to allow Speech Recognition.'); return false } + status = await new Promise(resolve => FaceclawSpeech.requestAuthorization(resolve)) + } + if (status !== 3) { this.setStatus('Allow Speech Recognition for Faceclaw in iPhone Settings.'); return false } + if (usePhoneMic) { + let microphoneStatus = FaceclawSpeech.microphoneAuthorizationStatus() + if (microphoneStatus === 0) { + if (!foreground) { this.setStatus('Open Faceclaw on the phone to allow microphone access.'); return false } + microphoneStatus = await new Promise(resolve => FaceclawSpeech.requestMicrophoneAuthorization(resolve)) + } + if (microphoneStatus !== 3) { this.setStatus('Allow Microphone access for Faceclaw in iPhone Settings.'); return false } + } + return true + } + get statusText(): string { return this.status } + async startGlassesCapture(session: IosMicrophoneSession, log: (message: string) => void, endpointing = false): Promise { + this.stop() + const generation = ++this.generation + this.log = log; this.source = 'glasses'; this.session = session; this.packets = 0; this.capturing = true + this.completion = new Promise(resolve => { this.resolveCompletion = resolve }) + const error = String(this.ensureNative().startWithEndpointing(endpointing) ?? '') + if (error) { this.stop(); this.setStatus(error); this.emitEnd(); return } + try { + await session.setMicrophone(true, packet => { + if (generation !== this.generation || !this.capturing) return + this.packets++ + const copy = new Uint8Array(packet) + this.native.acceptPacket(NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength)) + }) + if (generation !== this.generation || !this.capturing) return + log('Voice: glasses microphone enabled; using on-device recognition') + this.audioTimer = setTimeout(() => { + this.audioTimer = null + if (generation === this.generation && this.capturing && !this.packets) { + this.stop(); this.setStatus('No microphone audio received. Reconnect the glasses and try again.'); this.emitEnd() + } + }, 5000) + } catch (error) { + if (generation !== this.generation) return + this.stop(); this.setStatus(error instanceof Error ? error.message : String(error)); this.emitEnd() + } + } + async startPhoneCapture(log: (message: string) => void, endpointing = false): Promise { + this.stop() + this.log = log; this.source = 'phone'; this.capturing = true + this.completion = new Promise(resolve => { this.resolveCompletion = resolve }) + const error = String(this.ensureNative().startPhoneWithEndpointing(endpointing) ?? '') + if (error) { this.stop(); this.setStatus(error); this.emitEnd(); return } + log('Voice: phone default microphone enabled; using on-device recognition') + } + stopPhoneCapture(): void { + if (this.source !== 'phone') return + this.stop(); this.setStatus('Phone voice input stopped.'); this.emitEnd() + } + stopPushToTalk(): Promise { + const completion = this.completion + if (!this.capturing) return completion + this.capturing = false; this.finalizing = true + this.clearAudioTimer(); this.releaseMicrophone() + this.setStatus('Recognizing…'); this.native?.finish() + return completion + } + stop(): void { + ++this.generation + this.capturing = this.finalizing = false + this.source = null + this.clearAudioTimer(); this.releaseMicrophone(); this.native?.cancel() + this.finishCompletion() + } + handleSessionEnded(message = 'Glasses disconnected. Start voice input again after reconnecting.'): void { + if (this.source !== 'glasses' || !this.capturing && !this.finalizing) return + this.stop(); this.setStatus(message); this.emitEnd() + } + private clearAudioTimer(): void { if (this.audioTimer !== null) clearTimeout(this.audioTimer); this.audioTimer = null } + private releaseMicrophone(): void { + const session = this.session; this.session = null + if (session) void session.setMicrophone(false).catch(error => this.log(`Voice microphone cleanup: ${error}`)) + } + private emitEnd(): void { for (const fn of [...this.ends]) fn() } + private finishCompletion(): void { + const resolve = this.resolveCompletion; this.resolveCompletion = null; resolve?.() + } + private receive(event: { kind: string; text?: string; final?: boolean; message?: string; seconds?: number; packets?: number; rms?: number; missing?: number; errors?: number }): void { + if (!this.capturing && !this.finalizing) return + if (event.kind === 'transcript') { + this.log(`Voice: ${event.final ? 'final' : 'partial'} transcript (${event.text?.length ?? 0} characters)`) + for (const fn of [...this.transcripts]) fn({ text: event.text ?? '', isFinal: !!event.final }) + } else if (event.kind === 'status') this.setStatus(event.message ?? '') + else if (event.kind === 'audio') { + this.log(`Voice audio: ${event.seconds?.toFixed(1)}s, packets=${event.packets}, RMS=${event.rms?.toFixed(4)}, missing=${event.missing}, errors=${event.errors}`) + if (this.capturing) this.setStatus(`Listening on ${this.source === 'phone' ? 'phone' : 'glasses'}… ${event.seconds?.toFixed(0)}s`) + } else if (event.kind === 'finishing') { + this.stopPushToTalk(); this.emitEnd() + } else if (event.kind === 'ended') { + this.source = null + this.capturing = this.finalizing = false + this.clearAudioTimer(); this.releaseMicrophone() + this.finishCompletion() + this.setStatus(event.message ?? 'Ready to send'); this.emitEnd() + } + } +} +export const voiceControlBridge = new IosVoiceControlBridge() diff --git a/app/native/weather.ts b/app/native/weather.ts index 8cc073e9..f1e98a5f 100644 --- a/app/native/weather.ts +++ b/app/native/weather.ts @@ -1,4 +1,4 @@ -import { hasLocationPermission } from "../g2/android-permissions"; +import { hasLocationPermission } from "./location-permissions"; import { getCurrentLocation, type CurrentLocation } from "./location"; import { fetchWithUserAgent } from "../util/http"; import { USER_AGENT } from "../version"; diff --git a/app/native/worker-buzzer.ts b/app/native/worker-buzzer.ts new file mode 100644 index 00000000..b6730f6a --- /dev/null +++ b/app/native/worker-buzzer.ts @@ -0,0 +1,17 @@ +import type { WorkerAppReply } from "../ui/shell/worker-window"; +import { JavaDirectBuffer } from "./java-direct-buffer"; + +declare const com: any; + +// Reused Java-side buffer for this isolate's tone payloads: passing a JS +// ArrayBuffer to Java leaks it (java-direct-buffer.ts). +const buzzerBuffer = new JavaDirectBuffer(); + +/** Workers use the main-thread BLE session on iOS and the native queue on Android. */ +export function playWorkerBuzzerSequence(payload: Uint8Array): void { + if (global.isIOS) { + global.postMessage({ type: "buzzer-sequence", payload: Array.from(payload) } satisfies WorkerAppReply); + } else { + com.faceclaw.app.FaceclawBleCommunicator.getActive()?.playBuzzerSequence(buzzerBuffer.load(payload)); + } +} diff --git a/app/phone-ui/README.md b/app/phone-ui/README.md new file mode 100644 index 00000000..c8d13a6c --- /dev/null +++ b/app/phone-ui/README.md @@ -0,0 +1,33 @@ +# Main-screen UI + +Android and iOS share `remote-controls.xml`, `remote-controls.css`, and +`RemoteControlsViewModel`: tab order and selection, screen-size choices, +brightness/Auto settings, watch labels, ring markings, and microphone/keyboard +buttons. Import the stylesheet at the top of each platform's application CSS +(before any rules), so the NativeScript CSS pipeline includes it. + +Android's `MainViewModel` extends that shared model and connects it to +`dashboardController`. iOS loads the same XML with `Builder.load` and connects +it to `IosPreviewController`. Keep platform-specific transport, page lifecycle, +and navigation in those adapters. Android uses NativeScript gesture events; +iOS feeds raw touches to `PhoneGestureRecognizer`, so the discrete pad gesture +bindings in the XML are Android-only to avoid delivering each gesture twice. + +The iOS shell uses the same portrait preview-above-controls layout and 345-DIP +landscape controls column. It respects safe areas, uses the same overflow menu +labels/order, and retains device diagnostics under the connection status. +Conversations and Record screen are disabled on iOS because their storage and +recording services are currently Android-only. Take screenshot opens the iOS +share sheet. Text settings still use native dialogs. Keyboard input uses the +shared `keyboard-input-panel.xml` multiline editor and shell keyboard session, +with separate Agent and App destinations when available. iOS keeps the editor +above the software keyboard in both orientations. + +Voice input uses the glasses microphone when connected and the phone's default +audio input in preview mode. The phone path requires both Speech Recognition +and Microphone permissions and stops when Faceclaw leaves the foreground; +connected-glasses capture retains its existing background behavior. + +The iOS microphone and keyboard image assets are 3x rasterizations of the +Android vector drawables with the same names; regenerate them from those +vectors if the icon designs change. diff --git a/app/phone-ui/ble-bandwidth-meter.ts b/app/phone-ui/ble-bandwidth-meter.ts new file mode 100644 index 00000000..b534f009 --- /dev/null +++ b/app/phone-ui/ble-bandwidth-meter.ts @@ -0,0 +1,27 @@ +import type { BleTrafficSample } from '../native/ble-traffic' + +/** Shared Android/iOS five-second traffic readout. Poll once per second. */ +export class BleBandwidthMeter { + private history: Array<{ atMs: number; bytes: number; frames: number }> = [] + reset(): void { this.history = [] } + sample(sample: BleTrafficSample, atMs: number): string { + const previous = this.history[this.history.length - 1] + if (previous && (atMs <= previous.atMs || sample.bytes < previous.bytes || sample.frames < previous.frames)) this.reset() + this.history.push({ atMs, bytes: sample.bytes, frames: sample.frames }) + while (this.history.length && this.history[0]!.atMs < atMs - 5000) this.history.shift() + let label = `BLE sent: ${formatCount(sample.messages)} messages, ${formatCount(sample.bytes)} bytes` + const oldest = this.history[0]!, elapsedSec = (atMs - oldest.atMs) / 1000 + if (elapsedSec > 0) { + const byteDelta = sample.bytes - oldest.bytes, frameDelta = sample.frames - oldest.frames + label += ` · ${formatByteRate(byteDelta / elapsedSec)}, ${(frameDelta / elapsedSec).toFixed(1)} fps` + if (frameDelta > 0) label += `, ${formatCount(byteDelta / frameDelta)} B/frame` + } + return label + } +} +function formatCount(value: number): string { return String(Math.round(value)).replace(/\B(?=(\d{3})+(?!\d))/g, ',') } +function formatByteRate(bytesPerSec: number): string { + if (bytesPerSec >= 1e6) return (bytesPerSec / 1e6).toFixed(2) + ' MB/s' + if (bytesPerSec >= 1e3) return (bytesPerSec / 1e3).toFixed(1) + ' kB/s' + return Math.round(bytesPerSec) + ' B/s' +} diff --git a/app/phone-ui/config-page-shared.ts b/app/phone-ui/config-page-shared.ts new file mode 100644 index 00000000..301d0039 --- /dev/null +++ b/app/phone-ui/config-page-shared.ts @@ -0,0 +1,13 @@ +import { NavigatedData, Page } from "@nativescript/core"; + +import { ConfigViewModel } from "./config-view-model"; + +export function navigatingTo(args: NavigatedData): void { + const page = args.object as Page; + // Preserve the model across back-navigation (returning from the firmware + // check page) so the onboarding "Continue" flow isn't lost. + if (!page.bindingContext) { + const context = (args.context as { onboarding?: boolean } | undefined) ?? undefined; + page.bindingContext = new ConfigViewModel({ onboarding: context?.onboarding ?? false }); + } +} diff --git a/app/phone-ui/config-page.ios.ts b/app/phone-ui/config-page.ios.ts new file mode 100644 index 00000000..b242bfe6 --- /dev/null +++ b/app/phone-ui/config-page.ios.ts @@ -0,0 +1 @@ +export { navigatingTo } from "./config-page-shared"; diff --git a/app/phone-ui/config-page.ts b/app/phone-ui/config-page.ts index 301d0039..b242bfe6 100644 --- a/app/phone-ui/config-page.ts +++ b/app/phone-ui/config-page.ts @@ -1,13 +1 @@ -import { NavigatedData, Page } from "@nativescript/core"; - -import { ConfigViewModel } from "./config-view-model"; - -export function navigatingTo(args: NavigatedData): void { - const page = args.object as Page; - // Preserve the model across back-navigation (returning from the firmware - // check page) so the onboarding "Continue" flow isn't lost. - if (!page.bindingContext) { - const context = (args.context as { onboarding?: boolean } | undefined) ?? undefined; - page.bindingContext = new ConfigViewModel({ onboarding: context?.onboarding ?? false }); - } -} +export { navigatingTo } from "./config-page-shared"; diff --git a/app/phone-ui/config-page.xml b/app/phone-ui/config-page.xml index cffe6f9e..d1b7288d 100644 --- a/app/phone-ui/config-page.xml +++ b/app/phone-ui/config-page.xml @@ -16,21 +16,21 @@ -