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.

497 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 };
  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. void learn_osc ( void );
  98. Hints hints;
  99. Port ( Module *module, Direction direction, Type type, const char *name = 0 )
  100. {
  101. _name = name;
  102. _direction = direction;
  103. _type = type;
  104. _buf = 0;
  105. _nframes = 0;
  106. _connected = 0;
  107. _module = module;
  108. _scaled_signal = 0;
  109. _unscaled_signal = 0;
  110. }
  111. Port ( const Port& p )
  112. {
  113. _name = p._name;
  114. _direction = p._direction;
  115. _type = p._type;
  116. _buf = p._buf;
  117. _nframes = p._nframes;
  118. _connected = p._connected;
  119. _module = p._module;
  120. hints = p.hints;
  121. _scaled_signal = p._scaled_signal;
  122. _unscaled_signal = p._unscaled_signal;
  123. }
  124. virtual ~Port ( )
  125. {
  126. // change_osc_path( NULL );
  127. // disconnect();
  128. }
  129. const char *name ( void ) const { return _name; }
  130. Type type ( void ) const { return _type; }
  131. Direction direction ( void ) const { return _direction; }
  132. Module * module ( void ) const { return _module; }
  133. nframes_t nframes ( void ) const { return _nframes; }
  134. void buffer ( void *buf, nframes_t nframes ) { _buf = buf; _nframes = nframes; };
  135. void *buffer ( void ) const { return _buf; }
  136. OSC::Signal *scaled_signal ( void ) { return _scaled_signal; }
  137. const char *osc_path ( )
  138. {
  139. if ( _scaled_signal )
  140. return _scaled_signal->path();
  141. else
  142. return NULL;
  143. }
  144. char *osc_number_path ( void );
  145. void update_osc_port ( )
  146. {
  147. // if ( INPUT == _direction )
  148. change_osc_path( generate_osc_path() );
  149. }
  150. void destroy_osc_port ( )
  151. {
  152. delete _unscaled_signal;
  153. delete _scaled_signal;
  154. _unscaled_signal = _scaled_signal = NULL;
  155. }
  156. void control_value_no_callback ( float f )
  157. {
  158. /* can also be called from the OSC thread */
  159. ASSERT( Thread::is( "UI" ) || Thread::is( "OSC" ),
  160. "Function called from wrong thread! (is %s)", Thread::current()->name() );
  161. if ( buffer() )
  162. {
  163. *((float*)buffer()) = f;
  164. }
  165. if ( _scaled_signal )
  166. {
  167. if ( hints.ranged )
  168. {
  169. // scale value to range.
  170. float scale = hints.maximum - hints.minimum;
  171. float offset = hints.minimum;
  172. f = ( f - offset ) / scale;
  173. }
  174. if ( f > 1.0 )
  175. f = 1.0;
  176. else if ( f < 0.0 )
  177. f = 0.0;
  178. // _scaled_signal->value( f );
  179. }
  180. }
  181. void control_value ( float f )
  182. {
  183. control_value_no_callback( f );
  184. _module->handle_control_changed( this );
  185. if ( connected() )
  186. connected_port()->_module->handle_control_changed( connected_port() );
  187. }
  188. float control_value ( ) const
  189. {
  190. if ( buffer() )
  191. return *((float*)buffer());
  192. else
  193. return 0.0f;
  194. }
  195. bool connected ( void ) const { return _connected; }
  196. bool connected_osc ( void ) const;
  197. Port *connected_port ( void ) const
  198. {
  199. return _connected;
  200. }
  201. void connect_to ( Port *to )
  202. {
  203. _buf = to->_buf;
  204. to->_connected = this;
  205. _connected = to;
  206. }
  207. void connect_to ( void *buf )
  208. {
  209. _buf = buf;
  210. update_connected_port_buffer();
  211. }
  212. void send_feedback ( void );
  213. void disconnect ( void )
  214. {
  215. if ( _connected && _connected != (void*)0x01 )
  216. {
  217. _connected->_connected = NULL;
  218. _connected = NULL;
  219. }
  220. else
  221. _connected = NULL;
  222. /* FIXME: do something! */
  223. }
  224. private:
  225. char *generate_osc_path ( void );
  226. void change_osc_path ( char *path );
  227. Port *_connected;
  228. Type _type;
  229. Direction _direction;
  230. const char *_name;
  231. void *_buf;
  232. nframes_t _nframes;
  233. Module *_module;
  234. OSC::Signal *_scaled_signal;
  235. OSC::Signal *_unscaled_signal;
  236. static void handle_signal_connection_state_changed ( OSC::Signal *, void *o );
  237. };
  238. void bbox ( int &X, int &Y, int &W, int &H )
  239. {
  240. X += + 5;
  241. Y += 5;
  242. W -= 10;
  243. H -= 10;
  244. }
  245. Module ( int W, int H, const char *L = 0 );
  246. Module ( );
  247. Module ( bool is_default, int W, int H, const char *L = 0 );
  248. virtual ~Module ( );
  249. LOG_NAME_FUNC( Module );
  250. nframes_t nframes ( void ) const { return _nframes; }
  251. void resize_buffers ( nframes_t v ) { _nframes = v; }
  252. int instances ( void ) const { return _instances; }
  253. void instances ( int i ) { _instances = i; }
  254. bool is_being_controlled ( void ) const
  255. {
  256. for ( nframes_t i = control_input.size(); i--; )
  257. if ( control_input[i].connected() )
  258. return true;
  259. return false;
  260. }
  261. bool is_controlling ( void ) const
  262. {
  263. for ( nframes_t i = control_output.size(); i--; )
  264. if ( control_output[i].connected() )
  265. return true;
  266. return false;
  267. }
  268. bool
  269. is_being_controlled_osc ( void ) const
  270. {
  271. for ( nframes_t i = control_input.size(); i--; )
  272. if ( control_input[i].connected_osc() )
  273. return true;
  274. return false;
  275. }
  276. virtual const char *name ( void ) const = 0;
  277. std::vector<Port> audio_input;
  278. std::vector<Port> audio_output;
  279. std::vector<Port> control_input;
  280. std::vector<Port> control_output;
  281. void add_port ( const Port &p )
  282. {
  283. if ( p.type() == Port::AUDIO && p.direction() == Port::INPUT )
  284. audio_input.push_back( p );
  285. else if ( p.type() == Port::AUDIO && p.direction() == Port::OUTPUT )
  286. audio_output.push_back( p );
  287. else if ( p.type() == Port::CONTROL && p.direction() == Port::INPUT )
  288. control_input.push_back( p );
  289. else if ( p.type() == Port::CONTROL && p.direction() == Port::OUTPUT )
  290. control_output.push_back( p );
  291. }
  292. int noutputs ( void ) const
  293. {
  294. return audio_output.size();
  295. }
  296. int ninputs ( void ) const
  297. {
  298. return audio_input.size();
  299. }
  300. int ncontrol_inputs ( void ) const
  301. {
  302. return control_input.size();
  303. }
  304. int ncontrol_outputs ( void ) const
  305. {
  306. return control_output.size();
  307. }
  308. int nvisible_control_inputs ( void ) const
  309. {
  310. int n = 0;
  311. for ( std::vector<Port>::const_iterator i = control_input.begin();
  312. i != control_input.end();
  313. i++ )
  314. if ( i->hints.visible )
  315. n++;
  316. return n;
  317. }
  318. virtual bool bypass ( void ) const { return _bypass; }
  319. virtual void bypass ( bool v ) { _bypass = v; redraw(); }
  320. virtual bool bypassable ( void ) const
  321. {
  322. return ninputs() == noutputs() ||
  323. ( ninputs() == 1 && noutputs() == 2 );
  324. }
  325. int control_input_port_index ( Port *p )
  326. {
  327. for ( nframes_t i = control_input.size(); i--; )
  328. if ( &control_input[i] == p )
  329. return i;
  330. return -1;
  331. }
  332. int control_output_port_index ( Port *p )
  333. {
  334. for ( nframes_t i = control_output.size(); i--; )
  335. if ( &control_output[i] == p )
  336. return i;
  337. return -1;
  338. }
  339. Chain *chain ( void ) const { return _chain; }
  340. void chain ( Chain * v );
  341. char *get_parameters ( void ) const;
  342. void set_parameters ( const char * );
  343. void send_feedback ( void );
  344. virtual bool initialize ( void ) { return true; }
  345. /* for the given number of inputs, return how many outputs this
  346. * plugin would have. -1 if this plugin can't support so many
  347. * inputs. */
  348. virtual int can_support_inputs ( int n ) = 0;
  349. /* called by the chain whenever we need to adjust our input
  350. * channel configuration, but only if can_support_inputs() returns
  351. * true */
  352. virtual bool configure_inputs ( int n ) = 0;
  353. virtual void process ( nframes_t ) = 0;
  354. /* called whenever the module is initialized or when the sample rate is changed at runtime */
  355. virtual void handle_sample_rate_change ( nframes_t sample_rate ) {}
  356. /* called whenever the value of a control port is changed.
  357. This can be used to take appropriate action from the GUI thread */
  358. virtual void handle_control_changed ( Port * );
  359. /* called whenever the name of the chain changes (usually because
  360. * the name of the mixer strip changed). */
  361. virtual void handle_chain_name_changed ();
  362. virtual void handle_port_connection_change () {}
  363. #define MODULE_CLONE_FUNC(class) \
  364. virtual Module *clone_empty ( void ) const \
  365. { \
  366. return new class (); \
  367. }
  368. virtual Module *clone_empty ( void ) const { return NULL; }
  369. Module *clone ( Chain *dest ) const;
  370. Module *clone ( void ) const;
  371. protected:
  372. nframes_t sample_rate ( void ) const { return Module::_sample_rate; }
  373. void draw_connections ( void );
  374. void draw_label ( int X, int Y, int W, int H );
  375. void draw_box ( int X, int Y, int W, int H );
  376. virtual void draw ( void ) { Module::draw_box(x(),y(),w(),h()); Module::draw_label(x(),y(),w(),h()); }
  377. virtual int handle ( int m );
  378. virtual void get ( Log_Entry &e ) const;
  379. virtual void set ( Log_Entry &e );
  380. public:
  381. static void set_sample_rate ( nframes_t srate ) { _sample_rate = srate; }
  382. void command_open_parameter_editor();
  383. virtual void command_activate ( void );
  384. virtual void command_deactivate ( void );
  385. virtual void command_remove ( void );
  386. };