aboutsummaryrefslogtreecommitdiff
path: root/pdoctave_send.c
blob: 0ad0f2f6a047d0440ba37b6cefe3fd5c1a49bc50 (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
#include <string.h>
#ifndef __pdoctave_send_c_
#define __pdoctave_send_c_
#include "pdoctave.h"  /* writing a command to octave */
#include "pdoctave_dataframe.h"
#include "m_pd.h"

static t_class *pdoctave_send_class;

typedef struct _PDOctaveSend_ PDOctaveSend;
struct _PDOctaveSend_
{
   t_object x_obj;
   SharedDataFrame *sdf;
   char oct_command[150];
   char *oct_name;
   void *data;
};

static void *newPDOctaveSend (t_symbol *s, int argc, t_atom *argv)
{
   PDOctaveSend *pdoctsnd_obj = (PDOctaveSend *)
      pd_new (pdoctave_send_class);
   t_symbol *name;

   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 ((pdoctsnd_obj->sdf = newSharedDataFrame ())==0) {
	   error("pdoctave_send: failed to get shared memory");
	   return 0;
   }
   pdoctsnd_obj->data = 0;
   pdoctsnd_obj->oct_name = name->s_name;

   return ((void *) pdoctsnd_obj);
}

static void pDOctaveSendBang (PDOctaveSend *pdoctsnd_obj)
{
   char *cmd;
   strcpy(pdoctsnd_obj->oct_command, pdoctsnd_obj->oct_name);
   cmd = pdoctsnd_obj->oct_command + strlen(pdoctsnd_obj->oct_name);
   strcpy(cmd, "=read_shared_mem(");
   cmd += 17;
   sprintf(cmd, "%d", getSharedDataFrameId(pdoctsnd_obj->sdf));
   cmd = cmd+strlen(cmd);
   strcpy (cmd, ");\n");

   writeToOctaveStdIN (pdoctsnd_obj->oct_command);
   freeSharedData (pdoctsnd_obj->sdf, &(pdoctsnd_obj->data));
   //removeSharedData (pdoctsnd_obj->sdf, &(pdoctsnd_obj->data));
}

static void copyFloats (float *f, t_atom *a, int n)
{
   for (;n--;a++,f++)
      *f = atom_getfloat(a);
}

static DatTyp pdSelectorClassify (char *selector)
{
   DatTyp pdtyp;
   if (!strcmp (selector, "matrix"))
      pdtyp = MATRIX;
   else if (!strcmp (selector, "float"))
      pdtyp = FLOAT;
   else if (!strcmp (selector, "list"))
      pdtyp = LIST;
   else if (!strcmp (selector, "symbol"))
      pdtyp = SYMBOL;
   else
      pdtyp = UNKNOWN;
   return pdtyp;
}

static void pDOctaveSend (PDOctaveSend *pdoctsnd_obj, 
      t_symbol *s, int argc, t_atom *argv)
{
   DatTyp pdtyp;
   SharedDataFrame *sdf;

   char *selector = s->s_name;
   t_symbol *symptr;
   float *f;
   char *c;
   int count = argc;
   
   pdtyp = pdSelectorClassify (selector);

   sdf = pdoctsnd_obj->sdf;
   
   //sleepUntilWriteUnBlocked (sdf, STD_USLEEP_TIME);
   if ((sleepUntilWriteUnBlocked (sdf))==0) {
	   error("pdoctave_send: pd and octave scheduling error, restart pd");
	   return;
   }
   blockForWriting (sdf);
   
   if (pdoctsnd_obj->data)
      removeSharedData (sdf, &(pdoctsnd_obj->data));
  
   if (pdtyp != SYMBOL) {
      if (pdoctsnd_obj->data = newSharedData (sdf, argc, sizeof(float), pdtyp)) {
	 f = (float *) pdoctsnd_obj->data;
	 copyFloats (f, argv, argc); 
      }
      else {
	 post("pdoctave_send: allocation of shared memory size %d bytes failed!",
	       sizeof(float) * argc);
	 return;
      }
   }
   else {
      symptr = atom_getsymbol (argv);
      if (pdoctsnd_obj->data = newSharedData (sdf, strlen(symptr->s_name)+1, sizeof(char), pdtyp)) {
	 c = (char *) pdoctsnd_obj->data;
	 strcpy (c, symptr->s_name);
      }
      else {
	 post("pdoctave_send: allocation of shared memory size %d bytes failed!",
	       sizeof(char) * (strlen(symptr->s_name)+1));
	 return;
      }
   }
   blockForReading (sdf);
   freeSharedData (sdf, &(pdoctsnd_obj->data));
   pDOctaveSendBang (pdoctsnd_obj);
   if ((sleepUntilReadUnBlocked (sdf))==0) {
		   error("pdoctave_send: pd and octave scheduling error, restart pd");
		   return;
   }
   unBlockForWriting (sdf);
}

static void deletePDOctaveSend (PDOctaveSend *pdoctsnd_obj)
{
   removeSharedData (pdoctsnd_obj->sdf, &(pdoctsnd_obj->data));
   removeSharedDataFrame (&(pdoctsnd_obj->sdf));
}

void pdoctave_send_setup (void)
{
   pdoctave_send_class = class_new 
      (gensym("pdoctave_send"),
       (t_newmethod) newPDOctaveSend,
       (t_method) deletePDOctaveSend,
       sizeof (PDOctaveSend),
       CLASS_DEFAULT, A_GIMME, 0);
   class_addbang (pdoctave_send_class, (t_method) pDOctaveSendBang);
   class_addanything (pdoctave_send_class, (t_method) pDOctaveSend);
   post("pdoctave_send successfully loaded");
}
#endif