Skip to content

RF Sentinel (RFSentinel): free, open-source police detector app for Android. Spots police body cams, Flock ALPR cameras, AirTag trackers, Remote ID drones, smart glasses and fake cell towers from Bluetooth LE and WiFi. No root.

P25 voice step 3: IMBE vocoder (mbelib port), recordings to speech

CIS-C04ad578479f4a
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/ImbeDecoder.kt b/app/src/main/java/com/rfsentinel/app/sdr/ImbeDecoder.kt
new file mode 100644
index 0000000..3999b4e
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/sdr/ImbeDecoder.kt
@@ -0,0 +1,377 @@
+package com.rfsentinel.app.sdr
+
+import kotlin.math.E
+import kotlin.math.PI
+import kotlin.math.cos
+import kotlin.math.ln
+import kotlin.math.pow
+import kotlin.math.sqrt
+import kotlin.random.Random
+
+/**
+ * P25 voice, third step: the IMBE 4400 vocoder, from a codeword's 88 info bits ([P25Voice.Imbe]) to 20 ms of speech,
+ * 160 samples at 8 kHz. One decoder per call: each codeword's spectrum is coded against the one before it.
+ * A port of mbelib (ISC licence, after TIA-102.BABA): its decoding, spectral enhancement and synthesis, its tables;
+ * a damaged codeword repeats the last good one, up to three times, then the call goes silent.
+ */
+class ImbeDecoder(private val rnd: Random = Random(1)) {
+
+ /** One codeword's model parameters: fundamental, harmonics, voicing, magnitudes, phases (index 1..L). */
+ private class Parms {
+ var w0 = 0.0
+ var L = 0
+ var repeat = 0
+ val Vl = IntArray(SLOTS)
+ val Ml = DoubleArray(SLOTS)
+ val log2Ml = DoubleArray(SLOTS)
+ val PHIl = DoubleArray(SLOTS)
+ val PSIl = DoubleArray(SLOTS)
+
+ fun copyFrom(o: Parms) {
+ w0 = o.w0; L = o.L; repeat = o.repeat
+ o.Vl.copyInto(Vl); o.Ml.copyInto(Ml); o.log2Ml.copyInto(log2Ml); o.PHIl.copyInto(PHIl); o.PSIl.copyInto(PSIl)
+ }
+ }
+
+ private val cur = Parms()
+ private val prev = Parms()
+ private val prevEnhanced = Parms()
+
+ init { reset() }
+
+ private fun reset() {
+ prev.w0 = 0.09378; prev.L = 30; prev.repeat = 0
+ prev.Vl.fill(0); prev.Ml.fill(0.0); prev.log2Ml.fill(0.0); prev.PHIl.fill(0.0); prev.PSIl.fill(PI / 2)
+ cur.copyFrom(prev); prevEnhanced.copyFrom(prev)
+ }
+
+ /** 20 ms of speech from one codeword; [errors]: the bits its error correction fixed. */
+ fun decode(bits: ByteArray, errors: Int): ShortArray {
+ val out = DoubleArray(N)
+ if (!parms(bits) || errors > 5) {
+ cur.copyFrom(prev)
+ cur.repeat++
+ } else cur.repeat = 0
+ if (cur.repeat <= 3) {
+ prev.copyFrom(cur)
+ enhance()
+ synthesize(out)
+ prevEnhanced.copyFrom(cur)
+ } else reset()
+ return ShortArray(N) { (out[it] * GAIN).coerceIn(-32760.0, 32760.0).toInt().toShort() }
+ }
+
+ /** The codeword's bits to [cur]; false when they hold no speech (silence, or not valid). */
+ private fun parms(d: ByteArray): Boolean {
+ cur.repeat = prev.repeat
+ var b0 = 0
+ for (i in intArrayOf(0, 1, 2, 3, 4, 5, 85, 86)) b0 = b0 shl 1 or d[i].toInt()
+ if (b0 > 207) return false
+ val w0 = 4 * PI / (b0 + 39.5)
+ val L = (0.9254 * (PI / w0 + 0.25).toInt()).toInt()
+ if (L !in 9..56) return false
+ cur.w0 = w0; cur.L = L
+ val L9 = L - 9
+ val K = if (L < 37) (L + 2) / 3 else 12
+
+ // The info bits, priority ordered, back to b_m (bit k of each).
+ val bb = Array(58) { IntArray(12) }
+ for (i in 6 until 85) { val p = L9 * 79 + i - 6; bb[BO_M[p] - '0'][BO_B[p] - '0'] = d[i].toInt() }
+ fun value(m: Int, bits: Int): Int { var v = 0; for (x in bits - 1 downTo 0) v = v shl 1 or bb[m][x]; return v }
+
+ // Voicing: one bit per three harmonics.
+ var j = 1; var k = K - 1
+ for (l in 1..L) {
+ cur.Vl[l] = bb[1][k]
+ if (j == 3) { j = 1; if (k > 0) k-- } else j++
+ }
+
+ // Gains G1..G6, inverse DCT to the block averages R1..R6.
+ val g = DoubleArray(7)
+ g[1] = B2[value(2, 6)].toDouble()
+ for (i in 2..6) {
+ val bits = BA[L9 * 10 + 2 * (i - 2)].toInt(); val step = BA[L9 * 10 + 2 * (i - 2) + 1].toDouble()
+ g[i] = step * (value(i + 1, bits) - 2.0.pow(bits - 1) + 0.5)
+ }
+ // Each block: its average and higher order DCT coefficients, inverse DCT to its share of the L residuals.
+ val t = DoubleArray(SLOTS)
+ var m = 8; var l = 1
+ for (i in 1..6) {
+ var r = 0.0
+ for (n in 1..6) r += (if (n == 1) 1 else 2) * g[n] * cos(PI * (n - 1) * (i - 0.5) / 6)
+ val ji = L / 6 + if (i > 6 - L % 6) 1 else 0
+ val c = DoubleArray(ji + 1)
+ c[1] = r
+ for (kk in 2..ji) {
+ val bm = HOBA[L9 * 50 + m - 8] - '0'
+ c[kk] = if (bm == 0) 0.0 else QUANT_STEP[bm - 1] * STAND_DEV[kk - 2] * (value(m, bm) - 2.0.pow(bm - 1) + 0.5)
+ m++
+ }
+ for (jj in 1..ji) {
+ var s = 0.0
+ for (kk in 1..ji) s += (if (kk == 1) 1 else 2) * c[kk] * cos(PI * (kk - 1) * (jj - 0.5) / ji)
+ t[l++] = s
+ }
+ }
+
+ // The residuals add to the previous spectrum, resampled to L harmonics.
+ val rho = when { L <= 15 -> 0.4; L <= 24 -> 0.03 * L - 0.05; else -> 0.7 }
+ if (L > prev.L) for (x in prev.L + 1..L) { prev.Ml[x] = prev.Ml[prev.L]; prev.log2Ml[x] = prev.log2Ml[prev.L] }
+ val kl = IntArray(L + 1); val dl = DoubleArray(L + 1)
+ var sum = 0.0
+ for (x in 1..L) {
+ val f = prev.L.toDouble() / L * x
+ kl[x] = f.toInt(); dl[x] = f - kl[x]
+ sum += (1 - dl[x]) * prev.log2Ml[kl[x]] + dl[x] * prev.log2Ml[kl[x] + 1]
+ }
+ sum *= rho / L
+ for (x in 1..L) {
+ cur.log2Ml[x] = t[x] + rho * (1 - dl[x]) * prev.log2Ml[kl[x]] + rho * dl[x] * prev.log2Ml[kl[x] + 1] - sum
+ cur.Ml[x] = 2.0.pow(cur.log2Ml[x])
+ }
+ return true
+ }
+
+ /** Spectral amplitude enhancement: sharpens formants, keeps the energy. */
+ private fun enhance() {
+ val L = cur.L; val w0 = cur.w0; val ml = cur.Ml
+ var rm0 = 0.0; var rm1 = 0.0
+ for (l in 1..L) { rm0 += ml[l] * ml[l]; rm1 += ml[l] * ml[l] * cos(w0 * l) }
+ val r2m0 = rm0 * rm0; val r2m1 = rm1 * rm1
+ for (l in 1..L) {
+ if (ml[l] == 0.0 || 8 * l <= L) continue
+ val w = sqrt(ml[l]) * (0.96 * PI * (r2m0 + r2m1 - 2 * rm0 * rm1 * cos(w0 * l)) / (w0 * rm0 * (r2m0 - r2m1))).pow(0.25)
+ ml[l] *= when { w > 1.2 -> 1.2; w < 0.5 -> 0.5; else -> w }
+ }
+ var sum = 0.0
+ for (l in 1..L) sum += ml[l] * ml[l]
+ val gamma = if (sum == 0.0) 1.0 else sqrt(rm0 / sum)
+ for (l in 1..L) ml[l] *= gamma
+ }
+
+ private fun randPhase() = rnd.nextDouble() * 2 * PI - PI
+
+ /** 160 samples from [prevEnhanced] fading into [cur]: voiced harmonics as sines, unvoiced ones as a noisy sine mix. */
+ private fun synthesize(out: DoubleArray) {
+ val c = cur; val p = prevEnhanced
+ val cw0 = c.w0; val pw0 = p.w0
+ val numUv = (1..c.L).count { c.Vl[it] == 0 }
+ val maxl = if (c.L > p.L) {
+ for (l in p.L + 1..c.L) { p.Ml[l] = 0.0; p.Vl[l] = 1 }
+ c.L
+ } else {
+ for (l in c.L + 1..p.L) { c.Ml[l] = 0.0; c.Vl[l] = 1 }
+ p.L
+ }
+ for (l in 1..56) {
+ c.PSIl[l] = p.PSIl[l] + (pw0 + cw0) * (l * N) / 2.0
+ c.PHIl[l] = if (l <= c.L / 4) c.PSIl[l] else c.PSIl[l] + numUv * randPhase() / c.L
+ }
+ val rphase = DoubleArray(UV_QUALITY)
+ // Unvoiced: UV_QUALITY sines spread around harmonic l, plus noise above 2.7 kHz.
+ fun unvoiced(w0: Double, l: Int, n: Int): Double {
+ var s = 0.0
+ for (i in 0 until UV_QUALITY) {
+ s += cos(w0 * n * (l + i * UV_STEP - UV_OFFSET) + rphase[i])
+ if (w0 * l > UV_THRESHOLD) s += (w0 * l - UV_THRESHOLD) * UV_RAND * rnd.nextDouble()
+ }
+ return s * UV_SINE * Q_FACTOR
+ }
+ for (l in 1..maxl) {
+ val cv = c.Vl[l] == 1; val pv = p.Vl[l] == 1
+ when {
+ !cv && pv -> {
+ for (i in rphase.indices) rphase[i] = randPhase()
+ for (n in 0 until N) out[n] += ws(n + N) * p.Ml[l] * cos(pw0 * l * n + p.PHIl[l]) +
+ unvoiced(cw0, l, n) * ws(n) * c.Ml[l]
+ }
+ cv && !pv -> {
+ for (i in rphase.indices) rphase[i] = randPhase()
+ for (n in 0 until N) out[n] += ws(n) * c.Ml[l] * cos(cw0 * l * (n - N) + c.PHIl[l]) +
+ unvoiced(pw0, l, n) * ws(n + N) * p.Ml[l]
+ }
+ cv || pv -> for (n in 0 until N) out[n] += ws(n + N) * p.Ml[l] * cos(pw0 * l * n + p.PHIl[l]) +
+ ws(n) * c.Ml[l] * cos(cw0 * l * (n - N) + c.PHIl[l])
+ else -> {
+ for (i in rphase.indices) rphase[i] = randPhase()
+ val prevPart = DoubleArray(N) { n -> unvoiced(pw0, l, n) * ws(n + N) * p.Ml[l] }
+ for (i in rphase.indices) rphase[i] = randPhase()
+ for (n in 0 until N) out[n] += prevPart[n] + unvoiced(cw0, l, n) * ws(n) * c.Ml[l]
+ }
+ }
+ }
+ }
+
+ companion object {
+ /** Samples per codeword: 20 ms at 8 kHz. */
+ const val N = 160
+ const val RATE = 8000
+ private const val SLOTS = 58
+ private const val GAIN = 7.0
+ private const val UV_QUALITY = 3
+ private const val UV_STEP = 1.0 / UV_QUALITY
+ private const val UV_OFFSET = UV_STEP * (UV_QUALITY - 1) / 2
+ private val Q_FACTOR = ln(UV_QUALITY.toDouble()) / UV_QUALITY
+ private const val UV_THRESHOLD = 2700 * PI / 4000
+ private const val UV_SINE = 1.3591409 * E
+ private const val UV_RAND = 2.0
+
+ /** The synthesis window (mbelib's Ws): 0, a 50-sample ramp, flat around n = 160, back down. */
+ private fun ws(n: Int) = (minOf(n - 55, 265 - n) / 50.0).coerceIn(0.0, 1.0)
+
+ /** Gain b2 → G1. */
+ private val B2 = floatArrayOf(
+ -2.842205f, -2.694235f, -2.558260f, -2.382850f, -2.221042f, -2.095574f, -1.980845f, -1.836058f,
+ -1.645556f, -1.417658f, -1.261301f, -1.125631f, -0.958207f, -0.781591f, -0.555837f, -0.346976f,
+ -0.147249f, 0.027755f, 0.211495f, 0.388380f, 0.552873f, 0.737223f, 0.932197f, 1.139032f,
+ 1.320955f, 1.483433f, 1.648297f, 1.801447f, 1.942731f, 2.118613f, 2.321486f, 2.504443f,
+ 2.653909f, 2.780654f, 2.925355f, 3.076390f, 3.220825f, 3.402869f, 3.585096f, 3.784606f,
+ 3.955521f, 4.155636f, 4.314009f, 4.444150f, 4.577542f, 4.735552f, 4.909493f, 5.085264f,
+ 5.254767f, 5.411894f, 5.568094f, 5.738523f, 5.919215f, 6.087701f, 6.280685f, 6.464201f,
+ 6.647736f, 6.834672f, 7.022583f, 7.211777f, 7.471016f, 7.738948f, 8.124863f, 8.695827f,
+ )
+ /** Per L (9..56), for G2..G6: bits, step size. */
+ private val BA = floatArrayOf(
+ 10f, 0.003100f, 9f, 0.004020f, 9f, 0.003360f, 9f, 0.002900f, 9f, 0.002640f,
+ 9f, 0.006200f, 9f, 0.004020f, 8f, 0.006720f, 8f, 0.005800f, 8f, 0.005280f,
+ 8f, 0.012400f, 8f, 0.008040f, 8f, 0.006720f, 7f, 0.011600f, 7f, 0.010560f,
+ 8f, 0.012400f, 7f, 0.016080f, 7f, 0.013440f, 7f, 0.011600f, 7f, 0.010560f,
+ 7f, 0.024800f, 7f, 0.016080f, 7f, 0.013440f, 6f, 0.021750f, 6f, 0.019800f,
+ 7f, 0.024800f, 6f, 0.030150f, 6f, 0.025200f, 6f, 0.021750f, 6f, 0.019800f,
+ 7f, 0.024800f, 6f, 0.030150f, 6f, 0.025200f, 6f, 0.021750f, 5f, 0.036960f,
+ 6f, 0.046500f, 6f, 0.030150f, 6f, 0.025200f, 5f, 0.040600f, 5f, 0.036960f,
+ 6f, 0.046500f, 6f, 0.030150f, 5f, 0.047040f, 5f, 0.040600f, 5f, 0.036960f,
+ 6f, 0.046500f, 5f, 0.056280f, 5f, 0.047040f, 5f, 0.040600f, 5f, 0.036960f,
+ 6f, 0.046500f, 5f, 0.056280f, 5f, 0.047040f, 4f, 0.058000f, 4f, 0.052800f,
+ 6f, 0.046500f, 5f, 0.056280f, 5f, 0.047040f, 4f, 0.058000f, 4f, 0.052800f,
+ 5f, 0.086800f, 5f, 0.056280f, 5f, 0.047040f, 4f, 0.058000f, 4f, 0.052800f,
+ 5f, 0.086800f, 5f, 0.056280f, 4f, 0.067200f, 4f, 0.058000f, 4f, 0.052800f,
+ 5f, 0.086800f, 4f, 0.080400f, 4f, 0.067200f, 4f, 0.068000f, 4f, 0.052800f,
+ 5f, 0.086800f, 4f, 0.080400f, 4f, 0.067200f, 4f, 0.058000f, 4f, 0.052800f,
+ 5f, 0.086800f, 4f, 0.080400f, 4f, 0.067200f, 4f, 0.058000f, 3f, 0.085800f,
+ 5f, 0.086800f, 4f, 0.080400f, 4f, 0.067200f, 3f, 0.094250f, 3f, 0.085800f,
+ 5f, 0.086800f, 4f, 0.080400f, 4f, 0.067200f, 3f, 0.094250f, 3f, 0.085800f,
+ 4f, 0.124000f, 4f, 0.080400f, 4f, 0.067200f, 3f, 0.094250f, 3f, 0.085800f,
+ 4f, 0.124000f, 4f, 0.080400f, 4f, 0.067200f, 3f, 0.094250f, 3f, 0.085800f,
+ 4f, 0.124000f, 4f, 0.080400f, 4f, 0.067200f, 3f, 0.094250f, 3f, 0.085800f,
+ 4f, 0.124000f, 4f, 0.080400f, 3f, 0.109200f, 3f, 0.094250f, 3f, 0.085800f,
+ 4f, 0.124000f, 4f, 0.080400f, 3f, 0.109200f, 3f, 0.094250f, 3f, 0.085800f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 3f, 0.094250f, 3f, 0.085800f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 3f, 0.094250f, 3f, 0.085800f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 3f, 0.094250f, 3f, 0.085800f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 3f, 0.094250f, 3f, 0.085800f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 3f, 0.094250f, 2f, 0.112200f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 3f, 0.094250f, 2f, 0.112200f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 3f, 0.094250f, 2f, 0.112200f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 3f, 0.094250f, 2f, 0.112200f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 4f, 0.124000f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 3f, 0.109200f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 2f, 0.142800f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 2f, 0.142800f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 2f, 0.142800f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 2f, 0.142800f, 2f, 0.123250f, 2f, 0.112200f,
+ 3f, 0.201500f, 3f, 0.130650f, 2f, 0.142800f, 2f, 0.123250f, 2f, 0.112200f,
+ )
+ private val QUANT_STEP = floatArrayOf(1.2f, 0.85f, 0.65f, 0.40f, 0.28f, 0.15f, 0.08f, 0.04f, 0.02f, 0.01f)
+ private val STAND_DEV = floatArrayOf(0.307f, 0.241f, 0.207f, 0.190f, 0.179f, 0.173f, 0.165f, 0.170f, 0.170f)
+ /** Per L, 50 values: bits of each higher order DCT coefficient b8.. (char - '0'). */
+ private const val HOBA =
+ "987000000000000000000000000000000000000000000000009765000000000000000000000000000000000000000000000097654000000000000000" +
+ "000000000000000000000000000000876543000000000000000000000000000000000000000000007765433000000000000000000000000000000000" +
+ "000000000077544343000000000000000000000000000000000000000000675443333000000000000000000000000000000000000000006654433332" +
+ "000000000000000000000000000000000000000055544433232000000000000000000000000000000000000000545543333222000000000000000000" +
+ "000000000000000000005454433332321000000000000000000000000000000000000054544332321321000000000000000000000000000000000000" +
+ "445443322321321000000000000000000000000000000000004444432322321221000000000000000000000000000000000043443432322221221000" +
+ "000000000000000000000000000000433433332321221221000000000000000000000000000000004334333323212212111000000000000000000000" +
+ "000000000043343332232122112211000000000000000000000000000000432432322322122112211000000000000000000000000000004324323222" +
+ "321122112111000000000000000000000000000033243223222321121112111000000000000000000000000000332233223221321121112111000000" +
+ "000000000000000000003322332232212211211121111000000000000000000000000033223322322122112111121110000000000000000000000000" +
+ "332233223211221112111121111000000000000000000000003222322232211221112111121110000000000000000000000032223222232111221112" +
+ "111021110000000000000000000000322213222132111221112111021110000000000000000000003222132222321112111121110211110000000000" +
+ "000000000032221322213211121111211110211110000000000000000000322213222132111221110211110211100000000000000000003222132211" +
+ "321111221110211110211100000000000000000032211322211321111221110211110211100000000000000000322211322211221111221110211100" +
+ "211100000000000000003222113222112211112111102111002111100000000000000032211132221122111121111021111002111100000000000000" +
+ "322111322111221111221110021111002111100000000000003221113221112211111221110021111002111100000000000032211132211112211111" +
+ "221110021111002111000000000000322111132211112211111221110021110002111000000000003221111322111122111102211100211100021111" +
+ "000000000032211113221111221111022111002111100021110000000000322111132111112211110221110002111100021111000000003211111322" +
+ "111122111110221110002111100021111000000032111113221111122111110221110002111100021110000000322111103221111022111110221110" +
+ "002111100021110000003221111032211110221111102211100021110000211110000032211110322111102211111022111000211110000211100000"
+ /** Per L, 79 values: which b_m each of info bits 6..84 belongs to (char - '0'). */
+ internal const val BO_M =
+ "222334567834567893456789:3456789:3456789:311122456789:3456789:3456789:3456789:222234834567834567893456789:3456789:;34567" +
+ "81111229:;3456789:;3456789:;3456789:;22228345834567893456789:3456789:;3456789:;<1111223456789:;<3456789:;<3456789:;<2222" +
+ "3834567893456789:3456789:;3456789:;<3451111226789:;<=3456789:;<=3456789:;<=2222345893456789:3456789:;3456789:;<345678911" +
+ "11122:;<=>3456789:;<=>3456789:;<=>222238934567893456789:3456789:;<>3456789:;<1111122=>?3456789:;<=>?3456789:;<=>?2222393" +
+ "456893456789:3456789:;<3456789:;<=>?1111122@3456789:;<=>?@3456789:;<=>?@2222345893456789:3456789:;<3456789:;<=>?@3411111" +
+ "12256789:;<=>?@A3456789:;<=>?@A2222343456789:3456789:;<=3456789:;<=>?A345611111122789:;<=>?@AB3456789:;<=>?@AB2222334567" +
+ "8:;3456789:;<3456789:;<=>?@345678111111229:;<=>?@ABC3456789:;<=>?@ABC222233458:3456789:;<3456789:;<=>?@B3456789:11111112" +
+ "2;<=>?@ABC3456789:;<=>?@ABCD222233458:3456789:;<3456789:;<=>@C3456789:;111111122<=>?@ACD3456789:;<=>?@ABCDE2222345:34567" +
+ "89:;<3456789:;<=>AD3456789:;<=111111122>?@ABDE3456789:;<=>?@ABCDEF2222343456789:;<3456789:;<=?B3456789:;<=>?@1111111122A" +
+ "BCEF3456789:;<=>?@ABCDEFG22223345678:;=3456789:;<=>@3456789:;<=>?@AB1111111122CDFG3456789:;<=>?@ABCDEFGH22223345678;3456" +
+ "789:;<=>?A3456789:;<=>?@ABD1111111122EGH3456789:;<=>?@ABCDEFGHI2222334568;3456789:;<=>?A3456789:;<=>?@ABDE11111111122G34" +
+ "56789:;<=>?@ABCDEFGHIJ222233458;3456789:;<=>A3456789:;<=>?@ABDEHI111111111223456789:;<=>?@ABCDEFGHIJK222233458;3456789;<" +
+ ">A3456789:;<=>?@ABCEFIJ311111111122456789:;<=>?@ABCDEFGHIJKL22223458;3456789;<>B3456789:;<=>?@ABCFGJ3451111111111226789:" +
+ ";<=>?@ABCDEFGHIJKLM2222345;3456789;<?C3456789:;<=>?@ABCDGK3456111111111122789:;<=>?@ABCDEFGHIJKLMN22223453456789<=@D3456" +
+ "789:;<=>?@ABDEHL3456711111111112289:;<=>?@ABCDEFGHIJKLMNO2222343456789<=@3456789:;<=>?@ABDEHL34567891111111111122:;<=>?@" +
+ "ABCDEFGHIJKLMNOP2222343456789<=@3456789:;<=>?@ABDEHM34567891111111111122:;<=>?@ABCDEFGHIJKLMNOP222233456789<=@3456789:;<" +
+ "=>?@ADEIN3456789:;1111111111122<=>?@ABCDEFGHIJKLMNOPQR22223345678<@3456789:;<=>?@ABEFJO3456789:;<11111111111122=>?@ABCDE" +
+ "FGHIJKLMNOPQR22223345678<A3456789:;<=>?@ABFGKP3456789:;<11111111111122=>?@ABCDEFGHIJKLMNPQRS22223345678=B3456789:;=>?@BC" +
+ "GHLQ3456789:;<=11111111111122>?@ABCDEFGHIJKLMNOQRST2222334568=B3456789:;=>?@ABCGLQ3456789:;<=>11111111111122?@ABCDEFGHIJ" +
+ "KLMNOQRSTU2222334568=B3456789:;=>?@BCGLR3456789:;<=>?11111111111122@ABCDEFGHIJKLMNOPRSTUV2222334568=B3456789:;=>?@BCGHMS" +
+ "3456789:;<=>11111111111122?@ABCDEFGHIJKMNOPQSTUV2222334568=B3456789:;=>?BCHINT3456789:;<=>?11111111111122@ABCDEFGHIJKLNO" +
+ "PQRTUVW222233458=C3456789:=>?@CDIJOU3456789:;<=>?@11111111111122ABCDEFGHIJKLMOPQRSUVWX222233458>3456789:;>?@ADEJKPV34567" +
+ "89:;<=>?@11111111111122ABCDEFGHIJKLMNPQRSVWXY222233458>3456789:;>?@ADEJPV3456789:;<=>?@A11111111111122BCDEFGHIJKLMNPQRSV" +
+ "WXYZ222233458>3456789:>?@ADEJPW3456789:;<=>?@AB11111111111122CDEFGHIJKLMNPQRSTWXYZ[222233458>3456789:>?@DEJKQX3456789:;<" +
+ "=>?@AB11111111111122CDEFGHIJKLMNQRSTUXYZ[\\22223458>3456789:>?@DEKLRY3456789:;<=>?@ABC11111111111122DEFGHIJKLMNORSTUVYZ[" +
+ "\\]22223458>3456789:>?@EFLMSZ3456789:;<=>?@ABC11111111111122DEFGHIJKLMNOPSTUVWZ[\\]22223458?3456789:?@AFGMNT[3456789:;<=" +
+ ">?@ABC11111111111122DEFGHIJKLMNOPQTUVW[\\]^22223458?3456789:?@AFGMNT[3456789:;<=>?@ABC11111111111122DEFGHIJKMNOPQTUVW[\\" +
+ "]^_22223458?3456789:?@AFGMNT\\3456789:;<=>?@ABC11111111111122DEFGHIJKMNOPQTUVWX\\]^_22223458?3456789:?@FGMNU]3456789:;<=" +
+ ">?@ABCD11111111111122EFGHIJKMNOPQUVWXY]^_`a2222348?3456789?@AFGNOV^3456789:;<=>?@ABCDE11111111111122FGHIJKLNOPQRVWXYZ^_`" +
+ "ab2222348?3456789?@AGHOPW_3456789:;<=>?@ABCDE11111111111122FGHIJKLMOPQRSWXYZ[_`ab2222348@3456789:@ABHIPQX`3456789:;<=>@A" +
+ "BCDE11111111111122FHIJKLMNPQRSTXYZ[\\`abc2222348@3456789:@ABHIPQX`3456789:;<=>@ABCDE11111111111122FHIJKLMNPQRSTXYZ[`abcd" +
+ "2222348@3456789:@ABHIPQXa3456789:;<=>@ABCDE11111111111122FHIJKLMNPQRSTXYZ[\\abcd2"
+ /** ...and which bit of it. */
+ internal const val BO_B =
+ "543988888877777776666666655555555444444443210213333333222222221111111100000000054388877777766666665555555544444444433333" +
+ "332102133322222222211111111100000000005438777766666665555555544444444433333333333210212222222222111111111100000000000543" +
+ "776666666555555554444444443333333333222321021222222221111111111100000000000054366666555555554444444443333333333222222243" +
+ "210212222211111111111100000000000005436665555555444444443333333333322222222224321021222111111111111100000000000000543665" +
+ "555554444444433333333332222222222222432102121111111111111100000000000000054355555444444443333333333222222222222221154321" +
+ "021111111111111100000000000000005435544444444333333333332222222222222211115432102111111111111100000000000000000543544444" +
+ "444333333333322222222222222111111543210211111111111100000000000000000054354444433333333332222222222222221111111165432102" +
+ "111111111100000000000000000005435444443333333333222222222222221111111116543210211111111100000000000000000000543444433333" +
+ "333332222222222222211111111111654321021111111100000000000000000000054344333333333322222222222221111111111111176543210211" +
+ "111100000000000000000000005434333333333222222222222211111111111111117654321021111100000000000000000000000543433333332222" +
+ "222222222211111111111111111765432102111100000000000000000000000054343333332222222222222211111111111111111187654321021100" +
+ "000000000000000000000005434333332222222222222111111111111111111118765432102100000000000000000000000000543433333222222222" +
+ "221111111111111111111110876543210210000000000000000000000000054333333222222222221111111111111111111100098765432102100000" +
+ "000000000000000000005433333222222222221111111111111111111100009876543210210000000000000000000000000543333222222222221111" +
+ "1111111111111111000009876543210210000000000000000000000000543332222222222111111111111111111110000000:9876543210210000000" +
+ "00000000000000000543332222222222111111111111111111110000000:987654321021000000000000000000000000543322222222221111111111" +
+ "111111111000000000:987654321021000000000000000000000000543322222222111111111111111111110000000000;:987654321021000000000" +
+ "00000000000000543322222222111111111111111111110000000000;:98765432102100000000000000000000000543322222222111111111111111" +
+ "111100000000000;:98765432102100000000000000000000000543322222221111111111111111111000000000000;:987654321021000000000000" +
+ "00000000000543322222221111111111111111110000000000000;:98765432102100000000000000000000000543322222221111111111111111111" +
+ "000000000000;:98765432102100000000000000000000000543322222221111111111111111110000000000000;:987654321021000000000000000" +
+ "00000000543322222211111111111111111100000000000000;:98765432102100000000000000000000000543322222111111111111111111100000" +
+ "000000000;:98765432102100000000000000000000000543322222111111111111111111000000000000000;:987654321021000000000000000000" +
+ "00000543322222111111111111111110000000000000000;:98765432102100000000000000000000000543322222111111111111111110000000000" +
+ "000000;:98765432102100000000000000000000000543222221111111111111111100000000000000000;:987654321021000000000000000000000" +
+ "00543222221111111111111111100000000000000000;:98765432102100000000000000000000000543222221111111111111111100000000000000" +
+ "000;:98765432102100000000000000000000000543222221111111111111111100000000000000000;:987654321021000000000000000000000005" +
+ "43222221111111111111111100000000000000000;:98765432102100000000000000000000000543222221111111111111111000000000000000000" +
+ ";:98765432102100000000000000000000000543222211111111111111110000000000000000000;:987654321021000000000000000000000005432" +
+ "22211111111111111110000000000000000000;:98765432102100000000000000000000000543222211111111111111111000000000000000000;:9" +
+ "8765432102100000000000000000000000543222211111111111111111000000000000000000;:987654321021000000000000000000000005432222" +
+ "11111111111111111000000000000000000;:98765432102100000000000000000000000"
+ }
+}
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/NOTICE.md b/app/src/main/java/com/rfsentinel/app/sdr/NOTICE.md
new file mode 100644
index 0000000..99ed495
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/sdr/NOTICE.md
@@ -0,0 +1,25 @@
+# P25 voice: sources and licenses
+
+- `P25Voice.kt` takes a P25 Phase 1 voice frame (LDU1 / LDU2) apart into its nine IMBE codewords and corrects them (Golay 23,12, Hamming 15,11, the PN descrambling, the hex-word algorithm ID), with the frame layout and interleave tables of DSD.
+- `ImbeDecoder.kt` is the IMBE 4400 vocoder: a Kotlin port of mbelib's IMBE parameter decoding, spectral enhancement and speech synthesis, with its tables (`B2`, `BA`, `HOBA`, `BO_M` / `BO_B`, generated from `imbe7200x4400_const.h`; the synthesis window and block lengths computed).
+
+| Part | Origin | License |
+|---|---|---|
+| LDU layout, IMBE interleave tables, Golay / PN decoding | [DSD](https://github.com/szechyjs/dsd) (Copyright (C) 2010 DSD Author) and [mbelib](https://github.com/szechyjs/mbelib) | ISC |
+| IMBE 7200x4400 parameter decoding, spectral amplitude enhancement, speech synthesis and tables | [mbelib](https://github.com/szechyjs/mbelib) (Copyright (C) 2010 mbelib Author) | ISC |
+
+The ISC license of DSD and mbelib:
+
+> Permission to use, copy, modify, and/or distribute this software for any
+> purpose with or without fee is hereby granted, provided that the above
+> copyright notice and this permission notice appear in all copies.
+>
+> THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+> REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+> AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+> INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+> LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+> OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+> PERFORMANCE OF THIS SOFTWARE.
+
+IMBE is a voice coding method of Digital Voice Systems, Inc. (DVSI); the P25 standard (TIA-102.BABA) describes it.
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/P25Record.kt b/app/src/main/java/com/rfsentinel/app/sdr/P25Record.kt
index ac77dff..2d31206 100644
--- a/app/src/main/java/com/rfsentinel/app/sdr/P25Record.kt
+++ b/app/src/main/java/com/rfsentinel/app/sdr/P25Record.kt
@@ -63,11 +63,12 @@ object P25Record {
fun wav(): ByteArray = out.toByteArray().let { header(it.size) + it }
}
- /** A 44-byte PCM WAV header: 2 channels, 16 bits, [RATE]. */
- internal fun header(dataBytes: Int): ByteArray {
+ /** A 44-byte PCM WAV header, 16 bits: by default 2 channels at [RATE]. */
+ internal fun header(dataBytes: Int, channels: Int = 2, rate: Int = RATE): ByteArray {
val b = java.nio.ByteBuffer.allocate(44).order(java.nio.ByteOrder.LITTLE_ENDIAN)
b.put("RIFF".toByteArray()).putInt(36 + dataBytes).put("WAVE".toByteArray())
- b.put("fmt ".toByteArray()).putInt(16).putShort(1).putShort(2).putInt(RATE).putInt(RATE * 4).putShort(4).putShort(16)
+ b.put("fmt ".toByteArray()).putInt(16).putShort(1).putShort(channels.toShort()).putInt(rate)
+ .putInt(rate * 2 * channels).putShort((2 * channels).toShort()).putShort(16)
b.put("data".toByteArray()).putInt(dataBytes)
return b.array()
}
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/P25Voice.kt b/app/src/main/java/com/rfsentinel/app/sdr/P25Voice.kt
index 0d215a8..5a69a31 100644
--- a/app/src/main/java/com/rfsentinel/app/sdr/P25Voice.kt
+++ b/app/src/main/java/com/rfsentinel/app/sdr/P25Voice.kt
@@ -164,4 +164,13 @@ object P25Voice {
while (k + 4 <= end) { demod.feedChannel(b.getShort(k).toDouble(), b.getShort(k + 2).toDouble()); k += 4 }
return Decoded(frames, demod.syncs)
}
+
+ /** A call's speech as a WAV (8 kHz mono, 20 ms per codeword); empty when any of it is encrypted. */
+ fun speechWav(frames: List<Frame>): ByteArray {
+ if (frames.isEmpty() || frames.any { it.encrypted }) return ByteArray(0)
+ val imbe = ImbeDecoder()
+ val b = java.nio.ByteBuffer.allocate(frames.sumOf { it.voice.size } * ImbeDecoder.N * 2).order(java.nio.ByteOrder.LITTLE_ENDIAN)
+ for (f in frames) for (v in f.voice) for (s in imbe.decode(v.bits, v.errors)) b.putShort(s)
+ return P25Record.header(b.capacity(), 1, ImbeDecoder.RATE) + b.array()
+ }
}
diff --git a/app/src/test/java/com/rfsentinel/app/sdr/ImbeDecoderTest.kt b/app/src/test/java/com/rfsentinel/app/sdr/ImbeDecoderTest.kt
new file mode 100644
index 0000000..aa3d7fc
--- /dev/null
+++ b/app/src/test/java/com/rfsentinel/app/sdr/ImbeDecoderTest.kt
@@ -0,0 +1,52 @@
+package com.rfsentinel.app.sdr
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import kotlin.math.PI
+
+class ImbeDecoderTest {
+
+ /** A codeword with fundamental code [b0], every band voiced, gain [gain], the other coefficients at 0. */
+ private fun codeword(b0: Int, gain: Int): ByteArray {
+ val d = ByteArray(88)
+ for (i in 0..5) d[i] = (b0 shr (7 - i) and 1).toByte()
+ d[85] = (b0 shr 1 and 1).toByte(); d[86] = (b0 and 1).toByte()
+ val l = (0.9254 * (PI / (4 * PI / (b0 + 39.5)) + 0.25).toInt()).toInt()
+ if (b0 <= 207) for (i in 6 until 85) {
+ val p = (l - 9) * 79 + i - 6
+ d[i] = when (ImbeDecoder.BO_M[p] - '0') {
+ 1 -> 1
+ 2 -> (gain shr (ImbeDecoder.BO_B[p] - '0') and 1)
+ else -> 0
+ }.toByte()
+ }
+ return d
+ }
+
+ @Test
+ fun voicedSpeechHasItsPitch() {
+ // b0 100: w0 = 4π / 139.5, a period of 69.75 samples (115 Hz).
+ val imbe = ImbeDecoder()
+ val out = (0 until 10).flatMap { imbe.decode(codeword(100, 40), 0).toList() }.drop(5 * ImbeDecoder.N).map { it.toDouble() }
+ assertEquals(5 * ImbeDecoder.N, out.size)
+ val energy = out.sumOf { it * it } / out.size
+ assertTrue("energy $energy", energy > 1e4)
+ fun corr(lag: Int) = (0 until out.size - lag).sumOf { out[it] * out[it + lag] }
+ val best = (40..120).maxBy { corr(it) }
+ assertTrue("strongest repeat at $best samples", best in 69..70)
+ }
+
+ @Test
+ fun aBadCodewordRepeatsTheLastThenGoesSilent() {
+ val imbe = ImbeDecoder()
+ repeat(3) { imbe.decode(codeword(100, 40), 0) }
+ // Damaged (too many errors) or silence: the last good one goes on, three times.
+ repeat(3) { assertTrue(imbe.decode(codeword(100, 40), 9).any { it.toInt() != 0 }) }
+ assertTrue(imbe.decode(codeword(216, 0), 0).all { it.toInt() == 0 })
+ assertTrue(imbe.decode(codeword(216, 0), 0).all { it.toInt() == 0 })
+ // Then speech again.
+ repeat(2) { imbe.decode(codeword(100, 40), 0) }
+ assertTrue(imbe.decode(codeword(100, 40), 0).any { it.toInt() != 0 })
+ }
+}
diff --git a/app/src/test/java/com/rfsentinel/app/sdr/P25RecordingsCheck.kt b/app/src/test/java/com/rfsentinel/app/sdr/P25RecordingsCheck.kt
index a6319a7..8c917a7 100644
--- a/app/src/test/java/com/rfsentinel/app/sdr/P25RecordingsCheck.kt
+++ b/app/src/test/java/com/rfsentinel/app/sdr/P25RecordingsCheck.kt
@@ -14,7 +14,7 @@ class P25RecordingsCheck {
fun decodeRecordedCalls() {
val dir = System.getenv("P25_REC_DIR")?.let(::File)
assumeTrue(dir?.isDirectory == true)
- for (f in dir!!.listFiles { x -> x.name.endsWith(".wav") }.orEmpty().sortedBy { it.name }) {
+ for (f in dir!!.listFiles { x -> x.isFile && x.name.endsWith(".wav") }.orEmpty().sortedBy { it.name }) {
val out = P25Voice.decodeRecording(f.readBytes())
val words = out.frames.flatMap { it.voice }
val errors = words.sumOf { it.errors }
@@ -22,6 +22,9 @@ class P25RecordingsCheck {
"${out.frames.count { it.nid.duid == P25Voice.DUID_LDU1 }} LDU1), ${words.size} codewords, " +
"%.2f bit errors each, algid ${out.frames.mapNotNull { it.algid }.distinct().joinToString { "%02X".format(it) }}"
.format(java.util.Locale.US, if (words.isEmpty()) 0.0 else errors.toDouble() / words.size))
+ // The speech, to listen to: next to the recordings, still outside the repository.
+ val speech = P25Voice.speechWav(out.frames)
+ if (speech.isNotEmpty()) File(dir, "speech").apply { mkdirs() }.resolve(f.name).writeBytes(speech)
}
}
}
diff --git a/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.kt b/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.kt
index 32df2e8..1556206 100644
--- a/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.kt
@@ -104,5 +104,8 @@ class P25VoiceTest {
assertArrayEquals(v2[i], out.frames[1].voice[i].bits)
}
assertEquals(P25Voice.ALGID_CLEAR, out.frames[1].algid)
+ // 18 codewords of 20 ms each, as 8 kHz mono.
+ assertEquals(44 + 18 * ImbeDecoder.N * 2, P25Voice.speechWav(out.frames).size)
+ assertEquals(0, P25Voice.speechWav(listOf(P25Voice.decode(ldu(P25Voice.DUID_LDU2, v1, algid = 0x84))!!)).size)
}
}