Jamoma API  0.6.0.a19
TTAudioGraphDescription.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup audioGraphLibrary
4  *
5  * @brief Describes a node in an audio processing graph
6  *
7  * @details
8  *
9  * @authors Timothy Place, Trond Lossius
10  *
11  * @copyright Copyright © 2010, 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 
17 #include <fstream>
18 #include <iostream>
19 
20 using namespace std;
21 
22 
23 void TTAudioGraphDescription::exportRuby(const TTString& fullpathToFile)
24 {
25  TTString rubyContent;
26  int index = -1;
27  TTStringVector nodeNames;
28  ofstream rubyFile(fullpathToFile.c_str());
29 
30  rubyContent += "require \"Jamoma\"\n\n";
31  exportRubyNode(rubyContent, index, nodeNames);
32  rubyFile.write(rubyContent.c_str(), rubyContent.size());
33  rubyFile.close();
34 }
35 
36 
37 // WARNING: this code has been largely duplicated to TTGraphDescription.h
38 int TTAudioGraphDescription::exportRubyNode(TTString& content, int& index, TTStringVector& nodeNames)
39 {
40  char objName[16];
41  int localIndex;
42 
43  index++;
44  localIndex = index;
45  snprintf(objName, 16, "obj%i", index);
46  nodeNames.push_back(TTString(objName));
47 
48  content += objName;
49  content += " = TTAudio.new \"";
50  content += mClassName.c_str();
51  content += "\"\n";
52 #ifdef SORRY_THIS_IS_BROKEN
53  for (TTAudioGraphDescriptionIter input = mAudioDescriptions.begin(); input != mAudioDescriptions.end(); input++) {
54  int inputIndex = input->exportRubyNode(content, index, nodeNames);
55  content += objName;
56  content += ".connect_audio ";
57  content += nodeNames[inputIndex];
58  content += "\n";
59  }
60  for (TTGraphDescriptionIter input = mControlDescription.mInputDescriptions.begin(); input != mControlDescription.mInputDescriptions.end(); input++) {
61  int inputIndex = input->exportRubyNode(content, index, nodeNames); // note: calls into TTGraph's exportRubyNode
62  content += objName;
63  content += ".connect ";
64  content += nodeNames[inputIndex];
65  content += "\n";
66  }
67 #endif
68  return localIndex;
69 }
70 
71 
72 void TTAudioGraphDescription::exportCpp(const TTString& fullpathToFile)
73 {
74  TTString content;
75  int index = -1;
76  TTStringVector nodeNames;
77  ofstream aFile(fullpathToFile.c_str());
78 
79  content += "#include \"TTAudioGraphAPI.h\"\n\n";
80  content += "int main()\n{\n";
81  content += " TTAudioGraphInit();\n\n";
82 
83  exportCppNode(content, index, nodeNames);
84 
85  content += " return 0;\n";
86  content += "}\n";
87  aFile.write(content.c_str(), content.size());
88  aFile.close();
89 }
90 
91 int TTAudioGraphDescription::exportCppNode(TTString& content, int& index, TTStringVector& nodeNames)
92 {
93  char objName[16];
94  int localIndex;
95 
96  index++;
97  localIndex = index;
98  snprintf(objName, 16, "obj%i", index);
99  nodeNames.push_back(TTString(objName));
100 
101  content += " TTAudioGraphObjectPtr ";
102  content += objName;
103  content += ";\n";
104  content += " TTObjectBaseInstantiate(TT(\"audio.object\"), (TTObjectBasePtr*)&";
105  content += objName;
106  content += ", TTValue(TT(\"";
107  content += mClassName.c_str();
108  content += "\")))\n\n";
109 #ifdef SORRY_THIS_IS_BROKEN
110 
111  for (TTAudioGraphDescriptionIter input = mAudioDescriptions.begin(); input != mAudioDescriptions.end(); input++) {
112  int inputIndex = input->exportCppNode(content, index, nodeNames);
113  content += " ";
114  content += objName;
115  content += "->connectAudio(";
116  content += nodeNames[inputIndex];
117  content += ");\n";
118  }
119  for (TTGraphDescriptionIter input = mControlDescription.mInputDescriptions.begin(); input != mControlDescription.mInputDescriptions.end(); input++) {
120  int inputIndex = input->exportCppNode(content, index, nodeNames); // note: calls into TTGraph's exportCppNode
121  content += " ";
122  content += objName;
123  content += "->connect(";
124  content += nodeNames[inputIndex];
125  content += ");\n";
126  }
127 #endif
128  return localIndex;
129 }
130 
131 
132 void TTAudioGraphDescription::exportMax(const TTString& fullpathToFile)
133 {
134  TTString content;
135  int index = -1;
136  TTStringVector nodeNames;
137  ofstream aFile(fullpathToFile.c_str());
138 
139  content += "{\n";
140  content += " \"patcher\" : {\n";
141  content += " \"boxes\" : [\n";
142 
143  exportMaxNode(content, index, nodeNames);
144 
145  content += " ]\n";
146  content += " }\n";
147  content += "}\n";
148  aFile.write(content.c_str(), content.size());
149  aFile.close();
150 }
151 
152 int TTAudioGraphDescription::exportMaxNode(TTString& content, int& index, TTStringVector& nodeNames)
153 {
154  char objName[16];
155  char location[16];
156  int localIndex;
157 
158  index++;
159  localIndex = index;
160  snprintf(objName, 16, "obj%i", index);
161  nodeNames.push_back(TTString(objName));
162 
163  if (index > 0)
164  content += ",\n";
165 
166  content += " {\n";
167  content += " \"box\" : {\n";
168  content += " \"id\" : \"";
169  content += objName;
170  content += "\",\n";
171  content += " \"maxclass\" : \"newobj\",\n";
172  content += " \"text\" : \"jcom.";
173 
174  // TODO: is there a better way to know about object name mappings?
175  //if (mClassName == TT("dac"))
176  // content += "dac";
177  //else if (mClassName == TT("adc"))
178  // content += "adc";
179  //else
180  if (mClassName == TT("operator"))
181  content += "op";
182  else
183  content += mClassName.c_str();
184 
185  content += "≈\",\n";
186  content += " \"patching_rect\" : [ 50.0, ";
187  snprintf(location, 16, "%f", 400.0 - (index * 40.0));
188  content += location;
189  content += ", 100.0, 20.0]\n";
190  content += " }\n";
191  content += " }\n";
192 #ifdef SORRY_THIS_IS_BROKEN
193 
194  for (TTAudioGraphDescriptionIter input = mAudioDescriptions.begin(); input != mAudioDescriptions.end(); input++) {
195  int inputIndex;
196 
197  inputIndex = input->exportMaxNode(content, index, nodeNames);
198 
199  if (input == mAudioDescriptions.begin()) {
200  for (TTGraphDescriptionIter graphInput = mControlDescription.mInputDescriptions.begin(); graphInput != mControlDescription.mInputDescriptions.end(); graphInput++) {
201  int graphInputIndex = graphInput->exportMaxNode(content, index, nodeNames); // note: calls into TTGraph's exportMaxNode
202 
203  if (index == graphInputIndex) { // I think this means that we are processing the top of the chain?)
204  content += " ],";
205  content += " \"lines\" : [";
206  }
207  else
208  content += ",\n";
209 
210  content += " {\n";
211  content += " \"patchline\" : {\n";
212  content += " \"destination\" : [ \"";
213  content += objName;
214  content += "\", 0],\n";
215  content += " \"source\" : [ \"";
216  content += nodeNames[graphInputIndex];
217  content += "\", 0]\n";
218  content += " }\n";
219  content += " }\n";
220  }
221  }
222 
223  if (index == inputIndex) { // I think this means that we are processing the top of the chain?)
224  content += " ],";
225  content += " \"lines\" : [";
226  }
227  else
228  content += ",\n";
229 
230  content += " {\n";
231  content += " \"patchline\" : {\n";
232  content += " \"destination\" : [ \"";
233  content += objName;
234  content += "\", 0],\n";
235  content += " \"source\" : [ \"";
236  content += nodeNames[inputIndex];
237  content += "\", 0]\n";
238  content += " }\n";
239  content += " }\n";
240  }
241 #endif
242 
243  return localIndex;
244 }
Describes a node in an audio processing graph.
const char * c_str() const
Return a pointer to the internal C-string.
Definition: TTString.h:83
STL namespace.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
The TTString class is used to represent a string.
Definition: TTString.h:34