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.

695 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 "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. DMESSAGE( "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. ASSERT( start <= end, "programming error: invalid loop trigger! (%lu-%lu)", start, end );
  233. _start = start;
  234. _end = end;
  235. _index = 0;
  236. }
  237. void
  238. pattern::stop ( void ) const
  239. {
  240. _playing = false;
  241. _start = 0;
  242. _end = 0;
  243. _index = 0;
  244. }
  245. void
  246. pattern::mode ( int n )
  247. {
  248. if ( song.play_mode == TRIGGER )
  249. {
  250. switch ( n )
  251. {
  252. case PLAY:
  253. _triggered = true;
  254. break;
  255. case MUTE:
  256. _triggered = false;
  257. break;
  258. }
  259. return;
  260. }
  261. if ( n == SOLO )
  262. {
  263. if ( pattern::_solo )
  264. ((Grid*)pattern::pattern_by_number( pattern::_solo ))->mode( PLAY );
  265. pattern::_solo = _number;
  266. Grid::mode( SOLO );
  267. }
  268. else
  269. {
  270. if ( pattern::_solo == _number )
  271. pattern::_solo = 0;
  272. Grid::mode( n );
  273. }
  274. }
  275. int
  276. pattern::mode ( void ) const
  277. {
  278. if ( song.play_mode == TRIGGER )
  279. {
  280. if ( ! _triggered )
  281. return MUTE;
  282. else
  283. return PLAY;
  284. }
  285. if ( pattern::_solo )
  286. {
  287. if ( pattern::_solo == _number )
  288. return SOLO;
  289. else
  290. return MUTE;
  291. }
  292. else
  293. return Grid::mode();
  294. }
  295. /* WARNING: runs in the RT thread! */
  296. // output notes from /start/ to /end/ (absolute)
  297. void
  298. pattern::play ( tick_t start, tick_t end ) const
  299. {
  300. /* get our own copy of this pointer so UI thread can change it. */
  301. const data *d = const_cast< const data * >(_rd);
  302. if ( start > _end )
  303. {
  304. stop();
  305. WARNING( "attempt to play a loop (pattern %d) that has ended (%lu, %lu)", number(), start, _end );
  306. return;
  307. }
  308. else
  309. if ( end < _start )
  310. // not ready yet
  311. return;
  312. if ( start < _start )
  313. start = _start;
  314. if ( end > _end )
  315. end = _end;
  316. // where we are in the absolute time
  317. tick_t tick = start - _start;
  318. int num_played = tick / d->length;
  319. tick_t offset = _start + (d->length * num_played);
  320. const event *e;
  321. _index = tick % d->length;
  322. if ( _index < end - start )
  323. {
  324. DMESSAGE( "%s pattern %d at tick %lu (ls: %lu, le: %lu, o: %lu)", _playing ? "Looped" : "Triggered", number(), start, _start, _end, offset );
  325. _cleared = false;
  326. }
  327. _playing = true;
  328. if ( mode() == MUTE )
  329. goto done;
  330. try_again:
  331. // pattern is empty
  332. if ( d->events.empty() )
  333. goto done;
  334. for ( e = d->events.first(); e; e = e->next() )
  335. {
  336. // MESSAGE( "s[%ld] -> t[%ld] : %ld, len %ld", start, end, e->timestamp(), _length ); // (*e).print();
  337. tick_t ts = e->timestamp() + offset;
  338. if ( ts >= end )
  339. goto done;
  340. if ( ts >= start )
  341. {
  342. midievent me = *e;
  343. // MESSAGE( "timestamp %d, tick %d, ts - start == %lu", e->timestamp(), start,
  344. // e->timestamp() - start);
  345. /* set the channel */
  346. me.channel( _channel );
  347. /* set the in-cycle timestamp */
  348. me.timestamp ( ts - start );
  349. if ( me.is_note_on() )
  350. {
  351. if ( mapping.translate( &me ) )
  352. midi_output_event( _port, &me, 1 + e->note_duration() );
  353. }
  354. else
  355. if ( me.is_note_off() )
  356. {
  357. if ( mapping.translate( &me ) )
  358. midi_output_event( _port, &me, 0 );
  359. }
  360. else
  361. /* any other event type */
  362. midi_output_event( _port, &me );
  363. }
  364. }
  365. // ran out of events, but there's still some loop left to play.
  366. offset += d->length;
  367. goto try_again;
  368. DMESSAGE( "out of events, resetting to satisfy loop" );
  369. done:
  370. if ( _end == end )
  371. {
  372. /* we're doing playing this trigger */
  373. DMESSAGE( "Pattern %d ended at tick %lu (ls: %lu, le: %lu, o: %lu)", number(), end, _start, _end, offset );
  374. stop();
  375. }
  376. }
  377. /* Import /track/ of /f/ as new pattern */
  378. pattern *
  379. pattern::import ( smf *f, int track )
  380. {
  381. if ( ! f->seek_track( track ) )
  382. return NULL;
  383. pattern *p = new pattern;
  384. p->lock();
  385. p->load( f );
  386. /* file could have any notes in it... Use Chromatic scale to
  387. ensure all are visible */
  388. p->mapping.open( Mapping::SCALE, "Chromatic" );
  389. p->unlock();
  390. p->fit();
  391. return p;
  392. }
  393. /** fill pattern from current track of /f/ */
  394. void
  395. pattern::load ( smf *f )
  396. {
  397. lock();
  398. f->read_pattern_info( this );
  399. tick_t len;
  400. list <midievent> *e = f->read_track_events( &len );
  401. /* set channel to channel of first event... */
  402. if ( e->size() )
  403. _channel = e->front().channel();
  404. /* copy events into pattern */
  405. _rw->events = *e;
  406. delete e;
  407. if ( len )
  408. _rw->length = len;
  409. unlock();
  410. // print();
  411. }
  412. /** save (export) pattern to file /name/ */
  413. void
  414. pattern::save ( const char *name ) const
  415. {
  416. smf f;
  417. /* open for writing */
  418. f.open( name, smf::WRITE );
  419. /* writing SMF 0 track */
  420. f.write_header( 0 );
  421. f.open_track( _name, _number );
  422. Grid::dump( &f, _channel );
  423. f.close_track( length() );
  424. }
  425. /** dump pattern as a track in an already open MIDI file */
  426. void
  427. pattern::dump ( smf *f ) const
  428. {
  429. f->open_track( _name, _number );
  430. f->write_pattern_info( this );
  431. Grid::dump( f, _channel );
  432. f->close_track( length() );
  433. }
  434. void
  435. pattern::randomize_row ( int y, int feel, float probability )
  436. {
  437. lock();
  438. int l = PPQN * 4 / _note;
  439. int bx = ts_to_x( _rw->length - l );
  440. float *p = (float *)alloca( feel * sizeof( float ) );
  441. float prob = probability;
  442. for ( int i = 0; i < feel; i++ )
  443. {
  444. p[i] = prob;
  445. // reduce probability as we move away from center
  446. prob *= 0.5;
  447. }
  448. for ( int x = 0; x < bx; x++ )
  449. {
  450. float r = ((float)rand()) / RAND_MAX;
  451. if ( p[ x % feel ] + r >= 1 )
  452. put( x, y, l );
  453. }
  454. unlock();
  455. }
  456. /*************/
  457. /* Recording */
  458. /*************/
  459. void
  460. pattern::record ( int mode )
  461. {
  462. _recording = true;
  463. pattern::_pattern_recording = _number;
  464. }
  465. void
  466. pattern::record_stop ( void )
  467. {
  468. if ( ! _recording )
  469. return;
  470. _recording = false;
  471. if ( config.record_mode == NEW )
  472. trim();
  473. pattern::_recorded_events.clear();
  474. }
  475. /*******************************/
  476. /* Pattern specific accessors. */
  477. /*******************************/
  478. int
  479. pattern::port ( void ) const
  480. {
  481. return _port;
  482. }
  483. void
  484. pattern::port ( int p )
  485. {
  486. _port = p;
  487. }
  488. int
  489. pattern::channel ( void ) const
  490. {
  491. return _channel;
  492. }
  493. void
  494. pattern::channel ( int c )
  495. {
  496. _channel = c;
  497. }
  498. int
  499. pattern::note ( void ) const
  500. {
  501. return _note;
  502. }
  503. void
  504. pattern::note ( int n )
  505. {
  506. _note = n;
  507. }
  508. int
  509. pattern::ppqn ( void ) const
  510. {
  511. return _ppqn;
  512. }
  513. void
  514. pattern::ppqn ( int n )
  515. {
  516. _ppqn = n;
  517. }
  518. int
  519. pattern::key ( void ) const
  520. {
  521. return mapping.key();
  522. }
  523. void
  524. pattern::key ( int k )
  525. {
  526. mapping.key( k );
  527. }