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.

740 lines
15KB

  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 "pattern.H"
  19. #include "non.H"
  20. #include "common.h"
  21. #include "smf.H"
  22. #include "jack.H"
  23. #include "transport.H"
  24. #include <math.h>
  25. #include <MIDI/event_list.H>
  26. using namespace MIDI;
  27. event_list pattern::_recorded_events;
  28. vector <pattern*> pattern::_patterns;
  29. int pattern::_solo;
  30. int pattern::_pattern_recording;
  31. signal <void> pattern::signal_create_destroy;
  32. pattern::pattern ( void )
  33. {
  34. viewport.h = 32;
  35. viewport.w = 32;
  36. viewport.y = 64 - ( viewport.h + ( viewport.h / 2 ) );
  37. _channel = _port = 0;
  38. _ppqn = 4;
  39. _bpb = 4;
  40. _note = 8;
  41. _queued = -1;
  42. int _bars = 2;
  43. // we need to reinitalize this.
  44. data *d = const_cast< data * >( _rd );
  45. d->length = x_to_ts( _bpb * _ppqn * _bars );
  46. // mapping.open( Mapping::INSTRUMENT, "Default" );
  47. mapping.open( Mapping::SCALE, "Major" );
  48. _add();
  49. char *s;
  50. asprintf( &s, "Pattern %d", number() );
  51. name( s );
  52. }
  53. void
  54. pattern::_add ( void )
  55. {
  56. // keep track of all the patterns
  57. pattern::_patterns.push_back( this );
  58. _number = patterns();
  59. signal_create_destroy();
  60. }
  61. pattern::~pattern ( void )
  62. {
  63. DMESSAGE( "deleting pattern %d", number() );
  64. signal_create_destroy();
  65. }
  66. /* copy constructor */
  67. pattern::pattern ( const pattern &rhs ) : Grid( rhs )
  68. {
  69. _note = rhs._note;
  70. _port = rhs._port;
  71. _channel = rhs._channel;
  72. _queued = rhs._queued;
  73. mapping = rhs.mapping;
  74. _add();
  75. }
  76. pattern *
  77. pattern::clone ( void )
  78. {
  79. return new pattern( *this );
  80. }
  81. /******************/
  82. /* Static methods */
  83. /******************/
  84. int
  85. pattern::solo ( void )
  86. {
  87. return pattern::_solo;
  88. }
  89. int
  90. pattern::patterns ( void )
  91. {
  92. return pattern::_patterns.size();
  93. }
  94. // this is the static one
  95. pattern *
  96. pattern::pattern_by_number ( int n )
  97. {
  98. if ( n <= patterns() && n > 0 )
  99. {
  100. return pattern::_patterns[ n - 1 ];
  101. }
  102. return NULL;
  103. }
  104. /** delete all patterns in preparation for a reload */
  105. void
  106. pattern::reset ( void )
  107. {
  108. for ( int n = pattern::patterns(); n-- ; )
  109. {
  110. delete pattern::_patterns.back();
  111. pattern::_patterns.pop_back();
  112. }
  113. }
  114. /* runs in the UI thread */
  115. /* records a MIDI event into a temporary buffer. It'll only be
  116. * permanently added to pattern after recording stops or the pattern
  117. * loops. */
  118. void
  119. pattern::record_event ( const midievent *me )
  120. {
  121. /* set the events aside in a dedicated list--the recording pattern
  122. * will decide what to do with them the next time around the
  123. * loop */
  124. /* FIXME: how does the pattern decide when to loop? It seems
  125. reasonable that /merge/ and /replace/ modes should be bound to
  126. the previous pattern length, but what about "NEW" mode? Should it
  127. just use this entire list as a new pattern (of whatever length)
  128. when recording is halted? */
  129. event *e = new event;
  130. *e = *me;
  131. pattern::_recorded_events.append( e );
  132. record_mode_e mode = config.record_mode;
  133. if ( mode == OVERWRITE || mode == LAYER )
  134. {
  135. pattern *p = pattern::recording();
  136. if ( ! p->_cleared )
  137. {
  138. if ( mode == LAYER )
  139. {
  140. p->record_stop();
  141. p = p->clone();
  142. p->record( 0 );
  143. }
  144. p->clear();
  145. p->_cleared = true;
  146. }
  147. mode = MERGE;
  148. }
  149. /* let's fill in the pattern 'live' in merge mode. looks a little
  150. complicated because we have to wait for a note-off before it's
  151. safe to insert */
  152. if ( mode == MERGE || mode == NEW )
  153. {
  154. pattern *p = pattern::recording();
  155. p->lock();
  156. event_list *el = &pattern::_recorded_events;
  157. if ( e->is_note_off() )
  158. {
  159. event *off = e;
  160. for ( event *on = el->last(); on; on = on->prev() )
  161. {
  162. if ( on->is_note_on() &&
  163. on->is_same_note( off ) )
  164. // &&
  165. // *on < *e )
  166. {
  167. el->unlink( on );
  168. el->unlink( off );
  169. tick_t duration = off->timestamp() - on->timestamp();
  170. /* place within loop */
  171. on->timestamp(
  172. fmod( on->timestamp() - p->_start, p->_rw->length ) );
  173. on->link( off );
  174. on->note_duration( duration );
  175. p->_rw->events.mix( on );
  176. break;
  177. }
  178. }
  179. }
  180. else
  181. if ( ! e->is_note_on() )
  182. {
  183. // if ( ! filter )
  184. e->timestamp( fmod( e->timestamp(), p->_rw->length ) );
  185. el->unlink( e );
  186. p->_rw->events.insert( e );
  187. }
  188. p->unlock();
  189. }
  190. }
  191. pattern *
  192. pattern::recording ( void )
  193. {
  194. return pattern::pattern_by_number( pattern::_pattern_recording );
  195. }
  196. /*******************/
  197. /* Virtual Methods */
  198. /*******************/
  199. /* allows us to create a new pattern/phrase from a base class pointer */
  200. pattern *
  201. pattern::create ( void )
  202. {
  203. if ( pattern::patterns() < 128 )
  204. {
  205. return new pattern;
  206. }
  207. else
  208. return NULL;
  209. }
  210. pattern *
  211. pattern::by_number ( int n ) const
  212. {
  213. return pattern::pattern_by_number( n );
  214. }
  215. /** what to do when the row name is pressed */
  216. void
  217. pattern::row_name_press ( int y )
  218. {
  219. /* echo note */
  220. midievent e;
  221. e.status( event::NOTE_ON );
  222. e.channel( _channel );
  223. e.timestamp( default_length() );
  224. e.note( y );
  225. e.note_velocity( 64 );
  226. midi_output_immediate_event ( _port, &e );
  227. }
  228. void
  229. pattern::put ( int x, int y, tick_t l )
  230. {
  231. l = l ? l : default_length();
  232. Grid::put( x, y, l );
  233. if ( ! transport.rolling )
  234. {
  235. /* echo note */
  236. midievent e;
  237. e.status( event::NOTE_ON );
  238. e.channel( _channel );
  239. e.timestamp( l );
  240. e.note( y_to_note( y ) );
  241. e.note_velocity( 64 );
  242. midi_output_immediate_event ( _port, &e );
  243. }
  244. }
  245. const char *
  246. pattern::row_name ( int r ) const
  247. {
  248. return mapping.note_name( y_to_note( r ) );
  249. }
  250. void
  251. pattern::draw_row_names ( Canvas *c ) const
  252. {
  253. for ( int y = 128; y-- ; )
  254. c->draw_row_name( y, mapping.note_name( y_to_note( y ) ), mapping.velocity( y_to_note( y ) ) );
  255. }
  256. void
  257. pattern::trigger ( tick_t start, tick_t end )
  258. {
  259. /* ASSERT( end != -1 && start <= end, "programming error: invalid loop trigger! (%lu-%lu)", start, end ); */
  260. _start = start;
  261. _end = end;
  262. }
  263. /* trigger forever */
  264. void
  265. pattern::trigger ( void )
  266. {
  267. trigger( transport.frame / transport.frames_per_tick, INFINITY );
  268. }
  269. void
  270. pattern::stop ( void ) const
  271. {
  272. _playing = false;
  273. _start = 0;
  274. _end = 0;
  275. _index = 0;
  276. }
  277. void
  278. pattern::mode ( int n )
  279. {
  280. if ( QUEUE == song.play_mode )
  281. {
  282. queue( n );
  283. return;
  284. }
  285. if ( n == SOLO )
  286. {
  287. if ( pattern::_solo )
  288. ((Grid*)pattern::pattern_by_number( pattern::_solo ))->mode( PLAY );
  289. pattern::_solo = _number;
  290. Grid::mode( SOLO );
  291. }
  292. else
  293. {
  294. if ( pattern::_solo == _number )
  295. pattern::_solo = 0;
  296. Grid::mode( n );
  297. }
  298. }
  299. int
  300. pattern::mode ( void ) const
  301. {
  302. if ( pattern::_solo )
  303. {
  304. if ( pattern::_solo == _number )
  305. return SOLO;
  306. else
  307. return MUTE;
  308. }
  309. else
  310. return Grid::mode();
  311. }
  312. /* queue a mode change for the next loop */
  313. void
  314. pattern::queue ( int m )
  315. {
  316. _queued = m;
  317. }
  318. int
  319. pattern::queue ( void ) const
  320. {
  321. return _queued;
  322. }
  323. /* WARNING: runs in the RT thread! */
  324. // output notes from /start/ to /end/ (absolute)
  325. void
  326. pattern::play ( tick_t start, tick_t end ) const
  327. {
  328. /* get our own copy of this pointer so UI thread can change it. */
  329. const data *d = const_cast< const data * >(_rd);
  330. if ( start > _end )
  331. {
  332. stop();
  333. // WARNING( "attempt to play a loop (pattern %d) that has ended (%lu, %lu)", number(), start, _end );
  334. return;
  335. }
  336. else
  337. if ( end < _start )
  338. // not ready yet
  339. return;
  340. if ( start < _start )
  341. start = _start;
  342. if ( end > _end )
  343. end = _end;
  344. // where we are in the absolute time
  345. tick_t tick = start - _start;
  346. int num_played = tick / d->length;
  347. tick_t offset = _start + (d->length * num_played);
  348. const event *e;
  349. _index = fmod( tick, d->length );
  350. bool reset_queued = false;
  351. if ( _index < end - start )
  352. {
  353. /* period covers the beginning of the loop */
  354. DMESSAGE( "%s pattern %d at tick %f (ls: %f, le: %f, o: %f)", _playing ? "Looped" : "Triggered", number(), start, _start, _end, offset );
  355. _cleared = false;
  356. if ( PLAY == _queued || SOLO == _queued )
  357. {
  358. /* set the start point to loop boundary */
  359. start = start - _index;
  360. _mode = _queued;
  361. if ( SOLO == _mode )
  362. {
  363. if ( pattern::_solo )
  364. ((Grid*)pattern::pattern_by_number( pattern::_solo ))->mode( PLAY );
  365. pattern::_solo = _number;
  366. }
  367. else
  368. {
  369. if ( pattern::_solo == _number )
  370. pattern::_solo = 0;
  371. }
  372. reset_queued = true;
  373. }
  374. }
  375. else if ( _index >= d->length - ( end - start ) )
  376. {
  377. if ( MUTE == _queued )
  378. {
  379. /* set the end point to loop boundary */
  380. end = end - _index;
  381. reset_queued = true;
  382. }
  383. }
  384. _playing = true;
  385. if ( mode() == MUTE )
  386. goto done;
  387. try_again:
  388. // pattern is empty
  389. if ( d->events.empty() )
  390. goto done;
  391. for ( e = d->events.first(); e; e = e->next() )
  392. {
  393. // MESSAGE( "s[%ld] -> t[%ld] : %ld, len %ld", start, end, e->timestamp(), _length ); // (*e).print();
  394. tick_t ts = e->timestamp() + offset;
  395. if ( ts >= end )
  396. goto done;
  397. if ( ts >= start )
  398. {
  399. midievent me = *e;
  400. // MESSAGE( "timestamp %d, tick %d, ts - start == %lu", e->timestamp(), start,
  401. // e->timestamp() - start);
  402. /* set the channel */
  403. me.channel( _channel );
  404. /* set the in-cycle timestamp */
  405. me.timestamp ( ts - start );
  406. if ( me.is_note_on() )
  407. {
  408. if ( mapping.translate( &me ) )
  409. midi_output_event( _port, &me, 1 + e->note_duration() );
  410. }
  411. else
  412. if ( me.is_note_off() )
  413. {
  414. if ( mapping.translate( &me ) )
  415. midi_output_event( _port, &me, 0 );
  416. }
  417. else
  418. /* any other event type */
  419. midi_output_event( _port, &me );
  420. }
  421. }
  422. // ran out of events, but there's still some loop left to play.
  423. offset += d->length;
  424. goto try_again;
  425. DMESSAGE( "out of events, resetting to satisfy loop" );
  426. done:
  427. if ( _queued >= 0 && reset_queued )
  428. {
  429. _mode = _queued;
  430. _queued = -1;
  431. }
  432. if ( _end == end )
  433. {
  434. /* we're done playing this trigger */
  435. DMESSAGE( "Pattern %d ended at tick %f (ls: %f, le: %f, o: %f)", number(), end, _start, _end, offset );
  436. stop();
  437. }
  438. }
  439. /* Import /track/ of /f/ as new pattern */
  440. pattern *
  441. pattern::import ( smf *f, int track )
  442. {
  443. if ( ! f->seek_track( track ) )
  444. return NULL;
  445. pattern *p = new pattern;
  446. p->lock();
  447. p->load( f );
  448. /* file could have any notes in it... Use Chromatic scale to
  449. ensure all are visible */
  450. p->mapping.open( Mapping::SCALE, "Chromatic" );
  451. p->unlock();
  452. p->fit();
  453. return p;
  454. }
  455. /** fill pattern from current track of /f/ */
  456. void
  457. pattern::load ( smf *f )
  458. {
  459. lock();
  460. f->read_pattern_info( this );
  461. tick_t len;
  462. list <midievent> *e = f->read_track_events( &len );
  463. /* set channel to channel of first event... */
  464. if ( e->size() )
  465. _channel = e->front().channel();
  466. /* copy events into pattern */
  467. _rw->events = *e;
  468. delete e;
  469. if ( len )
  470. _rw->length = len;
  471. unlock();
  472. // print();
  473. }
  474. /** save (export) pattern to file /name/ */
  475. void
  476. pattern::save ( const char *name ) const
  477. {
  478. smf f;
  479. /* open for writing */
  480. f.open( name, smf::WRITE );
  481. /* writing SMF 0 track */
  482. f.write_header( 0 );
  483. f.open_track( _name, _number );
  484. Grid::dump( &f, _channel );
  485. f.close_track( length() );
  486. }
  487. /** dump pattern as a track in an already open MIDI file */
  488. void
  489. pattern::dump ( smf *f ) const
  490. {
  491. f->open_track( _name, _number );
  492. f->write_pattern_info( this );
  493. Grid::dump( f, _channel );
  494. f->close_track( length() );
  495. }
  496. /*************/
  497. /* Recording */
  498. /*************/
  499. void
  500. pattern::record ( int mode )
  501. {
  502. _recording = true;
  503. pattern::_pattern_recording = _number;
  504. }
  505. void
  506. pattern::record_stop ( void )
  507. {
  508. if ( ! _recording )
  509. return;
  510. _recording = false;
  511. if ( config.record_mode == NEW )
  512. trim();
  513. pattern::_recorded_events.clear();
  514. }
  515. /*******************************/
  516. /* Pattern specific accessors. */
  517. /*******************************/
  518. int
  519. pattern::port ( void ) const
  520. {
  521. return _port;
  522. }
  523. void
  524. pattern::port ( int p )
  525. {
  526. _port = p;
  527. }
  528. int
  529. pattern::channel ( void ) const
  530. {
  531. return _channel;
  532. }
  533. void
  534. pattern::channel ( int c )
  535. {
  536. _channel = c;
  537. }
  538. int
  539. pattern::note ( void ) const
  540. {
  541. return _note;
  542. }
  543. void
  544. pattern::note ( int n )
  545. {
  546. _note = n;
  547. }
  548. int
  549. pattern::ppqn ( void ) const
  550. {
  551. return _ppqn;
  552. }
  553. void
  554. pattern::ppqn ( int n )
  555. {
  556. _ppqn = n;
  557. }
  558. int
  559. pattern::key ( void ) const
  560. {
  561. return mapping.key();
  562. }
  563. void
  564. pattern::key ( int k )
  565. {
  566. mapping.key( k );
  567. }