aboutsummaryrefslogtreecommitdiff
path: root/src/MSPBuffer.c
blob: 084fdf39a4f92c615a489ede888bb9134cc58c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "native_classes.h"
#include "type_handler.h"
#include "pdj.h"


static t_garray *getArray(JNIEnv *env, jstring name) {
	t_symbol *s = jstring2symbol(env, name);
	t_garray *ret = (t_garray *)pd_findbyclass(s, garray_class);

  	if ( ret == NULL ) {
  		post("pdj: array %s not found", s->s_name);
  		return NULL;
  	}

	return ret;	
}


JNIEXPORT jlong JNICALL Java_com_cycling74_msp_MSPBuffer_getSize
  (JNIEnv *env, jclass cls, jstring name) {
	t_garray *array = getArray(env, name);  	
  	
  	if ( array == NULL )
  		return -1;

	return garray_npoints(array);	
}


JNIEXPORT jfloatArray JNICALL Java_com_cycling74_msp_MSPBuffer_peek
  (JNIEnv *env , jclass cls, jstring name) {
	t_garray *array = getArray(env, name);  	
	t_sample *vec;
	jfloatArray ret;
  	int size;
  	
  	if ( array == NULL )
  		return NULL;
  	
  	if ( ! garray_getfloatarray(array, &size, &vec) )
  		return NULL;
  	
	ret = (*env)->NewFloatArray(env, size);
	
  	if ( ret == NULL ) {
  		SHOWEXC;
  		return NULL;
  	}
  		
  	(*env)->SetFloatArrayRegion(env, ret, 0, size, (jfloat *) vec);

  	return ret;
}


JNIEXPORT void JNICALL Java_com_cycling74_msp_MSPBuffer_poke
  (JNIEnv *env, jclass cls, jstring name, jfloatArray values) {
	t_garray *array = getArray(env, name);  	
	t_sample *vec;
	jsize jarray_length;
  	int size;

	if ( array == NULL )
		return;
		
  	if ( ! garray_getfloatarray(array, &size, &vec) )
  		return;
	 	
	jarray_length = (*env)->GetArrayLength(env, values);
	
	if ( jarray_length < size ) {
		error("pdj: warning: array too short");
	} else {
		size = jarray_length;
	}
	
	(*env)->GetFloatArrayRegion(env, values, 0, size, vec);
	garray_redraw(array);
}


JNIEXPORT void JNICALL Java_com_cycling74_msp_MSPBuffer_setSize
  (JNIEnv *env, jclass cls, jstring name, jint channel, jlong size) {
  	t_garray *array = getArray(env, name);
  	
  	if ( array == NULL )
  		return;
  	
	garray_resize(array, size);  	
}


JNIEXPORT jfloatArray JNICALL Java_com_cycling74_msp_MSPBuffer_getArray
  (JNIEnv *env, jclass cls, jstring name, jlong from, jlong arraySize) {
	t_garray *array = getArray(env, name);  	
	t_sample *vec;
	jfloatArray ret;
  	int size, ifrom = (int) from, iarraySize = (int) arraySize;
  	
  	if ( array == NULL )
  		return NULL;
  	
  	if ( ! garray_getfloatarray(array, &size, &vec) )
  		return NULL;
  	
  	if ( iarraySize != -1 ) {
	  	if ( size < ifrom ) {
	  		error("pdj: array is shorter than the starting point");
	  		return NULL;
	  	}
  	
	  	if ( size < ifrom + iarraySize ) {
	  		error("pdj: array is not big enough to fill the array");
	  		return NULL;
	  	}
	  	size = iarraySize;
  	}
  	
	ret = (*env)->NewFloatArray(env, size);
  	if ( ret == NULL ) {
  		SHOWEXC;
  		return NULL;
  	}
  	
  	vec += ifrom;
  	(*env)->SetFloatArrayRegion(env, ret, 0, size, (jfloat *) vec);

  	return ret;
}


JNIEXPORT void JNICALL Java_com_cycling74_msp_MSPBuffer_setArray
  (JNIEnv *env, jclass cls, jstring name, jlong from, jfloatArray values) {
	t_garray *array = getArray(env, name);  	
	t_sample *vec;
	jsize jarray_length;
  	int size, ifrom = from;

	if ( array == NULL )
		return;
		
  	if ( ! garray_getfloatarray(array, &size, &vec) )
  		return;
  		
	if ( size < from ) {
		error("pdj: array too short from starting point");
		return;
	}
	jarray_length = (*env)->GetArrayLength(env, values);
	
	if ( jarray_length + ifrom > size ) {
		error("pdj: warning: array too short from java array size");
		return;
	} 

	vec += from;	
	(*env)->GetFloatArrayRegion(env, values, 0, jarray_length, vec);
	garray_redraw(array);
}