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
201
202
203
204
205
|
#include <string.h>
#ifndef __pdoctave_get_c_
#define __pdoctave_get_c_
#include "pdoctave.h" /* writing a command to octave */
#include "pdoctave_dataframe.h"
#include "m_pd.h"
static t_class *pdoctave_get_class;
typedef struct _PDOctaveGet_ PDOctaveGet;
struct _PDOctaveGet_
{
t_object x_obj;
t_outlet *outlet;
SharedDataFrame *sdf;
char oct_command[150];
char *oct_name;
void *data;
int list_length;
t_atom *list;
};
static void *newPDOctaveGet (t_symbol *s, int argc, t_atom *argv)
{
PDOctaveGet *pdoctget_obj = (PDOctaveGet *)
pd_new (pdoctave_get_class);
t_symbol *name;
// post("getpdoctaveinstances returned %d", getPDOctaveInstances());
if (getPDOctaveInstances()<1) {
error("Octave not running, insert a 'pdoctave' object!!");
return 0;
}
if (argc>0)
name = atom_getsymbol(argv);
else
name = gensym ("pdm1");
if ((pdoctget_obj->sdf = newSharedDataFrame ())==0) {
error("pdoctave_get: failed to get shared memory");
return 0;
}
pdoctget_obj->data = 0;
pdoctget_obj->oct_name = name->s_name;
pdoctget_obj->outlet = outlet_new (&pdoctget_obj->x_obj, 0);
return ((void *) pdoctget_obj);
}
static void pDOctaveGetCommand (PDOctaveGet *pdoctget_obj)
{
char *cmd;
cmd = pdoctget_obj->oct_command;
strcpy(cmd, "try\n write_shared_mem(");
cmd += strlen(cmd);
strcpy(cmd, pdoctget_obj->oct_name);
cmd += strlen (cmd);
*cmd++ = ',';
sprintf(cmd, "%d", getSharedDataFrameId(pdoctget_obj->sdf));
cmd += strlen(cmd);
strcpy (cmd, ");\n catch\n disp(\"undefined variable\"); write_shared_mem(");
cmd += strlen(cmd);
strcpy (cmd, "[],");
cmd +=strlen(cmd);
sprintf(cmd, "%d", getSharedDataFrameId(pdoctget_obj->sdf));
cmd +=strlen(cmd);
strcpy (cmd,");\n end\n\0");
//post("pdoctave_get: %s",pdoctget_obj->oct_command);
writeToOctaveStdIN (pdoctget_obj->oct_command);
}
static void copyFloats (float *f, t_atom *a, int n)
{
for (;n--;a++,f++)
SETFLOAT (a, *f);
}
static void outletListAllocation (PDOctaveGet *pdoctget_obj, int newsize)
{
if (newsize != pdoctget_obj->list_length) {
if (newsize > 0) {
if (pdoctget_obj->list_length > 0) {
pdoctget_obj->list = (t_atom *)
resizebytes (pdoctget_obj->list,
sizeof(t_atom) * pdoctget_obj->list_length,
sizeof(t_atom) * newsize);
pdoctget_obj->list_length = newsize;
}
else {
pdoctget_obj->list = (t_atom *) getbytes (sizeof(t_atom) * newsize);
pdoctget_obj->list_length = newsize;
}
}
else if (newsize == 0) {
freebytes (pdoctget_obj->list, sizeof(t_atom) * pdoctget_obj->list_length);
pdoctget_obj->list = 0;
pdoctget_obj->list_length = 0;
}
}
}
static void pdoctaveConvertData (PDOctaveGet *pdoctget_obj)
{
int size = getSharedDataVecLength(pdoctget_obj->sdf);
t_symbol *s;
switch (getSharedDataType (pdoctget_obj->sdf)) {
case FLOAT:
case MATRIX:
case LIST:
outletListAllocation (pdoctget_obj, size);
copyFloats ((float *) pdoctget_obj->data, pdoctget_obj->list, size);
break;
case SYMBOL:
size = 1;
outletListAllocation (pdoctget_obj, 1);
s = gensym((char*)pdoctget_obj->data);
SETSYMBOL (pdoctget_obj->list, s);
break;
case UNKNOWN:
post("pdoctave_get: unknown return value");
}
removeSharedData (pdoctget_obj->sdf, &(pdoctget_obj->data));
}
static void pdoctaveOutletList (PDOctaveGet *pdoctget_obj)
{
t_symbol *s;
switch (getSharedDataType (pdoctget_obj->sdf)) {
case FLOAT:
s = gensym("float");
break;
case LIST:
s = gensym("list");
break;
case MATRIX:
s = gensym("matrix");
break;
case SYMBOL:
s = gensym("symbol");
break;
case UNKNOWN:
post("pdoctave_get: unknown return value");
s = gensym("");
return;
}
outlet_anything (pdoctget_obj->outlet,
s, pdoctget_obj->list_length, pdoctget_obj->list);
}
static void pDOctaveGetBang (PDOctaveGet *pdoctget_obj)
{
SharedDataFrame *sdf;
sdf = pdoctget_obj->sdf;
if((sleepUntilReadUnBlocked (sdf))==0) {
error("pdoctave_get: pd and octave scheduling error, restart pd!");
return;
}
blockForReading (sdf);
// sending read command
blockForWriting (sdf);
pDOctaveGetCommand (pdoctget_obj);
// waiting for results
if((sleepUntilWriteUnBlocked (sdf))==0) {
error("pdoctave_get: pd and octave scheduling error, restart pd!");
unBlockForReading (sdf);
return;
}
if ((pdoctget_obj->data = getSharedData (sdf))==0) {
error("pdoctave_get: empty data package received");
unBlockForReading (sdf);
return;
}
// converting incoming data
pdoctaveConvertData (pdoctget_obj);
unBlockForReading (sdf);
// outletting data
pdoctaveOutletList (pdoctget_obj);
}
static void deletePDOctaveGet (PDOctaveGet *pdoctget_obj)
{
removeSharedData (pdoctget_obj->sdf, &(pdoctget_obj->data));
removeSharedDataFrame (&(pdoctget_obj->sdf));
}
void pdoctave_get_setup (void)
{
pdoctave_get_class = class_new
(gensym("pdoctave_get"),
(t_newmethod) newPDOctaveGet,
(t_method) deletePDOctaveGet,
sizeof (PDOctaveGet),
CLASS_DEFAULT, A_GIMME, 0);
class_addbang (pdoctave_get_class, (t_method) pDOctaveGetBang);
post("pdoctave_get successfully loaded");
}
#endif
|