aboutsummaryrefslogtreecommitdiff
path: root/pdoctave_get.c
diff options
context:
space:
mode:
authorFranz Zotter <fzotter@users.sourceforge.net>2006-02-22 12:48:15 +0000
committerFranz Zotter <fzotter@users.sourceforge.net>2006-02-22 12:48:15 +0000
commit97d9a1382b32a7055f407265d24f8dc900e04741 (patch)
treeb2804dbff72d634e2e0a38507e7c8c6c8768033a /pdoctave_get.c
These is the beginning of a try to connect the pd to octave. Especially when using the iemmatrix features in pd, this feature becomes sensible. On the whole it shall enable the user to use the command line parser of octave.svn2git-root
The take-over strategy is bad at this starting point. No threading, no practical data transmission. Perhaps the common work on it can help. (Franz) svn path=/trunk/externals/iem/pdoctave/; revision=4611
Diffstat (limited to 'pdoctave_get.c')
-rw-r--r--pdoctave_get.c183
1 files changed, 183 insertions, 0 deletions
diff --git a/pdoctave_get.c b/pdoctave_get.c
new file mode 100644
index 0000000..1c7df7b
--- /dev/null
+++ b/pdoctave_get.c
@@ -0,0 +1,183 @@
+#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) {
+ post("Octave not running, insert a 'pdoctave' object!!");
+ }
+ if (argc>0)
+ name = atom_getsymbol(argv);
+ else
+ name = gensym ("pdm1");
+
+ pdoctget_obj->sdf = newSharedDataFrame ();
+ 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, "write_shared_mem(");
+ cmd += 17;
+ strcpy(cmd, pdoctget_obj->oct_name);
+ cmd += strlen (cmd);
+ *cmd++ = ',';
+ sprintf(cmd, "%d", getSharedDataFrameId(pdoctget_obj->sdf));
+ cmd += strlen(cmd);
+ strcpy (cmd, ")\n");
+
+ 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");
+ 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;
+
+ sleepUntilReadUnBlocked (sdf,STD_USLEEP_TIME);
+ blockForReading (sdf);
+
+ // sending read command
+ blockForWriting (sdf);
+ pDOctaveGetCommand (pdoctget_obj);
+ sleepUntilWriteUnBlocked (sdf,STD_USLEEP_TIME);
+
+ // waiting for results
+ pdoctget_obj->data = getSharedData (sdf);
+
+ // 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
+