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.
Kotlin airtagalprandroidandroid-autoanti-surveillancebluetooth-low-energybody-cameracounter-surveillancedrone-detectionflock-safetyimsi-catcher-detectorpolice-detectorpolice-scannerprivacyremote-idrtl-sdrsdrsurveillance-detectiontracker-detectionwifi
P25 voice step 4: listen live (watched or all clear calls), mute, playing mark
CIS-C0d944bdaf9303
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/P25Follow.kt b/app/src/main/java/com/rfsentinel/app/sdr/P25Follow.ktnew file mode 100644index 0000000..e0f00d9--- /dev/null+++ b/app/src/main/java/com/rfsentinel/app/sdr/P25Follow.kt@@ -0,0 +1,38 @@+package com.rfsentinel.app.sdr++/**+ * P25 voice, fourth step: one call heard live. A voice channel's samples (as [P25Demod.feed] takes them) to speech:+ * its voice frames ([P25Voice]) through the vocoder ([ImbeDecoder]) to [onSpeech], 20 ms (8 kHz) at a time.+ * [ended] once the call's terminator is heard, or it turns out encrypted; [quietS]: seconds since the last voice frame.+ */+class P25Follow(offsetHz: Double, private val onSpeech: (ShortArray) -> Unit) {++ private val imbe = ImbeDecoder()+ private var samples = 0L+ private var lastVoice = 0L+ /** Voice frames played (each 180 ms). */+ var frames = 0; private set+ var ended = false; private set++ private val demod = P25Demod(offsetHz, P25Voice.LDU_DIBITS) { f ->+ val v = P25Voice.decode(f)+ when {+ v == null -> if (P25Frame.nid(P25Frame.stripStatus(f))?.duid in TERMINATORS) ended = true+ v.encrypted -> ended = true+ else -> { frames++; lastVoice = samples; for (c in v.voice) onSpeech(imbe.decode(c.bits, c.errors)) }+ }+ }++ fun feed(buf: ByteArray, count: Int) {+ if (ended) return+ demod.feed(buf, count)+ samples += count+ }++ val quietS get() = (samples - lastVoice).toDouble() / P25Demod.SAMPLE_RATE++ companion object {+ /** The end of a call: a terminator, plain or with link control. */+ private val TERMINATORS = setOf(0x3, 0xF)+ }+}diff --git a/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.kt b/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.ktindex 5f43d41..f508b5f 100644--- a/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.kt+++ b/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.kt@@ -42,6 +42,9 @@ object SdrRadio {private const val INPUT_READS = 16 // ~70 ms of the inputprivate const val INPUT_OFFSET_HZ = 250_000L // the input this far above the tuned centre, clear of the DC spikeprivate const val RECORD_GAP_MS = 20_000L // at most one call recording this often: each costs P25Record.SECONDS of control channel+ private const val FOLLOW_START_S = 1.5 // no voice this long after tuning to a call: it isn't there (or already over)+ private const val FOLLOW_QUIET_S = 1.0 // voice gone this long: the call is over+ private const val FOLLOW_MAX_MS = 120_000L // then back to the control channel regardless@Volatile var status: String = ""; private set/** ADS-B messages decoded since the app started, for the main screen's messages-per-second line. */@@ -95,6 +98,16 @@ object SdrRadio {@Volatile var onP25Input: ((P25History.Call, Long, Int) -> Unit)? = null/** Settings › RTL-SDR radio: save the radio signal of clear calls ([P25Record]). */@Volatile var p25Record = false+ /** Settings › Listen to P25 voice: which clear calls to play live ([P25Follow]). */+ enum class Listen { OFF, WATCHED, ALL }+ @Volatile var p25Listen = Listen.OFF+ /** The watched talkgroups (Settings › Watch these talkgroups). */+ @Volatile var p25Watched: Set<Int> = emptySet()+ /** P25 talkgroups › Mute: no call is played, the SDR stays on the control channel. */+ @Volatile var p25Mute = false+ /** The call playing now (the P25 screen marks it), or null. */+ @Volatile var p25Playing: P25History.Call? = null; private set+ private var speaker: android.media.AudioTrack? = null/*** The waterfall screen zoomed in: hold this frequency instead of sweeping (or receiving ADS-B),* until set back to null. Picked up within a chunk.@@ -443,9 +456,11 @@ object SdrRadio {var messages = 0; var lastStatus = 0Lvar pending: Pair<P25History.Call, Long>? = null; var lastInput = 0L; var inputs = 0; var heard = 0var toRecord: P25History.Call? = null; var lastRecord = 0L; var recorded = 0+ var toFollow: P25History.Call? = null; var followed = 0// A call's input: once it's on the air, tune there for a moment and measure it (the control channel comes back after).- // A call to record goes first: its voice channel, for a few seconds.+ // A call to play goes first, for as long as it lasts; then a call to record, its voice channel for a few seconds.val detour = detour@{+ toFollow?.let { call -> toFollow = null; if (followCall(sdr, call, stop)) followed++; return@detour true }toRecord?.let { call -> toRecord = null; if (recordCall(sdr, call)) recorded++; return@detour true }val (call, hz) = pending ?: return@detour falseif (System.currentTimeMillis() - call.time < INPUT_DELAY_MS) return@detour false@@ -466,8 +481,12 @@ object SdrRadio {p25Log.inputHz(c.channel!!)?.let { hz -> pending = c to hz; lastInput = now }}}+ val listen = p25Listen+ if (listen != Listen.OFF && !p25Mute && toFollow == null) {+ calls.lastOrNull { !it.encrypted && it.hz != null && (listen == Listen.ALL || it.tg in p25Watched) }?.let { toFollow = it }+ }if (p25Record && toRecord == null && now - lastRecord >= RECORD_GAP_MS) {- calls.lastOrNull { !it.encrypted && it.hz != null }?.let { toRecord = it; lastRecord = now }+ calls.lastOrNull { !it.encrypted && it.hz != null && it !== toFollow }?.let { toRecord = it; lastRecord = now }}if (now - lastStatus >= 2_000L && !halted) { lastStatus = now; status = "$deviceLabel · P25 · control channel $where · $messages messages" + turnNote() }}@@ -475,10 +494,57 @@ object SdrRadio {UsbWifi.log("$deviceLabel: P25 ${if (lost) "control channel went quiet" else "turn over or stopped"} after $messages messages (${hunt.blocks} blocks in all), " +p25Log.rows().let { r -> "${r.size} talkgroups / private calls, ${r.sumOf { it.calls }} calls" } +(if (inputs > 0) ", $inputs input checks, $heard radios heard nearby" else "") +- (if (recorded > 0) ", $recorded calls recorded" else ""))+ (if (recorded > 0) ", $recorded calls recorded" else "") ++ (if (followed > 0) ", $followed calls played" else ""))}+ speaker?.release(); speaker = null}+ /**+ * Plays [call] live from its voice channel until it ends, goes quiet, or listening stops ([FOLLOW_MAX_MS] at most);+ * false when no voice was heard. Frequency kept off the log.+ */+ private fun followCall(sdr: SdrDevice, call: P25History.Call, stop: () -> Boolean): Boolean {+ val hz = call.hz ?: return false+ val out = speaker ?: runCatching { newSpeaker() }.onFailure { UsbWifi.log("$deviceLabel: P25 no audio output - ${UsbWifi.describe(it)}") }+ .getOrNull()?.also { speaker = it } ?: return false+ if (!sdr.tune(hz - INPUT_OFFSET_HZ)) return false+ sdr.resetBuffer()+ val buf = ByteArray(XFER)+ repeat(sdr.settleReads) { sdr.read(buf) }+ var started = false+ val follow = P25Follow(INPUT_OFFSET_HZ.toDouble()) { pcm ->+ // A little silence first: voice comes in 180 ms bursts, and the buffer shouldn't run dry between them.+ if (!started) { started = true; out.write(ShortArray(ImbeDecoder.RATE / 5), 0, ImbeDecoder.RATE / 5, android.media.AudioTrack.WRITE_NON_BLOCKING) }+ out.write(pcm, 0, pcm.size, android.media.AudioTrack.WRITE_NON_BLOCKING)+ }+ p25Playing = call+ status = "$deviceLabel · P25 · playing ${call.tg?.let { "talkgroup $it" } ?: "a private call"}" + turnNote()+ out.play()+ val start = System.currentTimeMillis()+ var fails = 0+ try {+ while (!stop() && !p25Mute && p25Listen != Listen.OFF && !follow.ended && fails < STALL_CHUNKS &&+ follow.quietS < (if (follow.frames == 0) FOLLOW_START_S else FOLLOW_QUIET_S) && System.currentTimeMillis() - start < FOLLOW_MAX_MS) {+ val n = sdr.read(buf)+ if (n <= 0) { fails++; continue }+ follow.feed(buf, n / 2)+ }+ } finally { p25Playing = null; out.stop() } // stop: what's buffered still plays+ if (follow.frames > 0) UsbWifi.log("$deviceLabel: P25 call played (%.1f s)".format(java.util.Locale.US, follow.frames * 0.18))+ return follow.frames > 0+ }++ /** 8 kHz mono speech out, as media (so it goes to the car or headphones like other audio). */+ private fun newSpeaker(): android.media.AudioTrack = android.media.AudioTrack.Builder()+ .setAudioAttributes(android.media.AudioAttributes.Builder().setUsage(android.media.AudioAttributes.USAGE_MEDIA)+ .setContentType(android.media.AudioAttributes.CONTENT_TYPE_SPEECH).build())+ .setAudioFormat(android.media.AudioFormat.Builder().setSampleRate(ImbeDecoder.RATE)+ .setEncoding(android.media.AudioFormat.ENCODING_PCM_16BIT).setChannelMask(android.media.AudioFormat.CHANNEL_OUT_MONO).build())+ .setTransferMode(android.media.AudioTrack.MODE_STREAM)+ .setBufferSizeInBytes(ImbeDecoder.RATE) // half a second+ .build()+/** Records [P25Record.SECONDS] of [call]'s voice channel to a file; false when nothing was saved. Frequency kept off the log. */private fun recordCall(sdr: SdrDevice, call: P25History.Call): Boolean {val ctx = appContext ?: return falsediff --git a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.ktindex bfebeb9..3ede72f 100644--- a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt+++ b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt@@ -426,6 +426,9 @@ class ScanForegroundService : Service() {com.rfsentinel.app.sdr.SdrRadio.onP25Call = { tg -> pipeline.post { onP25Call(tg) } }com.rfsentinel.app.sdr.SdrRadio.p25Inputs = Prefs.p25Inputs(this)com.rfsentinel.app.sdr.SdrRadio.p25Record = Prefs.p25Record(this)+ com.rfsentinel.app.sdr.SdrRadio.p25Listen = com.rfsentinel.app.sdr.SdrRadio.Listen.entries[Prefs.p25Listen(this)]+ com.rfsentinel.app.sdr.SdrRadio.p25Watched = com.rfsentinel.app.sdr.P25Log.watched(Prefs.p25Talkgroups(this)).keys+ com.rfsentinel.app.sdr.SdrRadio.p25Mute = Prefs.p25Mute(this)com.rfsentinel.app.sdr.SdrRadio.onP25Input = { call, hz, snr -> pipeline.post { onP25Input(call, hz, snr) } }com.rfsentinel.app.sdr.SdrRadio.location = { (lastLocation ?: lastFix)?.let { it.latitude to it.longitude } }com.rfsentinel.app.sdr.SdrRadio.onAircraft = { planes, unlocated -> onlineWatch.localAircraft(planes, unlocated) }diff --git a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.ktindex 4d1d962..410b937 100644--- a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt+++ b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt@@ -1418,7 +1418,7 @@ class SettingsActivity : AppCompatActivity() {"A 1090 MHz antenna helps; the stock one hears out to about 50 km.") { Prefs.setSdrAdsb(this, it) }val p25Box = scan("P25 control channel", Prefs.sdrP25(this),"Experimental. Finds the P25 trunked-radio control channel here (700 / 800 MHz, used in the United States and Canada) and " +- "follows it: which talkgroups get a channel, and when (Tools › P25 talkgroups). Signalling only: voice is never decoded. " ++ "follows it: which talkgroups get a channel, and when (Tools › P25 talkgroups). Voice is only played with Listen to P25 voice, below. " +"Needs Two-way radio turned on. On by default when your region is the United States or Canada. Receiving radio " +"signalling is legal in many places but not all, and some forbid it in a vehicle or passing on what you hear: " +"check the law where you are.") { Prefs.setSdrP25(this, it) }@@ -1446,7 +1446,7 @@ class SettingsActivity : AppCompatActivity() {Prefs.setP25Inputs(this, it)}scan("Record P25 voice calls", Prefs.p25Record(this),- "For building P25 voice: when a clear (not encrypted) call starts, the SDR leaves the control channel for " ++ "For testing P25 voice: when a clear (not encrypted) call starts, the SDR leaves the control channel for " +"${com.rfsentinel.app.sdr.P25Record.SECONDS} s and saves that call's radio signal to a file, about 1 MB each. Nothing " +"is decoded or played. At most one call every 20 s; the last ${com.rfsentinel.app.sdr.P25Record.KEEP} are kept. " +"Share them with the button below. Recording radio calls is not allowed everywhere: check the law where you are.") {@@ -1456,7 +1456,14 @@ class SettingsActivity : AppCompatActivity() {text = "Share P25 recordings"setOnClickListener { shareP25Recordings() }})- val talkgroupViews = (box.childCount - 8 until box.childCount).map { box.getChildAt(it) }+ box.addView(infoRow(choiceRow("Listen to P25 voice", listOf("Off", "Watched talkgroups", "All clear calls"), Prefs.p25Listen(this)) {+ Prefs.setP25Listen(this, it)+ }, "Plays clear (not encrypted) P25 calls as they happen, through the speaker, headphones or car like other audio. " ++ "The SDR leaves the control channel for the call and misses its messages meanwhile: one call at a time, at most " ++ "2 minutes each. Watched talkgroups: only those in Watch these talkgroups. Mute on the P25 talkgroups screen pauses it. " ++ "Encrypted calls can't be played. Listening to radio calls is not allowed everywhere, and some places forbid it in " ++ "a vehicle: check the law where you are.", "Listen to P25 voice"))+ val talkgroupViews = (box.childCount - 9 until box.childCount).map { box.getChildAt(it) }var sweepViews = emptyList<View>() // the sweep's own settings, belowshowSdrOptions = {val ticked = listOf(sweepBox, adsbBox, p25Box).count { it.isChecked }diff --git a/app/src/main/java/com/rfsentinel/app/ui/P25Activity.kt b/app/src/main/java/com/rfsentinel/app/ui/P25Activity.ktindex 9030874..99bb49d 100644--- a/app/src/main/java/com/rfsentinel/app/ui/P25Activity.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/P25Activity.kt@@ -34,7 +34,7 @@ import java.util.Locale* The P25 control channel the SDR follows (Settings > RTL-SDR radio > P25 control channel): its site, and the* talkgroups and private calls granted a channel there, most recent first. Watched talkgroups are named and* marked. History: the kept call log ([P25History]) per talkgroup over the last day and week, and its busiest hour.- * Signalling only: nothing here is voice.+ * With Settings › Listen to P25 voice on, the call playing is marked, and Mute pauses listening.*/class P25Activity : AppCompatActivity() {@@ -61,7 +61,8 @@ class P25Activity : AppCompatActivity() {val r = rows[position]ItemDetectionBinding.bind(holder.itemView).apply {val name = r.tg?.let { watched[it] }- labelText.text = listOfNotNull(title(r), name?.ifEmpty { "watched" }, "EMERGENCY".takeIf { r.emergency },+ val playing = SdrRadio.p25Playing?.let { if (r.tg != null) it.tg == r.tg else it.tg == null && it.target == r.target } == true+ labelText.text = listOfNotNull(title(r), "playing".takeIf { playing }, name?.ifEmpty { "watched" }, "EMERGENCY".takeIf { r.emergency },"encrypted".takeIf { r.encrypted }).joinToString(" · ")labelText.setTextColor(when {r.emergency -> EMERGENCY@@ -105,6 +106,8 @@ class P25Activity : AppCompatActivity() {menu.add(0, MENU_HISTORY, 0, if (history == null) "History" else "Live").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)menu.add(0, MENU_EXPORT, 1, "Export call log")+ if (Prefs.p25Listen(this) != 0) menu.add(0, MENU_MUTE, 0, if (Prefs.p25Mute(this)) "Unmute" else "Mute")+ .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)return true}@@ -116,6 +119,11 @@ class P25Activity : AppCompatActivity() {invalidateOptionsMenu(); refresh(); true}MENU_EXPORT -> { exportLog(); true }+ MENU_MUTE -> {+ val mute = !Prefs.p25Mute(this)+ Prefs.setP25Mute(this, mute); SdrRadio.p25Mute = mute+ invalidateOptionsMenu(); true+ }else -> super.onOptionsItemSelected(item)}@@ -207,6 +215,7 @@ class P25Activity : AppCompatActivity() {companion object {private const val MENU_HISTORY = 1private const val MENU_EXPORT = 2+ private const val MENU_MUTE = 3private const val HISTORY_MS = 30_000Lprivate val EMERGENCY = 0xFFD0503C.toInt()diff --git a/app/src/main/java/com/rfsentinel/app/util/Prefs.kt b/app/src/main/java/com/rfsentinel/app/util/Prefs.ktindex 25d1674..9c0391a 100644--- a/app/src/main/java/com/rfsentinel/app/util/Prefs.kt+++ b/app/src/main/java/com/rfsentinel/app/util/Prefs.kt@@ -660,6 +660,12 @@ object Prefs {/** Save the radio signal of clear calls ([com.rfsentinel.app.sdr.P25Record]), for the voice decoder. Off by default. */fun p25Record(context: Context): Boolean = bool(context, "p25_record", false)fun setP25Record(context: Context, v: Boolean) = setBool(context, "p25_record", v)+ /** Which clear P25 calls to play live: 0 none (default), 1 watched talkgroups, 2 all ([com.rfsentinel.app.sdr.SdrRadio.Listen]). */+ fun p25Listen(context: Context): Int = int(context, "p25_listen", 0).coerceIn(0, 2)+ fun setP25Listen(context: Context, v: Int) = setInt(context, "p25_listen", v.coerceIn(0, 2))+ /** P25 talkgroups › Mute: listening paused. */+ fun p25Mute(context: Context): Boolean = bool(context, "p25_mute", false)+ fun setP25Mute(context: Context, v: Boolean) = setBool(context, "p25_mute", v)/** Seconds the SDR gives each ticked scan in turn, 30-300: a first P25 search takes up to about 15 s. */fun sdrTurnS(context: Context): Int = int(context, "sdr_turn_s", 60).coerceIn(30, 300)fun setSdrTurnS(context: Context, v: Int) = setInt(context, "sdr_turn_s", v.coerceIn(30, 300))diff --git a/app/src/test/java/com/rfsentinel/app/online/WazeScreensTest.kt b/app/src/test/java/com/rfsentinel/app/online/WazeScreensTest.ktindex afdfa02..768d949 100644--- a/app/src/test/java/com/rfsentinel/app/online/WazeScreensTest.kt+++ b/app/src/test/java/com/rfsentinel/app/online/WazeScreensTest.kt@@ -69,7 +69,7 @@ class WazeScreensTest {// Waze alone adds four: alert range, map range, and one interval slider per source.assertTrue("sliders: ${sliders.size}", sliders.size >= 4)sliders.forEach { it.measure(exactly, atMost) } // throws if a value does not fit its steps- assertEquals("one level menu per kind of report", WazePolice.Type.entries.size, views.filterIsInstance<AppCompatSpinner>().count { it.contentDescription !in setOf("Snooze length", "Swipe right on an alert", "Swipe left on an alert", "Re-center after", "Alert from", "WiFi adapter", "Bluetooth adapter", "SDR") })+ assertEquals("one level menu per kind of report", WazePolice.Type.entries.size, views.filterIsInstance<AppCompatSpinner>().count { it.contentDescription !in setOf("Snooze length", "Swipe right on an alert", "Swipe left on an alert", "Re-center after", "Alert from", "WiFi adapter", "Bluetooth adapter", "SDR", "Listen to P25 voice") })val text = views.filterIsInstance<TextView>().joinToString("\n") { it.text }assertTrue(text.contains("Alert when a report is within"))assertTrue(text.contains("Show reports on the map within"))diff --git a/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.kt b/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.ktindex 1556206..d3c455a 100644--- a/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.kt+++ b/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.kt@@ -37,10 +37,41 @@ class P25VoiceTest {for (i in 0 until 9) codeword(voice[i]).copyInto(d, P25Voice.VOICE_AT[i])hexWord(algid shr 2).copyInto(d, 452); hexWord(algid and 3 shl 4).copyInto(d, 457)hit(d)+ return withStatus(d)+ }++ /** A call's terminator after its sync, padded to an LDU's length. */+ private fun tdu(): IntArray {+ val d = IntArray(816)+ P25TestSignal.dibitsOf(P25Frame.nidCodeword(0x293 shl 4 or 0x3) shl 1, 32).copyInto(d)+ return withStatus(d)+ }++ private fun withStatus(d: IntArray): IntArray {val data = ArrayDeque(d.toList())return IntArray(P25Voice.LDU_DIBITS) { i -> if ((i + 25) % 36 == 0) 2 else data.removeFirst() }}+ /** [dibits] on the air as the dongle hears them: 250 kHz up, plus 700 Hz of crystal error, 8-bit I/Q. */+ private fun iq(dibits: IntArray): ByteArray {+ val rate = P25Demod.SAMPLE_RATE+ val perSymbol = rate / P25Demod.SYMBOL_RATE+ val iq = ByteArray(dibits.size * perSymbol * 2)+ val rnd = java.util.Random(9)+ var phase = 0.0; var k = 0+ for (d in dibits) repeat(perSymbol) {+ phase += 2 * PI * (250_700.0 + P25TestSignal.hz(d)) / rate+ iq[k++] = (127.5 + 50 * cos(phase) + rnd.nextGaussian() * 4).roundToInt().coerceIn(0, 255).toByte()+ iq[k++] = (127.5 + 50 * sin(phase) + rnd.nextGaussian() * 4).roundToInt().coerceIn(0, 255).toByte()+ }+ return iq+ }++ private fun chunks(iq: ByteArray, feed: (ByteArray, Int) -> Unit) {+ var at = 0+ while (at < iq.size) { val len = minOf(16_384, iq.size - at); feed(iq.copyOfRange(at, at + len), len / 2); at += len }+ }+private fun voice(seed: Long): List<ByteArray> {val rnd = java.util.Random(seed)return List(9) { ByteArray(88) { rnd.nextInt(2).toByte() } }@@ -79,23 +110,12 @@ class P25VoiceTest {@Testfun aRecordedCallIsDecoded() {- // Two LDUs on the air, as the dongle hears them 250 kHz up (plus 700 Hz of crystal error), recorded as P25Record does.+ // Two LDUs on the air, recorded as P25Record does.val v1 = voice(3); val v2 = voice(4)val dibits = IntArray(60) { (it * 5 + 1) % 4 } + P25TestSignal.SYNC + ldu(P25Voice.DUID_LDU1, v1) +P25TestSignal.SYNC + ldu(P25Voice.DUID_LDU2, v2) + IntArray(60) { it % 4 }- val rate = P25Demod.SAMPLE_RATE- val perSymbol = rate / P25Demod.SYMBOL_RATE- val iq = ByteArray(dibits.size * perSymbol * 2)- val rnd = java.util.Random(9)- var phase = 0.0; var k = 0- for (d in dibits) repeat(perSymbol) {- phase += 2 * PI * (250_700.0 + P25TestSignal.hz(d)) / rate- iq[k++] = (127.5 + 50 * cos(phase) + rnd.nextGaussian() * 4).roundToInt().coerceIn(0, 255).toByte()- iq[k++] = (127.5 + 50 * sin(phase) + rnd.nextGaussian() * 4).roundToInt().coerceIn(0, 255).toByte()- }val rec = P25Record.Baseband(250_000L)- var at = 0- while (at < iq.size) { val len = minOf(16_384, iq.size - at); rec.feed(iq.copyOfRange(at, at + len), len / 2); at += len }+ chunks(iq(dibits), rec::feed)val out = P25Voice.decodeRecording(rec.wav())assertEquals(2, out.frames.size)@@ -108,4 +128,25 @@ class P25VoiceTest {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)}++ @Test+ fun aCallIsFollowedLiveUntilItsTerminator() {+ val dibits = IntArray(60) { (it * 5 + 1) % 4 } + P25TestSignal.SYNC + ldu(P25Voice.DUID_LDU1, voice(5)) ++ P25TestSignal.SYNC + ldu(P25Voice.DUID_LDU2, voice(6)) + P25TestSignal.SYNC + tdu() ++ // After the terminator: more voice, which isn't played.+ P25TestSignal.SYNC + ldu(P25Voice.DUID_LDU1, voice(7)) + IntArray(60) { it % 4 }+ var played = 0+ val follow = P25Follow(250_000.0) { assertEquals(ImbeDecoder.N, it.size); played++ }+ chunks(iq(dibits), follow::feed)+ assertEquals(2, follow.frames)+ assertEquals(18, played)+ assertTrue(follow.ended)++ // An encrypted call ends it before anything more is played.+ var later = 0+ val enc = P25Follow(250_000.0) { later++ }+ chunks(iq(IntArray(60) { it % 4 } + P25TestSignal.SYNC + ldu(P25Voice.DUID_LDU2, voice(8), algid = 0x84) + IntArray(60) { it % 4 }), enc::feed)+ assertTrue(enc.ended)+ assertEquals(0, later)+ }}