From 164e193ef035c8ea209995cec448563e0c881f30 Mon Sep 17 00:00:00 2001 From: Denny Sheirer Date: Sat, 21 Nov 2026 05:31:04 -0500 Subject: [PATCH] #2740 JMBE version 3.0.0 library with support for EDACS PROVOICE IMBE codec. (#2741) - JmbeAudioModule synthesizes from host corrected frames (jmbe-api CorrectedIMBEFrame/CorrectedAMBEFrame) when the codec is IAudioCodecV3, and falls back to the raw frame bytes for JMBE version 1 and 2 libraries - LDUMessage, P25P2 AbstractVoiceTimeslot, DMR VoiceMessage and NXDN Audio cache their corrected voice frames - P25P1, P25P2, DMR, NXDN audio modules and MBECallSequenceConverter only correct frames when the codec supports it - DMR queued voice frames carry the optional corrected frame - Error correct MBE voice frames in sdrtrunk and synthesize with JMBE V3 codecs Co-authored-by: denny --- build.gradle | 2 +- settings.gradle | 9 ++ .../audio/codec/mbe/JmbeAudioModule.java | 103 +++++++++++++++--- .../codec/mbe/MBECallSequenceConverter.java | 9 +- .../decoder/JmbeLibraryPreferenceEditor.java | 11 +- .../io/github/dsheirer/jmbe/JmbeCreator.java | 12 +- .../github/dsheirer/jmbe/github/GitHub.java | 70 ++++++++---- .../github/dsheirer/jmbe/github/Version.java | 12 +- .../decode/dmr/audio/DMRAudioModule.java | 31 ++++-- .../dmr/message/voice/VoiceMessage.java | 25 ++++- .../decode/nxdn/audio/NXDNAudioModule.java | 10 +- .../module/decode/nxdn/layer3/call/Audio.java | 24 ++++ .../decode/p25/audio/P25P1AudioModule.java | 12 +- .../decode/p25/audio/P25P2AudioModule.java | 22 ++-- .../p25/phase1/message/ldu/LDUMessage.java | 23 +++- .../timeslot/AbstractVoiceTimeslot.java | 27 ++++- .../github/dsheirer/jmbe/JmbeCreatorTest.java | 86 +++++++++++++++ 17 files changed, 415 insertions(+), 73 deletions(-) create mode 100644 src/test/java/io/github/dsheirer/jmbe/JmbeCreatorTest.java diff --git a/build.gradle b/build.gradle index d8f29e6d..426f2339 100644 --- a/build.gradle +++ b/build.gradle @@ -138,7 +138,6 @@ dependencies { //Jitpack imports implementation 'com.github.dnault:libresample4j:master' - implementation 'com.github.DSheirer.jmbe:api:v2.1.1' implementation 'com.github.nwaldispuehl:java-lame:v3.98.4' //mavenCentral/jcenter imports @@ -158,6 +157,7 @@ dependencies { implementation 'io.github.dsheirer:jairspyhf:1.8.1.1' //Airspy HF+ tuners - GitHub release, see settings.gradle implementation 'io.github.dsheirer:jbladerf:2.6.0.1' //bladeRF tuners - GitHub release, see settings.gradle implementation 'io.github.dsheirer:jhackrf:2026.01.3.1' //HackRF tuners - GitHub release, see settings.gradle + implementation 'io.github.dsheirer:jmbe-api:3.0.0' //JMBE audio codec interfaces - GitHub release, see settings.gradle implementation 'io.github.dsheirer:jlibusb:1.0.30.1' //libusb and USB hotplug for the tuner libraries - GitHub release; not pulled in transitively implementation 'io.github.dsheirer:jhydrasdr:1.1.4.1' //HydraSDR tuners - GitHub release, see settings.gradle implementation 'io.github.dsheirer:jlimesdr:25.1.0.2' //LimeSDR tuners - GitHub release, see settings.gradle diff --git a/settings.gradle b/settings.gradle index e7822725..55501abf 100644 --- a/settings.gradle +++ b/settings.gradle @@ -83,6 +83,15 @@ dependencyResolutionManagement { content { includeModule 'io.github.dsheirer', 'jbladerf' } } + //JMBE API is published as a GitHub release. It holds only the codec interfaces; users create the JMBE codec + //library itself with the JMBE creator, which sdrtrunk downloads from the release matching the API major version + ivy { + url = 'https://github.com/DSheirer/jmbe/releases/download' + patternLayout { artifact 'v[revision]/[artifact]-[revision].[ext]' } + metadataSources { artifact() } + content { includeModule 'io.github.dsheirer', 'jmbe-api' } + } + //radio-reference-api is published as a GitHub release. Its dependencies (jackson-dataformat-xml, slf4j-api) //are not pulled in transitively and are declared directly in build.gradle ivy { diff --git a/src/main/java/io/github/dsheirer/audio/codec/mbe/JmbeAudioModule.java b/src/main/java/io/github/dsheirer/audio/codec/mbe/JmbeAudioModule.java index d17b81b9..d7cae871 100644 --- a/src/main/java/io/github/dsheirer/audio/codec/mbe/JmbeAudioModule.java +++ b/src/main/java/io/github/dsheirer/audio/codec/mbe/JmbeAudioModule.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2024 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,10 +37,14 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import jmbe.frame.ICorrectedFrame; import jmbe.iface.IAudioCodec; import jmbe.iface.IAudioCodecLibrary; import jmbe.iface.IAudioCodecLibraryV2; +import jmbe.iface.IAudioCodecLibraryV3; import jmbe.iface.IAudioCodecV2; +import jmbe.iface.IAudioCodecV3; +import jmbe.iface.IAudioWithMetadata; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,6 +56,13 @@ public abstract class JmbeAudioModule extends AbstractAudioModule implements Lis { private static final Logger LOG = LoggerFactory.getLogger(JmbeAudioModule.class); private static final String JMBE_AUDIO_LIBRARY = "JMBE"; + + /** + * JMBE library entry point class names, newest first. The loader uses the newest version that the library + * provides, falling back to older versions for libraries created by older JMBE releases. + */ + private static final List JMBE_LIBRARY_CLASS_NAMES = List.of("jmbe.JMBEAudioLibraryV3", + "jmbe.JMBEAudioLibraryV2", "jmbe.JMBEAudioLibrary"); private static final List mLibraryLoadStatusLogged = new ArrayList<>(); private IAudioCodec mAudioCodec; private final UserPreferences mUserPreferences; @@ -128,6 +139,56 @@ public abstract class JmbeAudioModule extends AbstractAudioModule implements Lis return getAudioCodec() != null; } + /** + * Indicates if the loaded audio codec can synthesize audio from a voice frame that sdrtrunk has already error + * corrected. Only JMBE version 3 libraries support this. Version 1 and 2 libraries only accept the raw voice + * frame bytes and perform the error detection and correction themselves. + * + * Callers can use this to skip creating corrected frames when the codec can't use them. + */ + protected boolean isCorrectedFrameSupported() + { + return mAudioCodec instanceof IAudioCodecV3; + } + + /** + * Synthesizes audio for the voice frame. When the codec supports it (JMBE version 3), the audio is synthesized + * from the corrected frame so the codec doesn't repeat the error detection and correction. Otherwise, the raw + * frame bytes are passed to the codec (JMBE version 1 and 2). + * + * @param frame bytes for the voice frame as transmitted + * @param correctedFrame for the same voice frame, or null to let the codec perform the error correction + * @return audio samples + */ + protected float[] getAudio(byte[] frame, ICorrectedFrame correctedFrame) + { + if(correctedFrame != null && mAudioCodec instanceof IAudioCodecV3 v3) + { + return v3.getAudio(correctedFrame); + } + + return mAudioCodec.getAudio(frame); + } + + /** + * Synthesizes audio with optional metadata (tones, DTMF, etc.) for the voice frame. When the codec supports it + * (JMBE version 3), the audio is synthesized from the corrected frame so the codec doesn't repeat the error + * detection and correction. Otherwise, the raw frame bytes are passed to the codec (JMBE version 1 and 2). + * + * @param frame bytes for the voice frame as transmitted + * @param correctedFrame for the same voice frame, or null to let the codec perform the error correction + * @return audio samples with optional metadata + */ + protected IAudioWithMetadata getAudioWithMetadata(byte[] frame, ICorrectedFrame correctedFrame) + { + if(correctedFrame != null && mAudioCodec instanceof IAudioCodecV3 v3) + { + return v3.getAudioWithMetadata(correctedFrame); + } + + return mAudioCodec.getAudioWithMetadata(frame); + } + @Override public Listener getMessageListener() { @@ -177,26 +238,25 @@ public abstract class JmbeAudioModule extends AbstractAudioModule implements Lis URLClassLoader childClassLoader = new URLClassLoader(new URL[]{path.toUri().toURL()}, this.getClass().getClassLoader()); - try - { - sLoadedJmbeAudioConverterClass = Class.forName("jmbe.JMBEAudioLibraryV2", true, childClassLoader); - } - catch(ClassNotFoundException legacyLibrary) + for(String className: JMBE_LIBRARY_CLASS_NAMES) { try { - sLoadedJmbeAudioConverterClass = Class.forName("jmbe.JMBEAudioLibrary", true, childClassLoader); + sLoadedJmbeAudioConverterClass = Class.forName(className, true, childClassLoader); + break; } - catch(ClassNotFoundException missingLibrary) + catch(ClassNotFoundException notFound) { - if(!mLibraryLoadStatusLogged.contains(JMBE_AUDIO_LIBRARY)) - { - LOG.error("Couldn't load JMBE audio conversion library from [" + path + - "] - neither V2 nor V1 library class was found"); - mLibraryLoadStatusLogged.add(JMBE_AUDIO_LIBRARY); - } + //Library doesn't contain this version of the library class - try the next older version } } + + if(sLoadedJmbeAudioConverterClass == null && !mLibraryLoadStatusLogged.contains(JMBE_AUDIO_LIBRARY)) + { + LOG.error("Couldn't load JMBE audio conversion library from [" + path + + "] - none of the library classes " + JMBE_LIBRARY_CLASS_NAMES + " were found"); + mLibraryLoadStatusLogged.add(JMBE_AUDIO_LIBRARY); + } } catch(IllegalArgumentException iae) { @@ -229,9 +289,18 @@ public abstract class JmbeAudioModule extends AbstractAudioModule implements Lis if((library.getMajorVersion() == 1 && library.getMinorVersion() >= 0 && library.getBuildVersion() >= 0) || library.getMajorVersion() >= 1) { - audioConverter = library instanceof IAudioCodecLibraryV2 libraryV2 ? - libraryV2.getAudioConverterV2(getCodecName()) : - library.getAudioConverter(getCodecName()); + if(library instanceof IAudioCodecLibraryV3 libraryV3) + { + audioConverter = libraryV3.getAudioConverterV3(getCodecName()); + } + else if(library instanceof IAudioCodecLibraryV2 libraryV2) + { + audioConverter = libraryV2.getAudioConverterV2(getCodecName()); + } + else + { + audioConverter = library.getAudioConverter(getCodecName()); + } String codecStatusKey = JMBE_AUDIO_LIBRARY + ":codec:" + getCodecName(); if(!mLibraryLoadStatusLogged.contains(codecStatusKey)) diff --git a/src/main/java/io/github/dsheirer/audio/codec/mbe/MBECallSequenceConverter.java b/src/main/java/io/github/dsheirer/audio/codec/mbe/MBECallSequenceConverter.java index 60099f3a..afb69311 100644 --- a/src/main/java/io/github/dsheirer/audio/codec/mbe/MBECallSequenceConverter.java +++ b/src/main/java/io/github/dsheirer/audio/codec/mbe/MBECallSequenceConverter.java @@ -36,7 +36,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collection; -import jmbe.iface.IAudioCodec; +import jmbe.frame.CorrectedIMBEFrame; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.SuffixFileFilter; @@ -111,12 +111,15 @@ public class MBECallSequenceConverter } } - IAudioCodec codec = audioModule.getAudioCodec(); + boolean correctFrames = audioModule.isCorrectedFrameSupported(); for(VoiceFrame voiceFrame: callSequence.getVoiceFrames()) { byte[] frameBytes = voiceFrame.getFrameBytes(); - float[] audio = codec.getAudio(frameBytes); + + //Only error correct the frame in sdrtrunk when the JMBE library (version 3+) can synthesize from it + float[] audio = audioModule.getAudio(frameBytes, + correctFrames ? CorrectedIMBEFrame.correct(frameBytes) : null); audioModule.addAudio(audio); } diff --git a/src/main/java/io/github/dsheirer/gui/preference/decoder/JmbeLibraryPreferenceEditor.java b/src/main/java/io/github/dsheirer/gui/preference/decoder/JmbeLibraryPreferenceEditor.java index 2fae07c8..ef415d00 100644 --- a/src/main/java/io/github/dsheirer/gui/preference/decoder/JmbeLibraryPreferenceEditor.java +++ b/src/main/java/io/github/dsheirer/gui/preference/decoder/JmbeLibraryPreferenceEditor.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2022 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -171,12 +171,17 @@ public class JmbeLibraryPreferenceEditor extends VBox try { Version current = mUserPreferences.getJmbeLibraryPreference().getCurrentVersion(); - final Release release = GitHub.getLatestRelease(JmbeCreator.GITHUB_JMBE_RELEASES_URL); + final Release release = GitHub.getLatestRelease(JmbeCreator.GITHUB_JMBE_RELEASES_URL, + JmbeCreator.JMBE_API_MAJOR_VERSION); - mLog.info("Checking for JMBE Library Updates ..."); + mLog.info("Checking for JMBE Library Updates for API major version " + + JmbeCreator.JMBE_API_MAJOR_VERSION + " ..."); mLog.info("Current: " + (current != null ? current.toString() : "empty")); + //Offer the latest matching release when there is no current library, when the current library is a + //different (incompatible) major version, or when the matching release is newer than the current one. final boolean canUpdate = (release != null) && ((current == null) || + !current.hasMajor() || current.getMajor() != JmbeCreator.JMBE_API_MAJOR_VERSION || (release.getVersion().compareTo(current) > 0)); if(release != null) diff --git a/src/main/java/io/github/dsheirer/jmbe/JmbeCreator.java b/src/main/java/io/github/dsheirer/jmbe/JmbeCreator.java index f3279bfd..c85f4eea 100644 --- a/src/main/java/io/github/dsheirer/jmbe/JmbeCreator.java +++ b/src/main/java/io/github/dsheirer/jmbe/JmbeCreator.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2025 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -51,6 +51,14 @@ public class JmbeCreator { private final static Logger mLog = LoggerFactory.getLogger(JmbeCreator.class); public static final String GITHUB_JMBE_RELEASES_URL = "https://api.github.com/repos/dsheirer/jmbe/releases"; + + /** + * Major version of the JMBE API (com.github.DSheirer.jmbe:api) that this application is compiled against. Only + * JMBE creator releases with this same major version are downloaded, so that a newer, incompatible major version + * release can't replace the user's library. Update this value whenever the JMBE API dependency major version + * changes. JmbeCreatorTest verifies that this value matches the API jar on the classpath. + */ + public static final int JMBE_API_MAJOR_VERSION = 3; public static final String CREATOR_SCRIPT_LINUX = "creator"; public static final String CREATOR_SCRIPT_WINDOWS = "creator.bat"; @@ -331,7 +339,7 @@ public class JmbeCreator public static void main(String[] args) { - Release release = GitHub.getLatestRelease(GITHUB_JMBE_RELEASES_URL); + Release release = GitHub.getLatestRelease(GITHUB_JMBE_RELEASES_URL, JMBE_API_MAJOR_VERSION); Path library = Paths.get("/home/denny/temp/jmbe.jar"); JmbeCreator jmbeCreator = new JmbeCreator(release, library); diff --git a/src/main/java/io/github/dsheirer/jmbe/github/GitHub.java b/src/main/java/io/github/dsheirer/jmbe/github/GitHub.java index d4726356..57ea6e4d 100644 --- a/src/main/java/io/github/dsheirer/jmbe/github/GitHub.java +++ b/src/main/java/io/github/dsheirer/jmbe/github/GitHub.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (C) 2015-2020 Dennis Sheirer + * Copyright (C) 2015-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,7 +19,6 @@ package io.github.dsheirer.jmbe.github; -import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -33,6 +32,8 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.file.Path; import java.nio.file.StandardOpenOption; +import java.util.ArrayList; +import java.util.List; /** * Utilities for accessing GitHub repositories and releases @@ -41,6 +42,12 @@ public class GitHub { private final static Logger mLog = LoggerFactory.getLogger(GitHub.class); + /** + * Query suffix to request the maximum page size. GitHub returns 30 releases per page by default, which could + * eventually push older major version releases off of the first page. + */ + private static final String RELEASES_PAGE_SIZE = "?per_page=100"; + private GitHub() { //There's nothing to construct in this class @@ -82,14 +89,17 @@ public class GitHub } /** - * Obtain the latest release object from the repository. + * Obtains the latest release from the repository whose major version matches the specified major version. Newer + * releases with a different major version are ignored so that the downloaded JMBE creator produces a library that + * is compatible with the JMBE API that this application was compiled against. * @param repositoryURL for the GitHub api - * @return the latest release or null + * @param majorVersion that the release must match. + * @return the latest matching release or null */ - public static Release getLatestRelease(String repositoryURL) + public static Release getLatestRelease(String repositoryURL, int majorVersion) { HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest.newBuilder().uri(URI.create(repositoryURL)).build(); + HttpRequest request = HttpRequest.newBuilder().uri(URI.create(repositoryURL + RELEASES_PAGE_SIZE)).build(); try { @@ -97,7 +107,7 @@ public class GitHub if(response.statusCode() == 200) { - return parseResponse(response.body()); + return getLatestRelease(parseReleases(response.body()), majorVersion); } else { @@ -113,13 +123,38 @@ public class GitHub } /** - * Parses a json string containing an array or GitHub release objects and returns the latest release. - * @param json string returned from URL call - * @return latest release version + * Finds the latest release in the list whose major version matches the specified major version. + * @param releases to search + * @param majorVersion that the release must match + * @return latest matching release or null */ - private static Release parseResponse(String json) + public static Release getLatestRelease(List releases, int majorVersion) { - Release release = null; + Release latest = null; + + for(Release release: releases) + { + Version version = release.getVersion(); + + if(version.hasMajor() && version.getMajor() == majorVersion && + (latest == null || latest.getVersion().compareTo(version) < 0)) + { + latest = release; + } + } + + return latest; + } + + /** + * Parses a json string containing an array of GitHub release objects. Release objects that don't have a + * parsable version tag are ignored. + * @param json string returned from URL call + * @return list of releases, possibly empty + */ + public static List parseReleases(String json) + { + List releases = new ArrayList<>(); if(json != null) { @@ -127,9 +162,7 @@ public class GitHub if(element.isJsonArray()) { - JsonArray array = element.getAsJsonArray(); - - for(JsonElement child: array) + for(JsonElement child: element.getAsJsonArray()) { if(child.isJsonObject()) { @@ -138,17 +171,14 @@ public class GitHub if(version != null) { - if(release == null || release.getVersion().compareTo(version) < 0) - { - release = new Release(version, releaseObject); - } + releases.add(new Release(version, releaseObject)); } } } } } - return release; + return releases; } /** diff --git a/src/main/java/io/github/dsheirer/jmbe/github/Version.java b/src/main/java/io/github/dsheirer/jmbe/github/Version.java index 1ae257e3..99874c17 100644 --- a/src/main/java/io/github/dsheirer/jmbe/github/Version.java +++ b/src/main/java/io/github/dsheirer/jmbe/github/Version.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2020 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -167,7 +167,7 @@ public class Version implements Comparable { if(hasMajor() && other.hasMajor()) { - if(getMajor() != other.getMajor()) + if(!getMajor().equals(other.getMajor())) { return Integer.compare(getMajor(), other.getMajor()); } @@ -175,7 +175,7 @@ public class Version implements Comparable { if(hasMinor() && other.hasMinor()) { - if(getMinor() != other.getMinor()) + if(!getMinor().equals(other.getMinor())) { return Integer.compare(getMinor(), other.getMinor()); } @@ -183,7 +183,7 @@ public class Version implements Comparable { if(hasRelease() && other.hasRelease()) { - if(getRelease() != other.getRelease()) + if(!getRelease().equals(other.getRelease())) { return Integer.compare(getRelease(), other.getRelease()); } @@ -200,6 +200,10 @@ public class Version implements Comparable return 0; } } + else if(hasPatch() == other.hasPatch()) + { + return 0; + } else { return hasPatch() ? 1 : -1; diff --git a/src/main/java/io/github/dsheirer/module/decode/dmr/audio/DMRAudioModule.java b/src/main/java/io/github/dsheirer/module/decode/dmr/audio/DMRAudioModule.java index d13ec1dc..f0784aa4 100644 --- a/src/main/java/io/github/dsheirer/module/decode/dmr/audio/DMRAudioModule.java +++ b/src/main/java/io/github/dsheirer/module/decode/dmr/audio/DMRAudioModule.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2024 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -47,6 +47,7 @@ import io.github.dsheirer.sample.Listener; import java.util.ArrayList; import java.util.List; import java.util.Map; +import jmbe.frame.CorrectedAMBEFrame; import jmbe.iface.IAudioWithMetadata; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,7 +61,7 @@ public class DMRAudioModule extends AmbeAudioModule implements IdentifierUpdateP private SquelchStateListener mSquelchStateListener = new SquelchStateListener(); private ToneMetadataProcessor mToneMetadataProcessor = new ToneMetadataProcessor(); private Listener mIdentifierUpdateNotificationListener; - private List mQueuedAmbeFrames = new ArrayList<>(); + private List mQueuedAmbeFrames = new ArrayList<>(); private boolean mEncryptedCallStateEstablished = false; private boolean mEncryptedCall = false; private Listener mMessageListener; @@ -175,9 +176,16 @@ public class DMRAudioModule extends AmbeAudioModule implements IdentifierUpdateP else { List frames = voiceMessage.getAMBEFrames(); - for(byte[] frame: frames) + + //Only error correct the frames in sdrtrunk when the JMBE library (version 3+) can synthesize + //from them + List correctedFrames = isCorrectedFrameSupported() ? + voiceMessage.getCorrectedAMBEFrames() : null; + + for(int x = 0; x < frames.size(); x++) { - processAudio(frame, message.getTimestamp()); + processAudio(new AmbeFrame(frames.get(x), correctedFrames != null ? correctedFrames.get(x) : null), + message.getTimestamp()); } } } @@ -197,7 +205,7 @@ public class DMRAudioModule extends AmbeAudioModule implements IdentifierUpdateP * Processes the audio frame. Queues the frame until encryption state is determined. Once determined, the audio * frames are dequeued and audio is generated. */ - private void processAudio(byte[] frame, long timestamp) + private void processAudio(AmbeFrame frame, long timestamp) { if(mEncryptedCallStateEstablished) { @@ -212,7 +220,7 @@ public class DMRAudioModule extends AmbeAudioModule implements IdentifierUpdateP { if(!mEncryptedCall) { - for(byte[] queuedFrame: mQueuedAmbeFrames) + for(AmbeFrame queuedFrame: mQueuedAmbeFrames) { produceAudio(queuedFrame, timestamp); } @@ -232,11 +240,11 @@ public class DMRAudioModule extends AmbeAudioModule implements IdentifierUpdateP } } - private void produceAudio(byte[] frame, long timestamp) + private void produceAudio(AmbeFrame frame, long timestamp) { try { - IAudioWithMetadata audioWithMetadata = getAudioCodec().getAudioWithMetadata(frame); + IAudioWithMetadata audioWithMetadata = getAudioWithMetadata(frame.frame(), frame.correctedFrame()); addAudio(audioWithMetadata.getAudio()); processMetadata(audioWithMetadata, timestamp); } @@ -407,4 +415,11 @@ public class DMRAudioModule extends AmbeAudioModule implements IdentifierUpdateP mCurrentTone = null; } } + + /** + * AMBE voice frame as transmitted, with the optional error corrected version of the frame. + * @param frame bytes as transmitted + * @param correctedFrame for the frame, or null when the JMBE library can't synthesize from corrected frames + */ + private record AmbeFrame(byte[] frame, CorrectedAMBEFrame correctedFrame) {} } diff --git a/src/main/java/io/github/dsheirer/module/decode/dmr/message/voice/VoiceMessage.java b/src/main/java/io/github/dsheirer/module/decode/dmr/message/voice/VoiceMessage.java index e66f6e6e..ddf72862 100644 --- a/src/main/java/io/github/dsheirer/module/decode/dmr/message/voice/VoiceMessage.java +++ b/src/main/java/io/github/dsheirer/module/decode/dmr/message/voice/VoiceMessage.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2023 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,6 +27,7 @@ import io.github.dsheirer.module.decode.dmr.message.DMRBurst; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import jmbe.frame.CorrectedAMBEFrame; /** * Base DMR Voice Message @@ -41,6 +42,7 @@ public abstract class VoiceMessage extends DMRBurst public static int[] FRAME_2_IV_FRAGMENT = new int[]{215, 211, 207, 203}; public static int[] FRAME_3_IV_FRAGMENT = new int[]{287, 283, 279, 275}; private Boolean mValid; + private List mCorrectedAMBEFrames; /** * DMR message frame. This message comprises a 24-bit prefix and a 264-bit message frame. Outbound base @@ -92,6 +94,27 @@ public abstract class VoiceMessage extends DMRBurst return frames; } + /** + * Error corrected versions of the three AMBE voice frames from getAMBEFrames(), in the same order. The frames are + * error corrected once and cached. + */ + public synchronized List getCorrectedAMBEFrames() + { + if(mCorrectedAMBEFrames == null) + { + List corrected = new ArrayList<>(); + + for(byte[] frame: getAMBEFrames()) + { + corrected.add(CorrectedAMBEFrame.correct(frame)); + } + + mCorrectedAMBEFrames = corrected; + } + + return mCorrectedAMBEFrames; + } + /** * Extracts the four low-order bits from the deinterleaved C3 vector of each of the three AMBE frames that carry * fragments of the initialization vector (IV) for encrypted calls. Since we don't have access to the deinterleaved diff --git a/src/main/java/io/github/dsheirer/module/decode/nxdn/audio/NXDNAudioModule.java b/src/main/java/io/github/dsheirer/module/decode/nxdn/audio/NXDNAudioModule.java index 833aa6db..5b54f64f 100644 --- a/src/main/java/io/github/dsheirer/module/decode/nxdn/audio/NXDNAudioModule.java +++ b/src/main/java/io/github/dsheirer/module/decode/nxdn/audio/NXDNAudioModule.java @@ -35,6 +35,7 @@ import io.github.dsheirer.preference.UserPreferences; import io.github.dsheirer.sample.Listener; import java.util.ArrayList; import java.util.List; +import jmbe.frame.CorrectedAMBEFrame; /** * NXDN AMBE audio module @@ -147,9 +148,14 @@ public class NXDNAudioModule extends AmbeAudioModule { if(!mEncryptedCall && mAudioCodec != null && mAudioCodec.equals(AudioCodec.HALF_RATE)) //Full rate not yet supported { - for(byte[] frame : audio.getAudioFrames()) + List frames = audio.getAudioFrames(); + + //Only error correct the frames in sdrtrunk when the JMBE library (version 3+) can synthesize from them + List correctedFrames = isCorrectedFrameSupported() ? audio.getCorrectedAudioFrames() : null; + + for(int x = 0; x < frames.size(); x++) { - addAudio(getAudioCodec().getAudio(frame)); + addAudio(getAudio(frames.get(x), correctedFrames != null ? correctedFrames.get(x) : null)); } } else diff --git a/src/main/java/io/github/dsheirer/module/decode/nxdn/layer3/call/Audio.java b/src/main/java/io/github/dsheirer/module/decode/nxdn/layer3/call/Audio.java index a9b6e3ee..09365273 100644 --- a/src/main/java/io/github/dsheirer/module/decode/nxdn/layer3/call/Audio.java +++ b/src/main/java/io/github/dsheirer/module/decode/nxdn/layer3/call/Audio.java @@ -30,8 +30,10 @@ import io.github.dsheirer.module.decode.nxdn.layer2.SACCHFragment; import io.github.dsheirer.module.decode.nxdn.layer3.type.AudioCodec; import io.github.dsheirer.util.ByteUtil; import java.nio.ByteOrder; +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import jmbe.frame.CorrectedAMBEFrame; /** * NXDN audio frame data @@ -46,6 +48,7 @@ public class Audio extends NXDNMessage private final AudioCodec mAudioCodec; private final List mAudioFrames; + private List mCorrectedAudioFrames; private SACCHFragment mSACCHFragment; /** @@ -119,6 +122,27 @@ public class Audio extends NXDNMessage return mAudioFrames; } + /** + * Error corrected versions of the audio frames from getAudioFrames(), in the same order. The frames are error + * corrected once and cached. + */ + public synchronized List getCorrectedAudioFrames() + { + if(mCorrectedAudioFrames == null) + { + List corrected = new ArrayList<>(); + + for(byte[] frame: mAudioFrames) + { + corrected.add(CorrectedAMBEFrame.correct(frame)); + } + + mCorrectedAudioFrames = corrected; + } + + return mCorrectedAudioFrames; + } + /** * AMBE+ audio codec format * @return full or half rate diff --git a/src/main/java/io/github/dsheirer/module/decode/p25/audio/P25P1AudioModule.java b/src/main/java/io/github/dsheirer/module/decode/p25/audio/P25P1AudioModule.java index e5b6257e..273b4db5 100644 --- a/src/main/java/io/github/dsheirer/module/decode/p25/audio/P25P1AudioModule.java +++ b/src/main/java/io/github/dsheirer/module/decode/p25/audio/P25P1AudioModule.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2024 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,6 +31,7 @@ import io.github.dsheirer.preference.UserPreferences; import io.github.dsheirer.sample.Listener; import java.util.ArrayList; import java.util.List; +import jmbe.frame.CorrectedIMBEFrame; public class P25P1AudioModule extends ImbeAudioModule { @@ -133,9 +134,14 @@ public class P25P1AudioModule extends ImbeAudioModule { if(!mEncryptedCall) { - for(byte[] frame : ldu.getIMBEFrames()) + List frames = ldu.getIMBEFrames(); + + //Only error correct the frames in sdrtrunk when the JMBE library (version 3+) can synthesize from them + List correctedFrames = isCorrectedFrameSupported() ? ldu.getCorrectedIMBEFrames() : null; + + for(int x = 0; x < frames.size(); x++) { - addAudio(getAudioCodec().getAudio(frame)); + addAudio(getAudio(frames.get(x), correctedFrames != null ? correctedFrames.get(x) : null)); } } else diff --git a/src/main/java/io/github/dsheirer/module/decode/p25/audio/P25P2AudioModule.java b/src/main/java/io/github/dsheirer/module/decode/p25/audio/P25P2AudioModule.java index c551a1d5..2e29c65e 100644 --- a/src/main/java/io/github/dsheirer/module/decode/p25/audio/P25P2AudioModule.java +++ b/src/main/java/io/github/dsheirer/module/decode/p25/audio/P25P2AudioModule.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2024 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -48,6 +48,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Queue; +import jmbe.frame.CorrectedAMBEFrame; import jmbe.iface.IAudioWithMetadata; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -119,7 +120,7 @@ public class P25P2AudioModule extends AmbeAudioModule implements IdentifierUpdat { if(!mEncryptedCall) { - processAudio(abstractVoiceTimeslot.getVoiceFrames(), message.getTimestamp()); + processAudio(abstractVoiceTimeslot, message.getTimestamp()); } } else @@ -174,20 +175,27 @@ public class P25P2AudioModule extends AmbeAudioModule implements IdentifierUpdat /** * Process the audio voice frames - * @param voiceFrames to process + * @param timeslot containing the voice frames to process * @param timestamp of the carrier message */ - private void processAudio(List voiceFrames, long timestamp) + private void processAudio(AbstractVoiceTimeslot timeslot, long timestamp) { if(hasAudioCodec()) { - for(BinaryMessage voiceFrame: voiceFrames) + List voiceFrames = timeslot.getVoiceFrames(); + + //Only error correct the frames in sdrtrunk when the JMBE library (version 3+) can synthesize from them + List correctedFrames = isCorrectedFrameSupported() ? + timeslot.getCorrectedVoiceFrames() : null; + + for(int x = 0; x < voiceFrames.size(); x++) { - byte[] voiceFrameBytes = voiceFrame.getBytes(); + byte[] voiceFrameBytes = voiceFrames.get(x).getBytes(); try { - IAudioWithMetadata audioWithMetadata = getAudioCodec().getAudioWithMetadata(voiceFrameBytes); + IAudioWithMetadata audioWithMetadata = getAudioWithMetadata(voiceFrameBytes, + correctedFrames != null ? correctedFrames.get(x) : null); addAudio(audioWithMetadata.getAudio()); processMetadata(audioWithMetadata, timestamp); } diff --git a/src/main/java/io/github/dsheirer/module/decode/p25/phase1/message/ldu/LDUMessage.java b/src/main/java/io/github/dsheirer/module/decode/p25/phase1/message/ldu/LDUMessage.java index 59675d91..ca6a8714 100644 --- a/src/main/java/io/github/dsheirer/module/decode/p25/phase1/message/ldu/LDUMessage.java +++ b/src/main/java/io/github/dsheirer/module/decode/p25/phase1/message/ldu/LDUMessage.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2024 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,6 +23,7 @@ import io.github.dsheirer.bits.CorrectedBinaryMessage; import io.github.dsheirer.module.decode.p25.phase1.message.P25P1Message; import java.util.ArrayList; import java.util.List; +import jmbe.frame.CorrectedIMBEFrame; /** * LDU voice frame @@ -41,6 +42,7 @@ public abstract class LDUMessage extends P25P1Message public static final int[] LOW_SPEED_DATA = {1392, 1393, 1394, 1395, 1396, 1397, 1398, 1399, 1408, 1409, 1410, 1411, 1412, 1413, 1414, 1415}; private List mIMBIFrames; + private List mCorrectedIMBEFrames; /** * Constructs an instance @@ -98,4 +100,23 @@ public abstract class LDUMessage extends P25P1Message return mIMBIFrames; } + + /** + * Error corrected versions of the 9 IMBE voice frames from getIMBEFrames(), in the same order. The frames are + * deinterleaved, derandomized, and error corrected once and cached. + */ + public synchronized List getCorrectedIMBEFrames() + { + if(mCorrectedIMBEFrames == null) + { + mCorrectedIMBEFrames = new ArrayList<>(); + + for(byte[] frame: getIMBEFrames()) + { + mCorrectedIMBEFrames.add(CorrectedIMBEFrame.correct(frame)); + } + } + + return mCorrectedIMBEFrames; + } } diff --git a/src/main/java/io/github/dsheirer/module/decode/p25/phase2/timeslot/AbstractVoiceTimeslot.java b/src/main/java/io/github/dsheirer/module/decode/p25/phase2/timeslot/AbstractVoiceTimeslot.java index cc48a90f..caf3b7a3 100644 --- a/src/main/java/io/github/dsheirer/module/decode/p25/phase2/timeslot/AbstractVoiceTimeslot.java +++ b/src/main/java/io/github/dsheirer/module/decode/p25/phase2/timeslot/AbstractVoiceTimeslot.java @@ -1,7 +1,7 @@ /* * * * ****************************************************************************** - * * Copyright (C) 2014-2019 Dennis Sheirer + * * Copyright (C) 2014-2026 Dennis Sheirer * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by @@ -27,8 +27,10 @@ import io.github.dsheirer.bits.CorrectedBinaryMessage; import io.github.dsheirer.identifier.Identifier; import io.github.dsheirer.module.decode.p25.phase2.enumeration.DataUnitID; +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import jmbe.frame.CorrectedAMBEFrame; public abstract class AbstractVoiceTimeslot extends Timeslot { @@ -38,8 +40,31 @@ public abstract class AbstractVoiceTimeslot extends Timeslot super(message, dataUnitID, scramblingSequence, timeslot, timestamp); } + private List mCorrectedVoiceFrames; + public abstract List getVoiceFrames(); + /** + * Error corrected versions of the AMBE voice frames from getVoiceFrames(), in the same order. The frames are + * error corrected once and cached. + */ + public synchronized List getCorrectedVoiceFrames() + { + if(mCorrectedVoiceFrames == null) + { + List corrected = new ArrayList<>(); + + for(BinaryMessage frame: getVoiceFrames()) + { + corrected.add(CorrectedAMBEFrame.correct(frame.getBytes())); + } + + mCorrectedVoiceFrames = corrected; + } + + return mCorrectedVoiceFrames; + } + @Override public List getIdentifiers() { diff --git a/src/test/java/io/github/dsheirer/jmbe/JmbeCreatorTest.java b/src/test/java/io/github/dsheirer/jmbe/JmbeCreatorTest.java new file mode 100644 index 00000000..07659dc8 --- /dev/null +++ b/src/test/java/io/github/dsheirer/jmbe/JmbeCreatorTest.java @@ -0,0 +1,86 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ + +package io.github.dsheirer.jmbe; + +import io.github.dsheirer.jmbe.github.GitHub; +import io.github.dsheirer.jmbe.github.Release; +import io.github.dsheirer.jmbe.github.Version; +import java.util.List; +import jmbe.iface.IAudioCodecLibrary; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests for selecting the JMBE creator release that matches the JMBE API major version. + */ +public class JmbeCreatorTest +{ + private static final String RELEASES_JSON = """ + [ + {"tag_name": "v3.0.0", "name": "JMBE 3.0.0", "assets": []}, + {"tag_name": "v2.1.1", "name": "JMBE 2.1.1", "assets": []}, + {"tag_name": "v2.0.9", "name": "JMBE 2.0.9", "assets": []}, + {"tag_name": "v1.0.9", "name": "JMBE 1.0.9", "assets": []}, + {"tag_name": "not-a-version", "name": "Junk", "assets": []}, + {"tag_name": "v2.1.0", "name": "JMBE 2.1.0", "assets": []} + ] + """; + + @Test + void apiMajorVersionMatchesApiDependency() + { + String implementationVersion = IAudioCodecLibrary.class.getPackage().getImplementationVersion(); + assertNotNull(implementationVersion, "JMBE API jar manifest has no Implementation-Version"); + Version apiVersion = Version.fromString(implementationVersion); + assertNotNull(apiVersion, "Unparsable JMBE API version: " + implementationVersion); + assertEquals(JmbeCreator.JMBE_API_MAJOR_VERSION, (int)apiVersion.getMajor(), + "JmbeCreator.JMBE_API_MAJOR_VERSION doesn't match the JMBE API dependency version " + implementationVersion); + } + + @Test + void parseIgnoresUnversionedReleases() + { + assertEquals(5, GitHub.parseReleases(RELEASES_JSON).size()); + } + + @Test + void selectsLatestReleaseForMajorVersion() + { + List releases = GitHub.parseReleases(RELEASES_JSON); + assertEquals("2.1.1", GitHub.getLatestRelease(releases, 2).getVersion().toString()); + assertEquals("3.0.0", GitHub.getLatestRelease(releases, 3).getVersion().toString()); + assertEquals("1.0.9", GitHub.getLatestRelease(releases, 1).getVersion().toString()); + assertNull(GitHub.getLatestRelease(releases, 4)); + } + + @Test + void versionCompare() + { + assertEquals(0, Version.fromString("v2.1.1").compareTo(Version.fromString("2.1.1"))); + assertTrue(Version.fromString("v2.1.1a").compareTo(Version.fromString("v2.1.1")) > 0); + assertTrue(Version.fromString("v2.1.1").compareTo(Version.fromString("v2.1.1a")) < 0); + assertTrue(Version.fromString("v2.1.200").compareTo(Version.fromString("v2.1.199")) > 0); + assertTrue(Version.fromString("v2.10.0").compareTo(Version.fromString("v2.9.9")) > 0); + } +}