Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

443 lines
13KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2009 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #pragma once
  19. #include <FL/Fl.H>
  20. #include <FL/Fl_Group.H>
  21. #include <stdlib.h>
  22. #include "debug.h"
  23. #include <vector>
  24. #include "Thread.H"
  25. #include "Loggable.H"
  26. #include "JACK/Port.H"
  27. #include "OSC/Endpoint.H"
  28. class Chain;
  29. class Module_Parameter_Editor;
  30. class Fl_Menu_;
  31. class Fl_Menu_Button;
  32. class Fl_Button;
  33. class Module : public Fl_Group, public Loggable {
  34. int _ins;
  35. int _outs;
  36. int _instances;
  37. nframes_t _nframes;
  38. Chain *_chain;
  39. bool _is_default;
  40. Module_Parameter_Editor *_editor;
  41. static Module *_copied_module_empty;
  42. static char *_copied_module_settings;
  43. void init ( void );
  44. void insert_menu_cb ( const Fl_Menu_ *m );
  45. static void insert_menu_cb ( Fl_Widget *w, void *v );
  46. void menu_cb ( const Fl_Menu_ *m );
  47. static void menu_cb ( Fl_Widget *w, void *v );
  48. Fl_Menu_Button & menu ( void ) const;
  49. void copy ( void ) const;
  50. void paste_before ( void );
  51. protected:
  52. volatile bool _bypass;
  53. public:
  54. /* true if this module was added by default and not under normal user control */
  55. bool is_default ( void ) const { return _is_default; }
  56. void is_default ( bool v ) { _is_default = v; }
  57. virtual bool allows_external_control ( void ) const { return true; }
  58. class Port
  59. {
  60. /* char *type_names[] = { "Audio", "Control" }; */
  61. /* char *direction_names[] = { "Input", "Output" }; */
  62. void update_connected_port_buffer ( void )
  63. {
  64. if ( connected() )
  65. connected_port()->_buf = _buf;
  66. }
  67. public:
  68. enum Direction { INPUT, OUTPUT };
  69. enum Type { AUDIO, CONTROL };
  70. /* hints for control ports (specifically control inputs) */
  71. struct Hints
  72. {
  73. enum Type { LINEAR, LOGARITHMIC, BOOLEAN, INTEGER };
  74. Type type;
  75. bool ranged;
  76. float minimum;
  77. float maximum;
  78. float default_value;
  79. int dimensions;
  80. Hints ( )
  81. {
  82. type = LINEAR;
  83. ranged = false;
  84. minimum = 0;
  85. maximum = 0;
  86. default_value = 0.0f;
  87. dimensions = 1;
  88. }
  89. };
  90. static int osc_control_change_exact ( float v, void *user_data );
  91. static int osc_control_change_cv ( float v, void *user_data );
  92. Hints hints;
  93. Port ( Module *module, Direction direction, Type type, const char *name = 0 )
  94. {
  95. _name = name;
  96. _direction = direction;
  97. _type = type;
  98. _buf = 0;
  99. _nframes = 0;
  100. _connected = 0;
  101. _module = module;
  102. _scaled_signal = 0;
  103. _unscaled_signal = 0;
  104. }
  105. Port ( const Port& p )
  106. {
  107. _name = p._name;
  108. _direction = p._direction;
  109. _type = p._type;
  110. _buf = p._buf;
  111. _nframes = p._nframes;
  112. _connected = p._connected;
  113. _module = p._module;
  114. hints = p.hints;
  115. _scaled_signal = p._scaled_signal;
  116. _unscaled_signal = p._unscaled_signal;
  117. }
  118. virtual ~Port ( )
  119. {
  120. // change_osc_path( NULL );
  121. // disconnect();
  122. }
  123. const char *name ( void ) const { return _name; }
  124. Type type ( void ) const { return _type; }
  125. Direction direction ( void ) const { return _direction; }
  126. Module * module ( void ) const { return _module; }
  127. nframes_t nframes ( void ) const { return _nframes; }
  128. void buffer ( void *buf, nframes_t nframes ) { _buf = buf; _nframes = nframes; };
  129. void *buffer ( void ) const { return _buf; }
  130. const char *osc_path ( )
  131. {
  132. if ( _scaled_signal )
  133. return _scaled_signal->path();
  134. else
  135. return NULL;
  136. }
  137. void update_osc_port ( )
  138. {
  139. // if ( INPUT == _direction )
  140. change_osc_path( generate_osc_path() );
  141. }
  142. void destroy_osc_port ( )
  143. {
  144. delete _unscaled_signal;
  145. delete _scaled_signal;
  146. _unscaled_signal = _scaled_signal = NULL;
  147. }
  148. void control_value_no_callback ( float f )
  149. {
  150. /* can also be called from the OSC thread */
  151. ASSERT( Thread::is( "UI" ) || Thread::is( "OSC" ),
  152. "Function called from wrong thread! (is %s)", Thread::current()->name() );
  153. if ( buffer() )
  154. {
  155. *((float*)buffer()) = f;
  156. }
  157. }
  158. void control_value ( float f )
  159. {
  160. control_value_no_callback( f );
  161. _module->handle_control_changed( this );
  162. if ( connected() )
  163. connected_port()->_module->handle_control_changed( connected_port() );
  164. }
  165. float control_value ( ) const
  166. {
  167. if ( buffer() )
  168. return *((float*)buffer());
  169. else
  170. return 0.0f;
  171. }
  172. bool connected ( void ) const { return _connected; }
  173. bool connected_osc ( void ) const;
  174. Port *connected_port ( void ) const
  175. {
  176. return _connected;
  177. }
  178. void connect_to ( Port *to )
  179. {
  180. _buf = to->_buf;
  181. to->_connected = this;
  182. _connected = to;
  183. }
  184. void connect_to ( void *buf )
  185. {
  186. _buf = buf;
  187. update_connected_port_buffer();
  188. }
  189. void disconnect ( void )
  190. {
  191. if ( _connected && _connected != (void*)0x01 )
  192. {
  193. _connected->_connected = NULL;
  194. _connected = NULL;
  195. }
  196. else
  197. _connected = NULL;
  198. /* FIXME: do something! */
  199. }
  200. private:
  201. char *generate_osc_path ( void );
  202. void change_osc_path ( char *path );
  203. Port *_connected;
  204. Type _type;
  205. Direction _direction;
  206. const char *_name;
  207. void *_buf;
  208. nframes_t _nframes;
  209. Module *_module;
  210. OSC::Signal *_scaled_signal;
  211. OSC::Signal *_unscaled_signal;
  212. static void handle_signal_connection_state_changed ( OSC::Signal *, void *o );
  213. };
  214. void bbox ( int &X, int &Y, int &W, int &H )
  215. {
  216. X = x() + 5;
  217. Y = y() + 5;
  218. W = w() - 10;
  219. H = h() - 10;
  220. }
  221. Module ( int W, int H, const char *L = 0 );
  222. Module ( );
  223. Module ( bool is_default, int W, int H, const char *L = 0 );
  224. virtual ~Module ( );
  225. LOG_NAME_FUNC( Module );
  226. nframes_t nframes ( void ) const { return _nframes; }
  227. void resize_buffers ( nframes_t v ) { _nframes = v; }
  228. int instances ( void ) const { return _instances; }
  229. void instances ( int i ) { _instances = i; }
  230. bool is_being_controlled ( void ) const
  231. {
  232. for ( nframes_t i = control_input.size(); i--; )
  233. if ( control_input[i].connected() )
  234. return true;
  235. return false;
  236. }
  237. bool is_controlling ( void ) const
  238. {
  239. for ( nframes_t i = control_output.size(); i--; )
  240. if ( control_output[i].connected() )
  241. return true;
  242. return false;
  243. }
  244. bool
  245. is_being_controlled_osc ( void ) const
  246. {
  247. for ( nframes_t i = control_input.size(); i--; )
  248. if ( control_input[i].connected_osc() )
  249. return true;
  250. return false;
  251. }
  252. virtual const char *name ( void ) const = 0;
  253. std::vector<Port> audio_input;
  254. std::vector<Port> audio_output;
  255. std::vector<Port> control_input;
  256. std::vector<Port> control_output;
  257. void add_port ( const Port &p )
  258. {
  259. if ( p.type() == Port::AUDIO && p.direction() == Port::INPUT )
  260. audio_input.push_back( p );
  261. else if ( p.type() == Port::AUDIO && p.direction() == Port::OUTPUT )
  262. audio_output.push_back( p );
  263. else if ( p.type() == Port::CONTROL && p.direction() == Port::INPUT )
  264. control_input.push_back( p );
  265. else if ( p.type() == Port::CONTROL && p.direction() == Port::OUTPUT )
  266. control_output.push_back( p );
  267. }
  268. int noutputs ( void ) const
  269. {
  270. return audio_output.size();
  271. }
  272. int ninputs ( void ) const
  273. {
  274. return audio_input.size();
  275. }
  276. int ncontrol_inputs ( void ) const
  277. {
  278. return control_input.size();
  279. }
  280. int ncontrol_outputs ( void ) const
  281. {
  282. return control_output.size();
  283. }
  284. virtual bool bypass ( void ) const { return _bypass; }
  285. virtual void bypass ( bool v ) { _bypass = v; redraw(); }
  286. virtual bool bypassable ( void ) const
  287. {
  288. return ninputs() == noutputs() ||
  289. ( ninputs() == 1 && noutputs() == 2 );
  290. }
  291. int control_input_port_index ( Port *p )
  292. {
  293. for ( nframes_t i = control_input.size(); i--; )
  294. if ( &control_input[i] == p )
  295. return i;
  296. return -1;
  297. }
  298. int control_output_port_index ( Port *p )
  299. {
  300. for ( nframes_t i = control_output.size(); i--; )
  301. if ( &control_output[i] == p )
  302. return i;
  303. return -1;
  304. }
  305. Chain *chain ( void ) const { return _chain; }
  306. void chain ( Chain * v );
  307. char *get_parameters ( void ) const;
  308. void set_parameters ( const char * );
  309. virtual bool initialize ( void ) { return true; }
  310. /* for the given number of inputs, return how many outputs this
  311. * plugin would have. -1 if this plugin can't support so many
  312. * inputs. */
  313. virtual int can_support_inputs ( int n ) = 0;
  314. /* called by the chain whenever we need to adjust our input
  315. * channel configuration, but only if can_support_inputs() returns
  316. * true */
  317. virtual bool configure_inputs ( int n ) = 0;
  318. virtual void process ( nframes_t ) = 0;
  319. /* called whenever the value of a control port is changed.
  320. This can be used to take appropriate action from the GUI thread */
  321. virtual void handle_control_changed ( Port * );
  322. /* called whenever the name of the chain changes (usually because
  323. * the name of the mixer strip changed). */
  324. virtual void handle_chain_name_changed ();
  325. virtual void handle_port_connection_change () {}
  326. #define MODULE_CLONE_FUNC(class) \
  327. virtual Module *clone_empty ( void ) const \
  328. { \
  329. return new class (); \
  330. }
  331. virtual Module *clone_empty ( void ) const { return NULL; }
  332. Module *clone ( Chain *dest ) const;
  333. Module *clone ( void ) const;
  334. protected:
  335. void draw_connections ( void );
  336. void draw_label ( void );
  337. void draw_box ( void );
  338. virtual void draw ( void ) { Module::draw_box(); Module::draw_label(); }
  339. virtual int handle ( int m );
  340. virtual void get ( Log_Entry &e ) const;
  341. virtual void set ( Log_Entry &e );
  342. public:
  343. void command_open_parameter_editor();
  344. virtual void command_activate ( void );
  345. virtual void command_deactivate ( void );
  346. virtual void command_remove ( void );
  347. };