Skip to content

Commit

Permalink
[voskstt] Fix load on linux arm (openhab#13556)
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
  • Loading branch information
GiviMAD authored and nemerdaud committed Feb 28, 2023
1 parent f770d92 commit 8b4478e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions bundles/org.openhab.voice.voskstt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The following platforms are supported:
* osx
* win64

**On Linux this binary requires the package libatomic to be installed (apt install libatomic1).**

## Configuring the model

Before you can use this service you should configure your language model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.vosk.Recognizer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.jna.NativeLibrary;

/**
* The {@link VoskSTTService} class is a service implementation to use Vosk-API for Speech-to-Text.
Expand All @@ -77,11 +78,6 @@ public class VoskSTTService implements STTService {
logger.info("vosk dir created {}", VOSK_FOLDER);
}
}
try {
LibVosk.setLogLevel(LogLevel.WARNINGS);
} catch (UnsatisfiedLinkError e) {
logger.warn("UnsatisfiedLinkError: {}", e.getMessage());
}
}
private final Logger logger = LoggerFactory.getLogger(VoskSTTService.class);
private final ScheduledExecutorService executor = ThreadPoolManager.getScheduledPool("OH-voice-voskstt");
Expand All @@ -96,7 +92,18 @@ public VoskSTTService(@Reference LocaleService localeService) {

@Activate
protected void activate(Map<String, Object> config) {
configChange(config);
try {
String osName = System.getProperty("os.name", "generic").toLowerCase();
String osArch = System.getProperty("os.arch", "").toLowerCase();
if (osName.contains("linux") && (osArch.equals("arm") || osArch.equals("armv7l"))) {
// workaround for loading required shared libraries
loadSharedLibrariesArmv7l();
}
LibVosk.setLogLevel(LogLevel.WARNINGS);
configChange(config);
} catch (LinkageError e) {
logger.warn("LinkageError, service will not work: {}", e.getMessage());
}
}

@Modified
Expand Down Expand Up @@ -305,4 +312,22 @@ private void trySleep(long ms) {
private boolean isExpiredInterval(long interval, long referenceTime) {
return System.currentTimeMillis() - referenceTime > interval;
}

private void loadSharedLibrariesArmv7l() {
logger.debug("loading required shared libraries for linux arm");
var libatomicArmLibPath = Path.of("/usr/lib/arm-linux-gnueabihf/libatomic.so.1");
if (libatomicArmLibPath.toFile().exists()) {
var libatomicArmLibFolderPath = libatomicArmLibPath.getParent().toAbsolutePath();
String libraryPath = System.getProperty("jna.library.path", System.getProperty("java.library.path"));
if (!libraryPath.contains(libatomicArmLibFolderPath.toString())) {
libraryPath = libatomicArmLibFolderPath + "/:" + libraryPath;
System.setProperty("jna.library.path", libraryPath);
logger.debug("jna library path updated: {}", libraryPath);
}
NativeLibrary.getInstance("libatomic");
logger.debug("loaded libatomic shared library");
} else {
throw new LinkageError("Required shared library libatomic is missing");
}
}
}

0 comments on commit 8b4478e

Please sign in to comment.