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.

309 lines
7.0KB

  1. /**********************************************************************************/
  2. /* Copyright (C) 2007,2008 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. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include "non.H"
  23. // #include "gui/input.H"
  24. #include "gui/ui.H"
  25. #include "jack.H"
  26. #include "NSM.H"
  27. #include "pattern.H"
  28. #include "phrase.H"
  29. #ifdef HAVE_XPM
  30. #include "FL/Fl.H"
  31. #include "FL/x.H"
  32. #include <X11/xpm.h>
  33. #include "../icons/icon-16x16.xpm"
  34. #endif
  35. extern const char *BUILD_ID;
  36. extern const char *VERSION;
  37. const double NSM_CHECK_INTERVAL = 0.25f;
  38. Canvas *pattern_c, *phrase_c, *trigger_c;
  39. sequence *playlist;
  40. global_settings config;
  41. song_settings song;
  42. NSM_Client *nsm;
  43. char *instance_name;
  44. /* default to pattern mode */
  45. UI *ui;
  46. void
  47. quit ( void )
  48. {
  49. /* clean up, only for valgrind's sake */
  50. delete ui;
  51. delete pattern_c;
  52. delete phrase_c;
  53. delete trigger_c;
  54. midi_all_sound_off();
  55. // wait for it...
  56. sleep( 1 );
  57. midi_shutdown();
  58. MESSAGE( "Your fun is over" );
  59. exit( 0 );
  60. }
  61. void
  62. clear_song ( void )
  63. {
  64. // song.filename = NULL;
  65. pattern_c->grid( NULL );
  66. phrase_c->grid( NULL );
  67. playlist->reset();
  68. playlist->insert( 0, 1 );
  69. pattern_c->grid( new pattern );
  70. phrase_c->grid( new phrase );
  71. song.dirty( false );
  72. }
  73. void
  74. init_song ( void )
  75. {
  76. if ( ! midi_is_active() )
  77. setup_jack();
  78. if ( !( nsm && nsm->is_active() ) )
  79. song.filename = NULL;
  80. clear_song();
  81. if ( nsm && nsm->is_active() )
  82. save_song( song.filename );
  83. }
  84. void
  85. handle_midi_input ( void )
  86. {
  87. midievent e;
  88. while ( ( midi_input_event( PERFORMANCE, &e ) ) )
  89. {
  90. pattern::record_event( &e );
  91. }
  92. }
  93. bool
  94. load_song ( const char *name )
  95. {
  96. if ( ! midi_is_active() )
  97. setup_jack();
  98. MESSAGE( "loading song \"%s\"", name );
  99. Grid *pattern_grid = pattern_c->grid();
  100. Grid *phrase_grid = phrase_c->grid();
  101. pattern_c->grid( NULL );
  102. phrase_c->grid( NULL );
  103. if ( ! playlist->load( name ) )
  104. {
  105. WARNING( "failed to load song file" );
  106. goto failed;
  107. }
  108. pattern_c->grid( pattern::pattern_by_number( 1 ) );
  109. phrase_c->grid( phrase::phrase_by_number( 1 ) );
  110. song.filename = strdup( name );
  111. song.dirty( false );
  112. return true;
  113. failed:
  114. pattern_c->grid( pattern_grid );
  115. phrase_c->grid( phrase_grid );
  116. return false;
  117. }
  118. bool
  119. save_song ( const char *name )
  120. {
  121. playlist->save( name );
  122. song.filename = strdup( name );
  123. song.dirty( false );
  124. return true;
  125. }
  126. void
  127. setup_jack ( )
  128. {
  129. const char *jack_name;
  130. jack_name = midi_init( instance_name );
  131. if ( ! jack_name )
  132. ASSERTION( "Could not initialize MIDI system! (is Jack running and with MIDI ports enabled?)" );
  133. if ( ! transport.valid )
  134. {
  135. if ( transport.master )
  136. ASSERTION( "The version of JACK you are using does not appear to be capable of passing BBT positional information." );
  137. else
  138. ASSERTION( "Either the version of JACK you are using does pass BBT information, or the current timebase master does not provide it." );
  139. }
  140. }
  141. static int got_sigterm = 0;
  142. void
  143. sigterm_handler ( int )
  144. {
  145. got_sigterm = 1;
  146. Fl::awake();
  147. }
  148. void
  149. check_sigterm ( void * )
  150. {
  151. if ( got_sigterm )
  152. {
  153. MESSAGE( "Got SIGTERM, quitting..." );
  154. quit();
  155. }
  156. }
  157. void
  158. check_nsm ( void * v )
  159. {
  160. nsm->check();
  161. Fl::repeat_timeout( NSM_CHECK_INTERVAL, check_nsm, v );
  162. }
  163. int
  164. main ( int argc, char **argv )
  165. {
  166. #ifdef HAVE_XPM
  167. fl_open_display();
  168. Pixmap p, mask;
  169. XpmCreatePixmapFromData(fl_display, DefaultRootWindow(fl_display),
  170. (char**)icon_16x16, &p, &mask, NULL);
  171. #endif
  172. config.follow_playhead = true;
  173. config.record_mode = MERGE;
  174. song.play_mode = PATTERN;
  175. song.random.feel = 8;
  176. song.random.probability = 0.33;
  177. asprintf( &config.user_config_dir, "%s/%s", getenv( "HOME" ), USER_CONFIG_DIR );
  178. mkdir( config.user_config_dir, 0777 );
  179. printf( "%s %s %s -- %s\n", APP_TITLE, VERSION, BUILD_ID, COPYRIGHT );
  180. playlist = new sequence;
  181. pattern_c = new Canvas;
  182. phrase_c = new Canvas;
  183. trigger_c = new Canvas;
  184. nsm = new NSM_Client;
  185. song.filename = NULL;
  186. clear_song();
  187. pattern::signal_create_destroy.connect( mem_fun( phrase_c, &Canvas::v_zoom_fit ) );
  188. pattern::signal_create_destroy.connect( mem_fun( song, &song_settings::set_dirty ) );
  189. phrase::signal_create_destroy.connect( mem_fun( song, &song_settings::set_dirty ) );
  190. //
  191. song.dirty( false );
  192. init_colors();
  193. Fl::visual( FL_RGB );
  194. ui = new UI;
  195. #ifdef HAVE_XPM
  196. ui->main_window->icon((char *)p);
  197. #endif
  198. ui->main_window->show( argc, argv );
  199. instance_name = strdup( APP_NAME );
  200. const char *nsm_url = getenv( "NSM_URL" );
  201. if ( nsm_url )
  202. {
  203. if ( ! nsm->init( nsm_url ) )
  204. {
  205. nsm->announce( APP_NAME, ":switch:dirty:", argv[0] );
  206. song.signal_dirty.connect( sigc::mem_fun( nsm, &NSM_Client::is_dirty ) );
  207. song.signal_clean.connect( sigc::mem_fun( nsm, &NSM_Client::is_clean ) );
  208. // poll so we can keep OSC handlers running in the GUI thread and avoid extra sync
  209. Fl::add_timeout( NSM_CHECK_INTERVAL, check_nsm, NULL );
  210. }
  211. else
  212. WARNING( "Error initializing NSM" );
  213. }
  214. else
  215. {
  216. setup_jack();
  217. if ( argc > 1 )
  218. {
  219. /* maybe a filename on the commandline */
  220. if ( ! load_song( argv[ 1 ] ) )
  221. ASSERTION( "Could not load song \"%s\" specified on command line", argv[ 1 ] );
  222. }
  223. }
  224. MESSAGE( "Initializing GUI" );
  225. Fl::add_check( check_sigterm );
  226. ui->run();
  227. return 0;
  228. }