Jamoma API  0.6.0.a19
j~.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxExternalsDSP
4  *
5  * @brief j~ : creates an MSP external that is able to wrap any Jamoma class
6  *
7  * @details
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright © 2012 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 "TTClassWrapperMax.h"
18 #include "TTDSP.h"
19 
20 // Prototypes for methods
21 t_object* jamoma_new(t_symbol *s, long argc, t_atom* argv);
22 t_object* wrappedClass_new(t_symbol *name, long argc, t_atom* argv);
23 void wrappedClass_free(WrappedInstancePtr x);
24 
25 // Globals
26 static t_class* s_jamoma_class;
27 static t_hashtab* s_jamoma_class_hash = NULL;
28 
29 /************************************************************************************/
30 // Define a Max class that does essentially nothing but let users type the name into an object box.
31 // The class we actually use will be defined in the new method.
32 
33 int C74_EXPORT main(void)
34 {
35  s_jamoma_class = class_new("j~", (method)jamoma_new, (method)wrappedClass_free, sizeof(WrappedInstance), (method)0L, A_GIMME, 0);
36  class_register(CLASS_BOX, s_jamoma_class);
37 
38  common_symbols_init();
39  TTDSPInit();
40  return 0;
41 }
42 
43 /************************************************************************************/
44 // When a new instance of jamoma~ is created, we look at the first argument to get a Jamoma class name.
45 // Then we wrap that class as a Max class.
46 // Finally, we proceed to then instantiate the new Max class instead of the jamoma~ Max class.
47 
48 t_object* jamoma_new(t_symbol *s, long argc, t_atom* argv)
49 {
50  int attrstart = attr_args_offset(argc, argv);
51  int i = 0;
52  int channelCount = 2;
53  t_symbol *className = gensym("gain");
54  WrappedClassPtr classWrapper = NULL;
55  char maxClassName[256];
56 
57  if (!attrstart) {
58  error("must specify a jamoma class as the first argument");
59  return NULL;
60  }
61  while (attrstart--) {
62  if (atom_gettype(argv+i) == A_LONG)
63  channelCount = atom_getlong(argv+i);
64  else if (atom_gettype(argv+i) == A_SYM)
65  className = atom_getsym(argv+i);
66  i++;
67  }
68 
69  snprintf(maxClassName, 256, "j.%s~", className->s_name);
70 
71  if (!s_jamoma_class_hash)
72  s_jamoma_class_hash = hashtab_new(0);
73  hashtab_lookup(s_jamoma_class_hash, className, (t_object**)&classWrapper);
74 
75  if (!classWrapper) {
76  wrapTTClassAsMaxClass(className->s_name, maxClassName, &classWrapper);
77  hashtab_store(s_jamoma_class_hash, className, (t_object*)(classWrapper));
78  }
79 
80  return wrappedClass_new(gensym(maxClassName), argc-1, argv+1);
81 }
Jamoma DSP Library.
WrappedInstance * WrappedInstancePtr
Pointer to a wrapped instance of our object.
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~.cpp:33
void TTDSP_EXPORT TTDSPInit(const char *pathToBinaries=NULL)
Initialise the Jamoma DSP library, as well as Jamoma Foundation foundation if needed.
Definition: TTDSP.cpp:30