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.

506 lines
15KB

  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 nframes_t _sample_rate;
  42. static Module *_copied_module_empty;
  43. static char *_copied_module_settings;
  44. void init ( void );
  45. void insert_menu_cb ( const Fl_Menu_ *m );
  46. static void insert_menu_cb ( Fl_Widget *w, void *v );
  47. void menu_cb ( const Fl_Menu_ *m );
  48. static void menu_cb ( Fl_Widget *w, void *v );
  49. Fl_Menu_Button & menu ( void ) const;
  50. void copy ( void ) const;
  51. void paste_before ( void );
  52. protected:
  53. volatile bool _bypass;
  54. public:
  55. /* true if this module was added by default and not under normal user control */
  56. bool is_default ( void ) const { return _is_default; }
  57. void is_default ( bool v ) { _is_default = v; }
  58. virtual void update ( void ) {}
  59. class Port
  60. {
  61. /* char *type_names[] = { "Audio", "Control" }; */
  62. /* char *direction_names[] = { "Input", "Output" }; */
  63. void update_connected_port_buffer ( void )
  64. {
  65. if ( connected() )
  66. connected_port()->_buf = _buf;
  67. }
  68. public:
  69. enum Direction { INPUT, OUTPUT };
  70. enum Type { AUDIO, CONTROL, AUX_AUDIO };
  71. /* hints for control ports (specifically control inputs) */
  72. struct Hints
  73. {
  74. enum Type { LINEAR, LOGARITHMIC, BOOLEAN, INTEGER };
  75. Type type;
  76. bool ranged;
  77. float minimum;
  78. float maximum;
  79. float default_value;
  80. int dimensions;
  81. bool visible;
  82. Hints ( )
  83. {
  84. type = LINEAR;
  85. ranged = false;
  86. minimum = 0;
  87. maximum = 1; /* FIXME: totally bogus, but some
  88. * plugins (SWH delays) don't
  89. * provide an upper bound. */
  90. default_value = 0.0f;
  91. dimensions = 1;
  92. visible = true;
  93. }
  94. };
  95. static int osc_control_change_exact ( float v, void *user_data );
  96. static int osc_control_change_cv ( float v, void *user_data );
  97. Hints hints;
  98. Port ( Module *module, Direction direction, Type type, const char *name = 0 )
  99. {
  100. _name = name;
  101. _direction = direction;
  102. _type = type;
  103. _buf = 0;
  104. _nframes = 0;
  105. _connected = 0;
  106. _module = module;
  107. _scaled_signal = 0;
  108. _unscaled_signal = 0;
  109. _by_number_path = 0;
  110. _by_number_number = -1;
  111. _jack_port = 0;
  112. }
  113. Port ( const Port& p )
  114. {
  115. _name = p._name;
  116. _direction = p._direction;
  117. _type = p._type;
  118. _buf = p._buf;
  119. _nframes = p._nframes;
  120. _connected = p._connected;
  121. _module = p._module;
  122. hints = p.hints;
  123. _scaled_signal = p._scaled_signal;
  124. _unscaled_signal = p._unscaled_signal;
  125. _by_number_path = 0;
  126. _by_number_number = -1;
  127. _jack_port = p._jack_port;
  128. }
  129. virtual ~Port ( )
  130. {
  131. if ( _by_number_path )
  132. free( _by_number_path );
  133. _by_number_path = NULL;
  134. // change_osc_path( NULL );
  135. // disconnect();
  136. }
  137. const char *name ( void ) const { return _name; }
  138. Type type ( void ) const { return _type; }
  139. Direction direction ( void ) const { return _direction; }
  140. Module * module ( void ) const { return _module; }
  141. nframes_t nframes ( void ) const { return _nframes; }
  142. void buffer ( void *buf, nframes_t nframes ) { _buf = buf; _nframes = nframes; };
  143. void *buffer ( void ) const { return _buf; }
  144. OSC::Signal *scaled_signal ( void ) { return _scaled_signal; }
  145. int _by_number_number;
  146. char *_by_number_path;
  147. const char *osc_path ( )
  148. {
  149. if ( _scaled_signal )
  150. return _scaled_signal->path();
  151. else
  152. return NULL;
  153. }
  154. const char *osc_number_path ( void );
  155. void update_osc_port ( )
  156. {
  157. // if ( INPUT == _direction )
  158. change_osc_path( generate_osc_path() );
  159. }
  160. void destroy_osc_port ( )
  161. {
  162. delete _unscaled_signal;
  163. delete _scaled_signal;
  164. _unscaled_signal = _scaled_signal = NULL;
  165. }
  166. void control_value_no_callback ( float f )
  167. {
  168. /* can also be called from the OSC thread */
  169. ASSERT( Thread::is( "UI" ) || Thread::is( "OSC" ),
  170. "Function called from wrong thread! (is %s)", Thread::current()->name() );
  171. if ( buffer() )
  172. {
  173. *((float*)buffer()) = f;
  174. }
  175. }
  176. void control_value ( float f )
  177. {
  178. control_value_no_callback( f );
  179. _module->handle_control_changed( this );
  180. if ( connected() )
  181. connected_port()->_module->handle_control_changed( connected_port() );
  182. }
  183. float control_value ( void ) const
  184. {
  185. if ( buffer() )
  186. return *((float*)buffer());
  187. else
  188. return 0.0f;
  189. }
  190. bool connected ( void ) const { return _connected; }
  191. bool connected_osc ( void ) const;
  192. Port *connected_port ( void ) const
  193. {
  194. return _connected;
  195. }
  196. void connect_to ( Port *to )
  197. {
  198. _buf = to->_buf;
  199. to->_connected = this;
  200. _connected = to;
  201. }
  202. void connect_to ( void *buf )
  203. {
  204. _buf = buf;
  205. update_connected_port_buffer();
  206. }
  207. void send_feedback ( void );
  208. void disconnect ( void )
  209. {
  210. if ( _connected && _connected != (void*)0x01 )
  211. {
  212. _connected->_connected = NULL;
  213. if ( _connected->_module )
  214. _connected->_module->handle_control_disconnect( this );
  215. _connected = NULL;
  216. }
  217. else
  218. _connected = NULL;
  219. /* FIXME: do something! */
  220. }
  221. void jack_port ( JACK::Port *v ) { _jack_port = v; }
  222. JACK::Port *jack_port ( void ) { return _jack_port; }
  223. private:
  224. char *generate_osc_path ( void );
  225. void change_osc_path ( char *path );
  226. Port *_connected;
  227. Type _type;
  228. Direction _direction;
  229. const char *_name;
  230. void *_buf;
  231. nframes_t _nframes;
  232. Module *_module;
  233. /* used for auxilliary I/Os */
  234. JACK::Port *_jack_port;
  235. OSC::Signal *_scaled_signal;
  236. OSC::Signal *_unscaled_signal;
  237. static void handle_signal_connection_state_changed ( OSC::Signal *, void *o );
  238. };
  239. void bbox ( int &X, int &Y, int &W, int &H )
  240. {
  241. X += + 5;
  242. Y += 5;
  243. W -= 10;
  244. H -= 10;
  245. }
  246. Module ( int W, int H, const char *L = 0 );
  247. Module ( );
  248. Module ( bool is_default, int W, int H, const char *L = 0 );
  249. virtual ~Module ( );
  250. LOG_NAME_FUNC( Module );
  251. nframes_t nframes ( void ) const { return _nframes; }
  252. void resize_buffers ( nframes_t v ) { _nframes = v; }
  253. int instances ( void ) const { return _instances; }
  254. void instances ( int i ) { _instances = i; }
  255. bool is_being_controlled ( void ) const
  256. {
  257. for ( nframes_t i = control_input.size(); i--; )
  258. if ( control_input[i].connected() )
  259. return true;
  260. return false;
  261. }
  262. bool is_controlling ( void ) const
  263. {
  264. for ( nframes_t i = control_output.size(); i--; )
  265. if ( control_output[i].connected() )
  266. return true;
  267. return false;
  268. }
  269. /* bool */
  270. /* is_being_controlled_osc ( void ) const */
  271. /* { */
  272. /* for ( nframes_t i = control_input.size(); i--; ) */
  273. /* if ( control_input[i].connected_osc() ) */
  274. /* return true; */
  275. /* return false; */
  276. /* } */
  277. virtual const char *name ( void ) const = 0;
  278. virtual const char *basename ( void ) const { return "Module"; }
  279. std::vector<Port> audio_input;
  280. std::vector<Port> audio_output;
  281. std::vector<Port> control_input;
  282. std::vector<Port> control_output;
  283. std::vector<Port> aux_audio_input;
  284. std::vector<Port> aux_audio_output;
  285. void add_port ( const Port &p )
  286. {
  287. if ( p.type() == Port::AUDIO && p.direction() == Port::INPUT )
  288. audio_input.push_back( p );
  289. else if ( p.type() == Port::AUDIO && p.direction() == Port::OUTPUT )
  290. audio_output.push_back( p );
  291. else if ( p.type() == Port::CONTROL && p.direction() == Port::INPUT )
  292. control_input.push_back( p );
  293. else if ( p.type() == Port::CONTROL && p.direction() == Port::OUTPUT )
  294. control_output.push_back( p );
  295. }
  296. int noutputs ( void ) const
  297. {
  298. return audio_output.size();
  299. }
  300. int ninputs ( void ) const
  301. {
  302. return audio_input.size();
  303. }
  304. int ncontrol_inputs ( void ) const
  305. {
  306. return control_input.size();
  307. }
  308. int ncontrol_outputs ( void ) const
  309. {
  310. return control_output.size();
  311. }
  312. int nvisible_control_inputs ( void ) const
  313. {
  314. int n = 0;
  315. for ( std::vector<Port>::const_iterator i = control_input.begin();
  316. i != control_input.end();
  317. i++ )
  318. if ( i->hints.visible )
  319. n++;
  320. return n;
  321. }
  322. virtual bool bypass ( void ) const { return _bypass; }
  323. virtual void bypass ( bool v ) { _bypass = v; redraw(); }
  324. virtual bool bypassable ( void ) const
  325. {
  326. return ninputs() == noutputs() ||
  327. ( ninputs() == 1 && noutputs() == 2 );
  328. }
  329. int control_input_port_index ( Port *p )
  330. {
  331. for ( nframes_t i = control_input.size(); i--; )
  332. if ( &control_input[i] == p )
  333. return i;
  334. return -1;
  335. }
  336. int control_output_port_index ( Port *p )
  337. {
  338. for ( nframes_t i = control_output.size(); i--; )
  339. if ( &control_output[i] == p )
  340. return i;
  341. return -1;
  342. }
  343. Chain *chain ( void ) const { return _chain; }
  344. void chain ( Chain * v );
  345. char *get_parameters ( void ) const;
  346. void set_parameters ( const char * );
  347. void send_feedback ( void );
  348. virtual bool initialize ( void ) { return true; }
  349. /* for the given number of inputs, return how many outputs this
  350. * plugin would have. -1 if this plugin can't support so many
  351. * inputs. */
  352. virtual int can_support_inputs ( int n ) = 0;
  353. /* called by the chain whenever we need to adjust our input
  354. * channel configuration, but only if can_support_inputs() returns
  355. * true */
  356. virtual bool configure_inputs ( int n ) = 0;
  357. virtual void process ( nframes_t ) = 0;
  358. /* called whenever the module is initialized or when the sample rate is changed at runtime */
  359. virtual void handle_sample_rate_change ( nframes_t sample_rate ) {}
  360. /* called whenever the value of a control port is changed.
  361. This can be used to take appropriate action from the GUI thread */
  362. virtual void handle_control_changed ( Port * );
  363. virtual void handle_control_disconnect ( Port * ) {}
  364. /* called whenever the name of the chain changes (usually because
  365. * the name of the mixer strip changed). */
  366. virtual void handle_chain_name_changed ();
  367. virtual void handle_port_connection_change () {}
  368. #define MODULE_CLONE_FUNC(class) \
  369. virtual Module *clone_empty ( void ) const \
  370. { \
  371. return new class (); \
  372. }
  373. virtual Module *clone_empty ( void ) const { return NULL; }
  374. Module *clone ( Chain *dest ) const;
  375. Module *clone ( void ) const;
  376. protected:
  377. nframes_t sample_rate ( void ) const { return Module::_sample_rate; }
  378. void draw_connections ( void );
  379. void draw_label ( int X, int Y, int W, int H );
  380. void draw_box ( int X, int Y, int W, int H );
  381. virtual void draw ( void ) { Module::draw_box(x(),y(),w(),h()); Module::draw_label(x(),y(),w(),h()); }
  382. virtual int handle ( int m );
  383. virtual void get ( Log_Entry &e ) const;
  384. virtual void set ( Log_Entry &e );
  385. bool add_aux_port ( bool input, const char *prefix, int n );
  386. public:
  387. void freeze_ports ( void );
  388. void thaw_ports ( void );
  389. bool add_aux_audio_output ( const char *prefix, int n );
  390. bool add_aux_audio_input ( const char *prefix, int n );
  391. static void set_sample_rate ( nframes_t srate ) { _sample_rate = srate; }
  392. void command_open_parameter_editor();
  393. virtual void command_activate ( void );
  394. virtual void command_deactivate ( void );
  395. virtual void command_remove ( void );
  396. };