aboutsummaryrefslogtreecommitdiff
path: root/k_cext.h
blob: e3749522123bb3685610bf7c1d6f72f10264e0a7 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 *   k_cext.h copyright 2002 Kjetil S. Matheussen.
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation; either version 2.1 of
 *   the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//#include <stdbool.h>
#ifdef _MSC_VER
typedef int bool;
#define true 1
#define false 0
#else
#include <stdbool.h>
#endif

#include <stdarg.h>

#ifdef MSW
#define K_EXTERN __declspec(dllexport) extern
#else
#define K_EXTERN extern
#endif

typedef struct k_cext
{
  t_object x_ob;
  t_float *values;

  int num_ins;
  int num_outs;

  t_inlet **inlets;
  t_outlet **outlets;

  void (*k_cext_process)(struct k_cext *x);
  void *handle;

  char filename[40];

  bool iscext;

  void *userdata; // This attribute /can/ be used by a patch, but using "static" is much cleaner, so please don't.
} t_k_cext;


/* The following functions are used by intsort and floatsort */
K_EXTERN int k_cext_intcompare(const void *p1, const void *p2);
K_EXTERN int k_cext_floatcompare(const void *p1, const void *p2);


/* The following functions are system dependant, and called internally from k_cext only.
   All ports must implement these functions.
 */

K_EXTERN int k_sys_getprocessfunction(t_k_cext *x,char *funcname,char *name);
K_EXTERN void k_sys_freehandle(t_k_cext *x);
K_EXTERN void k_sys_mktempfilename(char *to);
K_EXTERN void k_sys_writeincludes(FILE *file);
K_EXTERN void k_sys_makecompilestring(char *to,char *name,char *funcname);
K_EXTERN void k_sys_deletefile(char *name);
K_EXTERN void k_sys_init(void);

/* TB value accessing functions*/
K_EXTERN t_float k_cext_getvalue(char c[]);
K_EXTERN int k_cext_setvalue(char c[],float f);


#define V(a) (x->values[a])
#define I(a) ((int)(x->values[a]))


#define O(a,b) outlet_float(x->outlets[a],b)

#define O0(b) O(0,b)
#define O1(b) O(1,b)
#define O2(b) O(2,b)
#define O3(b) O(3,b)
#define O4(b) O(4,b)
#define O5(b) O(5,b)
#define O6(b) O(6,b)

#define BETWEEN(dasmin,dasmax) ((dasmin) + (((float)(dasmax-(dasmin)))*rand())/(RAND_MAX+1.0))
#define RANDOM(dasmax) BETWEEN(0,dasmax)

#define SEND(symname,val) \
do{ \
  static t_symbol *k_cext_internal_symbol=NULL; \
  if(k_cext_internal_symbol==NULL) k_cext_internal_symbol=gensym(symname); \
  if(k_cext_internal_symbol->s_thing) pd_float(k_cext_internal_symbol->s_thing, val);  \
}while(0)



#define INTARRAY(name,len) int name[len]={0}
#define FLOATARRAY(name,len) t_float name[len]={0.0f}

#define INTSORT(a,b) qsort((void *)(a),b, sizeof (int), k_cext_intcompare);
#define FLOATSORT(a,b) qsort((void *)(a),b, sizeof (float), k_cext_floatcompare);

/* TB: values and bang outlets */
#define Ob(a) outlet_bang(x->outlets[a]);
float k_cext_getvalue(char c[]);
int k_cext_setvalue(char c[],float f);
#define VALUE(char) k_cext_getvalue(char)
#define SETVALUE(char,float) k_cext_setvalue(char,float)


#define IF if(
#define FOR for(
#define RANGE(a,b,c) for(a=b;a<c;a++)
#define WHILE while(
#define SWITCH switch(

#define THEN )
#define BEGIN {

#define LOOP for(;;){

#define ELIF }else if(

/* If you think "END ELSE BEGIN" is more natural, just write "END else BEGIN". */
#define ELSE }else{

#define END }
#define ENDFOR END
#define ENDRANGE END
#define ENDIF END
#define ENDWHILE END
#define ENDLOOP END
#define ENDSWITCH END

#define SC ;

#define NL "\n"
  
#define SP " "


typedef int (*k_cext_f_int_callback)(t_k_cext *x,...);
typedef float (*k_cext_f_float_callback)(t_k_cext *x,...);

#ifndef _MSC_VER
static k_cext_f_int_callback *k_cext_int_funcs[];
static k_cext_f_float_callback *k_cext_float_funcs[];
static t_k_cext **k_cext_int_x[];
static t_k_cext **k_cext_float_x[];
#else
k_cext_f_int_callback *k_cext_int_funcs[];
k_cext_f_float_callback *k_cext_float_funcs[];
t_k_cext **k_cext_int_x[];
t_k_cext **k_cext_float_x[];
#endif

bool k_cext_get_int_funcs(k_cext_f_int_callback **funcs,t_k_cext **xs[],int length,...);
bool k_cext_get_float_funcs(k_cext_f_float_callback **funcs,t_k_cext **xs[],int length,...);


#define INT_(b,a) (*k_cext_int_funcs[a])
#define I_(a) *k_cext_int_x[a]


/* 
   The alternative suggested by Thomas Grill might be better:

#define INT_0(b,a) (*k_cext_int_funcs[a])(*k_cext_int_x[a])
#define INT_1(b,a,c) (*k_cext_int_funcs[a])(*k_cext_int_x[a],c)
#define INT_2(b,a,c,d) (*k_cext_int_funcs[a])(*k_cext_int_x[a],c,d)
#define INT_3(b,a,c,e) (*k_cext_int_funcs[a])(*k_cext_int_x[a],c,d,e)
#define INT_4(b,a,c,e,f) (*k_cext_int_funcs[a])(*k_cext_int_x[a],c,d,e,f)
...
 */


/* VC is reported not to be an iso99 c compiler, so the following very nice macro can't be used. #$¥5¥@{@{@$¥@ !!! */
/* #define F_INT(a,...) (*k_cext_int_funcs[a])(*k_cext_int_x[a],__VA_ARGS__) */


#define FLOAT_(b,a) (*k_cext_float_funcs[a])
#define F_(a) *k_cext_float_x[a]

/* Same here. */
/* #define F_FLOAT(a,...) (*k_cext_float_funcs[a])(*k_cext_float_x[a],__VA_ARGS__) */