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.

689 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. 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. DMESSAGE( "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. if ( mapping.translate( &me ) )
  353. midi_output_event( _port, &me, 1 + e->note_duration() );
  354. }
  355. else
  356. if ( me.is_note_off() )
  357. {
  358. if ( mapping.translate( &me ) )
  359. midi_output_event( _port, &me, 0 );
  360. }
  361. else
  362. /* any other event type */
  363. midi_output_event( _port, &me );
  364. }
  365. }
  366. // ran out of events, but there's still some loop left to play.
  367. offset += d->length;
  368. goto try_again;
  369. DMESSAGE( "out of events, resetting to satisfy loop" );
  370. done: ;
  371. }
  372. /* Import /track/ of /f/ as new pattern */
  373. pattern *
  374. pattern::import ( smf *f, int track )
  375. {
  376. if ( ! f->seek_track( track ) )
  377. return NULL;
  378. pattern *p = new pattern;
  379. p->lock();
  380. p->load( f );
  381. /* file could have any notes in it... Use Chromatic scale to
  382. ensure all are visible */
  383. p->mapping.open( Mapping::SCALE, "Chromatic" );
  384. p->unlock();
  385. p->fit();
  386. return p;
  387. }
  388. /** fill pattern from current track of /f/ */
  389. void
  390. pattern::load ( smf *f )
  391. {
  392. lock();
  393. f->read_pattern_info( this );
  394. tick_t len;
  395. list <midievent> *e = f->read_track_events( &len );
  396. /* set channel to channel of first event... */
  397. if ( e->size() )
  398. _channel = e->front().channel();
  399. /* copy events into pattern */
  400. _rw->events = *e;
  401. delete e;
  402. if ( len )
  403. _rw->length = len;
  404. unlock();
  405. // print();
  406. }
  407. /** save (export) pattern to file /name/ */
  408. void
  409. pattern::save ( const char *name ) const
  410. {
  411. smf f;
  412. /* open for writing */
  413. f.open( name, smf::WRITE );
  414. /* writing SMF 0 track */
  415. f.write_header( 0 );
  416. f.open_track( _name, _number );
  417. Grid::dump( &f, _channel );
  418. f.close_track( length() );
  419. }
  420. /** dump pattern as a track in an already open MIDI file */
  421. void
  422. pattern::dump ( smf *f ) const
  423. {
  424. f->open_track( _name, _number );
  425. f->write_pattern_info( this );
  426. Grid::dump( f, _channel );
  427. f->close_track( length() );
  428. }
  429. void
  430. pattern::randomize_row ( int y, int feel, float probability )
  431. {
  432. lock();
  433. int l = PPQN * 4 / _note;
  434. int bx = ts_to_x( _rw->length - l );
  435. float *p = (float *)alloca( feel * sizeof( float ) );
  436. float prob = probability;
  437. for ( int i = 0; i < feel; i++ )
  438. {
  439. p[i] = prob;
  440. // reduce probability as we move away from center
  441. prob *= 0.5;
  442. }
  443. for ( int x = 0; x < bx; x++ )
  444. {
  445. float r = ((float)rand()) / RAND_MAX;
  446. if ( p[ x % feel ] + r >= 1 )
  447. put( x, y, l );
  448. }
  449. unlock();
  450. }
  451. /*************/
  452. /* Recording */
  453. /*************/
  454. void
  455. pattern::record ( int mode )
  456. {
  457. _recording = true;
  458. pattern::_pattern_recording = _number;
  459. }
  460. void
  461. pattern::record_stop ( void )
  462. {
  463. if ( ! _recording )
  464. return;
  465. _recording = false;
  466. if ( config.record_mode == NEW )
  467. trim();
  468. pattern::_recorded_events.clear();
  469. }
  470. /*******************************/
  471. /* Pattern specific accessors. */
  472. /*******************************/
  473. int
  474. pattern::port ( void ) const
  475. {
  476. return _port;
  477. }
  478. void
  479. pattern::port ( int p )
  480. {
  481. _port = p;
  482. }
  483. int
  484. pattern::channel ( void ) const
  485. {
  486. return _channel;
  487. }
  488. void
  489. pattern::channel ( int c )
  490. {
  491. _channel = c;
  492. }
  493. int
  494. pattern::note ( void ) const
  495. {
  496. return _note;
  497. }
  498. void
  499. pattern::note ( int n )
  500. {
  501. _note = n;
  502. }
  503. int
  504. pattern::ppqn ( void ) const
  505. {
  506. return _ppqn;
  507. }
  508. void
  509. pattern::ppqn ( int n )
  510. {
  511. _ppqn = n;
  512. }
  513. int
  514. pattern::key ( void ) const
  515. {
  516. return mapping.key();
  517. }
  518. void
  519. pattern::key ( int k )
  520. {
  521. mapping.key( k );
  522. }