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.

851 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 "grid.H"
  19. #include "common.h"
  20. #include "canvas.H"
  21. #include "non.H"
  22. #include "smf.H"
  23. #define RD ( _locked ? ASSERTION( "invalid read" ) : _rd )
  24. #define WR ( ! locked ? ASSERTION( "invalid write" ) : _wr )
  25. Grid::Grid ( void )
  26. {
  27. _name = NULL;
  28. _notes = NULL;
  29. _number = 0;
  30. _height = 0;
  31. _rd = new data;
  32. _rw = NULL;
  33. // we need to initialize it here.
  34. data *d = (data *)_rd;
  35. _mode = 0;
  36. _locked = 0;
  37. d->length = 0;
  38. _bpb = 4;
  39. _ppqn = 1;
  40. viewport.h = 32;
  41. viewport.w = 32;
  42. viewport.x = 0;
  43. viewport.y = 0;
  44. _start = _end = _index = 0;
  45. }
  46. Grid::~Grid ( void )
  47. {
  48. DEBUG( "deleting grid" );
  49. if ( _name )
  50. free( _name );
  51. if ( _notes )
  52. free( _notes );
  53. if ( _rw )
  54. delete _rw;
  55. if ( _rd )
  56. delete _rd;
  57. }
  58. /* copy constructor */
  59. Grid::Grid ( const Grid &rhs )
  60. {
  61. _rd = new data( *rhs._rd );
  62. _rw = NULL;
  63. _name = rhs._name ? strdup( rhs._name ) : NULL;
  64. _notes = rhs._notes ? strdup( rhs._notes ) : NULL;
  65. _number = rhs._number;
  66. _height = rhs._height;
  67. _draw_shape = rhs._draw_shape;
  68. _mode = 0;
  69. _locked = 0;
  70. _playing = false;
  71. _index = 0;
  72. _start = 0;
  73. _end = 0;
  74. _bpb = rhs._bpb;
  75. _ppqn = rhs._ppqn;
  76. viewport = rhs.viewport;
  77. }
  78. #if 0
  79. const data *
  80. Grid::rd ( void )
  81. {
  82. if ( _locked )
  83. ASSERTION( "invalid read" );
  84. return _rd;
  85. }
  86. data *
  87. Grid::wr ( void )
  88. {
  89. if ( ! _locked )
  90. ASSERTION( "invalid write" );
  91. return _rw;
  92. }
  93. #endif
  94. void
  95. Grid::lock ( void )
  96. {
  97. if ( ! _locked++ )
  98. _rw = new data( *_rd );
  99. }
  100. void
  101. Grid::unlock ( void )
  102. {
  103. if ( 0 == --_locked )
  104. {
  105. _history.push_back( const_cast<data *>( _rd ) );
  106. if ( _history.size() > MAX_UNDO + 1 )
  107. {
  108. data *d = _history.front();
  109. if ( d == _rw || d == _rd )
  110. ASSERTION( "something bad has happend." );
  111. delete d;
  112. _history.pop_front();
  113. }
  114. // swap the copy back in (atomically).
  115. _rd = (const data *)_rw;
  116. _rw = NULL;
  117. signal_events_change();
  118. }
  119. }
  120. event *
  121. Grid::_event ( int x, int y, bool write ) const
  122. {
  123. const data *d = const_cast< data * >(_rd);
  124. const event_list *r = write ? &_rw->events : &d->events;
  125. if ( r->empty() || x_to_ts( x ) > _rd->length )
  126. return NULL;
  127. int note = y_to_note( y );
  128. tick_t xt = x_to_ts( x );
  129. for ( event *e = r->first(); e; e = e->next() )
  130. {
  131. if ( ! e->is_note_on() )
  132. continue;
  133. if ( e->note() != note )
  134. continue;
  135. unsigned long ts = e->timestamp();
  136. unsigned long l = 0;
  137. if ( e->linked() )
  138. l = e->link()->timestamp() - ts;
  139. else
  140. WARNING( "found unlinked event... event list is corrupt." );
  141. if ( xt >= ts && xt < ts + l )
  142. // this is a little nasty
  143. return const_cast<event *>(e);
  144. }
  145. return NULL;
  146. }
  147. bool
  148. Grid::_delete ( int x, int y )
  149. {
  150. event *e = _event ( x, y, true );
  151. if ( e )
  152. {
  153. if ( e->linked() )
  154. _rw->events.remove( e->link() );
  155. _rw->events.remove( e );
  156. return true;
  157. }
  158. return false;
  159. }
  160. bool
  161. Grid::_get ( struct dash *d, int x, int y ) const
  162. {
  163. event *e = _event ( x, y, false );
  164. if ( e )
  165. {
  166. tick_t ts = e->timestamp();
  167. tick_t l = 0;
  168. if ( e->linked() )
  169. l = e->link()->timestamp() - ts;
  170. else
  171. WARNING( "Found unlinked note on" );
  172. d->timestamp = ts_to_x( ts );
  173. d->length = ts_to_x( l );
  174. d->color = e->note_velocity();
  175. return true;
  176. }
  177. return false;
  178. }
  179. void
  180. Grid::clear ( void )
  181. {
  182. lock();
  183. _rw->events.clear();
  184. unlock();
  185. }
  186. int
  187. Grid::get ( struct dash *d, int x, int y ) const
  188. {
  189. return _get( d, x, y );
  190. }
  191. void
  192. Grid::del ( int x, int y )
  193. {
  194. lock();
  195. _delete( x, y );
  196. unlock();
  197. }
  198. int
  199. Grid::next_note_x ( int x ) const
  200. {
  201. for ( const event *e = _rd->events.first(); e; e = e->next() )
  202. if ( e->is_note_on() && (ts_to_x( e->timestamp() ) > (uint)x ) )
  203. return ts_to_x( e->timestamp() );
  204. return 0;
  205. }
  206. int
  207. Grid::prev_note_x ( int x ) const
  208. {
  209. for ( const event *e = _rd->events.last(); e; e = e->prev() )
  210. if ( e->is_note_on() && (ts_to_x( e->timestamp() ) < (uint)x) )
  211. return ts_to_x( e->timestamp() );
  212. return 0;
  213. }
  214. void
  215. Grid::_fix_length ( void )
  216. {
  217. tick_t beats = (_rw->length / PPQN);
  218. tick_t rem = _rw->length % PPQN;
  219. _rw->length = (rem ? (beats + 1) : beats) * PPQN;
  220. }
  221. /** Trim the length of the grid to the last event */
  222. void
  223. Grid::trim ( void )
  224. {
  225. lock();
  226. event *e = _rw->events.last();
  227. if ( e )
  228. {
  229. tick_t ts = e->timestamp();
  230. _rw->length = ts;
  231. _fix_length();
  232. }
  233. unlock();
  234. }
  235. void
  236. Grid::fit ( void )
  237. {
  238. int hi, lo;
  239. _rd->events.hi_lo_note( &hi, &lo );
  240. viewport.h = abs( hi - lo ) + 1;
  241. viewport.y = note_to_y( hi );
  242. }
  243. /** Expand the length of the grid to the last event */
  244. void
  245. Grid::expand ( void )
  246. {
  247. lock();
  248. event *e = _rw->events.last();
  249. if ( e )
  250. {
  251. tick_t ts = e->timestamp();
  252. _rw->length = ts > _rw->length ? ts : _rw->length;
  253. _fix_length();
  254. }
  255. unlock();
  256. }
  257. void
  258. Grid::put ( int x, int y, tick_t l )
  259. {
  260. int xl = ts_to_x( l );
  261. tick_t ts = x_to_ts( x );
  262. event *on = new event;
  263. event *off = new event;
  264. struct dash d;
  265. // Don't allow overlap (Why not?)
  266. if ( get( &d, x, y ) || get( &d, x + xl - 1, y ) )
  267. return;
  268. DEBUG( "put %d,%d", x, y );
  269. lock();
  270. int note = y_to_note( y );
  271. on->status( event::NOTE_ON );
  272. on->note( note );
  273. on->timestamp( ts );
  274. on->note_velocity( 64 );
  275. on->link( off );
  276. off->status( event::NOTE_OFF );
  277. off->note( note );
  278. off->timestamp( ts + l );
  279. off->note_velocity( 64 );
  280. off->link( on );
  281. _rw->events.insert( on );
  282. _rw->events.insert( off );
  283. expand();
  284. unlock();
  285. }
  286. // void
  287. // pattern::move ( int x, int y, int nx )
  288. // {
  289. // event *e = _event( x, y, false );
  290. // if ( e )
  291. // e->timestamp( nx );
  292. // }
  293. void
  294. Grid::move ( int x, int y, int nx, int ny )
  295. {
  296. lock();
  297. event *e = _event( x, y, true );
  298. if ( e )
  299. {
  300. DEBUG( "moving note" );
  301. event *on = e,
  302. *off = e->link();
  303. _rw->events.unlink( on );
  304. _rw->events.unlink( off );
  305. on->note( y_to_note( ny ) );
  306. tick_t l = on->note_duration();
  307. on->timestamp( x_to_ts( ny ) );
  308. on->note_duration( l );
  309. _rw->events.insert( off );
  310. _rw->events.insert( on );
  311. }
  312. unlock();
  313. }
  314. void
  315. Grid::adj_velocity ( int x, int y, int n )
  316. {
  317. lock();
  318. event *e = _event( x, y, true );
  319. if ( e )
  320. {
  321. DEBUG( "adjusting velocity" );
  322. {
  323. int v = e->note_velocity();
  324. v += n;
  325. if ( v > 127 )
  326. v = 127;
  327. e->note_velocity( v > 0 ? v : 1 );
  328. }
  329. }
  330. unlock();
  331. }
  332. void
  333. Grid::adj_duration ( int x, int y, int l )
  334. {
  335. lock();
  336. event *e = _event( x, y, true );
  337. if ( e )
  338. {
  339. DEBUG( "adjusting duration" );
  340. {
  341. int v = ts_to_x( e->note_duration() );
  342. v += l;
  343. e->note_duration( x_to_ts( v > 0 ? v : 1 ) );
  344. _rw->events.sort( e->link() );
  345. }
  346. }
  347. unlock();
  348. }
  349. void
  350. Grid::select ( int x, int y, bool b )
  351. {
  352. lock();
  353. event *e = _event( x, y, true );
  354. if ( e )
  355. if ( b )
  356. e->select();
  357. else
  358. e->deselect();
  359. unlock();
  360. }
  361. /** insert /l/ ticks of time after /x/ */
  362. void
  363. Grid::insert_time ( int l, int r )
  364. {
  365. tick_t start = x_to_ts( l );
  366. tick_t end = x_to_ts( r );
  367. lock();
  368. _rw->events.insert_time( start, end - start );
  369. expand();
  370. unlock();
  371. }
  372. /** select all events in range (notes straddling the border will also be selected */
  373. void
  374. Grid::select ( int l, int r )
  375. {
  376. tick_t start = x_to_ts( l );
  377. tick_t end = x_to_ts( r );
  378. lock();
  379. _rw->events.select( start, end );
  380. unlock();
  381. }
  382. /** delete events from /x/ to /l/, compressing time. */
  383. void
  384. Grid::delete_time ( int l, int r )
  385. {
  386. tick_t start = x_to_ts( l );
  387. tick_t end = x_to_ts( r );
  388. lock();
  389. _rw->events.delete_time( start, end );
  390. unlock();
  391. }
  392. void
  393. Grid::select_none ( void )
  394. {
  395. lock();
  396. _rw->events.select_none();
  397. unlock();
  398. }
  399. void
  400. Grid::delete_selected ( void )
  401. {
  402. lock();
  403. _rw->events.remove_selected();
  404. unlock();
  405. }
  406. void
  407. Grid::move_selected ( int l )
  408. {
  409. long o = x_to_ts( abs( l ) );
  410. if ( l < 0 )
  411. o = 0 - o;
  412. lock();
  413. // MESSAGE( "moving by %ld", o );
  414. _rw->events.move_selected( o );
  415. unlock();
  416. }
  417. void
  418. Grid::crop ( int l, int r )
  419. {
  420. lock();
  421. if ( (uint)r < ts_to_x( _rw->length ) )
  422. delete_time( r, ts_to_x( _rw->length ) );
  423. if ( l > 0 )
  424. delete_time( 0, l );
  425. trim();
  426. unlock();
  427. }
  428. void
  429. Grid::_relink ( void )
  430. {
  431. _rw->events.relink();
  432. }
  433. void
  434. Grid::record_event ( event *e )
  435. {
  436. WARNING( "unimplemented" );
  437. /* lock(); */
  438. /* _rw->events.push_back( *e ); */
  439. /* _rw->events.sort(); */
  440. /* unlock(); */
  441. }
  442. /* Dump the event list -- used by pattern / phrase dumppers */
  443. void
  444. Grid::dump ( smf *f, int channel, bool translate ) const
  445. {
  446. data *d = const_cast<data *>(_rd);
  447. midievent me;
  448. for ( event *e = d->events.first(); e; e = e->next() )
  449. {
  450. // e->print();
  451. me = *e;
  452. me.channel( channel );
  453. /* if ( me.is_note_on() || me.is_note_off() ) */
  454. /* if ( translate ) */
  455. /* d->mapping.translate( &me ); */
  456. f->write_event( &me );
  457. }
  458. }
  459. void
  460. Grid::print ( void ) const
  461. {
  462. data *d = const_cast<data *>(_rd);
  463. for ( event *e = d->events.first(); e; e = e->next() )
  464. e->print();
  465. }
  466. void
  467. Grid::draw ( Canvas *c, int bx, int by, int bw, int bh )
  468. {
  469. c->clear();
  470. tick_t start = x_to_ts( bx );
  471. tick_t end = x_to_ts( bx + bw );
  472. data *d = const_cast< data *>( _rd );
  473. for ( event *e = d->events.first(); e; e = e->next() )
  474. {
  475. if ( ! e->is_note_on() )
  476. continue;
  477. tick_t ts = e->timestamp();
  478. if ( ! e->link() )
  479. ASSERTION( "wtf. note is not linked!" );
  480. tick_t tse = e->link()->timestamp();
  481. // if ( ts >= start && ts <= end )
  482. if ( tse >= start && ts <= end )
  483. c->draw_dash( ts_to_x( ts ), note_to_y( e->note() ), ts_to_x( tse - ts ),
  484. _draw_shape, e->note_velocity(), e->selected() );
  485. }
  486. c->flip();
  487. }
  488. /*******************************************/
  489. /* Generic accessors -- boy C++ is verbose */
  490. /*******************************************/
  491. /** Returns the index (playhead) for this grid */
  492. tick_t
  493. Grid::index ( void ) const
  494. {
  495. /* FIXME: considering the type of tick_t, we really need some kind
  496. of locking here to insure that this thread doesn't read _index
  497. while the RT thread is writing it. */
  498. return _index;
  499. }
  500. bool
  501. Grid::playing ( void ) const
  502. {
  503. return _playing;
  504. }
  505. int
  506. Grid::height ( void ) const
  507. {
  508. return _height;
  509. }
  510. void
  511. Grid::height ( int h )
  512. {
  513. _height = h;
  514. }
  515. tick_t
  516. Grid::length ( void ) const
  517. {
  518. return _rd->length;
  519. }
  520. void
  521. Grid::length ( tick_t l )
  522. {
  523. lock();
  524. _rw->length = l;
  525. unlock();
  526. }
  527. int
  528. Grid::bars ( void ) const
  529. {
  530. return ts_to_x( _rd->length ) / (_ppqn * _bpb);
  531. }
  532. int
  533. Grid::beats ( void ) const
  534. {
  535. return ts_to_x( _rd->length ) / _ppqn;
  536. }
  537. int
  538. Grid::division ( void ) const
  539. {
  540. return _bpb * _ppqn;
  541. }
  542. int
  543. Grid::subdivision ( void ) const
  544. {
  545. return _ppqn;
  546. }
  547. int
  548. Grid::ppqn ( void ) const
  549. {
  550. return _ppqn;
  551. }
  552. /** set grid resolution to /n/, where 0 is 1/4 note, 1 is 1/8 note 2 is 1/16 note, etc. */
  553. void
  554. Grid::resolution ( unsigned int n )
  555. {
  556. if ( n < 4 )
  557. ASSERTION( "bad resolution: %d", n );
  558. _ppqn = n / 4;
  559. DEBUG( "%d setting resolution to %d", n, _ppqn );
  560. signal_events_change();
  561. }
  562. int
  563. Grid::resolution ( void ) const
  564. {
  565. return _ppqn * 4;
  566. }
  567. int
  568. Grid::number ( void ) const
  569. {
  570. return _number;
  571. }
  572. void
  573. Grid::name ( char *s )
  574. {
  575. if ( _name ) free ( _name );
  576. _name = s;
  577. }
  578. const char *
  579. Grid::name ( void ) const
  580. {
  581. return _name;
  582. }
  583. void
  584. Grid::notes ( char *s )
  585. {
  586. if ( _notes ) free ( _notes );
  587. _notes = s;
  588. }
  589. char *
  590. Grid::notes ( void ) const
  591. {
  592. return _notes;
  593. }
  594. void
  595. Grid::mode ( int m )
  596. {
  597. _mode = m;
  598. signal_settings_change();
  599. }
  600. int
  601. Grid::mode ( void ) const
  602. {
  603. return _mode;
  604. }
  605. int
  606. Grid::draw_shape ( void ) const
  607. {
  608. return _draw_shape;
  609. }
  610. /** return a pointer to a copy of grid's event list in raw form */
  611. event_list *
  612. Grid::events ( void ) const
  613. {
  614. data * d = const_cast< data * >( _rd );
  615. return new event_list( d->events );
  616. }
  617. /** replace event list with a copy of /el/ */
  618. void
  619. Grid::events ( const event_list * el )
  620. {
  621. lock();
  622. _rw->events = *el;
  623. unlock();
  624. }