77import java .io .IOException ;
88import java .io .InputStream ;
99
10+ import martin .tempest .core .exceptions .TSDRException ;
11+ import martin .tempest .core .exceptions .TSDRLibraryNotCompatible ;
12+
1013/**
1114 * This is a Java wrapper library for TSDRLibrary
1215 *
1518 */
1619public class TSDRLibrary {
1720
21+ private static TSDRSource [] PLUGINS = new TSDRSource [] {new TSDRSource ("TSDRPlugin_RawFile" )};
22+
1823 // If the binaries weren't loaded, this will go off
19- private static Exception m_e = null ;
24+ private static TSDRLibraryNotCompatible m_e = null ;
2025
2126 /**
2227 * Extracts a library to a temporary path and prays for the OS to delete it after the app closes.
2328 * @param name
2429 * @return
2530 * @throws IOException
2631 */
27- static final File extractLibrary (final String name ) throws IOException {
32+ static final File extractLibrary (final String name ) throws TSDRLibraryNotCompatible {
2833 final String rawOSNAME = System .getProperty ("os.name" ).toLowerCase ();
2934 final String rawARCHNAME = System .getProperty ("os.arch" ).toLowerCase ();
3035
@@ -49,7 +54,7 @@ else if (rawARCHNAME.contains("64"))
4954 ARCHNAME = "X86" ;
5055
5156 if (OSNAME == null || EXT == null || ARCHNAME == null )
52- throw new RuntimeException ("Your OS or CPU is not yet supported, sorry." );
57+ throw new TSDRLibraryNotCompatible ("Your OS or CPU is not yet supported, sorry." );
5358
5459 final String relative_path = "lib/" +OSNAME +"/" +ARCHNAME +"/" +name +EXT ;
5560
@@ -60,21 +65,25 @@ else if (rawARCHNAME.contains("64"))
6065 in = new FileInputStream (relative_path );
6166 } catch (FileNotFoundException e ) {}
6267
63- if (in == null ) throw new RuntimeException ("The library has not been compiled for your OS/Architecture yet (" +OSNAME +"/" +ARCHNAME +")." );
64-
65-
66- byte [] buffer = new byte [in .available ()];
68+ if (in == null ) throw new TSDRLibraryNotCompatible ("The library has not been compiled for your OS/Architecture yet (" +OSNAME +"/" +ARCHNAME +")." );
6769
68- int read = -1 ;
69- final File temp = new File (System .getProperty ("java.io.tmpdir" ), name +EXT );
70- temp .deleteOnExit ();
71- final FileOutputStream fos = new FileOutputStream (temp );
72-
73- while ((read = in .read (buffer )) != -1 ) {
74- fos .write (buffer , 0 , read );
70+ File temp ;
71+ try {
72+ byte [] buffer = new byte [in .available ()];
73+
74+ int read = -1 ;
75+ temp = new File (System .getProperty ("java.io.tmpdir" ), name +EXT );
76+ temp .deleteOnExit ();
77+ final FileOutputStream fos = new FileOutputStream (temp );
78+
79+ while ((read = in .read (buffer )) != -1 ) {
80+ fos .write (buffer , 0 , read );
81+ }
82+ fos .close ();
83+ in .close ();
84+ } catch (IOException e ) {
85+ throw new TSDRLibraryNotCompatible (e );
7586 }
76- fos .close ();
77- in .close ();
7887
7988 return temp ;
8089 }
@@ -84,7 +93,7 @@ else if (rawARCHNAME.contains("64"))
8493 * @param name
8594 * @throws IOException
8695 */
87- static final void loadLibrary (final String name ) throws IOException {
96+ static final void loadLibrary (final String name ) throws TSDRLibraryNotCompatible {
8897 try {
8998 // try traditional method
9099 System .loadLibrary (name );
@@ -102,15 +111,32 @@ static final void loadLibrary(final String name) throws IOException {
102111 try {
103112 loadLibrary ("TSDRLibrary" );
104113 loadLibrary ("TSDRLibraryNDK" );
105- extractLibrary ( "TSDRPlugin_RawFile" );
106- } catch (IOException e ) {
114+
115+ } catch (TSDRLibraryNotCompatible e ) {
107116 m_e = e ;
108117 }
109118 }
110119
111- public TSDRLibrary () throws Exception {
120+ public TSDRLibrary () throws TSDRLibraryNotCompatible {
112121 if (m_e != null ) throw m_e ;
113122 }
114123
115- public native void test ();
124+ private native void nativeLoadPlugin (String filepath ) throws TSDRException ;
125+ public native void pluginParams (String params ) throws TSDRException ;
126+ public native void setSampleRate (long rate ) throws TSDRException ;
127+ public native void setBaseFreq (long freq ) throws TSDRException ;
128+ public native void start () throws TSDRException ;
129+ public native void stop () throws TSDRException ;
130+ public native void setGain (float gain ) throws TSDRException ;
131+ public native void readAsync () throws TSDRException ;
132+ public native void unloadPlugin () throws TSDRException ;
133+
134+ public void loadSource (final TSDRSource plugin ) throws TSDRException {
135+ nativeLoadPlugin (extractLibrary (plugin .libname ).getAbsolutePath ());
136+ }
137+
138+ public static TSDRSource [] getAllSources () {
139+ return PLUGINS ;
140+ }
141+
116142}
0 commit comments