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.

825 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. DEBUG( "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 )
  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. DEBUG( "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. DEBUG( "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. DEBUG( "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. DEBUG( "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::select ( int x, int y, bool b )
  332. {
  333. lock();
  334. event *e = _event( x, y, true );
  335. if ( e )
  336. if ( b )
  337. e->select();
  338. else
  339. e->deselect();
  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. /** delete events from /x/ to /l/, compressing time. */
  364. void
  365. Grid::delete_time ( int l, int r )
  366. {
  367. tick_t start = x_to_ts( l );
  368. tick_t end = x_to_ts( r );
  369. lock();
  370. _rw->events.delete_time( start, end );
  371. unlock();
  372. }
  373. void
  374. Grid::select_none ( void )
  375. {
  376. lock();
  377. _rw->events.select_none();
  378. unlock();
  379. }
  380. void
  381. Grid::delete_selected ( void )
  382. {
  383. lock();
  384. _rw->events.remove_selected();
  385. unlock();
  386. }
  387. void
  388. Grid::move_selected ( int l )
  389. {
  390. long o = x_to_ts( abs( l ) );
  391. if ( l < 0 )
  392. o = 0 - o;
  393. lock();
  394. // MESSAGE( "moving by %ld", o );
  395. _rw->events.move_selected( o );
  396. unlock();
  397. }
  398. void
  399. Grid::crop ( int l, int r )
  400. {
  401. lock();
  402. if ( (uint)r < ts_to_x( _rw->length ) )
  403. delete_time( r, ts_to_x( _rw->length ) );
  404. if ( l > 0 )
  405. delete_time( 0, l );
  406. trim();
  407. unlock();
  408. }
  409. void
  410. Grid::_relink ( void )
  411. {
  412. _rw->events.relink();
  413. }
  414. void
  415. Grid::record_event ( event *e )
  416. {
  417. WARNING( "unimplemented" );
  418. /* lock(); */
  419. /* _rw->events.push_back( *e ); */
  420. /* _rw->events.sort(); */
  421. /* unlock(); */
  422. }
  423. /* Dump the event list -- used by pattern / phrase dumppers */
  424. void
  425. Grid::dump ( smf *f, int channel, bool translate ) const
  426. {
  427. data *d = const_cast<data *>(_rd);
  428. midievent me;
  429. for ( event *e = d->events.first(); e; e = e->next() )
  430. {
  431. // e->print();
  432. me = *e;
  433. me.channel( channel );
  434. /* if ( me.is_note_on() || me.is_note_off() ) */
  435. /* if ( translate ) */
  436. /* d->mapping.translate( &me ); */
  437. f->write_event( &me );
  438. }
  439. }
  440. void
  441. Grid::print ( void ) const
  442. {
  443. data *d = const_cast<data *>(_rd);
  444. for ( event *e = d->events.first(); e; e = e->next() )
  445. e->print();
  446. }
  447. void
  448. Grid::draw ( Canvas *c, int bx, int by, int bw, int bh )
  449. {
  450. c->clear();
  451. tick_t start = x_to_ts( bx );
  452. tick_t end = x_to_ts( bx + bw );
  453. data *d = const_cast< data *>( _rd );
  454. for ( event *e = d->events.first(); e; e = e->next() )
  455. {
  456. if ( ! e->is_note_on() )
  457. continue;
  458. tick_t ts = e->timestamp();
  459. ASSERT( e->link(), "found a non-linked note" );
  460. tick_t tse = e->link()->timestamp();
  461. // if ( ts >= start && ts <= end )
  462. if ( tse >= start && ts <= end )
  463. c->draw_dash( ts_to_x( ts ), note_to_y( e->note() ), ts_to_x( tse - ts ),
  464. _draw_shape, e->note_velocity(), e->selected() );
  465. }
  466. c->flip();
  467. }
  468. /*******************************************/
  469. /* Generic accessors -- boy C++ is verbose */
  470. /*******************************************/
  471. /** Returns the index (playhead) for this grid */
  472. tick_t
  473. Grid::index ( void ) const
  474. {
  475. /* FIXME: considering the type of tick_t, we really need some kind
  476. of locking here to insure that this thread doesn't read _index
  477. while the RT thread is writing it. */
  478. return _index;
  479. }
  480. bool
  481. Grid::playing ( void ) const
  482. {
  483. return _playing;
  484. }
  485. int
  486. Grid::height ( void ) const
  487. {
  488. return _height;
  489. }
  490. void
  491. Grid::height ( int h )
  492. {
  493. _height = h;
  494. }
  495. tick_t
  496. Grid::length ( void ) const
  497. {
  498. return _rd->length;
  499. }
  500. void
  501. Grid::length ( tick_t l )
  502. {
  503. lock();
  504. _rw->length = l;
  505. unlock();
  506. }
  507. int
  508. Grid::bars ( void ) const
  509. {
  510. return ts_to_x( _rd->length ) / (_ppqn * _bpb);
  511. }
  512. int
  513. Grid::beats ( void ) const
  514. {
  515. return ts_to_x( _rd->length ) / _ppqn;
  516. }
  517. int
  518. Grid::division ( void ) const
  519. {
  520. return _bpb * _ppqn;
  521. }
  522. int
  523. Grid::subdivision ( void ) const
  524. {
  525. return _ppqn;
  526. }
  527. int
  528. Grid::ppqn ( void ) const
  529. {
  530. return _ppqn;
  531. }
  532. /** set grid resolution to /n/, where 0 is 1/4 note, 1 is 1/8 note 2 is 1/16 note, etc. */
  533. void
  534. Grid::resolution ( unsigned int n )
  535. {
  536. if ( n < 4 )
  537. ASSERTION( "bad resolution: %d", n );
  538. _ppqn = n / 4;
  539. DEBUG( "%d setting resolution to %d", n, _ppqn );
  540. signal_events_change();
  541. }
  542. int
  543. Grid::resolution ( void ) const
  544. {
  545. return _ppqn * 4;
  546. }
  547. int
  548. Grid::number ( void ) const
  549. {
  550. return _number;
  551. }
  552. void
  553. Grid::name ( char *s )
  554. {
  555. if ( _name ) free ( _name );
  556. _name = s;
  557. }
  558. const char *
  559. Grid::name ( void ) const
  560. {
  561. return _name;
  562. }
  563. void
  564. Grid::notes ( char *s )
  565. {
  566. if ( _notes ) free ( _notes );
  567. _notes = s;
  568. }
  569. char *
  570. Grid::notes ( void ) const
  571. {
  572. return _notes;
  573. }
  574. void
  575. Grid::mode ( int m )
  576. {
  577. _mode = m;
  578. signal_settings_change();
  579. }
  580. int
  581. Grid::mode ( void ) const
  582. {
  583. return _mode;
  584. }
  585. int
  586. Grid::draw_shape ( void ) const
  587. {
  588. return _draw_shape;
  589. }
  590. /** return a pointer to a copy of grid's event list in raw form */
  591. event_list *
  592. Grid::events ( void ) const
  593. {
  594. data * d = const_cast< data * >( _rd );
  595. return new event_list( d->events );
  596. }
  597. /** replace event list with a copy of /el/ */
  598. void
  599. Grid::events ( const event_list * el )
  600. {
  601. lock();
  602. _rw->events = *el;
  603. unlock();
  604. }