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.

472 lines
14KB

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