Jamoma API  0.6.0.a19
j.midi.out.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternalsGraph
4  *
5  * @brief j.midi.out# - Jamoma Graph external object for Max
6  *
7  * @details
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright Copyright © 2011 by Timothy Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "maxGraph.h"
18 
19 
20 // Data Structure for this object
21 struct MidiOut {
22  t_object obj;
23  TTGraphObjectBasePtr graphObject;
24  TTPtr graphOutlets[16]; // this _must_ be third (for the setup call)
25  t_symbol* attrKey;
26  t_symbol* attrValue;
27 };
28 typedef MidiOut* MidiOutPtr;
29 
30 
31 // Prototypes for methods
32 MidiOutPtr MidiOutNew (t_symbol* msg, long argc, t_atom* argv);
33 void MidiOutFree (MidiOutPtr self);
34 void MidiOutAssist (MidiOutPtr self, void* b, long msg, long arg, char* dst);
35 void MidiOutGetDeviceNames (MidiOutPtr self);
36 t_max_err MidiOutSetDevice (MidiOutPtr self, void* attr, long argc, t_atom* argv);
37 t_max_err MidiOutGetDevice (MidiOutPtr self, void* attr, long* argc, t_atom** argv);
38 
39 
40 // Globals
41 static t_class* sMidiOutClass;
42 
43 
44 /************************************************************************************/
45 // Main() Function
46 
47 int C74_EXPORT main(void)
48 {
49  t_class* c;
50 
51  TTGraphInit();
52  common_symbols_init();
53 
54  c = class_new("j.midi.out-", (method)MidiOutNew, (method)MidiOutFree, sizeof(MidiOut), (method)0L, A_GIMME, 0);
55 
56  class_addmethod(c, (method)MidiOutGetDeviceNames, "getAvailableDeviceNames", 0);
57  class_addmethod(c, (method)MaxGraphReset, "graph.reset", A_CANT, 0);
58  class_addmethod(c, (method)MaxGraphSetup, "graph.setup", A_CANT, 0);
59  class_addmethod(c, (method)MaxGraphConnect, "graph.connect", A_OBJ, A_LONG, 0);
60  class_addmethod(c, (method)MaxGraphDrop, "graph.drop", A_CANT, 0);
61  class_addmethod(c, (method)MaxGraphObject, "graph.object", A_CANT, 0);
62  class_addmethod(c, (method)MidiOutAssist, "assist", A_CANT, 0);
63  class_addmethod(c, (method)object_obex_dumpout, "dumpout", A_CANT, 0);
64 
65  CLASS_ATTR_SYM(c, "device", 0, MidiOut, obj);
66  CLASS_ATTR_ACCESSORS(c, "device", MidiOutGetDevice, MidiOutSetDevice);
67 
68  class_register(_sym_box, c);
69  sMidiOutClass = c;
70  return 0;
71 }
72 
73 
74 /************************************************************************************/
75 // Object Creation Method
76 
77 MidiOutPtr MidiOutNew(t_symbol* msg, long argc, t_atom* argv)
78 {
79  MidiOutPtr self;
80  TTValue v;
81  TTErr err;
82 
83  self = MidiOutPtr(object_alloc(sMidiOutClass));
84  if (self) {
85  object_obex_store((void*)self, _sym_dumpout, (t_object*)outlet_new(self, NULL)); // dumpout
86  self->graphOutlets[0] = outlet_new(self, "graph.connect");
87 
88  v.resize(2);
89  v[0] = "midi.out";
90  v[1] = 1;
91  err = TTObjectBaseInstantiate(TT("graph.object"), (TTObjectBasePtr*)&self->graphObject, v);
92 
93  if (!self->graphObject->mKernel.valid()) {
94  object_error(SELF, "cannot load Jamoma object");
95  return NULL;
96  }
97 
98  attr_args_process(self, argc, argv);
99  }
100  return self;
101 }
102 
103 
104 // Memory Deallocation
105 void MidiOutFree(MidiOutPtr self)
106 {
107  TTObjectBaseRelease((TTObjectBasePtr*)&self->graphObject);
108 }
109 
110 
111 /************************************************************************************/
112 // Methods bound to input/inlets
113 
114 // Method for Assistance Messages
115 void MidiOutAssist(MidiOutPtr self, void* b, long msg, long arg, char* dst)
116 {
117  if (msg==1) // Inlets
118  strcpy (dst, "dictionary input and control messages");
119  else if (msg==2) { // Outlets
120  if (arg == 0)
121  strcpy(dst, "dictionary output");
122  else
123  strcpy(dst, "dumpout");
124  }
125 }
126 
127 
128 void MidiOutGetDeviceNames(MidiOutPtr self)
129 {
130  TTValue v, none;
131  TTErr err;
132  long ac;
133  t_atom* ap;
134  TTSymbol name;
135 
136  err = self->graphObject->mKernel.send(TT("getAvailableDeviceNames"), none, v);
137  if (!err) {
138  ac = v.size();
139  ap = new t_atom[ac];
140 
141  for (long i=0; i<ac; i++) {
142  name = v[i];
143  atom_setsym(ap+i, gensym((char*)name.c_str()));
144  }
145  object_obex_dumpout(self, gensym("getAvailableDeviceNames"), ac, ap);
146  delete ap;
147  }
148 }
149 
150 
151 t_max_err MidiOutSetDevice(MidiOutPtr self, void* attr, long argc, t_atom* argv)
152 {
153  if (argc) {
154  t_symbol* s = atom_getsym(argv);
155  self->graphObject->mKernel.set(TT("device"), TT(s->s_name));
156  }
157  return MAX_ERR_NONE;
158 }
159 
160 
161 t_max_err MidiOutGetDevice(MidiOutPtr self, void* attr, long* argc, t_atom** argv)
162 {
163  TTValue v;
164  TTSymbol s;
165 
166  self->graphObject->mKernel.get(TT("device"), v);
167  s = v[0];
168  if (!s)
169  return MAX_ERR_GENERIC;
170 
171  *argc = 1;
172  if (!(*argv)) // otherwise use memory passed in
173  *argv = (t_atom *)sysmem_newptr(sizeof(t_atom));
174  atom_setsym(*argv, gensym((char*)s.c_str()));
175  return MAX_ERR_NONE;
176 }
The TTGraphObjectBase wraps a TTDSP object such that it is possible to build a dynamic graph of audio...
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
size_type size() const noexcept
Return the number of elements.
Base class for all first-class Jamoma objects.
Definition: TTObjectBase.h:109
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
const char * c_str() const
Return a pointer to the internal string as a C-string.
Definition: TTSymbol.h:77
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
int C74_EXPORT main(void)
Set up this class as a Max external the first time an object of this kind is instantiated.
Definition: j.midi.out.cpp:47
void resize(size_type n)
Change the number of elements.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34