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.

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