PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Core/Score/implementations/DemoApp/main.cpp

https://github.com/tap/TapTools
C++ | 339 lines | 191 code | 88 blank | 60 comment | 15 complexity | 3fb2266a83e60ff253ed7732b89d367a MD5 | raw file
Possible License(s): LGPL-2.1
  1. #include "TTModular.h"
  2. #include "TTScore.h"
  3. #include <iostream>
  4. #include <string>
  5. // A class for our application
  6. class DemoApp
  7. {
  8. public:
  9. DemoApp(){};
  10. ~DemoApp(){};
  11. // This application is divided into four main functions
  12. void SetupModular();
  13. void SetupScore();
  14. void SetMessage(std::string s);
  15. void Quit();
  16. private:
  17. // Declare the application manager and our application
  18. TTObject mApplicationManager;
  19. TTObject mApplicationDemo;
  20. // Declare callbacks used to observe scenario execution
  21. TTObject mEventStatusChangedCallback;
  22. // Declare a thread used to poll information about scenario execution at another rate
  23. TTThread* mPollingThread;
  24. public:
  25. // Declare publicly all datas of our application to retreive them from the callback function
  26. TTObject mDataDemoParameter; // a parameter is relative to the state of our application
  27. TTObject mDataDemoMessage; // a message is a kind of command to send to our application
  28. TTObject mDataDemoReturn; // a return is a kind of notification sent by our application
  29. // Declare publicly the scenario to retreive it from the callback function
  30. TTObject mScenario;
  31. // Be friend with each callback or function
  32. friend TTErr DemoAppDataReturnValueCallback(const TTValue& baton, const TTValue& value);
  33. friend TTErr DemoAppEventStatusChangedCallback(const TTValue& baton, const TTValue& value);
  34. friend void DemoAppScenarioPollingThread(DemoApp* demoApp);
  35. };
  36. // Callback function to get data's value back
  37. TTErr DemoAppDataReturnValueCallback(const TTValue& baton, const TTValue& value);
  38. // Callback function to be notified when an event status is changing
  39. TTErr DemoAppEventStatusChangedCallback(const TTValue& baton, const TTValue& value);
  40. // Function to poll information about scenario execution at another rate
  41. void DemoAppScenarioPollingThread(DemoApp* demoApp);
  42. int
  43. main(int argc, char **argv)
  44. {
  45. DemoApp app;
  46. TTLogMessage("\n*** Start of Jamoma Modular and Score demonstration ***\n");
  47. app.SetupModular();
  48. app.SetupScore();
  49. // read command from console
  50. do
  51. {
  52. TTLogMessage("\nType a command : \n");
  53. std::string s;
  54. std::getline(std::cin, s);
  55. // quit the application
  56. if (!s.compare("quit"))
  57. {
  58. app.Quit();
  59. TTLogMessage("\n*** End of Jamoma Modular and Score demonstration ***\n");
  60. return EXIT_SUCCESS;
  61. }
  62. // update mDataDemoMessage data with the command
  63. else
  64. {
  65. app.SetMessage(s);
  66. }
  67. }
  68. while (YES);
  69. }
  70. void
  71. DemoApp::SetupModular()
  72. {
  73. TTValue args;
  74. TTLogMessage("\n*** Initialisation of Modular environnement ***\n");
  75. /////////////////////////////////////////////////////////////////////
  76. // Init the Modular library (passing the folder path where all the dylibs are)
  77. TTModularInit("/usr/local/jamoma/extensions");
  78. // Create an application manager
  79. mApplicationManager = TTObject("ApplicationManager");
  80. TTLogMessage("\n*** Creation of mApplicationDemo application ***\n");
  81. /////////////////////////////////////////////////////////////////////
  82. // Create a local application called "demo" and get it back
  83. mApplicationDemo = mApplicationManager.send("ApplicationInstantiateLocal", "demo");
  84. if (!mApplicationDemo.valid())
  85. {
  86. TTLogError("Error : can't create demo application \n");
  87. return;
  88. }
  89. TTLogMessage("\n*** Creation of mApplicationDemo datas ***\n");
  90. /////////////////////////////////////////////////////////////////////
  91. // Create a parameter data and set its callback function and baton and some attributes
  92. mDataDemoParameter = TTObject("Data", "parameter");
  93. // Setup the callback mechanism to get the value back
  94. args = TTValue(TTPtr(this), mDataDemoParameter);
  95. mDataDemoParameter.set("baton", args);
  96. mDataDemoParameter.set("function", TTPtr(&DemoAppDataReturnValueCallback));
  97. // Setup the data attributes depending of its use inside the application
  98. mDataDemoParameter.set("type", "decimal");
  99. mDataDemoParameter.set("rangeBounds", TTValue(0., 1.));
  100. mDataDemoParameter.set("rangeClipmode", "low");
  101. mDataDemoParameter.set("description", "any information relative to the state of the application");
  102. // Register the parameter data into mApplicationDemo at an address
  103. args = TTValue("/myParameter", mDataDemoParameter);
  104. mApplicationDemo.send("ObjectRegister", args);
  105. // Create a message data and set its callback function and baton and some attributes
  106. mDataDemoMessage = TTObject("Data", "message");
  107. // Setup the callback mechanism to get the value back
  108. args = TTValue(TTPtr(this), mDataDemoMessage);
  109. mDataDemoMessage.set("baton", args);
  110. mDataDemoMessage.set("function", TTPtr(&DemoAppDataReturnValueCallback));
  111. // Setup the data attributes depending of its use inside the application
  112. mDataDemoMessage.set("type", "string");
  113. mDataDemoMessage.set("description", "any information to provide to the application");
  114. // Register the message data into mApplicationDemo at an address
  115. args = TTValue("/myMessage", mDataDemoMessage);
  116. mApplicationDemo.send("ObjectRegister", args);
  117. // Create a return data and set its callback function and baton and some attributes
  118. mDataDemoReturn = TTObject("Data", "return");
  119. // Setup the callback mechanism to get the value back
  120. args = TTValue(TTPtr(this), mDataDemoReturn);
  121. mDataDemoReturn.set("baton", args);
  122. mDataDemoReturn.set("function", TTPtr(&DemoAppDataReturnValueCallback));
  123. // Setup the data attributes depending of its use inside the application
  124. mDataDemoReturn.set("type", "integer");
  125. mDataDemoReturn.set("defaultValue", 0);
  126. mDataDemoReturn.set("description", "any information the application returns back");
  127. // Register the return data into mApplicationDemo at an address
  128. args = TTValue("/myReturn", mDataDemoReturn);
  129. mApplicationDemo.send("ObjectRegister", args);
  130. // Initialise the application and all datas inside (using defaultValue attribute)
  131. mApplicationDemo.send("Init");
  132. }
  133. void
  134. DemoApp::SetupScore()
  135. {
  136. TTObject xmlHandler("XmlHandler");
  137. TTLogMessage("\n*** Initialisation of Score environnement ***\n");
  138. /////////////////////////////////////////////////////////////////////
  139. // Init the Score library (passing the folder path where all the dylibs are)
  140. TTScoreInit("/usr/local/jamoma/extensions");
  141. TTLogMessage("\n*** Reading of an interactive scenario file ***\n");
  142. /////////////////////////////////////////////////////////////////////
  143. // Create an empty Scenario
  144. mScenario = TTObject("Scenario");
  145. // Read DemoScenario1.score file to fill mScenario
  146. xmlHandler.set("object", mScenario);
  147. xmlHandler.send("Read", "../DemoScenario.score");
  148. TTLogMessage("\n*** Prepare scenario observation ***\n");
  149. /////////////////////////////////////////////////////////////////////
  150. // Create a callback for the "EventStatusChanged" notification sent by each event
  151. mEventStatusChangedCallback = TTObject("callback");
  152. mEventStatusChangedCallback.set("baton", TTPtr(this));
  153. mEventStatusChangedCallback.set("function", TTPtr(&DemoAppEventStatusChangedCallback));
  154. mEventStatusChangedCallback.set("notification", TTSymbol("EventStatusChanged"));
  155. // Get all events of the scenario and attach a callback to them
  156. TTValue timeEvents;
  157. mScenario.get("timeEvents", timeEvents);
  158. for (TTElementIter it = timeEvents.begin() ; it != timeEvents.end() ; it++)
  159. {
  160. TTObject event = TTElement(*it);
  161. event.registerObserverForNotifications(mEventStatusChangedCallback);
  162. }
  163. TTLogMessage("\n*** Start scenario execution ***\n");
  164. /////////////////////////////////////////////////////////////////////
  165. // Set the execution speed of the scenario
  166. mScenario.set("speed", 2.);
  167. // Start the scenario
  168. mScenario.send("Start");
  169. // Poll Scenario information
  170. mPollingThread = new TTThread(TTThreadCallbackType(DemoAppScenarioPollingThread), this);
  171. }
  172. void
  173. DemoApp::SetMessage(std::string s)
  174. {
  175. TTSymbol message(s);
  176. mDataDemoMessage.send("Command", message);
  177. }
  178. void
  179. DemoApp::Quit()
  180. {
  181. TTLogMessage("\n*** Release mApplicationDemo datas ***\n");
  182. /////////////////////////////////////////////////////////////////////
  183. // Unregister the parameter
  184. if ( mApplicationDemo.send("ObjectUnregister", "/myParameter"))
  185. TTLogError("Error : can't unregister data at /myParameter address \n");
  186. // Unregister the message
  187. if (mApplicationDemo.send("ObjectUnregister", "/myMessage"))
  188. TTLogError("Error : can't unregister data at /myMessage address \n");
  189. // Unregister the return
  190. if (mApplicationDemo.send("ObjectUnregister", "/myReturn"))
  191. TTLogError("Error : can't unregister data at /myReturn address \n");
  192. TTLogMessage("\n*** Release application ***\n");
  193. /////////////////////////////////////////////////////////////////////
  194. mApplicationManager.send("ApplicationRelease", "demo");
  195. // quit the program normally
  196. std::exit(EXIT_SUCCESS);
  197. }
  198. TTErr
  199. DemoAppDataReturnValueCallback(const TTValue& baton, const TTValue& value)
  200. {
  201. DemoApp* demoApp = (DemoApp*)TTPtr(baton[0]);
  202. TTObject anObject = baton[1];
  203. // Reteive which data has been updated
  204. if (anObject.instance() == demoApp->mDataDemoParameter.instance())
  205. {
  206. // print the returned value
  207. TTLogMessage("/myParameter has been updated to %s \n", value.toString().data());
  208. return kTTErrNone;
  209. }
  210. if (anObject.instance() == demoApp->mDataDemoMessage.instance())
  211. {
  212. // print the returned value
  213. TTLogMessage("/myMessage has been updated to %s \n", value.toString().data());
  214. return kTTErrNone;
  215. }
  216. if (anObject.instance() == demoApp->mDataDemoReturn.instance())
  217. {
  218. // print the returned value
  219. TTLogMessage("/myReturn has been updated to %s \n", value.toString().data());
  220. return kTTErrNone;
  221. }
  222. return kTTErrGeneric;
  223. }
  224. TTErr
  225. DemoAppEventStatusChangedCallback(const TTValue& baton, const TTValue& value)
  226. {
  227. DemoApp* demoApp = (DemoApp*)TTPtr(baton[0]);
  228. TTObject event = value[0];
  229. TTSymbol newStatus = value[1];
  230. TTSymbol oldStatus = value[2];
  231. // get the name of the event
  232. TTSymbol name;
  233. event.get("name", name);
  234. // print the event status
  235. TTLogMessage("%s status : %s \n", name.c_str(), newStatus.c_str());
  236. return kTTErrNone;
  237. }
  238. void DemoAppScenarioPollingThread(DemoApp* demoApp)
  239. {
  240. TTBoolean isRunning;
  241. do
  242. {
  243. // wait
  244. demoApp->mPollingThread->sleep(1);
  245. // look at the running state of the scnario
  246. demoApp->mScenario.get("running", isRunning);
  247. }
  248. while (isRunning);
  249. // quit DemoApp
  250. demoApp->Quit();
  251. }