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.

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