Skip to content

Commit 654e233

Browse files
committed
Working GUI and pump of images
1 parent 371872d commit 654e233

9 files changed

Lines changed: 200 additions & 48 deletions

File tree

JavaGUI/jni/TSDRLibraryNDK.c

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <string.h>
88
#include <stdlib.h>
99

10-
tsdr_lib_t tsdr;
10+
tsdr_lib_t tsdr_instance;
1111

1212
#define THROW(x) error(env, x, "")
1313

@@ -28,6 +28,9 @@ void error_translate (int exception_code, char * exceptionclass) {
2828
case TSDR_NOT_IMPLEMENTED:
2929
strcpy(exceptionclass, "martin/tempest/core/exceptions/TSDRFunctionNotImplemented");
3030
return;
31+
case TSDR_WRONG_WIDTHHEIGHT:
32+
strcpy(exceptionclass, "martin/tempest/core/exceptions/TSDRWrongWidthHeightException");
33+
return;
3134
default:
3235
strcpy(exceptionclass, "java/lang/Exception");
3336
return;
@@ -64,34 +67,31 @@ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_init (JNIEnv * env,
6467

6568
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeLoadPlugin (JNIEnv * env, jobject obj, jstring path) {
6669
const char *npath = (*env)->GetStringUTFChars(env, path, 0);
67-
THROW(tsdr_loadplugin(&tsdr, npath));
70+
THROW(tsdr_loadplugin(&tsdr_instance, npath));
6871
(*env)->ReleaseStringUTFChars(env, path, npath);
6972
}
7073

7174
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_pluginParams (JNIEnv * env, jobject obj, jstring params) {
7275
const char *nparams = (*env)->GetStringUTFChars(env, params, 0);
73-
THROW(tsdr_pluginparams(&tsdr, nparams));
76+
THROW(tsdr_pluginparams(&tsdr_instance, nparams));
7477
(*env)->ReleaseStringUTFChars(env, params, nparams);
7578
}
7679

7780
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setSampleRate (JNIEnv * env, jobject obj, jlong rate) {
78-
THROW(tsdr_setsamplerate(&tsdr, (uint32_t) rate));
81+
THROW(tsdr_setsamplerate(&tsdr_instance, (uint32_t) rate));
7982
}
8083

8184
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setBaseFreq (JNIEnv * env, jobject obj, jlong freq) {
82-
THROW(tsdr_setbasefreq(&tsdr, (uint32_t) freq));
85+
THROW(tsdr_setbasefreq(&tsdr_instance, (uint32_t) freq));
8386
}
8487

85-
void read_async(float *buf, uint32_t len, void *ctx) {
88+
void read_async(float *buf, int width, int height, void *ctx) {
8689
java_context_t * context = (java_context_t *) ctx;
8790
JNIEnv *env;
8891

8992
if ((*jvm)->GetEnv(jvm, (void **)&env, javaversion) == JNI_EDETACHED)
9093
(*jvm)->AttachCurrentThread(jvm, (void **) &env, 0);
9194

92-
const int width = 800;
93-
const int height = 600;
94-
9595
jclass cls = (*env)->GetObjectClass(env, context->obj);
9696

9797
// fixSize(200, 200);
@@ -100,16 +100,13 @@ void read_async(float *buf, uint32_t len, void *ctx) {
100100

101101
jintArray pixels_obj = (*env)->GetObjectField(env, context->obj, (*env)->GetFieldID(env, cls, "pixels", "[I"));
102102
jint * pixels = (*env)->GetIntArrayElements(env,pixels_obj,0);
103+
jint * data = pixels;
103104

104105
int i;
105106
const int size = width * height;
106107
for (i = 0; i < size; i++) {
107-
const int x = i % width;
108-
const int y = i / width;
109-
110-
const float rat = y / (float) height;
111-
const int col = (int) (buf[i] * 255.0f);
112-
pixels[i] = col | (col << 8) | (col << 16);
108+
const int col = (int) (*(buf++) * 255.0f);
109+
*(data++) = col | (col << 8) | (col << 16);
113110
}
114111

115112
// release elements
@@ -121,24 +118,37 @@ void read_async(float *buf, uint32_t len, void *ctx) {
121118
}
122119

123120
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_start (JNIEnv * env, jobject obj) {
121+
124122
java_context_t * context = (java_context_t *) malloc(sizeof(java_context_t));
125123
context->obj = (*env)->NewGlobalRef(env, obj);
126124
(*env)->DeleteLocalRef(env, obj);
127125

128-
tsdr_readasync(&tsdr, read_async, (void *) context, 800, 600);
126+
THROW(tsdr_readasync(&tsdr_instance, read_async, (void *) context));
129127

130128
(*env)->DeleteGlobalRef(env, context->obj);
131129
free(context);
132130
}
133131

134132
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_stop (JNIEnv * env, jobject obj) {
135-
THROW(tsdr_stop(&tsdr));
133+
THROW(tsdr_stop(&tsdr_instance));
136134
}
137135

138136
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setGain (JNIEnv * env, jobject obj, jfloat gain) {
139-
THROW(tsdr_setgain(&tsdr, (float) gain));
137+
THROW(tsdr_setgain(&tsdr_instance, (float) gain));
140138
}
141139

142140
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_unloadPlugin (JNIEnv * env, jobject obj) {
143-
THROW(tsdr_unloadplugin(&tsdr));
141+
THROW(tsdr_unloadplugin(&tsdr_instance));
142+
}
143+
144+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setResolution (JNIEnv * env, jobject obj, jint width, jint height) {
145+
THROW(tsdr_setresolution(&tsdr_instance, (int) width, (int) height));
146+
}
147+
148+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setVfreq (JNIEnv * env, jobject obj, jfloat freq) {
149+
THROW(tsdr_setvfreq(&tsdr_instance, (float) freq));
150+
}
151+
152+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setHfreq (JNIEnv * env, jobject obj, jfloat freq) {
153+
THROW(tsdr_sethfreq(&tsdr_instance, (float) freq));
144154
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package martin.tempest;
2+
import java.awt.Color;
3+
import java.awt.Graphics;
4+
import java.awt.image.BufferedImage;
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.sql.Timestamp;
8+
9+
import javax.imageio.ImageIO;
10+
import javax.swing.JPanel;
11+
12+
13+
public class ImageVisualizer extends JPanel {
14+
15+
private static final long serialVersionUID = -6754436015453195809L;
16+
private static final int COUNT_TO_AVG = 5;
17+
18+
private BufferedImage todraw = null;
19+
20+
private long prev = System.currentTimeMillis();
21+
private int count = 0;
22+
private int fps;
23+
24+
private int color = 255;
25+
26+
public void drawImage(final BufferedImage image) {
27+
if (image != null) color = 0;
28+
if (todraw == null) todraw = image;
29+
30+
if (todraw != null && image != null) {
31+
synchronized (todraw) {
32+
todraw = image;
33+
}
34+
35+
}
36+
37+
count++;
38+
if (count > COUNT_TO_AVG) {
39+
final long now = System.currentTimeMillis();
40+
fps = (int) Math.round((1000.0 * COUNT_TO_AVG) / (double) (now - prev));
41+
count = 0;
42+
prev = now;
43+
}
44+
45+
repaint();
46+
}
47+
48+
@Override
49+
public void paint(Graphics g) {
50+
51+
52+
if (todraw != null) {
53+
synchronized (todraw) {
54+
g.drawImage(todraw, 0, 0, getWidth(), getHeight(), null);
55+
}
56+
}
57+
58+
g.setColor(new Color(color, 255, color));
59+
color += 80; if (color > 255) color = 255;
60+
g.fillRect(0, 0, 40, 20);
61+
g.setColor(Color.black);
62+
g.drawString(fps+" fps", 10, 15);
63+
64+
}
65+
66+
public void saveImageToPNGFile(final File dir) {
67+
final File file = new File(dir, new Timestamp((new java.util.Date()).getTime()).toString().replace(" ", "").replace(":", "")+".png");
68+
if (todraw != null) {
69+
synchronized (todraw) {
70+
try {
71+
ImageIO.write(todraw, "png", file);
72+
System.out.println("File "+file+" saved!");
73+
} catch (IOException e) {
74+
e.printStackTrace();
75+
}
76+
}
77+
} else
78+
System.err.println("No file on screen to write!");
79+
80+
}
81+
}
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package martin.tempest;
22

3+
import java.awt.BorderLayout;
34
import java.awt.image.BufferedImage;
4-
import java.io.File;
5-
import java.io.IOException;
65

7-
import javax.imageio.ImageIO;
6+
import javax.swing.JFrame;
87

98
import martin.tempest.core.TSDRLibrary;
109
import martin.tempest.core.exceptions.TSDRException;
1110

1211
public class Main implements TSDRLibrary.FrameReadyCallback {
12+
13+
private static final int WIDTH = 800;
14+
private static final int HEIGHT = 600;
15+
16+
private final ImageVisualizer viz = new ImageVisualizer();
17+
private JFrame frame;
1318

1419
public static void main(String[] args) {
1520
try {
@@ -21,7 +26,14 @@ public static void main(String[] args) {
2126
}
2227

2328
public Main() throws TSDRException {
24-
TSDRLibrary sdrlib = new TSDRLibrary();
29+
frame = new JFrame();
30+
frame.setLayout(new BorderLayout());
31+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32+
frame.getContentPane().add(viz, BorderLayout.CENTER);
33+
frame.setSize(WIDTH,HEIGHT);
34+
frame.setVisible(true);
35+
36+
TSDRLibrary sdrlib = new TSDRLibrary(WIDTH, HEIGHT);
2537
sdrlib.loadSource(TSDRLibrary.getAllSources()[0]);
2638
sdrlib.registerFrameReadyCallback(this);
2739
sdrlib.start();
@@ -30,12 +42,7 @@ public Main() throws TSDRException {
3042

3143
@Override
3244
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-
}
45+
viz.drawImage(frame);
3946
}
4047

4148
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ static final void loadLibrary(final String name) throws TSDRLibraryNotCompatible
125125
}
126126
}
127127

128-
public TSDRLibrary() throws TSDRLibraryNotCompatible {
128+
public TSDRLibrary(int width, int height) throws TSDRException {
129129
if (m_e != null) throw m_e;
130130
init();
131+
setResolution(width, height);
131132
}
132133

133134
private native void init();
@@ -139,6 +140,9 @@ public TSDRLibrary() throws TSDRLibraryNotCompatible {
139140
public native void stop() throws TSDRException;
140141
public native void setGain(float gain) throws TSDRException;
141142
public native void unloadPlugin() throws TSDRException;
143+
public native void setResolution(int width, int height) throws TSDRException;
144+
public native void setVfreq(float freq) throws TSDRException;
145+
public native void setHfreq(float freq) throws TSDRException;
142146

143147
public void loadSource(final TSDRSource plugin) throws TSDRException {
144148
nativeLoadPlugin(extractLibrary(plugin.libname).getAbsolutePath());
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package martin.tempest.core.exceptions;
2+
3+
public class TSDRWrongWidthHeightException extends TSDRException {
4+
5+
private static final long serialVersionUID = -7807615914689137600L;
6+
7+
public TSDRWrongWidthHeightException(final Exception e) {
8+
super(e);
9+
}
10+
11+
public TSDRWrongWidthHeightException(final String msg) {
12+
super(msg);
13+
}
14+
}

TSDRPlugin_RawFile/src/TSDRCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#define TSDR_OK (0)
55
#define TSDR_ERR_PLUGIN (1)
6+
#define TSDR_WRONG_WIDTHHEIGHT (2)
67
#define TSDR_NOT_IMPLEMENTED (404)
78

89
#endif

0 commit comments

Comments
 (0)