Skip to content

Commit

Permalink
Make tts-adapter-sapinative work without OSGi
Browse files Browse the repository at this point in the history
  • Loading branch information
bertfrees committed Mar 17, 2022
1 parent cba7806 commit 0eb76a3
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 104 deletions.
171 changes: 78 additions & 93 deletions tts/tts-adapters/tts-adapter-sapinative/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<description>Implementation of the TTS API for the SAPI native version</description>

<dependencies>
<dependency>
<groupId>org.daisy.pipeline</groupId>
<artifactId>common-utils</artifactId>
</dependency>
<dependency>
<groupId>org.daisy.pipeline.modules</groupId>
<artifactId>tts-common</artifactId>
Expand Down Expand Up @@ -47,6 +51,20 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Import-Package>
net.sf.saxon.*;version="${saxon.versionRange}",
!org.daisy.common.spi,
*
</Import-Package>
</instructions>
</configuration>
</plugin>

<!-- note: Run mvn compile native:javah to generate again the C++ headers from -->
<!-- the Java class. -->
<plugin>
Expand All @@ -71,99 +89,66 @@
</execution>
</executions>
</plugin>

<!-- Extract the libraries from the NAR package -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.daisy.pipeline.bindings</groupId>
<artifactId>sapi-nar</artifactId>
<version>1.0.0</version>
<classifier>x64-Windows-msvc-jni</classifier>
<type>nar</type>
</artifactItem>
<artifactItem>
<groupId>org.daisy.pipeline.bindings</groupId>
<artifactId>sapi-nar</artifactId>
<version>1.0.0</version>
<classifier>x86-Windows-msvc-jni</classifier>
<type>nar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="target/generated-resources/">
<fileset dir="target/dependency">
<include name="**/*.dll"/>
</fileset>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>

<!-- Package the dynamic library into an OSGI bundle -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Import-Package>
net.sf.saxon.*;version="${saxon.versionRange}",
!org.daisy.common.spi,
*
</Import-Package>
</instructions>
</configuration>
<executions>
<execution>
<id>default-bundle</id>
<phase>none</phase>
</execution>
<execution>
<id>windows-bundle</id>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
<configuration>
<instructions>
<Bundle-NativeCode>
x86/sapinative.dll;osname=Win32;processor=x86,
x64/sapinative.dll;osname=Win32;processor=x86-64
</Bundle-NativeCode>
<Include-Resource>
transform-ssml.xsl=src/main/resources/transform-ssml.xsl,
x64/sapinative.dll=target/generated-resources/lib/x64-Windows-msvc/jni/sapi-nar.dll,
x86/sapinative.dll=target/generated-resources/lib/x86-Windows-msvc/jni/sapi-nar.dll
</Include-Resource>
</instructions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
<profiles>
<!-- Extract the libraries from the NAR package -->
<profile>
<id>generate-dll</id>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.daisy.pipeline.bindings</groupId>
<artifactId>sapi-nar</artifactId>
<version>1.0.0</version>
<classifier>x64-Windows-msvc-jni</classifier>
<type>nar</type>
</artifactItem>
<artifactItem>
<groupId>org.daisy.pipeline.bindings</groupId>
<artifactId>sapi-nar</artifactId>
<version>1.0.0</version>
<classifier>x86-Windows-msvc-jni</classifier>
<type>nar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy file="target/dependency/lib/x64-Windows-msvc/jni/sapi-nar.dll"
tofile="src/main/resources/x64/sapinative.dll"/>
<copy file="target/dependency/lib/x86-Windows-msvc/jni/sapi-nar.dll"
tofile="src/main/resources/x86/sapinative.dll"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.daisy.pipeline.tts.sapinative.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Map;

import javax.naming.directory.InvalidAttributeValueException;
import javax.sound.sampled.AudioFormat;

import org.daisy.common.file.URLs;
import org.daisy.pipeline.tts.sapinative.SAPILib;
import org.daisy.pipeline.tts.TTSEngine;
import org.daisy.pipeline.tts.TTSService;
Expand Down Expand Up @@ -37,7 +44,7 @@ public TTSEngine newEngine(Map<String, String> params) throws Throwable {
}

if (mFirstLoad) {
System.loadLibrary("sapinative");
loadDLL();
mFirstLoad = false;
}

Expand Down Expand Up @@ -79,4 +86,46 @@ private static int convertToInt(Map<String, String> params, String prop, int def
return defaultVal;
}

/**
* Unpack and load sapinative.dll
*/
static void loadDLL() throws SynthesisException {
if (!System.getProperty("os.name").toLowerCase().startsWith("windows"))
throw new SynthesisException("Not on Windows");
URL dll; {
String arch = System.getProperty("os.arch").toLowerCase();
if (arch.equals("amd64") || arch.equals("x86_64"))
dll = URLs.getResourceFromJAR("x64/sapinative.dll", SAPIservice.class);
else
dll = URLs.getResourceFromJAR("x86/sapinative.dll", SAPIservice.class);
}
File dllFile; {
try {
dllFile = new File(URLs.asURI(dll));
} catch (IllegalArgumentException iae) {
try {
File tmpDirectory = Files.createTempDirectory("pipeline-").toFile();
tmpDirectory.deleteOnExit();
dllFile = new File(tmpDirectory, "sapinative.dll");
dllFile.deleteOnExit();
dllFile.getParentFile().mkdirs();
dllFile.createNewFile();
FileOutputStream writer = new FileOutputStream(dllFile);
dll.openConnection();
InputStream reader = dll.openStream();
byte[] buffer = new byte[153600];
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) > 0) {
writer.write(buffer, 0, bytesRead);
buffer = new byte[153600];
}
writer.close();
reader.close();
} catch (IOException e) {
throw new SynthesisException("Could not unpack sapinative.dll", e);
}
}
}
System.load(dllFile.getAbsolutePath());
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.daisy.pipeline.tts.sapinative.impl;

import org.daisy.pipeline.tts.sapinative.SAPILib;
import org.daisy.pipeline.tts.TTSService.SynthesisException;

import org.junit.AfterClass;
import org.junit.Assert;
Expand All @@ -10,16 +11,8 @@
public class SAPINativeTest {

@BeforeClass
public static void load() {
String libpath = System.getProperty("user.dir") + "/"
+ "target/generated-resources/lib/";
if (System.getProperty("os.arch").endsWith("64"))
libpath += "x64";
else
libpath += "x86";
libpath += "-Windows-msvc/jni/sapi-nar.dll";

System.load(libpath);
public static void load() throws SynthesisException {
SAPIservice.loadDLL();
SAPILib.initialize(8000, 16);
}

Expand Down

0 comments on commit 0eb76a3

Please sign in to comment.