Mercurial > hg > beaglert
changeset 230:af211ee57867 mergingClockSync
Working basic_libpd
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sat, 09 Apr 2016 08:19:09 +0100 |
parents | c768ed1055b0 |
children | c0bf6157f67e |
files | projects/basic_libpd/m_pd.h projects/basic_libpd/main.cpp projects/basic_libpd/render.cpp projects/basic_libpd/s_stuff.h projects/basic_libpd/z_libpd.h |
diffstat | 5 files changed, 1542 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/basic_libpd/m_pd.h Sat Apr 09 08:19:09 2016 +0100 @@ -0,0 +1,770 @@ +/* Copyright (c) 1997-1999 Miller Puckette. +* For information on usage and redistribution, and for a DISCLAIMER OF ALL +* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ + +#ifndef __m_pd_h_ + +#if defined(_LANGUAGE_C_PLUS_PLUS) || defined(__cplusplus) +extern "C" { +#endif + +#define PD_MAJOR_VERSION 0 +#define PD_MINOR_VERSION 46 +#define PD_BUGFIX_VERSION 6 +#define PD_TEST_VERSION "" +extern int pd_compatibilitylevel; /* e.g., 43 for pd 0.43 compatibility */ + +/* old name for "MSW" flag -- we have to take it for the sake of many old +"nmakefiles" for externs, which will define NT and not MSW */ +#if defined(NT) && !defined(MSW) +#define MSW +#endif + +/* These pragmas are only used for MSVC, not MinGW or Cygwin <hans@at.or.at> */ +#ifdef _MSC_VER +/* #pragma warning( disable : 4091 ) */ +#pragma warning( disable : 4305 ) /* uncast const double to float */ +#pragma warning( disable : 4244 ) /* uncast float/int conversion etc. */ +#pragma warning( disable : 4101 ) /* unused automatic variables */ +#endif /* _MSC_VER */ + + /* the external storage class is "extern" in UNIX; in MSW it's ugly. */ +#ifdef _WIN32 +#ifdef PD_INTERNAL +#define EXTERN __declspec(dllexport) extern +#else +#define EXTERN __declspec(dllimport) extern +#endif /* PD_INTERNAL */ +#else +#define EXTERN extern +#endif /* _WIN32 */ + + /* and depending on the compiler, hidden data structures are + declared differently: */ +#if defined( __GNUC__) || defined( __BORLANDC__ ) || defined( __MWERKS__ ) +#define EXTERN_STRUCT struct +#else +#define EXTERN_STRUCT extern struct +#endif + +/* Define some attributes, specific to the compiler */ +#if defined(__GNUC__) +#define ATTRIBUTE_FORMAT_PRINTF(a, b) __attribute__ ((format (printf, a, b))) +#else +#define ATTRIBUTE_FORMAT_PRINTF(a, b) +#endif + +#if !defined(_SIZE_T) && !defined(_SIZE_T_) +#include <stddef.h> /* just for size_t -- how lame! */ +#endif + +/* Microsoft Visual Studio is not C99, it does not provide stdint.h */ +#ifdef _MSC_VER +typedef signed __int8 int8_t; +typedef signed __int16 int16_t; +typedef signed __int32 int32_t; +typedef signed __int64 int64_t; +typedef unsigned __int8 uint8_t; +typedef unsigned __int16 uint16_t; +typedef unsigned __int32 uint32_t; +typedef unsigned __int64 uint64_t; +#else +# include <stdint.h> +#endif + +/* for FILE, needed by sys_fopen() and sys_fclose() only */ +#include <stdio.h> + +#define MAXPDSTRING 1000 /* use this for anything you want */ +#define MAXPDARG 5 /* max number of args we can typecheck today */ + +/* signed and unsigned integer types the size of a pointer: */ +#if !defined(PD_LONGINTTYPE) +#define PD_LONGINTTYPE long +#endif + +#if !defined(PD_FLOATSIZE) + /* normally, our floats (t_float, t_sample,...) are 32bit */ +# define PD_FLOATSIZE 32 +#endif + +#if PD_FLOATSIZE == 32 +# define PD_FLOATTYPE float +/* an unsigned int of the same size as FLOATTYPE: */ +# define PD_FLOATUINTTYPE unsigned int + +#elif PD_FLOATSIZE == 64 +# define PD_FLOATTYPE double +# define PD_FLOATUINTTYPE unsigned long +#else +# error invalid FLOATSIZE: must be 32 or 64 +#endif + +typedef PD_LONGINTTYPE t_int; /* pointer-size integer */ +typedef PD_FLOATTYPE t_float; /* a float type at most the same size */ +typedef PD_FLOATTYPE t_floatarg; /* float type for function calls */ + +typedef struct _symbol +{ + char *s_name; + struct _class **s_thing; + struct _symbol *s_next; +} t_symbol; + +EXTERN_STRUCT _array; +#define t_array struct _array /* g_canvas.h */ + +/* pointers to glist and array elements go through a "stub" which sticks +around after the glist or array is freed. The stub itself is deleted when +both the glist/array is gone and the refcount is zero, ensuring that no +gpointers are pointing here. */ + +#define GP_NONE 0 /* the stub points nowhere (has been cut off) */ +#define GP_GLIST 1 /* the stub points to a glist element */ +#define GP_ARRAY 2 /* ... or array */ + +typedef struct _gstub +{ + union + { + struct _glist *gs_glist; /* glist we're in */ + struct _array *gs_array; /* array we're in */ + } gs_un; + int gs_which; /* GP_GLIST/GP_ARRAY */ + int gs_refcount; /* number of gpointers pointing here */ +} t_gstub; + +typedef struct _gpointer /* pointer to a gobj in a glist */ +{ + union + { + struct _scalar *gp_scalar; /* scalar we're in (if glist) */ + union word *gp_w; /* raw data (if array) */ + } gp_un; + int gp_valid; /* number which must match gpointee */ + t_gstub *gp_stub; /* stub which points to glist/array */ +} t_gpointer; + +typedef union word +{ + t_float w_float; + t_symbol *w_symbol; + t_gpointer *w_gpointer; + t_array *w_array; + struct _binbuf *w_binbuf; + int w_index; +} t_word; + +typedef enum +{ + A_NULL, + A_FLOAT, + A_SYMBOL, + A_POINTER, + A_SEMI, + A_COMMA, + A_DEFFLOAT, + A_DEFSYM, + A_DOLLAR, + A_DOLLSYM, + A_GIMME, + A_CANT +} t_atomtype; + +#define A_DEFSYMBOL A_DEFSYM /* better name for this */ + +typedef struct _atom +{ + t_atomtype a_type; + union word a_w; +} t_atom; + +EXTERN_STRUCT _class; +#define t_class struct _class + +EXTERN_STRUCT _outlet; +#define t_outlet struct _outlet + +EXTERN_STRUCT _inlet; +#define t_inlet struct _inlet + +EXTERN_STRUCT _binbuf; +#define t_binbuf struct _binbuf + +EXTERN_STRUCT _clock; +#define t_clock struct _clock + +EXTERN_STRUCT _outconnect; +#define t_outconnect struct _outconnect + +EXTERN_STRUCT _glist; +#define t_glist struct _glist +#define t_canvas struct _glist /* LATER lose this */ + +typedef t_class *t_pd; /* pure datum: nothing but a class pointer */ + +typedef struct _gobj /* a graphical object */ +{ + t_pd g_pd; /* pure datum header (class) */ + struct _gobj *g_next; /* next in list */ +} t_gobj; + +typedef struct _scalar /* a graphical object holding data */ +{ + t_gobj sc_gobj; /* header for graphical object */ + t_symbol *sc_template; /* template name (LATER replace with pointer) */ + t_word sc_vec[1]; /* indeterminate-length array of words */ +} t_scalar; + +typedef struct _text /* patchable object - graphical, with text */ +{ + t_gobj te_g; /* header for graphical object */ + t_binbuf *te_binbuf; /* holder for the text */ + t_outlet *te_outlet; /* linked list of outlets */ + t_inlet *te_inlet; /* linked list of inlets */ + short te_xpix; /* x&y location (within the toplevel) */ + short te_ypix; + short te_width; /* requested width in chars, 0 if auto */ + unsigned int te_type:2; /* from defs below */ +} t_text; + +#define T_TEXT 0 /* just a textual comment */ +#define T_OBJECT 1 /* a MAX style patchable object */ +#define T_MESSAGE 2 /* a MAX stype message */ +#define T_ATOM 3 /* a cell to display a number or symbol */ + +#define te_pd te_g.g_pd + + /* t_object is synonym for t_text (LATER unify them) */ + +typedef struct _text t_object; + +#define ob_outlet te_outlet +#define ob_inlet te_inlet +#define ob_binbuf te_binbuf +#define ob_pd te_g.g_pd +#define ob_g te_g + +typedef void (*t_method)(void); +typedef void *(*t_newmethod)( void); + +/* in ARM 64 a varargs prototype generates a different function call sequence +from a fixed one, so in that special case we make a more restrictive +definition for t_gotfn. This will break some code in the "chaos" package +in Pd extended. (that code will run incorrectly anyhow so why not catch it +at compile time anyhow.) */ +#if defined(__APPLE__) && defined(__aarch64__) +typedef void (*t_gotfn)(void *x); +#else +typedef void (*t_gotfn)(void *x, ...); +#endif + +/* ---------------- pre-defined objects and symbols --------------*/ +EXTERN t_pd pd_objectmaker; /* factory for creating "object" boxes */ +EXTERN t_pd pd_canvasmaker; /* factory for creating canvases */ +EXTERN t_symbol s_pointer; +EXTERN t_symbol s_float; +EXTERN t_symbol s_symbol; +EXTERN t_symbol s_bang; +EXTERN t_symbol s_list; +EXTERN t_symbol s_anything; +EXTERN t_symbol s_signal; +EXTERN t_symbol s__N; +EXTERN t_symbol s__X; +EXTERN t_symbol s_x; +EXTERN t_symbol s_y; +EXTERN t_symbol s_; + +/* --------- prototypes from the central message system ----------- */ +EXTERN void pd_typedmess(t_pd *x, t_symbol *s, int argc, t_atom *argv); +EXTERN void pd_forwardmess(t_pd *x, int argc, t_atom *argv); +EXTERN t_symbol *gensym(const char *s); +EXTERN t_gotfn getfn(t_pd *x, t_symbol *s); +EXTERN t_gotfn zgetfn(t_pd *x, t_symbol *s); +EXTERN void nullfn(void); +EXTERN void pd_vmess(t_pd *x, t_symbol *s, char *fmt, ...); + +/* the following macrose are for sending non-type-checkable mesages, i.e., +using function lookup but circumventing type checking on arguments. Only +use for internal messaging protected by A_CANT so that the message can't +be generated at patch level. */ +#define mess0(x, s) ((*getfn((x), (s)))((x))) +typedef void (*t_gotfn1)(void *x, void *arg1); +#define mess1(x, s, a) ((*(t_gotfn1)getfn((x), (s)))((x), (a))) +typedef void (*t_gotfn2)(void *x, void *arg1, void *arg2); +#define mess2(x, s, a,b) ((*(t_gotfn2)getfn((x), (s)))((x), (a),(b))) +typedef void (*t_gotfn3)(void *x, void *arg1, void *arg2, void *arg3); +#define mess3(x, s, a,b,c) ((*(t_gotfn3)getfn((x), (s)))((x), (a),(b),(c))) +typedef void (*t_gotfn4)(void *x, + void *arg1, void *arg2, void *arg3, void *arg4); +#define mess4(x, s, a,b,c,d) \ + ((*(t_gotfn4)getfn((x), (s)))((x), (a),(b),(c),(d))) +typedef void (*t_gotfn5)(void *x, + void *arg1, void *arg2, void *arg3, void *arg4, void *arg5); +#define mess5(x, s, a,b,c,d,e) \ + ((*(t_gotfn5)getfn((x), (s)))((x), (a),(b),(c),(d),(e))) + +EXTERN void obj_list(t_object *x, t_symbol *s, int argc, t_atom *argv); +EXTERN t_pd *pd_newest(void); + +/* --------------- memory management -------------------- */ +EXTERN void *getbytes(size_t nbytes); +EXTERN void *getzbytes(size_t nbytes); +EXTERN void *copybytes(void *src, size_t nbytes); +EXTERN void freebytes(void *x, size_t nbytes); +EXTERN void *resizebytes(void *x, size_t oldsize, size_t newsize); + +/* -------------------- atoms ----------------------------- */ + +#define SETSEMI(atom) ((atom)->a_type = A_SEMI, (atom)->a_w.w_index = 0) +#define SETCOMMA(atom) ((atom)->a_type = A_COMMA, (atom)->a_w.w_index = 0) +#define SETPOINTER(atom, gp) ((atom)->a_type = A_POINTER, \ + (atom)->a_w.w_gpointer = (gp)) +#define SETFLOAT(atom, f) ((atom)->a_type = A_FLOAT, (atom)->a_w.w_float = (f)) +#define SETSYMBOL(atom, s) ((atom)->a_type = A_SYMBOL, \ + (atom)->a_w.w_symbol = (s)) +#define SETDOLLAR(atom, n) ((atom)->a_type = A_DOLLAR, \ + (atom)->a_w.w_index = (n)) +#define SETDOLLSYM(atom, s) ((atom)->a_type = A_DOLLSYM, \ + (atom)->a_w.w_symbol= (s)) + +EXTERN t_float atom_getfloat(t_atom *a); +EXTERN t_int atom_getint(t_atom *a); +EXTERN t_symbol *atom_getsymbol(t_atom *a); +EXTERN t_symbol *atom_gensym(t_atom *a); +EXTERN t_float atom_getfloatarg(int which, int argc, t_atom *argv); +EXTERN t_int atom_getintarg(int which, int argc, t_atom *argv); +EXTERN t_symbol *atom_getsymbolarg(int which, int argc, t_atom *argv); + +EXTERN void atom_string(t_atom *a, char *buf, unsigned int bufsize); + +/* ------------------ binbufs --------------- */ + +EXTERN t_binbuf *binbuf_new(void); +EXTERN void binbuf_free(t_binbuf *x); +EXTERN t_binbuf *binbuf_duplicate(t_binbuf *y); + +EXTERN void binbuf_text(t_binbuf *x, char *text, size_t size); +EXTERN void binbuf_gettext(t_binbuf *x, char **bufp, int *lengthp); +EXTERN void binbuf_clear(t_binbuf *x); +EXTERN void binbuf_add(t_binbuf *x, int argc, t_atom *argv); +EXTERN void binbuf_addv(t_binbuf *x, char *fmt, ...); +EXTERN void binbuf_addbinbuf(t_binbuf *x, t_binbuf *y); +EXTERN void binbuf_addsemi(t_binbuf *x); +EXTERN void binbuf_restore(t_binbuf *x, int argc, t_atom *argv); +EXTERN void binbuf_print(t_binbuf *x); +EXTERN int binbuf_getnatom(t_binbuf *x); +EXTERN t_atom *binbuf_getvec(t_binbuf *x); +EXTERN int binbuf_resize(t_binbuf *x, int newsize); +EXTERN void binbuf_eval(t_binbuf *x, t_pd *target, int argc, t_atom *argv); +EXTERN int binbuf_read(t_binbuf *b, char *filename, char *dirname, + int crflag); +EXTERN int binbuf_read_via_canvas(t_binbuf *b, char *filename, t_canvas *canvas, + int crflag); +EXTERN int binbuf_read_via_path(t_binbuf *b, char *filename, char *dirname, + int crflag); +EXTERN int binbuf_write(t_binbuf *x, char *filename, char *dir, + int crflag); +EXTERN void binbuf_evalfile(t_symbol *name, t_symbol *dir); +EXTERN t_symbol *binbuf_realizedollsym(t_symbol *s, int ac, t_atom *av, + int tonew); + +/* ------------------ clocks --------------- */ + +EXTERN t_clock *clock_new(void *owner, t_method fn); +EXTERN void clock_set(t_clock *x, double systime); +EXTERN void clock_delay(t_clock *x, double delaytime); +EXTERN void clock_unset(t_clock *x); +EXTERN void clock_setunit(t_clock *x, double timeunit, int sampflag); +EXTERN double clock_getlogicaltime(void); +EXTERN double clock_getsystime(void); /* OBSOLETE; use clock_getlogicaltime() */ +EXTERN double clock_gettimesince(double prevsystime); +EXTERN double clock_gettimesincewithunits(double prevsystime, + double units, int sampflag); +EXTERN double clock_getsystimeafter(double delaytime); +EXTERN void clock_free(t_clock *x); + +/* ----------------- pure data ---------------- */ +EXTERN t_pd *pd_new(t_class *cls); +EXTERN void pd_free(t_pd *x); +EXTERN void pd_bind(t_pd *x, t_symbol *s); +EXTERN void pd_unbind(t_pd *x, t_symbol *s); +EXTERN t_pd *pd_findbyclass(t_symbol *s, t_class *c); +EXTERN void pd_pushsym(t_pd *x); +EXTERN void pd_popsym(t_pd *x); +EXTERN t_symbol *pd_getfilename(void); +EXTERN t_symbol *pd_getdirname(void); +EXTERN void pd_bang(t_pd *x); +EXTERN void pd_pointer(t_pd *x, t_gpointer *gp); +EXTERN void pd_float(t_pd *x, t_float f); +EXTERN void pd_symbol(t_pd *x, t_symbol *s); +EXTERN void pd_list(t_pd *x, t_symbol *s, int argc, t_atom *argv); +EXTERN void pd_anything(t_pd *x, t_symbol *s, int argc, t_atom *argv); +#define pd_class(x) (*(x)) + +/* ----------------- pointers ---------------- */ +EXTERN void gpointer_init(t_gpointer *gp); +EXTERN void gpointer_copy(const t_gpointer *gpfrom, t_gpointer *gpto); +EXTERN void gpointer_unset(t_gpointer *gp); +EXTERN int gpointer_check(const t_gpointer *gp, int headok); + +/* ----------------- patchable "objects" -------------- */ +EXTERN t_inlet *inlet_new(t_object *owner, t_pd *dest, t_symbol *s1, + t_symbol *s2); +EXTERN t_inlet *pointerinlet_new(t_object *owner, t_gpointer *gp); +EXTERN t_inlet *floatinlet_new(t_object *owner, t_float *fp); +EXTERN t_inlet *symbolinlet_new(t_object *owner, t_symbol **sp); +EXTERN t_inlet *signalinlet_new(t_object *owner, t_float f); +EXTERN void inlet_free(t_inlet *x); + +EXTERN t_outlet *outlet_new(t_object *owner, t_symbol *s); +EXTERN void outlet_bang(t_outlet *x); +EXTERN void outlet_pointer(t_outlet *x, t_gpointer *gp); +EXTERN void outlet_float(t_outlet *x, t_float f); +EXTERN void outlet_symbol(t_outlet *x, t_symbol *s); +EXTERN void outlet_list(t_outlet *x, t_symbol *s, int argc, t_atom *argv); +EXTERN void outlet_anything(t_outlet *x, t_symbol *s, int argc, t_atom *argv); +EXTERN t_symbol *outlet_getsymbol(t_outlet *x); +EXTERN void outlet_free(t_outlet *x); +EXTERN t_object *pd_checkobject(t_pd *x); + + +/* -------------------- canvases -------------- */ + +EXTERN void glob_setfilename(void *dummy, t_symbol *name, t_symbol *dir); + +EXTERN void canvas_setargs(int argc, t_atom *argv); +EXTERN void canvas_getargs(int *argcp, t_atom **argvp); +EXTERN t_symbol *canvas_getcurrentdir(void); +EXTERN t_glist *canvas_getcurrent(void); +EXTERN void canvas_makefilename(t_glist *c, char *file, + char *result,int resultsize); +EXTERN t_symbol *canvas_getdir(t_glist *x); +EXTERN char sys_font[]; /* default typeface set in s_main.c */ +EXTERN char sys_fontweight[]; /* default font weight set in s_main.c */ +EXTERN int sys_fontwidth(int fontsize); +EXTERN int sys_fontheight(int fontsize); +EXTERN void canvas_dataproperties(t_glist *x, t_scalar *sc, t_binbuf *b); +EXTERN int canvas_open(t_canvas *x, const char *name, const char *ext, + char *dirresult, char **nameresult, unsigned int size, int bin); + +/* ---------------- widget behaviors ---------------------- */ + +EXTERN_STRUCT _widgetbehavior; +#define t_widgetbehavior struct _widgetbehavior + +EXTERN_STRUCT _parentwidgetbehavior; +#define t_parentwidgetbehavior struct _parentwidgetbehavior +EXTERN t_parentwidgetbehavior *pd_getparentwidget(t_pd *x); + +/* -------------------- classes -------------- */ + +#define CLASS_DEFAULT 0 /* flags for new classes below */ +#define CLASS_PD 1 +#define CLASS_GOBJ 2 +#define CLASS_PATCHABLE 3 +#define CLASS_NOINLET 8 + +#define CLASS_TYPEMASK 3 + + +EXTERN t_class *class_new(t_symbol *name, t_newmethod newmethod, + t_method freemethod, size_t size, int flags, t_atomtype arg1, ...); +EXTERN void class_addcreator(t_newmethod newmethod, t_symbol *s, + t_atomtype type1, ...); +EXTERN void class_addmethod(t_class *c, t_method fn, t_symbol *sel, + t_atomtype arg1, ...); +EXTERN void class_addbang(t_class *c, t_method fn); +EXTERN void class_addpointer(t_class *c, t_method fn); +EXTERN void class_doaddfloat(t_class *c, t_method fn); +EXTERN void class_addsymbol(t_class *c, t_method fn); +EXTERN void class_addlist(t_class *c, t_method fn); +EXTERN void class_addanything(t_class *c, t_method fn); +EXTERN void class_sethelpsymbol(t_class *c, t_symbol *s); +EXTERN void class_setwidget(t_class *c, t_widgetbehavior *w); +EXTERN void class_setparentwidget(t_class *c, t_parentwidgetbehavior *w); +EXTERN t_parentwidgetbehavior *class_parentwidget(t_class *c); +EXTERN char *class_getname(t_class *c); +EXTERN char *class_gethelpname(t_class *c); +EXTERN char *class_gethelpdir(t_class *c); +EXTERN void class_setdrawcommand(t_class *c); +EXTERN int class_isdrawcommand(t_class *c); +EXTERN void class_domainsignalin(t_class *c, int onset); +EXTERN void class_set_extern_dir(t_symbol *s); +#define CLASS_MAINSIGNALIN(c, type, field) \ + class_domainsignalin(c, (char *)(&((type *)0)->field) - (char *)0) + + /* prototype for functions to save Pd's to a binbuf */ +typedef void (*t_savefn)(t_gobj *x, t_binbuf *b); +EXTERN void class_setsavefn(t_class *c, t_savefn f); +EXTERN t_savefn class_getsavefn(t_class *c); +EXTERN void obj_saveformat(t_object *x, t_binbuf *bb); /* add format to bb */ + + /* prototype for functions to open properties dialogs */ +typedef void (*t_propertiesfn)(t_gobj *x, struct _glist *glist); +EXTERN void class_setpropertiesfn(t_class *c, t_propertiesfn f); +EXTERN t_propertiesfn class_getpropertiesfn(t_class *c); + +#ifndef PD_CLASS_DEF +#define class_addbang(x, y) class_addbang((x), (t_method)(y)) +#define class_addpointer(x, y) class_addpointer((x), (t_method)(y)) +#define class_addfloat(x, y) class_doaddfloat((x), (t_method)(y)) +#define class_addsymbol(x, y) class_addsymbol((x), (t_method)(y)) +#define class_addlist(x, y) class_addlist((x), (t_method)(y)) +#define class_addanything(x, y) class_addanything((x), (t_method)(y)) +#endif + +/* ------------ printing --------------------------------- */ +EXTERN void post(const char *fmt, ...); +EXTERN void startpost(const char *fmt, ...); +EXTERN void poststring(const char *s); +EXTERN void postfloat(t_floatarg f); +EXTERN void postatom(int argc, t_atom *argv); +EXTERN void endpost(void); +EXTERN void error(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2); +EXTERN void verbose(int level, const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(2, 3); +EXTERN void bug(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2); +EXTERN void pd_error(void *object, const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(2, 3); +EXTERN void logpost(const void *object, const int level, const char *fmt, ...) + ATTRIBUTE_FORMAT_PRINTF(3, 4); +EXTERN void sys_logerror(const char *object, const char *s); +EXTERN void sys_unixerror(const char *object); +EXTERN void sys_ouch(void); + + +/* ------------ system interface routines ------------------- */ +EXTERN int sys_isreadablefile(const char *name); +EXTERN int sys_isabsolutepath(const char *dir); +EXTERN void sys_bashfilename(const char *from, char *to); +EXTERN void sys_unbashfilename(const char *from, char *to); +EXTERN int open_via_path(const char *dir, const char *name, const char *ext, + char *dirresult, char **nameresult, unsigned int size, int bin); +EXTERN int sched_geteventno(void); +EXTERN double sys_getrealtime(void); +EXTERN int (*sys_idlehook)(void); /* hook to add idle time computation */ + +/* Win32's open()/fopen() do not handle UTF-8 filenames so we need + * these internal versions that handle UTF-8 filenames the same across + * all platforms. They are recommended for use in external + * objectclasses as well so they work with Unicode filenames on Windows */ +EXTERN int sys_open(const char *path, int oflag, ...); +EXTERN int sys_close(int fd); +EXTERN FILE *sys_fopen(const char *filename, const char *mode); +EXTERN int sys_fclose(FILE *stream); + +/* ------------ threading ------------------- */ +EXTERN void sys_lock(void); +EXTERN void sys_unlock(void); +EXTERN int sys_trylock(void); + + +/* --------------- signals ----------------------------------- */ + +typedef PD_FLOATTYPE t_sample; +typedef union _sampleint_union { + t_sample f; + PD_FLOATUINTTYPE i; +} t_sampleint_union; +#define MAXLOGSIG 32 +#define MAXSIGSIZE (1 << MAXLOGSIG) + +typedef struct _signal +{ + int s_n; /* number of points in the array */ + t_sample *s_vec; /* the array */ + t_float s_sr; /* sample rate */ + int s_refcount; /* number of times used */ + int s_isborrowed; /* whether we're going to borrow our array */ + struct _signal *s_borrowedfrom; /* signal to borrow it from */ + struct _signal *s_nextfree; /* next in freelist */ + struct _signal *s_nextused; /* next in used list */ + int s_vecsize; /* allocated size of array in points */ +} t_signal; + +typedef t_int *(*t_perfroutine)(t_int *args); + +EXTERN t_int *plus_perform(t_int *args); +EXTERN t_int *zero_perform(t_int *args); +EXTERN t_int *copy_perform(t_int *args); + +EXTERN void dsp_add_plus(t_sample *in1, t_sample *in2, t_sample *out, int n); +EXTERN void dsp_add_copy(t_sample *in, t_sample *out, int n); +EXTERN void dsp_add_scalarcopy(t_float *in, t_sample *out, int n); +EXTERN void dsp_add_zero(t_sample *out, int n); + +EXTERN int sys_getblksize(void); +EXTERN t_float sys_getsr(void); +EXTERN int sys_get_inchannels(void); +EXTERN int sys_get_outchannels(void); + +EXTERN void dsp_add(t_perfroutine f, int n, ...); +EXTERN void dsp_addv(t_perfroutine f, int n, t_int *vec); +EXTERN void pd_fft(t_float *buf, int npoints, int inverse); +EXTERN int ilog2(int n); + +EXTERN void mayer_fht(t_sample *fz, int n); +EXTERN void mayer_fft(int n, t_sample *real, t_sample *imag); +EXTERN void mayer_ifft(int n, t_sample *real, t_sample *imag); +EXTERN void mayer_realfft(int n, t_sample *real); +EXTERN void mayer_realifft(int n, t_sample *real); + +EXTERN float *cos_table; +#define LOGCOSTABSIZE 9 +#define COSTABSIZE (1<<LOGCOSTABSIZE) + +EXTERN int canvas_suspend_dsp(void); +EXTERN void canvas_resume_dsp(int oldstate); +EXTERN void canvas_update_dsp(void); +EXTERN int canvas_dspstate; + +/* up/downsampling */ +typedef struct _resample +{ + int method; /* up/downsampling method ID */ + + int downsample; /* downsampling factor */ + int upsample; /* upsampling factor */ + + t_sample *s_vec; /* here we hold the resampled data */ + int s_n; + + t_sample *coeffs; /* coefficients for filtering... */ + int coefsize; + + t_sample *buffer; /* buffer for filtering */ + int bufsize; +} t_resample; + +EXTERN void resample_init(t_resample *x); +EXTERN void resample_free(t_resample *x); + +EXTERN void resample_dsp(t_resample *x, t_sample *in, int insize, t_sample *out, int outsize, int method); +EXTERN void resamplefrom_dsp(t_resample *x, t_sample *in, int insize, int outsize, int method); +EXTERN void resampleto_dsp(t_resample *x, t_sample *out, int insize, int outsize, int method); + +/* ----------------------- utility functions for signals -------------- */ +EXTERN t_float mtof(t_float); +EXTERN t_float ftom(t_float); +EXTERN t_float rmstodb(t_float); +EXTERN t_float powtodb(t_float); +EXTERN t_float dbtorms(t_float); +EXTERN t_float dbtopow(t_float); + +EXTERN t_float q8_sqrt(t_float); +EXTERN t_float q8_rsqrt(t_float); +#ifndef N32 +EXTERN t_float qsqrt(t_float); /* old names kept for extern compatibility */ +EXTERN t_float qrsqrt(t_float); +#endif +/* --------------------- data --------------------------------- */ + + /* graphical arrays */ +EXTERN_STRUCT _garray; +#define t_garray struct _garray + +EXTERN t_class *garray_class; +EXTERN int garray_getfloatarray(t_garray *x, int *size, t_float **vec); +EXTERN int garray_getfloatwords(t_garray *x, int *size, t_word **vec); +EXTERN void garray_redraw(t_garray *x); +EXTERN int garray_npoints(t_garray *x); +EXTERN char *garray_vec(t_garray *x); +EXTERN void garray_resize(t_garray *x, t_floatarg f); /* avoid; use this: */ +EXTERN void garray_resize_long(t_garray *x, long n); /* better version */ +EXTERN void garray_usedindsp(t_garray *x); +EXTERN void garray_setsaveit(t_garray *x, int saveit); +EXTERN t_glist *garray_getglist(t_garray *x); +EXTERN t_array *garray_getarray(t_garray *x); +EXTERN t_class *scalar_class; + +EXTERN t_float *value_get(t_symbol *s); +EXTERN void value_release(t_symbol *s); +EXTERN int value_getfloat(t_symbol *s, t_float *f); +EXTERN int value_setfloat(t_symbol *s, t_float f); + +/* ------- GUI interface - functions to send strings to TK --------- */ +typedef void (*t_guicallbackfn)(t_gobj *client, t_glist *glist); + +EXTERN void sys_vgui(char *fmt, ...); +EXTERN void sys_gui(char *s); +EXTERN void sys_pretendguibytes(int n); +EXTERN void sys_queuegui(void *client, t_glist *glist, t_guicallbackfn f); +EXTERN void sys_unqueuegui(void *client); + /* dialog window creation and destruction */ +EXTERN void gfxstub_new(t_pd *owner, void *key, const char *cmd); +EXTERN void gfxstub_deleteforkey(void *key); + +extern t_class *glob_pdobject; /* object to send "pd" messages */ + +/*------------- Max 0.26 compatibility --------------------*/ + +/* the following reflects the new way classes are laid out, with the class + pointing to the messlist and not vice versa. Externs shouldn't feel it. */ +typedef t_class *t_externclass; + +EXTERN void c_extern(t_externclass *cls, t_newmethod newroutine, + t_method freeroutine, t_symbol *name, size_t size, int tiny, \ + t_atomtype arg1, ...); +EXTERN void c_addmess(t_method fn, t_symbol *sel, t_atomtype arg1, ...); + +#define t_getbytes getbytes +#define t_freebytes freebytes +#define t_resizebytes resizebytes +#define typedmess pd_typedmess +#define vmess pd_vmess + +/* A definition to help gui objects straddle 0.34-0.35 changes. If this is +defined, there is a "te_xpix" field in objects, not a "te_xpos" as before: */ + +#define PD_USE_TE_XPIX + +#ifndef _MSC_VER /* Microoft compiler can't handle "inline" function/macros */ +#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) +/* a test for NANs and denormals. Should only be necessary on i386. */ +# if PD_FLOATSIZE == 32 +static inline int PD_BADFLOAT(t_sample f) { + t_sampleint_union u; + u.f=f; + return ((u.i & 0x7f800000)==0) || ((u.i&0x7f800000)==0x7f800000); +} +/* more stringent test: anything not between 1e-19 and 1e19 in absolute val */ +static inline int PD_BIGORSMALL(t_sample f) { + t_sampleint_union u; + u.f=f; + return ((u.i & 0x60000000)==0) || ((u.i & 0x60000000)==0x60000000); +} +# else +# warning 64bit mode: BIGORSMALL not implemented yet +# define PD_BADFLOAT(f) 0 +# define PD_BIGORSMALL(f) 0 +# endif +#else +# define PD_BADFLOAT(f) 0 +# define PD_BIGORSMALL(f) 0 +#endif +#else /* _MSC_VER */ +#define PD_BADFLOAT(f) ((((*(unsigned int*)&(f))&0x7f800000)==0) || \ + (((*(unsigned int*)&(f))&0x7f800000)==0x7f800000)) +/* more stringent test: anything not between 1e-19 and 1e19 in absolute val */ +#define PD_BIGORSMALL(f) ((((*(unsigned int*)&(f))&0x60000000)==0) || \ + (((*(unsigned int*)&(f))&0x60000000)==0x60000000)) +#endif /* _MSC_VER */ + /* get version number at run time */ +EXTERN void sys_getversion(int *major, int *minor, int *bugfix); + +EXTERN_STRUCT _pdinstance; +#define t_pdinstance struct _pdinstance /* m_imp.h */ + +/* m_pd.c */ + +EXTERN t_pdinstance *pdinstance_new( void); +EXTERN void pd_setinstance(t_pdinstance *x); +EXTERN void pdinstance_free(t_pdinstance *x); +EXTERN t_canvas *pd_getcanvaslist(void); +EXTERN int pd_getdspstate(void); + +#if defined(_LANGUAGE_C_PLUS_PLUS) || defined(__cplusplus) +} +#endif + +#define __m_pd_h_ +#endif /* __m_pd_h_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/basic_libpd/main.cpp Sat Apr 09 08:19:09 2016 +0100 @@ -0,0 +1,91 @@ +/* + * default_main.cpp + * + * Created on: Oct 24, 2014 + * Author: parallels + */ +#include <unistd.h> +#include <iostream> +#include <cstdlib> +#include <libgen.h> +#include <signal.h> +#include <getopt.h> +#include "../include/BeagleRT.h" + +using namespace std; + +// Handle Ctrl-C by requesting that the audio rendering stop +void interrupt_handler(int var) +{ + gShouldStop = true; +} + +// Print usage information +void usage(const char * processName) +{ + cerr << "Usage: " << processName << " [options]" << endl; + + BeagleRT_usage(); + + cerr << " --help [-h]: Print this menu\n"; +} + +int main(int argc, char *argv[]) +{ + BeagleRTInitSettings settings; // Standard audio settings + + struct option customOptions[] = + { + {"help", 0, NULL, 'h'}, + {NULL, 0, NULL, 0} + }; + + // Set default settings + BeagleRT_defaultSettings(&settings); + + // Parse command-line arguments + while (1) { + int c; + if ((c = BeagleRT_getopt_long(argc, argv, "h", customOptions, &settings)) < 0) + break; + switch (c) { + case 'h': + usage(basename(argv[0])); + exit(0); + case '?': + default: + usage(basename(argv[0])); + exit(1); + } + } + + // Initialise the PRU audio device + if(BeagleRT_initAudio(&settings, 0) != 0) { + cout << "Error: unable to initialise audio" << endl; + return -1; + } + + // Start the audio device running + if(BeagleRT_startAudio()) { + cout << "Error: unable to start real-time audio" << endl; + return -1; + } + + // Set up interrupt handler to catch Control-C and SIGTERM + signal(SIGINT, interrupt_handler); + signal(SIGTERM, interrupt_handler); + + // Run until told to stop + while(!gShouldStop) { + usleep(100000); + } + + // Stop the audio device + BeagleRT_stopAudio(); + + // Clean up any resources allocated for audio + BeagleRT_cleanupAudio(); + + // All done! + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/basic_libpd/render.cpp Sat Apr 09 08:19:09 2016 +0100 @@ -0,0 +1,155 @@ +/* + * render.cpp + * + * Created on: Oct 24, 2014 + * Author: parallels + */ + +#include <BeagleRT.h> +#include <cmath> +#include <Utilities.h> +#include <I2c_Codec.h> +#include <PRU.h> +#include <stdio.h> +#include "z_libpd.h" +#include <UdpServer.h> + +// setup() is called once before the audio rendering starts. +// Use it to perform any initialisation and allocation which is dependent +// on the period size or sample rate. +// +// userData holds an opaque pointer to a data structure that was passed +// in from the call to initAudio(). +// +// Return true on success; returning false halts the program. +#define DEFDACBLKSIZE 8u //make sure this matches the one used to compile libpd + +const int gChannelsInUse = 6; +int gBufLength; + +float* gInBuf; +float* gOutBuf; + +void pdnoteon(int ch, int pitch, int vel) { + printf("noteon: %d %d %d\n", ch, pitch, vel); +} + +void BeagleRT_printHook(const char *recv){ + rt_printf("%s", recv); +} + +UdpServer udpServer; + +void udpRead(){ + char dest[100] = {0}; + while(!gShouldStop){ + libpd_sys_microsleep(0); + usleep(1000); + } +} + +AuxiliaryTask udpReadTask; +bool setup(BeagleRTContext *context, void *userData) +{ + udpServer.bindToPort(1234); + + // check that we are not running with a blocksize smaller than DEFDACBLKSIZE + // it would still work, but the load would be executed unevenly between calls to render + if(context->audioFrames < DEFDACBLKSIZE){ + fprintf(stderr, "Error: minimum block size must be %d\n", DEFDACBLKSIZE); + return false; + } + + // check that the sampling rate of the analogs is the same as audio if running with + // more than 2 channels (that is with analog). If we fix the TODO in render, then + // this test is not needed. + if(context->analogFrames != context->audioFrames){ + fprintf(stderr, "Error: analog and audio sampling rates must be the same\n"); + return false; + } + //following lines borrowed from libpd/samples/c/pdtest/pdtest.c + // init pd + libpd_set_printhook(BeagleRT_printHook); // set this before calling libpd_init + libpd_set_noteonhook(pdnoteon); + libpd_init(); + //TODO: analyse the ASCII of the patch file and find the in/outs to use + libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate); + + libpd_start_message(1); // one entry in list + libpd_add_float(1.0f); + libpd_finish_message("pd", "dsp"); + + gBufLength = max(DEFDACBLKSIZE, context->audioFrames); + unsigned int bufferSize = sizeof(float)*gChannelsInUse*gBufLength; + gInBuf = (float*)malloc(bufferSize); + gOutBuf = (float*)malloc(bufferSize); + // no need to memset to zero + + + char file[] = "_main.pd"; + char folder[] = "./"; + // open patch [; pd open file folder( + libpd_openfile(file, folder); + + udpReadTask = BeagleRT_createAuxiliaryTask(udpRead, 60, "udpReadTask"); + BeagleRT_scheduleAuxiliaryTask(udpReadTask); + return true; +} + +// render() is called regularly at the highest priority by the audio engine. +// Input and output are given from the audio hardware and the other +// ADCs and DACs (if available). If only audio is available, numMatrixFrames +// will be 0. +BeagleRTContext *c; +void render(BeagleRTContext *context, void *userData) +{ + static int inW = 0; + static int outR = 0; +/* + * NOTE: if you are only using audio (or only analogs) and you are using interleaved buffers + * and the blocksize of Bela is the same as DEFDACBLKSIZE, then you probably + * do not need the for loops before and after libpd_process_float, so you can save quite some + * memory operations. + */ + + for(unsigned int n = 0; n < context->audioFrames; n++){ //pd buffers are interleaved + for(unsigned int k = 0; k < context->audioChannels; k++){ + gInBuf[inW++] = audioReadFrame(context, n, k); + } + for(unsigned int k = 0; k < gChannelsInUse - context->audioChannels; k ++){ // add analogs + gInBuf[inW++] = analogReadFrame(context, n, k); + // TODO: Apply here sampling rate conversion from analogs to audio + } + if(inW == gBufLength * gChannelsInUse){ + inW = 0; + } + } + + if(inW == 0){ //if the buffer is full, process it + int numberOfPdBlocksToProcess = gBufLength/DEFDACBLKSIZE; + libpd_process_float(numberOfPdBlocksToProcess, gInBuf, gOutBuf); + outR = 0; // reset the read pointer. NOTE: hopefully this is not needed EXCEPT the first time + } + + for(unsigned int n = 0; n < context->audioFrames; n++){ //pd buffers are interleaved + for(unsigned int k = 0; k < context->audioChannels; k++){ + audioWriteFrame(context, n, k, gOutBuf[outR++]); + } + //add analogs here, limit them to channelsInUse + for(unsigned int k = 0; k < gChannelsInUse - context->audioChannels; k ++){ // add analogs + analogWriteFrame(context, n, k, gOutBuf[outR++]); + // TODO: Apply here sampling rate conversion from analogs to audio + } + if(outR == gBufLength * gChannelsInUse){ + outR = 0; + } + } +} +// cleanup() is called once at the end, after the audio has stopped. +// Release any resources that were allocated in setup(). + +void cleanup(BeagleRTContext *context, void *userData) +{ + free(gInBuf); + free(gOutBuf); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/basic_libpd/s_stuff.h Sat Apr 09 08:19:09 2016 +0100 @@ -0,0 +1,411 @@ +/* Copyright (c) 1997-1999 Miller Puckette. +* For information on usage and redistribution, and for a DISCLAIMER OF ALL +* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ + +/* Audio and MIDI I/O, and other scheduling and system stuff. */ + +/* NOTE: this file describes Pd implementation details which may change +in future releases. The public (stable) API is in m_pd.h. */ + +/* in s_path.c */ + +typedef struct _namelist /* element in a linked list of stored strings */ +{ + struct _namelist *nl_next; /* next in list */ + char *nl_string; /* the string */ +} t_namelist; + +t_namelist *namelist_append(t_namelist *listwas, const char *s, int allowdup); +t_namelist *namelist_append_files(t_namelist *listwas, const char *s); +void namelist_free(t_namelist *listwas); +char *namelist_get(t_namelist *namelist, int n); +void sys_setextrapath(const char *p); +extern int sys_usestdpath; +extern t_namelist *sys_externlist; +extern t_namelist *sys_searchpath; +extern t_namelist *sys_helppath; +int sys_open_absolute(const char *name, const char* ext, + char *dirresult, char **nameresult, unsigned int size, int bin, int *fdp); +int sys_trytoopenone(const char *dir, const char *name, const char* ext, + char *dirresult, char **nameresult, unsigned int size, int bin); +t_symbol *sys_decodedialog(t_symbol *s); + +/* s_file.c */ + +void sys_loadpreferences( void); +void sys_savepreferences( void); +extern int sys_defeatrt; +extern t_symbol *sys_flags; + +/* s_main.c */ +extern int sys_debuglevel; +extern int sys_verbose; +extern int sys_noloadbang; +extern int sys_nogui; +extern char *sys_guicmd; + +EXTERN int sys_nearestfontsize(int fontsize); +EXTERN int sys_hostfontsize(int fontsize); + +extern int sys_defaultfont; +EXTERN t_symbol *sys_libdir; /* library directory for auxilliary files */ +extern t_symbol *sys_guidir; /* directory holding pd_gui, u_pdsend, etc */ + +/* s_loader.c */ + +typedef int (*loader_t)(t_canvas *canvas, char *classname); /* callback type */ +EXTERN int sys_load_lib(t_canvas *canvas, char *filename); +EXTERN void sys_register_loader(loader_t loader); + +/* s_audio.c */ + +#define SENDDACS_NO 0 /* return values for sys_send_dacs() */ +#define SENDDACS_YES 1 +#define SENDDACS_SLEPT 2 + +#define DEFDACBLKSIZE 8 +extern int sys_schedblocksize; /* audio block size for scheduler */ +extern int sys_hipriority; /* real-time flag, true if priority boosted */ +EXTERN t_sample *sys_soundout; +EXTERN t_sample *sys_soundin; +extern int sys_inchannels; +extern int sys_outchannels; +extern int sys_advance_samples; /* scheduler advance in samples */ +extern int sys_blocksize; /* audio I/O block size in sample frames */ +extern t_float sys_dacsr; +extern int sys_schedadvance; +extern int sys_sleepgrain; +EXTERN void sys_set_audio_settings(int naudioindev, int *audioindev, + int nchindev, int *chindev, + int naudiooutdev, int *audiooutdev, int nchoutdev, int *choutdev, + int srate, int advance, int callback, int blocksize); +/* the same as above, but reopens the audio subsystem if needed */ +EXTERN void sys_set_audio_settings_reopen(int naudioindev, int *audioindev, + int nchindev, int *chindev, + int naudiooutdev, int *audiooutdev, int nchoutdev, int *choutdev, + int srate, int advance, int callback, int blocksize); +EXTERN void sys_reopen_audio( void); +EXTERN void sys_close_audio(void); + /* return true if the interface prefers always being open (ala jack) : */ +EXTERN int audio_shouldkeepopen( void); +EXTERN int audio_isopen( void); /* true if audio interface is open */ +EXTERN int sys_audiodevnametonumber(int output, const char *name); +EXTERN void sys_audiodevnumbertoname(int output, int devno, char *name, + int namesize); + +int sys_send_dacs(void); +void sys_reportidle(void); +void sys_set_priority(int higher); +void sys_audiobuf(int nbufs); +void sys_getmeters(t_sample *inmax, t_sample *outmax); +void sys_listdevs(void); +void sys_setblocksize(int n); + +EXTERN void sys_get_audio_devs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, int *canmulti, int *cancallback, + int maxndev, int devdescsize); +EXTERN void sys_get_audio_apis(char *buf); + +/* s_midi.c */ +#define MAXMIDIINDEV 16 /* max. number of input ports */ +#define MAXMIDIOUTDEV 16 /* max. number of output ports */ +extern int sys_midiapi; +extern int sys_nmidiin; +extern int sys_nmidiout; +extern int sys_midiindevlist[]; +extern int sys_midioutdevlist[]; + +EXTERN void sys_open_midi(int nmidiin, int *midiinvec, + int nmidiout, int *midioutvec, int enable); + +EXTERN void sys_get_midi_apis(char *buf); +EXTERN void sys_get_midi_devs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, + int maxndev, int devdescsize); +EXTERN void sys_get_midi_params(int *pnmidiindev, int *pmidiindev, + int *pnmidioutdev, int *pmidioutdev); +EXTERN int sys_mididevnametonumber(int output, const char *name); +EXTERN void sys_mididevnumbertoname(int output, int devno, char *name, + int namesize); + +EXTERN void sys_reopen_midi( void); +EXTERN void sys_close_midi( void); +EXTERN void sys_putmidimess(int portno, int a, int b, int c); +EXTERN void sys_putmidibyte(int portno, int a); +EXTERN void sys_poll_midi(void); +EXTERN void sys_setmiditimediff(double inbuftime, double outbuftime); +EXTERN void sys_midibytein(int portno, int byte); + + /* implemented in the system dependent MIDI code (s_midi_pm.c, etc. ) */ +void midi_getdevs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, int maxndev, int devdescsize); +void sys_do_open_midi(int nmidiindev, int *midiindev, + int nmidioutdev, int *midioutdev); + +#ifdef USEAPI_ALSA +EXTERN void sys_alsa_putmidimess(int portno, int a, int b, int c); +EXTERN void sys_alsa_putmidibyte(int portno, int a); +EXTERN void sys_alsa_poll_midi(void); +EXTERN void sys_alsa_setmiditimediff(double inbuftime, double outbuftime); +EXTERN void sys_alsa_midibytein(int portno, int byte); +EXTERN void sys_alsa_close_midi( void); + + + /* implemented in the system dependent MIDI code (s_midi_pm.c, etc. ) */ +void midi_alsa_getdevs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, int maxndev, int devdescsize); +void sys_alsa_do_open_midi(int nmidiindev, int *midiindev, + int nmidioutdev, int *midioutdev); +#endif + +/* m_sched.c */ +EXTERN void sys_log_error(int type); +#define ERR_NOTHING 0 +#define ERR_ADCSLEPT 1 +#define ERR_DACSLEPT 2 +#define ERR_RESYNC 3 +#define ERR_DATALATE 4 + +#define SCHED_AUDIO_NONE 0 +#define SCHED_AUDIO_POLL 1 +#define SCHED_AUDIO_CALLBACK 2 +void sched_set_using_audio(int flag); + +/* s_inter.c */ + +EXTERN void sys_microsleep(int microsec); +EXTERN void sys_init_fdpoll(void); + +EXTERN void sys_bail(int exitcode); +EXTERN int sys_pollgui(void); + +EXTERN_STRUCT _socketreceiver; +#define t_socketreceiver struct _socketreceiver + +typedef void (*t_socketnotifier)(void *x, int n); +typedef void (*t_socketreceivefn)(void *x, t_binbuf *b); + +EXTERN t_socketreceiver *socketreceiver_new(void *owner, + t_socketnotifier notifier, t_socketreceivefn socketreceivefn, int udp); +EXTERN void socketreceiver_read(t_socketreceiver *x, int fd); +EXTERN void sys_sockerror(char *s); +EXTERN void sys_closesocket(int fd); + +typedef void (*t_fdpollfn)(void *ptr, int fd); +EXTERN void sys_addpollfn(int fd, t_fdpollfn fn, void *ptr); +EXTERN void sys_rmpollfn(int fd); +#if defined(USEAPI_OSS) || defined(USEAPI_ALSA) +void sys_setalarm(int microsec); +#endif + +#define API_NONE 0 +#define API_ALSA 1 +#define API_OSS 2 +#define API_MMIO 3 +#define API_PORTAUDIO 4 +#define API_JACK 5 +#define API_SGI 6 /* gone */ +#define API_AUDIOUNIT 7 +#define API_ESD 8 /* no idea what this was, probably gone now */ +#define API_DUMMY 9 + + /* figure out which API should be the default. The one we judge most + likely to offer a working device takes precedence so that if you + start up Pd for the first time there's a reasonable chance you'll have + sound. (You'd think portaudio would be best but it seems to default + to jack on linux, and and on Windows we only use it for ASIO). + If nobody shows up, define DUMMY and make it the default.*/ +#if defined(USEAPI_MMIO) +# define API_DEFAULT API_MMIO +# define API_DEFSTRING "MMIO" +#elif defined(USEAPI_ALSA) +# define API_DEFAULT API_ALSA +# define API_DEFSTRING "ALSA" +#elif defined(USEAPI_OSS) +# define API_DEFAULT API_OSS +# define API_DEFSTRING "OSS" +#elif defined(USEAPI_AUDIOUNIT) +# define API_DEFAULT API_AUDIOUNIT +# define API_DEFSTRING "AudioUnit" +#elif defined(USEAPI_ESD) +# define API_DEFAULT API_ESD +# define API_DEFSTRING "ESD (?)" +#elif defined(USEAPI_PORTAUDIO) +# define API_DEFAULT API_PORTAUDIO +# define API_DEFSTRING "portaudio" +#elif defined(USEAPI_JACK) +# define API_DEFAULT API_JACK +# define API_DEFSTRING "Jack audio connection kit" +#else +# ifndef USEAPI_DUMMY /* we need at least one so bring in the dummy */ +# define USEAPI_DUMMY +# endif /* USEAPI_DUMMY */ +# define API_DEFAULT API_DUMMY +# define API_DEFSTRING "dummy audio" +#endif + +#define DEFAULTAUDIODEV 0 + +#define MAXAUDIOINDEV 4 +#define MAXAUDIOOUTDEV 4 + +#define DEFMIDIDEV 0 + +#define DEFAULTSRATE 44100 +#ifdef _WIN32 +#define DEFAULTADVANCE 80 +#else +#ifdef __APPLE__ +#define DEFAULTADVANCE 5 /* this is in addition to their own delay */ +#else +#define DEFAULTADVANCE 25 +#endif +#endif + +typedef void (*t_audiocallback)(void); + +int pa_open_audio(int inchans, int outchans, int rate, t_sample *soundin, + t_sample *soundout, int framesperbuf, int nbuffers, + int indeviceno, int outdeviceno, t_audiocallback callback); +void pa_close_audio(void); +int pa_send_dacs(void); +void sys_reportidle(void); +void pa_listdevs(void); +void pa_getdevs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, int *canmulti, + int maxndev, int devdescsize); + +int oss_open_audio(int naudioindev, int *audioindev, int nchindev, + int *chindev, int naudiooutdev, int *audiooutdev, int nchoutdev, + int *choutdev, int rate, int blocksize); +void oss_close_audio(void); +int oss_send_dacs(void); +void oss_reportidle(void); +void oss_getdevs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, int *canmulti, + int maxndev, int devdescsize); + +int alsa_open_audio(int naudioindev, int *audioindev, int nchindev, + int *chindev, int naudiooutdev, int *audiooutdev, int nchoutdev, + int *choutdev, int rate, int blocksize); +void alsa_close_audio(void); +int alsa_send_dacs(void); +void alsa_reportidle(void); +void alsa_getdevs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, int *canmulti, + int maxndev, int devdescsize); + +int jack_open_audio(int wantinchans, int wantoutchans, int srate, + t_audiocallback callback); +void jack_close_audio(void); +int jack_send_dacs(void); +void jack_reportidle(void); +void jack_getdevs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, int *canmulti, + int maxndev, int devdescsize); +void jack_listdevs(void); + +int mmio_open_audio(int naudioindev, int *audioindev, + int nchindev, int *chindev, int naudiooutdev, int *audiooutdev, + int nchoutdev, int *choutdev, int rate, int blocksize); +void mmio_close_audio( void); +void mmio_reportidle(void); +int mmio_send_dacs(void); +void mmio_getdevs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, int *canmulti, + int maxndev, int devdescsize); + +int audiounit_open_audio(int naudioindev, int *audioindev, int nchindev, + int *chindev, int naudiooutdev, int *audiooutdev, int nchoutdev, + int *choutdev, int rate); +void audiounit_close_audio(void); +int audiounit_send_dacs(void); +void audiounit_listdevs(void); +void audiounit_getdevs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, int *canmulti, + int maxndev, int devdescsize); + +int esd_open_audio(int naudioindev, int *audioindev, int nchindev, + int *chindev, int naudiooutdev, int *audiooutdev, int nchoutdev, + int *choutdev, int rate); +void esd_close_audio(void); +int esd_send_dacs(void); +void esd_listdevs(void); +void esd_getdevs(char *indevlist, int *nindevs, + char *outdevlist, int *noutdevs, int *canmulti, + int maxndev, int devdescsize); + +int dummy_open_audio(int nin, int nout, int sr); +int dummy_close_audio( void); +int dummy_send_dacs( void); +void dummy_getdevs(char *indevlist, int *nindevs, char *outdevlist, + int *noutdevs, int *canmulti, int maxndev, int devdescsize); +void dummy_listdevs( void); + +void sys_listmididevs(void); +EXTERN void sys_set_midi_api(int whichapi); +EXTERN void sys_set_audio_api(int whichapi); +EXTERN int sys_audioapi; +EXTERN void sys_set_audio_state(int onoff); + +/* API dependent audio flags and settings */ +void oss_set32bit( void); +void linux_alsa_devname(char *devname); + +EXTERN void sys_get_audio_params( + int *pnaudioindev, int *paudioindev, int *chindev, + int *pnaudiooutdev, int *paudiooutdev, int *choutdev, + int *prate, int *padvance, int *callback, int *blocksize); +void sys_save_audio_params( + int naudioindev, int *audioindev, int *chindev, + int naudiooutdev, int *audiooutdev, int *choutdev, + int rate, int advance, int callback, int blocksize); + +/* s_file.c */ + +typedef void (*t_printhook)(const char *s); +extern t_printhook sys_printhook; /* set this to override printing */ +extern int sys_printtostderr; + +/* jsarlo { */ + +EXTERN double sys_time; +EXTERN double sys_time_per_dsp_tick; +EXTERN int sys_externalschedlib; + +EXTERN t_sample* get_sys_soundout(void ) ; +EXTERN t_sample* get_sys_soundin(void ) ; +EXTERN int* get_sys_main_advance(void ) ; +EXTERN double* get_sys_time_per_dsp_tick(void ) ; +EXTERN int* get_sys_schedblocksize(void ) ; +EXTERN double* get_sys_time(void ) ; +EXTERN t_float* get_sys_dacsr(void ) ; +EXTERN int* get_sys_sleepgrain(void ) ; +EXTERN int* get_sys_schedadvance(void ) ; + +EXTERN void sys_clearhist(void ); +EXTERN void sys_initmidiqueue(void ); +EXTERN int sys_addhist(int phase); +EXTERN void sys_setmiditimediff(double inbuftime, double outbuftime); +EXTERN void sched_tick( void); +EXTERN void sys_pollmidiqueue(void ); +EXTERN int sys_pollgui(void ); +EXTERN void sys_setchsr(int chin, int chout, int sr); + +EXTERN void inmidi_realtimein(int portno, int cmd); +EXTERN void inmidi_byte(int portno, int byte); +EXTERN void inmidi_sysex(int portno, int byte); +EXTERN void inmidi_noteon(int portno, int channel, int pitch, int velo); +EXTERN void inmidi_controlchange(int portno, + int channel, + int ctlnumber, + int value); +EXTERN void inmidi_programchange(int portno, int channel, int value); +EXTERN void inmidi_pitchbend(int portno, int channel, int value); +EXTERN void inmidi_aftertouch(int portno, int channel, int value); +EXTERN void inmidi_polyaftertouch(int portno, + int channel, + int pitch, + int value); +/* } jsarlo */ +extern t_widgetbehavior text_widgetbehavior;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/basic_libpd/z_libpd.h Sat Apr 09 08:19:09 2016 +0100 @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2010 Peter Brinkmann (peter.brinkmann@gmail.com) + * + * For information on usage and redistribution, and for a DISCLAIMER OF ALL + * WARRANTIES, see the file, "LICENSE.txt," in this distribution. + * + * See https://github.com/libpd/libpd/wiki for documentation + * + */ + +#ifndef __Z_LIBPD_H__ +#define __Z_LIBPD_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "m_pd.h" + +EXTERN int libpd_init(void); +EXTERN void libpd_clear_search_path(void); +EXTERN void libpd_add_to_search_path(const char *sym); + +EXTERN void *libpd_openfile(const char *basename, const char *dirname); +EXTERN void libpd_closefile(void *p); +EXTERN int libpd_getdollarzero(void *p); + +EXTERN int libpd_blocksize(void); +EXTERN int libpd_init_audio(int inChans, int outChans, int sampleRate); +EXTERN int libpd_process_raw(const float *inBuffer, float *outBuffer); +EXTERN int libpd_process_short(const int ticks, + const short *inBuffer, short *outBuffer); +EXTERN int libpd_process_float(int ticks, + const float *inBuffer, float *outBuffer); +EXTERN int libpd_process_double(int ticks, + const double *inBuffer, double *outBuffer); + +EXTERN int libpd_arraysize(const char *name); +// The parameters of the next two functions are inspired by memcpy. +EXTERN int libpd_read_array(float *dest, const char *src, int offset, int n); +EXTERN int libpd_write_array(const char *dest, int offset, float *src, int n); + +EXTERN int libpd_bang(const char *recv); +EXTERN int libpd_float(const char *recv, float x); +EXTERN int libpd_symbol(const char *recv, const char *sym); + +EXTERN void libpd_set_float(t_atom *v, float x); +EXTERN void libpd_set_symbol(t_atom *v, const char *sym); +EXTERN int libpd_list(const char *recv, int argc, t_atom *argv); +EXTERN int libpd_message(const char *recv, const char *msg, int argc, t_atom *argv); + +EXTERN int libpd_start_message(int max_length); +EXTERN void libpd_add_float(float x); +EXTERN void libpd_add_symbol(const char *sym); +EXTERN int libpd_finish_list(const char *recv); +EXTERN int libpd_finish_message(const char *recv, const char *msg); + +EXTERN int libpd_exists(const char *sym); +EXTERN void *libpd_bind(const char *sym); +EXTERN void libpd_unbind(void *p); + +EXTERN int libpd_is_float(t_atom *a); +EXTERN int libpd_is_symbol(t_atom *a); +EXTERN float libpd_get_float(t_atom *a); +EXTERN char *libpd_get_symbol(t_atom *a); +EXTERN t_atom *libpd_next_atom(t_atom *a); + +typedef void (*t_libpd_printhook)(const char *recv); +typedef void (*t_libpd_banghook)(const char *recv); +typedef void (*t_libpd_floathook)(const char *recv, float x); +typedef void (*t_libpd_symbolhook)(const char *recv, const char *sym); +typedef void (*t_libpd_listhook)(const char *recv, int argc, t_atom *argv); +typedef void (*t_libpd_messagehook)(const char *recv, const char *msg, + int argc, t_atom *argv); + +EXTERN void libpd_set_printhook(const t_libpd_printhook hook); +EXTERN void libpd_set_banghook(const t_libpd_banghook hook); +EXTERN void libpd_set_floathook(const t_libpd_floathook hook); +EXTERN void libpd_set_symbolhook(const t_libpd_symbolhook hook); +EXTERN void libpd_set_listhook(const t_libpd_listhook hook); +EXTERN void libpd_set_messagehook(const t_libpd_messagehook hook); + +EXTERN int libpd_noteon(int channel, int pitch, int velocity); +EXTERN int libpd_controlchange(int channel, int controller, int value); +EXTERN int libpd_programchange(int channel, int value); +EXTERN int libpd_pitchbend(int channel, int value); +EXTERN int libpd_aftertouch(int channel, int value); +EXTERN int libpd_polyaftertouch(int channel, int pitch, int value); +EXTERN int libpd_midibyte(int port, int byte); +EXTERN int libpd_sysex(int port, int byte); +EXTERN int libpd_sysrealtime(int port, int byte); + +typedef void (*t_libpd_noteonhook)(int channel, int pitch, int velocity); +typedef void (*t_libpd_controlchangehook)(int channel, + int controller, int value); +typedef void (*t_libpd_programchangehook)(int channel, int value); +typedef void (*t_libpd_pitchbendhook)(int channel, int value); +typedef void (*t_libpd_aftertouchhook)(int channel, int value); +typedef void (*t_libpd_polyaftertouchhook)(int channel, int pitch, int value); +typedef void (*t_libpd_midibytehook)(int port, int byte); + +EXTERN void libpd_set_noteonhook(const t_libpd_noteonhook hook); +EXTERN void libpd_set_controlchangehook(const t_libpd_controlchangehook hook); +EXTERN void libpd_set_programchangehook(const t_libpd_programchangehook hook); +EXTERN void libpd_set_pitchbendhook(const t_libpd_pitchbendhook hook); +EXTERN void libpd_set_aftertouchhook(const t_libpd_aftertouchhook hook); +EXTERN void libpd_set_polyaftertouchhook(const t_libpd_polyaftertouchhook hook); +EXTERN void libpd_set_midibytehook(const t_libpd_midibytehook hook); +EXTERN void libpd_sys_microsleep(int sleep); +#ifdef __cplusplus +} +#endif + +#endif