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.

435 lines
12KB

  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 <stdio.h>
  20. #include <unistd.h>
  21. #include <math.h>
  22. /* jack */
  23. #include <jack/jack.h>
  24. #include <jack/midiport.h>
  25. #include <jack/ringbuffer.h>
  26. #include <jack/thread.h>
  27. #include "non.H"
  28. #include "transport.H"
  29. #include "pattern.H"
  30. #include "phrase.H"
  31. #include "event_list.H"
  32. #ifdef JACK_MIDI_PROTO_API
  33. /* correct for prototype version of API */
  34. #define jack_midi_event_reserve( p, f, l ) jack_midi_event_reserve( p, f, l, nframes )
  35. #define jack_midi_event_get( e, b, f ) jack_midi_event_get( e, b, f, nframes )
  36. #define jack_midi_get_event_count( b ) jack_midi_get_event_count( b, nframes )
  37. #define jack_midi_clear_buffer( b ) jack_midi_clear_buffer( b, nframes )
  38. #define jack_midi_event_write( b, f, d, s ) jack_midi_event_write( b, f, d, s, nframes )
  39. #endif
  40. jack_client_t *client;
  41. int sample_rate;
  42. const int MAX_PORT = 16;
  43. /* timers for notes on all channels and ports. When a note is played,
  44. * the respective value in this array is set to the note duraction in
  45. * ticks. Decremented in each process cycle, when this value reaches
  46. * zero, a note off is generated--regardless of the state of the
  47. * transport */
  48. double notes_on[MAX_PORT][16][128];
  49. typedef unsigned char byte_t;
  50. int num_output_ports = 1;
  51. event_list freelist;
  52. typedef struct {
  53. void *buf;
  54. jack_ringbuffer_t *ring_buf; /* for realtime output and recording */
  55. event_list events; /* events to be output this cycle */
  56. jack_port_t *port;
  57. } port_t;
  58. static port_t output[MAX_PORT];
  59. static port_t input[2]; /* control, performance */
  60. jack_nframes_t nframes; /* for compatibility with older jack */
  61. /** get next recorded event, if any--runs in UI thread */
  62. midievent *
  63. midi_input_event ( int port )
  64. {
  65. if ( jack_ringbuffer_read_space( input[ port ].ring_buf ) >= sizeof( midievent ) )
  66. {
  67. midievent *me = new midievent;
  68. // MESSAGE( "passing midi input to non-RT thread" );
  69. if ( jack_ringbuffer_read( input[ port ].ring_buf, (char *)me, sizeof( midievent ) ) )
  70. return me;
  71. }
  72. return NULL;
  73. }
  74. /**
  75. * Queue an event for output. /tick/ is relative to the current cycle! */
  76. void
  77. midi_output_event ( int port, const midievent *e )
  78. {
  79. event *fe = freelist.first();
  80. if ( ! fe )
  81. WARNING( "output buffer underrun" );
  82. else
  83. {
  84. freelist.unlink( fe );
  85. *fe = *e;
  86. output[ port ].events.insert( fe );
  87. }
  88. }
  89. /** same as above, but only for note-on + duration */
  90. void
  91. midi_output_event ( int port, const midievent *e, tick_t duration )
  92. {
  93. if ( duration )
  94. {
  95. if ( notes_on[ port ][ e->channel() ][ e->note() ] > transport.ticks_per_period )
  96. WARNING( "duplicate note on?" );
  97. else
  98. {
  99. notes_on[ port ][ e->channel() ][ e->note() ] = (duration + 0.0001f) + (e->timestamp());
  100. midi_output_event( port, e );
  101. }
  102. }
  103. else
  104. {
  105. /* if ( notes_on[ port ][ e->channel() ][ e->note() ] ) */
  106. /* WARNING( "note still on when note-off came" ); */
  107. }
  108. }
  109. void
  110. midi_write_event ( int port, const midievent *e )
  111. {
  112. byte_t *buffer;
  113. // what I want here is to translate a PPQN tick into the
  114. // current period.
  115. jack_nframes_t frame = transport.frames_per_tick * e->timestamp();
  116. int l = e->size();
  117. buffer = jack_midi_event_reserve( output[ port ].buf, frame, l );
  118. if ( ! buffer )
  119. {
  120. WARNING( "could not reserve buffer at frame %d, note event dropped!", frame );
  121. return;
  122. }
  123. #ifndef NDEBUG
  124. e->pretty_print();
  125. #endif
  126. e->raw( buffer, l );
  127. }
  128. /** Call this to send an event immediately from UI thread. Timestamp is meaningless */
  129. void
  130. midi_output_immediate_event ( int port, const midievent *e )
  131. {
  132. if ( jack_ringbuffer_write( output[ port ].ring_buf, (const char *)e, sizeof( midievent ) ) != sizeof( midievent ) )
  133. WARNING( "output ringbuffer overrun" );
  134. else
  135. if ( e->is_note_on() )
  136. {
  137. /* use timestamp as duration */
  138. notes_on[ port ][ e->channel() ][ e->note() ] = e->timestamp();
  139. }
  140. }
  141. /** stop all notes on all channels of all ports */
  142. void
  143. midi_all_sound_off ( void )
  144. {
  145. MESSAGE( "stopping all sound" );
  146. midievent e;
  147. /* all notes off */
  148. e.status( midievent::CONTROL_CHANGE );
  149. e.data( 123, 0 );
  150. e.timestamp( 0 );
  151. for ( int p = MAX_PORT; p--; )
  152. for ( int c = 16; c--; )
  153. {
  154. e.channel( c );
  155. midi_output_immediate_event( p, &e );
  156. }
  157. }
  158. static int
  159. process ( jack_nframes_t nframes, void *arg )
  160. {
  161. static tick_t oph = 0;
  162. static tick_t onph = 0;
  163. static int old_play_mode = PATTERN;
  164. ::nframes = nframes;
  165. // init all port buffers (maybe we should only do this as needed)
  166. /* loop over stuff */
  167. transport.nframes = nframes;
  168. transport.poll();
  169. tick_t ph = trunc( transport.ticks );
  170. tick_t nph = trunc( transport.ticks + transport.ticks_per_period );
  171. if ( ! transport.valid )
  172. goto schedule;
  173. if ( ( ! transport.rolling ) || ph == oph )
  174. goto schedule;
  175. if ( ph != onph )
  176. WARNING( "dropped ticks" );
  177. onph = nph;
  178. // MESSAGE( "tpp %f %f-%f", transport.ticks_per_period, ph, nph );
  179. // MESSAGE( "tpp %f %lu-%lu", transport.ticks_per_period, ph, nph );
  180. switch ( old_play_mode )
  181. {
  182. case PATTERN:
  183. case TRIGGER:
  184. {
  185. // stop all patterns.
  186. for ( uint i = pattern::patterns(); i--; )
  187. {
  188. pattern *p = pattern::pattern_by_number( i + 1 );
  189. p->stop();
  190. }
  191. break;
  192. }
  193. }
  194. switch ( song.play_mode )
  195. {
  196. case SEQUENCE:
  197. // first handle patterns already playing
  198. for ( uint i = pattern::patterns(); i--; )
  199. {
  200. pattern *p = pattern::pattern_by_number( i + 1 );
  201. if ( p && p->playing() )
  202. p->play( ph, nph );
  203. }
  204. playlist->play( ph, nph );
  205. break;
  206. case PATTERN:
  207. case TRIGGER:
  208. {
  209. for ( uint i = pattern::patterns(); i--; )
  210. {
  211. pattern *p = pattern::pattern_by_number( i + 1 );
  212. p->trigger( 0, -1 );
  213. p->play( ph, nph );
  214. }
  215. break;
  216. }
  217. }
  218. old_play_mode = song.play_mode;
  219. oph = ph;
  220. /* handle midi input */
  221. for ( int j = transport.recording ? 2 : 1; j--; )
  222. {
  223. static midievent e;
  224. input[j].buf = jack_port_get_buffer( input[j].port, nframes );
  225. jack_midi_event_t ev;
  226. jack_nframes_t count = jack_midi_get_event_count( input[j].buf );
  227. for ( uint i = 0; i < count; ++i )
  228. {
  229. // MESSAGE( "Got midi input!" );
  230. jack_midi_event_get( &ev, input[j].buf, i );
  231. /* time is frame within cycle, convert to absolute tick */
  232. e.timestamp( ph + (ev.time / transport.frames_per_tick) );
  233. e.status( ev.buffer[0] );
  234. e.lsb( ev.buffer[1] );
  235. if ( ev.size == 3 )
  236. e.msb( ev.buffer[2] );
  237. if ( jack_ringbuffer_write( input[j].ring_buf, (char*)&e, sizeof( midievent ) ) != sizeof( midievent ) )
  238. WARNING( "input buffer overrun" );
  239. }
  240. }
  241. schedule:
  242. for ( int i = MAX_PORT; i-- ; )
  243. {
  244. /* reserve and clear buffers */
  245. output[ i ].buf = jack_port_get_buffer( output[ i ].port, nframes );
  246. jack_midi_clear_buffer( output[ i ].buf );
  247. /* handle scheduled note offs */
  248. for ( uint j = 16; j-- ; )
  249. {
  250. register double *note = &notes_on[ i ][ j ][ 0 ];
  251. for ( register uint k = 0; k < 128; note++, k++ )
  252. if ( *note )
  253. if ( ( *note -= transport.ticks_per_period ) <= 0 )
  254. {
  255. static midievent e;
  256. e.status( midievent::NOTE_OFF );
  257. e.channel( j );
  258. e.note( k );
  259. e.note_velocity( 64 );
  260. e.timestamp( ((tick_t)transport.ticks_per_period) + *note );
  261. *note = 0;
  262. midi_output_event( i, &e );
  263. }
  264. }
  265. static midievent e;
  266. /* first, write any immediate events from the UI thread */
  267. while ( jack_ringbuffer_read( output[ i ].ring_buf, (char *)&e, sizeof( midievent ) ) )
  268. {
  269. // MESSAGE( "sending immediate event" );
  270. // FIXME: could we do better?
  271. e.timestamp( 0 );
  272. midi_output_event( i, &e );
  273. }
  274. /* Write queued events */
  275. event *n;
  276. for ( event *e = output[ i ].events.first(); e; e = n )
  277. {
  278. n = e->next();
  279. midi_write_event( i, e );
  280. output[ i ].events.unlink( e );
  281. freelist.append( e );
  282. }
  283. }
  284. return 0;
  285. }
  286. int
  287. midi_init ( void )
  288. {
  289. MESSAGE( "Initializing Jack MIDI" );
  290. if (( client = jack_client_new ( APP_NAME )) == 0 )
  291. return 0;
  292. /* create output ports */
  293. for ( int i = 0; i < MAX_PORT; i++ )
  294. {
  295. char pat[40];
  296. sprintf( pat, "midi_out-%d", i + 1 );
  297. output[i].port = jack_port_register( client, pat, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0 );
  298. output[i].ring_buf = jack_ringbuffer_create( 16 * 16 * sizeof( midievent ) ); // why this value?
  299. jack_ringbuffer_reset( output[i].ring_buf );
  300. }
  301. /* create input ports */
  302. input[0].port = jack_port_register( client, "control_in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0 );
  303. input[0].ring_buf = jack_ringbuffer_create( 128 * sizeof( midievent ) ); // why this value?
  304. jack_ringbuffer_reset( input[0].ring_buf );
  305. input[1].port = jack_port_register( client, "midi_in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0 );
  306. input[1].ring_buf = jack_ringbuffer_create( 128 * sizeof( midievent ) ); // why this value?
  307. jack_ringbuffer_reset( input[1].ring_buf );
  308. /* preallocate events */
  309. for ( int i = 32 * 16 * MAX_PORT; i-- ; )
  310. freelist.append( new event );
  311. DEBUG( "allocated output buffer space for %lu events", freelist.size() );
  312. /* clear notes */
  313. for ( int p = MAX_PORT; p--; )
  314. for ( int c = 16; c-- ; )
  315. for ( int n = 128; n-- ; )
  316. notes_on[ p ][ c ][ n ] = 0;
  317. //1 jack_set_buffer_size_callback( client, bufsize, 0 );
  318. jack_set_process_callback( client, process, 0 );
  319. jack_activate( client );
  320. sample_rate = jack_get_sample_rate( client );
  321. /* /\* initialize buffer size *\/ */
  322. /* transport_poll(); */
  323. /* bufsize( jack_get_buffer_size( client ), 0 ); */
  324. if ( jack_set_timebase_callback( client, 1, Transport::timebase, NULL ) == 0 )
  325. {
  326. MESSAGE( "running as timebase master" );
  327. transport.master = true;
  328. }
  329. else
  330. WARNING( "could not take over as timebase master" );
  331. return 1;
  332. }
  333. void
  334. midi_shutdown ( void )
  335. {
  336. // TODO: wait for all queued events to play.
  337. jack_deactivate( client );
  338. }