Skip to content

Commit 028019e

Browse files
committed
Image generation working
1 parent 4c11f23 commit 028019e

13 files changed

Lines changed: 139 additions & 150 deletions

File tree

JavaGUI/jni/TSDRLibraryNDK.c

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "TSDRLibraryNDK.h"
44
#include "include\TSDRLibrary.h"
55
#include "include\TSDRCodes.h"
6+
#include <stdint.h>
67
#include <string.h>
78

89
tsdr_lib_t tsdr;
@@ -56,33 +57,65 @@ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeLoadPlugin (JN
5657
}
5758

5859
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_pluginParams (JNIEnv * env, jobject obj, jstring params) {
59-
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "pluginParams");
60+
const char *nparams = (*env)->GetStringUTFChars(env, params, 0);
61+
THROW(tsdr_pluginparams(&tsdr, nparams));
62+
(*env)->ReleaseStringUTFChars(env, params, nparams);
6063
}
6164

6265
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setSampleRate (JNIEnv * env, jobject obj, jlong rate) {
63-
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "setSampleRate");
66+
THROW(tsdr_setsamplerate(&tsdr, (uint32_t) rate));
6467
}
6568

6669
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setBaseFreq (JNIEnv * env, jobject obj, jlong freq) {
67-
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "setBaseFreq");
70+
THROW(tsdr_setbasefreq(&tsdr, (uint32_t) freq));
71+
}
72+
73+
void read_async(float *buf, uint32_t len, void *ctx) {
74+
6875
}
6976

7077
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_start (JNIEnv * env, jobject obj) {
71-
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "start");
78+
79+
const int width = 800;
80+
const int height = 600;
81+
82+
jclass cls = (*env)->GetObjectClass(env, obj);
83+
84+
// fixSize(200, 200);
85+
jmethodID fixSize = (*env)->GetMethodID(env, cls, "fixSize", "(II)V");
86+
(*env)->CallVoidMethod(env, obj, fixSize, width, height);
87+
88+
printf("Get buf\n");
89+
jintArray pixels_obj = (*env)->GetObjectField(env, obj, (*env)->GetFieldID(env, cls, "pixels", "[I"));
90+
jint * pixels = (*env)->GetIntArrayElements(env,pixels_obj,0);
91+
92+
int i;
93+
const int size = width * height;
94+
for (i = 0; i < size; i++) {
95+
const int x = i % width;
96+
const int y = i / width;
97+
98+
const float rat = y / (float) height;
99+
const int col = (x > (width / 2)) ? (rat * 255.0f) : (255.0f - rat * 255.0f);
100+
pixels[i] = col | (col << 8) | (col << 16);
101+
}
102+
103+
// release elements
104+
(*env)->ReleaseIntArrayElements(env,pixels_obj,pixels,NULL);
105+
106+
// notifyCallbacks();
107+
jmethodID notifyCallbacks = (*env)->GetMethodID(env, cls, "notifyCallbacks", "()V");
108+
(*env)->CallVoidMethod(env, obj, notifyCallbacks);
72109
}
73110

74111
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_stop (JNIEnv * env, jobject obj) {
75-
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "stop");
112+
THROW(tsdr_stop(&tsdr));
76113
}
77114

78115
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setGain (JNIEnv * env, jobject obj, jfloat gain) {
79-
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "setGain");
80-
}
81-
82-
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_readAsync (JNIEnv * env, jobject obj) {
83-
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "readAsync");
116+
THROW(tsdr_setgain(&tsdr, (float) gain));
84117
}
85118

86119
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_unloadPlugin (JNIEnv * env, jobject obj) {
87-
error(env, TSDR_NOT_IMPLEMENTED, "%s not implemented", "unloadPlugin");
120+
THROW(tsdr_unloadplugin(&tsdr));
88121
}

JavaGUI/jni/TSDRLibraryNDK.h

Lines changed: 0 additions & 85 deletions
This file was deleted.

JavaGUI/jni/include/TSDRCodes.h

Lines changed: 0 additions & 8 deletions
This file was deleted.

JavaGUI/jni/include/TSDRLibrary.h

Lines changed: 0 additions & 22 deletions
This file was deleted.

JavaGUI/makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ plugins :
8787

8888
# Clean the jni stuff
8989
cleanjni: cleanplugins
90+
rm -rf *.log
9091
@$(MAKE) -C jni/ clean
9192

9293
# Clean the plugin libraries
Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
package martin.tempest;
22

3+
import java.awt.image.BufferedImage;
4+
import java.io.File;
5+
import java.io.IOException;
6+
7+
import javax.imageio.ImageIO;
8+
39
import martin.tempest.core.TSDRLibrary;
10+
import martin.tempest.core.exceptions.TSDRException;
411

5-
public class Main {
12+
public class Main implements TSDRLibrary.FrameReadyCallback {
613

714
public static void main(String[] args) {
815
try {
9-
TSDRLibrary sdrlib = new TSDRLibrary();
10-
sdrlib.loadSource(TSDRLibrary.getAllSources()[0]);
11-
sdrlib.setBaseFreq(0);
16+
new Main();
1217
} catch(Exception e) {
1318
System.err.println("Cannot load! Reason: "+e.getLocalizedMessage());
1419
e.printStackTrace();
1520
}
1621
}
1722

23+
public Main() throws TSDRException {
24+
TSDRLibrary sdrlib = new TSDRLibrary();
25+
sdrlib.loadSource(TSDRLibrary.getAllSources()[0]);
26+
sdrlib.registerFrameReadyCallback(this);
27+
sdrlib.start();
28+
29+
}
30+
31+
@Override
32+
public void onFrameReady(TSDRLibrary lib, BufferedImage frame) {
33+
System.out.println("Ouch :) "+frame.getWidth()+" x "+frame.getHeight());
34+
try {
35+
ImageIO.write(frame, "png", new File("D:\\marto.png"));
36+
} catch (IOException e) {
37+
e.printStackTrace();
38+
}
39+
}
40+
1841
}

JavaGUI/src/martin/tempest/core/TSDRLibrary.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package martin.tempest.core;
22

3+
import java.awt.image.BufferedImage;
4+
import java.awt.image.DataBufferInt;
35
import java.io.File;
46
import java.io.FileInputStream;
57
import java.io.FileNotFoundException;
68
import java.io.FileOutputStream;
79
import java.io.IOException;
810
import java.io.InputStream;
11+
import java.util.LinkedList;
912

1013
import martin.tempest.core.exceptions.TSDRException;
1114
import martin.tempest.core.exceptions.TSDRLibraryNotCompatible;
@@ -20,8 +23,13 @@ public class TSDRLibrary {
2023

2124
private static TSDRSource[] PLUGINS = new TSDRSource[] {new TSDRSource("TSDRPlugin_RawFile")};
2225

26+
// TODO! STATIC?!?!?!?!?!?!??!
27+
private BufferedImage bimage;
28+
private int[] pixels;
29+
2330
// If the binaries weren't loaded, this will go off
2431
private static TSDRLibraryNotCompatible m_e = null;
32+
private final LinkedList<FrameReadyCallback> callbacks = new LinkedList<FrameReadyCallback>();
2533

2634
/**
2735
* Extracts a library to a temporary path and prays for the OS to delete it after the app closes.
@@ -128,7 +136,6 @@ public TSDRLibrary() throws TSDRLibraryNotCompatible {
128136
public native void start() throws TSDRException;
129137
public native void stop() throws TSDRException;
130138
public native void setGain(float gain) throws TSDRException;
131-
public native void readAsync() throws TSDRException;
132139
public native void unloadPlugin() throws TSDRException;
133140

134141
public void loadSource(final TSDRSource plugin) throws TSDRException {
@@ -139,4 +146,43 @@ public static TSDRSource[] getAllSources() {
139146
return PLUGINS;
140147
}
141148

149+
public boolean registerFrameReadyCallback(final FrameReadyCallback callback) {
150+
return callbacks.add(callback);
151+
}
152+
153+
public boolean unregisterFrameReadyCallback(final FrameReadyCallback callback) {
154+
return callbacks.remove(callback);
155+
}
156+
157+
@Override
158+
protected void finalize() throws Throwable {
159+
unloadPlugin();
160+
}
161+
162+
public interface FrameReadyCallback {
163+
void onFrameReady(final TSDRLibrary lib, final BufferedImage frame);
164+
}
165+
166+
/**
167+
* The native code should call this method to initialize or resize the buffer array before accessing it
168+
* @param x the width of the frame
169+
* @param y the height of the frame
170+
*/
171+
private void fixSize(final int x, final int y) {
172+
if (bimage == null || bimage.getWidth() != x || bimage.getHeight() != y) {
173+
bimage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
174+
pixels = ((DataBufferInt) bimage.getRaster().getDataBuffer()).getData();
175+
}
176+
}
177+
178+
/**
179+
* The native code should invoke this method when it has written data to the buffer variable.
180+
* This method writes the result into the bitmap
181+
*/
182+
private void notifyCallbacks() {
183+
assert(bimage != null && pixels != null);
184+
185+
for (final FrameReadyCallback callback : callbacks) callback.onFrameReady(this, bimage);
186+
}
187+
142188
}

TSDRPlugin_RawFile/src/TSDRPlugin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
#include <stdint.h>
55

6-
typedef void(*tsdrplugin_readasync_function)(unsigned char *buf, uint32_t len, void *ctx);
6+
typedef void(*tsdrplugin_readasync_function)(float *buf, uint32_t len, void *ctx);
77

88
void tsdrplugin_getName(char *);
99
int tsdrplugin_init(char * params);
1010
int tsdrplugin_setsamplerate(uint32_t rate);
1111
int tsdrplugin_setbasefreq(uint32_t freq);
1212
int tsdrplugin_stop(void);
1313
int tsdrplugin_setgain(float gain);
14-
int tsdrplugin_readasync(tsdrplugin_readasync_function cb, uint32_t buf_num, uint32_t buf_len);
14+
int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx, uint32_t buf_num, uint32_t buf_len);
1515

1616
#endif

TSDRPlugin_RawFile/src/TSDRPlugin_RawFile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ int tsdrplugin_setgain(float gain) {
2828
return TSDR_NOT_IMPLEMENTED;
2929
}
3030

31-
int tsdrplugin_readasync(tsdrplugin_readasync_function cb, uint32_t buf_num, uint32_t buf_len) {
31+
int tsdrplugin_readasync(tsdrplugin_readasync_function cb, void *ctx, uint32_t buf_num, uint32_t buf_len) {
3232
return TSDR_NOT_IMPLEMENTED;
3333
}

0 commit comments

Comments
 (0)