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.

686 lines
13KB

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