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.

263 lines
7.3KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 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. /**********/
  19. /* Engine */
  20. /**********/
  21. #include "../Audio_Region.H"
  22. #include "Audio_File.H"
  23. #include "dsp.h"
  24. #include "util/Thread.H"
  25. /** Apply a (portion of) fade from /start/ to /end/ assuming a
  26. * buffer size of /nframes/. /start/ and /end/ are relative to the
  27. * given buffer, and /start/ may be negative. */
  28. void
  29. Audio_Region::Fade::apply ( sample_t *buf, Audio_Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const
  30. {
  31. // printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end );
  32. if ( ! nframes )
  33. return;
  34. const nframes_t i = start > 0 ? start : 0;
  35. const nframes_t e = end > nframes ? nframes : end;
  36. assert( i < nframes );
  37. const float inc = increment();
  38. float fi = ( i - start ) / (float)length;
  39. // buf += i;
  40. buf = &buf[ i ];
  41. nframes_t n = e - i;
  42. assert( i + n <= nframes );
  43. if ( dir == Fade::Out )
  44. {
  45. fi = 1.0f - fi;
  46. for ( ; n--; fi -= inc )
  47. *(buf++) *= gain( fi );
  48. }
  49. else
  50. for ( ; n--; fi += inc )
  51. *(buf++) *= gain( fi );
  52. }
  53. /** read the overlapping part of /channel/ at /pos/ for /nframes/ of
  54. this region into /buf/, where /pos/ is in timeline frames */
  55. /* this runs in the diskstream thread. */
  56. /* FIXME: it is far more efficient to read all the channels from a
  57. multichannel source at once... But how should we handle the case of a
  58. mismatch between the number of channels in this region's source and
  59. the number of channels on the track/buffer this data is being read
  60. for? Would it not be better to simply buffer and deinterlace the
  61. frames in the Audio_File class instead, so that sequential requests
  62. for different channels at the same position avoid hitting the disk
  63. again? */
  64. nframes_t
  65. Audio_Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
  66. {
  67. THREAD_ASSERT( Playback );
  68. const Range r = _range;
  69. /* do nothing if we aren't covered by this frame range */
  70. if ( pos > r.start + r.length || pos + nframes < r.start )
  71. return 0;
  72. /* calculate offsets into file and sample buffer */
  73. nframes_t sofs, ofs, cnt;
  74. cnt = nframes;
  75. if ( pos < r.start )
  76. {
  77. sofs = 0;
  78. ofs = r.start - pos;
  79. cnt -= ofs;
  80. }
  81. else
  82. {
  83. ofs = 0;
  84. sofs = pos - r.start;
  85. }
  86. if ( ofs >= nframes )
  87. return 0;
  88. // const nframes_t start = ofs + r.start + sofs;
  89. const nframes_t start = r.offset + sofs;
  90. const nframes_t len = min( cnt, nframes - ofs );
  91. if ( len == 0 )
  92. return 0;
  93. /* now that we know how much and where to read, get on with it */
  94. // printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
  95. if ( _loop )
  96. {
  97. nframes_t lofs = sofs % _loop;
  98. nframes_t lstart = r.offset + lofs;
  99. if ( lofs + len > _loop )
  100. {
  101. /* this buffer covers a loop bounary */
  102. /* read the first part */
  103. cnt = _clip->read( buf + ofs, channel, lstart, len - ( ( lofs + len ) - _loop ) );
  104. /* read the second part */
  105. cnt += _clip->read( buf + ofs + cnt, channel, lstart + cnt, len - cnt );
  106. /* TODO: declick/crossfade transition? */
  107. assert( cnt == len );
  108. }
  109. else
  110. cnt = _clip->read( buf + ofs, channel, lstart, len );
  111. }
  112. else
  113. cnt = _clip->read( buf + ofs, channel, start, len );
  114. /* apply gain */
  115. buffer_apply_gain( buf + ofs, cnt, _scale );
  116. /* perform declicking if necessary */
  117. /* FIXME: keep the declick defults someplace else */
  118. Fade declick;
  119. declick.length = 256;
  120. declick.type = Fade::Linear;
  121. {
  122. Fade fade;
  123. fade = declick < _fade_in ? _fade_in : declick;
  124. /* do fade in if necessary */
  125. if ( sofs < fade.length )
  126. {
  127. const long d = 0 - sofs;
  128. assert( cnt <= nframes );
  129. fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt );
  130. }
  131. fade = declick < _fade_out ? _fade_out : declick;
  132. /* do fade out if necessary */
  133. // if ( start + cnt + fade.length > r.end )
  134. if ( start + fade.length > ( r.offset + r.length ) )
  135. {
  136. const nframes_t d = ( r.offset + r.length ) - start;
  137. assert( cnt <= nframes );
  138. fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
  139. }
  140. }
  141. // printf( "read %lu frames\n", cnt );
  142. return cnt;
  143. }
  144. /** prepare for capturing */
  145. void
  146. Audio_Region::prepare ( void )
  147. {
  148. log_start();
  149. }
  150. /** write /nframes/ from /buf/ to source. /buf/ is interleaved and
  151. must match the channel layout of the write source! */
  152. nframes_t
  153. Audio_Region::write ( nframes_t nframes )
  154. {
  155. THREAD_ASSERT( Capture );
  156. _range.length += nframes;
  157. /* FIXME: too much? */
  158. // _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), 10/* FIXME: guess */, h() );
  159. if ( 0 == ( timeline->ts_to_x( _range.length ) % 20 ) )
  160. {
  161. nframes_t oldl = _clip->length();
  162. /* get the new size. Remember, this is a read-only handle on the source--not the same
  163. one being written to */
  164. _clip->close();
  165. _clip->open();
  166. int W = timeline->ts_to_x( _clip->length() - oldl );
  167. /* why - 1? */
  168. if ( W )
  169. {
  170. ++W;
  171. Fl::lock();
  172. sequence()->damage( FL_DAMAGE_ALL, x() + w() - W, y(), W, h() );
  173. // Fl::awake();
  174. Fl::unlock();
  175. }
  176. }
  177. return nframes;
  178. }
  179. /** finalize region capture. Assumes that this *is* a captured region
  180. and that no other regions refer to the same source */
  181. bool
  182. Audio_Region::finalize ( nframes_t frame )
  183. {
  184. THREAD_ASSERT( Capture );
  185. DMESSAGE( "finalizing capture region" );
  186. _range.length = frame - _range.start;
  187. log_end();
  188. _clip->close();
  189. _clip->open();
  190. /* FIXME: should we attempt to truncate the file? */
  191. Fl::lock();
  192. redraw();
  193. Fl::awake();
  194. Fl::unlock();
  195. return true;
  196. }